app/testpmd: add VXLAN encap/decap
[dpdk.git] / app / test-pmd / cmdline_flow.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016 6WIND S.A.
3  * Copyright 2016 Mellanox Technologies, Ltd
4  */
5
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <inttypes.h>
10 #include <errno.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <arpa/inet.h>
14 #include <sys/socket.h>
15
16 #include <rte_common.h>
17 #include <rte_eth_ctrl.h>
18 #include <rte_ethdev.h>
19 #include <rte_byteorder.h>
20 #include <cmdline_parse.h>
21 #include <cmdline_parse_etheraddr.h>
22 #include <rte_flow.h>
23
24 #include "testpmd.h"
25
26 /** Parser token indices. */
27 enum index {
28         /* Special tokens. */
29         ZERO = 0,
30         END,
31
32         /* Common tokens. */
33         INTEGER,
34         UNSIGNED,
35         PREFIX,
36         BOOLEAN,
37         STRING,
38         MAC_ADDR,
39         IPV4_ADDR,
40         IPV6_ADDR,
41         RULE_ID,
42         PORT_ID,
43         GROUP_ID,
44         PRIORITY_LEVEL,
45
46         /* Top-level command. */
47         FLOW,
48
49         /* Sub-level commands. */
50         VALIDATE,
51         CREATE,
52         DESTROY,
53         FLUSH,
54         QUERY,
55         LIST,
56         ISOLATE,
57
58         /* Destroy arguments. */
59         DESTROY_RULE,
60
61         /* Query arguments. */
62         QUERY_ACTION,
63
64         /* List arguments. */
65         LIST_GROUP,
66
67         /* Validate/create arguments. */
68         GROUP,
69         PRIORITY,
70         INGRESS,
71         EGRESS,
72         TRANSFER,
73
74         /* Validate/create pattern. */
75         PATTERN,
76         ITEM_PARAM_IS,
77         ITEM_PARAM_SPEC,
78         ITEM_PARAM_LAST,
79         ITEM_PARAM_MASK,
80         ITEM_PARAM_PREFIX,
81         ITEM_NEXT,
82         ITEM_END,
83         ITEM_VOID,
84         ITEM_INVERT,
85         ITEM_ANY,
86         ITEM_ANY_NUM,
87         ITEM_PF,
88         ITEM_VF,
89         ITEM_VF_ID,
90         ITEM_PHY_PORT,
91         ITEM_PHY_PORT_INDEX,
92         ITEM_PORT_ID,
93         ITEM_PORT_ID_ID,
94         ITEM_MARK,
95         ITEM_MARK_ID,
96         ITEM_RAW,
97         ITEM_RAW_RELATIVE,
98         ITEM_RAW_SEARCH,
99         ITEM_RAW_OFFSET,
100         ITEM_RAW_LIMIT,
101         ITEM_RAW_PATTERN,
102         ITEM_ETH,
103         ITEM_ETH_DST,
104         ITEM_ETH_SRC,
105         ITEM_ETH_TYPE,
106         ITEM_VLAN,
107         ITEM_VLAN_TCI,
108         ITEM_VLAN_PCP,
109         ITEM_VLAN_DEI,
110         ITEM_VLAN_VID,
111         ITEM_VLAN_INNER_TYPE,
112         ITEM_IPV4,
113         ITEM_IPV4_TOS,
114         ITEM_IPV4_TTL,
115         ITEM_IPV4_PROTO,
116         ITEM_IPV4_SRC,
117         ITEM_IPV4_DST,
118         ITEM_IPV6,
119         ITEM_IPV6_TC,
120         ITEM_IPV6_FLOW,
121         ITEM_IPV6_PROTO,
122         ITEM_IPV6_HOP,
123         ITEM_IPV6_SRC,
124         ITEM_IPV6_DST,
125         ITEM_ICMP,
126         ITEM_ICMP_TYPE,
127         ITEM_ICMP_CODE,
128         ITEM_UDP,
129         ITEM_UDP_SRC,
130         ITEM_UDP_DST,
131         ITEM_TCP,
132         ITEM_TCP_SRC,
133         ITEM_TCP_DST,
134         ITEM_TCP_FLAGS,
135         ITEM_SCTP,
136         ITEM_SCTP_SRC,
137         ITEM_SCTP_DST,
138         ITEM_SCTP_TAG,
139         ITEM_SCTP_CKSUM,
140         ITEM_VXLAN,
141         ITEM_VXLAN_VNI,
142         ITEM_E_TAG,
143         ITEM_E_TAG_GRP_ECID_B,
144         ITEM_NVGRE,
145         ITEM_NVGRE_TNI,
146         ITEM_MPLS,
147         ITEM_MPLS_LABEL,
148         ITEM_GRE,
149         ITEM_GRE_PROTO,
150         ITEM_FUZZY,
151         ITEM_FUZZY_THRESH,
152         ITEM_GTP,
153         ITEM_GTP_TEID,
154         ITEM_GTPC,
155         ITEM_GTPU,
156         ITEM_GENEVE,
157         ITEM_GENEVE_VNI,
158         ITEM_GENEVE_PROTO,
159         ITEM_VXLAN_GPE,
160         ITEM_VXLAN_GPE_VNI,
161         ITEM_ARP_ETH_IPV4,
162         ITEM_ARP_ETH_IPV4_SHA,
163         ITEM_ARP_ETH_IPV4_SPA,
164         ITEM_ARP_ETH_IPV4_THA,
165         ITEM_ARP_ETH_IPV4_TPA,
166         ITEM_IPV6_EXT,
167         ITEM_IPV6_EXT_NEXT_HDR,
168         ITEM_ICMP6,
169         ITEM_ICMP6_TYPE,
170         ITEM_ICMP6_CODE,
171         ITEM_ICMP6_ND_NS,
172         ITEM_ICMP6_ND_NS_TARGET_ADDR,
173         ITEM_ICMP6_ND_NA,
174         ITEM_ICMP6_ND_NA_TARGET_ADDR,
175         ITEM_ICMP6_ND_OPT,
176         ITEM_ICMP6_ND_OPT_TYPE,
177         ITEM_ICMP6_ND_OPT_SLA_ETH,
178         ITEM_ICMP6_ND_OPT_SLA_ETH_SLA,
179         ITEM_ICMP6_ND_OPT_TLA_ETH,
180         ITEM_ICMP6_ND_OPT_TLA_ETH_TLA,
181
182         /* Validate/create actions. */
183         ACTIONS,
184         ACTION_NEXT,
185         ACTION_END,
186         ACTION_VOID,
187         ACTION_PASSTHRU,
188         ACTION_JUMP,
189         ACTION_JUMP_GROUP,
190         ACTION_MARK,
191         ACTION_MARK_ID,
192         ACTION_FLAG,
193         ACTION_QUEUE,
194         ACTION_QUEUE_INDEX,
195         ACTION_DROP,
196         ACTION_COUNT,
197         ACTION_COUNT_SHARED,
198         ACTION_COUNT_ID,
199         ACTION_RSS,
200         ACTION_RSS_FUNC,
201         ACTION_RSS_LEVEL,
202         ACTION_RSS_FUNC_DEFAULT,
203         ACTION_RSS_FUNC_TOEPLITZ,
204         ACTION_RSS_FUNC_SIMPLE_XOR,
205         ACTION_RSS_TYPES,
206         ACTION_RSS_TYPE,
207         ACTION_RSS_KEY,
208         ACTION_RSS_KEY_LEN,
209         ACTION_RSS_QUEUES,
210         ACTION_RSS_QUEUE,
211         ACTION_PF,
212         ACTION_VF,
213         ACTION_VF_ORIGINAL,
214         ACTION_VF_ID,
215         ACTION_PHY_PORT,
216         ACTION_PHY_PORT_ORIGINAL,
217         ACTION_PHY_PORT_INDEX,
218         ACTION_PORT_ID,
219         ACTION_PORT_ID_ORIGINAL,
220         ACTION_PORT_ID_ID,
221         ACTION_METER,
222         ACTION_METER_ID,
223         ACTION_OF_SET_MPLS_TTL,
224         ACTION_OF_SET_MPLS_TTL_MPLS_TTL,
225         ACTION_OF_DEC_MPLS_TTL,
226         ACTION_OF_SET_NW_TTL,
227         ACTION_OF_SET_NW_TTL_NW_TTL,
228         ACTION_OF_DEC_NW_TTL,
229         ACTION_OF_COPY_TTL_OUT,
230         ACTION_OF_COPY_TTL_IN,
231         ACTION_OF_POP_VLAN,
232         ACTION_OF_PUSH_VLAN,
233         ACTION_OF_PUSH_VLAN_ETHERTYPE,
234         ACTION_OF_SET_VLAN_VID,
235         ACTION_OF_SET_VLAN_VID_VLAN_VID,
236         ACTION_OF_SET_VLAN_PCP,
237         ACTION_OF_SET_VLAN_PCP_VLAN_PCP,
238         ACTION_OF_POP_MPLS,
239         ACTION_OF_POP_MPLS_ETHERTYPE,
240         ACTION_OF_PUSH_MPLS,
241         ACTION_OF_PUSH_MPLS_ETHERTYPE,
242         ACTION_VXLAN_ENCAP,
243         ACTION_VXLAN_DECAP,
244 };
245
246 /** Maximum size for pattern in struct rte_flow_item_raw. */
247 #define ITEM_RAW_PATTERN_SIZE 40
248
249 /** Storage size for struct rte_flow_item_raw including pattern. */
250 #define ITEM_RAW_SIZE \
251         (sizeof(struct rte_flow_item_raw) + ITEM_RAW_PATTERN_SIZE)
252
253 /** Maximum number of queue indices in struct rte_flow_action_rss. */
254 #define ACTION_RSS_QUEUE_NUM 32
255
256 /** Storage for struct rte_flow_action_rss including external data. */
257 struct action_rss_data {
258         struct rte_flow_action_rss conf;
259         uint8_t key[RSS_HASH_KEY_LENGTH];
260         uint16_t queue[ACTION_RSS_QUEUE_NUM];
261 };
262
263 /** Maximum number of items in struct rte_flow_action_vxlan_encap. */
264 #define ACTION_VXLAN_ENCAP_ITEMS_NUM 6
265
266 /** Storage for struct rte_flow_action_vxlan_encap including external data. */
267 struct action_vxlan_encap_data {
268         struct rte_flow_action_vxlan_encap conf;
269         struct rte_flow_item items[ACTION_VXLAN_ENCAP_ITEMS_NUM];
270         struct rte_flow_item_eth item_eth;
271         struct rte_flow_item_vlan item_vlan;
272         union {
273                 struct rte_flow_item_ipv4 item_ipv4;
274                 struct rte_flow_item_ipv6 item_ipv6;
275         };
276         struct rte_flow_item_udp item_udp;
277         struct rte_flow_item_vxlan item_vxlan;
278 };
279
280 /** Maximum number of subsequent tokens and arguments on the stack. */
281 #define CTX_STACK_SIZE 16
282
283 /** Parser context. */
284 struct context {
285         /** Stack of subsequent token lists to process. */
286         const enum index *next[CTX_STACK_SIZE];
287         /** Arguments for stacked tokens. */
288         const void *args[CTX_STACK_SIZE];
289         enum index curr; /**< Current token index. */
290         enum index prev; /**< Index of the last token seen. */
291         int next_num; /**< Number of entries in next[]. */
292         int args_num; /**< Number of entries in args[]. */
293         uint32_t eol:1; /**< EOL has been detected. */
294         uint32_t last:1; /**< No more arguments. */
295         portid_t port; /**< Current port ID (for completions). */
296         uint32_t objdata; /**< Object-specific data. */
297         void *object; /**< Address of current object for relative offsets. */
298         void *objmask; /**< Object a full mask must be written to. */
299 };
300
301 /** Token argument. */
302 struct arg {
303         uint32_t hton:1; /**< Use network byte ordering. */
304         uint32_t sign:1; /**< Value is signed. */
305         uint32_t bounded:1; /**< Value is bounded. */
306         uintmax_t min; /**< Minimum value if bounded. */
307         uintmax_t max; /**< Maximum value if bounded. */
308         uint32_t offset; /**< Relative offset from ctx->object. */
309         uint32_t size; /**< Field size. */
310         const uint8_t *mask; /**< Bit-mask to use instead of offset/size. */
311 };
312
313 /** Parser token definition. */
314 struct token {
315         /** Type displayed during completion (defaults to "TOKEN"). */
316         const char *type;
317         /** Help displayed during completion (defaults to token name). */
318         const char *help;
319         /** Private data used by parser functions. */
320         const void *priv;
321         /**
322          * Lists of subsequent tokens to push on the stack. Each call to the
323          * parser consumes the last entry of that stack.
324          */
325         const enum index *const *next;
326         /** Arguments stack for subsequent tokens that need them. */
327         const struct arg *const *args;
328         /**
329          * Token-processing callback, returns -1 in case of error, the
330          * length of the matched string otherwise. If NULL, attempts to
331          * match the token name.
332          *
333          * If buf is not NULL, the result should be stored in it according
334          * to context. An error is returned if not large enough.
335          */
336         int (*call)(struct context *ctx, const struct token *token,
337                     const char *str, unsigned int len,
338                     void *buf, unsigned int size);
339         /**
340          * Callback that provides possible values for this token, used for
341          * completion. Returns -1 in case of error, the number of possible
342          * values otherwise. If NULL, the token name is used.
343          *
344          * If buf is not NULL, entry index ent is written to buf and the
345          * full length of the entry is returned (same behavior as
346          * snprintf()).
347          */
348         int (*comp)(struct context *ctx, const struct token *token,
349                     unsigned int ent, char *buf, unsigned int size);
350         /** Mandatory token name, no default value. */
351         const char *name;
352 };
353
354 /** Static initializer for the next field. */
355 #define NEXT(...) (const enum index *const []){ __VA_ARGS__, NULL, }
356
357 /** Static initializer for a NEXT() entry. */
358 #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, }
359
360 /** Static initializer for the args field. */
361 #define ARGS(...) (const struct arg *const []){ __VA_ARGS__, NULL, }
362
363 /** Static initializer for ARGS() to target a field. */
364 #define ARGS_ENTRY(s, f) \
365         (&(const struct arg){ \
366                 .offset = offsetof(s, f), \
367                 .size = sizeof(((s *)0)->f), \
368         })
369
370 /** Static initializer for ARGS() to target a bit-field. */
371 #define ARGS_ENTRY_BF(s, f, b) \
372         (&(const struct arg){ \
373                 .size = sizeof(s), \
374                 .mask = (const void *)&(const s){ .f = (1 << (b)) - 1 }, \
375         })
376
377 /** Static initializer for ARGS() to target an arbitrary bit-mask. */
378 #define ARGS_ENTRY_MASK(s, f, m) \
379         (&(const struct arg){ \
380                 .offset = offsetof(s, f), \
381                 .size = sizeof(((s *)0)->f), \
382                 .mask = (const void *)(m), \
383         })
384
385 /** Same as ARGS_ENTRY_MASK() using network byte ordering for the value. */
386 #define ARGS_ENTRY_MASK_HTON(s, f, m) \
387         (&(const struct arg){ \
388                 .hton = 1, \
389                 .offset = offsetof(s, f), \
390                 .size = sizeof(((s *)0)->f), \
391                 .mask = (const void *)(m), \
392         })
393
394 /** Static initializer for ARGS() to target a pointer. */
395 #define ARGS_ENTRY_PTR(s, f) \
396         (&(const struct arg){ \
397                 .size = sizeof(*((s *)0)->f), \
398         })
399
400 /** Static initializer for ARGS() with arbitrary offset and size. */
401 #define ARGS_ENTRY_ARB(o, s) \
402         (&(const struct arg){ \
403                 .offset = (o), \
404                 .size = (s), \
405         })
406
407 /** Same as ARGS_ENTRY_ARB() with bounded values. */
408 #define ARGS_ENTRY_ARB_BOUNDED(o, s, i, a) \
409         (&(const struct arg){ \
410                 .bounded = 1, \
411                 .min = (i), \
412                 .max = (a), \
413                 .offset = (o), \
414                 .size = (s), \
415         })
416
417 /** Same as ARGS_ENTRY() using network byte ordering. */
418 #define ARGS_ENTRY_HTON(s, f) \
419         (&(const struct arg){ \
420                 .hton = 1, \
421                 .offset = offsetof(s, f), \
422                 .size = sizeof(((s *)0)->f), \
423         })
424
425 /** Parser output buffer layout expected by cmd_flow_parsed(). */
426 struct buffer {
427         enum index command; /**< Flow command. */
428         portid_t port; /**< Affected port ID. */
429         union {
430                 struct {
431                         struct rte_flow_attr attr;
432                         struct rte_flow_item *pattern;
433                         struct rte_flow_action *actions;
434                         uint32_t pattern_n;
435                         uint32_t actions_n;
436                         uint8_t *data;
437                 } vc; /**< Validate/create arguments. */
438                 struct {
439                         uint32_t *rule;
440                         uint32_t rule_n;
441                 } destroy; /**< Destroy arguments. */
442                 struct {
443                         uint32_t rule;
444                         struct rte_flow_action action;
445                 } query; /**< Query arguments. */
446                 struct {
447                         uint32_t *group;
448                         uint32_t group_n;
449                 } list; /**< List arguments. */
450                 struct {
451                         int set;
452                 } isolate; /**< Isolated mode arguments. */
453         } args; /**< Command arguments. */
454 };
455
456 /** Private data for pattern items. */
457 struct parse_item_priv {
458         enum rte_flow_item_type type; /**< Item type. */
459         uint32_t size; /**< Size of item specification structure. */
460 };
461
462 #define PRIV_ITEM(t, s) \
463         (&(const struct parse_item_priv){ \
464                 .type = RTE_FLOW_ITEM_TYPE_ ## t, \
465                 .size = s, \
466         })
467
468 /** Private data for actions. */
469 struct parse_action_priv {
470         enum rte_flow_action_type type; /**< Action type. */
471         uint32_t size; /**< Size of action configuration structure. */
472 };
473
474 #define PRIV_ACTION(t, s) \
475         (&(const struct parse_action_priv){ \
476                 .type = RTE_FLOW_ACTION_TYPE_ ## t, \
477                 .size = s, \
478         })
479
480 static const enum index next_vc_attr[] = {
481         GROUP,
482         PRIORITY,
483         INGRESS,
484         EGRESS,
485         TRANSFER,
486         PATTERN,
487         ZERO,
488 };
489
490 static const enum index next_destroy_attr[] = {
491         DESTROY_RULE,
492         END,
493         ZERO,
494 };
495
496 static const enum index next_list_attr[] = {
497         LIST_GROUP,
498         END,
499         ZERO,
500 };
501
502 static const enum index item_param[] = {
503         ITEM_PARAM_IS,
504         ITEM_PARAM_SPEC,
505         ITEM_PARAM_LAST,
506         ITEM_PARAM_MASK,
507         ITEM_PARAM_PREFIX,
508         ZERO,
509 };
510
511 static const enum index next_item[] = {
512         ITEM_END,
513         ITEM_VOID,
514         ITEM_INVERT,
515         ITEM_ANY,
516         ITEM_PF,
517         ITEM_VF,
518         ITEM_PHY_PORT,
519         ITEM_PORT_ID,
520         ITEM_MARK,
521         ITEM_RAW,
522         ITEM_ETH,
523         ITEM_VLAN,
524         ITEM_IPV4,
525         ITEM_IPV6,
526         ITEM_ICMP,
527         ITEM_UDP,
528         ITEM_TCP,
529         ITEM_SCTP,
530         ITEM_VXLAN,
531         ITEM_E_TAG,
532         ITEM_NVGRE,
533         ITEM_MPLS,
534         ITEM_GRE,
535         ITEM_FUZZY,
536         ITEM_GTP,
537         ITEM_GTPC,
538         ITEM_GTPU,
539         ITEM_GENEVE,
540         ITEM_VXLAN_GPE,
541         ITEM_ARP_ETH_IPV4,
542         ITEM_IPV6_EXT,
543         ITEM_ICMP6,
544         ITEM_ICMP6_ND_NS,
545         ITEM_ICMP6_ND_NA,
546         ITEM_ICMP6_ND_OPT,
547         ITEM_ICMP6_ND_OPT_SLA_ETH,
548         ITEM_ICMP6_ND_OPT_TLA_ETH,
549         ZERO,
550 };
551
552 static const enum index item_fuzzy[] = {
553         ITEM_FUZZY_THRESH,
554         ITEM_NEXT,
555         ZERO,
556 };
557
558 static const enum index item_any[] = {
559         ITEM_ANY_NUM,
560         ITEM_NEXT,
561         ZERO,
562 };
563
564 static const enum index item_vf[] = {
565         ITEM_VF_ID,
566         ITEM_NEXT,
567         ZERO,
568 };
569
570 static const enum index item_phy_port[] = {
571         ITEM_PHY_PORT_INDEX,
572         ITEM_NEXT,
573         ZERO,
574 };
575
576 static const enum index item_port_id[] = {
577         ITEM_PORT_ID_ID,
578         ITEM_NEXT,
579         ZERO,
580 };
581
582 static const enum index item_mark[] = {
583         ITEM_MARK_ID,
584         ITEM_NEXT,
585         ZERO,
586 };
587
588 static const enum index item_raw[] = {
589         ITEM_RAW_RELATIVE,
590         ITEM_RAW_SEARCH,
591         ITEM_RAW_OFFSET,
592         ITEM_RAW_LIMIT,
593         ITEM_RAW_PATTERN,
594         ITEM_NEXT,
595         ZERO,
596 };
597
598 static const enum index item_eth[] = {
599         ITEM_ETH_DST,
600         ITEM_ETH_SRC,
601         ITEM_ETH_TYPE,
602         ITEM_NEXT,
603         ZERO,
604 };
605
606 static const enum index item_vlan[] = {
607         ITEM_VLAN_TCI,
608         ITEM_VLAN_PCP,
609         ITEM_VLAN_DEI,
610         ITEM_VLAN_VID,
611         ITEM_VLAN_INNER_TYPE,
612         ITEM_NEXT,
613         ZERO,
614 };
615
616 static const enum index item_ipv4[] = {
617         ITEM_IPV4_TOS,
618         ITEM_IPV4_TTL,
619         ITEM_IPV4_PROTO,
620         ITEM_IPV4_SRC,
621         ITEM_IPV4_DST,
622         ITEM_NEXT,
623         ZERO,
624 };
625
626 static const enum index item_ipv6[] = {
627         ITEM_IPV6_TC,
628         ITEM_IPV6_FLOW,
629         ITEM_IPV6_PROTO,
630         ITEM_IPV6_HOP,
631         ITEM_IPV6_SRC,
632         ITEM_IPV6_DST,
633         ITEM_NEXT,
634         ZERO,
635 };
636
637 static const enum index item_icmp[] = {
638         ITEM_ICMP_TYPE,
639         ITEM_ICMP_CODE,
640         ITEM_NEXT,
641         ZERO,
642 };
643
644 static const enum index item_udp[] = {
645         ITEM_UDP_SRC,
646         ITEM_UDP_DST,
647         ITEM_NEXT,
648         ZERO,
649 };
650
651 static const enum index item_tcp[] = {
652         ITEM_TCP_SRC,
653         ITEM_TCP_DST,
654         ITEM_TCP_FLAGS,
655         ITEM_NEXT,
656         ZERO,
657 };
658
659 static const enum index item_sctp[] = {
660         ITEM_SCTP_SRC,
661         ITEM_SCTP_DST,
662         ITEM_SCTP_TAG,
663         ITEM_SCTP_CKSUM,
664         ITEM_NEXT,
665         ZERO,
666 };
667
668 static const enum index item_vxlan[] = {
669         ITEM_VXLAN_VNI,
670         ITEM_NEXT,
671         ZERO,
672 };
673
674 static const enum index item_e_tag[] = {
675         ITEM_E_TAG_GRP_ECID_B,
676         ITEM_NEXT,
677         ZERO,
678 };
679
680 static const enum index item_nvgre[] = {
681         ITEM_NVGRE_TNI,
682         ITEM_NEXT,
683         ZERO,
684 };
685
686 static const enum index item_mpls[] = {
687         ITEM_MPLS_LABEL,
688         ITEM_NEXT,
689         ZERO,
690 };
691
692 static const enum index item_gre[] = {
693         ITEM_GRE_PROTO,
694         ITEM_NEXT,
695         ZERO,
696 };
697
698 static const enum index item_gtp[] = {
699         ITEM_GTP_TEID,
700         ITEM_NEXT,
701         ZERO,
702 };
703
704 static const enum index item_geneve[] = {
705         ITEM_GENEVE_VNI,
706         ITEM_GENEVE_PROTO,
707         ITEM_NEXT,
708         ZERO,
709 };
710
711 static const enum index item_vxlan_gpe[] = {
712         ITEM_VXLAN_GPE_VNI,
713         ITEM_NEXT,
714         ZERO,
715 };
716
717 static const enum index item_arp_eth_ipv4[] = {
718         ITEM_ARP_ETH_IPV4_SHA,
719         ITEM_ARP_ETH_IPV4_SPA,
720         ITEM_ARP_ETH_IPV4_THA,
721         ITEM_ARP_ETH_IPV4_TPA,
722         ITEM_NEXT,
723         ZERO,
724 };
725
726 static const enum index item_ipv6_ext[] = {
727         ITEM_IPV6_EXT_NEXT_HDR,
728         ITEM_NEXT,
729         ZERO,
730 };
731
732 static const enum index item_icmp6[] = {
733         ITEM_ICMP6_TYPE,
734         ITEM_ICMP6_CODE,
735         ITEM_NEXT,
736         ZERO,
737 };
738
739 static const enum index item_icmp6_nd_ns[] = {
740         ITEM_ICMP6_ND_NS_TARGET_ADDR,
741         ITEM_NEXT,
742         ZERO,
743 };
744
745 static const enum index item_icmp6_nd_na[] = {
746         ITEM_ICMP6_ND_NA_TARGET_ADDR,
747         ITEM_NEXT,
748         ZERO,
749 };
750
751 static const enum index item_icmp6_nd_opt[] = {
752         ITEM_ICMP6_ND_OPT_TYPE,
753         ITEM_NEXT,
754         ZERO,
755 };
756
757 static const enum index item_icmp6_nd_opt_sla_eth[] = {
758         ITEM_ICMP6_ND_OPT_SLA_ETH_SLA,
759         ITEM_NEXT,
760         ZERO,
761 };
762
763 static const enum index item_icmp6_nd_opt_tla_eth[] = {
764         ITEM_ICMP6_ND_OPT_TLA_ETH_TLA,
765         ITEM_NEXT,
766         ZERO,
767 };
768
769 static const enum index next_action[] = {
770         ACTION_END,
771         ACTION_VOID,
772         ACTION_PASSTHRU,
773         ACTION_JUMP,
774         ACTION_MARK,
775         ACTION_FLAG,
776         ACTION_QUEUE,
777         ACTION_DROP,
778         ACTION_COUNT,
779         ACTION_RSS,
780         ACTION_PF,
781         ACTION_VF,
782         ACTION_PHY_PORT,
783         ACTION_PORT_ID,
784         ACTION_METER,
785         ACTION_OF_SET_MPLS_TTL,
786         ACTION_OF_DEC_MPLS_TTL,
787         ACTION_OF_SET_NW_TTL,
788         ACTION_OF_DEC_NW_TTL,
789         ACTION_OF_COPY_TTL_OUT,
790         ACTION_OF_COPY_TTL_IN,
791         ACTION_OF_POP_VLAN,
792         ACTION_OF_PUSH_VLAN,
793         ACTION_OF_SET_VLAN_VID,
794         ACTION_OF_SET_VLAN_PCP,
795         ACTION_OF_POP_MPLS,
796         ACTION_OF_PUSH_MPLS,
797         ACTION_VXLAN_ENCAP,
798         ACTION_VXLAN_DECAP,
799         ZERO,
800 };
801
802 static const enum index action_mark[] = {
803         ACTION_MARK_ID,
804         ACTION_NEXT,
805         ZERO,
806 };
807
808 static const enum index action_queue[] = {
809         ACTION_QUEUE_INDEX,
810         ACTION_NEXT,
811         ZERO,
812 };
813
814 static const enum index action_count[] = {
815         ACTION_COUNT_ID,
816         ACTION_COUNT_SHARED,
817         ACTION_NEXT,
818         ZERO,
819 };
820
821 static const enum index action_rss[] = {
822         ACTION_RSS_FUNC,
823         ACTION_RSS_LEVEL,
824         ACTION_RSS_TYPES,
825         ACTION_RSS_KEY,
826         ACTION_RSS_KEY_LEN,
827         ACTION_RSS_QUEUES,
828         ACTION_NEXT,
829         ZERO,
830 };
831
832 static const enum index action_vf[] = {
833         ACTION_VF_ORIGINAL,
834         ACTION_VF_ID,
835         ACTION_NEXT,
836         ZERO,
837 };
838
839 static const enum index action_phy_port[] = {
840         ACTION_PHY_PORT_ORIGINAL,
841         ACTION_PHY_PORT_INDEX,
842         ACTION_NEXT,
843         ZERO,
844 };
845
846 static const enum index action_port_id[] = {
847         ACTION_PORT_ID_ORIGINAL,
848         ACTION_PORT_ID_ID,
849         ACTION_NEXT,
850         ZERO,
851 };
852
853 static const enum index action_meter[] = {
854         ACTION_METER_ID,
855         ACTION_NEXT,
856         ZERO,
857 };
858
859 static const enum index action_of_set_mpls_ttl[] = {
860         ACTION_OF_SET_MPLS_TTL_MPLS_TTL,
861         ACTION_NEXT,
862         ZERO,
863 };
864
865 static const enum index action_of_set_nw_ttl[] = {
866         ACTION_OF_SET_NW_TTL_NW_TTL,
867         ACTION_NEXT,
868         ZERO,
869 };
870
871 static const enum index action_of_push_vlan[] = {
872         ACTION_OF_PUSH_VLAN_ETHERTYPE,
873         ACTION_NEXT,
874         ZERO,
875 };
876
877 static const enum index action_of_set_vlan_vid[] = {
878         ACTION_OF_SET_VLAN_VID_VLAN_VID,
879         ACTION_NEXT,
880         ZERO,
881 };
882
883 static const enum index action_of_set_vlan_pcp[] = {
884         ACTION_OF_SET_VLAN_PCP_VLAN_PCP,
885         ACTION_NEXT,
886         ZERO,
887 };
888
889 static const enum index action_of_pop_mpls[] = {
890         ACTION_OF_POP_MPLS_ETHERTYPE,
891         ACTION_NEXT,
892         ZERO,
893 };
894
895 static const enum index action_of_push_mpls[] = {
896         ACTION_OF_PUSH_MPLS_ETHERTYPE,
897         ACTION_NEXT,
898         ZERO,
899 };
900
901 static const enum index action_jump[] = {
902         ACTION_JUMP_GROUP,
903         ACTION_NEXT,
904         ZERO,
905 };
906
907 static int parse_init(struct context *, const struct token *,
908                       const char *, unsigned int,
909                       void *, unsigned int);
910 static int parse_vc(struct context *, const struct token *,
911                     const char *, unsigned int,
912                     void *, unsigned int);
913 static int parse_vc_spec(struct context *, const struct token *,
914                          const char *, unsigned int, void *, unsigned int);
915 static int parse_vc_conf(struct context *, const struct token *,
916                          const char *, unsigned int, void *, unsigned int);
917 static int parse_vc_action_rss(struct context *, const struct token *,
918                                const char *, unsigned int, void *,
919                                unsigned int);
920 static int parse_vc_action_rss_func(struct context *, const struct token *,
921                                     const char *, unsigned int, void *,
922                                     unsigned int);
923 static int parse_vc_action_rss_type(struct context *, const struct token *,
924                                     const char *, unsigned int, void *,
925                                     unsigned int);
926 static int parse_vc_action_rss_queue(struct context *, const struct token *,
927                                      const char *, unsigned int, void *,
928                                      unsigned int);
929 static int parse_vc_action_vxlan_encap(struct context *, const struct token *,
930                                        const char *, unsigned int, void *,
931                                        unsigned int);
932 static int parse_destroy(struct context *, const struct token *,
933                          const char *, unsigned int,
934                          void *, unsigned int);
935 static int parse_flush(struct context *, const struct token *,
936                        const char *, unsigned int,
937                        void *, unsigned int);
938 static int parse_query(struct context *, const struct token *,
939                        const char *, unsigned int,
940                        void *, unsigned int);
941 static int parse_action(struct context *, const struct token *,
942                         const char *, unsigned int,
943                         void *, unsigned int);
944 static int parse_list(struct context *, const struct token *,
945                       const char *, unsigned int,
946                       void *, unsigned int);
947 static int parse_isolate(struct context *, const struct token *,
948                          const char *, unsigned int,
949                          void *, unsigned int);
950 static int parse_int(struct context *, const struct token *,
951                      const char *, unsigned int,
952                      void *, unsigned int);
953 static int parse_prefix(struct context *, const struct token *,
954                         const char *, unsigned int,
955                         void *, unsigned int);
956 static int parse_boolean(struct context *, const struct token *,
957                          const char *, unsigned int,
958                          void *, unsigned int);
959 static int parse_string(struct context *, const struct token *,
960                         const char *, unsigned int,
961                         void *, unsigned int);
962 static int parse_mac_addr(struct context *, const struct token *,
963                           const char *, unsigned int,
964                           void *, unsigned int);
965 static int parse_ipv4_addr(struct context *, const struct token *,
966                            const char *, unsigned int,
967                            void *, unsigned int);
968 static int parse_ipv6_addr(struct context *, const struct token *,
969                            const char *, unsigned int,
970                            void *, unsigned int);
971 static int parse_port(struct context *, const struct token *,
972                       const char *, unsigned int,
973                       void *, unsigned int);
974 static int comp_none(struct context *, const struct token *,
975                      unsigned int, char *, unsigned int);
976 static int comp_boolean(struct context *, const struct token *,
977                         unsigned int, char *, unsigned int);
978 static int comp_action(struct context *, const struct token *,
979                        unsigned int, char *, unsigned int);
980 static int comp_port(struct context *, const struct token *,
981                      unsigned int, char *, unsigned int);
982 static int comp_rule_id(struct context *, const struct token *,
983                         unsigned int, char *, unsigned int);
984 static int comp_vc_action_rss_type(struct context *, const struct token *,
985                                    unsigned int, char *, unsigned int);
986 static int comp_vc_action_rss_queue(struct context *, const struct token *,
987                                     unsigned int, char *, unsigned int);
988
989 /** Token definitions. */
990 static const struct token token_list[] = {
991         /* Special tokens. */
992         [ZERO] = {
993                 .name = "ZERO",
994                 .help = "null entry, abused as the entry point",
995                 .next = NEXT(NEXT_ENTRY(FLOW)),
996         },
997         [END] = {
998                 .name = "",
999                 .type = "RETURN",
1000                 .help = "command may end here",
1001         },
1002         /* Common tokens. */
1003         [INTEGER] = {
1004                 .name = "{int}",
1005                 .type = "INTEGER",
1006                 .help = "integer value",
1007                 .call = parse_int,
1008                 .comp = comp_none,
1009         },
1010         [UNSIGNED] = {
1011                 .name = "{unsigned}",
1012                 .type = "UNSIGNED",
1013                 .help = "unsigned integer value",
1014                 .call = parse_int,
1015                 .comp = comp_none,
1016         },
1017         [PREFIX] = {
1018                 .name = "{prefix}",
1019                 .type = "PREFIX",
1020                 .help = "prefix length for bit-mask",
1021                 .call = parse_prefix,
1022                 .comp = comp_none,
1023         },
1024         [BOOLEAN] = {
1025                 .name = "{boolean}",
1026                 .type = "BOOLEAN",
1027                 .help = "any boolean value",
1028                 .call = parse_boolean,
1029                 .comp = comp_boolean,
1030         },
1031         [STRING] = {
1032                 .name = "{string}",
1033                 .type = "STRING",
1034                 .help = "fixed string",
1035                 .call = parse_string,
1036                 .comp = comp_none,
1037         },
1038         [MAC_ADDR] = {
1039                 .name = "{MAC address}",
1040                 .type = "MAC-48",
1041                 .help = "standard MAC address notation",
1042                 .call = parse_mac_addr,
1043                 .comp = comp_none,
1044         },
1045         [IPV4_ADDR] = {
1046                 .name = "{IPv4 address}",
1047                 .type = "IPV4 ADDRESS",
1048                 .help = "standard IPv4 address notation",
1049                 .call = parse_ipv4_addr,
1050                 .comp = comp_none,
1051         },
1052         [IPV6_ADDR] = {
1053                 .name = "{IPv6 address}",
1054                 .type = "IPV6 ADDRESS",
1055                 .help = "standard IPv6 address notation",
1056                 .call = parse_ipv6_addr,
1057                 .comp = comp_none,
1058         },
1059         [RULE_ID] = {
1060                 .name = "{rule id}",
1061                 .type = "RULE ID",
1062                 .help = "rule identifier",
1063                 .call = parse_int,
1064                 .comp = comp_rule_id,
1065         },
1066         [PORT_ID] = {
1067                 .name = "{port_id}",
1068                 .type = "PORT ID",
1069                 .help = "port identifier",
1070                 .call = parse_port,
1071                 .comp = comp_port,
1072         },
1073         [GROUP_ID] = {
1074                 .name = "{group_id}",
1075                 .type = "GROUP ID",
1076                 .help = "group identifier",
1077                 .call = parse_int,
1078                 .comp = comp_none,
1079         },
1080         [PRIORITY_LEVEL] = {
1081                 .name = "{level}",
1082                 .type = "PRIORITY",
1083                 .help = "priority level",
1084                 .call = parse_int,
1085                 .comp = comp_none,
1086         },
1087         /* Top-level command. */
1088         [FLOW] = {
1089                 .name = "flow",
1090                 .type = "{command} {port_id} [{arg} [...]]",
1091                 .help = "manage ingress/egress flow rules",
1092                 .next = NEXT(NEXT_ENTRY
1093                              (VALIDATE,
1094                               CREATE,
1095                               DESTROY,
1096                               FLUSH,
1097                               LIST,
1098                               QUERY,
1099                               ISOLATE)),
1100                 .call = parse_init,
1101         },
1102         /* Sub-level commands. */
1103         [VALIDATE] = {
1104                 .name = "validate",
1105                 .help = "check whether a flow rule can be created",
1106                 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
1107                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
1108                 .call = parse_vc,
1109         },
1110         [CREATE] = {
1111                 .name = "create",
1112                 .help = "create a flow rule",
1113                 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
1114                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
1115                 .call = parse_vc,
1116         },
1117         [DESTROY] = {
1118                 .name = "destroy",
1119                 .help = "destroy specific flow rules",
1120                 .next = NEXT(NEXT_ENTRY(DESTROY_RULE), NEXT_ENTRY(PORT_ID)),
1121                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
1122                 .call = parse_destroy,
1123         },
1124         [FLUSH] = {
1125                 .name = "flush",
1126                 .help = "destroy all flow rules",
1127                 .next = NEXT(NEXT_ENTRY(PORT_ID)),
1128                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
1129                 .call = parse_flush,
1130         },
1131         [QUERY] = {
1132                 .name = "query",
1133                 .help = "query an existing flow rule",
1134                 .next = NEXT(NEXT_ENTRY(QUERY_ACTION),
1135                              NEXT_ENTRY(RULE_ID),
1136                              NEXT_ENTRY(PORT_ID)),
1137                 .args = ARGS(ARGS_ENTRY(struct buffer, args.query.action.type),
1138                              ARGS_ENTRY(struct buffer, args.query.rule),
1139                              ARGS_ENTRY(struct buffer, port)),
1140                 .call = parse_query,
1141         },
1142         [LIST] = {
1143                 .name = "list",
1144                 .help = "list existing flow rules",
1145                 .next = NEXT(next_list_attr, NEXT_ENTRY(PORT_ID)),
1146                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
1147                 .call = parse_list,
1148         },
1149         [ISOLATE] = {
1150                 .name = "isolate",
1151                 .help = "restrict ingress traffic to the defined flow rules",
1152                 .next = NEXT(NEXT_ENTRY(BOOLEAN),
1153                              NEXT_ENTRY(PORT_ID)),
1154                 .args = ARGS(ARGS_ENTRY(struct buffer, args.isolate.set),
1155                              ARGS_ENTRY(struct buffer, port)),
1156                 .call = parse_isolate,
1157         },
1158         /* Destroy arguments. */
1159         [DESTROY_RULE] = {
1160                 .name = "rule",
1161                 .help = "specify a rule identifier",
1162                 .next = NEXT(next_destroy_attr, NEXT_ENTRY(RULE_ID)),
1163                 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.destroy.rule)),
1164                 .call = parse_destroy,
1165         },
1166         /* Query arguments. */
1167         [QUERY_ACTION] = {
1168                 .name = "{action}",
1169                 .type = "ACTION",
1170                 .help = "action to query, must be part of the rule",
1171                 .call = parse_action,
1172                 .comp = comp_action,
1173         },
1174         /* List arguments. */
1175         [LIST_GROUP] = {
1176                 .name = "group",
1177                 .help = "specify a group",
1178                 .next = NEXT(next_list_attr, NEXT_ENTRY(GROUP_ID)),
1179                 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.list.group)),
1180                 .call = parse_list,
1181         },
1182         /* Validate/create attributes. */
1183         [GROUP] = {
1184                 .name = "group",
1185                 .help = "specify a group",
1186                 .next = NEXT(next_vc_attr, NEXT_ENTRY(GROUP_ID)),
1187                 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, group)),
1188                 .call = parse_vc,
1189         },
1190         [PRIORITY] = {
1191                 .name = "priority",
1192                 .help = "specify a priority level",
1193                 .next = NEXT(next_vc_attr, NEXT_ENTRY(PRIORITY_LEVEL)),
1194                 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, priority)),
1195                 .call = parse_vc,
1196         },
1197         [INGRESS] = {
1198                 .name = "ingress",
1199                 .help = "affect rule to ingress",
1200                 .next = NEXT(next_vc_attr),
1201                 .call = parse_vc,
1202         },
1203         [EGRESS] = {
1204                 .name = "egress",
1205                 .help = "affect rule to egress",
1206                 .next = NEXT(next_vc_attr),
1207                 .call = parse_vc,
1208         },
1209         [TRANSFER] = {
1210                 .name = "transfer",
1211                 .help = "apply rule directly to endpoints found in pattern",
1212                 .next = NEXT(next_vc_attr),
1213                 .call = parse_vc,
1214         },
1215         /* Validate/create pattern. */
1216         [PATTERN] = {
1217                 .name = "pattern",
1218                 .help = "submit a list of pattern items",
1219                 .next = NEXT(next_item),
1220                 .call = parse_vc,
1221         },
1222         [ITEM_PARAM_IS] = {
1223                 .name = "is",
1224                 .help = "match value perfectly (with full bit-mask)",
1225                 .call = parse_vc_spec,
1226         },
1227         [ITEM_PARAM_SPEC] = {
1228                 .name = "spec",
1229                 .help = "match value according to configured bit-mask",
1230                 .call = parse_vc_spec,
1231         },
1232         [ITEM_PARAM_LAST] = {
1233                 .name = "last",
1234                 .help = "specify upper bound to establish a range",
1235                 .call = parse_vc_spec,
1236         },
1237         [ITEM_PARAM_MASK] = {
1238                 .name = "mask",
1239                 .help = "specify bit-mask with relevant bits set to one",
1240                 .call = parse_vc_spec,
1241         },
1242         [ITEM_PARAM_PREFIX] = {
1243                 .name = "prefix",
1244                 .help = "generate bit-mask from a prefix length",
1245                 .call = parse_vc_spec,
1246         },
1247         [ITEM_NEXT] = {
1248                 .name = "/",
1249                 .help = "specify next pattern item",
1250                 .next = NEXT(next_item),
1251         },
1252         [ITEM_END] = {
1253                 .name = "end",
1254                 .help = "end list of pattern items",
1255                 .priv = PRIV_ITEM(END, 0),
1256                 .next = NEXT(NEXT_ENTRY(ACTIONS)),
1257                 .call = parse_vc,
1258         },
1259         [ITEM_VOID] = {
1260                 .name = "void",
1261                 .help = "no-op pattern item",
1262                 .priv = PRIV_ITEM(VOID, 0),
1263                 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1264                 .call = parse_vc,
1265         },
1266         [ITEM_INVERT] = {
1267                 .name = "invert",
1268                 .help = "perform actions when pattern does not match",
1269                 .priv = PRIV_ITEM(INVERT, 0),
1270                 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1271                 .call = parse_vc,
1272         },
1273         [ITEM_ANY] = {
1274                 .name = "any",
1275                 .help = "match any protocol for the current layer",
1276                 .priv = PRIV_ITEM(ANY, sizeof(struct rte_flow_item_any)),
1277                 .next = NEXT(item_any),
1278                 .call = parse_vc,
1279         },
1280         [ITEM_ANY_NUM] = {
1281                 .name = "num",
1282                 .help = "number of layers covered",
1283                 .next = NEXT(item_any, NEXT_ENTRY(UNSIGNED), item_param),
1284                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_any, num)),
1285         },
1286         [ITEM_PF] = {
1287                 .name = "pf",
1288                 .help = "match traffic from/to the physical function",
1289                 .priv = PRIV_ITEM(PF, 0),
1290                 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1291                 .call = parse_vc,
1292         },
1293         [ITEM_VF] = {
1294                 .name = "vf",
1295                 .help = "match traffic from/to a virtual function ID",
1296                 .priv = PRIV_ITEM(VF, sizeof(struct rte_flow_item_vf)),
1297                 .next = NEXT(item_vf),
1298                 .call = parse_vc,
1299         },
1300         [ITEM_VF_ID] = {
1301                 .name = "id",
1302                 .help = "VF ID",
1303                 .next = NEXT(item_vf, NEXT_ENTRY(UNSIGNED), item_param),
1304                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_vf, id)),
1305         },
1306         [ITEM_PHY_PORT] = {
1307                 .name = "phy_port",
1308                 .help = "match traffic from/to a specific physical port",
1309                 .priv = PRIV_ITEM(PHY_PORT,
1310                                   sizeof(struct rte_flow_item_phy_port)),
1311                 .next = NEXT(item_phy_port),
1312                 .call = parse_vc,
1313         },
1314         [ITEM_PHY_PORT_INDEX] = {
1315                 .name = "index",
1316                 .help = "physical port index",
1317                 .next = NEXT(item_phy_port, NEXT_ENTRY(UNSIGNED), item_param),
1318                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_phy_port, index)),
1319         },
1320         [ITEM_PORT_ID] = {
1321                 .name = "port_id",
1322                 .help = "match traffic from/to a given DPDK port ID",
1323                 .priv = PRIV_ITEM(PORT_ID,
1324                                   sizeof(struct rte_flow_item_port_id)),
1325                 .next = NEXT(item_port_id),
1326                 .call = parse_vc,
1327         },
1328         [ITEM_PORT_ID_ID] = {
1329                 .name = "id",
1330                 .help = "DPDK port ID",
1331                 .next = NEXT(item_port_id, NEXT_ENTRY(UNSIGNED), item_param),
1332                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_port_id, id)),
1333         },
1334         [ITEM_MARK] = {
1335                 .name = "mark",
1336                 .help = "match traffic against value set in previously matched rule",
1337                 .priv = PRIV_ITEM(MARK, sizeof(struct rte_flow_item_mark)),
1338                 .next = NEXT(item_mark),
1339                 .call = parse_vc,
1340         },
1341         [ITEM_MARK_ID] = {
1342                 .name = "id",
1343                 .help = "Integer value to match against",
1344                 .next = NEXT(item_mark, NEXT_ENTRY(UNSIGNED), item_param),
1345                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_mark, id)),
1346         },
1347         [ITEM_RAW] = {
1348                 .name = "raw",
1349                 .help = "match an arbitrary byte string",
1350                 .priv = PRIV_ITEM(RAW, ITEM_RAW_SIZE),
1351                 .next = NEXT(item_raw),
1352                 .call = parse_vc,
1353         },
1354         [ITEM_RAW_RELATIVE] = {
1355                 .name = "relative",
1356                 .help = "look for pattern after the previous item",
1357                 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1358                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1359                                            relative, 1)),
1360         },
1361         [ITEM_RAW_SEARCH] = {
1362                 .name = "search",
1363                 .help = "search pattern from offset (see also limit)",
1364                 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1365                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1366                                            search, 1)),
1367         },
1368         [ITEM_RAW_OFFSET] = {
1369                 .name = "offset",
1370                 .help = "absolute or relative offset for pattern",
1371                 .next = NEXT(item_raw, NEXT_ENTRY(INTEGER), item_param),
1372                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, offset)),
1373         },
1374         [ITEM_RAW_LIMIT] = {
1375                 .name = "limit",
1376                 .help = "search area limit for start of pattern",
1377                 .next = NEXT(item_raw, NEXT_ENTRY(UNSIGNED), item_param),
1378                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, limit)),
1379         },
1380         [ITEM_RAW_PATTERN] = {
1381                 .name = "pattern",
1382                 .help = "byte string to look for",
1383                 .next = NEXT(item_raw,
1384                              NEXT_ENTRY(STRING),
1385                              NEXT_ENTRY(ITEM_PARAM_IS,
1386                                         ITEM_PARAM_SPEC,
1387                                         ITEM_PARAM_MASK)),
1388                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, pattern),
1389                              ARGS_ENTRY(struct rte_flow_item_raw, length),
1390                              ARGS_ENTRY_ARB(sizeof(struct rte_flow_item_raw),
1391                                             ITEM_RAW_PATTERN_SIZE)),
1392         },
1393         [ITEM_ETH] = {
1394                 .name = "eth",
1395                 .help = "match Ethernet header",
1396                 .priv = PRIV_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
1397                 .next = NEXT(item_eth),
1398                 .call = parse_vc,
1399         },
1400         [ITEM_ETH_DST] = {
1401                 .name = "dst",
1402                 .help = "destination MAC",
1403                 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1404                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, dst)),
1405         },
1406         [ITEM_ETH_SRC] = {
1407                 .name = "src",
1408                 .help = "source MAC",
1409                 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1410                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, src)),
1411         },
1412         [ITEM_ETH_TYPE] = {
1413                 .name = "type",
1414                 .help = "EtherType",
1415                 .next = NEXT(item_eth, NEXT_ENTRY(UNSIGNED), item_param),
1416                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, type)),
1417         },
1418         [ITEM_VLAN] = {
1419                 .name = "vlan",
1420                 .help = "match 802.1Q/ad VLAN tag",
1421                 .priv = PRIV_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
1422                 .next = NEXT(item_vlan),
1423                 .call = parse_vc,
1424         },
1425         [ITEM_VLAN_TCI] = {
1426                 .name = "tci",
1427                 .help = "tag control information",
1428                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1429                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tci)),
1430         },
1431         [ITEM_VLAN_PCP] = {
1432                 .name = "pcp",
1433                 .help = "priority code point",
1434                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1435                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1436                                                   tci, "\xe0\x00")),
1437         },
1438         [ITEM_VLAN_DEI] = {
1439                 .name = "dei",
1440                 .help = "drop eligible indicator",
1441                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1442                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1443                                                   tci, "\x10\x00")),
1444         },
1445         [ITEM_VLAN_VID] = {
1446                 .name = "vid",
1447                 .help = "VLAN identifier",
1448                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1449                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1450                                                   tci, "\x0f\xff")),
1451         },
1452         [ITEM_VLAN_INNER_TYPE] = {
1453                 .name = "inner_type",
1454                 .help = "inner EtherType",
1455                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1456                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan,
1457                                              inner_type)),
1458         },
1459         [ITEM_IPV4] = {
1460                 .name = "ipv4",
1461                 .help = "match IPv4 header",
1462                 .priv = PRIV_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
1463                 .next = NEXT(item_ipv4),
1464                 .call = parse_vc,
1465         },
1466         [ITEM_IPV4_TOS] = {
1467                 .name = "tos",
1468                 .help = "type of service",
1469                 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1470                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1471                                              hdr.type_of_service)),
1472         },
1473         [ITEM_IPV4_TTL] = {
1474                 .name = "ttl",
1475                 .help = "time to live",
1476                 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1477                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1478                                              hdr.time_to_live)),
1479         },
1480         [ITEM_IPV4_PROTO] = {
1481                 .name = "proto",
1482                 .help = "next protocol ID",
1483                 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1484                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1485                                              hdr.next_proto_id)),
1486         },
1487         [ITEM_IPV4_SRC] = {
1488                 .name = "src",
1489                 .help = "source address",
1490                 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1491                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1492                                              hdr.src_addr)),
1493         },
1494         [ITEM_IPV4_DST] = {
1495                 .name = "dst",
1496                 .help = "destination address",
1497                 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1498                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1499                                              hdr.dst_addr)),
1500         },
1501         [ITEM_IPV6] = {
1502                 .name = "ipv6",
1503                 .help = "match IPv6 header",
1504                 .priv = PRIV_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
1505                 .next = NEXT(item_ipv6),
1506                 .call = parse_vc,
1507         },
1508         [ITEM_IPV6_TC] = {
1509                 .name = "tc",
1510                 .help = "traffic class",
1511                 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1512                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1513                                                   hdr.vtc_flow,
1514                                                   "\x0f\xf0\x00\x00")),
1515         },
1516         [ITEM_IPV6_FLOW] = {
1517                 .name = "flow",
1518                 .help = "flow label",
1519                 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1520                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1521                                                   hdr.vtc_flow,
1522                                                   "\x00\x0f\xff\xff")),
1523         },
1524         [ITEM_IPV6_PROTO] = {
1525                 .name = "proto",
1526                 .help = "protocol (next header)",
1527                 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1528                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1529                                              hdr.proto)),
1530         },
1531         [ITEM_IPV6_HOP] = {
1532                 .name = "hop",
1533                 .help = "hop limit",
1534                 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1535                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1536                                              hdr.hop_limits)),
1537         },
1538         [ITEM_IPV6_SRC] = {
1539                 .name = "src",
1540                 .help = "source address",
1541                 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1542                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1543                                              hdr.src_addr)),
1544         },
1545         [ITEM_IPV6_DST] = {
1546                 .name = "dst",
1547                 .help = "destination address",
1548                 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1549                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1550                                              hdr.dst_addr)),
1551         },
1552         [ITEM_ICMP] = {
1553                 .name = "icmp",
1554                 .help = "match ICMP header",
1555                 .priv = PRIV_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
1556                 .next = NEXT(item_icmp),
1557                 .call = parse_vc,
1558         },
1559         [ITEM_ICMP_TYPE] = {
1560                 .name = "type",
1561                 .help = "ICMP packet type",
1562                 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1563                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1564                                              hdr.icmp_type)),
1565         },
1566         [ITEM_ICMP_CODE] = {
1567                 .name = "code",
1568                 .help = "ICMP packet code",
1569                 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1570                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1571                                              hdr.icmp_code)),
1572         },
1573         [ITEM_UDP] = {
1574                 .name = "udp",
1575                 .help = "match UDP header",
1576                 .priv = PRIV_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
1577                 .next = NEXT(item_udp),
1578                 .call = parse_vc,
1579         },
1580         [ITEM_UDP_SRC] = {
1581                 .name = "src",
1582                 .help = "UDP source port",
1583                 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1584                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1585                                              hdr.src_port)),
1586         },
1587         [ITEM_UDP_DST] = {
1588                 .name = "dst",
1589                 .help = "UDP destination port",
1590                 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1591                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1592                                              hdr.dst_port)),
1593         },
1594         [ITEM_TCP] = {
1595                 .name = "tcp",
1596                 .help = "match TCP header",
1597                 .priv = PRIV_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
1598                 .next = NEXT(item_tcp),
1599                 .call = parse_vc,
1600         },
1601         [ITEM_TCP_SRC] = {
1602                 .name = "src",
1603                 .help = "TCP source port",
1604                 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1605                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1606                                              hdr.src_port)),
1607         },
1608         [ITEM_TCP_DST] = {
1609                 .name = "dst",
1610                 .help = "TCP destination port",
1611                 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1612                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1613                                              hdr.dst_port)),
1614         },
1615         [ITEM_TCP_FLAGS] = {
1616                 .name = "flags",
1617                 .help = "TCP flags",
1618                 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1619                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1620                                              hdr.tcp_flags)),
1621         },
1622         [ITEM_SCTP] = {
1623                 .name = "sctp",
1624                 .help = "match SCTP header",
1625                 .priv = PRIV_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
1626                 .next = NEXT(item_sctp),
1627                 .call = parse_vc,
1628         },
1629         [ITEM_SCTP_SRC] = {
1630                 .name = "src",
1631                 .help = "SCTP source port",
1632                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1633                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1634                                              hdr.src_port)),
1635         },
1636         [ITEM_SCTP_DST] = {
1637                 .name = "dst",
1638                 .help = "SCTP destination port",
1639                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1640                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1641                                              hdr.dst_port)),
1642         },
1643         [ITEM_SCTP_TAG] = {
1644                 .name = "tag",
1645                 .help = "validation tag",
1646                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1647                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1648                                              hdr.tag)),
1649         },
1650         [ITEM_SCTP_CKSUM] = {
1651                 .name = "cksum",
1652                 .help = "checksum",
1653                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1654                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1655                                              hdr.cksum)),
1656         },
1657         [ITEM_VXLAN] = {
1658                 .name = "vxlan",
1659                 .help = "match VXLAN header",
1660                 .priv = PRIV_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
1661                 .next = NEXT(item_vxlan),
1662                 .call = parse_vc,
1663         },
1664         [ITEM_VXLAN_VNI] = {
1665                 .name = "vni",
1666                 .help = "VXLAN identifier",
1667                 .next = NEXT(item_vxlan, NEXT_ENTRY(UNSIGNED), item_param),
1668                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vxlan, vni)),
1669         },
1670         [ITEM_E_TAG] = {
1671                 .name = "e_tag",
1672                 .help = "match E-Tag header",
1673                 .priv = PRIV_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
1674                 .next = NEXT(item_e_tag),
1675                 .call = parse_vc,
1676         },
1677         [ITEM_E_TAG_GRP_ECID_B] = {
1678                 .name = "grp_ecid_b",
1679                 .help = "GRP and E-CID base",
1680                 .next = NEXT(item_e_tag, NEXT_ENTRY(UNSIGNED), item_param),
1681                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_e_tag,
1682                                                   rsvd_grp_ecid_b,
1683                                                   "\x3f\xff")),
1684         },
1685         [ITEM_NVGRE] = {
1686                 .name = "nvgre",
1687                 .help = "match NVGRE header",
1688                 .priv = PRIV_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
1689                 .next = NEXT(item_nvgre),
1690                 .call = parse_vc,
1691         },
1692         [ITEM_NVGRE_TNI] = {
1693                 .name = "tni",
1694                 .help = "virtual subnet ID",
1695                 .next = NEXT(item_nvgre, NEXT_ENTRY(UNSIGNED), item_param),
1696                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_nvgre, tni)),
1697         },
1698         [ITEM_MPLS] = {
1699                 .name = "mpls",
1700                 .help = "match MPLS header",
1701                 .priv = PRIV_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
1702                 .next = NEXT(item_mpls),
1703                 .call = parse_vc,
1704         },
1705         [ITEM_MPLS_LABEL] = {
1706                 .name = "label",
1707                 .help = "MPLS label",
1708                 .next = NEXT(item_mpls, NEXT_ENTRY(UNSIGNED), item_param),
1709                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_mpls,
1710                                                   label_tc_s,
1711                                                   "\xff\xff\xf0")),
1712         },
1713         [ITEM_GRE] = {
1714                 .name = "gre",
1715                 .help = "match GRE header",
1716                 .priv = PRIV_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
1717                 .next = NEXT(item_gre),
1718                 .call = parse_vc,
1719         },
1720         [ITEM_GRE_PROTO] = {
1721                 .name = "protocol",
1722                 .help = "GRE protocol type",
1723                 .next = NEXT(item_gre, NEXT_ENTRY(UNSIGNED), item_param),
1724                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gre,
1725                                              protocol)),
1726         },
1727         [ITEM_FUZZY] = {
1728                 .name = "fuzzy",
1729                 .help = "fuzzy pattern match, expect faster than default",
1730                 .priv = PRIV_ITEM(FUZZY,
1731                                 sizeof(struct rte_flow_item_fuzzy)),
1732                 .next = NEXT(item_fuzzy),
1733                 .call = parse_vc,
1734         },
1735         [ITEM_FUZZY_THRESH] = {
1736                 .name = "thresh",
1737                 .help = "match accuracy threshold",
1738                 .next = NEXT(item_fuzzy, NEXT_ENTRY(UNSIGNED), item_param),
1739                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_fuzzy,
1740                                         thresh)),
1741         },
1742         [ITEM_GTP] = {
1743                 .name = "gtp",
1744                 .help = "match GTP header",
1745                 .priv = PRIV_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
1746                 .next = NEXT(item_gtp),
1747                 .call = parse_vc,
1748         },
1749         [ITEM_GTP_TEID] = {
1750                 .name = "teid",
1751                 .help = "tunnel endpoint identifier",
1752                 .next = NEXT(item_gtp, NEXT_ENTRY(UNSIGNED), item_param),
1753                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gtp, teid)),
1754         },
1755         [ITEM_GTPC] = {
1756                 .name = "gtpc",
1757                 .help = "match GTP header",
1758                 .priv = PRIV_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
1759                 .next = NEXT(item_gtp),
1760                 .call = parse_vc,
1761         },
1762         [ITEM_GTPU] = {
1763                 .name = "gtpu",
1764                 .help = "match GTP header",
1765                 .priv = PRIV_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
1766                 .next = NEXT(item_gtp),
1767                 .call = parse_vc,
1768         },
1769         [ITEM_GENEVE] = {
1770                 .name = "geneve",
1771                 .help = "match GENEVE header",
1772                 .priv = PRIV_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
1773                 .next = NEXT(item_geneve),
1774                 .call = parse_vc,
1775         },
1776         [ITEM_GENEVE_VNI] = {
1777                 .name = "vni",
1778                 .help = "virtual network identifier",
1779                 .next = NEXT(item_geneve, NEXT_ENTRY(UNSIGNED), item_param),
1780                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_geneve, vni)),
1781         },
1782         [ITEM_GENEVE_PROTO] = {
1783                 .name = "protocol",
1784                 .help = "GENEVE protocol type",
1785                 .next = NEXT(item_geneve, NEXT_ENTRY(UNSIGNED), item_param),
1786                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_geneve,
1787                                              protocol)),
1788         },
1789         [ITEM_VXLAN_GPE] = {
1790                 .name = "vxlan-gpe",
1791                 .help = "match VXLAN-GPE header",
1792                 .priv = PRIV_ITEM(VXLAN_GPE,
1793                                   sizeof(struct rte_flow_item_vxlan_gpe)),
1794                 .next = NEXT(item_vxlan_gpe),
1795                 .call = parse_vc,
1796         },
1797         [ITEM_VXLAN_GPE_VNI] = {
1798                 .name = "vni",
1799                 .help = "VXLAN-GPE identifier",
1800                 .next = NEXT(item_vxlan_gpe, NEXT_ENTRY(UNSIGNED), item_param),
1801                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vxlan_gpe,
1802                                              vni)),
1803         },
1804         [ITEM_ARP_ETH_IPV4] = {
1805                 .name = "arp_eth_ipv4",
1806                 .help = "match ARP header for Ethernet/IPv4",
1807                 .priv = PRIV_ITEM(ARP_ETH_IPV4,
1808                                   sizeof(struct rte_flow_item_arp_eth_ipv4)),
1809                 .next = NEXT(item_arp_eth_ipv4),
1810                 .call = parse_vc,
1811         },
1812         [ITEM_ARP_ETH_IPV4_SHA] = {
1813                 .name = "sha",
1814                 .help = "sender hardware address",
1815                 .next = NEXT(item_arp_eth_ipv4, NEXT_ENTRY(MAC_ADDR),
1816                              item_param),
1817                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_arp_eth_ipv4,
1818                                              sha)),
1819         },
1820         [ITEM_ARP_ETH_IPV4_SPA] = {
1821                 .name = "spa",
1822                 .help = "sender IPv4 address",
1823                 .next = NEXT(item_arp_eth_ipv4, NEXT_ENTRY(IPV4_ADDR),
1824                              item_param),
1825                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_arp_eth_ipv4,
1826                                              spa)),
1827         },
1828         [ITEM_ARP_ETH_IPV4_THA] = {
1829                 .name = "tha",
1830                 .help = "target hardware address",
1831                 .next = NEXT(item_arp_eth_ipv4, NEXT_ENTRY(MAC_ADDR),
1832                              item_param),
1833                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_arp_eth_ipv4,
1834                                              tha)),
1835         },
1836         [ITEM_ARP_ETH_IPV4_TPA] = {
1837                 .name = "tpa",
1838                 .help = "target IPv4 address",
1839                 .next = NEXT(item_arp_eth_ipv4, NEXT_ENTRY(IPV4_ADDR),
1840                              item_param),
1841                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_arp_eth_ipv4,
1842                                              tpa)),
1843         },
1844         [ITEM_IPV6_EXT] = {
1845                 .name = "ipv6_ext",
1846                 .help = "match presence of any IPv6 extension header",
1847                 .priv = PRIV_ITEM(IPV6_EXT,
1848                                   sizeof(struct rte_flow_item_ipv6_ext)),
1849                 .next = NEXT(item_ipv6_ext),
1850                 .call = parse_vc,
1851         },
1852         [ITEM_IPV6_EXT_NEXT_HDR] = {
1853                 .name = "next_hdr",
1854                 .help = "next header",
1855                 .next = NEXT(item_ipv6_ext, NEXT_ENTRY(UNSIGNED), item_param),
1856                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6_ext,
1857                                              next_hdr)),
1858         },
1859         [ITEM_ICMP6] = {
1860                 .name = "icmp6",
1861                 .help = "match any ICMPv6 header",
1862                 .priv = PRIV_ITEM(ICMP6, sizeof(struct rte_flow_item_icmp6)),
1863                 .next = NEXT(item_icmp6),
1864                 .call = parse_vc,
1865         },
1866         [ITEM_ICMP6_TYPE] = {
1867                 .name = "type",
1868                 .help = "ICMPv6 type",
1869                 .next = NEXT(item_icmp6, NEXT_ENTRY(UNSIGNED), item_param),
1870                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp6,
1871                                              type)),
1872         },
1873         [ITEM_ICMP6_CODE] = {
1874                 .name = "code",
1875                 .help = "ICMPv6 code",
1876                 .next = NEXT(item_icmp6, NEXT_ENTRY(UNSIGNED), item_param),
1877                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp6,
1878                                              code)),
1879         },
1880         [ITEM_ICMP6_ND_NS] = {
1881                 .name = "icmp6_nd_ns",
1882                 .help = "match ICMPv6 neighbor discovery solicitation",
1883                 .priv = PRIV_ITEM(ICMP6_ND_NS,
1884                                   sizeof(struct rte_flow_item_icmp6_nd_ns)),
1885                 .next = NEXT(item_icmp6_nd_ns),
1886                 .call = parse_vc,
1887         },
1888         [ITEM_ICMP6_ND_NS_TARGET_ADDR] = {
1889                 .name = "target_addr",
1890                 .help = "target address",
1891                 .next = NEXT(item_icmp6_nd_ns, NEXT_ENTRY(IPV6_ADDR),
1892                              item_param),
1893                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp6_nd_ns,
1894                                              target_addr)),
1895         },
1896         [ITEM_ICMP6_ND_NA] = {
1897                 .name = "icmp6_nd_na",
1898                 .help = "match ICMPv6 neighbor discovery advertisement",
1899                 .priv = PRIV_ITEM(ICMP6_ND_NA,
1900                                   sizeof(struct rte_flow_item_icmp6_nd_na)),
1901                 .next = NEXT(item_icmp6_nd_na),
1902                 .call = parse_vc,
1903         },
1904         [ITEM_ICMP6_ND_NA_TARGET_ADDR] = {
1905                 .name = "target_addr",
1906                 .help = "target address",
1907                 .next = NEXT(item_icmp6_nd_na, NEXT_ENTRY(IPV6_ADDR),
1908                              item_param),
1909                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp6_nd_na,
1910                                              target_addr)),
1911         },
1912         [ITEM_ICMP6_ND_OPT] = {
1913                 .name = "icmp6_nd_opt",
1914                 .help = "match presence of any ICMPv6 neighbor discovery"
1915                         " option",
1916                 .priv = PRIV_ITEM(ICMP6_ND_OPT,
1917                                   sizeof(struct rte_flow_item_icmp6_nd_opt)),
1918                 .next = NEXT(item_icmp6_nd_opt),
1919                 .call = parse_vc,
1920         },
1921         [ITEM_ICMP6_ND_OPT_TYPE] = {
1922                 .name = "type",
1923                 .help = "ND option type",
1924                 .next = NEXT(item_icmp6_nd_opt, NEXT_ENTRY(UNSIGNED),
1925                              item_param),
1926                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp6_nd_opt,
1927                                              type)),
1928         },
1929         [ITEM_ICMP6_ND_OPT_SLA_ETH] = {
1930                 .name = "icmp6_nd_opt_sla_eth",
1931                 .help = "match ICMPv6 neighbor discovery source Ethernet"
1932                         " link-layer address option",
1933                 .priv = PRIV_ITEM
1934                         (ICMP6_ND_OPT_SLA_ETH,
1935                          sizeof(struct rte_flow_item_icmp6_nd_opt_sla_eth)),
1936                 .next = NEXT(item_icmp6_nd_opt_sla_eth),
1937                 .call = parse_vc,
1938         },
1939         [ITEM_ICMP6_ND_OPT_SLA_ETH_SLA] = {
1940                 .name = "sla",
1941                 .help = "source Ethernet LLA",
1942                 .next = NEXT(item_icmp6_nd_opt_sla_eth, NEXT_ENTRY(MAC_ADDR),
1943                              item_param),
1944                 .args = ARGS(ARGS_ENTRY_HTON
1945                              (struct rte_flow_item_icmp6_nd_opt_sla_eth, sla)),
1946         },
1947         [ITEM_ICMP6_ND_OPT_TLA_ETH] = {
1948                 .name = "icmp6_nd_opt_tla_eth",
1949                 .help = "match ICMPv6 neighbor discovery target Ethernet"
1950                         " link-layer address option",
1951                 .priv = PRIV_ITEM
1952                         (ICMP6_ND_OPT_TLA_ETH,
1953                          sizeof(struct rte_flow_item_icmp6_nd_opt_tla_eth)),
1954                 .next = NEXT(item_icmp6_nd_opt_tla_eth),
1955                 .call = parse_vc,
1956         },
1957         [ITEM_ICMP6_ND_OPT_TLA_ETH_TLA] = {
1958                 .name = "tla",
1959                 .help = "target Ethernet LLA",
1960                 .next = NEXT(item_icmp6_nd_opt_tla_eth, NEXT_ENTRY(MAC_ADDR),
1961                              item_param),
1962                 .args = ARGS(ARGS_ENTRY_HTON
1963                              (struct rte_flow_item_icmp6_nd_opt_tla_eth, tla)),
1964         },
1965
1966         /* Validate/create actions. */
1967         [ACTIONS] = {
1968                 .name = "actions",
1969                 .help = "submit a list of associated actions",
1970                 .next = NEXT(next_action),
1971                 .call = parse_vc,
1972         },
1973         [ACTION_NEXT] = {
1974                 .name = "/",
1975                 .help = "specify next action",
1976                 .next = NEXT(next_action),
1977         },
1978         [ACTION_END] = {
1979                 .name = "end",
1980                 .help = "end list of actions",
1981                 .priv = PRIV_ACTION(END, 0),
1982                 .call = parse_vc,
1983         },
1984         [ACTION_VOID] = {
1985                 .name = "void",
1986                 .help = "no-op action",
1987                 .priv = PRIV_ACTION(VOID, 0),
1988                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1989                 .call = parse_vc,
1990         },
1991         [ACTION_PASSTHRU] = {
1992                 .name = "passthru",
1993                 .help = "let subsequent rule process matched packets",
1994                 .priv = PRIV_ACTION(PASSTHRU, 0),
1995                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1996                 .call = parse_vc,
1997         },
1998         [ACTION_JUMP] = {
1999                 .name = "jump",
2000                 .help = "redirect traffic to a given group",
2001                 .priv = PRIV_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
2002                 .next = NEXT(action_jump),
2003                 .call = parse_vc,
2004         },
2005         [ACTION_JUMP_GROUP] = {
2006                 .name = "group",
2007                 .help = "group to redirect traffic to",
2008                 .next = NEXT(action_jump, NEXT_ENTRY(UNSIGNED)),
2009                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_jump, group)),
2010                 .call = parse_vc_conf,
2011         },
2012         [ACTION_MARK] = {
2013                 .name = "mark",
2014                 .help = "attach 32 bit value to packets",
2015                 .priv = PRIV_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
2016                 .next = NEXT(action_mark),
2017                 .call = parse_vc,
2018         },
2019         [ACTION_MARK_ID] = {
2020                 .name = "id",
2021                 .help = "32 bit value to return with packets",
2022                 .next = NEXT(action_mark, NEXT_ENTRY(UNSIGNED)),
2023                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_mark, id)),
2024                 .call = parse_vc_conf,
2025         },
2026         [ACTION_FLAG] = {
2027                 .name = "flag",
2028                 .help = "flag packets",
2029                 .priv = PRIV_ACTION(FLAG, 0),
2030                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2031                 .call = parse_vc,
2032         },
2033         [ACTION_QUEUE] = {
2034                 .name = "queue",
2035                 .help = "assign packets to a given queue index",
2036                 .priv = PRIV_ACTION(QUEUE,
2037                                     sizeof(struct rte_flow_action_queue)),
2038                 .next = NEXT(action_queue),
2039                 .call = parse_vc,
2040         },
2041         [ACTION_QUEUE_INDEX] = {
2042                 .name = "index",
2043                 .help = "queue index to use",
2044                 .next = NEXT(action_queue, NEXT_ENTRY(UNSIGNED)),
2045                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_queue, index)),
2046                 .call = parse_vc_conf,
2047         },
2048         [ACTION_DROP] = {
2049                 .name = "drop",
2050                 .help = "drop packets (note: passthru has priority)",
2051                 .priv = PRIV_ACTION(DROP, 0),
2052                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2053                 .call = parse_vc,
2054         },
2055         [ACTION_COUNT] = {
2056                 .name = "count",
2057                 .help = "enable counters for this rule",
2058                 .priv = PRIV_ACTION(COUNT,
2059                                     sizeof(struct rte_flow_action_count)),
2060                 .next = NEXT(action_count),
2061                 .call = parse_vc,
2062         },
2063         [ACTION_COUNT_ID] = {
2064                 .name = "identifier",
2065                 .help = "counter identifier to use",
2066                 .next = NEXT(action_count, NEXT_ENTRY(UNSIGNED)),
2067                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_count, id)),
2068                 .call = parse_vc_conf,
2069         },
2070         [ACTION_COUNT_SHARED] = {
2071                 .name = "shared",
2072                 .help = "shared counter",
2073                 .next = NEXT(action_count, NEXT_ENTRY(BOOLEAN)),
2074                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_count,
2075                                            shared, 1)),
2076                 .call = parse_vc_conf,
2077         },
2078         [ACTION_RSS] = {
2079                 .name = "rss",
2080                 .help = "spread packets among several queues",
2081                 .priv = PRIV_ACTION(RSS, sizeof(struct action_rss_data)),
2082                 .next = NEXT(action_rss),
2083                 .call = parse_vc_action_rss,
2084         },
2085         [ACTION_RSS_FUNC] = {
2086                 .name = "func",
2087                 .help = "RSS hash function to apply",
2088                 .next = NEXT(action_rss,
2089                              NEXT_ENTRY(ACTION_RSS_FUNC_DEFAULT,
2090                                         ACTION_RSS_FUNC_TOEPLITZ,
2091                                         ACTION_RSS_FUNC_SIMPLE_XOR)),
2092         },
2093         [ACTION_RSS_FUNC_DEFAULT] = {
2094                 .name = "default",
2095                 .help = "default hash function",
2096                 .call = parse_vc_action_rss_func,
2097         },
2098         [ACTION_RSS_FUNC_TOEPLITZ] = {
2099                 .name = "toeplitz",
2100                 .help = "Toeplitz hash function",
2101                 .call = parse_vc_action_rss_func,
2102         },
2103         [ACTION_RSS_FUNC_SIMPLE_XOR] = {
2104                 .name = "simple_xor",
2105                 .help = "simple XOR hash function",
2106                 .call = parse_vc_action_rss_func,
2107         },
2108         [ACTION_RSS_LEVEL] = {
2109                 .name = "level",
2110                 .help = "encapsulation level for \"types\"",
2111                 .next = NEXT(action_rss, NEXT_ENTRY(UNSIGNED)),
2112                 .args = ARGS(ARGS_ENTRY_ARB
2113                              (offsetof(struct action_rss_data, conf) +
2114                               offsetof(struct rte_flow_action_rss, level),
2115                               sizeof(((struct rte_flow_action_rss *)0)->
2116                                      level))),
2117         },
2118         [ACTION_RSS_TYPES] = {
2119                 .name = "types",
2120                 .help = "specific RSS hash types",
2121                 .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_TYPE)),
2122         },
2123         [ACTION_RSS_TYPE] = {
2124                 .name = "{type}",
2125                 .help = "RSS hash type",
2126                 .call = parse_vc_action_rss_type,
2127                 .comp = comp_vc_action_rss_type,
2128         },
2129         [ACTION_RSS_KEY] = {
2130                 .name = "key",
2131                 .help = "RSS hash key",
2132                 .next = NEXT(action_rss, NEXT_ENTRY(STRING)),
2133                 .args = ARGS(ARGS_ENTRY_ARB(0, 0),
2134                              ARGS_ENTRY_ARB
2135                              (offsetof(struct action_rss_data, conf) +
2136                               offsetof(struct rte_flow_action_rss, key_len),
2137                               sizeof(((struct rte_flow_action_rss *)0)->
2138                                      key_len)),
2139                              ARGS_ENTRY(struct action_rss_data, key)),
2140         },
2141         [ACTION_RSS_KEY_LEN] = {
2142                 .name = "key_len",
2143                 .help = "RSS hash key length in bytes",
2144                 .next = NEXT(action_rss, NEXT_ENTRY(UNSIGNED)),
2145                 .args = ARGS(ARGS_ENTRY_ARB_BOUNDED
2146                              (offsetof(struct action_rss_data, conf) +
2147                               offsetof(struct rte_flow_action_rss, key_len),
2148                               sizeof(((struct rte_flow_action_rss *)0)->
2149                                      key_len),
2150                               0,
2151                               RSS_HASH_KEY_LENGTH)),
2152         },
2153         [ACTION_RSS_QUEUES] = {
2154                 .name = "queues",
2155                 .help = "queue indices to use",
2156                 .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_QUEUE)),
2157                 .call = parse_vc_conf,
2158         },
2159         [ACTION_RSS_QUEUE] = {
2160                 .name = "{queue}",
2161                 .help = "queue index",
2162                 .call = parse_vc_action_rss_queue,
2163                 .comp = comp_vc_action_rss_queue,
2164         },
2165         [ACTION_PF] = {
2166                 .name = "pf",
2167                 .help = "direct traffic to physical function",
2168                 .priv = PRIV_ACTION(PF, 0),
2169                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2170                 .call = parse_vc,
2171         },
2172         [ACTION_VF] = {
2173                 .name = "vf",
2174                 .help = "direct traffic to a virtual function ID",
2175                 .priv = PRIV_ACTION(VF, sizeof(struct rte_flow_action_vf)),
2176                 .next = NEXT(action_vf),
2177                 .call = parse_vc,
2178         },
2179         [ACTION_VF_ORIGINAL] = {
2180                 .name = "original",
2181                 .help = "use original VF ID if possible",
2182                 .next = NEXT(action_vf, NEXT_ENTRY(BOOLEAN)),
2183                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_vf,
2184                                            original, 1)),
2185                 .call = parse_vc_conf,
2186         },
2187         [ACTION_VF_ID] = {
2188                 .name = "id",
2189                 .help = "VF ID",
2190                 .next = NEXT(action_vf, NEXT_ENTRY(UNSIGNED)),
2191                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_vf, id)),
2192                 .call = parse_vc_conf,
2193         },
2194         [ACTION_PHY_PORT] = {
2195                 .name = "phy_port",
2196                 .help = "direct packets to physical port index",
2197                 .priv = PRIV_ACTION(PHY_PORT,
2198                                     sizeof(struct rte_flow_action_phy_port)),
2199                 .next = NEXT(action_phy_port),
2200                 .call = parse_vc,
2201         },
2202         [ACTION_PHY_PORT_ORIGINAL] = {
2203                 .name = "original",
2204                 .help = "use original port index if possible",
2205                 .next = NEXT(action_phy_port, NEXT_ENTRY(BOOLEAN)),
2206                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_phy_port,
2207                                            original, 1)),
2208                 .call = parse_vc_conf,
2209         },
2210         [ACTION_PHY_PORT_INDEX] = {
2211                 .name = "index",
2212                 .help = "physical port index",
2213                 .next = NEXT(action_phy_port, NEXT_ENTRY(UNSIGNED)),
2214                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_phy_port,
2215                                         index)),
2216                 .call = parse_vc_conf,
2217         },
2218         [ACTION_PORT_ID] = {
2219                 .name = "port_id",
2220                 .help = "direct matching traffic to a given DPDK port ID",
2221                 .priv = PRIV_ACTION(PORT_ID,
2222                                     sizeof(struct rte_flow_action_port_id)),
2223                 .next = NEXT(action_port_id),
2224                 .call = parse_vc,
2225         },
2226         [ACTION_PORT_ID_ORIGINAL] = {
2227                 .name = "original",
2228                 .help = "use original DPDK port ID if possible",
2229                 .next = NEXT(action_port_id, NEXT_ENTRY(BOOLEAN)),
2230                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_port_id,
2231                                            original, 1)),
2232                 .call = parse_vc_conf,
2233         },
2234         [ACTION_PORT_ID_ID] = {
2235                 .name = "id",
2236                 .help = "DPDK port ID",
2237                 .next = NEXT(action_port_id, NEXT_ENTRY(UNSIGNED)),
2238                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_port_id, id)),
2239                 .call = parse_vc_conf,
2240         },
2241         [ACTION_METER] = {
2242                 .name = "meter",
2243                 .help = "meter the directed packets at given id",
2244                 .priv = PRIV_ACTION(METER,
2245                                     sizeof(struct rte_flow_action_meter)),
2246                 .next = NEXT(action_meter),
2247                 .call = parse_vc,
2248         },
2249         [ACTION_METER_ID] = {
2250                 .name = "mtr_id",
2251                 .help = "meter id to use",
2252                 .next = NEXT(action_meter, NEXT_ENTRY(UNSIGNED)),
2253                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_meter, mtr_id)),
2254                 .call = parse_vc_conf,
2255         },
2256         [ACTION_OF_SET_MPLS_TTL] = {
2257                 .name = "of_set_mpls_ttl",
2258                 .help = "OpenFlow's OFPAT_SET_MPLS_TTL",
2259                 .priv = PRIV_ACTION
2260                         (OF_SET_MPLS_TTL,
2261                          sizeof(struct rte_flow_action_of_set_mpls_ttl)),
2262                 .next = NEXT(action_of_set_mpls_ttl),
2263                 .call = parse_vc,
2264         },
2265         [ACTION_OF_SET_MPLS_TTL_MPLS_TTL] = {
2266                 .name = "mpls_ttl",
2267                 .help = "MPLS TTL",
2268                 .next = NEXT(action_of_set_mpls_ttl, NEXT_ENTRY(UNSIGNED)),
2269                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_of_set_mpls_ttl,
2270                                         mpls_ttl)),
2271                 .call = parse_vc_conf,
2272         },
2273         [ACTION_OF_DEC_MPLS_TTL] = {
2274                 .name = "of_dec_mpls_ttl",
2275                 .help = "OpenFlow's OFPAT_DEC_MPLS_TTL",
2276                 .priv = PRIV_ACTION(OF_DEC_MPLS_TTL, 0),
2277                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2278                 .call = parse_vc,
2279         },
2280         [ACTION_OF_SET_NW_TTL] = {
2281                 .name = "of_set_nw_ttl",
2282                 .help = "OpenFlow's OFPAT_SET_NW_TTL",
2283                 .priv = PRIV_ACTION
2284                         (OF_SET_NW_TTL,
2285                          sizeof(struct rte_flow_action_of_set_nw_ttl)),
2286                 .next = NEXT(action_of_set_nw_ttl),
2287                 .call = parse_vc,
2288         },
2289         [ACTION_OF_SET_NW_TTL_NW_TTL] = {
2290                 .name = "nw_ttl",
2291                 .help = "IP TTL",
2292                 .next = NEXT(action_of_set_nw_ttl, NEXT_ENTRY(UNSIGNED)),
2293                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_of_set_nw_ttl,
2294                                         nw_ttl)),
2295                 .call = parse_vc_conf,
2296         },
2297         [ACTION_OF_DEC_NW_TTL] = {
2298                 .name = "of_dec_nw_ttl",
2299                 .help = "OpenFlow's OFPAT_DEC_NW_TTL",
2300                 .priv = PRIV_ACTION(OF_DEC_NW_TTL, 0),
2301                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2302                 .call = parse_vc,
2303         },
2304         [ACTION_OF_COPY_TTL_OUT] = {
2305                 .name = "of_copy_ttl_out",
2306                 .help = "OpenFlow's OFPAT_COPY_TTL_OUT",
2307                 .priv = PRIV_ACTION(OF_COPY_TTL_OUT, 0),
2308                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2309                 .call = parse_vc,
2310         },
2311         [ACTION_OF_COPY_TTL_IN] = {
2312                 .name = "of_copy_ttl_in",
2313                 .help = "OpenFlow's OFPAT_COPY_TTL_IN",
2314                 .priv = PRIV_ACTION(OF_COPY_TTL_IN, 0),
2315                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2316                 .call = parse_vc,
2317         },
2318         [ACTION_OF_POP_VLAN] = {
2319                 .name = "of_pop_vlan",
2320                 .help = "OpenFlow's OFPAT_POP_VLAN",
2321                 .priv = PRIV_ACTION(OF_POP_VLAN, 0),
2322                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2323                 .call = parse_vc,
2324         },
2325         [ACTION_OF_PUSH_VLAN] = {
2326                 .name = "of_push_vlan",
2327                 .help = "OpenFlow's OFPAT_PUSH_VLAN",
2328                 .priv = PRIV_ACTION
2329                         (OF_PUSH_VLAN,
2330                          sizeof(struct rte_flow_action_of_push_vlan)),
2331                 .next = NEXT(action_of_push_vlan),
2332                 .call = parse_vc,
2333         },
2334         [ACTION_OF_PUSH_VLAN_ETHERTYPE] = {
2335                 .name = "ethertype",
2336                 .help = "EtherType",
2337                 .next = NEXT(action_of_push_vlan, NEXT_ENTRY(UNSIGNED)),
2338                 .args = ARGS(ARGS_ENTRY_HTON
2339                              (struct rte_flow_action_of_push_vlan,
2340                               ethertype)),
2341                 .call = parse_vc_conf,
2342         },
2343         [ACTION_OF_SET_VLAN_VID] = {
2344                 .name = "of_set_vlan_vid",
2345                 .help = "OpenFlow's OFPAT_SET_VLAN_VID",
2346                 .priv = PRIV_ACTION
2347                         (OF_SET_VLAN_VID,
2348                          sizeof(struct rte_flow_action_of_set_vlan_vid)),
2349                 .next = NEXT(action_of_set_vlan_vid),
2350                 .call = parse_vc,
2351         },
2352         [ACTION_OF_SET_VLAN_VID_VLAN_VID] = {
2353                 .name = "vlan_vid",
2354                 .help = "VLAN id",
2355                 .next = NEXT(action_of_set_vlan_vid, NEXT_ENTRY(UNSIGNED)),
2356                 .args = ARGS(ARGS_ENTRY_HTON
2357                              (struct rte_flow_action_of_set_vlan_vid,
2358                               vlan_vid)),
2359                 .call = parse_vc_conf,
2360         },
2361         [ACTION_OF_SET_VLAN_PCP] = {
2362                 .name = "of_set_vlan_pcp",
2363                 .help = "OpenFlow's OFPAT_SET_VLAN_PCP",
2364                 .priv = PRIV_ACTION
2365                         (OF_SET_VLAN_PCP,
2366                          sizeof(struct rte_flow_action_of_set_vlan_pcp)),
2367                 .next = NEXT(action_of_set_vlan_pcp),
2368                 .call = parse_vc,
2369         },
2370         [ACTION_OF_SET_VLAN_PCP_VLAN_PCP] = {
2371                 .name = "vlan_pcp",
2372                 .help = "VLAN priority",
2373                 .next = NEXT(action_of_set_vlan_pcp, NEXT_ENTRY(UNSIGNED)),
2374                 .args = ARGS(ARGS_ENTRY_HTON
2375                              (struct rte_flow_action_of_set_vlan_pcp,
2376                               vlan_pcp)),
2377                 .call = parse_vc_conf,
2378         },
2379         [ACTION_OF_POP_MPLS] = {
2380                 .name = "of_pop_mpls",
2381                 .help = "OpenFlow's OFPAT_POP_MPLS",
2382                 .priv = PRIV_ACTION(OF_POP_MPLS,
2383                                     sizeof(struct rte_flow_action_of_pop_mpls)),
2384                 .next = NEXT(action_of_pop_mpls),
2385                 .call = parse_vc,
2386         },
2387         [ACTION_OF_POP_MPLS_ETHERTYPE] = {
2388                 .name = "ethertype",
2389                 .help = "EtherType",
2390                 .next = NEXT(action_of_pop_mpls, NEXT_ENTRY(UNSIGNED)),
2391                 .args = ARGS(ARGS_ENTRY_HTON
2392                              (struct rte_flow_action_of_pop_mpls,
2393                               ethertype)),
2394                 .call = parse_vc_conf,
2395         },
2396         [ACTION_OF_PUSH_MPLS] = {
2397                 .name = "of_push_mpls",
2398                 .help = "OpenFlow's OFPAT_PUSH_MPLS",
2399                 .priv = PRIV_ACTION
2400                         (OF_PUSH_MPLS,
2401                          sizeof(struct rte_flow_action_of_push_mpls)),
2402                 .next = NEXT(action_of_push_mpls),
2403                 .call = parse_vc,
2404         },
2405         [ACTION_OF_PUSH_MPLS_ETHERTYPE] = {
2406                 .name = "ethertype",
2407                 .help = "EtherType",
2408                 .next = NEXT(action_of_push_mpls, NEXT_ENTRY(UNSIGNED)),
2409                 .args = ARGS(ARGS_ENTRY_HTON
2410                              (struct rte_flow_action_of_push_mpls,
2411                               ethertype)),
2412                 .call = parse_vc_conf,
2413         },
2414         [ACTION_VXLAN_ENCAP] = {
2415                 .name = "vxlan_encap",
2416                 .help = "VXLAN encapsulation, uses configuration set by \"set"
2417                         " vxlan\"",
2418                 .priv = PRIV_ACTION(VXLAN_ENCAP,
2419                                     sizeof(struct action_vxlan_encap_data)),
2420                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2421                 .call = parse_vc_action_vxlan_encap,
2422         },
2423         [ACTION_VXLAN_DECAP] = {
2424                 .name = "vxlan_decap",
2425                 .help = "Performs a decapsulation action by stripping all"
2426                         " headers of the VXLAN tunnel network overlay from the"
2427                         " matched flow.",
2428                 .priv = PRIV_ACTION(VXLAN_DECAP, 0),
2429                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2430                 .call = parse_vc,
2431         },
2432 };
2433
2434 /** Remove and return last entry from argument stack. */
2435 static const struct arg *
2436 pop_args(struct context *ctx)
2437 {
2438         return ctx->args_num ? ctx->args[--ctx->args_num] : NULL;
2439 }
2440
2441 /** Add entry on top of the argument stack. */
2442 static int
2443 push_args(struct context *ctx, const struct arg *arg)
2444 {
2445         if (ctx->args_num == CTX_STACK_SIZE)
2446                 return -1;
2447         ctx->args[ctx->args_num++] = arg;
2448         return 0;
2449 }
2450
2451 /** Spread value into buffer according to bit-mask. */
2452 static size_t
2453 arg_entry_bf_fill(void *dst, uintmax_t val, const struct arg *arg)
2454 {
2455         uint32_t i = arg->size;
2456         uint32_t end = 0;
2457         int sub = 1;
2458         int add = 0;
2459         size_t len = 0;
2460
2461         if (!arg->mask)
2462                 return 0;
2463 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
2464         if (!arg->hton) {
2465                 i = 0;
2466                 end = arg->size;
2467                 sub = 0;
2468                 add = 1;
2469         }
2470 #endif
2471         while (i != end) {
2472                 unsigned int shift = 0;
2473                 uint8_t *buf = (uint8_t *)dst + arg->offset + (i -= sub);
2474
2475                 for (shift = 0; arg->mask[i] >> shift; ++shift) {
2476                         if (!(arg->mask[i] & (1 << shift)))
2477                                 continue;
2478                         ++len;
2479                         if (!dst)
2480                                 continue;
2481                         *buf &= ~(1 << shift);
2482                         *buf |= (val & 1) << shift;
2483                         val >>= 1;
2484                 }
2485                 i += add;
2486         }
2487         return len;
2488 }
2489
2490 /** Compare a string with a partial one of a given length. */
2491 static int
2492 strcmp_partial(const char *full, const char *partial, size_t partial_len)
2493 {
2494         int r = strncmp(full, partial, partial_len);
2495
2496         if (r)
2497                 return r;
2498         if (strlen(full) <= partial_len)
2499                 return 0;
2500         return full[partial_len];
2501 }
2502
2503 /**
2504  * Parse a prefix length and generate a bit-mask.
2505  *
2506  * Last argument (ctx->args) is retrieved to determine mask size, storage
2507  * location and whether the result must use network byte ordering.
2508  */
2509 static int
2510 parse_prefix(struct context *ctx, const struct token *token,
2511              const char *str, unsigned int len,
2512              void *buf, unsigned int size)
2513 {
2514         const struct arg *arg = pop_args(ctx);
2515         static const uint8_t conv[] = "\x00\x80\xc0\xe0\xf0\xf8\xfc\xfe\xff";
2516         char *end;
2517         uintmax_t u;
2518         unsigned int bytes;
2519         unsigned int extra;
2520
2521         (void)token;
2522         /* Argument is expected. */
2523         if (!arg)
2524                 return -1;
2525         errno = 0;
2526         u = strtoumax(str, &end, 0);
2527         if (errno || (size_t)(end - str) != len)
2528                 goto error;
2529         if (arg->mask) {
2530                 uintmax_t v = 0;
2531
2532                 extra = arg_entry_bf_fill(NULL, 0, arg);
2533                 if (u > extra)
2534                         goto error;
2535                 if (!ctx->object)
2536                         return len;
2537                 extra -= u;
2538                 while (u--)
2539                         (v <<= 1, v |= 1);
2540                 v <<= extra;
2541                 if (!arg_entry_bf_fill(ctx->object, v, arg) ||
2542                     !arg_entry_bf_fill(ctx->objmask, -1, arg))
2543                         goto error;
2544                 return len;
2545         }
2546         bytes = u / 8;
2547         extra = u % 8;
2548         size = arg->size;
2549         if (bytes > size || bytes + !!extra > size)
2550                 goto error;
2551         if (!ctx->object)
2552                 return len;
2553         buf = (uint8_t *)ctx->object + arg->offset;
2554 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
2555         if (!arg->hton) {
2556                 memset((uint8_t *)buf + size - bytes, 0xff, bytes);
2557                 memset(buf, 0x00, size - bytes);
2558                 if (extra)
2559                         ((uint8_t *)buf)[size - bytes - 1] = conv[extra];
2560         } else
2561 #endif
2562         {
2563                 memset(buf, 0xff, bytes);
2564                 memset((uint8_t *)buf + bytes, 0x00, size - bytes);
2565                 if (extra)
2566                         ((uint8_t *)buf)[bytes] = conv[extra];
2567         }
2568         if (ctx->objmask)
2569                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2570         return len;
2571 error:
2572         push_args(ctx, arg);
2573         return -1;
2574 }
2575
2576 /** Default parsing function for token name matching. */
2577 static int
2578 parse_default(struct context *ctx, const struct token *token,
2579               const char *str, unsigned int len,
2580               void *buf, unsigned int size)
2581 {
2582         (void)ctx;
2583         (void)buf;
2584         (void)size;
2585         if (strcmp_partial(token->name, str, len))
2586                 return -1;
2587         return len;
2588 }
2589
2590 /** Parse flow command, initialize output buffer for subsequent tokens. */
2591 static int
2592 parse_init(struct context *ctx, const struct token *token,
2593            const char *str, unsigned int len,
2594            void *buf, unsigned int size)
2595 {
2596         struct buffer *out = buf;
2597
2598         /* Token name must match. */
2599         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2600                 return -1;
2601         /* Nothing else to do if there is no buffer. */
2602         if (!out)
2603                 return len;
2604         /* Make sure buffer is large enough. */
2605         if (size < sizeof(*out))
2606                 return -1;
2607         /* Initialize buffer. */
2608         memset(out, 0x00, sizeof(*out));
2609         memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
2610         ctx->objdata = 0;
2611         ctx->object = out;
2612         ctx->objmask = NULL;
2613         return len;
2614 }
2615
2616 /** Parse tokens for validate/create commands. */
2617 static int
2618 parse_vc(struct context *ctx, const struct token *token,
2619          const char *str, unsigned int len,
2620          void *buf, unsigned int size)
2621 {
2622         struct buffer *out = buf;
2623         uint8_t *data;
2624         uint32_t data_size;
2625
2626         /* Token name must match. */
2627         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2628                 return -1;
2629         /* Nothing else to do if there is no buffer. */
2630         if (!out)
2631                 return len;
2632         if (!out->command) {
2633                 if (ctx->curr != VALIDATE && ctx->curr != CREATE)
2634                         return -1;
2635                 if (sizeof(*out) > size)
2636                         return -1;
2637                 out->command = ctx->curr;
2638                 ctx->objdata = 0;
2639                 ctx->object = out;
2640                 ctx->objmask = NULL;
2641                 out->args.vc.data = (uint8_t *)out + size;
2642                 return len;
2643         }
2644         ctx->objdata = 0;
2645         ctx->object = &out->args.vc.attr;
2646         ctx->objmask = NULL;
2647         switch (ctx->curr) {
2648         case GROUP:
2649         case PRIORITY:
2650                 return len;
2651         case INGRESS:
2652                 out->args.vc.attr.ingress = 1;
2653                 return len;
2654         case EGRESS:
2655                 out->args.vc.attr.egress = 1;
2656                 return len;
2657         case TRANSFER:
2658                 out->args.vc.attr.transfer = 1;
2659                 return len;
2660         case PATTERN:
2661                 out->args.vc.pattern =
2662                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
2663                                                sizeof(double));
2664                 ctx->object = out->args.vc.pattern;
2665                 ctx->objmask = NULL;
2666                 return len;
2667         case ACTIONS:
2668                 out->args.vc.actions =
2669                         (void *)RTE_ALIGN_CEIL((uintptr_t)
2670                                                (out->args.vc.pattern +
2671                                                 out->args.vc.pattern_n),
2672                                                sizeof(double));
2673                 ctx->object = out->args.vc.actions;
2674                 ctx->objmask = NULL;
2675                 return len;
2676         default:
2677                 if (!token->priv)
2678                         return -1;
2679                 break;
2680         }
2681         if (!out->args.vc.actions) {
2682                 const struct parse_item_priv *priv = token->priv;
2683                 struct rte_flow_item *item =
2684                         out->args.vc.pattern + out->args.vc.pattern_n;
2685
2686                 data_size = priv->size * 3; /* spec, last, mask */
2687                 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
2688                                                (out->args.vc.data - data_size),
2689                                                sizeof(double));
2690                 if ((uint8_t *)item + sizeof(*item) > data)
2691                         return -1;
2692                 *item = (struct rte_flow_item){
2693                         .type = priv->type,
2694                 };
2695                 ++out->args.vc.pattern_n;
2696                 ctx->object = item;
2697                 ctx->objmask = NULL;
2698         } else {
2699                 const struct parse_action_priv *priv = token->priv;
2700                 struct rte_flow_action *action =
2701                         out->args.vc.actions + out->args.vc.actions_n;
2702
2703                 data_size = priv->size; /* configuration */
2704                 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
2705                                                (out->args.vc.data - data_size),
2706                                                sizeof(double));
2707                 if ((uint8_t *)action + sizeof(*action) > data)
2708                         return -1;
2709                 *action = (struct rte_flow_action){
2710                         .type = priv->type,
2711                         .conf = data_size ? data : NULL,
2712                 };
2713                 ++out->args.vc.actions_n;
2714                 ctx->object = action;
2715                 ctx->objmask = NULL;
2716         }
2717         memset(data, 0, data_size);
2718         out->args.vc.data = data;
2719         ctx->objdata = data_size;
2720         return len;
2721 }
2722
2723 /** Parse pattern item parameter type. */
2724 static int
2725 parse_vc_spec(struct context *ctx, const struct token *token,
2726               const char *str, unsigned int len,
2727               void *buf, unsigned int size)
2728 {
2729         struct buffer *out = buf;
2730         struct rte_flow_item *item;
2731         uint32_t data_size;
2732         int index;
2733         int objmask = 0;
2734
2735         (void)size;
2736         /* Token name must match. */
2737         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2738                 return -1;
2739         /* Parse parameter types. */
2740         switch (ctx->curr) {
2741                 static const enum index prefix[] = NEXT_ENTRY(PREFIX);
2742
2743         case ITEM_PARAM_IS:
2744                 index = 0;
2745                 objmask = 1;
2746                 break;
2747         case ITEM_PARAM_SPEC:
2748                 index = 0;
2749                 break;
2750         case ITEM_PARAM_LAST:
2751                 index = 1;
2752                 break;
2753         case ITEM_PARAM_PREFIX:
2754                 /* Modify next token to expect a prefix. */
2755                 if (ctx->next_num < 2)
2756                         return -1;
2757                 ctx->next[ctx->next_num - 2] = prefix;
2758                 /* Fall through. */
2759         case ITEM_PARAM_MASK:
2760                 index = 2;
2761                 break;
2762         default:
2763                 return -1;
2764         }
2765         /* Nothing else to do if there is no buffer. */
2766         if (!out)
2767                 return len;
2768         if (!out->args.vc.pattern_n)
2769                 return -1;
2770         item = &out->args.vc.pattern[out->args.vc.pattern_n - 1];
2771         data_size = ctx->objdata / 3; /* spec, last, mask */
2772         /* Point to selected object. */
2773         ctx->object = out->args.vc.data + (data_size * index);
2774         if (objmask) {
2775                 ctx->objmask = out->args.vc.data + (data_size * 2); /* mask */
2776                 item->mask = ctx->objmask;
2777         } else
2778                 ctx->objmask = NULL;
2779         /* Update relevant item pointer. */
2780         *((const void **[]){ &item->spec, &item->last, &item->mask })[index] =
2781                 ctx->object;
2782         return len;
2783 }
2784
2785 /** Parse action configuration field. */
2786 static int
2787 parse_vc_conf(struct context *ctx, const struct token *token,
2788               const char *str, unsigned int len,
2789               void *buf, unsigned int size)
2790 {
2791         struct buffer *out = buf;
2792
2793         (void)size;
2794         /* Token name must match. */
2795         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2796                 return -1;
2797         /* Nothing else to do if there is no buffer. */
2798         if (!out)
2799                 return len;
2800         /* Point to selected object. */
2801         ctx->object = out->args.vc.data;
2802         ctx->objmask = NULL;
2803         return len;
2804 }
2805
2806 /** Parse RSS action. */
2807 static int
2808 parse_vc_action_rss(struct context *ctx, const struct token *token,
2809                     const char *str, unsigned int len,
2810                     void *buf, unsigned int size)
2811 {
2812         struct buffer *out = buf;
2813         struct rte_flow_action *action;
2814         struct action_rss_data *action_rss_data;
2815         unsigned int i;
2816         int ret;
2817
2818         ret = parse_vc(ctx, token, str, len, buf, size);
2819         if (ret < 0)
2820                 return ret;
2821         /* Nothing else to do if there is no buffer. */
2822         if (!out)
2823                 return ret;
2824         if (!out->args.vc.actions_n)
2825                 return -1;
2826         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
2827         /* Point to selected object. */
2828         ctx->object = out->args.vc.data;
2829         ctx->objmask = NULL;
2830         /* Set up default configuration. */
2831         action_rss_data = ctx->object;
2832         *action_rss_data = (struct action_rss_data){
2833                 .conf = (struct rte_flow_action_rss){
2834                         .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
2835                         .level = 0,
2836                         .types = rss_hf,
2837                         .key_len = sizeof(action_rss_data->key),
2838                         .queue_num = RTE_MIN(nb_rxq, ACTION_RSS_QUEUE_NUM),
2839                         .key = action_rss_data->key,
2840                         .queue = action_rss_data->queue,
2841                 },
2842                 .key = "testpmd's default RSS hash key, "
2843                         "override it for better balancing",
2844                 .queue = { 0 },
2845         };
2846         for (i = 0; i < action_rss_data->conf.queue_num; ++i)
2847                 action_rss_data->queue[i] = i;
2848         if (!port_id_is_invalid(ctx->port, DISABLED_WARN) &&
2849             ctx->port != (portid_t)RTE_PORT_ALL) {
2850                 struct rte_eth_dev_info info;
2851
2852                 rte_eth_dev_info_get(ctx->port, &info);
2853                 action_rss_data->conf.key_len =
2854                         RTE_MIN(sizeof(action_rss_data->key),
2855                                 info.hash_key_size);
2856         }
2857         action->conf = &action_rss_data->conf;
2858         return ret;
2859 }
2860
2861 /**
2862  * Parse func field for RSS action.
2863  *
2864  * The RTE_ETH_HASH_FUNCTION_* value to assign is derived from the
2865  * ACTION_RSS_FUNC_* index that called this function.
2866  */
2867 static int
2868 parse_vc_action_rss_func(struct context *ctx, const struct token *token,
2869                          const char *str, unsigned int len,
2870                          void *buf, unsigned int size)
2871 {
2872         struct action_rss_data *action_rss_data;
2873         enum rte_eth_hash_function func;
2874
2875         (void)buf;
2876         (void)size;
2877         /* Token name must match. */
2878         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2879                 return -1;
2880         switch (ctx->curr) {
2881         case ACTION_RSS_FUNC_DEFAULT:
2882                 func = RTE_ETH_HASH_FUNCTION_DEFAULT;
2883                 break;
2884         case ACTION_RSS_FUNC_TOEPLITZ:
2885                 func = RTE_ETH_HASH_FUNCTION_TOEPLITZ;
2886                 break;
2887         case ACTION_RSS_FUNC_SIMPLE_XOR:
2888                 func = RTE_ETH_HASH_FUNCTION_SIMPLE_XOR;
2889                 break;
2890         default:
2891                 return -1;
2892         }
2893         if (!ctx->object)
2894                 return len;
2895         action_rss_data = ctx->object;
2896         action_rss_data->conf.func = func;
2897         return len;
2898 }
2899
2900 /**
2901  * Parse type field for RSS action.
2902  *
2903  * Valid tokens are type field names and the "end" token.
2904  */
2905 static int
2906 parse_vc_action_rss_type(struct context *ctx, const struct token *token,
2907                           const char *str, unsigned int len,
2908                           void *buf, unsigned int size)
2909 {
2910         static const enum index next[] = NEXT_ENTRY(ACTION_RSS_TYPE);
2911         struct action_rss_data *action_rss_data;
2912         unsigned int i;
2913
2914         (void)token;
2915         (void)buf;
2916         (void)size;
2917         if (ctx->curr != ACTION_RSS_TYPE)
2918                 return -1;
2919         if (!(ctx->objdata >> 16) && ctx->object) {
2920                 action_rss_data = ctx->object;
2921                 action_rss_data->conf.types = 0;
2922         }
2923         if (!strcmp_partial("end", str, len)) {
2924                 ctx->objdata &= 0xffff;
2925                 return len;
2926         }
2927         for (i = 0; rss_type_table[i].str; ++i)
2928                 if (!strcmp_partial(rss_type_table[i].str, str, len))
2929                         break;
2930         if (!rss_type_table[i].str)
2931                 return -1;
2932         ctx->objdata = 1 << 16 | (ctx->objdata & 0xffff);
2933         /* Repeat token. */
2934         if (ctx->next_num == RTE_DIM(ctx->next))
2935                 return -1;
2936         ctx->next[ctx->next_num++] = next;
2937         if (!ctx->object)
2938                 return len;
2939         action_rss_data = ctx->object;
2940         action_rss_data->conf.types |= rss_type_table[i].rss_type;
2941         return len;
2942 }
2943
2944 /**
2945  * Parse queue field for RSS action.
2946  *
2947  * Valid tokens are queue indices and the "end" token.
2948  */
2949 static int
2950 parse_vc_action_rss_queue(struct context *ctx, const struct token *token,
2951                           const char *str, unsigned int len,
2952                           void *buf, unsigned int size)
2953 {
2954         static const enum index next[] = NEXT_ENTRY(ACTION_RSS_QUEUE);
2955         struct action_rss_data *action_rss_data;
2956         int ret;
2957         int i;
2958
2959         (void)token;
2960         (void)buf;
2961         (void)size;
2962         if (ctx->curr != ACTION_RSS_QUEUE)
2963                 return -1;
2964         i = ctx->objdata >> 16;
2965         if (!strcmp_partial("end", str, len)) {
2966                 ctx->objdata &= 0xffff;
2967                 goto end;
2968         }
2969         if (i >= ACTION_RSS_QUEUE_NUM)
2970                 return -1;
2971         if (push_args(ctx,
2972                       ARGS_ENTRY_ARB(offsetof(struct action_rss_data, queue) +
2973                                      i * sizeof(action_rss_data->queue[i]),
2974                                      sizeof(action_rss_data->queue[i]))))
2975                 return -1;
2976         ret = parse_int(ctx, token, str, len, NULL, 0);
2977         if (ret < 0) {
2978                 pop_args(ctx);
2979                 return -1;
2980         }
2981         ++i;
2982         ctx->objdata = i << 16 | (ctx->objdata & 0xffff);
2983         /* Repeat token. */
2984         if (ctx->next_num == RTE_DIM(ctx->next))
2985                 return -1;
2986         ctx->next[ctx->next_num++] = next;
2987 end:
2988         if (!ctx->object)
2989                 return len;
2990         action_rss_data = ctx->object;
2991         action_rss_data->conf.queue_num = i;
2992         action_rss_data->conf.queue = i ? action_rss_data->queue : NULL;
2993         return len;
2994 }
2995
2996 /** Parse VXLAN encap action. */
2997 static int
2998 parse_vc_action_vxlan_encap(struct context *ctx, const struct token *token,
2999                             const char *str, unsigned int len,
3000                             void *buf, unsigned int size)
3001 {
3002         struct buffer *out = buf;
3003         struct rte_flow_action *action;
3004         struct action_vxlan_encap_data *action_vxlan_encap_data;
3005         int ret;
3006
3007         ret = parse_vc(ctx, token, str, len, buf, size);
3008         if (ret < 0)
3009                 return ret;
3010         /* Nothing else to do if there is no buffer. */
3011         if (!out)
3012                 return ret;
3013         if (!out->args.vc.actions_n)
3014                 return -1;
3015         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
3016         /* Point to selected object. */
3017         ctx->object = out->args.vc.data;
3018         ctx->objmask = NULL;
3019         /* Set up default configuration. */
3020         action_vxlan_encap_data = ctx->object;
3021         *action_vxlan_encap_data = (struct action_vxlan_encap_data){
3022                 .conf = (struct rte_flow_action_vxlan_encap){
3023                         .definition = action_vxlan_encap_data->items,
3024                 },
3025                 .items = {
3026                         {
3027                                 .type = RTE_FLOW_ITEM_TYPE_ETH,
3028                                 .spec = &action_vxlan_encap_data->item_eth,
3029                                 .mask = &rte_flow_item_eth_mask,
3030                         },
3031                         {
3032                                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
3033                                 .spec = &action_vxlan_encap_data->item_vlan,
3034                                 .mask = &rte_flow_item_vlan_mask,
3035                         },
3036                         {
3037                                 .type = RTE_FLOW_ITEM_TYPE_IPV4,
3038                                 .spec = &action_vxlan_encap_data->item_ipv4,
3039                                 .mask = &rte_flow_item_ipv4_mask,
3040                         },
3041                         {
3042                                 .type = RTE_FLOW_ITEM_TYPE_UDP,
3043                                 .spec = &action_vxlan_encap_data->item_udp,
3044                                 .mask = &rte_flow_item_udp_mask,
3045                         },
3046                         {
3047                                 .type = RTE_FLOW_ITEM_TYPE_VXLAN,
3048                                 .spec = &action_vxlan_encap_data->item_vxlan,
3049                                 .mask = &rte_flow_item_vxlan_mask,
3050                         },
3051                         {
3052                                 .type = RTE_FLOW_ITEM_TYPE_END,
3053                         },
3054                 },
3055                 .item_eth.type = 0,
3056                 .item_vlan = {
3057                         .tci = vxlan_encap_conf.vlan_tci,
3058                         .inner_type = 0,
3059                 },
3060                 .item_ipv4.hdr = {
3061                         .src_addr = vxlan_encap_conf.ipv4_src,
3062                         .dst_addr = vxlan_encap_conf.ipv4_dst,
3063                 },
3064                 .item_udp.hdr = {
3065                         .src_port = vxlan_encap_conf.udp_src,
3066                         .dst_port = vxlan_encap_conf.udp_dst,
3067                 },
3068                 .item_vxlan.flags = 0,
3069         };
3070         memcpy(action_vxlan_encap_data->item_eth.dst.addr_bytes,
3071                vxlan_encap_conf.eth_dst, ETHER_ADDR_LEN);
3072         memcpy(action_vxlan_encap_data->item_eth.src.addr_bytes,
3073                vxlan_encap_conf.eth_src, ETHER_ADDR_LEN);
3074         if (!vxlan_encap_conf.select_ipv4) {
3075                 memcpy(&action_vxlan_encap_data->item_ipv6.hdr.src_addr,
3076                        &vxlan_encap_conf.ipv6_src,
3077                        sizeof(vxlan_encap_conf.ipv6_src));
3078                 memcpy(&action_vxlan_encap_data->item_ipv6.hdr.dst_addr,
3079                        &vxlan_encap_conf.ipv6_dst,
3080                        sizeof(vxlan_encap_conf.ipv6_dst));
3081                 action_vxlan_encap_data->items[2] = (struct rte_flow_item){
3082                         .type = RTE_FLOW_ITEM_TYPE_IPV6,
3083                         .spec = &action_vxlan_encap_data->item_ipv6,
3084                         .mask = &rte_flow_item_ipv6_mask,
3085                 };
3086         }
3087         if (!vxlan_encap_conf.select_vlan)
3088                 action_vxlan_encap_data->items[1].type =
3089                         RTE_FLOW_ITEM_TYPE_VOID;
3090         memcpy(action_vxlan_encap_data->item_vxlan.vni, vxlan_encap_conf.vni,
3091                RTE_DIM(vxlan_encap_conf.vni));
3092         action->conf = &action_vxlan_encap_data->conf;
3093         return ret;
3094 }
3095
3096 /** Parse tokens for destroy command. */
3097 static int
3098 parse_destroy(struct context *ctx, const struct token *token,
3099               const char *str, unsigned int len,
3100               void *buf, unsigned int size)
3101 {
3102         struct buffer *out = buf;
3103
3104         /* Token name must match. */
3105         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
3106                 return -1;
3107         /* Nothing else to do if there is no buffer. */
3108         if (!out)
3109                 return len;
3110         if (!out->command) {
3111                 if (ctx->curr != DESTROY)
3112                         return -1;
3113                 if (sizeof(*out) > size)
3114                         return -1;
3115                 out->command = ctx->curr;
3116                 ctx->objdata = 0;
3117                 ctx->object = out;
3118                 ctx->objmask = NULL;
3119                 out->args.destroy.rule =
3120                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
3121                                                sizeof(double));
3122                 return len;
3123         }
3124         if (((uint8_t *)(out->args.destroy.rule + out->args.destroy.rule_n) +
3125              sizeof(*out->args.destroy.rule)) > (uint8_t *)out + size)
3126                 return -1;
3127         ctx->objdata = 0;
3128         ctx->object = out->args.destroy.rule + out->args.destroy.rule_n++;
3129         ctx->objmask = NULL;
3130         return len;
3131 }
3132
3133 /** Parse tokens for flush command. */
3134 static int
3135 parse_flush(struct context *ctx, const struct token *token,
3136             const char *str, unsigned int len,
3137             void *buf, unsigned int size)
3138 {
3139         struct buffer *out = buf;
3140
3141         /* Token name must match. */
3142         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
3143                 return -1;
3144         /* Nothing else to do if there is no buffer. */
3145         if (!out)
3146                 return len;
3147         if (!out->command) {
3148                 if (ctx->curr != FLUSH)
3149                         return -1;
3150                 if (sizeof(*out) > size)
3151                         return -1;
3152                 out->command = ctx->curr;
3153                 ctx->objdata = 0;
3154                 ctx->object = out;
3155                 ctx->objmask = NULL;
3156         }
3157         return len;
3158 }
3159
3160 /** Parse tokens for query command. */
3161 static int
3162 parse_query(struct context *ctx, const struct token *token,
3163             const char *str, unsigned int len,
3164             void *buf, unsigned int size)
3165 {
3166         struct buffer *out = buf;
3167
3168         /* Token name must match. */
3169         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
3170                 return -1;
3171         /* Nothing else to do if there is no buffer. */
3172         if (!out)
3173                 return len;
3174         if (!out->command) {
3175                 if (ctx->curr != QUERY)
3176                         return -1;
3177                 if (sizeof(*out) > size)
3178                         return -1;
3179                 out->command = ctx->curr;
3180                 ctx->objdata = 0;
3181                 ctx->object = out;
3182                 ctx->objmask = NULL;
3183         }
3184         return len;
3185 }
3186
3187 /** Parse action names. */
3188 static int
3189 parse_action(struct context *ctx, const struct token *token,
3190              const char *str, unsigned int len,
3191              void *buf, unsigned int size)
3192 {
3193         struct buffer *out = buf;
3194         const struct arg *arg = pop_args(ctx);
3195         unsigned int i;
3196
3197         (void)size;
3198         /* Argument is expected. */
3199         if (!arg)
3200                 return -1;
3201         /* Parse action name. */
3202         for (i = 0; next_action[i]; ++i) {
3203                 const struct parse_action_priv *priv;
3204
3205                 token = &token_list[next_action[i]];
3206                 if (strcmp_partial(token->name, str, len))
3207                         continue;
3208                 priv = token->priv;
3209                 if (!priv)
3210                         goto error;
3211                 if (out)
3212                         memcpy((uint8_t *)ctx->object + arg->offset,
3213                                &priv->type,
3214                                arg->size);
3215                 return len;
3216         }
3217 error:
3218         push_args(ctx, arg);
3219         return -1;
3220 }
3221
3222 /** Parse tokens for list command. */
3223 static int
3224 parse_list(struct context *ctx, const struct token *token,
3225            const char *str, unsigned int len,
3226            void *buf, unsigned int size)
3227 {
3228         struct buffer *out = buf;
3229
3230         /* Token name must match. */
3231         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
3232                 return -1;
3233         /* Nothing else to do if there is no buffer. */
3234         if (!out)
3235                 return len;
3236         if (!out->command) {
3237                 if (ctx->curr != LIST)
3238                         return -1;
3239                 if (sizeof(*out) > size)
3240                         return -1;
3241                 out->command = ctx->curr;
3242                 ctx->objdata = 0;
3243                 ctx->object = out;
3244                 ctx->objmask = NULL;
3245                 out->args.list.group =
3246                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
3247                                                sizeof(double));
3248                 return len;
3249         }
3250         if (((uint8_t *)(out->args.list.group + out->args.list.group_n) +
3251              sizeof(*out->args.list.group)) > (uint8_t *)out + size)
3252                 return -1;
3253         ctx->objdata = 0;
3254         ctx->object = out->args.list.group + out->args.list.group_n++;
3255         ctx->objmask = NULL;
3256         return len;
3257 }
3258
3259 /** Parse tokens for isolate command. */
3260 static int
3261 parse_isolate(struct context *ctx, const struct token *token,
3262               const char *str, unsigned int len,
3263               void *buf, unsigned int size)
3264 {
3265         struct buffer *out = buf;
3266
3267         /* Token name must match. */
3268         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
3269                 return -1;
3270         /* Nothing else to do if there is no buffer. */
3271         if (!out)
3272                 return len;
3273         if (!out->command) {
3274                 if (ctx->curr != ISOLATE)
3275                         return -1;
3276                 if (sizeof(*out) > size)
3277                         return -1;
3278                 out->command = ctx->curr;
3279                 ctx->objdata = 0;
3280                 ctx->object = out;
3281                 ctx->objmask = NULL;
3282         }
3283         return len;
3284 }
3285
3286 /**
3287  * Parse signed/unsigned integers 8 to 64-bit long.
3288  *
3289  * Last argument (ctx->args) is retrieved to determine integer type and
3290  * storage location.
3291  */
3292 static int
3293 parse_int(struct context *ctx, const struct token *token,
3294           const char *str, unsigned int len,
3295           void *buf, unsigned int size)
3296 {
3297         const struct arg *arg = pop_args(ctx);
3298         uintmax_t u;
3299         char *end;
3300
3301         (void)token;
3302         /* Argument is expected. */
3303         if (!arg)
3304                 return -1;
3305         errno = 0;
3306         u = arg->sign ?
3307                 (uintmax_t)strtoimax(str, &end, 0) :
3308                 strtoumax(str, &end, 0);
3309         if (errno || (size_t)(end - str) != len)
3310                 goto error;
3311         if (arg->bounded &&
3312             ((arg->sign && ((intmax_t)u < (intmax_t)arg->min ||
3313                             (intmax_t)u > (intmax_t)arg->max)) ||
3314              (!arg->sign && (u < arg->min || u > arg->max))))
3315                 goto error;
3316         if (!ctx->object)
3317                 return len;
3318         if (arg->mask) {
3319                 if (!arg_entry_bf_fill(ctx->object, u, arg) ||
3320                     !arg_entry_bf_fill(ctx->objmask, -1, arg))
3321                         goto error;
3322                 return len;
3323         }
3324         buf = (uint8_t *)ctx->object + arg->offset;
3325         size = arg->size;
3326 objmask:
3327         switch (size) {
3328         case sizeof(uint8_t):
3329                 *(uint8_t *)buf = u;
3330                 break;
3331         case sizeof(uint16_t):
3332                 *(uint16_t *)buf = arg->hton ? rte_cpu_to_be_16(u) : u;
3333                 break;
3334         case sizeof(uint8_t [3]):
3335 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
3336                 if (!arg->hton) {
3337                         ((uint8_t *)buf)[0] = u;
3338                         ((uint8_t *)buf)[1] = u >> 8;
3339                         ((uint8_t *)buf)[2] = u >> 16;
3340                         break;
3341                 }
3342 #endif
3343                 ((uint8_t *)buf)[0] = u >> 16;
3344                 ((uint8_t *)buf)[1] = u >> 8;
3345                 ((uint8_t *)buf)[2] = u;
3346                 break;
3347         case sizeof(uint32_t):
3348                 *(uint32_t *)buf = arg->hton ? rte_cpu_to_be_32(u) : u;
3349                 break;
3350         case sizeof(uint64_t):
3351                 *(uint64_t *)buf = arg->hton ? rte_cpu_to_be_64(u) : u;
3352                 break;
3353         default:
3354                 goto error;
3355         }
3356         if (ctx->objmask && buf != (uint8_t *)ctx->objmask + arg->offset) {
3357                 u = -1;
3358                 buf = (uint8_t *)ctx->objmask + arg->offset;
3359                 goto objmask;
3360         }
3361         return len;
3362 error:
3363         push_args(ctx, arg);
3364         return -1;
3365 }
3366
3367 /**
3368  * Parse a string.
3369  *
3370  * Three arguments (ctx->args) are retrieved from the stack to store data,
3371  * its actual length and address (in that order).
3372  */
3373 static int
3374 parse_string(struct context *ctx, const struct token *token,
3375              const char *str, unsigned int len,
3376              void *buf, unsigned int size)
3377 {
3378         const struct arg *arg_data = pop_args(ctx);
3379         const struct arg *arg_len = pop_args(ctx);
3380         const struct arg *arg_addr = pop_args(ctx);
3381         char tmp[16]; /* Ought to be enough. */
3382         int ret;
3383
3384         /* Arguments are expected. */
3385         if (!arg_data)
3386                 return -1;
3387         if (!arg_len) {
3388                 push_args(ctx, arg_data);
3389                 return -1;
3390         }
3391         if (!arg_addr) {
3392                 push_args(ctx, arg_len);
3393                 push_args(ctx, arg_data);
3394                 return -1;
3395         }
3396         size = arg_data->size;
3397         /* Bit-mask fill is not supported. */
3398         if (arg_data->mask || size < len)
3399                 goto error;
3400         if (!ctx->object)
3401                 return len;
3402         /* Let parse_int() fill length information first. */
3403         ret = snprintf(tmp, sizeof(tmp), "%u", len);
3404         if (ret < 0)
3405                 goto error;
3406         push_args(ctx, arg_len);
3407         ret = parse_int(ctx, token, tmp, ret, NULL, 0);
3408         if (ret < 0) {
3409                 pop_args(ctx);
3410                 goto error;
3411         }
3412         buf = (uint8_t *)ctx->object + arg_data->offset;
3413         /* Output buffer is not necessarily NUL-terminated. */
3414         memcpy(buf, str, len);
3415         memset((uint8_t *)buf + len, 0x00, size - len);
3416         if (ctx->objmask)
3417                 memset((uint8_t *)ctx->objmask + arg_data->offset, 0xff, len);
3418         /* Save address if requested. */
3419         if (arg_addr->size) {
3420                 memcpy((uint8_t *)ctx->object + arg_addr->offset,
3421                        (void *[]){
3422                         (uint8_t *)ctx->object + arg_data->offset
3423                        },
3424                        arg_addr->size);
3425                 if (ctx->objmask)
3426                         memcpy((uint8_t *)ctx->objmask + arg_addr->offset,
3427                                (void *[]){
3428                                 (uint8_t *)ctx->objmask + arg_data->offset
3429                                },
3430                                arg_addr->size);
3431         }
3432         return len;
3433 error:
3434         push_args(ctx, arg_addr);
3435         push_args(ctx, arg_len);
3436         push_args(ctx, arg_data);
3437         return -1;
3438 }
3439
3440 /**
3441  * Parse a MAC address.
3442  *
3443  * Last argument (ctx->args) is retrieved to determine storage size and
3444  * location.
3445  */
3446 static int
3447 parse_mac_addr(struct context *ctx, const struct token *token,
3448                const char *str, unsigned int len,
3449                void *buf, unsigned int size)
3450 {
3451         const struct arg *arg = pop_args(ctx);
3452         struct ether_addr tmp;
3453         int ret;
3454
3455         (void)token;
3456         /* Argument is expected. */
3457         if (!arg)
3458                 return -1;
3459         size = arg->size;
3460         /* Bit-mask fill is not supported. */
3461         if (arg->mask || size != sizeof(tmp))
3462                 goto error;
3463         /* Only network endian is supported. */
3464         if (!arg->hton)
3465                 goto error;
3466         ret = cmdline_parse_etheraddr(NULL, str, &tmp, size);
3467         if (ret < 0 || (unsigned int)ret != len)
3468                 goto error;
3469         if (!ctx->object)
3470                 return len;
3471         buf = (uint8_t *)ctx->object + arg->offset;
3472         memcpy(buf, &tmp, size);
3473         if (ctx->objmask)
3474                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
3475         return len;
3476 error:
3477         push_args(ctx, arg);
3478         return -1;
3479 }
3480
3481 /**
3482  * Parse an IPv4 address.
3483  *
3484  * Last argument (ctx->args) is retrieved to determine storage size and
3485  * location.
3486  */
3487 static int
3488 parse_ipv4_addr(struct context *ctx, const struct token *token,
3489                 const char *str, unsigned int len,
3490                 void *buf, unsigned int size)
3491 {
3492         const struct arg *arg = pop_args(ctx);
3493         char str2[len + 1];
3494         struct in_addr tmp;
3495         int ret;
3496
3497         /* Argument is expected. */
3498         if (!arg)
3499                 return -1;
3500         size = arg->size;
3501         /* Bit-mask fill is not supported. */
3502         if (arg->mask || size != sizeof(tmp))
3503                 goto error;
3504         /* Only network endian is supported. */
3505         if (!arg->hton)
3506                 goto error;
3507         memcpy(str2, str, len);
3508         str2[len] = '\0';
3509         ret = inet_pton(AF_INET, str2, &tmp);
3510         if (ret != 1) {
3511                 /* Attempt integer parsing. */
3512                 push_args(ctx, arg);
3513                 return parse_int(ctx, token, str, len, buf, size);
3514         }
3515         if (!ctx->object)
3516                 return len;
3517         buf = (uint8_t *)ctx->object + arg->offset;
3518         memcpy(buf, &tmp, size);
3519         if (ctx->objmask)
3520                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
3521         return len;
3522 error:
3523         push_args(ctx, arg);
3524         return -1;
3525 }
3526
3527 /**
3528  * Parse an IPv6 address.
3529  *
3530  * Last argument (ctx->args) is retrieved to determine storage size and
3531  * location.
3532  */
3533 static int
3534 parse_ipv6_addr(struct context *ctx, const struct token *token,
3535                 const char *str, unsigned int len,
3536                 void *buf, unsigned int size)
3537 {
3538         const struct arg *arg = pop_args(ctx);
3539         char str2[len + 1];
3540         struct in6_addr tmp;
3541         int ret;
3542
3543         (void)token;
3544         /* Argument is expected. */
3545         if (!arg)
3546                 return -1;
3547         size = arg->size;
3548         /* Bit-mask fill is not supported. */
3549         if (arg->mask || size != sizeof(tmp))
3550                 goto error;
3551         /* Only network endian is supported. */
3552         if (!arg->hton)
3553                 goto error;
3554         memcpy(str2, str, len);
3555         str2[len] = '\0';
3556         ret = inet_pton(AF_INET6, str2, &tmp);
3557         if (ret != 1)
3558                 goto error;
3559         if (!ctx->object)
3560                 return len;
3561         buf = (uint8_t *)ctx->object + arg->offset;
3562         memcpy(buf, &tmp, size);
3563         if (ctx->objmask)
3564                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
3565         return len;
3566 error:
3567         push_args(ctx, arg);
3568         return -1;
3569 }
3570
3571 /** Boolean values (even indices stand for false). */
3572 static const char *const boolean_name[] = {
3573         "0", "1",
3574         "false", "true",
3575         "no", "yes",
3576         "N", "Y",
3577         "off", "on",
3578         NULL,
3579 };
3580
3581 /**
3582  * Parse a boolean value.
3583  *
3584  * Last argument (ctx->args) is retrieved to determine storage size and
3585  * location.
3586  */
3587 static int
3588 parse_boolean(struct context *ctx, const struct token *token,
3589               const char *str, unsigned int len,
3590               void *buf, unsigned int size)
3591 {
3592         const struct arg *arg = pop_args(ctx);
3593         unsigned int i;
3594         int ret;
3595
3596         /* Argument is expected. */
3597         if (!arg)
3598                 return -1;
3599         for (i = 0; boolean_name[i]; ++i)
3600                 if (!strcmp_partial(boolean_name[i], str, len))
3601                         break;
3602         /* Process token as integer. */
3603         if (boolean_name[i])
3604                 str = i & 1 ? "1" : "0";
3605         push_args(ctx, arg);
3606         ret = parse_int(ctx, token, str, strlen(str), buf, size);
3607         return ret > 0 ? (int)len : ret;
3608 }
3609
3610 /** Parse port and update context. */
3611 static int
3612 parse_port(struct context *ctx, const struct token *token,
3613            const char *str, unsigned int len,
3614            void *buf, unsigned int size)
3615 {
3616         struct buffer *out = &(struct buffer){ .port = 0 };
3617         int ret;
3618
3619         if (buf)
3620                 out = buf;
3621         else {
3622                 ctx->objdata = 0;
3623                 ctx->object = out;
3624                 ctx->objmask = NULL;
3625                 size = sizeof(*out);
3626         }
3627         ret = parse_int(ctx, token, str, len, out, size);
3628         if (ret >= 0)
3629                 ctx->port = out->port;
3630         if (!buf)
3631                 ctx->object = NULL;
3632         return ret;
3633 }
3634
3635 /** No completion. */
3636 static int
3637 comp_none(struct context *ctx, const struct token *token,
3638           unsigned int ent, char *buf, unsigned int size)
3639 {
3640         (void)ctx;
3641         (void)token;
3642         (void)ent;
3643         (void)buf;
3644         (void)size;
3645         return 0;
3646 }
3647
3648 /** Complete boolean values. */
3649 static int
3650 comp_boolean(struct context *ctx, const struct token *token,
3651              unsigned int ent, char *buf, unsigned int size)
3652 {
3653         unsigned int i;
3654
3655         (void)ctx;
3656         (void)token;
3657         for (i = 0; boolean_name[i]; ++i)
3658                 if (buf && i == ent)
3659                         return snprintf(buf, size, "%s", boolean_name[i]);
3660         if (buf)
3661                 return -1;
3662         return i;
3663 }
3664
3665 /** Complete action names. */
3666 static int
3667 comp_action(struct context *ctx, const struct token *token,
3668             unsigned int ent, char *buf, unsigned int size)
3669 {
3670         unsigned int i;
3671
3672         (void)ctx;
3673         (void)token;
3674         for (i = 0; next_action[i]; ++i)
3675                 if (buf && i == ent)
3676                         return snprintf(buf, size, "%s",
3677                                         token_list[next_action[i]].name);
3678         if (buf)
3679                 return -1;
3680         return i;
3681 }
3682
3683 /** Complete available ports. */
3684 static int
3685 comp_port(struct context *ctx, const struct token *token,
3686           unsigned int ent, char *buf, unsigned int size)
3687 {
3688         unsigned int i = 0;
3689         portid_t p;
3690
3691         (void)ctx;
3692         (void)token;
3693         RTE_ETH_FOREACH_DEV(p) {
3694                 if (buf && i == ent)
3695                         return snprintf(buf, size, "%u", p);
3696                 ++i;
3697         }
3698         if (buf)
3699                 return -1;
3700         return i;
3701 }
3702
3703 /** Complete available rule IDs. */
3704 static int
3705 comp_rule_id(struct context *ctx, const struct token *token,
3706              unsigned int ent, char *buf, unsigned int size)
3707 {
3708         unsigned int i = 0;
3709         struct rte_port *port;
3710         struct port_flow *pf;
3711
3712         (void)token;
3713         if (port_id_is_invalid(ctx->port, DISABLED_WARN) ||
3714             ctx->port == (portid_t)RTE_PORT_ALL)
3715                 return -1;
3716         port = &ports[ctx->port];
3717         for (pf = port->flow_list; pf != NULL; pf = pf->next) {
3718                 if (buf && i == ent)
3719                         return snprintf(buf, size, "%u", pf->id);
3720                 ++i;
3721         }
3722         if (buf)
3723                 return -1;
3724         return i;
3725 }
3726
3727 /** Complete type field for RSS action. */
3728 static int
3729 comp_vc_action_rss_type(struct context *ctx, const struct token *token,
3730                         unsigned int ent, char *buf, unsigned int size)
3731 {
3732         unsigned int i;
3733
3734         (void)ctx;
3735         (void)token;
3736         for (i = 0; rss_type_table[i].str; ++i)
3737                 ;
3738         if (!buf)
3739                 return i + 1;
3740         if (ent < i)
3741                 return snprintf(buf, size, "%s", rss_type_table[ent].str);
3742         if (ent == i)
3743                 return snprintf(buf, size, "end");
3744         return -1;
3745 }
3746
3747 /** Complete queue field for RSS action. */
3748 static int
3749 comp_vc_action_rss_queue(struct context *ctx, const struct token *token,
3750                          unsigned int ent, char *buf, unsigned int size)
3751 {
3752         (void)ctx;
3753         (void)token;
3754         if (!buf)
3755                 return nb_rxq + 1;
3756         if (ent < nb_rxq)
3757                 return snprintf(buf, size, "%u", ent);
3758         if (ent == nb_rxq)
3759                 return snprintf(buf, size, "end");
3760         return -1;
3761 }
3762
3763 /** Internal context. */
3764 static struct context cmd_flow_context;
3765
3766 /** Global parser instance (cmdline API). */
3767 cmdline_parse_inst_t cmd_flow;
3768
3769 /** Initialize context. */
3770 static void
3771 cmd_flow_context_init(struct context *ctx)
3772 {
3773         /* A full memset() is not necessary. */
3774         ctx->curr = ZERO;
3775         ctx->prev = ZERO;
3776         ctx->next_num = 0;
3777         ctx->args_num = 0;
3778         ctx->eol = 0;
3779         ctx->last = 0;
3780         ctx->port = 0;
3781         ctx->objdata = 0;
3782         ctx->object = NULL;
3783         ctx->objmask = NULL;
3784 }
3785
3786 /** Parse a token (cmdline API). */
3787 static int
3788 cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result,
3789                unsigned int size)
3790 {
3791         struct context *ctx = &cmd_flow_context;
3792         const struct token *token;
3793         const enum index *list;
3794         int len;
3795         int i;
3796
3797         (void)hdr;
3798         token = &token_list[ctx->curr];
3799         /* Check argument length. */
3800         ctx->eol = 0;
3801         ctx->last = 1;
3802         for (len = 0; src[len]; ++len)
3803                 if (src[len] == '#' || isspace(src[len]))
3804                         break;
3805         if (!len)
3806                 return -1;
3807         /* Last argument and EOL detection. */
3808         for (i = len; src[i]; ++i)
3809                 if (src[i] == '#' || src[i] == '\r' || src[i] == '\n')
3810                         break;
3811                 else if (!isspace(src[i])) {
3812                         ctx->last = 0;
3813                         break;
3814                 }
3815         for (; src[i]; ++i)
3816                 if (src[i] == '\r' || src[i] == '\n') {
3817                         ctx->eol = 1;
3818                         break;
3819                 }
3820         /* Initialize context if necessary. */
3821         if (!ctx->next_num) {
3822                 if (!token->next)
3823                         return 0;
3824                 ctx->next[ctx->next_num++] = token->next[0];
3825         }
3826         /* Process argument through candidates. */
3827         ctx->prev = ctx->curr;
3828         list = ctx->next[ctx->next_num - 1];
3829         for (i = 0; list[i]; ++i) {
3830                 const struct token *next = &token_list[list[i]];
3831                 int tmp;
3832
3833                 ctx->curr = list[i];
3834                 if (next->call)
3835                         tmp = next->call(ctx, next, src, len, result, size);
3836                 else
3837                         tmp = parse_default(ctx, next, src, len, result, size);
3838                 if (tmp == -1 || tmp != len)
3839                         continue;
3840                 token = next;
3841                 break;
3842         }
3843         if (!list[i])
3844                 return -1;
3845         --ctx->next_num;
3846         /* Push subsequent tokens if any. */
3847         if (token->next)
3848                 for (i = 0; token->next[i]; ++i) {
3849                         if (ctx->next_num == RTE_DIM(ctx->next))
3850                                 return -1;
3851                         ctx->next[ctx->next_num++] = token->next[i];
3852                 }
3853         /* Push arguments if any. */
3854         if (token->args)
3855                 for (i = 0; token->args[i]; ++i) {
3856                         if (ctx->args_num == RTE_DIM(ctx->args))
3857                                 return -1;
3858                         ctx->args[ctx->args_num++] = token->args[i];
3859                 }
3860         return len;
3861 }
3862
3863 /** Return number of completion entries (cmdline API). */
3864 static int
3865 cmd_flow_complete_get_nb(cmdline_parse_token_hdr_t *hdr)
3866 {
3867         struct context *ctx = &cmd_flow_context;
3868         const struct token *token = &token_list[ctx->curr];
3869         const enum index *list;
3870         int i;
3871
3872         (void)hdr;
3873         /* Count number of tokens in current list. */
3874         if (ctx->next_num)
3875                 list = ctx->next[ctx->next_num - 1];
3876         else
3877                 list = token->next[0];
3878         for (i = 0; list[i]; ++i)
3879                 ;
3880         if (!i)
3881                 return 0;
3882         /*
3883          * If there is a single token, use its completion callback, otherwise
3884          * return the number of entries.
3885          */
3886         token = &token_list[list[0]];
3887         if (i == 1 && token->comp) {
3888                 /* Save index for cmd_flow_get_help(). */
3889                 ctx->prev = list[0];
3890                 return token->comp(ctx, token, 0, NULL, 0);
3891         }
3892         return i;
3893 }
3894
3895 /** Return a completion entry (cmdline API). */
3896 static int
3897 cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,
3898                           char *dst, unsigned int size)
3899 {
3900         struct context *ctx = &cmd_flow_context;
3901         const struct token *token = &token_list[ctx->curr];
3902         const enum index *list;
3903         int i;
3904
3905         (void)hdr;
3906         /* Count number of tokens in current list. */
3907         if (ctx->next_num)
3908                 list = ctx->next[ctx->next_num - 1];
3909         else
3910                 list = token->next[0];
3911         for (i = 0; list[i]; ++i)
3912                 ;
3913         if (!i)
3914                 return -1;
3915         /* If there is a single token, use its completion callback. */
3916         token = &token_list[list[0]];
3917         if (i == 1 && token->comp) {
3918                 /* Save index for cmd_flow_get_help(). */
3919                 ctx->prev = list[0];
3920                 return token->comp(ctx, token, index, dst, size) < 0 ? -1 : 0;
3921         }
3922         /* Otherwise make sure the index is valid and use defaults. */
3923         if (index >= i)
3924                 return -1;
3925         token = &token_list[list[index]];
3926         snprintf(dst, size, "%s", token->name);
3927         /* Save index for cmd_flow_get_help(). */
3928         ctx->prev = list[index];
3929         return 0;
3930 }
3931
3932 /** Populate help strings for current token (cmdline API). */
3933 static int
3934 cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
3935 {
3936         struct context *ctx = &cmd_flow_context;
3937         const struct token *token = &token_list[ctx->prev];
3938
3939         (void)hdr;
3940         if (!size)
3941                 return -1;
3942         /* Set token type and update global help with details. */
3943         snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
3944         if (token->help)
3945                 cmd_flow.help_str = token->help;
3946         else
3947                 cmd_flow.help_str = token->name;
3948         return 0;
3949 }
3950
3951 /** Token definition template (cmdline API). */
3952 static struct cmdline_token_hdr cmd_flow_token_hdr = {
3953         .ops = &(struct cmdline_token_ops){
3954                 .parse = cmd_flow_parse,
3955                 .complete_get_nb = cmd_flow_complete_get_nb,
3956                 .complete_get_elt = cmd_flow_complete_get_elt,
3957                 .get_help = cmd_flow_get_help,
3958         },
3959         .offset = 0,
3960 };
3961
3962 /** Populate the next dynamic token. */
3963 static void
3964 cmd_flow_tok(cmdline_parse_token_hdr_t **hdr,
3965              cmdline_parse_token_hdr_t **hdr_inst)
3966 {
3967         struct context *ctx = &cmd_flow_context;
3968
3969         /* Always reinitialize context before requesting the first token. */
3970         if (!(hdr_inst - cmd_flow.tokens))
3971                 cmd_flow_context_init(ctx);
3972         /* Return NULL when no more tokens are expected. */
3973         if (!ctx->next_num && ctx->curr) {
3974                 *hdr = NULL;
3975                 return;
3976         }
3977         /* Determine if command should end here. */
3978         if (ctx->eol && ctx->last && ctx->next_num) {
3979                 const enum index *list = ctx->next[ctx->next_num - 1];
3980                 int i;
3981
3982                 for (i = 0; list[i]; ++i) {
3983                         if (list[i] != END)
3984                                 continue;
3985                         *hdr = NULL;
3986                         return;
3987                 }
3988         }
3989         *hdr = &cmd_flow_token_hdr;
3990 }
3991
3992 /** Dispatch parsed buffer to function calls. */
3993 static void
3994 cmd_flow_parsed(const struct buffer *in)
3995 {
3996         switch (in->command) {
3997         case VALIDATE:
3998                 port_flow_validate(in->port, &in->args.vc.attr,
3999                                    in->args.vc.pattern, in->args.vc.actions);
4000                 break;
4001         case CREATE:
4002                 port_flow_create(in->port, &in->args.vc.attr,
4003                                  in->args.vc.pattern, in->args.vc.actions);
4004                 break;
4005         case DESTROY:
4006                 port_flow_destroy(in->port, in->args.destroy.rule_n,
4007                                   in->args.destroy.rule);
4008                 break;
4009         case FLUSH:
4010                 port_flow_flush(in->port);
4011                 break;
4012         case QUERY:
4013                 port_flow_query(in->port, in->args.query.rule,
4014                                 &in->args.query.action);
4015                 break;
4016         case LIST:
4017                 port_flow_list(in->port, in->args.list.group_n,
4018                                in->args.list.group);
4019                 break;
4020         case ISOLATE:
4021                 port_flow_isolate(in->port, in->args.isolate.set);
4022                 break;
4023         default:
4024                 break;
4025         }
4026 }
4027
4028 /** Token generator and output processing callback (cmdline API). */
4029 static void
4030 cmd_flow_cb(void *arg0, struct cmdline *cl, void *arg2)
4031 {
4032         if (cl == NULL)
4033                 cmd_flow_tok(arg0, arg2);
4034         else
4035                 cmd_flow_parsed(arg0);
4036 }
4037
4038 /** Global parser instance (cmdline API). */
4039 cmdline_parse_inst_t cmd_flow = {
4040         .f = cmd_flow_cb,
4041         .data = NULL, /**< Unused. */
4042         .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
4043         .tokens = {
4044                 NULL,
4045         }, /**< Tokens are returned by cmd_flow_tok(). */
4046 };