app/testpmd: add L4 items to flow command
[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
43 #include <rte_common.h>
44 #include <rte_ethdev.h>
45 #include <rte_byteorder.h>
46 #include <cmdline_parse.h>
47 #include <cmdline_parse_etheraddr.h>
48 #include <rte_flow.h>
49
50 #include "testpmd.h"
51
52 /** Parser token indices. */
53 enum index {
54         /* Special tokens. */
55         ZERO = 0,
56         END,
57
58         /* Common tokens. */
59         INTEGER,
60         UNSIGNED,
61         PREFIX,
62         BOOLEAN,
63         STRING,
64         MAC_ADDR,
65         IPV4_ADDR,
66         IPV6_ADDR,
67         RULE_ID,
68         PORT_ID,
69         GROUP_ID,
70         PRIORITY_LEVEL,
71
72         /* Top-level command. */
73         FLOW,
74
75         /* Sub-level commands. */
76         VALIDATE,
77         CREATE,
78         DESTROY,
79         FLUSH,
80         QUERY,
81         LIST,
82
83         /* Destroy arguments. */
84         DESTROY_RULE,
85
86         /* Query arguments. */
87         QUERY_ACTION,
88
89         /* List arguments. */
90         LIST_GROUP,
91
92         /* Validate/create arguments. */
93         GROUP,
94         PRIORITY,
95         INGRESS,
96         EGRESS,
97
98         /* Validate/create pattern. */
99         PATTERN,
100         ITEM_PARAM_IS,
101         ITEM_PARAM_SPEC,
102         ITEM_PARAM_LAST,
103         ITEM_PARAM_MASK,
104         ITEM_PARAM_PREFIX,
105         ITEM_NEXT,
106         ITEM_END,
107         ITEM_VOID,
108         ITEM_INVERT,
109         ITEM_ANY,
110         ITEM_ANY_NUM,
111         ITEM_PF,
112         ITEM_VF,
113         ITEM_VF_ID,
114         ITEM_PORT,
115         ITEM_PORT_INDEX,
116         ITEM_RAW,
117         ITEM_RAW_RELATIVE,
118         ITEM_RAW_SEARCH,
119         ITEM_RAW_OFFSET,
120         ITEM_RAW_LIMIT,
121         ITEM_RAW_PATTERN,
122         ITEM_ETH,
123         ITEM_ETH_DST,
124         ITEM_ETH_SRC,
125         ITEM_ETH_TYPE,
126         ITEM_VLAN,
127         ITEM_VLAN_TPID,
128         ITEM_VLAN_TCI,
129         ITEM_IPV4,
130         ITEM_IPV4_SRC,
131         ITEM_IPV4_DST,
132         ITEM_IPV6,
133         ITEM_IPV6_SRC,
134         ITEM_IPV6_DST,
135         ITEM_ICMP,
136         ITEM_ICMP_TYPE,
137         ITEM_ICMP_CODE,
138         ITEM_UDP,
139         ITEM_UDP_SRC,
140         ITEM_UDP_DST,
141         ITEM_TCP,
142         ITEM_TCP_SRC,
143         ITEM_TCP_DST,
144         ITEM_SCTP,
145         ITEM_SCTP_SRC,
146         ITEM_SCTP_DST,
147         ITEM_VXLAN,
148         ITEM_VXLAN_VNI,
149
150         /* Validate/create actions. */
151         ACTIONS,
152         ACTION_NEXT,
153         ACTION_END,
154         ACTION_VOID,
155         ACTION_PASSTHRU,
156 };
157
158 /** Size of pattern[] field in struct rte_flow_item_raw. */
159 #define ITEM_RAW_PATTERN_SIZE 36
160
161 /** Storage size for struct rte_flow_item_raw including pattern. */
162 #define ITEM_RAW_SIZE \
163         (offsetof(struct rte_flow_item_raw, pattern) + ITEM_RAW_PATTERN_SIZE)
164
165 /** Maximum number of subsequent tokens and arguments on the stack. */
166 #define CTX_STACK_SIZE 16
167
168 /** Parser context. */
169 struct context {
170         /** Stack of subsequent token lists to process. */
171         const enum index *next[CTX_STACK_SIZE];
172         /** Arguments for stacked tokens. */
173         const void *args[CTX_STACK_SIZE];
174         enum index curr; /**< Current token index. */
175         enum index prev; /**< Index of the last token seen. */
176         int next_num; /**< Number of entries in next[]. */
177         int args_num; /**< Number of entries in args[]. */
178         uint32_t reparse:1; /**< Start over from the beginning. */
179         uint32_t eol:1; /**< EOL has been detected. */
180         uint32_t last:1; /**< No more arguments. */
181         uint16_t port; /**< Current port ID (for completions). */
182         uint32_t objdata; /**< Object-specific data. */
183         void *object; /**< Address of current object for relative offsets. */
184         void *objmask; /**< Object a full mask must be written to. */
185 };
186
187 /** Token argument. */
188 struct arg {
189         uint32_t hton:1; /**< Use network byte ordering. */
190         uint32_t sign:1; /**< Value is signed. */
191         uint32_t offset; /**< Relative offset from ctx->object. */
192         uint32_t size; /**< Field size. */
193         const uint8_t *mask; /**< Bit-mask to use instead of offset/size. */
194 };
195
196 /** Parser token definition. */
197 struct token {
198         /** Type displayed during completion (defaults to "TOKEN"). */
199         const char *type;
200         /** Help displayed during completion (defaults to token name). */
201         const char *help;
202         /** Private data used by parser functions. */
203         const void *priv;
204         /**
205          * Lists of subsequent tokens to push on the stack. Each call to the
206          * parser consumes the last entry of that stack.
207          */
208         const enum index *const *next;
209         /** Arguments stack for subsequent tokens that need them. */
210         const struct arg *const *args;
211         /**
212          * Token-processing callback, returns -1 in case of error, the
213          * length of the matched string otherwise. If NULL, attempts to
214          * match the token name.
215          *
216          * If buf is not NULL, the result should be stored in it according
217          * to context. An error is returned if not large enough.
218          */
219         int (*call)(struct context *ctx, const struct token *token,
220                     const char *str, unsigned int len,
221                     void *buf, unsigned int size);
222         /**
223          * Callback that provides possible values for this token, used for
224          * completion. Returns -1 in case of error, the number of possible
225          * values otherwise. If NULL, the token name is used.
226          *
227          * If buf is not NULL, entry index ent is written to buf and the
228          * full length of the entry is returned (same behavior as
229          * snprintf()).
230          */
231         int (*comp)(struct context *ctx, const struct token *token,
232                     unsigned int ent, char *buf, unsigned int size);
233         /** Mandatory token name, no default value. */
234         const char *name;
235 };
236
237 /** Static initializer for the next field. */
238 #define NEXT(...) (const enum index *const []){ __VA_ARGS__, NULL, }
239
240 /** Static initializer for a NEXT() entry. */
241 #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, }
242
243 /** Static initializer for the args field. */
244 #define ARGS(...) (const struct arg *const []){ __VA_ARGS__, NULL, }
245
246 /** Static initializer for ARGS() to target a field. */
247 #define ARGS_ENTRY(s, f) \
248         (&(const struct arg){ \
249                 .offset = offsetof(s, f), \
250                 .size = sizeof(((s *)0)->f), \
251         })
252
253 /** Static initializer for ARGS() to target a bit-field. */
254 #define ARGS_ENTRY_BF(s, f, b) \
255         (&(const struct arg){ \
256                 .size = sizeof(s), \
257                 .mask = (const void *)&(const s){ .f = (1 << (b)) - 1 }, \
258         })
259
260 /** Static initializer for ARGS() to target a pointer. */
261 #define ARGS_ENTRY_PTR(s, f) \
262         (&(const struct arg){ \
263                 .size = sizeof(*((s *)0)->f), \
264         })
265
266 /** Static initializer for ARGS() with arbitrary size. */
267 #define ARGS_ENTRY_USZ(s, f, sz) \
268         (&(const struct arg){ \
269                 .offset = offsetof(s, f), \
270                 .size = (sz), \
271         })
272
273 /** Same as ARGS_ENTRY() using network byte ordering. */
274 #define ARGS_ENTRY_HTON(s, f) \
275         (&(const struct arg){ \
276                 .hton = 1, \
277                 .offset = offsetof(s, f), \
278                 .size = sizeof(((s *)0)->f), \
279         })
280
281 /** Parser output buffer layout expected by cmd_flow_parsed(). */
282 struct buffer {
283         enum index command; /**< Flow command. */
284         uint16_t port; /**< Affected port ID. */
285         union {
286                 struct {
287                         struct rte_flow_attr attr;
288                         struct rte_flow_item *pattern;
289                         struct rte_flow_action *actions;
290                         uint32_t pattern_n;
291                         uint32_t actions_n;
292                         uint8_t *data;
293                 } vc; /**< Validate/create arguments. */
294                 struct {
295                         uint32_t *rule;
296                         uint32_t rule_n;
297                 } destroy; /**< Destroy arguments. */
298                 struct {
299                         uint32_t rule;
300                         enum rte_flow_action_type action;
301                 } query; /**< Query arguments. */
302                 struct {
303                         uint32_t *group;
304                         uint32_t group_n;
305                 } list; /**< List arguments. */
306         } args; /**< Command arguments. */
307 };
308
309 /** Private data for pattern items. */
310 struct parse_item_priv {
311         enum rte_flow_item_type type; /**< Item type. */
312         uint32_t size; /**< Size of item specification structure. */
313 };
314
315 #define PRIV_ITEM(t, s) \
316         (&(const struct parse_item_priv){ \
317                 .type = RTE_FLOW_ITEM_TYPE_ ## t, \
318                 .size = s, \
319         })
320
321 /** Private data for actions. */
322 struct parse_action_priv {
323         enum rte_flow_action_type type; /**< Action type. */
324         uint32_t size; /**< Size of action configuration structure. */
325 };
326
327 #define PRIV_ACTION(t, s) \
328         (&(const struct parse_action_priv){ \
329                 .type = RTE_FLOW_ACTION_TYPE_ ## t, \
330                 .size = s, \
331         })
332
333 static const enum index next_vc_attr[] = {
334         GROUP,
335         PRIORITY,
336         INGRESS,
337         EGRESS,
338         PATTERN,
339         ZERO,
340 };
341
342 static const enum index next_destroy_attr[] = {
343         DESTROY_RULE,
344         END,
345         ZERO,
346 };
347
348 static const enum index next_list_attr[] = {
349         LIST_GROUP,
350         END,
351         ZERO,
352 };
353
354 static const enum index item_param[] = {
355         ITEM_PARAM_IS,
356         ITEM_PARAM_SPEC,
357         ITEM_PARAM_LAST,
358         ITEM_PARAM_MASK,
359         ITEM_PARAM_PREFIX,
360         ZERO,
361 };
362
363 static const enum index next_item[] = {
364         ITEM_END,
365         ITEM_VOID,
366         ITEM_INVERT,
367         ITEM_ANY,
368         ITEM_PF,
369         ITEM_VF,
370         ITEM_PORT,
371         ITEM_RAW,
372         ITEM_ETH,
373         ITEM_VLAN,
374         ITEM_IPV4,
375         ITEM_IPV6,
376         ITEM_ICMP,
377         ITEM_UDP,
378         ITEM_TCP,
379         ITEM_SCTP,
380         ITEM_VXLAN,
381         ZERO,
382 };
383
384 static const enum index item_any[] = {
385         ITEM_ANY_NUM,
386         ITEM_NEXT,
387         ZERO,
388 };
389
390 static const enum index item_vf[] = {
391         ITEM_VF_ID,
392         ITEM_NEXT,
393         ZERO,
394 };
395
396 static const enum index item_port[] = {
397         ITEM_PORT_INDEX,
398         ITEM_NEXT,
399         ZERO,
400 };
401
402 static const enum index item_raw[] = {
403         ITEM_RAW_RELATIVE,
404         ITEM_RAW_SEARCH,
405         ITEM_RAW_OFFSET,
406         ITEM_RAW_LIMIT,
407         ITEM_RAW_PATTERN,
408         ITEM_NEXT,
409         ZERO,
410 };
411
412 static const enum index item_eth[] = {
413         ITEM_ETH_DST,
414         ITEM_ETH_SRC,
415         ITEM_ETH_TYPE,
416         ITEM_NEXT,
417         ZERO,
418 };
419
420 static const enum index item_vlan[] = {
421         ITEM_VLAN_TPID,
422         ITEM_VLAN_TCI,
423         ITEM_NEXT,
424         ZERO,
425 };
426
427 static const enum index item_ipv4[] = {
428         ITEM_IPV4_SRC,
429         ITEM_IPV4_DST,
430         ITEM_NEXT,
431         ZERO,
432 };
433
434 static const enum index item_ipv6[] = {
435         ITEM_IPV6_SRC,
436         ITEM_IPV6_DST,
437         ITEM_NEXT,
438         ZERO,
439 };
440
441 static const enum index item_icmp[] = {
442         ITEM_ICMP_TYPE,
443         ITEM_ICMP_CODE,
444         ITEM_NEXT,
445         ZERO,
446 };
447
448 static const enum index item_udp[] = {
449         ITEM_UDP_SRC,
450         ITEM_UDP_DST,
451         ITEM_NEXT,
452         ZERO,
453 };
454
455 static const enum index item_tcp[] = {
456         ITEM_TCP_SRC,
457         ITEM_TCP_DST,
458         ITEM_NEXT,
459         ZERO,
460 };
461
462 static const enum index item_sctp[] = {
463         ITEM_SCTP_SRC,
464         ITEM_SCTP_DST,
465         ITEM_NEXT,
466         ZERO,
467 };
468
469 static const enum index item_vxlan[] = {
470         ITEM_VXLAN_VNI,
471         ITEM_NEXT,
472         ZERO,
473 };
474
475 static const enum index next_action[] = {
476         ACTION_END,
477         ACTION_VOID,
478         ACTION_PASSTHRU,
479         ZERO,
480 };
481
482 static int parse_init(struct context *, const struct token *,
483                       const char *, unsigned int,
484                       void *, unsigned int);
485 static int parse_vc(struct context *, const struct token *,
486                     const char *, unsigned int,
487                     void *, unsigned int);
488 static int parse_vc_spec(struct context *, const struct token *,
489                          const char *, unsigned int, void *, unsigned int);
490 static int parse_destroy(struct context *, const struct token *,
491                          const char *, unsigned int,
492                          void *, unsigned int);
493 static int parse_flush(struct context *, const struct token *,
494                        const char *, unsigned int,
495                        void *, unsigned int);
496 static int parse_query(struct context *, const struct token *,
497                        const char *, unsigned int,
498                        void *, unsigned int);
499 static int parse_action(struct context *, const struct token *,
500                         const char *, unsigned int,
501                         void *, unsigned int);
502 static int parse_list(struct context *, const struct token *,
503                       const char *, unsigned int,
504                       void *, unsigned int);
505 static int parse_int(struct context *, const struct token *,
506                      const char *, unsigned int,
507                      void *, unsigned int);
508 static int parse_prefix(struct context *, const struct token *,
509                         const char *, unsigned int,
510                         void *, unsigned int);
511 static int parse_boolean(struct context *, const struct token *,
512                          const char *, unsigned int,
513                          void *, unsigned int);
514 static int parse_string(struct context *, const struct token *,
515                         const char *, unsigned int,
516                         void *, unsigned int);
517 static int parse_mac_addr(struct context *, const struct token *,
518                           const char *, unsigned int,
519                           void *, unsigned int);
520 static int parse_ipv4_addr(struct context *, const struct token *,
521                            const char *, unsigned int,
522                            void *, unsigned int);
523 static int parse_ipv6_addr(struct context *, const struct token *,
524                            const char *, unsigned int,
525                            void *, unsigned int);
526 static int parse_port(struct context *, const struct token *,
527                       const char *, unsigned int,
528                       void *, unsigned int);
529 static int comp_none(struct context *, const struct token *,
530                      unsigned int, char *, unsigned int);
531 static int comp_boolean(struct context *, const struct token *,
532                         unsigned int, char *, unsigned int);
533 static int comp_action(struct context *, const struct token *,
534                        unsigned int, char *, unsigned int);
535 static int comp_port(struct context *, const struct token *,
536                      unsigned int, char *, unsigned int);
537 static int comp_rule_id(struct context *, const struct token *,
538                         unsigned int, char *, unsigned int);
539
540 /** Token definitions. */
541 static const struct token token_list[] = {
542         /* Special tokens. */
543         [ZERO] = {
544                 .name = "ZERO",
545                 .help = "null entry, abused as the entry point",
546                 .next = NEXT(NEXT_ENTRY(FLOW)),
547         },
548         [END] = {
549                 .name = "",
550                 .type = "RETURN",
551                 .help = "command may end here",
552         },
553         /* Common tokens. */
554         [INTEGER] = {
555                 .name = "{int}",
556                 .type = "INTEGER",
557                 .help = "integer value",
558                 .call = parse_int,
559                 .comp = comp_none,
560         },
561         [UNSIGNED] = {
562                 .name = "{unsigned}",
563                 .type = "UNSIGNED",
564                 .help = "unsigned integer value",
565                 .call = parse_int,
566                 .comp = comp_none,
567         },
568         [PREFIX] = {
569                 .name = "{prefix}",
570                 .type = "PREFIX",
571                 .help = "prefix length for bit-mask",
572                 .call = parse_prefix,
573                 .comp = comp_none,
574         },
575         [BOOLEAN] = {
576                 .name = "{boolean}",
577                 .type = "BOOLEAN",
578                 .help = "any boolean value",
579                 .call = parse_boolean,
580                 .comp = comp_boolean,
581         },
582         [STRING] = {
583                 .name = "{string}",
584                 .type = "STRING",
585                 .help = "fixed string",
586                 .call = parse_string,
587                 .comp = comp_none,
588         },
589         [MAC_ADDR] = {
590                 .name = "{MAC address}",
591                 .type = "MAC-48",
592                 .help = "standard MAC address notation",
593                 .call = parse_mac_addr,
594                 .comp = comp_none,
595         },
596         [IPV4_ADDR] = {
597                 .name = "{IPv4 address}",
598                 .type = "IPV4 ADDRESS",
599                 .help = "standard IPv4 address notation",
600                 .call = parse_ipv4_addr,
601                 .comp = comp_none,
602         },
603         [IPV6_ADDR] = {
604                 .name = "{IPv6 address}",
605                 .type = "IPV6 ADDRESS",
606                 .help = "standard IPv6 address notation",
607                 .call = parse_ipv6_addr,
608                 .comp = comp_none,
609         },
610         [RULE_ID] = {
611                 .name = "{rule id}",
612                 .type = "RULE ID",
613                 .help = "rule identifier",
614                 .call = parse_int,
615                 .comp = comp_rule_id,
616         },
617         [PORT_ID] = {
618                 .name = "{port_id}",
619                 .type = "PORT ID",
620                 .help = "port identifier",
621                 .call = parse_port,
622                 .comp = comp_port,
623         },
624         [GROUP_ID] = {
625                 .name = "{group_id}",
626                 .type = "GROUP ID",
627                 .help = "group identifier",
628                 .call = parse_int,
629                 .comp = comp_none,
630         },
631         [PRIORITY_LEVEL] = {
632                 .name = "{level}",
633                 .type = "PRIORITY",
634                 .help = "priority level",
635                 .call = parse_int,
636                 .comp = comp_none,
637         },
638         /* Top-level command. */
639         [FLOW] = {
640                 .name = "flow",
641                 .type = "{command} {port_id} [{arg} [...]]",
642                 .help = "manage ingress/egress flow rules",
643                 .next = NEXT(NEXT_ENTRY
644                              (VALIDATE,
645                               CREATE,
646                               DESTROY,
647                               FLUSH,
648                               LIST,
649                               QUERY)),
650                 .call = parse_init,
651         },
652         /* Sub-level commands. */
653         [VALIDATE] = {
654                 .name = "validate",
655                 .help = "check whether a flow rule can be created",
656                 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
657                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
658                 .call = parse_vc,
659         },
660         [CREATE] = {
661                 .name = "create",
662                 .help = "create a flow rule",
663                 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
664                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
665                 .call = parse_vc,
666         },
667         [DESTROY] = {
668                 .name = "destroy",
669                 .help = "destroy specific flow rules",
670                 .next = NEXT(NEXT_ENTRY(DESTROY_RULE), NEXT_ENTRY(PORT_ID)),
671                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
672                 .call = parse_destroy,
673         },
674         [FLUSH] = {
675                 .name = "flush",
676                 .help = "destroy all flow rules",
677                 .next = NEXT(NEXT_ENTRY(PORT_ID)),
678                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
679                 .call = parse_flush,
680         },
681         [QUERY] = {
682                 .name = "query",
683                 .help = "query an existing flow rule",
684                 .next = NEXT(NEXT_ENTRY(QUERY_ACTION),
685                              NEXT_ENTRY(RULE_ID),
686                              NEXT_ENTRY(PORT_ID)),
687                 .args = ARGS(ARGS_ENTRY(struct buffer, args.query.action),
688                              ARGS_ENTRY(struct buffer, args.query.rule),
689                              ARGS_ENTRY(struct buffer, port)),
690                 .call = parse_query,
691         },
692         [LIST] = {
693                 .name = "list",
694                 .help = "list existing flow rules",
695                 .next = NEXT(next_list_attr, NEXT_ENTRY(PORT_ID)),
696                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
697                 .call = parse_list,
698         },
699         /* Destroy arguments. */
700         [DESTROY_RULE] = {
701                 .name = "rule",
702                 .help = "specify a rule identifier",
703                 .next = NEXT(next_destroy_attr, NEXT_ENTRY(RULE_ID)),
704                 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.destroy.rule)),
705                 .call = parse_destroy,
706         },
707         /* Query arguments. */
708         [QUERY_ACTION] = {
709                 .name = "{action}",
710                 .type = "ACTION",
711                 .help = "action to query, must be part of the rule",
712                 .call = parse_action,
713                 .comp = comp_action,
714         },
715         /* List arguments. */
716         [LIST_GROUP] = {
717                 .name = "group",
718                 .help = "specify a group",
719                 .next = NEXT(next_list_attr, NEXT_ENTRY(GROUP_ID)),
720                 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.list.group)),
721                 .call = parse_list,
722         },
723         /* Validate/create attributes. */
724         [GROUP] = {
725                 .name = "group",
726                 .help = "specify a group",
727                 .next = NEXT(next_vc_attr, NEXT_ENTRY(GROUP_ID)),
728                 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, group)),
729                 .call = parse_vc,
730         },
731         [PRIORITY] = {
732                 .name = "priority",
733                 .help = "specify a priority level",
734                 .next = NEXT(next_vc_attr, NEXT_ENTRY(PRIORITY_LEVEL)),
735                 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, priority)),
736                 .call = parse_vc,
737         },
738         [INGRESS] = {
739                 .name = "ingress",
740                 .help = "affect rule to ingress",
741                 .next = NEXT(next_vc_attr),
742                 .call = parse_vc,
743         },
744         [EGRESS] = {
745                 .name = "egress",
746                 .help = "affect rule to egress",
747                 .next = NEXT(next_vc_attr),
748                 .call = parse_vc,
749         },
750         /* Validate/create pattern. */
751         [PATTERN] = {
752                 .name = "pattern",
753                 .help = "submit a list of pattern items",
754                 .next = NEXT(next_item),
755                 .call = parse_vc,
756         },
757         [ITEM_PARAM_IS] = {
758                 .name = "is",
759                 .help = "match value perfectly (with full bit-mask)",
760                 .call = parse_vc_spec,
761         },
762         [ITEM_PARAM_SPEC] = {
763                 .name = "spec",
764                 .help = "match value according to configured bit-mask",
765                 .call = parse_vc_spec,
766         },
767         [ITEM_PARAM_LAST] = {
768                 .name = "last",
769                 .help = "specify upper bound to establish a range",
770                 .call = parse_vc_spec,
771         },
772         [ITEM_PARAM_MASK] = {
773                 .name = "mask",
774                 .help = "specify bit-mask with relevant bits set to one",
775                 .call = parse_vc_spec,
776         },
777         [ITEM_PARAM_PREFIX] = {
778                 .name = "prefix",
779                 .help = "generate bit-mask from a prefix length",
780                 .call = parse_vc_spec,
781         },
782         [ITEM_NEXT] = {
783                 .name = "/",
784                 .help = "specify next pattern item",
785                 .next = NEXT(next_item),
786         },
787         [ITEM_END] = {
788                 .name = "end",
789                 .help = "end list of pattern items",
790                 .priv = PRIV_ITEM(END, 0),
791                 .next = NEXT(NEXT_ENTRY(ACTIONS)),
792                 .call = parse_vc,
793         },
794         [ITEM_VOID] = {
795                 .name = "void",
796                 .help = "no-op pattern item",
797                 .priv = PRIV_ITEM(VOID, 0),
798                 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
799                 .call = parse_vc,
800         },
801         [ITEM_INVERT] = {
802                 .name = "invert",
803                 .help = "perform actions when pattern does not match",
804                 .priv = PRIV_ITEM(INVERT, 0),
805                 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
806                 .call = parse_vc,
807         },
808         [ITEM_ANY] = {
809                 .name = "any",
810                 .help = "match any protocol for the current layer",
811                 .priv = PRIV_ITEM(ANY, sizeof(struct rte_flow_item_any)),
812                 .next = NEXT(item_any),
813                 .call = parse_vc,
814         },
815         [ITEM_ANY_NUM] = {
816                 .name = "num",
817                 .help = "number of layers covered",
818                 .next = NEXT(item_any, NEXT_ENTRY(UNSIGNED), item_param),
819                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_any, num)),
820         },
821         [ITEM_PF] = {
822                 .name = "pf",
823                 .help = "match packets addressed to the physical function",
824                 .priv = PRIV_ITEM(PF, 0),
825                 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
826                 .call = parse_vc,
827         },
828         [ITEM_VF] = {
829                 .name = "vf",
830                 .help = "match packets addressed to a virtual function ID",
831                 .priv = PRIV_ITEM(VF, sizeof(struct rte_flow_item_vf)),
832                 .next = NEXT(item_vf),
833                 .call = parse_vc,
834         },
835         [ITEM_VF_ID] = {
836                 .name = "id",
837                 .help = "destination VF ID",
838                 .next = NEXT(item_vf, NEXT_ENTRY(UNSIGNED), item_param),
839                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_vf, id)),
840         },
841         [ITEM_PORT] = {
842                 .name = "port",
843                 .help = "device-specific physical port index to use",
844                 .priv = PRIV_ITEM(PORT, sizeof(struct rte_flow_item_port)),
845                 .next = NEXT(item_port),
846                 .call = parse_vc,
847         },
848         [ITEM_PORT_INDEX] = {
849                 .name = "index",
850                 .help = "physical port index",
851                 .next = NEXT(item_port, NEXT_ENTRY(UNSIGNED), item_param),
852                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_port, index)),
853         },
854         [ITEM_RAW] = {
855                 .name = "raw",
856                 .help = "match an arbitrary byte string",
857                 .priv = PRIV_ITEM(RAW, ITEM_RAW_SIZE),
858                 .next = NEXT(item_raw),
859                 .call = parse_vc,
860         },
861         [ITEM_RAW_RELATIVE] = {
862                 .name = "relative",
863                 .help = "look for pattern after the previous item",
864                 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
865                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
866                                            relative, 1)),
867         },
868         [ITEM_RAW_SEARCH] = {
869                 .name = "search",
870                 .help = "search pattern from offset (see also limit)",
871                 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
872                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
873                                            search, 1)),
874         },
875         [ITEM_RAW_OFFSET] = {
876                 .name = "offset",
877                 .help = "absolute or relative offset for pattern",
878                 .next = NEXT(item_raw, NEXT_ENTRY(INTEGER), item_param),
879                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, offset)),
880         },
881         [ITEM_RAW_LIMIT] = {
882                 .name = "limit",
883                 .help = "search area limit for start of pattern",
884                 .next = NEXT(item_raw, NEXT_ENTRY(UNSIGNED), item_param),
885                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, limit)),
886         },
887         [ITEM_RAW_PATTERN] = {
888                 .name = "pattern",
889                 .help = "byte string to look for",
890                 .next = NEXT(item_raw,
891                              NEXT_ENTRY(STRING),
892                              NEXT_ENTRY(ITEM_PARAM_IS,
893                                         ITEM_PARAM_SPEC,
894                                         ITEM_PARAM_MASK)),
895                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, length),
896                              ARGS_ENTRY_USZ(struct rte_flow_item_raw,
897                                             pattern,
898                                             ITEM_RAW_PATTERN_SIZE)),
899         },
900         [ITEM_ETH] = {
901                 .name = "eth",
902                 .help = "match Ethernet header",
903                 .priv = PRIV_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
904                 .next = NEXT(item_eth),
905                 .call = parse_vc,
906         },
907         [ITEM_ETH_DST] = {
908                 .name = "dst",
909                 .help = "destination MAC",
910                 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
911                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_eth, dst)),
912         },
913         [ITEM_ETH_SRC] = {
914                 .name = "src",
915                 .help = "source MAC",
916                 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
917                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_eth, src)),
918         },
919         [ITEM_ETH_TYPE] = {
920                 .name = "type",
921                 .help = "EtherType",
922                 .next = NEXT(item_eth, NEXT_ENTRY(UNSIGNED), item_param),
923                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, type)),
924         },
925         [ITEM_VLAN] = {
926                 .name = "vlan",
927                 .help = "match 802.1Q/ad VLAN tag",
928                 .priv = PRIV_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
929                 .next = NEXT(item_vlan),
930                 .call = parse_vc,
931         },
932         [ITEM_VLAN_TPID] = {
933                 .name = "tpid",
934                 .help = "tag protocol identifier",
935                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
936                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tpid)),
937         },
938         [ITEM_VLAN_TCI] = {
939                 .name = "tci",
940                 .help = "tag control information",
941                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
942                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tci)),
943         },
944         [ITEM_IPV4] = {
945                 .name = "ipv4",
946                 .help = "match IPv4 header",
947                 .priv = PRIV_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
948                 .next = NEXT(item_ipv4),
949                 .call = parse_vc,
950         },
951         [ITEM_IPV4_SRC] = {
952                 .name = "src",
953                 .help = "source address",
954                 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
955                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
956                                              hdr.src_addr)),
957         },
958         [ITEM_IPV4_DST] = {
959                 .name = "dst",
960                 .help = "destination address",
961                 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
962                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
963                                              hdr.dst_addr)),
964         },
965         [ITEM_IPV6] = {
966                 .name = "ipv6",
967                 .help = "match IPv6 header",
968                 .priv = PRIV_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
969                 .next = NEXT(item_ipv6),
970                 .call = parse_vc,
971         },
972         [ITEM_IPV6_SRC] = {
973                 .name = "src",
974                 .help = "source address",
975                 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
976                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
977                                              hdr.src_addr)),
978         },
979         [ITEM_IPV6_DST] = {
980                 .name = "dst",
981                 .help = "destination address",
982                 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
983                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
984                                              hdr.dst_addr)),
985         },
986         [ITEM_ICMP] = {
987                 .name = "icmp",
988                 .help = "match ICMP header",
989                 .priv = PRIV_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
990                 .next = NEXT(item_icmp),
991                 .call = parse_vc,
992         },
993         [ITEM_ICMP_TYPE] = {
994                 .name = "type",
995                 .help = "ICMP packet type",
996                 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
997                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
998                                              hdr.icmp_type)),
999         },
1000         [ITEM_ICMP_CODE] = {
1001                 .name = "code",
1002                 .help = "ICMP packet code",
1003                 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1004                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1005                                              hdr.icmp_code)),
1006         },
1007         [ITEM_UDP] = {
1008                 .name = "udp",
1009                 .help = "match UDP header",
1010                 .priv = PRIV_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
1011                 .next = NEXT(item_udp),
1012                 .call = parse_vc,
1013         },
1014         [ITEM_UDP_SRC] = {
1015                 .name = "src",
1016                 .help = "UDP source port",
1017                 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1018                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1019                                              hdr.src_port)),
1020         },
1021         [ITEM_UDP_DST] = {
1022                 .name = "dst",
1023                 .help = "UDP destination port",
1024                 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1025                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1026                                              hdr.dst_port)),
1027         },
1028         [ITEM_TCP] = {
1029                 .name = "tcp",
1030                 .help = "match TCP header",
1031                 .priv = PRIV_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
1032                 .next = NEXT(item_tcp),
1033                 .call = parse_vc,
1034         },
1035         [ITEM_TCP_SRC] = {
1036                 .name = "src",
1037                 .help = "TCP source port",
1038                 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1039                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1040                                              hdr.src_port)),
1041         },
1042         [ITEM_TCP_DST] = {
1043                 .name = "dst",
1044                 .help = "TCP destination port",
1045                 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1046                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1047                                              hdr.dst_port)),
1048         },
1049         [ITEM_SCTP] = {
1050                 .name = "sctp",
1051                 .help = "match SCTP header",
1052                 .priv = PRIV_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
1053                 .next = NEXT(item_sctp),
1054                 .call = parse_vc,
1055         },
1056         [ITEM_SCTP_SRC] = {
1057                 .name = "src",
1058                 .help = "SCTP source port",
1059                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1060                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1061                                              hdr.src_port)),
1062         },
1063         [ITEM_SCTP_DST] = {
1064                 .name = "dst",
1065                 .help = "SCTP destination port",
1066                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1067                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1068                                              hdr.dst_port)),
1069         },
1070         [ITEM_VXLAN] = {
1071                 .name = "vxlan",
1072                 .help = "match VXLAN header",
1073                 .priv = PRIV_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
1074                 .next = NEXT(item_vxlan),
1075                 .call = parse_vc,
1076         },
1077         [ITEM_VXLAN_VNI] = {
1078                 .name = "vni",
1079                 .help = "VXLAN identifier",
1080                 .next = NEXT(item_vxlan, NEXT_ENTRY(UNSIGNED), item_param),
1081                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vxlan, vni)),
1082         },
1083         /* Validate/create actions. */
1084         [ACTIONS] = {
1085                 .name = "actions",
1086                 .help = "submit a list of associated actions",
1087                 .next = NEXT(next_action),
1088                 .call = parse_vc,
1089         },
1090         [ACTION_NEXT] = {
1091                 .name = "/",
1092                 .help = "specify next action",
1093                 .next = NEXT(next_action),
1094         },
1095         [ACTION_END] = {
1096                 .name = "end",
1097                 .help = "end list of actions",
1098                 .priv = PRIV_ACTION(END, 0),
1099                 .call = parse_vc,
1100         },
1101         [ACTION_VOID] = {
1102                 .name = "void",
1103                 .help = "no-op action",
1104                 .priv = PRIV_ACTION(VOID, 0),
1105                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1106                 .call = parse_vc,
1107         },
1108         [ACTION_PASSTHRU] = {
1109                 .name = "passthru",
1110                 .help = "let subsequent rule process matched packets",
1111                 .priv = PRIV_ACTION(PASSTHRU, 0),
1112                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1113                 .call = parse_vc,
1114         },
1115 };
1116
1117 /** Remove and return last entry from argument stack. */
1118 static const struct arg *
1119 pop_args(struct context *ctx)
1120 {
1121         return ctx->args_num ? ctx->args[--ctx->args_num] : NULL;
1122 }
1123
1124 /** Add entry on top of the argument stack. */
1125 static int
1126 push_args(struct context *ctx, const struct arg *arg)
1127 {
1128         if (ctx->args_num == CTX_STACK_SIZE)
1129                 return -1;
1130         ctx->args[ctx->args_num++] = arg;
1131         return 0;
1132 }
1133
1134 /** Spread value into buffer according to bit-mask. */
1135 static size_t
1136 arg_entry_bf_fill(void *dst, uintmax_t val, const struct arg *arg)
1137 {
1138         uint32_t i = arg->size;
1139         uint32_t end = 0;
1140         int sub = 1;
1141         int add = 0;
1142         size_t len = 0;
1143
1144         if (!arg->mask)
1145                 return 0;
1146 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1147         if (!arg->hton) {
1148                 i = 0;
1149                 end = arg->size;
1150                 sub = 0;
1151                 add = 1;
1152         }
1153 #endif
1154         while (i != end) {
1155                 unsigned int shift = 0;
1156                 uint8_t *buf = (uint8_t *)dst + arg->offset + (i -= sub);
1157
1158                 for (shift = 0; arg->mask[i] >> shift; ++shift) {
1159                         if (!(arg->mask[i] & (1 << shift)))
1160                                 continue;
1161                         ++len;
1162                         if (!dst)
1163                                 continue;
1164                         *buf &= ~(1 << shift);
1165                         *buf |= (val & 1) << shift;
1166                         val >>= 1;
1167                 }
1168                 i += add;
1169         }
1170         return len;
1171 }
1172
1173 /**
1174  * Parse a prefix length and generate a bit-mask.
1175  *
1176  * Last argument (ctx->args) is retrieved to determine mask size, storage
1177  * location and whether the result must use network byte ordering.
1178  */
1179 static int
1180 parse_prefix(struct context *ctx, const struct token *token,
1181              const char *str, unsigned int len,
1182              void *buf, unsigned int size)
1183 {
1184         const struct arg *arg = pop_args(ctx);
1185         static const uint8_t conv[] = "\x00\x80\xc0\xe0\xf0\xf8\xfc\xfe\xff";
1186         char *end;
1187         uintmax_t u;
1188         unsigned int bytes;
1189         unsigned int extra;
1190
1191         (void)token;
1192         /* Argument is expected. */
1193         if (!arg)
1194                 return -1;
1195         errno = 0;
1196         u = strtoumax(str, &end, 0);
1197         if (errno || (size_t)(end - str) != len)
1198                 goto error;
1199         if (arg->mask) {
1200                 uintmax_t v = 0;
1201
1202                 extra = arg_entry_bf_fill(NULL, 0, arg);
1203                 if (u > extra)
1204                         goto error;
1205                 if (!ctx->object)
1206                         return len;
1207                 extra -= u;
1208                 while (u--)
1209                         (v <<= 1, v |= 1);
1210                 v <<= extra;
1211                 if (!arg_entry_bf_fill(ctx->object, v, arg) ||
1212                     !arg_entry_bf_fill(ctx->objmask, -1, arg))
1213                         goto error;
1214                 return len;
1215         }
1216         bytes = u / 8;
1217         extra = u % 8;
1218         size = arg->size;
1219         if (bytes > size || bytes + !!extra > size)
1220                 goto error;
1221         if (!ctx->object)
1222                 return len;
1223         buf = (uint8_t *)ctx->object + arg->offset;
1224 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1225         if (!arg->hton) {
1226                 memset((uint8_t *)buf + size - bytes, 0xff, bytes);
1227                 memset(buf, 0x00, size - bytes);
1228                 if (extra)
1229                         ((uint8_t *)buf)[size - bytes - 1] = conv[extra];
1230         } else
1231 #endif
1232         {
1233                 memset(buf, 0xff, bytes);
1234                 memset((uint8_t *)buf + bytes, 0x00, size - bytes);
1235                 if (extra)
1236                         ((uint8_t *)buf)[bytes] = conv[extra];
1237         }
1238         if (ctx->objmask)
1239                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
1240         return len;
1241 error:
1242         push_args(ctx, arg);
1243         return -1;
1244 }
1245
1246 /** Default parsing function for token name matching. */
1247 static int
1248 parse_default(struct context *ctx, const struct token *token,
1249               const char *str, unsigned int len,
1250               void *buf, unsigned int size)
1251 {
1252         (void)ctx;
1253         (void)buf;
1254         (void)size;
1255         if (strncmp(str, token->name, len))
1256                 return -1;
1257         return len;
1258 }
1259
1260 /** Parse flow command, initialize output buffer for subsequent tokens. */
1261 static int
1262 parse_init(struct context *ctx, const struct token *token,
1263            const char *str, unsigned int len,
1264            void *buf, unsigned int size)
1265 {
1266         struct buffer *out = buf;
1267
1268         /* Token name must match. */
1269         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1270                 return -1;
1271         /* Nothing else to do if there is no buffer. */
1272         if (!out)
1273                 return len;
1274         /* Make sure buffer is large enough. */
1275         if (size < sizeof(*out))
1276                 return -1;
1277         /* Initialize buffer. */
1278         memset(out, 0x00, sizeof(*out));
1279         memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
1280         ctx->objdata = 0;
1281         ctx->object = out;
1282         ctx->objmask = NULL;
1283         return len;
1284 }
1285
1286 /** Parse tokens for validate/create commands. */
1287 static int
1288 parse_vc(struct context *ctx, const struct token *token,
1289          const char *str, unsigned int len,
1290          void *buf, unsigned int size)
1291 {
1292         struct buffer *out = buf;
1293         uint8_t *data;
1294         uint32_t data_size;
1295
1296         /* Token name must match. */
1297         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1298                 return -1;
1299         /* Nothing else to do if there is no buffer. */
1300         if (!out)
1301                 return len;
1302         if (!out->command) {
1303                 if (ctx->curr != VALIDATE && ctx->curr != CREATE)
1304                         return -1;
1305                 if (sizeof(*out) > size)
1306                         return -1;
1307                 out->command = ctx->curr;
1308                 ctx->objdata = 0;
1309                 ctx->object = out;
1310                 ctx->objmask = NULL;
1311                 out->args.vc.data = (uint8_t *)out + size;
1312                 return len;
1313         }
1314         ctx->objdata = 0;
1315         ctx->object = &out->args.vc.attr;
1316         ctx->objmask = NULL;
1317         switch (ctx->curr) {
1318         case GROUP:
1319         case PRIORITY:
1320                 return len;
1321         case INGRESS:
1322                 out->args.vc.attr.ingress = 1;
1323                 return len;
1324         case EGRESS:
1325                 out->args.vc.attr.egress = 1;
1326                 return len;
1327         case PATTERN:
1328                 out->args.vc.pattern =
1329                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
1330                                                sizeof(double));
1331                 ctx->object = out->args.vc.pattern;
1332                 ctx->objmask = NULL;
1333                 return len;
1334         case ACTIONS:
1335                 out->args.vc.actions =
1336                         (void *)RTE_ALIGN_CEIL((uintptr_t)
1337                                                (out->args.vc.pattern +
1338                                                 out->args.vc.pattern_n),
1339                                                sizeof(double));
1340                 ctx->object = out->args.vc.actions;
1341                 ctx->objmask = NULL;
1342                 return len;
1343         default:
1344                 if (!token->priv)
1345                         return -1;
1346                 break;
1347         }
1348         if (!out->args.vc.actions) {
1349                 const struct parse_item_priv *priv = token->priv;
1350                 struct rte_flow_item *item =
1351                         out->args.vc.pattern + out->args.vc.pattern_n;
1352
1353                 data_size = priv->size * 3; /* spec, last, mask */
1354                 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
1355                                                (out->args.vc.data - data_size),
1356                                                sizeof(double));
1357                 if ((uint8_t *)item + sizeof(*item) > data)
1358                         return -1;
1359                 *item = (struct rte_flow_item){
1360                         .type = priv->type,
1361                 };
1362                 ++out->args.vc.pattern_n;
1363                 ctx->object = item;
1364                 ctx->objmask = NULL;
1365         } else {
1366                 const struct parse_action_priv *priv = token->priv;
1367                 struct rte_flow_action *action =
1368                         out->args.vc.actions + out->args.vc.actions_n;
1369
1370                 data_size = priv->size; /* configuration */
1371                 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
1372                                                (out->args.vc.data - data_size),
1373                                                sizeof(double));
1374                 if ((uint8_t *)action + sizeof(*action) > data)
1375                         return -1;
1376                 *action = (struct rte_flow_action){
1377                         .type = priv->type,
1378                 };
1379                 ++out->args.vc.actions_n;
1380                 ctx->object = action;
1381                 ctx->objmask = NULL;
1382         }
1383         memset(data, 0, data_size);
1384         out->args.vc.data = data;
1385         ctx->objdata = data_size;
1386         return len;
1387 }
1388
1389 /** Parse pattern item parameter type. */
1390 static int
1391 parse_vc_spec(struct context *ctx, const struct token *token,
1392               const char *str, unsigned int len,
1393               void *buf, unsigned int size)
1394 {
1395         struct buffer *out = buf;
1396         struct rte_flow_item *item;
1397         uint32_t data_size;
1398         int index;
1399         int objmask = 0;
1400
1401         (void)size;
1402         /* Token name must match. */
1403         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1404                 return -1;
1405         /* Parse parameter types. */
1406         switch (ctx->curr) {
1407         case ITEM_PARAM_IS:
1408                 index = 0;
1409                 objmask = 1;
1410                 break;
1411         case ITEM_PARAM_SPEC:
1412                 index = 0;
1413                 break;
1414         case ITEM_PARAM_LAST:
1415                 index = 1;
1416                 break;
1417         case ITEM_PARAM_PREFIX:
1418                 /* Modify next token to expect a prefix. */
1419                 if (ctx->next_num < 2)
1420                         return -1;
1421                 ctx->next[ctx->next_num - 2] = NEXT_ENTRY(PREFIX);
1422                 /* Fall through. */
1423         case ITEM_PARAM_MASK:
1424                 index = 2;
1425                 break;
1426         default:
1427                 return -1;
1428         }
1429         /* Nothing else to do if there is no buffer. */
1430         if (!out)
1431                 return len;
1432         if (!out->args.vc.pattern_n)
1433                 return -1;
1434         item = &out->args.vc.pattern[out->args.vc.pattern_n - 1];
1435         data_size = ctx->objdata / 3; /* spec, last, mask */
1436         /* Point to selected object. */
1437         ctx->object = out->args.vc.data + (data_size * index);
1438         if (objmask) {
1439                 ctx->objmask = out->args.vc.data + (data_size * 2); /* mask */
1440                 item->mask = ctx->objmask;
1441         } else
1442                 ctx->objmask = NULL;
1443         /* Update relevant item pointer. */
1444         *((const void **[]){ &item->spec, &item->last, &item->mask })[index] =
1445                 ctx->object;
1446         return len;
1447 }
1448
1449 /** Parse tokens for destroy command. */
1450 static int
1451 parse_destroy(struct context *ctx, const struct token *token,
1452               const char *str, unsigned int len,
1453               void *buf, unsigned int size)
1454 {
1455         struct buffer *out = buf;
1456
1457         /* Token name must match. */
1458         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1459                 return -1;
1460         /* Nothing else to do if there is no buffer. */
1461         if (!out)
1462                 return len;
1463         if (!out->command) {
1464                 if (ctx->curr != DESTROY)
1465                         return -1;
1466                 if (sizeof(*out) > size)
1467                         return -1;
1468                 out->command = ctx->curr;
1469                 ctx->objdata = 0;
1470                 ctx->object = out;
1471                 ctx->objmask = NULL;
1472                 out->args.destroy.rule =
1473                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
1474                                                sizeof(double));
1475                 return len;
1476         }
1477         if (((uint8_t *)(out->args.destroy.rule + out->args.destroy.rule_n) +
1478              sizeof(*out->args.destroy.rule)) > (uint8_t *)out + size)
1479                 return -1;
1480         ctx->objdata = 0;
1481         ctx->object = out->args.destroy.rule + out->args.destroy.rule_n++;
1482         ctx->objmask = NULL;
1483         return len;
1484 }
1485
1486 /** Parse tokens for flush command. */
1487 static int
1488 parse_flush(struct context *ctx, const struct token *token,
1489             const char *str, unsigned int len,
1490             void *buf, unsigned int size)
1491 {
1492         struct buffer *out = buf;
1493
1494         /* Token name must match. */
1495         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1496                 return -1;
1497         /* Nothing else to do if there is no buffer. */
1498         if (!out)
1499                 return len;
1500         if (!out->command) {
1501                 if (ctx->curr != FLUSH)
1502                         return -1;
1503                 if (sizeof(*out) > size)
1504                         return -1;
1505                 out->command = ctx->curr;
1506                 ctx->objdata = 0;
1507                 ctx->object = out;
1508                 ctx->objmask = NULL;
1509         }
1510         return len;
1511 }
1512
1513 /** Parse tokens for query command. */
1514 static int
1515 parse_query(struct context *ctx, const struct token *token,
1516             const char *str, unsigned int len,
1517             void *buf, unsigned int size)
1518 {
1519         struct buffer *out = buf;
1520
1521         /* Token name must match. */
1522         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1523                 return -1;
1524         /* Nothing else to do if there is no buffer. */
1525         if (!out)
1526                 return len;
1527         if (!out->command) {
1528                 if (ctx->curr != QUERY)
1529                         return -1;
1530                 if (sizeof(*out) > size)
1531                         return -1;
1532                 out->command = ctx->curr;
1533                 ctx->objdata = 0;
1534                 ctx->object = out;
1535                 ctx->objmask = NULL;
1536         }
1537         return len;
1538 }
1539
1540 /** Parse action names. */
1541 static int
1542 parse_action(struct context *ctx, const struct token *token,
1543              const char *str, unsigned int len,
1544              void *buf, unsigned int size)
1545 {
1546         struct buffer *out = buf;
1547         const struct arg *arg = pop_args(ctx);
1548         unsigned int i;
1549
1550         (void)size;
1551         /* Argument is expected. */
1552         if (!arg)
1553                 return -1;
1554         /* Parse action name. */
1555         for (i = 0; next_action[i]; ++i) {
1556                 const struct parse_action_priv *priv;
1557
1558                 token = &token_list[next_action[i]];
1559                 if (strncmp(token->name, str, len))
1560                         continue;
1561                 priv = token->priv;
1562                 if (!priv)
1563                         goto error;
1564                 if (out)
1565                         memcpy((uint8_t *)ctx->object + arg->offset,
1566                                &priv->type,
1567                                arg->size);
1568                 return len;
1569         }
1570 error:
1571         push_args(ctx, arg);
1572         return -1;
1573 }
1574
1575 /** Parse tokens for list command. */
1576 static int
1577 parse_list(struct context *ctx, const struct token *token,
1578            const char *str, unsigned int len,
1579            void *buf, unsigned int size)
1580 {
1581         struct buffer *out = buf;
1582
1583         /* Token name must match. */
1584         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1585                 return -1;
1586         /* Nothing else to do if there is no buffer. */
1587         if (!out)
1588                 return len;
1589         if (!out->command) {
1590                 if (ctx->curr != LIST)
1591                         return -1;
1592                 if (sizeof(*out) > size)
1593                         return -1;
1594                 out->command = ctx->curr;
1595                 ctx->objdata = 0;
1596                 ctx->object = out;
1597                 ctx->objmask = NULL;
1598                 out->args.list.group =
1599                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
1600                                                sizeof(double));
1601                 return len;
1602         }
1603         if (((uint8_t *)(out->args.list.group + out->args.list.group_n) +
1604              sizeof(*out->args.list.group)) > (uint8_t *)out + size)
1605                 return -1;
1606         ctx->objdata = 0;
1607         ctx->object = out->args.list.group + out->args.list.group_n++;
1608         ctx->objmask = NULL;
1609         return len;
1610 }
1611
1612 /**
1613  * Parse signed/unsigned integers 8 to 64-bit long.
1614  *
1615  * Last argument (ctx->args) is retrieved to determine integer type and
1616  * storage location.
1617  */
1618 static int
1619 parse_int(struct context *ctx, const struct token *token,
1620           const char *str, unsigned int len,
1621           void *buf, unsigned int size)
1622 {
1623         const struct arg *arg = pop_args(ctx);
1624         uintmax_t u;
1625         char *end;
1626
1627         (void)token;
1628         /* Argument is expected. */
1629         if (!arg)
1630                 return -1;
1631         errno = 0;
1632         u = arg->sign ?
1633                 (uintmax_t)strtoimax(str, &end, 0) :
1634                 strtoumax(str, &end, 0);
1635         if (errno || (size_t)(end - str) != len)
1636                 goto error;
1637         if (!ctx->object)
1638                 return len;
1639         if (arg->mask) {
1640                 if (!arg_entry_bf_fill(ctx->object, u, arg) ||
1641                     !arg_entry_bf_fill(ctx->objmask, -1, arg))
1642                         goto error;
1643                 return len;
1644         }
1645         buf = (uint8_t *)ctx->object + arg->offset;
1646         size = arg->size;
1647 objmask:
1648         switch (size) {
1649         case sizeof(uint8_t):
1650                 *(uint8_t *)buf = u;
1651                 break;
1652         case sizeof(uint16_t):
1653                 *(uint16_t *)buf = arg->hton ? rte_cpu_to_be_16(u) : u;
1654                 break;
1655         case sizeof(uint8_t [3]):
1656 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1657                 if (!arg->hton) {
1658                         ((uint8_t *)buf)[0] = u;
1659                         ((uint8_t *)buf)[1] = u >> 8;
1660                         ((uint8_t *)buf)[2] = u >> 16;
1661                         break;
1662                 }
1663 #endif
1664                 ((uint8_t *)buf)[0] = u >> 16;
1665                 ((uint8_t *)buf)[1] = u >> 8;
1666                 ((uint8_t *)buf)[2] = u;
1667                 break;
1668         case sizeof(uint32_t):
1669                 *(uint32_t *)buf = arg->hton ? rte_cpu_to_be_32(u) : u;
1670                 break;
1671         case sizeof(uint64_t):
1672                 *(uint64_t *)buf = arg->hton ? rte_cpu_to_be_64(u) : u;
1673                 break;
1674         default:
1675                 goto error;
1676         }
1677         if (ctx->objmask && buf != (uint8_t *)ctx->objmask + arg->offset) {
1678                 u = -1;
1679                 buf = (uint8_t *)ctx->objmask + arg->offset;
1680                 goto objmask;
1681         }
1682         return len;
1683 error:
1684         push_args(ctx, arg);
1685         return -1;
1686 }
1687
1688 /**
1689  * Parse a string.
1690  *
1691  * Two arguments (ctx->args) are retrieved from the stack to store data and
1692  * its length (in that order).
1693  */
1694 static int
1695 parse_string(struct context *ctx, const struct token *token,
1696              const char *str, unsigned int len,
1697              void *buf, unsigned int size)
1698 {
1699         const struct arg *arg_data = pop_args(ctx);
1700         const struct arg *arg_len = pop_args(ctx);
1701         char tmp[16]; /* Ought to be enough. */
1702         int ret;
1703
1704         /* Arguments are expected. */
1705         if (!arg_data)
1706                 return -1;
1707         if (!arg_len) {
1708                 push_args(ctx, arg_data);
1709                 return -1;
1710         }
1711         size = arg_data->size;
1712         /* Bit-mask fill is not supported. */
1713         if (arg_data->mask || size < len)
1714                 goto error;
1715         if (!ctx->object)
1716                 return len;
1717         /* Let parse_int() fill length information first. */
1718         ret = snprintf(tmp, sizeof(tmp), "%u", len);
1719         if (ret < 0)
1720                 goto error;
1721         push_args(ctx, arg_len);
1722         ret = parse_int(ctx, token, tmp, ret, NULL, 0);
1723         if (ret < 0) {
1724                 pop_args(ctx);
1725                 goto error;
1726         }
1727         buf = (uint8_t *)ctx->object + arg_data->offset;
1728         /* Output buffer is not necessarily NUL-terminated. */
1729         memcpy(buf, str, len);
1730         memset((uint8_t *)buf + len, 0x55, size - len);
1731         if (ctx->objmask)
1732                 memset((uint8_t *)ctx->objmask + arg_data->offset, 0xff, len);
1733         return len;
1734 error:
1735         push_args(ctx, arg_len);
1736         push_args(ctx, arg_data);
1737         return -1;
1738 }
1739
1740 /**
1741  * Parse a MAC address.
1742  *
1743  * Last argument (ctx->args) is retrieved to determine storage size and
1744  * location.
1745  */
1746 static int
1747 parse_mac_addr(struct context *ctx, const struct token *token,
1748                const char *str, unsigned int len,
1749                void *buf, unsigned int size)
1750 {
1751         const struct arg *arg = pop_args(ctx);
1752         struct ether_addr tmp;
1753         int ret;
1754
1755         (void)token;
1756         /* Argument is expected. */
1757         if (!arg)
1758                 return -1;
1759         size = arg->size;
1760         /* Bit-mask fill is not supported. */
1761         if (arg->mask || size != sizeof(tmp))
1762                 goto error;
1763         ret = cmdline_parse_etheraddr(NULL, str, &tmp, size);
1764         if (ret < 0 || (unsigned int)ret != len)
1765                 goto error;
1766         if (!ctx->object)
1767                 return len;
1768         buf = (uint8_t *)ctx->object + arg->offset;
1769         memcpy(buf, &tmp, size);
1770         if (ctx->objmask)
1771                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
1772         return len;
1773 error:
1774         push_args(ctx, arg);
1775         return -1;
1776 }
1777
1778 /**
1779  * Parse an IPv4 address.
1780  *
1781  * Last argument (ctx->args) is retrieved to determine storage size and
1782  * location.
1783  */
1784 static int
1785 parse_ipv4_addr(struct context *ctx, const struct token *token,
1786                 const char *str, unsigned int len,
1787                 void *buf, unsigned int size)
1788 {
1789         const struct arg *arg = pop_args(ctx);
1790         char str2[len + 1];
1791         struct in_addr tmp;
1792         int ret;
1793
1794         /* Argument is expected. */
1795         if (!arg)
1796                 return -1;
1797         size = arg->size;
1798         /* Bit-mask fill is not supported. */
1799         if (arg->mask || size != sizeof(tmp))
1800                 goto error;
1801         /* Only network endian is supported. */
1802         if (!arg->hton)
1803                 goto error;
1804         memcpy(str2, str, len);
1805         str2[len] = '\0';
1806         ret = inet_pton(AF_INET, str2, &tmp);
1807         if (ret != 1) {
1808                 /* Attempt integer parsing. */
1809                 push_args(ctx, arg);
1810                 return parse_int(ctx, token, str, len, buf, size);
1811         }
1812         if (!ctx->object)
1813                 return len;
1814         buf = (uint8_t *)ctx->object + arg->offset;
1815         memcpy(buf, &tmp, size);
1816         if (ctx->objmask)
1817                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
1818         return len;
1819 error:
1820         push_args(ctx, arg);
1821         return -1;
1822 }
1823
1824 /**
1825  * Parse an IPv6 address.
1826  *
1827  * Last argument (ctx->args) is retrieved to determine storage size and
1828  * location.
1829  */
1830 static int
1831 parse_ipv6_addr(struct context *ctx, const struct token *token,
1832                 const char *str, unsigned int len,
1833                 void *buf, unsigned int size)
1834 {
1835         const struct arg *arg = pop_args(ctx);
1836         char str2[len + 1];
1837         struct in6_addr tmp;
1838         int ret;
1839
1840         (void)token;
1841         /* Argument is expected. */
1842         if (!arg)
1843                 return -1;
1844         size = arg->size;
1845         /* Bit-mask fill is not supported. */
1846         if (arg->mask || size != sizeof(tmp))
1847                 goto error;
1848         /* Only network endian is supported. */
1849         if (!arg->hton)
1850                 goto error;
1851         memcpy(str2, str, len);
1852         str2[len] = '\0';
1853         ret = inet_pton(AF_INET6, str2, &tmp);
1854         if (ret != 1)
1855                 goto error;
1856         if (!ctx->object)
1857                 return len;
1858         buf = (uint8_t *)ctx->object + arg->offset;
1859         memcpy(buf, &tmp, size);
1860         if (ctx->objmask)
1861                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
1862         return len;
1863 error:
1864         push_args(ctx, arg);
1865         return -1;
1866 }
1867
1868 /** Boolean values (even indices stand for false). */
1869 static const char *const boolean_name[] = {
1870         "0", "1",
1871         "false", "true",
1872         "no", "yes",
1873         "N", "Y",
1874         NULL,
1875 };
1876
1877 /**
1878  * Parse a boolean value.
1879  *
1880  * Last argument (ctx->args) is retrieved to determine storage size and
1881  * location.
1882  */
1883 static int
1884 parse_boolean(struct context *ctx, const struct token *token,
1885               const char *str, unsigned int len,
1886               void *buf, unsigned int size)
1887 {
1888         const struct arg *arg = pop_args(ctx);
1889         unsigned int i;
1890         int ret;
1891
1892         /* Argument is expected. */
1893         if (!arg)
1894                 return -1;
1895         for (i = 0; boolean_name[i]; ++i)
1896                 if (!strncmp(str, boolean_name[i], len))
1897                         break;
1898         /* Process token as integer. */
1899         if (boolean_name[i])
1900                 str = i & 1 ? "1" : "0";
1901         push_args(ctx, arg);
1902         ret = parse_int(ctx, token, str, strlen(str), buf, size);
1903         return ret > 0 ? (int)len : ret;
1904 }
1905
1906 /** Parse port and update context. */
1907 static int
1908 parse_port(struct context *ctx, const struct token *token,
1909            const char *str, unsigned int len,
1910            void *buf, unsigned int size)
1911 {
1912         struct buffer *out = &(struct buffer){ .port = 0 };
1913         int ret;
1914
1915         if (buf)
1916                 out = buf;
1917         else {
1918                 ctx->objdata = 0;
1919                 ctx->object = out;
1920                 ctx->objmask = NULL;
1921                 size = sizeof(*out);
1922         }
1923         ret = parse_int(ctx, token, str, len, out, size);
1924         if (ret >= 0)
1925                 ctx->port = out->port;
1926         if (!buf)
1927                 ctx->object = NULL;
1928         return ret;
1929 }
1930
1931 /** No completion. */
1932 static int
1933 comp_none(struct context *ctx, const struct token *token,
1934           unsigned int ent, char *buf, unsigned int size)
1935 {
1936         (void)ctx;
1937         (void)token;
1938         (void)ent;
1939         (void)buf;
1940         (void)size;
1941         return 0;
1942 }
1943
1944 /** Complete boolean values. */
1945 static int
1946 comp_boolean(struct context *ctx, const struct token *token,
1947              unsigned int ent, char *buf, unsigned int size)
1948 {
1949         unsigned int i;
1950
1951         (void)ctx;
1952         (void)token;
1953         for (i = 0; boolean_name[i]; ++i)
1954                 if (buf && i == ent)
1955                         return snprintf(buf, size, "%s", boolean_name[i]);
1956         if (buf)
1957                 return -1;
1958         return i;
1959 }
1960
1961 /** Complete action names. */
1962 static int
1963 comp_action(struct context *ctx, const struct token *token,
1964             unsigned int ent, char *buf, unsigned int size)
1965 {
1966         unsigned int i;
1967
1968         (void)ctx;
1969         (void)token;
1970         for (i = 0; next_action[i]; ++i)
1971                 if (buf && i == ent)
1972                         return snprintf(buf, size, "%s",
1973                                         token_list[next_action[i]].name);
1974         if (buf)
1975                 return -1;
1976         return i;
1977 }
1978
1979 /** Complete available ports. */
1980 static int
1981 comp_port(struct context *ctx, const struct token *token,
1982           unsigned int ent, char *buf, unsigned int size)
1983 {
1984         unsigned int i = 0;
1985         portid_t p;
1986
1987         (void)ctx;
1988         (void)token;
1989         FOREACH_PORT(p, ports) {
1990                 if (buf && i == ent)
1991                         return snprintf(buf, size, "%u", p);
1992                 ++i;
1993         }
1994         if (buf)
1995                 return -1;
1996         return i;
1997 }
1998
1999 /** Complete available rule IDs. */
2000 static int
2001 comp_rule_id(struct context *ctx, const struct token *token,
2002              unsigned int ent, char *buf, unsigned int size)
2003 {
2004         unsigned int i = 0;
2005         struct rte_port *port;
2006         struct port_flow *pf;
2007
2008         (void)token;
2009         if (port_id_is_invalid(ctx->port, DISABLED_WARN) ||
2010             ctx->port == (uint16_t)RTE_PORT_ALL)
2011                 return -1;
2012         port = &ports[ctx->port];
2013         for (pf = port->flow_list; pf != NULL; pf = pf->next) {
2014                 if (buf && i == ent)
2015                         return snprintf(buf, size, "%u", pf->id);
2016                 ++i;
2017         }
2018         if (buf)
2019                 return -1;
2020         return i;
2021 }
2022
2023 /** Internal context. */
2024 static struct context cmd_flow_context;
2025
2026 /** Global parser instance (cmdline API). */
2027 cmdline_parse_inst_t cmd_flow;
2028
2029 /** Initialize context. */
2030 static void
2031 cmd_flow_context_init(struct context *ctx)
2032 {
2033         /* A full memset() is not necessary. */
2034         ctx->curr = ZERO;
2035         ctx->prev = ZERO;
2036         ctx->next_num = 0;
2037         ctx->args_num = 0;
2038         ctx->reparse = 0;
2039         ctx->eol = 0;
2040         ctx->last = 0;
2041         ctx->port = 0;
2042         ctx->objdata = 0;
2043         ctx->object = NULL;
2044         ctx->objmask = NULL;
2045 }
2046
2047 /** Parse a token (cmdline API). */
2048 static int
2049 cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result,
2050                unsigned int size)
2051 {
2052         struct context *ctx = &cmd_flow_context;
2053         const struct token *token;
2054         const enum index *list;
2055         int len;
2056         int i;
2057
2058         (void)hdr;
2059         /* Restart as requested. */
2060         if (ctx->reparse)
2061                 cmd_flow_context_init(ctx);
2062         token = &token_list[ctx->curr];
2063         /* Check argument length. */
2064         ctx->eol = 0;
2065         ctx->last = 1;
2066         for (len = 0; src[len]; ++len)
2067                 if (src[len] == '#' || isspace(src[len]))
2068                         break;
2069         if (!len)
2070                 return -1;
2071         /* Last argument and EOL detection. */
2072         for (i = len; src[i]; ++i)
2073                 if (src[i] == '#' || src[i] == '\r' || src[i] == '\n')
2074                         break;
2075                 else if (!isspace(src[i])) {
2076                         ctx->last = 0;
2077                         break;
2078                 }
2079         for (; src[i]; ++i)
2080                 if (src[i] == '\r' || src[i] == '\n') {
2081                         ctx->eol = 1;
2082                         break;
2083                 }
2084         /* Initialize context if necessary. */
2085         if (!ctx->next_num) {
2086                 if (!token->next)
2087                         return 0;
2088                 ctx->next[ctx->next_num++] = token->next[0];
2089         }
2090         /* Process argument through candidates. */
2091         ctx->prev = ctx->curr;
2092         list = ctx->next[ctx->next_num - 1];
2093         for (i = 0; list[i]; ++i) {
2094                 const struct token *next = &token_list[list[i]];
2095                 int tmp;
2096
2097                 ctx->curr = list[i];
2098                 if (next->call)
2099                         tmp = next->call(ctx, next, src, len, result, size);
2100                 else
2101                         tmp = parse_default(ctx, next, src, len, result, size);
2102                 if (tmp == -1 || tmp != len)
2103                         continue;
2104                 token = next;
2105                 break;
2106         }
2107         if (!list[i])
2108                 return -1;
2109         --ctx->next_num;
2110         /* Push subsequent tokens if any. */
2111         if (token->next)
2112                 for (i = 0; token->next[i]; ++i) {
2113                         if (ctx->next_num == RTE_DIM(ctx->next))
2114                                 return -1;
2115                         ctx->next[ctx->next_num++] = token->next[i];
2116                 }
2117         /* Push arguments if any. */
2118         if (token->args)
2119                 for (i = 0; token->args[i]; ++i) {
2120                         if (ctx->args_num == RTE_DIM(ctx->args))
2121                                 return -1;
2122                         ctx->args[ctx->args_num++] = token->args[i];
2123                 }
2124         return len;
2125 }
2126
2127 /** Return number of completion entries (cmdline API). */
2128 static int
2129 cmd_flow_complete_get_nb(cmdline_parse_token_hdr_t *hdr)
2130 {
2131         struct context *ctx = &cmd_flow_context;
2132         const struct token *token = &token_list[ctx->curr];
2133         const enum index *list;
2134         int i;
2135
2136         (void)hdr;
2137         /* Tell cmd_flow_parse() that context must be reinitialized. */
2138         ctx->reparse = 1;
2139         /* Count number of tokens in current list. */
2140         if (ctx->next_num)
2141                 list = ctx->next[ctx->next_num - 1];
2142         else
2143                 list = token->next[0];
2144         for (i = 0; list[i]; ++i)
2145                 ;
2146         if (!i)
2147                 return 0;
2148         /*
2149          * If there is a single token, use its completion callback, otherwise
2150          * return the number of entries.
2151          */
2152         token = &token_list[list[0]];
2153         if (i == 1 && token->comp) {
2154                 /* Save index for cmd_flow_get_help(). */
2155                 ctx->prev = list[0];
2156                 return token->comp(ctx, token, 0, NULL, 0);
2157         }
2158         return i;
2159 }
2160
2161 /** Return a completion entry (cmdline API). */
2162 static int
2163 cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,
2164                           char *dst, unsigned int size)
2165 {
2166         struct context *ctx = &cmd_flow_context;
2167         const struct token *token = &token_list[ctx->curr];
2168         const enum index *list;
2169         int i;
2170
2171         (void)hdr;
2172         /* Tell cmd_flow_parse() that context must be reinitialized. */
2173         ctx->reparse = 1;
2174         /* Count number of tokens in current list. */
2175         if (ctx->next_num)
2176                 list = ctx->next[ctx->next_num - 1];
2177         else
2178                 list = token->next[0];
2179         for (i = 0; list[i]; ++i)
2180                 ;
2181         if (!i)
2182                 return -1;
2183         /* If there is a single token, use its completion callback. */
2184         token = &token_list[list[0]];
2185         if (i == 1 && token->comp) {
2186                 /* Save index for cmd_flow_get_help(). */
2187                 ctx->prev = list[0];
2188                 return token->comp(ctx, token, index, dst, size) < 0 ? -1 : 0;
2189         }
2190         /* Otherwise make sure the index is valid and use defaults. */
2191         if (index >= i)
2192                 return -1;
2193         token = &token_list[list[index]];
2194         snprintf(dst, size, "%s", token->name);
2195         /* Save index for cmd_flow_get_help(). */
2196         ctx->prev = list[index];
2197         return 0;
2198 }
2199
2200 /** Populate help strings for current token (cmdline API). */
2201 static int
2202 cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
2203 {
2204         struct context *ctx = &cmd_flow_context;
2205         const struct token *token = &token_list[ctx->prev];
2206
2207         (void)hdr;
2208         /* Tell cmd_flow_parse() that context must be reinitialized. */
2209         ctx->reparse = 1;
2210         if (!size)
2211                 return -1;
2212         /* Set token type and update global help with details. */
2213         snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
2214         if (token->help)
2215                 cmd_flow.help_str = token->help;
2216         else
2217                 cmd_flow.help_str = token->name;
2218         return 0;
2219 }
2220
2221 /** Token definition template (cmdline API). */
2222 static struct cmdline_token_hdr cmd_flow_token_hdr = {
2223         .ops = &(struct cmdline_token_ops){
2224                 .parse = cmd_flow_parse,
2225                 .complete_get_nb = cmd_flow_complete_get_nb,
2226                 .complete_get_elt = cmd_flow_complete_get_elt,
2227                 .get_help = cmd_flow_get_help,
2228         },
2229         .offset = 0,
2230 };
2231
2232 /** Populate the next dynamic token. */
2233 static void
2234 cmd_flow_tok(cmdline_parse_token_hdr_t **hdr,
2235              cmdline_parse_token_hdr_t *(*hdrs)[])
2236 {
2237         struct context *ctx = &cmd_flow_context;
2238
2239         /* Always reinitialize context before requesting the first token. */
2240         if (!(hdr - *hdrs))
2241                 cmd_flow_context_init(ctx);
2242         /* Return NULL when no more tokens are expected. */
2243         if (!ctx->next_num && ctx->curr) {
2244                 *hdr = NULL;
2245                 return;
2246         }
2247         /* Determine if command should end here. */
2248         if (ctx->eol && ctx->last && ctx->next_num) {
2249                 const enum index *list = ctx->next[ctx->next_num - 1];
2250                 int i;
2251
2252                 for (i = 0; list[i]; ++i) {
2253                         if (list[i] != END)
2254                                 continue;
2255                         *hdr = NULL;
2256                         return;
2257                 }
2258         }
2259         *hdr = &cmd_flow_token_hdr;
2260 }
2261
2262 /** Dispatch parsed buffer to function calls. */
2263 static void
2264 cmd_flow_parsed(const struct buffer *in)
2265 {
2266         switch (in->command) {
2267         case VALIDATE:
2268                 port_flow_validate(in->port, &in->args.vc.attr,
2269                                    in->args.vc.pattern, in->args.vc.actions);
2270                 break;
2271         case CREATE:
2272                 port_flow_create(in->port, &in->args.vc.attr,
2273                                  in->args.vc.pattern, in->args.vc.actions);
2274                 break;
2275         case DESTROY:
2276                 port_flow_destroy(in->port, in->args.destroy.rule_n,
2277                                   in->args.destroy.rule);
2278                 break;
2279         case FLUSH:
2280                 port_flow_flush(in->port);
2281                 break;
2282         case QUERY:
2283                 port_flow_query(in->port, in->args.query.rule,
2284                                 in->args.query.action);
2285                 break;
2286         case LIST:
2287                 port_flow_list(in->port, in->args.list.group_n,
2288                                in->args.list.group);
2289                 break;
2290         default:
2291                 break;
2292         }
2293 }
2294
2295 /** Token generator and output processing callback (cmdline API). */
2296 static void
2297 cmd_flow_cb(void *arg0, struct cmdline *cl, void *arg2)
2298 {
2299         if (cl == NULL)
2300                 cmd_flow_tok(arg0, arg2);
2301         else
2302                 cmd_flow_parsed(arg0);
2303 }
2304
2305 /** Global parser instance (cmdline API). */
2306 cmdline_parse_inst_t cmd_flow = {
2307         .f = cmd_flow_cb,
2308         .data = NULL, /**< Unused. */
2309         .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
2310         .tokens = {
2311                 NULL,
2312         }, /**< Tokens are returned by cmd_flow_tok(). */
2313 };