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_destroy(struct rte_eth_dev *dev,
189 struct rte_flow *flow,
190 struct rte_flow_error *error);
193 tap_flow_isolate(struct rte_eth_dev *dev,
195 struct rte_flow_error *error);
197 static int bpf_rss_key(enum bpf_rss_key_e cmd, __u32 *key_idx);
198 static int rss_enable(struct pmd_internals *pmd,
199 const struct rte_flow_attr *attr,
200 struct rte_flow_error *error);
201 static int rss_add_actions(struct rte_flow *flow, struct pmd_internals *pmd,
202 const struct rte_flow_action_rss *rss,
203 struct rte_flow_error *error);
205 static const struct rte_flow_ops tap_flow_ops = {
206 .validate = tap_flow_validate,
207 .create = tap_flow_create,
208 .destroy = tap_flow_destroy,
209 .flush = tap_flow_flush,
210 .isolate = tap_flow_isolate,
213 /* Static initializer for items. */
215 (const enum rte_flow_item_type []){ \
216 __VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
219 /* Structure to generate a simple graph of layers supported by the NIC. */
220 struct tap_flow_items {
221 /* Bit-mask corresponding to what is supported for this item. */
223 const unsigned int mask_sz; /* Bit-mask size in bytes. */
225 * Bit-mask corresponding to the default mask, if none is provided
226 * along with the item.
228 const void *default_mask;
230 * Conversion function from rte_flow to netlink attributes.
233 * rte_flow item to convert.
235 * Internal structure to store the conversion.
238 * 0 on success, negative value otherwise.
240 int (*convert)(const struct rte_flow_item *item, void *data);
241 /** List of possible following items. */
242 const enum rte_flow_item_type *const items;
245 /* Graph of supported items and associated actions. */
246 static const struct tap_flow_items tap_flow_items[] = {
247 [RTE_FLOW_ITEM_TYPE_END] = {
248 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH),
250 [RTE_FLOW_ITEM_TYPE_ETH] = {
252 RTE_FLOW_ITEM_TYPE_VLAN,
253 RTE_FLOW_ITEM_TYPE_IPV4,
254 RTE_FLOW_ITEM_TYPE_IPV6),
255 .mask = &(const struct rte_flow_item_eth){
256 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
257 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
260 .mask_sz = sizeof(struct rte_flow_item_eth),
261 .default_mask = &rte_flow_item_eth_mask,
262 .convert = tap_flow_create_eth,
264 [RTE_FLOW_ITEM_TYPE_VLAN] = {
265 .items = ITEMS(RTE_FLOW_ITEM_TYPE_IPV4,
266 RTE_FLOW_ITEM_TYPE_IPV6),
267 .mask = &(const struct rte_flow_item_vlan){
269 /* DEI matching is not supported */
270 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
276 .mask_sz = sizeof(struct rte_flow_item_vlan),
277 .default_mask = &rte_flow_item_vlan_mask,
278 .convert = tap_flow_create_vlan,
280 [RTE_FLOW_ITEM_TYPE_IPV4] = {
281 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
282 RTE_FLOW_ITEM_TYPE_TCP),
283 .mask = &(const struct rte_flow_item_ipv4){
290 .mask_sz = sizeof(struct rte_flow_item_ipv4),
291 .default_mask = &rte_flow_item_ipv4_mask,
292 .convert = tap_flow_create_ipv4,
294 [RTE_FLOW_ITEM_TYPE_IPV6] = {
295 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
296 RTE_FLOW_ITEM_TYPE_TCP),
297 .mask = &(const struct rte_flow_item_ipv6){
300 "\xff\xff\xff\xff\xff\xff\xff\xff"
301 "\xff\xff\xff\xff\xff\xff\xff\xff",
304 "\xff\xff\xff\xff\xff\xff\xff\xff"
305 "\xff\xff\xff\xff\xff\xff\xff\xff",
310 .mask_sz = sizeof(struct rte_flow_item_ipv6),
311 .default_mask = &rte_flow_item_ipv6_mask,
312 .convert = tap_flow_create_ipv6,
314 [RTE_FLOW_ITEM_TYPE_UDP] = {
315 .mask = &(const struct rte_flow_item_udp){
321 .mask_sz = sizeof(struct rte_flow_item_udp),
322 .default_mask = &rte_flow_item_udp_mask,
323 .convert = tap_flow_create_udp,
325 [RTE_FLOW_ITEM_TYPE_TCP] = {
326 .mask = &(const struct rte_flow_item_tcp){
332 .mask_sz = sizeof(struct rte_flow_item_tcp),
333 .default_mask = &rte_flow_item_tcp_mask,
334 .convert = tap_flow_create_tcp,
339 * TC rules, by growing priority
341 * Remote netdevice Tap netdevice
342 * +-------------+-------------+ +-------------+-------------+
343 * | Ingress | Egress | | Ingress | Egress |
344 * |-------------|-------------| |-------------|-------------|
345 * | | \ / | | | REMOTE TX | prio 1
346 * | | \ / | | | \ / | prio 2
347 * | EXPLICIT | \ / | | EXPLICIT | \ / | .
348 * | | \ / | | | \ / | .
349 * | RULES | X | | RULES | X | .
350 * | . | / \ | | . | / \ | .
351 * | . | / \ | | . | / \ | .
352 * | . | / \ | | . | / \ | .
353 * | . | / \ | | . | / \ | .
355 * .... .... .... ....
357 * | . | \ / | | . | \ / | .
358 * | . | \ / | | . | \ / | .
359 * | | \ / | | | \ / |
360 * | LOCAL_MAC | \ / | | \ / | \ / | last prio - 5
361 * | PROMISC | X | | \ / | X | last prio - 4
362 * | ALLMULTI | / \ | | X | / \ | last prio - 3
363 * | BROADCAST | / \ | | / \ | / \ | last prio - 2
364 * | BROADCASTV6 | / \ | | / \ | / \ | last prio - 1
365 * | xx | / \ | | ISOLATE | / \ | last prio
366 * +-------------+-------------+ +-------------+-------------+
368 * The implicit flow rules are stored in a list in with mandatorily the last two
369 * being the ISOLATE and REMOTE_TX rules. e.g.:
371 * LOCAL_MAC -> BROADCAST -> BROADCASTV6 -> REMOTE_TX -> ISOLATE -> NULL
373 * That enables tap_flow_isolate() to remove implicit rules by popping the list
374 * head and remove it as long as it applies on the remote netdevice. The
375 * implicit rule for TX redirection is not removed, as isolate concerns only
379 static struct remote_rule implicit_rte_flows[TAP_REMOTE_MAX_IDX] = {
380 [TAP_REMOTE_LOCAL_MAC] = {
383 .priority = PRIORITY_MASK - TAP_REMOTE_LOCAL_MAC,
387 .type = RTE_FLOW_ITEM_TYPE_ETH,
388 .mask = &(const struct rte_flow_item_eth){
389 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
393 .type = RTE_FLOW_ITEM_TYPE_END,
395 .mirred = TCA_EGRESS_REDIR,
397 [TAP_REMOTE_BROADCAST] = {
400 .priority = PRIORITY_MASK - TAP_REMOTE_BROADCAST,
404 .type = RTE_FLOW_ITEM_TYPE_ETH,
405 .mask = &(const struct rte_flow_item_eth){
406 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
408 .spec = &(const struct rte_flow_item_eth){
409 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
413 .type = RTE_FLOW_ITEM_TYPE_END,
415 .mirred = TCA_EGRESS_MIRROR,
417 [TAP_REMOTE_BROADCASTV6] = {
420 .priority = PRIORITY_MASK - TAP_REMOTE_BROADCASTV6,
424 .type = RTE_FLOW_ITEM_TYPE_ETH,
425 .mask = &(const struct rte_flow_item_eth){
426 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
428 .spec = &(const struct rte_flow_item_eth){
429 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
433 .type = RTE_FLOW_ITEM_TYPE_END,
435 .mirred = TCA_EGRESS_MIRROR,
437 [TAP_REMOTE_PROMISC] = {
440 .priority = PRIORITY_MASK - TAP_REMOTE_PROMISC,
444 .type = RTE_FLOW_ITEM_TYPE_VOID,
447 .type = RTE_FLOW_ITEM_TYPE_END,
449 .mirred = TCA_EGRESS_MIRROR,
451 [TAP_REMOTE_ALLMULTI] = {
454 .priority = PRIORITY_MASK - TAP_REMOTE_ALLMULTI,
458 .type = RTE_FLOW_ITEM_TYPE_ETH,
459 .mask = &(const struct rte_flow_item_eth){
460 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
462 .spec = &(const struct rte_flow_item_eth){
463 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
467 .type = RTE_FLOW_ITEM_TYPE_END,
469 .mirred = TCA_EGRESS_MIRROR,
474 .priority = TAP_REMOTE_TX,
478 .type = RTE_FLOW_ITEM_TYPE_VOID,
481 .type = RTE_FLOW_ITEM_TYPE_END,
483 .mirred = TCA_EGRESS_MIRROR,
488 .priority = PRIORITY_MASK - TAP_ISOLATE,
492 .type = RTE_FLOW_ITEM_TYPE_VOID,
495 .type = RTE_FLOW_ITEM_TYPE_END,
501 * Make as much checks as possible on an Ethernet item, and if a flow is
502 * provided, fill it appropriately with Ethernet info.
505 * Item specification.
506 * @param[in, out] data
507 * Additional data structure to tell next layers we've been here.
510 * 0 if checks are alright, -1 otherwise.
513 tap_flow_create_eth(const struct rte_flow_item *item, void *data)
515 struct convert_data *info = (struct convert_data *)data;
516 const struct rte_flow_item_eth *spec = item->spec;
517 const struct rte_flow_item_eth *mask = item->mask;
518 struct rte_flow *flow = info->flow;
521 /* use default mask if none provided */
523 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_ETH].default_mask;
524 /* TC does not support eth_type masking. Only accept if exact match. */
525 if (mask->type && mask->type != 0xffff)
529 /* store eth_type for consistency if ipv4/6 pattern item comes next */
530 if (spec->type & mask->type)
531 info->eth_type = spec->type;
535 if (!is_zero_ether_addr(&spec->dst)) {
536 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_ETH_DST, ETHER_ADDR_LEN,
537 &spec->dst.addr_bytes);
538 tap_nlattr_add(&msg->nh,
539 TCA_FLOWER_KEY_ETH_DST_MASK, ETHER_ADDR_LEN,
540 &mask->dst.addr_bytes);
542 if (!is_zero_ether_addr(&mask->src)) {
543 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_ETH_SRC, ETHER_ADDR_LEN,
544 &spec->src.addr_bytes);
545 tap_nlattr_add(&msg->nh,
546 TCA_FLOWER_KEY_ETH_SRC_MASK, ETHER_ADDR_LEN,
547 &mask->src.addr_bytes);
553 * Make as much checks as possible on a VLAN item, and if a flow is provided,
554 * fill it appropriately with VLAN info.
557 * Item specification.
558 * @param[in, out] data
559 * Additional data structure to tell next layers we've been here.
562 * 0 if checks are alright, -1 otherwise.
565 tap_flow_create_vlan(const struct rte_flow_item *item, void *data)
567 struct convert_data *info = (struct convert_data *)data;
568 const struct rte_flow_item_vlan *spec = item->spec;
569 const struct rte_flow_item_vlan *mask = item->mask;
570 struct rte_flow *flow = info->flow;
573 /* use default mask if none provided */
575 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_VLAN].default_mask;
576 /* TC does not support tpid masking. Only accept if exact match. */
577 if (mask->tpid && mask->tpid != 0xffff)
579 /* Double-tagging not supported. */
580 if (spec && mask->tpid && spec->tpid != htons(ETH_P_8021Q))
586 msg->t.tcm_info = TC_H_MAKE(msg->t.tcm_info, htons(ETH_P_8021Q));
587 #define VLAN_PRIO(tci) ((tci) >> 13)
588 #define VLAN_ID(tci) ((tci) & 0xfff)
592 uint16_t tci = ntohs(spec->tci) & mask->tci;
593 uint16_t prio = VLAN_PRIO(tci);
594 uint8_t vid = VLAN_ID(tci);
597 tap_nlattr_add8(&msg->nh,
598 TCA_FLOWER_KEY_VLAN_PRIO, prio);
600 tap_nlattr_add16(&msg->nh,
601 TCA_FLOWER_KEY_VLAN_ID, vid);
607 * Make as much checks as possible on an IPv4 item, and if a flow is provided,
608 * fill it appropriately with IPv4 info.
611 * Item specification.
612 * @param[in, out] data
613 * Additional data structure to tell next layers we've been here.
616 * 0 if checks are alright, -1 otherwise.
619 tap_flow_create_ipv4(const struct rte_flow_item *item, void *data)
621 struct convert_data *info = (struct convert_data *)data;
622 const struct rte_flow_item_ipv4 *spec = item->spec;
623 const struct rte_flow_item_ipv4 *mask = item->mask;
624 struct rte_flow *flow = info->flow;
627 /* use default mask if none provided */
629 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_IPV4].default_mask;
630 /* check that previous eth type is compatible with ipv4 */
631 if (info->eth_type && info->eth_type != htons(ETH_P_IP))
633 /* store ip_proto for consistency if udp/tcp pattern item comes next */
635 info->ip_proto = spec->hdr.next_proto_id;
640 info->eth_type = htons(ETH_P_IP);
643 if (spec->hdr.dst_addr) {
644 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_DST,
646 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_DST_MASK,
649 if (spec->hdr.src_addr) {
650 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_SRC,
652 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_SRC_MASK,
655 if (spec->hdr.next_proto_id)
656 tap_nlattr_add8(&msg->nh, TCA_FLOWER_KEY_IP_PROTO,
657 spec->hdr.next_proto_id);
662 * Make as much checks as possible on an IPv6 item, and if a flow is provided,
663 * fill it appropriately with IPv6 info.
666 * Item specification.
667 * @param[in, out] data
668 * Additional data structure to tell next layers we've been here.
671 * 0 if checks are alright, -1 otherwise.
674 tap_flow_create_ipv6(const struct rte_flow_item *item, void *data)
676 struct convert_data *info = (struct convert_data *)data;
677 const struct rte_flow_item_ipv6 *spec = item->spec;
678 const struct rte_flow_item_ipv6 *mask = item->mask;
679 struct rte_flow *flow = info->flow;
680 uint8_t empty_addr[16] = { 0 };
683 /* use default mask if none provided */
685 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_IPV6].default_mask;
686 /* check that previous eth type is compatible with ipv6 */
687 if (info->eth_type && info->eth_type != htons(ETH_P_IPV6))
689 /* store ip_proto for consistency if udp/tcp pattern item comes next */
691 info->ip_proto = spec->hdr.proto;
696 info->eth_type = htons(ETH_P_IPV6);
699 if (memcmp(spec->hdr.dst_addr, empty_addr, 16)) {
700 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_DST,
701 sizeof(spec->hdr.dst_addr), &spec->hdr.dst_addr);
702 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_DST_MASK,
703 sizeof(mask->hdr.dst_addr), &mask->hdr.dst_addr);
705 if (memcmp(spec->hdr.src_addr, empty_addr, 16)) {
706 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_SRC,
707 sizeof(spec->hdr.src_addr), &spec->hdr.src_addr);
708 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_SRC_MASK,
709 sizeof(mask->hdr.src_addr), &mask->hdr.src_addr);
712 tap_nlattr_add8(&msg->nh,
713 TCA_FLOWER_KEY_IP_PROTO, spec->hdr.proto);
718 * Make as much checks as possible on a UDP item, and if a flow is provided,
719 * fill it appropriately with UDP info.
722 * Item specification.
723 * @param[in, out] data
724 * Additional data structure to tell next layers we've been here.
727 * 0 if checks are alright, -1 otherwise.
730 tap_flow_create_udp(const struct rte_flow_item *item, void *data)
732 struct convert_data *info = (struct convert_data *)data;
733 const struct rte_flow_item_udp *spec = item->spec;
734 const struct rte_flow_item_udp *mask = item->mask;
735 struct rte_flow *flow = info->flow;
738 /* use default mask if none provided */
740 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_UDP].default_mask;
741 /* check that previous ip_proto is compatible with udp */
742 if (info->ip_proto && info->ip_proto != IPPROTO_UDP)
744 /* TC does not support UDP port masking. Only accept if exact match. */
745 if ((mask->hdr.src_port && mask->hdr.src_port != 0xffff) ||
746 (mask->hdr.dst_port && mask->hdr.dst_port != 0xffff))
751 tap_nlattr_add8(&msg->nh, TCA_FLOWER_KEY_IP_PROTO, IPPROTO_UDP);
754 if (spec->hdr.dst_port & mask->hdr.dst_port)
755 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_UDP_DST,
757 if (spec->hdr.src_port & mask->hdr.src_port)
758 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_UDP_SRC,
764 * Make as much checks as possible on a TCP item, and if a flow is provided,
765 * fill it appropriately with TCP info.
768 * Item specification.
769 * @param[in, out] data
770 * Additional data structure to tell next layers we've been here.
773 * 0 if checks are alright, -1 otherwise.
776 tap_flow_create_tcp(const struct rte_flow_item *item, void *data)
778 struct convert_data *info = (struct convert_data *)data;
779 const struct rte_flow_item_tcp *spec = item->spec;
780 const struct rte_flow_item_tcp *mask = item->mask;
781 struct rte_flow *flow = info->flow;
784 /* use default mask if none provided */
786 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_TCP].default_mask;
787 /* check that previous ip_proto is compatible with tcp */
788 if (info->ip_proto && info->ip_proto != IPPROTO_TCP)
790 /* TC does not support TCP port masking. Only accept if exact match. */
791 if ((mask->hdr.src_port && mask->hdr.src_port != 0xffff) ||
792 (mask->hdr.dst_port && mask->hdr.dst_port != 0xffff))
797 tap_nlattr_add8(&msg->nh, TCA_FLOWER_KEY_IP_PROTO, IPPROTO_TCP);
800 if (spec->hdr.dst_port & mask->hdr.dst_port)
801 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_TCP_DST,
803 if (spec->hdr.src_port & mask->hdr.src_port)
804 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_TCP_SRC,
810 * Check support for a given item.
813 * Item specification.
815 * Bit-Mask size in bytes.
816 * @param[in] supported_mask
817 * Bit-mask covering supported fields to compare with spec, last and mask in
819 * @param[in] default_mask
820 * Bit-mask default mask if none is provided in \item.
826 tap_flow_item_validate(const struct rte_flow_item *item,
828 const uint8_t *supported_mask,
829 const uint8_t *default_mask)
833 /* An empty layer is allowed, as long as all fields are NULL */
834 if (!item->spec && (item->mask || item->last))
836 /* Is the item spec compatible with what the NIC supports? */
837 if (item->spec && !item->mask) {
839 const uint8_t *spec = item->spec;
841 for (i = 0; i < size; ++i)
842 if ((spec[i] | supported_mask[i]) != supported_mask[i])
844 /* Is the default mask compatible with what the NIC supports? */
845 for (i = 0; i < size; i++)
846 if ((default_mask[i] | supported_mask[i]) !=
850 /* Is the item last compatible with what the NIC supports? */
851 if (item->last && !item->mask) {
853 const uint8_t *spec = item->last;
855 for (i = 0; i < size; ++i)
856 if ((spec[i] | supported_mask[i]) != supported_mask[i])
859 /* Is the item mask compatible with what the NIC supports? */
862 const uint8_t *spec = item->mask;
864 for (i = 0; i < size; ++i)
865 if ((spec[i] | supported_mask[i]) != supported_mask[i])
869 * Once masked, Are item spec and item last equal?
870 * TC does not support range so anything else is invalid.
872 if (item->spec && item->last) {
875 const uint8_t *apply = default_mask;
880 for (i = 0; i < size; ++i) {
881 spec[i] = ((const uint8_t *)item->spec)[i] & apply[i];
882 last[i] = ((const uint8_t *)item->last)[i] & apply[i];
884 ret = memcmp(spec, last, size);
890 * Configure the kernel with a TC action and its configured parameters
891 * Handled actions: "gact", "mirred", "skbedit", "bpf"
894 * Pointer to rte flow containing the netlink message
896 * @param[in, out] act_index
897 * Pointer to action sequence number in the TC command
900 * Pointer to struct holding the action parameters
903 * -1 on failure, 0 on success
906 add_action(struct rte_flow *flow, size_t *act_index, struct action_data *adata)
908 struct nlmsg *msg = &flow->msg;
910 if (tap_nlattr_nested_start(msg, (*act_index)++) < 0)
913 tap_nlattr_add(&msg->nh, TCA_ACT_KIND,
914 strlen(adata->id) + 1, adata->id);
915 if (tap_nlattr_nested_start(msg, TCA_ACT_OPTIONS) < 0)
917 if (strcmp("gact", adata->id) == 0) {
918 tap_nlattr_add(&msg->nh, TCA_GACT_PARMS, sizeof(adata->gact),
920 } else if (strcmp("mirred", adata->id) == 0) {
921 if (adata->mirred.eaction == TCA_EGRESS_MIRROR)
922 adata->mirred.action = TC_ACT_PIPE;
924 adata->mirred.action = TC_ACT_STOLEN;
925 tap_nlattr_add(&msg->nh, TCA_MIRRED_PARMS,
926 sizeof(adata->mirred),
928 } else if (strcmp("skbedit", adata->id) == 0) {
929 tap_nlattr_add(&msg->nh, TCA_SKBEDIT_PARMS,
930 sizeof(adata->skbedit.skbedit),
931 &adata->skbedit.skbedit);
932 tap_nlattr_add16(&msg->nh, TCA_SKBEDIT_QUEUE_MAPPING,
933 adata->skbedit.queue);
934 } else if (strcmp("bpf", adata->id) == 0) {
935 tap_nlattr_add32(&msg->nh, TCA_ACT_BPF_FD, adata->bpf.bpf_fd);
936 tap_nlattr_add(&msg->nh, TCA_ACT_BPF_NAME,
937 strlen(adata->bpf.annotation) + 1,
938 adata->bpf.annotation);
939 tap_nlattr_add(&msg->nh, TCA_ACT_BPF_PARMS,
940 sizeof(adata->bpf.bpf),
945 tap_nlattr_nested_finish(msg); /* nested TCA_ACT_OPTIONS */
946 tap_nlattr_nested_finish(msg); /* nested act_index */
951 * Helper function to send a serie of TC actions to the kernel
954 * Pointer to rte flow containing the netlink message
956 * @param[in] nb_actions
957 * Number of actions in an array of action structs
960 * Pointer to an array of action structs
962 * @param[in] classifier_actions
963 * The classifier on behave of which the actions are configured
966 * -1 on failure, 0 on success
969 add_actions(struct rte_flow *flow, int nb_actions, struct action_data *data,
970 int classifier_action)
972 struct nlmsg *msg = &flow->msg;
973 size_t act_index = 1;
976 if (tap_nlattr_nested_start(msg, classifier_action) < 0)
978 for (i = 0; i < nb_actions; i++)
979 if (add_action(flow, &act_index, data + i) < 0)
981 tap_nlattr_nested_finish(msg); /* nested TCA_FLOWER_ACT */
986 * Validate a flow supported by TC.
987 * If flow param is not NULL, then also fill the netlink message inside.
990 * Pointer to private structure.
992 * Flow rule attributes.
994 * Pattern specification (list terminated by the END pattern item).
996 * Associated actions (list terminated by the END action).
998 * Perform verbose error reporting if not NULL.
999 * @param[in, out] flow
1000 * Flow structure to update.
1002 * If set to TCA_EGRESS_REDIR, provided actions will be replaced with a
1003 * redirection to the tap netdevice, and the TC rule will be configured
1004 * on the remote netdevice in pmd.
1005 * If set to TCA_EGRESS_MIRROR, provided actions will be replaced with a
1006 * mirroring to the tap netdevice, and the TC rule will be configured
1007 * on the remote netdevice in pmd. Matching packets will thus be duplicated.
1008 * If set to 0, the standard behavior is to be used: set correct actions for
1009 * the TC rule, and apply it on the tap netdevice.
1012 * 0 on success, a negative errno value otherwise and rte_errno is set.
1015 priv_flow_process(struct pmd_internals *pmd,
1016 const struct rte_flow_attr *attr,
1017 const struct rte_flow_item items[],
1018 const struct rte_flow_action actions[],
1019 struct rte_flow_error *error,
1020 struct rte_flow *flow,
1023 const struct tap_flow_items *cur_item = tap_flow_items;
1024 struct convert_data data = {
1029 int action = 0; /* Only one action authorized for now */
1031 if (attr->group > MAX_GROUP) {
1033 error, EINVAL, RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1034 NULL, "group value too big: cannot exceed 15");
1037 if (attr->priority > MAX_PRIORITY) {
1039 error, EINVAL, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1040 NULL, "priority value too big");
1043 uint16_t group = attr->group << GROUP_SHIFT;
1044 uint16_t prio = group | (attr->priority +
1045 RSS_PRIORITY_OFFSET + PRIORITY_OFFSET);
1046 flow->msg.t.tcm_info = TC_H_MAKE(prio << 16,
1047 flow->msg.t.tcm_info);
1052 * If attr->ingress, the rule applies on remote ingress
1053 * to match incoming packets
1054 * If attr->egress, the rule applies on tap ingress (as
1055 * seen from the kernel) to deal with packets going out
1056 * from the DPDK app.
1058 flow->msg.t.tcm_parent = TC_H_MAKE(TC_H_INGRESS, 0);
1060 /* Standard rule on tap egress (kernel standpoint). */
1061 flow->msg.t.tcm_parent =
1062 TC_H_MAKE(MULTIQ_MAJOR_HANDLE, 0);
1064 /* use flower filter type */
1065 tap_nlattr_add(&flow->msg.nh, TCA_KIND, sizeof("flower"), "flower");
1066 if (tap_nlattr_nested_start(&flow->msg, TCA_OPTIONS) < 0)
1067 goto exit_item_not_supported;
1069 for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
1070 const struct tap_flow_items *token = NULL;
1074 if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
1078 cur_item->items[i] != RTE_FLOW_ITEM_TYPE_END;
1080 if (cur_item->items[i] == items->type) {
1081 token = &tap_flow_items[items->type];
1086 goto exit_item_not_supported;
1088 err = tap_flow_item_validate(
1089 items, cur_item->mask_sz,
1090 (const uint8_t *)cur_item->mask,
1091 (const uint8_t *)cur_item->default_mask);
1093 goto exit_item_not_supported;
1094 if (flow && cur_item->convert) {
1095 err = cur_item->convert(items, &data);
1097 goto exit_item_not_supported;
1102 tap_nlattr_add16(&flow->msg.nh, TCA_FLOWER_KEY_ETH_TYPE,
1103 htons(ETH_P_8021Q));
1104 tap_nlattr_add16(&flow->msg.nh,
1105 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1107 data.eth_type : htons(ETH_P_ALL));
1108 } else if (data.eth_type) {
1109 tap_nlattr_add16(&flow->msg.nh, TCA_FLOWER_KEY_ETH_TYPE,
1113 if (mirred && flow) {
1114 struct action_data adata = {
1122 * If attr->egress && mirred, then this is a special
1123 * case where the rule must be applied on the tap, to
1124 * redirect packets coming from the DPDK App, out
1125 * through the remote netdevice.
1127 adata.mirred.ifindex = attr->ingress ? pmd->if_index :
1128 pmd->remote_if_index;
1129 if (mirred == TCA_EGRESS_MIRROR)
1130 adata.mirred.action = TC_ACT_PIPE;
1132 adata.mirred.action = TC_ACT_STOLEN;
1133 if (add_actions(flow, 1, &adata, TCA_FLOWER_ACT) < 0)
1134 goto exit_action_not_supported;
1138 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
1141 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID) {
1143 } else if (actions->type == RTE_FLOW_ACTION_TYPE_DROP) {
1145 goto exit_action_not_supported;
1148 struct action_data adata = {
1151 .action = TC_ACT_SHOT,
1155 err = add_actions(flow, 1, &adata,
1158 } else if (actions->type == RTE_FLOW_ACTION_TYPE_PASSTHRU) {
1160 goto exit_action_not_supported;
1163 struct action_data adata = {
1167 .action = TC_ACT_UNSPEC,
1171 err = add_actions(flow, 1, &adata,
1174 } else if (actions->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
1175 const struct rte_flow_action_queue *queue =
1176 (const struct rte_flow_action_queue *)
1180 goto exit_action_not_supported;
1183 (queue->index > pmd->dev->data->nb_rx_queues - 1))
1184 goto exit_action_not_supported;
1186 struct action_data adata = {
1190 .action = TC_ACT_PIPE,
1192 .queue = queue->index,
1196 err = add_actions(flow, 1, &adata,
1199 } else if (actions->type == RTE_FLOW_ACTION_TYPE_RSS) {
1200 const struct rte_flow_action_rss *rss =
1201 (const struct rte_flow_action_rss *)
1205 goto exit_action_not_supported;
1207 if (!pmd->rss_enabled) {
1208 err = rss_enable(pmd, attr, error);
1210 goto exit_action_not_supported;
1213 err = rss_add_actions(flow, pmd, rss, error);
1215 goto exit_action_not_supported;
1218 goto exit_action_not_supported;
1222 tap_nlattr_nested_finish(&flow->msg); /* nested TCA_OPTIONS */
1224 exit_item_not_supported:
1225 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1226 items, "item not supported");
1228 exit_action_not_supported:
1229 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
1230 actions, "action not supported");
1239 * @see rte_flow_validate()
1243 tap_flow_validate(struct rte_eth_dev *dev,
1244 const struct rte_flow_attr *attr,
1245 const struct rte_flow_item items[],
1246 const struct rte_flow_action actions[],
1247 struct rte_flow_error *error)
1249 struct pmd_internals *pmd = dev->data->dev_private;
1251 return priv_flow_process(pmd, attr, items, actions, error, NULL, 0);
1255 * Set a unique handle in a flow.
1257 * The kernel supports TC rules with equal priority, as long as they use the
1258 * same matching fields (e.g.: dst mac and ipv4) with different values (and
1259 * full mask to ensure no collision is possible).
1260 * In those rules, the handle (uint32_t) is the part that would identify
1261 * specifically each rule.
1263 * On 32-bit architectures, the handle can simply be the flow's pointer address.
1264 * On 64-bit architectures, we rely on jhash(flow) to find a (sufficiently)
1267 * @param[in, out] flow
1268 * The flow that needs its handle set.
1271 tap_flow_set_handle(struct rte_flow *flow)
1273 uint32_t handle = 0;
1275 if (sizeof(flow) > 4)
1276 handle = rte_jhash(&flow, sizeof(flow), 1);
1278 handle = (uintptr_t)flow;
1279 /* must be at least 1 to avoid letting the kernel choose one for us */
1282 flow->msg.t.tcm_handle = handle;
1288 * @see rte_flow_create()
1291 static struct rte_flow *
1292 tap_flow_create(struct rte_eth_dev *dev,
1293 const struct rte_flow_attr *attr,
1294 const struct rte_flow_item items[],
1295 const struct rte_flow_action actions[],
1296 struct rte_flow_error *error)
1298 struct pmd_internals *pmd = dev->data->dev_private;
1299 struct rte_flow *remote_flow = NULL;
1300 struct rte_flow *flow = NULL;
1301 struct nlmsg *msg = NULL;
1304 if (!pmd->if_index) {
1305 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1307 "can't create rule, ifindex not found");
1311 * No rules configured through standard rte_flow should be set on the
1312 * priorities used by implicit rules.
1314 if ((attr->group == MAX_GROUP) &&
1315 attr->priority > (MAX_PRIORITY - TAP_REMOTE_MAX_IDX)) {
1317 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1318 NULL, "priority value too big");
1321 flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1323 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1324 NULL, "cannot allocate memory for rte_flow");
1328 tc_init_msg(msg, pmd->if_index, RTM_NEWTFILTER,
1329 NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
1330 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1331 tap_flow_set_handle(flow);
1332 if (priv_flow_process(pmd, attr, items, actions, error, flow, 0))
1334 err = tap_nl_send(pmd->nlsk_fd, &msg->nh);
1336 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1337 NULL, "couldn't send request to kernel");
1340 err = tap_nl_recv_ack(pmd->nlsk_fd);
1343 "Kernel refused TC filter rule creation (%d): %s\n",
1344 errno, strerror(errno));
1345 rte_flow_error_set(error, EEXIST, RTE_FLOW_ERROR_TYPE_HANDLE,
1347 "overlapping rules or Kernel too old for flower support");
1350 LIST_INSERT_HEAD(&pmd->flows, flow, next);
1352 * If a remote device is configured, a TC rule with identical items for
1353 * matching must be set on that device, with a single action: redirect
1354 * to the local pmd->if_index.
1356 if (pmd->remote_if_index) {
1357 remote_flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1360 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1361 "cannot allocate memory for rte_flow");
1364 msg = &remote_flow->msg;
1365 /* set the rule if_index for the remote netdevice */
1367 msg, pmd->remote_if_index, RTM_NEWTFILTER,
1368 NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
1369 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1370 tap_flow_set_handle(remote_flow);
1371 if (priv_flow_process(pmd, attr, items, NULL,
1372 error, remote_flow, TCA_EGRESS_REDIR)) {
1374 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1375 NULL, "rte flow rule validation failed");
1378 err = tap_nl_send(pmd->nlsk_fd, &msg->nh);
1381 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1382 NULL, "Failure sending nl request");
1385 err = tap_nl_recv_ack(pmd->nlsk_fd);
1388 "Kernel refused TC filter rule creation (%d): %s\n",
1389 errno, strerror(errno));
1391 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1393 "overlapping rules or Kernel too old for flower support");
1396 flow->remote_flow = remote_flow;
1401 rte_free(remote_flow);
1408 * Destroy a flow using pointer to pmd_internal.
1410 * @param[in, out] pmd
1411 * Pointer to private structure.
1413 * Pointer to the flow to destroy.
1414 * @param[in, out] error
1415 * Pointer to the flow error handler
1417 * @return 0 if the flow could be destroyed, -1 otherwise.
1420 tap_flow_destroy_pmd(struct pmd_internals *pmd,
1421 struct rte_flow *flow,
1422 struct rte_flow_error *error)
1424 struct rte_flow *remote_flow = flow->remote_flow;
1428 LIST_REMOVE(flow, next);
1429 flow->msg.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1430 flow->msg.nh.nlmsg_type = RTM_DELTFILTER;
1432 ret = tap_nl_send(pmd->nlsk_fd, &flow->msg.nh);
1434 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1435 NULL, "couldn't send request to kernel");
1438 ret = tap_nl_recv_ack(pmd->nlsk_fd);
1439 /* If errno is ENOENT, the rule is already no longer in the kernel. */
1440 if (ret < 0 && errno == ENOENT)
1444 "Kernel refused TC filter rule deletion (%d): %s\n",
1445 errno, strerror(errno));
1447 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1448 "couldn't receive kernel ack to our request");
1451 /* Close opened BPF file descriptors of this flow */
1452 for (i = 0; i < SEC_MAX; i++)
1453 if (flow->bpf_fd[i] != 0) {
1454 close(flow->bpf_fd[i]);
1455 flow->bpf_fd[i] = 0;
1458 /* Release map key for this RSS rule */
1459 ret = bpf_rss_key(KEY_CMD_RELEASE, &flow->key_idx);
1462 error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1463 "Failed to release BPF RSS key");
1469 remote_flow->msg.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1470 remote_flow->msg.nh.nlmsg_type = RTM_DELTFILTER;
1472 ret = tap_nl_send(pmd->nlsk_fd, &remote_flow->msg.nh);
1475 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1476 NULL, "Failure sending nl request");
1479 ret = tap_nl_recv_ack(pmd->nlsk_fd);
1480 if (ret < 0 && errno == ENOENT)
1484 "Kernel refused TC filter rule deletion (%d): %s\n",
1485 errno, strerror(errno));
1487 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1488 NULL, "Failure trying to receive nl ack");
1494 rte_free(remote_flow);
1502 * @see rte_flow_destroy()
1506 tap_flow_destroy(struct rte_eth_dev *dev,
1507 struct rte_flow *flow,
1508 struct rte_flow_error *error)
1510 struct pmd_internals *pmd = dev->data->dev_private;
1512 return tap_flow_destroy_pmd(pmd, flow, error);
1516 * Enable/disable flow isolation.
1518 * @see rte_flow_isolate()
1522 tap_flow_isolate(struct rte_eth_dev *dev,
1524 struct rte_flow_error *error __rte_unused)
1526 struct pmd_internals *pmd = dev->data->dev_private;
1529 pmd->flow_isolate = 1;
1531 pmd->flow_isolate = 0;
1533 * If netdevice is there, setup appropriate flow rules immediately.
1534 * Otherwise it will be set when bringing up the netdevice (tun_alloc).
1536 if (!pmd->rxq[0].fd)
1539 struct rte_flow *flow;
1542 flow = LIST_FIRST(&pmd->implicit_flows);
1546 * Remove all implicit rules on the remote.
1547 * Keep the local rule to redirect packets on TX.
1548 * Keep also the last implicit local rule: ISOLATE.
1550 if (flow->msg.t.tcm_ifindex == pmd->if_index)
1552 if (tap_flow_destroy_pmd(pmd, flow, NULL) < 0)
1555 /* Switch the TC rule according to pmd->flow_isolate */
1556 if (tap_flow_implicit_create(pmd, TAP_ISOLATE) == -1)
1559 /* Switch the TC rule according to pmd->flow_isolate */
1560 if (tap_flow_implicit_create(pmd, TAP_ISOLATE) == -1)
1562 if (!pmd->remote_if_index)
1564 if (tap_flow_implicit_create(pmd, TAP_REMOTE_TX) < 0)
1566 if (tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0)
1568 if (tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCAST) < 0)
1570 if (tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCASTV6) < 0)
1572 if (dev->data->promiscuous &&
1573 tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC) < 0)
1575 if (dev->data->all_multicast &&
1576 tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI) < 0)
1581 pmd->flow_isolate = 0;
1582 return rte_flow_error_set(
1583 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1584 "TC rule creation failed");
1588 * Destroy all flows.
1590 * @see rte_flow_flush()
1594 tap_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1596 struct pmd_internals *pmd = dev->data->dev_private;
1597 struct rte_flow *flow;
1599 while (!LIST_EMPTY(&pmd->flows)) {
1600 flow = LIST_FIRST(&pmd->flows);
1601 if (tap_flow_destroy(dev, flow, error) < 0)
1608 * Add an implicit flow rule on the remote device to make sure traffic gets to
1609 * the tap netdevice from there.
1612 * Pointer to private structure.
1614 * The idx in the implicit_rte_flows array specifying which rule to apply.
1616 * @return -1 if the rule couldn't be applied, 0 otherwise.
1618 int tap_flow_implicit_create(struct pmd_internals *pmd,
1619 enum implicit_rule_index idx)
1621 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE;
1622 struct rte_flow_action *actions = implicit_rte_flows[idx].actions;
1623 struct rte_flow_action isolate_actions[2] = {
1625 .type = RTE_FLOW_ACTION_TYPE_END,
1628 struct rte_flow_item *items = implicit_rte_flows[idx].items;
1629 struct rte_flow_attr *attr = &implicit_rte_flows[idx].attr;
1630 struct rte_flow_item_eth eth_local = { .type = 0 };
1631 uint16_t if_index = pmd->remote_if_index;
1632 struct rte_flow *remote_flow = NULL;
1633 struct nlmsg *msg = NULL;
1635 struct rte_flow_item items_local[2] = {
1637 .type = items[0].type,
1639 .mask = items[0].mask,
1642 .type = items[1].type,
1646 remote_flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1648 RTE_LOG(ERR, PMD, "Cannot allocate memory for rte_flow\n");
1651 msg = &remote_flow->msg;
1652 if (idx == TAP_REMOTE_TX) {
1653 if_index = pmd->if_index;
1654 } else if (idx == TAP_ISOLATE) {
1655 if_index = pmd->if_index;
1656 /* Don't be exclusive for this rule, it can be changed later. */
1657 flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE;
1658 isolate_actions[0].type = pmd->flow_isolate ?
1659 RTE_FLOW_ACTION_TYPE_DROP :
1660 RTE_FLOW_ACTION_TYPE_PASSTHRU;
1661 actions = isolate_actions;
1662 } else if (idx == TAP_REMOTE_LOCAL_MAC) {
1664 * eth addr couldn't be set in implicit_rte_flows[] as it is not
1665 * known at compile time.
1667 memcpy(ð_local.dst, &pmd->eth_addr, sizeof(pmd->eth_addr));
1668 items = items_local;
1670 tc_init_msg(msg, if_index, RTM_NEWTFILTER, flags);
1671 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1673 * The ISOLATE rule is always present and must have a static handle, as
1674 * the action is changed whether the feature is enabled (DROP) or
1675 * disabled (PASSTHRU).
1677 if (idx == TAP_ISOLATE)
1678 remote_flow->msg.t.tcm_handle = ISOLATE_HANDLE;
1680 tap_flow_set_handle(remote_flow);
1681 if (priv_flow_process(pmd, attr, items, actions, NULL,
1682 remote_flow, implicit_rte_flows[idx].mirred)) {
1683 RTE_LOG(ERR, PMD, "rte flow rule validation failed\n");
1686 err = tap_nl_send(pmd->nlsk_fd, &msg->nh);
1688 RTE_LOG(ERR, PMD, "Failure sending nl request\n");
1691 err = tap_nl_recv_ack(pmd->nlsk_fd);
1694 "Kernel refused TC filter rule creation (%d): %s\n",
1695 errno, strerror(errno));
1698 LIST_INSERT_HEAD(&pmd->implicit_flows, remote_flow, next);
1702 rte_free(remote_flow);
1707 * Remove specific implicit flow rule on the remote device.
1709 * @param[in, out] pmd
1710 * Pointer to private structure.
1712 * The idx in the implicit_rte_flows array specifying which rule to remove.
1714 * @return -1 if one of the implicit rules couldn't be created, 0 otherwise.
1716 int tap_flow_implicit_destroy(struct pmd_internals *pmd,
1717 enum implicit_rule_index idx)
1719 struct rte_flow *remote_flow;
1721 int idx_prio = implicit_rte_flows[idx].attr.priority + PRIORITY_OFFSET;
1723 for (remote_flow = LIST_FIRST(&pmd->implicit_flows);
1725 remote_flow = LIST_NEXT(remote_flow, next)) {
1726 cur_prio = (remote_flow->msg.t.tcm_info >> 16) & PRIORITY_MASK;
1727 if (cur_prio != idx_prio)
1729 return tap_flow_destroy_pmd(pmd, remote_flow, NULL);
1735 * Destroy all implicit flows.
1737 * @see rte_flow_flush()
1740 tap_flow_implicit_flush(struct pmd_internals *pmd, struct rte_flow_error *error)
1742 struct rte_flow *remote_flow;
1744 while (!LIST_EMPTY(&pmd->implicit_flows)) {
1745 remote_flow = LIST_FIRST(&pmd->implicit_flows);
1746 if (tap_flow_destroy_pmd(pmd, remote_flow, error) < 0)
1752 #define MAX_RSS_KEYS 256
1753 #define SEC_NAME_CLS_Q "cls_q"
1755 const char *sec_name[SEC_MAX] = {
1756 [SEC_L3_L4] = "l3_l4",
1760 * Enable RSS on tap: create TC rules for queuing.
1762 * @param[in, out] pmd
1763 * Pointer to private structure.
1766 * Pointer to rte_flow to get flow group
1769 * Pointer to error reporting if not NULL.
1771 * @return 0 on success, negative value on failure.
1773 static int rss_enable(struct pmd_internals *pmd,
1774 const struct rte_flow_attr *attr,
1775 struct rte_flow_error *error)
1777 struct rte_flow *rss_flow = NULL;
1778 struct nlmsg *msg = NULL;
1779 /* 4096 is the maximum number of instructions for a BPF program */
1780 char annotation[64];
1784 /* unlimit locked memory */
1785 struct rlimit memlock_limit = {
1786 .rlim_cur = RLIM_INFINITY,
1787 .rlim_max = RLIM_INFINITY,
1789 setrlimit(RLIMIT_MEMLOCK, &memlock_limit);
1791 /* Get a new map key for a new RSS rule */
1792 err = bpf_rss_key(KEY_CMD_INIT, NULL);
1795 error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1796 "Failed to initialize BPF RSS keys");
1802 * Create BPF RSS MAP
1804 pmd->map_fd = tap_flow_bpf_rss_map_create(sizeof(__u32), /* key size */
1805 sizeof(struct rss_key),
1807 if (pmd->map_fd < 0) {
1809 "Failed to create BPF map (%d): %s\n",
1810 errno, strerror(errno));
1812 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1813 "Kernel too old or not configured "
1814 "to support BPF maps");
1820 * Add a rule per queue to match reclassified packets and direct them to
1821 * the correct queue.
1823 for (i = 0; i < pmd->dev->data->nb_rx_queues; i++) {
1824 pmd->bpf_fd[i] = tap_flow_bpf_cls_q(i);
1825 if (pmd->bpf_fd[i] < 0) {
1827 "Failed to load BPF section %s for queue %d",
1830 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1832 "Kernel too old or not configured "
1833 "to support BPF programs loading");
1838 rss_flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1841 "Cannot allocate memory for rte_flow");
1844 msg = &rss_flow->msg;
1845 tc_init_msg(msg, pmd->if_index, RTM_NEWTFILTER, NLM_F_REQUEST |
1846 NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
1847 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1848 tap_flow_set_handle(rss_flow);
1849 uint16_t group = attr->group << GROUP_SHIFT;
1850 uint16_t prio = group | (i + PRIORITY_OFFSET);
1851 msg->t.tcm_info = TC_H_MAKE(prio << 16, msg->t.tcm_info);
1852 msg->t.tcm_parent = TC_H_MAKE(MULTIQ_MAJOR_HANDLE, 0);
1854 tap_nlattr_add(&msg->nh, TCA_KIND, sizeof("bpf"), "bpf");
1855 if (tap_nlattr_nested_start(msg, TCA_OPTIONS) < 0)
1857 tap_nlattr_add32(&msg->nh, TCA_BPF_FD, pmd->bpf_fd[i]);
1858 snprintf(annotation, sizeof(annotation), "[%s%d]",
1860 tap_nlattr_add(&msg->nh, TCA_BPF_NAME, strlen(annotation) + 1,
1864 struct action_data adata = {
1868 .action = TC_ACT_PIPE,
1873 if (add_actions(rss_flow, 1, &adata, TCA_BPF_ACT) < 0)
1876 tap_nlattr_nested_finish(msg); /* nested TCA_OPTIONS */
1878 /* Netlink message is now ready to be sent */
1879 if (tap_nl_send(pmd->nlsk_fd, &msg->nh) < 0)
1881 err = tap_nl_recv_ack(pmd->nlsk_fd);
1884 "Kernel refused TC filter rule creation (%d): %s\n",
1885 errno, strerror(errno));
1888 LIST_INSERT_HEAD(&pmd->rss_flows, rss_flow, next);
1891 pmd->rss_enabled = 1;
1896 * Manage bpf RSS keys repository with operations: init, get, release
1899 * Command on RSS keys: init, get, release
1901 * @param[in, out] key_idx
1902 * Pointer to RSS Key index (out for get command, in for release command)
1904 * @return -1 if couldn't get, release or init the RSS keys, 0 otherwise.
1906 static int bpf_rss_key(enum bpf_rss_key_e cmd, __u32 *key_idx)
1910 static __u32 num_used_keys;
1911 static __u32 rss_keys[MAX_RSS_KEYS] = {KEY_STAT_UNSPEC};
1912 static __u32 rss_keys_initialized;
1916 if (!rss_keys_initialized)
1919 if (num_used_keys == RTE_DIM(rss_keys))
1922 *key_idx = num_used_keys % RTE_DIM(rss_keys);
1923 while (rss_keys[*key_idx] == KEY_STAT_USED)
1924 *key_idx = (*key_idx + 1) % RTE_DIM(rss_keys);
1926 rss_keys[*key_idx] = KEY_STAT_USED;
1931 case KEY_CMD_RELEASE:
1932 if (!rss_keys_initialized) {
1937 if (rss_keys[*key_idx] == KEY_STAT_USED) {
1938 rss_keys[*key_idx] = KEY_STAT_AVAILABLE;
1945 for (i = 0; i < RTE_DIM(rss_keys); i++)
1946 rss_keys[i] = KEY_STAT_AVAILABLE;
1948 rss_keys_initialized = 1;
1953 case KEY_CMD_DEINIT:
1954 for (i = 0; i < RTE_DIM(rss_keys); i++)
1955 rss_keys[i] = KEY_STAT_UNSPEC;
1957 rss_keys_initialized = 0;
1970 * Add RSS hash calculations and queue selection
1972 * @param[in, out] pmd
1973 * Pointer to internal structure. Used to set/get RSS map fd
1976 * Pointer to RSS flow actions
1979 * Pointer to error reporting if not NULL.
1981 * @return 0 on success, negative value on failure
1983 static int rss_add_actions(struct rte_flow *flow, struct pmd_internals *pmd,
1984 const struct rte_flow_action_rss *rss,
1985 struct rte_flow_error *error)
1987 /* 4096 is the maximum number of instructions for a BPF program */
1990 struct rss_key rss_entry = { .hash_fields = 0,
1993 /* Get a new map key for a new RSS rule */
1994 err = bpf_rss_key(KEY_CMD_GET, &flow->key_idx);
1997 error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1998 "Failed to get BPF RSS key");
2003 /* Update RSS map entry with queues */
2004 rss_entry.nb_queues = rss->num;
2005 for (i = 0; i < rss->num; i++)
2006 rss_entry.queues[i] = rss->queue[i];
2007 rss_entry.hash_fields =
2008 (1 << HASH_FIELD_IPV4_L3_L4) | (1 << HASH_FIELD_IPV6_L3_L4);
2010 /* Add this RSS entry to map */
2011 err = tap_flow_bpf_update_rss_elem(pmd->map_fd,
2012 &flow->key_idx, &rss_entry);
2016 "Failed to update BPF map entry #%u (%d): %s\n",
2017 flow->key_idx, errno, strerror(errno));
2019 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2020 "Kernel too old or not configured "
2021 "to support BPF maps updates");
2028 * Load bpf rules to calculate hash for this key_idx
2031 flow->bpf_fd[SEC_L3_L4] =
2032 tap_flow_bpf_calc_l3_l4_hash(flow->key_idx, pmd->map_fd);
2033 if (flow->bpf_fd[SEC_L3_L4] < 0) {
2035 "Failed to load BPF section %s (%d): %s\n",
2036 sec_name[SEC_L3_L4], errno, strerror(errno));
2038 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2039 "Kernel too old or not configured "
2040 "to support BPF program loading");
2047 struct action_data adata[] = {
2051 .bpf_fd = flow->bpf_fd[SEC_L3_L4],
2052 .annotation = sec_name[SEC_L3_L4],
2054 .action = TC_ACT_PIPE,
2060 if (add_actions(flow, RTE_DIM(adata), adata,
2061 TCA_FLOWER_ACT) < 0)
2069 * Manage filter operations.
2072 * Pointer to Ethernet device structure.
2073 * @param filter_type
2076 * Operation to perform.
2078 * Pointer to operation-specific structure.
2081 * 0 on success, negative errno value on failure.
2084 tap_dev_filter_ctrl(struct rte_eth_dev *dev,
2085 enum rte_filter_type filter_type,
2086 enum rte_filter_op filter_op,
2089 switch (filter_type) {
2090 case RTE_ETH_FILTER_GENERIC:
2091 if (filter_op != RTE_ETH_FILTER_GET)
2093 *(const void **)arg = &tap_flow_ops;
2096 RTE_LOG(ERR, PMD, "%p: filter type (%d) not supported\n",
2097 (void *)dev, filter_type);