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