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