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