3f754b4800e49644b2300e94153d410a1874ee9f
[dpdk.git] / drivers / net / mlx4 / mlx4_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox Technologies, Ltd
4  */
5
6 /**
7  * @file
8  * Flow API operations for mlx4 driver.
9  */
10
11 #include <arpa/inet.h>
12 #include <assert.h>
13 #include <errno.h>
14 #include <stdalign.h>
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <string.h>
18 #include <sys/queue.h>
19
20 /* Verbs headers do not support -pedantic. */
21 #ifdef PEDANTIC
22 #pragma GCC diagnostic ignored "-Wpedantic"
23 #endif
24 #include <infiniband/verbs.h>
25 #ifdef PEDANTIC
26 #pragma GCC diagnostic error "-Wpedantic"
27 #endif
28
29 #include <rte_byteorder.h>
30 #include <rte_errno.h>
31 #include <rte_eth_ctrl.h>
32 #include <rte_ethdev_driver.h>
33 #include <rte_ether.h>
34 #include <rte_flow.h>
35 #include <rte_flow_driver.h>
36 #include <rte_malloc.h>
37
38 /* PMD headers. */
39 #include "mlx4.h"
40 #include "mlx4_glue.h"
41 #include "mlx4_flow.h"
42 #include "mlx4_rxtx.h"
43 #include "mlx4_utils.h"
44
45 /** Static initializer for a list of subsequent item types. */
46 #define NEXT_ITEM(...) \
47         (const enum rte_flow_item_type []){ \
48                 __VA_ARGS__, RTE_FLOW_ITEM_TYPE_END, \
49         }
50
51 /** Processor structure associated with a flow item. */
52 struct mlx4_flow_proc_item {
53         /** Bit-mask for fields supported by this PMD. */
54         const void *mask_support;
55         /** Bit-mask to use when @p item->mask is not provided. */
56         const void *mask_default;
57         /** Size in bytes for @p mask_support and @p mask_default. */
58         const unsigned int mask_sz;
59         /** Merge a pattern item into a flow rule handle. */
60         int (*merge)(struct rte_flow *flow,
61                      const struct rte_flow_item *item,
62                      const struct mlx4_flow_proc_item *proc,
63                      struct rte_flow_error *error);
64         /** Size in bytes of the destination structure. */
65         const unsigned int dst_sz;
66         /** List of possible subsequent items. */
67         const enum rte_flow_item_type *const next_item;
68 };
69
70 /** Shared resources for drop flow rules. */
71 struct mlx4_drop {
72         struct ibv_qp *qp; /**< QP target. */
73         struct ibv_cq *cq; /**< CQ associated with above QP. */
74         struct priv *priv; /**< Back pointer to private data. */
75         uint32_t refcnt; /**< Reference count. */
76 };
77
78 /**
79  * Convert DPDK RSS hash types to their Verbs equivalent.
80  *
81  * This function returns the supported (default) set when @p types has
82  * special value (uint64_t)-1.
83  *
84  * @param priv
85  *   Pointer to private structure.
86  * @param types
87  *   Hash types in DPDK format (see struct rte_eth_rss_conf).
88  *
89  * @return
90  *   A valid Verbs RSS hash fields mask for mlx4 on success, (uint64_t)-1
91  *   otherwise and rte_errno is set.
92  */
93 uint64_t
94 mlx4_conv_rss_types(struct priv *priv, uint64_t types)
95 {
96         enum { IPV4, IPV6, TCP, UDP, };
97         const uint64_t in[] = {
98                 [IPV4] = (ETH_RSS_IPV4 |
99                           ETH_RSS_FRAG_IPV4 |
100                           ETH_RSS_NONFRAG_IPV4_TCP |
101                           ETH_RSS_NONFRAG_IPV4_UDP |
102                           ETH_RSS_NONFRAG_IPV4_OTHER),
103                 [IPV6] = (ETH_RSS_IPV6 |
104                           ETH_RSS_FRAG_IPV6 |
105                           ETH_RSS_NONFRAG_IPV6_TCP |
106                           ETH_RSS_NONFRAG_IPV6_UDP |
107                           ETH_RSS_NONFRAG_IPV6_OTHER |
108                           ETH_RSS_IPV6_EX |
109                           ETH_RSS_IPV6_TCP_EX |
110                           ETH_RSS_IPV6_UDP_EX),
111                 [TCP] = (ETH_RSS_NONFRAG_IPV4_TCP |
112                          ETH_RSS_NONFRAG_IPV6_TCP |
113                          ETH_RSS_IPV6_TCP_EX),
114                 [UDP] = (ETH_RSS_NONFRAG_IPV4_UDP |
115                          ETH_RSS_NONFRAG_IPV6_UDP |
116                          ETH_RSS_IPV6_UDP_EX),
117         };
118         const uint64_t out[RTE_DIM(in)] = {
119                 [IPV4] = IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4,
120                 [IPV6] = IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6,
121                 [TCP] = IBV_RX_HASH_SRC_PORT_TCP | IBV_RX_HASH_DST_PORT_TCP,
122                 [UDP] = IBV_RX_HASH_SRC_PORT_UDP | IBV_RX_HASH_DST_PORT_UDP,
123         };
124         uint64_t seen = 0;
125         uint64_t conv = 0;
126         unsigned int i;
127
128         if (types == (uint64_t)-1)
129                 return priv->hw_rss_sup;
130         for (i = 0; i != RTE_DIM(in); ++i)
131                 if (types & in[i]) {
132                         seen |= types & in[i];
133                         conv |= out[i];
134                 }
135         if ((conv & priv->hw_rss_sup) == conv && !(types & ~seen))
136                 return conv;
137         rte_errno = ENOTSUP;
138         return (uint64_t)-1;
139 }
140
141 /**
142  * Merge Ethernet pattern item into flow rule handle.
143  *
144  * Additional mlx4-specific constraints on supported fields:
145  *
146  * - No support for partial masks, except in the specific case of matching
147  *   all multicast traffic (@p spec->dst and @p mask->dst equal to
148  *   01:00:00:00:00:00).
149  * - Not providing @p item->spec or providing an empty @p mask->dst is
150  *   *only* supported if the rule doesn't specify additional matching
151  *   criteria (i.e. rule is promiscuous-like).
152  *
153  * @param[in, out] flow
154  *   Flow rule handle to update.
155  * @param[in] item
156  *   Pattern item to merge.
157  * @param[in] proc
158  *   Associated item-processing object.
159  * @param[out] error
160  *   Perform verbose error reporting if not NULL.
161  *
162  * @return
163  *   0 on success, a negative errno value otherwise and rte_errno is set.
164  */
165 static int
166 mlx4_flow_merge_eth(struct rte_flow *flow,
167                     const struct rte_flow_item *item,
168                     const struct mlx4_flow_proc_item *proc,
169                     struct rte_flow_error *error)
170 {
171         const struct rte_flow_item_eth *spec = item->spec;
172         const struct rte_flow_item_eth *mask =
173                 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
174         struct ibv_flow_spec_eth *eth;
175         const char *msg;
176         unsigned int i;
177
178         if (!mask) {
179                 flow->promisc = 1;
180         } else {
181                 uint32_t sum_dst = 0;
182                 uint32_t sum_src = 0;
183
184                 for (i = 0; i != sizeof(mask->dst.addr_bytes); ++i) {
185                         sum_dst += mask->dst.addr_bytes[i];
186                         sum_src += mask->src.addr_bytes[i];
187                 }
188                 if (sum_src) {
189                         msg = "mlx4 does not support source MAC matching";
190                         goto error;
191                 } else if (!sum_dst) {
192                         flow->promisc = 1;
193                 } else if (sum_dst == 1 && mask->dst.addr_bytes[0] == 1) {
194                         if (!(spec->dst.addr_bytes[0] & 1)) {
195                                 msg = "mlx4 does not support the explicit"
196                                         " exclusion of all multicast traffic";
197                                 goto error;
198                         }
199                         flow->allmulti = 1;
200                 } else if (sum_dst != (UINT8_C(0xff) * ETHER_ADDR_LEN)) {
201                         msg = "mlx4 does not support matching partial"
202                                 " Ethernet fields";
203                         goto error;
204                 }
205         }
206         if (!flow->ibv_attr)
207                 return 0;
208         if (flow->promisc) {
209                 flow->ibv_attr->type = IBV_FLOW_ATTR_ALL_DEFAULT;
210                 return 0;
211         }
212         if (flow->allmulti) {
213                 flow->ibv_attr->type = IBV_FLOW_ATTR_MC_DEFAULT;
214                 return 0;
215         }
216         ++flow->ibv_attr->num_of_specs;
217         eth = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
218         *eth = (struct ibv_flow_spec_eth) {
219                 .type = IBV_FLOW_SPEC_ETH,
220                 .size = sizeof(*eth),
221         };
222         memcpy(eth->val.dst_mac, spec->dst.addr_bytes, ETHER_ADDR_LEN);
223         memcpy(eth->mask.dst_mac, mask->dst.addr_bytes, ETHER_ADDR_LEN);
224         /* Remove unwanted bits from values. */
225         for (i = 0; i < ETHER_ADDR_LEN; ++i) {
226                 eth->val.dst_mac[i] &= eth->mask.dst_mac[i];
227         }
228         return 0;
229 error:
230         return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
231                                   item, msg);
232 }
233
234 /**
235  * Merge VLAN pattern item into flow rule handle.
236  *
237  * Additional mlx4-specific constraints on supported fields:
238  *
239  * - Matching *all* VLAN traffic by omitting @p item->spec or providing an
240  *   empty @p item->mask would also include non-VLAN traffic. Doing so is
241  *   therefore unsupported.
242  * - No support for partial masks.
243  *
244  * @param[in, out] flow
245  *   Flow rule handle to update.
246  * @param[in] item
247  *   Pattern item to merge.
248  * @param[in] proc
249  *   Associated item-processing object.
250  * @param[out] error
251  *   Perform verbose error reporting if not NULL.
252  *
253  * @return
254  *   0 on success, a negative errno value otherwise and rte_errno is set.
255  */
256 static int
257 mlx4_flow_merge_vlan(struct rte_flow *flow,
258                      const struct rte_flow_item *item,
259                      const struct mlx4_flow_proc_item *proc,
260                      struct rte_flow_error *error)
261 {
262         const struct rte_flow_item_vlan *spec = item->spec;
263         const struct rte_flow_item_vlan *mask =
264                 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
265         struct ibv_flow_spec_eth *eth;
266         const char *msg;
267
268         if (!mask || !mask->tci) {
269                 msg = "mlx4 cannot match all VLAN traffic while excluding"
270                         " non-VLAN traffic, TCI VID must be specified";
271                 goto error;
272         }
273         if (mask->tci != RTE_BE16(0x0fff)) {
274                 msg = "mlx4 does not support partial TCI VID matching";
275                 goto error;
276         }
277         if (!flow->ibv_attr)
278                 return 0;
279         eth = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size -
280                        sizeof(*eth));
281         eth->val.vlan_tag = spec->tci;
282         eth->mask.vlan_tag = mask->tci;
283         eth->val.vlan_tag &= eth->mask.vlan_tag;
284         return 0;
285 error:
286         return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
287                                   item, msg);
288 }
289
290 /**
291  * Merge IPv4 pattern item into flow rule handle.
292  *
293  * Additional mlx4-specific constraints on supported fields:
294  *
295  * - No support for partial masks.
296  *
297  * @param[in, out] flow
298  *   Flow rule handle to update.
299  * @param[in] item
300  *   Pattern item to merge.
301  * @param[in] proc
302  *   Associated item-processing object.
303  * @param[out] error
304  *   Perform verbose error reporting if not NULL.
305  *
306  * @return
307  *   0 on success, a negative errno value otherwise and rte_errno is set.
308  */
309 static int
310 mlx4_flow_merge_ipv4(struct rte_flow *flow,
311                      const struct rte_flow_item *item,
312                      const struct mlx4_flow_proc_item *proc,
313                      struct rte_flow_error *error)
314 {
315         const struct rte_flow_item_ipv4 *spec = item->spec;
316         const struct rte_flow_item_ipv4 *mask =
317                 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
318         struct ibv_flow_spec_ipv4 *ipv4;
319         const char *msg;
320
321         if (mask &&
322             ((uint32_t)(mask->hdr.src_addr + 1) > UINT32_C(1) ||
323              (uint32_t)(mask->hdr.dst_addr + 1) > UINT32_C(1))) {
324                 msg = "mlx4 does not support matching partial IPv4 fields";
325                 goto error;
326         }
327         if (!flow->ibv_attr)
328                 return 0;
329         ++flow->ibv_attr->num_of_specs;
330         ipv4 = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
331         *ipv4 = (struct ibv_flow_spec_ipv4) {
332                 .type = IBV_FLOW_SPEC_IPV4,
333                 .size = sizeof(*ipv4),
334         };
335         if (!spec)
336                 return 0;
337         ipv4->val = (struct ibv_flow_ipv4_filter) {
338                 .src_ip = spec->hdr.src_addr,
339                 .dst_ip = spec->hdr.dst_addr,
340         };
341         ipv4->mask = (struct ibv_flow_ipv4_filter) {
342                 .src_ip = mask->hdr.src_addr,
343                 .dst_ip = mask->hdr.dst_addr,
344         };
345         /* Remove unwanted bits from values. */
346         ipv4->val.src_ip &= ipv4->mask.src_ip;
347         ipv4->val.dst_ip &= ipv4->mask.dst_ip;
348         return 0;
349 error:
350         return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
351                                   item, msg);
352 }
353
354 /**
355  * Merge UDP pattern item into flow rule handle.
356  *
357  * Additional mlx4-specific constraints on supported fields:
358  *
359  * - No support for partial masks.
360  * - Due to HW/FW limitation, flow rule priority is not taken into account
361  *   when matching UDP destination ports, doing is therefore only supported
362  *   at the highest priority level (0).
363  *
364  * @param[in, out] flow
365  *   Flow rule handle to update.
366  * @param[in] item
367  *   Pattern item to merge.
368  * @param[in] proc
369  *   Associated item-processing object.
370  * @param[out] error
371  *   Perform verbose error reporting if not NULL.
372  *
373  * @return
374  *   0 on success, a negative errno value otherwise and rte_errno is set.
375  */
376 static int
377 mlx4_flow_merge_udp(struct rte_flow *flow,
378                     const struct rte_flow_item *item,
379                     const struct mlx4_flow_proc_item *proc,
380                     struct rte_flow_error *error)
381 {
382         const struct rte_flow_item_udp *spec = item->spec;
383         const struct rte_flow_item_udp *mask =
384                 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
385         struct ibv_flow_spec_tcp_udp *udp;
386         const char *msg;
387
388         if (mask &&
389             ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
390              (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
391                 msg = "mlx4 does not support matching partial UDP fields";
392                 goto error;
393         }
394         if (mask && mask->hdr.dst_port && flow->priority) {
395                 msg = "combining UDP destination port matching with a nonzero"
396                         " priority level is not supported";
397                 goto error;
398         }
399         if (!flow->ibv_attr)
400                 return 0;
401         ++flow->ibv_attr->num_of_specs;
402         udp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
403         *udp = (struct ibv_flow_spec_tcp_udp) {
404                 .type = IBV_FLOW_SPEC_UDP,
405                 .size = sizeof(*udp),
406         };
407         if (!spec)
408                 return 0;
409         udp->val.dst_port = spec->hdr.dst_port;
410         udp->val.src_port = spec->hdr.src_port;
411         udp->mask.dst_port = mask->hdr.dst_port;
412         udp->mask.src_port = mask->hdr.src_port;
413         /* Remove unwanted bits from values. */
414         udp->val.src_port &= udp->mask.src_port;
415         udp->val.dst_port &= udp->mask.dst_port;
416         return 0;
417 error:
418         return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
419                                   item, msg);
420 }
421
422 /**
423  * Merge TCP pattern item into flow rule handle.
424  *
425  * Additional mlx4-specific constraints on supported fields:
426  *
427  * - No support for partial masks.
428  *
429  * @param[in, out] flow
430  *   Flow rule handle to update.
431  * @param[in] item
432  *   Pattern item to merge.
433  * @param[in] proc
434  *   Associated item-processing object.
435  * @param[out] error
436  *   Perform verbose error reporting if not NULL.
437  *
438  * @return
439  *   0 on success, a negative errno value otherwise and rte_errno is set.
440  */
441 static int
442 mlx4_flow_merge_tcp(struct rte_flow *flow,
443                     const struct rte_flow_item *item,
444                     const struct mlx4_flow_proc_item *proc,
445                     struct rte_flow_error *error)
446 {
447         const struct rte_flow_item_tcp *spec = item->spec;
448         const struct rte_flow_item_tcp *mask =
449                 spec ? (item->mask ? item->mask : proc->mask_default) : NULL;
450         struct ibv_flow_spec_tcp_udp *tcp;
451         const char *msg;
452
453         if (mask &&
454             ((uint16_t)(mask->hdr.src_port + 1) > UINT16_C(1) ||
455              (uint16_t)(mask->hdr.dst_port + 1) > UINT16_C(1))) {
456                 msg = "mlx4 does not support matching partial TCP fields";
457                 goto error;
458         }
459         if (!flow->ibv_attr)
460                 return 0;
461         ++flow->ibv_attr->num_of_specs;
462         tcp = (void *)((uintptr_t)flow->ibv_attr + flow->ibv_attr_size);
463         *tcp = (struct ibv_flow_spec_tcp_udp) {
464                 .type = IBV_FLOW_SPEC_TCP,
465                 .size = sizeof(*tcp),
466         };
467         if (!spec)
468                 return 0;
469         tcp->val.dst_port = spec->hdr.dst_port;
470         tcp->val.src_port = spec->hdr.src_port;
471         tcp->mask.dst_port = mask->hdr.dst_port;
472         tcp->mask.src_port = mask->hdr.src_port;
473         /* Remove unwanted bits from values. */
474         tcp->val.src_port &= tcp->mask.src_port;
475         tcp->val.dst_port &= tcp->mask.dst_port;
476         return 0;
477 error:
478         return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
479                                   item, msg);
480 }
481
482 /**
483  * Perform basic sanity checks on a pattern item.
484  *
485  * @param[in] item
486  *   Item specification.
487  * @param[in] proc
488  *   Associated item-processing object.
489  * @param[out] error
490  *   Perform verbose error reporting if not NULL.
491  *
492  * @return
493  *   0 on success, a negative errno value otherwise and rte_errno is set.
494  */
495 static int
496 mlx4_flow_item_check(const struct rte_flow_item *item,
497                      const struct mlx4_flow_proc_item *proc,
498                      struct rte_flow_error *error)
499 {
500         const uint8_t *mask;
501         unsigned int i;
502
503         /* item->last and item->mask cannot exist without item->spec. */
504         if (!item->spec && (item->mask || item->last))
505                 return rte_flow_error_set
506                         (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
507                          "\"mask\" or \"last\" field provided without a"
508                          " corresponding \"spec\"");
509         /* No spec, no mask, no problem. */
510         if (!item->spec)
511                 return 0;
512         mask = item->mask ?
513                 (const uint8_t *)item->mask :
514                 (const uint8_t *)proc->mask_default;
515         assert(mask);
516         /*
517          * Single-pass check to make sure that:
518          * - Mask is supported, no bits are set outside proc->mask_support.
519          * - Both item->spec and item->last are included in mask.
520          */
521         for (i = 0; i != proc->mask_sz; ++i) {
522                 if (!mask[i])
523                         continue;
524                 if ((mask[i] | ((const uint8_t *)proc->mask_support)[i]) !=
525                     ((const uint8_t *)proc->mask_support)[i])
526                         return rte_flow_error_set
527                                 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
528                                  item, "unsupported field found in \"mask\"");
529                 if (item->last &&
530                     (((const uint8_t *)item->spec)[i] & mask[i]) !=
531                     (((const uint8_t *)item->last)[i] & mask[i]))
532                         return rte_flow_error_set
533                                 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
534                                  item,
535                                  "range between \"spec\" and \"last\""
536                                  " is larger than \"mask\"");
537         }
538         return 0;
539 }
540
541 /** Graph of supported items and associated actions. */
542 static const struct mlx4_flow_proc_item mlx4_flow_proc_item_list[] = {
543         [RTE_FLOW_ITEM_TYPE_END] = {
544                 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_ETH),
545         },
546         [RTE_FLOW_ITEM_TYPE_ETH] = {
547                 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_VLAN,
548                                        RTE_FLOW_ITEM_TYPE_IPV4),
549                 .mask_support = &(const struct rte_flow_item_eth){
550                         /* Only destination MAC can be matched. */
551                         .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
552                 },
553                 .mask_default = &rte_flow_item_eth_mask,
554                 .mask_sz = sizeof(struct rte_flow_item_eth),
555                 .merge = mlx4_flow_merge_eth,
556                 .dst_sz = sizeof(struct ibv_flow_spec_eth),
557         },
558         [RTE_FLOW_ITEM_TYPE_VLAN] = {
559                 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_IPV4),
560                 .mask_support = &(const struct rte_flow_item_vlan){
561                         /* Only TCI VID matching is supported. */
562                         .tci = RTE_BE16(0x0fff),
563                 },
564                 .mask_default = &rte_flow_item_vlan_mask,
565                 .mask_sz = sizeof(struct rte_flow_item_vlan),
566                 .merge = mlx4_flow_merge_vlan,
567                 .dst_sz = 0,
568         },
569         [RTE_FLOW_ITEM_TYPE_IPV4] = {
570                 .next_item = NEXT_ITEM(RTE_FLOW_ITEM_TYPE_UDP,
571                                        RTE_FLOW_ITEM_TYPE_TCP),
572                 .mask_support = &(const struct rte_flow_item_ipv4){
573                         .hdr = {
574                                 .src_addr = RTE_BE32(0xffffffff),
575                                 .dst_addr = RTE_BE32(0xffffffff),
576                         },
577                 },
578                 .mask_default = &rte_flow_item_ipv4_mask,
579                 .mask_sz = sizeof(struct rte_flow_item_ipv4),
580                 .merge = mlx4_flow_merge_ipv4,
581                 .dst_sz = sizeof(struct ibv_flow_spec_ipv4),
582         },
583         [RTE_FLOW_ITEM_TYPE_UDP] = {
584                 .mask_support = &(const struct rte_flow_item_udp){
585                         .hdr = {
586                                 .src_port = RTE_BE16(0xffff),
587                                 .dst_port = RTE_BE16(0xffff),
588                         },
589                 },
590                 .mask_default = &rte_flow_item_udp_mask,
591                 .mask_sz = sizeof(struct rte_flow_item_udp),
592                 .merge = mlx4_flow_merge_udp,
593                 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
594         },
595         [RTE_FLOW_ITEM_TYPE_TCP] = {
596                 .mask_support = &(const struct rte_flow_item_tcp){
597                         .hdr = {
598                                 .src_port = RTE_BE16(0xffff),
599                                 .dst_port = RTE_BE16(0xffff),
600                         },
601                 },
602                 .mask_default = &rte_flow_item_tcp_mask,
603                 .mask_sz = sizeof(struct rte_flow_item_tcp),
604                 .merge = mlx4_flow_merge_tcp,
605                 .dst_sz = sizeof(struct ibv_flow_spec_tcp_udp),
606         },
607 };
608
609 /**
610  * Make sure a flow rule is supported and initialize associated structure.
611  *
612  * @param priv
613  *   Pointer to private structure.
614  * @param[in] attr
615  *   Flow rule attributes.
616  * @param[in] pattern
617  *   Pattern specification (list terminated by the END pattern item).
618  * @param[in] actions
619  *   Associated actions (list terminated by the END action).
620  * @param[out] error
621  *   Perform verbose error reporting if not NULL.
622  * @param[in, out] addr
623  *   Buffer where the resulting flow rule handle pointer must be stored.
624  *   If NULL, stop processing after validation stage.
625  *
626  * @return
627  *   0 on success, a negative errno value otherwise and rte_errno is set.
628  */
629 static int
630 mlx4_flow_prepare(struct priv *priv,
631                   const struct rte_flow_attr *attr,
632                   const struct rte_flow_item pattern[],
633                   const struct rte_flow_action actions[],
634                   struct rte_flow_error *error,
635                   struct rte_flow **addr)
636 {
637         const struct rte_flow_item *item;
638         const struct rte_flow_action *action;
639         const struct mlx4_flow_proc_item *proc;
640         struct rte_flow temp = { .ibv_attr_size = sizeof(*temp.ibv_attr) };
641         struct rte_flow *flow = &temp;
642         const char *msg = NULL;
643         int overlap;
644
645         if (attr->group)
646                 return rte_flow_error_set
647                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
648                          NULL, "groups are not supported");
649         if (attr->priority > MLX4_FLOW_PRIORITY_LAST)
650                 return rte_flow_error_set
651                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY,
652                          NULL, "maximum priority level is "
653                          MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST));
654         if (attr->egress)
655                 return rte_flow_error_set
656                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_EGRESS,
657                          NULL, "egress is not supported");
658         if (attr->transfer)
659                 return rte_flow_error_set
660                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER,
661                          NULL, "transfer is not supported");
662         if (!attr->ingress)
663                 return rte_flow_error_set
664                         (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
665                          NULL, "only ingress is supported");
666 fill:
667         overlap = 0;
668         proc = mlx4_flow_proc_item_list;
669         flow->priority = attr->priority;
670         /* Go over pattern. */
671         for (item = pattern; item->type; ++item) {
672                 const struct mlx4_flow_proc_item *next = NULL;
673                 unsigned int i;
674                 int err;
675
676                 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
677                         continue;
678                 if (item->type == MLX4_FLOW_ITEM_TYPE_INTERNAL) {
679                         flow->internal = 1;
680                         continue;
681                 }
682                 if (flow->promisc || flow->allmulti) {
683                         msg = "mlx4 does not support additional matching"
684                                 " criteria combined with indiscriminate"
685                                 " matching on Ethernet headers";
686                         goto exit_item_not_supported;
687                 }
688                 for (i = 0; proc->next_item && proc->next_item[i]; ++i) {
689                         if (proc->next_item[i] == item->type) {
690                                 next = &mlx4_flow_proc_item_list[item->type];
691                                 break;
692                         }
693                 }
694                 if (!next)
695                         goto exit_item_not_supported;
696                 proc = next;
697                 /*
698                  * Perform basic sanity checks only once, while handle is
699                  * not allocated.
700                  */
701                 if (flow == &temp) {
702                         err = mlx4_flow_item_check(item, proc, error);
703                         if (err)
704                                 return err;
705                 }
706                 if (proc->merge) {
707                         err = proc->merge(flow, item, proc, error);
708                         if (err)
709                                 return err;
710                 }
711                 flow->ibv_attr_size += proc->dst_sz;
712         }
713         /* Go over actions list. */
714         for (action = actions; action->type; ++action) {
715                 /* This one may appear anywhere multiple times. */
716                 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
717                         continue;
718                 /* Fate-deciding actions may appear exactly once. */
719                 if (overlap) {
720                         msg = "cannot combine several fate-deciding actions,"
721                                 " choose between DROP, QUEUE or RSS";
722                         goto exit_action_not_supported;
723                 }
724                 overlap = 1;
725                 switch (action->type) {
726                         const struct rte_flow_action_queue *queue;
727                         const struct rte_flow_action_rss *rss;
728                         const uint8_t *rss_key;
729                         uint32_t rss_key_len;
730                         uint64_t fields;
731                         unsigned int i;
732
733                 case RTE_FLOW_ACTION_TYPE_DROP:
734                         flow->drop = 1;
735                         break;
736                 case RTE_FLOW_ACTION_TYPE_QUEUE:
737                         if (flow->rss)
738                                 break;
739                         queue = action->conf;
740                         if (queue->index >= priv->dev->data->nb_rx_queues) {
741                                 msg = "queue target index beyond number of"
742                                         " configured Rx queues";
743                                 goto exit_action_not_supported;
744                         }
745                         flow->rss = mlx4_rss_get
746                                 (priv, 0, mlx4_rss_hash_key_default, 1,
747                                  &queue->index);
748                         if (!flow->rss) {
749                                 msg = "not enough resources for additional"
750                                         " single-queue RSS context";
751                                 goto exit_action_not_supported;
752                         }
753                         break;
754                 case RTE_FLOW_ACTION_TYPE_RSS:
755                         if (flow->rss)
756                                 break;
757                         rss = action->conf;
758                         /* Default RSS configuration if none is provided. */
759                         if (rss->key_len) {
760                                 rss_key = rss->key;
761                                 rss_key_len = rss->key_len;
762                         } else {
763                                 rss_key = mlx4_rss_hash_key_default;
764                                 rss_key_len = MLX4_RSS_HASH_KEY_SIZE;
765                         }
766                         /* Sanity checks. */
767                         for (i = 0; i < rss->queue_num; ++i)
768                                 if (rss->queue[i] >=
769                                     priv->dev->data->nb_rx_queues)
770                                         break;
771                         if (i != rss->queue_num) {
772                                 msg = "queue index target beyond number of"
773                                         " configured Rx queues";
774                                 goto exit_action_not_supported;
775                         }
776                         if (!rte_is_power_of_2(rss->queue_num)) {
777                                 msg = "for RSS, mlx4 requires the number of"
778                                         " queues to be a power of two";
779                                 goto exit_action_not_supported;
780                         }
781                         if (rss_key_len != sizeof(flow->rss->key)) {
782                                 msg = "mlx4 supports exactly one RSS hash key"
783                                         " length: "
784                                         MLX4_STR_EXPAND(MLX4_RSS_HASH_KEY_SIZE);
785                                 goto exit_action_not_supported;
786                         }
787                         for (i = 1; i < rss->queue_num; ++i)
788                                 if (rss->queue[i] - rss->queue[i - 1] != 1)
789                                         break;
790                         if (i != rss->queue_num) {
791                                 msg = "mlx4 requires RSS contexts to use"
792                                         " consecutive queue indices only";
793                                 goto exit_action_not_supported;
794                         }
795                         if (rss->queue[0] % rss->queue_num) {
796                                 msg = "mlx4 requires the first queue of a RSS"
797                                         " context to be aligned on a multiple"
798                                         " of the context size";
799                                 goto exit_action_not_supported;
800                         }
801                         if (rss->func &&
802                             rss->func != RTE_ETH_HASH_FUNCTION_TOEPLITZ) {
803                                 msg = "the only supported RSS hash function"
804                                         " is Toeplitz";
805                                 goto exit_action_not_supported;
806                         }
807                         if (rss->level) {
808                                 msg = "a nonzero RSS encapsulation level is"
809                                         " not supported";
810                                 goto exit_action_not_supported;
811                         }
812                         rte_errno = 0;
813                         fields = mlx4_conv_rss_types(priv, rss->types);
814                         if (fields == (uint64_t)-1 && rte_errno) {
815                                 msg = "unsupported RSS hash type requested";
816                                 goto exit_action_not_supported;
817                         }
818                         flow->rss = mlx4_rss_get
819                                 (priv, fields, rss_key, rss->queue_num,
820                                  rss->queue);
821                         if (!flow->rss) {
822                                 msg = "either invalid parameters or not enough"
823                                         " resources for additional multi-queue"
824                                         " RSS context";
825                                 goto exit_action_not_supported;
826                         }
827                         break;
828                 default:
829                         goto exit_action_not_supported;
830                 }
831         }
832         /* When fate is unknown, drop traffic. */
833         if (!overlap)
834                 flow->drop = 1;
835         /* Validation ends here. */
836         if (!addr) {
837                 if (flow->rss)
838                         mlx4_rss_put(flow->rss);
839                 return 0;
840         }
841         if (flow == &temp) {
842                 /* Allocate proper handle based on collected data. */
843                 const struct mlx4_malloc_vec vec[] = {
844                         {
845                                 .align = alignof(struct rte_flow),
846                                 .size = sizeof(*flow),
847                                 .addr = (void **)&flow,
848                         },
849                         {
850                                 .align = alignof(struct ibv_flow_attr),
851                                 .size = temp.ibv_attr_size,
852                                 .addr = (void **)&temp.ibv_attr,
853                         },
854                 };
855
856                 if (!mlx4_zmallocv(__func__, vec, RTE_DIM(vec))) {
857                         if (temp.rss)
858                                 mlx4_rss_put(temp.rss);
859                         return rte_flow_error_set
860                                 (error, -rte_errno,
861                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
862                                  "flow rule handle allocation failure");
863                 }
864                 /* Most fields will be updated by second pass. */
865                 *flow = (struct rte_flow){
866                         .ibv_attr = temp.ibv_attr,
867                         .ibv_attr_size = sizeof(*flow->ibv_attr),
868                         .rss = temp.rss,
869                 };
870                 *flow->ibv_attr = (struct ibv_flow_attr){
871                         .type = IBV_FLOW_ATTR_NORMAL,
872                         .size = sizeof(*flow->ibv_attr),
873                         .priority = attr->priority,
874                         .port = priv->port,
875                 };
876                 goto fill;
877         }
878         *addr = flow;
879         return 0;
880 exit_item_not_supported:
881         return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
882                                   item, msg ? msg : "item not supported");
883 exit_action_not_supported:
884         return rte_flow_error_set(error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
885                                   action, msg ? msg : "action not supported");
886 }
887
888 /**
889  * Validate a flow supported by the NIC.
890  *
891  * @see rte_flow_validate()
892  * @see rte_flow_ops
893  */
894 static int
895 mlx4_flow_validate(struct rte_eth_dev *dev,
896                    const struct rte_flow_attr *attr,
897                    const struct rte_flow_item pattern[],
898                    const struct rte_flow_action actions[],
899                    struct rte_flow_error *error)
900 {
901         struct priv *priv = dev->data->dev_private;
902
903         return mlx4_flow_prepare(priv, attr, pattern, actions, error, NULL);
904 }
905
906 /**
907  * Get a drop flow rule resources instance.
908  *
909  * @param priv
910  *   Pointer to private structure.
911  *
912  * @return
913  *   Pointer to drop flow resources on success, NULL otherwise and rte_errno
914  *   is set.
915  */
916 static struct mlx4_drop *
917 mlx4_drop_get(struct priv *priv)
918 {
919         struct mlx4_drop *drop = priv->drop;
920
921         if (drop) {
922                 assert(drop->refcnt);
923                 assert(drop->priv == priv);
924                 ++drop->refcnt;
925                 return drop;
926         }
927         drop = rte_malloc(__func__, sizeof(*drop), 0);
928         if (!drop)
929                 goto error;
930         *drop = (struct mlx4_drop){
931                 .priv = priv,
932                 .refcnt = 1,
933         };
934         drop->cq = mlx4_glue->create_cq(priv->ctx, 1, NULL, NULL, 0);
935         if (!drop->cq)
936                 goto error;
937         drop->qp = mlx4_glue->create_qp
938                 (priv->pd,
939                  &(struct ibv_qp_init_attr){
940                         .send_cq = drop->cq,
941                         .recv_cq = drop->cq,
942                         .qp_type = IBV_QPT_RAW_PACKET,
943                  });
944         if (!drop->qp)
945                 goto error;
946         priv->drop = drop;
947         return drop;
948 error:
949         if (drop->qp)
950                 claim_zero(mlx4_glue->destroy_qp(drop->qp));
951         if (drop->cq)
952                 claim_zero(mlx4_glue->destroy_cq(drop->cq));
953         if (drop)
954                 rte_free(drop);
955         rte_errno = ENOMEM;
956         return NULL;
957 }
958
959 /**
960  * Give back a drop flow rule resources instance.
961  *
962  * @param drop
963  *   Pointer to drop flow rule resources.
964  */
965 static void
966 mlx4_drop_put(struct mlx4_drop *drop)
967 {
968         assert(drop->refcnt);
969         if (--drop->refcnt)
970                 return;
971         drop->priv->drop = NULL;
972         claim_zero(mlx4_glue->destroy_qp(drop->qp));
973         claim_zero(mlx4_glue->destroy_cq(drop->cq));
974         rte_free(drop);
975 }
976
977 /**
978  * Toggle a configured flow rule.
979  *
980  * @param priv
981  *   Pointer to private structure.
982  * @param flow
983  *   Flow rule handle to toggle.
984  * @param enable
985  *   Whether associated Verbs flow must be created or removed.
986  * @param[out] error
987  *   Perform verbose error reporting if not NULL.
988  *
989  * @return
990  *   0 on success, a negative errno value otherwise and rte_errno is set.
991  */
992 static int
993 mlx4_flow_toggle(struct priv *priv,
994                  struct rte_flow *flow,
995                  int enable,
996                  struct rte_flow_error *error)
997 {
998         struct ibv_qp *qp = NULL;
999         const char *msg;
1000         int err;
1001
1002         if (!enable) {
1003                 if (!flow->ibv_flow)
1004                         return 0;
1005                 claim_zero(mlx4_glue->destroy_flow(flow->ibv_flow));
1006                 flow->ibv_flow = NULL;
1007                 if (flow->drop)
1008                         mlx4_drop_put(priv->drop);
1009                 else if (flow->rss)
1010                         mlx4_rss_detach(flow->rss);
1011                 return 0;
1012         }
1013         assert(flow->ibv_attr);
1014         if (!flow->internal &&
1015             !priv->isolated &&
1016             flow->ibv_attr->priority == MLX4_FLOW_PRIORITY_LAST) {
1017                 if (flow->ibv_flow) {
1018                         claim_zero(mlx4_glue->destroy_flow(flow->ibv_flow));
1019                         flow->ibv_flow = NULL;
1020                         if (flow->drop)
1021                                 mlx4_drop_put(priv->drop);
1022                         else if (flow->rss)
1023                                 mlx4_rss_detach(flow->rss);
1024                 }
1025                 err = EACCES;
1026                 msg = ("priority level "
1027                        MLX4_STR_EXPAND(MLX4_FLOW_PRIORITY_LAST)
1028                        " is reserved when not in isolated mode");
1029                 goto error;
1030         }
1031         if (flow->rss) {
1032                 struct mlx4_rss *rss = flow->rss;
1033                 int missing = 0;
1034                 unsigned int i;
1035
1036                 /* Stop at the first nonexistent target queue. */
1037                 for (i = 0; i != rss->queues; ++i)
1038                         if (rss->queue_id[i] >=
1039                             priv->dev->data->nb_rx_queues ||
1040                             !priv->dev->data->rx_queues[rss->queue_id[i]]) {
1041                                 missing = 1;
1042                                 break;
1043                         }
1044                 if (flow->ibv_flow) {
1045                         if (missing ^ !flow->drop)
1046                                 return 0;
1047                         /* Verbs flow needs updating. */
1048                         claim_zero(mlx4_glue->destroy_flow(flow->ibv_flow));
1049                         flow->ibv_flow = NULL;
1050                         if (flow->drop)
1051                                 mlx4_drop_put(priv->drop);
1052                         else
1053                                 mlx4_rss_detach(rss);
1054                 }
1055                 if (!missing) {
1056                         err = mlx4_rss_attach(rss);
1057                         if (err) {
1058                                 err = -err;
1059                                 msg = "cannot create indirection table or hash"
1060                                         " QP to associate flow rule with";
1061                                 goto error;
1062                         }
1063                         qp = rss->qp;
1064                 }
1065                 /* A missing target queue drops traffic implicitly. */
1066                 flow->drop = missing;
1067         }
1068         if (flow->drop) {
1069                 if (flow->ibv_flow)
1070                         return 0;
1071                 mlx4_drop_get(priv);
1072                 if (!priv->drop) {
1073                         err = rte_errno;
1074                         msg = "resources for drop flow rule cannot be created";
1075                         goto error;
1076                 }
1077                 qp = priv->drop->qp;
1078         }
1079         assert(qp);
1080         if (flow->ibv_flow)
1081                 return 0;
1082         flow->ibv_flow = mlx4_glue->create_flow(qp, flow->ibv_attr);
1083         if (flow->ibv_flow)
1084                 return 0;
1085         if (flow->drop)
1086                 mlx4_drop_put(priv->drop);
1087         else if (flow->rss)
1088                 mlx4_rss_detach(flow->rss);
1089         err = errno;
1090         msg = "flow rule rejected by device";
1091 error:
1092         return rte_flow_error_set
1093                 (error, err, RTE_FLOW_ERROR_TYPE_HANDLE, flow, msg);
1094 }
1095
1096 /**
1097  * Create a flow.
1098  *
1099  * @see rte_flow_create()
1100  * @see rte_flow_ops
1101  */
1102 static struct rte_flow *
1103 mlx4_flow_create(struct rte_eth_dev *dev,
1104                  const struct rte_flow_attr *attr,
1105                  const struct rte_flow_item pattern[],
1106                  const struct rte_flow_action actions[],
1107                  struct rte_flow_error *error)
1108 {
1109         struct priv *priv = dev->data->dev_private;
1110         struct rte_flow *flow;
1111         int err;
1112
1113         err = mlx4_flow_prepare(priv, attr, pattern, actions, error, &flow);
1114         if (err)
1115                 return NULL;
1116         err = mlx4_flow_toggle(priv, flow, priv->started, error);
1117         if (!err) {
1118                 struct rte_flow *curr = LIST_FIRST(&priv->flows);
1119
1120                 /* New rules are inserted after internal ones. */
1121                 if (!curr || !curr->internal) {
1122                         LIST_INSERT_HEAD(&priv->flows, flow, next);
1123                 } else {
1124                         while (LIST_NEXT(curr, next) &&
1125                                LIST_NEXT(curr, next)->internal)
1126                                 curr = LIST_NEXT(curr, next);
1127                         LIST_INSERT_AFTER(curr, flow, next);
1128                 }
1129                 return flow;
1130         }
1131         if (flow->rss)
1132                 mlx4_rss_put(flow->rss);
1133         rte_flow_error_set(error, -err, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1134                            error->message);
1135         rte_free(flow);
1136         return NULL;
1137 }
1138
1139 /**
1140  * Configure isolated mode.
1141  *
1142  * @see rte_flow_isolate()
1143  * @see rte_flow_ops
1144  */
1145 static int
1146 mlx4_flow_isolate(struct rte_eth_dev *dev,
1147                   int enable,
1148                   struct rte_flow_error *error)
1149 {
1150         struct priv *priv = dev->data->dev_private;
1151
1152         if (!!enable == !!priv->isolated)
1153                 return 0;
1154         priv->isolated = !!enable;
1155         if (mlx4_flow_sync(priv, error)) {
1156                 priv->isolated = !enable;
1157                 return -rte_errno;
1158         }
1159         return 0;
1160 }
1161
1162 /**
1163  * Destroy a flow rule.
1164  *
1165  * @see rte_flow_destroy()
1166  * @see rte_flow_ops
1167  */
1168 static int
1169 mlx4_flow_destroy(struct rte_eth_dev *dev,
1170                   struct rte_flow *flow,
1171                   struct rte_flow_error *error)
1172 {
1173         struct priv *priv = dev->data->dev_private;
1174         int err = mlx4_flow_toggle(priv, flow, 0, error);
1175
1176         if (err)
1177                 return err;
1178         LIST_REMOVE(flow, next);
1179         if (flow->rss)
1180                 mlx4_rss_put(flow->rss);
1181         rte_free(flow);
1182         return 0;
1183 }
1184
1185 /**
1186  * Destroy user-configured flow rules.
1187  *
1188  * This function skips internal flows rules.
1189  *
1190  * @see rte_flow_flush()
1191  * @see rte_flow_ops
1192  */
1193 static int
1194 mlx4_flow_flush(struct rte_eth_dev *dev,
1195                 struct rte_flow_error *error)
1196 {
1197         struct priv *priv = dev->data->dev_private;
1198         struct rte_flow *flow = LIST_FIRST(&priv->flows);
1199
1200         while (flow) {
1201                 struct rte_flow *next = LIST_NEXT(flow, next);
1202
1203                 if (!flow->internal)
1204                         mlx4_flow_destroy(dev, flow, error);
1205                 flow = next;
1206         }
1207         return 0;
1208 }
1209
1210 /**
1211  * Helper function to determine the next configured VLAN filter.
1212  *
1213  * @param priv
1214  *   Pointer to private structure.
1215  * @param vlan
1216  *   VLAN ID to use as a starting point.
1217  *
1218  * @return
1219  *   Next configured VLAN ID or a high value (>= 4096) if there is none.
1220  */
1221 static uint16_t
1222 mlx4_flow_internal_next_vlan(struct priv *priv, uint16_t vlan)
1223 {
1224         while (vlan < 4096) {
1225                 if (priv->dev->data->vlan_filter_conf.ids[vlan / 64] &
1226                     (UINT64_C(1) << (vlan % 64)))
1227                         return vlan;
1228                 ++vlan;
1229         }
1230         return vlan;
1231 }
1232
1233 /**
1234  * Generate internal flow rules.
1235  *
1236  * Various flow rules are created depending on the mode the device is in:
1237  *
1238  * 1. Promiscuous:
1239  *       port MAC + broadcast + catch-all (VLAN filtering is ignored).
1240  * 2. All multicast:
1241  *       port MAC/VLAN + broadcast + catch-all multicast.
1242  * 3. Otherwise:
1243  *       port MAC/VLAN + broadcast MAC/VLAN.
1244  *
1245  * About MAC flow rules:
1246  *
1247  * - MAC flow rules are generated from @p dev->data->mac_addrs
1248  *   (@p priv->mac array).
1249  * - An additional flow rule for Ethernet broadcasts is also generated.
1250  * - All these are per-VLAN if @p DEV_RX_OFFLOAD_VLAN_FILTER
1251  *   is enabled and VLAN filters are configured.
1252  *
1253  * @param priv
1254  *   Pointer to private structure.
1255  * @param[out] error
1256  *   Perform verbose error reporting if not NULL.
1257  *
1258  * @return
1259  *   0 on success, a negative errno value otherwise and rte_errno is set.
1260  */
1261 static int
1262 mlx4_flow_internal(struct priv *priv, struct rte_flow_error *error)
1263 {
1264         struct rte_flow_attr attr = {
1265                 .priority = MLX4_FLOW_PRIORITY_LAST,
1266                 .ingress = 1,
1267         };
1268         struct rte_flow_item_eth eth_spec;
1269         const struct rte_flow_item_eth eth_mask = {
1270                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
1271         };
1272         const struct rte_flow_item_eth eth_allmulti = {
1273                 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
1274         };
1275         struct rte_flow_item_vlan vlan_spec;
1276         const struct rte_flow_item_vlan vlan_mask = {
1277                 .tci = RTE_BE16(0x0fff),
1278         };
1279         struct rte_flow_item pattern[] = {
1280                 {
1281                         .type = MLX4_FLOW_ITEM_TYPE_INTERNAL,
1282                 },
1283                 {
1284                         .type = RTE_FLOW_ITEM_TYPE_ETH,
1285                         .spec = &eth_spec,
1286                         .mask = &eth_mask,
1287                 },
1288                 {
1289                         /* Replaced with VLAN if filtering is enabled. */
1290                         .type = RTE_FLOW_ITEM_TYPE_END,
1291                 },
1292                 {
1293                         .type = RTE_FLOW_ITEM_TYPE_END,
1294                 },
1295         };
1296         /*
1297          * Round number of queues down to their previous power of 2 to
1298          * comply with RSS context limitations. Extra queues silently do not
1299          * get RSS by default.
1300          */
1301         uint32_t queues =
1302                 rte_align32pow2(priv->dev->data->nb_rx_queues + 1) >> 1;
1303         uint16_t queue[queues];
1304         struct rte_flow_action_rss action_rss = {
1305                 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
1306                 .level = 0,
1307                 .types = -1,
1308                 .key_len = MLX4_RSS_HASH_KEY_SIZE,
1309                 .queue_num = queues,
1310                 .key = mlx4_rss_hash_key_default,
1311                 .queue = queue,
1312         };
1313         struct rte_flow_action actions[] = {
1314                 {
1315                         .type = RTE_FLOW_ACTION_TYPE_RSS,
1316                         .conf = &action_rss,
1317                 },
1318                 {
1319                         .type = RTE_FLOW_ACTION_TYPE_END,
1320                 },
1321         };
1322         struct ether_addr *rule_mac = &eth_spec.dst;
1323         rte_be16_t *rule_vlan =
1324                 (priv->dev->data->dev_conf.rxmode.offloads &
1325                  DEV_RX_OFFLOAD_VLAN_FILTER) &&
1326                 !priv->dev->data->promiscuous ?
1327                 &vlan_spec.tci :
1328                 NULL;
1329         uint16_t vlan = 0;
1330         struct rte_flow *flow;
1331         unsigned int i;
1332         int err = 0;
1333
1334         /* Nothing to be done if there are no Rx queues. */
1335         if (!queues)
1336                 goto error;
1337         /* Prepare default RSS configuration. */
1338         for (i = 0; i != queues; ++i)
1339                 queue[i] = i;
1340         /*
1341          * Set up VLAN item if filtering is enabled and at least one VLAN
1342          * filter is configured.
1343          */
1344         if (rule_vlan) {
1345                 vlan = mlx4_flow_internal_next_vlan(priv, 0);
1346                 if (vlan < 4096) {
1347                         pattern[2] = (struct rte_flow_item){
1348                                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
1349                                 .spec = &vlan_spec,
1350                                 .mask = &vlan_mask,
1351                         };
1352 next_vlan:
1353                         *rule_vlan = rte_cpu_to_be_16(vlan);
1354                 } else {
1355                         rule_vlan = NULL;
1356                 }
1357         }
1358         for (i = 0; i != RTE_DIM(priv->mac) + 1; ++i) {
1359                 const struct ether_addr *mac;
1360
1361                 /* Broadcasts are handled by an extra iteration. */
1362                 if (i < RTE_DIM(priv->mac))
1363                         mac = &priv->mac[i];
1364                 else
1365                         mac = &eth_mask.dst;
1366                 if (is_zero_ether_addr(mac))
1367                         continue;
1368                 /* Check if MAC flow rule is already present. */
1369                 for (flow = LIST_FIRST(&priv->flows);
1370                      flow && flow->internal;
1371                      flow = LIST_NEXT(flow, next)) {
1372                         const struct ibv_flow_spec_eth *eth =
1373                                 (const void *)((uintptr_t)flow->ibv_attr +
1374                                                sizeof(*flow->ibv_attr));
1375                         unsigned int j;
1376
1377                         if (!flow->mac)
1378                                 continue;
1379                         assert(flow->ibv_attr->type == IBV_FLOW_ATTR_NORMAL);
1380                         assert(flow->ibv_attr->num_of_specs == 1);
1381                         assert(eth->type == IBV_FLOW_SPEC_ETH);
1382                         assert(flow->rss);
1383                         if (rule_vlan &&
1384                             (eth->val.vlan_tag != *rule_vlan ||
1385                              eth->mask.vlan_tag != RTE_BE16(0x0fff)))
1386                                 continue;
1387                         if (!rule_vlan && eth->mask.vlan_tag)
1388                                 continue;
1389                         for (j = 0; j != sizeof(mac->addr_bytes); ++j)
1390                                 if (eth->val.dst_mac[j] != mac->addr_bytes[j] ||
1391                                     eth->mask.dst_mac[j] != UINT8_C(0xff) ||
1392                                     eth->val.src_mac[j] != UINT8_C(0x00) ||
1393                                     eth->mask.src_mac[j] != UINT8_C(0x00))
1394                                         break;
1395                         if (j != sizeof(mac->addr_bytes))
1396                                 continue;
1397                         if (flow->rss->queues != queues ||
1398                             memcmp(flow->rss->queue_id, action_rss.queue,
1399                                    queues * sizeof(flow->rss->queue_id[0])))
1400                                 continue;
1401                         break;
1402                 }
1403                 if (!flow || !flow->internal) {
1404                         /* Not found, create a new flow rule. */
1405                         memcpy(rule_mac, mac, sizeof(*mac));
1406                         flow = mlx4_flow_create(priv->dev, &attr, pattern,
1407                                                 actions, error);
1408                         if (!flow) {
1409                                 err = -rte_errno;
1410                                 goto error;
1411                         }
1412                 }
1413                 flow->select = 1;
1414                 flow->mac = 1;
1415         }
1416         if (rule_vlan) {
1417                 vlan = mlx4_flow_internal_next_vlan(priv, vlan + 1);
1418                 if (vlan < 4096)
1419                         goto next_vlan;
1420         }
1421         /* Take care of promiscuous and all multicast flow rules. */
1422         if (priv->dev->data->promiscuous || priv->dev->data->all_multicast) {
1423                 for (flow = LIST_FIRST(&priv->flows);
1424                      flow && flow->internal;
1425                      flow = LIST_NEXT(flow, next)) {
1426                         if (priv->dev->data->promiscuous) {
1427                                 if (flow->promisc)
1428                                         break;
1429                         } else {
1430                                 assert(priv->dev->data->all_multicast);
1431                                 if (flow->allmulti)
1432                                         break;
1433                         }
1434                 }
1435                 if (flow && flow->internal) {
1436                         assert(flow->rss);
1437                         if (flow->rss->queues != queues ||
1438                             memcmp(flow->rss->queue_id, action_rss.queue,
1439                                    queues * sizeof(flow->rss->queue_id[0])))
1440                                 flow = NULL;
1441                 }
1442                 if (!flow || !flow->internal) {
1443                         /* Not found, create a new flow rule. */
1444                         if (priv->dev->data->promiscuous) {
1445                                 pattern[1].spec = NULL;
1446                                 pattern[1].mask = NULL;
1447                         } else {
1448                                 assert(priv->dev->data->all_multicast);
1449                                 pattern[1].spec = &eth_allmulti;
1450                                 pattern[1].mask = &eth_allmulti;
1451                         }
1452                         pattern[2] = pattern[3];
1453                         flow = mlx4_flow_create(priv->dev, &attr, pattern,
1454                                                 actions, error);
1455                         if (!flow) {
1456                                 err = -rte_errno;
1457                                 goto error;
1458                         }
1459                 }
1460                 assert(flow->promisc || flow->allmulti);
1461                 flow->select = 1;
1462         }
1463 error:
1464         /* Clear selection and clean up stale internal flow rules. */
1465         flow = LIST_FIRST(&priv->flows);
1466         while (flow && flow->internal) {
1467                 struct rte_flow *next = LIST_NEXT(flow, next);
1468
1469                 if (!flow->select)
1470                         claim_zero(mlx4_flow_destroy(priv->dev, flow, error));
1471                 else
1472                         flow->select = 0;
1473                 flow = next;
1474         }
1475         return err;
1476 }
1477
1478 /**
1479  * Synchronize flow rules.
1480  *
1481  * This function synchronizes flow rules with the state of the device by
1482  * taking into account isolated mode and whether target queues are
1483  * configured.
1484  *
1485  * @param priv
1486  *   Pointer to private structure.
1487  * @param[out] error
1488  *   Perform verbose error reporting if not NULL.
1489  *
1490  * @return
1491  *   0 on success, a negative errno value otherwise and rte_errno is set.
1492  */
1493 int
1494 mlx4_flow_sync(struct priv *priv, struct rte_flow_error *error)
1495 {
1496         struct rte_flow *flow;
1497         int ret;
1498
1499         /* Internal flow rules are guaranteed to come first in the list. */
1500         if (priv->isolated) {
1501                 /*
1502                  * Get rid of them in isolated mode, stop at the first
1503                  * non-internal rule found.
1504                  */
1505                 for (flow = LIST_FIRST(&priv->flows);
1506                      flow && flow->internal;
1507                      flow = LIST_FIRST(&priv->flows))
1508                         claim_zero(mlx4_flow_destroy(priv->dev, flow, error));
1509         } else {
1510                 /* Refresh internal rules. */
1511                 ret = mlx4_flow_internal(priv, error);
1512                 if (ret)
1513                         return ret;
1514         }
1515         /* Toggle the remaining flow rules . */
1516         LIST_FOREACH(flow, &priv->flows, next) {
1517                 ret = mlx4_flow_toggle(priv, flow, priv->started, error);
1518                 if (ret)
1519                         return ret;
1520         }
1521         if (!priv->started)
1522                 assert(!priv->drop);
1523         return 0;
1524 }
1525
1526 /**
1527  * Clean up all flow rules.
1528  *
1529  * Unlike mlx4_flow_flush(), this function takes care of all remaining flow
1530  * rules regardless of whether they are internal or user-configured.
1531  *
1532  * @param priv
1533  *   Pointer to private structure.
1534  */
1535 void
1536 mlx4_flow_clean(struct priv *priv)
1537 {
1538         struct rte_flow *flow;
1539
1540         while ((flow = LIST_FIRST(&priv->flows)))
1541                 mlx4_flow_destroy(priv->dev, flow, NULL);
1542         assert(LIST_EMPTY(&priv->rss));
1543 }
1544
1545 static const struct rte_flow_ops mlx4_flow_ops = {
1546         .validate = mlx4_flow_validate,
1547         .create = mlx4_flow_create,
1548         .destroy = mlx4_flow_destroy,
1549         .flush = mlx4_flow_flush,
1550         .isolate = mlx4_flow_isolate,
1551 };
1552
1553 /**
1554  * Manage filter operations.
1555  *
1556  * @param dev
1557  *   Pointer to Ethernet device structure.
1558  * @param filter_type
1559  *   Filter type.
1560  * @param filter_op
1561  *   Operation to perform.
1562  * @param arg
1563  *   Pointer to operation-specific structure.
1564  *
1565  * @return
1566  *   0 on success, negative errno value otherwise and rte_errno is set.
1567  */
1568 int
1569 mlx4_filter_ctrl(struct rte_eth_dev *dev,
1570                  enum rte_filter_type filter_type,
1571                  enum rte_filter_op filter_op,
1572                  void *arg)
1573 {
1574         switch (filter_type) {
1575         case RTE_ETH_FILTER_GENERIC:
1576                 if (filter_op != RTE_ETH_FILTER_GET)
1577                         break;
1578                 *(const void **)arg = &mlx4_flow_ops;
1579                 return 0;
1580         default:
1581                 ERROR("%p: filter type (%d) not supported",
1582                       (void *)dev, filter_type);
1583                 break;
1584         }
1585         rte_errno = ENOTSUP;
1586         return -rte_errno;
1587 }