4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/queue.h>
38 #include <sys/resource.h>
40 #include <rte_byteorder.h>
41 #include <rte_jhash.h>
42 #include <rte_malloc.h>
43 #include <rte_eth_tap.h>
45 #include <tap_autoconf.h>
46 #include <tap_tcmsgs.h>
49 #ifndef HAVE_TC_FLOWER
51 * For kernels < 4.2, this enum is not defined. Runtime checks will be made to
52 * avoid sending TC messages the kernel cannot understand.
59 TCA_FLOWER_KEY_ETH_DST, /* ETH_ALEN */
60 TCA_FLOWER_KEY_ETH_DST_MASK, /* ETH_ALEN */
61 TCA_FLOWER_KEY_ETH_SRC, /* ETH_ALEN */
62 TCA_FLOWER_KEY_ETH_SRC_MASK, /* ETH_ALEN */
63 TCA_FLOWER_KEY_ETH_TYPE, /* be16 */
64 TCA_FLOWER_KEY_IP_PROTO, /* u8 */
65 TCA_FLOWER_KEY_IPV4_SRC, /* be32 */
66 TCA_FLOWER_KEY_IPV4_SRC_MASK, /* be32 */
67 TCA_FLOWER_KEY_IPV4_DST, /* be32 */
68 TCA_FLOWER_KEY_IPV4_DST_MASK, /* be32 */
69 TCA_FLOWER_KEY_IPV6_SRC, /* struct in6_addr */
70 TCA_FLOWER_KEY_IPV6_SRC_MASK, /* struct in6_addr */
71 TCA_FLOWER_KEY_IPV6_DST, /* struct in6_addr */
72 TCA_FLOWER_KEY_IPV6_DST_MASK, /* struct in6_addr */
73 TCA_FLOWER_KEY_TCP_SRC, /* be16 */
74 TCA_FLOWER_KEY_TCP_DST, /* be16 */
75 TCA_FLOWER_KEY_UDP_SRC, /* be16 */
76 TCA_FLOWER_KEY_UDP_DST, /* be16 */
79 #ifndef HAVE_TC_VLAN_ID
81 /* TCA_FLOWER_FLAGS, */
82 TCA_FLOWER_KEY_VLAN_ID = TCA_FLOWER_KEY_UDP_DST + 2, /* be16 */
83 TCA_FLOWER_KEY_VLAN_PRIO, /* u8 */
84 TCA_FLOWER_KEY_VLAN_ETH_TYPE, /* be16 */
88 * For kernels < 4.2 BPF related enums may not be defined.
89 * Runtime checks will be carried out to gracefully report on TC messages that
90 * are rejected by the kernel. Rejection reasons may be due to:
91 * 1. enum is not defined
92 * 2. enum is defined but kernel is not configured to support BPF system calls,
93 * BPF classifications or BPF actions.
105 #ifndef HAVE_TC_BPF_FD
107 TCA_BPF_FD = TCA_BPF_OPS + 1,
111 #ifndef HAVE_TC_ACT_BPF
132 #ifndef HAVE_TC_ACT_BPF_FD
134 TCA_ACT_BPF_FD = TCA_ACT_BPF_OPS + 1,
139 /* RSS key management */
153 #define ISOLATE_HANDLE 1
156 LIST_ENTRY(rte_flow) next; /* Pointer to the next rte_flow structure */
157 struct rte_flow *remote_flow; /* associated remote flow */
158 int bpf_fd[SEC_MAX]; /* list of bfs fds per ELF section */
159 uint32_t key_idx; /* RSS rule key index into BPF map */
163 struct convert_data {
167 struct rte_flow *flow;
171 struct rte_flow_attr attr;
172 struct rte_flow_item items[2];
173 struct rte_flow_action actions[2];
182 struct tc_mirred mirred;
184 struct tc_skbedit skbedit;
188 struct tc_act_bpf bpf;
190 const char *annotation;
195 static int tap_flow_create_eth(const struct rte_flow_item *item, void *data);
196 static int tap_flow_create_vlan(const struct rte_flow_item *item, void *data);
197 static int tap_flow_create_ipv4(const struct rte_flow_item *item, void *data);
198 static int tap_flow_create_ipv6(const struct rte_flow_item *item, void *data);
199 static int tap_flow_create_udp(const struct rte_flow_item *item, void *data);
200 static int tap_flow_create_tcp(const struct rte_flow_item *item, void *data);
202 tap_flow_validate(struct rte_eth_dev *dev,
203 const struct rte_flow_attr *attr,
204 const struct rte_flow_item items[],
205 const struct rte_flow_action actions[],
206 struct rte_flow_error *error);
208 static struct rte_flow *
209 tap_flow_create(struct rte_eth_dev *dev,
210 const struct rte_flow_attr *attr,
211 const struct rte_flow_item items[],
212 const struct rte_flow_action actions[],
213 struct rte_flow_error *error);
216 tap_flow_destroy(struct rte_eth_dev *dev,
217 struct rte_flow *flow,
218 struct rte_flow_error *error);
221 tap_flow_isolate(struct rte_eth_dev *dev,
223 struct rte_flow_error *error);
225 static int bpf_rss_key(enum bpf_rss_key_e cmd, __u32 *key_idx);
226 static int rss_enable(struct pmd_internals *pmd,
227 const struct rte_flow_attr *attr,
228 struct rte_flow_error *error);
229 static int rss_add_actions(struct rte_flow *flow, struct pmd_internals *pmd,
230 const struct rte_flow_action_rss *rss,
231 struct rte_flow_error *error);
233 static const struct rte_flow_ops tap_flow_ops = {
234 .validate = tap_flow_validate,
235 .create = tap_flow_create,
236 .destroy = tap_flow_destroy,
237 .flush = tap_flow_flush,
238 .isolate = tap_flow_isolate,
241 /* Static initializer for items. */
243 (const enum rte_flow_item_type []){ \
244 __VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
247 /* Structure to generate a simple graph of layers supported by the NIC. */
248 struct tap_flow_items {
249 /* Bit-mask corresponding to what is supported for this item. */
251 const unsigned int mask_sz; /* Bit-mask size in bytes. */
253 * Bit-mask corresponding to the default mask, if none is provided
254 * along with the item.
256 const void *default_mask;
258 * Conversion function from rte_flow to netlink attributes.
261 * rte_flow item to convert.
263 * Internal structure to store the conversion.
266 * 0 on success, negative value otherwise.
268 int (*convert)(const struct rte_flow_item *item, void *data);
269 /** List of possible following items. */
270 const enum rte_flow_item_type *const items;
273 /* Graph of supported items and associated actions. */
274 static const struct tap_flow_items tap_flow_items[] = {
275 [RTE_FLOW_ITEM_TYPE_END] = {
276 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH),
278 [RTE_FLOW_ITEM_TYPE_ETH] = {
280 RTE_FLOW_ITEM_TYPE_VLAN,
281 RTE_FLOW_ITEM_TYPE_IPV4,
282 RTE_FLOW_ITEM_TYPE_IPV6),
283 .mask = &(const struct rte_flow_item_eth){
284 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
285 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
288 .mask_sz = sizeof(struct rte_flow_item_eth),
289 .default_mask = &rte_flow_item_eth_mask,
290 .convert = tap_flow_create_eth,
292 [RTE_FLOW_ITEM_TYPE_VLAN] = {
293 .items = ITEMS(RTE_FLOW_ITEM_TYPE_IPV4,
294 RTE_FLOW_ITEM_TYPE_IPV6),
295 .mask = &(const struct rte_flow_item_vlan){
297 /* DEI matching is not supported */
298 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
304 .mask_sz = sizeof(struct rte_flow_item_vlan),
305 .default_mask = &rte_flow_item_vlan_mask,
306 .convert = tap_flow_create_vlan,
308 [RTE_FLOW_ITEM_TYPE_IPV4] = {
309 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
310 RTE_FLOW_ITEM_TYPE_TCP),
311 .mask = &(const struct rte_flow_item_ipv4){
318 .mask_sz = sizeof(struct rte_flow_item_ipv4),
319 .default_mask = &rte_flow_item_ipv4_mask,
320 .convert = tap_flow_create_ipv4,
322 [RTE_FLOW_ITEM_TYPE_IPV6] = {
323 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
324 RTE_FLOW_ITEM_TYPE_TCP),
325 .mask = &(const struct rte_flow_item_ipv6){
328 "\xff\xff\xff\xff\xff\xff\xff\xff"
329 "\xff\xff\xff\xff\xff\xff\xff\xff",
332 "\xff\xff\xff\xff\xff\xff\xff\xff"
333 "\xff\xff\xff\xff\xff\xff\xff\xff",
338 .mask_sz = sizeof(struct rte_flow_item_ipv6),
339 .default_mask = &rte_flow_item_ipv6_mask,
340 .convert = tap_flow_create_ipv6,
342 [RTE_FLOW_ITEM_TYPE_UDP] = {
343 .mask = &(const struct rte_flow_item_udp){
349 .mask_sz = sizeof(struct rte_flow_item_udp),
350 .default_mask = &rte_flow_item_udp_mask,
351 .convert = tap_flow_create_udp,
353 [RTE_FLOW_ITEM_TYPE_TCP] = {
354 .mask = &(const struct rte_flow_item_tcp){
360 .mask_sz = sizeof(struct rte_flow_item_tcp),
361 .default_mask = &rte_flow_item_tcp_mask,
362 .convert = tap_flow_create_tcp,
367 * TC rules, by growing priority
369 * Remote netdevice Tap netdevice
370 * +-------------+-------------+ +-------------+-------------+
371 * | Ingress | Egress | | Ingress | Egress |
372 * |-------------|-------------| |-------------|-------------|
373 * | | \ / | | | REMOTE TX | prio 1
374 * | | \ / | | | \ / | prio 2
375 * | EXPLICIT | \ / | | EXPLICIT | \ / | .
376 * | | \ / | | | \ / | .
377 * | RULES | X | | RULES | X | .
378 * | . | / \ | | . | / \ | .
379 * | . | / \ | | . | / \ | .
380 * | . | / \ | | . | / \ | .
381 * | . | / \ | | . | / \ | .
383 * .... .... .... ....
385 * | . | \ / | | . | \ / | .
386 * | . | \ / | | . | \ / | .
387 * | | \ / | | | \ / |
388 * | LOCAL_MAC | \ / | | \ / | \ / | last prio - 5
389 * | PROMISC | X | | \ / | X | last prio - 4
390 * | ALLMULTI | / \ | | X | / \ | last prio - 3
391 * | BROADCAST | / \ | | / \ | / \ | last prio - 2
392 * | BROADCASTV6 | / \ | | / \ | / \ | last prio - 1
393 * | xx | / \ | | ISOLATE | / \ | last prio
394 * +-------------+-------------+ +-------------+-------------+
396 * The implicit flow rules are stored in a list in with mandatorily the last two
397 * being the ISOLATE and REMOTE_TX rules. e.g.:
399 * LOCAL_MAC -> BROADCAST -> BROADCASTV6 -> REMOTE_TX -> ISOLATE -> NULL
401 * That enables tap_flow_isolate() to remove implicit rules by popping the list
402 * head and remove it as long as it applies on the remote netdevice. The
403 * implicit rule for TX redirection is not removed, as isolate concerns only
407 static struct remote_rule implicit_rte_flows[TAP_REMOTE_MAX_IDX] = {
408 [TAP_REMOTE_LOCAL_MAC] = {
411 .priority = PRIORITY_MASK - TAP_REMOTE_LOCAL_MAC,
415 .type = RTE_FLOW_ITEM_TYPE_ETH,
416 .mask = &(const struct rte_flow_item_eth){
417 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
421 .type = RTE_FLOW_ITEM_TYPE_END,
423 .mirred = TCA_EGRESS_REDIR,
425 [TAP_REMOTE_BROADCAST] = {
428 .priority = PRIORITY_MASK - TAP_REMOTE_BROADCAST,
432 .type = RTE_FLOW_ITEM_TYPE_ETH,
433 .mask = &(const struct rte_flow_item_eth){
434 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
436 .spec = &(const struct rte_flow_item_eth){
437 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
441 .type = RTE_FLOW_ITEM_TYPE_END,
443 .mirred = TCA_EGRESS_MIRROR,
445 [TAP_REMOTE_BROADCASTV6] = {
448 .priority = PRIORITY_MASK - TAP_REMOTE_BROADCASTV6,
452 .type = RTE_FLOW_ITEM_TYPE_ETH,
453 .mask = &(const struct rte_flow_item_eth){
454 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
456 .spec = &(const struct rte_flow_item_eth){
457 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
461 .type = RTE_FLOW_ITEM_TYPE_END,
463 .mirred = TCA_EGRESS_MIRROR,
465 [TAP_REMOTE_PROMISC] = {
468 .priority = PRIORITY_MASK - TAP_REMOTE_PROMISC,
472 .type = RTE_FLOW_ITEM_TYPE_VOID,
475 .type = RTE_FLOW_ITEM_TYPE_END,
477 .mirred = TCA_EGRESS_MIRROR,
479 [TAP_REMOTE_ALLMULTI] = {
482 .priority = PRIORITY_MASK - TAP_REMOTE_ALLMULTI,
486 .type = RTE_FLOW_ITEM_TYPE_ETH,
487 .mask = &(const struct rte_flow_item_eth){
488 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
490 .spec = &(const struct rte_flow_item_eth){
491 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
495 .type = RTE_FLOW_ITEM_TYPE_END,
497 .mirred = TCA_EGRESS_MIRROR,
502 .priority = TAP_REMOTE_TX,
506 .type = RTE_FLOW_ITEM_TYPE_VOID,
509 .type = RTE_FLOW_ITEM_TYPE_END,
511 .mirred = TCA_EGRESS_MIRROR,
516 .priority = PRIORITY_MASK - TAP_ISOLATE,
520 .type = RTE_FLOW_ITEM_TYPE_VOID,
523 .type = RTE_FLOW_ITEM_TYPE_END,
529 * Make as much checks as possible on an Ethernet item, and if a flow is
530 * provided, fill it appropriately with Ethernet info.
533 * Item specification.
534 * @param[in, out] data
535 * Additional data structure to tell next layers we've been here.
538 * 0 if checks are alright, -1 otherwise.
541 tap_flow_create_eth(const struct rte_flow_item *item, void *data)
543 struct convert_data *info = (struct convert_data *)data;
544 const struct rte_flow_item_eth *spec = item->spec;
545 const struct rte_flow_item_eth *mask = item->mask;
546 struct rte_flow *flow = info->flow;
549 /* use default mask if none provided */
551 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_ETH].default_mask;
552 /* TC does not support eth_type masking. Only accept if exact match. */
553 if (mask->type && mask->type != 0xffff)
557 /* store eth_type for consistency if ipv4/6 pattern item comes next */
558 if (spec->type & mask->type)
559 info->eth_type = spec->type;
563 if (!is_zero_ether_addr(&spec->dst)) {
564 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_ETH_DST, ETHER_ADDR_LEN,
565 &spec->dst.addr_bytes);
566 tap_nlattr_add(&msg->nh,
567 TCA_FLOWER_KEY_ETH_DST_MASK, ETHER_ADDR_LEN,
568 &mask->dst.addr_bytes);
570 if (!is_zero_ether_addr(&mask->src)) {
571 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_ETH_SRC, ETHER_ADDR_LEN,
572 &spec->src.addr_bytes);
573 tap_nlattr_add(&msg->nh,
574 TCA_FLOWER_KEY_ETH_SRC_MASK, ETHER_ADDR_LEN,
575 &mask->src.addr_bytes);
581 * Make as much checks as possible on a VLAN item, and if a flow is provided,
582 * fill it appropriately with VLAN info.
585 * Item specification.
586 * @param[in, out] data
587 * Additional data structure to tell next layers we've been here.
590 * 0 if checks are alright, -1 otherwise.
593 tap_flow_create_vlan(const struct rte_flow_item *item, void *data)
595 struct convert_data *info = (struct convert_data *)data;
596 const struct rte_flow_item_vlan *spec = item->spec;
597 const struct rte_flow_item_vlan *mask = item->mask;
598 struct rte_flow *flow = info->flow;
601 /* use default mask if none provided */
603 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_VLAN].default_mask;
604 /* TC does not support tpid masking. Only accept if exact match. */
605 if (mask->tpid && mask->tpid != 0xffff)
607 /* Double-tagging not supported. */
608 if (spec && mask->tpid && spec->tpid != htons(ETH_P_8021Q))
614 msg->t.tcm_info = TC_H_MAKE(msg->t.tcm_info, htons(ETH_P_8021Q));
615 #define VLAN_PRIO(tci) ((tci) >> 13)
616 #define VLAN_ID(tci) ((tci) & 0xfff)
620 uint16_t tci = ntohs(spec->tci) & mask->tci;
621 uint16_t prio = VLAN_PRIO(tci);
622 uint8_t vid = VLAN_ID(tci);
625 tap_nlattr_add8(&msg->nh,
626 TCA_FLOWER_KEY_VLAN_PRIO, prio);
628 tap_nlattr_add16(&msg->nh,
629 TCA_FLOWER_KEY_VLAN_ID, vid);
635 * Make as much checks as possible on an IPv4 item, and if a flow is provided,
636 * fill it appropriately with IPv4 info.
639 * Item specification.
640 * @param[in, out] data
641 * Additional data structure to tell next layers we've been here.
644 * 0 if checks are alright, -1 otherwise.
647 tap_flow_create_ipv4(const struct rte_flow_item *item, void *data)
649 struct convert_data *info = (struct convert_data *)data;
650 const struct rte_flow_item_ipv4 *spec = item->spec;
651 const struct rte_flow_item_ipv4 *mask = item->mask;
652 struct rte_flow *flow = info->flow;
655 /* use default mask if none provided */
657 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_IPV4].default_mask;
658 /* check that previous eth type is compatible with ipv4 */
659 if (info->eth_type && info->eth_type != htons(ETH_P_IP))
661 /* store ip_proto for consistency if udp/tcp pattern item comes next */
663 info->ip_proto = spec->hdr.next_proto_id;
668 info->eth_type = htons(ETH_P_IP);
671 if (spec->hdr.dst_addr) {
672 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_DST,
674 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_DST_MASK,
677 if (spec->hdr.src_addr) {
678 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_SRC,
680 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_SRC_MASK,
683 if (spec->hdr.next_proto_id)
684 tap_nlattr_add8(&msg->nh, TCA_FLOWER_KEY_IP_PROTO,
685 spec->hdr.next_proto_id);
690 * Make as much checks as possible on an IPv6 item, and if a flow is provided,
691 * fill it appropriately with IPv6 info.
694 * Item specification.
695 * @param[in, out] data
696 * Additional data structure to tell next layers we've been here.
699 * 0 if checks are alright, -1 otherwise.
702 tap_flow_create_ipv6(const struct rte_flow_item *item, void *data)
704 struct convert_data *info = (struct convert_data *)data;
705 const struct rte_flow_item_ipv6 *spec = item->spec;
706 const struct rte_flow_item_ipv6 *mask = item->mask;
707 struct rte_flow *flow = info->flow;
708 uint8_t empty_addr[16] = { 0 };
711 /* use default mask if none provided */
713 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_IPV6].default_mask;
714 /* check that previous eth type is compatible with ipv6 */
715 if (info->eth_type && info->eth_type != htons(ETH_P_IPV6))
717 /* store ip_proto for consistency if udp/tcp pattern item comes next */
719 info->ip_proto = spec->hdr.proto;
724 info->eth_type = htons(ETH_P_IPV6);
727 if (memcmp(spec->hdr.dst_addr, empty_addr, 16)) {
728 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_DST,
729 sizeof(spec->hdr.dst_addr), &spec->hdr.dst_addr);
730 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_DST_MASK,
731 sizeof(mask->hdr.dst_addr), &mask->hdr.dst_addr);
733 if (memcmp(spec->hdr.src_addr, empty_addr, 16)) {
734 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_SRC,
735 sizeof(spec->hdr.src_addr), &spec->hdr.src_addr);
736 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_SRC_MASK,
737 sizeof(mask->hdr.src_addr), &mask->hdr.src_addr);
740 tap_nlattr_add8(&msg->nh,
741 TCA_FLOWER_KEY_IP_PROTO, spec->hdr.proto);
746 * Make as much checks as possible on a UDP item, and if a flow is provided,
747 * fill it appropriately with UDP info.
750 * Item specification.
751 * @param[in, out] data
752 * Additional data structure to tell next layers we've been here.
755 * 0 if checks are alright, -1 otherwise.
758 tap_flow_create_udp(const struct rte_flow_item *item, void *data)
760 struct convert_data *info = (struct convert_data *)data;
761 const struct rte_flow_item_udp *spec = item->spec;
762 const struct rte_flow_item_udp *mask = item->mask;
763 struct rte_flow *flow = info->flow;
766 /* use default mask if none provided */
768 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_UDP].default_mask;
769 /* check that previous ip_proto is compatible with udp */
770 if (info->ip_proto && info->ip_proto != IPPROTO_UDP)
772 /* TC does not support UDP port masking. Only accept if exact match. */
773 if ((mask->hdr.src_port && mask->hdr.src_port != 0xffff) ||
774 (mask->hdr.dst_port && mask->hdr.dst_port != 0xffff))
779 tap_nlattr_add8(&msg->nh, TCA_FLOWER_KEY_IP_PROTO, IPPROTO_UDP);
782 if (spec->hdr.dst_port & mask->hdr.dst_port)
783 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_UDP_DST,
785 if (spec->hdr.src_port & mask->hdr.src_port)
786 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_UDP_SRC,
792 * Make as much checks as possible on a TCP item, and if a flow is provided,
793 * fill it appropriately with TCP info.
796 * Item specification.
797 * @param[in, out] data
798 * Additional data structure to tell next layers we've been here.
801 * 0 if checks are alright, -1 otherwise.
804 tap_flow_create_tcp(const struct rte_flow_item *item, void *data)
806 struct convert_data *info = (struct convert_data *)data;
807 const struct rte_flow_item_tcp *spec = item->spec;
808 const struct rte_flow_item_tcp *mask = item->mask;
809 struct rte_flow *flow = info->flow;
812 /* use default mask if none provided */
814 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_TCP].default_mask;
815 /* check that previous ip_proto is compatible with tcp */
816 if (info->ip_proto && info->ip_proto != IPPROTO_TCP)
818 /* TC does not support TCP port masking. Only accept if exact match. */
819 if ((mask->hdr.src_port && mask->hdr.src_port != 0xffff) ||
820 (mask->hdr.dst_port && mask->hdr.dst_port != 0xffff))
825 tap_nlattr_add8(&msg->nh, TCA_FLOWER_KEY_IP_PROTO, IPPROTO_TCP);
828 if (spec->hdr.dst_port & mask->hdr.dst_port)
829 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_TCP_DST,
831 if (spec->hdr.src_port & mask->hdr.src_port)
832 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_TCP_SRC,
838 * Check support for a given item.
841 * Item specification.
843 * Bit-Mask size in bytes.
844 * @param[in] supported_mask
845 * Bit-mask covering supported fields to compare with spec, last and mask in
847 * @param[in] default_mask
848 * Bit-mask default mask if none is provided in \item.
854 tap_flow_item_validate(const struct rte_flow_item *item,
856 const uint8_t *supported_mask,
857 const uint8_t *default_mask)
861 /* An empty layer is allowed, as long as all fields are NULL */
862 if (!item->spec && (item->mask || item->last))
864 /* Is the item spec compatible with what the NIC supports? */
865 if (item->spec && !item->mask) {
867 const uint8_t *spec = item->spec;
869 for (i = 0; i < size; ++i)
870 if ((spec[i] | supported_mask[i]) != supported_mask[i])
872 /* Is the default mask compatible with what the NIC supports? */
873 for (i = 0; i < size; i++)
874 if ((default_mask[i] | supported_mask[i]) !=
878 /* Is the item last compatible with what the NIC supports? */
879 if (item->last && !item->mask) {
881 const uint8_t *spec = item->last;
883 for (i = 0; i < size; ++i)
884 if ((spec[i] | supported_mask[i]) != supported_mask[i])
887 /* Is the item mask compatible with what the NIC supports? */
890 const uint8_t *spec = item->mask;
892 for (i = 0; i < size; ++i)
893 if ((spec[i] | supported_mask[i]) != supported_mask[i])
897 * Once masked, Are item spec and item last equal?
898 * TC does not support range so anything else is invalid.
900 if (item->spec && item->last) {
903 const uint8_t *apply = default_mask;
908 for (i = 0; i < size; ++i) {
909 spec[i] = ((const uint8_t *)item->spec)[i] & apply[i];
910 last[i] = ((const uint8_t *)item->last)[i] & apply[i];
912 ret = memcmp(spec, last, size);
918 * Configure the kernel with a TC action and its configured parameters
919 * Handled actions: "gact", "mirred", "skbedit", "bpf"
922 * Pointer to rte flow containing the netlink message
924 * @param[in, out] act_index
925 * Pointer to action sequence number in the TC command
928 * Pointer to struct holding the action parameters
931 * -1 on failure, 0 on success
934 add_action(struct rte_flow *flow, size_t *act_index, struct action_data *adata)
936 struct nlmsg *msg = &flow->msg;
938 if (tap_nlattr_nested_start(msg, (*act_index)++) < 0)
941 tap_nlattr_add(&msg->nh, TCA_ACT_KIND,
942 strlen(adata->id) + 1, adata->id);
943 if (tap_nlattr_nested_start(msg, TCA_ACT_OPTIONS) < 0)
945 if (strcmp("gact", adata->id) == 0) {
946 tap_nlattr_add(&msg->nh, TCA_GACT_PARMS, sizeof(adata->gact),
948 } else if (strcmp("mirred", adata->id) == 0) {
949 if (adata->mirred.eaction == TCA_EGRESS_MIRROR)
950 adata->mirred.action = TC_ACT_PIPE;
952 adata->mirred.action = TC_ACT_STOLEN;
953 tap_nlattr_add(&msg->nh, TCA_MIRRED_PARMS,
954 sizeof(adata->mirred),
956 } else if (strcmp("skbedit", adata->id) == 0) {
957 tap_nlattr_add(&msg->nh, TCA_SKBEDIT_PARMS,
958 sizeof(adata->skbedit.skbedit),
959 &adata->skbedit.skbedit);
960 tap_nlattr_add16(&msg->nh, TCA_SKBEDIT_QUEUE_MAPPING,
961 adata->skbedit.queue);
962 } else if (strcmp("bpf", adata->id) == 0) {
963 tap_nlattr_add32(&msg->nh, TCA_ACT_BPF_FD, adata->bpf.bpf_fd);
964 tap_nlattr_add(&msg->nh, TCA_ACT_BPF_NAME,
965 strlen(adata->bpf.annotation) + 1,
966 adata->bpf.annotation);
967 tap_nlattr_add(&msg->nh, TCA_ACT_BPF_PARMS,
968 sizeof(adata->bpf.bpf),
973 tap_nlattr_nested_finish(msg); /* nested TCA_ACT_OPTIONS */
974 tap_nlattr_nested_finish(msg); /* nested act_index */
979 * Helper function to send a serie of TC actions to the kernel
982 * Pointer to rte flow containing the netlink message
984 * @param[in] nb_actions
985 * Number of actions in an array of action structs
988 * Pointer to an array of action structs
990 * @param[in] classifier_actions
991 * The classifier on behave of which the actions are configured
994 * -1 on failure, 0 on success
997 add_actions(struct rte_flow *flow, int nb_actions, struct action_data *data,
998 int classifier_action)
1000 struct nlmsg *msg = &flow->msg;
1001 size_t act_index = 1;
1004 if (tap_nlattr_nested_start(msg, classifier_action) < 0)
1006 for (i = 0; i < nb_actions; i++)
1007 if (add_action(flow, &act_index, data + i) < 0)
1009 tap_nlattr_nested_finish(msg); /* nested TCA_FLOWER_ACT */
1014 * Validate a flow supported by TC.
1015 * If flow param is not NULL, then also fill the netlink message inside.
1018 * Pointer to private structure.
1020 * Flow rule attributes.
1021 * @param[in] pattern
1022 * Pattern specification (list terminated by the END pattern item).
1023 * @param[in] actions
1024 * Associated actions (list terminated by the END action).
1026 * Perform verbose error reporting if not NULL.
1027 * @param[in, out] flow
1028 * Flow structure to update.
1030 * If set to TCA_EGRESS_REDIR, provided actions will be replaced with a
1031 * redirection to the tap netdevice, and the TC rule will be configured
1032 * on the remote netdevice in pmd.
1033 * If set to TCA_EGRESS_MIRROR, provided actions will be replaced with a
1034 * mirroring to the tap netdevice, and the TC rule will be configured
1035 * on the remote netdevice in pmd. Matching packets will thus be duplicated.
1036 * If set to 0, the standard behavior is to be used: set correct actions for
1037 * the TC rule, and apply it on the tap netdevice.
1040 * 0 on success, a negative errno value otherwise and rte_errno is set.
1043 priv_flow_process(struct pmd_internals *pmd,
1044 const struct rte_flow_attr *attr,
1045 const struct rte_flow_item items[],
1046 const struct rte_flow_action actions[],
1047 struct rte_flow_error *error,
1048 struct rte_flow *flow,
1051 const struct tap_flow_items *cur_item = tap_flow_items;
1052 struct convert_data data = {
1057 int action = 0; /* Only one action authorized for now */
1059 if (attr->group > MAX_GROUP) {
1061 error, EINVAL, RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1062 NULL, "group value too big: cannot exceed 15");
1065 if (attr->priority > MAX_PRIORITY) {
1067 error, EINVAL, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1068 NULL, "priority value too big");
1071 uint16_t group = attr->group << GROUP_SHIFT;
1072 uint16_t prio = group | (attr->priority +
1073 RSS_PRIORITY_OFFSET + PRIORITY_OFFSET);
1074 flow->msg.t.tcm_info = TC_H_MAKE(prio << 16,
1075 flow->msg.t.tcm_info);
1080 * If attr->ingress, the rule applies on remote ingress
1081 * to match incoming packets
1082 * If attr->egress, the rule applies on tap ingress (as
1083 * seen from the kernel) to deal with packets going out
1084 * from the DPDK app.
1086 flow->msg.t.tcm_parent = TC_H_MAKE(TC_H_INGRESS, 0);
1088 /* Standard rule on tap egress (kernel standpoint). */
1089 flow->msg.t.tcm_parent =
1090 TC_H_MAKE(MULTIQ_MAJOR_HANDLE, 0);
1092 /* use flower filter type */
1093 tap_nlattr_add(&flow->msg.nh, TCA_KIND, sizeof("flower"), "flower");
1094 if (tap_nlattr_nested_start(&flow->msg, TCA_OPTIONS) < 0)
1095 goto exit_item_not_supported;
1097 for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
1098 const struct tap_flow_items *token = NULL;
1102 if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
1106 cur_item->items[i] != RTE_FLOW_ITEM_TYPE_END;
1108 if (cur_item->items[i] == items->type) {
1109 token = &tap_flow_items[items->type];
1114 goto exit_item_not_supported;
1116 err = tap_flow_item_validate(
1117 items, cur_item->mask_sz,
1118 (const uint8_t *)cur_item->mask,
1119 (const uint8_t *)cur_item->default_mask);
1121 goto exit_item_not_supported;
1122 if (flow && cur_item->convert) {
1123 err = cur_item->convert(items, &data);
1125 goto exit_item_not_supported;
1130 tap_nlattr_add16(&flow->msg.nh, TCA_FLOWER_KEY_ETH_TYPE,
1131 htons(ETH_P_8021Q));
1132 tap_nlattr_add16(&flow->msg.nh,
1133 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1135 data.eth_type : htons(ETH_P_ALL));
1136 } else if (data.eth_type) {
1137 tap_nlattr_add16(&flow->msg.nh, TCA_FLOWER_KEY_ETH_TYPE,
1141 if (mirred && flow) {
1142 struct action_data adata = {
1150 * If attr->egress && mirred, then this is a special
1151 * case where the rule must be applied on the tap, to
1152 * redirect packets coming from the DPDK App, out
1153 * through the remote netdevice.
1155 adata.mirred.ifindex = attr->ingress ? pmd->if_index :
1156 pmd->remote_if_index;
1157 if (mirred == TCA_EGRESS_MIRROR)
1158 adata.mirred.action = TC_ACT_PIPE;
1160 adata.mirred.action = TC_ACT_STOLEN;
1161 if (add_actions(flow, 1, &adata, TCA_FLOWER_ACT) < 0)
1162 goto exit_action_not_supported;
1166 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
1169 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID) {
1171 } else if (actions->type == RTE_FLOW_ACTION_TYPE_DROP) {
1173 goto exit_action_not_supported;
1176 struct action_data adata = {
1179 .action = TC_ACT_SHOT,
1183 err = add_actions(flow, 1, &adata,
1186 } else if (actions->type == RTE_FLOW_ACTION_TYPE_PASSTHRU) {
1188 goto exit_action_not_supported;
1191 struct action_data adata = {
1195 .action = TC_ACT_UNSPEC,
1199 err = add_actions(flow, 1, &adata,
1202 } else if (actions->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
1203 const struct rte_flow_action_queue *queue =
1204 (const struct rte_flow_action_queue *)
1208 goto exit_action_not_supported;
1211 (queue->index > pmd->dev->data->nb_rx_queues - 1))
1212 goto exit_action_not_supported;
1214 struct action_data adata = {
1218 .action = TC_ACT_PIPE,
1220 .queue = queue->index,
1224 err = add_actions(flow, 1, &adata,
1227 } else if (actions->type == RTE_FLOW_ACTION_TYPE_RSS) {
1228 const struct rte_flow_action_rss *rss =
1229 (const struct rte_flow_action_rss *)
1233 goto exit_action_not_supported;
1235 if (!pmd->rss_enabled) {
1236 err = rss_enable(pmd, attr, error);
1238 goto exit_action_not_supported;
1241 err = rss_add_actions(flow, pmd, rss, error);
1243 goto exit_action_not_supported;
1246 goto exit_action_not_supported;
1250 tap_nlattr_nested_finish(&flow->msg); /* nested TCA_OPTIONS */
1252 exit_item_not_supported:
1253 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1254 items, "item not supported");
1256 exit_action_not_supported:
1257 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
1258 actions, "action not supported");
1267 * @see rte_flow_validate()
1271 tap_flow_validate(struct rte_eth_dev *dev,
1272 const struct rte_flow_attr *attr,
1273 const struct rte_flow_item items[],
1274 const struct rte_flow_action actions[],
1275 struct rte_flow_error *error)
1277 struct pmd_internals *pmd = dev->data->dev_private;
1279 return priv_flow_process(pmd, attr, items, actions, error, NULL, 0);
1283 * Set a unique handle in a flow.
1285 * The kernel supports TC rules with equal priority, as long as they use the
1286 * same matching fields (e.g.: dst mac and ipv4) with different values (and
1287 * full mask to ensure no collision is possible).
1288 * In those rules, the handle (uint32_t) is the part that would identify
1289 * specifically each rule.
1291 * On 32-bit architectures, the handle can simply be the flow's pointer address.
1292 * On 64-bit architectures, we rely on jhash(flow) to find a (sufficiently)
1295 * @param[in, out] flow
1296 * The flow that needs its handle set.
1299 tap_flow_set_handle(struct rte_flow *flow)
1301 uint32_t handle = 0;
1303 if (sizeof(flow) > 4)
1304 handle = rte_jhash(&flow, sizeof(flow), 1);
1306 handle = (uintptr_t)flow;
1307 /* must be at least 1 to avoid letting the kernel choose one for us */
1310 flow->msg.t.tcm_handle = handle;
1316 * @see rte_flow_create()
1319 static struct rte_flow *
1320 tap_flow_create(struct rte_eth_dev *dev,
1321 const struct rte_flow_attr *attr,
1322 const struct rte_flow_item items[],
1323 const struct rte_flow_action actions[],
1324 struct rte_flow_error *error)
1326 struct pmd_internals *pmd = dev->data->dev_private;
1327 struct rte_flow *remote_flow = NULL;
1328 struct rte_flow *flow = NULL;
1329 struct nlmsg *msg = NULL;
1332 if (!pmd->if_index) {
1333 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1335 "can't create rule, ifindex not found");
1339 * No rules configured through standard rte_flow should be set on the
1340 * priorities used by implicit rules.
1342 if ((attr->group == MAX_GROUP) &&
1343 attr->priority > (MAX_PRIORITY - TAP_REMOTE_MAX_IDX)) {
1345 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1346 NULL, "priority value too big");
1349 flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1351 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1352 NULL, "cannot allocate memory for rte_flow");
1356 tc_init_msg(msg, pmd->if_index, RTM_NEWTFILTER,
1357 NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
1358 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1359 tap_flow_set_handle(flow);
1360 if (priv_flow_process(pmd, attr, items, actions, error, flow, 0))
1362 err = tap_nl_send(pmd->nlsk_fd, &msg->nh);
1364 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1365 NULL, "couldn't send request to kernel");
1368 err = tap_nl_recv_ack(pmd->nlsk_fd);
1371 "Kernel refused TC filter rule creation (%d): %s\n",
1372 errno, strerror(errno));
1373 rte_flow_error_set(error, EEXIST, RTE_FLOW_ERROR_TYPE_HANDLE,
1375 "overlapping rules or Kernel too old for flower support");
1378 LIST_INSERT_HEAD(&pmd->flows, flow, next);
1380 * If a remote device is configured, a TC rule with identical items for
1381 * matching must be set on that device, with a single action: redirect
1382 * to the local pmd->if_index.
1384 if (pmd->remote_if_index) {
1385 remote_flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1388 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1389 "cannot allocate memory for rte_flow");
1392 msg = &remote_flow->msg;
1393 /* set the rule if_index for the remote netdevice */
1395 msg, pmd->remote_if_index, RTM_NEWTFILTER,
1396 NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
1397 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1398 tap_flow_set_handle(remote_flow);
1399 if (priv_flow_process(pmd, attr, items, NULL,
1400 error, remote_flow, TCA_EGRESS_REDIR)) {
1402 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1403 NULL, "rte flow rule validation failed");
1406 err = tap_nl_send(pmd->nlsk_fd, &msg->nh);
1409 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1410 NULL, "Failure sending nl request");
1413 err = tap_nl_recv_ack(pmd->nlsk_fd);
1416 "Kernel refused TC filter rule creation (%d): %s\n",
1417 errno, strerror(errno));
1419 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1421 "overlapping rules or Kernel too old for flower support");
1424 flow->remote_flow = remote_flow;
1429 rte_free(remote_flow);
1436 * Destroy a flow using pointer to pmd_internal.
1438 * @param[in, out] pmd
1439 * Pointer to private structure.
1441 * Pointer to the flow to destroy.
1442 * @param[in, out] error
1443 * Pointer to the flow error handler
1445 * @return 0 if the flow could be destroyed, -1 otherwise.
1448 tap_flow_destroy_pmd(struct pmd_internals *pmd,
1449 struct rte_flow *flow,
1450 struct rte_flow_error *error)
1452 struct rte_flow *remote_flow = flow->remote_flow;
1456 LIST_REMOVE(flow, next);
1457 flow->msg.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1458 flow->msg.nh.nlmsg_type = RTM_DELTFILTER;
1460 ret = tap_nl_send(pmd->nlsk_fd, &flow->msg.nh);
1462 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1463 NULL, "couldn't send request to kernel");
1466 ret = tap_nl_recv_ack(pmd->nlsk_fd);
1467 /* If errno is ENOENT, the rule is already no longer in the kernel. */
1468 if (ret < 0 && errno == ENOENT)
1472 "Kernel refused TC filter rule deletion (%d): %s\n",
1473 errno, strerror(errno));
1475 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1476 "couldn't receive kernel ack to our request");
1479 /* Close opened BPF file descriptors of this flow */
1480 for (i = 0; i < SEC_MAX; i++)
1481 if (flow->bpf_fd[i] != 0) {
1482 close(flow->bpf_fd[i]);
1483 flow->bpf_fd[i] = 0;
1486 /* Release map key for this RSS rule */
1487 ret = bpf_rss_key(KEY_CMD_RELEASE, &flow->key_idx);
1490 error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1491 "Failed to release BPF RSS key");
1497 remote_flow->msg.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1498 remote_flow->msg.nh.nlmsg_type = RTM_DELTFILTER;
1500 ret = tap_nl_send(pmd->nlsk_fd, &remote_flow->msg.nh);
1503 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1504 NULL, "Failure sending nl request");
1507 ret = tap_nl_recv_ack(pmd->nlsk_fd);
1508 if (ret < 0 && errno == ENOENT)
1512 "Kernel refused TC filter rule deletion (%d): %s\n",
1513 errno, strerror(errno));
1515 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1516 NULL, "Failure trying to receive nl ack");
1522 rte_free(remote_flow);
1530 * @see rte_flow_destroy()
1534 tap_flow_destroy(struct rte_eth_dev *dev,
1535 struct rte_flow *flow,
1536 struct rte_flow_error *error)
1538 struct pmd_internals *pmd = dev->data->dev_private;
1540 return tap_flow_destroy_pmd(pmd, flow, error);
1544 * Enable/disable flow isolation.
1546 * @see rte_flow_isolate()
1550 tap_flow_isolate(struct rte_eth_dev *dev,
1552 struct rte_flow_error *error __rte_unused)
1554 struct pmd_internals *pmd = dev->data->dev_private;
1557 pmd->flow_isolate = 1;
1559 pmd->flow_isolate = 0;
1561 * If netdevice is there, setup appropriate flow rules immediately.
1562 * Otherwise it will be set when bringing up the netdevice (tun_alloc).
1564 if (!pmd->rxq[0].fd)
1567 struct rte_flow *flow;
1570 flow = LIST_FIRST(&pmd->implicit_flows);
1574 * Remove all implicit rules on the remote.
1575 * Keep the local rule to redirect packets on TX.
1576 * Keep also the last implicit local rule: ISOLATE.
1578 if (flow->msg.t.tcm_ifindex == pmd->if_index)
1580 if (tap_flow_destroy_pmd(pmd, flow, NULL) < 0)
1583 /* Switch the TC rule according to pmd->flow_isolate */
1584 if (tap_flow_implicit_create(pmd, TAP_ISOLATE) == -1)
1587 /* Switch the TC rule according to pmd->flow_isolate */
1588 if (tap_flow_implicit_create(pmd, TAP_ISOLATE) == -1)
1590 if (!pmd->remote_if_index)
1592 if (tap_flow_implicit_create(pmd, TAP_REMOTE_TX) < 0)
1594 if (tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0)
1596 if (tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCAST) < 0)
1598 if (tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCASTV6) < 0)
1600 if (dev->data->promiscuous &&
1601 tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC) < 0)
1603 if (dev->data->all_multicast &&
1604 tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI) < 0)
1609 pmd->flow_isolate = 0;
1610 return rte_flow_error_set(
1611 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1612 "TC rule creation failed");
1616 * Destroy all flows.
1618 * @see rte_flow_flush()
1622 tap_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1624 struct pmd_internals *pmd = dev->data->dev_private;
1625 struct rte_flow *flow;
1627 while (!LIST_EMPTY(&pmd->flows)) {
1628 flow = LIST_FIRST(&pmd->flows);
1629 if (tap_flow_destroy(dev, flow, error) < 0)
1636 * Add an implicit flow rule on the remote device to make sure traffic gets to
1637 * the tap netdevice from there.
1640 * Pointer to private structure.
1642 * The idx in the implicit_rte_flows array specifying which rule to apply.
1644 * @return -1 if the rule couldn't be applied, 0 otherwise.
1646 int tap_flow_implicit_create(struct pmd_internals *pmd,
1647 enum implicit_rule_index idx)
1649 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE;
1650 struct rte_flow_action *actions = implicit_rte_flows[idx].actions;
1651 struct rte_flow_action isolate_actions[2] = {
1653 .type = RTE_FLOW_ACTION_TYPE_END,
1656 struct rte_flow_item *items = implicit_rte_flows[idx].items;
1657 struct rte_flow_attr *attr = &implicit_rte_flows[idx].attr;
1658 struct rte_flow_item_eth eth_local = { .type = 0 };
1659 uint16_t if_index = pmd->remote_if_index;
1660 struct rte_flow *remote_flow = NULL;
1661 struct nlmsg *msg = NULL;
1663 struct rte_flow_item items_local[2] = {
1665 .type = items[0].type,
1667 .mask = items[0].mask,
1670 .type = items[1].type,
1674 remote_flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1676 RTE_LOG(ERR, PMD, "Cannot allocate memory for rte_flow\n");
1679 msg = &remote_flow->msg;
1680 if (idx == TAP_REMOTE_TX) {
1681 if_index = pmd->if_index;
1682 } else if (idx == TAP_ISOLATE) {
1683 if_index = pmd->if_index;
1684 /* Don't be exclusive for this rule, it can be changed later. */
1685 flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE;
1686 isolate_actions[0].type = pmd->flow_isolate ?
1687 RTE_FLOW_ACTION_TYPE_DROP :
1688 RTE_FLOW_ACTION_TYPE_PASSTHRU;
1689 actions = isolate_actions;
1690 } else if (idx == TAP_REMOTE_LOCAL_MAC) {
1692 * eth addr couldn't be set in implicit_rte_flows[] as it is not
1693 * known at compile time.
1695 memcpy(ð_local.dst, &pmd->eth_addr, sizeof(pmd->eth_addr));
1696 items = items_local;
1698 tc_init_msg(msg, if_index, RTM_NEWTFILTER, flags);
1699 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1701 * The ISOLATE rule is always present and must have a static handle, as
1702 * the action is changed whether the feature is enabled (DROP) or
1703 * disabled (PASSTHRU).
1705 if (idx == TAP_ISOLATE)
1706 remote_flow->msg.t.tcm_handle = ISOLATE_HANDLE;
1708 tap_flow_set_handle(remote_flow);
1709 if (priv_flow_process(pmd, attr, items, actions, NULL,
1710 remote_flow, implicit_rte_flows[idx].mirred)) {
1711 RTE_LOG(ERR, PMD, "rte flow rule validation failed\n");
1714 err = tap_nl_send(pmd->nlsk_fd, &msg->nh);
1716 RTE_LOG(ERR, PMD, "Failure sending nl request\n");
1719 err = tap_nl_recv_ack(pmd->nlsk_fd);
1722 "Kernel refused TC filter rule creation (%d): %s\n",
1723 errno, strerror(errno));
1726 LIST_INSERT_HEAD(&pmd->implicit_flows, remote_flow, next);
1730 rte_free(remote_flow);
1735 * Remove specific implicit flow rule on the remote device.
1737 * @param[in, out] pmd
1738 * Pointer to private structure.
1740 * The idx in the implicit_rte_flows array specifying which rule to remove.
1742 * @return -1 if one of the implicit rules couldn't be created, 0 otherwise.
1744 int tap_flow_implicit_destroy(struct pmd_internals *pmd,
1745 enum implicit_rule_index idx)
1747 struct rte_flow *remote_flow;
1749 int idx_prio = implicit_rte_flows[idx].attr.priority + PRIORITY_OFFSET;
1751 for (remote_flow = LIST_FIRST(&pmd->implicit_flows);
1753 remote_flow = LIST_NEXT(remote_flow, next)) {
1754 cur_prio = (remote_flow->msg.t.tcm_info >> 16) & PRIORITY_MASK;
1755 if (cur_prio != idx_prio)
1757 return tap_flow_destroy_pmd(pmd, remote_flow, NULL);
1763 * Destroy all implicit flows.
1765 * @see rte_flow_flush()
1768 tap_flow_implicit_flush(struct pmd_internals *pmd, struct rte_flow_error *error)
1770 struct rte_flow *remote_flow;
1772 while (!LIST_EMPTY(&pmd->implicit_flows)) {
1773 remote_flow = LIST_FIRST(&pmd->implicit_flows);
1774 if (tap_flow_destroy_pmd(pmd, remote_flow, error) < 0)
1780 #define MAX_RSS_KEYS 256
1781 #define SEC_NAME_CLS_Q "cls_q"
1783 const char *sec_name[SEC_MAX] = {
1784 [SEC_L3_L4] = "l3_l4",
1788 * Enable RSS on tap: create TC rules for queuing.
1790 * @param[in, out] pmd
1791 * Pointer to private structure.
1794 * Pointer to rte_flow to get flow group
1797 * Pointer to error reporting if not NULL.
1799 * @return 0 on success, negative value on failure.
1801 static int rss_enable(struct pmd_internals *pmd,
1802 const struct rte_flow_attr *attr,
1803 struct rte_flow_error *error)
1805 struct rte_flow *rss_flow = NULL;
1806 struct nlmsg *msg = NULL;
1807 /* 4096 is the maximum number of instructions for a BPF program */
1808 char annotation[64];
1812 /* unlimit locked memory */
1813 struct rlimit memlock_limit = {
1814 .rlim_cur = RLIM_INFINITY,
1815 .rlim_max = RLIM_INFINITY,
1817 setrlimit(RLIMIT_MEMLOCK, &memlock_limit);
1819 /* Get a new map key for a new RSS rule */
1820 err = bpf_rss_key(KEY_CMD_INIT, NULL);
1823 error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1824 "Failed to initialize BPF RSS keys");
1830 * Create BPF RSS MAP
1832 pmd->map_fd = tap_flow_bpf_rss_map_create(sizeof(__u32), /* key size */
1833 sizeof(struct rss_key),
1835 if (pmd->map_fd < 0) {
1837 "Failed to create BPF map (%d): %s\n",
1838 errno, strerror(errno));
1840 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1841 "Kernel too old or not configured "
1842 "to support BPF maps");
1848 * Add a rule per queue to match reclassified packets and direct them to
1849 * the correct queue.
1851 for (i = 0; i < pmd->dev->data->nb_rx_queues; i++) {
1852 pmd->bpf_fd[i] = tap_flow_bpf_cls_q(i);
1853 if (pmd->bpf_fd[i] < 0) {
1855 "Failed to load BPF section %s for queue %d",
1858 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1860 "Kernel too old or not configured "
1861 "to support BPF programs loading");
1866 rss_flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1869 "Cannot allocate memory for rte_flow");
1872 msg = &rss_flow->msg;
1873 tc_init_msg(msg, pmd->if_index, RTM_NEWTFILTER, NLM_F_REQUEST |
1874 NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
1875 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1876 tap_flow_set_handle(rss_flow);
1877 uint16_t group = attr->group << GROUP_SHIFT;
1878 uint16_t prio = group | (i + PRIORITY_OFFSET);
1879 msg->t.tcm_info = TC_H_MAKE(prio << 16, msg->t.tcm_info);
1880 msg->t.tcm_parent = TC_H_MAKE(MULTIQ_MAJOR_HANDLE, 0);
1882 tap_nlattr_add(&msg->nh, TCA_KIND, sizeof("bpf"), "bpf");
1883 if (tap_nlattr_nested_start(msg, TCA_OPTIONS) < 0)
1885 tap_nlattr_add32(&msg->nh, TCA_BPF_FD, pmd->bpf_fd[i]);
1886 snprintf(annotation, sizeof(annotation), "[%s%d]",
1888 tap_nlattr_add(&msg->nh, TCA_BPF_NAME, strlen(annotation) + 1,
1892 struct action_data adata = {
1896 .action = TC_ACT_PIPE,
1901 if (add_actions(rss_flow, 1, &adata, TCA_BPF_ACT) < 0)
1904 tap_nlattr_nested_finish(msg); /* nested TCA_OPTIONS */
1906 /* Netlink message is now ready to be sent */
1907 if (tap_nl_send(pmd->nlsk_fd, &msg->nh) < 0)
1909 err = tap_nl_recv_ack(pmd->nlsk_fd);
1912 "Kernel refused TC filter rule creation (%d): %s\n",
1913 errno, strerror(errno));
1916 LIST_INSERT_HEAD(&pmd->rss_flows, rss_flow, next);
1919 pmd->rss_enabled = 1;
1924 * Manage bpf RSS keys repository with operations: init, get, release
1927 * Command on RSS keys: init, get, release
1929 * @param[in, out] key_idx
1930 * Pointer to RSS Key index (out for get command, in for release command)
1932 * @return -1 if couldn't get, release or init the RSS keys, 0 otherwise.
1934 static int bpf_rss_key(enum bpf_rss_key_e cmd, __u32 *key_idx)
1938 static __u32 num_used_keys;
1939 static __u32 rss_keys[MAX_RSS_KEYS] = {KEY_STAT_UNSPEC};
1940 static __u32 rss_keys_initialized;
1944 if (!rss_keys_initialized)
1947 if (num_used_keys == RTE_DIM(rss_keys))
1950 *key_idx = num_used_keys % RTE_DIM(rss_keys);
1951 while (rss_keys[*key_idx] == KEY_STAT_USED)
1952 *key_idx = (*key_idx + 1) % RTE_DIM(rss_keys);
1954 rss_keys[*key_idx] = KEY_STAT_USED;
1959 case KEY_CMD_RELEASE:
1960 if (!rss_keys_initialized) {
1965 if (rss_keys[*key_idx] == KEY_STAT_USED) {
1966 rss_keys[*key_idx] = KEY_STAT_AVAILABLE;
1973 for (i = 0; i < RTE_DIM(rss_keys); i++)
1974 rss_keys[i] = KEY_STAT_AVAILABLE;
1976 rss_keys_initialized = 1;
1981 case KEY_CMD_DEINIT:
1982 for (i = 0; i < RTE_DIM(rss_keys); i++)
1983 rss_keys[i] = KEY_STAT_UNSPEC;
1985 rss_keys_initialized = 0;
1998 * Add RSS hash calculations and queue selection
2000 * @param[in, out] pmd
2001 * Pointer to internal structure. Used to set/get RSS map fd
2004 * Pointer to RSS flow actions
2007 * Pointer to error reporting if not NULL.
2009 * @return 0 on success, negative value on failure
2011 static int rss_add_actions(struct rte_flow *flow, struct pmd_internals *pmd,
2012 const struct rte_flow_action_rss *rss,
2013 struct rte_flow_error *error)
2015 /* 4096 is the maximum number of instructions for a BPF program */
2018 struct rss_key rss_entry = { .hash_fields = 0,
2021 /* Get a new map key for a new RSS rule */
2022 err = bpf_rss_key(KEY_CMD_GET, &flow->key_idx);
2025 error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2026 "Failed to get BPF RSS key");
2031 /* Update RSS map entry with queues */
2032 rss_entry.nb_queues = rss->num;
2033 for (i = 0; i < rss->num; i++)
2034 rss_entry.queues[i] = rss->queue[i];
2035 rss_entry.hash_fields =
2036 (1 << HASH_FIELD_IPV4_L3_L4) | (1 << HASH_FIELD_IPV6_L3_L4);
2038 /* Add this RSS entry to map */
2039 err = tap_flow_bpf_update_rss_elem(pmd->map_fd,
2040 &flow->key_idx, &rss_entry);
2044 "Failed to update BPF map entry #%u (%d): %s\n",
2045 flow->key_idx, errno, strerror(errno));
2047 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2048 "Kernel too old or not configured "
2049 "to support BPF maps updates");
2056 * Load bpf rules to calculate hash for this key_idx
2059 flow->bpf_fd[SEC_L3_L4] =
2060 tap_flow_bpf_calc_l3_l4_hash(flow->key_idx, pmd->map_fd);
2061 if (flow->bpf_fd[SEC_L3_L4] < 0) {
2063 "Failed to load BPF section %s (%d): %s\n",
2064 sec_name[SEC_L3_L4], errno, strerror(errno));
2066 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2067 "Kernel too old or not configured "
2068 "to support BPF program loading");
2075 struct action_data adata[] = {
2079 .bpf_fd = flow->bpf_fd[SEC_L3_L4],
2080 .annotation = sec_name[SEC_L3_L4],
2082 .action = TC_ACT_PIPE,
2088 if (add_actions(flow, RTE_DIM(adata), adata,
2089 TCA_FLOWER_ACT) < 0)
2097 * Manage filter operations.
2100 * Pointer to Ethernet device structure.
2101 * @param filter_type
2104 * Operation to perform.
2106 * Pointer to operation-specific structure.
2109 * 0 on success, negative errno value on failure.
2112 tap_dev_filter_ctrl(struct rte_eth_dev *dev,
2113 enum rte_filter_type filter_type,
2114 enum rte_filter_op filter_op,
2117 switch (filter_type) {
2118 case RTE_ETH_FILTER_GENERIC:
2119 if (filter_op != RTE_ETH_FILTER_GET)
2121 *(const void **)arg = &tap_flow_ops;
2124 RTE_LOG(ERR, PMD, "%p: filter type (%d) not supported\n",
2125 (void *)dev, filter_type);