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