ethtool命令设置接收端哈希功能,按照哈希结果将数据流分发到网卡的不同接收队列中。例如以下命令,指定ipv4的tcp数据流中参与哈希的字段(sdfn):
/ # ethtool --config-ntuple eth0 rx-flow-hash tcp4 sdfn
同样,使用ethtool命令查看设置结果如下:
/ # ethtool --show-ntuple eth0 rx-flow-hash tcp4
TCP over IPV4 flows use these fields for computing Hash flow key:
IP SA
IP DA
L4 bytes 0 & 1 [TCP/UDP src port]
L4 bytes 2 & 3 [TCP/UDP dst port]
以intel的IGB驱动为例,网卡在接收到ipv4 tcp的数据流时,根据指定的字段计算hash值(例如Toeplitz哈希算法),之后将hash值的后7位数值作为索引,到网卡的对照表中找到相应的队列索引,将数据包添加到此队列中。对照表由128个表项组成,每个表项对应一个队列索引。使用ethtool命令可查看和修改对照表,如下的对照表,128个表项平均分成4部分,分别对应着网卡的4个接收队列。即数据流计算得到的hash值如果为0-31,添加到接收队列0:如果为32-63,添加到队列1,依次类推。
/ # ethtool --show-rxfh-indir eth0
RX flow hash indirection table for m4/1 with 4 RX ring(s):
0: 0 0 0 0 0 0 0 0
8: 0 0 0 0 0 0 0 0
16: 0 0 0 0 0 0 0 0
24: 0 0 0 0 0 0 0 0
32: 1 1 1 1 1 1 1 1
40: 1 1 1 1 1 1 1 1
48: 1 1 1 1 1 1 1 1
56: 1 1 1 1 1 1 1 1
64: 2 2 2 2 2 2 2 2
72: 2 2 2 2 2 2 2 2
80: 2 2 2 2 2 2 2 2
88: 2 2 2 2 2 2 2 2
96: 3 3 3 3 3 3 3 3
104: 3 3 3 3 3 3 3 3
112: 3 3 3 3 3 3 3 3
120: 3 3 3 3 3 3 3 3
ethtool控制部分
配置命令rx-flow-hash由ethtool函数do_stxclass处理,最终由ioctl的命令ETHTOOL_SRXFH下发到内核的网卡驱动中。
static int do_srxclass(struct cmd_context *ctx)
{
if (ctx->argc == 3 && !strcmp(ctx->argp[0], "rx-flow-h