1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018 6WIND S.A.
3 * Copyright 2018 Mellanox Technologies, Ltd
8 #include <libmnl/libmnl.h>
9 #include <linux/gen_stats.h>
10 #include <linux/if_ether.h>
11 #include <linux/netlink.h>
12 #include <linux/pkt_cls.h>
13 #include <linux/pkt_sched.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/tc_act/tc_gact.h>
16 #include <linux/tc_act/tc_mirred.h>
17 #include <netinet/in.h>
23 #include <sys/socket.h>
25 #include <rte_byteorder.h>
26 #include <rte_errno.h>
27 #include <rte_ether.h>
29 #include <rte_malloc.h>
30 #include <rte_common.h>
33 #include "mlx5_flow.h"
34 #include "mlx5_autoconf.h"
36 #ifdef HAVE_TC_ACT_VLAN
38 #include <linux/tc_act/tc_vlan.h>
40 #else /* HAVE_TC_ACT_VLAN */
42 #define TCA_VLAN_ACT_POP 1
43 #define TCA_VLAN_ACT_PUSH 2
44 #define TCA_VLAN_ACT_MODIFY 3
45 #define TCA_VLAN_PARMS 2
46 #define TCA_VLAN_PUSH_VLAN_ID 3
47 #define TCA_VLAN_PUSH_VLAN_PROTOCOL 4
48 #define TCA_VLAN_PAD 5
49 #define TCA_VLAN_PUSH_VLAN_PRIORITY 6
56 #endif /* HAVE_TC_ACT_VLAN */
58 #ifdef HAVE_TC_ACT_PEDIT
60 #include <linux/tc_act/tc_pedit.h>
62 #else /* HAVE_TC_ACT_VLAN */
76 TCA_PEDIT_KEY_EX_HTYPE = 1,
77 TCA_PEDIT_KEY_EX_CMD = 2,
78 __TCA_PEDIT_KEY_EX_MAX
81 enum pedit_header_type {
82 TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK = 0,
83 TCA_PEDIT_KEY_EX_HDR_TYPE_ETH = 1,
84 TCA_PEDIT_KEY_EX_HDR_TYPE_IP4 = 2,
85 TCA_PEDIT_KEY_EX_HDR_TYPE_IP6 = 3,
86 TCA_PEDIT_KEY_EX_HDR_TYPE_TCP = 4,
87 TCA_PEDIT_KEY_EX_HDR_TYPE_UDP = 5,
92 TCA_PEDIT_KEY_EX_CMD_SET = 0,
93 TCA_PEDIT_KEY_EX_CMD_ADD = 1,
100 __u32 off; /*offset */
107 struct tc_pedit_sel {
111 struct tc_pedit_key keys[0];
114 #endif /* HAVE_TC_ACT_VLAN */
116 #ifdef HAVE_TC_ACT_TUNNEL_KEY
118 #include <linux/tc_act/tc_tunnel_key.h>
120 #ifndef HAVE_TCA_TUNNEL_KEY_ENC_DST_PORT
121 #define TCA_TUNNEL_KEY_ENC_DST_PORT 9
124 #ifndef HAVE_TCA_TUNNEL_KEY_NO_CSUM
125 #define TCA_TUNNEL_KEY_NO_CSUM 10
128 #else /* HAVE_TC_ACT_TUNNEL_KEY */
130 #define TCA_ACT_TUNNEL_KEY 17
131 #define TCA_TUNNEL_KEY_ACT_SET 1
132 #define TCA_TUNNEL_KEY_ACT_RELEASE 2
133 #define TCA_TUNNEL_KEY_PARMS 2
134 #define TCA_TUNNEL_KEY_ENC_IPV4_SRC 3
135 #define TCA_TUNNEL_KEY_ENC_IPV4_DST 4
136 #define TCA_TUNNEL_KEY_ENC_IPV6_SRC 5
137 #define TCA_TUNNEL_KEY_ENC_IPV6_DST 6
138 #define TCA_TUNNEL_KEY_ENC_KEY_ID 7
139 #define TCA_TUNNEL_KEY_ENC_DST_PORT 9
140 #define TCA_TUNNEL_KEY_NO_CSUM 10
142 struct tc_tunnel_key {
147 #endif /* HAVE_TC_ACT_TUNNEL_KEY */
149 /* Normally found in linux/netlink.h. */
150 #ifndef NETLINK_CAP_ACK
151 #define NETLINK_CAP_ACK 10
154 /* Normally found in linux/pkt_sched.h. */
155 #ifndef TC_H_MIN_INGRESS
156 #define TC_H_MIN_INGRESS 0xfff2u
159 /* Normally found in linux/pkt_cls.h. */
160 #ifndef TCA_CLS_FLAGS_SKIP_SW
161 #define TCA_CLS_FLAGS_SKIP_SW (1 << 1)
163 #ifndef TCA_CLS_FLAGS_IN_HW
164 #define TCA_CLS_FLAGS_IN_HW (1 << 2)
166 #ifndef HAVE_TCA_CHAIN
169 #ifndef HAVE_TCA_FLOWER_ACT
170 #define TCA_FLOWER_ACT 3
172 #ifndef HAVE_TCA_FLOWER_FLAGS
173 #define TCA_FLOWER_FLAGS 22
175 #ifndef HAVE_TCA_FLOWER_KEY_ETH_TYPE
176 #define TCA_FLOWER_KEY_ETH_TYPE 8
178 #ifndef HAVE_TCA_FLOWER_KEY_ETH_DST
179 #define TCA_FLOWER_KEY_ETH_DST 4
181 #ifndef HAVE_TCA_FLOWER_KEY_ETH_DST_MASK
182 #define TCA_FLOWER_KEY_ETH_DST_MASK 5
184 #ifndef HAVE_TCA_FLOWER_KEY_ETH_SRC
185 #define TCA_FLOWER_KEY_ETH_SRC 6
187 #ifndef HAVE_TCA_FLOWER_KEY_ETH_SRC_MASK
188 #define TCA_FLOWER_KEY_ETH_SRC_MASK 7
190 #ifndef HAVE_TCA_FLOWER_KEY_IP_PROTO
191 #define TCA_FLOWER_KEY_IP_PROTO 9
193 #ifndef HAVE_TCA_FLOWER_KEY_IPV4_SRC
194 #define TCA_FLOWER_KEY_IPV4_SRC 10
196 #ifndef HAVE_TCA_FLOWER_KEY_IPV4_SRC_MASK
197 #define TCA_FLOWER_KEY_IPV4_SRC_MASK 11
199 #ifndef HAVE_TCA_FLOWER_KEY_IPV4_DST
200 #define TCA_FLOWER_KEY_IPV4_DST 12
202 #ifndef HAVE_TCA_FLOWER_KEY_IPV4_DST_MASK
203 #define TCA_FLOWER_KEY_IPV4_DST_MASK 13
205 #ifndef HAVE_TCA_FLOWER_KEY_IPV6_SRC
206 #define TCA_FLOWER_KEY_IPV6_SRC 14
208 #ifndef HAVE_TCA_FLOWER_KEY_IPV6_SRC_MASK
209 #define TCA_FLOWER_KEY_IPV6_SRC_MASK 15
211 #ifndef HAVE_TCA_FLOWER_KEY_IPV6_DST
212 #define TCA_FLOWER_KEY_IPV6_DST 16
214 #ifndef HAVE_TCA_FLOWER_KEY_IPV6_DST_MASK
215 #define TCA_FLOWER_KEY_IPV6_DST_MASK 17
217 #ifndef HAVE_TCA_FLOWER_KEY_TCP_SRC
218 #define TCA_FLOWER_KEY_TCP_SRC 18
220 #ifndef HAVE_TCA_FLOWER_KEY_TCP_SRC_MASK
221 #define TCA_FLOWER_KEY_TCP_SRC_MASK 35
223 #ifndef HAVE_TCA_FLOWER_KEY_TCP_DST
224 #define TCA_FLOWER_KEY_TCP_DST 19
226 #ifndef HAVE_TCA_FLOWER_KEY_TCP_DST_MASK
227 #define TCA_FLOWER_KEY_TCP_DST_MASK 36
229 #ifndef HAVE_TCA_FLOWER_KEY_UDP_SRC
230 #define TCA_FLOWER_KEY_UDP_SRC 20
232 #ifndef HAVE_TCA_FLOWER_KEY_UDP_SRC_MASK
233 #define TCA_FLOWER_KEY_UDP_SRC_MASK 37
235 #ifndef HAVE_TCA_FLOWER_KEY_UDP_DST
236 #define TCA_FLOWER_KEY_UDP_DST 21
238 #ifndef HAVE_TCA_FLOWER_KEY_UDP_DST_MASK
239 #define TCA_FLOWER_KEY_UDP_DST_MASK 38
241 #ifndef HAVE_TCA_FLOWER_KEY_VLAN_ID
242 #define TCA_FLOWER_KEY_VLAN_ID 23
244 #ifndef HAVE_TCA_FLOWER_KEY_VLAN_PRIO
245 #define TCA_FLOWER_KEY_VLAN_PRIO 24
247 #ifndef HAVE_TCA_FLOWER_KEY_VLAN_ETH_TYPE
248 #define TCA_FLOWER_KEY_VLAN_ETH_TYPE 25
250 #ifndef HAVE_TCA_FLOWER_KEY_ENC_KEY_ID
251 #define TCA_FLOWER_KEY_ENC_KEY_ID 26
253 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV4_SRC
254 #define TCA_FLOWER_KEY_ENC_IPV4_SRC 27
256 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK
257 #define TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK 28
259 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV4_DST
260 #define TCA_FLOWER_KEY_ENC_IPV4_DST 29
262 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV4_DST_MASK
263 #define TCA_FLOWER_KEY_ENC_IPV4_DST_MASK 30
265 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV6_SRC
266 #define TCA_FLOWER_KEY_ENC_IPV6_SRC 31
268 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK
269 #define TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK 32
271 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV6_DST
272 #define TCA_FLOWER_KEY_ENC_IPV6_DST 33
274 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV6_DST_MASK
275 #define TCA_FLOWER_KEY_ENC_IPV6_DST_MASK 34
277 #ifndef HAVE_TCA_FLOWER_KEY_ENC_UDP_SRC_PORT
278 #define TCA_FLOWER_KEY_ENC_UDP_SRC_PORT 43
280 #ifndef HAVE_TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK
281 #define TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK 44
283 #ifndef HAVE_TCA_FLOWER_KEY_ENC_UDP_DST_PORT
284 #define TCA_FLOWER_KEY_ENC_UDP_DST_PORT 45
286 #ifndef HAVE_TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK
287 #define TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK 46
289 #ifndef HAVE_TCA_FLOWER_KEY_TCP_FLAGS
290 #define TCA_FLOWER_KEY_TCP_FLAGS 71
292 #ifndef HAVE_TCA_FLOWER_KEY_TCP_FLAGS_MASK
293 #define TCA_FLOWER_KEY_TCP_FLAGS_MASK 72
295 #ifndef HAVE_TC_ACT_GOTO_CHAIN
296 #define TC_ACT_GOTO_CHAIN 0x20000000
299 #ifndef IPV6_ADDR_LEN
300 #define IPV6_ADDR_LEN 16
303 #ifndef IPV4_ADDR_LEN
304 #define IPV4_ADDR_LEN 4
308 #define TP_PORT_LEN 2 /* Transport Port (UDP/TCP) Length */
315 #ifndef TCA_ACT_MAX_PRIO
316 #define TCA_ACT_MAX_PRIO 32
319 /** UDP port range of VXLAN devices created by driver. */
320 #define MLX5_VXLAN_PORT_MIN 30000
321 #define MLX5_VXLAN_PORT_MAX 60000
322 #define MLX5_VXLAN_DEVICE_PFX "vmlx_"
324 /** Tunnel action type, used for @p type in header structure. */
325 enum flow_tcf_tunact_type {
326 FLOW_TCF_TUNACT_VXLAN_DECAP,
327 FLOW_TCF_TUNACT_VXLAN_ENCAP,
330 /** Flags used for @p mask in tunnel action encap descriptors. */
331 #define FLOW_TCF_ENCAP_ETH_SRC (1u << 0)
332 #define FLOW_TCF_ENCAP_ETH_DST (1u << 1)
333 #define FLOW_TCF_ENCAP_IPV4_SRC (1u << 2)
334 #define FLOW_TCF_ENCAP_IPV4_DST (1u << 3)
335 #define FLOW_TCF_ENCAP_IPV6_SRC (1u << 4)
336 #define FLOW_TCF_ENCAP_IPV6_DST (1u << 5)
337 #define FLOW_TCF_ENCAP_UDP_SRC (1u << 6)
338 #define FLOW_TCF_ENCAP_UDP_DST (1u << 7)
339 #define FLOW_TCF_ENCAP_VXLAN_VNI (1u << 8)
342 * Structure for holding netlink context.
343 * Note the size of the message buffer which is MNL_SOCKET_BUFFER_SIZE.
344 * Using this (8KB) buffer size ensures that netlink messages will never be
347 struct mlx5_flow_tcf_context {
348 struct mnl_socket *nl; /* NETLINK_ROUTE libmnl socket. */
349 uint32_t seq; /* Message sequence number. */
350 uint32_t buf_size; /* Message buffer size. */
351 uint8_t *buf; /* Message buffer. */
355 * Neigh rule structure. The neigh rule is applied via Netlink to
356 * outer tunnel iface in order to provide destination MAC address
357 * for the VXLAN encapsultion. The neigh rule is implicitly related
358 * to the Flow itself and can be shared by multiple Flows.
360 struct tcf_neigh_rule {
361 LIST_ENTRY(tcf_neigh_rule) next;
363 struct ether_addr eth;
370 uint8_t dst[IPV6_ADDR_LEN];
376 * Local rule structure. The local rule is applied via Netlink to
377 * outer tunnel iface in order to provide local and peer IP addresses
378 * of the VXLAN tunnel for encapsulation. The local rule is implicitly
379 * related to the Flow itself and can be shared by multiple Flows.
381 struct tcf_local_rule {
382 LIST_ENTRY(tcf_local_rule) next;
391 uint8_t dst[IPV6_ADDR_LEN];
392 uint8_t src[IPV6_ADDR_LEN];
397 /** Outer interface VXLAN encapsulation rules container. */
399 LIST_ENTRY(tcf_irule) next;
400 LIST_HEAD(, tcf_neigh_rule) neigh;
401 LIST_HEAD(, tcf_local_rule) local;
403 unsigned int ifouter; /**< Own interface index. */
406 /** VXLAN virtual netdev. */
408 LIST_ENTRY(tcf_vtep) next;
410 unsigned int ifindex; /**< Own interface index. */
415 /** Tunnel descriptor header, common for all tunnel types. */
416 struct flow_tcf_tunnel_hdr {
417 uint32_t type; /**< Tunnel action type. */
418 struct tcf_vtep *vtep; /**< Virtual tunnel endpoint device. */
419 unsigned int ifindex_org; /**< Original dst/src interface */
420 unsigned int *ifindex_ptr; /**< Interface ptr in message. */
423 struct flow_tcf_vxlan_decap {
424 struct flow_tcf_tunnel_hdr hdr;
428 struct flow_tcf_vxlan_encap {
429 struct flow_tcf_tunnel_hdr hdr;
430 struct tcf_irule *iface;
433 struct ether_addr dst;
434 struct ether_addr src;
442 uint8_t dst[IPV6_ADDR_LEN];
443 uint8_t src[IPV6_ADDR_LEN];
455 /** Structure used when extracting the values of a flow counters
456 * from a netlink message.
458 struct flow_tcf_stats_basic {
460 struct gnet_stats_basic counters;
463 /** Empty masks for known item types. */
465 struct rte_flow_item_port_id port_id;
466 struct rte_flow_item_eth eth;
467 struct rte_flow_item_vlan vlan;
468 struct rte_flow_item_ipv4 ipv4;
469 struct rte_flow_item_ipv6 ipv6;
470 struct rte_flow_item_tcp tcp;
471 struct rte_flow_item_udp udp;
472 struct rte_flow_item_vxlan vxlan;
473 } flow_tcf_mask_empty = {
477 /** Supported masks for known item types. */
478 static const struct {
479 struct rte_flow_item_port_id port_id;
480 struct rte_flow_item_eth eth;
481 struct rte_flow_item_vlan vlan;
482 struct rte_flow_item_ipv4 ipv4;
483 struct rte_flow_item_ipv6 ipv6;
484 struct rte_flow_item_tcp tcp;
485 struct rte_flow_item_udp udp;
486 struct rte_flow_item_vxlan vxlan;
487 } flow_tcf_mask_supported = {
492 .type = RTE_BE16(0xffff),
493 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
494 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
497 /* PCP and VID only, no DEI. */
498 .tci = RTE_BE16(0xefff),
499 .inner_type = RTE_BE16(0xffff),
502 .next_proto_id = 0xff,
503 .src_addr = RTE_BE32(0xffffffff),
504 .dst_addr = RTE_BE32(0xffffffff),
509 "\xff\xff\xff\xff\xff\xff\xff\xff"
510 "\xff\xff\xff\xff\xff\xff\xff\xff",
512 "\xff\xff\xff\xff\xff\xff\xff\xff"
513 "\xff\xff\xff\xff\xff\xff\xff\xff",
516 .src_port = RTE_BE16(0xffff),
517 .dst_port = RTE_BE16(0xffff),
521 .src_port = RTE_BE16(0xffff),
522 .dst_port = RTE_BE16(0xffff),
525 .vni = "\xff\xff\xff",
529 #define SZ_NLATTR_HDR MNL_ALIGN(sizeof(struct nlattr))
530 #define SZ_NLATTR_NEST SZ_NLATTR_HDR
531 #define SZ_NLATTR_DATA_OF(len) MNL_ALIGN(SZ_NLATTR_HDR + (len))
532 #define SZ_NLATTR_TYPE_OF(typ) SZ_NLATTR_DATA_OF(sizeof(typ))
533 #define SZ_NLATTR_STRZ_OF(str) SZ_NLATTR_DATA_OF(strlen(str) + 1)
535 #define PTOI_TABLE_SZ_MAX(dev) (mlx5_dev_to_port_id((dev)->device, NULL, 0) + 2)
537 /** DPDK port to network interface index (ifindex) conversion. */
538 struct flow_tcf_ptoi {
539 uint16_t port_id; /**< DPDK port ID. */
540 unsigned int ifindex; /**< Network interface index. */
543 /* Due to a limitation on driver/FW. */
544 #define MLX5_TCF_GROUP_ID_MAX 3
547 * Due to a limitation on driver/FW, priority ranges from 1 to 16 in kernel.
548 * Priority in rte_flow attribute starts from 0 and is added by 1 in
549 * translation. This is subject to be changed to determine the max priority
550 * based on trial-and-error like Verbs driver once the restriction is lifted or
551 * the range is extended.
553 #define MLX5_TCF_GROUP_PRIORITY_MAX 15
555 #define MLX5_TCF_FATE_ACTIONS \
556 (MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_PORT_ID | \
557 MLX5_FLOW_ACTION_JUMP)
559 #define MLX5_TCF_VLAN_ACTIONS \
560 (MLX5_FLOW_ACTION_OF_POP_VLAN | MLX5_FLOW_ACTION_OF_PUSH_VLAN | \
561 MLX5_FLOW_ACTION_OF_SET_VLAN_VID | MLX5_FLOW_ACTION_OF_SET_VLAN_PCP)
563 #define MLX5_TCF_VXLAN_ACTIONS \
564 (MLX5_FLOW_ACTION_VXLAN_ENCAP | MLX5_FLOW_ACTION_VXLAN_DECAP)
566 #define MLX5_TCF_PEDIT_ACTIONS \
567 (MLX5_FLOW_ACTION_SET_IPV4_SRC | MLX5_FLOW_ACTION_SET_IPV4_DST | \
568 MLX5_FLOW_ACTION_SET_IPV6_SRC | MLX5_FLOW_ACTION_SET_IPV6_DST | \
569 MLX5_FLOW_ACTION_SET_TP_SRC | MLX5_FLOW_ACTION_SET_TP_DST | \
570 MLX5_FLOW_ACTION_SET_TTL | MLX5_FLOW_ACTION_DEC_TTL | \
571 MLX5_FLOW_ACTION_SET_MAC_SRC | MLX5_FLOW_ACTION_SET_MAC_DST)
573 #define MLX5_TCF_CONFIG_ACTIONS \
574 (MLX5_FLOW_ACTION_PORT_ID | MLX5_FLOW_ACTION_JUMP | \
575 MLX5_FLOW_ACTION_OF_PUSH_VLAN | MLX5_FLOW_ACTION_OF_SET_VLAN_VID | \
576 MLX5_FLOW_ACTION_OF_SET_VLAN_PCP | \
577 (MLX5_TCF_PEDIT_ACTIONS & ~MLX5_FLOW_ACTION_DEC_TTL))
579 #define MAX_PEDIT_KEYS 128
580 #define SZ_PEDIT_KEY_VAL 4
582 #define NUM_OF_PEDIT_KEYS(sz) \
583 (((sz) / SZ_PEDIT_KEY_VAL) + (((sz) % SZ_PEDIT_KEY_VAL) ? 1 : 0))
585 struct pedit_key_ex {
586 enum pedit_header_type htype;
590 struct pedit_parser {
591 struct tc_pedit_sel sel;
592 struct tc_pedit_key keys[MAX_PEDIT_KEYS];
593 struct pedit_key_ex keys_ex[MAX_PEDIT_KEYS];
597 * Create space for using the implicitly created TC flow counter.
600 * Pointer to the Ethernet device structure.
603 * A pointer to the counter data structure, NULL otherwise and
606 static struct mlx5_flow_counter *
607 flow_tcf_counter_new(void)
609 struct mlx5_flow_counter *cnt;
612 * eswitch counter cannot be shared and its id is unknown.
613 * currently returning all with id 0.
614 * in the future maybe better to switch to unique numbers.
616 struct mlx5_flow_counter tmpl = {
619 cnt = rte_calloc(__func__, 1, sizeof(*cnt), 0);
625 /* Implicit counter, do not add to list. */
630 * Set pedit key of MAC address
633 * pointer to action specification
634 * @param[in,out] p_parser
635 * pointer to pedit_parser
638 flow_tcf_pedit_key_set_mac(const struct rte_flow_action *actions,
639 struct pedit_parser *p_parser)
641 int idx = p_parser->sel.nkeys;
642 uint32_t off = actions->type == RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
643 offsetof(struct ether_hdr, s_addr) :
644 offsetof(struct ether_hdr, d_addr);
645 const struct rte_flow_action_set_mac *conf =
646 (const struct rte_flow_action_set_mac *)actions->conf;
648 p_parser->keys[idx].off = off;
649 p_parser->keys[idx].mask = ~UINT32_MAX;
650 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_ETH;
651 p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_SET;
652 memcpy(&p_parser->keys[idx].val,
653 conf->mac_addr, SZ_PEDIT_KEY_VAL);
655 p_parser->keys[idx].off = off + SZ_PEDIT_KEY_VAL;
656 p_parser->keys[idx].mask = 0xFFFF0000;
657 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_ETH;
658 p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_SET;
659 memcpy(&p_parser->keys[idx].val,
660 conf->mac_addr + SZ_PEDIT_KEY_VAL,
661 ETHER_ADDR_LEN - SZ_PEDIT_KEY_VAL);
662 p_parser->sel.nkeys = (++idx);
666 * Set pedit key of decrease/set ttl
669 * pointer to action specification
670 * @param[in,out] p_parser
671 * pointer to pedit_parser
672 * @param[in] item_flags
673 * flags of all items presented
676 flow_tcf_pedit_key_set_dec_ttl(const struct rte_flow_action *actions,
677 struct pedit_parser *p_parser,
680 int idx = p_parser->sel.nkeys;
682 p_parser->keys[idx].mask = 0xFFFFFF00;
683 if (item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV4) {
684 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_IP4;
685 p_parser->keys[idx].off =
686 offsetof(struct ipv4_hdr, time_to_live);
688 if (item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV6) {
689 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6;
690 p_parser->keys[idx].off =
691 offsetof(struct ipv6_hdr, hop_limits);
693 if (actions->type == RTE_FLOW_ACTION_TYPE_DEC_TTL) {
694 p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_ADD;
695 p_parser->keys[idx].val = 0x000000FF;
697 p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_SET;
698 p_parser->keys[idx].val =
699 (__u32)((const struct rte_flow_action_set_ttl *)
700 actions->conf)->ttl_value;
702 p_parser->sel.nkeys = (++idx);
706 * Set pedit key of transport (TCP/UDP) port value
709 * pointer to action specification
710 * @param[in,out] p_parser
711 * pointer to pedit_parser
712 * @param[in] item_flags
713 * flags of all items presented
716 flow_tcf_pedit_key_set_tp_port(const struct rte_flow_action *actions,
717 struct pedit_parser *p_parser,
720 int idx = p_parser->sel.nkeys;
722 if (item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP)
723 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_UDP;
724 if (item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP)
725 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_TCP;
726 p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_SET;
727 /* offset of src/dst port is same for TCP and UDP */
728 p_parser->keys[idx].off =
729 actions->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
730 offsetof(struct tcp_hdr, src_port) :
731 offsetof(struct tcp_hdr, dst_port);
732 p_parser->keys[idx].mask = 0xFFFF0000;
733 p_parser->keys[idx].val =
734 (__u32)((const struct rte_flow_action_set_tp *)
735 actions->conf)->port;
736 p_parser->sel.nkeys = (++idx);
740 * Set pedit key of ipv6 address
743 * pointer to action specification
744 * @param[in,out] p_parser
745 * pointer to pedit_parser
748 flow_tcf_pedit_key_set_ipv6_addr(const struct rte_flow_action *actions,
749 struct pedit_parser *p_parser)
751 int idx = p_parser->sel.nkeys;
752 int keys = NUM_OF_PEDIT_KEYS(IPV6_ADDR_LEN);
754 actions->type == RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
755 offsetof(struct ipv6_hdr, src_addr) :
756 offsetof(struct ipv6_hdr, dst_addr);
757 const struct rte_flow_action_set_ipv6 *conf =
758 (const struct rte_flow_action_set_ipv6 *)actions->conf;
760 for (int i = 0; i < keys; i++, idx++) {
761 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6;
762 p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_SET;
763 p_parser->keys[idx].off = off_base + i * SZ_PEDIT_KEY_VAL;
764 p_parser->keys[idx].mask = ~UINT32_MAX;
765 memcpy(&p_parser->keys[idx].val,
766 conf->ipv6_addr + i * SZ_PEDIT_KEY_VAL,
769 p_parser->sel.nkeys += keys;
773 * Set pedit key of ipv4 address
776 * pointer to action specification
777 * @param[in,out] p_parser
778 * pointer to pedit_parser
781 flow_tcf_pedit_key_set_ipv4_addr(const struct rte_flow_action *actions,
782 struct pedit_parser *p_parser)
784 int idx = p_parser->sel.nkeys;
786 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_IP4;
787 p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_SET;
788 p_parser->keys[idx].off =
789 actions->type == RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
790 offsetof(struct ipv4_hdr, src_addr) :
791 offsetof(struct ipv4_hdr, dst_addr);
792 p_parser->keys[idx].mask = ~UINT32_MAX;
793 p_parser->keys[idx].val =
794 ((const struct rte_flow_action_set_ipv4 *)
795 actions->conf)->ipv4_addr;
796 p_parser->sel.nkeys = (++idx);
800 * Create the pedit's na attribute in netlink message
801 * on pre-allocate message buffer
804 * pointer to pre-allocated netlink message buffer
805 * @param[in,out] actions
806 * pointer to pointer of actions specification.
807 * @param[in,out] action_flags
808 * pointer to actions flags
809 * @param[in] item_flags
810 * flags of all item presented
813 flow_tcf_create_pedit_mnl_msg(struct nlmsghdr *nl,
814 const struct rte_flow_action **actions,
817 struct pedit_parser p_parser;
818 struct nlattr *na_act_options;
819 struct nlattr *na_pedit_keys;
821 memset(&p_parser, 0, sizeof(p_parser));
822 mnl_attr_put_strz(nl, TCA_ACT_KIND, "pedit");
823 na_act_options = mnl_attr_nest_start(nl, TCA_ACT_OPTIONS);
824 /* all modify header actions should be in one tc-pedit action */
825 for (; (*actions)->type != RTE_FLOW_ACTION_TYPE_END; (*actions)++) {
826 switch ((*actions)->type) {
827 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
828 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
829 flow_tcf_pedit_key_set_ipv4_addr(*actions, &p_parser);
831 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
832 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
833 flow_tcf_pedit_key_set_ipv6_addr(*actions, &p_parser);
835 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
836 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
837 flow_tcf_pedit_key_set_tp_port(*actions,
838 &p_parser, item_flags);
840 case RTE_FLOW_ACTION_TYPE_SET_TTL:
841 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
842 flow_tcf_pedit_key_set_dec_ttl(*actions,
843 &p_parser, item_flags);
845 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
846 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
847 flow_tcf_pedit_key_set_mac(*actions, &p_parser);
850 goto pedit_mnl_msg_done;
854 p_parser.sel.action = TC_ACT_PIPE;
855 mnl_attr_put(nl, TCA_PEDIT_PARMS_EX,
856 sizeof(p_parser.sel) +
857 p_parser.sel.nkeys * sizeof(struct tc_pedit_key),
860 mnl_attr_nest_start(nl, TCA_PEDIT_KEYS_EX | NLA_F_NESTED);
861 for (int i = 0; i < p_parser.sel.nkeys; i++) {
862 struct nlattr *na_pedit_key =
863 mnl_attr_nest_start(nl,
864 TCA_PEDIT_KEY_EX | NLA_F_NESTED);
865 mnl_attr_put_u16(nl, TCA_PEDIT_KEY_EX_HTYPE,
866 p_parser.keys_ex[i].htype);
867 mnl_attr_put_u16(nl, TCA_PEDIT_KEY_EX_CMD,
868 p_parser.keys_ex[i].cmd);
869 mnl_attr_nest_end(nl, na_pedit_key);
871 mnl_attr_nest_end(nl, na_pedit_keys);
872 mnl_attr_nest_end(nl, na_act_options);
877 * Calculate max memory size of one TC-pedit actions.
878 * One TC-pedit action can contain set of keys each defining
879 * a rewrite element (rte_flow action)
881 * @param[in,out] actions
882 * actions specification.
883 * @param[in,out] action_flags
885 * @param[in,out] size
888 * Max memory size of one TC-pedit action
891 flow_tcf_get_pedit_actions_size(const struct rte_flow_action **actions,
892 uint64_t *action_flags)
898 pedit_size += SZ_NLATTR_NEST + /* na_act_index. */
899 SZ_NLATTR_STRZ_OF("pedit") +
900 SZ_NLATTR_NEST; /* TCA_ACT_OPTIONS. */
901 for (; (*actions)->type != RTE_FLOW_ACTION_TYPE_END; (*actions)++) {
902 switch ((*actions)->type) {
903 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
904 keys += NUM_OF_PEDIT_KEYS(IPV4_ADDR_LEN);
905 flags |= MLX5_FLOW_ACTION_SET_IPV4_SRC;
907 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
908 keys += NUM_OF_PEDIT_KEYS(IPV4_ADDR_LEN);
909 flags |= MLX5_FLOW_ACTION_SET_IPV4_DST;
911 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
912 keys += NUM_OF_PEDIT_KEYS(IPV6_ADDR_LEN);
913 flags |= MLX5_FLOW_ACTION_SET_IPV6_SRC;
915 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
916 keys += NUM_OF_PEDIT_KEYS(IPV6_ADDR_LEN);
917 flags |= MLX5_FLOW_ACTION_SET_IPV6_DST;
919 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
920 /* TCP is as same as UDP */
921 keys += NUM_OF_PEDIT_KEYS(TP_PORT_LEN);
922 flags |= MLX5_FLOW_ACTION_SET_TP_SRC;
924 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
925 /* TCP is as same as UDP */
926 keys += NUM_OF_PEDIT_KEYS(TP_PORT_LEN);
927 flags |= MLX5_FLOW_ACTION_SET_TP_DST;
929 case RTE_FLOW_ACTION_TYPE_SET_TTL:
930 keys += NUM_OF_PEDIT_KEYS(TTL_LEN);
931 flags |= MLX5_FLOW_ACTION_SET_TTL;
933 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
934 keys += NUM_OF_PEDIT_KEYS(TTL_LEN);
935 flags |= MLX5_FLOW_ACTION_DEC_TTL;
937 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
938 keys += NUM_OF_PEDIT_KEYS(ETHER_ADDR_LEN);
939 flags |= MLX5_FLOW_ACTION_SET_MAC_SRC;
941 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
942 keys += NUM_OF_PEDIT_KEYS(ETHER_ADDR_LEN);
943 flags |= MLX5_FLOW_ACTION_SET_MAC_DST;
946 goto get_pedit_action_size_done;
949 get_pedit_action_size_done:
950 /* TCA_PEDIT_PARAMS_EX */
952 SZ_NLATTR_DATA_OF(sizeof(struct tc_pedit_sel) +
953 keys * sizeof(struct tc_pedit_key));
954 pedit_size += SZ_NLATTR_NEST; /* TCA_PEDIT_KEYS */
956 /* TCA_PEDIT_KEY_EX + HTYPE + CMD */
957 (SZ_NLATTR_NEST + SZ_NLATTR_DATA_OF(2) +
958 SZ_NLATTR_DATA_OF(2));
959 (*action_flags) |= flags;
965 * Retrieve mask for pattern item.
967 * This function does basic sanity checks on a pattern item in order to
968 * return the most appropriate mask for it.
971 * Item specification.
972 * @param[in] mask_default
973 * Default mask for pattern item as specified by the flow API.
974 * @param[in] mask_supported
975 * Mask fields supported by the implementation.
976 * @param[in] mask_empty
977 * Empty mask to return when there is no specification.
979 * Perform verbose error reporting if not NULL.
982 * Either @p item->mask or one of the mask parameters on success, NULL
983 * otherwise and rte_errno is set.
986 flow_tcf_item_mask(const struct rte_flow_item *item, const void *mask_default,
987 const void *mask_supported, const void *mask_empty,
988 size_t mask_size, struct rte_flow_error *error)
993 /* item->last and item->mask cannot exist without item->spec. */
994 if (!item->spec && (item->mask || item->last)) {
995 rte_flow_error_set(error, EINVAL,
996 RTE_FLOW_ERROR_TYPE_ITEM, item,
997 "\"mask\" or \"last\" field provided without"
998 " a corresponding \"spec\"");
1001 /* No spec, no mask, no problem. */
1004 mask = item->mask ? item->mask : mask_default;
1007 * Single-pass check to make sure that:
1008 * - Mask is supported, no bits are set outside mask_supported.
1009 * - Both item->spec and item->last are included in mask.
1011 for (i = 0; i != mask_size; ++i) {
1014 if ((mask[i] | ((const uint8_t *)mask_supported)[i]) !=
1015 ((const uint8_t *)mask_supported)[i]) {
1016 rte_flow_error_set(error, ENOTSUP,
1017 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1018 "unsupported field found"
1023 (((const uint8_t *)item->spec)[i] & mask[i]) !=
1024 (((const uint8_t *)item->last)[i] & mask[i])) {
1025 rte_flow_error_set(error, EINVAL,
1026 RTE_FLOW_ERROR_TYPE_ITEM_LAST,
1028 "range between \"spec\" and \"last\""
1029 " not comprised in \"mask\"");
1037 * Build a conversion table between port ID and ifindex.
1040 * Pointer to Ethernet device.
1042 * Pointer to ptoi table.
1044 * Size of ptoi table provided.
1047 * Size of ptoi table filled.
1050 flow_tcf_build_ptoi_table(struct rte_eth_dev *dev, struct flow_tcf_ptoi *ptoi,
1053 unsigned int n = mlx5_dev_to_port_id(dev->device, NULL, 0);
1054 uint16_t port_id[n + 1];
1056 unsigned int own = 0;
1058 /* At least one port is needed when no switch domain is present. */
1061 port_id[0] = dev->data->port_id;
1063 n = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, n), n);
1067 for (i = 0; i != n; ++i) {
1068 struct rte_eth_dev_info dev_info;
1070 rte_eth_dev_info_get(port_id[i], &dev_info);
1071 if (port_id[i] == dev->data->port_id)
1073 ptoi[i].port_id = port_id[i];
1074 ptoi[i].ifindex = dev_info.if_index;
1076 /* Ensure first entry of ptoi[] is the current device. */
1079 ptoi[0] = ptoi[own];
1080 ptoi[own] = ptoi[n];
1082 /* An entry with zero ifindex terminates ptoi[]. */
1083 ptoi[n].port_id = 0;
1084 ptoi[n].ifindex = 0;
1089 * Verify the @p attr will be correctly understood by the E-switch.
1092 * Pointer to flow attributes
1094 * Pointer to error structure.
1097 * 0 on success, a negative errno value otherwise and rte_errno is set.
1100 flow_tcf_validate_attributes(const struct rte_flow_attr *attr,
1101 struct rte_flow_error *error)
1104 * Supported attributes: groups, some priorities and ingress only.
1105 * group is supported only if kernel supports chain. Don't care about
1106 * transfer as it is the caller's problem.
1108 if (attr->group > MLX5_TCF_GROUP_ID_MAX)
1109 return rte_flow_error_set(error, ENOTSUP,
1110 RTE_FLOW_ERROR_TYPE_ATTR_GROUP, attr,
1111 "group ID larger than "
1112 RTE_STR(MLX5_TCF_GROUP_ID_MAX)
1113 " isn't supported");
1114 else if (attr->priority > MLX5_TCF_GROUP_PRIORITY_MAX)
1115 return rte_flow_error_set(error, ENOTSUP,
1116 RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1118 "priority more than "
1119 RTE_STR(MLX5_TCF_GROUP_PRIORITY_MAX)
1120 " is not supported");
1122 return rte_flow_error_set(error, EINVAL,
1123 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1124 attr, "only ingress is supported");
1126 return rte_flow_error_set(error, ENOTSUP,
1127 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1128 attr, "egress is not supported");
1133 * Validate VXLAN_ENCAP action RTE_FLOW_ITEM_TYPE_ETH item for E-Switch.
1134 * The routine checks the L2 fields to be used in encapsulation header.
1137 * Pointer to the item structure.
1139 * Pointer to the error structure.
1142 * 0 on success, a negative errno value otherwise and rte_errno is set.
1145 flow_tcf_validate_vxlan_encap_eth(const struct rte_flow_item *item,
1146 struct rte_flow_error *error)
1148 const struct rte_flow_item_eth *spec = item->spec;
1149 const struct rte_flow_item_eth *mask = item->mask;
1153 * Specification for L2 addresses can be empty
1154 * because these ones are optional and not
1155 * required directly by tc rule. Kernel tries
1156 * to resolve these ones on its own
1161 /* If mask is not specified use the default one. */
1162 mask = &rte_flow_item_eth_mask;
1164 if (memcmp(&mask->dst,
1165 &flow_tcf_mask_empty.eth.dst,
1166 sizeof(flow_tcf_mask_empty.eth.dst))) {
1167 if (memcmp(&mask->dst,
1168 &rte_flow_item_eth_mask.dst,
1169 sizeof(rte_flow_item_eth_mask.dst)))
1170 return rte_flow_error_set
1172 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1173 "no support for partial mask on"
1174 " \"eth.dst\" field");
1176 if (memcmp(&mask->src,
1177 &flow_tcf_mask_empty.eth.src,
1178 sizeof(flow_tcf_mask_empty.eth.src))) {
1179 if (memcmp(&mask->src,
1180 &rte_flow_item_eth_mask.src,
1181 sizeof(rte_flow_item_eth_mask.src)))
1182 return rte_flow_error_set
1184 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1185 "no support for partial mask on"
1186 " \"eth.src\" field");
1188 if (mask->type != RTE_BE16(0x0000)) {
1189 if (mask->type != RTE_BE16(0xffff))
1190 return rte_flow_error_set
1192 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1193 "no support for partial mask on"
1194 " \"eth.type\" field");
1196 "outer ethernet type field"
1197 " cannot be forced for vxlan"
1198 " encapsulation, parameter ignored");
1204 * Validate VXLAN_ENCAP action RTE_FLOW_ITEM_TYPE_IPV4 item for E-Switch.
1205 * The routine checks the IPv4 fields to be used in encapsulation header.
1208 * Pointer to the item structure.
1210 * Pointer to the error structure.
1213 * 0 on success, a negative errno value otherwise and rte_errno is set.
1216 flow_tcf_validate_vxlan_encap_ipv4(const struct rte_flow_item *item,
1217 struct rte_flow_error *error)
1219 const struct rte_flow_item_ipv4 *spec = item->spec;
1220 const struct rte_flow_item_ipv4 *mask = item->mask;
1224 * Specification for IP addresses cannot be empty
1225 * because it is required by tunnel_key parameter.
1227 return rte_flow_error_set(error, EINVAL,
1228 RTE_FLOW_ERROR_TYPE_ITEM, item,
1229 "NULL outer ipv4 address"
1230 " specification for vxlan"
1234 mask = &rte_flow_item_ipv4_mask;
1235 if (mask->hdr.dst_addr != RTE_BE32(0x00000000)) {
1236 if (mask->hdr.dst_addr != RTE_BE32(0xffffffff))
1237 return rte_flow_error_set
1239 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1240 "no support for partial mask on"
1241 " \"ipv4.hdr.dst_addr\" field"
1242 " for vxlan encapsulation");
1243 /* More IPv4 address validations can be put here. */
1246 * Kernel uses the destination IP address to determine
1247 * the routing path and obtain the MAC destination
1248 * address, so IP destination address must be
1249 * specified in the tc rule.
1251 return rte_flow_error_set(error, EINVAL,
1252 RTE_FLOW_ERROR_TYPE_ITEM, item,
1253 "outer ipv4 destination address"
1254 " must be specified for"
1255 " vxlan encapsulation");
1257 if (mask->hdr.src_addr != RTE_BE32(0x00000000)) {
1258 if (mask->hdr.src_addr != RTE_BE32(0xffffffff))
1259 return rte_flow_error_set
1261 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1262 "no support for partial mask on"
1263 " \"ipv4.hdr.src_addr\" field"
1264 " for vxlan encapsulation");
1265 /* More IPv4 address validations can be put here. */
1268 * Kernel uses the source IP address to select the
1269 * interface for egress encapsulated traffic, so
1270 * it must be specified in the tc rule.
1272 return rte_flow_error_set(error, EINVAL,
1273 RTE_FLOW_ERROR_TYPE_ITEM, item,
1274 "outer ipv4 source address"
1275 " must be specified for"
1276 " vxlan encapsulation");
1282 * Validate VXLAN_ENCAP action RTE_FLOW_ITEM_TYPE_IPV6 item for E-Switch.
1283 * The routine checks the IPv6 fields to be used in encapsulation header.
1286 * Pointer to the item structure.
1288 * Pointer to the error structure.
1291 * 0 on success, a negative errno value otherwise and rte_errno is set.
1294 flow_tcf_validate_vxlan_encap_ipv6(const struct rte_flow_item *item,
1295 struct rte_flow_error *error)
1297 const struct rte_flow_item_ipv6 *spec = item->spec;
1298 const struct rte_flow_item_ipv6 *mask = item->mask;
1302 * Specification for IP addresses cannot be empty
1303 * because it is required by tunnel_key parameter.
1305 return rte_flow_error_set(error, EINVAL,
1306 RTE_FLOW_ERROR_TYPE_ITEM, item,
1307 "NULL outer ipv6 address"
1308 " specification for"
1309 " vxlan encapsulation");
1312 mask = &rte_flow_item_ipv6_mask;
1313 if (memcmp(&mask->hdr.dst_addr,
1314 &flow_tcf_mask_empty.ipv6.hdr.dst_addr,
1316 if (memcmp(&mask->hdr.dst_addr,
1317 &rte_flow_item_ipv6_mask.hdr.dst_addr,
1319 return rte_flow_error_set
1321 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1322 "no support for partial mask on"
1323 " \"ipv6.hdr.dst_addr\" field"
1324 " for vxlan encapsulation");
1325 /* More IPv6 address validations can be put here. */
1328 * Kernel uses the destination IP address to determine
1329 * the routing path and obtain the MAC destination
1330 * address (heigh or gate), so IP destination address
1331 * must be specified within the tc rule.
1333 return rte_flow_error_set(error, EINVAL,
1334 RTE_FLOW_ERROR_TYPE_ITEM, item,
1335 "outer ipv6 destination address"
1336 " must be specified for"
1337 " vxlan encapsulation");
1339 if (memcmp(&mask->hdr.src_addr,
1340 &flow_tcf_mask_empty.ipv6.hdr.src_addr,
1342 if (memcmp(&mask->hdr.src_addr,
1343 &rte_flow_item_ipv6_mask.hdr.src_addr,
1345 return rte_flow_error_set
1347 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1348 "no support for partial mask on"
1349 " \"ipv6.hdr.src_addr\" field"
1350 " for vxlan encapsulation");
1351 /* More L3 address validation can be put here. */
1354 * Kernel uses the source IP address to select the
1355 * interface for egress encapsulated traffic, so
1356 * it must be specified in the tc rule.
1358 return rte_flow_error_set(error, EINVAL,
1359 RTE_FLOW_ERROR_TYPE_ITEM, item,
1360 "outer L3 source address"
1361 " must be specified for"
1362 " vxlan encapsulation");
1368 * Validate VXLAN_ENCAP action RTE_FLOW_ITEM_TYPE_UDP item for E-Switch.
1369 * The routine checks the UDP fields to be used in encapsulation header.
1372 * Pointer to the item structure.
1374 * Pointer to the error structure.
1377 * 0 on success, a negative errno value otherwise and rte_errno is set.
1380 flow_tcf_validate_vxlan_encap_udp(const struct rte_flow_item *item,
1381 struct rte_flow_error *error)
1383 const struct rte_flow_item_udp *spec = item->spec;
1384 const struct rte_flow_item_udp *mask = item->mask;
1388 * Specification for UDP ports cannot be empty
1389 * because it is required by tunnel_key parameter.
1391 return rte_flow_error_set(error, EINVAL,
1392 RTE_FLOW_ERROR_TYPE_ITEM, item,
1393 "NULL UDP port specification "
1394 " for vxlan encapsulation");
1397 mask = &rte_flow_item_udp_mask;
1398 if (mask->hdr.dst_port != RTE_BE16(0x0000)) {
1399 if (mask->hdr.dst_port != RTE_BE16(0xffff))
1400 return rte_flow_error_set
1402 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1403 "no support for partial mask on"
1404 " \"udp.hdr.dst_port\" field"
1405 " for vxlan encapsulation");
1406 if (!spec->hdr.dst_port)
1407 return rte_flow_error_set
1409 RTE_FLOW_ERROR_TYPE_ITEM, item,
1410 "outer UDP remote port cannot be"
1411 " 0 for vxlan encapsulation");
1413 return rte_flow_error_set(error, EINVAL,
1414 RTE_FLOW_ERROR_TYPE_ITEM, item,
1415 "outer UDP remote port"
1416 " must be specified for"
1417 " vxlan encapsulation");
1419 if (mask->hdr.src_port != RTE_BE16(0x0000)) {
1420 if (mask->hdr.src_port != RTE_BE16(0xffff))
1421 return rte_flow_error_set
1423 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1424 "no support for partial mask on"
1425 " \"udp.hdr.src_port\" field"
1426 " for vxlan encapsulation");
1428 "outer UDP source port cannot be"
1429 " forced for vxlan encapsulation,"
1430 " parameter ignored");
1436 * Validate VXLAN_ENCAP action RTE_FLOW_ITEM_TYPE_VXLAN item for E-Switch.
1437 * The routine checks the VNIP fields to be used in encapsulation header.
1440 * Pointer to the item structure.
1442 * Pointer to the error structure.
1445 * 0 on success, a negative errno value otherwise and rte_errno is set.
1448 flow_tcf_validate_vxlan_encap_vni(const struct rte_flow_item *item,
1449 struct rte_flow_error *error)
1451 const struct rte_flow_item_vxlan *spec = item->spec;
1452 const struct rte_flow_item_vxlan *mask = item->mask;
1455 /* Outer VNI is required by tunnel_key parameter. */
1456 return rte_flow_error_set(error, EINVAL,
1457 RTE_FLOW_ERROR_TYPE_ITEM, item,
1458 "NULL VNI specification"
1459 " for vxlan encapsulation");
1462 mask = &rte_flow_item_vxlan_mask;
1463 if (!mask->vni[0] && !mask->vni[1] && !mask->vni[2])
1464 return rte_flow_error_set(error, EINVAL,
1465 RTE_FLOW_ERROR_TYPE_ITEM, item,
1466 "outer VNI must be specified "
1467 "for vxlan encapsulation");
1468 if (mask->vni[0] != 0xff ||
1469 mask->vni[1] != 0xff ||
1470 mask->vni[2] != 0xff)
1471 return rte_flow_error_set(error, ENOTSUP,
1472 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1473 "no support for partial mask on"
1474 " \"vxlan.vni\" field");
1476 if (!spec->vni[0] && !spec->vni[1] && !spec->vni[2])
1477 return rte_flow_error_set(error, EINVAL,
1478 RTE_FLOW_ERROR_TYPE_ITEM, item,
1479 "vxlan vni cannot be 0");
1484 * Validate VXLAN_ENCAP action item list for E-Switch.
1485 * The routine checks items to be used in encapsulation header.
1488 * Pointer to the VXLAN_ENCAP action structure.
1490 * Pointer to the error structure.
1493 * 0 on success, a negative errno value otherwise and rte_errno is set.
1496 flow_tcf_validate_vxlan_encap(const struct rte_flow_action *action,
1497 struct rte_flow_error *error)
1499 const struct rte_flow_item *items;
1501 uint32_t item_flags = 0;
1504 return rte_flow_error_set(error, EINVAL,
1505 RTE_FLOW_ERROR_TYPE_ACTION, action,
1506 "Missing vxlan tunnel"
1507 " action configuration");
1508 items = ((const struct rte_flow_action_vxlan_encap *)
1509 action->conf)->definition;
1511 return rte_flow_error_set(error, EINVAL,
1512 RTE_FLOW_ERROR_TYPE_ACTION, action,
1513 "Missing vxlan tunnel"
1514 " encapsulation parameters");
1515 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1516 switch (items->type) {
1517 case RTE_FLOW_ITEM_TYPE_VOID:
1519 case RTE_FLOW_ITEM_TYPE_ETH:
1520 ret = mlx5_flow_validate_item_eth(items, item_flags,
1524 ret = flow_tcf_validate_vxlan_encap_eth(items, error);
1527 item_flags |= MLX5_FLOW_LAYER_OUTER_L2;
1530 case RTE_FLOW_ITEM_TYPE_IPV4:
1531 ret = mlx5_flow_validate_item_ipv4(items, item_flags,
1535 ret = flow_tcf_validate_vxlan_encap_ipv4(items, error);
1538 item_flags |= MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1540 case RTE_FLOW_ITEM_TYPE_IPV6:
1541 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
1545 ret = flow_tcf_validate_vxlan_encap_ipv6(items, error);
1548 item_flags |= MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1550 case RTE_FLOW_ITEM_TYPE_UDP:
1551 ret = mlx5_flow_validate_item_udp(items, item_flags,
1555 ret = flow_tcf_validate_vxlan_encap_udp(items, error);
1558 item_flags |= MLX5_FLOW_LAYER_OUTER_L4_UDP;
1560 case RTE_FLOW_ITEM_TYPE_VXLAN:
1561 ret = mlx5_flow_validate_item_vxlan(items,
1565 ret = flow_tcf_validate_vxlan_encap_vni(items, error);
1568 item_flags |= MLX5_FLOW_LAYER_VXLAN;
1571 return rte_flow_error_set
1573 RTE_FLOW_ERROR_TYPE_ITEM, items,
1574 "vxlan encap item not supported");
1577 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
1578 return rte_flow_error_set(error, EINVAL,
1579 RTE_FLOW_ERROR_TYPE_ACTION, action,
1580 "no outer IP layer found"
1581 " for vxlan encapsulation");
1582 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1583 return rte_flow_error_set(error, EINVAL,
1584 RTE_FLOW_ERROR_TYPE_ACTION, action,
1585 "no outer UDP layer found"
1586 " for vxlan encapsulation");
1587 if (!(item_flags & MLX5_FLOW_LAYER_VXLAN))
1588 return rte_flow_error_set(error, EINVAL,
1589 RTE_FLOW_ERROR_TYPE_ACTION, action,
1590 "no VXLAN VNI found"
1591 " for vxlan encapsulation");
1596 * Validate outer RTE_FLOW_ITEM_TYPE_UDP item if tunnel item
1597 * RTE_FLOW_ITEM_TYPE_VXLAN is present in item list.
1600 * Outer UDP layer item (if any, NULL otherwise).
1602 * Pointer to the error structure.
1605 * 0 on success, a negative errno value otherwise and rte_errno is set.
1608 flow_tcf_validate_vxlan_decap_udp(const struct rte_flow_item *udp,
1609 struct rte_flow_error *error)
1611 const struct rte_flow_item_udp *spec = udp->spec;
1612 const struct rte_flow_item_udp *mask = udp->mask;
1616 * Specification for UDP ports cannot be empty
1617 * because it is required as decap parameter.
1619 return rte_flow_error_set(error, EINVAL,
1620 RTE_FLOW_ERROR_TYPE_ITEM, udp,
1621 "NULL UDP port specification"
1622 " for VXLAN decapsulation");
1624 mask = &rte_flow_item_udp_mask;
1625 if (mask->hdr.dst_port != RTE_BE16(0x0000)) {
1626 if (mask->hdr.dst_port != RTE_BE16(0xffff))
1627 return rte_flow_error_set
1629 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1630 "no support for partial mask on"
1631 " \"udp.hdr.dst_port\" field");
1632 if (!spec->hdr.dst_port)
1633 return rte_flow_error_set
1635 RTE_FLOW_ERROR_TYPE_ITEM, udp,
1636 "zero decap local UDP port");
1638 return rte_flow_error_set(error, EINVAL,
1639 RTE_FLOW_ERROR_TYPE_ITEM, udp,
1640 "outer UDP destination port must be "
1641 "specified for vxlan decapsulation");
1643 if (mask->hdr.src_port != RTE_BE16(0x0000)) {
1644 if (mask->hdr.src_port != RTE_BE16(0xffff))
1645 return rte_flow_error_set
1647 RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1648 "no support for partial mask on"
1649 " \"udp.hdr.src_port\" field");
1651 "outer UDP local port cannot be "
1652 "forced for VXLAN encapsulation, "
1653 "parameter ignored");
1659 * Validate flow for E-Switch.
1662 * Pointer to the priv structure.
1664 * Pointer to the flow attributes.
1666 * Pointer to the list of items.
1667 * @param[in] actions
1668 * Pointer to the list of actions.
1670 * Pointer to the error structure.
1673 * 0 on success, a negative errno value otherwise and rte_errno is set.
1676 flow_tcf_validate(struct rte_eth_dev *dev,
1677 const struct rte_flow_attr *attr,
1678 const struct rte_flow_item items[],
1679 const struct rte_flow_action actions[],
1680 struct rte_flow_error *error)
1683 const struct rte_flow_item_port_id *port_id;
1684 const struct rte_flow_item_eth *eth;
1685 const struct rte_flow_item_vlan *vlan;
1686 const struct rte_flow_item_ipv4 *ipv4;
1687 const struct rte_flow_item_ipv6 *ipv6;
1688 const struct rte_flow_item_tcp *tcp;
1689 const struct rte_flow_item_udp *udp;
1690 const struct rte_flow_item_vxlan *vxlan;
1693 const struct rte_flow_action_port_id *port_id;
1694 const struct rte_flow_action_jump *jump;
1695 const struct rte_flow_action_of_push_vlan *of_push_vlan;
1696 const struct rte_flow_action_of_set_vlan_vid *
1698 const struct rte_flow_action_of_set_vlan_pcp *
1700 const struct rte_flow_action_vxlan_encap *vxlan_encap;
1701 const struct rte_flow_action_set_ipv4 *set_ipv4;
1702 const struct rte_flow_action_set_ipv6 *set_ipv6;
1704 const struct rte_flow_item *outer_udp = NULL;
1705 rte_be16_t inner_etype = RTE_BE16(ETH_P_ALL);
1706 rte_be16_t outer_etype = RTE_BE16(ETH_P_ALL);
1707 rte_be16_t vlan_etype = RTE_BE16(ETH_P_ALL);
1708 uint64_t item_flags = 0;
1709 uint64_t action_flags = 0;
1710 uint8_t next_protocol = 0xff;
1711 unsigned int tcm_ifindex = 0;
1712 uint8_t pedit_validated = 0;
1713 struct flow_tcf_ptoi ptoi[PTOI_TABLE_SZ_MAX(dev)];
1714 struct rte_eth_dev *port_id_dev = NULL;
1715 bool in_port_id_set;
1718 claim_nonzero(flow_tcf_build_ptoi_table(dev, ptoi,
1719 PTOI_TABLE_SZ_MAX(dev)));
1720 ret = flow_tcf_validate_attributes(attr, error);
1723 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1725 uint64_t current_action_flag = 0;
1727 switch (actions->type) {
1728 case RTE_FLOW_ACTION_TYPE_VOID:
1730 case RTE_FLOW_ACTION_TYPE_PORT_ID:
1731 current_action_flag = MLX5_FLOW_ACTION_PORT_ID;
1734 conf.port_id = actions->conf;
1735 if (conf.port_id->original)
1738 for (i = 0; ptoi[i].ifindex; ++i)
1739 if (ptoi[i].port_id == conf.port_id->id)
1741 if (!ptoi[i].ifindex)
1742 return rte_flow_error_set
1744 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1746 "missing data to convert port ID to"
1748 port_id_dev = &rte_eth_devices[conf.port_id->id];
1750 case RTE_FLOW_ACTION_TYPE_JUMP:
1751 current_action_flag = MLX5_FLOW_ACTION_JUMP;
1754 conf.jump = actions->conf;
1755 if (attr->group >= conf.jump->group)
1756 return rte_flow_error_set
1758 RTE_FLOW_ERROR_TYPE_ACTION,
1760 "can jump only to a group forward");
1762 case RTE_FLOW_ACTION_TYPE_DROP:
1763 current_action_flag = MLX5_FLOW_ACTION_DROP;
1765 case RTE_FLOW_ACTION_TYPE_COUNT:
1767 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
1768 current_action_flag = MLX5_FLOW_ACTION_OF_POP_VLAN;
1770 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN: {
1771 rte_be16_t ethertype;
1773 current_action_flag = MLX5_FLOW_ACTION_OF_PUSH_VLAN;
1776 conf.of_push_vlan = actions->conf;
1777 ethertype = conf.of_push_vlan->ethertype;
1778 if (ethertype != RTE_BE16(ETH_P_8021Q) &&
1779 ethertype != RTE_BE16(ETH_P_8021AD))
1780 return rte_flow_error_set
1782 RTE_FLOW_ERROR_TYPE_ACTION, actions,
1783 "vlan push TPID must be "
1784 "802.1Q or 802.1AD");
1787 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
1788 if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN))
1789 return rte_flow_error_set
1791 RTE_FLOW_ERROR_TYPE_ACTION, actions,
1792 "vlan modify is not supported,"
1793 " set action must follow push action");
1794 current_action_flag = MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
1796 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
1797 if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN))
1798 return rte_flow_error_set
1800 RTE_FLOW_ERROR_TYPE_ACTION, actions,
1801 "vlan modify is not supported,"
1802 " set action must follow push action");
1803 current_action_flag = MLX5_FLOW_ACTION_OF_SET_VLAN_PCP;
1805 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
1806 current_action_flag = MLX5_FLOW_ACTION_VXLAN_DECAP;
1808 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
1809 ret = flow_tcf_validate_vxlan_encap(actions, error);
1812 current_action_flag = MLX5_FLOW_ACTION_VXLAN_ENCAP;
1814 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
1815 current_action_flag = MLX5_FLOW_ACTION_SET_IPV4_SRC;
1817 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
1818 current_action_flag = MLX5_FLOW_ACTION_SET_IPV4_DST;
1820 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
1821 current_action_flag = MLX5_FLOW_ACTION_SET_IPV6_SRC;
1823 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
1824 current_action_flag = MLX5_FLOW_ACTION_SET_IPV6_DST;
1826 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
1827 current_action_flag = MLX5_FLOW_ACTION_SET_TP_SRC;
1829 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
1830 current_action_flag = MLX5_FLOW_ACTION_SET_TP_DST;
1832 case RTE_FLOW_ACTION_TYPE_SET_TTL:
1833 current_action_flag = MLX5_FLOW_ACTION_SET_TTL;
1835 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
1836 current_action_flag = MLX5_FLOW_ACTION_DEC_TTL;
1838 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
1839 current_action_flag = MLX5_FLOW_ACTION_SET_MAC_SRC;
1841 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
1842 current_action_flag = MLX5_FLOW_ACTION_SET_MAC_DST;
1845 return rte_flow_error_set(error, ENOTSUP,
1846 RTE_FLOW_ERROR_TYPE_ACTION,
1848 "action not supported");
1850 if (current_action_flag & MLX5_TCF_CONFIG_ACTIONS) {
1852 return rte_flow_error_set
1854 RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1856 "action configuration not set");
1858 if ((current_action_flag & MLX5_TCF_PEDIT_ACTIONS) &&
1860 return rte_flow_error_set(error, ENOTSUP,
1861 RTE_FLOW_ERROR_TYPE_ACTION,
1863 "set actions should be "
1864 "listed successively");
1865 if ((current_action_flag & ~MLX5_TCF_PEDIT_ACTIONS) &&
1866 (action_flags & MLX5_TCF_PEDIT_ACTIONS))
1867 pedit_validated = 1;
1868 if ((current_action_flag & MLX5_TCF_FATE_ACTIONS) &&
1869 (action_flags & MLX5_TCF_FATE_ACTIONS))
1870 return rte_flow_error_set(error, EINVAL,
1871 RTE_FLOW_ERROR_TYPE_ACTION,
1873 "can't have multiple fate"
1875 if ((current_action_flag & MLX5_TCF_VXLAN_ACTIONS) &&
1876 (action_flags & MLX5_TCF_VXLAN_ACTIONS))
1877 return rte_flow_error_set(error, EINVAL,
1878 RTE_FLOW_ERROR_TYPE_ACTION,
1880 "can't have multiple vxlan"
1882 if ((current_action_flag & MLX5_TCF_VXLAN_ACTIONS) &&
1883 (action_flags & MLX5_TCF_VLAN_ACTIONS))
1884 return rte_flow_error_set(error, ENOTSUP,
1885 RTE_FLOW_ERROR_TYPE_ACTION,
1887 "can't have vxlan and vlan"
1888 " actions in the same rule");
1889 action_flags |= current_action_flag;
1891 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1894 switch (items->type) {
1895 case RTE_FLOW_ITEM_TYPE_VOID:
1897 case RTE_FLOW_ITEM_TYPE_PORT_ID:
1898 if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1899 return rte_flow_error_set
1901 RTE_FLOW_ERROR_TYPE_ITEM, items,
1902 "inner tunnel port id"
1903 " item is not supported");
1904 mask.port_id = flow_tcf_item_mask
1905 (items, &rte_flow_item_port_id_mask,
1906 &flow_tcf_mask_supported.port_id,
1907 &flow_tcf_mask_empty.port_id,
1908 sizeof(flow_tcf_mask_supported.port_id),
1912 if (mask.port_id == &flow_tcf_mask_empty.port_id) {
1916 spec.port_id = items->spec;
1917 if (mask.port_id->id && mask.port_id->id != 0xffffffff)
1918 return rte_flow_error_set
1920 RTE_FLOW_ERROR_TYPE_ITEM_MASK,
1922 "no support for partial mask on"
1924 if (!mask.port_id->id)
1927 for (i = 0; ptoi[i].ifindex; ++i)
1928 if (ptoi[i].port_id == spec.port_id->id)
1930 if (!ptoi[i].ifindex)
1931 return rte_flow_error_set
1933 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1935 "missing data to convert port ID to"
1937 if (in_port_id_set && ptoi[i].ifindex != tcm_ifindex)
1938 return rte_flow_error_set
1940 RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
1942 "cannot match traffic for"
1943 " several port IDs through"
1944 " a single flow rule");
1945 tcm_ifindex = ptoi[i].ifindex;
1948 case RTE_FLOW_ITEM_TYPE_ETH:
1949 ret = mlx5_flow_validate_item_eth(items, item_flags,
1953 item_flags |= (item_flags & MLX5_FLOW_LAYER_TUNNEL) ?
1954 MLX5_FLOW_LAYER_INNER_L2 :
1955 MLX5_FLOW_LAYER_OUTER_L2;
1957 * Redundant check due to different supported mask.
1958 * Same for the rest of items.
1960 mask.eth = flow_tcf_item_mask
1961 (items, &rte_flow_item_eth_mask,
1962 &flow_tcf_mask_supported.eth,
1963 &flow_tcf_mask_empty.eth,
1964 sizeof(flow_tcf_mask_supported.eth),
1968 if (mask.eth->type && mask.eth->type !=
1970 return rte_flow_error_set
1972 RTE_FLOW_ERROR_TYPE_ITEM_MASK,
1974 "no support for partial mask on"
1976 assert(items->spec);
1977 spec.eth = items->spec;
1978 if (mask.eth->type &&
1979 (item_flags & MLX5_FLOW_LAYER_TUNNEL) &&
1980 inner_etype != RTE_BE16(ETH_P_ALL) &&
1981 inner_etype != spec.eth->type)
1982 return rte_flow_error_set
1984 RTE_FLOW_ERROR_TYPE_ITEM,
1986 "inner eth_type conflict");
1987 if (mask.eth->type &&
1988 !(item_flags & MLX5_FLOW_LAYER_TUNNEL) &&
1989 outer_etype != RTE_BE16(ETH_P_ALL) &&
1990 outer_etype != spec.eth->type)
1991 return rte_flow_error_set
1993 RTE_FLOW_ERROR_TYPE_ITEM,
1995 "outer eth_type conflict");
1996 if (mask.eth->type) {
1997 if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
1998 inner_etype = spec.eth->type;
2000 outer_etype = spec.eth->type;
2003 case RTE_FLOW_ITEM_TYPE_VLAN:
2004 if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
2005 return rte_flow_error_set
2007 RTE_FLOW_ERROR_TYPE_ITEM, items,
2009 " is not supported");
2010 ret = mlx5_flow_validate_item_vlan(items, item_flags,
2014 item_flags |= MLX5_FLOW_LAYER_OUTER_VLAN;
2015 mask.vlan = flow_tcf_item_mask
2016 (items, &rte_flow_item_vlan_mask,
2017 &flow_tcf_mask_supported.vlan,
2018 &flow_tcf_mask_empty.vlan,
2019 sizeof(flow_tcf_mask_supported.vlan),
2023 if ((mask.vlan->tci & RTE_BE16(0xe000) &&
2024 (mask.vlan->tci & RTE_BE16(0xe000)) !=
2025 RTE_BE16(0xe000)) ||
2026 (mask.vlan->tci & RTE_BE16(0x0fff) &&
2027 (mask.vlan->tci & RTE_BE16(0x0fff)) !=
2028 RTE_BE16(0x0fff)) ||
2029 (mask.vlan->inner_type &&
2030 mask.vlan->inner_type != RTE_BE16(0xffff)))
2031 return rte_flow_error_set
2033 RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2035 "no support for partial masks on"
2036 " \"tci\" (PCP and VID parts) and"
2037 " \"inner_type\" fields");
2038 if (outer_etype != RTE_BE16(ETH_P_ALL) &&
2039 outer_etype != RTE_BE16(ETH_P_8021Q))
2040 return rte_flow_error_set
2042 RTE_FLOW_ERROR_TYPE_ITEM,
2044 "outer eth_type conflict,"
2046 outer_etype = RTE_BE16(ETH_P_8021Q);
2047 assert(items->spec);
2048 spec.vlan = items->spec;
2049 if (mask.vlan->inner_type &&
2050 vlan_etype != RTE_BE16(ETH_P_ALL) &&
2051 vlan_etype != spec.vlan->inner_type)
2052 return rte_flow_error_set
2054 RTE_FLOW_ERROR_TYPE_ITEM,
2056 "vlan eth_type conflict");
2057 if (mask.vlan->inner_type)
2058 vlan_etype = spec.vlan->inner_type;
2060 case RTE_FLOW_ITEM_TYPE_IPV4:
2061 ret = mlx5_flow_validate_item_ipv4(items, item_flags,
2065 item_flags |= (item_flags & MLX5_FLOW_LAYER_TUNNEL) ?
2066 MLX5_FLOW_LAYER_INNER_L3_IPV4 :
2067 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
2068 mask.ipv4 = flow_tcf_item_mask
2069 (items, &rte_flow_item_ipv4_mask,
2070 &flow_tcf_mask_supported.ipv4,
2071 &flow_tcf_mask_empty.ipv4,
2072 sizeof(flow_tcf_mask_supported.ipv4),
2076 if (mask.ipv4->hdr.next_proto_id &&
2077 mask.ipv4->hdr.next_proto_id != 0xff)
2078 return rte_flow_error_set
2080 RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2082 "no support for partial mask on"
2083 " \"hdr.next_proto_id\" field");
2084 else if (mask.ipv4->hdr.next_proto_id)
2086 ((const struct rte_flow_item_ipv4 *)
2087 (items->spec))->hdr.next_proto_id;
2088 if (item_flags & MLX5_FLOW_LAYER_TUNNEL) {
2089 if (inner_etype != RTE_BE16(ETH_P_ALL) &&
2090 inner_etype != RTE_BE16(ETH_P_IP))
2091 return rte_flow_error_set
2093 RTE_FLOW_ERROR_TYPE_ITEM,
2095 "inner eth_type conflict,"
2096 " IPv4 is required");
2097 inner_etype = RTE_BE16(ETH_P_IP);
2098 } else if (item_flags & MLX5_FLOW_LAYER_OUTER_VLAN) {
2099 if (vlan_etype != RTE_BE16(ETH_P_ALL) &&
2100 vlan_etype != RTE_BE16(ETH_P_IP))
2101 return rte_flow_error_set
2103 RTE_FLOW_ERROR_TYPE_ITEM,
2105 "vlan eth_type conflict,"
2106 " IPv4 is required");
2107 vlan_etype = RTE_BE16(ETH_P_IP);
2109 if (outer_etype != RTE_BE16(ETH_P_ALL) &&
2110 outer_etype != RTE_BE16(ETH_P_IP))
2111 return rte_flow_error_set
2113 RTE_FLOW_ERROR_TYPE_ITEM,
2115 "eth_type conflict,"
2116 " IPv4 is required");
2117 outer_etype = RTE_BE16(ETH_P_IP);
2120 case RTE_FLOW_ITEM_TYPE_IPV6:
2121 ret = mlx5_flow_validate_item_ipv6(items, item_flags,
2125 item_flags |= (item_flags & MLX5_FLOW_LAYER_TUNNEL) ?
2126 MLX5_FLOW_LAYER_INNER_L3_IPV6 :
2127 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
2128 mask.ipv6 = flow_tcf_item_mask
2129 (items, &rte_flow_item_ipv6_mask,
2130 &flow_tcf_mask_supported.ipv6,
2131 &flow_tcf_mask_empty.ipv6,
2132 sizeof(flow_tcf_mask_supported.ipv6),
2136 if (mask.ipv6->hdr.proto &&
2137 mask.ipv6->hdr.proto != 0xff)
2138 return rte_flow_error_set
2140 RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2142 "no support for partial mask on"
2143 " \"hdr.proto\" field");
2144 else if (mask.ipv6->hdr.proto)
2146 ((const struct rte_flow_item_ipv6 *)
2147 (items->spec))->hdr.proto;
2148 if (item_flags & MLX5_FLOW_LAYER_TUNNEL) {
2149 if (inner_etype != RTE_BE16(ETH_P_ALL) &&
2150 inner_etype != RTE_BE16(ETH_P_IPV6))
2151 return rte_flow_error_set
2153 RTE_FLOW_ERROR_TYPE_ITEM,
2155 "inner eth_type conflict,"
2156 " IPv6 is required");
2157 inner_etype = RTE_BE16(ETH_P_IPV6);
2158 } else if (item_flags & MLX5_FLOW_LAYER_OUTER_VLAN) {
2159 if (vlan_etype != RTE_BE16(ETH_P_ALL) &&
2160 vlan_etype != RTE_BE16(ETH_P_IPV6))
2161 return rte_flow_error_set
2163 RTE_FLOW_ERROR_TYPE_ITEM,
2165 "vlan eth_type conflict,"
2166 " IPv6 is required");
2167 vlan_etype = RTE_BE16(ETH_P_IPV6);
2169 if (outer_etype != RTE_BE16(ETH_P_ALL) &&
2170 outer_etype != RTE_BE16(ETH_P_IPV6))
2171 return rte_flow_error_set
2173 RTE_FLOW_ERROR_TYPE_ITEM,
2175 "eth_type conflict,"
2176 " IPv6 is required");
2177 outer_etype = RTE_BE16(ETH_P_IPV6);
2180 case RTE_FLOW_ITEM_TYPE_UDP:
2181 ret = mlx5_flow_validate_item_udp(items, item_flags,
2182 next_protocol, error);
2185 item_flags |= (item_flags & MLX5_FLOW_LAYER_TUNNEL) ?
2186 MLX5_FLOW_LAYER_INNER_L4_UDP :
2187 MLX5_FLOW_LAYER_OUTER_L4_UDP;
2188 mask.udp = flow_tcf_item_mask
2189 (items, &rte_flow_item_udp_mask,
2190 &flow_tcf_mask_supported.udp,
2191 &flow_tcf_mask_empty.udp,
2192 sizeof(flow_tcf_mask_supported.udp),
2197 * Save the presumed outer UDP item for extra check
2198 * if the tunnel item will be found later in the list.
2200 if (!(item_flags & MLX5_FLOW_LAYER_TUNNEL))
2203 case RTE_FLOW_ITEM_TYPE_TCP:
2204 ret = mlx5_flow_validate_item_tcp
2207 &flow_tcf_mask_supported.tcp,
2211 item_flags |= (item_flags & MLX5_FLOW_LAYER_TUNNEL) ?
2212 MLX5_FLOW_LAYER_INNER_L4_TCP :
2213 MLX5_FLOW_LAYER_OUTER_L4_TCP;
2214 mask.tcp = flow_tcf_item_mask
2215 (items, &rte_flow_item_tcp_mask,
2216 &flow_tcf_mask_supported.tcp,
2217 &flow_tcf_mask_empty.tcp,
2218 sizeof(flow_tcf_mask_supported.tcp),
2223 case RTE_FLOW_ITEM_TYPE_VXLAN:
2224 if (item_flags & MLX5_FLOW_LAYER_OUTER_VLAN)
2225 return rte_flow_error_set
2227 RTE_FLOW_ERROR_TYPE_ITEM, items,
2228 "vxlan tunnel over vlan"
2229 " is not supported");
2230 ret = mlx5_flow_validate_item_vxlan(items,
2234 item_flags |= MLX5_FLOW_LAYER_VXLAN;
2235 mask.vxlan = flow_tcf_item_mask
2236 (items, &rte_flow_item_vxlan_mask,
2237 &flow_tcf_mask_supported.vxlan,
2238 &flow_tcf_mask_empty.vxlan,
2239 sizeof(flow_tcf_mask_supported.vxlan), error);
2242 if (mask.vxlan->vni[0] != 0xff ||
2243 mask.vxlan->vni[1] != 0xff ||
2244 mask.vxlan->vni[2] != 0xff)
2245 return rte_flow_error_set
2247 RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2249 "no support for partial or "
2250 "empty mask on \"vxlan.vni\" field");
2252 * The VNI item assumes the VXLAN tunnel, it requires
2253 * at least the outer destination UDP port must be
2254 * specified without wildcards to allow kernel select
2255 * the virtual VXLAN device by port. Also outer IPv4
2256 * or IPv6 item must be specified (wilcards or even
2257 * zero mask are allowed) to let driver know the tunnel
2258 * IP version and process UDP traffic correctly.
2261 (MLX5_FLOW_LAYER_OUTER_L3_IPV4 |
2262 MLX5_FLOW_LAYER_OUTER_L3_IPV6)))
2263 return rte_flow_error_set
2265 RTE_FLOW_ERROR_TYPE_ACTION,
2267 "no outer IP pattern found"
2268 " for vxlan tunnel");
2269 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
2270 return rte_flow_error_set
2272 RTE_FLOW_ERROR_TYPE_ACTION,
2274 "no outer UDP pattern found"
2275 " for vxlan tunnel");
2277 * All items preceding the tunnel item become outer
2278 * ones and we should do extra validation for them
2279 * due to tc limitations for tunnel outer parameters.
2280 * Currently only outer UDP item requres extra check,
2281 * use the saved pointer instead of item list rescan.
2284 ret = flow_tcf_validate_vxlan_decap_udp
2288 /* Reset L4 protocol for inner parameters. */
2289 next_protocol = 0xff;
2292 return rte_flow_error_set(error, ENOTSUP,
2293 RTE_FLOW_ERROR_TYPE_ITEM,
2294 items, "item not supported");
2297 if ((action_flags & MLX5_TCF_PEDIT_ACTIONS) &&
2298 (action_flags & MLX5_FLOW_ACTION_DROP))
2299 return rte_flow_error_set(error, ENOTSUP,
2300 RTE_FLOW_ERROR_TYPE_ACTION,
2302 "set action is not compatible with "
2304 if ((action_flags & MLX5_TCF_PEDIT_ACTIONS) &&
2305 !(action_flags & MLX5_FLOW_ACTION_PORT_ID))
2306 return rte_flow_error_set(error, ENOTSUP,
2307 RTE_FLOW_ERROR_TYPE_ACTION,
2309 "set action must be followed by "
2312 (MLX5_FLOW_ACTION_SET_IPV4_SRC | MLX5_FLOW_ACTION_SET_IPV4_DST)) {
2313 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV4))
2314 return rte_flow_error_set(error, EINVAL,
2315 RTE_FLOW_ERROR_TYPE_ACTION,
2317 "no ipv4 item found in"
2321 (MLX5_FLOW_ACTION_SET_IPV6_SRC | MLX5_FLOW_ACTION_SET_IPV6_DST)) {
2322 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV6))
2323 return rte_flow_error_set(error, EINVAL,
2324 RTE_FLOW_ERROR_TYPE_ACTION,
2326 "no ipv6 item found in"
2330 (MLX5_FLOW_ACTION_SET_TP_SRC | MLX5_FLOW_ACTION_SET_TP_DST)) {
2332 (MLX5_FLOW_LAYER_OUTER_L4_UDP |
2333 MLX5_FLOW_LAYER_OUTER_L4_TCP)))
2334 return rte_flow_error_set(error, EINVAL,
2335 RTE_FLOW_ERROR_TYPE_ACTION,
2337 "no TCP/UDP item found in"
2341 * FW syndrome (0xA9C090):
2342 * set_flow_table_entry: push vlan action fte in fdb can ONLY be
2343 * forward to the uplink.
2345 if ((action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN) &&
2346 (action_flags & MLX5_FLOW_ACTION_PORT_ID) &&
2347 ((struct priv *)port_id_dev->data->dev_private)->representor)
2348 return rte_flow_error_set(error, ENOTSUP,
2349 RTE_FLOW_ERROR_TYPE_ACTION, actions,
2350 "vlan push can only be applied"
2351 " when forwarding to uplink port");
2353 * FW syndrome (0x294609):
2354 * set_flow_table_entry: modify/pop/push actions in fdb flow table
2355 * are supported only while forwarding to vport.
2357 if ((action_flags & MLX5_TCF_VLAN_ACTIONS) &&
2358 !(action_flags & MLX5_FLOW_ACTION_PORT_ID))
2359 return rte_flow_error_set(error, ENOTSUP,
2360 RTE_FLOW_ERROR_TYPE_ACTION, actions,
2361 "vlan actions are supported"
2362 " only with port_id action");
2363 if ((action_flags & MLX5_TCF_VXLAN_ACTIONS) &&
2364 !(action_flags & MLX5_FLOW_ACTION_PORT_ID))
2365 return rte_flow_error_set(error, ENOTSUP,
2366 RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2367 "vxlan actions are supported"
2368 " only with port_id action");
2369 if (!(action_flags & MLX5_TCF_FATE_ACTIONS))
2370 return rte_flow_error_set(error, EINVAL,
2371 RTE_FLOW_ERROR_TYPE_ACTION, actions,
2372 "no fate action is found");
2374 (MLX5_FLOW_ACTION_SET_TTL | MLX5_FLOW_ACTION_DEC_TTL)) {
2376 (MLX5_FLOW_LAYER_OUTER_L3_IPV4 |
2377 MLX5_FLOW_LAYER_OUTER_L3_IPV6)))
2378 return rte_flow_error_set(error, EINVAL,
2379 RTE_FLOW_ERROR_TYPE_ACTION,
2381 "no IP found in pattern");
2384 (MLX5_FLOW_ACTION_SET_MAC_SRC | MLX5_FLOW_ACTION_SET_MAC_DST)) {
2385 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L2))
2386 return rte_flow_error_set(error, ENOTSUP,
2387 RTE_FLOW_ERROR_TYPE_ACTION,
2389 "no ethernet found in"
2392 if ((action_flags & MLX5_FLOW_ACTION_VXLAN_DECAP) &&
2393 !(item_flags & MLX5_FLOW_LAYER_VXLAN))
2394 return rte_flow_error_set(error, EINVAL,
2395 RTE_FLOW_ERROR_TYPE_ACTION,
2397 "no VNI pattern found"
2398 " for vxlan decap action");
2399 if ((action_flags & MLX5_FLOW_ACTION_VXLAN_ENCAP) &&
2400 (item_flags & MLX5_FLOW_LAYER_TUNNEL))
2401 return rte_flow_error_set(error, EINVAL,
2402 RTE_FLOW_ERROR_TYPE_ACTION,
2404 "vxlan encap not supported"
2405 " for tunneled traffic");
2410 * Calculate maximum size of memory for flow items of Linux TC flower.
2413 * Pointer to the flow attributes.
2415 * Pointer to the list of items.
2416 * @param[out] action_flags
2417 * Pointer to the detected actions.
2420 * Maximum size of memory for items.
2423 flow_tcf_get_items_size(const struct rte_flow_attr *attr,
2424 const struct rte_flow_item items[],
2425 uint64_t *action_flags)
2429 size += SZ_NLATTR_STRZ_OF("flower") +
2430 SZ_NLATTR_TYPE_OF(uint16_t) + /* Outer ether type. */
2431 SZ_NLATTR_NEST + /* TCA_OPTIONS. */
2432 SZ_NLATTR_TYPE_OF(uint32_t); /* TCA_CLS_FLAGS_SKIP_SW. */
2433 if (attr->group > 0)
2434 size += SZ_NLATTR_TYPE_OF(uint32_t); /* TCA_CHAIN. */
2435 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2436 switch (items->type) {
2437 case RTE_FLOW_ITEM_TYPE_VOID:
2439 case RTE_FLOW_ITEM_TYPE_PORT_ID:
2441 case RTE_FLOW_ITEM_TYPE_ETH:
2442 size += SZ_NLATTR_DATA_OF(ETHER_ADDR_LEN) * 4;
2443 /* dst/src MAC addr and mask. */
2445 case RTE_FLOW_ITEM_TYPE_VLAN:
2446 size += SZ_NLATTR_TYPE_OF(uint16_t) +
2447 /* VLAN Ether type. */
2448 SZ_NLATTR_TYPE_OF(uint8_t) + /* VLAN prio. */
2449 SZ_NLATTR_TYPE_OF(uint16_t); /* VLAN ID. */
2451 case RTE_FLOW_ITEM_TYPE_IPV4:
2452 size += SZ_NLATTR_TYPE_OF(uint8_t) + /* IP proto. */
2453 SZ_NLATTR_TYPE_OF(uint32_t) * 4;
2454 /* dst/src IP addr and mask. */
2456 case RTE_FLOW_ITEM_TYPE_IPV6:
2457 size += SZ_NLATTR_TYPE_OF(uint8_t) + /* IP proto. */
2458 SZ_NLATTR_DATA_OF(IPV6_ADDR_LEN) * 4;
2459 /* dst/src IP addr and mask. */
2461 case RTE_FLOW_ITEM_TYPE_UDP:
2462 size += SZ_NLATTR_TYPE_OF(uint8_t) + /* IP proto. */
2463 SZ_NLATTR_TYPE_OF(uint16_t) * 4;
2464 /* dst/src port and mask. */
2466 case RTE_FLOW_ITEM_TYPE_TCP:
2467 size += SZ_NLATTR_TYPE_OF(uint8_t) + /* IP proto. */
2468 SZ_NLATTR_TYPE_OF(uint16_t) * 4;
2469 /* dst/src port and mask. */
2471 case RTE_FLOW_ITEM_TYPE_VXLAN:
2472 size += SZ_NLATTR_TYPE_OF(uint32_t);
2474 * There might be no VXLAN decap action in the action
2475 * list, nonetheless the VXLAN tunnel flow requires
2476 * the decap structure to be correctly applied to
2477 * VXLAN device, set the flag to create the structure.
2478 * Translation routine will not put the decap action
2479 * in tne Netlink message if there is no actual action
2482 *action_flags |= MLX5_FLOW_ACTION_VXLAN_DECAP;
2486 "unsupported item %p type %d,"
2487 " items must be validated before flow creation",
2488 (const void *)items, items->type);
2496 * Calculate size of memory to store the VXLAN encapsultion
2497 * related items in the Netlink message buffer. Items list
2498 * is specified by RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP action.
2499 * The item list should be validated.
2502 * RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP action object.
2503 * List of pattern items to scan data from.
2506 * The size the part of Netlink message buffer to store the
2507 * VXLAN encapsulation item attributes.
2510 flow_tcf_vxlan_encap_size(const struct rte_flow_action *action)
2512 const struct rte_flow_item *items;
2515 assert(action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP);
2516 assert(action->conf);
2518 items = ((const struct rte_flow_action_vxlan_encap *)
2519 action->conf)->definition;
2521 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2522 switch (items->type) {
2523 case RTE_FLOW_ITEM_TYPE_VOID:
2525 case RTE_FLOW_ITEM_TYPE_ETH:
2526 /* This item does not require message buffer. */
2528 case RTE_FLOW_ITEM_TYPE_IPV4:
2529 size += SZ_NLATTR_DATA_OF(IPV4_ADDR_LEN) * 2;
2531 case RTE_FLOW_ITEM_TYPE_IPV6:
2532 size += SZ_NLATTR_DATA_OF(IPV6_ADDR_LEN) * 2;
2534 case RTE_FLOW_ITEM_TYPE_UDP: {
2535 const struct rte_flow_item_udp *udp = items->mask;
2537 size += SZ_NLATTR_TYPE_OF(uint16_t);
2538 if (!udp || udp->hdr.src_port != RTE_BE16(0x0000))
2539 size += SZ_NLATTR_TYPE_OF(uint16_t);
2542 case RTE_FLOW_ITEM_TYPE_VXLAN:
2543 size += SZ_NLATTR_TYPE_OF(uint32_t);
2548 "unsupported item %p type %d,"
2549 " items must be validated"
2550 " before flow creation",
2551 (const void *)items, items->type);
2559 * Calculate maximum size of memory for flow actions of Linux TC flower and
2560 * extract specified actions.
2562 * @param[in] actions
2563 * Pointer to the list of actions.
2564 * @param[out] action_flags
2565 * Pointer to the detected actions.
2568 * Maximum size of memory for actions.
2571 flow_tcf_get_actions_and_size(const struct rte_flow_action actions[],
2572 uint64_t *action_flags)
2577 size += SZ_NLATTR_NEST; /* TCA_FLOWER_ACT. */
2578 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2579 switch (actions->type) {
2580 case RTE_FLOW_ACTION_TYPE_VOID:
2582 case RTE_FLOW_ACTION_TYPE_PORT_ID:
2583 size += SZ_NLATTR_NEST + /* na_act_index. */
2584 SZ_NLATTR_STRZ_OF("mirred") +
2585 SZ_NLATTR_NEST + /* TCA_ACT_OPTIONS. */
2586 SZ_NLATTR_TYPE_OF(struct tc_mirred);
2587 flags |= MLX5_FLOW_ACTION_PORT_ID;
2589 case RTE_FLOW_ACTION_TYPE_JUMP:
2590 size += SZ_NLATTR_NEST + /* na_act_index. */
2591 SZ_NLATTR_STRZ_OF("gact") +
2592 SZ_NLATTR_NEST + /* TCA_ACT_OPTIONS. */
2593 SZ_NLATTR_TYPE_OF(struct tc_gact);
2594 flags |= MLX5_FLOW_ACTION_JUMP;
2596 case RTE_FLOW_ACTION_TYPE_DROP:
2597 size += SZ_NLATTR_NEST + /* na_act_index. */
2598 SZ_NLATTR_STRZ_OF("gact") +
2599 SZ_NLATTR_NEST + /* TCA_ACT_OPTIONS. */
2600 SZ_NLATTR_TYPE_OF(struct tc_gact);
2601 flags |= MLX5_FLOW_ACTION_DROP;
2603 case RTE_FLOW_ACTION_TYPE_COUNT:
2605 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
2606 flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
2607 goto action_of_vlan;
2608 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
2609 flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
2610 goto action_of_vlan;
2611 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
2612 flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
2613 goto action_of_vlan;
2614 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
2615 flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_PCP;
2616 goto action_of_vlan;
2618 size += SZ_NLATTR_NEST + /* na_act_index. */
2619 SZ_NLATTR_STRZ_OF("vlan") +
2620 SZ_NLATTR_NEST + /* TCA_ACT_OPTIONS. */
2621 SZ_NLATTR_TYPE_OF(struct tc_vlan) +
2622 SZ_NLATTR_TYPE_OF(uint16_t) +
2623 /* VLAN protocol. */
2624 SZ_NLATTR_TYPE_OF(uint16_t) + /* VLAN ID. */
2625 SZ_NLATTR_TYPE_OF(uint8_t); /* VLAN prio. */
2627 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
2628 size += SZ_NLATTR_NEST + /* na_act_index. */
2629 SZ_NLATTR_STRZ_OF("tunnel_key") +
2630 SZ_NLATTR_NEST + /* TCA_ACT_OPTIONS. */
2631 SZ_NLATTR_TYPE_OF(uint8_t);
2632 size += SZ_NLATTR_TYPE_OF(struct tc_tunnel_key);
2633 size += flow_tcf_vxlan_encap_size(actions) +
2634 RTE_ALIGN_CEIL /* preceding encap params. */
2635 (sizeof(struct flow_tcf_vxlan_encap),
2637 flags |= MLX5_FLOW_ACTION_VXLAN_ENCAP;
2639 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
2640 size += SZ_NLATTR_NEST + /* na_act_index. */
2641 SZ_NLATTR_STRZ_OF("tunnel_key") +
2642 SZ_NLATTR_NEST + /* TCA_ACT_OPTIONS. */
2643 SZ_NLATTR_TYPE_OF(uint8_t);
2644 size += SZ_NLATTR_TYPE_OF(struct tc_tunnel_key);
2645 size += RTE_ALIGN_CEIL /* preceding decap params. */
2646 (sizeof(struct flow_tcf_vxlan_decap),
2648 flags |= MLX5_FLOW_ACTION_VXLAN_DECAP;
2650 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
2651 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
2652 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
2653 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
2654 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
2655 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
2656 case RTE_FLOW_ACTION_TYPE_SET_TTL:
2657 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
2658 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
2659 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
2660 size += flow_tcf_get_pedit_actions_size(&actions,
2665 "unsupported action %p type %d,"
2666 " items must be validated before flow creation",
2667 (const void *)actions, actions->type);
2671 *action_flags = flags;
2676 * Brand rtnetlink buffer with unique handle.
2678 * This handle should be unique for a given network interface to avoid
2682 * Pointer to Netlink message.
2684 * Unique 32-bit handle to use.
2687 flow_tcf_nl_brand(struct nlmsghdr *nlh, uint32_t handle)
2689 struct tcmsg *tcm = mnl_nlmsg_get_payload(nlh);
2691 tcm->tcm_handle = handle;
2692 DRV_LOG(DEBUG, "Netlink msg %p is branded with handle %x",
2693 (void *)nlh, handle);
2697 * Prepare a flow object for Linux TC flower. It calculates the maximum size of
2698 * memory required, allocates the memory, initializes Netlink message headers
2699 * and set unique TC message handle.
2702 * Pointer to the flow attributes.
2704 * Pointer to the list of items.
2705 * @param[in] actions
2706 * Pointer to the list of actions.
2708 * Pointer to the error structure.
2711 * Pointer to mlx5_flow object on success,
2712 * otherwise NULL and rte_errno is set.
2714 static struct mlx5_flow *
2715 flow_tcf_prepare(const struct rte_flow_attr *attr,
2716 const struct rte_flow_item items[],
2717 const struct rte_flow_action actions[],
2718 struct rte_flow_error *error)
2720 size_t size = RTE_ALIGN_CEIL
2721 (sizeof(struct mlx5_flow),
2722 alignof(struct flow_tcf_tunnel_hdr)) +
2723 MNL_ALIGN(sizeof(struct nlmsghdr)) +
2724 MNL_ALIGN(sizeof(struct tcmsg));
2725 struct mlx5_flow *dev_flow;
2726 uint64_t action_flags = 0;
2727 struct nlmsghdr *nlh;
2729 uint8_t *sp, *tun = NULL;
2731 size += flow_tcf_get_items_size(attr, items, &action_flags);
2732 size += flow_tcf_get_actions_and_size(actions, &action_flags);
2733 dev_flow = rte_zmalloc(__func__, size, MNL_ALIGNTO);
2735 rte_flow_error_set(error, ENOMEM,
2736 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2737 "not enough memory to create E-Switch flow");
2740 sp = (uint8_t *)(dev_flow + 1);
2741 if (action_flags & MLX5_FLOW_ACTION_VXLAN_ENCAP) {
2743 (sp, alignof(struct flow_tcf_tunnel_hdr));
2745 sp += RTE_ALIGN_CEIL
2746 (sizeof(struct flow_tcf_vxlan_encap),
2749 size -= RTE_ALIGN_CEIL
2750 (sizeof(struct flow_tcf_vxlan_encap),
2753 } else if (action_flags & MLX5_FLOW_ACTION_VXLAN_DECAP) {
2755 (sp, alignof(struct flow_tcf_tunnel_hdr));
2757 sp += RTE_ALIGN_CEIL
2758 (sizeof(struct flow_tcf_vxlan_decap),
2761 size -= RTE_ALIGN_CEIL
2762 (sizeof(struct flow_tcf_vxlan_decap),
2766 sp = RTE_PTR_ALIGN(sp, MNL_ALIGNTO);
2768 nlh = mnl_nlmsg_put_header(sp);
2769 tcm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tcm));
2770 *dev_flow = (struct mlx5_flow){
2771 .tcf = (struct mlx5_flow_tcf){
2773 .nlsize = size - RTE_ALIGN_CEIL
2774 (sizeof(struct mlx5_flow),
2775 alignof(struct flow_tcf_tunnel_hdr)),
2777 .tunnel = (struct flow_tcf_tunnel_hdr *)tun,
2782 if (action_flags & MLX5_FLOW_ACTION_VXLAN_DECAP)
2783 dev_flow->tcf.tunnel->type = FLOW_TCF_TUNACT_VXLAN_DECAP;
2784 else if (action_flags & MLX5_FLOW_ACTION_VXLAN_ENCAP)
2785 dev_flow->tcf.tunnel->type = FLOW_TCF_TUNACT_VXLAN_ENCAP;
2787 * Generate a reasonably unique handle based on the address of the
2790 * This is straightforward on 32-bit systems where the flow pointer can
2791 * be used directly. Otherwise, its least significant part is taken
2792 * after shifting it by the previous power of two of the pointed buffer
2795 if (sizeof(dev_flow) <= 4)
2796 flow_tcf_nl_brand(nlh, (uintptr_t)dev_flow);
2798 flow_tcf_nl_brand(nlh, (uintptr_t)dev_flow >>
2799 rte_log2_u32(rte_align32prevpow2(size)));
2804 * Make adjustments for supporting count actions.
2807 * Pointer to the Ethernet device structure.
2808 * @param[in] dev_flow
2809 * Pointer to mlx5_flow.
2811 * Pointer to error structure.
2814 * 0 On success else a negative errno value is returned and rte_errno is set.
2817 flow_tcf_translate_action_count(struct rte_eth_dev *dev __rte_unused,
2818 struct mlx5_flow *dev_flow,
2819 struct rte_flow_error *error)
2821 struct rte_flow *flow = dev_flow->flow;
2823 if (!flow->counter) {
2824 flow->counter = flow_tcf_counter_new();
2826 return rte_flow_error_set(error, rte_errno,
2827 RTE_FLOW_ERROR_TYPE_ACTION,
2829 "cannot get counter"
2836 * Convert VXLAN VNI to 32-bit integer.
2839 * VXLAN VNI in 24-bit wire format.
2842 * VXLAN VNI as a 32-bit integer value in network endian.
2844 static inline rte_be32_t
2845 vxlan_vni_as_be32(const uint8_t vni[3])
2851 .vni = { 0, vni[0], vni[1], vni[2] },
2857 * Helper function to process RTE_FLOW_ITEM_TYPE_ETH entry in configuration
2858 * of action RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP. Fills the MAC address fields
2859 * in the encapsulation parameters structure. The item must be prevalidated,
2860 * no any validation checks performed by function.
2863 * RTE_FLOW_ITEM_TYPE_ETH entry specification.
2865 * RTE_FLOW_ITEM_TYPE_ETH entry mask.
2867 * Structure to fill the gathered MAC address data.
2870 flow_tcf_parse_vxlan_encap_eth(const struct rte_flow_item_eth *spec,
2871 const struct rte_flow_item_eth *mask,
2872 struct flow_tcf_vxlan_encap *encap)
2874 /* Item must be validated before. No redundant checks. */
2876 if (!mask || !memcmp(&mask->dst,
2877 &rte_flow_item_eth_mask.dst,
2878 sizeof(rte_flow_item_eth_mask.dst))) {
2880 * Ethernet addresses are not supported by
2881 * tc as tunnel_key parameters. Destination
2882 * address is needed to form encap packet
2883 * header and retrieved by kernel from
2884 * implicit sources (ARP table, etc),
2885 * address masks are not supported at all.
2887 encap->eth.dst = spec->dst;
2888 encap->mask |= FLOW_TCF_ENCAP_ETH_DST;
2890 if (!mask || !memcmp(&mask->src,
2891 &rte_flow_item_eth_mask.src,
2892 sizeof(rte_flow_item_eth_mask.src))) {
2894 * Ethernet addresses are not supported by
2895 * tc as tunnel_key parameters. Source ethernet
2896 * address is ignored anyway.
2898 encap->eth.src = spec->src;
2899 encap->mask |= FLOW_TCF_ENCAP_ETH_SRC;
2904 * Helper function to process RTE_FLOW_ITEM_TYPE_IPV4 entry in configuration
2905 * of action RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP. Fills the IPV4 address fields
2906 * in the encapsulation parameters structure. The item must be prevalidated,
2907 * no any validation checks performed by function.
2910 * RTE_FLOW_ITEM_TYPE_IPV4 entry specification.
2912 * Structure to fill the gathered IPV4 address data.
2915 flow_tcf_parse_vxlan_encap_ipv4(const struct rte_flow_item_ipv4 *spec,
2916 struct flow_tcf_vxlan_encap *encap)
2918 /* Item must be validated before. No redundant checks. */
2920 encap->ipv4.dst = spec->hdr.dst_addr;
2921 encap->ipv4.src = spec->hdr.src_addr;
2922 encap->mask |= FLOW_TCF_ENCAP_IPV4_SRC |
2923 FLOW_TCF_ENCAP_IPV4_DST;
2927 * Helper function to process RTE_FLOW_ITEM_TYPE_IPV6 entry in configuration
2928 * of action RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP. Fills the IPV6 address fields
2929 * in the encapsulation parameters structure. The item must be prevalidated,
2930 * no any validation checks performed by function.
2933 * RTE_FLOW_ITEM_TYPE_IPV6 entry specification.
2935 * Structure to fill the gathered IPV6 address data.
2938 flow_tcf_parse_vxlan_encap_ipv6(const struct rte_flow_item_ipv6 *spec,
2939 struct flow_tcf_vxlan_encap *encap)
2941 /* Item must be validated before. No redundant checks. */
2943 memcpy(encap->ipv6.dst, spec->hdr.dst_addr, IPV6_ADDR_LEN);
2944 memcpy(encap->ipv6.src, spec->hdr.src_addr, IPV6_ADDR_LEN);
2945 encap->mask |= FLOW_TCF_ENCAP_IPV6_SRC |
2946 FLOW_TCF_ENCAP_IPV6_DST;
2950 * Helper function to process RTE_FLOW_ITEM_TYPE_UDP entry in configuration
2951 * of action RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP. Fills the UDP port fields
2952 * in the encapsulation parameters structure. The item must be prevalidated,
2953 * no any validation checks performed by function.
2956 * RTE_FLOW_ITEM_TYPE_UDP entry specification.
2958 * RTE_FLOW_ITEM_TYPE_UDP entry mask.
2960 * Structure to fill the gathered UDP port data.
2963 flow_tcf_parse_vxlan_encap_udp(const struct rte_flow_item_udp *spec,
2964 const struct rte_flow_item_udp *mask,
2965 struct flow_tcf_vxlan_encap *encap)
2968 encap->udp.dst = spec->hdr.dst_port;
2969 encap->mask |= FLOW_TCF_ENCAP_UDP_DST;
2970 if (!mask || mask->hdr.src_port != RTE_BE16(0x0000)) {
2971 encap->udp.src = spec->hdr.src_port;
2972 encap->mask |= FLOW_TCF_ENCAP_IPV4_SRC;
2977 * Helper function to process RTE_FLOW_ITEM_TYPE_VXLAN entry in configuration
2978 * of action RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP. Fills the VNI fields
2979 * in the encapsulation parameters structure. The item must be prevalidated,
2980 * no any validation checks performed by function.
2983 * RTE_FLOW_ITEM_TYPE_VXLAN entry specification.
2985 * Structure to fill the gathered VNI address data.
2988 flow_tcf_parse_vxlan_encap_vni(const struct rte_flow_item_vxlan *spec,
2989 struct flow_tcf_vxlan_encap *encap)
2991 /* Item must be validated before. Do not redundant checks. */
2993 memcpy(encap->vxlan.vni, spec->vni, sizeof(encap->vxlan.vni));
2994 encap->mask |= FLOW_TCF_ENCAP_VXLAN_VNI;
2998 * Populate consolidated encapsulation object from list of pattern items.
3000 * Helper function to process configuration of action such as
3001 * RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP. The item list should be
3002 * validated, there is no way to return an meaningful error.
3005 * RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP action object.
3006 * List of pattern items to gather data from.
3008 * Structure to fill gathered data.
3011 flow_tcf_vxlan_encap_parse(const struct rte_flow_action *action,
3012 struct flow_tcf_vxlan_encap *encap)
3015 const struct rte_flow_item_eth *eth;
3016 const struct rte_flow_item_ipv4 *ipv4;
3017 const struct rte_flow_item_ipv6 *ipv6;
3018 const struct rte_flow_item_udp *udp;
3019 const struct rte_flow_item_vxlan *vxlan;
3021 const struct rte_flow_item *items;
3023 assert(action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP);
3024 assert(action->conf);
3026 items = ((const struct rte_flow_action_vxlan_encap *)
3027 action->conf)->definition;
3029 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
3030 switch (items->type) {
3031 case RTE_FLOW_ITEM_TYPE_VOID:
3033 case RTE_FLOW_ITEM_TYPE_ETH:
3034 mask.eth = items->mask;
3035 spec.eth = items->spec;
3036 flow_tcf_parse_vxlan_encap_eth(spec.eth, mask.eth,
3039 case RTE_FLOW_ITEM_TYPE_IPV4:
3040 spec.ipv4 = items->spec;
3041 flow_tcf_parse_vxlan_encap_ipv4(spec.ipv4, encap);
3043 case RTE_FLOW_ITEM_TYPE_IPV6:
3044 spec.ipv6 = items->spec;
3045 flow_tcf_parse_vxlan_encap_ipv6(spec.ipv6, encap);
3047 case RTE_FLOW_ITEM_TYPE_UDP:
3048 mask.udp = items->mask;
3049 spec.udp = items->spec;
3050 flow_tcf_parse_vxlan_encap_udp(spec.udp, mask.udp,
3053 case RTE_FLOW_ITEM_TYPE_VXLAN:
3054 spec.vxlan = items->spec;
3055 flow_tcf_parse_vxlan_encap_vni(spec.vxlan, encap);
3060 "unsupported item %p type %d,"
3061 " items must be validated"
3062 " before flow creation",
3063 (const void *)items, items->type);
3071 * Translate flow for Linux TC flower and construct Netlink message.
3074 * Pointer to the priv structure.
3075 * @param[in, out] flow
3076 * Pointer to the sub flow.
3078 * Pointer to the flow attributes.
3080 * Pointer to the list of items.
3081 * @param[in] actions
3082 * Pointer to the list of actions.
3084 * Pointer to the error structure.
3087 * 0 on success, a negative errno value otherwise and rte_errno is set.
3090 flow_tcf_translate(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow,
3091 const struct rte_flow_attr *attr,
3092 const struct rte_flow_item items[],
3093 const struct rte_flow_action actions[],
3094 struct rte_flow_error *error)
3097 const struct rte_flow_item_port_id *port_id;
3098 const struct rte_flow_item_eth *eth;
3099 const struct rte_flow_item_vlan *vlan;
3100 const struct rte_flow_item_ipv4 *ipv4;
3101 const struct rte_flow_item_ipv6 *ipv6;
3102 const struct rte_flow_item_tcp *tcp;
3103 const struct rte_flow_item_udp *udp;
3104 const struct rte_flow_item_vxlan *vxlan;
3107 const struct rte_flow_action_port_id *port_id;
3108 const struct rte_flow_action_jump *jump;
3109 const struct rte_flow_action_of_push_vlan *of_push_vlan;
3110 const struct rte_flow_action_of_set_vlan_vid *
3112 const struct rte_flow_action_of_set_vlan_pcp *
3116 struct flow_tcf_tunnel_hdr *hdr;
3117 struct flow_tcf_vxlan_decap *vxlan;
3122 struct flow_tcf_tunnel_hdr *hdr;
3123 struct flow_tcf_vxlan_encap *vxlan;
3127 struct flow_tcf_ptoi ptoi[PTOI_TABLE_SZ_MAX(dev)];
3128 struct nlmsghdr *nlh = dev_flow->tcf.nlh;
3129 struct tcmsg *tcm = dev_flow->tcf.tcm;
3130 uint32_t na_act_index_cur;
3131 rte_be16_t inner_etype = RTE_BE16(ETH_P_ALL);
3132 rte_be16_t outer_etype = RTE_BE16(ETH_P_ALL);
3133 rte_be16_t vlan_etype = RTE_BE16(ETH_P_ALL);
3134 bool ip_proto_set = 0;
3135 bool tunnel_outer = 0;
3136 struct nlattr *na_flower;
3137 struct nlattr *na_flower_act;
3138 struct nlattr *na_vlan_id = NULL;
3139 struct nlattr *na_vlan_priority = NULL;
3140 uint64_t item_flags = 0;
3143 claim_nonzero(flow_tcf_build_ptoi_table(dev, ptoi,
3144 PTOI_TABLE_SZ_MAX(dev)));
3145 if (dev_flow->tcf.tunnel) {
3146 switch (dev_flow->tcf.tunnel->type) {
3147 case FLOW_TCF_TUNACT_VXLAN_DECAP:
3148 decap.vxlan = dev_flow->tcf.vxlan_decap;
3151 case FLOW_TCF_TUNACT_VXLAN_ENCAP:
3152 encap.vxlan = dev_flow->tcf.vxlan_encap;
3154 /* New tunnel actions can be added here. */
3160 nlh = dev_flow->tcf.nlh;
3161 tcm = dev_flow->tcf.tcm;
3162 /* Prepare API must have been called beforehand. */
3163 assert(nlh != NULL && tcm != NULL);
3164 tcm->tcm_family = AF_UNSPEC;
3165 tcm->tcm_ifindex = ptoi[0].ifindex;
3166 tcm->tcm_parent = TC_H_MAKE(TC_H_INGRESS, TC_H_MIN_INGRESS);
3168 * Priority cannot be zero to prevent the kernel from picking one
3171 tcm->tcm_info = TC_H_MAKE((attr->priority + 1) << 16, outer_etype);
3172 if (attr->group > 0)
3173 mnl_attr_put_u32(nlh, TCA_CHAIN, attr->group);
3174 mnl_attr_put_strz(nlh, TCA_KIND, "flower");
3175 na_flower = mnl_attr_nest_start(nlh, TCA_OPTIONS);
3176 for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
3179 switch (items->type) {
3180 case RTE_FLOW_ITEM_TYPE_VOID:
3182 case RTE_FLOW_ITEM_TYPE_PORT_ID:
3183 mask.port_id = flow_tcf_item_mask
3184 (items, &rte_flow_item_port_id_mask,
3185 &flow_tcf_mask_supported.port_id,
3186 &flow_tcf_mask_empty.port_id,
3187 sizeof(flow_tcf_mask_supported.port_id),
3189 assert(mask.port_id);
3190 if (mask.port_id == &flow_tcf_mask_empty.port_id)
3192 spec.port_id = items->spec;
3193 if (!mask.port_id->id)
3196 for (i = 0; ptoi[i].ifindex; ++i)
3197 if (ptoi[i].port_id == spec.port_id->id)
3199 assert(ptoi[i].ifindex);
3200 tcm->tcm_ifindex = ptoi[i].ifindex;
3202 case RTE_FLOW_ITEM_TYPE_ETH:
3203 item_flags |= (item_flags & MLX5_FLOW_LAYER_TUNNEL) ?
3204 MLX5_FLOW_LAYER_INNER_L2 :
3205 MLX5_FLOW_LAYER_OUTER_L2;
3206 mask.eth = flow_tcf_item_mask
3207 (items, &rte_flow_item_eth_mask,
3208 &flow_tcf_mask_supported.eth,
3209 &flow_tcf_mask_empty.eth,
3210 sizeof(flow_tcf_mask_supported.eth),
3213 if (mask.eth == &flow_tcf_mask_empty.eth)
3215 spec.eth = items->spec;
3216 if (mask.eth->type) {
3217 if (item_flags & MLX5_FLOW_LAYER_TUNNEL)
3218 inner_etype = spec.eth->type;
3220 outer_etype = spec.eth->type;
3224 "outer L2 addresses cannot be"
3225 " forced is outer ones for tunnel,"
3226 " parameter is ignored");
3229 if (!is_zero_ether_addr(&mask.eth->dst)) {
3230 mnl_attr_put(nlh, TCA_FLOWER_KEY_ETH_DST,
3232 spec.eth->dst.addr_bytes);
3233 mnl_attr_put(nlh, TCA_FLOWER_KEY_ETH_DST_MASK,
3235 mask.eth->dst.addr_bytes);
3237 if (!is_zero_ether_addr(&mask.eth->src)) {
3238 mnl_attr_put(nlh, TCA_FLOWER_KEY_ETH_SRC,
3240 spec.eth->src.addr_bytes);
3241 mnl_attr_put(nlh, TCA_FLOWER_KEY_ETH_SRC_MASK,
3243 mask.eth->src.addr_bytes);
3245 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3247 case RTE_FLOW_ITEM_TYPE_VLAN:
3250 assert(!tunnel_outer);
3251 item_flags |= MLX5_FLOW_LAYER_OUTER_VLAN;
3252 mask.vlan = flow_tcf_item_mask
3253 (items, &rte_flow_item_vlan_mask,
3254 &flow_tcf_mask_supported.vlan,
3255 &flow_tcf_mask_empty.vlan,
3256 sizeof(flow_tcf_mask_supported.vlan),
3259 if (mask.vlan == &flow_tcf_mask_empty.vlan)
3261 spec.vlan = items->spec;
3262 assert(outer_etype == RTE_BE16(ETH_P_ALL) ||
3263 outer_etype == RTE_BE16(ETH_P_8021Q));
3264 outer_etype = RTE_BE16(ETH_P_8021Q);
3265 if (mask.vlan->inner_type)
3266 vlan_etype = spec.vlan->inner_type;
3267 if (mask.vlan->tci & RTE_BE16(0xe000))
3268 mnl_attr_put_u8(nlh, TCA_FLOWER_KEY_VLAN_PRIO,
3270 (spec.vlan->tci) >> 13) & 0x7);
3271 if (mask.vlan->tci & RTE_BE16(0x0fff))
3272 mnl_attr_put_u16(nlh, TCA_FLOWER_KEY_VLAN_ID,
3276 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3278 case RTE_FLOW_ITEM_TYPE_IPV4:
3279 item_flags |= (item_flags & MLX5_FLOW_LAYER_TUNNEL) ?
3280 MLX5_FLOW_LAYER_INNER_L3_IPV4 :
3281 MLX5_FLOW_LAYER_OUTER_L3_IPV4;
3282 mask.ipv4 = flow_tcf_item_mask
3283 (items, &rte_flow_item_ipv4_mask,
3284 &flow_tcf_mask_supported.ipv4,
3285 &flow_tcf_mask_empty.ipv4,
3286 sizeof(flow_tcf_mask_supported.ipv4),
3289 if (item_flags & MLX5_FLOW_LAYER_TUNNEL) {
3290 assert(inner_etype == RTE_BE16(ETH_P_ALL) ||
3291 inner_etype == RTE_BE16(ETH_P_IP));
3292 inner_etype = RTE_BE16(ETH_P_IP);
3293 } else if (outer_etype == RTE_BE16(ETH_P_8021Q)) {
3294 assert(vlan_etype == RTE_BE16(ETH_P_ALL) ||
3295 vlan_etype == RTE_BE16(ETH_P_IP));
3296 vlan_etype = RTE_BE16(ETH_P_IP);
3298 assert(outer_etype == RTE_BE16(ETH_P_ALL) ||
3299 outer_etype == RTE_BE16(ETH_P_IP));
3300 outer_etype = RTE_BE16(ETH_P_IP);
3302 spec.ipv4 = items->spec;
3303 if (!tunnel_outer && mask.ipv4->hdr.next_proto_id) {
3305 * No way to set IP protocol for outer tunnel
3306 * layers. Usually it is fixed, for example,
3307 * to UDP for VXLAN/GPE.
3309 assert(spec.ipv4); /* Mask is not empty. */
3310 mnl_attr_put_u8(nlh, TCA_FLOWER_KEY_IP_PROTO,
3311 spec.ipv4->hdr.next_proto_id);
3314 if (mask.ipv4 == &flow_tcf_mask_empty.ipv4 ||
3315 (!mask.ipv4->hdr.src_addr &&
3316 !mask.ipv4->hdr.dst_addr)) {
3320 * For tunnel outer we must set outer IP key
3321 * anyway, even if the specification/mask is
3322 * empty. There is no another way to tell
3323 * kernel about he outer layer protocol.
3326 (nlh, TCA_FLOWER_KEY_ENC_IPV4_SRC,
3327 mask.ipv4->hdr.src_addr);
3329 (nlh, TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
3330 mask.ipv4->hdr.src_addr);
3331 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3334 if (mask.ipv4->hdr.src_addr) {
3336 (nlh, tunnel_outer ?
3337 TCA_FLOWER_KEY_ENC_IPV4_SRC :
3338 TCA_FLOWER_KEY_IPV4_SRC,
3339 spec.ipv4->hdr.src_addr);
3341 (nlh, tunnel_outer ?
3342 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK :
3343 TCA_FLOWER_KEY_IPV4_SRC_MASK,
3344 mask.ipv4->hdr.src_addr);
3346 if (mask.ipv4->hdr.dst_addr) {
3348 (nlh, tunnel_outer ?
3349 TCA_FLOWER_KEY_ENC_IPV4_DST :
3350 TCA_FLOWER_KEY_IPV4_DST,
3351 spec.ipv4->hdr.dst_addr);
3353 (nlh, tunnel_outer ?
3354 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK :
3355 TCA_FLOWER_KEY_IPV4_DST_MASK,
3356 mask.ipv4->hdr.dst_addr);
3358 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3360 case RTE_FLOW_ITEM_TYPE_IPV6: {
3361 bool ipv6_src, ipv6_dst;
3363 item_flags |= (item_flags & MLX5_FLOW_LAYER_TUNNEL) ?
3364 MLX5_FLOW_LAYER_INNER_L3_IPV6 :
3365 MLX5_FLOW_LAYER_OUTER_L3_IPV6;
3366 mask.ipv6 = flow_tcf_item_mask
3367 (items, &rte_flow_item_ipv6_mask,
3368 &flow_tcf_mask_supported.ipv6,
3369 &flow_tcf_mask_empty.ipv6,
3370 sizeof(flow_tcf_mask_supported.ipv6),
3373 if (item_flags & MLX5_FLOW_LAYER_TUNNEL) {
3374 assert(inner_etype == RTE_BE16(ETH_P_ALL) ||
3375 inner_etype == RTE_BE16(ETH_P_IPV6));
3376 inner_etype = RTE_BE16(ETH_P_IPV6);
3377 } else if (outer_etype == RTE_BE16(ETH_P_8021Q)) {
3378 assert(vlan_etype == RTE_BE16(ETH_P_ALL) ||
3379 vlan_etype == RTE_BE16(ETH_P_IPV6));
3380 vlan_etype = RTE_BE16(ETH_P_IPV6);
3382 assert(outer_etype == RTE_BE16(ETH_P_ALL) ||
3383 outer_etype == RTE_BE16(ETH_P_IPV6));
3384 outer_etype = RTE_BE16(ETH_P_IPV6);
3386 spec.ipv6 = items->spec;
3387 if (!tunnel_outer && mask.ipv6->hdr.proto) {
3389 * No way to set IP protocol for outer tunnel
3390 * layers. Usually it is fixed, for example,
3391 * to UDP for VXLAN/GPE.
3393 assert(spec.ipv6); /* Mask is not empty. */
3394 mnl_attr_put_u8(nlh, TCA_FLOWER_KEY_IP_PROTO,
3395 spec.ipv6->hdr.proto);
3398 ipv6_dst = !IN6_IS_ADDR_UNSPECIFIED
3399 (mask.ipv6->hdr.dst_addr);
3400 ipv6_src = !IN6_IS_ADDR_UNSPECIFIED
3401 (mask.ipv6->hdr.src_addr);
3402 if (mask.ipv6 == &flow_tcf_mask_empty.ipv6 ||
3403 (!ipv6_dst && !ipv6_src)) {
3407 * For tunnel outer we must set outer IP key
3408 * anyway, even if the specification/mask is
3409 * empty. There is no another way to tell
3410 * kernel about he outer layer protocol.
3413 TCA_FLOWER_KEY_ENC_IPV6_SRC,
3415 mask.ipv6->hdr.src_addr);
3417 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
3419 mask.ipv6->hdr.src_addr);
3420 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3424 mnl_attr_put(nlh, tunnel_outer ?
3425 TCA_FLOWER_KEY_ENC_IPV6_SRC :
3426 TCA_FLOWER_KEY_IPV6_SRC,
3428 spec.ipv6->hdr.src_addr);
3429 mnl_attr_put(nlh, tunnel_outer ?
3430 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK :
3431 TCA_FLOWER_KEY_IPV6_SRC_MASK,
3433 mask.ipv6->hdr.src_addr);
3436 mnl_attr_put(nlh, tunnel_outer ?
3437 TCA_FLOWER_KEY_ENC_IPV6_DST :
3438 TCA_FLOWER_KEY_IPV6_DST,
3440 spec.ipv6->hdr.dst_addr);
3441 mnl_attr_put(nlh, tunnel_outer ?
3442 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK :
3443 TCA_FLOWER_KEY_IPV6_DST_MASK,
3445 mask.ipv6->hdr.dst_addr);
3447 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3450 case RTE_FLOW_ITEM_TYPE_UDP:
3451 item_flags |= (item_flags & MLX5_FLOW_LAYER_TUNNEL) ?
3452 MLX5_FLOW_LAYER_INNER_L4_UDP :
3453 MLX5_FLOW_LAYER_OUTER_L4_UDP;
3454 mask.udp = flow_tcf_item_mask
3455 (items, &rte_flow_item_udp_mask,
3456 &flow_tcf_mask_supported.udp,
3457 &flow_tcf_mask_empty.udp,
3458 sizeof(flow_tcf_mask_supported.udp),
3461 spec.udp = items->spec;
3462 if (!tunnel_outer) {
3465 (nlh, TCA_FLOWER_KEY_IP_PROTO,
3467 if (mask.udp == &flow_tcf_mask_empty.udp)
3470 assert(mask.udp != &flow_tcf_mask_empty.udp);
3471 decap.vxlan->udp_port =
3473 (spec.udp->hdr.dst_port);
3475 if (mask.udp->hdr.src_port) {
3477 (nlh, tunnel_outer ?
3478 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT :
3479 TCA_FLOWER_KEY_UDP_SRC,
3480 spec.udp->hdr.src_port);
3482 (nlh, tunnel_outer ?
3483 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK :
3484 TCA_FLOWER_KEY_UDP_SRC_MASK,
3485 mask.udp->hdr.src_port);
3487 if (mask.udp->hdr.dst_port) {
3489 (nlh, tunnel_outer ?
3490 TCA_FLOWER_KEY_ENC_UDP_DST_PORT :
3491 TCA_FLOWER_KEY_UDP_DST,
3492 spec.udp->hdr.dst_port);
3494 (nlh, tunnel_outer ?
3495 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK :
3496 TCA_FLOWER_KEY_UDP_DST_MASK,
3497 mask.udp->hdr.dst_port);
3499 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3501 case RTE_FLOW_ITEM_TYPE_TCP:
3502 item_flags |= (item_flags & MLX5_FLOW_LAYER_TUNNEL) ?
3503 MLX5_FLOW_LAYER_INNER_L4_TCP :
3504 MLX5_FLOW_LAYER_OUTER_L4_TCP;
3505 mask.tcp = flow_tcf_item_mask
3506 (items, &rte_flow_item_tcp_mask,
3507 &flow_tcf_mask_supported.tcp,
3508 &flow_tcf_mask_empty.tcp,
3509 sizeof(flow_tcf_mask_supported.tcp),
3513 mnl_attr_put_u8(nlh, TCA_FLOWER_KEY_IP_PROTO,
3515 if (mask.tcp == &flow_tcf_mask_empty.tcp)
3517 spec.tcp = items->spec;
3518 if (mask.tcp->hdr.src_port) {
3519 mnl_attr_put_u16(nlh, TCA_FLOWER_KEY_TCP_SRC,
3520 spec.tcp->hdr.src_port);
3521 mnl_attr_put_u16(nlh,
3522 TCA_FLOWER_KEY_TCP_SRC_MASK,
3523 mask.tcp->hdr.src_port);
3525 if (mask.tcp->hdr.dst_port) {
3526 mnl_attr_put_u16(nlh, TCA_FLOWER_KEY_TCP_DST,
3527 spec.tcp->hdr.dst_port);
3528 mnl_attr_put_u16(nlh,
3529 TCA_FLOWER_KEY_TCP_DST_MASK,
3530 mask.tcp->hdr.dst_port);
3532 if (mask.tcp->hdr.tcp_flags) {
3535 TCA_FLOWER_KEY_TCP_FLAGS,
3537 (spec.tcp->hdr.tcp_flags));
3540 TCA_FLOWER_KEY_TCP_FLAGS_MASK,
3542 (mask.tcp->hdr.tcp_flags));
3544 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3546 case RTE_FLOW_ITEM_TYPE_VXLAN:
3547 assert(decap.vxlan);
3549 item_flags |= MLX5_FLOW_LAYER_VXLAN;
3550 spec.vxlan = items->spec;
3551 mnl_attr_put_u32(nlh,
3552 TCA_FLOWER_KEY_ENC_KEY_ID,
3553 vxlan_vni_as_be32(spec.vxlan->vni));
3554 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3557 return rte_flow_error_set(error, ENOTSUP,
3558 RTE_FLOW_ERROR_TYPE_ITEM,
3559 NULL, "item not supported");
3563 * Set the ether_type flower key and tc rule protocol:
3564 * - if there is nor VLAN neither VXLAN the key is taken from
3565 * eth item directly or deduced from L3 items.
3566 * - if there is vlan item then key is fixed to 802.1q.
3567 * - if there is vxlan item then key is set to inner tunnel type.
3568 * - simultaneous vlan and vxlan items are prohibited.
3570 if (outer_etype != RTE_BE16(ETH_P_ALL)) {
3571 tcm->tcm_info = TC_H_MAKE((attr->priority + 1) << 16,
3573 if (item_flags & MLX5_FLOW_LAYER_TUNNEL) {
3574 if (inner_etype != RTE_BE16(ETH_P_ALL))
3575 mnl_attr_put_u16(nlh,
3576 TCA_FLOWER_KEY_ETH_TYPE,
3579 mnl_attr_put_u16(nlh,
3580 TCA_FLOWER_KEY_ETH_TYPE,
3582 if (outer_etype == RTE_BE16(ETH_P_8021Q) &&
3583 vlan_etype != RTE_BE16(ETH_P_ALL))
3584 mnl_attr_put_u16(nlh,
3585 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
3588 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3590 na_flower_act = mnl_attr_nest_start(nlh, TCA_FLOWER_ACT);
3591 na_act_index_cur = 1;
3592 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3593 struct nlattr *na_act_index;
3594 struct nlattr *na_act;
3595 unsigned int vlan_act;
3598 switch (actions->type) {
3599 case RTE_FLOW_ACTION_TYPE_VOID:
3601 case RTE_FLOW_ACTION_TYPE_PORT_ID:
3602 conf.port_id = actions->conf;
3603 if (conf.port_id->original)
3606 for (i = 0; ptoi[i].ifindex; ++i)
3607 if (ptoi[i].port_id == conf.port_id->id)
3609 assert(ptoi[i].ifindex);
3611 mnl_attr_nest_start(nlh, na_act_index_cur++);
3612 assert(na_act_index);
3613 mnl_attr_put_strz(nlh, TCA_ACT_KIND, "mirred");
3614 na_act = mnl_attr_nest_start(nlh, TCA_ACT_OPTIONS);
3617 assert(dev_flow->tcf.tunnel);
3618 dev_flow->tcf.tunnel->ifindex_ptr =
3619 &((struct tc_mirred *)
3620 mnl_attr_get_payload
3621 (mnl_nlmsg_get_payload_tail
3624 mnl_attr_put(nlh, TCA_MIRRED_PARMS,
3625 sizeof(struct tc_mirred),
3626 &(struct tc_mirred){
3627 .action = TC_ACT_STOLEN,
3628 .eaction = TCA_EGRESS_REDIR,
3629 .ifindex = ptoi[i].ifindex,
3631 mnl_attr_nest_end(nlh, na_act);
3632 mnl_attr_nest_end(nlh, na_act_index);
3634 case RTE_FLOW_ACTION_TYPE_JUMP:
3635 conf.jump = actions->conf;
3637 mnl_attr_nest_start(nlh, na_act_index_cur++);
3638 assert(na_act_index);
3639 mnl_attr_put_strz(nlh, TCA_ACT_KIND, "gact");
3640 na_act = mnl_attr_nest_start(nlh, TCA_ACT_OPTIONS);
3642 mnl_attr_put(nlh, TCA_GACT_PARMS,
3643 sizeof(struct tc_gact),
3645 .action = TC_ACT_GOTO_CHAIN |
3648 mnl_attr_nest_end(nlh, na_act);
3649 mnl_attr_nest_end(nlh, na_act_index);
3651 case RTE_FLOW_ACTION_TYPE_DROP:
3653 mnl_attr_nest_start(nlh, na_act_index_cur++);
3654 assert(na_act_index);
3655 mnl_attr_put_strz(nlh, TCA_ACT_KIND, "gact");
3656 na_act = mnl_attr_nest_start(nlh, TCA_ACT_OPTIONS);
3658 mnl_attr_put(nlh, TCA_GACT_PARMS,
3659 sizeof(struct tc_gact),
3661 .action = TC_ACT_SHOT,
3663 mnl_attr_nest_end(nlh, na_act);
3664 mnl_attr_nest_end(nlh, na_act_index);
3666 case RTE_FLOW_ACTION_TYPE_COUNT:
3668 * Driver adds the count action implicitly for
3669 * each rule it creates.
3671 ret = flow_tcf_translate_action_count(dev,
3676 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
3677 conf.of_push_vlan = NULL;
3678 vlan_act = TCA_VLAN_ACT_POP;
3679 goto action_of_vlan;
3680 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
3681 conf.of_push_vlan = actions->conf;
3682 vlan_act = TCA_VLAN_ACT_PUSH;
3683 goto action_of_vlan;
3684 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
3685 conf.of_set_vlan_vid = actions->conf;
3687 goto override_na_vlan_id;
3688 vlan_act = TCA_VLAN_ACT_MODIFY;
3689 goto action_of_vlan;
3690 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
3691 conf.of_set_vlan_pcp = actions->conf;
3692 if (na_vlan_priority)
3693 goto override_na_vlan_priority;
3694 vlan_act = TCA_VLAN_ACT_MODIFY;
3695 goto action_of_vlan;
3698 mnl_attr_nest_start(nlh, na_act_index_cur++);
3699 assert(na_act_index);
3700 mnl_attr_put_strz(nlh, TCA_ACT_KIND, "vlan");
3701 na_act = mnl_attr_nest_start(nlh, TCA_ACT_OPTIONS);
3703 mnl_attr_put(nlh, TCA_VLAN_PARMS,
3704 sizeof(struct tc_vlan),
3706 .action = TC_ACT_PIPE,
3707 .v_action = vlan_act,
3709 if (vlan_act == TCA_VLAN_ACT_POP) {
3710 mnl_attr_nest_end(nlh, na_act);
3711 mnl_attr_nest_end(nlh, na_act_index);
3714 if (vlan_act == TCA_VLAN_ACT_PUSH)
3715 mnl_attr_put_u16(nlh,
3716 TCA_VLAN_PUSH_VLAN_PROTOCOL,
3717 conf.of_push_vlan->ethertype);
3718 na_vlan_id = mnl_nlmsg_get_payload_tail(nlh);
3719 mnl_attr_put_u16(nlh, TCA_VLAN_PAD, 0);
3720 na_vlan_priority = mnl_nlmsg_get_payload_tail(nlh);
3721 mnl_attr_put_u8(nlh, TCA_VLAN_PAD, 0);
3722 mnl_attr_nest_end(nlh, na_act);
3723 mnl_attr_nest_end(nlh, na_act_index);
3724 if (actions->type ==
3725 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID) {
3726 override_na_vlan_id:
3727 na_vlan_id->nla_type = TCA_VLAN_PUSH_VLAN_ID;
3728 *(uint16_t *)mnl_attr_get_payload(na_vlan_id) =
3730 (conf.of_set_vlan_vid->vlan_vid);
3731 } else if (actions->type ==
3732 RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP) {
3733 override_na_vlan_priority:
3734 na_vlan_priority->nla_type =
3735 TCA_VLAN_PUSH_VLAN_PRIORITY;
3736 *(uint8_t *)mnl_attr_get_payload
3737 (na_vlan_priority) =
3738 conf.of_set_vlan_pcp->vlan_pcp;
3741 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
3742 assert(decap.vxlan);
3743 assert(dev_flow->tcf.tunnel);
3744 dev_flow->tcf.tunnel->ifindex_ptr =
3745 (unsigned int *)&tcm->tcm_ifindex;
3747 mnl_attr_nest_start(nlh, na_act_index_cur++);
3748 assert(na_act_index);
3749 mnl_attr_put_strz(nlh, TCA_ACT_KIND, "tunnel_key");
3750 na_act = mnl_attr_nest_start(nlh, TCA_ACT_OPTIONS);
3752 mnl_attr_put(nlh, TCA_TUNNEL_KEY_PARMS,
3753 sizeof(struct tc_tunnel_key),
3754 &(struct tc_tunnel_key){
3755 .action = TC_ACT_PIPE,
3756 .t_action = TCA_TUNNEL_KEY_ACT_RELEASE,
3758 mnl_attr_nest_end(nlh, na_act);
3759 mnl_attr_nest_end(nlh, na_act_index);
3760 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3762 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
3763 assert(encap.vxlan);
3764 flow_tcf_vxlan_encap_parse(actions, encap.vxlan);
3766 mnl_attr_nest_start(nlh, na_act_index_cur++);
3767 assert(na_act_index);
3768 mnl_attr_put_strz(nlh, TCA_ACT_KIND, "tunnel_key");
3769 na_act = mnl_attr_nest_start(nlh, TCA_ACT_OPTIONS);
3771 mnl_attr_put(nlh, TCA_TUNNEL_KEY_PARMS,
3772 sizeof(struct tc_tunnel_key),
3773 &(struct tc_tunnel_key){
3774 .action = TC_ACT_PIPE,
3775 .t_action = TCA_TUNNEL_KEY_ACT_SET,
3777 if (encap.vxlan->mask & FLOW_TCF_ENCAP_UDP_DST)
3778 mnl_attr_put_u16(nlh,
3779 TCA_TUNNEL_KEY_ENC_DST_PORT,
3780 encap.vxlan->udp.dst);
3781 if (encap.vxlan->mask & FLOW_TCF_ENCAP_IPV4_SRC)
3782 mnl_attr_put_u32(nlh,
3783 TCA_TUNNEL_KEY_ENC_IPV4_SRC,
3784 encap.vxlan->ipv4.src);
3785 if (encap.vxlan->mask & FLOW_TCF_ENCAP_IPV4_DST)
3786 mnl_attr_put_u32(nlh,
3787 TCA_TUNNEL_KEY_ENC_IPV4_DST,
3788 encap.vxlan->ipv4.dst);
3789 if (encap.vxlan->mask & FLOW_TCF_ENCAP_IPV6_SRC)
3791 TCA_TUNNEL_KEY_ENC_IPV6_SRC,
3792 sizeof(encap.vxlan->ipv6.src),
3793 &encap.vxlan->ipv6.src);
3794 if (encap.vxlan->mask & FLOW_TCF_ENCAP_IPV6_DST)
3796 TCA_TUNNEL_KEY_ENC_IPV6_DST,
3797 sizeof(encap.vxlan->ipv6.dst),
3798 &encap.vxlan->ipv6.dst);
3799 if (encap.vxlan->mask & FLOW_TCF_ENCAP_VXLAN_VNI)
3800 mnl_attr_put_u32(nlh,
3801 TCA_TUNNEL_KEY_ENC_KEY_ID,
3803 (encap.vxlan->vxlan.vni));
3804 mnl_attr_put_u8(nlh, TCA_TUNNEL_KEY_NO_CSUM, 0);
3805 mnl_attr_nest_end(nlh, na_act);
3806 mnl_attr_nest_end(nlh, na_act_index);
3807 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3809 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
3810 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
3811 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
3812 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
3813 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
3814 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
3815 case RTE_FLOW_ACTION_TYPE_SET_TTL:
3816 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
3817 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
3818 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
3820 mnl_attr_nest_start(nlh, na_act_index_cur++);
3821 flow_tcf_create_pedit_mnl_msg(nlh,
3822 &actions, item_flags);
3823 mnl_attr_nest_end(nlh, na_act_index);
3826 return rte_flow_error_set(error, ENOTSUP,
3827 RTE_FLOW_ERROR_TYPE_ACTION,
3829 "action not supported");
3833 assert(na_flower_act);
3834 mnl_attr_nest_end(nlh, na_flower_act);
3835 dev_flow->tcf.ptc_flags = mnl_attr_get_payload
3836 (mnl_nlmsg_get_payload_tail(nlh));
3837 mnl_attr_put_u32(nlh, TCA_FLOWER_FLAGS, decap.vxlan ?
3838 0 : TCA_CLS_FLAGS_SKIP_SW);
3839 mnl_attr_nest_end(nlh, na_flower);
3840 if (dev_flow->tcf.tunnel && dev_flow->tcf.tunnel->ifindex_ptr)
3841 dev_flow->tcf.tunnel->ifindex_org =
3842 *dev_flow->tcf.tunnel->ifindex_ptr;
3843 assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3848 * Send Netlink message with acknowledgment.
3851 * Flow context to use.
3853 * Message to send. This function always raises the NLM_F_ACK flag before
3856 * Callback handler for received message.
3858 * Context pointer for callback handler.
3861 * 0 on success, a negative errno value otherwise and rte_errno is set.
3864 flow_tcf_nl_ack(struct mlx5_flow_tcf_context *tcf,
3865 struct nlmsghdr *nlh,
3866 mnl_cb_t cb, void *arg)
3868 unsigned int portid = mnl_socket_get_portid(tcf->nl);
3869 uint32_t seq = tcf->seq++;
3875 /* seq 0 is reserved for kernel event-driven notifications. */
3878 nlh->nlmsg_seq = seq;
3879 nlh->nlmsg_flags |= NLM_F_ACK;
3880 ret = mnl_socket_sendto(tcf->nl, nlh, nlh->nlmsg_len);
3882 /* Message send error occurres. */
3886 nlh = (struct nlmsghdr *)(tcf->buf);
3888 * The following loop postpones non-fatal errors until multipart
3889 * messages are complete.
3892 ret = mnl_socket_recvfrom(tcf->nl, tcf->buf, tcf->buf_size);
3896 * In case of overflow Will receive till
3897 * end of multipart message. We may lost part
3898 * of reply messages but mark and return an error.
3900 if (err != ENOSPC ||
3901 !(nlh->nlmsg_flags & NLM_F_MULTI) ||
3902 nlh->nlmsg_type == NLMSG_DONE)
3905 ret = mnl_cb_run(nlh, ret, seq, portid, cb, arg);
3908 * libmnl returns 0 if DONE or
3909 * success ACK message found.
3915 * ACK message with error found
3916 * or some error occurred.
3921 /* We should continue receiving. */
3930 #define MNL_BUF_EXTRA_SPACE 16
3931 #define MNL_REQUEST_SIZE_MIN 256
3932 #define MNL_REQUEST_SIZE_MAX 2048
3933 #define MNL_REQUEST_SIZE RTE_MIN(RTE_MAX(sysconf(_SC_PAGESIZE), \
3934 MNL_REQUEST_SIZE_MIN), MNL_REQUEST_SIZE_MAX)
3936 /* Data structures used by flow_tcf_xxx_cb() routines. */
3937 struct tcf_nlcb_buf {
3938 LIST_ENTRY(tcf_nlcb_buf) next;
3940 alignas(struct nlmsghdr)
3941 uint8_t msg[]; /**< Netlink message data. */
3944 struct tcf_nlcb_context {
3945 unsigned int ifindex; /**< Base interface index. */
3947 LIST_HEAD(, tcf_nlcb_buf) nlbuf;
3951 * Allocate space for netlink command in buffer list
3953 * @param[in, out] ctx
3954 * Pointer to callback context with command buffers list.
3956 * Required size of data buffer to be allocated.
3959 * Pointer to allocated memory, aligned as message header.
3960 * NULL if some error occurred.
3962 static struct nlmsghdr *
3963 flow_tcf_alloc_nlcmd(struct tcf_nlcb_context *ctx, uint32_t size)
3965 struct tcf_nlcb_buf *buf;
3966 struct nlmsghdr *nlh;
3968 size = NLMSG_ALIGN(size);
3969 buf = LIST_FIRST(&ctx->nlbuf);
3970 if (buf && (buf->size + size) <= ctx->bufsize) {
3971 nlh = (struct nlmsghdr *)&buf->msg[buf->size];
3975 if (size > ctx->bufsize) {
3976 DRV_LOG(WARNING, "netlink: too long command buffer requested");
3979 buf = rte_malloc(__func__,
3980 ctx->bufsize + sizeof(struct tcf_nlcb_buf),
3981 alignof(struct tcf_nlcb_buf));
3983 DRV_LOG(WARNING, "netlink: no memory for command buffer");
3986 LIST_INSERT_HEAD(&ctx->nlbuf, buf, next);
3988 nlh = (struct nlmsghdr *)&buf->msg[0];
3993 * Send the buffers with prepared netlink commands. Scans the list and
3994 * sends all found buffers. Buffers are sent and freed anyway in order
3995 * to prevent memory leakage if some every message in received packet.
3998 * Context object initialized by mlx5_flow_tcf_context_create().
3999 * @param[in, out] ctx
4000 * Pointer to callback context with command buffers list.
4003 * Zero value on success, negative errno value otherwise
4004 * and rte_errno is set.
4007 flow_tcf_send_nlcmd(struct mlx5_flow_tcf_context *tcf,
4008 struct tcf_nlcb_context *ctx)
4010 struct tcf_nlcb_buf *bc = LIST_FIRST(&ctx->nlbuf);
4014 struct tcf_nlcb_buf *bn = LIST_NEXT(bc, next);
4015 struct nlmsghdr *nlh;
4019 while (msg < bc->size) {
4021 * Send Netlink commands from buffer in one by one
4022 * fashion. If we send multiple rule deletion commands
4023 * in one Netlink message and some error occurs it may
4024 * cause multiple ACK error messages and break sequence
4025 * numbers of Netlink communication, because we expect
4026 * the only one ACK reply.
4028 assert((bc->size - msg) >= sizeof(struct nlmsghdr));
4029 nlh = (struct nlmsghdr *)&bc->msg[msg];
4030 assert((bc->size - msg) >= nlh->nlmsg_len);
4031 msg += nlh->nlmsg_len;
4032 rc = flow_tcf_nl_ack(tcf, nlh, NULL, NULL);
4035 "netlink: cleanup error %d", rc);
4043 LIST_INIT(&ctx->nlbuf);
4048 * Collect local IP address rules with scope link attribute on specified
4049 * network device. This is callback routine called by libmnl mnl_cb_run()
4050 * in loop for every message in received packet.
4053 * Pointer to reply header.
4054 * @param[in, out] arg
4055 * Opaque data pointer for this callback.
4058 * A positive, nonzero value on success, negative errno value otherwise
4059 * and rte_errno is set.
4062 flow_tcf_collect_local_cb(const struct nlmsghdr *nlh, void *arg)
4064 struct tcf_nlcb_context *ctx = arg;
4065 struct nlmsghdr *cmd;
4066 struct ifaddrmsg *ifa;
4068 struct nlattr *na_local = NULL;
4069 struct nlattr *na_peer = NULL;
4070 unsigned char family;
4073 if (nlh->nlmsg_type != RTM_NEWADDR) {
4077 ifa = mnl_nlmsg_get_payload(nlh);
4078 family = ifa->ifa_family;
4079 if (ifa->ifa_index != ctx->ifindex ||
4080 ifa->ifa_scope != RT_SCOPE_LINK ||
4081 !(ifa->ifa_flags & IFA_F_PERMANENT) ||
4082 (family != AF_INET && family != AF_INET6))
4084 mnl_attr_for_each(na, nlh, sizeof(*ifa)) {
4085 switch (mnl_attr_get_type(na)) {
4093 if (na_local && na_peer)
4096 if (!na_local || !na_peer)
4098 /* Local rule found with scope link, permanent and assigned peer. */
4099 size = MNL_ALIGN(sizeof(struct nlmsghdr)) +
4100 MNL_ALIGN(sizeof(struct ifaddrmsg)) +
4101 (family == AF_INET6 ? 2 * SZ_NLATTR_DATA_OF(IPV6_ADDR_LEN)
4102 : 2 * SZ_NLATTR_TYPE_OF(uint32_t));
4103 cmd = flow_tcf_alloc_nlcmd(ctx, size);
4108 cmd = mnl_nlmsg_put_header(cmd);
4109 cmd->nlmsg_type = RTM_DELADDR;
4110 cmd->nlmsg_flags = NLM_F_REQUEST;
4111 ifa = mnl_nlmsg_put_extra_header(cmd, sizeof(*ifa));
4112 ifa->ifa_flags = IFA_F_PERMANENT;
4113 ifa->ifa_scope = RT_SCOPE_LINK;
4114 ifa->ifa_index = ctx->ifindex;
4115 if (family == AF_INET) {
4116 ifa->ifa_family = AF_INET;
4117 ifa->ifa_prefixlen = 32;
4118 mnl_attr_put_u32(cmd, IFA_LOCAL, mnl_attr_get_u32(na_local));
4119 mnl_attr_put_u32(cmd, IFA_ADDRESS, mnl_attr_get_u32(na_peer));
4121 ifa->ifa_family = AF_INET6;
4122 ifa->ifa_prefixlen = 128;
4123 mnl_attr_put(cmd, IFA_LOCAL, IPV6_ADDR_LEN,
4124 mnl_attr_get_payload(na_local));
4125 mnl_attr_put(cmd, IFA_ADDRESS, IPV6_ADDR_LEN,
4126 mnl_attr_get_payload(na_peer));
4128 assert(size == cmd->nlmsg_len);
4133 * Cleanup the local IP addresses on outer interface.
4136 * Context object initialized by mlx5_flow_tcf_context_create().
4137 * @param[in] ifindex
4138 * Network inferface index to perform cleanup.
4141 flow_tcf_encap_local_cleanup(struct mlx5_flow_tcf_context *tcf,
4142 unsigned int ifindex)
4144 struct nlmsghdr *nlh;
4145 struct ifaddrmsg *ifa;
4146 struct tcf_nlcb_context ctx = {
4148 .bufsize = MNL_REQUEST_SIZE,
4149 .nlbuf = LIST_HEAD_INITIALIZER(),
4155 * Seek and destroy leftovers of local IP addresses with
4156 * matching properties "scope link".
4158 nlh = mnl_nlmsg_put_header(tcf->buf);
4159 nlh->nlmsg_type = RTM_GETADDR;
4160 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
4161 ifa = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifa));
4162 ifa->ifa_family = AF_UNSPEC;
4163 ifa->ifa_index = ifindex;
4164 ifa->ifa_scope = RT_SCOPE_LINK;
4165 ret = flow_tcf_nl_ack(tcf, nlh, flow_tcf_collect_local_cb, &ctx);
4167 DRV_LOG(WARNING, "netlink: query device list error %d", ret);
4168 ret = flow_tcf_send_nlcmd(tcf, &ctx);
4170 DRV_LOG(WARNING, "netlink: device delete error %d", ret);
4174 * Collect neigh permament rules on specified network device.
4175 * This is callback routine called by libmnl mnl_cb_run() in loop for
4176 * every message in received packet.
4179 * Pointer to reply header.
4180 * @param[in, out] arg
4181 * Opaque data pointer for this callback.
4184 * A positive, nonzero value on success, negative errno value otherwise
4185 * and rte_errno is set.
4188 flow_tcf_collect_neigh_cb(const struct nlmsghdr *nlh, void *arg)
4190 struct tcf_nlcb_context *ctx = arg;
4191 struct nlmsghdr *cmd;
4194 struct nlattr *na_ip = NULL;
4195 struct nlattr *na_mac = NULL;
4196 unsigned char family;
4199 if (nlh->nlmsg_type != RTM_NEWNEIGH) {
4203 ndm = mnl_nlmsg_get_payload(nlh);
4204 family = ndm->ndm_family;
4205 if (ndm->ndm_ifindex != (int)ctx->ifindex ||
4206 !(ndm->ndm_state & NUD_PERMANENT) ||
4207 (family != AF_INET && family != AF_INET6))
4209 mnl_attr_for_each(na, nlh, sizeof(*ndm)) {
4210 switch (mnl_attr_get_type(na)) {
4218 if (na_mac && na_ip)
4221 if (!na_mac || !na_ip)
4223 /* Neigh rule with permenent attribute found. */
4224 size = MNL_ALIGN(sizeof(struct nlmsghdr)) +
4225 MNL_ALIGN(sizeof(struct ndmsg)) +
4226 SZ_NLATTR_DATA_OF(ETHER_ADDR_LEN) +
4227 (family == AF_INET6 ? SZ_NLATTR_DATA_OF(IPV6_ADDR_LEN)
4228 : SZ_NLATTR_TYPE_OF(uint32_t));
4229 cmd = flow_tcf_alloc_nlcmd(ctx, size);
4234 cmd = mnl_nlmsg_put_header(cmd);
4235 cmd->nlmsg_type = RTM_DELNEIGH;
4236 cmd->nlmsg_flags = NLM_F_REQUEST;
4237 ndm = mnl_nlmsg_put_extra_header(cmd, sizeof(*ndm));
4238 ndm->ndm_ifindex = ctx->ifindex;
4239 ndm->ndm_state = NUD_PERMANENT;
4242 if (family == AF_INET) {
4243 ndm->ndm_family = AF_INET;
4244 mnl_attr_put_u32(cmd, NDA_DST, mnl_attr_get_u32(na_ip));
4246 ndm->ndm_family = AF_INET6;
4247 mnl_attr_put(cmd, NDA_DST, IPV6_ADDR_LEN,
4248 mnl_attr_get_payload(na_ip));
4250 mnl_attr_put(cmd, NDA_LLADDR, ETHER_ADDR_LEN,
4251 mnl_attr_get_payload(na_mac));
4252 assert(size == cmd->nlmsg_len);
4257 * Cleanup the neigh rules on outer interface.
4260 * Context object initialized by mlx5_flow_tcf_context_create().
4261 * @param[in] ifindex
4262 * Network inferface index to perform cleanup.
4265 flow_tcf_encap_neigh_cleanup(struct mlx5_flow_tcf_context *tcf,
4266 unsigned int ifindex)
4268 struct nlmsghdr *nlh;
4270 struct tcf_nlcb_context ctx = {
4272 .bufsize = MNL_REQUEST_SIZE,
4273 .nlbuf = LIST_HEAD_INITIALIZER(),
4278 /* Seek and destroy leftovers of neigh rules. */
4279 nlh = mnl_nlmsg_put_header(tcf->buf);
4280 nlh->nlmsg_type = RTM_GETNEIGH;
4281 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
4282 ndm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ndm));
4283 ndm->ndm_family = AF_UNSPEC;
4284 ndm->ndm_ifindex = ifindex;
4285 ndm->ndm_state = NUD_PERMANENT;
4286 ret = flow_tcf_nl_ack(tcf, nlh, flow_tcf_collect_neigh_cb, &ctx);
4288 DRV_LOG(WARNING, "netlink: query device list error %d", ret);
4289 ret = flow_tcf_send_nlcmd(tcf, &ctx);
4291 DRV_LOG(WARNING, "netlink: device delete error %d", ret);
4295 * Collect indices of VXLAN encap/decap interfaces associated with device.
4296 * This is callback routine called by libmnl mnl_cb_run() in loop for
4297 * every message in received packet.
4300 * Pointer to reply header.
4301 * @param[in, out] arg
4302 * Opaque data pointer for this callback.
4305 * A positive, nonzero value on success, negative errno value otherwise
4306 * and rte_errno is set.
4309 flow_tcf_collect_vxlan_cb(const struct nlmsghdr *nlh, void *arg)
4311 struct tcf_nlcb_context *ctx = arg;
4312 struct nlmsghdr *cmd;
4313 struct ifinfomsg *ifm;
4315 struct nlattr *na_info = NULL;
4316 struct nlattr *na_vxlan = NULL;
4318 unsigned int vxindex;
4321 if (nlh->nlmsg_type != RTM_NEWLINK) {
4325 ifm = mnl_nlmsg_get_payload(nlh);
4326 if (!ifm->ifi_index) {
4330 mnl_attr_for_each(na, nlh, sizeof(*ifm))
4331 if (mnl_attr_get_type(na) == IFLA_LINKINFO) {
4337 mnl_attr_for_each_nested(na, na_info) {
4338 switch (mnl_attr_get_type(na)) {
4339 case IFLA_INFO_KIND:
4340 if (!strncmp("vxlan", mnl_attr_get_str(na),
4341 mnl_attr_get_len(na)))
4344 case IFLA_INFO_DATA:
4348 if (found && na_vxlan)
4351 if (!found || !na_vxlan)
4354 mnl_attr_for_each_nested(na, na_vxlan) {
4355 if (mnl_attr_get_type(na) == IFLA_VXLAN_LINK &&
4356 mnl_attr_get_u32(na) == ctx->ifindex) {
4363 /* Attached VXLAN device found, store the command to delete. */
4364 vxindex = ifm->ifi_index;
4365 size = MNL_ALIGN(sizeof(struct nlmsghdr)) +
4366 MNL_ALIGN(sizeof(struct ifinfomsg));
4367 cmd = flow_tcf_alloc_nlcmd(ctx, size);
4372 cmd = mnl_nlmsg_put_header(cmd);
4373 cmd->nlmsg_type = RTM_DELLINK;
4374 cmd->nlmsg_flags = NLM_F_REQUEST;
4375 ifm = mnl_nlmsg_put_extra_header(cmd, sizeof(*ifm));
4376 ifm->ifi_family = AF_UNSPEC;
4377 ifm->ifi_index = vxindex;
4378 assert(size == cmd->nlmsg_len);
4383 * Cleanup the outer interface. Removes all found vxlan devices
4384 * attached to specified index, flushes the neigh and local IP
4388 * Context object initialized by mlx5_flow_tcf_context_create().
4389 * @param[in] ifindex
4390 * Network inferface index to perform cleanup.
4393 flow_tcf_encap_iface_cleanup(struct mlx5_flow_tcf_context *tcf,
4394 unsigned int ifindex)
4396 struct nlmsghdr *nlh;
4397 struct ifinfomsg *ifm;
4398 struct tcf_nlcb_context ctx = {
4400 .bufsize = MNL_REQUEST_SIZE,
4401 .nlbuf = LIST_HEAD_INITIALIZER(),
4407 * Seek and destroy leftover VXLAN encap/decap interfaces with
4408 * matching properties.
4410 nlh = mnl_nlmsg_put_header(tcf->buf);
4411 nlh->nlmsg_type = RTM_GETLINK;
4412 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
4413 ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
4414 ifm->ifi_family = AF_UNSPEC;
4415 ret = flow_tcf_nl_ack(tcf, nlh, flow_tcf_collect_vxlan_cb, &ctx);
4417 DRV_LOG(WARNING, "netlink: query device list error %d", ret);
4418 ret = flow_tcf_send_nlcmd(tcf, &ctx);
4420 DRV_LOG(WARNING, "netlink: device delete error %d", ret);
4424 * Emit Netlink message to add/remove local address to the outer device.
4425 * The address being added is visible within the link only (scope link).
4427 * Note that an implicit route is maintained by the kernel due to the
4428 * presence of a peer address (IFA_ADDRESS).
4430 * These rules are used for encapsultion only and allow to assign
4431 * the outer tunnel source IP address.
4434 * Libmnl socket context object.
4436 * Encapsulation properties (source address and its peer).
4437 * @param[in] ifindex
4438 * Network interface to apply rule.
4440 * Toggle between add and remove.
4442 * Perform verbose error reporting if not NULL.
4445 * 0 on success, a negative errno value otherwise and rte_errno is set.
4448 flow_tcf_rule_local(struct mlx5_flow_tcf_context *tcf,
4449 const struct flow_tcf_vxlan_encap *encap,
4450 unsigned int ifindex,
4452 struct rte_flow_error *error)
4454 struct nlmsghdr *nlh;
4455 struct ifaddrmsg *ifa;
4456 alignas(struct nlmsghdr)
4457 uint8_t buf[mnl_nlmsg_size(sizeof(*ifa) + 128)];
4459 nlh = mnl_nlmsg_put_header(buf);
4460 nlh->nlmsg_type = enable ? RTM_NEWADDR : RTM_DELADDR;
4462 NLM_F_REQUEST | (enable ? NLM_F_CREATE | NLM_F_REPLACE : 0);
4464 ifa = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifa));
4465 ifa->ifa_flags = IFA_F_PERMANENT;
4466 ifa->ifa_scope = RT_SCOPE_LINK;
4467 ifa->ifa_index = ifindex;
4468 if (encap->mask & FLOW_TCF_ENCAP_IPV4_SRC) {
4469 ifa->ifa_family = AF_INET;
4470 ifa->ifa_prefixlen = 32;
4471 mnl_attr_put_u32(nlh, IFA_LOCAL, encap->ipv4.src);
4472 if (encap->mask & FLOW_TCF_ENCAP_IPV4_DST)
4473 mnl_attr_put_u32(nlh, IFA_ADDRESS,
4476 assert(encap->mask & FLOW_TCF_ENCAP_IPV6_SRC);
4477 ifa->ifa_family = AF_INET6;
4478 ifa->ifa_prefixlen = 128;
4479 mnl_attr_put(nlh, IFA_LOCAL,
4480 sizeof(encap->ipv6.src),
4482 if (encap->mask & FLOW_TCF_ENCAP_IPV6_DST)
4483 mnl_attr_put(nlh, IFA_ADDRESS,
4484 sizeof(encap->ipv6.dst),
4487 if (!flow_tcf_nl_ack(tcf, nlh, NULL, NULL))
4489 return rte_flow_error_set(error, rte_errno,
4490 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4491 "netlink: cannot complete IFA request"
4496 * Emit Netlink message to add/remove neighbor.
4499 * Libmnl socket context object.
4501 * Encapsulation properties (destination address).
4502 * @param[in] ifindex
4503 * Network interface.
4505 * Toggle between add and remove.
4507 * Perform verbose error reporting if not NULL.
4510 * 0 on success, a negative errno value otherwise and rte_errno is set.
4513 flow_tcf_rule_neigh(struct mlx5_flow_tcf_context *tcf,
4514 const struct flow_tcf_vxlan_encap *encap,
4515 unsigned int ifindex,
4517 struct rte_flow_error *error)
4519 struct nlmsghdr *nlh;
4521 alignas(struct nlmsghdr)
4522 uint8_t buf[mnl_nlmsg_size(sizeof(*ndm) + 128)];
4524 nlh = mnl_nlmsg_put_header(buf);
4525 nlh->nlmsg_type = enable ? RTM_NEWNEIGH : RTM_DELNEIGH;
4527 NLM_F_REQUEST | (enable ? NLM_F_CREATE | NLM_F_REPLACE : 0);
4529 ndm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ndm));
4530 ndm->ndm_ifindex = ifindex;
4531 ndm->ndm_state = NUD_PERMANENT;
4534 if (encap->mask & FLOW_TCF_ENCAP_IPV4_DST) {
4535 ndm->ndm_family = AF_INET;
4536 mnl_attr_put_u32(nlh, NDA_DST, encap->ipv4.dst);
4538 assert(encap->mask & FLOW_TCF_ENCAP_IPV6_DST);
4539 ndm->ndm_family = AF_INET6;
4540 mnl_attr_put(nlh, NDA_DST, sizeof(encap->ipv6.dst),
4543 if (encap->mask & FLOW_TCF_ENCAP_ETH_SRC && enable)
4545 "outer ethernet source address cannot be "
4546 "forced for VXLAN encapsulation");
4547 if (encap->mask & FLOW_TCF_ENCAP_ETH_DST)
4548 mnl_attr_put(nlh, NDA_LLADDR, sizeof(encap->eth.dst),
4550 if (!flow_tcf_nl_ack(tcf, nlh, NULL, NULL))
4552 return rte_flow_error_set(error, rte_errno,
4553 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4554 "netlink: cannot complete ND request"
4559 * Manage the local IP addresses and their peers IP addresses on the
4560 * outer interface for encapsulation purposes. The kernel searches the
4561 * appropriate device for tunnel egress traffic using the outer source
4562 * IP, this IP should be assigned to the outer network device, otherwise
4563 * kernel rejects the rule.
4565 * Adds or removes the addresses using the Netlink command like this:
4566 * ip addr add <src_ip> peer <dst_ip> scope link dev <ifouter>
4568 * The addresses are local to the netdev ("scope link"), this reduces
4569 * the risk of conflicts. Note that an implicit route is maintained by
4570 * the kernel due to the presence of a peer address (IFA_ADDRESS).
4573 * Libmnl socket context object.
4575 * Object, contains rule database and ifouter index.
4576 * @param[in] dev_flow
4577 * Flow object, contains the tunnel parameters (for encap only).
4579 * Toggle between add and remove.
4581 * Perform verbose error reporting if not NULL.
4584 * 0 on success, a negative errno value otherwise and rte_errno is set.
4587 flow_tcf_encap_local(struct mlx5_flow_tcf_context *tcf,
4588 struct tcf_irule *iface,
4589 struct mlx5_flow *dev_flow,
4591 struct rte_flow_error *error)
4593 const struct flow_tcf_vxlan_encap *encap = dev_flow->tcf.vxlan_encap;
4594 struct tcf_local_rule *rule = NULL;
4598 assert(encap->hdr.type == FLOW_TCF_TUNACT_VXLAN_ENCAP);
4599 if (encap->mask & FLOW_TCF_ENCAP_IPV4_SRC) {
4600 assert(encap->mask & FLOW_TCF_ENCAP_IPV4_DST);
4601 LIST_FOREACH(rule, &iface->local, next) {
4602 if (rule->mask & FLOW_TCF_ENCAP_IPV4_SRC &&
4603 encap->ipv4.src == rule->ipv4.src &&
4604 encap->ipv4.dst == rule->ipv4.dst) {
4609 assert(encap->mask & FLOW_TCF_ENCAP_IPV6_SRC);
4610 assert(encap->mask & FLOW_TCF_ENCAP_IPV6_DST);
4611 LIST_FOREACH(rule, &iface->local, next) {
4612 if (rule->mask & FLOW_TCF_ENCAP_IPV6_SRC &&
4613 !memcmp(&encap->ipv6.src, &rule->ipv6.src,
4614 sizeof(encap->ipv6.src)) &&
4615 !memcmp(&encap->ipv6.dst, &rule->ipv6.dst,
4616 sizeof(encap->ipv6.dst))) {
4626 if (!rule->refcnt || !--rule->refcnt) {
4627 LIST_REMOVE(rule, next);
4628 return flow_tcf_rule_local(tcf, encap,
4629 iface->ifouter, false, error);
4634 DRV_LOG(WARNING, "disabling not existing local rule");
4635 rte_flow_error_set(error, ENOENT,
4636 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4637 "disabling not existing local rule");
4640 rule = rte_zmalloc(__func__, sizeof(struct tcf_local_rule),
4641 alignof(struct tcf_local_rule));
4643 rte_flow_error_set(error, ENOMEM,
4644 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4645 "unable to allocate memory for local rule");
4648 *rule = (struct tcf_local_rule){.refcnt = 0,
4651 if (encap->mask & FLOW_TCF_ENCAP_IPV4_SRC) {
4652 rule->mask = FLOW_TCF_ENCAP_IPV4_SRC
4653 | FLOW_TCF_ENCAP_IPV4_DST;
4654 rule->ipv4.src = encap->ipv4.src;
4655 rule->ipv4.dst = encap->ipv4.dst;
4657 rule->mask = FLOW_TCF_ENCAP_IPV6_SRC
4658 | FLOW_TCF_ENCAP_IPV6_DST;
4659 memcpy(&rule->ipv6.src, &encap->ipv6.src, IPV6_ADDR_LEN);
4660 memcpy(&rule->ipv6.dst, &encap->ipv6.dst, IPV6_ADDR_LEN);
4662 ret = flow_tcf_rule_local(tcf, encap, iface->ifouter, true, error);
4668 LIST_INSERT_HEAD(&iface->local, rule, next);
4673 * Manage the destination MAC/IP addresses neigh database, kernel uses
4674 * this one to determine the destination MAC address within encapsulation
4675 * header. Adds or removes the entries using the Netlink command like this:
4676 * ip neigh add dev <ifouter> lladdr <dst_mac> to <dst_ip> nud permanent
4679 * Libmnl socket context object.
4681 * Object, contains rule database and ifouter index.
4682 * @param[in] dev_flow
4683 * Flow object, contains the tunnel parameters (for encap only).
4685 * Toggle between add and remove.
4687 * Perform verbose error reporting if not NULL.
4690 * 0 on success, a negative errno value otherwise and rte_errno is set.
4693 flow_tcf_encap_neigh(struct mlx5_flow_tcf_context *tcf,
4694 struct tcf_irule *iface,
4695 struct mlx5_flow *dev_flow,
4697 struct rte_flow_error *error)
4699 const struct flow_tcf_vxlan_encap *encap = dev_flow->tcf.vxlan_encap;
4700 struct tcf_neigh_rule *rule = NULL;
4704 assert(encap->hdr.type == FLOW_TCF_TUNACT_VXLAN_ENCAP);
4705 if (encap->mask & FLOW_TCF_ENCAP_IPV4_DST) {
4706 assert(encap->mask & FLOW_TCF_ENCAP_IPV4_SRC);
4707 LIST_FOREACH(rule, &iface->neigh, next) {
4708 if (rule->mask & FLOW_TCF_ENCAP_IPV4_DST &&
4709 encap->ipv4.dst == rule->ipv4.dst) {
4714 assert(encap->mask & FLOW_TCF_ENCAP_IPV6_SRC);
4715 assert(encap->mask & FLOW_TCF_ENCAP_IPV6_DST);
4716 LIST_FOREACH(rule, &iface->neigh, next) {
4717 if (rule->mask & FLOW_TCF_ENCAP_IPV6_DST &&
4718 !memcmp(&encap->ipv6.dst, &rule->ipv6.dst,
4719 sizeof(encap->ipv6.dst))) {
4725 if (memcmp(&encap->eth.dst, &rule->eth,
4726 sizeof(encap->eth.dst))) {
4727 DRV_LOG(WARNING, "Destination MAC differs"
4729 rte_flow_error_set(error, EEXIST,
4730 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4731 NULL, "Different MAC address"
4732 " neigh rule for the same"
4740 if (!rule->refcnt || !--rule->refcnt) {
4741 LIST_REMOVE(rule, next);
4742 return flow_tcf_rule_neigh(tcf, encap,
4749 DRV_LOG(WARNING, "Disabling not existing neigh rule");
4750 rte_flow_error_set(error, ENOENT,
4751 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4752 "unable to allocate memory for neigh rule");
4755 rule = rte_zmalloc(__func__, sizeof(struct tcf_neigh_rule),
4756 alignof(struct tcf_neigh_rule));
4758 rte_flow_error_set(error, ENOMEM,
4759 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4760 "unable to allocate memory for neigh rule");
4763 *rule = (struct tcf_neigh_rule){.refcnt = 0,
4766 if (encap->mask & FLOW_TCF_ENCAP_IPV4_DST) {
4767 rule->mask = FLOW_TCF_ENCAP_IPV4_DST;
4768 rule->ipv4.dst = encap->ipv4.dst;
4770 rule->mask = FLOW_TCF_ENCAP_IPV6_DST;
4771 memcpy(&rule->ipv6.dst, &encap->ipv6.dst, IPV6_ADDR_LEN);
4773 memcpy(&rule->eth, &encap->eth.dst, sizeof(rule->eth));
4774 ret = flow_tcf_rule_neigh(tcf, encap, iface->ifouter, true, error);
4780 LIST_INSERT_HEAD(&iface->neigh, rule, next);
4784 /* VXLAN encap rule database for outer interfaces. */
4785 static LIST_HEAD(, tcf_irule) iface_list_vxlan = LIST_HEAD_INITIALIZER();
4787 /* VTEP device list is shared between PMD port instances. */
4788 static LIST_HEAD(, tcf_vtep) vtep_list_vxlan = LIST_HEAD_INITIALIZER();
4789 static pthread_mutex_t vtep_list_mutex = PTHREAD_MUTEX_INITIALIZER;
4792 * Acquire the VXLAN encap rules container for specified interface.
4793 * First looks for the container in the existing ones list, creates
4794 * and initializes the new container if existing not found.
4797 * Context object initialized by mlx5_flow_tcf_context_create().
4798 * @param[in] ifouter
4799 * Network interface index to create VXLAN encap rules on.
4801 * Perform verbose error reporting if not NULL.
4803 * Rule container pointer on success,
4804 * NULL otherwise and rte_errno is set.
4806 static struct tcf_irule*
4807 flow_tcf_encap_irule_acquire(struct mlx5_flow_tcf_context *tcf,
4808 unsigned int ifouter,
4809 struct rte_flow_error *error)
4811 struct tcf_irule *iface;
4813 /* Look whether the container for encap rules is created. */
4815 LIST_FOREACH(iface, &iface_list_vxlan, next) {
4816 if (iface->ifouter == ifouter)
4820 /* Container already exists, just increment the reference. */
4824 /* Not found, we should create the new container. */
4825 iface = rte_zmalloc(__func__, sizeof(*iface),
4826 alignof(struct tcf_irule));
4828 rte_flow_error_set(error, ENOMEM,
4829 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4830 "unable to allocate memory for container");
4833 *iface = (struct tcf_irule){
4834 .local = LIST_HEAD_INITIALIZER(),
4835 .neigh = LIST_HEAD_INITIALIZER(),
4839 /* Interface cleanup for new container created. */
4840 flow_tcf_encap_iface_cleanup(tcf, ifouter);
4841 flow_tcf_encap_local_cleanup(tcf, ifouter);
4842 flow_tcf_encap_neigh_cleanup(tcf, ifouter);
4843 LIST_INSERT_HEAD(&iface_list_vxlan, iface, next);
4848 * Releases VXLAN encap rules container by pointer. Decrements the
4849 * reference cointer and deletes the container if counter is zero.
4852 * VXLAN rule container pointer to release.
4855 flow_tcf_encap_irule_release(struct tcf_irule *iface)
4857 assert(iface->refcnt);
4858 if (--iface->refcnt == 0) {
4859 /* Reference counter is zero, delete the container. */
4860 assert(LIST_EMPTY(&iface->local));
4861 assert(LIST_EMPTY(&iface->neigh));
4862 LIST_REMOVE(iface, next);
4868 * Deletes VTEP network device.
4871 * Context object initialized by mlx5_flow_tcf_context_create().
4873 * Object represinting the network device to delete. Memory
4874 * allocated for this object is freed by routine.
4877 flow_tcf_vtep_delete(struct mlx5_flow_tcf_context *tcf,
4878 struct tcf_vtep *vtep)
4880 struct nlmsghdr *nlh;
4881 struct ifinfomsg *ifm;
4882 alignas(struct nlmsghdr)
4883 uint8_t buf[mnl_nlmsg_size(MNL_ALIGN(sizeof(*ifm))) +
4884 MNL_BUF_EXTRA_SPACE];
4887 assert(!vtep->refcnt);
4888 /* Delete only ifaces those we actually created. */
4889 if (vtep->created && vtep->ifindex) {
4890 DRV_LOG(INFO, "VTEP delete (%d)", vtep->ifindex);
4891 nlh = mnl_nlmsg_put_header(buf);
4892 nlh->nlmsg_type = RTM_DELLINK;
4893 nlh->nlmsg_flags = NLM_F_REQUEST;
4894 ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
4895 ifm->ifi_family = AF_UNSPEC;
4896 ifm->ifi_index = vtep->ifindex;
4897 assert(sizeof(buf) >= nlh->nlmsg_len);
4898 ret = flow_tcf_nl_ack(tcf, nlh, NULL, NULL);
4900 DRV_LOG(WARNING, "netlink: error deleting vxlan"
4901 " encap/decap ifindex %u",
4908 * Creates VTEP network device.
4911 * Context object initialized by mlx5_flow_tcf_context_create().
4913 * UDP port of created VTEP device.
4915 * Perform verbose error reporting if not NULL.
4918 * Pointer to created device structure on success,
4919 * NULL otherwise and rte_errno is set.
4921 #ifdef HAVE_IFLA_VXLAN_COLLECT_METADATA
4922 static struct tcf_vtep*
4923 flow_tcf_vtep_create(struct mlx5_flow_tcf_context *tcf,
4924 uint16_t port, struct rte_flow_error *error)
4926 struct tcf_vtep *vtep;
4927 struct nlmsghdr *nlh;
4928 struct ifinfomsg *ifm;
4929 char name[sizeof(MLX5_VXLAN_DEVICE_PFX) + 24];
4930 alignas(struct nlmsghdr)
4931 uint8_t buf[mnl_nlmsg_size(sizeof(*ifm)) +
4932 SZ_NLATTR_DATA_OF(sizeof(name)) +
4933 SZ_NLATTR_NEST * 2 +
4934 SZ_NLATTR_STRZ_OF("vxlan") +
4935 SZ_NLATTR_DATA_OF(sizeof(uint32_t)) +
4936 SZ_NLATTR_DATA_OF(sizeof(uint16_t)) +
4937 SZ_NLATTR_DATA_OF(sizeof(uint8_t)) * 3 +
4938 MNL_BUF_EXTRA_SPACE];
4939 struct nlattr *na_info;
4940 struct nlattr *na_vxlan;
4941 rte_be16_t vxlan_port = rte_cpu_to_be_16(port);
4944 vtep = rte_zmalloc(__func__, sizeof(*vtep), alignof(struct tcf_vtep));
4946 rte_flow_error_set(error, ENOMEM,
4947 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4948 "unable to allocate memory for VTEP");
4951 *vtep = (struct tcf_vtep){
4954 memset(buf, 0, sizeof(buf));
4955 nlh = mnl_nlmsg_put_header(buf);
4956 nlh->nlmsg_type = RTM_NEWLINK;
4957 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
4958 ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
4959 ifm->ifi_family = AF_UNSPEC;
4962 ifm->ifi_flags = IFF_UP;
4963 ifm->ifi_change = 0xffffffff;
4964 snprintf(name, sizeof(name), "%s%u", MLX5_VXLAN_DEVICE_PFX, port);
4965 mnl_attr_put_strz(nlh, IFLA_IFNAME, name);
4966 na_info = mnl_attr_nest_start(nlh, IFLA_LINKINFO);
4968 mnl_attr_put_strz(nlh, IFLA_INFO_KIND, "vxlan");
4969 na_vxlan = mnl_attr_nest_start(nlh, IFLA_INFO_DATA);
4971 mnl_attr_put_u8(nlh, IFLA_VXLAN_COLLECT_METADATA, 1);
4972 mnl_attr_put_u8(nlh, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, 1);
4973 mnl_attr_put_u8(nlh, IFLA_VXLAN_LEARNING, 0);
4974 mnl_attr_put_u16(nlh, IFLA_VXLAN_PORT, vxlan_port);
4975 mnl_attr_nest_end(nlh, na_vxlan);
4976 mnl_attr_nest_end(nlh, na_info);
4977 assert(sizeof(buf) >= nlh->nlmsg_len);
4978 ret = flow_tcf_nl_ack(tcf, nlh, NULL, NULL);
4981 "netlink: VTEP %s create failure (%d)",
4983 if (rte_errno != EEXIST)
4985 * Some unhandled error occurred or device is
4986 * for encapsulation and cannot be shared.
4991 * Mark device we actually created.
4992 * We should explicitly delete
4993 * when we do not need it anymore.
4997 /* Try to get ifindex of created of pre-existing device. */
4998 ret = if_nametoindex(name);
5001 "VTEP %s failed to get index (%d)", name, errno);
5004 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5005 "netlink: failed to retrieve VTEP ifindex");
5008 vtep->ifindex = ret;
5009 memset(buf, 0, sizeof(buf));
5010 nlh = mnl_nlmsg_put_header(buf);
5011 nlh->nlmsg_type = RTM_NEWLINK;
5012 nlh->nlmsg_flags = NLM_F_REQUEST;
5013 ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
5014 ifm->ifi_family = AF_UNSPEC;
5016 ifm->ifi_index = vtep->ifindex;
5017 ifm->ifi_flags = IFF_UP;
5018 ifm->ifi_change = IFF_UP;
5019 ret = flow_tcf_nl_ack(tcf, nlh, NULL, NULL);
5021 rte_flow_error_set(error, -errno,
5022 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5023 "netlink: failed to set VTEP link up");
5024 DRV_LOG(WARNING, "netlink: VTEP %s set link up failure (%d)",
5028 ret = mlx5_flow_tcf_init(tcf, vtep->ifindex, error);
5030 DRV_LOG(WARNING, "VTEP %s init failure (%d)", name, rte_errno);
5033 DRV_LOG(INFO, "VTEP create (%d, %d)", vtep->port, vtep->ifindex);
5037 flow_tcf_vtep_delete(tcf, vtep);
5044 static struct tcf_vtep*
5045 flow_tcf_vtep_create(struct mlx5_flow_tcf_context *tcf __rte_unused,
5046 uint16_t port __rte_unused,
5047 struct rte_flow_error *error)
5049 rte_flow_error_set(error, ENOTSUP,
5050 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5051 "netlink: failed to create VTEP, "
5052 "vxlan metadata are not supported by kernel");
5055 #endif /* HAVE_IFLA_VXLAN_COLLECT_METADATA */
5058 * Acquire target interface index for VXLAN tunneling decapsulation.
5059 * In order to share the UDP port within the other interfaces the
5060 * VXLAN device created as not attached to any interface (if created).
5063 * Context object initialized by mlx5_flow_tcf_context_create().
5064 * @param[in] dev_flow
5065 * Flow tcf object with tunnel structure pointer set.
5067 * Perform verbose error reporting if not NULL.
5069 * Interface descriptor pointer on success,
5070 * NULL otherwise and rte_errno is set.
5072 static struct tcf_vtep*
5073 flow_tcf_decap_vtep_acquire(struct mlx5_flow_tcf_context *tcf,
5074 struct mlx5_flow *dev_flow,
5075 struct rte_flow_error *error)
5077 struct tcf_vtep *vtep;
5078 uint16_t port = dev_flow->tcf.vxlan_decap->udp_port;
5080 LIST_FOREACH(vtep, &vtep_list_vxlan, next) {
5081 if (vtep->port == port)
5085 /* Device exists, just increment the reference counter. */
5087 assert(vtep->ifindex);
5090 /* No decapsulation device exists, try to create the new one. */
5091 vtep = flow_tcf_vtep_create(tcf, port, error);
5093 LIST_INSERT_HEAD(&vtep_list_vxlan, vtep, next);
5098 * Aqcuire target interface index for VXLAN tunneling encapsulation.
5101 * Context object initialized by mlx5_flow_tcf_context_create().
5102 * @param[in] ifouter
5103 * Network interface index to attach VXLAN encap device to.
5104 * @param[in] dev_flow
5105 * Flow tcf object with tunnel structure pointer set.
5107 * Perform verbose error reporting if not NULL.
5109 * Interface descriptor pointer on success,
5110 * NULL otherwise and rte_errno is set.
5112 static struct tcf_vtep*
5113 flow_tcf_encap_vtep_acquire(struct mlx5_flow_tcf_context *tcf,
5114 unsigned int ifouter,
5115 struct mlx5_flow *dev_flow,
5116 struct rte_flow_error *error)
5118 static uint16_t port;
5119 struct tcf_vtep *vtep;
5120 struct tcf_irule *iface;
5124 /* Look whether the VTEP for specified port is created. */
5125 port = rte_be_to_cpu_16(dev_flow->tcf.vxlan_encap->udp.dst);
5126 LIST_FOREACH(vtep, &vtep_list_vxlan, next) {
5127 if (vtep->port == port)
5131 /* VTEP already exists, just increment the reference. */
5134 /* Not found, we should create the new VTEP. */
5135 vtep = flow_tcf_vtep_create(tcf, port, error);
5138 LIST_INSERT_HEAD(&vtep_list_vxlan, vtep, next);
5140 assert(vtep->ifindex);
5141 iface = flow_tcf_encap_irule_acquire(tcf, ifouter, error);
5143 if (--vtep->refcnt == 0)
5144 flow_tcf_vtep_delete(tcf, vtep);
5147 dev_flow->tcf.vxlan_encap->iface = iface;
5148 /* Create local ipaddr with peer to specify the outer IPs. */
5149 ret = flow_tcf_encap_local(tcf, iface, dev_flow, true, error);
5151 /* Create neigh rule to specify outer destination MAC. */
5152 ret = flow_tcf_encap_neigh(tcf, iface, dev_flow, true, error);
5154 flow_tcf_encap_local(tcf, iface,
5155 dev_flow, false, error);
5158 dev_flow->tcf.vxlan_encap->iface = NULL;
5159 flow_tcf_encap_irule_release(iface);
5160 if (--vtep->refcnt == 0)
5161 flow_tcf_vtep_delete(tcf, vtep);
5168 * Acquires target interface index for tunneling of any type.
5169 * Creates the new VTEP if needed.
5172 * Context object initialized by mlx5_flow_tcf_context_create().
5173 * @param[in] ifouter
5174 * Network interface index to create VXLAN encap rules on.
5175 * @param[in] dev_flow
5176 * Flow tcf object with tunnel structure pointer set.
5178 * Perform verbose error reporting if not NULL.
5180 * Interface descriptor pointer on success,
5181 * NULL otherwise and rte_errno is set.
5183 static struct tcf_vtep*
5184 flow_tcf_vtep_acquire(struct mlx5_flow_tcf_context *tcf,
5185 unsigned int ifouter,
5186 struct mlx5_flow *dev_flow,
5187 struct rte_flow_error *error)
5189 struct tcf_vtep *vtep = NULL;
5191 assert(dev_flow->tcf.tunnel);
5192 pthread_mutex_lock(&vtep_list_mutex);
5193 switch (dev_flow->tcf.tunnel->type) {
5194 case FLOW_TCF_TUNACT_VXLAN_ENCAP:
5195 vtep = flow_tcf_encap_vtep_acquire(tcf, ifouter,
5198 case FLOW_TCF_TUNACT_VXLAN_DECAP:
5199 vtep = flow_tcf_decap_vtep_acquire(tcf, dev_flow, error);
5202 rte_flow_error_set(error, ENOTSUP,
5203 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5204 "unsupported tunnel type");
5207 pthread_mutex_unlock(&vtep_list_mutex);
5212 * Release tunneling interface by ifindex. Decrements reference
5213 * counter and actually removes the device if counter is zero.
5216 * Context object initialized by mlx5_flow_tcf_context_create().
5218 * VTEP device descriptor structure.
5219 * @param[in] dev_flow
5220 * Flow tcf object with tunnel structure pointer set.
5223 flow_tcf_vtep_release(struct mlx5_flow_tcf_context *tcf,
5224 struct tcf_vtep *vtep,
5225 struct mlx5_flow *dev_flow)
5227 assert(dev_flow->tcf.tunnel);
5228 pthread_mutex_lock(&vtep_list_mutex);
5229 switch (dev_flow->tcf.tunnel->type) {
5230 case FLOW_TCF_TUNACT_VXLAN_DECAP:
5232 case FLOW_TCF_TUNACT_VXLAN_ENCAP: {
5233 struct tcf_irule *iface;
5235 /* Remove the encap ancillary rules first. */
5236 iface = dev_flow->tcf.vxlan_encap->iface;
5238 flow_tcf_encap_neigh(tcf, iface, dev_flow, false, NULL);
5239 flow_tcf_encap_local(tcf, iface, dev_flow, false, NULL);
5240 flow_tcf_encap_irule_release(iface);
5241 dev_flow->tcf.vxlan_encap->iface = NULL;
5246 DRV_LOG(WARNING, "Unsupported tunnel type");
5249 assert(vtep->refcnt);
5250 if (--vtep->refcnt == 0) {
5251 LIST_REMOVE(vtep, next);
5252 flow_tcf_vtep_delete(tcf, vtep);
5254 pthread_mutex_unlock(&vtep_list_mutex);
5257 struct tcf_nlcb_query {
5260 uint32_t flags_valid:1;
5264 * Collect queried rule attributes. This is callback routine called by
5265 * libmnl mnl_cb_run() in loop for every message in received packet.
5266 * Current implementation collects the flower flags only.
5269 * Pointer to reply header.
5270 * @param[in, out] arg
5271 * Context pointer for this callback.
5274 * A positive, nonzero value on success (required by libmnl
5275 * to continue messages processing).
5278 flow_tcf_collect_query_cb(const struct nlmsghdr *nlh, void *arg)
5280 struct tcf_nlcb_query *query = arg;
5281 struct tcmsg *tcm = mnl_nlmsg_get_payload(nlh);
5282 struct nlattr *na, *na_opt;
5283 bool flower = false;
5285 if (nlh->nlmsg_type != RTM_NEWTFILTER ||
5286 tcm->tcm_handle != query->handle)
5288 mnl_attr_for_each(na, nlh, sizeof(*tcm)) {
5289 switch (mnl_attr_get_type(na)) {
5291 if (strcmp(mnl_attr_get_payload(na), "flower")) {
5292 /* Not flower filter, drop entire message. */
5299 /* Not flower options, drop entire message. */
5302 /* Check nested flower options. */
5303 mnl_attr_for_each_nested(na_opt, na) {
5304 switch (mnl_attr_get_type(na_opt)) {
5305 case TCA_FLOWER_FLAGS:
5306 query->flags_valid = 1;
5308 mnl_attr_get_u32(na_opt);
5319 * Query a TC flower rule flags via netlink.
5322 * Context object initialized by mlx5_flow_tcf_context_create().
5323 * @param[in] dev_flow
5324 * Pointer to the flow.
5325 * @param[out] pflags
5326 * pointer to the data retrieved by the query.
5329 * 0 on success, a negative errno value otherwise.
5332 flow_tcf_query_flags(struct mlx5_flow_tcf_context *tcf,
5333 struct mlx5_flow *dev_flow,
5336 struct nlmsghdr *nlh;
5338 struct tcf_nlcb_query query = {
5339 .handle = dev_flow->tcf.tcm->tcm_handle,
5342 nlh = mnl_nlmsg_put_header(tcf->buf);
5343 nlh->nlmsg_type = RTM_GETTFILTER;
5344 nlh->nlmsg_flags = NLM_F_REQUEST;
5345 tcm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tcm));
5346 memcpy(tcm, dev_flow->tcf.tcm, sizeof(*tcm));
5348 * Ignore Netlink error for filter query operations.
5349 * The reply length is sent by kernel as errno.
5350 * Just check we got the flags option.
5352 flow_tcf_nl_ack(tcf, nlh, flow_tcf_collect_query_cb, &query);
5353 if (!query.flags_valid) {
5357 *pflags = query.tc_flags;
5362 * Query and check the in_hw set for specified rule.
5365 * Context object initialized by mlx5_flow_tcf_context_create().
5366 * @param[in] dev_flow
5367 * Pointer to the flow to check.
5370 * 0 on success, a negative errno value otherwise.
5373 flow_tcf_check_inhw(struct mlx5_flow_tcf_context *tcf,
5374 struct mlx5_flow *dev_flow)
5379 ret = flow_tcf_query_flags(tcf, dev_flow, &flags);
5382 return (flags & TCA_CLS_FLAGS_IN_HW) ? 0 : -ENOENT;
5386 * Remove flow from E-Switch by sending Netlink message.
5389 * Pointer to Ethernet device.
5390 * @param[in, out] flow
5391 * Pointer to the sub flow.
5394 flow_tcf_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
5396 struct priv *priv = dev->data->dev_private;
5397 struct mlx5_flow_tcf_context *ctx = priv->tcf_context;
5398 struct mlx5_flow *dev_flow;
5399 struct nlmsghdr *nlh;
5403 dev_flow = LIST_FIRST(&flow->dev_flows);
5406 /* E-Switch flow can't be expanded. */
5407 assert(!LIST_NEXT(dev_flow, next));
5408 if (dev_flow->tcf.applied) {
5409 nlh = dev_flow->tcf.nlh;
5410 nlh->nlmsg_type = RTM_DELTFILTER;
5411 nlh->nlmsg_flags = NLM_F_REQUEST;
5412 flow_tcf_nl_ack(ctx, nlh, NULL, NULL);
5413 if (dev_flow->tcf.tunnel) {
5414 assert(dev_flow->tcf.tunnel->vtep);
5415 flow_tcf_vtep_release(ctx,
5416 dev_flow->tcf.tunnel->vtep,
5418 dev_flow->tcf.tunnel->vtep = NULL;
5420 dev_flow->tcf.applied = 0;
5425 * Apply flow to E-Switch by sending Netlink message.
5428 * Pointer to Ethernet device.
5429 * @param[in, out] flow
5430 * Pointer to the sub flow.
5432 * Pointer to the error structure.
5435 * 0 on success, a negative errno value otherwise and rte_errno is set.
5438 flow_tcf_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
5439 struct rte_flow_error *error)
5441 struct priv *priv = dev->data->dev_private;
5442 struct mlx5_flow_tcf_context *ctx = priv->tcf_context;
5443 struct mlx5_flow *dev_flow;
5444 struct nlmsghdr *nlh;
5446 dev_flow = LIST_FIRST(&flow->dev_flows);
5447 /* E-Switch flow can't be expanded. */
5448 assert(!LIST_NEXT(dev_flow, next));
5449 if (dev_flow->tcf.applied)
5451 nlh = dev_flow->tcf.nlh;
5452 nlh->nlmsg_type = RTM_NEWTFILTER;
5453 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
5454 if (dev_flow->tcf.tunnel) {
5456 * Replace the interface index, target for
5457 * encapsulation, source for decapsulation.
5459 assert(!dev_flow->tcf.tunnel->vtep);
5460 assert(dev_flow->tcf.tunnel->ifindex_ptr);
5461 /* Acquire actual VTEP device when rule is being applied. */
5462 dev_flow->tcf.tunnel->vtep =
5463 flow_tcf_vtep_acquire(ctx,
5464 dev_flow->tcf.tunnel->ifindex_org,
5466 if (!dev_flow->tcf.tunnel->vtep)
5468 DRV_LOG(INFO, "Replace ifindex: %d->%d",
5469 dev_flow->tcf.tunnel->vtep->ifindex,
5470 dev_flow->tcf.tunnel->ifindex_org);
5471 *dev_flow->tcf.tunnel->ifindex_ptr =
5472 dev_flow->tcf.tunnel->vtep->ifindex;
5474 if (!flow_tcf_nl_ack(ctx, nlh, NULL, NULL)) {
5475 dev_flow->tcf.applied = 1;
5476 if (*dev_flow->tcf.ptc_flags & TCA_CLS_FLAGS_SKIP_SW)
5479 * Rule was applied without skip_sw flag set.
5480 * We should check whether the rule was acctually
5481 * accepted by hardware (have look at in_hw flag).
5483 if (flow_tcf_check_inhw(ctx, dev_flow)) {
5484 flow_tcf_remove(dev, flow);
5485 return rte_flow_error_set
5487 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5488 "netlink: rule has no in_hw flag set");
5492 if (dev_flow->tcf.tunnel) {
5493 /* Rollback the VTEP configuration if rule apply failed. */
5494 assert(dev_flow->tcf.tunnel->vtep);
5495 flow_tcf_vtep_release(ctx, dev_flow->tcf.tunnel->vtep,
5497 dev_flow->tcf.tunnel->vtep = NULL;
5499 return rte_flow_error_set(error, rte_errno,
5500 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5501 "netlink: failed to create TC flow rule");
5505 * Remove flow from E-Switch and release resources of the device flow.
5508 * Pointer to Ethernet device.
5509 * @param[in, out] flow
5510 * Pointer to the sub flow.
5513 flow_tcf_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
5515 struct mlx5_flow *dev_flow;
5519 flow_tcf_remove(dev, flow);
5520 if (flow->counter) {
5521 if (--flow->counter->ref_cnt == 0) {
5522 rte_free(flow->counter);
5523 flow->counter = NULL;
5526 dev_flow = LIST_FIRST(&flow->dev_flows);
5529 /* E-Switch flow can't be expanded. */
5530 assert(!LIST_NEXT(dev_flow, next));
5531 LIST_REMOVE(dev_flow, next);
5536 * Helper routine for figuring the space size required for a parse buffer.
5539 * array of values to use.
5541 * Current location in array.
5543 * Value to compare with.
5546 * The maximum between the given value and the array value on index.
5549 flow_tcf_arr_val_max(uint16_t array[], int idx, uint16_t value)
5551 return idx < 0 ? (value) : RTE_MAX((array)[idx], value);
5555 * Parse rtnetlink message attributes filling the attribute table with the info
5559 * Attribute table to be filled.
5561 * Maxinum entry in the attribute table.
5563 * The attributes section in the message to be parsed.
5565 * The length of the attributes section in the message.
5568 flow_tcf_nl_parse_rtattr(struct rtattr *tb[], int max,
5569 struct rtattr *rta, int len)
5571 unsigned short type;
5572 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
5573 while (RTA_OK(rta, len)) {
5574 type = rta->rta_type;
5575 if (type <= max && !tb[type])
5577 rta = RTA_NEXT(rta, len);
5582 * Extract flow counters from flower action.
5585 * flower action stats properties in the Netlink message received.
5587 * The backward sequence of rta_types, as written in the attribute table,
5588 * we need to traverse in order to get to the requested object.
5590 * Current location in rta_type table.
5592 * data holding the count statistics of the rte_flow retrieved from
5596 * 0 if data was found and retrieved, -1 otherwise.
5599 flow_tcf_nl_action_stats_parse_and_get(struct rtattr *rta,
5600 uint16_t rta_type[], int idx,
5601 struct gnet_stats_basic *data)
5603 int tca_stats_max = flow_tcf_arr_val_max(rta_type, idx,
5605 struct rtattr *tbs[tca_stats_max + 1];
5607 if (rta == NULL || idx < 0)
5609 flow_tcf_nl_parse_rtattr(tbs, tca_stats_max,
5610 RTA_DATA(rta), RTA_PAYLOAD(rta));
5611 switch (rta_type[idx]) {
5612 case TCA_STATS_BASIC:
5613 if (tbs[TCA_STATS_BASIC]) {
5614 memcpy(data, RTA_DATA(tbs[TCA_STATS_BASIC]),
5615 RTE_MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]),
5627 * Parse flower single action retrieving the requested action attribute,
5631 * flower action properties in the Netlink message received.
5633 * The backward sequence of rta_types, as written in the attribute table,
5634 * we need to traverse in order to get to the requested object.
5636 * Current location in rta_type table.
5638 * Count statistics retrieved from the message query.
5641 * 0 if data was found and retrieved, -1 otherwise.
5644 flow_tcf_nl_parse_one_action_and_get(struct rtattr *arg,
5645 uint16_t rta_type[], int idx, void *data)
5647 int tca_act_max = flow_tcf_arr_val_max(rta_type, idx, TCA_ACT_STATS);
5648 struct rtattr *tb[tca_act_max + 1];
5650 if (arg == NULL || idx < 0)
5652 flow_tcf_nl_parse_rtattr(tb, tca_act_max,
5653 RTA_DATA(arg), RTA_PAYLOAD(arg));
5654 if (tb[TCA_ACT_KIND] == NULL)
5656 switch (rta_type[idx]) {
5658 if (tb[TCA_ACT_STATS])
5659 return flow_tcf_nl_action_stats_parse_and_get
5662 (struct gnet_stats_basic *)data);
5671 * Parse flower action section in the message retrieving the requested
5672 * attribute from the first action that provides it.
5675 * flower section in the Netlink message received.
5677 * The backward sequence of rta_types, as written in the attribute table,
5678 * we need to traverse in order to get to the requested object.
5680 * Current location in rta_type table.
5682 * data retrieved from the message query.
5685 * 0 if data was found and retrieved, -1 otherwise.
5688 flow_tcf_nl_action_parse_and_get(struct rtattr *arg,
5689 uint16_t rta_type[], int idx, void *data)
5691 struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
5694 if (arg == NULL || idx < 0)
5696 flow_tcf_nl_parse_rtattr(tb, TCA_ACT_MAX_PRIO,
5697 RTA_DATA(arg), RTA_PAYLOAD(arg));
5698 switch (rta_type[idx]) {
5700 * flow counters are stored in the actions defined by the flow
5701 * and not in the flow itself, therefore we need to traverse the
5702 * flower chain of actions in search for them.
5704 * Note that the index is not decremented here.
5707 for (i = 0; i <= TCA_ACT_MAX_PRIO; i++) {
5709 !flow_tcf_nl_parse_one_action_and_get(tb[i],
5722 * Parse flower classifier options in the message, retrieving the requested
5723 * attribute if found.
5726 * flower section in the Netlink message received.
5728 * The backward sequence of rta_types, as written in the attribute table,
5729 * we need to traverse in order to get to the requested object.
5731 * Current location in rta_type table.
5733 * data retrieved from the message query.
5736 * 0 if data was found and retrieved, -1 otherwise.
5739 flow_tcf_nl_opts_parse_and_get(struct rtattr *opt,
5740 uint16_t rta_type[], int idx, void *data)
5742 int tca_flower_max = flow_tcf_arr_val_max(rta_type, idx,
5744 struct rtattr *tb[tca_flower_max + 1];
5746 if (!opt || idx < 0)
5748 flow_tcf_nl_parse_rtattr(tb, tca_flower_max,
5749 RTA_DATA(opt), RTA_PAYLOAD(opt));
5750 switch (rta_type[idx]) {
5751 case TCA_FLOWER_ACT:
5752 if (tb[TCA_FLOWER_ACT])
5753 return flow_tcf_nl_action_parse_and_get
5754 (tb[TCA_FLOWER_ACT],
5755 rta_type, --idx, data);
5764 * Parse Netlink reply on filter query, retrieving the flow counters.
5767 * Message received from Netlink.
5769 * The backward sequence of rta_types, as written in the attribute table,
5770 * we need to traverse in order to get to the requested object.
5772 * Current location in rta_type table.
5774 * data retrieved from the message query.
5777 * 0 if data was found and retrieved, -1 otherwise.
5780 flow_tcf_nl_filter_parse_and_get(struct nlmsghdr *cnlh,
5781 uint16_t rta_type[], int idx, void *data)
5783 struct nlmsghdr *nlh = cnlh;
5784 struct tcmsg *t = NLMSG_DATA(nlh);
5785 int len = nlh->nlmsg_len;
5786 int tca_max = flow_tcf_arr_val_max(rta_type, idx, TCA_OPTIONS);
5787 struct rtattr *tb[tca_max + 1];
5791 if (nlh->nlmsg_type != RTM_NEWTFILTER &&
5792 nlh->nlmsg_type != RTM_GETTFILTER &&
5793 nlh->nlmsg_type != RTM_DELTFILTER)
5795 len -= NLMSG_LENGTH(sizeof(*t));
5798 flow_tcf_nl_parse_rtattr(tb, tca_max, TCA_RTA(t), len);
5799 /* Not a TC flower flow - bail out */
5800 if (!tb[TCA_KIND] ||
5801 strcmp(RTA_DATA(tb[TCA_KIND]), "flower"))
5803 switch (rta_type[idx]) {
5805 if (tb[TCA_OPTIONS])
5806 return flow_tcf_nl_opts_parse_and_get(tb[TCA_OPTIONS],
5817 * A callback to parse Netlink reply on TC flower query.
5820 * Message received from Netlink.
5822 * Pointer to data area to be filled by the parsing routine.
5823 * assumed to be a pointer to struct flow_tcf_stats_basic.
5829 flow_tcf_nl_message_get_stats_basic(const struct nlmsghdr *nlh, void *data)
5832 * The backward sequence of rta_types to pass in order to get
5835 uint16_t rta_type[] = { TCA_STATS_BASIC, TCA_ACT_STATS,
5836 TCA_FLOWER_ACT, TCA_OPTIONS };
5837 struct flow_tcf_stats_basic *sb_data = data;
5839 const struct nlmsghdr *c;
5840 struct nlmsghdr *nc;
5841 } tnlh = { .c = nlh };
5843 if (!flow_tcf_nl_filter_parse_and_get(tnlh.nc, rta_type,
5844 RTE_DIM(rta_type) - 1,
5845 (void *)&sb_data->counters))
5846 sb_data->valid = true;
5851 * Query a TC flower rule for its statistics via netlink.
5854 * Pointer to Ethernet device.
5856 * Pointer to the sub flow.
5858 * data retrieved by the query.
5860 * Perform verbose error reporting if not NULL.
5863 * 0 on success, a negative errno value otherwise and rte_errno is set.
5866 flow_tcf_query_count(struct rte_eth_dev *dev,
5867 struct rte_flow *flow,
5869 struct rte_flow_error *error)
5871 struct flow_tcf_stats_basic sb_data;
5872 struct rte_flow_query_count *qc = data;
5873 struct priv *priv = dev->data->dev_private;
5874 struct mlx5_flow_tcf_context *ctx = priv->tcf_context;
5875 struct mnl_socket *nl = ctx->nl;
5876 struct mlx5_flow *dev_flow;
5877 struct nlmsghdr *nlh;
5878 uint32_t seq = priv->tcf_context->seq++;
5882 memset(&sb_data, 0, sizeof(sb_data));
5883 dev_flow = LIST_FIRST(&flow->dev_flows);
5884 /* E-Switch flow can't be expanded. */
5885 assert(!LIST_NEXT(dev_flow, next));
5886 if (!dev_flow->flow->counter)
5888 nlh = dev_flow->tcf.nlh;
5889 nlh->nlmsg_type = RTM_GETTFILTER;
5890 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ECHO;
5891 nlh->nlmsg_seq = seq;
5892 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) == -1)
5895 ret = mnl_socket_recvfrom(nl, ctx->buf, ctx->buf_size);
5898 ret = mnl_cb_run(ctx->buf, ret, seq,
5899 mnl_socket_get_portid(nl),
5900 flow_tcf_nl_message_get_stats_basic,
5903 /* Return the delta from last reset. */
5904 if (sb_data.valid) {
5905 /* Return the delta from last reset. */
5908 qc->hits = sb_data.counters.packets - flow->counter->hits;
5909 qc->bytes = sb_data.counters.bytes - flow->counter->bytes;
5911 flow->counter->hits = sb_data.counters.packets;
5912 flow->counter->bytes = sb_data.counters.bytes;
5916 return rte_flow_error_set(error, EINVAL,
5917 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5919 "flow does not have counter");
5921 return rte_flow_error_set
5922 (error, errno, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5923 NULL, "netlink: failed to read flow rule counters");
5925 return rte_flow_error_set
5926 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5927 NULL, "counters are not available.");
5933 * @see rte_flow_query()
5937 flow_tcf_query(struct rte_eth_dev *dev,
5938 struct rte_flow *flow,
5939 const struct rte_flow_action *actions,
5941 struct rte_flow_error *error)
5945 for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
5946 switch (actions->type) {
5947 case RTE_FLOW_ACTION_TYPE_VOID:
5949 case RTE_FLOW_ACTION_TYPE_COUNT:
5950 ret = flow_tcf_query_count(dev, flow, data, error);
5953 return rte_flow_error_set(error, ENOTSUP,
5954 RTE_FLOW_ERROR_TYPE_ACTION,
5956 "action not supported");
5962 const struct mlx5_flow_driver_ops mlx5_flow_tcf_drv_ops = {
5963 .validate = flow_tcf_validate,
5964 .prepare = flow_tcf_prepare,
5965 .translate = flow_tcf_translate,
5966 .apply = flow_tcf_apply,
5967 .remove = flow_tcf_remove,
5968 .destroy = flow_tcf_destroy,
5969 .query = flow_tcf_query,
5973 * Create and configure a libmnl socket for Netlink flow rules.
5976 * A valid libmnl socket object pointer on success, NULL otherwise and
5979 static struct mnl_socket *
5980 flow_tcf_mnl_socket_create(void)
5982 struct mnl_socket *nl = mnl_socket_open(NETLINK_ROUTE);
5985 mnl_socket_setsockopt(nl, NETLINK_CAP_ACK, &(int){ 1 },
5987 if (!mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID))
5992 mnl_socket_close(nl);
5997 * Destroy a libmnl socket.
6000 * Libmnl socket of the @p NETLINK_ROUTE kind.
6003 flow_tcf_mnl_socket_destroy(struct mnl_socket *nl)
6006 mnl_socket_close(nl);
6010 * Initialize ingress qdisc of a given network interface.
6013 * Pointer to tc-flower context to use.
6015 * Index of network interface to initialize.
6017 * Perform verbose error reporting if not NULL.
6020 * 0 on success, a negative errno value otherwise and rte_errno is set.
6023 mlx5_flow_tcf_init(struct mlx5_flow_tcf_context *ctx,
6024 unsigned int ifindex, struct rte_flow_error *error)
6026 struct nlmsghdr *nlh;
6028 alignas(struct nlmsghdr)
6029 uint8_t buf[mnl_nlmsg_size(sizeof(*tcm)) +
6030 SZ_NLATTR_STRZ_OF("ingress") +
6031 MNL_BUF_EXTRA_SPACE];
6033 /* Destroy existing ingress qdisc and everything attached to it. */
6034 nlh = mnl_nlmsg_put_header(buf);
6035 nlh->nlmsg_type = RTM_DELQDISC;
6036 nlh->nlmsg_flags = NLM_F_REQUEST;
6037 tcm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tcm));
6038 tcm->tcm_family = AF_UNSPEC;
6039 tcm->tcm_ifindex = ifindex;
6040 tcm->tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0);
6041 tcm->tcm_parent = TC_H_INGRESS;
6042 assert(sizeof(buf) >= nlh->nlmsg_len);
6043 /* Ignore errors when qdisc is already absent. */
6044 if (flow_tcf_nl_ack(ctx, nlh, NULL, NULL) &&
6045 rte_errno != EINVAL && rte_errno != ENOENT)
6046 return rte_flow_error_set(error, rte_errno,
6047 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6048 "netlink: failed to remove ingress"
6050 /* Create fresh ingress qdisc. */
6051 nlh = mnl_nlmsg_put_header(buf);
6052 nlh->nlmsg_type = RTM_NEWQDISC;
6053 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
6054 tcm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tcm));
6055 tcm->tcm_family = AF_UNSPEC;
6056 tcm->tcm_ifindex = ifindex;
6057 tcm->tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0);
6058 tcm->tcm_parent = TC_H_INGRESS;
6059 mnl_attr_put_strz_check(nlh, sizeof(buf), TCA_KIND, "ingress");
6060 assert(sizeof(buf) >= nlh->nlmsg_len);
6061 if (flow_tcf_nl_ack(ctx, nlh, NULL, NULL))
6062 return rte_flow_error_set(error, rte_errno,
6063 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
6064 "netlink: failed to create ingress"
6070 * Create libmnl context for Netlink flow rules.
6073 * A valid libmnl socket object pointer on success, NULL otherwise and
6076 struct mlx5_flow_tcf_context *
6077 mlx5_flow_tcf_context_create(void)
6079 struct mlx5_flow_tcf_context *ctx = rte_zmalloc(__func__,
6084 ctx->nl = flow_tcf_mnl_socket_create();
6087 ctx->buf_size = MNL_SOCKET_BUFFER_SIZE;
6088 ctx->buf = rte_zmalloc(__func__,
6089 ctx->buf_size, sizeof(uint32_t));
6092 ctx->seq = random();
6095 mlx5_flow_tcf_context_destroy(ctx);
6100 * Destroy a libmnl context.
6103 * Libmnl socket of the @p NETLINK_ROUTE kind.
6106 mlx5_flow_tcf_context_destroy(struct mlx5_flow_tcf_context *ctx)
6110 flow_tcf_mnl_socket_destroy(ctx->nl);