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