net/mlx5: fix Netlink communication routine
[dpdk.git] / drivers / net / mlx5 / mlx5_flow_tcf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 6WIND S.A.
3  * Copyright 2018 Mellanox Technologies, Ltd
4  */
5
6 #include <assert.h>
7 #include <errno.h>
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>
18 #include <stdalign.h>
19 #include <stdbool.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <sys/socket.h>
24
25 #include <rte_byteorder.h>
26 #include <rte_errno.h>
27 #include <rte_ether.h>
28 #include <rte_flow.h>
29 #include <rte_malloc.h>
30 #include <rte_common.h>
31
32 #include "mlx5.h"
33 #include "mlx5_flow.h"
34 #include "mlx5_autoconf.h"
35
36 #ifdef HAVE_TC_ACT_VLAN
37
38 #include <linux/tc_act/tc_vlan.h>
39
40 #else /* HAVE_TC_ACT_VLAN */
41
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
50
51 struct tc_vlan {
52         tc_gen;
53         int v_action;
54 };
55
56 #endif /* HAVE_TC_ACT_VLAN */
57
58 #ifdef HAVE_TC_ACT_PEDIT
59
60 #include <linux/tc_act/tc_pedit.h>
61
62 #else /* HAVE_TC_ACT_VLAN */
63
64 enum {
65         TCA_PEDIT_UNSPEC,
66         TCA_PEDIT_TM,
67         TCA_PEDIT_PARMS,
68         TCA_PEDIT_PAD,
69         TCA_PEDIT_PARMS_EX,
70         TCA_PEDIT_KEYS_EX,
71         TCA_PEDIT_KEY_EX,
72         __TCA_PEDIT_MAX
73 };
74
75 enum {
76         TCA_PEDIT_KEY_EX_HTYPE = 1,
77         TCA_PEDIT_KEY_EX_CMD = 2,
78         __TCA_PEDIT_KEY_EX_MAX
79 };
80
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,
88         __PEDIT_HDR_TYPE_MAX,
89 };
90
91 enum pedit_cmd {
92         TCA_PEDIT_KEY_EX_CMD_SET = 0,
93         TCA_PEDIT_KEY_EX_CMD_ADD = 1,
94         __PEDIT_CMD_MAX,
95 };
96
97 struct tc_pedit_key {
98         __u32 mask; /* AND */
99         __u32 val; /*XOR */
100         __u32 off; /*offset */
101         __u32 at;
102         __u32 offmask;
103         __u32 shift;
104 };
105
106 __extension__
107 struct tc_pedit_sel {
108         tc_gen;
109         unsigned char nkeys;
110         unsigned char flags;
111         struct tc_pedit_key keys[0];
112 };
113
114 #endif /* HAVE_TC_ACT_VLAN */
115
116 #ifdef HAVE_TC_ACT_TUNNEL_KEY
117
118 #include <linux/tc_act/tc_tunnel_key.h>
119
120 #ifndef HAVE_TCA_TUNNEL_KEY_ENC_DST_PORT
121 #define TCA_TUNNEL_KEY_ENC_DST_PORT 9
122 #endif
123
124 #ifndef HAVE_TCA_TUNNEL_KEY_NO_CSUM
125 #define TCA_TUNNEL_KEY_NO_CSUM 10
126 #endif
127
128 #else /* HAVE_TC_ACT_TUNNEL_KEY */
129
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
141
142 struct tc_tunnel_key {
143         tc_gen;
144         int t_action;
145 };
146
147 #endif /* HAVE_TC_ACT_TUNNEL_KEY */
148
149 /* Normally found in linux/netlink.h. */
150 #ifndef NETLINK_CAP_ACK
151 #define NETLINK_CAP_ACK 10
152 #endif
153
154 /* Normally found in linux/pkt_sched.h. */
155 #ifndef TC_H_MIN_INGRESS
156 #define TC_H_MIN_INGRESS 0xfff2u
157 #endif
158
159 /* Normally found in linux/pkt_cls.h. */
160 #ifndef TCA_CLS_FLAGS_SKIP_SW
161 #define TCA_CLS_FLAGS_SKIP_SW (1 << 1)
162 #endif
163 #ifndef HAVE_TCA_CHAIN
164 #define TCA_CHAIN 11
165 #endif
166 #ifndef HAVE_TCA_FLOWER_ACT
167 #define TCA_FLOWER_ACT 3
168 #endif
169 #ifndef HAVE_TCA_FLOWER_FLAGS
170 #define TCA_FLOWER_FLAGS 22
171 #endif
172 #ifndef HAVE_TCA_FLOWER_KEY_ETH_TYPE
173 #define TCA_FLOWER_KEY_ETH_TYPE 8
174 #endif
175 #ifndef HAVE_TCA_FLOWER_KEY_ETH_DST
176 #define TCA_FLOWER_KEY_ETH_DST 4
177 #endif
178 #ifndef HAVE_TCA_FLOWER_KEY_ETH_DST_MASK
179 #define TCA_FLOWER_KEY_ETH_DST_MASK 5
180 #endif
181 #ifndef HAVE_TCA_FLOWER_KEY_ETH_SRC
182 #define TCA_FLOWER_KEY_ETH_SRC 6
183 #endif
184 #ifndef HAVE_TCA_FLOWER_KEY_ETH_SRC_MASK
185 #define TCA_FLOWER_KEY_ETH_SRC_MASK 7
186 #endif
187 #ifndef HAVE_TCA_FLOWER_KEY_IP_PROTO
188 #define TCA_FLOWER_KEY_IP_PROTO 9
189 #endif
190 #ifndef HAVE_TCA_FLOWER_KEY_IPV4_SRC
191 #define TCA_FLOWER_KEY_IPV4_SRC 10
192 #endif
193 #ifndef HAVE_TCA_FLOWER_KEY_IPV4_SRC_MASK
194 #define TCA_FLOWER_KEY_IPV4_SRC_MASK 11
195 #endif
196 #ifndef HAVE_TCA_FLOWER_KEY_IPV4_DST
197 #define TCA_FLOWER_KEY_IPV4_DST 12
198 #endif
199 #ifndef HAVE_TCA_FLOWER_KEY_IPV4_DST_MASK
200 #define TCA_FLOWER_KEY_IPV4_DST_MASK 13
201 #endif
202 #ifndef HAVE_TCA_FLOWER_KEY_IPV6_SRC
203 #define TCA_FLOWER_KEY_IPV6_SRC 14
204 #endif
205 #ifndef HAVE_TCA_FLOWER_KEY_IPV6_SRC_MASK
206 #define TCA_FLOWER_KEY_IPV6_SRC_MASK 15
207 #endif
208 #ifndef HAVE_TCA_FLOWER_KEY_IPV6_DST
209 #define TCA_FLOWER_KEY_IPV6_DST 16
210 #endif
211 #ifndef HAVE_TCA_FLOWER_KEY_IPV6_DST_MASK
212 #define TCA_FLOWER_KEY_IPV6_DST_MASK 17
213 #endif
214 #ifndef HAVE_TCA_FLOWER_KEY_TCP_SRC
215 #define TCA_FLOWER_KEY_TCP_SRC 18
216 #endif
217 #ifndef HAVE_TCA_FLOWER_KEY_TCP_SRC_MASK
218 #define TCA_FLOWER_KEY_TCP_SRC_MASK 35
219 #endif
220 #ifndef HAVE_TCA_FLOWER_KEY_TCP_DST
221 #define TCA_FLOWER_KEY_TCP_DST 19
222 #endif
223 #ifndef HAVE_TCA_FLOWER_KEY_TCP_DST_MASK
224 #define TCA_FLOWER_KEY_TCP_DST_MASK 36
225 #endif
226 #ifndef HAVE_TCA_FLOWER_KEY_UDP_SRC
227 #define TCA_FLOWER_KEY_UDP_SRC 20
228 #endif
229 #ifndef HAVE_TCA_FLOWER_KEY_UDP_SRC_MASK
230 #define TCA_FLOWER_KEY_UDP_SRC_MASK 37
231 #endif
232 #ifndef HAVE_TCA_FLOWER_KEY_UDP_DST
233 #define TCA_FLOWER_KEY_UDP_DST 21
234 #endif
235 #ifndef HAVE_TCA_FLOWER_KEY_UDP_DST_MASK
236 #define TCA_FLOWER_KEY_UDP_DST_MASK 38
237 #endif
238 #ifndef HAVE_TCA_FLOWER_KEY_VLAN_ID
239 #define TCA_FLOWER_KEY_VLAN_ID 23
240 #endif
241 #ifndef HAVE_TCA_FLOWER_KEY_VLAN_PRIO
242 #define TCA_FLOWER_KEY_VLAN_PRIO 24
243 #endif
244 #ifndef HAVE_TCA_FLOWER_KEY_VLAN_ETH_TYPE
245 #define TCA_FLOWER_KEY_VLAN_ETH_TYPE 25
246 #endif
247 #ifndef HAVE_TCA_FLOWER_KEY_ENC_KEY_ID
248 #define TCA_FLOWER_KEY_ENC_KEY_ID 26
249 #endif
250 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV4_SRC
251 #define TCA_FLOWER_KEY_ENC_IPV4_SRC 27
252 #endif
253 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK
254 #define TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK 28
255 #endif
256 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV4_DST
257 #define TCA_FLOWER_KEY_ENC_IPV4_DST 29
258 #endif
259 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV4_DST_MASK
260 #define TCA_FLOWER_KEY_ENC_IPV4_DST_MASK 30
261 #endif
262 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV6_SRC
263 #define TCA_FLOWER_KEY_ENC_IPV6_SRC 31
264 #endif
265 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK
266 #define TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK 32
267 #endif
268 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV6_DST
269 #define TCA_FLOWER_KEY_ENC_IPV6_DST 33
270 #endif
271 #ifndef HAVE_TCA_FLOWER_KEY_ENC_IPV6_DST_MASK
272 #define TCA_FLOWER_KEY_ENC_IPV6_DST_MASK 34
273 #endif
274 #ifndef HAVE_TCA_FLOWER_KEY_ENC_UDP_SRC_PORT
275 #define TCA_FLOWER_KEY_ENC_UDP_SRC_PORT 43
276 #endif
277 #ifndef HAVE_TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK
278 #define TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK 44
279 #endif
280 #ifndef HAVE_TCA_FLOWER_KEY_ENC_UDP_DST_PORT
281 #define TCA_FLOWER_KEY_ENC_UDP_DST_PORT 45
282 #endif
283 #ifndef HAVE_TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK
284 #define TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK 46
285 #endif
286 #ifndef HAVE_TCA_FLOWER_KEY_TCP_FLAGS
287 #define TCA_FLOWER_KEY_TCP_FLAGS 71
288 #endif
289 #ifndef HAVE_TCA_FLOWER_KEY_TCP_FLAGS_MASK
290 #define TCA_FLOWER_KEY_TCP_FLAGS_MASK 72
291 #endif
292 #ifndef HAVE_TC_ACT_GOTO_CHAIN
293 #define TC_ACT_GOTO_CHAIN 0x20000000
294 #endif
295
296 #ifndef IPV6_ADDR_LEN
297 #define IPV6_ADDR_LEN 16
298 #endif
299
300 #ifndef IPV4_ADDR_LEN
301 #define IPV4_ADDR_LEN 4
302 #endif
303
304 #ifndef TP_PORT_LEN
305 #define TP_PORT_LEN 2 /* Transport Port (UDP/TCP) Length */
306 #endif
307
308 #ifndef TTL_LEN
309 #define TTL_LEN 1
310 #endif
311
312 #ifndef TCA_ACT_MAX_PRIO
313 #define TCA_ACT_MAX_PRIO 32
314 #endif
315
316 /** UDP port range of VXLAN devices created by driver. */
317 #define MLX5_VXLAN_PORT_MIN 30000
318 #define MLX5_VXLAN_PORT_MAX 60000
319 #define MLX5_VXLAN_DEVICE_PFX "vmlx_"
320
321 /** Tunnel action type, used for @p type in header structure. */
322 enum flow_tcf_tunact_type {
323         FLOW_TCF_TUNACT_VXLAN_DECAP,
324         FLOW_TCF_TUNACT_VXLAN_ENCAP,
325 };
326
327 /** Flags used for @p mask in tunnel action encap descriptors. */
328 #define FLOW_TCF_ENCAP_ETH_SRC (1u << 0)
329 #define FLOW_TCF_ENCAP_ETH_DST (1u << 1)
330 #define FLOW_TCF_ENCAP_IPV4_SRC (1u << 2)
331 #define FLOW_TCF_ENCAP_IPV4_DST (1u << 3)
332 #define FLOW_TCF_ENCAP_IPV6_SRC (1u << 4)
333 #define FLOW_TCF_ENCAP_IPV6_DST (1u << 5)
334 #define FLOW_TCF_ENCAP_UDP_SRC (1u << 6)
335 #define FLOW_TCF_ENCAP_UDP_DST (1u << 7)
336 #define FLOW_TCF_ENCAP_VXLAN_VNI (1u << 8)
337
338 /**
339  * Structure for holding netlink context.
340  * Note the size of the message buffer which is MNL_SOCKET_BUFFER_SIZE.
341  * Using this (8KB) buffer size ensures that netlink messages will never be
342  * truncated.
343  */
344 struct mlx5_flow_tcf_context {
345         struct mnl_socket *nl; /* NETLINK_ROUTE libmnl socket. */
346         uint32_t seq; /* Message sequence number. */
347         uint32_t buf_size; /* Message buffer size. */
348         uint8_t *buf; /* Message buffer. */
349 };
350
351 /**
352  * Neigh rule structure. The neigh rule is applied via Netlink to
353  * outer tunnel iface in order to provide destination MAC address
354  * for the VXLAN encapsultion. The neigh rule is implicitly related
355  * to the Flow itself and can be shared by multiple Flows.
356  */
357 struct tcf_neigh_rule {
358         LIST_ENTRY(tcf_neigh_rule) next;
359         uint32_t refcnt;
360         struct ether_addr eth;
361         uint16_t mask;
362         union {
363                 struct {
364                         rte_be32_t dst;
365                 } ipv4;
366                 struct {
367                         uint8_t dst[IPV6_ADDR_LEN];
368                 } ipv6;
369         };
370 };
371
372 /**
373  * Local rule structure. The local rule is applied via Netlink to
374  * outer tunnel iface in order to provide local and peer IP addresses
375  * of the VXLAN tunnel for encapsulation. The local rule is implicitly
376  * related to the Flow itself and can be shared by multiple Flows.
377  */
378 struct tcf_local_rule {
379         LIST_ENTRY(tcf_local_rule) next;
380         uint32_t refcnt;
381         uint16_t mask;
382         union {
383                 struct {
384                         rte_be32_t dst;
385                         rte_be32_t src;
386                 } ipv4;
387                 struct {
388                         uint8_t dst[IPV6_ADDR_LEN];
389                         uint8_t src[IPV6_ADDR_LEN];
390                 } ipv6;
391         };
392 };
393
394 /** VXLAN virtual netdev. */
395 struct tcf_vtep {
396         LIST_ENTRY(tcf_vtep) next;
397         LIST_HEAD(, tcf_neigh_rule) neigh;
398         LIST_HEAD(, tcf_local_rule) local;
399         uint32_t refcnt;
400         unsigned int ifindex; /**< Own interface index. */
401         unsigned int ifouter; /**< Index of device attached to. */
402         uint16_t port;
403         uint8_t created;
404 };
405
406 /** Tunnel descriptor header, common for all tunnel types. */
407 struct flow_tcf_tunnel_hdr {
408         uint32_t type; /**< Tunnel action type. */
409         struct tcf_vtep *vtep; /**< Virtual tunnel endpoint device. */
410         unsigned int ifindex_org; /**< Original dst/src interface */
411         unsigned int *ifindex_ptr; /**< Interface ptr in message. */
412 };
413
414 struct flow_tcf_vxlan_decap {
415         struct flow_tcf_tunnel_hdr hdr;
416         uint16_t udp_port;
417 };
418
419 struct flow_tcf_vxlan_encap {
420         struct flow_tcf_tunnel_hdr hdr;
421         uint32_t mask;
422         struct {
423                 struct ether_addr dst;
424                 struct ether_addr src;
425         } eth;
426         union {
427                 struct {
428                         rte_be32_t dst;
429                         rte_be32_t src;
430                 } ipv4;
431                 struct {
432                         uint8_t dst[IPV6_ADDR_LEN];
433                         uint8_t src[IPV6_ADDR_LEN];
434                 } ipv6;
435         };
436 struct {
437                 rte_be16_t src;
438                 rte_be16_t dst;
439         } udp;
440         struct {
441                 uint8_t vni[3];
442         } vxlan;
443 };
444
445 /** Structure used when extracting the values of a flow counters
446  * from a netlink message.
447  */
448 struct flow_tcf_stats_basic {
449         bool valid;
450         struct gnet_stats_basic counters;
451 };
452
453 /** Empty masks for known item types. */
454 static const union {
455         struct rte_flow_item_port_id port_id;
456         struct rte_flow_item_eth eth;
457         struct rte_flow_item_vlan vlan;
458         struct rte_flow_item_ipv4 ipv4;
459         struct rte_flow_item_ipv6 ipv6;
460         struct rte_flow_item_tcp tcp;
461         struct rte_flow_item_udp udp;
462         struct rte_flow_item_vxlan vxlan;
463 } flow_tcf_mask_empty;
464
465 /** Supported masks for known item types. */
466 static const struct {
467         struct rte_flow_item_port_id port_id;
468         struct rte_flow_item_eth eth;
469         struct rte_flow_item_vlan vlan;
470         struct rte_flow_item_ipv4 ipv4;
471         struct rte_flow_item_ipv6 ipv6;
472         struct rte_flow_item_tcp tcp;
473         struct rte_flow_item_udp udp;
474         struct rte_flow_item_vxlan vxlan;
475 } flow_tcf_mask_supported = {
476         .port_id = {
477                 .id = 0xffffffff,
478         },
479         .eth = {
480                 .type = RTE_BE16(0xffff),
481                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
482                 .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
483         },
484         .vlan = {
485                 /* PCP and VID only, no DEI. */
486                 .tci = RTE_BE16(0xefff),
487                 .inner_type = RTE_BE16(0xffff),
488         },
489         .ipv4.hdr = {
490                 .next_proto_id = 0xff,
491                 .src_addr = RTE_BE32(0xffffffff),
492                 .dst_addr = RTE_BE32(0xffffffff),
493         },
494         .ipv6.hdr = {
495                 .proto = 0xff,
496                 .src_addr =
497                         "\xff\xff\xff\xff\xff\xff\xff\xff"
498                         "\xff\xff\xff\xff\xff\xff\xff\xff",
499                 .dst_addr =
500                         "\xff\xff\xff\xff\xff\xff\xff\xff"
501                         "\xff\xff\xff\xff\xff\xff\xff\xff",
502         },
503         .tcp.hdr = {
504                 .src_port = RTE_BE16(0xffff),
505                 .dst_port = RTE_BE16(0xffff),
506                 .tcp_flags = 0xff,
507         },
508         .udp.hdr = {
509                 .src_port = RTE_BE16(0xffff),
510                 .dst_port = RTE_BE16(0xffff),
511         },
512         .vxlan = {
513                .vni = "\xff\xff\xff",
514         },
515 };
516
517 #define SZ_NLATTR_HDR MNL_ALIGN(sizeof(struct nlattr))
518 #define SZ_NLATTR_NEST SZ_NLATTR_HDR
519 #define SZ_NLATTR_DATA_OF(len) MNL_ALIGN(SZ_NLATTR_HDR + (len))
520 #define SZ_NLATTR_TYPE_OF(typ) SZ_NLATTR_DATA_OF(sizeof(typ))
521 #define SZ_NLATTR_STRZ_OF(str) SZ_NLATTR_DATA_OF(strlen(str) + 1)
522
523 #define PTOI_TABLE_SZ_MAX(dev) (mlx5_dev_to_port_id((dev)->device, NULL, 0) + 2)
524
525 /** DPDK port to network interface index (ifindex) conversion. */
526 struct flow_tcf_ptoi {
527         uint16_t port_id; /**< DPDK port ID. */
528         unsigned int ifindex; /**< Network interface index. */
529 };
530
531 /* Due to a limitation on driver/FW. */
532 #define MLX5_TCF_GROUP_ID_MAX 3
533
534 /*
535  * Due to a limitation on driver/FW, priority ranges from 1 to 16 in kernel.
536  * Priority in rte_flow attribute starts from 0 and is added by 1 in
537  * translation. This is subject to be changed to determine the max priority
538  * based on trial-and-error like Verbs driver once the restriction is lifted or
539  * the range is extended.
540  */
541 #define MLX5_TCF_GROUP_PRIORITY_MAX 15
542
543 #define MLX5_TCF_FATE_ACTIONS \
544         (MLX5_FLOW_ACTION_DROP | MLX5_FLOW_ACTION_PORT_ID | \
545          MLX5_FLOW_ACTION_JUMP)
546
547 #define MLX5_TCF_VLAN_ACTIONS \
548         (MLX5_FLOW_ACTION_OF_POP_VLAN | MLX5_FLOW_ACTION_OF_PUSH_VLAN | \
549          MLX5_FLOW_ACTION_OF_SET_VLAN_VID | MLX5_FLOW_ACTION_OF_SET_VLAN_PCP)
550
551 #define MLX5_TCF_VXLAN_ACTIONS \
552         (MLX5_FLOW_ACTION_VXLAN_ENCAP | MLX5_FLOW_ACTION_VXLAN_DECAP)
553
554 #define MLX5_TCF_PEDIT_ACTIONS \
555         (MLX5_FLOW_ACTION_SET_IPV4_SRC | MLX5_FLOW_ACTION_SET_IPV4_DST | \
556          MLX5_FLOW_ACTION_SET_IPV6_SRC | MLX5_FLOW_ACTION_SET_IPV6_DST | \
557          MLX5_FLOW_ACTION_SET_TP_SRC | MLX5_FLOW_ACTION_SET_TP_DST | \
558          MLX5_FLOW_ACTION_SET_TTL | MLX5_FLOW_ACTION_DEC_TTL | \
559          MLX5_FLOW_ACTION_SET_MAC_SRC | MLX5_FLOW_ACTION_SET_MAC_DST)
560
561 #define MLX5_TCF_CONFIG_ACTIONS \
562         (MLX5_FLOW_ACTION_PORT_ID | MLX5_FLOW_ACTION_JUMP | \
563          MLX5_FLOW_ACTION_OF_PUSH_VLAN | MLX5_FLOW_ACTION_OF_SET_VLAN_VID | \
564          MLX5_FLOW_ACTION_OF_SET_VLAN_PCP | \
565          (MLX5_TCF_PEDIT_ACTIONS & ~MLX5_FLOW_ACTION_DEC_TTL))
566
567 #define MAX_PEDIT_KEYS 128
568 #define SZ_PEDIT_KEY_VAL 4
569
570 #define NUM_OF_PEDIT_KEYS(sz) \
571         (((sz) / SZ_PEDIT_KEY_VAL) + (((sz) % SZ_PEDIT_KEY_VAL) ? 1 : 0))
572
573 struct pedit_key_ex {
574         enum pedit_header_type htype;
575         enum pedit_cmd cmd;
576 };
577
578 struct pedit_parser {
579         struct tc_pedit_sel sel;
580         struct tc_pedit_key keys[MAX_PEDIT_KEYS];
581         struct pedit_key_ex keys_ex[MAX_PEDIT_KEYS];
582 };
583
584 /**
585  * Create space for using the implicitly created TC flow counter.
586  *
587  * @param[in] dev
588  *   Pointer to the Ethernet device structure.
589  *
590  * @return
591  *   A pointer to the counter data structure, NULL otherwise and
592  *   rte_errno is set.
593  */
594 static struct mlx5_flow_counter *
595 flow_tcf_counter_new(void)
596 {
597         struct mlx5_flow_counter *cnt;
598
599         /*
600          * eswitch counter cannot be shared and its id is unknown.
601          * currently returning all with id 0.
602          * in the future maybe better to switch to unique numbers.
603          */
604         struct mlx5_flow_counter tmpl = {
605                 .ref_cnt = 1,
606         };
607         cnt = rte_calloc(__func__, 1, sizeof(*cnt), 0);
608         if (!cnt) {
609                 rte_errno = ENOMEM;
610                 return NULL;
611         }
612         *cnt = tmpl;
613         /* Implicit counter, do not add to list. */
614         return cnt;
615 }
616
617 /**
618  * Set pedit key of MAC address
619  *
620  * @param[in] actions
621  *   pointer to action specification
622  * @param[in,out] p_parser
623  *   pointer to pedit_parser
624  */
625 static void
626 flow_tcf_pedit_key_set_mac(const struct rte_flow_action *actions,
627                            struct pedit_parser *p_parser)
628 {
629         int idx = p_parser->sel.nkeys;
630         uint32_t off = actions->type == RTE_FLOW_ACTION_TYPE_SET_MAC_SRC ?
631                                         offsetof(struct ether_hdr, s_addr) :
632                                         offsetof(struct ether_hdr, d_addr);
633         const struct rte_flow_action_set_mac *conf =
634                 (const struct rte_flow_action_set_mac *)actions->conf;
635
636         p_parser->keys[idx].off = off;
637         p_parser->keys[idx].mask = ~UINT32_MAX;
638         p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_ETH;
639         p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_SET;
640         memcpy(&p_parser->keys[idx].val,
641                 conf->mac_addr, SZ_PEDIT_KEY_VAL);
642         idx++;
643         p_parser->keys[idx].off = off + SZ_PEDIT_KEY_VAL;
644         p_parser->keys[idx].mask = 0xFFFF0000;
645         p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_ETH;
646         p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_SET;
647         memcpy(&p_parser->keys[idx].val,
648                 conf->mac_addr + SZ_PEDIT_KEY_VAL,
649                 ETHER_ADDR_LEN - SZ_PEDIT_KEY_VAL);
650         p_parser->sel.nkeys = (++idx);
651 }
652
653 /**
654  * Set pedit key of decrease/set ttl
655  *
656  * @param[in] actions
657  *   pointer to action specification
658  * @param[in,out] p_parser
659  *   pointer to pedit_parser
660  * @param[in] item_flags
661  *   flags of all items presented
662  */
663 static void
664 flow_tcf_pedit_key_set_dec_ttl(const struct rte_flow_action *actions,
665                                 struct pedit_parser *p_parser,
666                                 uint64_t item_flags)
667 {
668         int idx = p_parser->sel.nkeys;
669
670         p_parser->keys[idx].mask = 0xFFFFFF00;
671         if (item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV4) {
672                 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_IP4;
673                 p_parser->keys[idx].off =
674                         offsetof(struct ipv4_hdr, time_to_live);
675         }
676         if (item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV6) {
677                 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6;
678                 p_parser->keys[idx].off =
679                         offsetof(struct ipv6_hdr, hop_limits);
680         }
681         if (actions->type == RTE_FLOW_ACTION_TYPE_DEC_TTL) {
682                 p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_ADD;
683                 p_parser->keys[idx].val = 0x000000FF;
684         } else {
685                 p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_SET;
686                 p_parser->keys[idx].val =
687                         (__u32)((const struct rte_flow_action_set_ttl *)
688                          actions->conf)->ttl_value;
689         }
690         p_parser->sel.nkeys = (++idx);
691 }
692
693 /**
694  * Set pedit key of transport (TCP/UDP) port value
695  *
696  * @param[in] actions
697  *   pointer to action specification
698  * @param[in,out] p_parser
699  *   pointer to pedit_parser
700  * @param[in] item_flags
701  *   flags of all items presented
702  */
703 static void
704 flow_tcf_pedit_key_set_tp_port(const struct rte_flow_action *actions,
705                                 struct pedit_parser *p_parser,
706                                 uint64_t item_flags)
707 {
708         int idx = p_parser->sel.nkeys;
709
710         if (item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP)
711                 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_UDP;
712         if (item_flags & MLX5_FLOW_LAYER_OUTER_L4_TCP)
713                 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_TCP;
714         p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_SET;
715         /* offset of src/dst port is same for TCP and UDP */
716         p_parser->keys[idx].off =
717                 actions->type == RTE_FLOW_ACTION_TYPE_SET_TP_SRC ?
718                 offsetof(struct tcp_hdr, src_port) :
719                 offsetof(struct tcp_hdr, dst_port);
720         p_parser->keys[idx].mask = 0xFFFF0000;
721         p_parser->keys[idx].val =
722                 (__u32)((const struct rte_flow_action_set_tp *)
723                                 actions->conf)->port;
724         p_parser->sel.nkeys = (++idx);
725 }
726
727 /**
728  * Set pedit key of ipv6 address
729  *
730  * @param[in] actions
731  *   pointer to action specification
732  * @param[in,out] p_parser
733  *   pointer to pedit_parser
734  */
735 static void
736 flow_tcf_pedit_key_set_ipv6_addr(const struct rte_flow_action *actions,
737                                  struct pedit_parser *p_parser)
738 {
739         int idx = p_parser->sel.nkeys;
740         int keys = NUM_OF_PEDIT_KEYS(IPV6_ADDR_LEN);
741         int off_base =
742                 actions->type == RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC ?
743                 offsetof(struct ipv6_hdr, src_addr) :
744                 offsetof(struct ipv6_hdr, dst_addr);
745         const struct rte_flow_action_set_ipv6 *conf =
746                 (const struct rte_flow_action_set_ipv6 *)actions->conf;
747
748         for (int i = 0; i < keys; i++, idx++) {
749                 p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_IP6;
750                 p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_SET;
751                 p_parser->keys[idx].off = off_base + i * SZ_PEDIT_KEY_VAL;
752                 p_parser->keys[idx].mask = ~UINT32_MAX;
753                 memcpy(&p_parser->keys[idx].val,
754                         conf->ipv6_addr + i *  SZ_PEDIT_KEY_VAL,
755                         SZ_PEDIT_KEY_VAL);
756         }
757         p_parser->sel.nkeys += keys;
758 }
759
760 /**
761  * Set pedit key of ipv4 address
762  *
763  * @param[in] actions
764  *   pointer to action specification
765  * @param[in,out] p_parser
766  *   pointer to pedit_parser
767  */
768 static void
769 flow_tcf_pedit_key_set_ipv4_addr(const struct rte_flow_action *actions,
770                                  struct pedit_parser *p_parser)
771 {
772         int idx = p_parser->sel.nkeys;
773
774         p_parser->keys_ex[idx].htype = TCA_PEDIT_KEY_EX_HDR_TYPE_IP4;
775         p_parser->keys_ex[idx].cmd = TCA_PEDIT_KEY_EX_CMD_SET;
776         p_parser->keys[idx].off =
777                 actions->type == RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC ?
778                 offsetof(struct ipv4_hdr, src_addr) :
779                 offsetof(struct ipv4_hdr, dst_addr);
780         p_parser->keys[idx].mask = ~UINT32_MAX;
781         p_parser->keys[idx].val =
782                 ((const struct rte_flow_action_set_ipv4 *)
783                  actions->conf)->ipv4_addr;
784         p_parser->sel.nkeys = (++idx);
785 }
786
787 /**
788  * Create the pedit's na attribute in netlink message
789  * on pre-allocate message buffer
790  *
791  * @param[in,out] nl
792  *   pointer to pre-allocated netlink message buffer
793  * @param[in,out] actions
794  *   pointer to pointer of actions specification.
795  * @param[in,out] action_flags
796  *   pointer to actions flags
797  * @param[in] item_flags
798  *   flags of all item presented
799  */
800 static void
801 flow_tcf_create_pedit_mnl_msg(struct nlmsghdr *nl,
802                               const struct rte_flow_action **actions,
803                               uint64_t item_flags)
804 {
805         struct pedit_parser p_parser;
806         struct nlattr *na_act_options;
807         struct nlattr *na_pedit_keys;
808
809         memset(&p_parser, 0, sizeof(p_parser));
810         mnl_attr_put_strz(nl, TCA_ACT_KIND, "pedit");
811         na_act_options = mnl_attr_nest_start(nl, TCA_ACT_OPTIONS);
812         /* all modify header actions should be in one tc-pedit action */
813         for (; (*actions)->type != RTE_FLOW_ACTION_TYPE_END; (*actions)++) {
814                 switch ((*actions)->type) {
815                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
816                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
817                         flow_tcf_pedit_key_set_ipv4_addr(*actions, &p_parser);
818                         break;
819                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
820                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
821                         flow_tcf_pedit_key_set_ipv6_addr(*actions, &p_parser);
822                         break;
823                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
824                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
825                         flow_tcf_pedit_key_set_tp_port(*actions,
826                                                         &p_parser, item_flags);
827                         break;
828                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
829                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
830                         flow_tcf_pedit_key_set_dec_ttl(*actions,
831                                                         &p_parser, item_flags);
832                         break;
833                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
834                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
835                         flow_tcf_pedit_key_set_mac(*actions, &p_parser);
836                         break;
837                 default:
838                         goto pedit_mnl_msg_done;
839                 }
840         }
841 pedit_mnl_msg_done:
842         p_parser.sel.action = TC_ACT_PIPE;
843         mnl_attr_put(nl, TCA_PEDIT_PARMS_EX,
844                      sizeof(p_parser.sel) +
845                      p_parser.sel.nkeys * sizeof(struct tc_pedit_key),
846                      &p_parser);
847         na_pedit_keys =
848                 mnl_attr_nest_start(nl, TCA_PEDIT_KEYS_EX | NLA_F_NESTED);
849         for (int i = 0; i < p_parser.sel.nkeys; i++) {
850                 struct nlattr *na_pedit_key =
851                         mnl_attr_nest_start(nl,
852                                             TCA_PEDIT_KEY_EX | NLA_F_NESTED);
853                 mnl_attr_put_u16(nl, TCA_PEDIT_KEY_EX_HTYPE,
854                                  p_parser.keys_ex[i].htype);
855                 mnl_attr_put_u16(nl, TCA_PEDIT_KEY_EX_CMD,
856                                  p_parser.keys_ex[i].cmd);
857                 mnl_attr_nest_end(nl, na_pedit_key);
858         }
859         mnl_attr_nest_end(nl, na_pedit_keys);
860         mnl_attr_nest_end(nl, na_act_options);
861         (*actions)--;
862 }
863
864 /**
865  * Calculate max memory size of one TC-pedit actions.
866  * One TC-pedit action can contain set of keys each defining
867  * a rewrite element (rte_flow action)
868  *
869  * @param[in,out] actions
870  *   actions specification.
871  * @param[in,out] action_flags
872  *   actions flags
873  * @param[in,out] size
874  *   accumulated size
875  * @return
876  *   Max memory size of one TC-pedit action
877  */
878 static int
879 flow_tcf_get_pedit_actions_size(const struct rte_flow_action **actions,
880                                 uint64_t *action_flags)
881 {
882         int pedit_size = 0;
883         int keys = 0;
884         uint64_t flags = 0;
885
886         pedit_size += SZ_NLATTR_NEST + /* na_act_index. */
887                       SZ_NLATTR_STRZ_OF("pedit") +
888                       SZ_NLATTR_NEST; /* TCA_ACT_OPTIONS. */
889         for (; (*actions)->type != RTE_FLOW_ACTION_TYPE_END; (*actions)++) {
890                 switch ((*actions)->type) {
891                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
892                         keys += NUM_OF_PEDIT_KEYS(IPV4_ADDR_LEN);
893                         flags |= MLX5_FLOW_ACTION_SET_IPV4_SRC;
894                         break;
895                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
896                         keys += NUM_OF_PEDIT_KEYS(IPV4_ADDR_LEN);
897                         flags |= MLX5_FLOW_ACTION_SET_IPV4_DST;
898                         break;
899                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
900                         keys += NUM_OF_PEDIT_KEYS(IPV6_ADDR_LEN);
901                         flags |= MLX5_FLOW_ACTION_SET_IPV6_SRC;
902                         break;
903                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
904                         keys += NUM_OF_PEDIT_KEYS(IPV6_ADDR_LEN);
905                         flags |= MLX5_FLOW_ACTION_SET_IPV6_DST;
906                         break;
907                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
908                         /* TCP is as same as UDP */
909                         keys += NUM_OF_PEDIT_KEYS(TP_PORT_LEN);
910                         flags |= MLX5_FLOW_ACTION_SET_TP_SRC;
911                         break;
912                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
913                         /* TCP is as same as UDP */
914                         keys += NUM_OF_PEDIT_KEYS(TP_PORT_LEN);
915                         flags |= MLX5_FLOW_ACTION_SET_TP_DST;
916                         break;
917                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
918                         keys += NUM_OF_PEDIT_KEYS(TTL_LEN);
919                         flags |= MLX5_FLOW_ACTION_SET_TTL;
920                         break;
921                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
922                         keys += NUM_OF_PEDIT_KEYS(TTL_LEN);
923                         flags |= MLX5_FLOW_ACTION_DEC_TTL;
924                         break;
925                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
926                         keys += NUM_OF_PEDIT_KEYS(ETHER_ADDR_LEN);
927                         flags |= MLX5_FLOW_ACTION_SET_MAC_SRC;
928                         break;
929                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
930                         keys += NUM_OF_PEDIT_KEYS(ETHER_ADDR_LEN);
931                         flags |= MLX5_FLOW_ACTION_SET_MAC_DST;
932                         break;
933                 default:
934                         goto get_pedit_action_size_done;
935                 }
936         }
937 get_pedit_action_size_done:
938         /* TCA_PEDIT_PARAMS_EX */
939         pedit_size +=
940                 SZ_NLATTR_DATA_OF(sizeof(struct tc_pedit_sel) +
941                                   keys * sizeof(struct tc_pedit_key));
942         pedit_size += SZ_NLATTR_NEST; /* TCA_PEDIT_KEYS */
943         pedit_size += keys *
944                       /* TCA_PEDIT_KEY_EX + HTYPE + CMD */
945                       (SZ_NLATTR_NEST + SZ_NLATTR_DATA_OF(2) +
946                        SZ_NLATTR_DATA_OF(2));
947         (*action_flags) |= flags;
948         (*actions)--;
949         return pedit_size;
950 }
951
952 /**
953  * Retrieve mask for pattern item.
954  *
955  * This function does basic sanity checks on a pattern item in order to
956  * return the most appropriate mask for it.
957  *
958  * @param[in] item
959  *   Item specification.
960  * @param[in] mask_default
961  *   Default mask for pattern item as specified by the flow API.
962  * @param[in] mask_supported
963  *   Mask fields supported by the implementation.
964  * @param[in] mask_empty
965  *   Empty mask to return when there is no specification.
966  * @param[out] error
967  *   Perform verbose error reporting if not NULL.
968  *
969  * @return
970  *   Either @p item->mask or one of the mask parameters on success, NULL
971  *   otherwise and rte_errno is set.
972  */
973 static const void *
974 flow_tcf_item_mask(const struct rte_flow_item *item, const void *mask_default,
975                    const void *mask_supported, const void *mask_empty,
976                    size_t mask_size, struct rte_flow_error *error)
977 {
978         const uint8_t *mask;
979         size_t i;
980
981         /* item->last and item->mask cannot exist without item->spec. */
982         if (!item->spec && (item->mask || item->last)) {
983                 rte_flow_error_set(error, EINVAL,
984                                    RTE_FLOW_ERROR_TYPE_ITEM, item,
985                                    "\"mask\" or \"last\" field provided without"
986                                    " a corresponding \"spec\"");
987                 return NULL;
988         }
989         /* No spec, no mask, no problem. */
990         if (!item->spec)
991                 return mask_empty;
992         mask = item->mask ? item->mask : mask_default;
993         assert(mask);
994         /*
995          * Single-pass check to make sure that:
996          * - Mask is supported, no bits are set outside mask_supported.
997          * - Both item->spec and item->last are included in mask.
998          */
999         for (i = 0; i != mask_size; ++i) {
1000                 if (!mask[i])
1001                         continue;
1002                 if ((mask[i] | ((const uint8_t *)mask_supported)[i]) !=
1003                     ((const uint8_t *)mask_supported)[i]) {
1004                         rte_flow_error_set(error, ENOTSUP,
1005                                            RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1006                                            "unsupported field found"
1007                                            " in \"mask\"");
1008                         return NULL;
1009                 }
1010                 if (item->last &&
1011                     (((const uint8_t *)item->spec)[i] & mask[i]) !=
1012                     (((const uint8_t *)item->last)[i] & mask[i])) {
1013                         rte_flow_error_set(error, EINVAL,
1014                                            RTE_FLOW_ERROR_TYPE_ITEM_LAST,
1015                                            item->last,
1016                                            "range between \"spec\" and \"last\""
1017                                            " not comprised in \"mask\"");
1018                         return NULL;
1019                 }
1020         }
1021         return mask;
1022 }
1023
1024 /**
1025  * Build a conversion table between port ID and ifindex.
1026  *
1027  * @param[in] dev
1028  *   Pointer to Ethernet device.
1029  * @param[out] ptoi
1030  *   Pointer to ptoi table.
1031  * @param[in] len
1032  *   Size of ptoi table provided.
1033  *
1034  * @return
1035  *   Size of ptoi table filled.
1036  */
1037 static unsigned int
1038 flow_tcf_build_ptoi_table(struct rte_eth_dev *dev, struct flow_tcf_ptoi *ptoi,
1039                           unsigned int len)
1040 {
1041         unsigned int n = mlx5_dev_to_port_id(dev->device, NULL, 0);
1042         uint16_t port_id[n + 1];
1043         unsigned int i;
1044         unsigned int own = 0;
1045
1046         /* At least one port is needed when no switch domain is present. */
1047         if (!n) {
1048                 n = 1;
1049                 port_id[0] = dev->data->port_id;
1050         } else {
1051                 n = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, n), n);
1052         }
1053         if (n > len)
1054                 return 0;
1055         for (i = 0; i != n; ++i) {
1056                 struct rte_eth_dev_info dev_info;
1057
1058                 rte_eth_dev_info_get(port_id[i], &dev_info);
1059                 if (port_id[i] == dev->data->port_id)
1060                         own = i;
1061                 ptoi[i].port_id = port_id[i];
1062                 ptoi[i].ifindex = dev_info.if_index;
1063         }
1064         /* Ensure first entry of ptoi[] is the current device. */
1065         if (own) {
1066                 ptoi[n] = ptoi[0];
1067                 ptoi[0] = ptoi[own];
1068                 ptoi[own] = ptoi[n];
1069         }
1070         /* An entry with zero ifindex terminates ptoi[]. */
1071         ptoi[n].port_id = 0;
1072         ptoi[n].ifindex = 0;
1073         return n;
1074 }
1075
1076 /**
1077  * Verify the @p attr will be correctly understood by the E-switch.
1078  *
1079  * @param[in] attr
1080  *   Pointer to flow attributes
1081  * @param[out] error
1082  *   Pointer to error structure.
1083  *
1084  * @return
1085  *   0 on success, a negative errno value otherwise and rte_errno is set.
1086  */
1087 static int
1088 flow_tcf_validate_attributes(const struct rte_flow_attr *attr,
1089                              struct rte_flow_error *error)
1090 {
1091         /*
1092          * Supported attributes: groups, some priorities and ingress only.
1093          * group is supported only if kernel supports chain. Don't care about
1094          * transfer as it is the caller's problem.
1095          */
1096         if (attr->group > MLX5_TCF_GROUP_ID_MAX)
1097                 return rte_flow_error_set(error, ENOTSUP,
1098                                           RTE_FLOW_ERROR_TYPE_ATTR_GROUP, attr,
1099                                           "group ID larger than "
1100                                           RTE_STR(MLX5_TCF_GROUP_ID_MAX)
1101                                           " isn't supported");
1102         else if (attr->priority > MLX5_TCF_GROUP_PRIORITY_MAX)
1103                 return rte_flow_error_set(error, ENOTSUP,
1104                                           RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1105                                           attr,
1106                                           "priority more than "
1107                                           RTE_STR(MLX5_TCF_GROUP_PRIORITY_MAX)
1108                                           " is not supported");
1109         if (!attr->ingress)
1110                 return rte_flow_error_set(error, EINVAL,
1111                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1112                                           attr, "only ingress is supported");
1113         if (attr->egress)
1114                 return rte_flow_error_set(error, ENOTSUP,
1115                                           RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
1116                                           attr, "egress is not supported");
1117         return 0;
1118 }
1119
1120 /**
1121  * Validate VXLAN_ENCAP action RTE_FLOW_ITEM_TYPE_ETH item for E-Switch.
1122  * The routine checks the L2 fields to be used in encapsulation header.
1123  *
1124  * @param[in] item
1125  *   Pointer to the item structure.
1126  * @param[out] error
1127  *   Pointer to the error structure.
1128  *
1129  * @return
1130  *   0 on success, a negative errno value otherwise and rte_errno is set.
1131  **/
1132 static int
1133 flow_tcf_validate_vxlan_encap_eth(const struct rte_flow_item *item,
1134                                   struct rte_flow_error *error)
1135 {
1136         const struct rte_flow_item_eth *spec = item->spec;
1137         const struct rte_flow_item_eth *mask = item->mask;
1138
1139         if (!spec) {
1140                 /*
1141                  * Specification for L2 addresses can be empty
1142                  * because these ones are optional and not
1143                  * required directly by tc rule. Kernel tries
1144                  * to resolve these ones on its own
1145                  */
1146                 return 0;
1147         }
1148         if (!mask) {
1149                 /* If mask is not specified use the default one. */
1150                 mask = &rte_flow_item_eth_mask;
1151         }
1152         if (memcmp(&mask->dst,
1153                    &flow_tcf_mask_empty.eth.dst,
1154                    sizeof(flow_tcf_mask_empty.eth.dst))) {
1155                 if (memcmp(&mask->dst,
1156                            &rte_flow_item_eth_mask.dst,
1157                            sizeof(rte_flow_item_eth_mask.dst)))
1158                         return rte_flow_error_set
1159                                 (error, ENOTSUP,
1160                                  RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1161                                  "no support for partial mask on"
1162                                  " \"eth.dst\" field");
1163         }
1164         if (memcmp(&mask->src,
1165                    &flow_tcf_mask_empty.eth.src,
1166                    sizeof(flow_tcf_mask_empty.eth.src))) {
1167                 if (memcmp(&mask->src,
1168                            &rte_flow_item_eth_mask.src,
1169                            sizeof(rte_flow_item_eth_mask.src)))
1170                         return rte_flow_error_set
1171                                 (error, ENOTSUP,
1172                                  RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1173                                  "no support for partial mask on"
1174                                  " \"eth.src\" field");
1175         }
1176         if (mask->type != RTE_BE16(0x0000)) {
1177                 if (mask->type != RTE_BE16(0xffff))
1178                         return rte_flow_error_set
1179                                 (error, ENOTSUP,
1180                                  RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1181                                  "no support for partial mask on"
1182                                  " \"eth.type\" field");
1183                 DRV_LOG(WARNING,
1184                         "outer ethernet type field"
1185                         " cannot be forced for vxlan"
1186                         " encapsulation, parameter ignored");
1187         }
1188         return 0;
1189 }
1190
1191 /**
1192  * Validate VXLAN_ENCAP action RTE_FLOW_ITEM_TYPE_IPV4 item for E-Switch.
1193  * The routine checks the IPv4 fields to be used in encapsulation header.
1194  *
1195  * @param[in] item
1196  *   Pointer to the item structure.
1197  * @param[out] error
1198  *   Pointer to the error structure.
1199  *
1200  * @return
1201  *   0 on success, a negative errno value otherwise and rte_errno is set.
1202  **/
1203 static int
1204 flow_tcf_validate_vxlan_encap_ipv4(const struct rte_flow_item *item,
1205                                    struct rte_flow_error *error)
1206 {
1207         const struct rte_flow_item_ipv4 *spec = item->spec;
1208         const struct rte_flow_item_ipv4 *mask = item->mask;
1209
1210         if (!spec) {
1211                 /*
1212                  * Specification for IP addresses cannot be empty
1213                  * because it is required by tunnel_key parameter.
1214                  */
1215                 return rte_flow_error_set(error, EINVAL,
1216                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1217                                           "NULL outer ipv4 address"
1218                                           " specification for vxlan"
1219                                           " encapsulation");
1220         }
1221         if (!mask)
1222                 mask = &rte_flow_item_ipv4_mask;
1223         if (mask->hdr.dst_addr != RTE_BE32(0x00000000)) {
1224                 if (mask->hdr.dst_addr != RTE_BE32(0xffffffff))
1225                         return rte_flow_error_set
1226                                 (error, ENOTSUP,
1227                                  RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1228                                  "no support for partial mask on"
1229                                  " \"ipv4.hdr.dst_addr\" field"
1230                                  " for vxlan encapsulation");
1231                 /* More IPv4 address validations can be put here. */
1232         } else {
1233                 /*
1234                  * Kernel uses the destination IP address to determine
1235                  * the routing path and obtain the MAC destination
1236                  * address, so IP destination address must be
1237                  * specified in the tc rule.
1238                  */
1239                 return rte_flow_error_set(error, EINVAL,
1240                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1241                                           "outer ipv4 destination address"
1242                                           " must be specified for"
1243                                           " vxlan encapsulation");
1244         }
1245         if (mask->hdr.src_addr != RTE_BE32(0x00000000)) {
1246                 if (mask->hdr.src_addr != RTE_BE32(0xffffffff))
1247                         return rte_flow_error_set
1248                                 (error, ENOTSUP,
1249                                  RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1250                                  "no support for partial mask on"
1251                                  " \"ipv4.hdr.src_addr\" field"
1252                                  " for vxlan encapsulation");
1253                 /* More IPv4 address validations can be put here. */
1254         } else {
1255                 /*
1256                  * Kernel uses the source IP address to select the
1257                  * interface for egress encapsulated traffic, so
1258                  * it must be specified in the tc rule.
1259                  */
1260                 return rte_flow_error_set(error, EINVAL,
1261                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1262                                           "outer ipv4 source address"
1263                                           " must be specified for"
1264                                           " vxlan encapsulation");
1265         }
1266         return 0;
1267 }
1268
1269 /**
1270  * Validate VXLAN_ENCAP action RTE_FLOW_ITEM_TYPE_IPV6 item for E-Switch.
1271  * The routine checks the IPv6 fields to be used in encapsulation header.
1272  *
1273  * @param[in] item
1274  *   Pointer to the item structure.
1275  * @param[out] error
1276  *   Pointer to the error structure.
1277  *
1278  * @return
1279  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1280  **/
1281 static int
1282 flow_tcf_validate_vxlan_encap_ipv6(const struct rte_flow_item *item,
1283                                    struct rte_flow_error *error)
1284 {
1285         const struct rte_flow_item_ipv6 *spec = item->spec;
1286         const struct rte_flow_item_ipv6 *mask = item->mask;
1287
1288         if (!spec) {
1289                 /*
1290                  * Specification for IP addresses cannot be empty
1291                  * because it is required by tunnel_key parameter.
1292                  */
1293                 return rte_flow_error_set(error, EINVAL,
1294                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1295                                           "NULL outer ipv6 address"
1296                                           " specification for"
1297                                           " vxlan encapsulation");
1298         }
1299         if (!mask)
1300                 mask = &rte_flow_item_ipv6_mask;
1301         if (memcmp(&mask->hdr.dst_addr,
1302                    &flow_tcf_mask_empty.ipv6.hdr.dst_addr,
1303                    IPV6_ADDR_LEN)) {
1304                 if (memcmp(&mask->hdr.dst_addr,
1305                            &rte_flow_item_ipv6_mask.hdr.dst_addr,
1306                            IPV6_ADDR_LEN))
1307                         return rte_flow_error_set
1308                                         (error, ENOTSUP,
1309                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1310                                          "no support for partial mask on"
1311                                          " \"ipv6.hdr.dst_addr\" field"
1312                                          " for vxlan encapsulation");
1313                 /* More IPv6 address validations can be put here. */
1314         } else {
1315                 /*
1316                  * Kernel uses the destination IP address to determine
1317                  * the routing path and obtain the MAC destination
1318                  * address (heigh or gate), so IP destination address
1319                  * must be specified within the tc rule.
1320                  */
1321                 return rte_flow_error_set(error, EINVAL,
1322                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1323                                           "outer ipv6 destination address"
1324                                           " must be specified for"
1325                                           " vxlan encapsulation");
1326         }
1327         if (memcmp(&mask->hdr.src_addr,
1328                    &flow_tcf_mask_empty.ipv6.hdr.src_addr,
1329                    IPV6_ADDR_LEN)) {
1330                 if (memcmp(&mask->hdr.src_addr,
1331                            &rte_flow_item_ipv6_mask.hdr.src_addr,
1332                            IPV6_ADDR_LEN))
1333                         return rte_flow_error_set
1334                                         (error, ENOTSUP,
1335                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1336                                          "no support for partial mask on"
1337                                          " \"ipv6.hdr.src_addr\" field"
1338                                          " for vxlan encapsulation");
1339                 /* More L3 address validation can be put here. */
1340         } else {
1341                 /*
1342                  * Kernel uses the source IP address to select the
1343                  * interface for egress encapsulated traffic, so
1344                  * it must be specified in the tc rule.
1345                  */
1346                 return rte_flow_error_set(error, EINVAL,
1347                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1348                                           "outer L3 source address"
1349                                           " must be specified for"
1350                                           " vxlan encapsulation");
1351         }
1352         return 0;
1353 }
1354
1355 /**
1356  * Validate VXLAN_ENCAP action RTE_FLOW_ITEM_TYPE_UDP item for E-Switch.
1357  * The routine checks the UDP fields to be used in encapsulation header.
1358  *
1359  * @param[in] item
1360  *   Pointer to the item structure.
1361  * @param[out] error
1362  *   Pointer to the error structure.
1363  *
1364  * @return
1365  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1366  **/
1367 static int
1368 flow_tcf_validate_vxlan_encap_udp(const struct rte_flow_item *item,
1369                                   struct rte_flow_error *error)
1370 {
1371         const struct rte_flow_item_udp *spec = item->spec;
1372         const struct rte_flow_item_udp *mask = item->mask;
1373
1374         if (!spec) {
1375                 /*
1376                  * Specification for UDP ports cannot be empty
1377                  * because it is required by tunnel_key parameter.
1378                  */
1379                 return rte_flow_error_set(error, EINVAL,
1380                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1381                                           "NULL UDP port specification "
1382                                           " for vxlan encapsulation");
1383         }
1384         if (!mask)
1385                 mask = &rte_flow_item_udp_mask;
1386         if (mask->hdr.dst_port != RTE_BE16(0x0000)) {
1387                 if (mask->hdr.dst_port != RTE_BE16(0xffff))
1388                         return rte_flow_error_set
1389                                         (error, ENOTSUP,
1390                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1391                                          "no support for partial mask on"
1392                                          " \"udp.hdr.dst_port\" field"
1393                                          " for vxlan encapsulation");
1394                 if (!spec->hdr.dst_port)
1395                         return rte_flow_error_set
1396                                         (error, EINVAL,
1397                                          RTE_FLOW_ERROR_TYPE_ITEM, item,
1398                                          "outer UDP remote port cannot be"
1399                                          " 0 for vxlan encapsulation");
1400         } else {
1401                 return rte_flow_error_set(error, EINVAL,
1402                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1403                                           "outer UDP remote port"
1404                                           " must be specified for"
1405                                           " vxlan encapsulation");
1406         }
1407         if (mask->hdr.src_port != RTE_BE16(0x0000)) {
1408                 if (mask->hdr.src_port != RTE_BE16(0xffff))
1409                         return rte_flow_error_set
1410                                         (error, ENOTSUP,
1411                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1412                                          "no support for partial mask on"
1413                                          " \"udp.hdr.src_port\" field"
1414                                          " for vxlan encapsulation");
1415                 DRV_LOG(WARNING,
1416                         "outer UDP source port cannot be"
1417                         " forced for vxlan encapsulation,"
1418                         " parameter ignored");
1419         }
1420         return 0;
1421 }
1422
1423 /**
1424  * Validate VXLAN_ENCAP action RTE_FLOW_ITEM_TYPE_VXLAN item for E-Switch.
1425  * The routine checks the VNIP fields to be used in encapsulation header.
1426  *
1427  * @param[in] item
1428  *   Pointer to the item structure.
1429  * @param[out] error
1430  *   Pointer to the error structure.
1431  *
1432  * @return
1433  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1434  **/
1435 static int
1436 flow_tcf_validate_vxlan_encap_vni(const struct rte_flow_item *item,
1437                                   struct rte_flow_error *error)
1438 {
1439         const struct rte_flow_item_vxlan *spec = item->spec;
1440         const struct rte_flow_item_vxlan *mask = item->mask;
1441
1442         if (!spec) {
1443                 /* Outer VNI is required by tunnel_key parameter. */
1444                 return rte_flow_error_set(error, EINVAL,
1445                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1446                                           "NULL VNI specification"
1447                                           " for vxlan encapsulation");
1448         }
1449         if (!mask)
1450                 mask = &rte_flow_item_vxlan_mask;
1451         if (!mask->vni[0] && !mask->vni[1] && !mask->vni[2])
1452                 return rte_flow_error_set(error, EINVAL,
1453                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1454                                           "outer VNI must be specified "
1455                                           "for vxlan encapsulation");
1456         if (mask->vni[0] != 0xff ||
1457             mask->vni[1] != 0xff ||
1458             mask->vni[2] != 0xff)
1459                 return rte_flow_error_set(error, ENOTSUP,
1460                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1461                                           "no support for partial mask on"
1462                                           " \"vxlan.vni\" field");
1463
1464         if (!spec->vni[0] && !spec->vni[1] && !spec->vni[2])
1465                 return rte_flow_error_set(error, EINVAL,
1466                                           RTE_FLOW_ERROR_TYPE_ITEM, item,
1467                                           "vxlan vni cannot be 0");
1468         return 0;
1469 }
1470
1471 /**
1472  * Validate VXLAN_ENCAP action item list for E-Switch.
1473  * The routine checks items to be used in encapsulation header.
1474  *
1475  * @param[in] action
1476  *   Pointer to the VXLAN_ENCAP action structure.
1477  * @param[out] error
1478  *   Pointer to the error structure.
1479  *
1480  * @return
1481  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1482  **/
1483 static int
1484 flow_tcf_validate_vxlan_encap(const struct rte_flow_action *action,
1485                               struct rte_flow_error *error)
1486 {
1487         const struct rte_flow_item *items;
1488         int ret;
1489         uint32_t item_flags = 0;
1490
1491         if (!action->conf)
1492                 return rte_flow_error_set(error, EINVAL,
1493                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1494                                           "Missing vxlan tunnel"
1495                                           " action configuration");
1496         items = ((const struct rte_flow_action_vxlan_encap *)
1497                                         action->conf)->definition;
1498         if (!items)
1499                 return rte_flow_error_set(error, EINVAL,
1500                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1501                                           "Missing vxlan tunnel"
1502                                           " encapsulation parameters");
1503         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1504                 switch (items->type) {
1505                 case RTE_FLOW_ITEM_TYPE_VOID:
1506                         break;
1507                 case RTE_FLOW_ITEM_TYPE_ETH:
1508                         ret = mlx5_flow_validate_item_eth(items, item_flags,
1509                                                           error);
1510                         if (ret < 0)
1511                                 return ret;
1512                         ret = flow_tcf_validate_vxlan_encap_eth(items, error);
1513                         if (ret < 0)
1514                                 return ret;
1515                         item_flags |= MLX5_FLOW_LAYER_OUTER_L2;
1516                         break;
1517                 break;
1518                 case RTE_FLOW_ITEM_TYPE_IPV4:
1519                         ret = mlx5_flow_validate_item_ipv4(items, item_flags,
1520                                                            error);
1521                         if (ret < 0)
1522                                 return ret;
1523                         ret = flow_tcf_validate_vxlan_encap_ipv4(items, error);
1524                         if (ret < 0)
1525                                 return ret;
1526                         item_flags |= MLX5_FLOW_LAYER_OUTER_L3_IPV4;
1527                         break;
1528                 case RTE_FLOW_ITEM_TYPE_IPV6:
1529                         ret = mlx5_flow_validate_item_ipv6(items, item_flags,
1530                                                            error);
1531                         if (ret < 0)
1532                                 return ret;
1533                         ret = flow_tcf_validate_vxlan_encap_ipv6(items, error);
1534                         if (ret < 0)
1535                                 return ret;
1536                         item_flags |= MLX5_FLOW_LAYER_OUTER_L3_IPV6;
1537                         break;
1538                 case RTE_FLOW_ITEM_TYPE_UDP:
1539                         ret = mlx5_flow_validate_item_udp(items, item_flags,
1540                                                            0xFF, error);
1541                         if (ret < 0)
1542                                 return ret;
1543                         ret = flow_tcf_validate_vxlan_encap_udp(items, error);
1544                         if (ret < 0)
1545                                 return ret;
1546                         item_flags |= MLX5_FLOW_LAYER_OUTER_L4_UDP;
1547                         break;
1548                 case RTE_FLOW_ITEM_TYPE_VXLAN:
1549                         ret = mlx5_flow_validate_item_vxlan(items,
1550                                                             item_flags, error);
1551                         if (ret < 0)
1552                                 return ret;
1553                         ret = flow_tcf_validate_vxlan_encap_vni(items, error);
1554                         if (ret < 0)
1555                                 return ret;
1556                         item_flags |= MLX5_FLOW_LAYER_VXLAN;
1557                         break;
1558                 default:
1559                         return rte_flow_error_set
1560                                         (error, ENOTSUP,
1561                                          RTE_FLOW_ERROR_TYPE_ITEM, items,
1562                                          "vxlan encap item not supported");
1563                 }
1564         }
1565         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3))
1566                 return rte_flow_error_set(error, EINVAL,
1567                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1568                                           "no outer IP layer found"
1569                                           " for vxlan encapsulation");
1570         if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
1571                 return rte_flow_error_set(error, EINVAL,
1572                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1573                                           "no outer UDP layer found"
1574                                           " for vxlan encapsulation");
1575         if (!(item_flags & MLX5_FLOW_LAYER_VXLAN))
1576                 return rte_flow_error_set(error, EINVAL,
1577                                           RTE_FLOW_ERROR_TYPE_ACTION, action,
1578                                           "no VXLAN VNI found"
1579                                           " for vxlan encapsulation");
1580         return 0;
1581 }
1582
1583 /**
1584  * Validate RTE_FLOW_ITEM_TYPE_IPV4 item if VXLAN_DECAP action
1585  * is present in actions list.
1586  *
1587  * @param[in] ipv4
1588  *   Outer IPv4 address item (if any, NULL otherwise).
1589  * @param[out] error
1590  *   Pointer to the error structure.
1591  *
1592  * @return
1593  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1594  **/
1595 static int
1596 flow_tcf_validate_vxlan_decap_ipv4(const struct rte_flow_item *ipv4,
1597                                    struct rte_flow_error *error)
1598 {
1599         const struct rte_flow_item_ipv4 *spec = ipv4->spec;
1600         const struct rte_flow_item_ipv4 *mask = ipv4->mask;
1601
1602         if (!spec) {
1603                 /*
1604                  * Specification for IP addresses cannot be empty
1605                  * because it is required as decap parameter.
1606                  */
1607                 return rte_flow_error_set(error, EINVAL,
1608                                           RTE_FLOW_ERROR_TYPE_ITEM, ipv4,
1609                                           "NULL outer ipv4 address"
1610                                           " specification for vxlan"
1611                                           " for vxlan decapsulation");
1612         }
1613         if (!mask)
1614                 mask = &rte_flow_item_ipv4_mask;
1615         if (mask->hdr.dst_addr != RTE_BE32(0x00000000)) {
1616                 if (mask->hdr.dst_addr != RTE_BE32(0xffffffff))
1617                         return rte_flow_error_set
1618                                         (error, ENOTSUP,
1619                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1620                                          "no support for partial mask on"
1621                                          " \"ipv4.hdr.dst_addr\" field");
1622                 /* More IP address validations can be put here. */
1623         } else {
1624                 /*
1625                  * Kernel uses the destination IP address
1626                  * to determine the ingress network interface
1627                  * for traffic being decapsulated.
1628                  */
1629                 return rte_flow_error_set(error, EINVAL,
1630                                           RTE_FLOW_ERROR_TYPE_ITEM, ipv4,
1631                                           "outer ipv4 destination address"
1632                                           " must be specified for"
1633                                           " vxlan decapsulation");
1634         }
1635         /* Source IP address is optional for decap. */
1636         if (mask->hdr.src_addr != RTE_BE32(0x00000000) &&
1637             mask->hdr.src_addr != RTE_BE32(0xffffffff))
1638                 return rte_flow_error_set(error, ENOTSUP,
1639                                           RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1640                                           "no support for partial mask on"
1641                                           " \"ipv4.hdr.src_addr\" field");
1642         return 0;
1643 }
1644
1645 /**
1646  * Validate RTE_FLOW_ITEM_TYPE_IPV6 item if VXLAN_DECAP action
1647  * is present in actions list.
1648  *
1649  * @param[in] ipv6
1650  *   Outer IPv6 address item (if any, NULL otherwise).
1651  * @param[out] error
1652  *   Pointer to the error structure.
1653  *
1654  * @return
1655  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1656  **/
1657 static int
1658 flow_tcf_validate_vxlan_decap_ipv6(const struct rte_flow_item *ipv6,
1659                                    struct rte_flow_error *error)
1660 {
1661         const struct rte_flow_item_ipv6 *spec = ipv6->spec;
1662         const struct rte_flow_item_ipv6 *mask = ipv6->mask;
1663
1664         if (!spec) {
1665                 /*
1666                  * Specification for IP addresses cannot be empty
1667                  * because it is required as decap parameter.
1668                  */
1669                 return rte_flow_error_set(error, EINVAL,
1670                                           RTE_FLOW_ERROR_TYPE_ITEM, ipv6,
1671                                           "NULL outer ipv6 address"
1672                                           " specification for vxlan"
1673                                           " decapsulation");
1674         }
1675         if (!mask)
1676                 mask = &rte_flow_item_ipv6_mask;
1677         if (memcmp(&mask->hdr.dst_addr,
1678                    &flow_tcf_mask_empty.ipv6.hdr.dst_addr,
1679                    IPV6_ADDR_LEN)) {
1680                 if (memcmp(&mask->hdr.dst_addr,
1681                         &rte_flow_item_ipv6_mask.hdr.dst_addr,
1682                         IPV6_ADDR_LEN))
1683                         return rte_flow_error_set
1684                                         (error, ENOTSUP,
1685                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1686                                          "no support for partial mask on"
1687                                          " \"ipv6.hdr.dst_addr\" field");
1688                 /* More IP address validations can be put here. */
1689         } else {
1690                 /*
1691                  * Kernel uses the destination IP address
1692                  * to determine the ingress network interface
1693                  * for traffic being decapsulated.
1694                  */
1695                 return rte_flow_error_set(error, EINVAL,
1696                                           RTE_FLOW_ERROR_TYPE_ITEM, ipv6,
1697                                           "outer ipv6 destination address must be "
1698                                           "specified for vxlan decapsulation");
1699         }
1700         /* Source IP address is optional for decap. */
1701         if (memcmp(&mask->hdr.src_addr,
1702                    &flow_tcf_mask_empty.ipv6.hdr.src_addr,
1703                    IPV6_ADDR_LEN)) {
1704                 if (memcmp(&mask->hdr.src_addr,
1705                            &rte_flow_item_ipv6_mask.hdr.src_addr,
1706                            IPV6_ADDR_LEN))
1707                         return rte_flow_error_set
1708                                         (error, ENOTSUP,
1709                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1710                                          "no support for partial mask on"
1711                                          " \"ipv6.hdr.src_addr\" field");
1712         }
1713         return 0;
1714 }
1715
1716 /**
1717  * Validate RTE_FLOW_ITEM_TYPE_UDP item if VXLAN_DECAP action
1718  * is present in actions list.
1719  *
1720  * @param[in] udp
1721  *   Outer UDP layer item (if any, NULL otherwise).
1722  * @param[out] error
1723  *   Pointer to the error structure.
1724  *
1725  * @return
1726  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1727  **/
1728 static int
1729 flow_tcf_validate_vxlan_decap_udp(const struct rte_flow_item *udp,
1730                                   struct rte_flow_error *error)
1731 {
1732         const struct rte_flow_item_udp *spec = udp->spec;
1733         const struct rte_flow_item_udp *mask = udp->mask;
1734
1735         if (!spec)
1736                 /*
1737                  * Specification for UDP ports cannot be empty
1738                  * because it is required as decap parameter.
1739                  */
1740                 return rte_flow_error_set(error, EINVAL,
1741                                           RTE_FLOW_ERROR_TYPE_ITEM, udp,
1742                                           "NULL UDP port specification"
1743                                           " for VXLAN decapsulation");
1744         if (!mask)
1745                 mask = &rte_flow_item_udp_mask;
1746         if (mask->hdr.dst_port != RTE_BE16(0x0000)) {
1747                 if (mask->hdr.dst_port != RTE_BE16(0xffff))
1748                         return rte_flow_error_set
1749                                         (error, ENOTSUP,
1750                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1751                                          "no support for partial mask on"
1752                                          " \"udp.hdr.dst_port\" field");
1753                 if (!spec->hdr.dst_port)
1754                         return rte_flow_error_set
1755                                         (error, EINVAL,
1756                                          RTE_FLOW_ERROR_TYPE_ITEM, udp,
1757                                          "zero decap local UDP port");
1758         } else {
1759                 return rte_flow_error_set(error, EINVAL,
1760                                           RTE_FLOW_ERROR_TYPE_ITEM, udp,
1761                                           "outer UDP destination port must be "
1762                                           "specified for vxlan decapsulation");
1763         }
1764         if (mask->hdr.src_port != RTE_BE16(0x0000)) {
1765                 if (mask->hdr.src_port != RTE_BE16(0xffff))
1766                         return rte_flow_error_set
1767                                         (error, ENOTSUP,
1768                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK, mask,
1769                                          "no support for partial mask on"
1770                                          " \"udp.hdr.src_port\" field");
1771                 DRV_LOG(WARNING,
1772                         "outer UDP local port cannot be "
1773                         "forced for VXLAN encapsulation, "
1774                         "parameter ignored");
1775         }
1776         return 0;
1777 }
1778
1779 /**
1780  * Validate flow for E-Switch.
1781  *
1782  * @param[in] priv
1783  *   Pointer to the priv structure.
1784  * @param[in] attr
1785  *   Pointer to the flow attributes.
1786  * @param[in] items
1787  *   Pointer to the list of items.
1788  * @param[in] actions
1789  *   Pointer to the list of actions.
1790  * @param[out] error
1791  *   Pointer to the error structure.
1792  *
1793  * @return
1794  *   0 on success, a negative errno value otherwise and rte_ernno is set.
1795  */
1796 static int
1797 flow_tcf_validate(struct rte_eth_dev *dev,
1798                   const struct rte_flow_attr *attr,
1799                   const struct rte_flow_item items[],
1800                   const struct rte_flow_action actions[],
1801                   struct rte_flow_error *error)
1802 {
1803         union {
1804                 const struct rte_flow_item_port_id *port_id;
1805                 const struct rte_flow_item_eth *eth;
1806                 const struct rte_flow_item_vlan *vlan;
1807                 const struct rte_flow_item_ipv4 *ipv4;
1808                 const struct rte_flow_item_ipv6 *ipv6;
1809                 const struct rte_flow_item_tcp *tcp;
1810                 const struct rte_flow_item_udp *udp;
1811                 const struct rte_flow_item_vxlan *vxlan;
1812         } spec, mask;
1813         union {
1814                 const struct rte_flow_action_port_id *port_id;
1815                 const struct rte_flow_action_jump *jump;
1816                 const struct rte_flow_action_of_push_vlan *of_push_vlan;
1817                 const struct rte_flow_action_of_set_vlan_vid *
1818                         of_set_vlan_vid;
1819                 const struct rte_flow_action_of_set_vlan_pcp *
1820                         of_set_vlan_pcp;
1821                 const struct rte_flow_action_vxlan_encap *vxlan_encap;
1822                 const struct rte_flow_action_set_ipv4 *set_ipv4;
1823                 const struct rte_flow_action_set_ipv6 *set_ipv6;
1824         } conf;
1825         uint64_t item_flags = 0;
1826         uint64_t action_flags = 0;
1827         uint8_t next_protocol = -1;
1828         unsigned int tcm_ifindex = 0;
1829         uint8_t pedit_validated = 0;
1830         struct flow_tcf_ptoi ptoi[PTOI_TABLE_SZ_MAX(dev)];
1831         struct rte_eth_dev *port_id_dev = NULL;
1832         bool in_port_id_set;
1833         int ret;
1834
1835         claim_nonzero(flow_tcf_build_ptoi_table(dev, ptoi,
1836                                                 PTOI_TABLE_SZ_MAX(dev)));
1837         ret = flow_tcf_validate_attributes(attr, error);
1838         if (ret < 0)
1839                 return ret;
1840         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1841                 unsigned int i;
1842                 uint64_t current_action_flag = 0;
1843
1844                 switch (actions->type) {
1845                 case RTE_FLOW_ACTION_TYPE_VOID:
1846                         break;
1847                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
1848                         current_action_flag = MLX5_FLOW_ACTION_PORT_ID;
1849                         if (!actions->conf)
1850                                 break;
1851                         conf.port_id = actions->conf;
1852                         if (conf.port_id->original)
1853                                 i = 0;
1854                         else
1855                                 for (i = 0; ptoi[i].ifindex; ++i)
1856                                         if (ptoi[i].port_id == conf.port_id->id)
1857                                                 break;
1858                         if (!ptoi[i].ifindex)
1859                                 return rte_flow_error_set
1860                                         (error, ENODEV,
1861                                          RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1862                                          conf.port_id,
1863                                          "missing data to convert port ID to"
1864                                          " ifindex");
1865                         port_id_dev = &rte_eth_devices[conf.port_id->id];
1866                         break;
1867                 case RTE_FLOW_ACTION_TYPE_JUMP:
1868                         current_action_flag = MLX5_FLOW_ACTION_JUMP;
1869                         if (!actions->conf)
1870                                 break;
1871                         conf.jump = actions->conf;
1872                         if (attr->group >= conf.jump->group)
1873                                 return rte_flow_error_set
1874                                         (error, ENOTSUP,
1875                                          RTE_FLOW_ERROR_TYPE_ACTION,
1876                                          actions,
1877                                          "can jump only to a group forward");
1878                         break;
1879                 case RTE_FLOW_ACTION_TYPE_DROP:
1880                         current_action_flag = MLX5_FLOW_ACTION_DROP;
1881                         break;
1882                 case RTE_FLOW_ACTION_TYPE_COUNT:
1883                         break;
1884                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
1885                         current_action_flag = MLX5_FLOW_ACTION_OF_POP_VLAN;
1886                         break;
1887                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
1888                         current_action_flag = MLX5_FLOW_ACTION_OF_PUSH_VLAN;
1889                         break;
1890                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
1891                         if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN))
1892                                 return rte_flow_error_set
1893                                         (error, ENOTSUP,
1894                                          RTE_FLOW_ERROR_TYPE_ACTION, actions,
1895                                          "vlan modify is not supported,"
1896                                          " set action must follow push action");
1897                         current_action_flag = MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
1898                         break;
1899                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
1900                         if (!(action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN))
1901                                 return rte_flow_error_set
1902                                         (error, ENOTSUP,
1903                                          RTE_FLOW_ERROR_TYPE_ACTION, actions,
1904                                          "vlan modify is not supported,"
1905                                          " set action must follow push action");
1906                         current_action_flag = MLX5_FLOW_ACTION_OF_SET_VLAN_PCP;
1907                         break;
1908                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
1909                         current_action_flag = MLX5_FLOW_ACTION_VXLAN_DECAP;
1910                         break;
1911                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
1912                         ret = flow_tcf_validate_vxlan_encap(actions, error);
1913                         if (ret < 0)
1914                                 return ret;
1915                         current_action_flag = MLX5_FLOW_ACTION_VXLAN_ENCAP;
1916                         break;
1917                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
1918                         current_action_flag = MLX5_FLOW_ACTION_SET_IPV4_SRC;
1919                         break;
1920                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
1921                         current_action_flag = MLX5_FLOW_ACTION_SET_IPV4_DST;
1922                         break;
1923                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
1924                         current_action_flag = MLX5_FLOW_ACTION_SET_IPV6_SRC;
1925                         break;
1926                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
1927                         current_action_flag = MLX5_FLOW_ACTION_SET_IPV6_DST;
1928                         break;
1929                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
1930                         current_action_flag = MLX5_FLOW_ACTION_SET_TP_SRC;
1931                         break;
1932                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
1933                         current_action_flag = MLX5_FLOW_ACTION_SET_TP_DST;
1934                         break;
1935                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
1936                         current_action_flag = MLX5_FLOW_ACTION_SET_TTL;
1937                         break;
1938                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
1939                         current_action_flag = MLX5_FLOW_ACTION_DEC_TTL;
1940                         break;
1941                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
1942                         current_action_flag = MLX5_FLOW_ACTION_SET_MAC_SRC;
1943                         break;
1944                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
1945                         current_action_flag = MLX5_FLOW_ACTION_SET_MAC_DST;
1946                         break;
1947                 default:
1948                         return rte_flow_error_set(error, ENOTSUP,
1949                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1950                                                   actions,
1951                                                   "action not supported");
1952                 }
1953                 if (current_action_flag & MLX5_TCF_CONFIG_ACTIONS) {
1954                         if (!actions->conf)
1955                                 return rte_flow_error_set
1956                                         (error, EINVAL,
1957                                          RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1958                                          actions,
1959                                          "action configuration not set");
1960                 }
1961                 if ((current_action_flag & MLX5_TCF_PEDIT_ACTIONS) &&
1962                     pedit_validated)
1963                         return rte_flow_error_set(error, ENOTSUP,
1964                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1965                                                   actions,
1966                                                   "set actions should be "
1967                                                   "listed successively");
1968                 if ((current_action_flag & ~MLX5_TCF_PEDIT_ACTIONS) &&
1969                     (action_flags & MLX5_TCF_PEDIT_ACTIONS))
1970                         pedit_validated = 1;
1971                 if ((current_action_flag & MLX5_TCF_FATE_ACTIONS) &&
1972                     (action_flags & MLX5_TCF_FATE_ACTIONS))
1973                         return rte_flow_error_set(error, EINVAL,
1974                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1975                                                   actions,
1976                                                   "can't have multiple fate"
1977                                                   " actions");
1978                 if ((current_action_flag & MLX5_TCF_VXLAN_ACTIONS) &&
1979                     (action_flags & MLX5_TCF_VXLAN_ACTIONS))
1980                         return rte_flow_error_set(error, EINVAL,
1981                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1982                                                   actions,
1983                                                   "can't have multiple vxlan"
1984                                                   " actions");
1985                 if ((current_action_flag & MLX5_TCF_VXLAN_ACTIONS) &&
1986                     (action_flags & MLX5_TCF_VLAN_ACTIONS))
1987                         return rte_flow_error_set(error, ENOTSUP,
1988                                                   RTE_FLOW_ERROR_TYPE_ACTION,
1989                                                   actions,
1990                                                   "can't have vxlan and vlan"
1991                                                   " actions in the same rule");
1992                 action_flags |= current_action_flag;
1993         }
1994         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
1995                 unsigned int i;
1996
1997                 if ((item_flags & MLX5_FLOW_LAYER_TUNNEL) &&
1998                     items->type != RTE_FLOW_ITEM_TYPE_ETH)
1999                         return rte_flow_error_set(error, ENOTSUP,
2000                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2001                                                   items,
2002                                                   "only L2 inner item"
2003                                                   " is supported");
2004                 switch (items->type) {
2005                 case RTE_FLOW_ITEM_TYPE_VOID:
2006                         break;
2007                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
2008                         mask.port_id = flow_tcf_item_mask
2009                                 (items, &rte_flow_item_port_id_mask,
2010                                  &flow_tcf_mask_supported.port_id,
2011                                  &flow_tcf_mask_empty.port_id,
2012                                  sizeof(flow_tcf_mask_supported.port_id),
2013                                  error);
2014                         if (!mask.port_id)
2015                                 return -rte_errno;
2016                         if (mask.port_id == &flow_tcf_mask_empty.port_id) {
2017                                 in_port_id_set = 1;
2018                                 break;
2019                         }
2020                         spec.port_id = items->spec;
2021                         if (mask.port_id->id && mask.port_id->id != 0xffffffff)
2022                                 return rte_flow_error_set
2023                                         (error, ENOTSUP,
2024                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2025                                          mask.port_id,
2026                                          "no support for partial mask on"
2027                                          " \"id\" field");
2028                         if (!mask.port_id->id)
2029                                 i = 0;
2030                         else
2031                                 for (i = 0; ptoi[i].ifindex; ++i)
2032                                         if (ptoi[i].port_id == spec.port_id->id)
2033                                                 break;
2034                         if (!ptoi[i].ifindex)
2035                                 return rte_flow_error_set
2036                                         (error, ENODEV,
2037                                          RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
2038                                          spec.port_id,
2039                                          "missing data to convert port ID to"
2040                                          " ifindex");
2041                         if (in_port_id_set && ptoi[i].ifindex != tcm_ifindex)
2042                                 return rte_flow_error_set
2043                                         (error, ENOTSUP,
2044                                          RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
2045                                          spec.port_id,
2046                                          "cannot match traffic for"
2047                                          " several port IDs through"
2048                                          " a single flow rule");
2049                         tcm_ifindex = ptoi[i].ifindex;
2050                         in_port_id_set = 1;
2051                         break;
2052                 case RTE_FLOW_ITEM_TYPE_ETH:
2053                         ret = mlx5_flow_validate_item_eth(items, item_flags,
2054                                                           error);
2055                         if (ret < 0)
2056                                 return ret;
2057                         item_flags |= (item_flags & MLX5_FLOW_LAYER_TUNNEL) ?
2058                                         MLX5_FLOW_LAYER_INNER_L2 :
2059                                         MLX5_FLOW_LAYER_OUTER_L2;
2060                         /* TODO:
2061                          * Redundant check due to different supported mask.
2062                          * Same for the rest of items.
2063                          */
2064                         mask.eth = flow_tcf_item_mask
2065                                 (items, &rte_flow_item_eth_mask,
2066                                  &flow_tcf_mask_supported.eth,
2067                                  &flow_tcf_mask_empty.eth,
2068                                  sizeof(flow_tcf_mask_supported.eth),
2069                                  error);
2070                         if (!mask.eth)
2071                                 return -rte_errno;
2072                         if (mask.eth->type && mask.eth->type !=
2073                             RTE_BE16(0xffff))
2074                                 return rte_flow_error_set
2075                                         (error, ENOTSUP,
2076                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2077                                          mask.eth,
2078                                          "no support for partial mask on"
2079                                          " \"type\" field");
2080                         break;
2081                 case RTE_FLOW_ITEM_TYPE_VLAN:
2082                         ret = mlx5_flow_validate_item_vlan(items, item_flags,
2083                                                            error);
2084                         if (ret < 0)
2085                                 return ret;
2086                         item_flags |= MLX5_FLOW_LAYER_OUTER_VLAN;
2087                         mask.vlan = flow_tcf_item_mask
2088                                 (items, &rte_flow_item_vlan_mask,
2089                                  &flow_tcf_mask_supported.vlan,
2090                                  &flow_tcf_mask_empty.vlan,
2091                                  sizeof(flow_tcf_mask_supported.vlan),
2092                                  error);
2093                         if (!mask.vlan)
2094                                 return -rte_errno;
2095                         if ((mask.vlan->tci & RTE_BE16(0xe000) &&
2096                              (mask.vlan->tci & RTE_BE16(0xe000)) !=
2097                               RTE_BE16(0xe000)) ||
2098                             (mask.vlan->tci & RTE_BE16(0x0fff) &&
2099                              (mask.vlan->tci & RTE_BE16(0x0fff)) !=
2100                               RTE_BE16(0x0fff)) ||
2101                             (mask.vlan->inner_type &&
2102                              mask.vlan->inner_type != RTE_BE16(0xffff)))
2103                                 return rte_flow_error_set
2104                                         (error, ENOTSUP,
2105                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2106                                          mask.vlan,
2107                                          "no support for partial masks on"
2108                                          " \"tci\" (PCP and VID parts) and"
2109                                          " \"inner_type\" fields");
2110                         break;
2111                 case RTE_FLOW_ITEM_TYPE_IPV4:
2112                         ret = mlx5_flow_validate_item_ipv4(items, item_flags,
2113                                                            error);
2114                         if (ret < 0)
2115                                 return ret;
2116                         item_flags |= MLX5_FLOW_LAYER_OUTER_L3_IPV4;
2117                         mask.ipv4 = flow_tcf_item_mask
2118                                 (items, &rte_flow_item_ipv4_mask,
2119                                  &flow_tcf_mask_supported.ipv4,
2120                                  &flow_tcf_mask_empty.ipv4,
2121                                  sizeof(flow_tcf_mask_supported.ipv4),
2122                                  error);
2123                         if (!mask.ipv4)
2124                                 return -rte_errno;
2125                         if (mask.ipv4->hdr.next_proto_id &&
2126                             mask.ipv4->hdr.next_proto_id != 0xff)
2127                                 return rte_flow_error_set
2128                                         (error, ENOTSUP,
2129                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2130                                          mask.ipv4,
2131                                          "no support for partial mask on"
2132                                          " \"hdr.next_proto_id\" field");
2133                         else if (mask.ipv4->hdr.next_proto_id)
2134                                 next_protocol =
2135                                         ((const struct rte_flow_item_ipv4 *)
2136                                          (items->spec))->hdr.next_proto_id;
2137                         if (action_flags & MLX5_FLOW_ACTION_VXLAN_DECAP) {
2138                                 ret = flow_tcf_validate_vxlan_decap_ipv4
2139                                                                 (items, error);
2140                                 if (ret < 0)
2141                                         return ret;
2142                         }
2143                         break;
2144                 case RTE_FLOW_ITEM_TYPE_IPV6:
2145                         ret = mlx5_flow_validate_item_ipv6(items, item_flags,
2146                                                            error);
2147                         if (ret < 0)
2148                                 return ret;
2149                         item_flags |= MLX5_FLOW_LAYER_OUTER_L3_IPV6;
2150                         mask.ipv6 = flow_tcf_item_mask
2151                                 (items, &rte_flow_item_ipv6_mask,
2152                                  &flow_tcf_mask_supported.ipv6,
2153                                  &flow_tcf_mask_empty.ipv6,
2154                                  sizeof(flow_tcf_mask_supported.ipv6),
2155                                  error);
2156                         if (!mask.ipv6)
2157                                 return -rte_errno;
2158                         if (mask.ipv6->hdr.proto &&
2159                             mask.ipv6->hdr.proto != 0xff)
2160                                 return rte_flow_error_set
2161                                         (error, ENOTSUP,
2162                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2163                                          mask.ipv6,
2164                                          "no support for partial mask on"
2165                                          " \"hdr.proto\" field");
2166                         else if (mask.ipv6->hdr.proto)
2167                                 next_protocol =
2168                                         ((const struct rte_flow_item_ipv6 *)
2169                                          (items->spec))->hdr.proto;
2170                         if (action_flags & MLX5_FLOW_ACTION_VXLAN_DECAP) {
2171                                 ret = flow_tcf_validate_vxlan_decap_ipv6
2172                                                                 (items, error);
2173                                 if (ret < 0)
2174                                         return ret;
2175                         }
2176                         break;
2177                 case RTE_FLOW_ITEM_TYPE_UDP:
2178                         ret = mlx5_flow_validate_item_udp(items, item_flags,
2179                                                           next_protocol, error);
2180                         if (ret < 0)
2181                                 return ret;
2182                         item_flags |= MLX5_FLOW_LAYER_OUTER_L4_UDP;
2183                         mask.udp = flow_tcf_item_mask
2184                                 (items, &rte_flow_item_udp_mask,
2185                                  &flow_tcf_mask_supported.udp,
2186                                  &flow_tcf_mask_empty.udp,
2187                                  sizeof(flow_tcf_mask_supported.udp),
2188                                  error);
2189                         if (!mask.udp)
2190                                 return -rte_errno;
2191                         if (action_flags & MLX5_FLOW_ACTION_VXLAN_DECAP) {
2192                                 ret = flow_tcf_validate_vxlan_decap_udp
2193                                                                 (items, error);
2194                                 if (ret < 0)
2195                                         return ret;
2196                         }
2197                         break;
2198                 case RTE_FLOW_ITEM_TYPE_TCP:
2199                         ret = mlx5_flow_validate_item_tcp
2200                                              (items, item_flags,
2201                                               next_protocol,
2202                                               &flow_tcf_mask_supported.tcp,
2203                                               error);
2204                         if (ret < 0)
2205                                 return ret;
2206                         item_flags |= MLX5_FLOW_LAYER_OUTER_L4_TCP;
2207                         mask.tcp = flow_tcf_item_mask
2208                                 (items, &rte_flow_item_tcp_mask,
2209                                  &flow_tcf_mask_supported.tcp,
2210                                  &flow_tcf_mask_empty.tcp,
2211                                  sizeof(flow_tcf_mask_supported.tcp),
2212                                  error);
2213                         if (!mask.tcp)
2214                                 return -rte_errno;
2215                         break;
2216                 case RTE_FLOW_ITEM_TYPE_VXLAN:
2217                         if (!(action_flags & MLX5_FLOW_ACTION_VXLAN_DECAP))
2218                                 return rte_flow_error_set
2219                                         (error, ENOTSUP,
2220                                          RTE_FLOW_ERROR_TYPE_ITEM,
2221                                          items,
2222                                          "vni pattern should be followed by"
2223                                          " vxlan decapsulation action");
2224                         ret = mlx5_flow_validate_item_vxlan(items,
2225                                                             item_flags, error);
2226                         if (ret < 0)
2227                                 return ret;
2228                         item_flags |= MLX5_FLOW_LAYER_VXLAN;
2229                         mask.vxlan = flow_tcf_item_mask
2230                                 (items, &rte_flow_item_vxlan_mask,
2231                                  &flow_tcf_mask_supported.vxlan,
2232                                  &flow_tcf_mask_empty.vxlan,
2233                                  sizeof(flow_tcf_mask_supported.vxlan), error);
2234                         if (!mask.vxlan)
2235                                 return -rte_errno;
2236                         if (mask.vxlan->vni[0] != 0xff ||
2237                             mask.vxlan->vni[1] != 0xff ||
2238                             mask.vxlan->vni[2] != 0xff)
2239                                 return rte_flow_error_set
2240                                         (error, ENOTSUP,
2241                                          RTE_FLOW_ERROR_TYPE_ITEM_MASK,
2242                                          mask.vxlan,
2243                                          "no support for partial or "
2244                                          "empty mask on \"vxlan.vni\" field");
2245                         break;
2246                 default:
2247                         return rte_flow_error_set(error, ENOTSUP,
2248                                                   RTE_FLOW_ERROR_TYPE_ITEM,
2249                                                   items, "item not supported");
2250                 }
2251         }
2252         if ((action_flags & MLX5_TCF_PEDIT_ACTIONS) &&
2253             (action_flags & MLX5_FLOW_ACTION_DROP))
2254                 return rte_flow_error_set(error, ENOTSUP,
2255                                           RTE_FLOW_ERROR_TYPE_ACTION,
2256                                           actions,
2257                                           "set action is not compatible with "
2258                                           "drop action");
2259         if ((action_flags & MLX5_TCF_PEDIT_ACTIONS) &&
2260             !(action_flags & MLX5_FLOW_ACTION_PORT_ID))
2261                 return rte_flow_error_set(error, ENOTSUP,
2262                                           RTE_FLOW_ERROR_TYPE_ACTION,
2263                                           actions,
2264                                           "set action must be followed by "
2265                                           "port_id action");
2266         if (action_flags &
2267            (MLX5_FLOW_ACTION_SET_IPV4_SRC | MLX5_FLOW_ACTION_SET_IPV4_DST)) {
2268                 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV4))
2269                         return rte_flow_error_set(error, EINVAL,
2270                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2271                                                   actions,
2272                                                   "no ipv4 item found in"
2273                                                   " pattern");
2274         }
2275         if (action_flags &
2276            (MLX5_FLOW_ACTION_SET_IPV6_SRC | MLX5_FLOW_ACTION_SET_IPV6_DST)) {
2277                 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L3_IPV6))
2278                         return rte_flow_error_set(error, EINVAL,
2279                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2280                                                   actions,
2281                                                   "no ipv6 item found in"
2282                                                   " pattern");
2283         }
2284         if (action_flags &
2285            (MLX5_FLOW_ACTION_SET_TP_SRC | MLX5_FLOW_ACTION_SET_TP_DST)) {
2286                 if (!(item_flags &
2287                      (MLX5_FLOW_LAYER_OUTER_L4_UDP |
2288                       MLX5_FLOW_LAYER_OUTER_L4_TCP)))
2289                         return rte_flow_error_set(error, EINVAL,
2290                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2291                                                   actions,
2292                                                   "no TCP/UDP item found in"
2293                                                   " pattern");
2294         }
2295         /*
2296          * FW syndrome (0xA9C090):
2297          *     set_flow_table_entry: push vlan action fte in fdb can ONLY be
2298          *     forward to the uplink.
2299          */
2300         if ((action_flags & MLX5_FLOW_ACTION_OF_PUSH_VLAN) &&
2301             (action_flags & MLX5_FLOW_ACTION_PORT_ID) &&
2302             ((struct priv *)port_id_dev->data->dev_private)->representor)
2303                 return rte_flow_error_set(error, ENOTSUP,
2304                                           RTE_FLOW_ERROR_TYPE_ACTION, actions,
2305                                           "vlan push can only be applied"
2306                                           " when forwarding to uplink port");
2307         /*
2308          * FW syndrome (0x294609):
2309          *     set_flow_table_entry: modify/pop/push actions in fdb flow table
2310          *     are supported only while forwarding to vport.
2311          */
2312         if ((action_flags & MLX5_TCF_VLAN_ACTIONS) &&
2313             !(action_flags & MLX5_FLOW_ACTION_PORT_ID))
2314                 return rte_flow_error_set(error, ENOTSUP,
2315                                           RTE_FLOW_ERROR_TYPE_ACTION, actions,
2316                                           "vlan actions are supported"
2317                                           " only with port_id action");
2318         if ((action_flags & MLX5_TCF_VXLAN_ACTIONS) &&
2319             !(action_flags & MLX5_FLOW_ACTION_PORT_ID))
2320                 return rte_flow_error_set(error, ENOTSUP,
2321                                           RTE_FLOW_ERROR_TYPE_ACTION, NULL,
2322                                           "vxlan actions are supported"
2323                                           " only with port_id action");
2324         if (!(action_flags & MLX5_TCF_FATE_ACTIONS))
2325                 return rte_flow_error_set(error, EINVAL,
2326                                           RTE_FLOW_ERROR_TYPE_ACTION, actions,
2327                                           "no fate action is found");
2328         if (action_flags &
2329            (MLX5_FLOW_ACTION_SET_TTL | MLX5_FLOW_ACTION_DEC_TTL)) {
2330                 if (!(item_flags &
2331                      (MLX5_FLOW_LAYER_OUTER_L3_IPV4 |
2332                       MLX5_FLOW_LAYER_OUTER_L3_IPV6)))
2333                         return rte_flow_error_set(error, EINVAL,
2334                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2335                                                   actions,
2336                                                   "no IP found in pattern");
2337         }
2338         if (action_flags &
2339             (MLX5_FLOW_ACTION_SET_MAC_SRC | MLX5_FLOW_ACTION_SET_MAC_DST)) {
2340                 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L2))
2341                         return rte_flow_error_set(error, ENOTSUP,
2342                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2343                                                   actions,
2344                                                   "no ethernet found in"
2345                                                   " pattern");
2346         }
2347         if (action_flags & MLX5_FLOW_ACTION_VXLAN_DECAP) {
2348                 if (!(item_flags &
2349                      (MLX5_FLOW_LAYER_OUTER_L3_IPV4 |
2350                       MLX5_FLOW_LAYER_OUTER_L3_IPV6)))
2351                         return rte_flow_error_set(error, EINVAL,
2352                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2353                                                   NULL,
2354                                                   "no outer IP pattern found"
2355                                                   " for vxlan decap action");
2356                 if (!(item_flags & MLX5_FLOW_LAYER_OUTER_L4_UDP))
2357                         return rte_flow_error_set(error, EINVAL,
2358                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2359                                                   NULL,
2360                                                   "no outer UDP pattern found"
2361                                                   " for vxlan decap action");
2362                 if (!(item_flags & MLX5_FLOW_LAYER_VXLAN))
2363                         return rte_flow_error_set(error, EINVAL,
2364                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2365                                                   NULL,
2366                                                   "no VNI pattern found"
2367                                                   " for vxlan decap action");
2368         }
2369         return 0;
2370 }
2371
2372 /**
2373  * Calculate maximum size of memory for flow items of Linux TC flower.
2374  *
2375  * @param[in] attr
2376  *   Pointer to the flow attributes.
2377  * @param[in] items
2378  *   Pointer to the list of items.
2379  *
2380  * @return
2381  *   Maximum size of memory for items.
2382  */
2383 static int
2384 flow_tcf_get_items_size(const struct rte_flow_attr *attr,
2385                         const struct rte_flow_item items[])
2386 {
2387         int size = 0;
2388
2389         size += SZ_NLATTR_STRZ_OF("flower") +
2390                 SZ_NLATTR_NEST + /* TCA_OPTIONS. */
2391                 SZ_NLATTR_TYPE_OF(uint32_t); /* TCA_CLS_FLAGS_SKIP_SW. */
2392         if (attr->group > 0)
2393                 size += SZ_NLATTR_TYPE_OF(uint32_t); /* TCA_CHAIN. */
2394         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2395                 switch (items->type) {
2396                 case RTE_FLOW_ITEM_TYPE_VOID:
2397                         break;
2398                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
2399                         break;
2400                 case RTE_FLOW_ITEM_TYPE_ETH:
2401                         size += SZ_NLATTR_TYPE_OF(uint16_t) + /* Ether type. */
2402                                 SZ_NLATTR_DATA_OF(ETHER_ADDR_LEN) * 4;
2403                                 /* dst/src MAC addr and mask. */
2404                         break;
2405                 case RTE_FLOW_ITEM_TYPE_VLAN:
2406                         size += SZ_NLATTR_TYPE_OF(uint16_t) + /* Ether type. */
2407                                 SZ_NLATTR_TYPE_OF(uint16_t) +
2408                                 /* VLAN Ether type. */
2409                                 SZ_NLATTR_TYPE_OF(uint8_t) + /* VLAN prio. */
2410                                 SZ_NLATTR_TYPE_OF(uint16_t); /* VLAN ID. */
2411                         break;
2412                 case RTE_FLOW_ITEM_TYPE_IPV4:
2413                         size += SZ_NLATTR_TYPE_OF(uint16_t) + /* Ether type. */
2414                                 SZ_NLATTR_TYPE_OF(uint8_t) + /* IP proto. */
2415                                 SZ_NLATTR_TYPE_OF(uint32_t) * 4;
2416                                 /* dst/src IP addr and mask. */
2417                         break;
2418                 case RTE_FLOW_ITEM_TYPE_IPV6:
2419                         size += SZ_NLATTR_TYPE_OF(uint16_t) + /* Ether type. */
2420                                 SZ_NLATTR_TYPE_OF(uint8_t) + /* IP proto. */
2421                                 SZ_NLATTR_DATA_OF(IPV6_ADDR_LEN) * 4;
2422                                 /* dst/src IP addr and mask. */
2423                         break;
2424                 case RTE_FLOW_ITEM_TYPE_UDP:
2425                         size += SZ_NLATTR_TYPE_OF(uint8_t) + /* IP proto. */
2426                                 SZ_NLATTR_TYPE_OF(uint16_t) * 4;
2427                                 /* dst/src port and mask. */
2428                         break;
2429                 case RTE_FLOW_ITEM_TYPE_TCP:
2430                         size += SZ_NLATTR_TYPE_OF(uint8_t) + /* IP proto. */
2431                                 SZ_NLATTR_TYPE_OF(uint16_t) * 4;
2432                                 /* dst/src port and mask. */
2433                         break;
2434                 case RTE_FLOW_ITEM_TYPE_VXLAN:
2435                         size += SZ_NLATTR_TYPE_OF(uint32_t);
2436                         break;
2437                 default:
2438                         DRV_LOG(WARNING,
2439                                 "unsupported item %p type %d,"
2440                                 " items must be validated before flow creation",
2441                                 (const void *)items, items->type);
2442                         break;
2443                 }
2444         }
2445         return size;
2446 }
2447
2448 /**
2449  * Calculate size of memory to store the VXLAN encapsultion
2450  * related items in the Netlink message buffer. Items list
2451  * is specified by RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP action.
2452  * The item list should be validated.
2453  *
2454  * @param[in] action
2455  *   RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP action object.
2456  *   List of pattern items to scan data from.
2457  *
2458  * @return
2459  *   The size the part of Netlink message buffer to store the
2460  *   VXLAN encapsulation item attributes.
2461  */
2462 static int
2463 flow_tcf_vxlan_encap_size(const struct rte_flow_action *action)
2464 {
2465         const struct rte_flow_item *items;
2466         int size = 0;
2467
2468         assert(action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP);
2469         assert(action->conf);
2470
2471         items = ((const struct rte_flow_action_vxlan_encap *)
2472                                         action->conf)->definition;
2473         assert(items);
2474         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2475                 switch (items->type) {
2476                 case RTE_FLOW_ITEM_TYPE_VOID:
2477                         break;
2478                 case RTE_FLOW_ITEM_TYPE_ETH:
2479                         /* This item does not require message buffer. */
2480                         break;
2481                 case RTE_FLOW_ITEM_TYPE_IPV4:
2482                         size += SZ_NLATTR_DATA_OF(IPV4_ADDR_LEN) * 2;
2483                         break;
2484                 case RTE_FLOW_ITEM_TYPE_IPV6:
2485                         size += SZ_NLATTR_DATA_OF(IPV6_ADDR_LEN) * 2;
2486                         break;
2487                 case RTE_FLOW_ITEM_TYPE_UDP: {
2488                         const struct rte_flow_item_udp *udp = items->mask;
2489
2490                         size += SZ_NLATTR_TYPE_OF(uint16_t);
2491                         if (!udp || udp->hdr.src_port != RTE_BE16(0x0000))
2492                                 size += SZ_NLATTR_TYPE_OF(uint16_t);
2493                         break;
2494                 }
2495                 case RTE_FLOW_ITEM_TYPE_VXLAN:
2496                         size += SZ_NLATTR_TYPE_OF(uint32_t);
2497                         break;
2498                 default:
2499                         assert(false);
2500                         DRV_LOG(WARNING,
2501                                 "unsupported item %p type %d,"
2502                                 " items must be validated"
2503                                 " before flow creation",
2504                                 (const void *)items, items->type);
2505                         return 0;
2506                 }
2507         }
2508         return size;
2509 }
2510
2511 /**
2512  * Calculate maximum size of memory for flow actions of Linux TC flower and
2513  * extract specified actions.
2514  *
2515  * @param[in] actions
2516  *   Pointer to the list of actions.
2517  * @param[out] action_flags
2518  *   Pointer to the detected actions.
2519  *
2520  * @return
2521  *   Maximum size of memory for actions.
2522  */
2523 static int
2524 flow_tcf_get_actions_and_size(const struct rte_flow_action actions[],
2525                               uint64_t *action_flags)
2526 {
2527         int size = 0;
2528         uint64_t flags = 0;
2529
2530         size += SZ_NLATTR_NEST; /* TCA_FLOWER_ACT. */
2531         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
2532                 switch (actions->type) {
2533                 case RTE_FLOW_ACTION_TYPE_VOID:
2534                         break;
2535                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
2536                         size += SZ_NLATTR_NEST + /* na_act_index. */
2537                                 SZ_NLATTR_STRZ_OF("mirred") +
2538                                 SZ_NLATTR_NEST + /* TCA_ACT_OPTIONS. */
2539                                 SZ_NLATTR_TYPE_OF(struct tc_mirred);
2540                         flags |= MLX5_FLOW_ACTION_PORT_ID;
2541                         break;
2542                 case RTE_FLOW_ACTION_TYPE_JUMP:
2543                         size += SZ_NLATTR_NEST + /* na_act_index. */
2544                                 SZ_NLATTR_STRZ_OF("gact") +
2545                                 SZ_NLATTR_NEST + /* TCA_ACT_OPTIONS. */
2546                                 SZ_NLATTR_TYPE_OF(struct tc_gact);
2547                         flags |= MLX5_FLOW_ACTION_JUMP;
2548                         break;
2549                 case RTE_FLOW_ACTION_TYPE_DROP:
2550                         size += SZ_NLATTR_NEST + /* na_act_index. */
2551                                 SZ_NLATTR_STRZ_OF("gact") +
2552                                 SZ_NLATTR_NEST + /* TCA_ACT_OPTIONS. */
2553                                 SZ_NLATTR_TYPE_OF(struct tc_gact);
2554                         flags |= MLX5_FLOW_ACTION_DROP;
2555                         break;
2556                 case RTE_FLOW_ACTION_TYPE_COUNT:
2557                         break;
2558                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
2559                         flags |= MLX5_FLOW_ACTION_OF_POP_VLAN;
2560                         goto action_of_vlan;
2561                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
2562                         flags |= MLX5_FLOW_ACTION_OF_PUSH_VLAN;
2563                         goto action_of_vlan;
2564                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
2565                         flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_VID;
2566                         goto action_of_vlan;
2567                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
2568                         flags |= MLX5_FLOW_ACTION_OF_SET_VLAN_PCP;
2569                         goto action_of_vlan;
2570 action_of_vlan:
2571                         size += SZ_NLATTR_NEST + /* na_act_index. */
2572                                 SZ_NLATTR_STRZ_OF("vlan") +
2573                                 SZ_NLATTR_NEST + /* TCA_ACT_OPTIONS. */
2574                                 SZ_NLATTR_TYPE_OF(struct tc_vlan) +
2575                                 SZ_NLATTR_TYPE_OF(uint16_t) +
2576                                 /* VLAN protocol. */
2577                                 SZ_NLATTR_TYPE_OF(uint16_t) + /* VLAN ID. */
2578                                 SZ_NLATTR_TYPE_OF(uint8_t); /* VLAN prio. */
2579                         break;
2580                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
2581                         size += SZ_NLATTR_NEST + /* na_act_index. */
2582                                 SZ_NLATTR_STRZ_OF("tunnel_key") +
2583                                 SZ_NLATTR_NEST + /* TCA_ACT_OPTIONS. */
2584                                 SZ_NLATTR_TYPE_OF(uint8_t);
2585                         size += SZ_NLATTR_TYPE_OF(struct tc_tunnel_key);
2586                         size += flow_tcf_vxlan_encap_size(actions) +
2587                                 RTE_ALIGN_CEIL /* preceding encap params. */
2588                                 (sizeof(struct flow_tcf_vxlan_encap),
2589                                 MNL_ALIGNTO);
2590                         flags |= MLX5_FLOW_ACTION_VXLAN_ENCAP;
2591                         break;
2592                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
2593                         size += SZ_NLATTR_NEST + /* na_act_index. */
2594                                 SZ_NLATTR_STRZ_OF("tunnel_key") +
2595                                 SZ_NLATTR_NEST + /* TCA_ACT_OPTIONS. */
2596                                 SZ_NLATTR_TYPE_OF(uint8_t);
2597                         size += SZ_NLATTR_TYPE_OF(struct tc_tunnel_key);
2598                         size += RTE_ALIGN_CEIL /* preceding decap params. */
2599                                 (sizeof(struct flow_tcf_vxlan_decap),
2600                                 MNL_ALIGNTO);
2601                         flags |= MLX5_FLOW_ACTION_VXLAN_DECAP;
2602                         break;
2603                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
2604                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
2605                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
2606                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
2607                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
2608                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
2609                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
2610                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
2611                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
2612                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
2613                         size += flow_tcf_get_pedit_actions_size(&actions,
2614                                                                 &flags);
2615                         break;
2616                 default:
2617                         DRV_LOG(WARNING,
2618                                 "unsupported action %p type %d,"
2619                                 " items must be validated before flow creation",
2620                                 (const void *)actions, actions->type);
2621                         break;
2622                 }
2623         }
2624         *action_flags = flags;
2625         return size;
2626 }
2627
2628 /**
2629  * Brand rtnetlink buffer with unique handle.
2630  *
2631  * This handle should be unique for a given network interface to avoid
2632  * collisions.
2633  *
2634  * @param nlh
2635  *   Pointer to Netlink message.
2636  * @param handle
2637  *   Unique 32-bit handle to use.
2638  */
2639 static void
2640 flow_tcf_nl_brand(struct nlmsghdr *nlh, uint32_t handle)
2641 {
2642         struct tcmsg *tcm = mnl_nlmsg_get_payload(nlh);
2643
2644         tcm->tcm_handle = handle;
2645         DRV_LOG(DEBUG, "Netlink msg %p is branded with handle %x",
2646                 (void *)nlh, handle);
2647 }
2648
2649 /**
2650  * Prepare a flow object for Linux TC flower. It calculates the maximum size of
2651  * memory required, allocates the memory, initializes Netlink message headers
2652  * and set unique TC message handle.
2653  *
2654  * @param[in] attr
2655  *   Pointer to the flow attributes.
2656  * @param[in] items
2657  *   Pointer to the list of items.
2658  * @param[in] actions
2659  *   Pointer to the list of actions.
2660  * @param[out] error
2661  *   Pointer to the error structure.
2662  *
2663  * @return
2664  *   Pointer to mlx5_flow object on success,
2665  *   otherwise NULL and rte_ernno is set.
2666  */
2667 static struct mlx5_flow *
2668 flow_tcf_prepare(const struct rte_flow_attr *attr,
2669                  const struct rte_flow_item items[],
2670                  const struct rte_flow_action actions[],
2671                  struct rte_flow_error *error)
2672 {
2673         size_t size = RTE_ALIGN_CEIL
2674                         (sizeof(struct mlx5_flow),
2675                          alignof(struct flow_tcf_tunnel_hdr)) +
2676                       MNL_ALIGN(sizeof(struct nlmsghdr)) +
2677                       MNL_ALIGN(sizeof(struct tcmsg));
2678         struct mlx5_flow *dev_flow;
2679         uint64_t action_flags = 0;
2680         struct nlmsghdr *nlh;
2681         struct tcmsg *tcm;
2682         uint8_t *sp, *tun = NULL;
2683
2684         size += flow_tcf_get_items_size(attr, items);
2685         size += flow_tcf_get_actions_and_size(actions, &action_flags);
2686         dev_flow = rte_zmalloc(__func__, size, MNL_ALIGNTO);
2687         if (!dev_flow) {
2688                 rte_flow_error_set(error, ENOMEM,
2689                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2690                                    "not enough memory to create E-Switch flow");
2691                 return NULL;
2692         }
2693         sp = (uint8_t *)(dev_flow + 1);
2694         if (action_flags & MLX5_FLOW_ACTION_VXLAN_ENCAP) {
2695                 sp = RTE_PTR_ALIGN
2696                         (sp, alignof(struct flow_tcf_tunnel_hdr));
2697                 tun = sp;
2698                 sp += RTE_ALIGN_CEIL
2699                         (sizeof(struct flow_tcf_vxlan_encap),
2700                         MNL_ALIGNTO);
2701 #ifndef NDEBUG
2702                 size -= RTE_ALIGN_CEIL
2703                         (sizeof(struct flow_tcf_vxlan_encap),
2704                         MNL_ALIGNTO);
2705 #endif
2706         } else if (action_flags & MLX5_FLOW_ACTION_VXLAN_DECAP) {
2707                 sp = RTE_PTR_ALIGN
2708                         (sp, alignof(struct flow_tcf_tunnel_hdr));
2709                 tun = sp;
2710                 sp += RTE_ALIGN_CEIL
2711                         (sizeof(struct flow_tcf_vxlan_decap),
2712                         MNL_ALIGNTO);
2713 #ifndef NDEBUG
2714                 size -= RTE_ALIGN_CEIL
2715                         (sizeof(struct flow_tcf_vxlan_decap),
2716                         MNL_ALIGNTO);
2717 #endif
2718         } else {
2719                 sp = RTE_PTR_ALIGN(sp, MNL_ALIGNTO);
2720         }
2721         nlh = mnl_nlmsg_put_header(sp);
2722         tcm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tcm));
2723         *dev_flow = (struct mlx5_flow){
2724                 .tcf = (struct mlx5_flow_tcf){
2725 #ifndef NDEBUG
2726                         .nlsize = size - RTE_ALIGN_CEIL
2727                                 (sizeof(struct mlx5_flow),
2728                                  alignof(struct flow_tcf_tunnel_hdr)),
2729 #endif
2730                         .tunnel = (struct flow_tcf_tunnel_hdr *)tun,
2731                         .nlh = nlh,
2732                         .tcm = tcm,
2733                 },
2734         };
2735         if (action_flags & MLX5_FLOW_ACTION_VXLAN_DECAP)
2736                 dev_flow->tcf.tunnel->type = FLOW_TCF_TUNACT_VXLAN_DECAP;
2737         else if (action_flags & MLX5_FLOW_ACTION_VXLAN_ENCAP)
2738                 dev_flow->tcf.tunnel->type = FLOW_TCF_TUNACT_VXLAN_ENCAP;
2739         /*
2740          * Generate a reasonably unique handle based on the address of the
2741          * target buffer.
2742          *
2743          * This is straightforward on 32-bit systems where the flow pointer can
2744          * be used directly. Otherwise, its least significant part is taken
2745          * after shifting it by the previous power of two of the pointed buffer
2746          * size.
2747          */
2748         if (sizeof(dev_flow) <= 4)
2749                 flow_tcf_nl_brand(nlh, (uintptr_t)dev_flow);
2750         else
2751                 flow_tcf_nl_brand(nlh, (uintptr_t)dev_flow >>
2752                                        rte_log2_u32(rte_align32prevpow2(size)));
2753         return dev_flow;
2754 }
2755
2756 /**
2757  * Make adjustments for supporting count actions.
2758  *
2759  * @param[in] dev
2760  *   Pointer to the Ethernet device structure.
2761  * @param[in] dev_flow
2762  *   Pointer to mlx5_flow.
2763  * @param[out] error
2764  *   Pointer to error structure.
2765  *
2766  * @return
2767  *   0 On success else a negative errno value is returned and rte_errno is set.
2768  */
2769 static int
2770 flow_tcf_translate_action_count(struct rte_eth_dev *dev __rte_unused,
2771                                   struct mlx5_flow *dev_flow,
2772                                   struct rte_flow_error *error)
2773 {
2774         struct rte_flow *flow = dev_flow->flow;
2775
2776         if (!flow->counter) {
2777                 flow->counter = flow_tcf_counter_new();
2778                 if (!flow->counter)
2779                         return rte_flow_error_set(error, rte_errno,
2780                                                   RTE_FLOW_ERROR_TYPE_ACTION,
2781                                                   NULL,
2782                                                   "cannot get counter"
2783                                                   " context.");
2784         }
2785         return 0;
2786 }
2787
2788 /**
2789  * Convert VXLAN VNI to 32-bit integer.
2790  *
2791  * @param[in] vni
2792  *   VXLAN VNI in 24-bit wire format.
2793  *
2794  * @return
2795  *   VXLAN VNI as a 32-bit integer value in network endian.
2796  */
2797 static inline rte_be32_t
2798 vxlan_vni_as_be32(const uint8_t vni[3])
2799 {
2800         union {
2801                 uint8_t vni[4];
2802                 rte_be32_t dword;
2803         } ret = {
2804                 .vni = { 0, vni[0], vni[1], vni[2] },
2805         };
2806         return ret.dword;
2807 }
2808
2809 /**
2810  * Helper function to process RTE_FLOW_ITEM_TYPE_ETH entry in configuration
2811  * of action RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP. Fills the MAC address fields
2812  * in the encapsulation parameters structure. The item must be prevalidated,
2813  * no any validation checks performed by function.
2814  *
2815  * @param[in] spec
2816  *   RTE_FLOW_ITEM_TYPE_ETH entry specification.
2817  * @param[in] mask
2818  *   RTE_FLOW_ITEM_TYPE_ETH entry mask.
2819  * @param[out] encap
2820  *   Structure to fill the gathered MAC address data.
2821  */
2822 static void
2823 flow_tcf_parse_vxlan_encap_eth(const struct rte_flow_item_eth *spec,
2824                                const struct rte_flow_item_eth *mask,
2825                                struct flow_tcf_vxlan_encap *encap)
2826 {
2827         /* Item must be validated before. No redundant checks. */
2828         assert(spec);
2829         if (!mask || !memcmp(&mask->dst,
2830                              &rte_flow_item_eth_mask.dst,
2831                              sizeof(rte_flow_item_eth_mask.dst))) {
2832                 /*
2833                  * Ethernet addresses are not supported by
2834                  * tc as tunnel_key parameters. Destination
2835                  * address is needed to form encap packet
2836                  * header and retrieved by kernel from
2837                  * implicit sources (ARP table, etc),
2838                  * address masks are not supported at all.
2839                  */
2840                 encap->eth.dst = spec->dst;
2841                 encap->mask |= FLOW_TCF_ENCAP_ETH_DST;
2842         }
2843         if (!mask || !memcmp(&mask->src,
2844                              &rte_flow_item_eth_mask.src,
2845                              sizeof(rte_flow_item_eth_mask.src))) {
2846                 /*
2847                  * Ethernet addresses are not supported by
2848                  * tc as tunnel_key parameters. Source ethernet
2849                  * address is ignored anyway.
2850                  */
2851                 encap->eth.src = spec->src;
2852                 encap->mask |= FLOW_TCF_ENCAP_ETH_SRC;
2853         }
2854 }
2855
2856 /**
2857  * Helper function to process RTE_FLOW_ITEM_TYPE_IPV4 entry in configuration
2858  * of action RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP. Fills the IPV4 address fields
2859  * in the encapsulation parameters structure. The item must be prevalidated,
2860  * no any validation checks performed by function.
2861  *
2862  * @param[in] spec
2863  *   RTE_FLOW_ITEM_TYPE_IPV4 entry specification.
2864  * @param[out] encap
2865  *   Structure to fill the gathered IPV4 address data.
2866  */
2867 static void
2868 flow_tcf_parse_vxlan_encap_ipv4(const struct rte_flow_item_ipv4 *spec,
2869                                 struct flow_tcf_vxlan_encap *encap)
2870 {
2871         /* Item must be validated before. No redundant checks. */
2872         assert(spec);
2873         encap->ipv4.dst = spec->hdr.dst_addr;
2874         encap->ipv4.src = spec->hdr.src_addr;
2875         encap->mask |= FLOW_TCF_ENCAP_IPV4_SRC |
2876                        FLOW_TCF_ENCAP_IPV4_DST;
2877 }
2878
2879 /**
2880  * Helper function to process RTE_FLOW_ITEM_TYPE_IPV6 entry in configuration
2881  * of action RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP. Fills the IPV6 address fields
2882  * in the encapsulation parameters structure. The item must be prevalidated,
2883  * no any validation checks performed by function.
2884  *
2885  * @param[in] spec
2886  *   RTE_FLOW_ITEM_TYPE_IPV6 entry specification.
2887  * @param[out] encap
2888  *   Structure to fill the gathered IPV6 address data.
2889  */
2890 static void
2891 flow_tcf_parse_vxlan_encap_ipv6(const struct rte_flow_item_ipv6 *spec,
2892                                 struct flow_tcf_vxlan_encap *encap)
2893 {
2894         /* Item must be validated before. No redundant checks. */
2895         assert(spec);
2896         memcpy(encap->ipv6.dst, spec->hdr.dst_addr, IPV6_ADDR_LEN);
2897         memcpy(encap->ipv6.src, spec->hdr.src_addr, IPV6_ADDR_LEN);
2898         encap->mask |= FLOW_TCF_ENCAP_IPV6_SRC |
2899                        FLOW_TCF_ENCAP_IPV6_DST;
2900 }
2901
2902 /**
2903  * Helper function to process RTE_FLOW_ITEM_TYPE_UDP entry in configuration
2904  * of action RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP. Fills the UDP port fields
2905  * in the encapsulation parameters structure. The item must be prevalidated,
2906  * no any validation checks performed by function.
2907  *
2908  * @param[in] spec
2909  *   RTE_FLOW_ITEM_TYPE_UDP entry specification.
2910  * @param[in] mask
2911  *   RTE_FLOW_ITEM_TYPE_UDP entry mask.
2912  * @param[out] encap
2913  *   Structure to fill the gathered UDP port data.
2914  */
2915 static void
2916 flow_tcf_parse_vxlan_encap_udp(const struct rte_flow_item_udp *spec,
2917                                const struct rte_flow_item_udp *mask,
2918                                struct flow_tcf_vxlan_encap *encap)
2919 {
2920         assert(spec);
2921         encap->udp.dst = spec->hdr.dst_port;
2922         encap->mask |= FLOW_TCF_ENCAP_UDP_DST;
2923         if (!mask || mask->hdr.src_port != RTE_BE16(0x0000)) {
2924                 encap->udp.src = spec->hdr.src_port;
2925                 encap->mask |= FLOW_TCF_ENCAP_IPV4_SRC;
2926         }
2927 }
2928
2929 /**
2930  * Helper function to process RTE_FLOW_ITEM_TYPE_VXLAN entry in configuration
2931  * of action RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP. Fills the VNI fields
2932  * in the encapsulation parameters structure. The item must be prevalidated,
2933  * no any validation checks performed by function.
2934  *
2935  * @param[in] spec
2936  *   RTE_FLOW_ITEM_TYPE_VXLAN entry specification.
2937  * @param[out] encap
2938  *   Structure to fill the gathered VNI address data.
2939  */
2940 static void
2941 flow_tcf_parse_vxlan_encap_vni(const struct rte_flow_item_vxlan *spec,
2942                                struct flow_tcf_vxlan_encap *encap)
2943 {
2944         /* Item must be validated before. Do not redundant checks. */
2945         assert(spec);
2946         memcpy(encap->vxlan.vni, spec->vni, sizeof(encap->vxlan.vni));
2947         encap->mask |= FLOW_TCF_ENCAP_VXLAN_VNI;
2948 }
2949
2950 /**
2951  * Populate consolidated encapsulation object from list of pattern items.
2952  *
2953  * Helper function to process configuration of action such as
2954  * RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP. The item list should be
2955  * validated, there is no way to return an meaningful error.
2956  *
2957  * @param[in] action
2958  *   RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP action object.
2959  *   List of pattern items to gather data from.
2960  * @param[out] src
2961  *   Structure to fill gathered data.
2962  */
2963 static void
2964 flow_tcf_vxlan_encap_parse(const struct rte_flow_action *action,
2965                            struct flow_tcf_vxlan_encap *encap)
2966 {
2967         union {
2968                 const struct rte_flow_item_eth *eth;
2969                 const struct rte_flow_item_ipv4 *ipv4;
2970                 const struct rte_flow_item_ipv6 *ipv6;
2971                 const struct rte_flow_item_udp *udp;
2972                 const struct rte_flow_item_vxlan *vxlan;
2973         } spec, mask;
2974         const struct rte_flow_item *items;
2975
2976         assert(action->type == RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP);
2977         assert(action->conf);
2978
2979         items = ((const struct rte_flow_action_vxlan_encap *)
2980                                         action->conf)->definition;
2981         assert(items);
2982         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
2983                 switch (items->type) {
2984                 case RTE_FLOW_ITEM_TYPE_VOID:
2985                         break;
2986                 case RTE_FLOW_ITEM_TYPE_ETH:
2987                         mask.eth = items->mask;
2988                         spec.eth = items->spec;
2989                         flow_tcf_parse_vxlan_encap_eth(spec.eth, mask.eth,
2990                                                        encap);
2991                         break;
2992                 case RTE_FLOW_ITEM_TYPE_IPV4:
2993                         spec.ipv4 = items->spec;
2994                         flow_tcf_parse_vxlan_encap_ipv4(spec.ipv4, encap);
2995                         break;
2996                 case RTE_FLOW_ITEM_TYPE_IPV6:
2997                         spec.ipv6 = items->spec;
2998                         flow_tcf_parse_vxlan_encap_ipv6(spec.ipv6, encap);
2999                         break;
3000                 case RTE_FLOW_ITEM_TYPE_UDP:
3001                         mask.udp = items->mask;
3002                         spec.udp = items->spec;
3003                         flow_tcf_parse_vxlan_encap_udp(spec.udp, mask.udp,
3004                                                        encap);
3005                         break;
3006                 case RTE_FLOW_ITEM_TYPE_VXLAN:
3007                         spec.vxlan = items->spec;
3008                         flow_tcf_parse_vxlan_encap_vni(spec.vxlan, encap);
3009                         break;
3010                 default:
3011                         assert(false);
3012                         DRV_LOG(WARNING,
3013                                 "unsupported item %p type %d,"
3014                                 " items must be validated"
3015                                 " before flow creation",
3016                                 (const void *)items, items->type);
3017                         encap->mask = 0;
3018                         return;
3019                 }
3020         }
3021 }
3022
3023 /**
3024  * Translate flow for Linux TC flower and construct Netlink message.
3025  *
3026  * @param[in] priv
3027  *   Pointer to the priv structure.
3028  * @param[in, out] flow
3029  *   Pointer to the sub flow.
3030  * @param[in] attr
3031  *   Pointer to the flow attributes.
3032  * @param[in] items
3033  *   Pointer to the list of items.
3034  * @param[in] actions
3035  *   Pointer to the list of actions.
3036  * @param[out] error
3037  *   Pointer to the error structure.
3038  *
3039  * @return
3040  *   0 on success, a negative errno value otherwise and rte_ernno is set.
3041  */
3042 static int
3043 flow_tcf_translate(struct rte_eth_dev *dev, struct mlx5_flow *dev_flow,
3044                    const struct rte_flow_attr *attr,
3045                    const struct rte_flow_item items[],
3046                    const struct rte_flow_action actions[],
3047                    struct rte_flow_error *error)
3048 {
3049         union {
3050                 const struct rte_flow_item_port_id *port_id;
3051                 const struct rte_flow_item_eth *eth;
3052                 const struct rte_flow_item_vlan *vlan;
3053                 const struct rte_flow_item_ipv4 *ipv4;
3054                 const struct rte_flow_item_ipv6 *ipv6;
3055                 const struct rte_flow_item_tcp *tcp;
3056                 const struct rte_flow_item_udp *udp;
3057                 const struct rte_flow_item_vxlan *vxlan;
3058         } spec, mask;
3059         union {
3060                 const struct rte_flow_action_port_id *port_id;
3061                 const struct rte_flow_action_jump *jump;
3062                 const struct rte_flow_action_of_push_vlan *of_push_vlan;
3063                 const struct rte_flow_action_of_set_vlan_vid *
3064                         of_set_vlan_vid;
3065                 const struct rte_flow_action_of_set_vlan_pcp *
3066                         of_set_vlan_pcp;
3067         } conf;
3068         union {
3069                 struct flow_tcf_tunnel_hdr *hdr;
3070                 struct flow_tcf_vxlan_decap *vxlan;
3071         } decap = {
3072                 .hdr = NULL,
3073         };
3074         union {
3075                 struct flow_tcf_tunnel_hdr *hdr;
3076                 struct flow_tcf_vxlan_encap *vxlan;
3077         } encap = {
3078                 .hdr = NULL,
3079         };
3080         struct flow_tcf_ptoi ptoi[PTOI_TABLE_SZ_MAX(dev)];
3081         struct nlmsghdr *nlh = dev_flow->tcf.nlh;
3082         struct tcmsg *tcm = dev_flow->tcf.tcm;
3083         uint32_t na_act_index_cur;
3084         bool eth_type_set = 0;
3085         bool vlan_present = 0;
3086         bool vlan_eth_type_set = 0;
3087         bool ip_proto_set = 0;
3088         struct nlattr *na_flower;
3089         struct nlattr *na_flower_act;
3090         struct nlattr *na_vlan_id = NULL;
3091         struct nlattr *na_vlan_priority = NULL;
3092         uint64_t item_flags = 0;
3093         int ret;
3094
3095         claim_nonzero(flow_tcf_build_ptoi_table(dev, ptoi,
3096                                                 PTOI_TABLE_SZ_MAX(dev)));
3097         if (dev_flow->tcf.tunnel) {
3098                 switch (dev_flow->tcf.tunnel->type) {
3099                 case FLOW_TCF_TUNACT_VXLAN_DECAP:
3100                         decap.vxlan = dev_flow->tcf.vxlan_decap;
3101                         break;
3102                 case FLOW_TCF_TUNACT_VXLAN_ENCAP:
3103                         encap.vxlan = dev_flow->tcf.vxlan_encap;
3104                         break;
3105                 /* New tunnel actions can be added here. */
3106                 default:
3107                         assert(false);
3108                         break;
3109                 }
3110         }
3111         nlh = dev_flow->tcf.nlh;
3112         tcm = dev_flow->tcf.tcm;
3113         /* Prepare API must have been called beforehand. */
3114         assert(nlh != NULL && tcm != NULL);
3115         tcm->tcm_family = AF_UNSPEC;
3116         tcm->tcm_ifindex = ptoi[0].ifindex;
3117         tcm->tcm_parent = TC_H_MAKE(TC_H_INGRESS, TC_H_MIN_INGRESS);
3118         /*
3119          * Priority cannot be zero to prevent the kernel from picking one
3120          * automatically.
3121          */
3122         tcm->tcm_info = TC_H_MAKE((attr->priority + 1) << 16,
3123                                   RTE_BE16(ETH_P_ALL));
3124         if (attr->group > 0)
3125                 mnl_attr_put_u32(nlh, TCA_CHAIN, attr->group);
3126         mnl_attr_put_strz(nlh, TCA_KIND, "flower");
3127         na_flower = mnl_attr_nest_start(nlh, TCA_OPTIONS);
3128         for (; items->type != RTE_FLOW_ITEM_TYPE_END; items++) {
3129                 unsigned int i;
3130
3131                 switch (items->type) {
3132                 case RTE_FLOW_ITEM_TYPE_VOID:
3133                         break;
3134                 case RTE_FLOW_ITEM_TYPE_PORT_ID:
3135                         mask.port_id = flow_tcf_item_mask
3136                                 (items, &rte_flow_item_port_id_mask,
3137                                  &flow_tcf_mask_supported.port_id,
3138                                  &flow_tcf_mask_empty.port_id,
3139                                  sizeof(flow_tcf_mask_supported.port_id),
3140                                  error);
3141                         assert(mask.port_id);
3142                         if (mask.port_id == &flow_tcf_mask_empty.port_id)
3143                                 break;
3144                         spec.port_id = items->spec;
3145                         if (!mask.port_id->id)
3146                                 i = 0;
3147                         else
3148                                 for (i = 0; ptoi[i].ifindex; ++i)
3149                                         if (ptoi[i].port_id == spec.port_id->id)
3150                                                 break;
3151                         assert(ptoi[i].ifindex);
3152                         tcm->tcm_ifindex = ptoi[i].ifindex;
3153                         break;
3154                 case RTE_FLOW_ITEM_TYPE_ETH:
3155                         item_flags |= (item_flags & MLX5_FLOW_LAYER_VXLAN) ?
3156                                       MLX5_FLOW_LAYER_INNER_L2 :
3157                                       MLX5_FLOW_LAYER_OUTER_L2;
3158                         mask.eth = flow_tcf_item_mask
3159                                 (items, &rte_flow_item_eth_mask,
3160                                  &flow_tcf_mask_supported.eth,
3161                                  &flow_tcf_mask_empty.eth,
3162                                  sizeof(flow_tcf_mask_supported.eth),
3163                                  error);
3164                         assert(mask.eth);
3165                         if (mask.eth == &flow_tcf_mask_empty.eth)
3166                                 break;
3167                         spec.eth = items->spec;
3168                         if (decap.vxlan &&
3169                             !(item_flags & MLX5_FLOW_LAYER_VXLAN)) {
3170                                 DRV_LOG(WARNING,
3171                                         "outer L2 addresses cannot be forced"
3172                                         " for vxlan decapsulation, parameter"
3173                                         " ignored");
3174                                 break;
3175                         }
3176                         if (mask.eth->type) {
3177                                 mnl_attr_put_u16(nlh, TCA_FLOWER_KEY_ETH_TYPE,
3178                                                  spec.eth->type);
3179                                 eth_type_set = 1;
3180                         }
3181                         if (!is_zero_ether_addr(&mask.eth->dst)) {
3182                                 mnl_attr_put(nlh, TCA_FLOWER_KEY_ETH_DST,
3183                                              ETHER_ADDR_LEN,
3184                                              spec.eth->dst.addr_bytes);
3185                                 mnl_attr_put(nlh, TCA_FLOWER_KEY_ETH_DST_MASK,
3186                                              ETHER_ADDR_LEN,
3187                                              mask.eth->dst.addr_bytes);
3188                         }
3189                         if (!is_zero_ether_addr(&mask.eth->src)) {
3190                                 mnl_attr_put(nlh, TCA_FLOWER_KEY_ETH_SRC,
3191                                              ETHER_ADDR_LEN,
3192                                              spec.eth->src.addr_bytes);
3193                                 mnl_attr_put(nlh, TCA_FLOWER_KEY_ETH_SRC_MASK,
3194                                              ETHER_ADDR_LEN,
3195                                              mask.eth->src.addr_bytes);
3196                         }
3197                         assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3198                         break;
3199                 case RTE_FLOW_ITEM_TYPE_VLAN:
3200                         assert(!encap.hdr);
3201                         assert(!decap.hdr);
3202                         item_flags |= MLX5_FLOW_LAYER_OUTER_VLAN;
3203                         mask.vlan = flow_tcf_item_mask
3204                                 (items, &rte_flow_item_vlan_mask,
3205                                  &flow_tcf_mask_supported.vlan,
3206                                  &flow_tcf_mask_empty.vlan,
3207                                  sizeof(flow_tcf_mask_supported.vlan),
3208                                  error);
3209                         assert(mask.vlan);
3210                         if (!eth_type_set)
3211                                 mnl_attr_put_u16(nlh, TCA_FLOWER_KEY_ETH_TYPE,
3212                                                  RTE_BE16(ETH_P_8021Q));
3213                         eth_type_set = 1;
3214                         vlan_present = 1;
3215                         if (mask.vlan == &flow_tcf_mask_empty.vlan)
3216                                 break;
3217                         spec.vlan = items->spec;
3218                         if (mask.vlan->inner_type) {
3219                                 mnl_attr_put_u16(nlh,
3220                                                  TCA_FLOWER_KEY_VLAN_ETH_TYPE,
3221                                                  spec.vlan->inner_type);
3222                                 vlan_eth_type_set = 1;
3223                         }
3224                         if (mask.vlan->tci & RTE_BE16(0xe000))
3225                                 mnl_attr_put_u8(nlh, TCA_FLOWER_KEY_VLAN_PRIO,
3226                                                 (rte_be_to_cpu_16
3227                                                  (spec.vlan->tci) >> 13) & 0x7);
3228                         if (mask.vlan->tci & RTE_BE16(0x0fff))
3229                                 mnl_attr_put_u16(nlh, TCA_FLOWER_KEY_VLAN_ID,
3230                                                  rte_be_to_cpu_16
3231                                                  (spec.vlan->tci &
3232                                                   RTE_BE16(0x0fff)));
3233                         assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3234                         break;
3235                 case RTE_FLOW_ITEM_TYPE_IPV4:
3236                         item_flags |= MLX5_FLOW_LAYER_OUTER_L3_IPV4;
3237                         mask.ipv4 = flow_tcf_item_mask
3238                                 (items, &rte_flow_item_ipv4_mask,
3239                                  &flow_tcf_mask_supported.ipv4,
3240                                  &flow_tcf_mask_empty.ipv4,
3241                                  sizeof(flow_tcf_mask_supported.ipv4),
3242                                  error);
3243                         assert(mask.ipv4);
3244                         spec.ipv4 = items->spec;
3245                         if (!decap.vxlan) {
3246                                 if (!eth_type_set && !vlan_eth_type_set)
3247                                         mnl_attr_put_u16
3248                                                 (nlh,
3249                                                  vlan_present ?
3250                                                  TCA_FLOWER_KEY_VLAN_ETH_TYPE :
3251                                                  TCA_FLOWER_KEY_ETH_TYPE,
3252                                                  RTE_BE16(ETH_P_IP));
3253                                 eth_type_set = 1;
3254                                 vlan_eth_type_set = 1;
3255                                 if (mask.ipv4 == &flow_tcf_mask_empty.ipv4)
3256                                         break;
3257                                 if (mask.ipv4->hdr.next_proto_id) {
3258                                         mnl_attr_put_u8
3259                                                 (nlh, TCA_FLOWER_KEY_IP_PROTO,
3260                                                  spec.ipv4->hdr.next_proto_id);
3261                                         ip_proto_set = 1;
3262                                 }
3263                         } else {
3264                                 assert(mask.ipv4 != &flow_tcf_mask_empty.ipv4);
3265                         }
3266                         if (mask.ipv4->hdr.src_addr) {
3267                                 mnl_attr_put_u32
3268                                         (nlh, decap.vxlan ?
3269                                          TCA_FLOWER_KEY_ENC_IPV4_SRC :
3270                                          TCA_FLOWER_KEY_IPV4_SRC,
3271                                          spec.ipv4->hdr.src_addr);
3272                                 mnl_attr_put_u32
3273                                         (nlh, decap.vxlan ?
3274                                          TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK :
3275                                          TCA_FLOWER_KEY_IPV4_SRC_MASK,
3276                                          mask.ipv4->hdr.src_addr);
3277                         }
3278                         if (mask.ipv4->hdr.dst_addr) {
3279                                 mnl_attr_put_u32
3280                                         (nlh, decap.vxlan ?
3281                                          TCA_FLOWER_KEY_ENC_IPV4_DST :
3282                                          TCA_FLOWER_KEY_IPV4_DST,
3283                                          spec.ipv4->hdr.dst_addr);
3284                                 mnl_attr_put_u32
3285                                         (nlh, decap.vxlan ?
3286                                          TCA_FLOWER_KEY_ENC_IPV4_DST_MASK :
3287                                          TCA_FLOWER_KEY_IPV4_DST_MASK,
3288                                          mask.ipv4->hdr.dst_addr);
3289                         }
3290                         assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3291                         break;
3292                 case RTE_FLOW_ITEM_TYPE_IPV6:
3293                         item_flags |= MLX5_FLOW_LAYER_OUTER_L3_IPV6;
3294                         mask.ipv6 = flow_tcf_item_mask
3295                                 (items, &rte_flow_item_ipv6_mask,
3296                                  &flow_tcf_mask_supported.ipv6,
3297                                  &flow_tcf_mask_empty.ipv6,
3298                                  sizeof(flow_tcf_mask_supported.ipv6),
3299                                  error);
3300                         assert(mask.ipv6);
3301                         spec.ipv6 = items->spec;
3302                         if (!decap.vxlan) {
3303                                 if (!eth_type_set || !vlan_eth_type_set) {
3304                                         mnl_attr_put_u16
3305                                                 (nlh,
3306                                                  vlan_present ?
3307                                                  TCA_FLOWER_KEY_VLAN_ETH_TYPE :
3308                                                  TCA_FLOWER_KEY_ETH_TYPE,
3309                                                  RTE_BE16(ETH_P_IPV6));
3310                                 }
3311                                 eth_type_set = 1;
3312                                 vlan_eth_type_set = 1;
3313                                 if (mask.ipv6 == &flow_tcf_mask_empty.ipv6)
3314                                         break;
3315                                 if (mask.ipv6->hdr.proto) {
3316                                         mnl_attr_put_u8
3317                                                 (nlh, TCA_FLOWER_KEY_IP_PROTO,
3318                                                  spec.ipv6->hdr.proto);
3319                                         ip_proto_set = 1;
3320                                 }
3321                         } else {
3322                                 assert(mask.ipv6 != &flow_tcf_mask_empty.ipv6);
3323                         }
3324                         if (!IN6_IS_ADDR_UNSPECIFIED(mask.ipv6->hdr.src_addr)) {
3325                                 mnl_attr_put(nlh, decap.vxlan ?
3326                                              TCA_FLOWER_KEY_ENC_IPV6_SRC :
3327                                              TCA_FLOWER_KEY_IPV6_SRC,
3328                                              IPV6_ADDR_LEN,
3329                                              spec.ipv6->hdr.src_addr);
3330                                 mnl_attr_put(nlh, decap.vxlan ?
3331                                              TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK :
3332                                              TCA_FLOWER_KEY_IPV6_SRC_MASK,
3333                                              IPV6_ADDR_LEN,
3334                                              mask.ipv6->hdr.src_addr);
3335                         }
3336                         if (!IN6_IS_ADDR_UNSPECIFIED(mask.ipv6->hdr.dst_addr)) {
3337                                 mnl_attr_put(nlh, decap.vxlan ?
3338                                              TCA_FLOWER_KEY_ENC_IPV6_DST :
3339                                              TCA_FLOWER_KEY_IPV6_DST,
3340                                              IPV6_ADDR_LEN,
3341                                              spec.ipv6->hdr.dst_addr);
3342                                 mnl_attr_put(nlh, decap.vxlan ?
3343                                              TCA_FLOWER_KEY_ENC_IPV6_DST_MASK :
3344                                              TCA_FLOWER_KEY_IPV6_DST_MASK,
3345                                              IPV6_ADDR_LEN,
3346                                              mask.ipv6->hdr.dst_addr);
3347                         }
3348                         assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3349                         break;
3350                 case RTE_FLOW_ITEM_TYPE_UDP:
3351                         item_flags |= MLX5_FLOW_LAYER_OUTER_L4_UDP;
3352                         mask.udp = flow_tcf_item_mask
3353                                 (items, &rte_flow_item_udp_mask,
3354                                  &flow_tcf_mask_supported.udp,
3355                                  &flow_tcf_mask_empty.udp,
3356                                  sizeof(flow_tcf_mask_supported.udp),
3357                                  error);
3358                         assert(mask.udp);
3359                         spec.udp = items->spec;
3360                         if (!decap.vxlan) {
3361                                 if (!ip_proto_set)
3362                                         mnl_attr_put_u8
3363                                                 (nlh, TCA_FLOWER_KEY_IP_PROTO,
3364                                                 IPPROTO_UDP);
3365                                 if (mask.udp == &flow_tcf_mask_empty.udp)
3366                                         break;
3367                         } else {
3368                                 assert(mask.udp != &flow_tcf_mask_empty.udp);
3369                                 decap.vxlan->udp_port =
3370                                         rte_be_to_cpu_16
3371                                                 (spec.udp->hdr.dst_port);
3372                         }
3373                         if (mask.udp->hdr.src_port) {
3374                                 mnl_attr_put_u16
3375                                         (nlh, decap.vxlan ?
3376                                          TCA_FLOWER_KEY_ENC_UDP_SRC_PORT :
3377                                          TCA_FLOWER_KEY_UDP_SRC,
3378                                          spec.udp->hdr.src_port);
3379                                 mnl_attr_put_u16
3380                                         (nlh, decap.vxlan ?
3381                                          TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK :
3382                                          TCA_FLOWER_KEY_UDP_SRC_MASK,
3383                                          mask.udp->hdr.src_port);
3384                         }
3385                         if (mask.udp->hdr.dst_port) {
3386                                 mnl_attr_put_u16
3387                                         (nlh, decap.vxlan ?
3388                                          TCA_FLOWER_KEY_ENC_UDP_DST_PORT :
3389                                          TCA_FLOWER_KEY_UDP_DST,
3390                                          spec.udp->hdr.dst_port);
3391                                 mnl_attr_put_u16
3392                                         (nlh, decap.vxlan ?
3393                                          TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK :
3394                                          TCA_FLOWER_KEY_UDP_DST_MASK,
3395                                          mask.udp->hdr.dst_port);
3396                         }
3397                         assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3398                         break;
3399                 case RTE_FLOW_ITEM_TYPE_TCP:
3400                         item_flags |= MLX5_FLOW_LAYER_OUTER_L4_TCP;
3401                         mask.tcp = flow_tcf_item_mask
3402                                 (items, &rte_flow_item_tcp_mask,
3403                                  &flow_tcf_mask_supported.tcp,
3404                                  &flow_tcf_mask_empty.tcp,
3405                                  sizeof(flow_tcf_mask_supported.tcp),
3406                                  error);
3407                         assert(mask.tcp);
3408                         if (!ip_proto_set)
3409                                 mnl_attr_put_u8(nlh, TCA_FLOWER_KEY_IP_PROTO,
3410                                                 IPPROTO_TCP);
3411                         if (mask.tcp == &flow_tcf_mask_empty.tcp)
3412                                 break;
3413                         spec.tcp = items->spec;
3414                         if (mask.tcp->hdr.src_port) {
3415                                 mnl_attr_put_u16(nlh, TCA_FLOWER_KEY_TCP_SRC,
3416                                                  spec.tcp->hdr.src_port);
3417                                 mnl_attr_put_u16(nlh,
3418                                                  TCA_FLOWER_KEY_TCP_SRC_MASK,
3419                                                  mask.tcp->hdr.src_port);
3420                         }
3421                         if (mask.tcp->hdr.dst_port) {
3422                                 mnl_attr_put_u16(nlh, TCA_FLOWER_KEY_TCP_DST,
3423                                                  spec.tcp->hdr.dst_port);
3424                                 mnl_attr_put_u16(nlh,
3425                                                  TCA_FLOWER_KEY_TCP_DST_MASK,
3426                                                  mask.tcp->hdr.dst_port);
3427                         }
3428                         if (mask.tcp->hdr.tcp_flags) {
3429                                 mnl_attr_put_u16
3430                                         (nlh,
3431                                          TCA_FLOWER_KEY_TCP_FLAGS,
3432                                          rte_cpu_to_be_16
3433                                                 (spec.tcp->hdr.tcp_flags));
3434                                 mnl_attr_put_u16
3435                                         (nlh,
3436                                          TCA_FLOWER_KEY_TCP_FLAGS_MASK,
3437                                          rte_cpu_to_be_16
3438                                                 (mask.tcp->hdr.tcp_flags));
3439                         }
3440                         assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3441                         break;
3442                 case RTE_FLOW_ITEM_TYPE_VXLAN:
3443                         assert(decap.vxlan);
3444                         item_flags |= MLX5_FLOW_LAYER_VXLAN;
3445                         spec.vxlan = items->spec;
3446                         mnl_attr_put_u32(nlh,
3447                                          TCA_FLOWER_KEY_ENC_KEY_ID,
3448                                          vxlan_vni_as_be32(spec.vxlan->vni));
3449                         assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3450                         break;
3451                 default:
3452                         return rte_flow_error_set(error, ENOTSUP,
3453                                                   RTE_FLOW_ERROR_TYPE_ITEM,
3454                                                   NULL, "item not supported");
3455                 }
3456         }
3457         na_flower_act = mnl_attr_nest_start(nlh, TCA_FLOWER_ACT);
3458         na_act_index_cur = 1;
3459         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
3460                 struct nlattr *na_act_index;
3461                 struct nlattr *na_act;
3462                 unsigned int vlan_act;
3463                 unsigned int i;
3464
3465                 switch (actions->type) {
3466                 case RTE_FLOW_ACTION_TYPE_VOID:
3467                         break;
3468                 case RTE_FLOW_ACTION_TYPE_PORT_ID:
3469                         conf.port_id = actions->conf;
3470                         if (conf.port_id->original)
3471                                 i = 0;
3472                         else
3473                                 for (i = 0; ptoi[i].ifindex; ++i)
3474                                         if (ptoi[i].port_id == conf.port_id->id)
3475                                                 break;
3476                         assert(ptoi[i].ifindex);
3477                         na_act_index =
3478                                 mnl_attr_nest_start(nlh, na_act_index_cur++);
3479                         assert(na_act_index);
3480                         mnl_attr_put_strz(nlh, TCA_ACT_KIND, "mirred");
3481                         na_act = mnl_attr_nest_start(nlh, TCA_ACT_OPTIONS);
3482                         assert(na_act);
3483                         if (encap.hdr) {
3484                                 assert(dev_flow->tcf.tunnel);
3485                                 dev_flow->tcf.tunnel->ifindex_ptr =
3486                                         &((struct tc_mirred *)
3487                                         mnl_attr_get_payload
3488                                         (mnl_nlmsg_get_payload_tail
3489                                                 (nlh)))->ifindex;
3490                         }
3491                         mnl_attr_put(nlh, TCA_MIRRED_PARMS,
3492                                      sizeof(struct tc_mirred),
3493                                      &(struct tc_mirred){
3494                                         .action = TC_ACT_STOLEN,
3495                                         .eaction = TCA_EGRESS_REDIR,
3496                                         .ifindex = ptoi[i].ifindex,
3497                                      });
3498                         mnl_attr_nest_end(nlh, na_act);
3499                         mnl_attr_nest_end(nlh, na_act_index);
3500                         break;
3501                 case RTE_FLOW_ACTION_TYPE_JUMP:
3502                         conf.jump = actions->conf;
3503                         na_act_index =
3504                                 mnl_attr_nest_start(nlh, na_act_index_cur++);
3505                         assert(na_act_index);
3506                         mnl_attr_put_strz(nlh, TCA_ACT_KIND, "gact");
3507                         na_act = mnl_attr_nest_start(nlh, TCA_ACT_OPTIONS);
3508                         assert(na_act);
3509                         mnl_attr_put(nlh, TCA_GACT_PARMS,
3510                                      sizeof(struct tc_gact),
3511                                      &(struct tc_gact){
3512                                         .action = TC_ACT_GOTO_CHAIN |
3513                                                   conf.jump->group,
3514                                      });
3515                         mnl_attr_nest_end(nlh, na_act);
3516                         mnl_attr_nest_end(nlh, na_act_index);
3517                         break;
3518                 case RTE_FLOW_ACTION_TYPE_DROP:
3519                         na_act_index =
3520                                 mnl_attr_nest_start(nlh, na_act_index_cur++);
3521                         assert(na_act_index);
3522                         mnl_attr_put_strz(nlh, TCA_ACT_KIND, "gact");
3523                         na_act = mnl_attr_nest_start(nlh, TCA_ACT_OPTIONS);
3524                         assert(na_act);
3525                         mnl_attr_put(nlh, TCA_GACT_PARMS,
3526                                      sizeof(struct tc_gact),
3527                                      &(struct tc_gact){
3528                                         .action = TC_ACT_SHOT,
3529                                      });
3530                         mnl_attr_nest_end(nlh, na_act);
3531                         mnl_attr_nest_end(nlh, na_act_index);
3532                         break;
3533                 case RTE_FLOW_ACTION_TYPE_COUNT:
3534                         /*
3535                          * Driver adds the count action implicitly for
3536                          * each rule it creates.
3537                          */
3538                         ret = flow_tcf_translate_action_count(dev,
3539                                                               dev_flow, error);
3540                         if (ret < 0)
3541                                 return ret;
3542                         break;
3543                 case RTE_FLOW_ACTION_TYPE_OF_POP_VLAN:
3544                         conf.of_push_vlan = NULL;
3545                         vlan_act = TCA_VLAN_ACT_POP;
3546                         goto action_of_vlan;
3547                 case RTE_FLOW_ACTION_TYPE_OF_PUSH_VLAN:
3548                         conf.of_push_vlan = actions->conf;
3549                         vlan_act = TCA_VLAN_ACT_PUSH;
3550                         goto action_of_vlan;
3551                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID:
3552                         conf.of_set_vlan_vid = actions->conf;
3553                         if (na_vlan_id)
3554                                 goto override_na_vlan_id;
3555                         vlan_act = TCA_VLAN_ACT_MODIFY;
3556                         goto action_of_vlan;
3557                 case RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP:
3558                         conf.of_set_vlan_pcp = actions->conf;
3559                         if (na_vlan_priority)
3560                                 goto override_na_vlan_priority;
3561                         vlan_act = TCA_VLAN_ACT_MODIFY;
3562                         goto action_of_vlan;
3563 action_of_vlan:
3564                         na_act_index =
3565                                 mnl_attr_nest_start(nlh, na_act_index_cur++);
3566                         assert(na_act_index);
3567                         mnl_attr_put_strz(nlh, TCA_ACT_KIND, "vlan");
3568                         na_act = mnl_attr_nest_start(nlh, TCA_ACT_OPTIONS);
3569                         assert(na_act);
3570                         mnl_attr_put(nlh, TCA_VLAN_PARMS,
3571                                      sizeof(struct tc_vlan),
3572                                      &(struct tc_vlan){
3573                                         .action = TC_ACT_PIPE,
3574                                         .v_action = vlan_act,
3575                                      });
3576                         if (vlan_act == TCA_VLAN_ACT_POP) {
3577                                 mnl_attr_nest_end(nlh, na_act);
3578                                 mnl_attr_nest_end(nlh, na_act_index);
3579                                 break;
3580                         }
3581                         if (vlan_act == TCA_VLAN_ACT_PUSH)
3582                                 mnl_attr_put_u16(nlh,
3583                                                  TCA_VLAN_PUSH_VLAN_PROTOCOL,
3584                                                  conf.of_push_vlan->ethertype);
3585                         na_vlan_id = mnl_nlmsg_get_payload_tail(nlh);
3586                         mnl_attr_put_u16(nlh, TCA_VLAN_PAD, 0);
3587                         na_vlan_priority = mnl_nlmsg_get_payload_tail(nlh);
3588                         mnl_attr_put_u8(nlh, TCA_VLAN_PAD, 0);
3589                         mnl_attr_nest_end(nlh, na_act);
3590                         mnl_attr_nest_end(nlh, na_act_index);
3591                         if (actions->type ==
3592                             RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_VID) {
3593 override_na_vlan_id:
3594                                 na_vlan_id->nla_type = TCA_VLAN_PUSH_VLAN_ID;
3595                                 *(uint16_t *)mnl_attr_get_payload(na_vlan_id) =
3596                                         rte_be_to_cpu_16
3597                                         (conf.of_set_vlan_vid->vlan_vid);
3598                         } else if (actions->type ==
3599                                    RTE_FLOW_ACTION_TYPE_OF_SET_VLAN_PCP) {
3600 override_na_vlan_priority:
3601                                 na_vlan_priority->nla_type =
3602                                         TCA_VLAN_PUSH_VLAN_PRIORITY;
3603                                 *(uint8_t *)mnl_attr_get_payload
3604                                         (na_vlan_priority) =
3605                                         conf.of_set_vlan_pcp->vlan_pcp;
3606                         }
3607                         break;
3608                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
3609                         assert(decap.vxlan);
3610                         assert(dev_flow->tcf.tunnel);
3611                         dev_flow->tcf.tunnel->ifindex_ptr =
3612                                 (unsigned int *)&tcm->tcm_ifindex;
3613                         na_act_index =
3614                                 mnl_attr_nest_start(nlh, na_act_index_cur++);
3615                         assert(na_act_index);
3616                         mnl_attr_put_strz(nlh, TCA_ACT_KIND, "tunnel_key");
3617                         na_act = mnl_attr_nest_start(nlh, TCA_ACT_OPTIONS);
3618                         assert(na_act);
3619                         mnl_attr_put(nlh, TCA_TUNNEL_KEY_PARMS,
3620                                 sizeof(struct tc_tunnel_key),
3621                                 &(struct tc_tunnel_key){
3622                                         .action = TC_ACT_PIPE,
3623                                         .t_action = TCA_TUNNEL_KEY_ACT_RELEASE,
3624                                         });
3625                         mnl_attr_nest_end(nlh, na_act);
3626                         mnl_attr_nest_end(nlh, na_act_index);
3627                         assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3628                         break;
3629                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
3630                         assert(encap.vxlan);
3631                         flow_tcf_vxlan_encap_parse(actions, encap.vxlan);
3632                         na_act_index =
3633                                 mnl_attr_nest_start(nlh, na_act_index_cur++);
3634                         assert(na_act_index);
3635                         mnl_attr_put_strz(nlh, TCA_ACT_KIND, "tunnel_key");
3636                         na_act = mnl_attr_nest_start(nlh, TCA_ACT_OPTIONS);
3637                         assert(na_act);
3638                         mnl_attr_put(nlh, TCA_TUNNEL_KEY_PARMS,
3639                                 sizeof(struct tc_tunnel_key),
3640                                 &(struct tc_tunnel_key){
3641                                         .action = TC_ACT_PIPE,
3642                                         .t_action = TCA_TUNNEL_KEY_ACT_SET,
3643                                         });
3644                         if (encap.vxlan->mask & FLOW_TCF_ENCAP_UDP_DST)
3645                                 mnl_attr_put_u16(nlh,
3646                                          TCA_TUNNEL_KEY_ENC_DST_PORT,
3647                                          encap.vxlan->udp.dst);
3648                         if (encap.vxlan->mask & FLOW_TCF_ENCAP_IPV4_SRC)
3649                                 mnl_attr_put_u32(nlh,
3650                                          TCA_TUNNEL_KEY_ENC_IPV4_SRC,
3651                                          encap.vxlan->ipv4.src);
3652                         if (encap.vxlan->mask & FLOW_TCF_ENCAP_IPV4_DST)
3653                                 mnl_attr_put_u32(nlh,
3654                                          TCA_TUNNEL_KEY_ENC_IPV4_DST,
3655                                          encap.vxlan->ipv4.dst);
3656                         if (encap.vxlan->mask & FLOW_TCF_ENCAP_IPV6_SRC)
3657                                 mnl_attr_put(nlh,
3658                                          TCA_TUNNEL_KEY_ENC_IPV6_SRC,
3659                                          sizeof(encap.vxlan->ipv6.src),
3660                                          &encap.vxlan->ipv6.src);
3661                         if (encap.vxlan->mask & FLOW_TCF_ENCAP_IPV6_DST)
3662                                 mnl_attr_put(nlh,
3663                                          TCA_TUNNEL_KEY_ENC_IPV6_DST,
3664                                          sizeof(encap.vxlan->ipv6.dst),
3665                                          &encap.vxlan->ipv6.dst);
3666                         if (encap.vxlan->mask & FLOW_TCF_ENCAP_VXLAN_VNI)
3667                                 mnl_attr_put_u32(nlh,
3668                                          TCA_TUNNEL_KEY_ENC_KEY_ID,
3669                                          vxlan_vni_as_be32
3670                                                 (encap.vxlan->vxlan.vni));
3671                         mnl_attr_put_u8(nlh, TCA_TUNNEL_KEY_NO_CSUM, 0);
3672                         mnl_attr_nest_end(nlh, na_act);
3673                         mnl_attr_nest_end(nlh, na_act_index);
3674                         assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3675                         break;
3676                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC:
3677                 case RTE_FLOW_ACTION_TYPE_SET_IPV4_DST:
3678                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC:
3679                 case RTE_FLOW_ACTION_TYPE_SET_IPV6_DST:
3680                 case RTE_FLOW_ACTION_TYPE_SET_TP_SRC:
3681                 case RTE_FLOW_ACTION_TYPE_SET_TP_DST:
3682                 case RTE_FLOW_ACTION_TYPE_SET_TTL:
3683                 case RTE_FLOW_ACTION_TYPE_DEC_TTL:
3684                 case RTE_FLOW_ACTION_TYPE_SET_MAC_SRC:
3685                 case RTE_FLOW_ACTION_TYPE_SET_MAC_DST:
3686                         na_act_index =
3687                                 mnl_attr_nest_start(nlh, na_act_index_cur++);
3688                         flow_tcf_create_pedit_mnl_msg(nlh,
3689                                                       &actions, item_flags);
3690                         mnl_attr_nest_end(nlh, na_act_index);
3691                         break;
3692                 default:
3693                         return rte_flow_error_set(error, ENOTSUP,
3694                                                   RTE_FLOW_ERROR_TYPE_ACTION,
3695                                                   actions,
3696                                                   "action not supported");
3697                 }
3698         }
3699         assert(na_flower);
3700         assert(na_flower_act);
3701         mnl_attr_nest_end(nlh, na_flower_act);
3702         mnl_attr_put_u32(nlh, TCA_FLOWER_FLAGS, decap.vxlan ?
3703                                                 0 : TCA_CLS_FLAGS_SKIP_SW);
3704         mnl_attr_nest_end(nlh, na_flower);
3705         if (dev_flow->tcf.tunnel && dev_flow->tcf.tunnel->ifindex_ptr)
3706                 dev_flow->tcf.tunnel->ifindex_org =
3707                         *dev_flow->tcf.tunnel->ifindex_ptr;
3708         assert(dev_flow->tcf.nlsize >= nlh->nlmsg_len);
3709         return 0;
3710 }
3711
3712 /**
3713  * Send Netlink message with acknowledgment.
3714  *
3715  * @param tcf
3716  *   Flow context to use.
3717  * @param nlh
3718  *   Message to send. This function always raises the NLM_F_ACK flag before
3719  *   sending.
3720  * @param[in] cb
3721  *   Callback handler for received message.
3722  * @param[in] arg
3723  *   Context pointer for callback handler.
3724  *
3725  * @return
3726  *   0 on success, a negative errno value otherwise and rte_errno is set.
3727  */
3728 static int
3729 flow_tcf_nl_ack(struct mlx5_flow_tcf_context *tcf,
3730                 struct nlmsghdr *nlh,
3731                 mnl_cb_t cb, void *arg)
3732 {
3733         unsigned int portid = mnl_socket_get_portid(tcf->nl);
3734         uint32_t seq = tcf->seq++;
3735         int ret, err = 0;
3736
3737         assert(tcf->nl);
3738         assert(tcf->buf);
3739         if (!seq) {
3740                 /* seq 0 is reserved for kernel event-driven notifications. */
3741                 seq = tcf->seq++;
3742         }
3743         nlh->nlmsg_seq = seq;
3744         nlh->nlmsg_flags |= NLM_F_ACK;
3745         ret = mnl_socket_sendto(tcf->nl, nlh, nlh->nlmsg_len);
3746         if (ret <= 0) {
3747                 /* Message send error occurres. */
3748                 rte_errno = errno;
3749                 return -rte_errno;
3750         }
3751         nlh = (struct nlmsghdr *)(tcf->buf);
3752         /*
3753          * The following loop postpones non-fatal errors until multipart
3754          * messages are complete.
3755          */
3756         while (true) {
3757                 ret = mnl_socket_recvfrom(tcf->nl, tcf->buf, tcf->buf_size);
3758                 if (ret < 0) {
3759                         err = errno;
3760                         /*
3761                          * In case of overflow Will receive till
3762                          * end of multipart message. We may lost part
3763                          * of reply messages but mark and return an error.
3764                          */
3765                         if (err != ENOSPC ||
3766                             !(nlh->nlmsg_flags & NLM_F_MULTI) ||
3767                             nlh->nlmsg_type == NLMSG_DONE)
3768                                 break;
3769                 } else {
3770                         ret = mnl_cb_run(nlh, ret, seq, portid, cb, arg);
3771                         if (!ret) {
3772                                 /*
3773                                  * libmnl returns 0 if DONE or
3774                                  * success ACK message found.
3775                                  */
3776                                 break;
3777                         }
3778                         if (ret < 0) {
3779                                 /*
3780                                  * ACK message with error found
3781                                  * or some error occurred.
3782                                  */
3783                                 err = errno;
3784                                 break;
3785                         }
3786                         /* We should continue receiving. */
3787                 }
3788         }
3789         if (!err)
3790                 return 0;
3791         rte_errno = err;
3792         return -err;
3793 }
3794
3795 #define MNL_BUF_EXTRA_SPACE 16
3796 #define MNL_REQUEST_SIZE_MIN 256
3797 #define MNL_REQUEST_SIZE_MAX 2048
3798 #define MNL_REQUEST_SIZE RTE_MIN(RTE_MAX(sysconf(_SC_PAGESIZE), \
3799                                  MNL_REQUEST_SIZE_MIN), MNL_REQUEST_SIZE_MAX)
3800
3801 /* Data structures used by flow_tcf_xxx_cb() routines. */
3802 struct tcf_nlcb_buf {
3803         LIST_ENTRY(tcf_nlcb_buf) next;
3804         uint32_t size;
3805         alignas(struct nlmsghdr)
3806         uint8_t msg[]; /**< Netlink message data. */
3807 };
3808
3809 struct tcf_nlcb_context {
3810         unsigned int ifindex; /**< Base interface index. */
3811         uint32_t bufsize;
3812         LIST_HEAD(, tcf_nlcb_buf) nlbuf;
3813 };
3814
3815 /**
3816  * Allocate space for netlink command in buffer list
3817  *
3818  * @param[in, out] ctx
3819  *   Pointer to callback context with command buffers list.
3820  * @param[in] size
3821  *   Required size of data buffer to be allocated.
3822  *
3823  * @return
3824  *   Pointer to allocated memory, aligned as message header.
3825  *   NULL if some error occurred.
3826  */
3827 static struct nlmsghdr *
3828 flow_tcf_alloc_nlcmd(struct tcf_nlcb_context *ctx, uint32_t size)
3829 {
3830         struct tcf_nlcb_buf *buf;
3831         struct nlmsghdr *nlh;
3832
3833         size = NLMSG_ALIGN(size);
3834         buf = LIST_FIRST(&ctx->nlbuf);
3835         if (buf && (buf->size + size) <= ctx->bufsize) {
3836                 nlh = (struct nlmsghdr *)&buf->msg[buf->size];
3837                 buf->size += size;
3838                 return nlh;
3839         }
3840         if (size > ctx->bufsize) {
3841                 DRV_LOG(WARNING, "netlink: too long command buffer requested");
3842                 return NULL;
3843         }
3844         buf = rte_malloc(__func__,
3845                         ctx->bufsize + sizeof(struct tcf_nlcb_buf),
3846                         alignof(struct tcf_nlcb_buf));
3847         if (!buf) {
3848                 DRV_LOG(WARNING, "netlink: no memory for command buffer");
3849                 return NULL;
3850         }
3851         LIST_INSERT_HEAD(&ctx->nlbuf, buf, next);
3852         buf->size = size;
3853         nlh = (struct nlmsghdr *)&buf->msg[0];
3854         return nlh;
3855 }
3856
3857 /**
3858  * Send the buffers with prepared netlink commands. Scans the list and
3859  * sends all found buffers. Buffers are sent and freed anyway in order
3860  * to prevent memory leakage if some every message in received packet.
3861  *
3862  * @param[in] tcf
3863  *   Context object initialized by mlx5_flow_tcf_context_create().
3864  * @param[in, out] ctx
3865  *   Pointer to callback context with command buffers list.
3866  *
3867  * @return
3868  *   Zero value on success, negative errno value otherwise
3869  *   and rte_errno is set.
3870  */
3871 static int
3872 flow_tcf_send_nlcmd(struct mlx5_flow_tcf_context *tcf,
3873                     struct tcf_nlcb_context *ctx)
3874 {
3875         struct tcf_nlcb_buf *bc = LIST_FIRST(&ctx->nlbuf);
3876         int ret = 0;
3877
3878         while (bc) {
3879                 struct tcf_nlcb_buf *bn = LIST_NEXT(bc, next);
3880                 struct nlmsghdr *nlh;
3881                 uint32_t msg = 0;
3882                 int rc;
3883
3884                 while (msg < bc->size) {
3885                         /*
3886                          * Send Netlink commands from buffer in one by one
3887                          * fashion. If we send multiple rule deletion commands
3888                          * in one Netlink message and some error occurs it may
3889                          * cause multiple ACK error messages and break sequence
3890                          * numbers of Netlink communication, because we expect
3891                          * the only one ACK reply.
3892                          */
3893                         assert((bc->size - msg) >= sizeof(struct nlmsghdr));
3894                         nlh = (struct nlmsghdr *)&bc->msg[msg];
3895                         assert((bc->size - msg) >= nlh->nlmsg_len);
3896                         msg += nlh->nlmsg_len;
3897                         rc = flow_tcf_nl_ack(tcf, nlh, NULL, NULL);
3898                         if (rc) {
3899                                 DRV_LOG(WARNING,
3900                                         "netlink: cleanup error %d", rc);
3901                                 if (!ret)
3902                                         ret = rc;
3903                         }
3904                 }
3905                 rte_free(bc);
3906                 bc = bn;
3907         }
3908         LIST_INIT(&ctx->nlbuf);
3909         return ret;
3910 }
3911
3912 /**
3913  * Collect local IP address rules with scope link attribute  on specified
3914  * network device. This is callback routine called by libmnl mnl_cb_run()
3915  * in loop for every message in received packet.
3916  *
3917  * @param[in] nlh
3918  *   Pointer to reply header.
3919  * @param[in, out] arg
3920  *   Opaque data pointer for this callback.
3921  *
3922  * @return
3923  *   A positive, nonzero value on success, negative errno value otherwise
3924  *   and rte_errno is set.
3925  */
3926 static int
3927 flow_tcf_collect_local_cb(const struct nlmsghdr *nlh, void *arg)
3928 {
3929         struct tcf_nlcb_context *ctx = arg;
3930         struct nlmsghdr *cmd;
3931         struct ifaddrmsg *ifa;
3932         struct nlattr *na;
3933         struct nlattr *na_local = NULL;
3934         struct nlattr *na_peer = NULL;
3935         unsigned char family;
3936         uint32_t size;
3937
3938         if (nlh->nlmsg_type != RTM_NEWADDR) {
3939                 rte_errno = EINVAL;
3940                 return -rte_errno;
3941         }
3942         ifa = mnl_nlmsg_get_payload(nlh);
3943         family = ifa->ifa_family;
3944         if (ifa->ifa_index != ctx->ifindex ||
3945             ifa->ifa_scope != RT_SCOPE_LINK ||
3946             !(ifa->ifa_flags & IFA_F_PERMANENT) ||
3947             (family != AF_INET && family != AF_INET6))
3948                 return 1;
3949         mnl_attr_for_each(na, nlh, sizeof(*ifa)) {
3950                 switch (mnl_attr_get_type(na)) {
3951                 case IFA_LOCAL:
3952                         na_local = na;
3953                         break;
3954                 case IFA_ADDRESS:
3955                         na_peer = na;
3956                         break;
3957                 }
3958                 if (na_local && na_peer)
3959                         break;
3960         }
3961         if (!na_local || !na_peer)
3962                 return 1;
3963         /* Local rule found with scope link, permanent and assigned peer. */
3964         size = MNL_ALIGN(sizeof(struct nlmsghdr)) +
3965                MNL_ALIGN(sizeof(struct ifaddrmsg)) +
3966                (family == AF_INET6 ? 2 * SZ_NLATTR_DATA_OF(IPV6_ADDR_LEN)
3967                                    : 2 * SZ_NLATTR_TYPE_OF(uint32_t));
3968         cmd = flow_tcf_alloc_nlcmd(ctx, size);
3969         if (!cmd) {
3970                 rte_errno = ENOMEM;
3971                 return -rte_errno;
3972         }
3973         cmd = mnl_nlmsg_put_header(cmd);
3974         cmd->nlmsg_type = RTM_DELADDR;
3975         cmd->nlmsg_flags = NLM_F_REQUEST;
3976         ifa = mnl_nlmsg_put_extra_header(cmd, sizeof(*ifa));
3977         ifa->ifa_flags = IFA_F_PERMANENT;
3978         ifa->ifa_scope = RT_SCOPE_LINK;
3979         ifa->ifa_index = ctx->ifindex;
3980         if (family == AF_INET) {
3981                 ifa->ifa_family = AF_INET;
3982                 ifa->ifa_prefixlen = 32;
3983                 mnl_attr_put_u32(cmd, IFA_LOCAL, mnl_attr_get_u32(na_local));
3984                 mnl_attr_put_u32(cmd, IFA_ADDRESS, mnl_attr_get_u32(na_peer));
3985         } else {
3986                 ifa->ifa_family = AF_INET6;
3987                 ifa->ifa_prefixlen = 128;
3988                 mnl_attr_put(cmd, IFA_LOCAL, IPV6_ADDR_LEN,
3989                         mnl_attr_get_payload(na_local));
3990                 mnl_attr_put(cmd, IFA_ADDRESS, IPV6_ADDR_LEN,
3991                         mnl_attr_get_payload(na_peer));
3992         }
3993         assert(size == cmd->nlmsg_len);
3994         return 1;
3995 }
3996
3997 /**
3998  * Cleanup the local IP addresses on outer interface.
3999  *
4000  * @param[in] tcf
4001  *   Context object initialized by mlx5_flow_tcf_context_create().
4002  * @param[in] ifindex
4003  *   Network inferface index to perform cleanup.
4004  */
4005 static void
4006 flow_tcf_encap_local_cleanup(struct mlx5_flow_tcf_context *tcf,
4007                             unsigned int ifindex)
4008 {
4009         struct nlmsghdr *nlh;
4010         struct ifaddrmsg *ifa;
4011         struct tcf_nlcb_context ctx = {
4012                 .ifindex = ifindex,
4013                 .bufsize = MNL_REQUEST_SIZE,
4014                 .nlbuf = LIST_HEAD_INITIALIZER(),
4015         };
4016         int ret;
4017
4018         assert(ifindex);
4019         /*
4020          * Seek and destroy leftovers of local IP addresses with
4021          * matching properties "scope link".
4022          */
4023         nlh = mnl_nlmsg_put_header(tcf->buf);
4024         nlh->nlmsg_type = RTM_GETADDR;
4025         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
4026         ifa = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifa));
4027         ifa->ifa_family = AF_UNSPEC;
4028         ifa->ifa_index = ifindex;
4029         ifa->ifa_scope = RT_SCOPE_LINK;
4030         ret = flow_tcf_nl_ack(tcf, nlh, flow_tcf_collect_local_cb, &ctx);
4031         if (ret)
4032                 DRV_LOG(WARNING, "netlink: query device list error %d", ret);
4033         ret = flow_tcf_send_nlcmd(tcf, &ctx);
4034         if (ret)
4035                 DRV_LOG(WARNING, "netlink: device delete error %d", ret);
4036 }
4037
4038 /**
4039  * Collect neigh permament rules on specified network device.
4040  * This is callback routine called by libmnl mnl_cb_run() in loop for
4041  * every message in received packet.
4042  *
4043  * @param[in] nlh
4044  *   Pointer to reply header.
4045  * @param[in, out] arg
4046  *   Opaque data pointer for this callback.
4047  *
4048  * @return
4049  *   A positive, nonzero value on success, negative errno value otherwise
4050  *   and rte_errno is set.
4051  */
4052 static int
4053 flow_tcf_collect_neigh_cb(const struct nlmsghdr *nlh, void *arg)
4054 {
4055         struct tcf_nlcb_context *ctx = arg;
4056         struct nlmsghdr *cmd;
4057         struct ndmsg *ndm;
4058         struct nlattr *na;
4059         struct nlattr *na_ip = NULL;
4060         struct nlattr *na_mac = NULL;
4061         unsigned char family;
4062         uint32_t size;
4063
4064         if (nlh->nlmsg_type != RTM_NEWNEIGH) {
4065                 rte_errno = EINVAL;
4066                 return -rte_errno;
4067         }
4068         ndm = mnl_nlmsg_get_payload(nlh);
4069         family = ndm->ndm_family;
4070         if (ndm->ndm_ifindex != (int)ctx->ifindex ||
4071            !(ndm->ndm_state & NUD_PERMANENT) ||
4072            (family != AF_INET && family != AF_INET6))
4073                 return 1;
4074         mnl_attr_for_each(na, nlh, sizeof(*ndm)) {
4075                 switch (mnl_attr_get_type(na)) {
4076                 case NDA_DST:
4077                         na_ip = na;
4078                         break;
4079                 case NDA_LLADDR:
4080                         na_mac = na;
4081                         break;
4082                 }
4083                 if (na_mac && na_ip)
4084                         break;
4085         }
4086         if (!na_mac || !na_ip)
4087                 return 1;
4088         /* Neigh rule with permenent attribute found. */
4089         size = MNL_ALIGN(sizeof(struct nlmsghdr)) +
4090                MNL_ALIGN(sizeof(struct ndmsg)) +
4091                SZ_NLATTR_DATA_OF(ETHER_ADDR_LEN) +
4092                (family == AF_INET6 ? SZ_NLATTR_DATA_OF(IPV6_ADDR_LEN)
4093                                    : SZ_NLATTR_TYPE_OF(uint32_t));
4094         cmd = flow_tcf_alloc_nlcmd(ctx, size);
4095         if (!cmd) {
4096                 rte_errno = ENOMEM;
4097                 return -rte_errno;
4098         }
4099         cmd = mnl_nlmsg_put_header(cmd);
4100         cmd->nlmsg_type = RTM_DELNEIGH;
4101         cmd->nlmsg_flags = NLM_F_REQUEST;
4102         ndm = mnl_nlmsg_put_extra_header(cmd, sizeof(*ndm));
4103         ndm->ndm_ifindex = ctx->ifindex;
4104         ndm->ndm_state = NUD_PERMANENT;
4105         ndm->ndm_flags = 0;
4106         ndm->ndm_type = 0;
4107         if (family == AF_INET) {
4108                 ndm->ndm_family = AF_INET;
4109                 mnl_attr_put_u32(cmd, NDA_DST, mnl_attr_get_u32(na_ip));
4110         } else {
4111                 ndm->ndm_family = AF_INET6;
4112                 mnl_attr_put(cmd, NDA_DST, IPV6_ADDR_LEN,
4113                              mnl_attr_get_payload(na_ip));
4114         }
4115         mnl_attr_put(cmd, NDA_LLADDR, ETHER_ADDR_LEN,
4116                      mnl_attr_get_payload(na_mac));
4117         assert(size == cmd->nlmsg_len);
4118         return 1;
4119 }
4120
4121 /**
4122  * Cleanup the neigh rules on outer interface.
4123  *
4124  * @param[in] tcf
4125  *   Context object initialized by mlx5_flow_tcf_context_create().
4126  * @param[in] ifindex
4127  *   Network inferface index to perform cleanup.
4128  */
4129 static void
4130 flow_tcf_encap_neigh_cleanup(struct mlx5_flow_tcf_context *tcf,
4131                             unsigned int ifindex)
4132 {
4133         struct nlmsghdr *nlh;
4134         struct ndmsg *ndm;
4135         struct tcf_nlcb_context ctx = {
4136                 .ifindex = ifindex,
4137                 .bufsize = MNL_REQUEST_SIZE,
4138                 .nlbuf = LIST_HEAD_INITIALIZER(),
4139         };
4140         int ret;
4141
4142         assert(ifindex);
4143         /* Seek and destroy leftovers of neigh rules. */
4144         nlh = mnl_nlmsg_put_header(tcf->buf);
4145         nlh->nlmsg_type = RTM_GETNEIGH;
4146         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
4147         ndm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ndm));
4148         ndm->ndm_family = AF_UNSPEC;
4149         ndm->ndm_ifindex = ifindex;
4150         ndm->ndm_state = NUD_PERMANENT;
4151         ret = flow_tcf_nl_ack(tcf, nlh, flow_tcf_collect_neigh_cb, &ctx);
4152         if (ret)
4153                 DRV_LOG(WARNING, "netlink: query device list error %d", ret);
4154         ret = flow_tcf_send_nlcmd(tcf, &ctx);
4155         if (ret)
4156                 DRV_LOG(WARNING, "netlink: device delete error %d", ret);
4157 }
4158
4159 /**
4160  * Collect indices of VXLAN encap/decap interfaces associated with device.
4161  * This is callback routine called by libmnl mnl_cb_run() in loop for
4162  * every message in received packet.
4163  *
4164  * @param[in] nlh
4165  *   Pointer to reply header.
4166  * @param[in, out] arg
4167  *   Opaque data pointer for this callback.
4168  *
4169  * @return
4170  *   A positive, nonzero value on success, negative errno value otherwise
4171  *   and rte_errno is set.
4172  */
4173 static int
4174 flow_tcf_collect_vxlan_cb(const struct nlmsghdr *nlh, void *arg)
4175 {
4176         struct tcf_nlcb_context *ctx = arg;
4177         struct nlmsghdr *cmd;
4178         struct ifinfomsg *ifm;
4179         struct nlattr *na;
4180         struct nlattr *na_info = NULL;
4181         struct nlattr *na_vxlan = NULL;
4182         bool found = false;
4183         unsigned int vxindex;
4184         uint32_t size;
4185
4186         if (nlh->nlmsg_type != RTM_NEWLINK) {
4187                 rte_errno = EINVAL;
4188                 return -rte_errno;
4189         }
4190         ifm = mnl_nlmsg_get_payload(nlh);
4191         if (!ifm->ifi_index) {
4192                 rte_errno = EINVAL;
4193                 return -rte_errno;
4194         }
4195         mnl_attr_for_each(na, nlh, sizeof(*ifm))
4196                 if (mnl_attr_get_type(na) == IFLA_LINKINFO) {
4197                         na_info = na;
4198                         break;
4199                 }
4200         if (!na_info)
4201                 return 1;
4202         mnl_attr_for_each_nested(na, na_info) {
4203                 switch (mnl_attr_get_type(na)) {
4204                 case IFLA_INFO_KIND:
4205                         if (!strncmp("vxlan", mnl_attr_get_str(na),
4206                                      mnl_attr_get_len(na)))
4207                                 found = true;
4208                         break;
4209                 case IFLA_INFO_DATA:
4210                         na_vxlan = na;
4211                         break;
4212                 }
4213                 if (found && na_vxlan)
4214                         break;
4215         }
4216         if (!found || !na_vxlan)
4217                 return 1;
4218         found = false;
4219         mnl_attr_for_each_nested(na, na_vxlan) {
4220                 if (mnl_attr_get_type(na) == IFLA_VXLAN_LINK &&
4221                     mnl_attr_get_u32(na) == ctx->ifindex) {
4222                         found = true;
4223                         break;
4224                 }
4225         }
4226         if (!found)
4227                 return 1;
4228         /* Attached VXLAN device found, store the command to delete. */
4229         vxindex = ifm->ifi_index;
4230         size = MNL_ALIGN(sizeof(struct nlmsghdr)) +
4231                MNL_ALIGN(sizeof(struct ifinfomsg));
4232         cmd = flow_tcf_alloc_nlcmd(ctx, size);
4233         if (!cmd) {
4234                 rte_errno = ENOMEM;
4235                 return -rte_errno;
4236         }
4237         cmd = mnl_nlmsg_put_header(cmd);
4238         cmd->nlmsg_type = RTM_DELLINK;
4239         cmd->nlmsg_flags = NLM_F_REQUEST;
4240         ifm = mnl_nlmsg_put_extra_header(cmd, sizeof(*ifm));
4241         ifm->ifi_family = AF_UNSPEC;
4242         ifm->ifi_index = vxindex;
4243         assert(size == cmd->nlmsg_len);
4244         return 1;
4245 }
4246
4247 /**
4248  * Cleanup the outer interface. Removes all found vxlan devices
4249  * attached to specified index, flushes the meigh and local IP
4250  * datavase.
4251  *
4252  * @param[in] tcf
4253  *   Context object initialized by mlx5_flow_tcf_context_create().
4254  * @param[in] ifindex
4255  *   Network inferface index to perform cleanup.
4256  */
4257 static void
4258 flow_tcf_encap_iface_cleanup(struct mlx5_flow_tcf_context *tcf,
4259                             unsigned int ifindex)
4260 {
4261         struct nlmsghdr *nlh;
4262         struct ifinfomsg *ifm;
4263         struct tcf_nlcb_context ctx = {
4264                 .ifindex = ifindex,
4265                 .bufsize = MNL_REQUEST_SIZE,
4266                 .nlbuf = LIST_HEAD_INITIALIZER(),
4267         };
4268         int ret;
4269
4270         assert(ifindex);
4271         /*
4272          * Seek and destroy leftover VXLAN encap/decap interfaces with
4273          * matching properties.
4274          */
4275         nlh = mnl_nlmsg_put_header(tcf->buf);
4276         nlh->nlmsg_type = RTM_GETLINK;
4277         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
4278         ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
4279         ifm->ifi_family = AF_UNSPEC;
4280         ret = flow_tcf_nl_ack(tcf, nlh, flow_tcf_collect_vxlan_cb, &ctx);
4281         if (ret)
4282                 DRV_LOG(WARNING, "netlink: query device list error %d", ret);
4283         ret = flow_tcf_send_nlcmd(tcf, &ctx);
4284         if (ret)
4285                 DRV_LOG(WARNING, "netlink: device delete error %d", ret);
4286 }
4287
4288 /**
4289  * Emit Netlink message to add/remove local address to the outer device.
4290  * The address being added is visible within the link only (scope link).
4291  *
4292  * Note that an implicit route is maintained by the kernel due to the
4293  * presence of a peer address (IFA_ADDRESS).
4294  *
4295  * These rules are used for encapsultion only and allow to assign
4296  * the outer tunnel source IP address.
4297  *
4298  * @param[in] tcf
4299  *   Libmnl socket context object.
4300  * @param[in] encap
4301  *   Encapsulation properties (source address and its peer).
4302  * @param[in] ifindex
4303  *   Network interface to apply rule.
4304  * @param[in] enable
4305  *   Toggle between add and remove.
4306  * @param[out] error
4307  *   Perform verbose error reporting if not NULL.
4308  *
4309  * @return
4310  *   0 on success, a negative errno value otherwise and rte_errno is set.
4311  */
4312 static int
4313 flow_tcf_rule_local(struct mlx5_flow_tcf_context *tcf,
4314                     const struct flow_tcf_vxlan_encap *encap,
4315                     unsigned int ifindex,
4316                     bool enable,
4317                     struct rte_flow_error *error)
4318 {
4319         struct nlmsghdr *nlh;
4320         struct ifaddrmsg *ifa;
4321         alignas(struct nlmsghdr)
4322         uint8_t buf[mnl_nlmsg_size(sizeof(*ifa) + 128)];
4323
4324         nlh = mnl_nlmsg_put_header(buf);
4325         nlh->nlmsg_type = enable ? RTM_NEWADDR : RTM_DELADDR;
4326         nlh->nlmsg_flags =
4327                 NLM_F_REQUEST | (enable ? NLM_F_CREATE | NLM_F_REPLACE : 0);
4328         nlh->nlmsg_seq = 0;
4329         ifa = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifa));
4330         ifa->ifa_flags = IFA_F_PERMANENT;
4331         ifa->ifa_scope = RT_SCOPE_LINK;
4332         ifa->ifa_index = ifindex;
4333         if (encap->mask & FLOW_TCF_ENCAP_IPV4_SRC) {
4334                 ifa->ifa_family = AF_INET;
4335                 ifa->ifa_prefixlen = 32;
4336                 mnl_attr_put_u32(nlh, IFA_LOCAL, encap->ipv4.src);
4337                 if (encap->mask & FLOW_TCF_ENCAP_IPV4_DST)
4338                         mnl_attr_put_u32(nlh, IFA_ADDRESS,
4339                                               encap->ipv4.dst);
4340         } else {
4341                 assert(encap->mask & FLOW_TCF_ENCAP_IPV6_SRC);
4342                 ifa->ifa_family = AF_INET6;
4343                 ifa->ifa_prefixlen = 128;
4344                 mnl_attr_put(nlh, IFA_LOCAL,
4345                                   sizeof(encap->ipv6.src),
4346                                   &encap->ipv6.src);
4347                 if (encap->mask & FLOW_TCF_ENCAP_IPV6_DST)
4348                         mnl_attr_put(nlh, IFA_ADDRESS,
4349                                           sizeof(encap->ipv6.dst),
4350                                           &encap->ipv6.dst);
4351         }
4352         if (!flow_tcf_nl_ack(tcf, nlh, NULL, NULL))
4353                 return 0;
4354         return rte_flow_error_set(error, rte_errno,
4355                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4356                                   "netlink: cannot complete IFA request"
4357                                   " (ip addr add)");
4358 }
4359
4360 /**
4361  * Emit Netlink message to add/remove neighbor.
4362  *
4363  * @param[in] tcf
4364  *   Libmnl socket context object.
4365  * @param[in] encap
4366  *   Encapsulation properties (destination address).
4367  * @param[in] ifindex
4368  *   Network interface.
4369  * @param[in] enable
4370  *   Toggle between add and remove.
4371  * @param[out] error
4372  *   Perform verbose error reporting if not NULL.
4373  *
4374  * @return
4375  *   0 on success, a negative errno value otherwise and rte_errno is set.
4376  */
4377 static int
4378 flow_tcf_rule_neigh(struct mlx5_flow_tcf_context *tcf,
4379                      const struct flow_tcf_vxlan_encap *encap,
4380                      unsigned int ifindex,
4381                      bool enable,
4382                      struct rte_flow_error *error)
4383 {
4384         struct nlmsghdr *nlh;
4385         struct ndmsg *ndm;
4386         alignas(struct nlmsghdr)
4387         uint8_t buf[mnl_nlmsg_size(sizeof(*ndm) + 128)];
4388
4389         nlh = mnl_nlmsg_put_header(buf);
4390         nlh->nlmsg_type = enable ? RTM_NEWNEIGH : RTM_DELNEIGH;
4391         nlh->nlmsg_flags =
4392                 NLM_F_REQUEST | (enable ? NLM_F_CREATE | NLM_F_REPLACE : 0);
4393         nlh->nlmsg_seq = 0;
4394         ndm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ndm));
4395         ndm->ndm_ifindex = ifindex;
4396         ndm->ndm_state = NUD_PERMANENT;
4397         ndm->ndm_flags = 0;
4398         ndm->ndm_type = 0;
4399         if (encap->mask & FLOW_TCF_ENCAP_IPV4_DST) {
4400                 ndm->ndm_family = AF_INET;
4401                 mnl_attr_put_u32(nlh, NDA_DST, encap->ipv4.dst);
4402         } else {
4403                 assert(encap->mask & FLOW_TCF_ENCAP_IPV6_DST);
4404                 ndm->ndm_family = AF_INET6;
4405                 mnl_attr_put(nlh, NDA_DST, sizeof(encap->ipv6.dst),
4406                                                  &encap->ipv6.dst);
4407         }
4408         if (encap->mask & FLOW_TCF_ENCAP_ETH_SRC && enable)
4409                 DRV_LOG(WARNING,
4410                         "outer ethernet source address cannot be "
4411                         "forced for VXLAN encapsulation");
4412         if (encap->mask & FLOW_TCF_ENCAP_ETH_DST)
4413                 mnl_attr_put(nlh, NDA_LLADDR, sizeof(encap->eth.dst),
4414                                                     &encap->eth.dst);
4415         if (!flow_tcf_nl_ack(tcf, nlh, NULL, NULL))
4416                 return 0;
4417         return rte_flow_error_set(error, rte_errno,
4418                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4419                                   "netlink: cannot complete ND request"
4420                                   " (ip neigh)");
4421 }
4422
4423 /**
4424  * Manage the local IP addresses and their peers IP addresses on the
4425  * outer interface for encapsulation purposes. The kernel searches the
4426  * appropriate device for tunnel egress traffic using the outer source
4427  * IP, this IP should be assigned to the outer network device, otherwise
4428  * kernel rejects the rule.
4429  *
4430  * Adds or removes the addresses using the Netlink command like this:
4431  *   ip addr add <src_ip> peer <dst_ip> scope link dev <ifouter>
4432  *
4433  * The addresses are local to the netdev ("scope link"), this reduces
4434  * the risk of conflicts. Note that an implicit route is maintained by
4435  * the kernel due to the presence of a peer address (IFA_ADDRESS).
4436  *
4437  * @param[in] tcf
4438  *   Libmnl socket context object.
4439  * @param[in] vtep
4440  *   VTEP object, contains rule database and ifouter index.
4441  * @param[in] dev_flow
4442  *   Flow object, contains the tunnel parameters (for encap only).
4443  * @param[in] enable
4444  *   Toggle between add and remove.
4445  * @param[out] error
4446  *   Perform verbose error reporting if not NULL.
4447  *
4448  * @return
4449  *   0 on success, a negative errno value otherwise and rte_errno is set.
4450  */
4451 static int
4452 flow_tcf_encap_local(struct mlx5_flow_tcf_context *tcf,
4453                      struct tcf_vtep *vtep,
4454                      struct mlx5_flow *dev_flow,
4455                      bool enable,
4456                      struct rte_flow_error *error)
4457 {
4458         const struct flow_tcf_vxlan_encap *encap = dev_flow->tcf.vxlan_encap;
4459         struct tcf_local_rule *rule;
4460         bool found = false;
4461         int ret;
4462
4463         assert(encap);
4464         assert(encap->hdr.type == FLOW_TCF_TUNACT_VXLAN_ENCAP);
4465         if (encap->mask & FLOW_TCF_ENCAP_IPV4_SRC) {
4466                 assert(encap->mask & FLOW_TCF_ENCAP_IPV4_DST);
4467                 LIST_FOREACH(rule, &vtep->local, next) {
4468                         if (rule->mask & FLOW_TCF_ENCAP_IPV4_SRC &&
4469                             encap->ipv4.src == rule->ipv4.src &&
4470                             encap->ipv4.dst == rule->ipv4.dst) {
4471                                 found = true;
4472                                 break;
4473                         }
4474                 }
4475         } else {
4476                 assert(encap->mask & FLOW_TCF_ENCAP_IPV6_SRC);
4477                 assert(encap->mask & FLOW_TCF_ENCAP_IPV6_DST);
4478                 LIST_FOREACH(rule, &vtep->local, next) {
4479                         if (rule->mask & FLOW_TCF_ENCAP_IPV6_SRC &&
4480                             !memcmp(&encap->ipv6.src, &rule->ipv6.src,
4481                                             sizeof(encap->ipv6.src)) &&
4482                             !memcmp(&encap->ipv6.dst, &rule->ipv6.dst,
4483                                             sizeof(encap->ipv6.dst))) {
4484                                 found = true;
4485                                 break;
4486                         }
4487                 }
4488         }
4489         if (found) {
4490                 if (enable) {
4491                         rule->refcnt++;
4492                         return 0;
4493                 }
4494                 if (!rule->refcnt || !--rule->refcnt) {
4495                         LIST_REMOVE(rule, next);
4496                         return flow_tcf_rule_local(tcf, encap,
4497                                         vtep->ifouter, false, error);
4498                 }
4499                 return 0;
4500         }
4501         if (!enable) {
4502                 DRV_LOG(WARNING, "disabling not existing local rule");
4503                 rte_flow_error_set(error, ENOENT,
4504                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4505                                    "disabling not existing local rule");
4506                 return -ENOENT;
4507         }
4508         rule = rte_zmalloc(__func__, sizeof(struct tcf_local_rule),
4509                                 alignof(struct tcf_local_rule));
4510         if (!rule) {
4511                 rte_flow_error_set(error, ENOMEM,
4512                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4513                                    "unable to allocate memory for local rule");
4514                 return -rte_errno;
4515         }
4516         *rule = (struct tcf_local_rule){.refcnt = 0,
4517                                         .mask = 0,
4518                                         };
4519         if (encap->mask & FLOW_TCF_ENCAP_IPV4_SRC) {
4520                 rule->mask = FLOW_TCF_ENCAP_IPV4_SRC
4521                            | FLOW_TCF_ENCAP_IPV4_DST;
4522                 rule->ipv4.src = encap->ipv4.src;
4523                 rule->ipv4.dst = encap->ipv4.dst;
4524         } else {
4525                 rule->mask = FLOW_TCF_ENCAP_IPV6_SRC
4526                            | FLOW_TCF_ENCAP_IPV6_DST;
4527                 memcpy(&rule->ipv6.src, &encap->ipv6.src, IPV6_ADDR_LEN);
4528                 memcpy(&rule->ipv6.dst, &encap->ipv6.dst, IPV6_ADDR_LEN);
4529         }
4530         ret = flow_tcf_rule_local(tcf, encap, vtep->ifouter, true, error);
4531         if (ret) {
4532                 rte_free(rule);
4533                 return ret;
4534         }
4535         rule->refcnt++;
4536         LIST_INSERT_HEAD(&vtep->local, rule, next);
4537         return 0;
4538 }
4539
4540 /**
4541  * Manage the destination MAC/IP addresses neigh database, kernel uses
4542  * this one to determine the destination MAC address within encapsulation
4543  * header. Adds or removes the entries using the Netlink command like this:
4544  *   ip neigh add dev <ifouter> lladdr <dst_mac> to <dst_ip> nud permanent
4545  *
4546  * @param[in] tcf
4547  *   Libmnl socket context object.
4548  * @param[in] vtep
4549  *   VTEP object, contains rule database and ifouter index.
4550  * @param[in] dev_flow
4551  *   Flow object, contains the tunnel parameters (for encap only).
4552  * @param[in] enable
4553  *   Toggle between add and remove.
4554  * @param[out] error
4555  *   Perform verbose error reporting if not NULL.
4556  *
4557  * @return
4558  *   0 on success, a negative errno value otherwise and rte_errno is set.
4559  */
4560 static int
4561 flow_tcf_encap_neigh(struct mlx5_flow_tcf_context *tcf,
4562                      struct tcf_vtep *vtep,
4563                      struct mlx5_flow *dev_flow,
4564                      bool enable,
4565                      struct rte_flow_error *error)
4566 {
4567         const struct flow_tcf_vxlan_encap *encap = dev_flow->tcf.vxlan_encap;
4568         struct tcf_neigh_rule *rule;
4569         bool found = false;
4570         int ret;
4571
4572         assert(encap);
4573         assert(encap->hdr.type == FLOW_TCF_TUNACT_VXLAN_ENCAP);
4574         if (encap->mask & FLOW_TCF_ENCAP_IPV4_DST) {
4575                 assert(encap->mask & FLOW_TCF_ENCAP_IPV4_SRC);
4576                 LIST_FOREACH(rule, &vtep->neigh, next) {
4577                         if (rule->mask & FLOW_TCF_ENCAP_IPV4_DST &&
4578                             encap->ipv4.dst == rule->ipv4.dst) {
4579                                 found = true;
4580                                 break;
4581                         }
4582                 }
4583         } else {
4584                 assert(encap->mask & FLOW_TCF_ENCAP_IPV6_SRC);
4585                 assert(encap->mask & FLOW_TCF_ENCAP_IPV6_DST);
4586                 LIST_FOREACH(rule, &vtep->neigh, next) {
4587                         if (rule->mask & FLOW_TCF_ENCAP_IPV6_DST &&
4588                             !memcmp(&encap->ipv6.dst, &rule->ipv6.dst,
4589                                                 sizeof(encap->ipv6.dst))) {
4590                                 found = true;
4591                                 break;
4592                         }
4593                 }
4594         }
4595         if (found) {
4596                 if (memcmp(&encap->eth.dst, &rule->eth,
4597                            sizeof(encap->eth.dst))) {
4598                         DRV_LOG(WARNING, "Destination MAC differs"
4599                                          " in neigh rule");
4600                         rte_flow_error_set(error, EEXIST,
4601                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
4602                                            NULL, "Different MAC address"
4603                                            " neigh rule for the same"
4604                                            " destination IP");
4605                                         return -EEXIST;
4606                 }
4607                 if (enable) {
4608                         rule->refcnt++;
4609                         return 0;
4610                 }
4611                 if (!rule->refcnt || !--rule->refcnt) {
4612                         LIST_REMOVE(rule, next);
4613                         return flow_tcf_rule_neigh(tcf, encap,
4614                                                    vtep->ifouter,
4615                                                    false, error);
4616                 }
4617                 return 0;
4618         }
4619         if (!enable) {
4620                 DRV_LOG(WARNING, "Disabling not existing neigh rule");
4621                 rte_flow_error_set(error, ENOENT,
4622                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4623                                    "unable to allocate memory for neigh rule");
4624                 return -ENOENT;
4625         }
4626         rule = rte_zmalloc(__func__, sizeof(struct tcf_neigh_rule),
4627                                 alignof(struct tcf_neigh_rule));
4628         if (!rule) {
4629                 rte_flow_error_set(error, ENOMEM,
4630                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4631                                    "unable to allocate memory for neigh rule");
4632                 return -rte_errno;
4633         }
4634         *rule = (struct tcf_neigh_rule){.refcnt = 0,
4635                                         .mask = 0,
4636                                         };
4637         if (encap->mask & FLOW_TCF_ENCAP_IPV4_DST) {
4638                 rule->mask = FLOW_TCF_ENCAP_IPV4_DST;
4639                 rule->ipv4.dst = encap->ipv4.dst;
4640         } else {
4641                 rule->mask = FLOW_TCF_ENCAP_IPV6_DST;
4642                 memcpy(&rule->ipv6.dst, &encap->ipv6.dst, IPV6_ADDR_LEN);
4643         }
4644         memcpy(&rule->eth, &encap->eth.dst, sizeof(rule->eth));
4645         ret = flow_tcf_rule_neigh(tcf, encap, vtep->ifouter, true, error);
4646         if (ret) {
4647                 rte_free(rule);
4648                 return ret;
4649         }
4650         rule->refcnt++;
4651         LIST_INSERT_HEAD(&vtep->neigh, rule, next);
4652         return 0;
4653 }
4654
4655 /* VTEP device list is shared between PMD port instances. */
4656 static LIST_HEAD(, tcf_vtep) vtep_list_vxlan = LIST_HEAD_INITIALIZER();
4657 static pthread_mutex_t vtep_list_mutex = PTHREAD_MUTEX_INITIALIZER;
4658
4659 /**
4660  * Deletes VTEP network device.
4661  *
4662  * @param[in] tcf
4663  *   Context object initialized by mlx5_flow_tcf_context_create().
4664  * @param[in] vtep
4665  *   Object represinting the network device to delete. Memory
4666  *   allocated for this object is freed by routine.
4667  */
4668 static void
4669 flow_tcf_vtep_delete(struct mlx5_flow_tcf_context *tcf,
4670                      struct tcf_vtep *vtep)
4671 {
4672         struct nlmsghdr *nlh;
4673         struct ifinfomsg *ifm;
4674         alignas(struct nlmsghdr)
4675         uint8_t buf[mnl_nlmsg_size(MNL_ALIGN(sizeof(*ifm))) +
4676                     MNL_BUF_EXTRA_SPACE];
4677         int ret;
4678
4679         assert(!vtep->refcnt);
4680         /* Delete only ifaces those we actually created. */
4681         if (vtep->created && vtep->ifindex) {
4682                 DRV_LOG(INFO, "VTEP delete (%d)", vtep->ifindex);
4683                 nlh = mnl_nlmsg_put_header(buf);
4684                 nlh->nlmsg_type = RTM_DELLINK;
4685                 nlh->nlmsg_flags = NLM_F_REQUEST;
4686                 ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
4687                 ifm->ifi_family = AF_UNSPEC;
4688                 ifm->ifi_index = vtep->ifindex;
4689                 assert(sizeof(buf) >= nlh->nlmsg_len);
4690                 ret = flow_tcf_nl_ack(tcf, nlh, NULL, NULL);
4691                 if (ret)
4692                         DRV_LOG(WARNING, "netlink: error deleting vxlan"
4693                                          " encap/decap ifindex %u",
4694                                          ifm->ifi_index);
4695         }
4696         rte_free(vtep);
4697 }
4698
4699 /**
4700  * Creates VTEP network device.
4701  *
4702  * @param[in] tcf
4703  *   Context object initialized by mlx5_flow_tcf_context_create().
4704  * @param[in] ifouter
4705  *   Outer interface to attach new-created VXLAN device
4706  *   If zero the VXLAN device will not be attached to any device.
4707  *   These VTEPs are used for decapsulation and can be precreated
4708  *   and shared between processes.
4709  * @param[in] port
4710  *   UDP port of created VTEP device.
4711  * @param[out] error
4712  *   Perform verbose error reporting if not NULL.
4713  *
4714  * @return
4715  * Pointer to created device structure on success,
4716  * NULL otherwise and rte_errno is set.
4717  */
4718 #ifdef HAVE_IFLA_VXLAN_COLLECT_METADATA
4719 static struct tcf_vtep*
4720 flow_tcf_vtep_create(struct mlx5_flow_tcf_context *tcf,
4721                      unsigned int ifouter,
4722                      uint16_t port, struct rte_flow_error *error)
4723 {
4724         struct tcf_vtep *vtep;
4725         struct nlmsghdr *nlh;
4726         struct ifinfomsg *ifm;
4727         char name[sizeof(MLX5_VXLAN_DEVICE_PFX) + 24];
4728         alignas(struct nlmsghdr)
4729         uint8_t buf[mnl_nlmsg_size(sizeof(*ifm)) +
4730                     SZ_NLATTR_DATA_OF(sizeof(name)) +
4731                     SZ_NLATTR_NEST * 2 +
4732                     SZ_NLATTR_STRZ_OF("vxlan") +
4733                     SZ_NLATTR_DATA_OF(sizeof(uint32_t)) +
4734                     SZ_NLATTR_DATA_OF(sizeof(uint16_t)) +
4735                     SZ_NLATTR_DATA_OF(sizeof(uint8_t)) * 3 +
4736                     MNL_BUF_EXTRA_SPACE];
4737         struct nlattr *na_info;
4738         struct nlattr *na_vxlan;
4739         rte_be16_t vxlan_port = rte_cpu_to_be_16(port);
4740         int ret;
4741
4742         vtep = rte_zmalloc(__func__, sizeof(*vtep), alignof(struct tcf_vtep));
4743         if (!vtep) {
4744                 rte_flow_error_set(error, ENOMEM,
4745                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4746                                    "unable to allocate memory for VTEP");
4747                 return NULL;
4748         }
4749         *vtep = (struct tcf_vtep){
4750                         .port = port,
4751                         .local = LIST_HEAD_INITIALIZER(),
4752                         .neigh = LIST_HEAD_INITIALIZER(),
4753         };
4754         memset(buf, 0, sizeof(buf));
4755         nlh = mnl_nlmsg_put_header(buf);
4756         nlh->nlmsg_type = RTM_NEWLINK;
4757         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE  | NLM_F_EXCL;
4758         ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
4759         ifm->ifi_family = AF_UNSPEC;
4760         ifm->ifi_type = 0;
4761         ifm->ifi_index = 0;
4762         ifm->ifi_flags = IFF_UP;
4763         ifm->ifi_change = 0xffffffff;
4764         snprintf(name, sizeof(name), "%s%u", MLX5_VXLAN_DEVICE_PFX, port);
4765         mnl_attr_put_strz(nlh, IFLA_IFNAME, name);
4766         na_info = mnl_attr_nest_start(nlh, IFLA_LINKINFO);
4767         assert(na_info);
4768         mnl_attr_put_strz(nlh, IFLA_INFO_KIND, "vxlan");
4769         na_vxlan = mnl_attr_nest_start(nlh, IFLA_INFO_DATA);
4770         if (ifouter)
4771                 mnl_attr_put_u32(nlh, IFLA_VXLAN_LINK, ifouter);
4772         assert(na_vxlan);
4773         mnl_attr_put_u8(nlh, IFLA_VXLAN_COLLECT_METADATA, 1);
4774         mnl_attr_put_u8(nlh, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, 1);
4775         mnl_attr_put_u8(nlh, IFLA_VXLAN_LEARNING, 0);
4776         mnl_attr_put_u16(nlh, IFLA_VXLAN_PORT, vxlan_port);
4777         mnl_attr_nest_end(nlh, na_vxlan);
4778         mnl_attr_nest_end(nlh, na_info);
4779         assert(sizeof(buf) >= nlh->nlmsg_len);
4780         ret = flow_tcf_nl_ack(tcf, nlh, NULL, NULL);
4781         if (ret) {
4782                 DRV_LOG(WARNING,
4783                         "netlink: VTEP %s create failure (%d)",
4784                         name, rte_errno);
4785                 if (rte_errno != EEXIST || ifouter)
4786                         /*
4787                          * Some unhandled error occurred or device is
4788                          * for encapsulation and cannot be shared.
4789                          */
4790                         goto error;
4791         } else {
4792                 /*
4793                  * Mark device we actually created.
4794                  * We should explicitly delete
4795                  * when we do not need it anymore.
4796                  */
4797                 vtep->created = 1;
4798         }
4799         /* Try to get ifindex of created of pre-existing device. */
4800         ret = if_nametoindex(name);
4801         if (!ret) {
4802                 DRV_LOG(WARNING,
4803                         "VTEP %s failed to get index (%d)", name, errno);
4804                 rte_flow_error_set
4805                         (error, -errno,
4806                          RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4807                          "netlink: failed to retrieve VTEP ifindex");
4808                 goto error;
4809         }
4810         vtep->ifindex = ret;
4811         vtep->ifouter = ifouter;
4812         memset(buf, 0, sizeof(buf));
4813         nlh = mnl_nlmsg_put_header(buf);
4814         nlh->nlmsg_type = RTM_NEWLINK;
4815         nlh->nlmsg_flags = NLM_F_REQUEST;
4816         ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
4817         ifm->ifi_family = AF_UNSPEC;
4818         ifm->ifi_type = 0;
4819         ifm->ifi_index = vtep->ifindex;
4820         ifm->ifi_flags = IFF_UP;
4821         ifm->ifi_change = IFF_UP;
4822         ret = flow_tcf_nl_ack(tcf, nlh, NULL, NULL);
4823         if (ret) {
4824                 rte_flow_error_set(error, -errno,
4825                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4826                                    "netlink: failed to set VTEP link up");
4827                 DRV_LOG(WARNING, "netlink: VTEP %s set link up failure (%d)",
4828                         name, rte_errno);
4829                 goto clean;
4830         }
4831         ret = mlx5_flow_tcf_init(tcf, vtep->ifindex, error);
4832         if (ret) {
4833                 DRV_LOG(WARNING, "VTEP %s init failure (%d)", name, rte_errno);
4834                 goto clean;
4835         }
4836         DRV_LOG(INFO, "VTEP create (%d, %d)", vtep->port, vtep->ifindex);
4837         vtep->refcnt = 1;
4838         return vtep;
4839 clean:
4840         flow_tcf_vtep_delete(tcf, vtep);
4841         return NULL;
4842 error:
4843         rte_free(vtep);
4844         return NULL;
4845 }
4846 #else
4847 static struct tcf_vtep*
4848 flow_tcf_vtep_create(struct mlx5_flow_tcf_context *tcf __rte_unused,
4849                      unsigned int ifouter __rte_unused,
4850                      uint16_t port __rte_unused,
4851                      struct rte_flow_error *error)
4852 {
4853         rte_flow_error_set(error, ENOTSUP,
4854                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4855                            "netlink: failed to create VTEP, "
4856                            "vxlan metadata are not supported by kernel");
4857         return NULL;
4858 }
4859 #endif /* HAVE_IFLA_VXLAN_COLLECT_METADATA */
4860
4861 /**
4862  * Acquire target interface index for VXLAN tunneling decapsulation.
4863  * In order to share the UDP port within the other interfaces the
4864  * VXLAN device created as not attached to any interface (if created).
4865  *
4866  * @param[in] tcf
4867  *   Context object initialized by mlx5_flow_tcf_context_create().
4868  * @param[in] dev_flow
4869  *   Flow tcf object with tunnel structure pointer set.
4870  * @param[out] error
4871  *   Perform verbose error reporting if not NULL.
4872  * @return
4873  *   Interface descriptor pointer on success,
4874  *   NULL otherwise and rte_errno is set.
4875  */
4876 static struct tcf_vtep*
4877 flow_tcf_decap_vtep_acquire(struct mlx5_flow_tcf_context *tcf,
4878                             struct mlx5_flow *dev_flow,
4879                             struct rte_flow_error *error)
4880 {
4881         struct tcf_vtep *vtep;
4882         uint16_t port = dev_flow->tcf.vxlan_decap->udp_port;
4883
4884         LIST_FOREACH(vtep, &vtep_list_vxlan, next) {
4885                 if (vtep->port == port)
4886                         break;
4887         }
4888         if (vtep && vtep->ifouter) {
4889                 rte_flow_error_set(error, -errno,
4890                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
4891                                    "Failed to create decap VTEP with specified"
4892                                    " UDP port, atatched device exists");
4893                 return NULL;
4894         }
4895         if (vtep) {
4896                 /* Device exists, just increment the reference counter. */
4897                 vtep->refcnt++;
4898                 assert(vtep->ifindex);
4899                 return vtep;
4900         }
4901         /* No decapsulation device exists, try to create the new one. */
4902         vtep = flow_tcf_vtep_create(tcf, 0, port, error);
4903         if (vtep)
4904                 LIST_INSERT_HEAD(&vtep_list_vxlan, vtep, next);
4905         return vtep;
4906 }
4907
4908 /**
4909  * Aqcuire target interface index for VXLAN tunneling encapsulation.
4910  *
4911  * @param[in] tcf
4912  *   Context object initialized by mlx5_flow_tcf_context_create().
4913  * @param[in] ifouter
4914  *   Network interface index to attach VXLAN encap device to.
4915  * @param[in] dev_flow
4916  *   Flow tcf object with tunnel structure pointer set.
4917  * @param[out] error
4918  *   Perform verbose error reporting if not NULL.
4919  * @return
4920  *   Interface descriptor pointer on success,
4921  *   NULL otherwise and rte_errno is set.
4922  */
4923 static struct tcf_vtep*
4924 flow_tcf_encap_vtep_acquire(struct mlx5_flow_tcf_context *tcf,
4925                             unsigned int ifouter,
4926                             struct mlx5_flow *dev_flow __rte_unused,
4927                             struct rte_flow_error *error)
4928 {
4929         static uint16_t encap_port = MLX5_VXLAN_PORT_MIN - 1;
4930         struct tcf_vtep *vtep;
4931         int ret;
4932
4933         assert(ifouter);
4934         /* Look whether the attached VTEP for encap is created. */
4935         LIST_FOREACH(vtep, &vtep_list_vxlan, next) {
4936                 if (vtep->ifouter == ifouter)
4937                         break;
4938         }
4939         if (vtep) {
4940                 /* VTEP already exists, just increment the reference. */
4941                 vtep->refcnt++;
4942         } else {
4943                 uint16_t pcnt;
4944
4945                 /* Not found, we should create the new attached VTEP. */
4946                 flow_tcf_encap_iface_cleanup(tcf, ifouter);
4947                 flow_tcf_encap_local_cleanup(tcf, ifouter);
4948                 flow_tcf_encap_neigh_cleanup(tcf, ifouter);
4949                 for (pcnt = 0; pcnt <= (MLX5_VXLAN_PORT_MAX
4950                                      - MLX5_VXLAN_PORT_MIN); pcnt++) {
4951                         encap_port++;
4952                         /* Wraparound the UDP port index. */
4953                         if (encap_port < MLX5_VXLAN_PORT_MIN ||
4954                             encap_port > MLX5_VXLAN_PORT_MAX)
4955                                 encap_port = MLX5_VXLAN_PORT_MIN;
4956                         /* Check whether UDP port is in already in use. */
4957                         LIST_FOREACH(vtep, &vtep_list_vxlan, next) {
4958                                 if (vtep->port == encap_port)
4959                                         break;
4960                         }
4961                         if (vtep) {
4962                                 /* Port is in use, try the next one. */
4963                                 vtep = NULL;
4964                                 continue;
4965                         }
4966                         vtep = flow_tcf_vtep_create(tcf, ifouter,
4967                                                     encap_port, error);
4968                         if (vtep) {
4969                                 LIST_INSERT_HEAD(&vtep_list_vxlan, vtep, next);
4970                                 break;
4971                         }
4972                         if (rte_errno != EEXIST)
4973                                 break;
4974                 }
4975                 if (!vtep)
4976                         return NULL;
4977         }
4978         assert(vtep->ifouter == ifouter);
4979         assert(vtep->ifindex);
4980         /* Create local ipaddr with peer to specify the outer IPs. */
4981         ret = flow_tcf_encap_local(tcf, vtep, dev_flow, true, error);
4982         if (!ret) {
4983                 /* Create neigh rule to specify outer destination MAC. */
4984                 ret = flow_tcf_encap_neigh(tcf, vtep, dev_flow, true, error);
4985                 if (ret)
4986                         flow_tcf_encap_local(tcf, vtep,
4987                                              dev_flow, false, error);
4988         }
4989         if (ret) {
4990                 if (--vtep->refcnt == 0)
4991                         flow_tcf_vtep_delete(tcf, vtep);
4992                 return NULL;
4993         }
4994         return vtep;
4995 }
4996
4997 /**
4998  * Acquires target interface index for tunneling of any type.
4999  * Creates the new VTEP if needed.
5000  *
5001  * @param[in] tcf
5002  *   Context object initialized by mlx5_flow_tcf_context_create().
5003  * @param[in] ifouter
5004  *   Network interface index to attach VXLAN encap device to.
5005  * @param[in] dev_flow
5006  *   Flow tcf object with tunnel structure pointer set.
5007  * @param[out] error
5008  *   Perform verbose error reporting if not NULL.
5009  * @return
5010  *   Interface descriptor pointer on success,
5011  *   NULL otherwise and rte_errno is set.
5012  */
5013 static struct tcf_vtep*
5014 flow_tcf_vtep_acquire(struct mlx5_flow_tcf_context *tcf,
5015                       unsigned int ifouter,
5016                       struct mlx5_flow *dev_flow,
5017                       struct rte_flow_error *error)
5018 {
5019         struct tcf_vtep *vtep = NULL;
5020
5021         assert(dev_flow->tcf.tunnel);
5022         pthread_mutex_lock(&vtep_list_mutex);
5023         switch (dev_flow->tcf.tunnel->type) {
5024         case FLOW_TCF_TUNACT_VXLAN_ENCAP:
5025                 vtep = flow_tcf_encap_vtep_acquire(tcf, ifouter,
5026                                                   dev_flow, error);
5027                 break;
5028         case FLOW_TCF_TUNACT_VXLAN_DECAP:
5029                 vtep = flow_tcf_decap_vtep_acquire(tcf, dev_flow, error);
5030                 break;
5031         default:
5032                 rte_flow_error_set(error, ENOTSUP,
5033                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5034                                    "unsupported tunnel type");
5035                 break;
5036         }
5037         pthread_mutex_unlock(&vtep_list_mutex);
5038         return vtep;
5039 }
5040
5041 /**
5042  * Release tunneling interface by ifindex. Decrements reference
5043  * counter and actually removes the device if counter is zero.
5044  *
5045  * @param[in] tcf
5046  *   Context object initialized by mlx5_flow_tcf_context_create().
5047  * @param[in] vtep
5048  *   VTEP device descriptor structure.
5049  * @param[in] dev_flow
5050  *   Flow tcf object with tunnel structure pointer set.
5051  */
5052 static void
5053 flow_tcf_vtep_release(struct mlx5_flow_tcf_context *tcf,
5054                       struct tcf_vtep *vtep,
5055                       struct mlx5_flow *dev_flow)
5056 {
5057         assert(dev_flow->tcf.tunnel);
5058         pthread_mutex_lock(&vtep_list_mutex);
5059         switch (dev_flow->tcf.tunnel->type) {
5060         case FLOW_TCF_TUNACT_VXLAN_DECAP:
5061                 break;
5062         case FLOW_TCF_TUNACT_VXLAN_ENCAP:
5063                 /* Remove the encap ancillary rules first. */
5064                 flow_tcf_encap_neigh(tcf, vtep, dev_flow, false, NULL);
5065                 flow_tcf_encap_local(tcf, vtep, dev_flow, false, NULL);
5066                 break;
5067         default:
5068                 assert(false);
5069                 DRV_LOG(WARNING, "Unsupported tunnel type");
5070                 break;
5071         }
5072         assert(vtep->refcnt);
5073         if (--vtep->refcnt == 0) {
5074                 LIST_REMOVE(vtep, next);
5075                 flow_tcf_vtep_delete(tcf, vtep);
5076         }
5077         pthread_mutex_unlock(&vtep_list_mutex);
5078 }
5079
5080
5081 /**
5082  * Apply flow to E-Switch by sending Netlink message.
5083  *
5084  * @param[in] dev
5085  *   Pointer to Ethernet device.
5086  * @param[in, out] flow
5087  *   Pointer to the sub flow.
5088  * @param[out] error
5089  *   Pointer to the error structure.
5090  *
5091  * @return
5092  *   0 on success, a negative errno value otherwise and rte_ernno is set.
5093  */
5094 static int
5095 flow_tcf_apply(struct rte_eth_dev *dev, struct rte_flow *flow,
5096                struct rte_flow_error *error)
5097 {
5098         struct priv *priv = dev->data->dev_private;
5099         struct mlx5_flow_tcf_context *ctx = priv->tcf_context;
5100         struct mlx5_flow *dev_flow;
5101         struct nlmsghdr *nlh;
5102
5103         dev_flow = LIST_FIRST(&flow->dev_flows);
5104         /* E-Switch flow can't be expanded. */
5105         assert(!LIST_NEXT(dev_flow, next));
5106         if (dev_flow->tcf.applied)
5107                 return 0;
5108         nlh = dev_flow->tcf.nlh;
5109         nlh->nlmsg_type = RTM_NEWTFILTER;
5110         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
5111         if (dev_flow->tcf.tunnel) {
5112                 /*
5113                  * Replace the interface index, target for
5114                  * encapsulation, source for decapsulation.
5115                  */
5116                 assert(!dev_flow->tcf.tunnel->vtep);
5117                 assert(dev_flow->tcf.tunnel->ifindex_ptr);
5118                 /* Acquire actual VTEP device when rule is being applied. */
5119                 dev_flow->tcf.tunnel->vtep =
5120                         flow_tcf_vtep_acquire(ctx,
5121                                         dev_flow->tcf.tunnel->ifindex_org,
5122                                         dev_flow, error);
5123                 if (!dev_flow->tcf.tunnel->vtep)
5124                         return -rte_errno;
5125                 DRV_LOG(INFO, "Replace ifindex: %d->%d",
5126                                 dev_flow->tcf.tunnel->vtep->ifindex,
5127                                 dev_flow->tcf.tunnel->ifindex_org);
5128                 *dev_flow->tcf.tunnel->ifindex_ptr =
5129                         dev_flow->tcf.tunnel->vtep->ifindex;
5130         }
5131         if (!flow_tcf_nl_ack(ctx, nlh, NULL, NULL)) {
5132                 dev_flow->tcf.applied = 1;
5133                 return 0;
5134         }
5135         if (dev_flow->tcf.tunnel) {
5136                 /* Rollback the VTEP configuration if rule apply failed. */
5137                 assert(dev_flow->tcf.tunnel->vtep);
5138                 flow_tcf_vtep_release(ctx, dev_flow->tcf.tunnel->vtep,
5139                                       dev_flow);
5140                 dev_flow->tcf.tunnel->vtep = NULL;
5141         }
5142         return rte_flow_error_set(error, rte_errno,
5143                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5144                                   "netlink: failed to create TC flow rule");
5145 }
5146
5147 /**
5148  * Remove flow from E-Switch by sending Netlink message.
5149  *
5150  * @param[in] dev
5151  *   Pointer to Ethernet device.
5152  * @param[in, out] flow
5153  *   Pointer to the sub flow.
5154  */
5155 static void
5156 flow_tcf_remove(struct rte_eth_dev *dev, struct rte_flow *flow)
5157 {
5158         struct priv *priv = dev->data->dev_private;
5159         struct mlx5_flow_tcf_context *ctx = priv->tcf_context;
5160         struct mlx5_flow *dev_flow;
5161         struct nlmsghdr *nlh;
5162
5163         if (!flow)
5164                 return;
5165         dev_flow = LIST_FIRST(&flow->dev_flows);
5166         if (!dev_flow)
5167                 return;
5168         /* E-Switch flow can't be expanded. */
5169         assert(!LIST_NEXT(dev_flow, next));
5170         if (dev_flow->tcf.applied) {
5171                 nlh = dev_flow->tcf.nlh;
5172                 nlh->nlmsg_type = RTM_DELTFILTER;
5173                 nlh->nlmsg_flags = NLM_F_REQUEST;
5174                 flow_tcf_nl_ack(ctx, nlh, NULL, NULL);
5175                 if (dev_flow->tcf.tunnel) {
5176                         assert(dev_flow->tcf.tunnel->vtep);
5177                         flow_tcf_vtep_release(ctx,
5178                                 dev_flow->tcf.tunnel->vtep,
5179                                 dev_flow);
5180                         dev_flow->tcf.tunnel->vtep = NULL;
5181                 }
5182                 dev_flow->tcf.applied = 0;
5183         }
5184 }
5185
5186 /**
5187  * Remove flow from E-Switch and release resources of the device flow.
5188  *
5189  * @param[in] dev
5190  *   Pointer to Ethernet device.
5191  * @param[in, out] flow
5192  *   Pointer to the sub flow.
5193  */
5194 static void
5195 flow_tcf_destroy(struct rte_eth_dev *dev, struct rte_flow *flow)
5196 {
5197         struct mlx5_flow *dev_flow;
5198
5199         if (!flow)
5200                 return;
5201         flow_tcf_remove(dev, flow);
5202         if (flow->counter) {
5203                 if (--flow->counter->ref_cnt == 0) {
5204                         rte_free(flow->counter);
5205                         flow->counter = NULL;
5206                 }
5207         }
5208         dev_flow = LIST_FIRST(&flow->dev_flows);
5209         if (!dev_flow)
5210                 return;
5211         /* E-Switch flow can't be expanded. */
5212         assert(!LIST_NEXT(dev_flow, next));
5213         LIST_REMOVE(dev_flow, next);
5214         rte_free(dev_flow);
5215 }
5216
5217 /**
5218  * Helper routine for figuring the space size required for a parse buffer.
5219  *
5220  * @param array
5221  *   array of values to use.
5222  * @param idx
5223  *   Current location in array.
5224  * @param value
5225  *   Value to compare with.
5226  *
5227  * @return
5228  *   The maximum between the given value and the array value on index.
5229  */
5230 static uint16_t
5231 flow_tcf_arr_val_max(uint16_t array[], int idx, uint16_t value)
5232 {
5233         return idx < 0 ? (value) : RTE_MAX((array)[idx], value);
5234 }
5235
5236 /**
5237  * Parse rtnetlink message attributes filling the attribute table with the info
5238  * retrieved.
5239  *
5240  * @param tb
5241  *   Attribute table to be filled.
5242  * @param[out] max
5243  *   Maxinum entry in the attribute table.
5244  * @param rte
5245  *   The attributes section in the message to be parsed.
5246  * @param len
5247  *   The length of the attributes section in the message.
5248  */
5249 static void
5250 flow_tcf_nl_parse_rtattr(struct rtattr *tb[], int max,
5251                          struct rtattr *rta, int len)
5252 {
5253         unsigned short type;
5254         memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
5255         while (RTA_OK(rta, len)) {
5256                 type = rta->rta_type;
5257                 if (type <= max && !tb[type])
5258                         tb[type] = rta;
5259                 rta = RTA_NEXT(rta, len);
5260         }
5261 }
5262
5263 /**
5264  * Extract flow counters from flower action.
5265  *
5266  * @param rta
5267  *   flower action stats properties in the Netlink message received.
5268  * @param rta_type
5269  *   The backward sequence of rta_types, as written in the attribute table,
5270  *   we need to traverse in order to get to the requested object.
5271  * @param idx
5272  *   Current location in rta_type table.
5273  * @param[out] data
5274  *   data holding the count statistics of the rte_flow retrieved from
5275  *   the message.
5276  *
5277  * @return
5278  *   0 if data was found and retrieved, -1 otherwise.
5279  */
5280 static int
5281 flow_tcf_nl_action_stats_parse_and_get(struct rtattr *rta,
5282                                        uint16_t rta_type[], int idx,
5283                                        struct gnet_stats_basic *data)
5284 {
5285         int tca_stats_max = flow_tcf_arr_val_max(rta_type, idx,
5286                                                  TCA_STATS_BASIC);
5287         struct rtattr *tbs[tca_stats_max + 1];
5288
5289         if (rta == NULL || idx < 0)
5290                 return -1;
5291         flow_tcf_nl_parse_rtattr(tbs, tca_stats_max,
5292                                  RTA_DATA(rta), RTA_PAYLOAD(rta));
5293         switch (rta_type[idx]) {
5294         case TCA_STATS_BASIC:
5295                 if (tbs[TCA_STATS_BASIC]) {
5296                         memcpy(data, RTA_DATA(tbs[TCA_STATS_BASIC]),
5297                                RTE_MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]),
5298                                sizeof(*data)));
5299                         return 0;
5300                 }
5301                 break;
5302         default:
5303                 break;
5304         }
5305         return -1;
5306 }
5307
5308 /**
5309  * Parse flower single action retrieving the requested action attribute,
5310  * if found.
5311  *
5312  * @param arg
5313  *   flower action properties in the Netlink message received.
5314  * @param rta_type
5315  *   The backward sequence of rta_types, as written in the attribute table,
5316  *   we need to traverse in order to get to the requested object.
5317  * @param idx
5318  *   Current location in rta_type table.
5319  * @param[out] data
5320  *   Count statistics retrieved from the message query.
5321  *
5322  * @return
5323  *   0 if data was found and retrieved, -1 otherwise.
5324  */
5325 static int
5326 flow_tcf_nl_parse_one_action_and_get(struct rtattr *arg,
5327                                      uint16_t rta_type[], int idx, void *data)
5328 {
5329         int tca_act_max = flow_tcf_arr_val_max(rta_type, idx, TCA_ACT_STATS);
5330         struct rtattr *tb[tca_act_max + 1];
5331
5332         if (arg == NULL || idx < 0)
5333                 return -1;
5334         flow_tcf_nl_parse_rtattr(tb, tca_act_max,
5335                                  RTA_DATA(arg), RTA_PAYLOAD(arg));
5336         if (tb[TCA_ACT_KIND] == NULL)
5337                 return -1;
5338         switch (rta_type[idx]) {
5339         case TCA_ACT_STATS:
5340                 if (tb[TCA_ACT_STATS])
5341                         return flow_tcf_nl_action_stats_parse_and_get
5342                                         (tb[TCA_ACT_STATS],
5343                                          rta_type, --idx,
5344                                          (struct gnet_stats_basic *)data);
5345                 break;
5346         default:
5347                 break;
5348         }
5349         return -1;
5350 }
5351
5352 /**
5353  * Parse flower action section in the message retrieving the requested
5354  * attribute from the first action that provides it.
5355  *
5356  * @param opt
5357  *   flower section in the Netlink message received.
5358  * @param rta_type
5359  *   The backward sequence of rta_types, as written in the attribute table,
5360  *   we need to traverse in order to get to the requested object.
5361  * @param idx
5362  *   Current location in rta_type table.
5363  * @param[out] data
5364  *   data retrieved from the message query.
5365  *
5366  * @return
5367  *   0 if data was found and retrieved, -1 otherwise.
5368  */
5369 static int
5370 flow_tcf_nl_action_parse_and_get(struct rtattr *arg,
5371                                  uint16_t rta_type[], int idx, void *data)
5372 {
5373         struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
5374         int i;
5375
5376         if (arg == NULL || idx < 0)
5377                 return -1;
5378         flow_tcf_nl_parse_rtattr(tb, TCA_ACT_MAX_PRIO,
5379                                  RTA_DATA(arg), RTA_PAYLOAD(arg));
5380         switch (rta_type[idx]) {
5381         /*
5382          * flow counters are stored in the actions defined by the flow
5383          * and not in the flow itself, therefore we need to traverse the
5384          * flower chain of actions in search for them.
5385          *
5386          * Note that the index is not decremented here.
5387          */
5388         case TCA_ACT_STATS:
5389                 for (i = 0; i <= TCA_ACT_MAX_PRIO; i++) {
5390                         if (tb[i] &&
5391                         !flow_tcf_nl_parse_one_action_and_get(tb[i],
5392                                                               rta_type,
5393                                                               idx, data))
5394                                 return 0;
5395                 }
5396                 break;
5397         default:
5398                 break;
5399         }
5400         return -1;
5401 }
5402
5403 /**
5404  * Parse flower classifier options in the message, retrieving the requested
5405  * attribute if found.
5406  *
5407  * @param opt
5408  *   flower section in the Netlink message received.
5409  * @param rta_type
5410  *   The backward sequence of rta_types, as written in the attribute table,
5411  *   we need to traverse in order to get to the requested object.
5412  * @param idx
5413  *   Current location in rta_type table.
5414  * @param[out] data
5415  *   data retrieved from the message query.
5416  *
5417  * @return
5418  *   0 if data was found and retrieved, -1 otherwise.
5419  */
5420 static int
5421 flow_tcf_nl_opts_parse_and_get(struct rtattr *opt,
5422                                uint16_t rta_type[], int idx, void *data)
5423 {
5424         int tca_flower_max = flow_tcf_arr_val_max(rta_type, idx,
5425                                                   TCA_FLOWER_ACT);
5426         struct rtattr *tb[tca_flower_max + 1];
5427
5428         if (!opt || idx < 0)
5429                 return -1;
5430         flow_tcf_nl_parse_rtattr(tb, tca_flower_max,
5431                                  RTA_DATA(opt), RTA_PAYLOAD(opt));
5432         switch (rta_type[idx]) {
5433         case TCA_FLOWER_ACT:
5434                 if (tb[TCA_FLOWER_ACT])
5435                         return flow_tcf_nl_action_parse_and_get
5436                                                         (tb[TCA_FLOWER_ACT],
5437                                                          rta_type, --idx, data);
5438                 break;
5439         default:
5440                 break;
5441         }
5442         return -1;
5443 }
5444
5445 /**
5446  * Parse Netlink reply on filter query, retrieving the flow counters.
5447  *
5448  * @param nlh
5449  *   Message received from Netlink.
5450  * @param rta_type
5451  *   The backward sequence of rta_types, as written in the attribute table,
5452  *   we need to traverse in order to get to the requested object.
5453  * @param idx
5454  *   Current location in rta_type table.
5455  * @param[out] data
5456  *   data retrieved from the message query.
5457  *
5458  * @return
5459  *   0 if data was found and retrieved, -1 otherwise.
5460  */
5461 static int
5462 flow_tcf_nl_filter_parse_and_get(struct nlmsghdr *cnlh,
5463                                  uint16_t rta_type[], int idx, void *data)
5464 {
5465         struct nlmsghdr *nlh = cnlh;
5466         struct tcmsg *t = NLMSG_DATA(nlh);
5467         int len = nlh->nlmsg_len;
5468         int tca_max = flow_tcf_arr_val_max(rta_type, idx, TCA_OPTIONS);
5469         struct rtattr *tb[tca_max + 1];
5470
5471         if (idx < 0)
5472                 return -1;
5473         if (nlh->nlmsg_type != RTM_NEWTFILTER &&
5474             nlh->nlmsg_type != RTM_GETTFILTER &&
5475             nlh->nlmsg_type != RTM_DELTFILTER)
5476                 return -1;
5477         len -= NLMSG_LENGTH(sizeof(*t));
5478         if (len < 0)
5479                 return -1;
5480         flow_tcf_nl_parse_rtattr(tb, tca_max, TCA_RTA(t), len);
5481         /* Not a TC flower flow - bail out */
5482         if (!tb[TCA_KIND] ||
5483             strcmp(RTA_DATA(tb[TCA_KIND]), "flower"))
5484                 return -1;
5485         switch (rta_type[idx]) {
5486         case TCA_OPTIONS:
5487                 if (tb[TCA_OPTIONS])
5488                         return flow_tcf_nl_opts_parse_and_get(tb[TCA_OPTIONS],
5489                                                               rta_type,
5490                                                               --idx, data);
5491                 break;
5492         default:
5493                 break;
5494         }
5495         return -1;
5496 }
5497
5498 /**
5499  * A callback to parse Netlink reply on TC flower query.
5500  *
5501  * @param nlh
5502  *   Message received from Netlink.
5503  * @param[out] data
5504  *   Pointer to data area to be filled by the parsing routine.
5505  *   assumed to be a pinter to struct flow_tcf_stats_basic.
5506  *
5507  * @return
5508  *   MNL_CB_OK value.
5509  */
5510 static int
5511 flow_tcf_nl_message_get_stats_basic(const struct nlmsghdr *nlh, void *data)
5512 {
5513         /*
5514          * The backward sequence of rta_types to pass in order to get
5515          *  to the counters.
5516          */
5517         uint16_t rta_type[] = { TCA_STATS_BASIC, TCA_ACT_STATS,
5518                                 TCA_FLOWER_ACT, TCA_OPTIONS };
5519         struct flow_tcf_stats_basic *sb_data = data;
5520         union {
5521                 const struct nlmsghdr *c;
5522                 struct nlmsghdr *nc;
5523         } tnlh = { .c = nlh };
5524
5525         if (!flow_tcf_nl_filter_parse_and_get(tnlh.nc, rta_type,
5526                                               RTE_DIM(rta_type) - 1,
5527                                               (void *)&sb_data->counters))
5528                 sb_data->valid = true;
5529         return MNL_CB_OK;
5530 }
5531
5532 /**
5533  * Query a TC flower rule for its statistics via netlink.
5534  *
5535  * @param[in] dev
5536  *   Pointer to Ethernet device.
5537  * @param[in] flow
5538  *   Pointer to the sub flow.
5539  * @param[out] data
5540  *   data retrieved by the query.
5541  * @param[out] error
5542  *   Perform verbose error reporting if not NULL.
5543  *
5544  * @return
5545  *   0 on success, a negative errno value otherwise and rte_errno is set.
5546  */
5547 static int
5548 flow_tcf_query_count(struct rte_eth_dev *dev,
5549                           struct rte_flow *flow,
5550                           void *data,
5551                           struct rte_flow_error *error)
5552 {
5553         struct flow_tcf_stats_basic sb_data = { 0 };
5554         struct rte_flow_query_count *qc = data;
5555         struct priv *priv = dev->data->dev_private;
5556         struct mlx5_flow_tcf_context *ctx = priv->tcf_context;
5557         struct mnl_socket *nl = ctx->nl;
5558         struct mlx5_flow *dev_flow;
5559         struct nlmsghdr *nlh;
5560         uint32_t seq = priv->tcf_context->seq++;
5561         ssize_t ret;
5562         assert(qc);
5563
5564         dev_flow = LIST_FIRST(&flow->dev_flows);
5565         /* E-Switch flow can't be expanded. */
5566         assert(!LIST_NEXT(dev_flow, next));
5567         if (!dev_flow->flow->counter)
5568                 goto notsup_exit;
5569         nlh = dev_flow->tcf.nlh;
5570         nlh->nlmsg_type = RTM_GETTFILTER;
5571         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ECHO;
5572         nlh->nlmsg_seq = seq;
5573         if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) == -1)
5574                 goto error_exit;
5575         do {
5576                 ret = mnl_socket_recvfrom(nl, ctx->buf, ctx->buf_size);
5577                 if (ret <= 0)
5578                         break;
5579                 ret = mnl_cb_run(ctx->buf, ret, seq,
5580                                  mnl_socket_get_portid(nl),
5581                                  flow_tcf_nl_message_get_stats_basic,
5582                                  (void *)&sb_data);
5583         } while (ret > 0);
5584         /* Return the delta from last reset. */
5585         if (sb_data.valid) {
5586                 /* Return the delta from last reset. */
5587                 qc->hits_set = 1;
5588                 qc->bytes_set = 1;
5589                 qc->hits = sb_data.counters.packets - flow->counter->hits;
5590                 qc->bytes = sb_data.counters.bytes - flow->counter->bytes;
5591                 if (qc->reset) {
5592                         flow->counter->hits = sb_data.counters.packets;
5593                         flow->counter->bytes = sb_data.counters.bytes;
5594                 }
5595                 return 0;
5596         }
5597         return rte_flow_error_set(error, EINVAL,
5598                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5599                                   NULL,
5600                                   "flow does not have counter");
5601 error_exit:
5602         return rte_flow_error_set
5603                         (error, errno, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5604                          NULL, "netlink: failed to read flow rule counters");
5605 notsup_exit:
5606         return rte_flow_error_set
5607                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
5608                          NULL, "counters are not available.");
5609 }
5610
5611 /**
5612  * Query a flow.
5613  *
5614  * @see rte_flow_query()
5615  * @see rte_flow_ops
5616  */
5617 static int
5618 flow_tcf_query(struct rte_eth_dev *dev,
5619                struct rte_flow *flow,
5620                const struct rte_flow_action *actions,
5621                void *data,
5622                struct rte_flow_error *error)
5623 {
5624         int ret = -EINVAL;
5625
5626         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
5627                 switch (actions->type) {
5628                 case RTE_FLOW_ACTION_TYPE_VOID:
5629                         break;
5630                 case RTE_FLOW_ACTION_TYPE_COUNT:
5631                         ret = flow_tcf_query_count(dev, flow, data, error);
5632                         break;
5633                 default:
5634                         return rte_flow_error_set(error, ENOTSUP,
5635                                                   RTE_FLOW_ERROR_TYPE_ACTION,
5636                                                   actions,
5637                                                   "action not supported");
5638                 }
5639         }
5640         return ret;
5641 }
5642
5643 const struct mlx5_flow_driver_ops mlx5_flow_tcf_drv_ops = {
5644         .validate = flow_tcf_validate,
5645         .prepare = flow_tcf_prepare,
5646         .translate = flow_tcf_translate,
5647         .apply = flow_tcf_apply,
5648         .remove = flow_tcf_remove,
5649         .destroy = flow_tcf_destroy,
5650         .query = flow_tcf_query,
5651 };
5652
5653 /**
5654  * Create and configure a libmnl socket for Netlink flow rules.
5655  *
5656  * @return
5657  *   A valid libmnl socket object pointer on success, NULL otherwise and
5658  *   rte_errno is set.
5659  */
5660 static struct mnl_socket *
5661 flow_tcf_mnl_socket_create(void)
5662 {
5663         struct mnl_socket *nl = mnl_socket_open(NETLINK_ROUTE);
5664
5665         if (nl) {
5666                 mnl_socket_setsockopt(nl, NETLINK_CAP_ACK, &(int){ 1 },
5667                                       sizeof(int));
5668                 if (!mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID))
5669                         return nl;
5670         }
5671         rte_errno = errno;
5672         if (nl)
5673                 mnl_socket_close(nl);
5674         return NULL;
5675 }
5676
5677 /**
5678  * Destroy a libmnl socket.
5679  *
5680  * @param nl
5681  *   Libmnl socket of the @p NETLINK_ROUTE kind.
5682  */
5683 static void
5684 flow_tcf_mnl_socket_destroy(struct mnl_socket *nl)
5685 {
5686         if (nl)
5687                 mnl_socket_close(nl);
5688 }
5689
5690 /**
5691  * Initialize ingress qdisc of a given network interface.
5692  *
5693  * @param ctx
5694  *   Pointer to tc-flower context to use.
5695  * @param ifindex
5696  *   Index of network interface to initialize.
5697  * @param[out] error
5698  *   Perform verbose error reporting if not NULL.
5699  *
5700  * @return
5701  *   0 on success, a negative errno value otherwise and rte_errno is set.
5702  */
5703 int
5704 mlx5_flow_tcf_init(struct mlx5_flow_tcf_context *ctx,
5705                    unsigned int ifindex, struct rte_flow_error *error)
5706 {
5707         struct nlmsghdr *nlh;
5708         struct tcmsg *tcm;
5709         alignas(struct nlmsghdr)
5710         uint8_t buf[mnl_nlmsg_size(sizeof(*tcm)) +
5711                     SZ_NLATTR_STRZ_OF("ingress") +
5712                     MNL_BUF_EXTRA_SPACE];
5713
5714         /* Destroy existing ingress qdisc and everything attached to it. */
5715         nlh = mnl_nlmsg_put_header(buf);
5716         nlh->nlmsg_type = RTM_DELQDISC;
5717         nlh->nlmsg_flags = NLM_F_REQUEST;
5718         tcm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tcm));
5719         tcm->tcm_family = AF_UNSPEC;
5720         tcm->tcm_ifindex = ifindex;
5721         tcm->tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0);
5722         tcm->tcm_parent = TC_H_INGRESS;
5723         assert(sizeof(buf) >= nlh->nlmsg_len);
5724         /* Ignore errors when qdisc is already absent. */
5725         if (flow_tcf_nl_ack(ctx, nlh, NULL, NULL) &&
5726             rte_errno != EINVAL && rte_errno != ENOENT)
5727                 return rte_flow_error_set(error, rte_errno,
5728                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5729                                           "netlink: failed to remove ingress"
5730                                           " qdisc");
5731         /* Create fresh ingress qdisc. */
5732         nlh = mnl_nlmsg_put_header(buf);
5733         nlh->nlmsg_type = RTM_NEWQDISC;
5734         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
5735         tcm = mnl_nlmsg_put_extra_header(nlh, sizeof(*tcm));
5736         tcm->tcm_family = AF_UNSPEC;
5737         tcm->tcm_ifindex = ifindex;
5738         tcm->tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0);
5739         tcm->tcm_parent = TC_H_INGRESS;
5740         mnl_attr_put_strz_check(nlh, sizeof(buf), TCA_KIND, "ingress");
5741         assert(sizeof(buf) >= nlh->nlmsg_len);
5742         if (flow_tcf_nl_ack(ctx, nlh, NULL, NULL))
5743                 return rte_flow_error_set(error, rte_errno,
5744                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
5745                                           "netlink: failed to create ingress"
5746                                           " qdisc");
5747         return 0;
5748 }
5749
5750 /**
5751  * Create libmnl context for Netlink flow rules.
5752  *
5753  * @return
5754  *   A valid libmnl socket object pointer on success, NULL otherwise and
5755  *   rte_errno is set.
5756  */
5757 struct mlx5_flow_tcf_context *
5758 mlx5_flow_tcf_context_create(void)
5759 {
5760         struct mlx5_flow_tcf_context *ctx = rte_zmalloc(__func__,
5761                                                         sizeof(*ctx),
5762                                                         sizeof(uint32_t));
5763         if (!ctx)
5764                 goto error;
5765         ctx->nl = flow_tcf_mnl_socket_create();
5766         if (!ctx->nl)
5767                 goto error;
5768         ctx->buf_size = MNL_SOCKET_BUFFER_SIZE;
5769         ctx->buf = rte_zmalloc(__func__,
5770                                ctx->buf_size, sizeof(uint32_t));
5771         if (!ctx->buf)
5772                 goto error;
5773         ctx->seq = random();
5774         return ctx;
5775 error:
5776         mlx5_flow_tcf_context_destroy(ctx);
5777         return NULL;
5778 }
5779
5780 /**
5781  * Destroy a libmnl context.
5782  *
5783  * @param ctx
5784  *   Libmnl socket of the @p NETLINK_ROUTE kind.
5785  */
5786 void
5787 mlx5_flow_tcf_context_destroy(struct mlx5_flow_tcf_context *ctx)
5788 {
5789         if (!ctx)
5790                 return;
5791         flow_tcf_mnl_socket_destroy(ctx->nl);
5792         rte_free(ctx->buf);
5793         rte_free(ctx);
5794 }