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