55226f12db682bf252b4275987a2dd8120d10937
[dpdk.git] / drivers / net / sfc / sfc_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2017-2018 Solarflare Communications Inc.
4  * All rights reserved.
5  *
6  * This software was jointly developed between OKTET Labs (under contract
7  * for Solarflare) and Solarflare Communications, Inc.
8  */
9
10 #include <rte_byteorder.h>
11 #include <rte_tailq.h>
12 #include <rte_common.h>
13 #include <rte_ethdev_driver.h>
14 #include <rte_eth_ctrl.h>
15 #include <rte_ether.h>
16 #include <rte_flow.h>
17 #include <rte_flow_driver.h>
18
19 #include "efx.h"
20
21 #include "sfc.h"
22 #include "sfc_rx.h"
23 #include "sfc_filter.h"
24 #include "sfc_flow.h"
25 #include "sfc_log.h"
26
27 /*
28  * At now flow API is implemented in such a manner that each
29  * flow rule is converted to one or more hardware filters.
30  * All elements of flow rule (attributes, pattern items, actions)
31  * correspond to one or more fields in the efx_filter_spec_s structure
32  * that is responsible for the hardware filter.
33  * If some required field is unset in the flow rule, then a handful
34  * of filter copies will be created to cover all possible values
35  * of such a field.
36  */
37
38 enum sfc_flow_item_layers {
39         SFC_FLOW_ITEM_ANY_LAYER,
40         SFC_FLOW_ITEM_START_LAYER,
41         SFC_FLOW_ITEM_L2,
42         SFC_FLOW_ITEM_L3,
43         SFC_FLOW_ITEM_L4,
44 };
45
46 typedef int (sfc_flow_item_parse)(const struct rte_flow_item *item,
47                                   efx_filter_spec_t *spec,
48                                   struct rte_flow_error *error);
49
50 struct sfc_flow_item {
51         enum rte_flow_item_type type;           /* Type of item */
52         enum sfc_flow_item_layers layer;        /* Layer of item */
53         enum sfc_flow_item_layers prev_layer;   /* Previous layer of item */
54         sfc_flow_item_parse *parse;             /* Parsing function */
55 };
56
57 static sfc_flow_item_parse sfc_flow_parse_void;
58 static sfc_flow_item_parse sfc_flow_parse_eth;
59 static sfc_flow_item_parse sfc_flow_parse_vlan;
60 static sfc_flow_item_parse sfc_flow_parse_ipv4;
61 static sfc_flow_item_parse sfc_flow_parse_ipv6;
62 static sfc_flow_item_parse sfc_flow_parse_tcp;
63 static sfc_flow_item_parse sfc_flow_parse_udp;
64 static sfc_flow_item_parse sfc_flow_parse_vxlan;
65 static sfc_flow_item_parse sfc_flow_parse_geneve;
66 static sfc_flow_item_parse sfc_flow_parse_nvgre;
67
68 typedef int (sfc_flow_spec_set_vals)(struct sfc_flow_spec *spec,
69                                      unsigned int filters_count_for_one_val,
70                                      struct rte_flow_error *error);
71
72 typedef boolean_t (sfc_flow_spec_check)(efx_filter_match_flags_t match,
73                                         efx_filter_spec_t *spec,
74                                         struct sfc_filter *filter);
75
76 struct sfc_flow_copy_flag {
77         /* EFX filter specification match flag */
78         efx_filter_match_flags_t flag;
79         /* Number of values of corresponding field */
80         unsigned int vals_count;
81         /* Function to set values in specifications */
82         sfc_flow_spec_set_vals *set_vals;
83         /*
84          * Function to check that the specification is suitable
85          * for adding this match flag
86          */
87         sfc_flow_spec_check *spec_check;
88 };
89
90 static sfc_flow_spec_set_vals sfc_flow_set_unknown_dst_flags;
91 static sfc_flow_spec_check sfc_flow_check_unknown_dst_flags;
92 static sfc_flow_spec_set_vals sfc_flow_set_ethertypes;
93 static sfc_flow_spec_set_vals sfc_flow_set_ifrm_unknown_dst_flags;
94 static sfc_flow_spec_check sfc_flow_check_ifrm_unknown_dst_flags;
95
96 static boolean_t
97 sfc_flow_is_zero(const uint8_t *buf, unsigned int size)
98 {
99         uint8_t sum = 0;
100         unsigned int i;
101
102         for (i = 0; i < size; i++)
103                 sum |= buf[i];
104
105         return (sum == 0) ? B_TRUE : B_FALSE;
106 }
107
108 /*
109  * Validate item and prepare structures spec and mask for parsing
110  */
111 static int
112 sfc_flow_parse_init(const struct rte_flow_item *item,
113                     const void **spec_ptr,
114                     const void **mask_ptr,
115                     const void *supp_mask,
116                     const void *def_mask,
117                     unsigned int size,
118                     struct rte_flow_error *error)
119 {
120         const uint8_t *spec;
121         const uint8_t *mask;
122         const uint8_t *last;
123         uint8_t match;
124         uint8_t supp;
125         unsigned int i;
126
127         if (item == NULL) {
128                 rte_flow_error_set(error, EINVAL,
129                                    RTE_FLOW_ERROR_TYPE_ITEM, NULL,
130                                    "NULL item");
131                 return -rte_errno;
132         }
133
134         if ((item->last != NULL || item->mask != NULL) && item->spec == NULL) {
135                 rte_flow_error_set(error, EINVAL,
136                                    RTE_FLOW_ERROR_TYPE_ITEM, item,
137                                    "Mask or last is set without spec");
138                 return -rte_errno;
139         }
140
141         /*
142          * If "mask" is not set, default mask is used,
143          * but if default mask is NULL, "mask" should be set
144          */
145         if (item->mask == NULL) {
146                 if (def_mask == NULL) {
147                         rte_flow_error_set(error, EINVAL,
148                                 RTE_FLOW_ERROR_TYPE_ITEM, NULL,
149                                 "Mask should be specified");
150                         return -rte_errno;
151                 }
152
153                 mask = def_mask;
154         } else {
155                 mask = item->mask;
156         }
157
158         spec = item->spec;
159         last = item->last;
160
161         if (spec == NULL)
162                 goto exit;
163
164         /*
165          * If field values in "last" are either 0 or equal to the corresponding
166          * values in "spec" then they are ignored
167          */
168         if (last != NULL &&
169             !sfc_flow_is_zero(last, size) &&
170             memcmp(last, spec, size) != 0) {
171                 rte_flow_error_set(error, ENOTSUP,
172                                    RTE_FLOW_ERROR_TYPE_ITEM, item,
173                                    "Ranging is not supported");
174                 return -rte_errno;
175         }
176
177         if (supp_mask == NULL) {
178                 rte_flow_error_set(error, EINVAL,
179                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
180                         "Supported mask for item should be specified");
181                 return -rte_errno;
182         }
183
184         /* Check that mask and spec not asks for more match than supp_mask */
185         for (i = 0; i < size; i++) {
186                 match = spec[i] | mask[i];
187                 supp = ((const uint8_t *)supp_mask)[i];
188
189                 if ((match | supp) != supp) {
190                         rte_flow_error_set(error, ENOTSUP,
191                                            RTE_FLOW_ERROR_TYPE_ITEM, item,
192                                            "Item's field is not supported");
193                         return -rte_errno;
194                 }
195         }
196
197 exit:
198         *spec_ptr = spec;
199         *mask_ptr = mask;
200         return 0;
201 }
202
203 /*
204  * Protocol parsers.
205  * Masking is not supported, so masks in items should be either
206  * full or empty (zeroed) and set only for supported fields which
207  * are specified in the supp_mask.
208  */
209
210 static int
211 sfc_flow_parse_void(__rte_unused const struct rte_flow_item *item,
212                     __rte_unused efx_filter_spec_t *efx_spec,
213                     __rte_unused struct rte_flow_error *error)
214 {
215         return 0;
216 }
217
218 /**
219  * Convert Ethernet item to EFX filter specification.
220  *
221  * @param item[in]
222  *   Item specification. Outer frame specification may only comprise
223  *   source/destination addresses and Ethertype field.
224  *   Inner frame specification may contain destination address only.
225  *   There is support for individual/group mask as well as for empty and full.
226  *   If the mask is NULL, default mask will be used. Ranging is not supported.
227  * @param efx_spec[in, out]
228  *   EFX filter specification to update.
229  * @param[out] error
230  *   Perform verbose error reporting if not NULL.
231  */
232 static int
233 sfc_flow_parse_eth(const struct rte_flow_item *item,
234                    efx_filter_spec_t *efx_spec,
235                    struct rte_flow_error *error)
236 {
237         int rc;
238         const struct rte_flow_item_eth *spec = NULL;
239         const struct rte_flow_item_eth *mask = NULL;
240         const struct rte_flow_item_eth supp_mask = {
241                 .dst.addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
242                 .src.addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
243                 .type = 0xffff,
244         };
245         const struct rte_flow_item_eth ifrm_supp_mask = {
246                 .dst.addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
247         };
248         const uint8_t ig_mask[EFX_MAC_ADDR_LEN] = {
249                 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
250         };
251         const struct rte_flow_item_eth *supp_mask_p;
252         const struct rte_flow_item_eth *def_mask_p;
253         uint8_t *loc_mac = NULL;
254         boolean_t is_ifrm = (efx_spec->efs_encap_type !=
255                 EFX_TUNNEL_PROTOCOL_NONE);
256
257         if (is_ifrm) {
258                 supp_mask_p = &ifrm_supp_mask;
259                 def_mask_p = &ifrm_supp_mask;
260                 loc_mac = efx_spec->efs_ifrm_loc_mac;
261         } else {
262                 supp_mask_p = &supp_mask;
263                 def_mask_p = &rte_flow_item_eth_mask;
264                 loc_mac = efx_spec->efs_loc_mac;
265         }
266
267         rc = sfc_flow_parse_init(item,
268                                  (const void **)&spec,
269                                  (const void **)&mask,
270                                  supp_mask_p, def_mask_p,
271                                  sizeof(struct rte_flow_item_eth),
272                                  error);
273         if (rc != 0)
274                 return rc;
275
276         /* If "spec" is not set, could be any Ethernet */
277         if (spec == NULL)
278                 return 0;
279
280         if (is_same_ether_addr(&mask->dst, &supp_mask.dst)) {
281                 efx_spec->efs_match_flags |= is_ifrm ?
282                         EFX_FILTER_MATCH_IFRM_LOC_MAC :
283                         EFX_FILTER_MATCH_LOC_MAC;
284                 rte_memcpy(loc_mac, spec->dst.addr_bytes,
285                            EFX_MAC_ADDR_LEN);
286         } else if (memcmp(mask->dst.addr_bytes, ig_mask,
287                           EFX_MAC_ADDR_LEN) == 0) {
288                 if (is_unicast_ether_addr(&spec->dst))
289                         efx_spec->efs_match_flags |= is_ifrm ?
290                                 EFX_FILTER_MATCH_IFRM_UNKNOWN_UCAST_DST :
291                                 EFX_FILTER_MATCH_UNKNOWN_UCAST_DST;
292                 else
293                         efx_spec->efs_match_flags |= is_ifrm ?
294                                 EFX_FILTER_MATCH_IFRM_UNKNOWN_MCAST_DST :
295                                 EFX_FILTER_MATCH_UNKNOWN_MCAST_DST;
296         } else if (!is_zero_ether_addr(&mask->dst)) {
297                 goto fail_bad_mask;
298         }
299
300         /*
301          * ifrm_supp_mask ensures that the source address and
302          * ethertype masks are equal to zero in inner frame,
303          * so these fields are filled in only for the outer frame
304          */
305         if (is_same_ether_addr(&mask->src, &supp_mask.src)) {
306                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_REM_MAC;
307                 rte_memcpy(efx_spec->efs_rem_mac, spec->src.addr_bytes,
308                            EFX_MAC_ADDR_LEN);
309         } else if (!is_zero_ether_addr(&mask->src)) {
310                 goto fail_bad_mask;
311         }
312
313         /*
314          * Ether type is in big-endian byte order in item and
315          * in little-endian in efx_spec, so byte swap is used
316          */
317         if (mask->type == supp_mask.type) {
318                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_ETHER_TYPE;
319                 efx_spec->efs_ether_type = rte_bswap16(spec->type);
320         } else if (mask->type != 0) {
321                 goto fail_bad_mask;
322         }
323
324         return 0;
325
326 fail_bad_mask:
327         rte_flow_error_set(error, EINVAL,
328                            RTE_FLOW_ERROR_TYPE_ITEM, item,
329                            "Bad mask in the ETH pattern item");
330         return -rte_errno;
331 }
332
333 /**
334  * Convert VLAN item to EFX filter specification.
335  *
336  * @param item[in]
337  *   Item specification. Only VID field is supported.
338  *   The mask can not be NULL. Ranging is not supported.
339  * @param efx_spec[in, out]
340  *   EFX filter specification to update.
341  * @param[out] error
342  *   Perform verbose error reporting if not NULL.
343  */
344 static int
345 sfc_flow_parse_vlan(const struct rte_flow_item *item,
346                     efx_filter_spec_t *efx_spec,
347                     struct rte_flow_error *error)
348 {
349         int rc;
350         uint16_t vid;
351         const struct rte_flow_item_vlan *spec = NULL;
352         const struct rte_flow_item_vlan *mask = NULL;
353         const struct rte_flow_item_vlan supp_mask = {
354                 .tci = rte_cpu_to_be_16(ETH_VLAN_ID_MAX),
355                 .inner_type = RTE_BE16(0xffff),
356         };
357
358         rc = sfc_flow_parse_init(item,
359                                  (const void **)&spec,
360                                  (const void **)&mask,
361                                  &supp_mask,
362                                  NULL,
363                                  sizeof(struct rte_flow_item_vlan),
364                                  error);
365         if (rc != 0)
366                 return rc;
367
368         /*
369          * VID is in big-endian byte order in item and
370          * in little-endian in efx_spec, so byte swap is used.
371          * If two VLAN items are included, the first matches
372          * the outer tag and the next matches the inner tag.
373          */
374         if (mask->tci == supp_mask.tci) {
375                 vid = rte_bswap16(spec->tci);
376
377                 if (!(efx_spec->efs_match_flags &
378                       EFX_FILTER_MATCH_OUTER_VID)) {
379                         efx_spec->efs_match_flags |= EFX_FILTER_MATCH_OUTER_VID;
380                         efx_spec->efs_outer_vid = vid;
381                 } else if (!(efx_spec->efs_match_flags &
382                              EFX_FILTER_MATCH_INNER_VID)) {
383                         efx_spec->efs_match_flags |= EFX_FILTER_MATCH_INNER_VID;
384                         efx_spec->efs_inner_vid = vid;
385                 } else {
386                         rte_flow_error_set(error, EINVAL,
387                                            RTE_FLOW_ERROR_TYPE_ITEM, item,
388                                            "More than two VLAN items");
389                         return -rte_errno;
390                 }
391         } else {
392                 rte_flow_error_set(error, EINVAL,
393                                    RTE_FLOW_ERROR_TYPE_ITEM, item,
394                                    "VLAN ID in TCI match is required");
395                 return -rte_errno;
396         }
397
398         if (efx_spec->efs_match_flags & EFX_FILTER_MATCH_ETHER_TYPE) {
399                 rte_flow_error_set(error, EINVAL,
400                                    RTE_FLOW_ERROR_TYPE_ITEM, item,
401                                    "VLAN TPID matching is not supported");
402                 return -rte_errno;
403         }
404         if (mask->inner_type == supp_mask.inner_type) {
405                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_ETHER_TYPE;
406                 efx_spec->efs_ether_type = rte_bswap16(spec->inner_type);
407         } else if (mask->inner_type) {
408                 rte_flow_error_set(error, EINVAL,
409                                    RTE_FLOW_ERROR_TYPE_ITEM, item,
410                                    "Bad mask for VLAN inner_type");
411                 return -rte_errno;
412         }
413
414         return 0;
415 }
416
417 /**
418  * Convert IPv4 item to EFX filter specification.
419  *
420  * @param item[in]
421  *   Item specification. Only source and destination addresses and
422  *   protocol fields are supported. If the mask is NULL, default
423  *   mask will be used. Ranging is not supported.
424  * @param efx_spec[in, out]
425  *   EFX filter specification to update.
426  * @param[out] error
427  *   Perform verbose error reporting if not NULL.
428  */
429 static int
430 sfc_flow_parse_ipv4(const struct rte_flow_item *item,
431                     efx_filter_spec_t *efx_spec,
432                     struct rte_flow_error *error)
433 {
434         int rc;
435         const struct rte_flow_item_ipv4 *spec = NULL;
436         const struct rte_flow_item_ipv4 *mask = NULL;
437         const uint16_t ether_type_ipv4 = rte_cpu_to_le_16(EFX_ETHER_TYPE_IPV4);
438         const struct rte_flow_item_ipv4 supp_mask = {
439                 .hdr = {
440                         .src_addr = 0xffffffff,
441                         .dst_addr = 0xffffffff,
442                         .next_proto_id = 0xff,
443                 }
444         };
445
446         rc = sfc_flow_parse_init(item,
447                                  (const void **)&spec,
448                                  (const void **)&mask,
449                                  &supp_mask,
450                                  &rte_flow_item_ipv4_mask,
451                                  sizeof(struct rte_flow_item_ipv4),
452                                  error);
453         if (rc != 0)
454                 return rc;
455
456         /*
457          * Filtering by IPv4 source and destination addresses requires
458          * the appropriate ETHER_TYPE in hardware filters
459          */
460         if (!(efx_spec->efs_match_flags & EFX_FILTER_MATCH_ETHER_TYPE)) {
461                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_ETHER_TYPE;
462                 efx_spec->efs_ether_type = ether_type_ipv4;
463         } else if (efx_spec->efs_ether_type != ether_type_ipv4) {
464                 rte_flow_error_set(error, EINVAL,
465                         RTE_FLOW_ERROR_TYPE_ITEM, item,
466                         "Ethertype in pattern with IPV4 item should be appropriate");
467                 return -rte_errno;
468         }
469
470         if (spec == NULL)
471                 return 0;
472
473         /*
474          * IPv4 addresses are in big-endian byte order in item and in
475          * efx_spec
476          */
477         if (mask->hdr.src_addr == supp_mask.hdr.src_addr) {
478                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_REM_HOST;
479                 efx_spec->efs_rem_host.eo_u32[0] = spec->hdr.src_addr;
480         } else if (mask->hdr.src_addr != 0) {
481                 goto fail_bad_mask;
482         }
483
484         if (mask->hdr.dst_addr == supp_mask.hdr.dst_addr) {
485                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_LOC_HOST;
486                 efx_spec->efs_loc_host.eo_u32[0] = spec->hdr.dst_addr;
487         } else if (mask->hdr.dst_addr != 0) {
488                 goto fail_bad_mask;
489         }
490
491         if (mask->hdr.next_proto_id == supp_mask.hdr.next_proto_id) {
492                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_IP_PROTO;
493                 efx_spec->efs_ip_proto = spec->hdr.next_proto_id;
494         } else if (mask->hdr.next_proto_id != 0) {
495                 goto fail_bad_mask;
496         }
497
498         return 0;
499
500 fail_bad_mask:
501         rte_flow_error_set(error, EINVAL,
502                            RTE_FLOW_ERROR_TYPE_ITEM, item,
503                            "Bad mask in the IPV4 pattern item");
504         return -rte_errno;
505 }
506
507 /**
508  * Convert IPv6 item to EFX filter specification.
509  *
510  * @param item[in]
511  *   Item specification. Only source and destination addresses and
512  *   next header fields are supported. If the mask is NULL, default
513  *   mask will be used. Ranging is not supported.
514  * @param efx_spec[in, out]
515  *   EFX filter specification to update.
516  * @param[out] error
517  *   Perform verbose error reporting if not NULL.
518  */
519 static int
520 sfc_flow_parse_ipv6(const struct rte_flow_item *item,
521                     efx_filter_spec_t *efx_spec,
522                     struct rte_flow_error *error)
523 {
524         int rc;
525         const struct rte_flow_item_ipv6 *spec = NULL;
526         const struct rte_flow_item_ipv6 *mask = NULL;
527         const uint16_t ether_type_ipv6 = rte_cpu_to_le_16(EFX_ETHER_TYPE_IPV6);
528         const struct rte_flow_item_ipv6 supp_mask = {
529                 .hdr = {
530                         .src_addr = { 0xff, 0xff, 0xff, 0xff,
531                                       0xff, 0xff, 0xff, 0xff,
532                                       0xff, 0xff, 0xff, 0xff,
533                                       0xff, 0xff, 0xff, 0xff },
534                         .dst_addr = { 0xff, 0xff, 0xff, 0xff,
535                                       0xff, 0xff, 0xff, 0xff,
536                                       0xff, 0xff, 0xff, 0xff,
537                                       0xff, 0xff, 0xff, 0xff },
538                         .proto = 0xff,
539                 }
540         };
541
542         rc = sfc_flow_parse_init(item,
543                                  (const void **)&spec,
544                                  (const void **)&mask,
545                                  &supp_mask,
546                                  &rte_flow_item_ipv6_mask,
547                                  sizeof(struct rte_flow_item_ipv6),
548                                  error);
549         if (rc != 0)
550                 return rc;
551
552         /*
553          * Filtering by IPv6 source and destination addresses requires
554          * the appropriate ETHER_TYPE in hardware filters
555          */
556         if (!(efx_spec->efs_match_flags & EFX_FILTER_MATCH_ETHER_TYPE)) {
557                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_ETHER_TYPE;
558                 efx_spec->efs_ether_type = ether_type_ipv6;
559         } else if (efx_spec->efs_ether_type != ether_type_ipv6) {
560                 rte_flow_error_set(error, EINVAL,
561                         RTE_FLOW_ERROR_TYPE_ITEM, item,
562                         "Ethertype in pattern with IPV6 item should be appropriate");
563                 return -rte_errno;
564         }
565
566         if (spec == NULL)
567                 return 0;
568
569         /*
570          * IPv6 addresses are in big-endian byte order in item and in
571          * efx_spec
572          */
573         if (memcmp(mask->hdr.src_addr, supp_mask.hdr.src_addr,
574                    sizeof(mask->hdr.src_addr)) == 0) {
575                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_REM_HOST;
576
577                 RTE_BUILD_BUG_ON(sizeof(efx_spec->efs_rem_host) !=
578                                  sizeof(spec->hdr.src_addr));
579                 rte_memcpy(&efx_spec->efs_rem_host, spec->hdr.src_addr,
580                            sizeof(efx_spec->efs_rem_host));
581         } else if (!sfc_flow_is_zero(mask->hdr.src_addr,
582                                      sizeof(mask->hdr.src_addr))) {
583                 goto fail_bad_mask;
584         }
585
586         if (memcmp(mask->hdr.dst_addr, supp_mask.hdr.dst_addr,
587                    sizeof(mask->hdr.dst_addr)) == 0) {
588                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_LOC_HOST;
589
590                 RTE_BUILD_BUG_ON(sizeof(efx_spec->efs_loc_host) !=
591                                  sizeof(spec->hdr.dst_addr));
592                 rte_memcpy(&efx_spec->efs_loc_host, spec->hdr.dst_addr,
593                            sizeof(efx_spec->efs_loc_host));
594         } else if (!sfc_flow_is_zero(mask->hdr.dst_addr,
595                                      sizeof(mask->hdr.dst_addr))) {
596                 goto fail_bad_mask;
597         }
598
599         if (mask->hdr.proto == supp_mask.hdr.proto) {
600                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_IP_PROTO;
601                 efx_spec->efs_ip_proto = spec->hdr.proto;
602         } else if (mask->hdr.proto != 0) {
603                 goto fail_bad_mask;
604         }
605
606         return 0;
607
608 fail_bad_mask:
609         rte_flow_error_set(error, EINVAL,
610                            RTE_FLOW_ERROR_TYPE_ITEM, item,
611                            "Bad mask in the IPV6 pattern item");
612         return -rte_errno;
613 }
614
615 /**
616  * Convert TCP item to EFX filter specification.
617  *
618  * @param item[in]
619  *   Item specification. Only source and destination ports fields
620  *   are supported. If the mask is NULL, default mask will be used.
621  *   Ranging is not supported.
622  * @param efx_spec[in, out]
623  *   EFX filter specification to update.
624  * @param[out] error
625  *   Perform verbose error reporting if not NULL.
626  */
627 static int
628 sfc_flow_parse_tcp(const struct rte_flow_item *item,
629                    efx_filter_spec_t *efx_spec,
630                    struct rte_flow_error *error)
631 {
632         int rc;
633         const struct rte_flow_item_tcp *spec = NULL;
634         const struct rte_flow_item_tcp *mask = NULL;
635         const struct rte_flow_item_tcp supp_mask = {
636                 .hdr = {
637                         .src_port = 0xffff,
638                         .dst_port = 0xffff,
639                 }
640         };
641
642         rc = sfc_flow_parse_init(item,
643                                  (const void **)&spec,
644                                  (const void **)&mask,
645                                  &supp_mask,
646                                  &rte_flow_item_tcp_mask,
647                                  sizeof(struct rte_flow_item_tcp),
648                                  error);
649         if (rc != 0)
650                 return rc;
651
652         /*
653          * Filtering by TCP source and destination ports requires
654          * the appropriate IP_PROTO in hardware filters
655          */
656         if (!(efx_spec->efs_match_flags & EFX_FILTER_MATCH_IP_PROTO)) {
657                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_IP_PROTO;
658                 efx_spec->efs_ip_proto = EFX_IPPROTO_TCP;
659         } else if (efx_spec->efs_ip_proto != EFX_IPPROTO_TCP) {
660                 rte_flow_error_set(error, EINVAL,
661                         RTE_FLOW_ERROR_TYPE_ITEM, item,
662                         "IP proto in pattern with TCP item should be appropriate");
663                 return -rte_errno;
664         }
665
666         if (spec == NULL)
667                 return 0;
668
669         /*
670          * Source and destination ports are in big-endian byte order in item and
671          * in little-endian in efx_spec, so byte swap is used
672          */
673         if (mask->hdr.src_port == supp_mask.hdr.src_port) {
674                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_REM_PORT;
675                 efx_spec->efs_rem_port = rte_bswap16(spec->hdr.src_port);
676         } else if (mask->hdr.src_port != 0) {
677                 goto fail_bad_mask;
678         }
679
680         if (mask->hdr.dst_port == supp_mask.hdr.dst_port) {
681                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_LOC_PORT;
682                 efx_spec->efs_loc_port = rte_bswap16(spec->hdr.dst_port);
683         } else if (mask->hdr.dst_port != 0) {
684                 goto fail_bad_mask;
685         }
686
687         return 0;
688
689 fail_bad_mask:
690         rte_flow_error_set(error, EINVAL,
691                            RTE_FLOW_ERROR_TYPE_ITEM, item,
692                            "Bad mask in the TCP pattern item");
693         return -rte_errno;
694 }
695
696 /**
697  * Convert UDP item to EFX filter specification.
698  *
699  * @param item[in]
700  *   Item specification. Only source and destination ports fields
701  *   are supported. If the mask is NULL, default mask will be used.
702  *   Ranging is not supported.
703  * @param efx_spec[in, out]
704  *   EFX filter specification to update.
705  * @param[out] error
706  *   Perform verbose error reporting if not NULL.
707  */
708 static int
709 sfc_flow_parse_udp(const struct rte_flow_item *item,
710                    efx_filter_spec_t *efx_spec,
711                    struct rte_flow_error *error)
712 {
713         int rc;
714         const struct rte_flow_item_udp *spec = NULL;
715         const struct rte_flow_item_udp *mask = NULL;
716         const struct rte_flow_item_udp supp_mask = {
717                 .hdr = {
718                         .src_port = 0xffff,
719                         .dst_port = 0xffff,
720                 }
721         };
722
723         rc = sfc_flow_parse_init(item,
724                                  (const void **)&spec,
725                                  (const void **)&mask,
726                                  &supp_mask,
727                                  &rte_flow_item_udp_mask,
728                                  sizeof(struct rte_flow_item_udp),
729                                  error);
730         if (rc != 0)
731                 return rc;
732
733         /*
734          * Filtering by UDP source and destination ports requires
735          * the appropriate IP_PROTO in hardware filters
736          */
737         if (!(efx_spec->efs_match_flags & EFX_FILTER_MATCH_IP_PROTO)) {
738                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_IP_PROTO;
739                 efx_spec->efs_ip_proto = EFX_IPPROTO_UDP;
740         } else if (efx_spec->efs_ip_proto != EFX_IPPROTO_UDP) {
741                 rte_flow_error_set(error, EINVAL,
742                         RTE_FLOW_ERROR_TYPE_ITEM, item,
743                         "IP proto in pattern with UDP item should be appropriate");
744                 return -rte_errno;
745         }
746
747         if (spec == NULL)
748                 return 0;
749
750         /*
751          * Source and destination ports are in big-endian byte order in item and
752          * in little-endian in efx_spec, so byte swap is used
753          */
754         if (mask->hdr.src_port == supp_mask.hdr.src_port) {
755                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_REM_PORT;
756                 efx_spec->efs_rem_port = rte_bswap16(spec->hdr.src_port);
757         } else if (mask->hdr.src_port != 0) {
758                 goto fail_bad_mask;
759         }
760
761         if (mask->hdr.dst_port == supp_mask.hdr.dst_port) {
762                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_LOC_PORT;
763                 efx_spec->efs_loc_port = rte_bswap16(spec->hdr.dst_port);
764         } else if (mask->hdr.dst_port != 0) {
765                 goto fail_bad_mask;
766         }
767
768         return 0;
769
770 fail_bad_mask:
771         rte_flow_error_set(error, EINVAL,
772                            RTE_FLOW_ERROR_TYPE_ITEM, item,
773                            "Bad mask in the UDP pattern item");
774         return -rte_errno;
775 }
776
777 /*
778  * Filters for encapsulated packets match based on the EtherType and IP
779  * protocol in the outer frame.
780  */
781 static int
782 sfc_flow_set_match_flags_for_encap_pkts(const struct rte_flow_item *item,
783                                         efx_filter_spec_t *efx_spec,
784                                         uint8_t ip_proto,
785                                         struct rte_flow_error *error)
786 {
787         if (!(efx_spec->efs_match_flags & EFX_FILTER_MATCH_IP_PROTO)) {
788                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_IP_PROTO;
789                 efx_spec->efs_ip_proto = ip_proto;
790         } else if (efx_spec->efs_ip_proto != ip_proto) {
791                 switch (ip_proto) {
792                 case EFX_IPPROTO_UDP:
793                         rte_flow_error_set(error, EINVAL,
794                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
795                                 "Outer IP header protocol must be UDP "
796                                 "in VxLAN/GENEVE pattern");
797                         return -rte_errno;
798
799                 case EFX_IPPROTO_GRE:
800                         rte_flow_error_set(error, EINVAL,
801                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
802                                 "Outer IP header protocol must be GRE "
803                                 "in NVGRE pattern");
804                         return -rte_errno;
805
806                 default:
807                         rte_flow_error_set(error, EINVAL,
808                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
809                                 "Only VxLAN/GENEVE/NVGRE tunneling patterns "
810                                 "are supported");
811                         return -rte_errno;
812                 }
813         }
814
815         if (efx_spec->efs_match_flags & EFX_FILTER_MATCH_ETHER_TYPE &&
816             efx_spec->efs_ether_type != EFX_ETHER_TYPE_IPV4 &&
817             efx_spec->efs_ether_type != EFX_ETHER_TYPE_IPV6) {
818                 rte_flow_error_set(error, EINVAL,
819                         RTE_FLOW_ERROR_TYPE_ITEM, item,
820                         "Outer frame EtherType in pattern with tunneling "
821                         "must be IPv4 or IPv6");
822                 return -rte_errno;
823         }
824
825         return 0;
826 }
827
828 static int
829 sfc_flow_set_efx_spec_vni_or_vsid(efx_filter_spec_t *efx_spec,
830                                   const uint8_t *vni_or_vsid_val,
831                                   const uint8_t *vni_or_vsid_mask,
832                                   const struct rte_flow_item *item,
833                                   struct rte_flow_error *error)
834 {
835         const uint8_t vni_or_vsid_full_mask[EFX_VNI_OR_VSID_LEN] = {
836                 0xff, 0xff, 0xff
837         };
838
839         if (memcmp(vni_or_vsid_mask, vni_or_vsid_full_mask,
840                    EFX_VNI_OR_VSID_LEN) == 0) {
841                 efx_spec->efs_match_flags |= EFX_FILTER_MATCH_VNI_OR_VSID;
842                 rte_memcpy(efx_spec->efs_vni_or_vsid, vni_or_vsid_val,
843                            EFX_VNI_OR_VSID_LEN);
844         } else if (!sfc_flow_is_zero(vni_or_vsid_mask, EFX_VNI_OR_VSID_LEN)) {
845                 rte_flow_error_set(error, EINVAL,
846                                    RTE_FLOW_ERROR_TYPE_ITEM, item,
847                                    "Unsupported VNI/VSID mask");
848                 return -rte_errno;
849         }
850
851         return 0;
852 }
853
854 /**
855  * Convert VXLAN item to EFX filter specification.
856  *
857  * @param item[in]
858  *   Item specification. Only VXLAN network identifier field is supported.
859  *   If the mask is NULL, default mask will be used.
860  *   Ranging is not supported.
861  * @param efx_spec[in, out]
862  *   EFX filter specification to update.
863  * @param[out] error
864  *   Perform verbose error reporting if not NULL.
865  */
866 static int
867 sfc_flow_parse_vxlan(const struct rte_flow_item *item,
868                      efx_filter_spec_t *efx_spec,
869                      struct rte_flow_error *error)
870 {
871         int rc;
872         const struct rte_flow_item_vxlan *spec = NULL;
873         const struct rte_flow_item_vxlan *mask = NULL;
874         const struct rte_flow_item_vxlan supp_mask = {
875                 .vni = { 0xff, 0xff, 0xff }
876         };
877
878         rc = sfc_flow_parse_init(item,
879                                  (const void **)&spec,
880                                  (const void **)&mask,
881                                  &supp_mask,
882                                  &rte_flow_item_vxlan_mask,
883                                  sizeof(struct rte_flow_item_vxlan),
884                                  error);
885         if (rc != 0)
886                 return rc;
887
888         rc = sfc_flow_set_match_flags_for_encap_pkts(item, efx_spec,
889                                                      EFX_IPPROTO_UDP, error);
890         if (rc != 0)
891                 return rc;
892
893         efx_spec->efs_encap_type = EFX_TUNNEL_PROTOCOL_VXLAN;
894         efx_spec->efs_match_flags |= EFX_FILTER_MATCH_ENCAP_TYPE;
895
896         if (spec == NULL)
897                 return 0;
898
899         rc = sfc_flow_set_efx_spec_vni_or_vsid(efx_spec, spec->vni,
900                                                mask->vni, item, error);
901
902         return rc;
903 }
904
905 /**
906  * Convert GENEVE item to EFX filter specification.
907  *
908  * @param item[in]
909  *   Item specification. Only Virtual Network Identifier and protocol type
910  *   fields are supported. But protocol type can be only Ethernet (0x6558).
911  *   If the mask is NULL, default mask will be used.
912  *   Ranging is not supported.
913  * @param efx_spec[in, out]
914  *   EFX filter specification to update.
915  * @param[out] error
916  *   Perform verbose error reporting if not NULL.
917  */
918 static int
919 sfc_flow_parse_geneve(const struct rte_flow_item *item,
920                       efx_filter_spec_t *efx_spec,
921                       struct rte_flow_error *error)
922 {
923         int rc;
924         const struct rte_flow_item_geneve *spec = NULL;
925         const struct rte_flow_item_geneve *mask = NULL;
926         const struct rte_flow_item_geneve supp_mask = {
927                 .protocol = RTE_BE16(0xffff),
928                 .vni = { 0xff, 0xff, 0xff }
929         };
930
931         rc = sfc_flow_parse_init(item,
932                                  (const void **)&spec,
933                                  (const void **)&mask,
934                                  &supp_mask,
935                                  &rte_flow_item_geneve_mask,
936                                  sizeof(struct rte_flow_item_geneve),
937                                  error);
938         if (rc != 0)
939                 return rc;
940
941         rc = sfc_flow_set_match_flags_for_encap_pkts(item, efx_spec,
942                                                      EFX_IPPROTO_UDP, error);
943         if (rc != 0)
944                 return rc;
945
946         efx_spec->efs_encap_type = EFX_TUNNEL_PROTOCOL_GENEVE;
947         efx_spec->efs_match_flags |= EFX_FILTER_MATCH_ENCAP_TYPE;
948
949         if (spec == NULL)
950                 return 0;
951
952         if (mask->protocol == supp_mask.protocol) {
953                 if (spec->protocol != rte_cpu_to_be_16(ETHER_TYPE_TEB)) {
954                         rte_flow_error_set(error, EINVAL,
955                                 RTE_FLOW_ERROR_TYPE_ITEM, item,
956                                 "GENEVE encap. protocol must be Ethernet "
957                                 "(0x6558) in the GENEVE pattern item");
958                         return -rte_errno;
959                 }
960         } else if (mask->protocol != 0) {
961                 rte_flow_error_set(error, EINVAL,
962                         RTE_FLOW_ERROR_TYPE_ITEM, item,
963                         "Unsupported mask for GENEVE encap. protocol");
964                 return -rte_errno;
965         }
966
967         rc = sfc_flow_set_efx_spec_vni_or_vsid(efx_spec, spec->vni,
968                                                mask->vni, item, error);
969
970         return rc;
971 }
972
973 /**
974  * Convert NVGRE item to EFX filter specification.
975  *
976  * @param item[in]
977  *   Item specification. Only virtual subnet ID field is supported.
978  *   If the mask is NULL, default mask will be used.
979  *   Ranging is not supported.
980  * @param efx_spec[in, out]
981  *   EFX filter specification to update.
982  * @param[out] error
983  *   Perform verbose error reporting if not NULL.
984  */
985 static int
986 sfc_flow_parse_nvgre(const struct rte_flow_item *item,
987                      efx_filter_spec_t *efx_spec,
988                      struct rte_flow_error *error)
989 {
990         int rc;
991         const struct rte_flow_item_nvgre *spec = NULL;
992         const struct rte_flow_item_nvgre *mask = NULL;
993         const struct rte_flow_item_nvgre supp_mask = {
994                 .tni = { 0xff, 0xff, 0xff }
995         };
996
997         rc = sfc_flow_parse_init(item,
998                                  (const void **)&spec,
999                                  (const void **)&mask,
1000                                  &supp_mask,
1001                                  &rte_flow_item_nvgre_mask,
1002                                  sizeof(struct rte_flow_item_nvgre),
1003                                  error);
1004         if (rc != 0)
1005                 return rc;
1006
1007         rc = sfc_flow_set_match_flags_for_encap_pkts(item, efx_spec,
1008                                                      EFX_IPPROTO_GRE, error);
1009         if (rc != 0)
1010                 return rc;
1011
1012         efx_spec->efs_encap_type = EFX_TUNNEL_PROTOCOL_NVGRE;
1013         efx_spec->efs_match_flags |= EFX_FILTER_MATCH_ENCAP_TYPE;
1014
1015         if (spec == NULL)
1016                 return 0;
1017
1018         rc = sfc_flow_set_efx_spec_vni_or_vsid(efx_spec, spec->tni,
1019                                                mask->tni, item, error);
1020
1021         return rc;
1022 }
1023
1024 static const struct sfc_flow_item sfc_flow_items[] = {
1025         {
1026                 .type = RTE_FLOW_ITEM_TYPE_VOID,
1027                 .prev_layer = SFC_FLOW_ITEM_ANY_LAYER,
1028                 .layer = SFC_FLOW_ITEM_ANY_LAYER,
1029                 .parse = sfc_flow_parse_void,
1030         },
1031         {
1032                 .type = RTE_FLOW_ITEM_TYPE_ETH,
1033                 .prev_layer = SFC_FLOW_ITEM_START_LAYER,
1034                 .layer = SFC_FLOW_ITEM_L2,
1035                 .parse = sfc_flow_parse_eth,
1036         },
1037         {
1038                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
1039                 .prev_layer = SFC_FLOW_ITEM_L2,
1040                 .layer = SFC_FLOW_ITEM_L2,
1041                 .parse = sfc_flow_parse_vlan,
1042         },
1043         {
1044                 .type = RTE_FLOW_ITEM_TYPE_IPV4,
1045                 .prev_layer = SFC_FLOW_ITEM_L2,
1046                 .layer = SFC_FLOW_ITEM_L3,
1047                 .parse = sfc_flow_parse_ipv4,
1048         },
1049         {
1050                 .type = RTE_FLOW_ITEM_TYPE_IPV6,
1051                 .prev_layer = SFC_FLOW_ITEM_L2,
1052                 .layer = SFC_FLOW_ITEM_L3,
1053                 .parse = sfc_flow_parse_ipv6,
1054         },
1055         {
1056                 .type = RTE_FLOW_ITEM_TYPE_TCP,
1057                 .prev_layer = SFC_FLOW_ITEM_L3,
1058                 .layer = SFC_FLOW_ITEM_L4,
1059                 .parse = sfc_flow_parse_tcp,
1060         },
1061         {
1062                 .type = RTE_FLOW_ITEM_TYPE_UDP,
1063                 .prev_layer = SFC_FLOW_ITEM_L3,
1064                 .layer = SFC_FLOW_ITEM_L4,
1065                 .parse = sfc_flow_parse_udp,
1066         },
1067         {
1068                 .type = RTE_FLOW_ITEM_TYPE_VXLAN,
1069                 .prev_layer = SFC_FLOW_ITEM_L4,
1070                 .layer = SFC_FLOW_ITEM_START_LAYER,
1071                 .parse = sfc_flow_parse_vxlan,
1072         },
1073         {
1074                 .type = RTE_FLOW_ITEM_TYPE_GENEVE,
1075                 .prev_layer = SFC_FLOW_ITEM_L4,
1076                 .layer = SFC_FLOW_ITEM_START_LAYER,
1077                 .parse = sfc_flow_parse_geneve,
1078         },
1079         {
1080                 .type = RTE_FLOW_ITEM_TYPE_NVGRE,
1081                 .prev_layer = SFC_FLOW_ITEM_L3,
1082                 .layer = SFC_FLOW_ITEM_START_LAYER,
1083                 .parse = sfc_flow_parse_nvgre,
1084         },
1085 };
1086
1087 /*
1088  * Protocol-independent flow API support
1089  */
1090 static int
1091 sfc_flow_parse_attr(const struct rte_flow_attr *attr,
1092                     struct rte_flow *flow,
1093                     struct rte_flow_error *error)
1094 {
1095         if (attr == NULL) {
1096                 rte_flow_error_set(error, EINVAL,
1097                                    RTE_FLOW_ERROR_TYPE_ATTR, NULL,
1098                                    "NULL attribute");
1099                 return -rte_errno;
1100         }
1101         if (attr->group != 0) {
1102                 rte_flow_error_set(error, ENOTSUP,
1103                                    RTE_FLOW_ERROR_TYPE_ATTR_GROUP, attr,
1104                                    "Groups are not supported");
1105                 return -rte_errno;
1106         }
1107         if (attr->priority != 0) {
1108                 rte_flow_error_set(error, ENOTSUP,
1109                                    RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY, attr,
1110                                    "Priorities are not supported");
1111                 return -rte_errno;
1112         }
1113         if (attr->egress != 0) {
1114                 rte_flow_error_set(error, ENOTSUP,
1115                                    RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, attr,
1116                                    "Egress is not supported");
1117                 return -rte_errno;
1118         }
1119         if (attr->transfer != 0) {
1120                 rte_flow_error_set(error, ENOTSUP,
1121                                    RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER, attr,
1122                                    "Transfer is not supported");
1123                 return -rte_errno;
1124         }
1125         if (attr->ingress == 0) {
1126                 rte_flow_error_set(error, ENOTSUP,
1127                                    RTE_FLOW_ERROR_TYPE_ATTR_INGRESS, attr,
1128                                    "Only ingress is supported");
1129                 return -rte_errno;
1130         }
1131
1132         flow->spec.template.efs_flags |= EFX_FILTER_FLAG_RX;
1133         flow->spec.template.efs_rss_context = EFX_RSS_CONTEXT_DEFAULT;
1134
1135         return 0;
1136 }
1137
1138 /* Get item from array sfc_flow_items */
1139 static const struct sfc_flow_item *
1140 sfc_flow_get_item(enum rte_flow_item_type type)
1141 {
1142         unsigned int i;
1143
1144         for (i = 0; i < RTE_DIM(sfc_flow_items); i++)
1145                 if (sfc_flow_items[i].type == type)
1146                         return &sfc_flow_items[i];
1147
1148         return NULL;
1149 }
1150
1151 static int
1152 sfc_flow_parse_pattern(const struct rte_flow_item pattern[],
1153                        struct rte_flow *flow,
1154                        struct rte_flow_error *error)
1155 {
1156         int rc;
1157         unsigned int prev_layer = SFC_FLOW_ITEM_ANY_LAYER;
1158         boolean_t is_ifrm = B_FALSE;
1159         const struct sfc_flow_item *item;
1160
1161         if (pattern == NULL) {
1162                 rte_flow_error_set(error, EINVAL,
1163                                    RTE_FLOW_ERROR_TYPE_ITEM_NUM, NULL,
1164                                    "NULL pattern");
1165                 return -rte_errno;
1166         }
1167
1168         for (; pattern->type != RTE_FLOW_ITEM_TYPE_END; pattern++) {
1169                 item = sfc_flow_get_item(pattern->type);
1170                 if (item == NULL) {
1171                         rte_flow_error_set(error, ENOTSUP,
1172                                            RTE_FLOW_ERROR_TYPE_ITEM, pattern,
1173                                            "Unsupported pattern item");
1174                         return -rte_errno;
1175                 }
1176
1177                 /*
1178                  * Omitting one or several protocol layers at the beginning
1179                  * of pattern is supported
1180                  */
1181                 if (item->prev_layer != SFC_FLOW_ITEM_ANY_LAYER &&
1182                     prev_layer != SFC_FLOW_ITEM_ANY_LAYER &&
1183                     item->prev_layer != prev_layer) {
1184                         rte_flow_error_set(error, ENOTSUP,
1185                                            RTE_FLOW_ERROR_TYPE_ITEM, pattern,
1186                                            "Unexpected sequence of pattern items");
1187                         return -rte_errno;
1188                 }
1189
1190                 /*
1191                  * Allow only VOID and ETH pattern items in the inner frame.
1192                  * Also check that there is only one tunneling protocol.
1193                  */
1194                 switch (item->type) {
1195                 case RTE_FLOW_ITEM_TYPE_VOID:
1196                 case RTE_FLOW_ITEM_TYPE_ETH:
1197                         break;
1198
1199                 case RTE_FLOW_ITEM_TYPE_VXLAN:
1200                 case RTE_FLOW_ITEM_TYPE_GENEVE:
1201                 case RTE_FLOW_ITEM_TYPE_NVGRE:
1202                         if (is_ifrm) {
1203                                 rte_flow_error_set(error, EINVAL,
1204                                         RTE_FLOW_ERROR_TYPE_ITEM,
1205                                         pattern,
1206                                         "More than one tunneling protocol");
1207                                 return -rte_errno;
1208                         }
1209                         is_ifrm = B_TRUE;
1210                         break;
1211
1212                 default:
1213                         if (is_ifrm) {
1214                                 rte_flow_error_set(error, EINVAL,
1215                                         RTE_FLOW_ERROR_TYPE_ITEM,
1216                                         pattern,
1217                                         "There is an unsupported pattern item "
1218                                         "in the inner frame");
1219                                 return -rte_errno;
1220                         }
1221                         break;
1222                 }
1223
1224                 rc = item->parse(pattern, &flow->spec.template, error);
1225                 if (rc != 0)
1226                         return rc;
1227
1228                 if (item->layer != SFC_FLOW_ITEM_ANY_LAYER)
1229                         prev_layer = item->layer;
1230         }
1231
1232         return 0;
1233 }
1234
1235 static int
1236 sfc_flow_parse_queue(struct sfc_adapter *sa,
1237                      const struct rte_flow_action_queue *queue,
1238                      struct rte_flow *flow)
1239 {
1240         struct sfc_rxq *rxq;
1241
1242         if (queue->index >= sa->rxq_count)
1243                 return -EINVAL;
1244
1245         rxq = sa->rxq_info[queue->index].rxq;
1246         flow->spec.template.efs_dmaq_id = (uint16_t)rxq->hw_index;
1247
1248         return 0;
1249 }
1250
1251 static int
1252 sfc_flow_parse_rss(struct sfc_adapter *sa,
1253                    const struct rte_flow_action_rss *action_rss,
1254                    struct rte_flow *flow)
1255 {
1256         struct sfc_rss *rss = &sa->rss;
1257         unsigned int rxq_sw_index;
1258         struct sfc_rxq *rxq;
1259         unsigned int rxq_hw_index_min;
1260         unsigned int rxq_hw_index_max;
1261         efx_rx_hash_type_t efx_hash_types;
1262         const uint8_t *rss_key;
1263         struct sfc_flow_rss *sfc_rss_conf = &flow->rss_conf;
1264         unsigned int i;
1265
1266         if (action_rss->queue_num == 0)
1267                 return -EINVAL;
1268
1269         rxq_sw_index = sa->rxq_count - 1;
1270         rxq = sa->rxq_info[rxq_sw_index].rxq;
1271         rxq_hw_index_min = rxq->hw_index;
1272         rxq_hw_index_max = 0;
1273
1274         for (i = 0; i < action_rss->queue_num; ++i) {
1275                 rxq_sw_index = action_rss->queue[i];
1276
1277                 if (rxq_sw_index >= sa->rxq_count)
1278                         return -EINVAL;
1279
1280                 rxq = sa->rxq_info[rxq_sw_index].rxq;
1281
1282                 if (rxq->hw_index < rxq_hw_index_min)
1283                         rxq_hw_index_min = rxq->hw_index;
1284
1285                 if (rxq->hw_index > rxq_hw_index_max)
1286                         rxq_hw_index_max = rxq->hw_index;
1287         }
1288
1289         switch (action_rss->func) {
1290         case RTE_ETH_HASH_FUNCTION_DEFAULT:
1291         case RTE_ETH_HASH_FUNCTION_TOEPLITZ:
1292                 break;
1293         default:
1294                 return -EINVAL;
1295         }
1296
1297         if (action_rss->level)
1298                 return -EINVAL;
1299
1300         if (action_rss->types) {
1301                 int rc;
1302
1303                 rc = sfc_rx_hf_rte_to_efx(sa, action_rss->types,
1304                                           &efx_hash_types);
1305                 if (rc != 0)
1306                         return -rc;
1307         } else {
1308                 unsigned int i;
1309
1310                 efx_hash_types = 0;
1311                 for (i = 0; i < rss->hf_map_nb_entries; ++i)
1312                         efx_hash_types |= rss->hf_map[i].efx;
1313         }
1314
1315         if (action_rss->key_len) {
1316                 if (action_rss->key_len != sizeof(rss->key))
1317                         return -EINVAL;
1318
1319                 rss_key = action_rss->key;
1320         } else {
1321                 rss_key = rss->key;
1322         }
1323
1324         flow->rss = B_TRUE;
1325
1326         sfc_rss_conf->rxq_hw_index_min = rxq_hw_index_min;
1327         sfc_rss_conf->rxq_hw_index_max = rxq_hw_index_max;
1328         sfc_rss_conf->rss_hash_types = efx_hash_types;
1329         rte_memcpy(sfc_rss_conf->rss_key, rss_key, sizeof(rss->key));
1330
1331         for (i = 0; i < RTE_DIM(sfc_rss_conf->rss_tbl); ++i) {
1332                 unsigned int nb_queues = action_rss->queue_num;
1333                 unsigned int rxq_sw_index = action_rss->queue[i % nb_queues];
1334                 struct sfc_rxq *rxq = sa->rxq_info[rxq_sw_index].rxq;
1335
1336                 sfc_rss_conf->rss_tbl[i] = rxq->hw_index - rxq_hw_index_min;
1337         }
1338
1339         return 0;
1340 }
1341
1342 static int
1343 sfc_flow_spec_flush(struct sfc_adapter *sa, struct sfc_flow_spec *spec,
1344                     unsigned int filters_count)
1345 {
1346         unsigned int i;
1347         int ret = 0;
1348
1349         for (i = 0; i < filters_count; i++) {
1350                 int rc;
1351
1352                 rc = efx_filter_remove(sa->nic, &spec->filters[i]);
1353                 if (ret == 0 && rc != 0) {
1354                         sfc_err(sa, "failed to remove filter specification "
1355                                 "(rc = %d)", rc);
1356                         ret = rc;
1357                 }
1358         }
1359
1360         return ret;
1361 }
1362
1363 static int
1364 sfc_flow_spec_insert(struct sfc_adapter *sa, struct sfc_flow_spec *spec)
1365 {
1366         unsigned int i;
1367         int rc = 0;
1368
1369         for (i = 0; i < spec->count; i++) {
1370                 rc = efx_filter_insert(sa->nic, &spec->filters[i]);
1371                 if (rc != 0) {
1372                         sfc_flow_spec_flush(sa, spec, i);
1373                         break;
1374                 }
1375         }
1376
1377         return rc;
1378 }
1379
1380 static int
1381 sfc_flow_spec_remove(struct sfc_adapter *sa, struct sfc_flow_spec *spec)
1382 {
1383         return sfc_flow_spec_flush(sa, spec, spec->count);
1384 }
1385
1386 static int
1387 sfc_flow_filter_insert(struct sfc_adapter *sa,
1388                        struct rte_flow *flow)
1389 {
1390         struct sfc_rss *rss = &sa->rss;
1391         struct sfc_flow_rss *flow_rss = &flow->rss_conf;
1392         uint32_t efs_rss_context = EFX_RSS_CONTEXT_DEFAULT;
1393         unsigned int i;
1394         int rc = 0;
1395
1396         if (flow->rss) {
1397                 unsigned int rss_spread = MIN(flow_rss->rxq_hw_index_max -
1398                                               flow_rss->rxq_hw_index_min + 1,
1399                                               EFX_MAXRSS);
1400
1401                 rc = efx_rx_scale_context_alloc(sa->nic,
1402                                                 EFX_RX_SCALE_EXCLUSIVE,
1403                                                 rss_spread,
1404                                                 &efs_rss_context);
1405                 if (rc != 0)
1406                         goto fail_scale_context_alloc;
1407
1408                 rc = efx_rx_scale_mode_set(sa->nic, efs_rss_context,
1409                                            rss->hash_alg,
1410                                            flow_rss->rss_hash_types, B_TRUE);
1411                 if (rc != 0)
1412                         goto fail_scale_mode_set;
1413
1414                 rc = efx_rx_scale_key_set(sa->nic, efs_rss_context,
1415                                           flow_rss->rss_key,
1416                                           sizeof(rss->key));
1417                 if (rc != 0)
1418                         goto fail_scale_key_set;
1419
1420                 /*
1421                  * At this point, fully elaborated filter specifications
1422                  * have been produced from the template. To make sure that
1423                  * RSS behaviour is consistent between them, set the same
1424                  * RSS context value everywhere.
1425                  */
1426                 for (i = 0; i < flow->spec.count; i++) {
1427                         efx_filter_spec_t *spec = &flow->spec.filters[i];
1428
1429                         spec->efs_rss_context = efs_rss_context;
1430                         spec->efs_dmaq_id = flow_rss->rxq_hw_index_min;
1431                         spec->efs_flags |= EFX_FILTER_FLAG_RX_RSS;
1432                 }
1433         }
1434
1435         rc = sfc_flow_spec_insert(sa, &flow->spec);
1436         if (rc != 0)
1437                 goto fail_filter_insert;
1438
1439         if (flow->rss) {
1440                 /*
1441                  * Scale table is set after filter insertion because
1442                  * the table entries are relative to the base RxQ ID
1443                  * and the latter is submitted to the HW by means of
1444                  * inserting a filter, so by the time of the request
1445                  * the HW knows all the information needed to verify
1446                  * the table entries, and the operation will succeed
1447                  */
1448                 rc = efx_rx_scale_tbl_set(sa->nic, efs_rss_context,
1449                                           flow_rss->rss_tbl,
1450                                           RTE_DIM(flow_rss->rss_tbl));
1451                 if (rc != 0)
1452                         goto fail_scale_tbl_set;
1453         }
1454
1455         return 0;
1456
1457 fail_scale_tbl_set:
1458         sfc_flow_spec_remove(sa, &flow->spec);
1459
1460 fail_filter_insert:
1461 fail_scale_key_set:
1462 fail_scale_mode_set:
1463         if (efs_rss_context != EFX_RSS_CONTEXT_DEFAULT)
1464                 efx_rx_scale_context_free(sa->nic, efs_rss_context);
1465
1466 fail_scale_context_alloc:
1467         return rc;
1468 }
1469
1470 static int
1471 sfc_flow_filter_remove(struct sfc_adapter *sa,
1472                        struct rte_flow *flow)
1473 {
1474         int rc = 0;
1475
1476         rc = sfc_flow_spec_remove(sa, &flow->spec);
1477         if (rc != 0)
1478                 return rc;
1479
1480         if (flow->rss) {
1481                 /*
1482                  * All specifications for a given flow rule have the same RSS
1483                  * context, so that RSS context value is taken from the first
1484                  * filter specification
1485                  */
1486                 efx_filter_spec_t *spec = &flow->spec.filters[0];
1487
1488                 rc = efx_rx_scale_context_free(sa->nic, spec->efs_rss_context);
1489         }
1490
1491         return rc;
1492 }
1493
1494 static int
1495 sfc_flow_parse_actions(struct sfc_adapter *sa,
1496                        const struct rte_flow_action actions[],
1497                        struct rte_flow *flow,
1498                        struct rte_flow_error *error)
1499 {
1500         int rc;
1501         boolean_t is_specified = B_FALSE;
1502
1503         if (actions == NULL) {
1504                 rte_flow_error_set(error, EINVAL,
1505                                    RTE_FLOW_ERROR_TYPE_ACTION_NUM, NULL,
1506                                    "NULL actions");
1507                 return -rte_errno;
1508         }
1509
1510         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1511                 /* This one may appear anywhere multiple times. */
1512                 if (actions->type == RTE_FLOW_ACTION_TYPE_VOID)
1513                         continue;
1514                 /* Fate-deciding actions may appear exactly once. */
1515                 if (is_specified) {
1516                         rte_flow_error_set
1517                                 (error, ENOTSUP, RTE_FLOW_ERROR_TYPE_ACTION,
1518                                  actions,
1519                                  "Cannot combine several fate-deciding actions,"
1520                                  "choose between QUEUE, RSS or DROP");
1521                         return -rte_errno;
1522                 }
1523                 switch (actions->type) {
1524                 case RTE_FLOW_ACTION_TYPE_QUEUE:
1525                         rc = sfc_flow_parse_queue(sa, actions->conf, flow);
1526                         if (rc != 0) {
1527                                 rte_flow_error_set(error, EINVAL,
1528                                         RTE_FLOW_ERROR_TYPE_ACTION, actions,
1529                                         "Bad QUEUE action");
1530                                 return -rte_errno;
1531                         }
1532
1533                         is_specified = B_TRUE;
1534                         break;
1535
1536                 case RTE_FLOW_ACTION_TYPE_RSS:
1537                         rc = sfc_flow_parse_rss(sa, actions->conf, flow);
1538                         if (rc != 0) {
1539                                 rte_flow_error_set(error, rc,
1540                                         RTE_FLOW_ERROR_TYPE_ACTION, actions,
1541                                         "Bad RSS action");
1542                                 return -rte_errno;
1543                         }
1544
1545                         is_specified = B_TRUE;
1546                         break;
1547
1548                 case RTE_FLOW_ACTION_TYPE_DROP:
1549                         flow->spec.template.efs_dmaq_id =
1550                                 EFX_FILTER_SPEC_RX_DMAQ_ID_DROP;
1551
1552                         is_specified = B_TRUE;
1553                         break;
1554
1555                 default:
1556                         rte_flow_error_set(error, ENOTSUP,
1557                                            RTE_FLOW_ERROR_TYPE_ACTION, actions,
1558                                            "Action is not supported");
1559                         return -rte_errno;
1560                 }
1561         }
1562
1563         /* When fate is unknown, drop traffic. */
1564         if (!is_specified) {
1565                 flow->spec.template.efs_dmaq_id =
1566                         EFX_FILTER_SPEC_RX_DMAQ_ID_DROP;
1567         }
1568
1569         return 0;
1570 }
1571
1572 /**
1573  * Set the EFX_FILTER_MATCH_UNKNOWN_UCAST_DST
1574  * and EFX_FILTER_MATCH_UNKNOWN_MCAST_DST match flags in the same
1575  * specifications after copying.
1576  *
1577  * @param spec[in, out]
1578  *   SFC flow specification to update.
1579  * @param filters_count_for_one_val[in]
1580  *   How many specifications should have the same match flag, what is the
1581  *   number of specifications before copying.
1582  * @param error[out]
1583  *   Perform verbose error reporting if not NULL.
1584  */
1585 static int
1586 sfc_flow_set_unknown_dst_flags(struct sfc_flow_spec *spec,
1587                                unsigned int filters_count_for_one_val,
1588                                struct rte_flow_error *error)
1589 {
1590         unsigned int i;
1591         static const efx_filter_match_flags_t vals[] = {
1592                 EFX_FILTER_MATCH_UNKNOWN_UCAST_DST,
1593                 EFX_FILTER_MATCH_UNKNOWN_MCAST_DST
1594         };
1595
1596         if (filters_count_for_one_val * RTE_DIM(vals) != spec->count) {
1597                 rte_flow_error_set(error, EINVAL,
1598                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1599                         "Number of specifications is incorrect while copying "
1600                         "by unknown destination flags");
1601                 return -rte_errno;
1602         }
1603
1604         for (i = 0; i < spec->count; i++) {
1605                 /* The check above ensures that divisor can't be zero here */
1606                 spec->filters[i].efs_match_flags |=
1607                         vals[i / filters_count_for_one_val];
1608         }
1609
1610         return 0;
1611 }
1612
1613 /**
1614  * Check that the following conditions are met:
1615  * - the list of supported filters has a filter
1616  *   with EFX_FILTER_MATCH_UNKNOWN_MCAST_DST flag instead of
1617  *   EFX_FILTER_MATCH_UNKNOWN_UCAST_DST, since this filter will also
1618  *   be inserted.
1619  *
1620  * @param match[in]
1621  *   The match flags of filter.
1622  * @param spec[in]
1623  *   Specification to be supplemented.
1624  * @param filter[in]
1625  *   SFC filter with list of supported filters.
1626  */
1627 static boolean_t
1628 sfc_flow_check_unknown_dst_flags(efx_filter_match_flags_t match,
1629                                  __rte_unused efx_filter_spec_t *spec,
1630                                  struct sfc_filter *filter)
1631 {
1632         unsigned int i;
1633         efx_filter_match_flags_t match_mcast_dst;
1634
1635         match_mcast_dst =
1636                 (match & ~EFX_FILTER_MATCH_UNKNOWN_UCAST_DST) |
1637                 EFX_FILTER_MATCH_UNKNOWN_MCAST_DST;
1638         for (i = 0; i < filter->supported_match_num; i++) {
1639                 if (match_mcast_dst == filter->supported_match[i])
1640                         return B_TRUE;
1641         }
1642
1643         return B_FALSE;
1644 }
1645
1646 /**
1647  * Set the EFX_FILTER_MATCH_ETHER_TYPE match flag and EFX_ETHER_TYPE_IPV4 and
1648  * EFX_ETHER_TYPE_IPV6 values of the corresponding field in the same
1649  * specifications after copying.
1650  *
1651  * @param spec[in, out]
1652  *   SFC flow specification to update.
1653  * @param filters_count_for_one_val[in]
1654  *   How many specifications should have the same EtherType value, what is the
1655  *   number of specifications before copying.
1656  * @param error[out]
1657  *   Perform verbose error reporting if not NULL.
1658  */
1659 static int
1660 sfc_flow_set_ethertypes(struct sfc_flow_spec *spec,
1661                         unsigned int filters_count_for_one_val,
1662                         struct rte_flow_error *error)
1663 {
1664         unsigned int i;
1665         static const uint16_t vals[] = {
1666                 EFX_ETHER_TYPE_IPV4, EFX_ETHER_TYPE_IPV6
1667         };
1668
1669         if (filters_count_for_one_val * RTE_DIM(vals) != spec->count) {
1670                 rte_flow_error_set(error, EINVAL,
1671                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1672                         "Number of specifications is incorrect "
1673                         "while copying by Ethertype");
1674                 return -rte_errno;
1675         }
1676
1677         for (i = 0; i < spec->count; i++) {
1678                 spec->filters[i].efs_match_flags |=
1679                         EFX_FILTER_MATCH_ETHER_TYPE;
1680
1681                 /*
1682                  * The check above ensures that
1683                  * filters_count_for_one_val is not 0
1684                  */
1685                 spec->filters[i].efs_ether_type =
1686                         vals[i / filters_count_for_one_val];
1687         }
1688
1689         return 0;
1690 }
1691
1692 /**
1693  * Set the EFX_FILTER_MATCH_IFRM_UNKNOWN_UCAST_DST and
1694  * EFX_FILTER_MATCH_IFRM_UNKNOWN_MCAST_DST match flags in the same
1695  * specifications after copying.
1696  *
1697  * @param spec[in, out]
1698  *   SFC flow specification to update.
1699  * @param filters_count_for_one_val[in]
1700  *   How many specifications should have the same match flag, what is the
1701  *   number of specifications before copying.
1702  * @param error[out]
1703  *   Perform verbose error reporting if not NULL.
1704  */
1705 static int
1706 sfc_flow_set_ifrm_unknown_dst_flags(struct sfc_flow_spec *spec,
1707                                     unsigned int filters_count_for_one_val,
1708                                     struct rte_flow_error *error)
1709 {
1710         unsigned int i;
1711         static const efx_filter_match_flags_t vals[] = {
1712                 EFX_FILTER_MATCH_IFRM_UNKNOWN_UCAST_DST,
1713                 EFX_FILTER_MATCH_IFRM_UNKNOWN_MCAST_DST
1714         };
1715
1716         if (filters_count_for_one_val * RTE_DIM(vals) != spec->count) {
1717                 rte_flow_error_set(error, EINVAL,
1718                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1719                         "Number of specifications is incorrect while copying "
1720                         "by inner frame unknown destination flags");
1721                 return -rte_errno;
1722         }
1723
1724         for (i = 0; i < spec->count; i++) {
1725                 /* The check above ensures that divisor can't be zero here */
1726                 spec->filters[i].efs_match_flags |=
1727                         vals[i / filters_count_for_one_val];
1728         }
1729
1730         return 0;
1731 }
1732
1733 /**
1734  * Check that the following conditions are met:
1735  * - the specification corresponds to a filter for encapsulated traffic
1736  * - the list of supported filters has a filter
1737  *   with EFX_FILTER_MATCH_IFRM_UNKNOWN_MCAST_DST flag instead of
1738  *   EFX_FILTER_MATCH_IFRM_UNKNOWN_UCAST_DST, since this filter will also
1739  *   be inserted.
1740  *
1741  * @param match[in]
1742  *   The match flags of filter.
1743  * @param spec[in]
1744  *   Specification to be supplemented.
1745  * @param filter[in]
1746  *   SFC filter with list of supported filters.
1747  */
1748 static boolean_t
1749 sfc_flow_check_ifrm_unknown_dst_flags(efx_filter_match_flags_t match,
1750                                       efx_filter_spec_t *spec,
1751                                       struct sfc_filter *filter)
1752 {
1753         unsigned int i;
1754         efx_tunnel_protocol_t encap_type = spec->efs_encap_type;
1755         efx_filter_match_flags_t match_mcast_dst;
1756
1757         if (encap_type == EFX_TUNNEL_PROTOCOL_NONE)
1758                 return B_FALSE;
1759
1760         match_mcast_dst =
1761                 (match & ~EFX_FILTER_MATCH_IFRM_UNKNOWN_UCAST_DST) |
1762                 EFX_FILTER_MATCH_IFRM_UNKNOWN_MCAST_DST;
1763         for (i = 0; i < filter->supported_match_num; i++) {
1764                 if (match_mcast_dst == filter->supported_match[i])
1765                         return B_TRUE;
1766         }
1767
1768         return B_FALSE;
1769 }
1770
1771 /*
1772  * Match flags that can be automatically added to filters.
1773  * Selecting the last minimum when searching for the copy flag ensures that the
1774  * EFX_FILTER_MATCH_UNKNOWN_UCAST_DST flag has a higher priority than
1775  * EFX_FILTER_MATCH_ETHER_TYPE. This is because the filter
1776  * EFX_FILTER_MATCH_UNKNOWN_UCAST_DST is at the end of the list of supported
1777  * filters.
1778  */
1779 static const struct sfc_flow_copy_flag sfc_flow_copy_flags[] = {
1780         {
1781                 .flag = EFX_FILTER_MATCH_UNKNOWN_UCAST_DST,
1782                 .vals_count = 2,
1783                 .set_vals = sfc_flow_set_unknown_dst_flags,
1784                 .spec_check = sfc_flow_check_unknown_dst_flags,
1785         },
1786         {
1787                 .flag = EFX_FILTER_MATCH_ETHER_TYPE,
1788                 .vals_count = 2,
1789                 .set_vals = sfc_flow_set_ethertypes,
1790                 .spec_check = NULL,
1791         },
1792         {
1793                 .flag = EFX_FILTER_MATCH_IFRM_UNKNOWN_UCAST_DST,
1794                 .vals_count = 2,
1795                 .set_vals = sfc_flow_set_ifrm_unknown_dst_flags,
1796                 .spec_check = sfc_flow_check_ifrm_unknown_dst_flags,
1797         },
1798 };
1799
1800 /* Get item from array sfc_flow_copy_flags */
1801 static const struct sfc_flow_copy_flag *
1802 sfc_flow_get_copy_flag(efx_filter_match_flags_t flag)
1803 {
1804         unsigned int i;
1805
1806         for (i = 0; i < RTE_DIM(sfc_flow_copy_flags); i++) {
1807                 if (sfc_flow_copy_flags[i].flag == flag)
1808                         return &sfc_flow_copy_flags[i];
1809         }
1810
1811         return NULL;
1812 }
1813
1814 /**
1815  * Make copies of the specifications, set match flag and values
1816  * of the field that corresponds to it.
1817  *
1818  * @param spec[in, out]
1819  *   SFC flow specification to update.
1820  * @param flag[in]
1821  *   The match flag to add.
1822  * @param error[out]
1823  *   Perform verbose error reporting if not NULL.
1824  */
1825 static int
1826 sfc_flow_spec_add_match_flag(struct sfc_flow_spec *spec,
1827                              efx_filter_match_flags_t flag,
1828                              struct rte_flow_error *error)
1829 {
1830         unsigned int i;
1831         unsigned int new_filters_count;
1832         unsigned int filters_count_for_one_val;
1833         const struct sfc_flow_copy_flag *copy_flag;
1834         int rc;
1835
1836         copy_flag = sfc_flow_get_copy_flag(flag);
1837         if (copy_flag == NULL) {
1838                 rte_flow_error_set(error, ENOTSUP,
1839                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1840                                    "Unsupported spec field for copying");
1841                 return -rte_errno;
1842         }
1843
1844         new_filters_count = spec->count * copy_flag->vals_count;
1845         if (new_filters_count > SF_FLOW_SPEC_NB_FILTERS_MAX) {
1846                 rte_flow_error_set(error, EINVAL,
1847                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1848                         "Too much EFX specifications in the flow rule");
1849                 return -rte_errno;
1850         }
1851
1852         /* Copy filters specifications */
1853         for (i = spec->count; i < new_filters_count; i++)
1854                 spec->filters[i] = spec->filters[i - spec->count];
1855
1856         filters_count_for_one_val = spec->count;
1857         spec->count = new_filters_count;
1858
1859         rc = copy_flag->set_vals(spec, filters_count_for_one_val, error);
1860         if (rc != 0)
1861                 return rc;
1862
1863         return 0;
1864 }
1865
1866 /**
1867  * Check that the given set of match flags missing in the original filter spec
1868  * could be covered by adding spec copies which specify the corresponding
1869  * flags and packet field values to match.
1870  *
1871  * @param miss_flags[in]
1872  *   Flags that are missing until the supported filter.
1873  * @param spec[in]
1874  *   Specification to be supplemented.
1875  * @param filter[in]
1876  *   SFC filter.
1877  *
1878  * @return
1879  *   Number of specifications after copy or 0, if the flags can not be added.
1880  */
1881 static unsigned int
1882 sfc_flow_check_missing_flags(efx_filter_match_flags_t miss_flags,
1883                              efx_filter_spec_t *spec,
1884                              struct sfc_filter *filter)
1885 {
1886         unsigned int i;
1887         efx_filter_match_flags_t copy_flags = 0;
1888         efx_filter_match_flags_t flag;
1889         efx_filter_match_flags_t match = spec->efs_match_flags | miss_flags;
1890         sfc_flow_spec_check *check;
1891         unsigned int multiplier = 1;
1892
1893         for (i = 0; i < RTE_DIM(sfc_flow_copy_flags); i++) {
1894                 flag = sfc_flow_copy_flags[i].flag;
1895                 check = sfc_flow_copy_flags[i].spec_check;
1896                 if ((flag & miss_flags) == flag) {
1897                         if (check != NULL && (!check(match, spec, filter)))
1898                                 continue;
1899
1900                         copy_flags |= flag;
1901                         multiplier *= sfc_flow_copy_flags[i].vals_count;
1902                 }
1903         }
1904
1905         if (copy_flags == miss_flags)
1906                 return multiplier;
1907
1908         return 0;
1909 }
1910
1911 /**
1912  * Attempt to supplement the specification template to the minimally
1913  * supported set of match flags. To do this, it is necessary to copy
1914  * the specifications, filling them with the values of fields that
1915  * correspond to the missing flags.
1916  * The necessary and sufficient filter is built from the fewest number
1917  * of copies which could be made to cover the minimally required set
1918  * of flags.
1919  *
1920  * @param sa[in]
1921  *   SFC adapter.
1922  * @param spec[in, out]
1923  *   SFC flow specification to update.
1924  * @param error[out]
1925  *   Perform verbose error reporting if not NULL.
1926  */
1927 static int
1928 sfc_flow_spec_filters_complete(struct sfc_adapter *sa,
1929                                struct sfc_flow_spec *spec,
1930                                struct rte_flow_error *error)
1931 {
1932         struct sfc_filter *filter = &sa->filter;
1933         efx_filter_match_flags_t miss_flags;
1934         efx_filter_match_flags_t min_miss_flags = 0;
1935         efx_filter_match_flags_t match;
1936         unsigned int min_multiplier = UINT_MAX;
1937         unsigned int multiplier;
1938         unsigned int i;
1939         int rc;
1940
1941         match = spec->template.efs_match_flags;
1942         for (i = 0; i < filter->supported_match_num; i++) {
1943                 if ((match & filter->supported_match[i]) == match) {
1944                         miss_flags = filter->supported_match[i] & (~match);
1945                         multiplier = sfc_flow_check_missing_flags(miss_flags,
1946                                 &spec->template, filter);
1947                         if (multiplier > 0) {
1948                                 if (multiplier <= min_multiplier) {
1949                                         min_multiplier = multiplier;
1950                                         min_miss_flags = miss_flags;
1951                                 }
1952                         }
1953                 }
1954         }
1955
1956         if (min_multiplier == UINT_MAX) {
1957                 rte_flow_error_set(error, ENOTSUP,
1958                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1959                                    "Flow rule pattern is not supported");
1960                 return -rte_errno;
1961         }
1962
1963         for (i = 0; i < RTE_DIM(sfc_flow_copy_flags); i++) {
1964                 efx_filter_match_flags_t flag = sfc_flow_copy_flags[i].flag;
1965
1966                 if ((flag & min_miss_flags) == flag) {
1967                         rc = sfc_flow_spec_add_match_flag(spec, flag, error);
1968                         if (rc != 0)
1969                                 return rc;
1970                 }
1971         }
1972
1973         return 0;
1974 }
1975
1976 /**
1977  * Check that set of match flags is referred to by a filter. Filter is
1978  * described by match flags with the ability to add OUTER_VID and INNER_VID
1979  * flags.
1980  *
1981  * @param match_flags[in]
1982  *   Set of match flags.
1983  * @param flags_pattern[in]
1984  *   Pattern of filter match flags.
1985  */
1986 static boolean_t
1987 sfc_flow_is_match_with_vids(efx_filter_match_flags_t match_flags,
1988                             efx_filter_match_flags_t flags_pattern)
1989 {
1990         if ((match_flags & flags_pattern) != flags_pattern)
1991                 return B_FALSE;
1992
1993         switch (match_flags & ~flags_pattern) {
1994         case 0:
1995         case EFX_FILTER_MATCH_OUTER_VID:
1996         case EFX_FILTER_MATCH_OUTER_VID | EFX_FILTER_MATCH_INNER_VID:
1997                 return B_TRUE;
1998         default:
1999                 return B_FALSE;
2000         }
2001 }
2002
2003 /**
2004  * Check whether the spec maps to a hardware filter which is known to be
2005  * ineffective despite being valid.
2006  *
2007  * @param spec[in]
2008  *   SFC flow specification.
2009  */
2010 static boolean_t
2011 sfc_flow_is_match_flags_exception(struct sfc_flow_spec *spec)
2012 {
2013         unsigned int i;
2014         uint16_t ether_type;
2015         uint8_t ip_proto;
2016         efx_filter_match_flags_t match_flags;
2017
2018         for (i = 0; i < spec->count; i++) {
2019                 match_flags = spec->filters[i].efs_match_flags;
2020
2021                 if (sfc_flow_is_match_with_vids(match_flags,
2022                                                 EFX_FILTER_MATCH_ETHER_TYPE) ||
2023                     sfc_flow_is_match_with_vids(match_flags,
2024                                                 EFX_FILTER_MATCH_ETHER_TYPE |
2025                                                 EFX_FILTER_MATCH_LOC_MAC)) {
2026                         ether_type = spec->filters[i].efs_ether_type;
2027                         if (ether_type == EFX_ETHER_TYPE_IPV4 ||
2028                             ether_type == EFX_ETHER_TYPE_IPV6)
2029                                 return B_TRUE;
2030                 } else if (sfc_flow_is_match_with_vids(match_flags,
2031                                 EFX_FILTER_MATCH_ETHER_TYPE |
2032                                 EFX_FILTER_MATCH_IP_PROTO) ||
2033                            sfc_flow_is_match_with_vids(match_flags,
2034                                 EFX_FILTER_MATCH_ETHER_TYPE |
2035                                 EFX_FILTER_MATCH_IP_PROTO |
2036                                 EFX_FILTER_MATCH_LOC_MAC)) {
2037                         ip_proto = spec->filters[i].efs_ip_proto;
2038                         if (ip_proto == EFX_IPPROTO_TCP ||
2039                             ip_proto == EFX_IPPROTO_UDP)
2040                                 return B_TRUE;
2041                 }
2042         }
2043
2044         return B_FALSE;
2045 }
2046
2047 static int
2048 sfc_flow_validate_match_flags(struct sfc_adapter *sa,
2049                               struct rte_flow *flow,
2050                               struct rte_flow_error *error)
2051 {
2052         efx_filter_spec_t *spec_tmpl = &flow->spec.template;
2053         efx_filter_match_flags_t match_flags = spec_tmpl->efs_match_flags;
2054         int rc;
2055
2056         /* Initialize the first filter spec with template */
2057         flow->spec.filters[0] = *spec_tmpl;
2058         flow->spec.count = 1;
2059
2060         if (!sfc_filter_is_match_supported(sa, match_flags)) {
2061                 rc = sfc_flow_spec_filters_complete(sa, &flow->spec, error);
2062                 if (rc != 0)
2063                         return rc;
2064         }
2065
2066         if (sfc_flow_is_match_flags_exception(&flow->spec)) {
2067                 rte_flow_error_set(error, ENOTSUP,
2068                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2069                         "The flow rule pattern is unsupported");
2070                 return -rte_errno;
2071         }
2072
2073         return 0;
2074 }
2075
2076 static int
2077 sfc_flow_parse(struct rte_eth_dev *dev,
2078                const struct rte_flow_attr *attr,
2079                const struct rte_flow_item pattern[],
2080                const struct rte_flow_action actions[],
2081                struct rte_flow *flow,
2082                struct rte_flow_error *error)
2083 {
2084         struct sfc_adapter *sa = dev->data->dev_private;
2085         int rc;
2086
2087         rc = sfc_flow_parse_attr(attr, flow, error);
2088         if (rc != 0)
2089                 goto fail_bad_value;
2090
2091         rc = sfc_flow_parse_pattern(pattern, flow, error);
2092         if (rc != 0)
2093                 goto fail_bad_value;
2094
2095         rc = sfc_flow_parse_actions(sa, actions, flow, error);
2096         if (rc != 0)
2097                 goto fail_bad_value;
2098
2099         rc = sfc_flow_validate_match_flags(sa, flow, error);
2100         if (rc != 0)
2101                 goto fail_bad_value;
2102
2103         return 0;
2104
2105 fail_bad_value:
2106         return rc;
2107 }
2108
2109 static int
2110 sfc_flow_validate(struct rte_eth_dev *dev,
2111                   const struct rte_flow_attr *attr,
2112                   const struct rte_flow_item pattern[],
2113                   const struct rte_flow_action actions[],
2114                   struct rte_flow_error *error)
2115 {
2116         struct rte_flow flow;
2117
2118         memset(&flow, 0, sizeof(flow));
2119
2120         return sfc_flow_parse(dev, attr, pattern, actions, &flow, error);
2121 }
2122
2123 static struct rte_flow *
2124 sfc_flow_create(struct rte_eth_dev *dev,
2125                 const struct rte_flow_attr *attr,
2126                 const struct rte_flow_item pattern[],
2127                 const struct rte_flow_action actions[],
2128                 struct rte_flow_error *error)
2129 {
2130         struct sfc_adapter *sa = dev->data->dev_private;
2131         struct rte_flow *flow = NULL;
2132         int rc;
2133
2134         flow = rte_zmalloc("sfc_rte_flow", sizeof(*flow), 0);
2135         if (flow == NULL) {
2136                 rte_flow_error_set(error, ENOMEM,
2137                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2138                                    "Failed to allocate memory");
2139                 goto fail_no_mem;
2140         }
2141
2142         rc = sfc_flow_parse(dev, attr, pattern, actions, flow, error);
2143         if (rc != 0)
2144                 goto fail_bad_value;
2145
2146         TAILQ_INSERT_TAIL(&sa->filter.flow_list, flow, entries);
2147
2148         sfc_adapter_lock(sa);
2149
2150         if (sa->state == SFC_ADAPTER_STARTED) {
2151                 rc = sfc_flow_filter_insert(sa, flow);
2152                 if (rc != 0) {
2153                         rte_flow_error_set(error, rc,
2154                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2155                                 "Failed to insert filter");
2156                         goto fail_filter_insert;
2157                 }
2158         }
2159
2160         sfc_adapter_unlock(sa);
2161
2162         return flow;
2163
2164 fail_filter_insert:
2165         TAILQ_REMOVE(&sa->filter.flow_list, flow, entries);
2166
2167 fail_bad_value:
2168         rte_free(flow);
2169         sfc_adapter_unlock(sa);
2170
2171 fail_no_mem:
2172         return NULL;
2173 }
2174
2175 static int
2176 sfc_flow_remove(struct sfc_adapter *sa,
2177                 struct rte_flow *flow,
2178                 struct rte_flow_error *error)
2179 {
2180         int rc = 0;
2181
2182         SFC_ASSERT(sfc_adapter_is_locked(sa));
2183
2184         if (sa->state == SFC_ADAPTER_STARTED) {
2185                 rc = sfc_flow_filter_remove(sa, flow);
2186                 if (rc != 0)
2187                         rte_flow_error_set(error, rc,
2188                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2189                                 "Failed to destroy flow rule");
2190         }
2191
2192         TAILQ_REMOVE(&sa->filter.flow_list, flow, entries);
2193         rte_free(flow);
2194
2195         return rc;
2196 }
2197
2198 static int
2199 sfc_flow_destroy(struct rte_eth_dev *dev,
2200                  struct rte_flow *flow,
2201                  struct rte_flow_error *error)
2202 {
2203         struct sfc_adapter *sa = dev->data->dev_private;
2204         struct rte_flow *flow_ptr;
2205         int rc = EINVAL;
2206
2207         sfc_adapter_lock(sa);
2208
2209         TAILQ_FOREACH(flow_ptr, &sa->filter.flow_list, entries) {
2210                 if (flow_ptr == flow)
2211                         rc = 0;
2212         }
2213         if (rc != 0) {
2214                 rte_flow_error_set(error, rc,
2215                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
2216                                    "Failed to find flow rule to destroy");
2217                 goto fail_bad_value;
2218         }
2219
2220         rc = sfc_flow_remove(sa, flow, error);
2221
2222 fail_bad_value:
2223         sfc_adapter_unlock(sa);
2224
2225         return -rc;
2226 }
2227
2228 static int
2229 sfc_flow_flush(struct rte_eth_dev *dev,
2230                struct rte_flow_error *error)
2231 {
2232         struct sfc_adapter *sa = dev->data->dev_private;
2233         struct rte_flow *flow;
2234         int rc = 0;
2235         int ret = 0;
2236
2237         sfc_adapter_lock(sa);
2238
2239         while ((flow = TAILQ_FIRST(&sa->filter.flow_list)) != NULL) {
2240                 rc = sfc_flow_remove(sa, flow, error);
2241                 if (rc != 0)
2242                         ret = rc;
2243         }
2244
2245         sfc_adapter_unlock(sa);
2246
2247         return -ret;
2248 }
2249
2250 static int
2251 sfc_flow_isolate(struct rte_eth_dev *dev, int enable,
2252                  struct rte_flow_error *error)
2253 {
2254         struct sfc_adapter *sa = dev->data->dev_private;
2255         struct sfc_port *port = &sa->port;
2256         int ret = 0;
2257
2258         sfc_adapter_lock(sa);
2259         if (sa->state != SFC_ADAPTER_INITIALIZED) {
2260                 rte_flow_error_set(error, EBUSY,
2261                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2262                                    NULL, "please close the port first");
2263                 ret = -rte_errno;
2264         } else {
2265                 port->isolated = (enable) ? B_TRUE : B_FALSE;
2266         }
2267         sfc_adapter_unlock(sa);
2268
2269         return ret;
2270 }
2271
2272 const struct rte_flow_ops sfc_flow_ops = {
2273         .validate = sfc_flow_validate,
2274         .create = sfc_flow_create,
2275         .destroy = sfc_flow_destroy,
2276         .flush = sfc_flow_flush,
2277         .query = NULL,
2278         .isolate = sfc_flow_isolate,
2279 };
2280
2281 void
2282 sfc_flow_init(struct sfc_adapter *sa)
2283 {
2284         SFC_ASSERT(sfc_adapter_is_locked(sa));
2285
2286         TAILQ_INIT(&sa->filter.flow_list);
2287 }
2288
2289 void
2290 sfc_flow_fini(struct sfc_adapter *sa)
2291 {
2292         struct rte_flow *flow;
2293
2294         SFC_ASSERT(sfc_adapter_is_locked(sa));
2295
2296         while ((flow = TAILQ_FIRST(&sa->filter.flow_list)) != NULL) {
2297                 TAILQ_REMOVE(&sa->filter.flow_list, flow, entries);
2298                 rte_free(flow);
2299         }
2300 }
2301
2302 void
2303 sfc_flow_stop(struct sfc_adapter *sa)
2304 {
2305         struct rte_flow *flow;
2306
2307         SFC_ASSERT(sfc_adapter_is_locked(sa));
2308
2309         TAILQ_FOREACH(flow, &sa->filter.flow_list, entries)
2310                 sfc_flow_filter_remove(sa, flow);
2311 }
2312
2313 int
2314 sfc_flow_start(struct sfc_adapter *sa)
2315 {
2316         struct rte_flow *flow;
2317         int rc = 0;
2318
2319         sfc_log_init(sa, "entry");
2320
2321         SFC_ASSERT(sfc_adapter_is_locked(sa));
2322
2323         TAILQ_FOREACH(flow, &sa->filter.flow_list, entries) {
2324                 rc = sfc_flow_filter_insert(sa, flow);
2325                 if (rc != 0)
2326                         goto fail_bad_flow;
2327         }
2328
2329         sfc_log_init(sa, "done");
2330
2331 fail_bad_flow:
2332         return rc;
2333 }