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