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