ethdev: alter behavior of flow API actions
[dpdk.git] / drivers / net / tap / tap_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox Technologies, Ltd
4  */
5
6 #include <errno.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <sys/queue.h>
10 #include <sys/resource.h>
11
12 #include <rte_byteorder.h>
13 #include <rte_jhash.h>
14 #include <rte_malloc.h>
15 #include <rte_eth_tap.h>
16 #include <tap_flow.h>
17 #include <tap_autoconf.h>
18 #include <tap_tcmsgs.h>
19 #include <tap_rss.h>
20
21 #ifndef HAVE_TC_FLOWER
22 /*
23  * For kernels < 4.2, this enum is not defined. Runtime checks will be made to
24  * avoid sending TC messages the kernel cannot understand.
25  */
26 enum {
27         TCA_FLOWER_UNSPEC,
28         TCA_FLOWER_CLASSID,
29         TCA_FLOWER_INDEV,
30         TCA_FLOWER_ACT,
31         TCA_FLOWER_KEY_ETH_DST,         /* ETH_ALEN */
32         TCA_FLOWER_KEY_ETH_DST_MASK,    /* ETH_ALEN */
33         TCA_FLOWER_KEY_ETH_SRC,         /* ETH_ALEN */
34         TCA_FLOWER_KEY_ETH_SRC_MASK,    /* ETH_ALEN */
35         TCA_FLOWER_KEY_ETH_TYPE,        /* be16 */
36         TCA_FLOWER_KEY_IP_PROTO,        /* u8 */
37         TCA_FLOWER_KEY_IPV4_SRC,        /* be32 */
38         TCA_FLOWER_KEY_IPV4_SRC_MASK,   /* be32 */
39         TCA_FLOWER_KEY_IPV4_DST,        /* be32 */
40         TCA_FLOWER_KEY_IPV4_DST_MASK,   /* be32 */
41         TCA_FLOWER_KEY_IPV6_SRC,        /* struct in6_addr */
42         TCA_FLOWER_KEY_IPV6_SRC_MASK,   /* struct in6_addr */
43         TCA_FLOWER_KEY_IPV6_DST,        /* struct in6_addr */
44         TCA_FLOWER_KEY_IPV6_DST_MASK,   /* struct in6_addr */
45         TCA_FLOWER_KEY_TCP_SRC,         /* be16 */
46         TCA_FLOWER_KEY_TCP_DST,         /* be16 */
47         TCA_FLOWER_KEY_UDP_SRC,         /* be16 */
48         TCA_FLOWER_KEY_UDP_DST,         /* be16 */
49 };
50 #endif
51 #ifndef HAVE_TC_VLAN_ID
52 enum {
53         /* TCA_FLOWER_FLAGS, */
54         TCA_FLOWER_KEY_VLAN_ID = TCA_FLOWER_KEY_UDP_DST + 2, /* be16 */
55         TCA_FLOWER_KEY_VLAN_PRIO,       /* u8   */
56         TCA_FLOWER_KEY_VLAN_ETH_TYPE,   /* be16 */
57 };
58 #endif
59 /*
60  * For kernels < 4.2 BPF related enums may not be defined.
61  * Runtime checks will be carried out to gracefully report on TC messages that
62  * are rejected by the kernel. Rejection reasons may be due to:
63  * 1. enum is not defined
64  * 2. enum is defined but kernel is not configured to support BPF system calls,
65  *    BPF classifications or BPF actions.
66  */
67 #ifndef HAVE_TC_BPF
68 enum {
69         TCA_BPF_UNSPEC,
70         TCA_BPF_ACT,
71         TCA_BPF_POLICE,
72         TCA_BPF_CLASSID,
73         TCA_BPF_OPS_LEN,
74         TCA_BPF_OPS,
75 };
76 #endif
77 #ifndef HAVE_TC_BPF_FD
78 enum {
79         TCA_BPF_FD = TCA_BPF_OPS + 1,
80         TCA_BPF_NAME,
81 };
82 #endif
83 #ifndef HAVE_TC_ACT_BPF
84 #define tc_gen \
85         __u32                 index; \
86         __u32                 capab; \
87         int                   action; \
88         int                   refcnt; \
89         int                   bindcnt
90
91 struct tc_act_bpf {
92         tc_gen;
93 };
94
95 enum {
96         TCA_ACT_BPF_UNSPEC,
97         TCA_ACT_BPF_TM,
98         TCA_ACT_BPF_PARMS,
99         TCA_ACT_BPF_OPS_LEN,
100         TCA_ACT_BPF_OPS,
101 };
102
103 #endif
104 #ifndef HAVE_TC_ACT_BPF_FD
105 enum {
106         TCA_ACT_BPF_FD = TCA_ACT_BPF_OPS + 1,
107         TCA_ACT_BPF_NAME,
108 };
109 #endif
110
111 /* RSS key management */
112 enum bpf_rss_key_e {
113         KEY_CMD_GET = 1,
114         KEY_CMD_RELEASE,
115         KEY_CMD_INIT,
116         KEY_CMD_DEINIT,
117 };
118
119 enum key_status_e {
120         KEY_STAT_UNSPEC,
121         KEY_STAT_USED,
122         KEY_STAT_AVAILABLE,
123 };
124
125 #define ISOLATE_HANDLE 1
126 #define REMOTE_PROMISCUOUS_HANDLE 2
127
128 struct rte_flow {
129         LIST_ENTRY(rte_flow) next; /* Pointer to the next rte_flow structure */
130         struct rte_flow *remote_flow; /* associated remote flow */
131         int bpf_fd[SEC_MAX]; /* list of bfs fds per ELF section */
132         uint32_t key_idx; /* RSS rule key index into BPF map */
133         struct nlmsg msg;
134 };
135
136 struct convert_data {
137         uint16_t eth_type;
138         uint16_t ip_proto;
139         uint8_t vlan;
140         struct rte_flow *flow;
141 };
142
143 struct remote_rule {
144         struct rte_flow_attr attr;
145         struct rte_flow_item items[2];
146         struct rte_flow_action actions[2];
147         int mirred;
148 };
149
150 struct action_data {
151         char id[16];
152
153         union {
154                 struct tc_gact gact;
155                 struct tc_mirred mirred;
156                 struct skbedit {
157                         struct tc_skbedit skbedit;
158                         uint16_t queue;
159                 } skbedit;
160                 struct bpf {
161                         struct tc_act_bpf bpf;
162                         int bpf_fd;
163                         const char *annotation;
164                 } bpf;
165         };
166 };
167
168 static int tap_flow_create_eth(const struct rte_flow_item *item, void *data);
169 static int tap_flow_create_vlan(const struct rte_flow_item *item, void *data);
170 static int tap_flow_create_ipv4(const struct rte_flow_item *item, void *data);
171 static int tap_flow_create_ipv6(const struct rte_flow_item *item, void *data);
172 static int tap_flow_create_udp(const struct rte_flow_item *item, void *data);
173 static int tap_flow_create_tcp(const struct rte_flow_item *item, void *data);
174 static int
175 tap_flow_validate(struct rte_eth_dev *dev,
176                   const struct rte_flow_attr *attr,
177                   const struct rte_flow_item items[],
178                   const struct rte_flow_action actions[],
179                   struct rte_flow_error *error);
180
181 static struct rte_flow *
182 tap_flow_create(struct rte_eth_dev *dev,
183                 const struct rte_flow_attr *attr,
184                 const struct rte_flow_item items[],
185                 const struct rte_flow_action actions[],
186                 struct rte_flow_error *error);
187
188 static void
189 tap_flow_free(struct pmd_internals *pmd,
190         struct rte_flow *flow);
191
192 static int
193 tap_flow_destroy(struct rte_eth_dev *dev,
194                  struct rte_flow *flow,
195                  struct rte_flow_error *error);
196
197 static int
198 tap_flow_isolate(struct rte_eth_dev *dev,
199                  int set,
200                  struct rte_flow_error *error);
201
202 static int bpf_rss_key(enum bpf_rss_key_e cmd, __u32 *key_idx);
203 static int rss_enable(struct pmd_internals *pmd,
204                         const struct rte_flow_attr *attr,
205                         struct rte_flow_error *error);
206 static int rss_add_actions(struct rte_flow *flow, struct pmd_internals *pmd,
207                         const struct rte_flow_action_rss *rss,
208                         struct rte_flow_error *error);
209
210 static const struct rte_flow_ops tap_flow_ops = {
211         .validate = tap_flow_validate,
212         .create = tap_flow_create,
213         .destroy = tap_flow_destroy,
214         .flush = tap_flow_flush,
215         .isolate = tap_flow_isolate,
216 };
217
218 /* Static initializer for items. */
219 #define ITEMS(...) \
220         (const enum rte_flow_item_type []){ \
221                 __VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
222         }
223
224 /* Structure to generate a simple graph of layers supported by the NIC. */
225 struct tap_flow_items {
226         /* Bit-mask corresponding to what is supported for this item. */
227         const void *mask;
228         const unsigned int mask_sz; /* Bit-mask size in bytes. */
229         /*
230          * Bit-mask corresponding to the default mask, if none is provided
231          * along with the item.
232          */
233         const void *default_mask;
234         /**
235          * Conversion function from rte_flow to netlink attributes.
236          *
237          * @param item
238          *   rte_flow item to convert.
239          * @param data
240          *   Internal structure to store the conversion.
241          *
242          * @return
243          *   0 on success, negative value otherwise.
244          */
245         int (*convert)(const struct rte_flow_item *item, void *data);
246         /** List of possible following items.  */
247         const enum rte_flow_item_type *const items;
248 };
249
250 /* Graph of supported items and associated actions. */
251 static const struct tap_flow_items tap_flow_items[] = {
252         [RTE_FLOW_ITEM_TYPE_END] = {
253                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_ETH),
254         },
255         [RTE_FLOW_ITEM_TYPE_ETH] = {
256                 .items = ITEMS(
257                         RTE_FLOW_ITEM_TYPE_VLAN,
258                         RTE_FLOW_ITEM_TYPE_IPV4,
259                         RTE_FLOW_ITEM_TYPE_IPV6),
260                 .mask = &(const struct rte_flow_item_eth){
261                         .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
262                         .src.addr_bytes = "\xff\xff\xff\xff\xff\xff",
263                         .type = -1,
264                 },
265                 .mask_sz = sizeof(struct rte_flow_item_eth),
266                 .default_mask = &rte_flow_item_eth_mask,
267                 .convert = tap_flow_create_eth,
268         },
269         [RTE_FLOW_ITEM_TYPE_VLAN] = {
270                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_IPV4,
271                                RTE_FLOW_ITEM_TYPE_IPV6),
272                 .mask = &(const struct rte_flow_item_vlan){
273                         .tpid = -1,
274                         /* DEI matching is not supported */
275 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
276                         .tci = 0xffef,
277 #else
278                         .tci = 0xefff,
279 #endif
280                 },
281                 .mask_sz = sizeof(struct rte_flow_item_vlan),
282                 .default_mask = &rte_flow_item_vlan_mask,
283                 .convert = tap_flow_create_vlan,
284         },
285         [RTE_FLOW_ITEM_TYPE_IPV4] = {
286                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
287                                RTE_FLOW_ITEM_TYPE_TCP),
288                 .mask = &(const struct rte_flow_item_ipv4){
289                         .hdr = {
290                                 .src_addr = -1,
291                                 .dst_addr = -1,
292                                 .next_proto_id = -1,
293                         },
294                 },
295                 .mask_sz = sizeof(struct rte_flow_item_ipv4),
296                 .default_mask = &rte_flow_item_ipv4_mask,
297                 .convert = tap_flow_create_ipv4,
298         },
299         [RTE_FLOW_ITEM_TYPE_IPV6] = {
300                 .items = ITEMS(RTE_FLOW_ITEM_TYPE_UDP,
301                                RTE_FLOW_ITEM_TYPE_TCP),
302                 .mask = &(const struct rte_flow_item_ipv6){
303                         .hdr = {
304                                 .src_addr = {
305                                         "\xff\xff\xff\xff\xff\xff\xff\xff"
306                                         "\xff\xff\xff\xff\xff\xff\xff\xff",
307                                 },
308                                 .dst_addr = {
309                                         "\xff\xff\xff\xff\xff\xff\xff\xff"
310                                         "\xff\xff\xff\xff\xff\xff\xff\xff",
311                                 },
312                                 .proto = -1,
313                         },
314                 },
315                 .mask_sz = sizeof(struct rte_flow_item_ipv6),
316                 .default_mask = &rte_flow_item_ipv6_mask,
317                 .convert = tap_flow_create_ipv6,
318         },
319         [RTE_FLOW_ITEM_TYPE_UDP] = {
320                 .mask = &(const struct rte_flow_item_udp){
321                         .hdr = {
322                                 .src_port = -1,
323                                 .dst_port = -1,
324                         },
325                 },
326                 .mask_sz = sizeof(struct rte_flow_item_udp),
327                 .default_mask = &rte_flow_item_udp_mask,
328                 .convert = tap_flow_create_udp,
329         },
330         [RTE_FLOW_ITEM_TYPE_TCP] = {
331                 .mask = &(const struct rte_flow_item_tcp){
332                         .hdr = {
333                                 .src_port = -1,
334                                 .dst_port = -1,
335                         },
336                 },
337                 .mask_sz = sizeof(struct rte_flow_item_tcp),
338                 .default_mask = &rte_flow_item_tcp_mask,
339                 .convert = tap_flow_create_tcp,
340         },
341 };
342
343 /*
344  *                TC rules, by growing priority
345  *
346  *        Remote netdevice                  Tap netdevice
347  * +-------------+-------------+  +-------------+-------------+
348  * |   Ingress   |   Egress    |  |   Ingress   |   Egress    |
349  * |-------------|-------------|  |-------------|-------------|
350  * |             |  \       /  |  |             |  REMOTE TX  | prio 1
351  * |             |   \     /   |  |             |   \     /   | prio 2
352  * |  EXPLICIT   |    \   /    |  |  EXPLICIT   |    \   /    |   .
353  * |             |     \ /     |  |             |     \ /     |   .
354  * |    RULES    |      X      |  |    RULES    |      X      |   .
355  * |      .      |     / \     |  |      .      |     / \     |   .
356  * |      .      |    /   \    |  |      .      |    /   \    |   .
357  * |      .      |   /     \   |  |      .      |   /     \   |   .
358  * |      .      |  /       \  |  |      .      |  /       \  |   .
359  *
360  *      ....           ....           ....           ....
361  *
362  * |      .      |  \       /  |  |      .      |  \       /  |   .
363  * |      .      |   \     /   |  |      .      |   \     /   |   .
364  * |             |    \   /    |  |             |    \   /    |
365  * |  LOCAL_MAC  |     \ /     |  |    \   /    |     \ /     | last prio - 5
366  * |   PROMISC   |      X      |  |     \ /     |      X      | last prio - 4
367  * |   ALLMULTI  |     / \     |  |      X      |     / \     | last prio - 3
368  * |  BROADCAST  |    /   \    |  |     / \     |    /   \    | last prio - 2
369  * | BROADCASTV6 |   /     \   |  |    /   \    |   /     \   | last prio - 1
370  * |     xx      |  /       \  |  |   ISOLATE   |  /       \  | last prio
371  * +-------------+-------------+  +-------------+-------------+
372  *
373  * The implicit flow rules are stored in a list in with mandatorily the last two
374  * being the ISOLATE and REMOTE_TX rules. e.g.:
375  *
376  * LOCAL_MAC -> BROADCAST -> BROADCASTV6 -> REMOTE_TX -> ISOLATE -> NULL
377  *
378  * That enables tap_flow_isolate() to remove implicit rules by popping the list
379  * head and remove it as long as it applies on the remote netdevice. The
380  * implicit rule for TX redirection is not removed, as isolate concerns only
381  * incoming traffic.
382  */
383
384 static struct remote_rule implicit_rte_flows[TAP_REMOTE_MAX_IDX] = {
385         [TAP_REMOTE_LOCAL_MAC] = {
386                 .attr = {
387                         .group = MAX_GROUP,
388                         .priority = PRIORITY_MASK - TAP_REMOTE_LOCAL_MAC,
389                         .ingress = 1,
390                 },
391                 .items[0] = {
392                         .type = RTE_FLOW_ITEM_TYPE_ETH,
393                         .mask =  &(const struct rte_flow_item_eth){
394                                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
395                         },
396                 },
397                 .items[1] = {
398                         .type = RTE_FLOW_ITEM_TYPE_END,
399                 },
400                 .mirred = TCA_EGRESS_REDIR,
401         },
402         [TAP_REMOTE_BROADCAST] = {
403                 .attr = {
404                         .group = MAX_GROUP,
405                         .priority = PRIORITY_MASK - TAP_REMOTE_BROADCAST,
406                         .ingress = 1,
407                 },
408                 .items[0] = {
409                         .type = RTE_FLOW_ITEM_TYPE_ETH,
410                         .mask =  &(const struct rte_flow_item_eth){
411                                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
412                         },
413                         .spec = &(const struct rte_flow_item_eth){
414                                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
415                         },
416                 },
417                 .items[1] = {
418                         .type = RTE_FLOW_ITEM_TYPE_END,
419                 },
420                 .mirred = TCA_EGRESS_MIRROR,
421         },
422         [TAP_REMOTE_BROADCASTV6] = {
423                 .attr = {
424                         .group = MAX_GROUP,
425                         .priority = PRIORITY_MASK - TAP_REMOTE_BROADCASTV6,
426                         .ingress = 1,
427                 },
428                 .items[0] = {
429                         .type = RTE_FLOW_ITEM_TYPE_ETH,
430                         .mask =  &(const struct rte_flow_item_eth){
431                                 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
432                         },
433                         .spec = &(const struct rte_flow_item_eth){
434                                 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
435                         },
436                 },
437                 .items[1] = {
438                         .type = RTE_FLOW_ITEM_TYPE_END,
439                 },
440                 .mirred = TCA_EGRESS_MIRROR,
441         },
442         [TAP_REMOTE_PROMISC] = {
443                 .attr = {
444                         .group = MAX_GROUP,
445                         .priority = PRIORITY_MASK - TAP_REMOTE_PROMISC,
446                         .ingress = 1,
447                 },
448                 .items[0] = {
449                         .type = RTE_FLOW_ITEM_TYPE_VOID,
450                 },
451                 .items[1] = {
452                         .type = RTE_FLOW_ITEM_TYPE_END,
453                 },
454                 .mirred = TCA_EGRESS_MIRROR,
455         },
456         [TAP_REMOTE_ALLMULTI] = {
457                 .attr = {
458                         .group = MAX_GROUP,
459                         .priority = PRIORITY_MASK - TAP_REMOTE_ALLMULTI,
460                         .ingress = 1,
461                 },
462                 .items[0] = {
463                         .type = RTE_FLOW_ITEM_TYPE_ETH,
464                         .mask =  &(const struct rte_flow_item_eth){
465                                 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
466                         },
467                         .spec = &(const struct rte_flow_item_eth){
468                                 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
469                         },
470                 },
471                 .items[1] = {
472                         .type = RTE_FLOW_ITEM_TYPE_END,
473                 },
474                 .mirred = TCA_EGRESS_MIRROR,
475         },
476         [TAP_REMOTE_TX] = {
477                 .attr = {
478                         .group = 0,
479                         .priority = TAP_REMOTE_TX,
480                         .egress = 1,
481                 },
482                 .items[0] = {
483                         .type = RTE_FLOW_ITEM_TYPE_VOID,
484                 },
485                 .items[1] = {
486                         .type = RTE_FLOW_ITEM_TYPE_END,
487                 },
488                 .mirred = TCA_EGRESS_MIRROR,
489         },
490         [TAP_ISOLATE] = {
491                 .attr = {
492                         .group = MAX_GROUP,
493                         .priority = PRIORITY_MASK - TAP_ISOLATE,
494                         .ingress = 1,
495                 },
496                 .items[0] = {
497                         .type = RTE_FLOW_ITEM_TYPE_VOID,
498                 },
499                 .items[1] = {
500                         .type = RTE_FLOW_ITEM_TYPE_END,
501                 },
502         },
503 };
504
505 /**
506  * Make as much checks as possible on an Ethernet item, and if a flow is
507  * provided, fill it appropriately with Ethernet info.
508  *
509  * @param[in] item
510  *   Item specification.
511  * @param[in, out] data
512  *   Additional data structure to tell next layers we've been here.
513  *
514  * @return
515  *   0 if checks are alright, -1 otherwise.
516  */
517 static int
518 tap_flow_create_eth(const struct rte_flow_item *item, void *data)
519 {
520         struct convert_data *info = (struct convert_data *)data;
521         const struct rte_flow_item_eth *spec = item->spec;
522         const struct rte_flow_item_eth *mask = item->mask;
523         struct rte_flow *flow = info->flow;
524         struct nlmsg *msg;
525
526         /* use default mask if none provided */
527         if (!mask)
528                 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_ETH].default_mask;
529         /* TC does not support eth_type masking. Only accept if exact match. */
530         if (mask->type && mask->type != 0xffff)
531                 return -1;
532         if (!spec)
533                 return 0;
534         /* store eth_type for consistency if ipv4/6 pattern item comes next */
535         if (spec->type & mask->type)
536                 info->eth_type = spec->type;
537         if (!flow)
538                 return 0;
539         msg = &flow->msg;
540         if (!is_zero_ether_addr(&spec->dst)) {
541                 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_ETH_DST, ETHER_ADDR_LEN,
542                            &spec->dst.addr_bytes);
543                 tap_nlattr_add(&msg->nh,
544                            TCA_FLOWER_KEY_ETH_DST_MASK, ETHER_ADDR_LEN,
545                            &mask->dst.addr_bytes);
546         }
547         if (!is_zero_ether_addr(&mask->src)) {
548                 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_ETH_SRC, ETHER_ADDR_LEN,
549                            &spec->src.addr_bytes);
550                 tap_nlattr_add(&msg->nh,
551                            TCA_FLOWER_KEY_ETH_SRC_MASK, ETHER_ADDR_LEN,
552                            &mask->src.addr_bytes);
553         }
554         return 0;
555 }
556
557 /**
558  * Make as much checks as possible on a VLAN item, and if a flow is provided,
559  * fill it appropriately with VLAN info.
560  *
561  * @param[in] item
562  *   Item specification.
563  * @param[in, out] data
564  *   Additional data structure to tell next layers we've been here.
565  *
566  * @return
567  *   0 if checks are alright, -1 otherwise.
568  */
569 static int
570 tap_flow_create_vlan(const struct rte_flow_item *item, void *data)
571 {
572         struct convert_data *info = (struct convert_data *)data;
573         const struct rte_flow_item_vlan *spec = item->spec;
574         const struct rte_flow_item_vlan *mask = item->mask;
575         struct rte_flow *flow = info->flow;
576         struct nlmsg *msg;
577
578         /* use default mask if none provided */
579         if (!mask)
580                 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_VLAN].default_mask;
581         /* TC does not support tpid masking. Only accept if exact match. */
582         if (mask->tpid && mask->tpid != 0xffff)
583                 return -1;
584         /* Double-tagging not supported. */
585         if (spec && mask->tpid && spec->tpid != htons(ETH_P_8021Q))
586                 return -1;
587         info->vlan = 1;
588         if (!flow)
589                 return 0;
590         msg = &flow->msg;
591         msg->t.tcm_info = TC_H_MAKE(msg->t.tcm_info, htons(ETH_P_8021Q));
592 #define VLAN_PRIO(tci) ((tci) >> 13)
593 #define VLAN_ID(tci) ((tci) & 0xfff)
594         if (!spec)
595                 return 0;
596         if (spec->tci) {
597                 uint16_t tci = ntohs(spec->tci) & mask->tci;
598                 uint16_t prio = VLAN_PRIO(tci);
599                 uint8_t vid = VLAN_ID(tci);
600
601                 if (prio)
602                         tap_nlattr_add8(&msg->nh,
603                                         TCA_FLOWER_KEY_VLAN_PRIO, prio);
604                 if (vid)
605                         tap_nlattr_add16(&msg->nh,
606                                          TCA_FLOWER_KEY_VLAN_ID, vid);
607         }
608         return 0;
609 }
610
611 /**
612  * Make as much checks as possible on an IPv4 item, and if a flow is provided,
613  * fill it appropriately with IPv4 info.
614  *
615  * @param[in] item
616  *   Item specification.
617  * @param[in, out] data
618  *   Additional data structure to tell next layers we've been here.
619  *
620  * @return
621  *   0 if checks are alright, -1 otherwise.
622  */
623 static int
624 tap_flow_create_ipv4(const struct rte_flow_item *item, void *data)
625 {
626         struct convert_data *info = (struct convert_data *)data;
627         const struct rte_flow_item_ipv4 *spec = item->spec;
628         const struct rte_flow_item_ipv4 *mask = item->mask;
629         struct rte_flow *flow = info->flow;
630         struct nlmsg *msg;
631
632         /* use default mask if none provided */
633         if (!mask)
634                 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_IPV4].default_mask;
635         /* check that previous eth type is compatible with ipv4 */
636         if (info->eth_type && info->eth_type != htons(ETH_P_IP))
637                 return -1;
638         /* store ip_proto for consistency if udp/tcp pattern item comes next */
639         if (spec)
640                 info->ip_proto = spec->hdr.next_proto_id;
641         if (!flow)
642                 return 0;
643         msg = &flow->msg;
644         if (!info->eth_type)
645                 info->eth_type = htons(ETH_P_IP);
646         if (!spec)
647                 return 0;
648         if (spec->hdr.dst_addr) {
649                 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_DST,
650                              spec->hdr.dst_addr);
651                 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_DST_MASK,
652                              mask->hdr.dst_addr);
653         }
654         if (spec->hdr.src_addr) {
655                 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_SRC,
656                              spec->hdr.src_addr);
657                 tap_nlattr_add32(&msg->nh, TCA_FLOWER_KEY_IPV4_SRC_MASK,
658                              mask->hdr.src_addr);
659         }
660         if (spec->hdr.next_proto_id)
661                 tap_nlattr_add8(&msg->nh, TCA_FLOWER_KEY_IP_PROTO,
662                             spec->hdr.next_proto_id);
663         return 0;
664 }
665
666 /**
667  * Make as much checks as possible on an IPv6 item, and if a flow is provided,
668  * fill it appropriately with IPv6 info.
669  *
670  * @param[in] item
671  *   Item specification.
672  * @param[in, out] data
673  *   Additional data structure to tell next layers we've been here.
674  *
675  * @return
676  *   0 if checks are alright, -1 otherwise.
677  */
678 static int
679 tap_flow_create_ipv6(const struct rte_flow_item *item, void *data)
680 {
681         struct convert_data *info = (struct convert_data *)data;
682         const struct rte_flow_item_ipv6 *spec = item->spec;
683         const struct rte_flow_item_ipv6 *mask = item->mask;
684         struct rte_flow *flow = info->flow;
685         uint8_t empty_addr[16] = { 0 };
686         struct nlmsg *msg;
687
688         /* use default mask if none provided */
689         if (!mask)
690                 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_IPV6].default_mask;
691         /* check that previous eth type is compatible with ipv6 */
692         if (info->eth_type && info->eth_type != htons(ETH_P_IPV6))
693                 return -1;
694         /* store ip_proto for consistency if udp/tcp pattern item comes next */
695         if (spec)
696                 info->ip_proto = spec->hdr.proto;
697         if (!flow)
698                 return 0;
699         msg = &flow->msg;
700         if (!info->eth_type)
701                 info->eth_type = htons(ETH_P_IPV6);
702         if (!spec)
703                 return 0;
704         if (memcmp(spec->hdr.dst_addr, empty_addr, 16)) {
705                 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_DST,
706                            sizeof(spec->hdr.dst_addr), &spec->hdr.dst_addr);
707                 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_DST_MASK,
708                            sizeof(mask->hdr.dst_addr), &mask->hdr.dst_addr);
709         }
710         if (memcmp(spec->hdr.src_addr, empty_addr, 16)) {
711                 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_SRC,
712                            sizeof(spec->hdr.src_addr), &spec->hdr.src_addr);
713                 tap_nlattr_add(&msg->nh, TCA_FLOWER_KEY_IPV6_SRC_MASK,
714                            sizeof(mask->hdr.src_addr), &mask->hdr.src_addr);
715         }
716         if (spec->hdr.proto)
717                 tap_nlattr_add8(&msg->nh,
718                                 TCA_FLOWER_KEY_IP_PROTO, spec->hdr.proto);
719         return 0;
720 }
721
722 /**
723  * Make as much checks as possible on a UDP item, and if a flow is provided,
724  * fill it appropriately with UDP info.
725  *
726  * @param[in] item
727  *   Item specification.
728  * @param[in, out] data
729  *   Additional data structure to tell next layers we've been here.
730  *
731  * @return
732  *   0 if checks are alright, -1 otherwise.
733  */
734 static int
735 tap_flow_create_udp(const struct rte_flow_item *item, void *data)
736 {
737         struct convert_data *info = (struct convert_data *)data;
738         const struct rte_flow_item_udp *spec = item->spec;
739         const struct rte_flow_item_udp *mask = item->mask;
740         struct rte_flow *flow = info->flow;
741         struct nlmsg *msg;
742
743         /* use default mask if none provided */
744         if (!mask)
745                 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_UDP].default_mask;
746         /* check that previous ip_proto is compatible with udp */
747         if (info->ip_proto && info->ip_proto != IPPROTO_UDP)
748                 return -1;
749         /* TC does not support UDP port masking. Only accept if exact match. */
750         if ((mask->hdr.src_port && mask->hdr.src_port != 0xffff) ||
751             (mask->hdr.dst_port && mask->hdr.dst_port != 0xffff))
752                 return -1;
753         if (!flow)
754                 return 0;
755         msg = &flow->msg;
756         tap_nlattr_add8(&msg->nh, TCA_FLOWER_KEY_IP_PROTO, IPPROTO_UDP);
757         if (!spec)
758                 return 0;
759         if (spec->hdr.dst_port & mask->hdr.dst_port)
760                 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_UDP_DST,
761                              spec->hdr.dst_port);
762         if (spec->hdr.src_port & mask->hdr.src_port)
763                 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_UDP_SRC,
764                              spec->hdr.src_port);
765         return 0;
766 }
767
768 /**
769  * Make as much checks as possible on a TCP item, and if a flow is provided,
770  * fill it appropriately with TCP info.
771  *
772  * @param[in] item
773  *   Item specification.
774  * @param[in, out] data
775  *   Additional data structure to tell next layers we've been here.
776  *
777  * @return
778  *   0 if checks are alright, -1 otherwise.
779  */
780 static int
781 tap_flow_create_tcp(const struct rte_flow_item *item, void *data)
782 {
783         struct convert_data *info = (struct convert_data *)data;
784         const struct rte_flow_item_tcp *spec = item->spec;
785         const struct rte_flow_item_tcp *mask = item->mask;
786         struct rte_flow *flow = info->flow;
787         struct nlmsg *msg;
788
789         /* use default mask if none provided */
790         if (!mask)
791                 mask = tap_flow_items[RTE_FLOW_ITEM_TYPE_TCP].default_mask;
792         /* check that previous ip_proto is compatible with tcp */
793         if (info->ip_proto && info->ip_proto != IPPROTO_TCP)
794                 return -1;
795         /* TC does not support TCP port masking. Only accept if exact match. */
796         if ((mask->hdr.src_port && mask->hdr.src_port != 0xffff) ||
797             (mask->hdr.dst_port && mask->hdr.dst_port != 0xffff))
798                 return -1;
799         if (!flow)
800                 return 0;
801         msg = &flow->msg;
802         tap_nlattr_add8(&msg->nh, TCA_FLOWER_KEY_IP_PROTO, IPPROTO_TCP);
803         if (!spec)
804                 return 0;
805         if (spec->hdr.dst_port & mask->hdr.dst_port)
806                 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_TCP_DST,
807                              spec->hdr.dst_port);
808         if (spec->hdr.src_port & mask->hdr.src_port)
809                 tap_nlattr_add16(&msg->nh, TCA_FLOWER_KEY_TCP_SRC,
810                              spec->hdr.src_port);
811         return 0;
812 }
813
814 /**
815  * Check support for a given item.
816  *
817  * @param[in] item
818  *   Item specification.
819  * @param size
820  *   Bit-Mask size in bytes.
821  * @param[in] supported_mask
822  *   Bit-mask covering supported fields to compare with spec, last and mask in
823  *   \item.
824  * @param[in] default_mask
825  *   Bit-mask default mask if none is provided in \item.
826  *
827  * @return
828  *   0 on success.
829  */
830 static int
831 tap_flow_item_validate(const struct rte_flow_item *item,
832                        unsigned int size,
833                        const uint8_t *supported_mask,
834                        const uint8_t *default_mask)
835 {
836         int ret = 0;
837
838         /* An empty layer is allowed, as long as all fields are NULL */
839         if (!item->spec && (item->mask || item->last))
840                 return -1;
841         /* Is the item spec compatible with what the NIC supports? */
842         if (item->spec && !item->mask) {
843                 unsigned int i;
844                 const uint8_t *spec = item->spec;
845
846                 for (i = 0; i < size; ++i)
847                         if ((spec[i] | supported_mask[i]) != supported_mask[i])
848                                 return -1;
849                 /* Is the default mask compatible with what the NIC supports? */
850                 for (i = 0; i < size; i++)
851                         if ((default_mask[i] | supported_mask[i]) !=
852                             supported_mask[i])
853                                 return -1;
854         }
855         /* Is the item last compatible with what the NIC supports? */
856         if (item->last && !item->mask) {
857                 unsigned int i;
858                 const uint8_t *spec = item->last;
859
860                 for (i = 0; i < size; ++i)
861                         if ((spec[i] | supported_mask[i]) != supported_mask[i])
862                                 return -1;
863         }
864         /* Is the item mask compatible with what the NIC supports? */
865         if (item->mask) {
866                 unsigned int i;
867                 const uint8_t *spec = item->mask;
868
869                 for (i = 0; i < size; ++i)
870                         if ((spec[i] | supported_mask[i]) != supported_mask[i])
871                                 return -1;
872         }
873         /**
874          * Once masked, Are item spec and item last equal?
875          * TC does not support range so anything else is invalid.
876          */
877         if (item->spec && item->last) {
878                 uint8_t spec[size];
879                 uint8_t last[size];
880                 const uint8_t *apply = default_mask;
881                 unsigned int i;
882
883                 if (item->mask)
884                         apply = item->mask;
885                 for (i = 0; i < size; ++i) {
886                         spec[i] = ((const uint8_t *)item->spec)[i] & apply[i];
887                         last[i] = ((const uint8_t *)item->last)[i] & apply[i];
888                 }
889                 ret = memcmp(spec, last, size);
890         }
891         return ret;
892 }
893
894 /**
895  * Configure the kernel with a TC action and its configured parameters
896  * Handled actions: "gact", "mirred", "skbedit", "bpf"
897  *
898  * @param[in] flow
899  *   Pointer to rte flow containing the netlink message
900  *
901  * @param[in, out] act_index
902  *   Pointer to action sequence number in the TC command
903  *
904  * @param[in] adata
905  *  Pointer to struct holding the action parameters
906  *
907  * @return
908  *   -1 on failure, 0 on success
909  */
910 static int
911 add_action(struct rte_flow *flow, size_t *act_index, struct action_data *adata)
912 {
913         struct nlmsg *msg = &flow->msg;
914
915         if (tap_nlattr_nested_start(msg, (*act_index)++) < 0)
916                 return -1;
917
918         tap_nlattr_add(&msg->nh, TCA_ACT_KIND,
919                                 strlen(adata->id) + 1, adata->id);
920         if (tap_nlattr_nested_start(msg, TCA_ACT_OPTIONS) < 0)
921                 return -1;
922         if (strcmp("gact", adata->id) == 0) {
923                 tap_nlattr_add(&msg->nh, TCA_GACT_PARMS, sizeof(adata->gact),
924                            &adata->gact);
925         } else if (strcmp("mirred", adata->id) == 0) {
926                 if (adata->mirred.eaction == TCA_EGRESS_MIRROR)
927                         adata->mirred.action = TC_ACT_PIPE;
928                 else /* REDIRECT */
929                         adata->mirred.action = TC_ACT_STOLEN;
930                 tap_nlattr_add(&msg->nh, TCA_MIRRED_PARMS,
931                            sizeof(adata->mirred),
932                            &adata->mirred);
933         } else if (strcmp("skbedit", adata->id) == 0) {
934                 tap_nlattr_add(&msg->nh, TCA_SKBEDIT_PARMS,
935                            sizeof(adata->skbedit.skbedit),
936                            &adata->skbedit.skbedit);
937                 tap_nlattr_add16(&msg->nh, TCA_SKBEDIT_QUEUE_MAPPING,
938                              adata->skbedit.queue);
939         } else if (strcmp("bpf", adata->id) == 0) {
940                 tap_nlattr_add32(&msg->nh, TCA_ACT_BPF_FD, adata->bpf.bpf_fd);
941                 tap_nlattr_add(&msg->nh, TCA_ACT_BPF_NAME,
942                            strlen(adata->bpf.annotation) + 1,
943                            adata->bpf.annotation);
944                 tap_nlattr_add(&msg->nh, TCA_ACT_BPF_PARMS,
945                            sizeof(adata->bpf.bpf),
946                            &adata->bpf.bpf);
947         } else {
948                 return -1;
949         }
950         tap_nlattr_nested_finish(msg); /* nested TCA_ACT_OPTIONS */
951         tap_nlattr_nested_finish(msg); /* nested act_index */
952         return 0;
953 }
954
955 /**
956  * Helper function to send a serie of TC actions to the kernel
957  *
958  * @param[in] flow
959  *   Pointer to rte flow containing the netlink message
960  *
961  * @param[in] nb_actions
962  *   Number of actions in an array of action structs
963  *
964  * @param[in] data
965  *   Pointer to an array of action structs
966  *
967  * @param[in] classifier_actions
968  *   The classifier on behave of which the actions are configured
969  *
970  * @return
971  *   -1 on failure, 0 on success
972  */
973 static int
974 add_actions(struct rte_flow *flow, int nb_actions, struct action_data *data,
975             int classifier_action)
976 {
977         struct nlmsg *msg = &flow->msg;
978         size_t act_index = 1;
979         int i;
980
981         if (tap_nlattr_nested_start(msg, classifier_action) < 0)
982                 return -1;
983         for (i = 0; i < nb_actions; i++)
984                 if (add_action(flow, &act_index, data + i) < 0)
985                         return -1;
986         tap_nlattr_nested_finish(msg); /* nested TCA_FLOWER_ACT */
987         return 0;
988 }
989
990 /**
991  * Validate a flow supported by TC.
992  * If flow param is not NULL, then also fill the netlink message inside.
993  *
994  * @param pmd
995  *   Pointer to private structure.
996  * @param[in] attr
997  *   Flow rule attributes.
998  * @param[in] pattern
999  *   Pattern specification (list terminated by the END pattern item).
1000  * @param[in] actions
1001  *   Associated actions (list terminated by the END action).
1002  * @param[out] error
1003  *   Perform verbose error reporting if not NULL.
1004  * @param[in, out] flow
1005  *   Flow structure to update.
1006  * @param[in] mirred
1007  *   If set to TCA_EGRESS_REDIR, provided actions will be replaced with a
1008  *   redirection to the tap netdevice, and the TC rule will be configured
1009  *   on the remote netdevice in pmd.
1010  *   If set to TCA_EGRESS_MIRROR, provided actions will be replaced with a
1011  *   mirroring to the tap netdevice, and the TC rule will be configured
1012  *   on the remote netdevice in pmd. Matching packets will thus be duplicated.
1013  *   If set to 0, the standard behavior is to be used: set correct actions for
1014  *   the TC rule, and apply it on the tap netdevice.
1015  *
1016  * @return
1017  *   0 on success, a negative errno value otherwise and rte_errno is set.
1018  */
1019 static int
1020 priv_flow_process(struct pmd_internals *pmd,
1021                   const struct rte_flow_attr *attr,
1022                   const struct rte_flow_item items[],
1023                   const struct rte_flow_action actions[],
1024                   struct rte_flow_error *error,
1025                   struct rte_flow *flow,
1026                   int mirred)
1027 {
1028         const struct tap_flow_items *cur_item = tap_flow_items;
1029         struct convert_data data = {
1030                 .eth_type = 0,
1031                 .ip_proto = 0,
1032                 .flow = flow,
1033         };
1034         int action = 0; /* Only one action authorized for now */
1035
1036         if (attr->group > MAX_GROUP) {
1037                 rte_flow_error_set(
1038                         error, EINVAL, RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
1039                         NULL, "group value too big: cannot exceed 15");
1040                 return -rte_errno;
1041         }
1042         if (attr->priority > MAX_PRIORITY) {
1043                 rte_flow_error_set(
1044                         error, EINVAL, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1045                         NULL, "priority value too big");
1046                 return -rte_errno;
1047         } else if (flow) {
1048                 uint16_t group = attr->group << GROUP_SHIFT;
1049                 uint16_t prio = group | (attr->priority +
1050                                 RSS_PRIORITY_OFFSET + PRIORITY_OFFSET);
1051                 flow->msg.t.tcm_info = TC_H_MAKE(prio << 16,
1052                                                  flow->msg.t.tcm_info);
1053         }
1054         if (flow) {
1055                 if (mirred) {
1056                         /*
1057                          * If attr->ingress, the rule applies on remote ingress
1058                          * to match incoming packets
1059                          * If attr->egress, the rule applies on tap ingress (as
1060                          * seen from the kernel) to deal with packets going out
1061                          * from the DPDK app.
1062                          */
1063                         flow->msg.t.tcm_parent = TC_H_MAKE(TC_H_INGRESS, 0);
1064                 } else {
1065                         /* Standard rule on tap egress (kernel standpoint). */
1066                         flow->msg.t.tcm_parent =
1067                                 TC_H_MAKE(MULTIQ_MAJOR_HANDLE, 0);
1068                 }
1069                 /* use flower filter type */
1070                 tap_nlattr_add(&flow->msg.nh, TCA_KIND, sizeof("flower"), "flower");
1071                 if (tap_nlattr_nested_start(&flow->msg, TCA_OPTIONS) < 0)
1072                         goto exit_item_not_supported;
1073         }
1074         for (; items->type != RTE_FLOW_ITEM_TYPE_END; ++items) {
1075                 const struct tap_flow_items *token = NULL;
1076                 unsigned int i;
1077                 int err = 0;
1078
1079                 if (items->type == RTE_FLOW_ITEM_TYPE_VOID)
1080                         continue;
1081                 for (i = 0;
1082                      cur_item->items &&
1083                      cur_item->items[i] != RTE_FLOW_ITEM_TYPE_END;
1084                      ++i) {
1085                         if (cur_item->items[i] == items->type) {
1086                                 token = &tap_flow_items[items->type];
1087                                 break;
1088                         }
1089                 }
1090                 if (!token)
1091                         goto exit_item_not_supported;
1092                 cur_item = token;
1093                 err = tap_flow_item_validate(
1094                         items, cur_item->mask_sz,
1095                         (const uint8_t *)cur_item->mask,
1096                         (const uint8_t *)cur_item->default_mask);
1097                 if (err)
1098                         goto exit_item_not_supported;
1099                 if (flow && cur_item->convert) {
1100                         err = cur_item->convert(items, &data);
1101                         if (err)
1102                                 goto exit_item_not_supported;
1103                 }
1104         }
1105         if (flow) {
1106                 if (data.vlan) {
1107                         tap_nlattr_add16(&flow->msg.nh, TCA_FLOWER_KEY_ETH_TYPE,
1108                                      htons(ETH_P_8021Q));
1109                         tap_nlattr_add16(&flow->msg.nh,
1110                                      TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1111                                      data.eth_type ?
1112                                      data.eth_type : htons(ETH_P_ALL));
1113                 } else if (data.eth_type) {
1114                         tap_nlattr_add16(&flow->msg.nh, TCA_FLOWER_KEY_ETH_TYPE,
1115                                      data.eth_type);
1116                 }
1117         }
1118         if (mirred && flow) {
1119                 struct action_data adata = {
1120                         .id = "mirred",
1121                         .mirred = {
1122                                 .eaction = mirred,
1123                         },
1124                 };
1125
1126                 /*
1127                  * If attr->egress && mirred, then this is a special
1128                  * case where the rule must be applied on the tap, to
1129                  * redirect packets coming from the DPDK App, out
1130                  * through the remote netdevice.
1131                  */
1132                 adata.mirred.ifindex = attr->ingress ? pmd->if_index :
1133                         pmd->remote_if_index;
1134                 if (mirred == TCA_EGRESS_MIRROR)
1135                         adata.mirred.action = TC_ACT_PIPE;
1136                 else
1137                         adata.mirred.action = TC_ACT_STOLEN;
1138                 if (add_actions(flow, 1, &adata, TCA_FLOWER_ACT) < 0)
1139                         goto exit_action_not_supported;
1140                 else
1141                         goto end;
1142         }
1143 actions:
1144         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; ++actions) {
1145                 int err = 0;
1146
1147                 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID) {
1148                         continue;
1149                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_DROP) {
1150                         if (action)
1151                                 goto exit_action_not_supported;
1152                         action = 1;
1153                         if (flow) {
1154                                 struct action_data adata = {
1155                                         .id = "gact",
1156                                         .gact = {
1157                                                 .action = TC_ACT_SHOT,
1158                                         },
1159                                 };
1160
1161                                 err = add_actions(flow, 1, &adata,
1162                                                   TCA_FLOWER_ACT);
1163                         }
1164                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_PASSTHRU) {
1165                         if (action)
1166                                 goto exit_action_not_supported;
1167                         action = 1;
1168                         if (flow) {
1169                                 struct action_data adata = {
1170                                         .id = "gact",
1171                                         .gact = {
1172                                                 /* continue */
1173                                                 .action = TC_ACT_UNSPEC,
1174                                         },
1175                                 };
1176
1177                                 err = add_actions(flow, 1, &adata,
1178                                                   TCA_FLOWER_ACT);
1179                         }
1180                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_QUEUE) {
1181                         const struct rte_flow_action_queue *queue =
1182                                 (const struct rte_flow_action_queue *)
1183                                 actions->conf;
1184
1185                         if (action)
1186                                 goto exit_action_not_supported;
1187                         action = 1;
1188                         if (!queue ||
1189                             (queue->index > pmd->dev->data->nb_rx_queues - 1))
1190                                 goto exit_action_not_supported;
1191                         if (flow) {
1192                                 struct action_data adata = {
1193                                         .id = "skbedit",
1194                                         .skbedit = {
1195                                                 .skbedit = {
1196                                                         .action = TC_ACT_PIPE,
1197                                                 },
1198                                                 .queue = queue->index,
1199                                         },
1200                                 };
1201
1202                                 err = add_actions(flow, 1, &adata,
1203                                         TCA_FLOWER_ACT);
1204                         }
1205                 } else if (actions->type == RTE_FLOW_ACTION_TYPE_RSS) {
1206                         const struct rte_flow_action_rss *rss =
1207                                 (const struct rte_flow_action_rss *)
1208                                 actions->conf;
1209
1210                         if (action++)
1211                                 goto exit_action_not_supported;
1212
1213                         if (!pmd->rss_enabled) {
1214                                 err = rss_enable(pmd, attr, error);
1215                                 if (err)
1216                                         goto exit_action_not_supported;
1217                         }
1218                         if (flow && rss)
1219                                 err = rss_add_actions(flow, pmd, rss, error);
1220                 } else {
1221                         goto exit_action_not_supported;
1222                 }
1223                 if (err)
1224                         goto exit_action_not_supported;
1225         }
1226         /* When fate is unknown, drop traffic. */
1227         if (!action) {
1228                 static const struct rte_flow_action drop[] = {
1229                         { .type = RTE_FLOW_ACTION_TYPE_DROP, },
1230                         { .type = RTE_FLOW_ACTION_TYPE_END, },
1231                 };
1232
1233                 actions = drop;
1234                 goto actions;
1235         }
1236 end:
1237         if (flow)
1238                 tap_nlattr_nested_finish(&flow->msg); /* nested TCA_OPTIONS */
1239         return 0;
1240 exit_item_not_supported:
1241         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
1242                            items, "item not supported");
1243         return -rte_errno;
1244 exit_action_not_supported:
1245         rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
1246                            actions, "action not supported");
1247         return -rte_errno;
1248 }
1249
1250
1251
1252 /**
1253  * Validate a flow.
1254  *
1255  * @see rte_flow_validate()
1256  * @see rte_flow_ops
1257  */
1258 static int
1259 tap_flow_validate(struct rte_eth_dev *dev,
1260                   const struct rte_flow_attr *attr,
1261                   const struct rte_flow_item items[],
1262                   const struct rte_flow_action actions[],
1263                   struct rte_flow_error *error)
1264 {
1265         struct pmd_internals *pmd = dev->data->dev_private;
1266
1267         return priv_flow_process(pmd, attr, items, actions, error, NULL, 0);
1268 }
1269
1270 /**
1271  * Set a unique handle in a flow.
1272  *
1273  * The kernel supports TC rules with equal priority, as long as they use the
1274  * same matching fields (e.g.: dst mac and ipv4) with different values (and
1275  * full mask to ensure no collision is possible).
1276  * In those rules, the handle (uint32_t) is the part that would identify
1277  * specifically each rule.
1278  *
1279  * On 32-bit architectures, the handle can simply be the flow's pointer address.
1280  * On 64-bit architectures, we rely on jhash(flow) to find a (sufficiently)
1281  * unique handle.
1282  *
1283  * @param[in, out] flow
1284  *   The flow that needs its handle set.
1285  */
1286 static void
1287 tap_flow_set_handle(struct rte_flow *flow)
1288 {
1289         uint32_t handle = 0;
1290
1291         if (sizeof(flow) > 4)
1292                 handle = rte_jhash(&flow, sizeof(flow), 1);
1293         else
1294                 handle = (uintptr_t)flow;
1295         /* must be at least 1 to avoid letting the kernel choose one for us */
1296         if (!handle)
1297                 handle = 1;
1298         flow->msg.t.tcm_handle = handle;
1299 }
1300
1301 /**
1302  * Free the flow opened file descriptors and allocated memory
1303  *
1304  * @param[in] flow
1305  *   Pointer to the flow to free
1306  *
1307  */
1308 static void
1309 tap_flow_free(struct pmd_internals *pmd, struct rte_flow *flow)
1310 {
1311         int i;
1312
1313         if (!flow)
1314                 return;
1315
1316         if (pmd->rss_enabled) {
1317                 /* Close flow BPF file descriptors */
1318                 for (i = 0; i < SEC_MAX; i++)
1319                         if (flow->bpf_fd[i] != 0) {
1320                                 close(flow->bpf_fd[i]);
1321                                 flow->bpf_fd[i] = 0;
1322                         }
1323
1324                 /* Release the map key for this RSS rule */
1325                 bpf_rss_key(KEY_CMD_RELEASE, &flow->key_idx);
1326                 flow->key_idx = 0;
1327         }
1328
1329         /* Free flow allocated memory */
1330         rte_free(flow);
1331 }
1332
1333 /**
1334  * Create a flow.
1335  *
1336  * @see rte_flow_create()
1337  * @see rte_flow_ops
1338  */
1339 static struct rte_flow *
1340 tap_flow_create(struct rte_eth_dev *dev,
1341                 const struct rte_flow_attr *attr,
1342                 const struct rte_flow_item items[],
1343                 const struct rte_flow_action actions[],
1344                 struct rte_flow_error *error)
1345 {
1346         struct pmd_internals *pmd = dev->data->dev_private;
1347         struct rte_flow *remote_flow = NULL;
1348         struct rte_flow *flow = NULL;
1349         struct nlmsg *msg = NULL;
1350         int err;
1351
1352         if (!pmd->if_index) {
1353                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1354                                    NULL,
1355                                    "can't create rule, ifindex not found");
1356                 goto fail;
1357         }
1358         /*
1359          * No rules configured through standard rte_flow should be set on the
1360          * priorities used by implicit rules.
1361          */
1362         if ((attr->group == MAX_GROUP) &&
1363             attr->priority > (MAX_PRIORITY - TAP_REMOTE_MAX_IDX)) {
1364                 rte_flow_error_set(
1365                         error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
1366                         NULL, "priority value too big");
1367                 goto fail;
1368         }
1369         flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1370         if (!flow) {
1371                 rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1372                                    NULL, "cannot allocate memory for rte_flow");
1373                 goto fail;
1374         }
1375         msg = &flow->msg;
1376         tc_init_msg(msg, pmd->if_index, RTM_NEWTFILTER,
1377                     NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
1378         msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1379         tap_flow_set_handle(flow);
1380         if (priv_flow_process(pmd, attr, items, actions, error, flow, 0))
1381                 goto fail;
1382         err = tap_nl_send(pmd->nlsk_fd, &msg->nh);
1383         if (err < 0) {
1384                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1385                                    NULL, "couldn't send request to kernel");
1386                 goto fail;
1387         }
1388         err = tap_nl_recv_ack(pmd->nlsk_fd);
1389         if (err < 0) {
1390                 RTE_LOG(ERR, PMD,
1391                         "Kernel refused TC filter rule creation (%d): %s\n",
1392                         errno, strerror(errno));
1393                 rte_flow_error_set(error, EEXIST, RTE_FLOW_ERROR_TYPE_HANDLE,
1394                                    NULL,
1395                                    "overlapping rules or Kernel too old for flower support");
1396                 goto fail;
1397         }
1398         LIST_INSERT_HEAD(&pmd->flows, flow, next);
1399         /**
1400          * If a remote device is configured, a TC rule with identical items for
1401          * matching must be set on that device, with a single action: redirect
1402          * to the local pmd->if_index.
1403          */
1404         if (pmd->remote_if_index) {
1405                 remote_flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1406                 if (!remote_flow) {
1407                         rte_flow_error_set(
1408                                 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1409                                 "cannot allocate memory for rte_flow");
1410                         goto fail;
1411                 }
1412                 msg = &remote_flow->msg;
1413                 /* set the rule if_index for the remote netdevice */
1414                 tc_init_msg(
1415                         msg, pmd->remote_if_index, RTM_NEWTFILTER,
1416                         NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
1417                 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1418                 tap_flow_set_handle(remote_flow);
1419                 if (priv_flow_process(pmd, attr, items, NULL,
1420                                       error, remote_flow, TCA_EGRESS_REDIR)) {
1421                         rte_flow_error_set(
1422                                 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1423                                 NULL, "rte flow rule validation failed");
1424                         goto fail;
1425                 }
1426                 err = tap_nl_send(pmd->nlsk_fd, &msg->nh);
1427                 if (err < 0) {
1428                         rte_flow_error_set(
1429                                 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1430                                 NULL, "Failure sending nl request");
1431                         goto fail;
1432                 }
1433                 err = tap_nl_recv_ack(pmd->nlsk_fd);
1434                 if (err < 0) {
1435                         RTE_LOG(ERR, PMD,
1436                                 "Kernel refused TC filter rule creation (%d): %s\n",
1437                                 errno, strerror(errno));
1438                         rte_flow_error_set(
1439                                 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1440                                 NULL,
1441                                 "overlapping rules or Kernel too old for flower support");
1442                         goto fail;
1443                 }
1444                 flow->remote_flow = remote_flow;
1445         }
1446         return flow;
1447 fail:
1448         if (remote_flow)
1449                 rte_free(remote_flow);
1450         if (flow)
1451                 tap_flow_free(pmd, flow);
1452         return NULL;
1453 }
1454
1455 /**
1456  * Destroy a flow using pointer to pmd_internal.
1457  *
1458  * @param[in, out] pmd
1459  *   Pointer to private structure.
1460  * @param[in] flow
1461  *   Pointer to the flow to destroy.
1462  * @param[in, out] error
1463  *   Pointer to the flow error handler
1464  *
1465  * @return 0 if the flow could be destroyed, -1 otherwise.
1466  */
1467 static int
1468 tap_flow_destroy_pmd(struct pmd_internals *pmd,
1469                      struct rte_flow *flow,
1470                      struct rte_flow_error *error)
1471 {
1472         struct rte_flow *remote_flow = flow->remote_flow;
1473         int ret = 0;
1474
1475         LIST_REMOVE(flow, next);
1476         flow->msg.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1477         flow->msg.nh.nlmsg_type = RTM_DELTFILTER;
1478
1479         ret = tap_nl_send(pmd->nlsk_fd, &flow->msg.nh);
1480         if (ret < 0) {
1481                 rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1482                                    NULL, "couldn't send request to kernel");
1483                 goto end;
1484         }
1485         ret = tap_nl_recv_ack(pmd->nlsk_fd);
1486         /* If errno is ENOENT, the rule is already no longer in the kernel. */
1487         if (ret < 0 && errno == ENOENT)
1488                 ret = 0;
1489         if (ret < 0) {
1490                 RTE_LOG(ERR, PMD,
1491                         "Kernel refused TC filter rule deletion (%d): %s\n",
1492                         errno, strerror(errno));
1493                 rte_flow_error_set(
1494                         error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1495                         "couldn't receive kernel ack to our request");
1496                 goto end;
1497         }
1498
1499         if (remote_flow) {
1500                 remote_flow->msg.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1501                 remote_flow->msg.nh.nlmsg_type = RTM_DELTFILTER;
1502
1503                 ret = tap_nl_send(pmd->nlsk_fd, &remote_flow->msg.nh);
1504                 if (ret < 0) {
1505                         rte_flow_error_set(
1506                                 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1507                                 NULL, "Failure sending nl request");
1508                         goto end;
1509                 }
1510                 ret = tap_nl_recv_ack(pmd->nlsk_fd);
1511                 if (ret < 0 && errno == ENOENT)
1512                         ret = 0;
1513                 if (ret < 0) {
1514                         RTE_LOG(ERR, PMD,
1515                                 "Kernel refused TC filter rule deletion (%d): %s\n",
1516                                 errno, strerror(errno));
1517                         rte_flow_error_set(
1518                                 error, ENOMEM, RTE_FLOW_ERROR_TYPE_HANDLE,
1519                                 NULL, "Failure trying to receive nl ack");
1520                         goto end;
1521                 }
1522         }
1523 end:
1524         if (remote_flow)
1525                 rte_free(remote_flow);
1526         tap_flow_free(pmd, flow);
1527         return ret;
1528 }
1529
1530 /**
1531  * Destroy a flow.
1532  *
1533  * @see rte_flow_destroy()
1534  * @see rte_flow_ops
1535  */
1536 static int
1537 tap_flow_destroy(struct rte_eth_dev *dev,
1538                  struct rte_flow *flow,
1539                  struct rte_flow_error *error)
1540 {
1541         struct pmd_internals *pmd = dev->data->dev_private;
1542
1543         return tap_flow_destroy_pmd(pmd, flow, error);
1544 }
1545
1546 /**
1547  * Enable/disable flow isolation.
1548  *
1549  * @see rte_flow_isolate()
1550  * @see rte_flow_ops
1551  */
1552 static int
1553 tap_flow_isolate(struct rte_eth_dev *dev,
1554                  int set,
1555                  struct rte_flow_error *error __rte_unused)
1556 {
1557         struct pmd_internals *pmd = dev->data->dev_private;
1558
1559         if (set)
1560                 pmd->flow_isolate = 1;
1561         else
1562                 pmd->flow_isolate = 0;
1563         /*
1564          * If netdevice is there, setup appropriate flow rules immediately.
1565          * Otherwise it will be set when bringing up the netdevice (tun_alloc).
1566          */
1567         if (!pmd->rxq[0].fd)
1568                 return 0;
1569         if (set) {
1570                 struct rte_flow *flow;
1571
1572                 while (1) {
1573                         flow = LIST_FIRST(&pmd->implicit_flows);
1574                         if (!flow)
1575                                 break;
1576                         /*
1577                          * Remove all implicit rules on the remote.
1578                          * Keep the local rule to redirect packets on TX.
1579                          * Keep also the last implicit local rule: ISOLATE.
1580                          */
1581                         if (flow->msg.t.tcm_ifindex == pmd->if_index)
1582                                 break;
1583                         if (tap_flow_destroy_pmd(pmd, flow, NULL) < 0)
1584                                 goto error;
1585                 }
1586                 /* Switch the TC rule according to pmd->flow_isolate */
1587                 if (tap_flow_implicit_create(pmd, TAP_ISOLATE) == -1)
1588                         goto error;
1589         } else {
1590                 /* Switch the TC rule according to pmd->flow_isolate */
1591                 if (tap_flow_implicit_create(pmd, TAP_ISOLATE) == -1)
1592                         goto error;
1593                 if (!pmd->remote_if_index)
1594                         return 0;
1595                 if (tap_flow_implicit_create(pmd, TAP_REMOTE_TX) < 0)
1596                         goto error;
1597                 if (tap_flow_implicit_create(pmd, TAP_REMOTE_LOCAL_MAC) < 0)
1598                         goto error;
1599                 if (tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCAST) < 0)
1600                         goto error;
1601                 if (tap_flow_implicit_create(pmd, TAP_REMOTE_BROADCASTV6) < 0)
1602                         goto error;
1603                 if (dev->data->promiscuous &&
1604                     tap_flow_implicit_create(pmd, TAP_REMOTE_PROMISC) < 0)
1605                         goto error;
1606                 if (dev->data->all_multicast &&
1607                     tap_flow_implicit_create(pmd, TAP_REMOTE_ALLMULTI) < 0)
1608                         goto error;
1609         }
1610         return 0;
1611 error:
1612         pmd->flow_isolate = 0;
1613         return rte_flow_error_set(
1614                 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1615                 "TC rule creation failed");
1616 }
1617
1618 /**
1619  * Destroy all flows.
1620  *
1621  * @see rte_flow_flush()
1622  * @see rte_flow_ops
1623  */
1624 int
1625 tap_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error)
1626 {
1627         struct pmd_internals *pmd = dev->data->dev_private;
1628         struct rte_flow *flow;
1629
1630         while (!LIST_EMPTY(&pmd->flows)) {
1631                 flow = LIST_FIRST(&pmd->flows);
1632                 if (tap_flow_destroy(dev, flow, error) < 0)
1633                         return -1;
1634         }
1635         return 0;
1636 }
1637
1638 /**
1639  * Add an implicit flow rule on the remote device to make sure traffic gets to
1640  * the tap netdevice from there.
1641  *
1642  * @param pmd
1643  *   Pointer to private structure.
1644  * @param[in] idx
1645  *   The idx in the implicit_rte_flows array specifying which rule to apply.
1646  *
1647  * @return -1 if the rule couldn't be applied, 0 otherwise.
1648  */
1649 int tap_flow_implicit_create(struct pmd_internals *pmd,
1650                              enum implicit_rule_index idx)
1651 {
1652         uint16_t flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE;
1653         struct rte_flow_action *actions = implicit_rte_flows[idx].actions;
1654         struct rte_flow_action isolate_actions[2] = {
1655                 [1] = {
1656                         .type = RTE_FLOW_ACTION_TYPE_END,
1657                 },
1658         };
1659         struct rte_flow_item *items = implicit_rte_flows[idx].items;
1660         struct rte_flow_attr *attr = &implicit_rte_flows[idx].attr;
1661         struct rte_flow_item_eth eth_local = { .type = 0 };
1662         uint16_t if_index = pmd->remote_if_index;
1663         struct rte_flow *remote_flow = NULL;
1664         struct nlmsg *msg = NULL;
1665         int err = 0;
1666         struct rte_flow_item items_local[2] = {
1667                 [0] = {
1668                         .type = items[0].type,
1669                         .spec = &eth_local,
1670                         .mask = items[0].mask,
1671                 },
1672                 [1] = {
1673                         .type = items[1].type,
1674                 }
1675         };
1676
1677         remote_flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1678         if (!remote_flow) {
1679                 RTE_LOG(ERR, PMD, "Cannot allocate memory for rte_flow\n");
1680                 goto fail;
1681         }
1682         msg = &remote_flow->msg;
1683         if (idx == TAP_REMOTE_TX) {
1684                 if_index = pmd->if_index;
1685         } else if (idx == TAP_ISOLATE) {
1686                 if_index = pmd->if_index;
1687                 /* Don't be exclusive for this rule, it can be changed later. */
1688                 flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE;
1689                 isolate_actions[0].type = pmd->flow_isolate ?
1690                         RTE_FLOW_ACTION_TYPE_DROP :
1691                         RTE_FLOW_ACTION_TYPE_PASSTHRU;
1692                 actions = isolate_actions;
1693         } else if (idx == TAP_REMOTE_LOCAL_MAC) {
1694                 /*
1695                  * eth addr couldn't be set in implicit_rte_flows[] as it is not
1696                  * known at compile time.
1697                  */
1698                 memcpy(&eth_local.dst, &pmd->eth_addr, sizeof(pmd->eth_addr));
1699                 items = items_local;
1700         }
1701         tc_init_msg(msg, if_index, RTM_NEWTFILTER, flags);
1702         msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1703         /*
1704          * The ISOLATE rule is always present and must have a static handle, as
1705          * the action is changed whether the feature is enabled (DROP) or
1706          * disabled (PASSTHRU).
1707          * There is just one REMOTE_PROMISCUOUS rule in all cases. It should
1708          * have a static handle such that adding it twice will fail with EEXIST
1709          * with any kernel version. Remark: old kernels may falsely accept the
1710          * same REMOTE_PROMISCUOUS rules if they had different handles.
1711          */
1712         if (idx == TAP_ISOLATE)
1713                 remote_flow->msg.t.tcm_handle = ISOLATE_HANDLE;
1714         else if (idx == TAP_REMOTE_PROMISC)
1715                 remote_flow->msg.t.tcm_handle = REMOTE_PROMISCUOUS_HANDLE;
1716         else
1717                 tap_flow_set_handle(remote_flow);
1718         if (priv_flow_process(pmd, attr, items, actions, NULL,
1719                               remote_flow, implicit_rte_flows[idx].mirred)) {
1720                 RTE_LOG(ERR, PMD, "rte flow rule validation failed\n");
1721                 goto fail;
1722         }
1723         err = tap_nl_send(pmd->nlsk_fd, &msg->nh);
1724         if (err < 0) {
1725                 RTE_LOG(ERR, PMD, "Failure sending nl request\n");
1726                 goto fail;
1727         }
1728         err = tap_nl_recv_ack(pmd->nlsk_fd);
1729         if (err < 0) {
1730                 /* Silently ignore re-entering remote promiscuous rule */
1731                 if (errno == EEXIST && idx == TAP_REMOTE_PROMISC)
1732                         goto success;
1733                 RTE_LOG(ERR, PMD,
1734                         "Kernel refused TC filter rule creation (%d): %s\n",
1735                         errno, strerror(errno));
1736                 goto fail;
1737         }
1738         LIST_INSERT_HEAD(&pmd->implicit_flows, remote_flow, next);
1739 success:
1740         return 0;
1741 fail:
1742         if (remote_flow)
1743                 rte_free(remote_flow);
1744         return -1;
1745 }
1746
1747 /**
1748  * Remove specific implicit flow rule on the remote device.
1749  *
1750  * @param[in, out] pmd
1751  *   Pointer to private structure.
1752  * @param[in] idx
1753  *   The idx in the implicit_rte_flows array specifying which rule to remove.
1754  *
1755  * @return -1 if one of the implicit rules couldn't be created, 0 otherwise.
1756  */
1757 int tap_flow_implicit_destroy(struct pmd_internals *pmd,
1758                               enum implicit_rule_index idx)
1759 {
1760         struct rte_flow *remote_flow;
1761         int cur_prio = -1;
1762         int idx_prio = implicit_rte_flows[idx].attr.priority + PRIORITY_OFFSET;
1763
1764         for (remote_flow = LIST_FIRST(&pmd->implicit_flows);
1765              remote_flow;
1766              remote_flow = LIST_NEXT(remote_flow, next)) {
1767                 cur_prio = (remote_flow->msg.t.tcm_info >> 16) & PRIORITY_MASK;
1768                 if (cur_prio != idx_prio)
1769                         continue;
1770                 return tap_flow_destroy_pmd(pmd, remote_flow, NULL);
1771         }
1772         return 0;
1773 }
1774
1775 /**
1776  * Destroy all implicit flows.
1777  *
1778  * @see rte_flow_flush()
1779  */
1780 int
1781 tap_flow_implicit_flush(struct pmd_internals *pmd, struct rte_flow_error *error)
1782 {
1783         struct rte_flow *remote_flow;
1784
1785         while (!LIST_EMPTY(&pmd->implicit_flows)) {
1786                 remote_flow = LIST_FIRST(&pmd->implicit_flows);
1787                 if (tap_flow_destroy_pmd(pmd, remote_flow, error) < 0)
1788                         return -1;
1789         }
1790         return 0;
1791 }
1792
1793 #define MAX_RSS_KEYS 256
1794 #define KEY_IDX_OFFSET (3 * MAX_RSS_KEYS)
1795 #define SEC_NAME_CLS_Q "cls_q"
1796
1797 const char *sec_name[SEC_MAX] = {
1798         [SEC_L3_L4] = "l3_l4",
1799 };
1800
1801 /**
1802  * Enable RSS on tap: create TC rules for queuing.
1803  *
1804  * @param[in, out] pmd
1805  *   Pointer to private structure.
1806  *
1807  * @param[in] attr
1808  *   Pointer to rte_flow to get flow group
1809  *
1810  * @param[out] error
1811  *   Pointer to error reporting if not NULL.
1812  *
1813  * @return 0 on success, negative value on failure.
1814  */
1815 static int rss_enable(struct pmd_internals *pmd,
1816                         const struct rte_flow_attr *attr,
1817                         struct rte_flow_error *error)
1818 {
1819         struct rte_flow *rss_flow = NULL;
1820         struct nlmsg *msg = NULL;
1821         /* 4096 is the maximum number of instructions for a BPF program */
1822         char annotation[64];
1823         int i;
1824         int err = 0;
1825
1826         /* unlimit locked memory */
1827         struct rlimit memlock_limit = {
1828                 .rlim_cur = RLIM_INFINITY,
1829                 .rlim_max = RLIM_INFINITY,
1830         };
1831         setrlimit(RLIMIT_MEMLOCK, &memlock_limit);
1832
1833          /* Get a new map key for a new RSS rule */
1834         err = bpf_rss_key(KEY_CMD_INIT, NULL);
1835         if (err < 0) {
1836                 rte_flow_error_set(
1837                         error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1838                         "Failed to initialize BPF RSS keys");
1839
1840                 return -1;
1841         }
1842
1843         /*
1844          *  Create BPF RSS MAP
1845          */
1846         pmd->map_fd = tap_flow_bpf_rss_map_create(sizeof(__u32), /* key size */
1847                                 sizeof(struct rss_key),
1848                                 MAX_RSS_KEYS);
1849         if (pmd->map_fd < 0) {
1850                 RTE_LOG(ERR, PMD,
1851                         "Failed to create BPF map (%d): %s\n",
1852                                 errno, strerror(errno));
1853                 rte_flow_error_set(
1854                         error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1855                         "Kernel too old or not configured "
1856                         "to support BPF maps");
1857
1858                 return -ENOTSUP;
1859         }
1860
1861         /*
1862          * Add a rule per queue to match reclassified packets and direct them to
1863          * the correct queue.
1864          */
1865         for (i = 0; i < pmd->dev->data->nb_rx_queues; i++) {
1866                 pmd->bpf_fd[i] = tap_flow_bpf_cls_q(i);
1867                 if (pmd->bpf_fd[i] < 0) {
1868                         RTE_LOG(ERR, PMD,
1869                                 "Failed to load BPF section %s for queue %d",
1870                                 SEC_NAME_CLS_Q, i);
1871                         rte_flow_error_set(
1872                                 error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE,
1873                                 NULL,
1874                                 "Kernel too old or not configured "
1875                                 "to support BPF programs loading");
1876
1877                         return -ENOTSUP;
1878                 }
1879
1880                 rss_flow = rte_malloc(__func__, sizeof(struct rte_flow), 0);
1881                 if (!rss_flow) {
1882                         RTE_LOG(ERR, PMD,
1883                                 "Cannot allocate memory for rte_flow");
1884                         return -1;
1885                 }
1886                 msg = &rss_flow->msg;
1887                 tc_init_msg(msg, pmd->if_index, RTM_NEWTFILTER, NLM_F_REQUEST |
1888                             NLM_F_ACK | NLM_F_EXCL | NLM_F_CREATE);
1889                 msg->t.tcm_info = TC_H_MAKE(0, htons(ETH_P_ALL));
1890                 tap_flow_set_handle(rss_flow);
1891                 uint16_t group = attr->group << GROUP_SHIFT;
1892                 uint16_t prio = group | (i + PRIORITY_OFFSET);
1893                 msg->t.tcm_info = TC_H_MAKE(prio << 16, msg->t.tcm_info);
1894                 msg->t.tcm_parent = TC_H_MAKE(MULTIQ_MAJOR_HANDLE, 0);
1895
1896                 tap_nlattr_add(&msg->nh, TCA_KIND, sizeof("bpf"), "bpf");
1897                 if (tap_nlattr_nested_start(msg, TCA_OPTIONS) < 0)
1898                         return -1;
1899                 tap_nlattr_add32(&msg->nh, TCA_BPF_FD, pmd->bpf_fd[i]);
1900                 snprintf(annotation, sizeof(annotation), "[%s%d]",
1901                         SEC_NAME_CLS_Q, i);
1902                 tap_nlattr_add(&msg->nh, TCA_BPF_NAME, strlen(annotation) + 1,
1903                            annotation);
1904                 /* Actions */
1905                 {
1906                         struct action_data adata = {
1907                                 .id = "skbedit",
1908                                 .skbedit = {
1909                                         .skbedit = {
1910                                                 .action = TC_ACT_PIPE,
1911                                         },
1912                                         .queue = i,
1913                                 },
1914                         };
1915                         if (add_actions(rss_flow, 1, &adata, TCA_BPF_ACT) < 0)
1916                                 return -1;
1917                 }
1918                 tap_nlattr_nested_finish(msg); /* nested TCA_OPTIONS */
1919
1920                 /* Netlink message is now ready to be sent */
1921                 if (tap_nl_send(pmd->nlsk_fd, &msg->nh) < 0)
1922                         return -1;
1923                 err = tap_nl_recv_ack(pmd->nlsk_fd);
1924                 if (err < 0) {
1925                         RTE_LOG(ERR, PMD,
1926                                 "Kernel refused TC filter rule creation (%d): %s\n",
1927                                 errno, strerror(errno));
1928                         return err;
1929                 }
1930                 LIST_INSERT_HEAD(&pmd->rss_flows, rss_flow, next);
1931         }
1932
1933         pmd->rss_enabled = 1;
1934         return err;
1935 }
1936
1937 /**
1938  * Manage bpf RSS keys repository with operations: init, get, release
1939  *
1940  * @param[in] cmd
1941  *   Command on RSS keys: init, get, release
1942  *
1943  * @param[in, out] key_idx
1944  *   Pointer to RSS Key index (out for get command, in for release command)
1945  *
1946  * @return -1 if couldn't get, release or init the RSS keys, 0 otherwise.
1947  */
1948 static int bpf_rss_key(enum bpf_rss_key_e cmd, __u32 *key_idx)
1949 {
1950         __u32 i;
1951         int err = 0;
1952         static __u32 num_used_keys;
1953         static __u32 rss_keys[MAX_RSS_KEYS] = {KEY_STAT_UNSPEC};
1954         static __u32 rss_keys_initialized;
1955         __u32 key;
1956
1957         switch (cmd) {
1958         case KEY_CMD_GET:
1959                 if (!rss_keys_initialized) {
1960                         err = -1;
1961                         break;
1962                 }
1963
1964                 if (num_used_keys == RTE_DIM(rss_keys)) {
1965                         err = -1;
1966                         break;
1967                 }
1968
1969                 *key_idx = num_used_keys % RTE_DIM(rss_keys);
1970                 while (rss_keys[*key_idx] == KEY_STAT_USED)
1971                         *key_idx = (*key_idx + 1) % RTE_DIM(rss_keys);
1972
1973                 rss_keys[*key_idx] = KEY_STAT_USED;
1974
1975                 /*
1976                  * Add an offset to key_idx in order to handle a case of
1977                  * RSS and non RSS flows mixture.
1978                  * If a non RSS flow is destroyed it has an eBPF map
1979                  * index 0 (initialized on flow creation) and might
1980                  * unintentionally remove RSS entry 0 from eBPF map.
1981                  * To avoid this issue, add an offset to the real index
1982                  * during a KEY_CMD_GET operation and subtract this offset
1983                  * during a KEY_CMD_RELEASE operation in order to restore
1984                  * the real index.
1985                  */
1986                 *key_idx += KEY_IDX_OFFSET;
1987                 num_used_keys++;
1988         break;
1989
1990         case KEY_CMD_RELEASE:
1991                 if (!rss_keys_initialized)
1992                         break;
1993
1994                 /*
1995                  * Subtract offest to restore real key index
1996                  * If a non RSS flow is falsely trying to release map
1997                  * entry 0 - the offset subtraction will calculate the real
1998                  * map index as an out-of-range value and the release operation
1999                  * will be silently ignored.
2000                  */
2001                 key = *key_idx - KEY_IDX_OFFSET;
2002                 if (key >= RTE_DIM(rss_keys))
2003                         break;
2004
2005                 if (rss_keys[key] == KEY_STAT_USED) {
2006                         rss_keys[key] = KEY_STAT_AVAILABLE;
2007                         num_used_keys--;
2008                 }
2009         break;
2010
2011         case KEY_CMD_INIT:
2012                 for (i = 0; i < RTE_DIM(rss_keys); i++)
2013                         rss_keys[i] = KEY_STAT_AVAILABLE;
2014
2015                 rss_keys_initialized = 1;
2016                 num_used_keys = 0;
2017         break;
2018
2019         case KEY_CMD_DEINIT:
2020                 for (i = 0; i < RTE_DIM(rss_keys); i++)
2021                         rss_keys[i] = KEY_STAT_UNSPEC;
2022
2023                 rss_keys_initialized = 0;
2024                 num_used_keys = 0;
2025         break;
2026
2027         default:
2028                 break;
2029         }
2030
2031         return err;
2032 }
2033
2034 /**
2035  * Add RSS hash calculations and queue selection
2036  *
2037  * @param[in, out] pmd
2038  *   Pointer to internal structure. Used to set/get RSS map fd
2039  *
2040  * @param[in] rss
2041  *   Pointer to RSS flow actions
2042  *
2043  * @param[out] error
2044  *   Pointer to error reporting if not NULL.
2045  *
2046  * @return 0 on success, negative value on failure
2047  */
2048 static int rss_add_actions(struct rte_flow *flow, struct pmd_internals *pmd,
2049                            const struct rte_flow_action_rss *rss,
2050                            struct rte_flow_error *error)
2051 {
2052         /* 4096 is the maximum number of instructions for a BPF program */
2053         int i;
2054         int err;
2055         struct rss_key rss_entry = { .hash_fields = 0,
2056                                      .key_size = 0 };
2057
2058         /* Get a new map key for a new RSS rule */
2059         err = bpf_rss_key(KEY_CMD_GET, &flow->key_idx);
2060         if (err < 0) {
2061                 rte_flow_error_set(
2062                         error, EINVAL, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2063                         "Failed to get BPF RSS key");
2064
2065                 return -1;
2066         }
2067
2068         /* Update RSS map entry with queues */
2069         rss_entry.nb_queues = rss->num;
2070         for (i = 0; i < rss->num; i++)
2071                 rss_entry.queues[i] = rss->queue[i];
2072         rss_entry.hash_fields =
2073                 (1 << HASH_FIELD_IPV4_L3_L4) | (1 << HASH_FIELD_IPV6_L3_L4);
2074
2075         /* Add this RSS entry to map */
2076         err = tap_flow_bpf_update_rss_elem(pmd->map_fd,
2077                                 &flow->key_idx, &rss_entry);
2078
2079         if (err) {
2080                 RTE_LOG(ERR, PMD,
2081                         "Failed to update BPF map entry #%u (%d): %s\n",
2082                         flow->key_idx, errno, strerror(errno));
2083                 rte_flow_error_set(
2084                         error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2085                         "Kernel too old or not configured "
2086                         "to support BPF maps updates");
2087
2088                 return -ENOTSUP;
2089         }
2090
2091
2092         /*
2093          * Load bpf rules to calculate hash for this key_idx
2094          */
2095
2096         flow->bpf_fd[SEC_L3_L4] =
2097                 tap_flow_bpf_calc_l3_l4_hash(flow->key_idx, pmd->map_fd);
2098         if (flow->bpf_fd[SEC_L3_L4] < 0) {
2099                 RTE_LOG(ERR, PMD,
2100                         "Failed to load BPF section %s (%d): %s\n",
2101                                 sec_name[SEC_L3_L4], errno, strerror(errno));
2102                 rte_flow_error_set(
2103                         error, ENOTSUP, RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2104                         "Kernel too old or not configured "
2105                         "to support BPF program loading");
2106
2107                 return -ENOTSUP;
2108         }
2109
2110         /* Actions */
2111         {
2112                 struct action_data adata[] = {
2113                         {
2114                                 .id = "bpf",
2115                                 .bpf = {
2116                                         .bpf_fd = flow->bpf_fd[SEC_L3_L4],
2117                                         .annotation = sec_name[SEC_L3_L4],
2118                                         .bpf = {
2119                                                 .action = TC_ACT_PIPE,
2120                                         },
2121                                 },
2122                         },
2123                 };
2124
2125                 if (add_actions(flow, RTE_DIM(adata), adata,
2126                         TCA_FLOWER_ACT) < 0)
2127                         return -1;
2128         }
2129
2130         return 0;
2131 }
2132
2133 /**
2134  * Manage filter operations.
2135  *
2136  * @param dev
2137  *   Pointer to Ethernet device structure.
2138  * @param filter_type
2139  *   Filter type.
2140  * @param filter_op
2141  *   Operation to perform.
2142  * @param arg
2143  *   Pointer to operation-specific structure.
2144  *
2145  * @return
2146  *   0 on success, negative errno value on failure.
2147  */
2148 int
2149 tap_dev_filter_ctrl(struct rte_eth_dev *dev,
2150                     enum rte_filter_type filter_type,
2151                     enum rte_filter_op filter_op,
2152                     void *arg)
2153 {
2154         switch (filter_type) {
2155         case RTE_ETH_FILTER_GENERIC:
2156                 if (filter_op != RTE_ETH_FILTER_GET)
2157                         return -EINVAL;
2158                 *(const void **)arg = &tap_flow_ops;
2159                 return 0;
2160         default:
2161                 RTE_LOG(ERR, PMD, "%p: filter type (%d) not supported\n",
2162                         (void *)dev, filter_type);
2163         }
2164         return -EINVAL;
2165 }
2166