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