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