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