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