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