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