1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox.
10 #include <sys/resource.h>
12 #include <rte_byteorder.h>
13 #include <rte_jhash.h>
14 #include <rte_malloc.h>
15 #include <rte_eth_tap.h>
17 #include <tap_autoconf.h>
18 #include <tap_tcmsgs.h>
21 #ifndef HAVE_TC_FLOWER
23 * For kernels < 4.2, this enum is not defined. Runtime checks will be made to
24 * avoid sending TC messages the kernel cannot understand.
31 TCA_FLOWER_KEY_ETH_DST, /* ETH_ALEN */
32 TCA_FLOWER_KEY_ETH_DST_MASK, /* ETH_ALEN */
33 TCA_FLOWER_KEY_ETH_SRC, /* ETH_ALEN */
34 TCA_FLOWER_KEY_ETH_SRC_MASK, /* ETH_ALEN */
35 TCA_FLOWER_KEY_ETH_TYPE, /* be16 */
36 TCA_FLOWER_KEY_IP_PROTO, /* u8 */
37 TCA_FLOWER_KEY_IPV4_SRC, /* be32 */
38 TCA_FLOWER_KEY_IPV4_SRC_MASK, /* be32 */
39 TCA_FLOWER_KEY_IPV4_DST, /* be32 */
40 TCA_FLOWER_KEY_IPV4_DST_MASK, /* be32 */
41 TCA_FLOWER_KEY_IPV6_SRC, /* struct in6_addr */
42 TCA_FLOWER_KEY_IPV6_SRC_MASK, /* struct in6_addr */
43 TCA_FLOWER_KEY_IPV6_DST, /* struct in6_addr */
44 TCA_FLOWER_KEY_IPV6_DST_MASK, /* struct in6_addr */
45 TCA_FLOWER_KEY_TCP_SRC, /* be16 */
46 TCA_FLOWER_KEY_TCP_DST, /* be16 */
47 TCA_FLOWER_KEY_UDP_SRC, /* be16 */
48 TCA_FLOWER_KEY_UDP_DST, /* be16 */
51 #ifndef HAVE_TC_VLAN_ID
53 /* TCA_FLOWER_FLAGS, */
54 TCA_FLOWER_KEY_VLAN_ID = TCA_FLOWER_KEY_UDP_DST + 2, /* be16 */
55 TCA_FLOWER_KEY_VLAN_PRIO, /* u8 */
56 TCA_FLOWER_KEY_VLAN_ETH_TYPE, /* be16 */
60 * For kernels < 4.2 BPF related enums may not be defined.
61 * Runtime checks will be carried out to gracefully report on TC messages that
62 * are rejected by the kernel. Rejection reasons may be due to:
63 * 1. enum is not defined
64 * 2. enum is defined but kernel is not configured to support BPF system calls,
65 * BPF classifications or BPF actions.
77 #ifndef HAVE_TC_BPF_FD
79 TCA_BPF_FD = TCA_BPF_OPS + 1,
83 #ifndef HAVE_TC_ACT_BPF
104 #ifndef HAVE_TC_ACT_BPF_FD
106 TCA_ACT_BPF_FD = TCA_ACT_BPF_OPS + 1,
111 /* RSS key management */
125 #define ISOLATE_HANDLE 1
128 LIST_ENTRY(rte_flow) next; /* Pointer to the next rte_flow structure */
129 struct rte_flow *remote_flow; /* associated remote flow */
130 int bpf_fd[SEC_MAX]; /* list of bfs fds per ELF section */
131 uint32_t key_idx; /* RSS rule key index into BPF map */
135 struct convert_data {
139 struct rte_flow *flow;
143 struct rte_flow_attr attr;
144 struct rte_flow_item items[2];
145 struct rte_flow_action actions[2];
154 struct tc_mirred mirred;
156 struct tc_skbedit skbedit;
160 struct tc_act_bpf bpf;
162 const char *annotation;
167 static int tap_flow_create_eth(const struct rte_flow_item *item, void *data);
168 static int tap_flow_create_vlan(const struct rte_flow_item *item, void *data);
169 static int tap_flow_create_ipv4(const struct rte_flow_item *item, void *data);
170 static int tap_flow_create_ipv6(const struct rte_flow_item *item, void *data);
171 static int tap_flow_create_udp(const struct rte_flow_item *item, void *data);
172 static int tap_flow_create_tcp(const struct rte_flow_item *item, void *data);
174 tap_flow_validate(struct rte_eth_dev *dev,
175 const struct rte_flow_attr *attr,
176 const struct rte_flow_item items[],
177 const struct rte_flow_action actions[],
178 struct rte_flow_error *error);
180 static struct rte_flow *
181 tap_flow_create(struct rte_eth_dev *dev,
182 const struct rte_flow_attr *attr,
183 const struct rte_flow_item items[],
184 const struct rte_flow_action actions[],
185 struct rte_flow_error *error);
188 tap_flow_free(struct pmd_internals *pmd,
189 struct rte_flow *flow);
192 tap_flow_destroy(struct rte_eth_dev *dev,
193 struct rte_flow *flow,
194 struct rte_flow_error *error);
197 tap_flow_isolate(struct rte_eth_dev *dev,
199 struct rte_flow_error *error);
201 static int bpf_rss_key(enum bpf_rss_key_e cmd, __u32 *key_idx);
202 static int rss_enable(struct pmd_internals *pmd,
203 const struct rte_flow_attr *attr,
204 struct rte_flow_error *error);
205 static int rss_add_actions(struct rte_flow *flow, struct pmd_internals *pmd,
206 const struct rte_flow_action_rss *rss,
207 struct rte_flow_error *error);
209 static const struct rte_flow_ops tap_flow_ops = {
210 .validate = tap_flow_validate,
211 .create = tap_flow_create,
212 .destroy = tap_flow_destroy,
213 .flush = tap_flow_flush,
214 .isolate = tap_flow_isolate,
217 /* Static initializer for items. */
219 (const enum rte_flow_item_type []){ \
220 __VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
223 /* Structure to generate a simple graph of layers supported by the NIC. */
224 struct tap_flow_items {
225 /* Bit-mask corresponding to what is supported for this item. */
227 const unsigned int mask_sz; /* Bit-mask size in bytes. */
229 * Bit-mask corresponding to the default mask, if none is provided
230 * along with the item.
232 const void *default_mask;
234 * Conversion function from rte_flow to netlink attributes.
237 * rte_flow item to convert.
239 * Internal structure to store the conversion.
242 * 0 on success, negative value otherwise.
244 int (*convert)(const struct rte_flow_item *item, void *data);
245 /** List of possible following items. */
246 const enum rte_flow_item_type *const items;
249 /* Graph of supported items and associated actions. */
250 static const struct tap_flow_items tap_flow_items[] = {
251 [RTE_FLOW_ITEM_TYPE_END] = {
252 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH),
254 [RTE_FLOW_ITEM_TYPE_ETH] = {
256 RTE_FLOW_ITEM_TYPE_VLAN,
257 RTE_FLOW_ITEM_TYPE_IPV4,
258 RTE_FLOW_ITEM_TYPE_IPV6),
259 .mask = &(const struct rte_flow_item_eth){
260 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
261 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
264 .mask_sz = sizeof(struct rte_flow_item_eth),
265 .default_mask = &rte_flow_item_eth_mask,
266 .convert = tap_flow_create_eth,
268 [RTE_FLOW_ITEM_TYPE_VLAN] = {
269 .items = ITEMS(RTE_FLOW_ITEM_TYPE_IPV4,
270 RTE_FLOW_ITEM_TYPE_IPV6),
271 .mask = &(const struct rte_flow_item_vlan){
273 /* DEI matching is not supported */
274 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
280 .mask_sz = sizeof(struct rte_flow_item_vlan),
281 .default_mask = &rte_flow_item_vlan_mask,
282 .convert = tap_flow_create_vlan,
284 [RTE_FLOW_ITEM_TYPE_IPV4] = {
285 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
286 RTE_FLOW_ITEM_TYPE_TCP),
287 .mask = &(const struct rte_flow_item_ipv4){
294 .mask_sz = sizeof(struct rte_flow_item_ipv4),
295 .default_mask = &rte_flow_item_ipv4_mask,
296 .convert = tap_flow_create_ipv4,
298 [RTE_FLOW_ITEM_TYPE_IPV6] = {
299 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
300 RTE_FLOW_ITEM_TYPE_TCP),
301 .mask = &(const struct rte_flow_item_ipv6){
304 "\xff\xff\xff\xff\xff\xff\xff\xff"
305 "\xff\xff\xff\xff\xff\xff\xff\xff",
308 "\xff\xff\xff\xff\xff\xff\xff\xff"
309 "\xff\xff\xff\xff\xff\xff\xff\xff",
314 .mask_sz = sizeof(struct rte_flow_item_ipv6),
315 .default_mask = &rte_flow_item_ipv6_mask,
316 .convert = tap_flow_create_ipv6,
318 [RTE_FLOW_ITEM_TYPE_UDP] = {
319 .mask = &(const struct rte_flow_item_udp){
325 .mask_sz = sizeof(struct rte_flow_item_udp),
326 .default_mask = &rte_flow_item_udp_mask,
327 .convert = tap_flow_create_udp,
329 [RTE_FLOW_ITEM_TYPE_TCP] = {
330 .mask = &(const struct rte_flow_item_tcp){
336 .mask_sz = sizeof(struct rte_flow_item_tcp),
337 .default_mask = &rte_flow_item_tcp_mask,
338 .convert = tap_flow_create_tcp,
343 * TC rules, by growing priority
345 * Remote netdevice Tap netdevice
346 * +-------------+-------------+ +-------------+-------------+
347 * | Ingress | Egress | | Ingress | Egress |
348 * |-------------|-------------| |-------------|-------------|
349 * | | \ / | | | REMOTE TX | prio 1
350 * | | \ / | | | \ / | prio 2
351 * | EXPLICIT | \ / | | EXPLICIT | \ / | .
352 * | | \ / | | | \ / | .
353 * | RULES | X | | RULES | X | .
354 * | . | / \ | | . | / \ | .
355 * | . | / \ | | . | / \ | .
356 * | . | / \ | | . | / \ | .
357 * | . | / \ | | . | / \ | .
359 * .... .... .... ....
361 * | . | \ / | | . | \ / | .
362 * | . | \ / | | . | \ / | .
363 * | | \ / | | | \ / |
364 * | LOCAL_MAC | \ / | | \ / | \ / | last prio - 5
365 * | PROMISC | X | | \ / | X | last prio - 4
366 * | ALLMULTI | / \ | | X | / \ | last prio - 3
367 * | BROADCAST | / \ | | / \ | / \ | last prio - 2
368 * | BROADCASTV6 | / \ | | / \ | / \ | last prio - 1
369 * | xx | / \ | | ISOLATE | / \ | last prio
370 * +-------------+-------------+ +-------------+-------------+
372 * The implicit flow rules are stored in a list in with mandatorily the last two
373 * being the ISOLATE and REMOTE_TX rules. e.g.:
375 * LOCAL_MAC -> BROADCAST -> BROADCASTV6 -> REMOTE_TX -> ISOLATE -> NULL
377 * That enables tap_flow_isolate() to remove implicit rules by popping the list
378 * head and remove it as long as it applies on the remote netdevice. The
379 * implicit rule for TX redirection is not removed, as isolate concerns only
383 static struct remote_rule implicit_rte_flows[TAP_REMOTE_MAX_IDX] = {
384 [TAP_REMOTE_LOCAL_MAC] = {
387 .priority = PRIORITY_MASK - TAP_REMOTE_LOCAL_MAC,
391 .type = RTE_FLOW_ITEM_TYPE_ETH,
392 .mask = &(const struct rte_flow_item_eth){
393 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
397 .type = RTE_FLOW_ITEM_TYPE_END,
399 .mirred = TCA_EGRESS_REDIR,
401 [TAP_REMOTE_BROADCAST] = {
404 .priority = PRIORITY_MASK - TAP_REMOTE_BROADCAST,
408 .type = RTE_FLOW_ITEM_TYPE_ETH,
409 .mask = &(const struct rte_flow_item_eth){
410 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
412 .spec = &(const struct rte_flow_item_eth){
413 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
417 .type = RTE_FLOW_ITEM_TYPE_END,
419 .mirred = TCA_EGRESS_MIRROR,
421 [TAP_REMOTE_BROADCASTV6] = {
424 .priority = PRIORITY_MASK - TAP_REMOTE_BROADCASTV6,
428 .type = RTE_FLOW_ITEM_TYPE_ETH,
429 .mask = &(const struct rte_flow_item_eth){
430 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
432 .spec = &(const struct rte_flow_item_eth){
433 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
437 .type = RTE_FLOW_ITEM_TYPE_END,
439 .mirred = TCA_EGRESS_MIRROR,
441 [TAP_REMOTE_PROMISC] = {
444 .priority = PRIORITY_MASK - TAP_REMOTE_PROMISC,
448 .type = RTE_FLOW_ITEM_TYPE_VOID,
451 .type = RTE_FLOW_ITEM_TYPE_END,
453 .mirred = TCA_EGRESS_MIRROR,
455 [TAP_REMOTE_ALLMULTI] = {
458 .priority = PRIORITY_MASK - TAP_REMOTE_ALLMULTI,
462 .type = RTE_FLOW_ITEM_TYPE_ETH,
463 .mask = &(const struct rte_flow_item_eth){
464 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
466 .spec = &(const struct rte_flow_item_eth){
467 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
471 .type = RTE_FLOW_ITEM_TYPE_END,
473 .mirred = TCA_EGRESS_MIRROR,
478 .priority = TAP_REMOTE_TX,
482 .type = RTE_FLOW_ITEM_TYPE_VOID,
485 .type = RTE_FLOW_ITEM_TYPE_END,
487 .mirred = TCA_EGRESS_MIRROR,
492 .priority = PRIORITY_MASK - TAP_ISOLATE,
496 .type = RTE_FLOW_ITEM_TYPE_VOID,
499 .type = RTE_FLOW_ITEM_TYPE_END,
505 * Make as much checks as possible on an Ethernet item, and if a flow is
506 * provided, fill it appropriately with Ethernet info.
509 * Item specification.
510 * @param[in, out] data
511 * Additional data structure to tell next layers we've been here.
514 * 0 if checks are alright, -1 otherwise.
517 tap_flow_create_eth(const struct rte_flow_item *item, void *data)
519 struct convert_data *info = (struct convert_data *)data;
520 const struct rte_flow_item_eth *spec = item->spec;
521 const struct rte_flow_item_eth *mask = item->mask;
522 struct rte_flow *flow = info->flow;
525 /* use default mask if none provided */
527 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_ETH].default_mask;
528 /* TC does not support eth_type masking. Only accept if exact match. */
529 if (mask->type && mask->type != 0xffff)
533 /* store eth_type for consistency if ipv4/6 pattern item comes next */
534 if (spec->type & mask->type)
535 info->eth_type = spec->type;
539 if (!is_zero_ether_addr(&spec->dst)) {
540 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_ETH_DST, ETHER_ADDR_LEN,
541 &spec->dst.addr_bytes);
542 tap_nlattr_add(&msg->nh,
543 TCA_FLOWER_KEY_ETH_DST_MASK, ETHER_ADDR_LEN,
544 &mask->dst.addr_bytes);
546 if (!is_zero_ether_addr(&mask->src)) {
547 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_ETH_SRC, ETHER_ADDR_LEN,
548 &spec->src.addr_bytes);
549 tap_nlattr_add(&msg->nh,
550 TCA_FLOWER_KEY_ETH_SRC_MASK, ETHER_ADDR_LEN,
551 &mask->src.addr_bytes);
557 * Make as much checks as possible on a VLAN item, and if a flow is provided,
558 * fill it appropriately with VLAN info.
561 * Item specification.
562 * @param[in, out] data
563 * Additional data structure to tell next layers we've been here.
566 * 0 if checks are alright, -1 otherwise.
569 tap_flow_create_vlan(const struct rte_flow_item *item, void *data)
571 struct convert_data *info = (struct convert_data *)data;
572 const struct rte_flow_item_vlan *spec = item->spec;
573 const struct rte_flow_item_vlan *mask = item->mask;
574 struct rte_flow *flow = info->flow;
577 /* use default mask if none provided */
579 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_VLAN].default_mask;
580 /* TC does not support tpid masking. Only accept if exact match. */
581 if (mask->tpid && mask->tpid != 0xffff)
583 /* Double-tagging not supported. */
584 if (spec && mask->tpid && spec->tpid != htons(ETH_P_8021Q))
590 msg->t.tcm_info = TC_H_MAKE(msg->t.tcm_info, htons(ETH_P_8021Q));
591 #define VLAN_PRIO(tci) ((tci) >> 13)
592 #define VLAN_ID(tci) ((tci) & 0xfff)
596 uint16_t tci = ntohs(spec->tci) & mask->tci;
597 uint16_t prio = VLAN_PRIO(tci);
598 uint8_t vid = VLAN_ID(tci);
601 tap_nlattr_add8(&msg->nh,
602 TCA_FLOWER_KEY_VLAN_PRIO, prio);
604 tap_nlattr_add16(&msg->nh,
605 TCA_FLOWER_KEY_VLAN_ID, vid);
611 * Make as much checks as possible on an IPv4 item, and if a flow is provided,
612 * fill it appropriately with IPv4 info.
615 * Item specification.
616 * @param[in, out] data
617 * Additional data structure to tell next layers we've been here.
620 * 0 if checks are alright, -1 otherwise.
623 tap_flow_create_ipv4(const struct rte_flow_item *item, void *data)
625 struct convert_data *info = (struct convert_data *)data;
626 const struct rte_flow_item_ipv4 *spec = item->spec;
627 const struct rte_flow_item_ipv4 *mask = item->mask;
628 struct rte_flow *flow = info->flow;
631 /* use default mask if none provided */
633 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_IPV4].default_mask;
634 /* check that previous eth type is compatible with ipv4 */
635 if (info->eth_type && info->eth_type != htons(ETH_P_IP))
637 /* store ip_proto for consistency if udp/tcp pattern item comes next */
639 info->ip_proto = spec->hdr.next_proto_id;
644 info->eth_type = htons(ETH_P_IP);
647 if (spec->hdr.dst_addr) {
648 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_DST,
650 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_DST_MASK,
653 if (spec->hdr.src_addr) {
654 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_SRC,
656 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_SRC_MASK,
659 if (spec->hdr.next_proto_id)
660 tap_nlattr_add8(&msg->nh, TCA_FLOWER_KEY_IP_PROTO,
661 spec->hdr.next_proto_id);
666 * Make as much checks as possible on an IPv6 item, and if a flow is provided,
667 * fill it appropriately with IPv6 info.
670 * Item specification.
671 * @param[in, out] data
672 * Additional data structure to tell next layers we've been here.
675 * 0 if checks are alright, -1 otherwise.
678 tap_flow_create_ipv6(const struct rte_flow_item *item, void *data)
680 struct convert_data *info = (struct convert_data *)data;
681 const struct rte_flow_item_ipv6 *spec = item->spec;
682 const struct rte_flow_item_ipv6 *mask = item->mask;
683 struct rte_flow *flow = info->flow;
684 uint8_t empty_addr[16] = { 0 };
687 /* use default mask if none provided */
689 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_IPV6].default_mask;
690 /* check that previous eth type is compatible with ipv6 */
691 if (info->eth_type && info->eth_type != htons(ETH_P_IPV6))
693 /* store ip_proto for consistency if udp/tcp pattern item comes next */
695 info->ip_proto = spec->hdr.proto;
700 info->eth_type = htons(ETH_P_IPV6);
703 if (memcmp(spec->hdr.dst_addr, empty_addr, 16)) {
704 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_DST,
705 sizeof(spec->hdr.dst_addr), &spec->hdr.dst_addr);
706 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_DST_MASK,
707 sizeof(mask->hdr.dst_addr), &mask->hdr.dst_addr);
709 if (memcmp(spec->hdr.src_addr, empty_addr, 16)) {
710 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_SRC,
711 sizeof(spec->hdr.src_addr), &spec->hdr.src_addr);
712 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_SRC_MASK,
713 sizeof(mask->hdr.src_addr), &mask->hdr.src_addr);
716 tap_nlattr_add8(&msg->nh,
717 TCA_FLOWER_KEY_IP_PROTO, spec->hdr.proto);
722 * Make as much checks as possible on a UDP item, and if a flow is provided,
723 * fill it appropriately with UDP info.
726 * Item specification.
727 * @param[in, out] data
728 * Additional data structure to tell next layers we've been here.
731 * 0 if checks are alright, -1 otherwise.
734 tap_flow_create_udp(const struct rte_flow_item *item, void *data)
736 struct convert_data *info = (struct convert_data *)data;
737 const struct rte_flow_item_udp *spec = item->spec;
738 const struct rte_flow_item_udp *mask = item->mask;
739 struct rte_flow *flow = info->flow;
742 /* use default mask if none provided */
744 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_UDP].default_mask;
745 /* check that previous ip_proto is compatible with udp */
746 if (info->ip_proto && info->ip_proto != IPPROTO_UDP)
748 /* TC does not support UDP port masking. Only accept if exact match. */
749 if ((mask->hdr.src_port && mask->hdr.src_port != 0xffff) ||
750 (mask->hdr.dst_port && mask->hdr.dst_port != 0xffff))
755 tap_nlattr_add8(&msg->nh, TCA_FLOWER_KEY_IP_PROTO, IPPROTO_UDP);
758 if (spec->hdr.dst_port & mask->hdr.dst_port)
759 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_UDP_DST,
761 if (spec->hdr.src_port & mask->hdr.src_port)
762 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_UDP_SRC,
768 * Make as much checks as possible on a TCP item, and if a flow is provided,
769 * fill it appropriately with TCP info.
772 * Item specification.
773 * @param[in, out] data
774 * Additional data structure to tell next layers we've been here.
777 * 0 if checks are alright, -1 otherwise.
780 tap_flow_create_tcp(const struct rte_flow_item *item, void *data)
782 struct convert_data *info = (struct convert_data *)data;
783 const struct rte_flow_item_tcp *spec = item->spec;
784 const struct rte_flow_item_tcp *mask = item->mask;
785 struct rte_flow *flow = info->flow;
788 /* use default mask if none provided */
790 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_TCP].default_mask;
791 /* check that previous ip_proto is compatible with tcp */
792 if (info->ip_proto && info->ip_proto != IPPROTO_TCP)
794 /* TC does not support TCP port masking. Only accept if exact match. */
795 if ((mask->hdr.src_port && mask->hdr.src_port != 0xffff) ||
796 (mask->hdr.dst_port && mask->hdr.dst_port != 0xffff))
801 tap_nlattr_add8(&msg->nh, TCA_FLOWER_KEY_IP_PROTO, IPPROTO_TCP);
804 if (spec->hdr.dst_port & mask->hdr.dst_port)
805 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_TCP_DST,
807 if (spec->hdr.src_port & mask->hdr.src_port)
808 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_TCP_SRC,
814 * Check support for a given item.
817 * Item specification.
819 * Bit-Mask size in bytes.
820 * @param[in] supported_mask
821 * Bit-mask covering supported fields to compare with spec, last and mask in
823 * @param[in] default_mask
824 * Bit-mask default mask if none is provided in \item.
830 tap_flow_item_validate(const struct rte_flow_item *item,
832 const uint8_t *supported_mask,
833 const uint8_t *default_mask)
837 /* An empty layer is allowed, as long as all fields are NULL */
838 if (!item->spec && (item->mask || item->last))
840 /* Is the item spec compatible with what the NIC supports? */
841 if (item->spec && !item->mask) {
843 const uint8_t *spec = item->spec;
845 for (i = 0; i < size; ++i)
846 if ((spec[i] | supported_mask[i]) != supported_mask[i])
848 /* Is the default mask compatible with what the NIC supports? */
849 for (i = 0; i < size; i++)
850 if ((default_mask[i] | supported_mask[i]) !=
854 /* Is the item last compatible with what the NIC supports? */
855 if (item->last && !item->mask) {
857 const uint8_t *spec = item->last;
859 for (i = 0; i < size; ++i)
860 if ((spec[i] | supported_mask[i]) != supported_mask[i])
863 /* Is the item mask compatible with what the NIC supports? */
866 const uint8_t *spec = item->mask;
868 for (i = 0; i < size; ++i)
869 if ((spec[i] | supported_mask[i]) != supported_mask[i])
873 * Once masked, Are item spec and item last equal?
874 * TC does not support range so anything else is invalid.
876 if (item->spec && item->last) {
879 const uint8_t *apply = default_mask;
884 for (i = 0; i < size; ++i) {
885 spec[i] = ((const uint8_t *)item->spec)[i] & apply[i];
886 last[i] = ((const uint8_t *)item->last)[i] & apply[i];
888 ret = memcmp(spec, last, size);
894 * Configure the kernel with a TC action and its configured parameters
895 * Handled actions: "gact", "mirred", "skbedit", "bpf"
898 * Pointer to rte flow containing the netlink message
900 * @param[in, out] act_index
901 * Pointer to action sequence number in the TC command
904 * Pointer to struct holding the action parameters
907 * -1 on failure, 0 on success
910 add_action(struct rte_flow *flow, size_t *act_index, struct action_data *adata)
912 struct nlmsg *msg = &flow->msg;
914 if (tap_nlattr_nested_start(msg, (*act_index)++) < 0)
917 tap_nlattr_add(&msg->nh, TCA_ACT_KIND,
918 strlen(adata->id) + 1, adata->id);
919 if (tap_nlattr_nested_start(msg, TCA_ACT_OPTIONS) < 0)
921 if (strcmp("gact", adata->id) == 0) {
922 tap_nlattr_add(&msg->nh, TCA_GACT_PARMS, sizeof(adata->gact),
924 } else if (strcmp("mirred", adata->id) == 0) {
925 if (adata->mirred.eaction == TCA_EGRESS_MIRROR)
926 adata->mirred.action = TC_ACT_PIPE;
928 adata->mirred.action = TC_ACT_STOLEN;
929 tap_nlattr_add(&msg->nh, TCA_MIRRED_PARMS,
930 sizeof(adata->mirred),
932 } else if (strcmp("skbedit", adata->id) == 0) {
933 tap_nlattr_add(&msg->nh, TCA_SKBEDIT_PARMS,
934 sizeof(adata->skbedit.skbedit),
935 &adata->skbedit.skbedit);
936 tap_nlattr_add16(&msg->nh, TCA_SKBEDIT_QUEUE_MAPPING,
937 adata->skbedit.queue);
938 } else if (strcmp("bpf", adata->id) == 0) {
939 tap_nlattr_add32(&msg->nh, TCA_ACT_BPF_FD, adata->bpf.bpf_fd);
940 tap_nlattr_add(&msg->nh, TCA_ACT_BPF_NAME,
941 strlen(adata->bpf.annotation) + 1,
942 adata->bpf.annotation);
943 tap_nlattr_add(&msg->nh, TCA_ACT_BPF_PARMS,
944 sizeof(adata->bpf.bpf),
949 tap_nlattr_nested_finish(msg); /* nested TCA_ACT_OPTIONS */
950 tap_nlattr_nested_finish(msg); /* nested act_index */
955 * Helper function to send a serie of TC actions to the kernel
958 * Pointer to rte flow containing the netlink message
960 * @param[in] nb_actions
961 * Number of actions in an array of action structs
964 * Pointer to an array of action structs
966 * @param[in] classifier_actions
967 * The classifier on behave of which the actions are configured
970 * -1 on failure, 0 on success
973 add_actions(struct rte_flow *flow, int nb_actions, struct action_data *data,
974 int classifier_action)
976 struct nlmsg *msg = &flow->msg;
977 size_t act_index = 1;
980 if (tap_nlattr_nested_start(msg, classifier_action) < 0)
982 for (i = 0; i < nb_actions; i++)
983 if (add_action(flow, &act_index, data + i) < 0)
985 tap_nlattr_nested_finish(msg); /* nested TCA_FLOWER_ACT */
990 * Validate a flow supported by TC.
991 * If flow param is not NULL, then also fill the netlink message inside.
994 * Pointer to private structure.
996 * Flow rule attributes.
998 * Pattern specification (list terminated by the END pattern item).
1000 * Associated actions (list terminated by the END action).
1002 * Perform verbose error reporting if not NULL.
1003 * @param[in, out] flow
1004 * Flow structure to update.
1006 * If set to TCA_EGRESS_REDIR, provided actions will be replaced with a
1007 * redirection to the tap netdevice, and the TC rule will be configured
1008 * on the remote netdevice in pmd.
1009 * If set to TCA_EGRESS_MIRROR, provided actions will be replaced with a
1010 * mirroring to the tap netdevice, and the TC rule will be configured
1011 * on the remote netdevice in pmd. Matching packets will thus be duplicated.
1012 * If set to 0, the standard behavior is to be used: set correct actions for
1013 * the TC rule, and apply it on the tap netdevice.
1016 * 0 on success, a negative errno value otherwise and rte_errno is set.
1019 priv_flow_process(struct pmd_internals *pmd,
1020 const struct rte_flow_attr *attr,
1021 const struct rte_flow_item items[],
1022 const struct rte_flow_action actions[],
1023 struct rte_flow_error *error,
1024 struct rte_flow *flow,
1027 const struct tap_flow_items *cur_item = tap_flow_items;
1028 struct convert_data data = {
1033 int action = 0; /* Only one action authorized for now */
1035 if (attr->group > MAX_GROUP) {
1037 error, EINVAL, RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1038 NULL, "group value too big: cannot exceed 15");
1041 if (attr->priority > MAX_PRIORITY) {
1043 error, EINVAL, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1044 NULL, "priority value too big");
1047 uint16_t group = attr->group << GROUP_SHIFT;
1048 uint16_t prio = group | (attr->priority +
1049 RSS_PRIORITY_OFFSET + PRIORITY_OFFSET);
1050 flow->msg.t.tcm_info = TC_H_MAKE(prio << 16,
1051 flow->msg.t.tcm_info);
1056 * If attr->ingress, the rule applies on remote ingress
1057 * to match incoming packets
1058 * If attr->egress, the rule applies on tap ingress (as
1059 * seen from the kernel) to deal with packets going out
1060 * from the DPDK app.
1062 flow->msg.t.tcm_parent = TC_H_MAKE(TC_H_INGRESS, 0);
1064 /* Standard rule on tap egress (kernel standpoint). */
1065 flow->msg.t.tcm_parent =
1066 TC_H_MAKE(MULTIQ_MAJOR_HANDLE, 0);
1068 /* use flower filter type */
1069 tap_nlattr_add(&flow->msg.nh, TCA_KIND, sizeof("flower"), "flower");
1070 if (tap_nlattr_nested_start(&flow->msg, TCA_OPTIONS) < 0)
1071 goto exit_item_not_supported;
1073 for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
1074 const struct tap_flow_items *token = NULL;
1078 if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
1082 cur_item->items[i] != RTE_FLOW_ITEM_TYPE_END;
1084 if (cur_item->items[i] == items->type) {
1085 token = &tap_flow_items[items->type];
1090 goto exit_item_not_supported;
1092 err = tap_flow_item_validate(
1093 items, cur_item->mask_sz,
1094 (const uint8_t *)cur_item->mask,
1095 (const uint8_t *)cur_item->default_mask);
1097 goto exit_item_not_supported;
1098 if (flow && cur_item->convert) {
1099 err = cur_item->convert(items, &data);
1101 goto exit_item_not_supported;
1106 tap_nlattr_add16(&flow->msg.nh, TCA_FLOWER_KEY_ETH_TYPE,
1107 htons(ETH_P_8021Q));
1108 tap_nlattr_add16(&flow->msg.nh,
1109 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1111 data.eth_type : htons(ETH_P_ALL));
1112 } else if (data.eth_type) {
1113 tap_nlattr_add16(&flow->msg.nh, TCA_FLOWER_KEY_ETH_TYPE,
1117 if (mirred && flow) {
1118 struct action_data adata = {
1126 * If attr->egress && mirred, then this is a special
1127 * case where the rule must be applied on the tap, to
1128 * redirect packets coming from the DPDK App, out
1129 * through the remote netdevice.
1131 adata.mirred.ifindex = attr->ingress ? pmd->if_index :
1132 pmd->remote_if_index;
1133 if (mirred == TCA_EGRESS_MIRROR)
1134 adata.mirred.action = TC_ACT_PIPE;
1136 adata.mirred.action = TC_ACT_STOLEN;
1137 if (add_actions(flow, 1, &adata, TCA_FLOWER_ACT) < 0)
1138 goto exit_action_not_supported;
1142 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
1145 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID) {
1147 } else if (actions->type == RTE_FLOW_ACTION_TYPE_DROP) {
1149 goto exit_action_not_supported;
1152 struct action_data adata = {
1155 .action = TC_ACT_SHOT,
1159 err = add_actions(flow, 1, &adata,
1162 } else if (actions->type == RTE_FLOW_ACTION_TYPE_PASSTHRU) {
1164 goto exit_action_not_supported;
1167 struct action_data adata = {
1171 .action = TC_ACT_UNSPEC,
1175 err = add_actions(flow, 1, &adata,
1178 } else if (actions->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
1179 const struct rte_flow_action_queue *queue =
1180 (const struct rte_flow_action_queue *)
1184 goto exit_action_not_supported;
1187 (queue->index > pmd->dev->data->nb_rx_queues - 1))
1188 goto exit_action_not_supported;
1190 struct action_data adata = {
1194 .action = TC_ACT_PIPE,
1196 .queue = queue->index,
1200 err = add_actions(flow, 1, &adata,
1203 } else if (actions->type == RTE_FLOW_ACTION_TYPE_RSS) {
1204 const struct rte_flow_action_rss *rss =
1205 (const struct rte_flow_action_rss *)
1209 goto exit_action_not_supported;
1211 if (!pmd->rss_enabled) {
1212 err = rss_enable(pmd, attr, error);
1214 goto exit_action_not_supported;
1217 err = rss_add_actions(flow, pmd, rss, error);
1219 goto exit_action_not_supported;
1222 goto exit_action_not_supported;
1226 tap_nlattr_nested_finish(&flow->msg); /* nested TCA_OPTIONS */
1228 exit_item_not_supported:
1229 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1230 items, "item not supported");
1232 exit_action_not_supported:
1233 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
1234 actions, "action not supported");
1243 * @see rte_flow_validate()
1247 tap_flow_validate(struct rte_eth_dev *dev,
1248 const struct rte_flow_attr *attr,
1249 const struct rte_flow_item items[],
1250 const struct rte_flow_action actions[],
1251 struct rte_flow_error *error)
1253 struct pmd_internals *pmd = dev->data->dev_private;
1255 return priv_flow_process(pmd, attr, items, actions, error, NULL, 0);
1259 * Set a unique handle in a flow.
1261 * The kernel supports TC rules with equal priority, as long as they use the
1262 * same matching fields (e.g.: dst mac and ipv4) with different values (and
1263 * full mask to ensure no collision is possible).
1264 * In those rules, the handle (uint32_t) is the part that would identify
1265 * specifically each rule.
1267 * On 32-bit architectures, the handle can simply be the flow's pointer address.
1268 * On 64-bit architectures, we rely on jhash(flow) to find a (sufficiently)
1271 * @param[in, out] flow
1272 * The flow that needs its handle set.
1275 tap_flow_set_handle(struct rte_flow *flow)
1277 uint32_t handle = 0;
1279 if (sizeof(flow) > 4)
1280 handle = rte_jhash(&flow, sizeof(flow), 1);
1282 handle = (uintptr_t)flow;
1283 /* must be at least 1 to avoid letting the kernel choose one for us */
1286 flow->msg.t.tcm_handle = handle;
1290 * Free the flow opened file descriptors and allocated memory
1293 * Pointer to the flow to free
1297 tap_flow_free(struct pmd_internals *pmd, struct rte_flow *flow)
1304 if (pmd->rss_enabled) {
1305 /* Close flow BPF file descriptors */
1306 for (i = 0; i < SEC_MAX; i++)
1307 if (flow->bpf_fd[i] != 0) {
1308 close(flow->bpf_fd[i]);
1309 flow->bpf_fd[i] = 0;
1312 /* Release the map key for this RSS rule */
1313 bpf_rss_key(KEY_CMD_RELEASE, &flow->key_idx);
1317 /* Free flow allocated memory */
1324 * @see rte_flow_create()
1327 static struct rte_flow *
1328 tap_flow_create(struct rte_eth_dev *dev,
1329 const struct rte_flow_attr *attr,
1330 const struct rte_flow_item items[],
1331 const struct rte_flow_action actions[],
1332 struct rte_flow_error *error)
1334 struct pmd_internals *pmd = dev->data->dev_private;
1335 struct rte_flow *remote_flow = NULL;
1336 struct rte_flow *flow = NULL;
1337 struct nlmsg *msg = NULL;
1340 if (!pmd->if_index) {
1341 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1343 "can't create rule, ifindex not found");
1347 * No rules configured through standard rte_flow should be set on the
1348 * priorities used by implicit rules.
1350 if ((attr->group == MAX_GROUP) &&
1351 attr->priority > (MAX_PRIORITY - TAP_REMOTE_MAX_IDX)) {
1353 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1354 NULL, "priority value too big");
1357 flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1359 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1360 NULL, "cannot allocate memory for rte_flow");
1364 tc_init_msg(msg, pmd->if_index, RTM_NEWTFILTER,
1365 NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
1366 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1367 tap_flow_set_handle(flow);
1368 if (priv_flow_process(pmd, attr, items, actions, error, flow, 0))
1370 err = tap_nl_send(pmd->nlsk_fd, &msg->nh);
1372 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1373 NULL, "couldn't send request to kernel");
1376 err = tap_nl_recv_ack(pmd->nlsk_fd);
1379 "Kernel refused TC filter rule creation (%d): %s\n",
1380 errno, strerror(errno));
1381 rte_flow_error_set(error, EEXIST, RTE_FLOW_ERROR_TYPE_HANDLE,
1383 "overlapping rules or Kernel too old for flower support");
1386 LIST_INSERT_HEAD(&pmd->flows, flow, next);
1388 * If a remote device is configured, a TC rule with identical items for
1389 * matching must be set on that device, with a single action: redirect
1390 * to the local pmd->if_index.
1392 if (pmd->remote_if_index) {
1393 remote_flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1396 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1397 "cannot allocate memory for rte_flow");
1400 msg = &remote_flow->msg;
1401 /* set the rule if_index for the remote netdevice */
1403 msg, pmd->remote_if_index, RTM_NEWTFILTER,
1404 NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
1405 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1406 tap_flow_set_handle(remote_flow);
1407 if (priv_flow_process(pmd, attr, items, NULL,
1408 error, remote_flow, TCA_EGRESS_REDIR)) {
1410 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1411 NULL, "rte flow rule validation failed");
1414 err = tap_nl_send(pmd->nlsk_fd, &msg->nh);
1417 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1418 NULL, "Failure sending nl request");
1421 err = tap_nl_recv_ack(pmd->nlsk_fd);
1424 "Kernel refused TC filter rule creation (%d): %s\n",
1425 errno, strerror(errno));
1427 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1429 "overlapping rules or Kernel too old for flower support");
1432 flow->remote_flow = remote_flow;
1437 rte_free(remote_flow);
1439 tap_flow_free(pmd, flow);
1444 * Destroy a flow using pointer to pmd_internal.
1446 * @param[in, out] pmd
1447 * Pointer to private structure.
1449 * Pointer to the flow to destroy.
1450 * @param[in, out] error
1451 * Pointer to the flow error handler
1453 * @return 0 if the flow could be destroyed, -1 otherwise.
1456 tap_flow_destroy_pmd(struct pmd_internals *pmd,
1457 struct rte_flow *flow,
1458 struct rte_flow_error *error)
1460 struct rte_flow *remote_flow = flow->remote_flow;
1463 LIST_REMOVE(flow, next);
1464 flow->msg.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1465 flow->msg.nh.nlmsg_type = RTM_DELTFILTER;
1467 ret = tap_nl_send(pmd->nlsk_fd, &flow->msg.nh);
1469 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1470 NULL, "couldn't send request to kernel");
1473 ret = tap_nl_recv_ack(pmd->nlsk_fd);
1474 /* If errno is ENOENT, the rule is already no longer in the kernel. */
1475 if (ret < 0 && errno == ENOENT)
1479 "Kernel refused TC filter rule deletion (%d): %s\n",
1480 errno, strerror(errno));
1482 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1483 "couldn't receive kernel ack to our request");
1488 remote_flow->msg.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1489 remote_flow->msg.nh.nlmsg_type = RTM_DELTFILTER;
1491 ret = tap_nl_send(pmd->nlsk_fd, &remote_flow->msg.nh);
1494 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1495 NULL, "Failure sending nl request");
1498 ret = tap_nl_recv_ack(pmd->nlsk_fd);
1499 if (ret < 0 && errno == ENOENT)
1503 "Kernel refused TC filter rule deletion (%d): %s\n",
1504 errno, strerror(errno));
1506 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1507 NULL, "Failure trying to receive nl ack");
1513 rte_free(remote_flow);
1514 tap_flow_free(pmd, flow);
1521 * @see rte_flow_destroy()
1525 tap_flow_destroy(struct rte_eth_dev *dev,
1526 struct rte_flow *flow,
1527 struct rte_flow_error *error)
1529 struct pmd_internals *pmd = dev->data->dev_private;
1531 return tap_flow_destroy_pmd(pmd, flow, error);
1535 * Enable/disable flow isolation.
1537 * @see rte_flow_isolate()
1541 tap_flow_isolate(struct rte_eth_dev *dev,
1543 struct rte_flow_error *error __rte_unused)
1545 struct pmd_internals *pmd = dev->data->dev_private;
1548 pmd->flow_isolate = 1;
1550 pmd->flow_isolate = 0;
1552 * If netdevice is there, setup appropriate flow rules immediately.
1553 * Otherwise it will be set when bringing up the netdevice (tun_alloc).
1555 if (!pmd->rxq[0].fd)
1558 struct rte_flow *flow;
1561 flow = LIST_FIRST(&pmd->implicit_flows);
1565 * Remove all implicit rules on the remote.
1566 * Keep the local rule to redirect packets on TX.
1567 * Keep also the last implicit local rule: ISOLATE.
1569 if (flow->msg.t.tcm_ifindex == pmd->if_index)
1571 if (tap_flow_destroy_pmd(pmd, flow, NULL) < 0)
1574 /* Switch the TC rule according to pmd->flow_isolate */
1575 if (tap_flow_implicit_create(pmd, TAP_ISOLATE) == -1)
1578 /* Switch the TC rule according to pmd->flow_isolate */
1579 if (tap_flow_implicit_create(pmd, TAP_ISOLATE) == -1)
1581 if (!pmd->remote_if_index)
1583 if (tap_flow_implicit_create(pmd, TAP_REMOTE_TX) < 0)
1585 if (tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0)
1587 if (tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCAST) < 0)
1589 if (tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCASTV6) < 0)
1591 if (dev->data->promiscuous &&
1592 tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC) < 0)
1594 if (dev->data->all_multicast &&
1595 tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI) < 0)
1600 pmd->flow_isolate = 0;
1601 return rte_flow_error_set(
1602 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1603 "TC rule creation failed");
1607 * Destroy all flows.
1609 * @see rte_flow_flush()
1613 tap_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1615 struct pmd_internals *pmd = dev->data->dev_private;
1616 struct rte_flow *flow;
1618 while (!LIST_EMPTY(&pmd->flows)) {
1619 flow = LIST_FIRST(&pmd->flows);
1620 if (tap_flow_destroy(dev, flow, error) < 0)
1627 * Add an implicit flow rule on the remote device to make sure traffic gets to
1628 * the tap netdevice from there.
1631 * Pointer to private structure.
1633 * The idx in the implicit_rte_flows array specifying which rule to apply.
1635 * @return -1 if the rule couldn't be applied, 0 otherwise.
1637 int tap_flow_implicit_create(struct pmd_internals *pmd,
1638 enum implicit_rule_index idx)
1640 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE;
1641 struct rte_flow_action *actions = implicit_rte_flows[idx].actions;
1642 struct rte_flow_action isolate_actions[2] = {
1644 .type = RTE_FLOW_ACTION_TYPE_END,
1647 struct rte_flow_item *items = implicit_rte_flows[idx].items;
1648 struct rte_flow_attr *attr = &implicit_rte_flows[idx].attr;
1649 struct rte_flow_item_eth eth_local = { .type = 0 };
1650 uint16_t if_index = pmd->remote_if_index;
1651 struct rte_flow *remote_flow = NULL;
1652 struct nlmsg *msg = NULL;
1654 struct rte_flow_item items_local[2] = {
1656 .type = items[0].type,
1658 .mask = items[0].mask,
1661 .type = items[1].type,
1665 remote_flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1667 RTE_LOG(ERR, PMD, "Cannot allocate memory for rte_flow\n");
1670 msg = &remote_flow->msg;
1671 if (idx == TAP_REMOTE_TX) {
1672 if_index = pmd->if_index;
1673 } else if (idx == TAP_ISOLATE) {
1674 if_index = pmd->if_index;
1675 /* Don't be exclusive for this rule, it can be changed later. */
1676 flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE;
1677 isolate_actions[0].type = pmd->flow_isolate ?
1678 RTE_FLOW_ACTION_TYPE_DROP :
1679 RTE_FLOW_ACTION_TYPE_PASSTHRU;
1680 actions = isolate_actions;
1681 } else if (idx == TAP_REMOTE_LOCAL_MAC) {
1683 * eth addr couldn't be set in implicit_rte_flows[] as it is not
1684 * known at compile time.
1686 memcpy(ð_local.dst, &pmd->eth_addr, sizeof(pmd->eth_addr));
1687 items = items_local;
1689 tc_init_msg(msg, if_index, RTM_NEWTFILTER, flags);
1690 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1692 * The ISOLATE rule is always present and must have a static handle, as
1693 * the action is changed whether the feature is enabled (DROP) or
1694 * disabled (PASSTHRU).
1696 if (idx == TAP_ISOLATE)
1697 remote_flow->msg.t.tcm_handle = ISOLATE_HANDLE;
1699 tap_flow_set_handle(remote_flow);
1700 if (priv_flow_process(pmd, attr, items, actions, NULL,
1701 remote_flow, implicit_rte_flows[idx].mirred)) {
1702 RTE_LOG(ERR, PMD, "rte flow rule validation failed\n");
1705 err = tap_nl_send(pmd->nlsk_fd, &msg->nh);
1707 RTE_LOG(ERR, PMD, "Failure sending nl request\n");
1710 err = tap_nl_recv_ack(pmd->nlsk_fd);
1713 "Kernel refused TC filter rule creation (%d): %s\n",
1714 errno, strerror(errno));
1717 LIST_INSERT_HEAD(&pmd->implicit_flows, remote_flow, next);
1721 rte_free(remote_flow);
1726 * Remove specific implicit flow rule on the remote device.
1728 * @param[in, out] pmd
1729 * Pointer to private structure.
1731 * The idx in the implicit_rte_flows array specifying which rule to remove.
1733 * @return -1 if one of the implicit rules couldn't be created, 0 otherwise.
1735 int tap_flow_implicit_destroy(struct pmd_internals *pmd,
1736 enum implicit_rule_index idx)
1738 struct rte_flow *remote_flow;
1740 int idx_prio = implicit_rte_flows[idx].attr.priority + PRIORITY_OFFSET;
1742 for (remote_flow = LIST_FIRST(&pmd->implicit_flows);
1744 remote_flow = LIST_NEXT(remote_flow, next)) {
1745 cur_prio = (remote_flow->msg.t.tcm_info >> 16) & PRIORITY_MASK;
1746 if (cur_prio != idx_prio)
1748 return tap_flow_destroy_pmd(pmd, remote_flow, NULL);
1754 * Destroy all implicit flows.
1756 * @see rte_flow_flush()
1759 tap_flow_implicit_flush(struct pmd_internals *pmd, struct rte_flow_error *error)
1761 struct rte_flow *remote_flow;
1763 while (!LIST_EMPTY(&pmd->implicit_flows)) {
1764 remote_flow = LIST_FIRST(&pmd->implicit_flows);
1765 if (tap_flow_destroy_pmd(pmd, remote_flow, error) < 0)
1771 #define MAX_RSS_KEYS 256
1772 #define KEY_IDX_OFFSET (3 * MAX_RSS_KEYS)
1773 #define SEC_NAME_CLS_Q "cls_q"
1775 const char *sec_name[SEC_MAX] = {
1776 [SEC_L3_L4] = "l3_l4",
1780 * Enable RSS on tap: create TC rules for queuing.
1782 * @param[in, out] pmd
1783 * Pointer to private structure.
1786 * Pointer to rte_flow to get flow group
1789 * Pointer to error reporting if not NULL.
1791 * @return 0 on success, negative value on failure.
1793 static int rss_enable(struct pmd_internals *pmd,
1794 const struct rte_flow_attr *attr,
1795 struct rte_flow_error *error)
1797 struct rte_flow *rss_flow = NULL;
1798 struct nlmsg *msg = NULL;
1799 /* 4096 is the maximum number of instructions for a BPF program */
1800 char annotation[64];
1804 /* unlimit locked memory */
1805 struct rlimit memlock_limit = {
1806 .rlim_cur = RLIM_INFINITY,
1807 .rlim_max = RLIM_INFINITY,
1809 setrlimit(RLIMIT_MEMLOCK, &memlock_limit);
1811 /* Get a new map key for a new RSS rule */
1812 err = bpf_rss_key(KEY_CMD_INIT, NULL);
1815 error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1816 "Failed to initialize BPF RSS keys");
1822 * Create BPF RSS MAP
1824 pmd->map_fd = tap_flow_bpf_rss_map_create(sizeof(__u32), /* key size */
1825 sizeof(struct rss_key),
1827 if (pmd->map_fd < 0) {
1829 "Failed to create BPF map (%d): %s\n",
1830 errno, strerror(errno));
1832 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1833 "Kernel too old or not configured "
1834 "to support BPF maps");
1840 * Add a rule per queue to match reclassified packets and direct them to
1841 * the correct queue.
1843 for (i = 0; i < pmd->dev->data->nb_rx_queues; i++) {
1844 pmd->bpf_fd[i] = tap_flow_bpf_cls_q(i);
1845 if (pmd->bpf_fd[i] < 0) {
1847 "Failed to load BPF section %s for queue %d",
1850 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1852 "Kernel too old or not configured "
1853 "to support BPF programs loading");
1858 rss_flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1861 "Cannot allocate memory for rte_flow");
1864 msg = &rss_flow->msg;
1865 tc_init_msg(msg, pmd->if_index, RTM_NEWTFILTER, NLM_F_REQUEST |
1866 NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
1867 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1868 tap_flow_set_handle(rss_flow);
1869 uint16_t group = attr->group << GROUP_SHIFT;
1870 uint16_t prio = group | (i + PRIORITY_OFFSET);
1871 msg->t.tcm_info = TC_H_MAKE(prio << 16, msg->t.tcm_info);
1872 msg->t.tcm_parent = TC_H_MAKE(MULTIQ_MAJOR_HANDLE, 0);
1874 tap_nlattr_add(&msg->nh, TCA_KIND, sizeof("bpf"), "bpf");
1875 if (tap_nlattr_nested_start(msg, TCA_OPTIONS) < 0)
1877 tap_nlattr_add32(&msg->nh, TCA_BPF_FD, pmd->bpf_fd[i]);
1878 snprintf(annotation, sizeof(annotation), "[%s%d]",
1880 tap_nlattr_add(&msg->nh, TCA_BPF_NAME, strlen(annotation) + 1,
1884 struct action_data adata = {
1888 .action = TC_ACT_PIPE,
1893 if (add_actions(rss_flow, 1, &adata, TCA_BPF_ACT) < 0)
1896 tap_nlattr_nested_finish(msg); /* nested TCA_OPTIONS */
1898 /* Netlink message is now ready to be sent */
1899 if (tap_nl_send(pmd->nlsk_fd, &msg->nh) < 0)
1901 err = tap_nl_recv_ack(pmd->nlsk_fd);
1904 "Kernel refused TC filter rule creation (%d): %s\n",
1905 errno, strerror(errno));
1908 LIST_INSERT_HEAD(&pmd->rss_flows, rss_flow, next);
1911 pmd->rss_enabled = 1;
1916 * Manage bpf RSS keys repository with operations: init, get, release
1919 * Command on RSS keys: init, get, release
1921 * @param[in, out] key_idx
1922 * Pointer to RSS Key index (out for get command, in for release command)
1924 * @return -1 if couldn't get, release or init the RSS keys, 0 otherwise.
1926 static int bpf_rss_key(enum bpf_rss_key_e cmd, __u32 *key_idx)
1930 static __u32 num_used_keys;
1931 static __u32 rss_keys[MAX_RSS_KEYS] = {KEY_STAT_UNSPEC};
1932 static __u32 rss_keys_initialized;
1937 if (!rss_keys_initialized) {
1942 if (num_used_keys == RTE_DIM(rss_keys)) {
1947 *key_idx = num_used_keys % RTE_DIM(rss_keys);
1948 while (rss_keys[*key_idx] == KEY_STAT_USED)
1949 *key_idx = (*key_idx + 1) % RTE_DIM(rss_keys);
1951 rss_keys[*key_idx] = KEY_STAT_USED;
1954 * Add an offset to key_idx in order to handle a case of
1955 * RSS and non RSS flows mixture.
1956 * If a non RSS flow is destroyed it has an eBPF map
1957 * index 0 (initialized on flow creation) and might
1958 * unintentionally remove RSS entry 0 from eBPF map.
1959 * To avoid this issue, add an offset to the real index
1960 * during a KEY_CMD_GET operation and subtract this offset
1961 * during a KEY_CMD_RELEASE operation in order to restore
1964 *key_idx += KEY_IDX_OFFSET;
1968 case KEY_CMD_RELEASE:
1969 if (!rss_keys_initialized)
1973 * Subtract offest to restore real key index
1974 * If a non RSS flow is falsely trying to release map
1975 * entry 0 - the offset subtraction will calculate the real
1976 * map index as an out-of-range value and the release operation
1977 * will be silently ignored.
1979 key = *key_idx - KEY_IDX_OFFSET;
1980 if (key >= RTE_DIM(rss_keys))
1983 if (rss_keys[key] == KEY_STAT_USED) {
1984 rss_keys[key] = KEY_STAT_AVAILABLE;
1990 for (i = 0; i < RTE_DIM(rss_keys); i++)
1991 rss_keys[i] = KEY_STAT_AVAILABLE;
1993 rss_keys_initialized = 1;
1997 case KEY_CMD_DEINIT:
1998 for (i = 0; i < RTE_DIM(rss_keys); i++)
1999 rss_keys[i] = KEY_STAT_UNSPEC;
2001 rss_keys_initialized = 0;
2013 * Add RSS hash calculations and queue selection
2015 * @param[in, out] pmd
2016 * Pointer to internal structure. Used to set/get RSS map fd
2019 * Pointer to RSS flow actions
2022 * Pointer to error reporting if not NULL.
2024 * @return 0 on success, negative value on failure
2026 static int rss_add_actions(struct rte_flow *flow, struct pmd_internals *pmd,
2027 const struct rte_flow_action_rss *rss,
2028 struct rte_flow_error *error)
2030 /* 4096 is the maximum number of instructions for a BPF program */
2033 struct rss_key rss_entry = { .hash_fields = 0,
2036 /* Get a new map key for a new RSS rule */
2037 err = bpf_rss_key(KEY_CMD_GET, &flow->key_idx);
2040 error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2041 "Failed to get BPF RSS key");
2046 /* Update RSS map entry with queues */
2047 rss_entry.nb_queues = rss->num;
2048 for (i = 0; i < rss->num; i++)
2049 rss_entry.queues[i] = rss->queue[i];
2050 rss_entry.hash_fields =
2051 (1 << HASH_FIELD_IPV4_L3_L4) | (1 << HASH_FIELD_IPV6_L3_L4);
2053 /* Add this RSS entry to map */
2054 err = tap_flow_bpf_update_rss_elem(pmd->map_fd,
2055 &flow->key_idx, &rss_entry);
2059 "Failed to update BPF map entry #%u (%d): %s\n",
2060 flow->key_idx, errno, strerror(errno));
2062 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2063 "Kernel too old or not configured "
2064 "to support BPF maps updates");
2071 * Load bpf rules to calculate hash for this key_idx
2074 flow->bpf_fd[SEC_L3_L4] =
2075 tap_flow_bpf_calc_l3_l4_hash(flow->key_idx, pmd->map_fd);
2076 if (flow->bpf_fd[SEC_L3_L4] < 0) {
2078 "Failed to load BPF section %s (%d): %s\n",
2079 sec_name[SEC_L3_L4], errno, strerror(errno));
2081 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2082 "Kernel too old or not configured "
2083 "to support BPF program loading");
2090 struct action_data adata[] = {
2094 .bpf_fd = flow->bpf_fd[SEC_L3_L4],
2095 .annotation = sec_name[SEC_L3_L4],
2097 .action = TC_ACT_PIPE,
2103 if (add_actions(flow, RTE_DIM(adata), adata,
2104 TCA_FLOWER_ACT) < 0)
2112 * Manage filter operations.
2115 * Pointer to Ethernet device structure.
2116 * @param filter_type
2119 * Operation to perform.
2121 * Pointer to operation-specific structure.
2124 * 0 on success, negative errno value on failure.
2127 tap_dev_filter_ctrl(struct rte_eth_dev *dev,
2128 enum rte_filter_type filter_type,
2129 enum rte_filter_op filter_op,
2132 switch (filter_type) {
2133 case RTE_ETH_FILTER_GENERIC:
2134 if (filter_op != RTE_ETH_FILTER_GET)
2136 *(const void **)arg = &tap_flow_ops;
2139 RTE_LOG(ERR, PMD, "%p: filter type (%d) not supported\n",
2140 (void *)dev, filter_type);