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