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