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