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