其时的高功用网卡一般都支撑对数据流的定向和过滤功用,可通过配备将指定的数据流定向到指定的设备队伍中,并且假设监听此队伍的中心正是工作处理此数据流的运用地址中心,将获得必定的功用优势。其他网卡的流过滤功用还可设定丢掉指定的流,可结束在硬件层面屏蔽不合法的访问等,而不需要处理器的干与。DPDK的示例flow_filtering演示第一种流定向功用。A8站长源码交易平台A8站长源码交易平台

该示例flow_filtering用于配备网卡的流过滤规则,结束匹配数据流的设备队伍定向功用。主函数是通用的初始化流程:包括EAL初始化,分配存储mbuf的内存池mempool,初始化接口init_port函数。DPDK的此示例运用一个port接口就可工作。随后调用结束流规则配备的函数generate_ipv4_flow。当然网卡的流过滤规则除了本示例的队伍定向功用,还有其它的功用,比如丢掉匹配的流等
<div class="table-box" font-size:14px;background-color:#ffffff;"="" style="word-wrap: break-word; font-family: "sans serif", tahoma, verdana, helvetica; font-size: 12px; white-space: normal; margin: 0px 0px 24px; padding: 0px; color: rgb(51, 51, 51);">
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | int main(int argc, char **argv) { ret = rte_eal_init(argc, argv); nr_ports = rte_eth_dev_count_avail(); if (nr_ports === 0 ) rte_exit(EXIT_FAILURE, ":: no Ethernet ports found "); port_id = 0; if (nr_ports != 1) { printf(":: warn: %d ports detected, but we use only one: port %u ", nr_ports, port_id); } mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", 4096, 128, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); if (mbuf_pool == NULL) rte_exit(EXIT_FAILURE, "Cannot init mbuf pool "); init_port(); flow = generate_ipv4_flow(port_id, selected_queue, SRC_IP, EMPTY_MASK, DEST_IP, FULL_MASK, &error); if (!flow) { printf("Flow can't be created %d message: %s ", error.type, error.message ? error.message : "(no stated reason)"); rte_exit(EXIT_FAILURE, "error in creating flow"); } main_loop(); return 0; } |
1 2 3 4 5 | static uint8_t selected_queue = 1; #define SRC_IP ((0<<24) + (0<<16) + (0<<8) + 0) /* src ip = 0.0.0.0 */ #define DEST_IP ((192<<24) + (168<<16) + (1<<8) + 1) /* dest ip = 192.168.1.1 */ #define FULL_MASK 0xffffffff /* full mask */ #define EMPTY_MASK 0x0 /* empty mask */ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | struct rte_flow *generate_ipv4_flow(uint16_t port_id, uint16_t rx_q, uint32_t src_ip, uint32_t src_mask, uint32_t dest_ip, uint32_t dest_mask, struct rte_flow_error *error) { struct rte_flow_attr attr; struct rte_flow_item pattern[MAX_PATTERN_NUM]; struct rte_flow_action action[MAX_ACTION_NUM]; struct rte_flow *flow = NULL; struct rte_flow_action_queue queue = { .index = rx_q }; struct rte_flow_item_ipv4 ip_spec; struct rte_flow_item_ipv4 ip_mask; /* set the rule attribute. in this case only ingress packets will be checked. */ memset(&attr, 0, sizeof(struct rte_flow_attr)); attr.ingress = 1; |
1 2 3 4 | /* create the action sequence. one action only, move packet to queue */ action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE; action[0].conf = &queue; action[1].type = RTE_FLOW_ACTION_TYPE_END; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /* set the first level of the pattern (ETH). since in this example we just want to get the ipv4 we set this level to allow all. */ pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH; /* * setting the second level of the pattern (IP). in this example this is the level we care about so we set it according to the parameters. */ memset(&ip_spec, 0, sizeof(struct rte_flow_item_ipv4)); memset(&ip_mask, 0, sizeof(struct rte_flow_item_ipv4)); ip_spec.hdr.dst_addr = htonl(dest_ip); ip_mask.hdr.dst_addr = dest_mask; ip_spec.hdr.src_addr = htonl(src_ip); ip_mask.hdr.src_addr = src_mask; pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4; pattern[1].spec = &ip_spec; pattern[1].mask = &ip_mask; /* the final level must be always type end */ pattern[2].type = RTE_FLOW_ITEM_TYPE_END; |
第四,以上流规则的参数都已设置结束。此处调用rte_flow_validate函数验证参数配备的是否正确。该函数结束位于文件lib/librte_ethdev/rte_flow.c中,其获取设备的流规则处理函数集rte_flow_ops,调用其间的validate函数。例如关于INTEL的IXGBE网卡驱动设备而言,validate函数指针指向ixgbe_flow_validate函数,其结束位于文件drivers/net/ixgbe/ixgbe_flow.c中。此函数也仅是查看定义的流规则参数网卡是否支撑,例如IXGBE网卡就不支撑MAC流辨认,以及查看参数中指定的队伍号是否超出设备支撑的最大队伍值等,但是并不确保通过validate查看的flow流规则参数必定能毕竟设置成功,由于网卡中存储流规则的内存或许已满。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | struct rte_flow *rte_flow_create(uint16_t port_id, const struct rte_flow_attr *attr, const struct rte_flow_item pattern[], const struct rte_flow_action actions[], struct rte_flow_error *error) { struct rte_eth_dev *dev = &rte_eth_devices[port_id]; struct rte_flow *flow; const struct rte_flow_ops *ops = rte_flow_ops_get(port_id, error); if (unlikely(!ops)) return NULL; if (likely(!!ops->create)) { flow = ops->create(dev, attr, pattern, actions, error); if (flow == NULL) flow_err(port_id, -rte_errno, error); return flow; } rte_flow_error_set(error, ENOSYS, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL, rte_strerror(ENOSYS)); return NULL; |
示例flow_filtering运用的首要是网卡的Flow Director功用。关于INTEL的网卡82599的IXGBE驱动而言,函数ixgbe_parse_fdir_filter_normal用来解析上层设置的流规则参数,首要由于在之前的匹配模型pattern中并未设置类型为RTE_FLOW_ITEM_TYPE_FUZZY的规则,signature_match不成立,此处运用RTE_FDIR_MODE_PERFECT类型匹配规则。其次由于匹配方式链中第一个指定的为RTE_FLOW_ITEM_TYPE_ETH类型,但是并没有指定相应的spec和mask,所以IXGBE驱动不做处理,跳到下一个pattern。终究的pattern类型设置的为RTE_FLOW_ITEM_TYPE_IPV4,即将之前设置的源和目的IP地址赋予规则的ixgbe_fdir.formatted结构的成员src_ip[0]和dst_ip[0],将掩码赋予mask.dst_ipv4_mask和mask.src_ipv4_mask变量。A8站长源码交易平台
1 2 3 4 5 6 7 8 | static int ixgbe_parse_fdir_act_attr(const struct rte_flow_attr *attr, const struct rte_flow_action actions[], struct ixgbe_fdir_rule *rule, struct rte_flow_error *error) { if (act->type == RTE_FLOW_ACTION_TYPE_QUEUE) { act_q = (const struct rte_flow_action_queue *)act->conf; rule->queue = act_q->index; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | static void main_loop(void) { while (!force_quit) { for (i = 0; i < nr_queues; i++) { nb_rx = rte_eth_rx_burst(port_id, i, mbufs, 32); if (nb_rx) { for (j = 0; j < nb_rx; j++) { struct rte_mbuf *m = mbufs[j]; eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *); print_ether_addr("src=", e_hdr->s_addr); print_ether_addr(" - dst=", e_hdr->d_addr); printf(" - queue=0x%x", (unsigned int)i); printf(" "); rte_pktmbuf_free(m); } } } } } |
终究,关于Linux而言,可运用ethtool东西配备以上的流规则。如下的指令依次为打开网卡设备的流规则功用;查看打开情况;配备目的IP地址为192.168.1.1的流定向到队伍1中,查看流规则列表;终究为删去流规则的指令。更多ethtool配备可查看其帮忙信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | / # ethtool --features eth0 ntuple on / # / # ethtool --show-features eth0 ntuple-filters: on / # / # ethtool --config-ntuple eth0 flow-type ip4 src-ip 0.0.0.0 m 0.0.0.0 dst-ip 192.168.1.1 m 255.255.255.255 action 1 Added rule with ID 7423 / # / # ethtool --show-ntuple eth0 4 RX rings available Total 1 rules Filter: 7423 Rule Type: Raw IPv4 Src IP addr: 0.0.0.0 mask: 255.255.255.255 Dest IP addr: 192.168.1.1 mask: 255.255.255.255 TOS: 0x0 mask: 0xff Protocol: 0 mask: 0xff L4 bytes: 0x0 mask: 0xffffffff Action: Direct to queue 1 / # / # ethtool --config-ntuple eth0 delete 7423 / # / # ethtool --show-ntuple eth0 4 RX rings available Total 0 rules / # |