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