app/testpmd: fix RSS flow action configuration
[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_ethdev.h>
18 #include <rte_byteorder.h>
19 #include <cmdline_parse.h>
20 #include <cmdline_parse_etheraddr.h>
21 #include <rte_flow.h>
22
23 #include "testpmd.h"
24
25 /** Parser token indices. */
26 enum index {
27         /* Special tokens. */
28         ZERO = 0,
29         END,
30
31         /* Common tokens. */
32         INTEGER,
33         UNSIGNED,
34         PREFIX,
35         BOOLEAN,
36         STRING,
37         MAC_ADDR,
38         IPV4_ADDR,
39         IPV6_ADDR,
40         RULE_ID,
41         PORT_ID,
42         GROUP_ID,
43         PRIORITY_LEVEL,
44
45         /* Top-level command. */
46         FLOW,
47
48         /* Sub-level commands. */
49         VALIDATE,
50         CREATE,
51         DESTROY,
52         FLUSH,
53         QUERY,
54         LIST,
55         ISOLATE,
56
57         /* Destroy arguments. */
58         DESTROY_RULE,
59
60         /* Query arguments. */
61         QUERY_ACTION,
62
63         /* List arguments. */
64         LIST_GROUP,
65
66         /* Validate/create arguments. */
67         GROUP,
68         PRIORITY,
69         INGRESS,
70         EGRESS,
71
72         /* Validate/create pattern. */
73         PATTERN,
74         ITEM_PARAM_IS,
75         ITEM_PARAM_SPEC,
76         ITEM_PARAM_LAST,
77         ITEM_PARAM_MASK,
78         ITEM_PARAM_PREFIX,
79         ITEM_NEXT,
80         ITEM_END,
81         ITEM_VOID,
82         ITEM_INVERT,
83         ITEM_ANY,
84         ITEM_ANY_NUM,
85         ITEM_PF,
86         ITEM_VF,
87         ITEM_VF_ID,
88         ITEM_PORT,
89         ITEM_PORT_INDEX,
90         ITEM_RAW,
91         ITEM_RAW_RELATIVE,
92         ITEM_RAW_SEARCH,
93         ITEM_RAW_OFFSET,
94         ITEM_RAW_LIMIT,
95         ITEM_RAW_PATTERN,
96         ITEM_ETH,
97         ITEM_ETH_DST,
98         ITEM_ETH_SRC,
99         ITEM_ETH_TYPE,
100         ITEM_VLAN,
101         ITEM_VLAN_TPID,
102         ITEM_VLAN_TCI,
103         ITEM_VLAN_PCP,
104         ITEM_VLAN_DEI,
105         ITEM_VLAN_VID,
106         ITEM_IPV4,
107         ITEM_IPV4_TOS,
108         ITEM_IPV4_TTL,
109         ITEM_IPV4_PROTO,
110         ITEM_IPV4_SRC,
111         ITEM_IPV4_DST,
112         ITEM_IPV6,
113         ITEM_IPV6_TC,
114         ITEM_IPV6_FLOW,
115         ITEM_IPV6_PROTO,
116         ITEM_IPV6_HOP,
117         ITEM_IPV6_SRC,
118         ITEM_IPV6_DST,
119         ITEM_ICMP,
120         ITEM_ICMP_TYPE,
121         ITEM_ICMP_CODE,
122         ITEM_UDP,
123         ITEM_UDP_SRC,
124         ITEM_UDP_DST,
125         ITEM_TCP,
126         ITEM_TCP_SRC,
127         ITEM_TCP_DST,
128         ITEM_TCP_FLAGS,
129         ITEM_SCTP,
130         ITEM_SCTP_SRC,
131         ITEM_SCTP_DST,
132         ITEM_SCTP_TAG,
133         ITEM_SCTP_CKSUM,
134         ITEM_VXLAN,
135         ITEM_VXLAN_VNI,
136         ITEM_E_TAG,
137         ITEM_E_TAG_GRP_ECID_B,
138         ITEM_NVGRE,
139         ITEM_NVGRE_TNI,
140         ITEM_MPLS,
141         ITEM_MPLS_LABEL,
142         ITEM_GRE,
143         ITEM_GRE_PROTO,
144         ITEM_FUZZY,
145         ITEM_FUZZY_THRESH,
146         ITEM_GTP,
147         ITEM_GTP_TEID,
148         ITEM_GTPC,
149         ITEM_GTPU,
150         ITEM_GENEVE,
151         ITEM_GENEVE_VNI,
152         ITEM_GENEVE_PROTO,
153
154         /* Validate/create actions. */
155         ACTIONS,
156         ACTION_NEXT,
157         ACTION_END,
158         ACTION_VOID,
159         ACTION_PASSTHRU,
160         ACTION_MARK,
161         ACTION_MARK_ID,
162         ACTION_FLAG,
163         ACTION_QUEUE,
164         ACTION_QUEUE_INDEX,
165         ACTION_DROP,
166         ACTION_COUNT,
167         ACTION_DUP,
168         ACTION_DUP_INDEX,
169         ACTION_RSS,
170         ACTION_RSS_QUEUES,
171         ACTION_RSS_QUEUE,
172         ACTION_PF,
173         ACTION_VF,
174         ACTION_VF_ORIGINAL,
175         ACTION_VF_ID,
176         ACTION_METER,
177         ACTION_METER_ID,
178 };
179
180 /** Size of pattern[] field in struct rte_flow_item_raw. */
181 #define ITEM_RAW_PATTERN_SIZE 36
182
183 /** Storage size for struct rte_flow_item_raw including pattern. */
184 #define ITEM_RAW_SIZE \
185         (offsetof(struct rte_flow_item_raw, pattern) + ITEM_RAW_PATTERN_SIZE)
186
187 /** Maximum number of queue indices in struct rte_flow_action_rss. */
188 #define ACTION_RSS_QUEUE_NUM 32
189
190 /** Storage for struct rte_flow_action_rss including external data. */
191 union action_rss_data {
192         struct rte_flow_action_rss conf;
193         struct {
194                 uint8_t conf_data[offsetof(struct rte_flow_action_rss, queue)];
195                 uint16_t queue[ACTION_RSS_QUEUE_NUM];
196                 struct rte_eth_rss_conf rss_conf;
197                 uint8_t rss_key[RSS_HASH_KEY_LENGTH];
198         } s;
199 };
200
201 /** Maximum number of subsequent tokens and arguments on the stack. */
202 #define CTX_STACK_SIZE 16
203
204 /** Parser context. */
205 struct context {
206         /** Stack of subsequent token lists to process. */
207         const enum index *next[CTX_STACK_SIZE];
208         /** Arguments for stacked tokens. */
209         const void *args[CTX_STACK_SIZE];
210         enum index curr; /**< Current token index. */
211         enum index prev; /**< Index of the last token seen. */
212         int next_num; /**< Number of entries in next[]. */
213         int args_num; /**< Number of entries in args[]. */
214         uint32_t eol:1; /**< EOL has been detected. */
215         uint32_t last:1; /**< No more arguments. */
216         portid_t port; /**< Current port ID (for completions). */
217         uint32_t objdata; /**< Object-specific data. */
218         void *object; /**< Address of current object for relative offsets. */
219         void *objmask; /**< Object a full mask must be written to. */
220 };
221
222 /** Token argument. */
223 struct arg {
224         uint32_t hton:1; /**< Use network byte ordering. */
225         uint32_t sign:1; /**< Value is signed. */
226         uint32_t offset; /**< Relative offset from ctx->object. */
227         uint32_t size; /**< Field size. */
228         const uint8_t *mask; /**< Bit-mask to use instead of offset/size. */
229 };
230
231 /** Parser token definition. */
232 struct token {
233         /** Type displayed during completion (defaults to "TOKEN"). */
234         const char *type;
235         /** Help displayed during completion (defaults to token name). */
236         const char *help;
237         /** Private data used by parser functions. */
238         const void *priv;
239         /**
240          * Lists of subsequent tokens to push on the stack. Each call to the
241          * parser consumes the last entry of that stack.
242          */
243         const enum index *const *next;
244         /** Arguments stack for subsequent tokens that need them. */
245         const struct arg *const *args;
246         /**
247          * Token-processing callback, returns -1 in case of error, the
248          * length of the matched string otherwise. If NULL, attempts to
249          * match the token name.
250          *
251          * If buf is not NULL, the result should be stored in it according
252          * to context. An error is returned if not large enough.
253          */
254         int (*call)(struct context *ctx, const struct token *token,
255                     const char *str, unsigned int len,
256                     void *buf, unsigned int size);
257         /**
258          * Callback that provides possible values for this token, used for
259          * completion. Returns -1 in case of error, the number of possible
260          * values otherwise. If NULL, the token name is used.
261          *
262          * If buf is not NULL, entry index ent is written to buf and the
263          * full length of the entry is returned (same behavior as
264          * snprintf()).
265          */
266         int (*comp)(struct context *ctx, const struct token *token,
267                     unsigned int ent, char *buf, unsigned int size);
268         /** Mandatory token name, no default value. */
269         const char *name;
270 };
271
272 /** Static initializer for the next field. */
273 #define NEXT(...) (const enum index *const []){ __VA_ARGS__, NULL, }
274
275 /** Static initializer for a NEXT() entry. */
276 #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, }
277
278 /** Static initializer for the args field. */
279 #define ARGS(...) (const struct arg *const []){ __VA_ARGS__, NULL, }
280
281 /** Static initializer for ARGS() to target a field. */
282 #define ARGS_ENTRY(s, f) \
283         (&(const struct arg){ \
284                 .offset = offsetof(s, f), \
285                 .size = sizeof(((s *)0)->f), \
286         })
287
288 /** Static initializer for ARGS() to target a bit-field. */
289 #define ARGS_ENTRY_BF(s, f, b) \
290         (&(const struct arg){ \
291                 .size = sizeof(s), \
292                 .mask = (const void *)&(const s){ .f = (1 << (b)) - 1 }, \
293         })
294
295 /** Static initializer for ARGS() to target an arbitrary bit-mask. */
296 #define ARGS_ENTRY_MASK(s, f, m) \
297         (&(const struct arg){ \
298                 .offset = offsetof(s, f), \
299                 .size = sizeof(((s *)0)->f), \
300                 .mask = (const void *)(m), \
301         })
302
303 /** Same as ARGS_ENTRY_MASK() using network byte ordering for the value. */
304 #define ARGS_ENTRY_MASK_HTON(s, f, m) \
305         (&(const struct arg){ \
306                 .hton = 1, \
307                 .offset = offsetof(s, f), \
308                 .size = sizeof(((s *)0)->f), \
309                 .mask = (const void *)(m), \
310         })
311
312 /** Static initializer for ARGS() to target a pointer. */
313 #define ARGS_ENTRY_PTR(s, f) \
314         (&(const struct arg){ \
315                 .size = sizeof(*((s *)0)->f), \
316         })
317
318 /** Static initializer for ARGS() with arbitrary size. */
319 #define ARGS_ENTRY_USZ(s, f, sz) \
320         (&(const struct arg){ \
321                 .offset = offsetof(s, f), \
322                 .size = (sz), \
323         })
324
325 /** Static initializer for ARGS() with arbitrary offset and size. */
326 #define ARGS_ENTRY_ARB(o, s) \
327         (&(const struct arg){ \
328                 .offset = (o), \
329                 .size = (s), \
330         })
331
332 /** Same as ARGS_ENTRY() using network byte ordering. */
333 #define ARGS_ENTRY_HTON(s, f) \
334         (&(const struct arg){ \
335                 .hton = 1, \
336                 .offset = offsetof(s, f), \
337                 .size = sizeof(((s *)0)->f), \
338         })
339
340 /** Parser output buffer layout expected by cmd_flow_parsed(). */
341 struct buffer {
342         enum index command; /**< Flow command. */
343         portid_t port; /**< Affected port ID. */
344         union {
345                 struct {
346                         struct rte_flow_attr attr;
347                         struct rte_flow_item *pattern;
348                         struct rte_flow_action *actions;
349                         uint32_t pattern_n;
350                         uint32_t actions_n;
351                         uint8_t *data;
352                 } vc; /**< Validate/create arguments. */
353                 struct {
354                         uint32_t *rule;
355                         uint32_t rule_n;
356                 } destroy; /**< Destroy arguments. */
357                 struct {
358                         uint32_t rule;
359                         enum rte_flow_action_type action;
360                 } query; /**< Query arguments. */
361                 struct {
362                         uint32_t *group;
363                         uint32_t group_n;
364                 } list; /**< List arguments. */
365                 struct {
366                         int set;
367                 } isolate; /**< Isolated mode arguments. */
368         } args; /**< Command arguments. */
369 };
370
371 /** Private data for pattern items. */
372 struct parse_item_priv {
373         enum rte_flow_item_type type; /**< Item type. */
374         uint32_t size; /**< Size of item specification structure. */
375 };
376
377 #define PRIV_ITEM(t, s) \
378         (&(const struct parse_item_priv){ \
379                 .type = RTE_FLOW_ITEM_TYPE_ ## t, \
380                 .size = s, \
381         })
382
383 /** Private data for actions. */
384 struct parse_action_priv {
385         enum rte_flow_action_type type; /**< Action type. */
386         uint32_t size; /**< Size of action configuration structure. */
387 };
388
389 #define PRIV_ACTION(t, s) \
390         (&(const struct parse_action_priv){ \
391                 .type = RTE_FLOW_ACTION_TYPE_ ## t, \
392                 .size = s, \
393         })
394
395 static const enum index next_vc_attr[] = {
396         GROUP,
397         PRIORITY,
398         INGRESS,
399         EGRESS,
400         PATTERN,
401         ZERO,
402 };
403
404 static const enum index next_destroy_attr[] = {
405         DESTROY_RULE,
406         END,
407         ZERO,
408 };
409
410 static const enum index next_list_attr[] = {
411         LIST_GROUP,
412         END,
413         ZERO,
414 };
415
416 static const enum index item_param[] = {
417         ITEM_PARAM_IS,
418         ITEM_PARAM_SPEC,
419         ITEM_PARAM_LAST,
420         ITEM_PARAM_MASK,
421         ITEM_PARAM_PREFIX,
422         ZERO,
423 };
424
425 static const enum index next_item[] = {
426         ITEM_END,
427         ITEM_VOID,
428         ITEM_INVERT,
429         ITEM_ANY,
430         ITEM_PF,
431         ITEM_VF,
432         ITEM_PORT,
433         ITEM_RAW,
434         ITEM_ETH,
435         ITEM_VLAN,
436         ITEM_IPV4,
437         ITEM_IPV6,
438         ITEM_ICMP,
439         ITEM_UDP,
440         ITEM_TCP,
441         ITEM_SCTP,
442         ITEM_VXLAN,
443         ITEM_E_TAG,
444         ITEM_NVGRE,
445         ITEM_MPLS,
446         ITEM_GRE,
447         ITEM_FUZZY,
448         ITEM_GTP,
449         ITEM_GTPC,
450         ITEM_GTPU,
451         ITEM_GENEVE,
452         ZERO,
453 };
454
455 static const enum index item_fuzzy[] = {
456         ITEM_FUZZY_THRESH,
457         ITEM_NEXT,
458         ZERO,
459 };
460
461 static const enum index item_any[] = {
462         ITEM_ANY_NUM,
463         ITEM_NEXT,
464         ZERO,
465 };
466
467 static const enum index item_vf[] = {
468         ITEM_VF_ID,
469         ITEM_NEXT,
470         ZERO,
471 };
472
473 static const enum index item_port[] = {
474         ITEM_PORT_INDEX,
475         ITEM_NEXT,
476         ZERO,
477 };
478
479 static const enum index item_raw[] = {
480         ITEM_RAW_RELATIVE,
481         ITEM_RAW_SEARCH,
482         ITEM_RAW_OFFSET,
483         ITEM_RAW_LIMIT,
484         ITEM_RAW_PATTERN,
485         ITEM_NEXT,
486         ZERO,
487 };
488
489 static const enum index item_eth[] = {
490         ITEM_ETH_DST,
491         ITEM_ETH_SRC,
492         ITEM_ETH_TYPE,
493         ITEM_NEXT,
494         ZERO,
495 };
496
497 static const enum index item_vlan[] = {
498         ITEM_VLAN_TPID,
499         ITEM_VLAN_TCI,
500         ITEM_VLAN_PCP,
501         ITEM_VLAN_DEI,
502         ITEM_VLAN_VID,
503         ITEM_NEXT,
504         ZERO,
505 };
506
507 static const enum index item_ipv4[] = {
508         ITEM_IPV4_TOS,
509         ITEM_IPV4_TTL,
510         ITEM_IPV4_PROTO,
511         ITEM_IPV4_SRC,
512         ITEM_IPV4_DST,
513         ITEM_NEXT,
514         ZERO,
515 };
516
517 static const enum index item_ipv6[] = {
518         ITEM_IPV6_TC,
519         ITEM_IPV6_FLOW,
520         ITEM_IPV6_PROTO,
521         ITEM_IPV6_HOP,
522         ITEM_IPV6_SRC,
523         ITEM_IPV6_DST,
524         ITEM_NEXT,
525         ZERO,
526 };
527
528 static const enum index item_icmp[] = {
529         ITEM_ICMP_TYPE,
530         ITEM_ICMP_CODE,
531         ITEM_NEXT,
532         ZERO,
533 };
534
535 static const enum index item_udp[] = {
536         ITEM_UDP_SRC,
537         ITEM_UDP_DST,
538         ITEM_NEXT,
539         ZERO,
540 };
541
542 static const enum index item_tcp[] = {
543         ITEM_TCP_SRC,
544         ITEM_TCP_DST,
545         ITEM_TCP_FLAGS,
546         ITEM_NEXT,
547         ZERO,
548 };
549
550 static const enum index item_sctp[] = {
551         ITEM_SCTP_SRC,
552         ITEM_SCTP_DST,
553         ITEM_SCTP_TAG,
554         ITEM_SCTP_CKSUM,
555         ITEM_NEXT,
556         ZERO,
557 };
558
559 static const enum index item_vxlan[] = {
560         ITEM_VXLAN_VNI,
561         ITEM_NEXT,
562         ZERO,
563 };
564
565 static const enum index item_e_tag[] = {
566         ITEM_E_TAG_GRP_ECID_B,
567         ITEM_NEXT,
568         ZERO,
569 };
570
571 static const enum index item_nvgre[] = {
572         ITEM_NVGRE_TNI,
573         ITEM_NEXT,
574         ZERO,
575 };
576
577 static const enum index item_mpls[] = {
578         ITEM_MPLS_LABEL,
579         ITEM_NEXT,
580         ZERO,
581 };
582
583 static const enum index item_gre[] = {
584         ITEM_GRE_PROTO,
585         ITEM_NEXT,
586         ZERO,
587 };
588
589 static const enum index item_gtp[] = {
590         ITEM_GTP_TEID,
591         ITEM_NEXT,
592         ZERO,
593 };
594
595 static const enum index item_geneve[] = {
596         ITEM_GENEVE_VNI,
597         ITEM_GENEVE_PROTO,
598         ITEM_NEXT,
599         ZERO,
600 };
601
602 static const enum index next_action[] = {
603         ACTION_END,
604         ACTION_VOID,
605         ACTION_PASSTHRU,
606         ACTION_MARK,
607         ACTION_FLAG,
608         ACTION_QUEUE,
609         ACTION_DROP,
610         ACTION_COUNT,
611         ACTION_DUP,
612         ACTION_RSS,
613         ACTION_PF,
614         ACTION_VF,
615         ACTION_METER,
616         ZERO,
617 };
618
619 static const enum index action_mark[] = {
620         ACTION_MARK_ID,
621         ACTION_NEXT,
622         ZERO,
623 };
624
625 static const enum index action_queue[] = {
626         ACTION_QUEUE_INDEX,
627         ACTION_NEXT,
628         ZERO,
629 };
630
631 static const enum index action_dup[] = {
632         ACTION_DUP_INDEX,
633         ACTION_NEXT,
634         ZERO,
635 };
636
637 static const enum index action_rss[] = {
638         ACTION_RSS_QUEUES,
639         ACTION_NEXT,
640         ZERO,
641 };
642
643 static const enum index action_vf[] = {
644         ACTION_VF_ORIGINAL,
645         ACTION_VF_ID,
646         ACTION_NEXT,
647         ZERO,
648 };
649
650 static const enum index action_meter[] = {
651         ACTION_METER_ID,
652         ACTION_NEXT,
653         ZERO,
654 };
655
656 static int parse_init(struct context *, const struct token *,
657                       const char *, unsigned int,
658                       void *, unsigned int);
659 static int parse_vc(struct context *, const struct token *,
660                     const char *, unsigned int,
661                     void *, unsigned int);
662 static int parse_vc_spec(struct context *, const struct token *,
663                          const char *, unsigned int, void *, unsigned int);
664 static int parse_vc_conf(struct context *, const struct token *,
665                          const char *, unsigned int, void *, unsigned int);
666 static int parse_vc_action_rss(struct context *, const struct token *,
667                                const char *, unsigned int, void *,
668                                unsigned int);
669 static int parse_vc_action_rss_queue(struct context *, const struct token *,
670                                      const char *, unsigned int, void *,
671                                      unsigned int);
672 static int parse_destroy(struct context *, const struct token *,
673                          const char *, unsigned int,
674                          void *, unsigned int);
675 static int parse_flush(struct context *, const struct token *,
676                        const char *, unsigned int,
677                        void *, unsigned int);
678 static int parse_query(struct context *, const struct token *,
679                        const char *, unsigned int,
680                        void *, unsigned int);
681 static int parse_action(struct context *, const struct token *,
682                         const char *, unsigned int,
683                         void *, unsigned int);
684 static int parse_list(struct context *, const struct token *,
685                       const char *, unsigned int,
686                       void *, unsigned int);
687 static int parse_isolate(struct context *, const struct token *,
688                          const char *, unsigned int,
689                          void *, unsigned int);
690 static int parse_int(struct context *, const struct token *,
691                      const char *, unsigned int,
692                      void *, unsigned int);
693 static int parse_prefix(struct context *, const struct token *,
694                         const char *, unsigned int,
695                         void *, unsigned int);
696 static int parse_boolean(struct context *, const struct token *,
697                          const char *, unsigned int,
698                          void *, unsigned int);
699 static int parse_string(struct context *, const struct token *,
700                         const char *, unsigned int,
701                         void *, unsigned int);
702 static int parse_mac_addr(struct context *, const struct token *,
703                           const char *, unsigned int,
704                           void *, unsigned int);
705 static int parse_ipv4_addr(struct context *, const struct token *,
706                            const char *, unsigned int,
707                            void *, unsigned int);
708 static int parse_ipv6_addr(struct context *, const struct token *,
709                            const char *, unsigned int,
710                            void *, unsigned int);
711 static int parse_port(struct context *, const struct token *,
712                       const char *, unsigned int,
713                       void *, unsigned int);
714 static int comp_none(struct context *, const struct token *,
715                      unsigned int, char *, unsigned int);
716 static int comp_boolean(struct context *, const struct token *,
717                         unsigned int, char *, unsigned int);
718 static int comp_action(struct context *, const struct token *,
719                        unsigned int, char *, unsigned int);
720 static int comp_port(struct context *, const struct token *,
721                      unsigned int, char *, unsigned int);
722 static int comp_rule_id(struct context *, const struct token *,
723                         unsigned int, char *, unsigned int);
724 static int comp_vc_action_rss_queue(struct context *, const struct token *,
725                                     unsigned int, char *, unsigned int);
726
727 /** Token definitions. */
728 static const struct token token_list[] = {
729         /* Special tokens. */
730         [ZERO] = {
731                 .name = "ZERO",
732                 .help = "null entry, abused as the entry point",
733                 .next = NEXT(NEXT_ENTRY(FLOW)),
734         },
735         [END] = {
736                 .name = "",
737                 .type = "RETURN",
738                 .help = "command may end here",
739         },
740         /* Common tokens. */
741         [INTEGER] = {
742                 .name = "{int}",
743                 .type = "INTEGER",
744                 .help = "integer value",
745                 .call = parse_int,
746                 .comp = comp_none,
747         },
748         [UNSIGNED] = {
749                 .name = "{unsigned}",
750                 .type = "UNSIGNED",
751                 .help = "unsigned integer value",
752                 .call = parse_int,
753                 .comp = comp_none,
754         },
755         [PREFIX] = {
756                 .name = "{prefix}",
757                 .type = "PREFIX",
758                 .help = "prefix length for bit-mask",
759                 .call = parse_prefix,
760                 .comp = comp_none,
761         },
762         [BOOLEAN] = {
763                 .name = "{boolean}",
764                 .type = "BOOLEAN",
765                 .help = "any boolean value",
766                 .call = parse_boolean,
767                 .comp = comp_boolean,
768         },
769         [STRING] = {
770                 .name = "{string}",
771                 .type = "STRING",
772                 .help = "fixed string",
773                 .call = parse_string,
774                 .comp = comp_none,
775         },
776         [MAC_ADDR] = {
777                 .name = "{MAC address}",
778                 .type = "MAC-48",
779                 .help = "standard MAC address notation",
780                 .call = parse_mac_addr,
781                 .comp = comp_none,
782         },
783         [IPV4_ADDR] = {
784                 .name = "{IPv4 address}",
785                 .type = "IPV4 ADDRESS",
786                 .help = "standard IPv4 address notation",
787                 .call = parse_ipv4_addr,
788                 .comp = comp_none,
789         },
790         [IPV6_ADDR] = {
791                 .name = "{IPv6 address}",
792                 .type = "IPV6 ADDRESS",
793                 .help = "standard IPv6 address notation",
794                 .call = parse_ipv6_addr,
795                 .comp = comp_none,
796         },
797         [RULE_ID] = {
798                 .name = "{rule id}",
799                 .type = "RULE ID",
800                 .help = "rule identifier",
801                 .call = parse_int,
802                 .comp = comp_rule_id,
803         },
804         [PORT_ID] = {
805                 .name = "{port_id}",
806                 .type = "PORT ID",
807                 .help = "port identifier",
808                 .call = parse_port,
809                 .comp = comp_port,
810         },
811         [GROUP_ID] = {
812                 .name = "{group_id}",
813                 .type = "GROUP ID",
814                 .help = "group identifier",
815                 .call = parse_int,
816                 .comp = comp_none,
817         },
818         [PRIORITY_LEVEL] = {
819                 .name = "{level}",
820                 .type = "PRIORITY",
821                 .help = "priority level",
822                 .call = parse_int,
823                 .comp = comp_none,
824         },
825         /* Top-level command. */
826         [FLOW] = {
827                 .name = "flow",
828                 .type = "{command} {port_id} [{arg} [...]]",
829                 .help = "manage ingress/egress flow rules",
830                 .next = NEXT(NEXT_ENTRY
831                              (VALIDATE,
832                               CREATE,
833                               DESTROY,
834                               FLUSH,
835                               LIST,
836                               QUERY,
837                               ISOLATE)),
838                 .call = parse_init,
839         },
840         /* Sub-level commands. */
841         [VALIDATE] = {
842                 .name = "validate",
843                 .help = "check whether a flow rule can be created",
844                 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
845                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
846                 .call = parse_vc,
847         },
848         [CREATE] = {
849                 .name = "create",
850                 .help = "create a flow rule",
851                 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
852                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
853                 .call = parse_vc,
854         },
855         [DESTROY] = {
856                 .name = "destroy",
857                 .help = "destroy specific flow rules",
858                 .next = NEXT(NEXT_ENTRY(DESTROY_RULE), NEXT_ENTRY(PORT_ID)),
859                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
860                 .call = parse_destroy,
861         },
862         [FLUSH] = {
863                 .name = "flush",
864                 .help = "destroy all flow rules",
865                 .next = NEXT(NEXT_ENTRY(PORT_ID)),
866                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
867                 .call = parse_flush,
868         },
869         [QUERY] = {
870                 .name = "query",
871                 .help = "query an existing flow rule",
872                 .next = NEXT(NEXT_ENTRY(QUERY_ACTION),
873                              NEXT_ENTRY(RULE_ID),
874                              NEXT_ENTRY(PORT_ID)),
875                 .args = ARGS(ARGS_ENTRY(struct buffer, args.query.action),
876                              ARGS_ENTRY(struct buffer, args.query.rule),
877                              ARGS_ENTRY(struct buffer, port)),
878                 .call = parse_query,
879         },
880         [LIST] = {
881                 .name = "list",
882                 .help = "list existing flow rules",
883                 .next = NEXT(next_list_attr, NEXT_ENTRY(PORT_ID)),
884                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
885                 .call = parse_list,
886         },
887         [ISOLATE] = {
888                 .name = "isolate",
889                 .help = "restrict ingress traffic to the defined flow rules",
890                 .next = NEXT(NEXT_ENTRY(BOOLEAN),
891                              NEXT_ENTRY(PORT_ID)),
892                 .args = ARGS(ARGS_ENTRY(struct buffer, args.isolate.set),
893                              ARGS_ENTRY(struct buffer, port)),
894                 .call = parse_isolate,
895         },
896         /* Destroy arguments. */
897         [DESTROY_RULE] = {
898                 .name = "rule",
899                 .help = "specify a rule identifier",
900                 .next = NEXT(next_destroy_attr, NEXT_ENTRY(RULE_ID)),
901                 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.destroy.rule)),
902                 .call = parse_destroy,
903         },
904         /* Query arguments. */
905         [QUERY_ACTION] = {
906                 .name = "{action}",
907                 .type = "ACTION",
908                 .help = "action to query, must be part of the rule",
909                 .call = parse_action,
910                 .comp = comp_action,
911         },
912         /* List arguments. */
913         [LIST_GROUP] = {
914                 .name = "group",
915                 .help = "specify a group",
916                 .next = NEXT(next_list_attr, NEXT_ENTRY(GROUP_ID)),
917                 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.list.group)),
918                 .call = parse_list,
919         },
920         /* Validate/create attributes. */
921         [GROUP] = {
922                 .name = "group",
923                 .help = "specify a group",
924                 .next = NEXT(next_vc_attr, NEXT_ENTRY(GROUP_ID)),
925                 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, group)),
926                 .call = parse_vc,
927         },
928         [PRIORITY] = {
929                 .name = "priority",
930                 .help = "specify a priority level",
931                 .next = NEXT(next_vc_attr, NEXT_ENTRY(PRIORITY_LEVEL)),
932                 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, priority)),
933                 .call = parse_vc,
934         },
935         [INGRESS] = {
936                 .name = "ingress",
937                 .help = "affect rule to ingress",
938                 .next = NEXT(next_vc_attr),
939                 .call = parse_vc,
940         },
941         [EGRESS] = {
942                 .name = "egress",
943                 .help = "affect rule to egress",
944                 .next = NEXT(next_vc_attr),
945                 .call = parse_vc,
946         },
947         /* Validate/create pattern. */
948         [PATTERN] = {
949                 .name = "pattern",
950                 .help = "submit a list of pattern items",
951                 .next = NEXT(next_item),
952                 .call = parse_vc,
953         },
954         [ITEM_PARAM_IS] = {
955                 .name = "is",
956                 .help = "match value perfectly (with full bit-mask)",
957                 .call = parse_vc_spec,
958         },
959         [ITEM_PARAM_SPEC] = {
960                 .name = "spec",
961                 .help = "match value according to configured bit-mask",
962                 .call = parse_vc_spec,
963         },
964         [ITEM_PARAM_LAST] = {
965                 .name = "last",
966                 .help = "specify upper bound to establish a range",
967                 .call = parse_vc_spec,
968         },
969         [ITEM_PARAM_MASK] = {
970                 .name = "mask",
971                 .help = "specify bit-mask with relevant bits set to one",
972                 .call = parse_vc_spec,
973         },
974         [ITEM_PARAM_PREFIX] = {
975                 .name = "prefix",
976                 .help = "generate bit-mask from a prefix length",
977                 .call = parse_vc_spec,
978         },
979         [ITEM_NEXT] = {
980                 .name = "/",
981                 .help = "specify next pattern item",
982                 .next = NEXT(next_item),
983         },
984         [ITEM_END] = {
985                 .name = "end",
986                 .help = "end list of pattern items",
987                 .priv = PRIV_ITEM(END, 0),
988                 .next = NEXT(NEXT_ENTRY(ACTIONS)),
989                 .call = parse_vc,
990         },
991         [ITEM_VOID] = {
992                 .name = "void",
993                 .help = "no-op pattern item",
994                 .priv = PRIV_ITEM(VOID, 0),
995                 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
996                 .call = parse_vc,
997         },
998         [ITEM_INVERT] = {
999                 .name = "invert",
1000                 .help = "perform actions when pattern does not match",
1001                 .priv = PRIV_ITEM(INVERT, 0),
1002                 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1003                 .call = parse_vc,
1004         },
1005         [ITEM_ANY] = {
1006                 .name = "any",
1007                 .help = "match any protocol for the current layer",
1008                 .priv = PRIV_ITEM(ANY, sizeof(struct rte_flow_item_any)),
1009                 .next = NEXT(item_any),
1010                 .call = parse_vc,
1011         },
1012         [ITEM_ANY_NUM] = {
1013                 .name = "num",
1014                 .help = "number of layers covered",
1015                 .next = NEXT(item_any, NEXT_ENTRY(UNSIGNED), item_param),
1016                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_any, num)),
1017         },
1018         [ITEM_PF] = {
1019                 .name = "pf",
1020                 .help = "match packets addressed to the physical function",
1021                 .priv = PRIV_ITEM(PF, 0),
1022                 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1023                 .call = parse_vc,
1024         },
1025         [ITEM_VF] = {
1026                 .name = "vf",
1027                 .help = "match packets addressed to a virtual function ID",
1028                 .priv = PRIV_ITEM(VF, sizeof(struct rte_flow_item_vf)),
1029                 .next = NEXT(item_vf),
1030                 .call = parse_vc,
1031         },
1032         [ITEM_VF_ID] = {
1033                 .name = "id",
1034                 .help = "destination VF ID",
1035                 .next = NEXT(item_vf, NEXT_ENTRY(UNSIGNED), item_param),
1036                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_vf, id)),
1037         },
1038         [ITEM_PORT] = {
1039                 .name = "port",
1040                 .help = "device-specific physical port index to use",
1041                 .priv = PRIV_ITEM(PORT, sizeof(struct rte_flow_item_port)),
1042                 .next = NEXT(item_port),
1043                 .call = parse_vc,
1044         },
1045         [ITEM_PORT_INDEX] = {
1046                 .name = "index",
1047                 .help = "physical port index",
1048                 .next = NEXT(item_port, NEXT_ENTRY(UNSIGNED), item_param),
1049                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_port, index)),
1050         },
1051         [ITEM_RAW] = {
1052                 .name = "raw",
1053                 .help = "match an arbitrary byte string",
1054                 .priv = PRIV_ITEM(RAW, ITEM_RAW_SIZE),
1055                 .next = NEXT(item_raw),
1056                 .call = parse_vc,
1057         },
1058         [ITEM_RAW_RELATIVE] = {
1059                 .name = "relative",
1060                 .help = "look for pattern after the previous item",
1061                 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1062                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1063                                            relative, 1)),
1064         },
1065         [ITEM_RAW_SEARCH] = {
1066                 .name = "search",
1067                 .help = "search pattern from offset (see also limit)",
1068                 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1069                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1070                                            search, 1)),
1071         },
1072         [ITEM_RAW_OFFSET] = {
1073                 .name = "offset",
1074                 .help = "absolute or relative offset for pattern",
1075                 .next = NEXT(item_raw, NEXT_ENTRY(INTEGER), item_param),
1076                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, offset)),
1077         },
1078         [ITEM_RAW_LIMIT] = {
1079                 .name = "limit",
1080                 .help = "search area limit for start of pattern",
1081                 .next = NEXT(item_raw, NEXT_ENTRY(UNSIGNED), item_param),
1082                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, limit)),
1083         },
1084         [ITEM_RAW_PATTERN] = {
1085                 .name = "pattern",
1086                 .help = "byte string to look for",
1087                 .next = NEXT(item_raw,
1088                              NEXT_ENTRY(STRING),
1089                              NEXT_ENTRY(ITEM_PARAM_IS,
1090                                         ITEM_PARAM_SPEC,
1091                                         ITEM_PARAM_MASK)),
1092                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, length),
1093                              ARGS_ENTRY_USZ(struct rte_flow_item_raw,
1094                                             pattern,
1095                                             ITEM_RAW_PATTERN_SIZE)),
1096         },
1097         [ITEM_ETH] = {
1098                 .name = "eth",
1099                 .help = "match Ethernet header",
1100                 .priv = PRIV_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
1101                 .next = NEXT(item_eth),
1102                 .call = parse_vc,
1103         },
1104         [ITEM_ETH_DST] = {
1105                 .name = "dst",
1106                 .help = "destination MAC",
1107                 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1108                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, dst)),
1109         },
1110         [ITEM_ETH_SRC] = {
1111                 .name = "src",
1112                 .help = "source MAC",
1113                 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1114                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, src)),
1115         },
1116         [ITEM_ETH_TYPE] = {
1117                 .name = "type",
1118                 .help = "EtherType",
1119                 .next = NEXT(item_eth, NEXT_ENTRY(UNSIGNED), item_param),
1120                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, type)),
1121         },
1122         [ITEM_VLAN] = {
1123                 .name = "vlan",
1124                 .help = "match 802.1Q/ad VLAN tag",
1125                 .priv = PRIV_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
1126                 .next = NEXT(item_vlan),
1127                 .call = parse_vc,
1128         },
1129         [ITEM_VLAN_TPID] = {
1130                 .name = "tpid",
1131                 .help = "tag protocol identifier",
1132                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1133                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tpid)),
1134         },
1135         [ITEM_VLAN_TCI] = {
1136                 .name = "tci",
1137                 .help = "tag control information",
1138                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1139                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tci)),
1140         },
1141         [ITEM_VLAN_PCP] = {
1142                 .name = "pcp",
1143                 .help = "priority code point",
1144                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1145                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1146                                                   tci, "\xe0\x00")),
1147         },
1148         [ITEM_VLAN_DEI] = {
1149                 .name = "dei",
1150                 .help = "drop eligible indicator",
1151                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1152                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1153                                                   tci, "\x10\x00")),
1154         },
1155         [ITEM_VLAN_VID] = {
1156                 .name = "vid",
1157                 .help = "VLAN identifier",
1158                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1159                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1160                                                   tci, "\x0f\xff")),
1161         },
1162         [ITEM_IPV4] = {
1163                 .name = "ipv4",
1164                 .help = "match IPv4 header",
1165                 .priv = PRIV_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
1166                 .next = NEXT(item_ipv4),
1167                 .call = parse_vc,
1168         },
1169         [ITEM_IPV4_TOS] = {
1170                 .name = "tos",
1171                 .help = "type of service",
1172                 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1173                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1174                                              hdr.type_of_service)),
1175         },
1176         [ITEM_IPV4_TTL] = {
1177                 .name = "ttl",
1178                 .help = "time to live",
1179                 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1180                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1181                                              hdr.time_to_live)),
1182         },
1183         [ITEM_IPV4_PROTO] = {
1184                 .name = "proto",
1185                 .help = "next protocol ID",
1186                 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1187                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1188                                              hdr.next_proto_id)),
1189         },
1190         [ITEM_IPV4_SRC] = {
1191                 .name = "src",
1192                 .help = "source address",
1193                 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1194                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1195                                              hdr.src_addr)),
1196         },
1197         [ITEM_IPV4_DST] = {
1198                 .name = "dst",
1199                 .help = "destination address",
1200                 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1201                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1202                                              hdr.dst_addr)),
1203         },
1204         [ITEM_IPV6] = {
1205                 .name = "ipv6",
1206                 .help = "match IPv6 header",
1207                 .priv = PRIV_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
1208                 .next = NEXT(item_ipv6),
1209                 .call = parse_vc,
1210         },
1211         [ITEM_IPV6_TC] = {
1212                 .name = "tc",
1213                 .help = "traffic class",
1214                 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1215                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1216                                                   hdr.vtc_flow,
1217                                                   "\x0f\xf0\x00\x00")),
1218         },
1219         [ITEM_IPV6_FLOW] = {
1220                 .name = "flow",
1221                 .help = "flow label",
1222                 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1223                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1224                                                   hdr.vtc_flow,
1225                                                   "\x00\x0f\xff\xff")),
1226         },
1227         [ITEM_IPV6_PROTO] = {
1228                 .name = "proto",
1229                 .help = "protocol (next header)",
1230                 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1231                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1232                                              hdr.proto)),
1233         },
1234         [ITEM_IPV6_HOP] = {
1235                 .name = "hop",
1236                 .help = "hop limit",
1237                 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1238                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1239                                              hdr.hop_limits)),
1240         },
1241         [ITEM_IPV6_SRC] = {
1242                 .name = "src",
1243                 .help = "source address",
1244                 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1245                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1246                                              hdr.src_addr)),
1247         },
1248         [ITEM_IPV6_DST] = {
1249                 .name = "dst",
1250                 .help = "destination address",
1251                 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1252                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1253                                              hdr.dst_addr)),
1254         },
1255         [ITEM_ICMP] = {
1256                 .name = "icmp",
1257                 .help = "match ICMP header",
1258                 .priv = PRIV_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
1259                 .next = NEXT(item_icmp),
1260                 .call = parse_vc,
1261         },
1262         [ITEM_ICMP_TYPE] = {
1263                 .name = "type",
1264                 .help = "ICMP packet type",
1265                 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1266                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1267                                              hdr.icmp_type)),
1268         },
1269         [ITEM_ICMP_CODE] = {
1270                 .name = "code",
1271                 .help = "ICMP packet code",
1272                 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1273                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1274                                              hdr.icmp_code)),
1275         },
1276         [ITEM_UDP] = {
1277                 .name = "udp",
1278                 .help = "match UDP header",
1279                 .priv = PRIV_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
1280                 .next = NEXT(item_udp),
1281                 .call = parse_vc,
1282         },
1283         [ITEM_UDP_SRC] = {
1284                 .name = "src",
1285                 .help = "UDP source port",
1286                 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1287                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1288                                              hdr.src_port)),
1289         },
1290         [ITEM_UDP_DST] = {
1291                 .name = "dst",
1292                 .help = "UDP destination port",
1293                 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1294                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1295                                              hdr.dst_port)),
1296         },
1297         [ITEM_TCP] = {
1298                 .name = "tcp",
1299                 .help = "match TCP header",
1300                 .priv = PRIV_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
1301                 .next = NEXT(item_tcp),
1302                 .call = parse_vc,
1303         },
1304         [ITEM_TCP_SRC] = {
1305                 .name = "src",
1306                 .help = "TCP source port",
1307                 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1308                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1309                                              hdr.src_port)),
1310         },
1311         [ITEM_TCP_DST] = {
1312                 .name = "dst",
1313                 .help = "TCP destination port",
1314                 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1315                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1316                                              hdr.dst_port)),
1317         },
1318         [ITEM_TCP_FLAGS] = {
1319                 .name = "flags",
1320                 .help = "TCP flags",
1321                 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1322                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1323                                              hdr.tcp_flags)),
1324         },
1325         [ITEM_SCTP] = {
1326                 .name = "sctp",
1327                 .help = "match SCTP header",
1328                 .priv = PRIV_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
1329                 .next = NEXT(item_sctp),
1330                 .call = parse_vc,
1331         },
1332         [ITEM_SCTP_SRC] = {
1333                 .name = "src",
1334                 .help = "SCTP source port",
1335                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1336                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1337                                              hdr.src_port)),
1338         },
1339         [ITEM_SCTP_DST] = {
1340                 .name = "dst",
1341                 .help = "SCTP destination port",
1342                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1343                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1344                                              hdr.dst_port)),
1345         },
1346         [ITEM_SCTP_TAG] = {
1347                 .name = "tag",
1348                 .help = "validation tag",
1349                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1350                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1351                                              hdr.tag)),
1352         },
1353         [ITEM_SCTP_CKSUM] = {
1354                 .name = "cksum",
1355                 .help = "checksum",
1356                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1357                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1358                                              hdr.cksum)),
1359         },
1360         [ITEM_VXLAN] = {
1361                 .name = "vxlan",
1362                 .help = "match VXLAN header",
1363                 .priv = PRIV_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
1364                 .next = NEXT(item_vxlan),
1365                 .call = parse_vc,
1366         },
1367         [ITEM_VXLAN_VNI] = {
1368                 .name = "vni",
1369                 .help = "VXLAN identifier",
1370                 .next = NEXT(item_vxlan, NEXT_ENTRY(UNSIGNED), item_param),
1371                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vxlan, vni)),
1372         },
1373         [ITEM_E_TAG] = {
1374                 .name = "e_tag",
1375                 .help = "match E-Tag header",
1376                 .priv = PRIV_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
1377                 .next = NEXT(item_e_tag),
1378                 .call = parse_vc,
1379         },
1380         [ITEM_E_TAG_GRP_ECID_B] = {
1381                 .name = "grp_ecid_b",
1382                 .help = "GRP and E-CID base",
1383                 .next = NEXT(item_e_tag, NEXT_ENTRY(UNSIGNED), item_param),
1384                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_e_tag,
1385                                                   rsvd_grp_ecid_b,
1386                                                   "\x3f\xff")),
1387         },
1388         [ITEM_NVGRE] = {
1389                 .name = "nvgre",
1390                 .help = "match NVGRE header",
1391                 .priv = PRIV_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
1392                 .next = NEXT(item_nvgre),
1393                 .call = parse_vc,
1394         },
1395         [ITEM_NVGRE_TNI] = {
1396                 .name = "tni",
1397                 .help = "virtual subnet ID",
1398                 .next = NEXT(item_nvgre, NEXT_ENTRY(UNSIGNED), item_param),
1399                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_nvgre, tni)),
1400         },
1401         [ITEM_MPLS] = {
1402                 .name = "mpls",
1403                 .help = "match MPLS header",
1404                 .priv = PRIV_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
1405                 .next = NEXT(item_mpls),
1406                 .call = parse_vc,
1407         },
1408         [ITEM_MPLS_LABEL] = {
1409                 .name = "label",
1410                 .help = "MPLS label",
1411                 .next = NEXT(item_mpls, NEXT_ENTRY(UNSIGNED), item_param),
1412                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_mpls,
1413                                                   label_tc_s,
1414                                                   "\xff\xff\xf0")),
1415         },
1416         [ITEM_GRE] = {
1417                 .name = "gre",
1418                 .help = "match GRE header",
1419                 .priv = PRIV_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
1420                 .next = NEXT(item_gre),
1421                 .call = parse_vc,
1422         },
1423         [ITEM_GRE_PROTO] = {
1424                 .name = "protocol",
1425                 .help = "GRE protocol type",
1426                 .next = NEXT(item_gre, NEXT_ENTRY(UNSIGNED), item_param),
1427                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gre,
1428                                              protocol)),
1429         },
1430         [ITEM_FUZZY] = {
1431                 .name = "fuzzy",
1432                 .help = "fuzzy pattern match, expect faster than default",
1433                 .priv = PRIV_ITEM(FUZZY,
1434                                 sizeof(struct rte_flow_item_fuzzy)),
1435                 .next = NEXT(item_fuzzy),
1436                 .call = parse_vc,
1437         },
1438         [ITEM_FUZZY_THRESH] = {
1439                 .name = "thresh",
1440                 .help = "match accuracy threshold",
1441                 .next = NEXT(item_fuzzy, NEXT_ENTRY(UNSIGNED), item_param),
1442                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_fuzzy,
1443                                         thresh)),
1444         },
1445         [ITEM_GTP] = {
1446                 .name = "gtp",
1447                 .help = "match GTP header",
1448                 .priv = PRIV_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
1449                 .next = NEXT(item_gtp),
1450                 .call = parse_vc,
1451         },
1452         [ITEM_GTP_TEID] = {
1453                 .name = "teid",
1454                 .help = "tunnel endpoint identifier",
1455                 .next = NEXT(item_gtp, NEXT_ENTRY(UNSIGNED), item_param),
1456                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gtp, teid)),
1457         },
1458         [ITEM_GTPC] = {
1459                 .name = "gtpc",
1460                 .help = "match GTP header",
1461                 .priv = PRIV_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
1462                 .next = NEXT(item_gtp),
1463                 .call = parse_vc,
1464         },
1465         [ITEM_GTPU] = {
1466                 .name = "gtpu",
1467                 .help = "match GTP header",
1468                 .priv = PRIV_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
1469                 .next = NEXT(item_gtp),
1470                 .call = parse_vc,
1471         },
1472         [ITEM_GENEVE] = {
1473                 .name = "geneve",
1474                 .help = "match GENEVE header",
1475                 .priv = PRIV_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
1476                 .next = NEXT(item_geneve),
1477                 .call = parse_vc,
1478         },
1479         [ITEM_GENEVE_VNI] = {
1480                 .name = "vni",
1481                 .help = "virtual network identifier",
1482                 .next = NEXT(item_geneve, NEXT_ENTRY(UNSIGNED), item_param),
1483                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_geneve, vni)),
1484         },
1485         [ITEM_GENEVE_PROTO] = {
1486                 .name = "protocol",
1487                 .help = "GENEVE protocol type",
1488                 .next = NEXT(item_geneve, NEXT_ENTRY(UNSIGNED), item_param),
1489                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_geneve,
1490                                              protocol)),
1491         },
1492
1493         /* Validate/create actions. */
1494         [ACTIONS] = {
1495                 .name = "actions",
1496                 .help = "submit a list of associated actions",
1497                 .next = NEXT(next_action),
1498                 .call = parse_vc,
1499         },
1500         [ACTION_NEXT] = {
1501                 .name = "/",
1502                 .help = "specify next action",
1503                 .next = NEXT(next_action),
1504         },
1505         [ACTION_END] = {
1506                 .name = "end",
1507                 .help = "end list of actions",
1508                 .priv = PRIV_ACTION(END, 0),
1509                 .call = parse_vc,
1510         },
1511         [ACTION_VOID] = {
1512                 .name = "void",
1513                 .help = "no-op action",
1514                 .priv = PRIV_ACTION(VOID, 0),
1515                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1516                 .call = parse_vc,
1517         },
1518         [ACTION_PASSTHRU] = {
1519                 .name = "passthru",
1520                 .help = "let subsequent rule process matched packets",
1521                 .priv = PRIV_ACTION(PASSTHRU, 0),
1522                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1523                 .call = parse_vc,
1524         },
1525         [ACTION_MARK] = {
1526                 .name = "mark",
1527                 .help = "attach 32 bit value to packets",
1528                 .priv = PRIV_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
1529                 .next = NEXT(action_mark),
1530                 .call = parse_vc,
1531         },
1532         [ACTION_MARK_ID] = {
1533                 .name = "id",
1534                 .help = "32 bit value to return with packets",
1535                 .next = NEXT(action_mark, NEXT_ENTRY(UNSIGNED)),
1536                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_mark, id)),
1537                 .call = parse_vc_conf,
1538         },
1539         [ACTION_FLAG] = {
1540                 .name = "flag",
1541                 .help = "flag packets",
1542                 .priv = PRIV_ACTION(FLAG, 0),
1543                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1544                 .call = parse_vc,
1545         },
1546         [ACTION_QUEUE] = {
1547                 .name = "queue",
1548                 .help = "assign packets to a given queue index",
1549                 .priv = PRIV_ACTION(QUEUE,
1550                                     sizeof(struct rte_flow_action_queue)),
1551                 .next = NEXT(action_queue),
1552                 .call = parse_vc,
1553         },
1554         [ACTION_QUEUE_INDEX] = {
1555                 .name = "index",
1556                 .help = "queue index to use",
1557                 .next = NEXT(action_queue, NEXT_ENTRY(UNSIGNED)),
1558                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_queue, index)),
1559                 .call = parse_vc_conf,
1560         },
1561         [ACTION_DROP] = {
1562                 .name = "drop",
1563                 .help = "drop packets (note: passthru has priority)",
1564                 .priv = PRIV_ACTION(DROP, 0),
1565                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1566                 .call = parse_vc,
1567         },
1568         [ACTION_COUNT] = {
1569                 .name = "count",
1570                 .help = "enable counters for this rule",
1571                 .priv = PRIV_ACTION(COUNT, 0),
1572                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1573                 .call = parse_vc,
1574         },
1575         [ACTION_DUP] = {
1576                 .name = "dup",
1577                 .help = "duplicate packets to a given queue index",
1578                 .priv = PRIV_ACTION(DUP, sizeof(struct rte_flow_action_dup)),
1579                 .next = NEXT(action_dup),
1580                 .call = parse_vc,
1581         },
1582         [ACTION_DUP_INDEX] = {
1583                 .name = "index",
1584                 .help = "queue index to duplicate packets to",
1585                 .next = NEXT(action_dup, NEXT_ENTRY(UNSIGNED)),
1586                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_dup, index)),
1587                 .call = parse_vc_conf,
1588         },
1589         [ACTION_RSS] = {
1590                 .name = "rss",
1591                 .help = "spread packets among several queues",
1592                 .priv = PRIV_ACTION(RSS, sizeof(union action_rss_data)),
1593                 .next = NEXT(action_rss),
1594                 .call = parse_vc_action_rss,
1595         },
1596         [ACTION_RSS_QUEUES] = {
1597                 .name = "queues",
1598                 .help = "queue indices to use",
1599                 .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_QUEUE)),
1600                 .call = parse_vc_conf,
1601         },
1602         [ACTION_RSS_QUEUE] = {
1603                 .name = "{queue}",
1604                 .help = "queue index",
1605                 .call = parse_vc_action_rss_queue,
1606                 .comp = comp_vc_action_rss_queue,
1607         },
1608         [ACTION_PF] = {
1609                 .name = "pf",
1610                 .help = "redirect packets to physical device function",
1611                 .priv = PRIV_ACTION(PF, 0),
1612                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1613                 .call = parse_vc,
1614         },
1615         [ACTION_VF] = {
1616                 .name = "vf",
1617                 .help = "redirect packets to virtual device function",
1618                 .priv = PRIV_ACTION(VF, sizeof(struct rte_flow_action_vf)),
1619                 .next = NEXT(action_vf),
1620                 .call = parse_vc,
1621         },
1622         [ACTION_VF_ORIGINAL] = {
1623                 .name = "original",
1624                 .help = "use original VF ID if possible",
1625                 .next = NEXT(action_vf, NEXT_ENTRY(BOOLEAN)),
1626                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_vf,
1627                                            original, 1)),
1628                 .call = parse_vc_conf,
1629         },
1630         [ACTION_VF_ID] = {
1631                 .name = "id",
1632                 .help = "VF ID to redirect packets to",
1633                 .next = NEXT(action_vf, NEXT_ENTRY(UNSIGNED)),
1634                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_vf, id)),
1635                 .call = parse_vc_conf,
1636         },
1637         [ACTION_METER] = {
1638                 .name = "meter",
1639                 .help = "meter the directed packets at given id",
1640                 .priv = PRIV_ACTION(METER,
1641                                     sizeof(struct rte_flow_action_meter)),
1642                 .next = NEXT(action_meter),
1643                 .call = parse_vc,
1644         },
1645         [ACTION_METER_ID] = {
1646                 .name = "mtr_id",
1647                 .help = "meter id to use",
1648                 .next = NEXT(action_meter, NEXT_ENTRY(UNSIGNED)),
1649                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_meter, mtr_id)),
1650                 .call = parse_vc_conf,
1651         },
1652 };
1653
1654 /** Remove and return last entry from argument stack. */
1655 static const struct arg *
1656 pop_args(struct context *ctx)
1657 {
1658         return ctx->args_num ? ctx->args[--ctx->args_num] : NULL;
1659 }
1660
1661 /** Add entry on top of the argument stack. */
1662 static int
1663 push_args(struct context *ctx, const struct arg *arg)
1664 {
1665         if (ctx->args_num == CTX_STACK_SIZE)
1666                 return -1;
1667         ctx->args[ctx->args_num++] = arg;
1668         return 0;
1669 }
1670
1671 /** Spread value into buffer according to bit-mask. */
1672 static size_t
1673 arg_entry_bf_fill(void *dst, uintmax_t val, const struct arg *arg)
1674 {
1675         uint32_t i = arg->size;
1676         uint32_t end = 0;
1677         int sub = 1;
1678         int add = 0;
1679         size_t len = 0;
1680
1681         if (!arg->mask)
1682                 return 0;
1683 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1684         if (!arg->hton) {
1685                 i = 0;
1686                 end = arg->size;
1687                 sub = 0;
1688                 add = 1;
1689         }
1690 #endif
1691         while (i != end) {
1692                 unsigned int shift = 0;
1693                 uint8_t *buf = (uint8_t *)dst + arg->offset + (i -= sub);
1694
1695                 for (shift = 0; arg->mask[i] >> shift; ++shift) {
1696                         if (!(arg->mask[i] & (1 << shift)))
1697                                 continue;
1698                         ++len;
1699                         if (!dst)
1700                                 continue;
1701                         *buf &= ~(1 << shift);
1702                         *buf |= (val & 1) << shift;
1703                         val >>= 1;
1704                 }
1705                 i += add;
1706         }
1707         return len;
1708 }
1709
1710 /** Compare a string with a partial one of a given length. */
1711 static int
1712 strcmp_partial(const char *full, const char *partial, size_t partial_len)
1713 {
1714         int r = strncmp(full, partial, partial_len);
1715
1716         if (r)
1717                 return r;
1718         if (strlen(full) <= partial_len)
1719                 return 0;
1720         return full[partial_len];
1721 }
1722
1723 /**
1724  * Parse a prefix length and generate a bit-mask.
1725  *
1726  * Last argument (ctx->args) is retrieved to determine mask size, storage
1727  * location and whether the result must use network byte ordering.
1728  */
1729 static int
1730 parse_prefix(struct context *ctx, const struct token *token,
1731              const char *str, unsigned int len,
1732              void *buf, unsigned int size)
1733 {
1734         const struct arg *arg = pop_args(ctx);
1735         static const uint8_t conv[] = "\x00\x80\xc0\xe0\xf0\xf8\xfc\xfe\xff";
1736         char *end;
1737         uintmax_t u;
1738         unsigned int bytes;
1739         unsigned int extra;
1740
1741         (void)token;
1742         /* Argument is expected. */
1743         if (!arg)
1744                 return -1;
1745         errno = 0;
1746         u = strtoumax(str, &end, 0);
1747         if (errno || (size_t)(end - str) != len)
1748                 goto error;
1749         if (arg->mask) {
1750                 uintmax_t v = 0;
1751
1752                 extra = arg_entry_bf_fill(NULL, 0, arg);
1753                 if (u > extra)
1754                         goto error;
1755                 if (!ctx->object)
1756                         return len;
1757                 extra -= u;
1758                 while (u--)
1759                         (v <<= 1, v |= 1);
1760                 v <<= extra;
1761                 if (!arg_entry_bf_fill(ctx->object, v, arg) ||
1762                     !arg_entry_bf_fill(ctx->objmask, -1, arg))
1763                         goto error;
1764                 return len;
1765         }
1766         bytes = u / 8;
1767         extra = u % 8;
1768         size = arg->size;
1769         if (bytes > size || bytes + !!extra > size)
1770                 goto error;
1771         if (!ctx->object)
1772                 return len;
1773         buf = (uint8_t *)ctx->object + arg->offset;
1774 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1775         if (!arg->hton) {
1776                 memset((uint8_t *)buf + size - bytes, 0xff, bytes);
1777                 memset(buf, 0x00, size - bytes);
1778                 if (extra)
1779                         ((uint8_t *)buf)[size - bytes - 1] = conv[extra];
1780         } else
1781 #endif
1782         {
1783                 memset(buf, 0xff, bytes);
1784                 memset((uint8_t *)buf + bytes, 0x00, size - bytes);
1785                 if (extra)
1786                         ((uint8_t *)buf)[bytes] = conv[extra];
1787         }
1788         if (ctx->objmask)
1789                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
1790         return len;
1791 error:
1792         push_args(ctx, arg);
1793         return -1;
1794 }
1795
1796 /** Default parsing function for token name matching. */
1797 static int
1798 parse_default(struct context *ctx, const struct token *token,
1799               const char *str, unsigned int len,
1800               void *buf, unsigned int size)
1801 {
1802         (void)ctx;
1803         (void)buf;
1804         (void)size;
1805         if (strcmp_partial(token->name, str, len))
1806                 return -1;
1807         return len;
1808 }
1809
1810 /** Parse flow command, initialize output buffer for subsequent tokens. */
1811 static int
1812 parse_init(struct context *ctx, const struct token *token,
1813            const char *str, unsigned int len,
1814            void *buf, unsigned int size)
1815 {
1816         struct buffer *out = buf;
1817
1818         /* Token name must match. */
1819         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1820                 return -1;
1821         /* Nothing else to do if there is no buffer. */
1822         if (!out)
1823                 return len;
1824         /* Make sure buffer is large enough. */
1825         if (size < sizeof(*out))
1826                 return -1;
1827         /* Initialize buffer. */
1828         memset(out, 0x00, sizeof(*out));
1829         memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
1830         ctx->objdata = 0;
1831         ctx->object = out;
1832         ctx->objmask = NULL;
1833         return len;
1834 }
1835
1836 /** Parse tokens for validate/create commands. */
1837 static int
1838 parse_vc(struct context *ctx, const struct token *token,
1839          const char *str, unsigned int len,
1840          void *buf, unsigned int size)
1841 {
1842         struct buffer *out = buf;
1843         uint8_t *data;
1844         uint32_t data_size;
1845
1846         /* Token name must match. */
1847         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1848                 return -1;
1849         /* Nothing else to do if there is no buffer. */
1850         if (!out)
1851                 return len;
1852         if (!out->command) {
1853                 if (ctx->curr != VALIDATE && ctx->curr != CREATE)
1854                         return -1;
1855                 if (sizeof(*out) > size)
1856                         return -1;
1857                 out->command = ctx->curr;
1858                 ctx->objdata = 0;
1859                 ctx->object = out;
1860                 ctx->objmask = NULL;
1861                 out->args.vc.data = (uint8_t *)out + size;
1862                 return len;
1863         }
1864         ctx->objdata = 0;
1865         ctx->object = &out->args.vc.attr;
1866         ctx->objmask = NULL;
1867         switch (ctx->curr) {
1868         case GROUP:
1869         case PRIORITY:
1870                 return len;
1871         case INGRESS:
1872                 out->args.vc.attr.ingress = 1;
1873                 return len;
1874         case EGRESS:
1875                 out->args.vc.attr.egress = 1;
1876                 return len;
1877         case PATTERN:
1878                 out->args.vc.pattern =
1879                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
1880                                                sizeof(double));
1881                 ctx->object = out->args.vc.pattern;
1882                 ctx->objmask = NULL;
1883                 return len;
1884         case ACTIONS:
1885                 out->args.vc.actions =
1886                         (void *)RTE_ALIGN_CEIL((uintptr_t)
1887                                                (out->args.vc.pattern +
1888                                                 out->args.vc.pattern_n),
1889                                                sizeof(double));
1890                 ctx->object = out->args.vc.actions;
1891                 ctx->objmask = NULL;
1892                 return len;
1893         default:
1894                 if (!token->priv)
1895                         return -1;
1896                 break;
1897         }
1898         if (!out->args.vc.actions) {
1899                 const struct parse_item_priv *priv = token->priv;
1900                 struct rte_flow_item *item =
1901                         out->args.vc.pattern + out->args.vc.pattern_n;
1902
1903                 data_size = priv->size * 3; /* spec, last, mask */
1904                 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
1905                                                (out->args.vc.data - data_size),
1906                                                sizeof(double));
1907                 if ((uint8_t *)item + sizeof(*item) > data)
1908                         return -1;
1909                 *item = (struct rte_flow_item){
1910                         .type = priv->type,
1911                 };
1912                 ++out->args.vc.pattern_n;
1913                 ctx->object = item;
1914                 ctx->objmask = NULL;
1915         } else {
1916                 const struct parse_action_priv *priv = token->priv;
1917                 struct rte_flow_action *action =
1918                         out->args.vc.actions + out->args.vc.actions_n;
1919
1920                 data_size = priv->size; /* configuration */
1921                 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
1922                                                (out->args.vc.data - data_size),
1923                                                sizeof(double));
1924                 if ((uint8_t *)action + sizeof(*action) > data)
1925                         return -1;
1926                 *action = (struct rte_flow_action){
1927                         .type = priv->type,
1928                         .conf = data_size ? data : NULL,
1929                 };
1930                 ++out->args.vc.actions_n;
1931                 ctx->object = action;
1932                 ctx->objmask = NULL;
1933         }
1934         memset(data, 0, data_size);
1935         out->args.vc.data = data;
1936         ctx->objdata = data_size;
1937         return len;
1938 }
1939
1940 /** Parse pattern item parameter type. */
1941 static int
1942 parse_vc_spec(struct context *ctx, const struct token *token,
1943               const char *str, unsigned int len,
1944               void *buf, unsigned int size)
1945 {
1946         struct buffer *out = buf;
1947         struct rte_flow_item *item;
1948         uint32_t data_size;
1949         int index;
1950         int objmask = 0;
1951
1952         (void)size;
1953         /* Token name must match. */
1954         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1955                 return -1;
1956         /* Parse parameter types. */
1957         switch (ctx->curr) {
1958                 static const enum index prefix[] = NEXT_ENTRY(PREFIX);
1959
1960         case ITEM_PARAM_IS:
1961                 index = 0;
1962                 objmask = 1;
1963                 break;
1964         case ITEM_PARAM_SPEC:
1965                 index = 0;
1966                 break;
1967         case ITEM_PARAM_LAST:
1968                 index = 1;
1969                 break;
1970         case ITEM_PARAM_PREFIX:
1971                 /* Modify next token to expect a prefix. */
1972                 if (ctx->next_num < 2)
1973                         return -1;
1974                 ctx->next[ctx->next_num - 2] = prefix;
1975                 /* Fall through. */
1976         case ITEM_PARAM_MASK:
1977                 index = 2;
1978                 break;
1979         default:
1980                 return -1;
1981         }
1982         /* Nothing else to do if there is no buffer. */
1983         if (!out)
1984                 return len;
1985         if (!out->args.vc.pattern_n)
1986                 return -1;
1987         item = &out->args.vc.pattern[out->args.vc.pattern_n - 1];
1988         data_size = ctx->objdata / 3; /* spec, last, mask */
1989         /* Point to selected object. */
1990         ctx->object = out->args.vc.data + (data_size * index);
1991         if (objmask) {
1992                 ctx->objmask = out->args.vc.data + (data_size * 2); /* mask */
1993                 item->mask = ctx->objmask;
1994         } else
1995                 ctx->objmask = NULL;
1996         /* Update relevant item pointer. */
1997         *((const void **[]){ &item->spec, &item->last, &item->mask })[index] =
1998                 ctx->object;
1999         return len;
2000 }
2001
2002 /** Parse action configuration field. */
2003 static int
2004 parse_vc_conf(struct context *ctx, const struct token *token,
2005               const char *str, unsigned int len,
2006               void *buf, unsigned int size)
2007 {
2008         struct buffer *out = buf;
2009
2010         (void)size;
2011         /* Token name must match. */
2012         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2013                 return -1;
2014         /* Nothing else to do if there is no buffer. */
2015         if (!out)
2016                 return len;
2017         /* Point to selected object. */
2018         ctx->object = out->args.vc.data;
2019         ctx->objmask = NULL;
2020         return len;
2021 }
2022
2023 /** Parse RSS action. */
2024 static int
2025 parse_vc_action_rss(struct context *ctx, const struct token *token,
2026                     const char *str, unsigned int len,
2027                     void *buf, unsigned int size)
2028 {
2029         struct buffer *out = buf;
2030         struct rte_flow_action *action;
2031         union action_rss_data *action_rss_data;
2032         unsigned int i;
2033         int ret;
2034
2035         ret = parse_vc(ctx, token, str, len, buf, size);
2036         if (ret < 0)
2037                 return ret;
2038         /* Nothing else to do if there is no buffer. */
2039         if (!out)
2040                 return ret;
2041         if (!out->args.vc.actions_n)
2042                 return -1;
2043         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
2044         /* Point to selected object. */
2045         ctx->object = out->args.vc.data;
2046         ctx->objmask = NULL;
2047         /* Set up default configuration. */
2048         action_rss_data = ctx->object;
2049         *action_rss_data = (union action_rss_data){
2050                 .conf = (struct rte_flow_action_rss){
2051                         .rss_conf = &action_rss_data->s.rss_conf,
2052                         .num = RTE_MIN(nb_rxq, ACTION_RSS_QUEUE_NUM),
2053                 },
2054         };
2055         action_rss_data->s.rss_conf = (struct rte_eth_rss_conf){
2056                 .rss_key = action_rss_data->s.rss_key,
2057                 .rss_key_len = sizeof(action_rss_data->s.rss_key),
2058                 .rss_hf = rss_hf,
2059         };
2060         strncpy((void *)action_rss_data->s.rss_key,
2061                 "testpmd's default RSS hash key",
2062                 sizeof(action_rss_data->s.rss_key));
2063         for (i = 0; i < action_rss_data->conf.num; ++i)
2064                 action_rss_data->conf.queue[i] = i;
2065         if (!port_id_is_invalid(ctx->port, DISABLED_WARN) &&
2066             ctx->port != (portid_t)RTE_PORT_ALL) {
2067                 struct rte_eth_dev_info info;
2068
2069                 rte_eth_dev_info_get(ctx->port, &info);
2070                 action_rss_data->s.rss_conf.rss_key_len =
2071                         RTE_MIN(sizeof(action_rss_data->s.rss_key),
2072                                 info.hash_key_size);
2073         }
2074         action->conf = &action_rss_data->conf;
2075         return ret;
2076 }
2077
2078 /**
2079  * Parse queue field for RSS action.
2080  *
2081  * Valid tokens are queue indices and the "end" token.
2082  */
2083 static int
2084 parse_vc_action_rss_queue(struct context *ctx, const struct token *token,
2085                           const char *str, unsigned int len,
2086                           void *buf, unsigned int size)
2087 {
2088         static const enum index next[] = NEXT_ENTRY(ACTION_RSS_QUEUE);
2089         union action_rss_data *action_rss_data;
2090         int ret;
2091         int i;
2092
2093         (void)token;
2094         (void)buf;
2095         (void)size;
2096         if (ctx->curr != ACTION_RSS_QUEUE)
2097                 return -1;
2098         i = ctx->objdata >> 16;
2099         if (!strcmp_partial("end", str, len)) {
2100                 ctx->objdata &= 0xffff;
2101                 return len;
2102         }
2103         if (i >= ACTION_RSS_QUEUE_NUM)
2104                 return -1;
2105         if (push_args(ctx,
2106                       ARGS_ENTRY_ARB(offsetof(struct rte_flow_action_rss,
2107                                               queue) +
2108                                      i * sizeof(action_rss_data->s.queue[i]),
2109                                      sizeof(action_rss_data->s.queue[i]))))
2110                 return -1;
2111         ret = parse_int(ctx, token, str, len, NULL, 0);
2112         if (ret < 0) {
2113                 pop_args(ctx);
2114                 return -1;
2115         }
2116         ++i;
2117         ctx->objdata = i << 16 | (ctx->objdata & 0xffff);
2118         /* Repeat token. */
2119         if (ctx->next_num == RTE_DIM(ctx->next))
2120                 return -1;
2121         ctx->next[ctx->next_num++] = next;
2122         if (!ctx->object)
2123                 return len;
2124         action_rss_data = ctx->object;
2125         action_rss_data->conf.num = i;
2126         return len;
2127 }
2128
2129 /** Parse tokens for destroy command. */
2130 static int
2131 parse_destroy(struct context *ctx, const struct token *token,
2132               const char *str, unsigned int len,
2133               void *buf, unsigned int size)
2134 {
2135         struct buffer *out = buf;
2136
2137         /* Token name must match. */
2138         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2139                 return -1;
2140         /* Nothing else to do if there is no buffer. */
2141         if (!out)
2142                 return len;
2143         if (!out->command) {
2144                 if (ctx->curr != DESTROY)
2145                         return -1;
2146                 if (sizeof(*out) > size)
2147                         return -1;
2148                 out->command = ctx->curr;
2149                 ctx->objdata = 0;
2150                 ctx->object = out;
2151                 ctx->objmask = NULL;
2152                 out->args.destroy.rule =
2153                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
2154                                                sizeof(double));
2155                 return len;
2156         }
2157         if (((uint8_t *)(out->args.destroy.rule + out->args.destroy.rule_n) +
2158              sizeof(*out->args.destroy.rule)) > (uint8_t *)out + size)
2159                 return -1;
2160         ctx->objdata = 0;
2161         ctx->object = out->args.destroy.rule + out->args.destroy.rule_n++;
2162         ctx->objmask = NULL;
2163         return len;
2164 }
2165
2166 /** Parse tokens for flush command. */
2167 static int
2168 parse_flush(struct context *ctx, const struct token *token,
2169             const char *str, unsigned int len,
2170             void *buf, unsigned int size)
2171 {
2172         struct buffer *out = buf;
2173
2174         /* Token name must match. */
2175         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2176                 return -1;
2177         /* Nothing else to do if there is no buffer. */
2178         if (!out)
2179                 return len;
2180         if (!out->command) {
2181                 if (ctx->curr != FLUSH)
2182                         return -1;
2183                 if (sizeof(*out) > size)
2184                         return -1;
2185                 out->command = ctx->curr;
2186                 ctx->objdata = 0;
2187                 ctx->object = out;
2188                 ctx->objmask = NULL;
2189         }
2190         return len;
2191 }
2192
2193 /** Parse tokens for query command. */
2194 static int
2195 parse_query(struct context *ctx, const struct token *token,
2196             const char *str, unsigned int len,
2197             void *buf, unsigned int size)
2198 {
2199         struct buffer *out = buf;
2200
2201         /* Token name must match. */
2202         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2203                 return -1;
2204         /* Nothing else to do if there is no buffer. */
2205         if (!out)
2206                 return len;
2207         if (!out->command) {
2208                 if (ctx->curr != QUERY)
2209                         return -1;
2210                 if (sizeof(*out) > size)
2211                         return -1;
2212                 out->command = ctx->curr;
2213                 ctx->objdata = 0;
2214                 ctx->object = out;
2215                 ctx->objmask = NULL;
2216         }
2217         return len;
2218 }
2219
2220 /** Parse action names. */
2221 static int
2222 parse_action(struct context *ctx, const struct token *token,
2223              const char *str, unsigned int len,
2224              void *buf, unsigned int size)
2225 {
2226         struct buffer *out = buf;
2227         const struct arg *arg = pop_args(ctx);
2228         unsigned int i;
2229
2230         (void)size;
2231         /* Argument is expected. */
2232         if (!arg)
2233                 return -1;
2234         /* Parse action name. */
2235         for (i = 0; next_action[i]; ++i) {
2236                 const struct parse_action_priv *priv;
2237
2238                 token = &token_list[next_action[i]];
2239                 if (strcmp_partial(token->name, str, len))
2240                         continue;
2241                 priv = token->priv;
2242                 if (!priv)
2243                         goto error;
2244                 if (out)
2245                         memcpy((uint8_t *)ctx->object + arg->offset,
2246                                &priv->type,
2247                                arg->size);
2248                 return len;
2249         }
2250 error:
2251         push_args(ctx, arg);
2252         return -1;
2253 }
2254
2255 /** Parse tokens for list command. */
2256 static int
2257 parse_list(struct context *ctx, const struct token *token,
2258            const char *str, unsigned int len,
2259            void *buf, unsigned int size)
2260 {
2261         struct buffer *out = buf;
2262
2263         /* Token name must match. */
2264         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2265                 return -1;
2266         /* Nothing else to do if there is no buffer. */
2267         if (!out)
2268                 return len;
2269         if (!out->command) {
2270                 if (ctx->curr != LIST)
2271                         return -1;
2272                 if (sizeof(*out) > size)
2273                         return -1;
2274                 out->command = ctx->curr;
2275                 ctx->objdata = 0;
2276                 ctx->object = out;
2277                 ctx->objmask = NULL;
2278                 out->args.list.group =
2279                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
2280                                                sizeof(double));
2281                 return len;
2282         }
2283         if (((uint8_t *)(out->args.list.group + out->args.list.group_n) +
2284              sizeof(*out->args.list.group)) > (uint8_t *)out + size)
2285                 return -1;
2286         ctx->objdata = 0;
2287         ctx->object = out->args.list.group + out->args.list.group_n++;
2288         ctx->objmask = NULL;
2289         return len;
2290 }
2291
2292 /** Parse tokens for isolate command. */
2293 static int
2294 parse_isolate(struct context *ctx, const struct token *token,
2295               const char *str, unsigned int len,
2296               void *buf, unsigned int size)
2297 {
2298         struct buffer *out = buf;
2299
2300         /* Token name must match. */
2301         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2302                 return -1;
2303         /* Nothing else to do if there is no buffer. */
2304         if (!out)
2305                 return len;
2306         if (!out->command) {
2307                 if (ctx->curr != ISOLATE)
2308                         return -1;
2309                 if (sizeof(*out) > size)
2310                         return -1;
2311                 out->command = ctx->curr;
2312                 ctx->objdata = 0;
2313                 ctx->object = out;
2314                 ctx->objmask = NULL;
2315         }
2316         return len;
2317 }
2318
2319 /**
2320  * Parse signed/unsigned integers 8 to 64-bit long.
2321  *
2322  * Last argument (ctx->args) is retrieved to determine integer type and
2323  * storage location.
2324  */
2325 static int
2326 parse_int(struct context *ctx, const struct token *token,
2327           const char *str, unsigned int len,
2328           void *buf, unsigned int size)
2329 {
2330         const struct arg *arg = pop_args(ctx);
2331         uintmax_t u;
2332         char *end;
2333
2334         (void)token;
2335         /* Argument is expected. */
2336         if (!arg)
2337                 return -1;
2338         errno = 0;
2339         u = arg->sign ?
2340                 (uintmax_t)strtoimax(str, &end, 0) :
2341                 strtoumax(str, &end, 0);
2342         if (errno || (size_t)(end - str) != len)
2343                 goto error;
2344         if (!ctx->object)
2345                 return len;
2346         if (arg->mask) {
2347                 if (!arg_entry_bf_fill(ctx->object, u, arg) ||
2348                     !arg_entry_bf_fill(ctx->objmask, -1, arg))
2349                         goto error;
2350                 return len;
2351         }
2352         buf = (uint8_t *)ctx->object + arg->offset;
2353         size = arg->size;
2354 objmask:
2355         switch (size) {
2356         case sizeof(uint8_t):
2357                 *(uint8_t *)buf = u;
2358                 break;
2359         case sizeof(uint16_t):
2360                 *(uint16_t *)buf = arg->hton ? rte_cpu_to_be_16(u) : u;
2361                 break;
2362         case sizeof(uint8_t [3]):
2363 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
2364                 if (!arg->hton) {
2365                         ((uint8_t *)buf)[0] = u;
2366                         ((uint8_t *)buf)[1] = u >> 8;
2367                         ((uint8_t *)buf)[2] = u >> 16;
2368                         break;
2369                 }
2370 #endif
2371                 ((uint8_t *)buf)[0] = u >> 16;
2372                 ((uint8_t *)buf)[1] = u >> 8;
2373                 ((uint8_t *)buf)[2] = u;
2374                 break;
2375         case sizeof(uint32_t):
2376                 *(uint32_t *)buf = arg->hton ? rte_cpu_to_be_32(u) : u;
2377                 break;
2378         case sizeof(uint64_t):
2379                 *(uint64_t *)buf = arg->hton ? rte_cpu_to_be_64(u) : u;
2380                 break;
2381         default:
2382                 goto error;
2383         }
2384         if (ctx->objmask && buf != (uint8_t *)ctx->objmask + arg->offset) {
2385                 u = -1;
2386                 buf = (uint8_t *)ctx->objmask + arg->offset;
2387                 goto objmask;
2388         }
2389         return len;
2390 error:
2391         push_args(ctx, arg);
2392         return -1;
2393 }
2394
2395 /**
2396  * Parse a string.
2397  *
2398  * Two arguments (ctx->args) are retrieved from the stack to store data and
2399  * its length (in that order).
2400  */
2401 static int
2402 parse_string(struct context *ctx, const struct token *token,
2403              const char *str, unsigned int len,
2404              void *buf, unsigned int size)
2405 {
2406         const struct arg *arg_data = pop_args(ctx);
2407         const struct arg *arg_len = pop_args(ctx);
2408         char tmp[16]; /* Ought to be enough. */
2409         int ret;
2410
2411         /* Arguments are expected. */
2412         if (!arg_data)
2413                 return -1;
2414         if (!arg_len) {
2415                 push_args(ctx, arg_data);
2416                 return -1;
2417         }
2418         size = arg_data->size;
2419         /* Bit-mask fill is not supported. */
2420         if (arg_data->mask || size < len)
2421                 goto error;
2422         if (!ctx->object)
2423                 return len;
2424         /* Let parse_int() fill length information first. */
2425         ret = snprintf(tmp, sizeof(tmp), "%u", len);
2426         if (ret < 0)
2427                 goto error;
2428         push_args(ctx, arg_len);
2429         ret = parse_int(ctx, token, tmp, ret, NULL, 0);
2430         if (ret < 0) {
2431                 pop_args(ctx);
2432                 goto error;
2433         }
2434         buf = (uint8_t *)ctx->object + arg_data->offset;
2435         /* Output buffer is not necessarily NUL-terminated. */
2436         memcpy(buf, str, len);
2437         memset((uint8_t *)buf + len, 0x55, size - len);
2438         if (ctx->objmask)
2439                 memset((uint8_t *)ctx->objmask + arg_data->offset, 0xff, len);
2440         return len;
2441 error:
2442         push_args(ctx, arg_len);
2443         push_args(ctx, arg_data);
2444         return -1;
2445 }
2446
2447 /**
2448  * Parse a MAC address.
2449  *
2450  * Last argument (ctx->args) is retrieved to determine storage size and
2451  * location.
2452  */
2453 static int
2454 parse_mac_addr(struct context *ctx, const struct token *token,
2455                const char *str, unsigned int len,
2456                void *buf, unsigned int size)
2457 {
2458         const struct arg *arg = pop_args(ctx);
2459         struct ether_addr tmp;
2460         int ret;
2461
2462         (void)token;
2463         /* Argument is expected. */
2464         if (!arg)
2465                 return -1;
2466         size = arg->size;
2467         /* Bit-mask fill is not supported. */
2468         if (arg->mask || size != sizeof(tmp))
2469                 goto error;
2470         /* Only network endian is supported. */
2471         if (!arg->hton)
2472                 goto error;
2473         ret = cmdline_parse_etheraddr(NULL, str, &tmp, size);
2474         if (ret < 0 || (unsigned int)ret != len)
2475                 goto error;
2476         if (!ctx->object)
2477                 return len;
2478         buf = (uint8_t *)ctx->object + arg->offset;
2479         memcpy(buf, &tmp, size);
2480         if (ctx->objmask)
2481                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2482         return len;
2483 error:
2484         push_args(ctx, arg);
2485         return -1;
2486 }
2487
2488 /**
2489  * Parse an IPv4 address.
2490  *
2491  * Last argument (ctx->args) is retrieved to determine storage size and
2492  * location.
2493  */
2494 static int
2495 parse_ipv4_addr(struct context *ctx, const struct token *token,
2496                 const char *str, unsigned int len,
2497                 void *buf, unsigned int size)
2498 {
2499         const struct arg *arg = pop_args(ctx);
2500         char str2[len + 1];
2501         struct in_addr tmp;
2502         int ret;
2503
2504         /* Argument is expected. */
2505         if (!arg)
2506                 return -1;
2507         size = arg->size;
2508         /* Bit-mask fill is not supported. */
2509         if (arg->mask || size != sizeof(tmp))
2510                 goto error;
2511         /* Only network endian is supported. */
2512         if (!arg->hton)
2513                 goto error;
2514         memcpy(str2, str, len);
2515         str2[len] = '\0';
2516         ret = inet_pton(AF_INET, str2, &tmp);
2517         if (ret != 1) {
2518                 /* Attempt integer parsing. */
2519                 push_args(ctx, arg);
2520                 return parse_int(ctx, token, str, len, buf, size);
2521         }
2522         if (!ctx->object)
2523                 return len;
2524         buf = (uint8_t *)ctx->object + arg->offset;
2525         memcpy(buf, &tmp, size);
2526         if (ctx->objmask)
2527                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2528         return len;
2529 error:
2530         push_args(ctx, arg);
2531         return -1;
2532 }
2533
2534 /**
2535  * Parse an IPv6 address.
2536  *
2537  * Last argument (ctx->args) is retrieved to determine storage size and
2538  * location.
2539  */
2540 static int
2541 parse_ipv6_addr(struct context *ctx, const struct token *token,
2542                 const char *str, unsigned int len,
2543                 void *buf, unsigned int size)
2544 {
2545         const struct arg *arg = pop_args(ctx);
2546         char str2[len + 1];
2547         struct in6_addr tmp;
2548         int ret;
2549
2550         (void)token;
2551         /* Argument is expected. */
2552         if (!arg)
2553                 return -1;
2554         size = arg->size;
2555         /* Bit-mask fill is not supported. */
2556         if (arg->mask || size != sizeof(tmp))
2557                 goto error;
2558         /* Only network endian is supported. */
2559         if (!arg->hton)
2560                 goto error;
2561         memcpy(str2, str, len);
2562         str2[len] = '\0';
2563         ret = inet_pton(AF_INET6, str2, &tmp);
2564         if (ret != 1)
2565                 goto error;
2566         if (!ctx->object)
2567                 return len;
2568         buf = (uint8_t *)ctx->object + arg->offset;
2569         memcpy(buf, &tmp, size);
2570         if (ctx->objmask)
2571                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2572         return len;
2573 error:
2574         push_args(ctx, arg);
2575         return -1;
2576 }
2577
2578 /** Boolean values (even indices stand for false). */
2579 static const char *const boolean_name[] = {
2580         "0", "1",
2581         "false", "true",
2582         "no", "yes",
2583         "N", "Y",
2584         NULL,
2585 };
2586
2587 /**
2588  * Parse a boolean value.
2589  *
2590  * Last argument (ctx->args) is retrieved to determine storage size and
2591  * location.
2592  */
2593 static int
2594 parse_boolean(struct context *ctx, const struct token *token,
2595               const char *str, unsigned int len,
2596               void *buf, unsigned int size)
2597 {
2598         const struct arg *arg = pop_args(ctx);
2599         unsigned int i;
2600         int ret;
2601
2602         /* Argument is expected. */
2603         if (!arg)
2604                 return -1;
2605         for (i = 0; boolean_name[i]; ++i)
2606                 if (!strcmp_partial(boolean_name[i], str, len))
2607                         break;
2608         /* Process token as integer. */
2609         if (boolean_name[i])
2610                 str = i & 1 ? "1" : "0";
2611         push_args(ctx, arg);
2612         ret = parse_int(ctx, token, str, strlen(str), buf, size);
2613         return ret > 0 ? (int)len : ret;
2614 }
2615
2616 /** Parse port and update context. */
2617 static int
2618 parse_port(struct context *ctx, const struct token *token,
2619            const char *str, unsigned int len,
2620            void *buf, unsigned int size)
2621 {
2622         struct buffer *out = &(struct buffer){ .port = 0 };
2623         int ret;
2624
2625         if (buf)
2626                 out = buf;
2627         else {
2628                 ctx->objdata = 0;
2629                 ctx->object = out;
2630                 ctx->objmask = NULL;
2631                 size = sizeof(*out);
2632         }
2633         ret = parse_int(ctx, token, str, len, out, size);
2634         if (ret >= 0)
2635                 ctx->port = out->port;
2636         if (!buf)
2637                 ctx->object = NULL;
2638         return ret;
2639 }
2640
2641 /** No completion. */
2642 static int
2643 comp_none(struct context *ctx, const struct token *token,
2644           unsigned int ent, char *buf, unsigned int size)
2645 {
2646         (void)ctx;
2647         (void)token;
2648         (void)ent;
2649         (void)buf;
2650         (void)size;
2651         return 0;
2652 }
2653
2654 /** Complete boolean values. */
2655 static int
2656 comp_boolean(struct context *ctx, const struct token *token,
2657              unsigned int ent, char *buf, unsigned int size)
2658 {
2659         unsigned int i;
2660
2661         (void)ctx;
2662         (void)token;
2663         for (i = 0; boolean_name[i]; ++i)
2664                 if (buf && i == ent)
2665                         return snprintf(buf, size, "%s", boolean_name[i]);
2666         if (buf)
2667                 return -1;
2668         return i;
2669 }
2670
2671 /** Complete action names. */
2672 static int
2673 comp_action(struct context *ctx, const struct token *token,
2674             unsigned int ent, char *buf, unsigned int size)
2675 {
2676         unsigned int i;
2677
2678         (void)ctx;
2679         (void)token;
2680         for (i = 0; next_action[i]; ++i)
2681                 if (buf && i == ent)
2682                         return snprintf(buf, size, "%s",
2683                                         token_list[next_action[i]].name);
2684         if (buf)
2685                 return -1;
2686         return i;
2687 }
2688
2689 /** Complete available ports. */
2690 static int
2691 comp_port(struct context *ctx, const struct token *token,
2692           unsigned int ent, char *buf, unsigned int size)
2693 {
2694         unsigned int i = 0;
2695         portid_t p;
2696
2697         (void)ctx;
2698         (void)token;
2699         RTE_ETH_FOREACH_DEV(p) {
2700                 if (buf && i == ent)
2701                         return snprintf(buf, size, "%u", p);
2702                 ++i;
2703         }
2704         if (buf)
2705                 return -1;
2706         return i;
2707 }
2708
2709 /** Complete available rule IDs. */
2710 static int
2711 comp_rule_id(struct context *ctx, const struct token *token,
2712              unsigned int ent, char *buf, unsigned int size)
2713 {
2714         unsigned int i = 0;
2715         struct rte_port *port;
2716         struct port_flow *pf;
2717
2718         (void)token;
2719         if (port_id_is_invalid(ctx->port, DISABLED_WARN) ||
2720             ctx->port == (portid_t)RTE_PORT_ALL)
2721                 return -1;
2722         port = &ports[ctx->port];
2723         for (pf = port->flow_list; pf != NULL; pf = pf->next) {
2724                 if (buf && i == ent)
2725                         return snprintf(buf, size, "%u", pf->id);
2726                 ++i;
2727         }
2728         if (buf)
2729                 return -1;
2730         return i;
2731 }
2732
2733 /** Complete queue field for RSS action. */
2734 static int
2735 comp_vc_action_rss_queue(struct context *ctx, const struct token *token,
2736                          unsigned int ent, char *buf, unsigned int size)
2737 {
2738         (void)ctx;
2739         (void)token;
2740         if (!buf)
2741                 return nb_rxq + 1;
2742         if (ent < nb_rxq)
2743                 return snprintf(buf, size, "%u", ent);
2744         if (ent == nb_rxq)
2745                 return snprintf(buf, size, "end");
2746         return -1;
2747 }
2748
2749 /** Internal context. */
2750 static struct context cmd_flow_context;
2751
2752 /** Global parser instance (cmdline API). */
2753 cmdline_parse_inst_t cmd_flow;
2754
2755 /** Initialize context. */
2756 static void
2757 cmd_flow_context_init(struct context *ctx)
2758 {
2759         /* A full memset() is not necessary. */
2760         ctx->curr = ZERO;
2761         ctx->prev = ZERO;
2762         ctx->next_num = 0;
2763         ctx->args_num = 0;
2764         ctx->eol = 0;
2765         ctx->last = 0;
2766         ctx->port = 0;
2767         ctx->objdata = 0;
2768         ctx->object = NULL;
2769         ctx->objmask = NULL;
2770 }
2771
2772 /** Parse a token (cmdline API). */
2773 static int
2774 cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result,
2775                unsigned int size)
2776 {
2777         struct context *ctx = &cmd_flow_context;
2778         const struct token *token;
2779         const enum index *list;
2780         int len;
2781         int i;
2782
2783         (void)hdr;
2784         token = &token_list[ctx->curr];
2785         /* Check argument length. */
2786         ctx->eol = 0;
2787         ctx->last = 1;
2788         for (len = 0; src[len]; ++len)
2789                 if (src[len] == '#' || isspace(src[len]))
2790                         break;
2791         if (!len)
2792                 return -1;
2793         /* Last argument and EOL detection. */
2794         for (i = len; src[i]; ++i)
2795                 if (src[i] == '#' || src[i] == '\r' || src[i] == '\n')
2796                         break;
2797                 else if (!isspace(src[i])) {
2798                         ctx->last = 0;
2799                         break;
2800                 }
2801         for (; src[i]; ++i)
2802                 if (src[i] == '\r' || src[i] == '\n') {
2803                         ctx->eol = 1;
2804                         break;
2805                 }
2806         /* Initialize context if necessary. */
2807         if (!ctx->next_num) {
2808                 if (!token->next)
2809                         return 0;
2810                 ctx->next[ctx->next_num++] = token->next[0];
2811         }
2812         /* Process argument through candidates. */
2813         ctx->prev = ctx->curr;
2814         list = ctx->next[ctx->next_num - 1];
2815         for (i = 0; list[i]; ++i) {
2816                 const struct token *next = &token_list[list[i]];
2817                 int tmp;
2818
2819                 ctx->curr = list[i];
2820                 if (next->call)
2821                         tmp = next->call(ctx, next, src, len, result, size);
2822                 else
2823                         tmp = parse_default(ctx, next, src, len, result, size);
2824                 if (tmp == -1 || tmp != len)
2825                         continue;
2826                 token = next;
2827                 break;
2828         }
2829         if (!list[i])
2830                 return -1;
2831         --ctx->next_num;
2832         /* Push subsequent tokens if any. */
2833         if (token->next)
2834                 for (i = 0; token->next[i]; ++i) {
2835                         if (ctx->next_num == RTE_DIM(ctx->next))
2836                                 return -1;
2837                         ctx->next[ctx->next_num++] = token->next[i];
2838                 }
2839         /* Push arguments if any. */
2840         if (token->args)
2841                 for (i = 0; token->args[i]; ++i) {
2842                         if (ctx->args_num == RTE_DIM(ctx->args))
2843                                 return -1;
2844                         ctx->args[ctx->args_num++] = token->args[i];
2845                 }
2846         return len;
2847 }
2848
2849 /** Return number of completion entries (cmdline API). */
2850 static int
2851 cmd_flow_complete_get_nb(cmdline_parse_token_hdr_t *hdr)
2852 {
2853         struct context *ctx = &cmd_flow_context;
2854         const struct token *token = &token_list[ctx->curr];
2855         const enum index *list;
2856         int i;
2857
2858         (void)hdr;
2859         /* Count number of tokens in current list. */
2860         if (ctx->next_num)
2861                 list = ctx->next[ctx->next_num - 1];
2862         else
2863                 list = token->next[0];
2864         for (i = 0; list[i]; ++i)
2865                 ;
2866         if (!i)
2867                 return 0;
2868         /*
2869          * If there is a single token, use its completion callback, otherwise
2870          * return the number of entries.
2871          */
2872         token = &token_list[list[0]];
2873         if (i == 1 && token->comp) {
2874                 /* Save index for cmd_flow_get_help(). */
2875                 ctx->prev = list[0];
2876                 return token->comp(ctx, token, 0, NULL, 0);
2877         }
2878         return i;
2879 }
2880
2881 /** Return a completion entry (cmdline API). */
2882 static int
2883 cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,
2884                           char *dst, unsigned int size)
2885 {
2886         struct context *ctx = &cmd_flow_context;
2887         const struct token *token = &token_list[ctx->curr];
2888         const enum index *list;
2889         int i;
2890
2891         (void)hdr;
2892         /* Count number of tokens in current list. */
2893         if (ctx->next_num)
2894                 list = ctx->next[ctx->next_num - 1];
2895         else
2896                 list = token->next[0];
2897         for (i = 0; list[i]; ++i)
2898                 ;
2899         if (!i)
2900                 return -1;
2901         /* If there is a single token, use its completion callback. */
2902         token = &token_list[list[0]];
2903         if (i == 1 && token->comp) {
2904                 /* Save index for cmd_flow_get_help(). */
2905                 ctx->prev = list[0];
2906                 return token->comp(ctx, token, index, dst, size) < 0 ? -1 : 0;
2907         }
2908         /* Otherwise make sure the index is valid and use defaults. */
2909         if (index >= i)
2910                 return -1;
2911         token = &token_list[list[index]];
2912         snprintf(dst, size, "%s", token->name);
2913         /* Save index for cmd_flow_get_help(). */
2914         ctx->prev = list[index];
2915         return 0;
2916 }
2917
2918 /** Populate help strings for current token (cmdline API). */
2919 static int
2920 cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
2921 {
2922         struct context *ctx = &cmd_flow_context;
2923         const struct token *token = &token_list[ctx->prev];
2924
2925         (void)hdr;
2926         if (!size)
2927                 return -1;
2928         /* Set token type and update global help with details. */
2929         snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
2930         if (token->help)
2931                 cmd_flow.help_str = token->help;
2932         else
2933                 cmd_flow.help_str = token->name;
2934         return 0;
2935 }
2936
2937 /** Token definition template (cmdline API). */
2938 static struct cmdline_token_hdr cmd_flow_token_hdr = {
2939         .ops = &(struct cmdline_token_ops){
2940                 .parse = cmd_flow_parse,
2941                 .complete_get_nb = cmd_flow_complete_get_nb,
2942                 .complete_get_elt = cmd_flow_complete_get_elt,
2943                 .get_help = cmd_flow_get_help,
2944         },
2945         .offset = 0,
2946 };
2947
2948 /** Populate the next dynamic token. */
2949 static void
2950 cmd_flow_tok(cmdline_parse_token_hdr_t **hdr,
2951              cmdline_parse_token_hdr_t **hdr_inst)
2952 {
2953         struct context *ctx = &cmd_flow_context;
2954
2955         /* Always reinitialize context before requesting the first token. */
2956         if (!(hdr_inst - cmd_flow.tokens))
2957                 cmd_flow_context_init(ctx);
2958         /* Return NULL when no more tokens are expected. */
2959         if (!ctx->next_num && ctx->curr) {
2960                 *hdr = NULL;
2961                 return;
2962         }
2963         /* Determine if command should end here. */
2964         if (ctx->eol && ctx->last && ctx->next_num) {
2965                 const enum index *list = ctx->next[ctx->next_num - 1];
2966                 int i;
2967
2968                 for (i = 0; list[i]; ++i) {
2969                         if (list[i] != END)
2970                                 continue;
2971                         *hdr = NULL;
2972                         return;
2973                 }
2974         }
2975         *hdr = &cmd_flow_token_hdr;
2976 }
2977
2978 /** Dispatch parsed buffer to function calls. */
2979 static void
2980 cmd_flow_parsed(const struct buffer *in)
2981 {
2982         switch (in->command) {
2983         case VALIDATE:
2984                 port_flow_validate(in->port, &in->args.vc.attr,
2985                                    in->args.vc.pattern, in->args.vc.actions);
2986                 break;
2987         case CREATE:
2988                 port_flow_create(in->port, &in->args.vc.attr,
2989                                  in->args.vc.pattern, in->args.vc.actions);
2990                 break;
2991         case DESTROY:
2992                 port_flow_destroy(in->port, in->args.destroy.rule_n,
2993                                   in->args.destroy.rule);
2994                 break;
2995         case FLUSH:
2996                 port_flow_flush(in->port);
2997                 break;
2998         case QUERY:
2999                 port_flow_query(in->port, in->args.query.rule,
3000                                 in->args.query.action);
3001                 break;
3002         case LIST:
3003                 port_flow_list(in->port, in->args.list.group_n,
3004                                in->args.list.group);
3005                 break;
3006         case ISOLATE:
3007                 port_flow_isolate(in->port, in->args.isolate.set);
3008                 break;
3009         default:
3010                 break;
3011         }
3012 }
3013
3014 /** Token generator and output processing callback (cmdline API). */
3015 static void
3016 cmd_flow_cb(void *arg0, struct cmdline *cl, void *arg2)
3017 {
3018         if (cl == NULL)
3019                 cmd_flow_tok(arg0, arg2);
3020         else
3021                 cmd_flow_parsed(arg0);
3022 }
3023
3024 /** Global parser instance (cmdline API). */
3025 cmdline_parse_inst_t cmd_flow = {
3026         .f = cmd_flow_cb,
3027         .data = NULL, /**< Unused. */
3028         .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
3029         .tokens = {
3030                 NULL,
3031         }, /**< Tokens are returned by cmd_flow_tok(). */
3032 };