New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / net / softnic / rte_eth_softnic_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <rte_common.h>
9 #include <rte_byteorder.h>
10 #include <rte_malloc.h>
11 #include <rte_string_fns.h>
12 #include <rte_flow.h>
13 #include <rte_flow_driver.h>
14 #include <rte_tailq.h>
15
16 #include "rte_eth_softnic_internals.h"
17 #include "rte_eth_softnic.h"
18
19 #define rte_htons rte_cpu_to_be_16
20 #define rte_htonl rte_cpu_to_be_32
21
22 #define rte_ntohs rte_be_to_cpu_16
23 #define rte_ntohl rte_be_to_cpu_32
24
25 static struct rte_flow *
26 softnic_flow_find(struct softnic_table *table,
27         struct softnic_table_rule_match *rule_match)
28 {
29         struct rte_flow *flow;
30
31         TAILQ_FOREACH(flow, &table->flows, node)
32                 if (memcmp(&flow->match, rule_match, sizeof(*rule_match)) == 0)
33                         return flow;
34
35         return NULL;
36 }
37
38 int
39 flow_attr_map_set(struct pmd_internals *softnic,
40                 uint32_t group_id,
41                 int ingress,
42                 const char *pipeline_name,
43                 uint32_t table_id)
44 {
45         struct pipeline *pipeline;
46         struct flow_attr_map *map;
47
48         if (group_id >= SOFTNIC_FLOW_MAX_GROUPS ||
49                         pipeline_name == NULL)
50                 return -1;
51
52         pipeline = softnic_pipeline_find(softnic, pipeline_name);
53         if (pipeline == NULL ||
54                         table_id >= pipeline->n_tables)
55                 return -1;
56
57         map = (ingress) ? &softnic->flow.ingress_map[group_id] :
58                 &softnic->flow.egress_map[group_id];
59         strcpy(map->pipeline_name, pipeline_name);
60         map->table_id = table_id;
61         map->valid = 1;
62
63         return 0;
64 }
65
66 struct flow_attr_map *
67 flow_attr_map_get(struct pmd_internals *softnic,
68                 uint32_t group_id,
69                 int ingress)
70 {
71         if (group_id >= SOFTNIC_FLOW_MAX_GROUPS)
72                 return NULL;
73
74         return (ingress) ? &softnic->flow.ingress_map[group_id] :
75                 &softnic->flow.egress_map[group_id];
76 }
77
78 static int
79 flow_pipeline_table_get(struct pmd_internals *softnic,
80                 const struct rte_flow_attr *attr,
81                 const char **pipeline_name,
82                 uint32_t *table_id,
83                 struct rte_flow_error *error)
84 {
85         struct flow_attr_map *map;
86
87         if (attr == NULL)
88                 return rte_flow_error_set(error,
89                                 EINVAL,
90                                 RTE_FLOW_ERROR_TYPE_ATTR,
91                                 NULL,
92                                 "Null attr");
93
94         if (!attr->ingress && !attr->egress)
95                 return rte_flow_error_set(error,
96                                 EINVAL,
97                                 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
98                                 attr,
99                                 "Ingress/egress not specified");
100
101         if (attr->ingress && attr->egress)
102                 return rte_flow_error_set(error,
103                                 EINVAL,
104                                 RTE_FLOW_ERROR_TYPE_ATTR_INGRESS,
105                                 attr,
106                                 "Setting both ingress and egress is not allowed");
107
108         map = flow_attr_map_get(softnic,
109                         attr->group,
110                         attr->ingress);
111         if (map == NULL ||
112                         map->valid == 0)
113                 return rte_flow_error_set(error,
114                                 EINVAL,
115                                 RTE_FLOW_ERROR_TYPE_ATTR_GROUP,
116                                 attr,
117                                 "Invalid group ID");
118
119         if (pipeline_name)
120                 *pipeline_name = map->pipeline_name;
121
122         if (table_id)
123                 *table_id = map->table_id;
124
125         return 0;
126 }
127
128 union flow_item {
129         uint8_t raw[TABLE_RULE_MATCH_SIZE_MAX];
130         struct rte_flow_item_eth eth;
131         struct rte_flow_item_vlan vlan;
132         struct rte_flow_item_ipv4 ipv4;
133         struct rte_flow_item_ipv6 ipv6;
134         struct rte_flow_item_icmp icmp;
135         struct rte_flow_item_udp udp;
136         struct rte_flow_item_tcp tcp;
137         struct rte_flow_item_sctp sctp;
138         struct rte_flow_item_vxlan vxlan;
139         struct rte_flow_item_e_tag e_tag;
140         struct rte_flow_item_nvgre nvgre;
141         struct rte_flow_item_mpls mpls;
142         struct rte_flow_item_gre gre;
143         struct rte_flow_item_gtp gtp;
144         struct rte_flow_item_esp esp;
145         struct rte_flow_item_geneve geneve;
146         struct rte_flow_item_vxlan_gpe vxlan_gpe;
147         struct rte_flow_item_arp_eth_ipv4 arp_eth_ipv4;
148         struct rte_flow_item_ipv6_ext ipv6_ext;
149         struct rte_flow_item_icmp6 icmp6;
150         struct rte_flow_item_icmp6_nd_ns icmp6_nd_ns;
151         struct rte_flow_item_icmp6_nd_na icmp6_nd_na;
152         struct rte_flow_item_icmp6_nd_opt icmp6_nd_opt;
153         struct rte_flow_item_icmp6_nd_opt_sla_eth icmp6_nd_opt_sla_eth;
154         struct rte_flow_item_icmp6_nd_opt_tla_eth icmp6_nd_opt_tla_eth;
155 };
156
157 static const union flow_item flow_item_raw_mask;
158
159 static int
160 flow_item_is_proto(enum rte_flow_item_type type,
161         const void **mask,
162         size_t *size)
163 {
164         switch (type) {
165         case RTE_FLOW_ITEM_TYPE_RAW:
166                 *mask = &flow_item_raw_mask;
167                 *size = sizeof(flow_item_raw_mask);
168                 return 1; /* TRUE */
169
170         case RTE_FLOW_ITEM_TYPE_ETH:
171                 *mask = &rte_flow_item_eth_mask;
172                 *size = sizeof(struct rte_flow_item_eth);
173                 return 1; /* TRUE */
174
175         case RTE_FLOW_ITEM_TYPE_VLAN:
176                 *mask = &rte_flow_item_vlan_mask;
177                 *size = sizeof(struct rte_flow_item_vlan);
178                 return 1;
179
180         case RTE_FLOW_ITEM_TYPE_IPV4:
181                 *mask = &rte_flow_item_ipv4_mask;
182                 *size = sizeof(struct rte_flow_item_ipv4);
183                 return 1;
184
185         case RTE_FLOW_ITEM_TYPE_IPV6:
186                 *mask = &rte_flow_item_ipv6_mask;
187                 *size = sizeof(struct rte_flow_item_ipv6);
188                 return 1;
189
190         case RTE_FLOW_ITEM_TYPE_ICMP:
191                 *mask = &rte_flow_item_icmp_mask;
192                 *size = sizeof(struct rte_flow_item_icmp);
193                 return 1;
194
195         case RTE_FLOW_ITEM_TYPE_UDP:
196                 *mask = &rte_flow_item_udp_mask;
197                 *size = sizeof(struct rte_flow_item_udp);
198                 return 1;
199
200         case RTE_FLOW_ITEM_TYPE_TCP:
201                 *mask = &rte_flow_item_tcp_mask;
202                 *size = sizeof(struct rte_flow_item_tcp);
203                 return 1;
204
205         case RTE_FLOW_ITEM_TYPE_SCTP:
206                 *mask = &rte_flow_item_sctp_mask;
207                 *size = sizeof(struct rte_flow_item_sctp);
208                 return 1;
209
210         case RTE_FLOW_ITEM_TYPE_VXLAN:
211                 *mask = &rte_flow_item_vxlan_mask;
212                 *size = sizeof(struct rte_flow_item_vxlan);
213                 return 1;
214
215         case RTE_FLOW_ITEM_TYPE_E_TAG:
216                 *mask = &rte_flow_item_e_tag_mask;
217                 *size = sizeof(struct rte_flow_item_e_tag);
218                 return 1;
219
220         case RTE_FLOW_ITEM_TYPE_NVGRE:
221                 *mask = &rte_flow_item_nvgre_mask;
222                 *size = sizeof(struct rte_flow_item_nvgre);
223                 return 1;
224
225         case RTE_FLOW_ITEM_TYPE_MPLS:
226                 *mask = &rte_flow_item_mpls_mask;
227                 *size = sizeof(struct rte_flow_item_mpls);
228                 return 1;
229
230         case RTE_FLOW_ITEM_TYPE_GRE:
231                 *mask = &rte_flow_item_gre_mask;
232                 *size = sizeof(struct rte_flow_item_gre);
233                 return 1;
234
235         case RTE_FLOW_ITEM_TYPE_GTP:
236         case RTE_FLOW_ITEM_TYPE_GTPC:
237         case RTE_FLOW_ITEM_TYPE_GTPU:
238                 *mask = &rte_flow_item_gtp_mask;
239                 *size = sizeof(struct rte_flow_item_gtp);
240                 return 1;
241
242         case RTE_FLOW_ITEM_TYPE_ESP:
243                 *mask = &rte_flow_item_esp_mask;
244                 *size = sizeof(struct rte_flow_item_esp);
245                 return 1;
246
247         case RTE_FLOW_ITEM_TYPE_GENEVE:
248                 *mask = &rte_flow_item_geneve_mask;
249                 *size = sizeof(struct rte_flow_item_geneve);
250                 return 1;
251
252         case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
253                 *mask = &rte_flow_item_vxlan_gpe_mask;
254                 *size = sizeof(struct rte_flow_item_vxlan_gpe);
255                 return 1;
256
257         case RTE_FLOW_ITEM_TYPE_ARP_ETH_IPV4:
258                 *mask = &rte_flow_item_arp_eth_ipv4_mask;
259                 *size = sizeof(struct rte_flow_item_arp_eth_ipv4);
260                 return 1;
261
262         case RTE_FLOW_ITEM_TYPE_IPV6_EXT:
263                 *mask = &rte_flow_item_ipv6_ext_mask;
264                 *size = sizeof(struct rte_flow_item_ipv6_ext);
265                 return 1;
266
267         case RTE_FLOW_ITEM_TYPE_ICMP6:
268                 *mask = &rte_flow_item_icmp6_mask;
269                 *size = sizeof(struct rte_flow_item_icmp6);
270                 return 1;
271
272         case RTE_FLOW_ITEM_TYPE_ICMP6_ND_NS:
273                 *mask = &rte_flow_item_icmp6_nd_ns_mask;
274                 *size = sizeof(struct rte_flow_item_icmp6_nd_ns);
275                 return 1;
276
277         case RTE_FLOW_ITEM_TYPE_ICMP6_ND_NA:
278                 *mask = &rte_flow_item_icmp6_nd_na_mask;
279                 *size = sizeof(struct rte_flow_item_icmp6_nd_na);
280                 return 1;
281
282         case RTE_FLOW_ITEM_TYPE_ICMP6_ND_OPT:
283                 *mask = &rte_flow_item_icmp6_nd_opt_mask;
284                 *size = sizeof(struct rte_flow_item_icmp6_nd_opt);
285                 return 1;
286
287         case RTE_FLOW_ITEM_TYPE_ICMP6_ND_OPT_SLA_ETH:
288                 *mask = &rte_flow_item_icmp6_nd_opt_sla_eth_mask;
289                 *size = sizeof(struct rte_flow_item_icmp6_nd_opt_sla_eth);
290                 return 1;
291
292         case RTE_FLOW_ITEM_TYPE_ICMP6_ND_OPT_TLA_ETH:
293                 *mask = &rte_flow_item_icmp6_nd_opt_tla_eth_mask;
294                 *size = sizeof(struct rte_flow_item_icmp6_nd_opt_tla_eth);
295                 return 1;
296
297         default: return 0; /* FALSE */
298         }
299 }
300
301 static int
302 flow_item_raw_preprocess(const struct rte_flow_item *item,
303         union flow_item *item_spec,
304         union flow_item *item_mask,
305         size_t *item_size,
306         int *item_disabled,
307         struct rte_flow_error *error)
308 {
309         const struct rte_flow_item_raw *item_raw_spec = item->spec;
310         const struct rte_flow_item_raw *item_raw_mask = item->mask;
311         const uint8_t *pattern;
312         const uint8_t *pattern_mask;
313         uint8_t *spec = (uint8_t *)item_spec;
314         uint8_t *mask = (uint8_t *)item_mask;
315         size_t pattern_length, pattern_offset, i;
316         int disabled;
317
318         if (!item->spec)
319                 return rte_flow_error_set(error,
320                         ENOTSUP,
321                         RTE_FLOW_ERROR_TYPE_ITEM,
322                         item,
323                         "RAW: Null specification");
324
325         if (item->last)
326                 return rte_flow_error_set(error,
327                         ENOTSUP,
328                         RTE_FLOW_ERROR_TYPE_ITEM,
329                         item,
330                         "RAW: Range not allowed (last must be NULL)");
331
332         if (item_raw_spec->relative == 0)
333                 return rte_flow_error_set(error,
334                         ENOTSUP,
335                         RTE_FLOW_ERROR_TYPE_ITEM,
336                         item,
337                         "RAW: Absolute offset not supported");
338
339         if (item_raw_spec->search)
340                 return rte_flow_error_set(error,
341                         ENOTSUP,
342                         RTE_FLOW_ERROR_TYPE_ITEM,
343                         item,
344                         "RAW: Search not supported");
345
346         if (item_raw_spec->offset < 0)
347                 return rte_flow_error_set(error,
348                         ENOTSUP, RTE_FLOW_ERROR_TYPE_ITEM,
349                         item,
350                         "RAW: Negative offset not supported");
351
352         if (item_raw_spec->length == 0)
353                 return rte_flow_error_set(error,
354                         ENOTSUP,
355                         RTE_FLOW_ERROR_TYPE_ITEM,
356                         item,
357                         "RAW: Zero pattern length");
358
359         if (item_raw_spec->offset + item_raw_spec->length >
360                 TABLE_RULE_MATCH_SIZE_MAX)
361                 return rte_flow_error_set(error,
362                         ENOTSUP,
363                         RTE_FLOW_ERROR_TYPE_ITEM,
364                         item,
365                         "RAW: Item too big");
366
367         if (!item_raw_spec->pattern && item_raw_mask && item_raw_mask->pattern)
368                 return rte_flow_error_set(error,
369                         ENOTSUP,
370                         RTE_FLOW_ERROR_TYPE_ITEM,
371                         item,
372                         "RAW: Non-NULL pattern mask not allowed with NULL pattern");
373
374         pattern = item_raw_spec->pattern;
375         pattern_mask = (item_raw_mask) ? item_raw_mask->pattern : NULL;
376         pattern_length = (size_t)item_raw_spec->length;
377         pattern_offset = (size_t)item_raw_spec->offset;
378
379         disabled = 0;
380         if (pattern_mask == NULL)
381                 disabled = 1;
382         else
383                 for (i = 0; i < pattern_length; i++)
384                         if ((pattern)[i])
385                                 disabled = 1;
386
387         memset(spec, 0, TABLE_RULE_MATCH_SIZE_MAX);
388         if (pattern)
389                 memcpy(&spec[pattern_offset], pattern, pattern_length);
390
391         memset(mask, 0, TABLE_RULE_MATCH_SIZE_MAX);
392         if (pattern_mask)
393                 memcpy(&mask[pattern_offset], pattern_mask, pattern_length);
394
395         *item_size = pattern_offset + pattern_length;
396         *item_disabled = disabled;
397
398         return 0;
399 }
400
401 static int
402 flow_item_proto_preprocess(const struct rte_flow_item *item,
403         union flow_item *item_spec,
404         union flow_item *item_mask,
405         size_t *item_size,
406         int *item_disabled,
407         struct rte_flow_error *error)
408 {
409         const void *mask_default;
410         uint8_t *spec = (uint8_t *)item_spec;
411         uint8_t *mask = (uint8_t *)item_mask;
412         size_t size, i;
413
414         if (!flow_item_is_proto(item->type, &mask_default, &size))
415                 return rte_flow_error_set(error,
416                         ENOTSUP,
417                         RTE_FLOW_ERROR_TYPE_ITEM,
418                         item,
419                         "Item type not supported");
420
421         if (item->type == RTE_FLOW_ITEM_TYPE_RAW)
422                 return flow_item_raw_preprocess(item,
423                         item_spec,
424                         item_mask,
425                         item_size,
426                         item_disabled,
427                         error);
428
429         /* spec */
430         if (!item->spec) {
431                 /* If spec is NULL, then last and mask also have to be NULL. */
432                 if (item->last || item->mask)
433                         return rte_flow_error_set(error,
434                                 EINVAL,
435                                 RTE_FLOW_ERROR_TYPE_ITEM,
436                                 item,
437                                 "Invalid item (NULL spec with non-NULL last or mask)");
438
439                 memset(item_spec, 0, size);
440                 memset(item_mask, 0, size);
441                 *item_size = size;
442                 *item_disabled = 1; /* TRUE */
443                 return 0;
444         }
445
446         memcpy(spec, item->spec, size);
447         *item_size = size;
448
449         /* mask */
450         if (item->mask)
451                 memcpy(mask, item->mask, size);
452         else
453                 memcpy(mask, mask_default, size);
454
455         /* disabled */
456         for (i = 0; i < size; i++)
457                 if (mask[i])
458                         break;
459         *item_disabled = (i == size) ? 1 : 0;
460
461         /* Apply mask over spec. */
462         for (i = 0; i < size; i++)
463                 spec[i] &= mask[i];
464
465         /* last */
466         if (item->last) {
467                 uint8_t last[size];
468
469                 /* init last */
470                 memcpy(last, item->last, size);
471                 for (i = 0; i < size; i++)
472                         last[i] &= mask[i];
473
474                 /* check for range */
475                 for (i = 0; i < size; i++)
476                         if (last[i] != spec[i])
477                                 return rte_flow_error_set(error,
478                                         ENOTSUP,
479                                         RTE_FLOW_ERROR_TYPE_ITEM,
480                                         item,
481                                         "Range not supported");
482         }
483
484         return 0;
485 }
486
487 /***
488  * Skip disabled protocol items and VOID items
489  * until any of the mutually exclusive conditions
490  * from the list below takes place:
491  *    (A) A protocol present in the proto_mask
492  *        is met (either ENABLED or DISABLED);
493  *    (B) A protocol NOT present in the proto_mask is met in ENABLED state;
494  *    (C) The END item is met.
495  */
496 static int
497 flow_item_skip_disabled_protos(const struct rte_flow_item **item,
498         uint64_t proto_mask,
499         size_t *length,
500         struct rte_flow_error *error)
501 {
502         size_t len = 0;
503
504         for ( ; (*item)->type != RTE_FLOW_ITEM_TYPE_END; (*item)++) {
505                 union flow_item spec, mask;
506                 size_t size;
507                 int disabled = 0, status;
508
509                 if ((*item)->type == RTE_FLOW_ITEM_TYPE_VOID)
510                         continue;
511
512                 status = flow_item_proto_preprocess(*item,
513                                 &spec,
514                                 &mask,
515                                 &size,
516                                 &disabled,
517                                 error);
518                 if (status)
519                         return status;
520
521                 if ((proto_mask & (1LLU << (*item)->type)) ||
522                                 !disabled)
523                         break;
524
525                 len += size;
526         }
527
528         if (length)
529                 *length = len;
530
531         return 0;
532 }
533
534 #define FLOW_ITEM_PROTO_IP \
535         ((1LLU << RTE_FLOW_ITEM_TYPE_IPV4) | \
536          (1LLU << RTE_FLOW_ITEM_TYPE_IPV6))
537
538 static void
539 flow_item_skip_void(const struct rte_flow_item **item)
540 {
541         for ( ; ; (*item)++)
542                 if ((*item)->type != RTE_FLOW_ITEM_TYPE_VOID)
543                         return;
544 }
545
546 #define IP_PROTOCOL_TCP 0x06
547 #define IP_PROTOCOL_UDP 0x11
548 #define IP_PROTOCOL_SCTP 0x84
549
550 static int
551 mask_to_depth(uint64_t mask,
552                 uint32_t *depth)
553 {
554         uint64_t n;
555
556         if (mask == UINT64_MAX) {
557                 if (depth)
558                         *depth = 64;
559
560                 return 0;
561         }
562
563         mask = ~mask;
564
565         if (mask & (mask + 1))
566                 return -1;
567
568         n = __builtin_popcountll(mask);
569         if (depth)
570                 *depth = (uint32_t)(64 - n);
571
572         return 0;
573 }
574
575 static int
576 ipv4_mask_to_depth(uint32_t mask,
577                 uint32_t *depth)
578 {
579         uint32_t d;
580         int status;
581
582         status = mask_to_depth(mask | (UINT64_MAX << 32), &d);
583         if (status)
584                 return status;
585
586         d -= 32;
587         if (depth)
588                 *depth = d;
589
590         return 0;
591 }
592
593 static int
594 ipv6_mask_to_depth(uint8_t *mask,
595         uint32_t *depth)
596 {
597         uint64_t *m = (uint64_t *)mask;
598         uint64_t m0 = rte_be_to_cpu_64(m[0]);
599         uint64_t m1 = rte_be_to_cpu_64(m[1]);
600         uint32_t d0, d1;
601         int status;
602
603         status = mask_to_depth(m0, &d0);
604         if (status)
605                 return status;
606
607         status = mask_to_depth(m1, &d1);
608         if (status)
609                 return status;
610
611         if (d0 < 64 && d1)
612                 return -1;
613
614         if (depth)
615                 *depth = d0 + d1;
616
617         return 0;
618 }
619
620 static int
621 port_mask_to_range(uint16_t port,
622         uint16_t port_mask,
623         uint16_t *port0,
624         uint16_t *port1)
625 {
626         int status;
627         uint16_t p0, p1;
628
629         status = mask_to_depth(port_mask | (UINT64_MAX << 16), NULL);
630         if (status)
631                 return -1;
632
633         p0 = port & port_mask;
634         p1 = p0 | ~port_mask;
635
636         if (port0)
637                 *port0 = p0;
638
639         if (port1)
640                 *port1 = p1;
641
642         return 0;
643 }
644
645 static int
646 flow_rule_match_acl_get(struct pmd_internals *softnic __rte_unused,
647                 struct pipeline *pipeline __rte_unused,
648                 struct softnic_table *table __rte_unused,
649                 const struct rte_flow_attr *attr,
650                 const struct rte_flow_item *item,
651                 struct softnic_table_rule_match *rule_match,
652                 struct rte_flow_error *error)
653 {
654         union flow_item spec, mask;
655         size_t size, length = 0;
656         int disabled = 0, status;
657         uint8_t ip_proto, ip_proto_mask;
658
659         memset(rule_match, 0, sizeof(*rule_match));
660         rule_match->match_type = TABLE_ACL;
661         rule_match->match.acl.priority = attr->priority;
662
663         /* VOID or disabled protos only, if any. */
664         status = flow_item_skip_disabled_protos(&item,
665                         FLOW_ITEM_PROTO_IP, &length, error);
666         if (status)
667                 return status;
668
669         /* IP only. */
670         status = flow_item_proto_preprocess(item, &spec, &mask,
671                         &size, &disabled, error);
672         if (status)
673                 return status;
674
675         switch (item->type) {
676         case RTE_FLOW_ITEM_TYPE_IPV4:
677         {
678                 uint32_t sa_depth, da_depth;
679
680                 status = ipv4_mask_to_depth(rte_ntohl(mask.ipv4.hdr.src_addr),
681                                 &sa_depth);
682                 if (status)
683                         return rte_flow_error_set(error,
684                                 EINVAL,
685                                 RTE_FLOW_ERROR_TYPE_ITEM,
686                                 item,
687                                 "ACL: Illegal IPv4 header source address mask");
688
689                 status = ipv4_mask_to_depth(rte_ntohl(mask.ipv4.hdr.dst_addr),
690                                 &da_depth);
691                 if (status)
692                         return rte_flow_error_set(error,
693                                 EINVAL,
694                                 RTE_FLOW_ERROR_TYPE_ITEM,
695                                 item,
696                                 "ACL: Illegal IPv4 header destination address mask");
697
698                 ip_proto = spec.ipv4.hdr.next_proto_id;
699                 ip_proto_mask = mask.ipv4.hdr.next_proto_id;
700
701                 rule_match->match.acl.ip_version = 1;
702                 rule_match->match.acl.ipv4.sa =
703                         rte_ntohl(spec.ipv4.hdr.src_addr);
704                 rule_match->match.acl.ipv4.da =
705                         rte_ntohl(spec.ipv4.hdr.dst_addr);
706                 rule_match->match.acl.sa_depth = sa_depth;
707                 rule_match->match.acl.da_depth = da_depth;
708                 rule_match->match.acl.proto = ip_proto;
709                 rule_match->match.acl.proto_mask = ip_proto_mask;
710                 break;
711         } /* RTE_FLOW_ITEM_TYPE_IPV4 */
712
713         case RTE_FLOW_ITEM_TYPE_IPV6:
714         {
715                 uint32_t sa_depth, da_depth;
716
717                 status = ipv6_mask_to_depth(mask.ipv6.hdr.src_addr, &sa_depth);
718                 if (status)
719                         return rte_flow_error_set(error,
720                                 EINVAL,
721                                 RTE_FLOW_ERROR_TYPE_ITEM,
722                                 item,
723                                 "ACL: Illegal IPv6 header source address mask");
724
725                 status = ipv6_mask_to_depth(mask.ipv6.hdr.dst_addr, &da_depth);
726                 if (status)
727                         return rte_flow_error_set(error,
728                                 EINVAL,
729                                 RTE_FLOW_ERROR_TYPE_ITEM,
730                                 item,
731                                 "ACL: Illegal IPv6 header destination address mask");
732
733                 ip_proto = spec.ipv6.hdr.proto;
734                 ip_proto_mask = mask.ipv6.hdr.proto;
735
736                 rule_match->match.acl.ip_version = 0;
737                 memcpy(rule_match->match.acl.ipv6.sa,
738                         spec.ipv6.hdr.src_addr,
739                         sizeof(spec.ipv6.hdr.src_addr));
740                 memcpy(rule_match->match.acl.ipv6.da,
741                         spec.ipv6.hdr.dst_addr,
742                         sizeof(spec.ipv6.hdr.dst_addr));
743                 rule_match->match.acl.sa_depth = sa_depth;
744                 rule_match->match.acl.da_depth = da_depth;
745                 rule_match->match.acl.proto = ip_proto;
746                 rule_match->match.acl.proto_mask = ip_proto_mask;
747                 break;
748         } /* RTE_FLOW_ITEM_TYPE_IPV6 */
749
750         default:
751                 return rte_flow_error_set(error,
752                         ENOTSUP,
753                         RTE_FLOW_ERROR_TYPE_ITEM,
754                         item,
755                         "ACL: IP protocol required");
756         } /* switch */
757
758         if (ip_proto_mask != UINT8_MAX)
759                 return rte_flow_error_set(error,
760                         EINVAL,
761                         RTE_FLOW_ERROR_TYPE_ITEM,
762                         item,
763                         "ACL: Illegal IP protocol mask");
764
765         item++;
766
767         /* VOID only, if any. */
768         flow_item_skip_void(&item);
769
770         /* TCP/UDP/SCTP only. */
771         status = flow_item_proto_preprocess(item, &spec, &mask,
772                         &size, &disabled, error);
773         if (status)
774                 return status;
775
776         switch (item->type) {
777         case RTE_FLOW_ITEM_TYPE_TCP:
778         {
779                 uint16_t sp0, sp1, dp0, dp1;
780
781                 if (ip_proto != IP_PROTOCOL_TCP)
782                         return rte_flow_error_set(error,
783                                 EINVAL,
784                                 RTE_FLOW_ERROR_TYPE_ITEM,
785                                 item,
786                                 "ACL: Item type is TCP, but IP protocol is not");
787
788                 status = port_mask_to_range(rte_ntohs(spec.tcp.hdr.src_port),
789                                 rte_ntohs(mask.tcp.hdr.src_port),
790                                 &sp0,
791                                 &sp1);
792
793                 if (status)
794                         return rte_flow_error_set(error,
795                                 EINVAL,
796                                 RTE_FLOW_ERROR_TYPE_ITEM,
797                                 item,
798                                 "ACL: Illegal TCP source port mask");
799
800                 status = port_mask_to_range(rte_ntohs(spec.tcp.hdr.dst_port),
801                                 rte_ntohs(mask.tcp.hdr.dst_port),
802                                 &dp0,
803                                 &dp1);
804
805                 if (status)
806                         return rte_flow_error_set(error,
807                                 EINVAL,
808                                 RTE_FLOW_ERROR_TYPE_ITEM,
809                                 item,
810                                 "ACL: Illegal TCP destination port mask");
811
812                 rule_match->match.acl.sp0 = sp0;
813                 rule_match->match.acl.sp1 = sp1;
814                 rule_match->match.acl.dp0 = dp0;
815                 rule_match->match.acl.dp1 = dp1;
816
817                 break;
818         } /* RTE_FLOW_ITEM_TYPE_TCP */
819
820         case RTE_FLOW_ITEM_TYPE_UDP:
821         {
822                 uint16_t sp0, sp1, dp0, dp1;
823
824                 if (ip_proto != IP_PROTOCOL_UDP)
825                         return rte_flow_error_set(error,
826                                 EINVAL,
827                                 RTE_FLOW_ERROR_TYPE_ITEM,
828                                 item,
829                                 "ACL: Item type is UDP, but IP protocol is not");
830
831                 status = port_mask_to_range(rte_ntohs(spec.udp.hdr.src_port),
832                         rte_ntohs(mask.udp.hdr.src_port),
833                         &sp0,
834                         &sp1);
835                 if (status)
836                         return rte_flow_error_set(error,
837                                 EINVAL,
838                                 RTE_FLOW_ERROR_TYPE_ITEM,
839                                 item,
840                                 "ACL: Illegal UDP source port mask");
841
842                 status = port_mask_to_range(rte_ntohs(spec.udp.hdr.dst_port),
843                         rte_ntohs(mask.udp.hdr.dst_port),
844                         &dp0,
845                         &dp1);
846                 if (status)
847                         return rte_flow_error_set(error,
848                                 EINVAL,
849                                 RTE_FLOW_ERROR_TYPE_ITEM,
850                                 item,
851                                 "ACL: Illegal UDP destination port mask");
852
853                 rule_match->match.acl.sp0 = sp0;
854                 rule_match->match.acl.sp1 = sp1;
855                 rule_match->match.acl.dp0 = dp0;
856                 rule_match->match.acl.dp1 = dp1;
857
858                 break;
859         } /* RTE_FLOW_ITEM_TYPE_UDP */
860
861         case RTE_FLOW_ITEM_TYPE_SCTP:
862         {
863                 uint16_t sp0, sp1, dp0, dp1;
864
865                 if (ip_proto != IP_PROTOCOL_SCTP)
866                         return rte_flow_error_set(error,
867                                 EINVAL,
868                                 RTE_FLOW_ERROR_TYPE_ITEM,
869                                 item,
870                                 "ACL: Item type is SCTP, but IP protocol is not");
871
872                 status = port_mask_to_range(rte_ntohs(spec.sctp.hdr.src_port),
873                         rte_ntohs(mask.sctp.hdr.src_port),
874                         &sp0,
875                         &sp1);
876
877                 if (status)
878                         return rte_flow_error_set(error,
879                                 EINVAL,
880                                 RTE_FLOW_ERROR_TYPE_ITEM,
881                                 item,
882                                 "ACL: Illegal SCTP source port mask");
883
884                 status = port_mask_to_range(rte_ntohs(spec.sctp.hdr.dst_port),
885                         rte_ntohs(mask.sctp.hdr.dst_port),
886                         &dp0,
887                         &dp1);
888                 if (status)
889                         return rte_flow_error_set(error,
890                                 EINVAL,
891                                 RTE_FLOW_ERROR_TYPE_ITEM,
892                                 item,
893                                 "ACL: Illegal SCTP destination port mask");
894
895                 rule_match->match.acl.sp0 = sp0;
896                 rule_match->match.acl.sp1 = sp1;
897                 rule_match->match.acl.dp0 = dp0;
898                 rule_match->match.acl.dp1 = dp1;
899
900                 break;
901         } /* RTE_FLOW_ITEM_TYPE_SCTP */
902
903         default:
904                 return rte_flow_error_set(error,
905                         ENOTSUP,
906                         RTE_FLOW_ERROR_TYPE_ITEM,
907                         item,
908                         "ACL: TCP/UDP/SCTP required");
909         } /* switch */
910
911         item++;
912
913         /* VOID or disabled protos only, if any. */
914         status = flow_item_skip_disabled_protos(&item, 0, NULL, error);
915         if (status)
916                 return status;
917
918         /* END only. */
919         if (item->type != RTE_FLOW_ITEM_TYPE_END)
920                 return rte_flow_error_set(error,
921                         EINVAL,
922                         RTE_FLOW_ERROR_TYPE_ITEM,
923                         item,
924                         "ACL: Expecting END item");
925
926         return 0;
927 }
928
929 /***
930  * Both *tmask* and *fmask* are byte arrays of size *tsize* and *fsize*
931  * respectively.
932  * They are located within a larger buffer at offsets *toffset* and *foffset*
933  * respectivelly. Both *tmask* and *fmask* represent bitmasks for the larger
934  * buffer.
935  * Question: are the two masks equivalent?
936  *
937  * Notes:
938  * 1. Offset basically indicates that the first offset bytes in the buffer
939  *    are "don't care", so offset is equivalent to pre-pending an "all-zeros"
940  *    array of *offset* bytes to the *mask*.
941  * 2. Each *mask* might contain a number of zero bytes at the beginning or
942  *    at the end.
943  * 3. Bytes in the larger buffer after the end of the *mask* are also considered
944  *    "don't care", so they are equivalent to appending an "all-zeros" array of
945  *    bytes to the *mask*.
946  *
947  * Example:
948  * Buffer = [xx xx xx xx xx xx xx xx], buffer size = 8 bytes
949  * tmask = [00 22 00 33 00], toffset = 2, tsize = 5
950  *    => buffer mask = [00 00 00 22 00 33 00 00]
951  * fmask = [22 00 33], foffset = 3, fsize = 3 =>
952  *    => buffer mask = [00 00 00 22 00 33 00 00]
953  * Therefore, the tmask and fmask from this example are equivalent.
954  */
955 static int
956 hash_key_mask_is_same(uint8_t *tmask,
957         size_t toffset,
958         size_t tsize,
959         uint8_t *fmask,
960         size_t foffset,
961         size_t fsize,
962         size_t *toffset_plus,
963         size_t *foffset_plus)
964 {
965         size_t tpos; /* Position of first non-zero byte in the tmask buffer. */
966         size_t fpos; /* Position of first non-zero byte in the fmask buffer. */
967
968         /* Compute tpos and fpos. */
969         for (tpos = 0; tmask[tpos] == 0; tpos++)
970                 ;
971         for (fpos = 0; fmask[fpos] == 0; fpos++)
972                 ;
973
974         if (toffset + tpos != foffset + fpos)
975                 return 0; /* FALSE */
976
977         tsize -= tpos;
978         fsize -= fpos;
979
980         if (tsize < fsize) {
981                 size_t i;
982
983                 for (i = 0; i < tsize; i++)
984                         if (tmask[tpos + i] != fmask[fpos + i])
985                                 return 0; /* FALSE */
986
987                 for ( ; i < fsize; i++)
988                         if (fmask[fpos + i])
989                                 return 0; /* FALSE */
990         } else {
991                 size_t i;
992
993                 for (i = 0; i < fsize; i++)
994                         if (tmask[tpos + i] != fmask[fpos + i])
995                                 return 0; /* FALSE */
996
997                 for ( ; i < tsize; i++)
998                         if (tmask[tpos + i])
999                                 return 0; /* FALSE */
1000         }
1001
1002         if (toffset_plus)
1003                 *toffset_plus = tpos;
1004
1005         if (foffset_plus)
1006                 *foffset_plus = fpos;
1007
1008         return 1; /* TRUE */
1009 }
1010
1011 static int
1012 flow_rule_match_hash_get(struct pmd_internals *softnic __rte_unused,
1013         struct pipeline *pipeline __rte_unused,
1014         struct softnic_table *table,
1015         const struct rte_flow_attr *attr __rte_unused,
1016         const struct rte_flow_item *item,
1017         struct softnic_table_rule_match *rule_match,
1018         struct rte_flow_error *error)
1019 {
1020         struct softnic_table_rule_match_hash key, key_mask;
1021         struct softnic_table_hash_params *params = &table->params.match.hash;
1022         size_t offset = 0, length = 0, tpos, fpos;
1023         int status;
1024
1025         memset(&key, 0, sizeof(key));
1026         memset(&key_mask, 0, sizeof(key_mask));
1027
1028         /* VOID or disabled protos only, if any. */
1029         status = flow_item_skip_disabled_protos(&item, 0, &offset, error);
1030         if (status)
1031                 return status;
1032
1033         if (item->type == RTE_FLOW_ITEM_TYPE_END)
1034                 return rte_flow_error_set(error,
1035                         EINVAL,
1036                         RTE_FLOW_ERROR_TYPE_ITEM,
1037                         item,
1038                         "HASH: END detected too early");
1039
1040         /* VOID or any protocols (enabled or disabled). */
1041         for ( ; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1042                 union flow_item spec, mask;
1043                 size_t size;
1044                 int disabled, status;
1045
1046                 if (item->type == RTE_FLOW_ITEM_TYPE_VOID)
1047                         continue;
1048
1049                 status = flow_item_proto_preprocess(item,
1050                         &spec,
1051                         &mask,
1052                         &size,
1053                         &disabled,
1054                         error);
1055                 if (status)
1056                         return status;
1057
1058                 if (length + size > sizeof(key)) {
1059                         if (disabled)
1060                                 break;
1061
1062                         return rte_flow_error_set(error,
1063                                 ENOTSUP,
1064                                 RTE_FLOW_ERROR_TYPE_ITEM,
1065                                 item,
1066                                 "HASH: Item too big");
1067                 }
1068
1069                 memcpy(&key.key[length], &spec, size);
1070                 memcpy(&key_mask.key[length], &mask, size);
1071                 length += size;
1072         }
1073
1074         if (item->type != RTE_FLOW_ITEM_TYPE_END) {
1075                 /* VOID or disabled protos only, if any. */
1076                 status = flow_item_skip_disabled_protos(&item, 0, NULL, error);
1077                 if (status)
1078                         return status;
1079
1080                 /* END only. */
1081                 if (item->type != RTE_FLOW_ITEM_TYPE_END)
1082                         return rte_flow_error_set(error,
1083                                 EINVAL,
1084                                 RTE_FLOW_ERROR_TYPE_ITEM,
1085                                 item,
1086                                 "HASH: Expecting END item");
1087         }
1088
1089         /* Compare flow key mask against table key mask. */
1090         offset += sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM;
1091
1092         if (!hash_key_mask_is_same(params->key_mask,
1093                 params->key_offset,
1094                 params->key_size,
1095                 key_mask.key,
1096                 offset,
1097                 length,
1098                 &tpos,
1099                 &fpos))
1100                 return rte_flow_error_set(error,
1101                         EINVAL,
1102                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1103                         NULL,
1104                         "HASH: Item list is not observing the match format");
1105
1106         /* Rule match. */
1107         memset(rule_match, 0, sizeof(*rule_match));
1108         rule_match->match_type = TABLE_HASH;
1109         memcpy(&rule_match->match.hash.key[tpos],
1110                 &key.key[fpos],
1111                 RTE_MIN(sizeof(rule_match->match.hash.key) - tpos,
1112                         length - fpos));
1113
1114         return 0;
1115 }
1116
1117 static int
1118 flow_rule_match_get(struct pmd_internals *softnic,
1119                 struct pipeline *pipeline,
1120                 struct softnic_table *table,
1121                 const struct rte_flow_attr *attr,
1122                 const struct rte_flow_item *item,
1123                 struct softnic_table_rule_match *rule_match,
1124                 struct rte_flow_error *error)
1125 {
1126         switch (table->params.match_type) {
1127         case TABLE_ACL:
1128                 return flow_rule_match_acl_get(softnic,
1129                         pipeline,
1130                         table,
1131                         attr,
1132                         item,
1133                         rule_match,
1134                         error);
1135
1136                 /* FALLTHROUGH */
1137
1138         case TABLE_HASH:
1139                 return flow_rule_match_hash_get(softnic,
1140                         pipeline,
1141                         table,
1142                         attr,
1143                         item,
1144                         rule_match,
1145                         error);
1146
1147                 /* FALLTHROUGH */
1148
1149         default:
1150                 return rte_flow_error_set(error,
1151                         ENOTSUP,
1152                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1153                         NULL,
1154                         "Unsupported pipeline table match type");
1155         }
1156 }
1157
1158 static int
1159 flow_rule_action_get(struct pmd_internals *softnic,
1160         struct pipeline *pipeline,
1161         struct softnic_table *table,
1162         const struct rte_flow_attr *attr,
1163         const struct rte_flow_action *action,
1164         struct softnic_table_rule_action *rule_action,
1165         struct rte_flow_error *error)
1166 {
1167         struct softnic_table_action_profile *profile;
1168         struct softnic_table_action_profile_params *params;
1169         int n_jump_queue_rss_drop = 0;
1170         int n_count = 0;
1171         int n_mark = 0;
1172         int n_vxlan_decap = 0;
1173
1174         profile = softnic_table_action_profile_find(softnic,
1175                 table->params.action_profile_name);
1176         if (profile == NULL)
1177                 return rte_flow_error_set(error,
1178                         EINVAL,
1179                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1180                         action,
1181                         "JUMP: Table action profile");
1182
1183         params = &profile->params;
1184
1185         for ( ; action->type != RTE_FLOW_ACTION_TYPE_END; action++) {
1186                 if (action->type == RTE_FLOW_ACTION_TYPE_VOID)
1187                         continue;
1188
1189                 switch (action->type) {
1190                 case RTE_FLOW_ACTION_TYPE_JUMP:
1191                 {
1192                         const struct rte_flow_action_jump *conf = action->conf;
1193                         struct flow_attr_map *map;
1194
1195                         if (conf == NULL)
1196                                 return rte_flow_error_set(error,
1197                                         EINVAL,
1198                                         RTE_FLOW_ERROR_TYPE_ACTION,
1199                                         action,
1200                                         "JUMP: Null configuration");
1201
1202                         if (n_jump_queue_rss_drop)
1203                                 return rte_flow_error_set(error,
1204                                         EINVAL,
1205                                         RTE_FLOW_ERROR_TYPE_ACTION,
1206                                         action,
1207                                         "Only one termination action is"
1208                                         " allowed per flow");
1209
1210                         if ((params->action_mask &
1211                                 (1LLU << RTE_TABLE_ACTION_FWD)) == 0)
1212                                 return rte_flow_error_set(error,
1213                                         EINVAL,
1214                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1215                                         NULL,
1216                                         "JUMP action not enabled for this table");
1217
1218                         n_jump_queue_rss_drop = 1;
1219
1220                         map = flow_attr_map_get(softnic,
1221                                 conf->group,
1222                                 attr->ingress);
1223                         if (map == NULL || map->valid == 0)
1224                                 return rte_flow_error_set(error,
1225                                         EINVAL,
1226                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1227                                         NULL,
1228                                         "JUMP: Invalid group mapping");
1229
1230                         if (strcmp(pipeline->name, map->pipeline_name) != 0)
1231                                 return rte_flow_error_set(error,
1232                                         ENOTSUP,
1233                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1234                                         NULL,
1235                                         "JUMP: Jump to table in different pipeline");
1236
1237                         /* RTE_TABLE_ACTION_FWD */
1238                         rule_action->fwd.action = RTE_PIPELINE_ACTION_TABLE;
1239                         rule_action->fwd.id = map->table_id;
1240                         rule_action->action_mask |= 1 << RTE_TABLE_ACTION_FWD;
1241                         break;
1242                 } /* RTE_FLOW_ACTION_TYPE_JUMP */
1243
1244                 case RTE_FLOW_ACTION_TYPE_QUEUE:
1245                 {
1246                         char name[NAME_SIZE];
1247                         struct rte_eth_dev *dev;
1248                         const struct rte_flow_action_queue *conf = action->conf;
1249                         uint32_t port_id;
1250                         int status;
1251
1252                         if (conf == NULL)
1253                                 return rte_flow_error_set(error,
1254                                         EINVAL,
1255                                         RTE_FLOW_ERROR_TYPE_ACTION,
1256                                         action,
1257                                         "QUEUE: Null configuration");
1258
1259                         if (n_jump_queue_rss_drop)
1260                                 return rte_flow_error_set(error,
1261                                         EINVAL,
1262                                         RTE_FLOW_ERROR_TYPE_ACTION,
1263                                         action,
1264                                         "Only one termination action is allowed"
1265                                         " per flow");
1266
1267                         if ((params->action_mask &
1268                                 (1LLU << RTE_TABLE_ACTION_FWD)) == 0)
1269                                 return rte_flow_error_set(error,
1270                                         EINVAL,
1271                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1272                                         NULL,
1273                                         "QUEUE action not enabled for this table");
1274
1275                         n_jump_queue_rss_drop = 1;
1276
1277                         dev = ETHDEV(softnic);
1278                         if (dev == NULL ||
1279                                 conf->index >= dev->data->nb_rx_queues)
1280                                 return rte_flow_error_set(error,
1281                                         EINVAL,
1282                                         RTE_FLOW_ERROR_TYPE_ACTION,
1283                                         action,
1284                                         "QUEUE: Invalid RX queue ID");
1285
1286                         sprintf(name, "RXQ%u", (uint32_t)conf->index);
1287
1288                         status = softnic_pipeline_port_out_find(softnic,
1289                                 pipeline->name,
1290                                 name,
1291                                 &port_id);
1292                         if (status)
1293                                 return rte_flow_error_set(error,
1294                                         ENOTSUP,
1295                                         RTE_FLOW_ERROR_TYPE_ACTION,
1296                                         action,
1297                                         "QUEUE: RX queue not accessible from this pipeline");
1298
1299                         /* RTE_TABLE_ACTION_FWD */
1300                         rule_action->fwd.action = RTE_PIPELINE_ACTION_PORT;
1301                         rule_action->fwd.id = port_id;
1302                         rule_action->action_mask |= 1 << RTE_TABLE_ACTION_FWD;
1303                         break;
1304                 } /*RTE_FLOW_ACTION_TYPE_QUEUE */
1305
1306                 case RTE_FLOW_ACTION_TYPE_RSS:
1307                 {
1308                         const struct rte_flow_action_rss *conf = action->conf;
1309                         uint32_t i;
1310
1311                         if (conf == NULL)
1312                                 return rte_flow_error_set(error,
1313                                         EINVAL,
1314                                         RTE_FLOW_ERROR_TYPE_ACTION,
1315                                         action,
1316                                         "RSS: Null configuration");
1317
1318                         if (!rte_is_power_of_2(conf->queue_num))
1319                                 return rte_flow_error_set(error,
1320                                         EINVAL,
1321                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1322                                         conf,
1323                                         "RSS: Number of queues must be a power of 2");
1324
1325                         if (conf->queue_num > RTE_DIM(rule_action->lb.out))
1326                                 return rte_flow_error_set(error,
1327                                         EINVAL,
1328                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1329                                         conf,
1330                                         "RSS: Number of queues too big");
1331
1332                         if (n_jump_queue_rss_drop)
1333                                 return rte_flow_error_set(error,
1334                                         EINVAL,
1335                                         RTE_FLOW_ERROR_TYPE_ACTION,
1336                                         action,
1337                                         "Only one termination action is allowed per flow");
1338
1339                         if (((params->action_mask &
1340                                 (1LLU << RTE_TABLE_ACTION_FWD)) == 0) ||
1341                                 ((params->action_mask &
1342                                 (1LLU << RTE_TABLE_ACTION_LB)) == 0))
1343                                 return rte_flow_error_set(error,
1344                                         ENOTSUP,
1345                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1346                                         NULL,
1347                                         "RSS action not supported by this table");
1348
1349                         if (params->lb.out_offset !=
1350                                 pipeline->params.offset_port_id)
1351                                 return rte_flow_error_set(error,
1352                                         EINVAL,
1353                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1354                                         NULL,
1355                                         "RSS action not supported by this pipeline");
1356
1357                         n_jump_queue_rss_drop = 1;
1358
1359                         /* RTE_TABLE_ACTION_LB */
1360                         for (i = 0; i < conf->queue_num; i++) {
1361                                 char name[NAME_SIZE];
1362                                 struct rte_eth_dev *dev;
1363                                 uint32_t port_id;
1364                                 int status;
1365
1366                                 dev = ETHDEV(softnic);
1367                                 if (dev == NULL ||
1368                                         conf->queue[i] >=
1369                                                 dev->data->nb_rx_queues)
1370                                         return rte_flow_error_set(error,
1371                                                 EINVAL,
1372                                                 RTE_FLOW_ERROR_TYPE_ACTION,
1373                                                 action,
1374                                                 "RSS: Invalid RX queue ID");
1375
1376                                 sprintf(name, "RXQ%u",
1377                                         (uint32_t)conf->queue[i]);
1378
1379                                 status = softnic_pipeline_port_out_find(softnic,
1380                                         pipeline->name,
1381                                         name,
1382                                         &port_id);
1383                                 if (status)
1384                                         return rte_flow_error_set(error,
1385                                                 ENOTSUP,
1386                                                 RTE_FLOW_ERROR_TYPE_ACTION,
1387                                                 action,
1388                                                 "RSS: RX queue not accessible from this pipeline");
1389
1390                                 rule_action->lb.out[i] = port_id;
1391                         }
1392
1393                         for ( ; i < RTE_DIM(rule_action->lb.out); i++)
1394                                 rule_action->lb.out[i] =
1395                                 rule_action->lb.out[i % conf->queue_num];
1396
1397                         rule_action->action_mask |= 1 << RTE_TABLE_ACTION_LB;
1398
1399                         /* RTE_TABLE_ACTION_FWD */
1400                         rule_action->fwd.action = RTE_PIPELINE_ACTION_PORT_META;
1401                         rule_action->action_mask |= 1 << RTE_TABLE_ACTION_FWD;
1402                         break;
1403                 } /* RTE_FLOW_ACTION_TYPE_RSS */
1404
1405                 case RTE_FLOW_ACTION_TYPE_DROP:
1406                 {
1407                         const void *conf = action->conf;
1408
1409                         if (conf != NULL)
1410                                 return rte_flow_error_set(error,
1411                                         EINVAL,
1412                                         RTE_FLOW_ERROR_TYPE_ACTION,
1413                                         action,
1414                                         "DROP: No configuration required");
1415
1416                         if (n_jump_queue_rss_drop)
1417                                 return rte_flow_error_set(error,
1418                                         EINVAL,
1419                                         RTE_FLOW_ERROR_TYPE_ACTION,
1420                                         action,
1421                                         "Only one termination action is allowed per flow");
1422                         if ((params->action_mask &
1423                                 (1LLU << RTE_TABLE_ACTION_FWD)) == 0)
1424                                 return rte_flow_error_set(error,
1425                                         ENOTSUP,
1426                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1427                                         NULL,
1428                                         "DROP action not supported by this table");
1429
1430                         n_jump_queue_rss_drop = 1;
1431
1432                         /* RTE_TABLE_ACTION_FWD */
1433                         rule_action->fwd.action = RTE_PIPELINE_ACTION_DROP;
1434                         rule_action->action_mask |= 1 << RTE_TABLE_ACTION_FWD;
1435                         break;
1436                 } /* RTE_FLOW_ACTION_TYPE_DROP */
1437
1438                 case RTE_FLOW_ACTION_TYPE_COUNT:
1439                 {
1440                         const struct rte_flow_action_count *conf = action->conf;
1441
1442                         if (conf == NULL)
1443                                 return rte_flow_error_set(error,
1444                                         EINVAL,
1445                                         RTE_FLOW_ERROR_TYPE_ACTION,
1446                                         action,
1447                                         "COUNT: Null configuration");
1448
1449                         if (conf->shared)
1450                                 return rte_flow_error_set(error,
1451                                         ENOTSUP,
1452                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1453                                         conf,
1454                                         "COUNT: Shared counters not supported");
1455
1456                         if (n_count)
1457                                 return rte_flow_error_set(error,
1458                                         ENOTSUP,
1459                                         RTE_FLOW_ERROR_TYPE_ACTION,
1460                                         action,
1461                                         "Only one COUNT action per flow");
1462
1463                         if ((params->action_mask &
1464                                 (1LLU << RTE_TABLE_ACTION_STATS)) == 0)
1465                                 return rte_flow_error_set(error,
1466                                         ENOTSUP,
1467                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1468                                         NULL,
1469                                         "COUNT action not supported by this table");
1470
1471                         n_count = 1;
1472
1473                         /* RTE_TABLE_ACTION_STATS */
1474                         rule_action->stats.n_packets = 0;
1475                         rule_action->stats.n_bytes = 0;
1476                         rule_action->action_mask |= 1 << RTE_TABLE_ACTION_STATS;
1477                         break;
1478                 } /* RTE_FLOW_ACTION_TYPE_COUNT */
1479
1480                 case RTE_FLOW_ACTION_TYPE_MARK:
1481                 {
1482                         const struct rte_flow_action_mark *conf = action->conf;
1483
1484                         if (conf == NULL)
1485                                 return rte_flow_error_set(error,
1486                                         EINVAL,
1487                                         RTE_FLOW_ERROR_TYPE_ACTION,
1488                                         action,
1489                                         "MARK: Null configuration");
1490
1491                         if (n_mark)
1492                                 return rte_flow_error_set(error,
1493                                         ENOTSUP,
1494                                         RTE_FLOW_ERROR_TYPE_ACTION,
1495                                         action,
1496                                         "Only one MARK action per flow");
1497
1498                         if ((params->action_mask &
1499                                 (1LLU << RTE_TABLE_ACTION_TAG)) == 0)
1500                                 return rte_flow_error_set(error,
1501                                         ENOTSUP,
1502                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1503                                         NULL,
1504                                         "MARK action not supported by this table");
1505
1506                         n_mark = 1;
1507
1508                         /* RTE_TABLE_ACTION_TAG */
1509                         rule_action->tag.tag = conf->id;
1510                         rule_action->action_mask |= 1 << RTE_TABLE_ACTION_TAG;
1511                         break;
1512                 } /* RTE_FLOW_ACTION_TYPE_MARK */
1513
1514                 case RTE_FLOW_ACTION_TYPE_VXLAN_DECAP:
1515                 {
1516                         const struct rte_flow_action_mark *conf = action->conf;
1517
1518                         if (conf)
1519                                 return rte_flow_error_set(error,
1520                                         EINVAL,
1521                                         RTE_FLOW_ERROR_TYPE_ACTION,
1522                                         action,
1523                                         "VXLAN DECAP: Non-null configuration");
1524
1525                         if (n_vxlan_decap)
1526                                 return rte_flow_error_set(error,
1527                                         ENOTSUP,
1528                                         RTE_FLOW_ERROR_TYPE_ACTION,
1529                                         action,
1530                                         "Only one VXLAN DECAP action per flow");
1531
1532                         if ((params->action_mask &
1533                                 (1LLU << RTE_TABLE_ACTION_DECAP)) == 0)
1534                                 return rte_flow_error_set(error,
1535                                         ENOTSUP,
1536                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1537                                         NULL,
1538                                         "VXLAN DECAP action not supported by this table");
1539
1540                         n_vxlan_decap = 1;
1541
1542                         /* RTE_TABLE_ACTION_DECAP */
1543                         rule_action->decap.n = 50; /* Ether/IPv4/UDP/VXLAN */
1544                         rule_action->action_mask |= 1 << RTE_TABLE_ACTION_DECAP;
1545                         break;
1546                 } /* RTE_FLOW_ACTION_TYPE_VXLAN_DECAP */
1547
1548                 case RTE_FLOW_ACTION_TYPE_METER:
1549                 {
1550                         const struct rte_flow_action_meter *conf = action->conf;
1551                         struct softnic_mtr_meter_profile *mp;
1552                         struct softnic_mtr *m;
1553                         uint32_t table_id = table - pipeline->table;
1554                         uint32_t meter_profile_id;
1555                         int status;
1556
1557                         if ((params->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) == 0)
1558                                 return rte_flow_error_set(error,
1559                                         EINVAL,
1560                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1561                                         NULL,
1562                                         "METER: Table action not supported");
1563
1564                         if (params->mtr.n_tc != 1)
1565                                 return rte_flow_error_set(error,
1566                                         EINVAL,
1567                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1568                                         NULL,
1569                                         "METER: Multiple TCs not supported");
1570
1571                         if (conf == NULL)
1572                                 return rte_flow_error_set(error,
1573                                         EINVAL,
1574                                         RTE_FLOW_ERROR_TYPE_ACTION,
1575                                         action,
1576                                         "METER: Null configuration");
1577
1578                         m = softnic_mtr_find(softnic, conf->mtr_id);
1579
1580                         if (m == NULL)
1581                                 return rte_flow_error_set(error,
1582                                         EINVAL,
1583                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1584                                         NULL,
1585                                         "METER: Invalid meter ID");
1586
1587                         if (m->flow)
1588                                 return rte_flow_error_set(error,
1589                                         EINVAL,
1590                                         RTE_FLOW_ERROR_TYPE_ACTION_CONF,
1591                                         NULL,
1592                                         "METER: Meter already attached to a flow");
1593
1594                         meter_profile_id = m->params.meter_profile_id;
1595                         mp = softnic_mtr_meter_profile_find(softnic, meter_profile_id);
1596
1597                         /* Add meter profile to pipeline table */
1598                         if (!softnic_pipeline_table_meter_profile_find(table,
1599                                         meter_profile_id)) {
1600                                 struct rte_table_action_meter_profile profile;
1601
1602                                 memset(&profile, 0, sizeof(profile));
1603                                 profile.alg = RTE_TABLE_ACTION_METER_TRTCM;
1604                                 profile.trtcm.cir = mp->params.trtcm_rfc2698.cir;
1605                                 profile.trtcm.pir = mp->params.trtcm_rfc2698.pir;
1606                                 profile.trtcm.cbs = mp->params.trtcm_rfc2698.cbs;
1607                                 profile.trtcm.pbs = mp->params.trtcm_rfc2698.pbs;
1608
1609                                 status = softnic_pipeline_table_mtr_profile_add(softnic,
1610                                                 pipeline->name,
1611                                                 table_id,
1612                                                 meter_profile_id,
1613                                                 &profile);
1614                                 if (status) {
1615                                         rte_flow_error_set(error,
1616                                                 EINVAL,
1617                                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1618                                                 NULL,
1619                                                 "METER: Table meter profile add failed");
1620                                         return -1;
1621                                 }
1622                         }
1623
1624                         /* RTE_TABLE_ACTION_METER */
1625                         rule_action->mtr.mtr[0].meter_profile_id = meter_profile_id;
1626                         rule_action->mtr.mtr[0].policer[e_RTE_METER_GREEN] =
1627                                 (enum rte_table_action_policer)m->params.action[RTE_MTR_GREEN];
1628                         rule_action->mtr.mtr[0].policer[e_RTE_METER_YELLOW] =
1629                                 (enum rte_table_action_policer)m->params.action[RTE_MTR_YELLOW];
1630                         rule_action->mtr.mtr[0].policer[e_RTE_METER_RED] =
1631                                 (enum rte_table_action_policer)m->params.action[RTE_MTR_RED];
1632                         rule_action->mtr.tc_mask = 1;
1633                         rule_action->action_mask |= 1 << RTE_TABLE_ACTION_MTR;
1634                         break;
1635                 } /* RTE_FLOW_ACTION_TYPE_METER */
1636
1637                 case RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP:
1638                 {
1639                         const struct rte_flow_action_vxlan_encap *conf =
1640                                 action->conf;
1641                         const struct rte_flow_item *item;
1642                         union flow_item spec, mask;
1643                         int disabled = 0, status;
1644                         size_t size;
1645
1646                         if (conf == NULL)
1647                                 return rte_flow_error_set(error,
1648                                         EINVAL,
1649                                         RTE_FLOW_ERROR_TYPE_ACTION,
1650                                         action,
1651                                         "VXLAN ENCAP: Null configuration");
1652
1653                         item = conf->definition;
1654                         if (item == NULL)
1655                                 return rte_flow_error_set(error,
1656                                         EINVAL,
1657                                         RTE_FLOW_ERROR_TYPE_ACTION,
1658                                         action,
1659                                         "VXLAN ENCAP: Null configuration definition");
1660
1661                         if (!(params->action_mask &
1662                                         (1LLU << RTE_TABLE_ACTION_ENCAP)))
1663                                 return rte_flow_error_set(error,
1664                                         EINVAL,
1665                                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1666                                         NULL,
1667                                         "VXLAN ENCAP: Encap action not enabled for this table");
1668
1669                         /* Check for Ether. */
1670                         flow_item_skip_void(&item);
1671                         status = flow_item_proto_preprocess(item, &spec, &mask,
1672                                 &size, &disabled, error);
1673                         if (status)
1674                                 return status;
1675
1676                         if (item->type != RTE_FLOW_ITEM_TYPE_ETH) {
1677                                 return rte_flow_error_set(error,
1678                                         EINVAL,
1679                                         RTE_FLOW_ERROR_TYPE_ITEM,
1680                                         item,
1681                                         "VXLAN ENCAP: first encap item should be ether");
1682                         }
1683                         ether_addr_copy(&spec.eth.dst,
1684                                         &rule_action->encap.vxlan.ether.da);
1685                         ether_addr_copy(&spec.eth.src,
1686                                         &rule_action->encap.vxlan.ether.sa);
1687
1688                         item++;
1689
1690                         /* Check for VLAN. */
1691                         flow_item_skip_void(&item);
1692                         status = flow_item_proto_preprocess(item, &spec, &mask,
1693                                         &size, &disabled, error);
1694                         if (status)
1695                                 return status;
1696
1697                         if (item->type == RTE_FLOW_ITEM_TYPE_VLAN) {
1698                                 if (!params->encap.vxlan.vlan)
1699                                         return rte_flow_error_set(error,
1700                                                 ENOTSUP,
1701                                                 RTE_FLOW_ERROR_TYPE_ITEM,
1702                                                 item,
1703                                                 "VXLAN ENCAP: vlan encap not supported by table");
1704
1705                                 uint16_t tci = rte_ntohs(spec.vlan.tci);
1706                                 rule_action->encap.vxlan.vlan.pcp =
1707                                         tci >> 13;
1708                                 rule_action->encap.vxlan.vlan.dei =
1709                                         (tci >> 12) & 0x1;
1710                                 rule_action->encap.vxlan.vlan.vid =
1711                                         tci & 0xfff;
1712
1713                                 item++;
1714
1715                                 flow_item_skip_void(&item);
1716                                 status = flow_item_proto_preprocess(item, &spec,
1717                                                 &mask, &size, &disabled, error);
1718                                 if (status)
1719                                         return status;
1720                         } else {
1721                                 if (params->encap.vxlan.vlan)
1722                                         return rte_flow_error_set(error,
1723                                                 ENOTSUP,
1724                                                 RTE_FLOW_ERROR_TYPE_ITEM,
1725                                                 item,
1726                                                 "VXLAN ENCAP: expecting vlan encap item");
1727                         }
1728
1729                         /* Check for IPV4/IPV6. */
1730                         switch (item->type) {
1731                         case RTE_FLOW_ITEM_TYPE_IPV4:
1732                         {
1733                                 rule_action->encap.vxlan.ipv4.sa =
1734                                         rte_ntohl(spec.ipv4.hdr.src_addr);
1735                                 rule_action->encap.vxlan.ipv4.da =
1736                                         rte_ntohl(spec.ipv4.hdr.dst_addr);
1737                                 rule_action->encap.vxlan.ipv4.dscp =
1738                                         spec.ipv4.hdr.type_of_service >> 2;
1739                                 rule_action->encap.vxlan.ipv4.ttl =
1740                                         spec.ipv4.hdr.time_to_live;
1741                                 break;
1742                         }
1743                         case RTE_FLOW_ITEM_TYPE_IPV6:
1744                         {
1745                                 uint32_t vtc_flow;
1746
1747                                 memcpy(&rule_action->encap.vxlan.ipv6.sa,
1748                                                 &spec.ipv6.hdr.src_addr,
1749                                                 sizeof(spec.ipv6.hdr.src_addr));
1750                                 memcpy(&rule_action->encap.vxlan.ipv6.da,
1751                                                 &spec.ipv6.hdr.dst_addr,
1752                                                 sizeof(spec.ipv6.hdr.dst_addr));
1753                                 vtc_flow = rte_ntohl(spec.ipv6.hdr.vtc_flow);
1754                                 rule_action->encap.vxlan.ipv6.flow_label =
1755                                                 vtc_flow & 0xfffff;
1756                                 rule_action->encap.vxlan.ipv6.dscp =
1757                                                 (vtc_flow >> 22) & 0x3f;
1758                                 rule_action->encap.vxlan.ipv6.hop_limit =
1759                                         spec.ipv6.hdr.hop_limits;
1760                                 break;
1761                         }
1762                         default:
1763                                 return rte_flow_error_set(error,
1764                                         EINVAL,
1765                                         RTE_FLOW_ERROR_TYPE_ITEM,
1766                                         item,
1767                                         "VXLAN ENCAP: encap item after ether should be ipv4/ipv6");
1768                         }
1769
1770                         item++;
1771
1772                         /* Check for UDP. */
1773                         flow_item_skip_void(&item);
1774                         status = flow_item_proto_preprocess(item, &spec, &mask,
1775                                         &size, &disabled, error);
1776                         if (status)
1777                                 return status;
1778
1779                         if (item->type != RTE_FLOW_ITEM_TYPE_UDP) {
1780                                 return rte_flow_error_set(error,
1781                                         EINVAL,
1782                                         RTE_FLOW_ERROR_TYPE_ITEM,
1783                                         item,
1784                                         "VXLAN ENCAP: encap item after ipv4/ipv6 should be udp");
1785                         }
1786                         rule_action->encap.vxlan.udp.sp =
1787                                 rte_ntohs(spec.udp.hdr.src_port);
1788                         rule_action->encap.vxlan.udp.dp =
1789                                 rte_ntohs(spec.udp.hdr.dst_port);
1790
1791                         item++;
1792
1793                         /* Check for VXLAN. */
1794                         flow_item_skip_void(&item);
1795                         status = flow_item_proto_preprocess(item, &spec, &mask,
1796                                         &size, &disabled, error);
1797                         if (status)
1798                                 return status;
1799
1800                         if (item->type != RTE_FLOW_ITEM_TYPE_VXLAN) {
1801                                 return rte_flow_error_set(error,
1802                                         EINVAL,
1803                                         RTE_FLOW_ERROR_TYPE_ITEM,
1804                                         item,
1805                                         "VXLAN ENCAP: encap item after udp should be vxlan");
1806                         }
1807                         rule_action->encap.vxlan.vxlan.vni =
1808                                 (spec.vxlan.vni[0] << 16U |
1809                                         spec.vxlan.vni[1] << 8U
1810                                         | spec.vxlan.vni[2]);
1811
1812                         item++;
1813
1814                         /* Check for END. */
1815                         flow_item_skip_void(&item);
1816
1817                         if (item->type != RTE_FLOW_ITEM_TYPE_END)
1818                                 return rte_flow_error_set(error,
1819                                         EINVAL,
1820                                         RTE_FLOW_ERROR_TYPE_ITEM,
1821                                         item,
1822                                         "VXLAN ENCAP: expecting END item");
1823
1824                         rule_action->encap.type = RTE_TABLE_ACTION_ENCAP_VXLAN;
1825                         rule_action->action_mask |= 1 << RTE_TABLE_ACTION_ENCAP;
1826                         break;
1827                 } /* RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP */
1828
1829                 default:
1830                         return -ENOTSUP;
1831                 }
1832         }
1833
1834         if (n_jump_queue_rss_drop == 0)
1835                 return rte_flow_error_set(error,
1836                         EINVAL,
1837                         RTE_FLOW_ERROR_TYPE_ACTION,
1838                         action,
1839                         "Flow does not have any terminating action");
1840
1841         return 0;
1842 }
1843
1844 static int
1845 pmd_flow_validate(struct rte_eth_dev *dev,
1846                 const struct rte_flow_attr *attr,
1847                 const struct rte_flow_item item[],
1848                 const struct rte_flow_action action[],
1849                 struct rte_flow_error *error)
1850 {
1851         struct softnic_table_rule_match rule_match;
1852         struct softnic_table_rule_action rule_action;
1853
1854         struct pmd_internals *softnic = dev->data->dev_private;
1855         struct pipeline *pipeline;
1856         struct softnic_table *table;
1857         const char *pipeline_name = NULL;
1858         uint32_t table_id = 0;
1859         int status;
1860
1861         /* Check input parameters. */
1862         if (attr == NULL)
1863                 return rte_flow_error_set(error,
1864                                 EINVAL,
1865                                 RTE_FLOW_ERROR_TYPE_ATTR,
1866                                 NULL, "Null attr");
1867
1868         if (item == NULL)
1869                 return rte_flow_error_set(error,
1870                                 EINVAL,
1871                                 RTE_FLOW_ERROR_TYPE_ITEM,
1872                                 NULL,
1873                                 "Null item");
1874
1875         if (action == NULL)
1876                 return rte_flow_error_set(error,
1877                                 EINVAL,
1878                                 RTE_FLOW_ERROR_TYPE_ACTION,
1879                                 NULL,
1880                                 "Null action");
1881
1882         /* Identify the pipeline table to add this flow to. */
1883         status = flow_pipeline_table_get(softnic, attr, &pipeline_name,
1884                                         &table_id, error);
1885         if (status)
1886                 return status;
1887
1888         pipeline = softnic_pipeline_find(softnic, pipeline_name);
1889         if (pipeline == NULL)
1890                 return rte_flow_error_set(error,
1891                                 EINVAL,
1892                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1893                                 NULL,
1894                                 "Invalid pipeline name");
1895
1896         if (table_id >= pipeline->n_tables)
1897                 return rte_flow_error_set(error,
1898                                 EINVAL,
1899                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1900                                 NULL,
1901                                 "Invalid pipeline table ID");
1902
1903         table = &pipeline->table[table_id];
1904
1905         /* Rule match. */
1906         memset(&rule_match, 0, sizeof(rule_match));
1907         status = flow_rule_match_get(softnic,
1908                         pipeline,
1909                         table,
1910                         attr,
1911                         item,
1912                         &rule_match,
1913                         error);
1914         if (status)
1915                 return status;
1916
1917         /* Rule action. */
1918         memset(&rule_action, 0, sizeof(rule_action));
1919         status = flow_rule_action_get(softnic,
1920                 pipeline,
1921                 table,
1922                 attr,
1923                 action,
1924                 &rule_action,
1925                 error);
1926         if (status)
1927                 return status;
1928
1929         return 0;
1930 }
1931
1932 static struct softnic_mtr *
1933 flow_action_meter_get(struct pmd_internals *softnic,
1934         const struct rte_flow_action *action)
1935 {
1936         for ( ; action->type != RTE_FLOW_ACTION_TYPE_END; action++)
1937                 if (action->type == RTE_FLOW_ACTION_TYPE_METER) {
1938                         const struct rte_flow_action_meter *conf = action->conf;
1939
1940                         if (conf == NULL)
1941                                 return NULL;
1942
1943                         return softnic_mtr_find(softnic, conf->mtr_id);
1944                 }
1945
1946         return NULL;
1947 }
1948
1949 static void
1950 flow_meter_owner_reset(struct pmd_internals *softnic,
1951         struct rte_flow *flow)
1952 {
1953         struct softnic_mtr_list *ml = &softnic->mtr.mtrs;
1954         struct softnic_mtr *m;
1955
1956         TAILQ_FOREACH(m, ml, node)
1957                 if (m->flow == flow) {
1958                         m->flow = NULL;
1959                         break;
1960                 }
1961 }
1962
1963 static void
1964 flow_meter_owner_set(struct pmd_internals *softnic,
1965         struct rte_flow *flow,
1966         struct softnic_mtr *mtr)
1967 {
1968         /* Reset current flow meter  */
1969         flow_meter_owner_reset(softnic, flow);
1970
1971         /* Set new flow meter */
1972         mtr->flow = flow;
1973 }
1974
1975 static int
1976 is_meter_action_enable(struct pmd_internals *softnic,
1977         struct softnic_table *table)
1978 {
1979         struct softnic_table_action_profile *profile =
1980                 softnic_table_action_profile_find(softnic,
1981                         table->params.action_profile_name);
1982         struct softnic_table_action_profile_params *params = &profile->params;
1983
1984         return (params->action_mask & (1LLU << RTE_TABLE_ACTION_MTR)) ? 1 : 0;
1985 }
1986
1987 static struct rte_flow *
1988 pmd_flow_create(struct rte_eth_dev *dev,
1989         const struct rte_flow_attr *attr,
1990         const struct rte_flow_item item[],
1991         const struct rte_flow_action action[],
1992         struct rte_flow_error *error)
1993 {
1994         struct softnic_table_rule_match rule_match;
1995         struct softnic_table_rule_action rule_action;
1996         void *rule_data;
1997
1998         struct pmd_internals *softnic = dev->data->dev_private;
1999         struct pipeline *pipeline;
2000         struct softnic_table *table;
2001         struct rte_flow *flow;
2002         struct softnic_mtr *mtr;
2003         const char *pipeline_name = NULL;
2004         uint32_t table_id = 0;
2005         int new_flow, status;
2006
2007         /* Check input parameters. */
2008         if (attr == NULL) {
2009                 rte_flow_error_set(error,
2010                         EINVAL,
2011                         RTE_FLOW_ERROR_TYPE_ATTR,
2012                         NULL,
2013                         "Null attr");
2014                 return NULL;
2015         }
2016
2017         if (item == NULL) {
2018                 rte_flow_error_set(error,
2019                         EINVAL,
2020                         RTE_FLOW_ERROR_TYPE_ITEM,
2021                         NULL,
2022                         "Null item");
2023                 return NULL;
2024         }
2025
2026         if (action == NULL) {
2027                 rte_flow_error_set(error,
2028                         EINVAL,
2029                         RTE_FLOW_ERROR_TYPE_ACTION,
2030                         NULL,
2031                         "Null action");
2032                 return NULL;
2033         }
2034
2035         /* Identify the pipeline table to add this flow to. */
2036         status = flow_pipeline_table_get(softnic, attr, &pipeline_name,
2037                                         &table_id, error);
2038         if (status)
2039                 return NULL;
2040
2041         pipeline = softnic_pipeline_find(softnic, pipeline_name);
2042         if (pipeline == NULL) {
2043                 rte_flow_error_set(error,
2044                         EINVAL,
2045                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2046                         NULL,
2047                         "Invalid pipeline name");
2048                 return NULL;
2049         }
2050
2051         if (table_id >= pipeline->n_tables) {
2052                 rte_flow_error_set(error,
2053                         EINVAL,
2054                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2055                         NULL,
2056                         "Invalid pipeline table ID");
2057                 return NULL;
2058         }
2059
2060         table = &pipeline->table[table_id];
2061
2062         /* Rule match. */
2063         memset(&rule_match, 0, sizeof(rule_match));
2064         status = flow_rule_match_get(softnic,
2065                 pipeline,
2066                 table,
2067                 attr,
2068                 item,
2069                 &rule_match,
2070                 error);
2071         if (status)
2072                 return NULL;
2073
2074         /* Rule action. */
2075         memset(&rule_action, 0, sizeof(rule_action));
2076         status = flow_rule_action_get(softnic,
2077                 pipeline,
2078                 table,
2079                 attr,
2080                 action,
2081                 &rule_action,
2082                 error);
2083         if (status)
2084                 return NULL;
2085
2086         /* Flow find/allocate. */
2087         new_flow = 0;
2088         flow = softnic_flow_find(table, &rule_match);
2089         if (flow == NULL) {
2090                 new_flow = 1;
2091                 flow = calloc(1, sizeof(struct rte_flow));
2092                 if (flow == NULL) {
2093                         rte_flow_error_set(error,
2094                                 ENOMEM,
2095                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2096                                 NULL,
2097                                 "Not enough memory for new flow");
2098                         return NULL;
2099                 }
2100         }
2101
2102         /* Rule add. */
2103         status = softnic_pipeline_table_rule_add(softnic,
2104                 pipeline_name,
2105                 table_id,
2106                 &rule_match,
2107                 &rule_action,
2108                 &rule_data);
2109         if (status) {
2110                 if (new_flow)
2111                         free(flow);
2112
2113                 rte_flow_error_set(error,
2114                         EINVAL,
2115                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2116                         NULL,
2117                         "Pipeline table rule add failed");
2118                 return NULL;
2119         }
2120
2121         /* Flow fill in. */
2122         memcpy(&flow->match, &rule_match, sizeof(rule_match));
2123         memcpy(&flow->action, &rule_action, sizeof(rule_action));
2124         flow->data = rule_data;
2125         flow->pipeline = pipeline;
2126         flow->table_id = table_id;
2127
2128         mtr = flow_action_meter_get(softnic, action);
2129         if (mtr)
2130                 flow_meter_owner_set(softnic, flow, mtr);
2131
2132         /* Flow add to list. */
2133         if (new_flow)
2134                 TAILQ_INSERT_TAIL(&table->flows, flow, node);
2135
2136         return flow;
2137 }
2138
2139 static int
2140 pmd_flow_destroy(struct rte_eth_dev *dev,
2141         struct rte_flow *flow,
2142         struct rte_flow_error *error)
2143 {
2144         struct pmd_internals *softnic = dev->data->dev_private;
2145         struct softnic_table *table;
2146         int status;
2147
2148         /* Check input parameters. */
2149         if (flow == NULL)
2150                 return rte_flow_error_set(error,
2151                         EINVAL,
2152                         RTE_FLOW_ERROR_TYPE_HANDLE,
2153                         NULL,
2154                         "Null flow");
2155
2156         table = &flow->pipeline->table[flow->table_id];
2157
2158         /* Rule delete. */
2159         status = softnic_pipeline_table_rule_delete(softnic,
2160                 flow->pipeline->name,
2161                 flow->table_id,
2162                 &flow->match);
2163         if (status)
2164                 return rte_flow_error_set(error,
2165                         EINVAL,
2166                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2167                         NULL,
2168                         "Pipeline table rule delete failed");
2169
2170         /* Update dependencies */
2171         if (is_meter_action_enable(softnic, table))
2172                 flow_meter_owner_reset(softnic, flow);
2173
2174         /* Flow delete. */
2175         TAILQ_REMOVE(&table->flows, flow, node);
2176         free(flow);
2177
2178         return 0;
2179 }
2180
2181 static int
2182 pmd_flow_flush(struct rte_eth_dev *dev,
2183         struct rte_flow_error *error)
2184 {
2185         struct pmd_internals *softnic = dev->data->dev_private;
2186         struct pipeline *pipeline;
2187         int fail_to_del_rule = 0;
2188         uint32_t i;
2189
2190         TAILQ_FOREACH(pipeline, &softnic->pipeline_list, node) {
2191                 /* Remove all the flows added to the tables. */
2192                 for (i = 0; i < pipeline->n_tables; i++) {
2193                         struct softnic_table *table = &pipeline->table[i];
2194                         struct rte_flow *flow;
2195                         void *temp;
2196                         int status;
2197
2198                         TAILQ_FOREACH_SAFE(flow, &table->flows, node, temp) {
2199                                 /* Rule delete. */
2200                                 status = softnic_pipeline_table_rule_delete
2201                                                 (softnic,
2202                                                 pipeline->name,
2203                                                 i,
2204                                                 &flow->match);
2205                                 if (status)
2206                                         fail_to_del_rule = 1;
2207                                 /* Update dependencies */
2208                                 if (is_meter_action_enable(softnic, table))
2209                                         flow_meter_owner_reset(softnic, flow);
2210
2211                                 /* Flow delete. */
2212                                 TAILQ_REMOVE(&table->flows, flow, node);
2213                                 free(flow);
2214                         }
2215                 }
2216         }
2217
2218         if (fail_to_del_rule)
2219                 return rte_flow_error_set(error,
2220                         EINVAL,
2221                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2222                         NULL,
2223                         "Some of the rules could not be deleted");
2224
2225         return 0;
2226 }
2227
2228 static int
2229 pmd_flow_query(struct rte_eth_dev *dev __rte_unused,
2230         struct rte_flow *flow,
2231         const struct rte_flow_action *action __rte_unused,
2232         void *data,
2233         struct rte_flow_error *error)
2234 {
2235         struct rte_table_action_stats_counters stats;
2236         struct softnic_table *table;
2237         struct rte_flow_query_count *flow_stats = data;
2238         int status;
2239
2240         /* Check input parameters. */
2241         if (flow == NULL)
2242                 return rte_flow_error_set(error,
2243                         EINVAL,
2244                         RTE_FLOW_ERROR_TYPE_HANDLE,
2245                         NULL,
2246                         "Null flow");
2247
2248         if (data == NULL)
2249                 return rte_flow_error_set(error,
2250                         EINVAL,
2251                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2252                         NULL,
2253                         "Null data");
2254
2255         table = &flow->pipeline->table[flow->table_id];
2256
2257         /* Rule stats read. */
2258         status = rte_table_action_stats_read(table->a,
2259                 flow->data,
2260                 &stats,
2261                 flow_stats->reset);
2262         if (status)
2263                 return rte_flow_error_set(error,
2264                         EINVAL,
2265                         RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2266                         NULL,
2267                         "Pipeline table rule stats read failed");
2268
2269         /* Fill in flow stats. */
2270         flow_stats->hits_set =
2271                 (table->ap->params.stats.n_packets_enabled) ? 1 : 0;
2272         flow_stats->bytes_set =
2273                 (table->ap->params.stats.n_bytes_enabled) ? 1 : 0;
2274         flow_stats->hits = stats.n_packets;
2275         flow_stats->bytes = stats.n_bytes;
2276
2277         return 0;
2278 }
2279
2280 const struct rte_flow_ops pmd_flow_ops = {
2281         .validate = pmd_flow_validate,
2282         .create = pmd_flow_create,
2283         .destroy = pmd_flow_destroy,
2284         .flush = pmd_flow_flush,
2285         .query = pmd_flow_query,
2286         .isolate = NULL,
2287 };