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