app/testpmd: support raw encap/decap actions
[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_string_fns.h>
17 #include <rte_common.h>
18 #include <rte_ethdev.h>
19 #include <rte_byteorder.h>
20 #include <cmdline_parse.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         START_SET,
31         END_SET,
32
33         /* Common tokens. */
34         INTEGER,
35         UNSIGNED,
36         PREFIX,
37         BOOLEAN,
38         STRING,
39         HEX,
40         MAC_ADDR,
41         IPV4_ADDR,
42         IPV6_ADDR,
43         RULE_ID,
44         PORT_ID,
45         GROUP_ID,
46         PRIORITY_LEVEL,
47
48         /* Top-level command. */
49         SET,
50         /* Sub-leve commands. */
51         SET_RAW_ENCAP,
52         SET_RAW_DECAP,
53
54         /* Top-level command. */
55         FLOW,
56         /* Sub-level commands. */
57         VALIDATE,
58         CREATE,
59         DESTROY,
60         FLUSH,
61         QUERY,
62         LIST,
63         ISOLATE,
64
65         /* Destroy arguments. */
66         DESTROY_RULE,
67
68         /* Query arguments. */
69         QUERY_ACTION,
70
71         /* List arguments. */
72         LIST_GROUP,
73
74         /* Validate/create arguments. */
75         GROUP,
76         PRIORITY,
77         INGRESS,
78         EGRESS,
79         TRANSFER,
80
81         /* Validate/create pattern. */
82         PATTERN,
83         ITEM_PARAM_IS,
84         ITEM_PARAM_SPEC,
85         ITEM_PARAM_LAST,
86         ITEM_PARAM_MASK,
87         ITEM_PARAM_PREFIX,
88         ITEM_NEXT,
89         ITEM_END,
90         ITEM_VOID,
91         ITEM_INVERT,
92         ITEM_ANY,
93         ITEM_ANY_NUM,
94         ITEM_PF,
95         ITEM_VF,
96         ITEM_VF_ID,
97         ITEM_PHY_PORT,
98         ITEM_PHY_PORT_INDEX,
99         ITEM_PORT_ID,
100         ITEM_PORT_ID_ID,
101         ITEM_MARK,
102         ITEM_MARK_ID,
103         ITEM_RAW,
104         ITEM_RAW_RELATIVE,
105         ITEM_RAW_SEARCH,
106         ITEM_RAW_OFFSET,
107         ITEM_RAW_LIMIT,
108         ITEM_RAW_PATTERN,
109         ITEM_ETH,
110         ITEM_ETH_DST,
111         ITEM_ETH_SRC,
112         ITEM_ETH_TYPE,
113         ITEM_VLAN,
114         ITEM_VLAN_TCI,
115         ITEM_VLAN_PCP,
116         ITEM_VLAN_DEI,
117         ITEM_VLAN_VID,
118         ITEM_VLAN_INNER_TYPE,
119         ITEM_IPV4,
120         ITEM_IPV4_TOS,
121         ITEM_IPV4_TTL,
122         ITEM_IPV4_PROTO,
123         ITEM_IPV4_SRC,
124         ITEM_IPV4_DST,
125         ITEM_IPV6,
126         ITEM_IPV6_TC,
127         ITEM_IPV6_FLOW,
128         ITEM_IPV6_PROTO,
129         ITEM_IPV6_HOP,
130         ITEM_IPV6_SRC,
131         ITEM_IPV6_DST,
132         ITEM_ICMP,
133         ITEM_ICMP_TYPE,
134         ITEM_ICMP_CODE,
135         ITEM_UDP,
136         ITEM_UDP_SRC,
137         ITEM_UDP_DST,
138         ITEM_TCP,
139         ITEM_TCP_SRC,
140         ITEM_TCP_DST,
141         ITEM_TCP_FLAGS,
142         ITEM_SCTP,
143         ITEM_SCTP_SRC,
144         ITEM_SCTP_DST,
145         ITEM_SCTP_TAG,
146         ITEM_SCTP_CKSUM,
147         ITEM_VXLAN,
148         ITEM_VXLAN_VNI,
149         ITEM_E_TAG,
150         ITEM_E_TAG_GRP_ECID_B,
151         ITEM_NVGRE,
152         ITEM_NVGRE_TNI,
153         ITEM_MPLS,
154         ITEM_MPLS_LABEL,
155         ITEM_GRE,
156         ITEM_GRE_PROTO,
157         ITEM_GRE_C_RSVD0_VER,
158         ITEM_GRE_C_BIT,
159         ITEM_GRE_K_BIT,
160         ITEM_GRE_S_BIT,
161         ITEM_FUZZY,
162         ITEM_FUZZY_THRESH,
163         ITEM_GTP,
164         ITEM_GTP_TEID,
165         ITEM_GTPC,
166         ITEM_GTPU,
167         ITEM_GENEVE,
168         ITEM_GENEVE_VNI,
169         ITEM_GENEVE_PROTO,
170         ITEM_VXLAN_GPE,
171         ITEM_VXLAN_GPE_VNI,
172         ITEM_ARP_ETH_IPV4,
173         ITEM_ARP_ETH_IPV4_SHA,
174         ITEM_ARP_ETH_IPV4_SPA,
175         ITEM_ARP_ETH_IPV4_THA,
176         ITEM_ARP_ETH_IPV4_TPA,
177         ITEM_IPV6_EXT,
178         ITEM_IPV6_EXT_NEXT_HDR,
179         ITEM_ICMP6,
180         ITEM_ICMP6_TYPE,
181         ITEM_ICMP6_CODE,
182         ITEM_ICMP6_ND_NS,
183         ITEM_ICMP6_ND_NS_TARGET_ADDR,
184         ITEM_ICMP6_ND_NA,
185         ITEM_ICMP6_ND_NA_TARGET_ADDR,
186         ITEM_ICMP6_ND_OPT,
187         ITEM_ICMP6_ND_OPT_TYPE,
188         ITEM_ICMP6_ND_OPT_SLA_ETH,
189         ITEM_ICMP6_ND_OPT_SLA_ETH_SLA,
190         ITEM_ICMP6_ND_OPT_TLA_ETH,
191         ITEM_ICMP6_ND_OPT_TLA_ETH_TLA,
192         ITEM_META,
193         ITEM_META_DATA,
194         ITEM_GRE_KEY,
195         ITEM_GRE_KEY_VALUE,
196
197         /* Validate/create actions. */
198         ACTIONS,
199         ACTION_NEXT,
200         ACTION_END,
201         ACTION_VOID,
202         ACTION_PASSTHRU,
203         ACTION_JUMP,
204         ACTION_JUMP_GROUP,
205         ACTION_MARK,
206         ACTION_MARK_ID,
207         ACTION_FLAG,
208         ACTION_QUEUE,
209         ACTION_QUEUE_INDEX,
210         ACTION_DROP,
211         ACTION_COUNT,
212         ACTION_COUNT_SHARED,
213         ACTION_COUNT_ID,
214         ACTION_RSS,
215         ACTION_RSS_FUNC,
216         ACTION_RSS_LEVEL,
217         ACTION_RSS_FUNC_DEFAULT,
218         ACTION_RSS_FUNC_TOEPLITZ,
219         ACTION_RSS_FUNC_SIMPLE_XOR,
220         ACTION_RSS_TYPES,
221         ACTION_RSS_TYPE,
222         ACTION_RSS_KEY,
223         ACTION_RSS_KEY_LEN,
224         ACTION_RSS_QUEUES,
225         ACTION_RSS_QUEUE,
226         ACTION_PF,
227         ACTION_VF,
228         ACTION_VF_ORIGINAL,
229         ACTION_VF_ID,
230         ACTION_PHY_PORT,
231         ACTION_PHY_PORT_ORIGINAL,
232         ACTION_PHY_PORT_INDEX,
233         ACTION_PORT_ID,
234         ACTION_PORT_ID_ORIGINAL,
235         ACTION_PORT_ID_ID,
236         ACTION_METER,
237         ACTION_METER_ID,
238         ACTION_OF_SET_MPLS_TTL,
239         ACTION_OF_SET_MPLS_TTL_MPLS_TTL,
240         ACTION_OF_DEC_MPLS_TTL,
241         ACTION_OF_SET_NW_TTL,
242         ACTION_OF_SET_NW_TTL_NW_TTL,
243         ACTION_OF_DEC_NW_TTL,
244         ACTION_OF_COPY_TTL_OUT,
245         ACTION_OF_COPY_TTL_IN,
246         ACTION_OF_POP_VLAN,
247         ACTION_OF_PUSH_VLAN,
248         ACTION_OF_PUSH_VLAN_ETHERTYPE,
249         ACTION_OF_SET_VLAN_VID,
250         ACTION_OF_SET_VLAN_VID_VLAN_VID,
251         ACTION_OF_SET_VLAN_PCP,
252         ACTION_OF_SET_VLAN_PCP_VLAN_PCP,
253         ACTION_OF_POP_MPLS,
254         ACTION_OF_POP_MPLS_ETHERTYPE,
255         ACTION_OF_PUSH_MPLS,
256         ACTION_OF_PUSH_MPLS_ETHERTYPE,
257         ACTION_VXLAN_ENCAP,
258         ACTION_VXLAN_DECAP,
259         ACTION_NVGRE_ENCAP,
260         ACTION_NVGRE_DECAP,
261         ACTION_L2_ENCAP,
262         ACTION_L2_DECAP,
263         ACTION_MPLSOGRE_ENCAP,
264         ACTION_MPLSOGRE_DECAP,
265         ACTION_MPLSOUDP_ENCAP,
266         ACTION_MPLSOUDP_DECAP,
267         ACTION_SET_IPV4_SRC,
268         ACTION_SET_IPV4_SRC_IPV4_SRC,
269         ACTION_SET_IPV4_DST,
270         ACTION_SET_IPV4_DST_IPV4_DST,
271         ACTION_SET_IPV6_SRC,
272         ACTION_SET_IPV6_SRC_IPV6_SRC,
273         ACTION_SET_IPV6_DST,
274         ACTION_SET_IPV6_DST_IPV6_DST,
275         ACTION_SET_TP_SRC,
276         ACTION_SET_TP_SRC_TP_SRC,
277         ACTION_SET_TP_DST,
278         ACTION_SET_TP_DST_TP_DST,
279         ACTION_MAC_SWAP,
280         ACTION_DEC_TTL,
281         ACTION_SET_TTL,
282         ACTION_SET_TTL_TTL,
283         ACTION_SET_MAC_SRC,
284         ACTION_SET_MAC_SRC_MAC_SRC,
285         ACTION_SET_MAC_DST,
286         ACTION_SET_MAC_DST_MAC_DST,
287         ACTION_INC_TCP_SEQ,
288         ACTION_INC_TCP_SEQ_VALUE,
289         ACTION_DEC_TCP_SEQ,
290         ACTION_DEC_TCP_SEQ_VALUE,
291         ACTION_INC_TCP_ACK,
292         ACTION_INC_TCP_ACK_VALUE,
293         ACTION_DEC_TCP_ACK,
294         ACTION_DEC_TCP_ACK_VALUE,
295         ACTION_RAW_ENCAP,
296         ACTION_RAW_DECAP,
297 };
298
299 /** Maximum size for pattern in struct rte_flow_item_raw. */
300 #define ITEM_RAW_PATTERN_SIZE 40
301
302 /** Storage size for struct rte_flow_item_raw including pattern. */
303 #define ITEM_RAW_SIZE \
304         (sizeof(struct rte_flow_item_raw) + ITEM_RAW_PATTERN_SIZE)
305
306 /** Maximum number of queue indices in struct rte_flow_action_rss. */
307 #define ACTION_RSS_QUEUE_NUM 32
308
309 /** Storage for struct rte_flow_action_rss including external data. */
310 struct action_rss_data {
311         struct rte_flow_action_rss conf;
312         uint8_t key[RSS_HASH_KEY_LENGTH];
313         uint16_t queue[ACTION_RSS_QUEUE_NUM];
314 };
315
316 /** Maximum number of items in struct rte_flow_action_vxlan_encap. */
317 #define ACTION_VXLAN_ENCAP_ITEMS_NUM 6
318
319 #define ACTION_RAW_ENCAP_MAX_DATA 128
320
321 /** Storage for struct rte_flow_action_raw_encap. */
322 struct raw_encap_conf {
323         uint8_t data[ACTION_RAW_ENCAP_MAX_DATA];
324         uint8_t preserve[ACTION_RAW_ENCAP_MAX_DATA];
325         size_t size;
326 };
327
328 struct raw_encap_conf raw_encap_conf = {.size = 0};
329
330 /** Storage for struct rte_flow_action_raw_decap. */
331 struct raw_decap_conf {
332         uint8_t data[ACTION_RAW_ENCAP_MAX_DATA];
333         size_t size;
334 };
335
336 struct raw_decap_conf raw_decap_conf = {.size = 0};
337
338 /** Storage for struct rte_flow_action_vxlan_encap including external data. */
339 struct action_vxlan_encap_data {
340         struct rte_flow_action_vxlan_encap conf;
341         struct rte_flow_item items[ACTION_VXLAN_ENCAP_ITEMS_NUM];
342         struct rte_flow_item_eth item_eth;
343         struct rte_flow_item_vlan item_vlan;
344         union {
345                 struct rte_flow_item_ipv4 item_ipv4;
346                 struct rte_flow_item_ipv6 item_ipv6;
347         };
348         struct rte_flow_item_udp item_udp;
349         struct rte_flow_item_vxlan item_vxlan;
350 };
351
352 /** Maximum number of items in struct rte_flow_action_nvgre_encap. */
353 #define ACTION_NVGRE_ENCAP_ITEMS_NUM 5
354
355 /** Storage for struct rte_flow_action_nvgre_encap including external data. */
356 struct action_nvgre_encap_data {
357         struct rte_flow_action_nvgre_encap conf;
358         struct rte_flow_item items[ACTION_NVGRE_ENCAP_ITEMS_NUM];
359         struct rte_flow_item_eth item_eth;
360         struct rte_flow_item_vlan item_vlan;
361         union {
362                 struct rte_flow_item_ipv4 item_ipv4;
363                 struct rte_flow_item_ipv6 item_ipv6;
364         };
365         struct rte_flow_item_nvgre item_nvgre;
366 };
367
368 /** Maximum data size in struct rte_flow_action_raw_encap. */
369 #define ACTION_RAW_ENCAP_MAX_DATA 128
370
371 /** Storage for struct rte_flow_action_raw_encap including external data. */
372 struct action_raw_encap_data {
373         struct rte_flow_action_raw_encap conf;
374         uint8_t data[ACTION_RAW_ENCAP_MAX_DATA];
375         uint8_t preserve[ACTION_RAW_ENCAP_MAX_DATA];
376 };
377
378 /** Storage for struct rte_flow_action_raw_decap including external data. */
379 struct action_raw_decap_data {
380         struct rte_flow_action_raw_decap conf;
381         uint8_t data[ACTION_RAW_ENCAP_MAX_DATA];
382 };
383
384 /** Maximum number of subsequent tokens and arguments on the stack. */
385 #define CTX_STACK_SIZE 16
386
387 /** Parser context. */
388 struct context {
389         /** Stack of subsequent token lists to process. */
390         const enum index *next[CTX_STACK_SIZE];
391         /** Arguments for stacked tokens. */
392         const void *args[CTX_STACK_SIZE];
393         enum index curr; /**< Current token index. */
394         enum index prev; /**< Index of the last token seen. */
395         int next_num; /**< Number of entries in next[]. */
396         int args_num; /**< Number of entries in args[]. */
397         uint32_t eol:1; /**< EOL has been detected. */
398         uint32_t last:1; /**< No more arguments. */
399         portid_t port; /**< Current port ID (for completions). */
400         uint32_t objdata; /**< Object-specific data. */
401         void *object; /**< Address of current object for relative offsets. */
402         void *objmask; /**< Object a full mask must be written to. */
403 };
404
405 /** Token argument. */
406 struct arg {
407         uint32_t hton:1; /**< Use network byte ordering. */
408         uint32_t sign:1; /**< Value is signed. */
409         uint32_t bounded:1; /**< Value is bounded. */
410         uintmax_t min; /**< Minimum value if bounded. */
411         uintmax_t max; /**< Maximum value if bounded. */
412         uint32_t offset; /**< Relative offset from ctx->object. */
413         uint32_t size; /**< Field size. */
414         const uint8_t *mask; /**< Bit-mask to use instead of offset/size. */
415 };
416
417 /** Parser token definition. */
418 struct token {
419         /** Type displayed during completion (defaults to "TOKEN"). */
420         const char *type;
421         /** Help displayed during completion (defaults to token name). */
422         const char *help;
423         /** Private data used by parser functions. */
424         const void *priv;
425         /**
426          * Lists of subsequent tokens to push on the stack. Each call to the
427          * parser consumes the last entry of that stack.
428          */
429         const enum index *const *next;
430         /** Arguments stack for subsequent tokens that need them. */
431         const struct arg *const *args;
432         /**
433          * Token-processing callback, returns -1 in case of error, the
434          * length of the matched string otherwise. If NULL, attempts to
435          * match the token name.
436          *
437          * If buf is not NULL, the result should be stored in it according
438          * to context. An error is returned if not large enough.
439          */
440         int (*call)(struct context *ctx, const struct token *token,
441                     const char *str, unsigned int len,
442                     void *buf, unsigned int size);
443         /**
444          * Callback that provides possible values for this token, used for
445          * completion. Returns -1 in case of error, the number of possible
446          * values otherwise. If NULL, the token name is used.
447          *
448          * If buf is not NULL, entry index ent is written to buf and the
449          * full length of the entry is returned (same behavior as
450          * snprintf()).
451          */
452         int (*comp)(struct context *ctx, const struct token *token,
453                     unsigned int ent, char *buf, unsigned int size);
454         /** Mandatory token name, no default value. */
455         const char *name;
456 };
457
458 /** Static initializer for the next field. */
459 #define NEXT(...) (const enum index *const []){ __VA_ARGS__, NULL, }
460
461 /** Static initializer for a NEXT() entry. */
462 #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, }
463
464 /** Static initializer for the args field. */
465 #define ARGS(...) (const struct arg *const []){ __VA_ARGS__, NULL, }
466
467 /** Static initializer for ARGS() to target a field. */
468 #define ARGS_ENTRY(s, f) \
469         (&(const struct arg){ \
470                 .offset = offsetof(s, f), \
471                 .size = sizeof(((s *)0)->f), \
472         })
473
474 /** Static initializer for ARGS() to target a bit-field. */
475 #define ARGS_ENTRY_BF(s, f, b) \
476         (&(const struct arg){ \
477                 .size = sizeof(s), \
478                 .mask = (const void *)&(const s){ .f = (1 << (b)) - 1 }, \
479         })
480
481 /** Static initializer for ARGS() to target an arbitrary bit-mask. */
482 #define ARGS_ENTRY_MASK(s, f, m) \
483         (&(const struct arg){ \
484                 .offset = offsetof(s, f), \
485                 .size = sizeof(((s *)0)->f), \
486                 .mask = (const void *)(m), \
487         })
488
489 /** Same as ARGS_ENTRY_MASK() using network byte ordering for the value. */
490 #define ARGS_ENTRY_MASK_HTON(s, f, m) \
491         (&(const struct arg){ \
492                 .hton = 1, \
493                 .offset = offsetof(s, f), \
494                 .size = sizeof(((s *)0)->f), \
495                 .mask = (const void *)(m), \
496         })
497
498 /** Static initializer for ARGS() to target a pointer. */
499 #define ARGS_ENTRY_PTR(s, f) \
500         (&(const struct arg){ \
501                 .size = sizeof(*((s *)0)->f), \
502         })
503
504 /** Static initializer for ARGS() with arbitrary offset and size. */
505 #define ARGS_ENTRY_ARB(o, s) \
506         (&(const struct arg){ \
507                 .offset = (o), \
508                 .size = (s), \
509         })
510
511 /** Same as ARGS_ENTRY_ARB() with bounded values. */
512 #define ARGS_ENTRY_ARB_BOUNDED(o, s, i, a) \
513         (&(const struct arg){ \
514                 .bounded = 1, \
515                 .min = (i), \
516                 .max = (a), \
517                 .offset = (o), \
518                 .size = (s), \
519         })
520
521 /** Same as ARGS_ENTRY() using network byte ordering. */
522 #define ARGS_ENTRY_HTON(s, f) \
523         (&(const struct arg){ \
524                 .hton = 1, \
525                 .offset = offsetof(s, f), \
526                 .size = sizeof(((s *)0)->f), \
527         })
528
529 /** Same as ARGS_ENTRY_HTON() for a single argument, without structure. */
530 #define ARG_ENTRY_HTON(s) \
531         (&(const struct arg){ \
532                 .hton = 1, \
533                 .offset = 0, \
534                 .size = sizeof(s), \
535         })
536
537 /** Parser output buffer layout expected by cmd_flow_parsed(). */
538 struct buffer {
539         enum index command; /**< Flow command. */
540         portid_t port; /**< Affected port ID. */
541         union {
542                 struct {
543                         struct rte_flow_attr attr;
544                         struct rte_flow_item *pattern;
545                         struct rte_flow_action *actions;
546                         uint32_t pattern_n;
547                         uint32_t actions_n;
548                         uint8_t *data;
549                 } vc; /**< Validate/create arguments. */
550                 struct {
551                         uint32_t *rule;
552                         uint32_t rule_n;
553                 } destroy; /**< Destroy arguments. */
554                 struct {
555                         uint32_t rule;
556                         struct rte_flow_action action;
557                 } query; /**< Query arguments. */
558                 struct {
559                         uint32_t *group;
560                         uint32_t group_n;
561                 } list; /**< List arguments. */
562                 struct {
563                         int set;
564                 } isolate; /**< Isolated mode arguments. */
565         } args; /**< Command arguments. */
566 };
567
568 /** Private data for pattern items. */
569 struct parse_item_priv {
570         enum rte_flow_item_type type; /**< Item type. */
571         uint32_t size; /**< Size of item specification structure. */
572 };
573
574 #define PRIV_ITEM(t, s) \
575         (&(const struct parse_item_priv){ \
576                 .type = RTE_FLOW_ITEM_TYPE_ ## t, \
577                 .size = s, \
578         })
579
580 /** Private data for actions. */
581 struct parse_action_priv {
582         enum rte_flow_action_type type; /**< Action type. */
583         uint32_t size; /**< Size of action configuration structure. */
584 };
585
586 #define PRIV_ACTION(t, s) \
587         (&(const struct parse_action_priv){ \
588                 .type = RTE_FLOW_ACTION_TYPE_ ## t, \
589                 .size = s, \
590         })
591
592 static const enum index next_vc_attr[] = {
593         GROUP,
594         PRIORITY,
595         INGRESS,
596         EGRESS,
597         TRANSFER,
598         PATTERN,
599         ZERO,
600 };
601
602 static const enum index next_destroy_attr[] = {
603         DESTROY_RULE,
604         END,
605         ZERO,
606 };
607
608 static const enum index next_list_attr[] = {
609         LIST_GROUP,
610         END,
611         ZERO,
612 };
613
614 static const enum index item_param[] = {
615         ITEM_PARAM_IS,
616         ITEM_PARAM_SPEC,
617         ITEM_PARAM_LAST,
618         ITEM_PARAM_MASK,
619         ITEM_PARAM_PREFIX,
620         ZERO,
621 };
622
623 static const enum index next_item[] = {
624         ITEM_END,
625         ITEM_VOID,
626         ITEM_INVERT,
627         ITEM_ANY,
628         ITEM_PF,
629         ITEM_VF,
630         ITEM_PHY_PORT,
631         ITEM_PORT_ID,
632         ITEM_MARK,
633         ITEM_RAW,
634         ITEM_ETH,
635         ITEM_VLAN,
636         ITEM_IPV4,
637         ITEM_IPV6,
638         ITEM_ICMP,
639         ITEM_UDP,
640         ITEM_TCP,
641         ITEM_SCTP,
642         ITEM_VXLAN,
643         ITEM_E_TAG,
644         ITEM_NVGRE,
645         ITEM_MPLS,
646         ITEM_GRE,
647         ITEM_FUZZY,
648         ITEM_GTP,
649         ITEM_GTPC,
650         ITEM_GTPU,
651         ITEM_GENEVE,
652         ITEM_VXLAN_GPE,
653         ITEM_ARP_ETH_IPV4,
654         ITEM_IPV6_EXT,
655         ITEM_ICMP6,
656         ITEM_ICMP6_ND_NS,
657         ITEM_ICMP6_ND_NA,
658         ITEM_ICMP6_ND_OPT,
659         ITEM_ICMP6_ND_OPT_SLA_ETH,
660         ITEM_ICMP6_ND_OPT_TLA_ETH,
661         ITEM_META,
662         ITEM_GRE_KEY,
663         END_SET,
664         ZERO,
665 };
666
667 static const enum index item_fuzzy[] = {
668         ITEM_FUZZY_THRESH,
669         ITEM_NEXT,
670         ZERO,
671 };
672
673 static const enum index item_any[] = {
674         ITEM_ANY_NUM,
675         ITEM_NEXT,
676         ZERO,
677 };
678
679 static const enum index item_vf[] = {
680         ITEM_VF_ID,
681         ITEM_NEXT,
682         ZERO,
683 };
684
685 static const enum index item_phy_port[] = {
686         ITEM_PHY_PORT_INDEX,
687         ITEM_NEXT,
688         ZERO,
689 };
690
691 static const enum index item_port_id[] = {
692         ITEM_PORT_ID_ID,
693         ITEM_NEXT,
694         ZERO,
695 };
696
697 static const enum index item_mark[] = {
698         ITEM_MARK_ID,
699         ITEM_NEXT,
700         ZERO,
701 };
702
703 static const enum index item_raw[] = {
704         ITEM_RAW_RELATIVE,
705         ITEM_RAW_SEARCH,
706         ITEM_RAW_OFFSET,
707         ITEM_RAW_LIMIT,
708         ITEM_RAW_PATTERN,
709         ITEM_NEXT,
710         ZERO,
711 };
712
713 static const enum index item_eth[] = {
714         ITEM_ETH_DST,
715         ITEM_ETH_SRC,
716         ITEM_ETH_TYPE,
717         ITEM_NEXT,
718         ZERO,
719 };
720
721 static const enum index item_vlan[] = {
722         ITEM_VLAN_TCI,
723         ITEM_VLAN_PCP,
724         ITEM_VLAN_DEI,
725         ITEM_VLAN_VID,
726         ITEM_VLAN_INNER_TYPE,
727         ITEM_NEXT,
728         ZERO,
729 };
730
731 static const enum index item_ipv4[] = {
732         ITEM_IPV4_TOS,
733         ITEM_IPV4_TTL,
734         ITEM_IPV4_PROTO,
735         ITEM_IPV4_SRC,
736         ITEM_IPV4_DST,
737         ITEM_NEXT,
738         ZERO,
739 };
740
741 static const enum index item_ipv6[] = {
742         ITEM_IPV6_TC,
743         ITEM_IPV6_FLOW,
744         ITEM_IPV6_PROTO,
745         ITEM_IPV6_HOP,
746         ITEM_IPV6_SRC,
747         ITEM_IPV6_DST,
748         ITEM_NEXT,
749         ZERO,
750 };
751
752 static const enum index item_icmp[] = {
753         ITEM_ICMP_TYPE,
754         ITEM_ICMP_CODE,
755         ITEM_NEXT,
756         ZERO,
757 };
758
759 static const enum index item_udp[] = {
760         ITEM_UDP_SRC,
761         ITEM_UDP_DST,
762         ITEM_NEXT,
763         ZERO,
764 };
765
766 static const enum index item_tcp[] = {
767         ITEM_TCP_SRC,
768         ITEM_TCP_DST,
769         ITEM_TCP_FLAGS,
770         ITEM_NEXT,
771         ZERO,
772 };
773
774 static const enum index item_sctp[] = {
775         ITEM_SCTP_SRC,
776         ITEM_SCTP_DST,
777         ITEM_SCTP_TAG,
778         ITEM_SCTP_CKSUM,
779         ITEM_NEXT,
780         ZERO,
781 };
782
783 static const enum index item_vxlan[] = {
784         ITEM_VXLAN_VNI,
785         ITEM_NEXT,
786         ZERO,
787 };
788
789 static const enum index item_e_tag[] = {
790         ITEM_E_TAG_GRP_ECID_B,
791         ITEM_NEXT,
792         ZERO,
793 };
794
795 static const enum index item_nvgre[] = {
796         ITEM_NVGRE_TNI,
797         ITEM_NEXT,
798         ZERO,
799 };
800
801 static const enum index item_mpls[] = {
802         ITEM_MPLS_LABEL,
803         ITEM_NEXT,
804         ZERO,
805 };
806
807 static const enum index item_gre[] = {
808         ITEM_GRE_PROTO,
809         ITEM_GRE_C_RSVD0_VER,
810         ITEM_GRE_C_BIT,
811         ITEM_GRE_K_BIT,
812         ITEM_GRE_S_BIT,
813         ITEM_NEXT,
814         ZERO,
815 };
816
817 static const enum index item_gre_key[] = {
818         ITEM_GRE_KEY_VALUE,
819         ITEM_NEXT,
820         ZERO,
821 };
822
823 static const enum index item_gtp[] = {
824         ITEM_GTP_TEID,
825         ITEM_NEXT,
826         ZERO,
827 };
828
829 static const enum index item_geneve[] = {
830         ITEM_GENEVE_VNI,
831         ITEM_GENEVE_PROTO,
832         ITEM_NEXT,
833         ZERO,
834 };
835
836 static const enum index item_vxlan_gpe[] = {
837         ITEM_VXLAN_GPE_VNI,
838         ITEM_NEXT,
839         ZERO,
840 };
841
842 static const enum index item_arp_eth_ipv4[] = {
843         ITEM_ARP_ETH_IPV4_SHA,
844         ITEM_ARP_ETH_IPV4_SPA,
845         ITEM_ARP_ETH_IPV4_THA,
846         ITEM_ARP_ETH_IPV4_TPA,
847         ITEM_NEXT,
848         ZERO,
849 };
850
851 static const enum index item_ipv6_ext[] = {
852         ITEM_IPV6_EXT_NEXT_HDR,
853         ITEM_NEXT,
854         ZERO,
855 };
856
857 static const enum index item_icmp6[] = {
858         ITEM_ICMP6_TYPE,
859         ITEM_ICMP6_CODE,
860         ITEM_NEXT,
861         ZERO,
862 };
863
864 static const enum index item_icmp6_nd_ns[] = {
865         ITEM_ICMP6_ND_NS_TARGET_ADDR,
866         ITEM_NEXT,
867         ZERO,
868 };
869
870 static const enum index item_icmp6_nd_na[] = {
871         ITEM_ICMP6_ND_NA_TARGET_ADDR,
872         ITEM_NEXT,
873         ZERO,
874 };
875
876 static const enum index item_icmp6_nd_opt[] = {
877         ITEM_ICMP6_ND_OPT_TYPE,
878         ITEM_NEXT,
879         ZERO,
880 };
881
882 static const enum index item_icmp6_nd_opt_sla_eth[] = {
883         ITEM_ICMP6_ND_OPT_SLA_ETH_SLA,
884         ITEM_NEXT,
885         ZERO,
886 };
887
888 static const enum index item_icmp6_nd_opt_tla_eth[] = {
889         ITEM_ICMP6_ND_OPT_TLA_ETH_TLA,
890         ITEM_NEXT,
891         ZERO,
892 };
893
894 static const enum index item_meta[] = {
895         ITEM_META_DATA,
896         ITEM_NEXT,
897         ZERO,
898 };
899
900 static const enum index next_action[] = {
901         ACTION_END,
902         ACTION_VOID,
903         ACTION_PASSTHRU,
904         ACTION_JUMP,
905         ACTION_MARK,
906         ACTION_FLAG,
907         ACTION_QUEUE,
908         ACTION_DROP,
909         ACTION_COUNT,
910         ACTION_RSS,
911         ACTION_PF,
912         ACTION_VF,
913         ACTION_PHY_PORT,
914         ACTION_PORT_ID,
915         ACTION_METER,
916         ACTION_OF_SET_MPLS_TTL,
917         ACTION_OF_DEC_MPLS_TTL,
918         ACTION_OF_SET_NW_TTL,
919         ACTION_OF_DEC_NW_TTL,
920         ACTION_OF_COPY_TTL_OUT,
921         ACTION_OF_COPY_TTL_IN,
922         ACTION_OF_POP_VLAN,
923         ACTION_OF_PUSH_VLAN,
924         ACTION_OF_SET_VLAN_VID,
925         ACTION_OF_SET_VLAN_PCP,
926         ACTION_OF_POP_MPLS,
927         ACTION_OF_PUSH_MPLS,
928         ACTION_VXLAN_ENCAP,
929         ACTION_VXLAN_DECAP,
930         ACTION_NVGRE_ENCAP,
931         ACTION_NVGRE_DECAP,
932         ACTION_L2_ENCAP,
933         ACTION_L2_DECAP,
934         ACTION_MPLSOGRE_ENCAP,
935         ACTION_MPLSOGRE_DECAP,
936         ACTION_MPLSOUDP_ENCAP,
937         ACTION_MPLSOUDP_DECAP,
938         ACTION_SET_IPV4_SRC,
939         ACTION_SET_IPV4_DST,
940         ACTION_SET_IPV6_SRC,
941         ACTION_SET_IPV6_DST,
942         ACTION_SET_TP_SRC,
943         ACTION_SET_TP_DST,
944         ACTION_MAC_SWAP,
945         ACTION_DEC_TTL,
946         ACTION_SET_TTL,
947         ACTION_SET_MAC_SRC,
948         ACTION_SET_MAC_DST,
949         ACTION_INC_TCP_SEQ,
950         ACTION_DEC_TCP_SEQ,
951         ACTION_INC_TCP_ACK,
952         ACTION_DEC_TCP_ACK,
953         ACTION_RAW_ENCAP,
954         ACTION_RAW_DECAP,
955         ZERO,
956 };
957
958 static const enum index action_mark[] = {
959         ACTION_MARK_ID,
960         ACTION_NEXT,
961         ZERO,
962 };
963
964 static const enum index action_queue[] = {
965         ACTION_QUEUE_INDEX,
966         ACTION_NEXT,
967         ZERO,
968 };
969
970 static const enum index action_count[] = {
971         ACTION_COUNT_ID,
972         ACTION_COUNT_SHARED,
973         ACTION_NEXT,
974         ZERO,
975 };
976
977 static const enum index action_rss[] = {
978         ACTION_RSS_FUNC,
979         ACTION_RSS_LEVEL,
980         ACTION_RSS_TYPES,
981         ACTION_RSS_KEY,
982         ACTION_RSS_KEY_LEN,
983         ACTION_RSS_QUEUES,
984         ACTION_NEXT,
985         ZERO,
986 };
987
988 static const enum index action_vf[] = {
989         ACTION_VF_ORIGINAL,
990         ACTION_VF_ID,
991         ACTION_NEXT,
992         ZERO,
993 };
994
995 static const enum index action_phy_port[] = {
996         ACTION_PHY_PORT_ORIGINAL,
997         ACTION_PHY_PORT_INDEX,
998         ACTION_NEXT,
999         ZERO,
1000 };
1001
1002 static const enum index action_port_id[] = {
1003         ACTION_PORT_ID_ORIGINAL,
1004         ACTION_PORT_ID_ID,
1005         ACTION_NEXT,
1006         ZERO,
1007 };
1008
1009 static const enum index action_meter[] = {
1010         ACTION_METER_ID,
1011         ACTION_NEXT,
1012         ZERO,
1013 };
1014
1015 static const enum index action_of_set_mpls_ttl[] = {
1016         ACTION_OF_SET_MPLS_TTL_MPLS_TTL,
1017         ACTION_NEXT,
1018         ZERO,
1019 };
1020
1021 static const enum index action_of_set_nw_ttl[] = {
1022         ACTION_OF_SET_NW_TTL_NW_TTL,
1023         ACTION_NEXT,
1024         ZERO,
1025 };
1026
1027 static const enum index action_of_push_vlan[] = {
1028         ACTION_OF_PUSH_VLAN_ETHERTYPE,
1029         ACTION_NEXT,
1030         ZERO,
1031 };
1032
1033 static const enum index action_of_set_vlan_vid[] = {
1034         ACTION_OF_SET_VLAN_VID_VLAN_VID,
1035         ACTION_NEXT,
1036         ZERO,
1037 };
1038
1039 static const enum index action_of_set_vlan_pcp[] = {
1040         ACTION_OF_SET_VLAN_PCP_VLAN_PCP,
1041         ACTION_NEXT,
1042         ZERO,
1043 };
1044
1045 static const enum index action_of_pop_mpls[] = {
1046         ACTION_OF_POP_MPLS_ETHERTYPE,
1047         ACTION_NEXT,
1048         ZERO,
1049 };
1050
1051 static const enum index action_of_push_mpls[] = {
1052         ACTION_OF_PUSH_MPLS_ETHERTYPE,
1053         ACTION_NEXT,
1054         ZERO,
1055 };
1056
1057 static const enum index action_set_ipv4_src[] = {
1058         ACTION_SET_IPV4_SRC_IPV4_SRC,
1059         ACTION_NEXT,
1060         ZERO,
1061 };
1062
1063 static const enum index action_set_mac_src[] = {
1064         ACTION_SET_MAC_SRC_MAC_SRC,
1065         ACTION_NEXT,
1066         ZERO,
1067 };
1068
1069 static const enum index action_set_ipv4_dst[] = {
1070         ACTION_SET_IPV4_DST_IPV4_DST,
1071         ACTION_NEXT,
1072         ZERO,
1073 };
1074
1075 static const enum index action_set_ipv6_src[] = {
1076         ACTION_SET_IPV6_SRC_IPV6_SRC,
1077         ACTION_NEXT,
1078         ZERO,
1079 };
1080
1081 static const enum index action_set_ipv6_dst[] = {
1082         ACTION_SET_IPV6_DST_IPV6_DST,
1083         ACTION_NEXT,
1084         ZERO,
1085 };
1086
1087 static const enum index action_set_tp_src[] = {
1088         ACTION_SET_TP_SRC_TP_SRC,
1089         ACTION_NEXT,
1090         ZERO,
1091 };
1092
1093 static const enum index action_set_tp_dst[] = {
1094         ACTION_SET_TP_DST_TP_DST,
1095         ACTION_NEXT,
1096         ZERO,
1097 };
1098
1099 static const enum index action_set_ttl[] = {
1100         ACTION_SET_TTL_TTL,
1101         ACTION_NEXT,
1102         ZERO,
1103 };
1104
1105 static const enum index action_jump[] = {
1106         ACTION_JUMP_GROUP,
1107         ACTION_NEXT,
1108         ZERO,
1109 };
1110
1111 static const enum index action_set_mac_dst[] = {
1112         ACTION_SET_MAC_DST_MAC_DST,
1113         ACTION_NEXT,
1114         ZERO,
1115 };
1116
1117 static const enum index action_inc_tcp_seq[] = {
1118         ACTION_INC_TCP_SEQ_VALUE,
1119         ACTION_NEXT,
1120         ZERO,
1121 };
1122
1123 static const enum index action_dec_tcp_seq[] = {
1124         ACTION_DEC_TCP_SEQ_VALUE,
1125         ACTION_NEXT,
1126         ZERO,
1127 };
1128
1129 static const enum index action_inc_tcp_ack[] = {
1130         ACTION_INC_TCP_ACK_VALUE,
1131         ACTION_NEXT,
1132         ZERO,
1133 };
1134
1135 static const enum index action_dec_tcp_ack[] = {
1136         ACTION_DEC_TCP_ACK_VALUE,
1137         ACTION_NEXT,
1138         ZERO,
1139 };
1140
1141 static int parse_set_raw_encap_decap(struct context *, const struct token *,
1142                                      const char *, unsigned int,
1143                                      void *, unsigned int);
1144 static int parse_set_init(struct context *, const struct token *,
1145                           const char *, unsigned int,
1146                           void *, unsigned int);
1147 static int parse_init(struct context *, const struct token *,
1148                       const char *, unsigned int,
1149                       void *, unsigned int);
1150 static int parse_vc(struct context *, const struct token *,
1151                     const char *, unsigned int,
1152                     void *, unsigned int);
1153 static int parse_vc_spec(struct context *, const struct token *,
1154                          const char *, unsigned int, void *, unsigned int);
1155 static int parse_vc_conf(struct context *, const struct token *,
1156                          const char *, unsigned int, void *, unsigned int);
1157 static int parse_vc_action_rss(struct context *, const struct token *,
1158                                const char *, unsigned int, void *,
1159                                unsigned int);
1160 static int parse_vc_action_rss_func(struct context *, const struct token *,
1161                                     const char *, unsigned int, void *,
1162                                     unsigned int);
1163 static int parse_vc_action_rss_type(struct context *, const struct token *,
1164                                     const char *, unsigned int, void *,
1165                                     unsigned int);
1166 static int parse_vc_action_rss_queue(struct context *, const struct token *,
1167                                      const char *, unsigned int, void *,
1168                                      unsigned int);
1169 static int parse_vc_action_vxlan_encap(struct context *, const struct token *,
1170                                        const char *, unsigned int, void *,
1171                                        unsigned int);
1172 static int parse_vc_action_nvgre_encap(struct context *, const struct token *,
1173                                        const char *, unsigned int, void *,
1174                                        unsigned int);
1175 static int parse_vc_action_l2_encap(struct context *, const struct token *,
1176                                     const char *, unsigned int, void *,
1177                                     unsigned int);
1178 static int parse_vc_action_l2_decap(struct context *, const struct token *,
1179                                     const char *, unsigned int, void *,
1180                                     unsigned int);
1181 static int parse_vc_action_mplsogre_encap(struct context *,
1182                                           const struct token *, const char *,
1183                                           unsigned int, void *, unsigned int);
1184 static int parse_vc_action_mplsogre_decap(struct context *,
1185                                           const struct token *, const char *,
1186                                           unsigned int, void *, unsigned int);
1187 static int parse_vc_action_mplsoudp_encap(struct context *,
1188                                           const struct token *, const char *,
1189                                           unsigned int, void *, unsigned int);
1190 static int parse_vc_action_mplsoudp_decap(struct context *,
1191                                           const struct token *, const char *,
1192                                           unsigned int, void *, unsigned int);
1193 static int parse_vc_action_raw_encap(struct context *,
1194                                      const struct token *, const char *,
1195                                      unsigned int, void *, unsigned int);
1196 static int parse_vc_action_raw_decap(struct context *,
1197                                      const struct token *, const char *,
1198                                      unsigned int, void *, unsigned int);
1199 static int parse_destroy(struct context *, const struct token *,
1200                          const char *, unsigned int,
1201                          void *, unsigned int);
1202 static int parse_flush(struct context *, const struct token *,
1203                        const char *, unsigned int,
1204                        void *, unsigned int);
1205 static int parse_query(struct context *, const struct token *,
1206                        const char *, unsigned int,
1207                        void *, unsigned int);
1208 static int parse_action(struct context *, const struct token *,
1209                         const char *, unsigned int,
1210                         void *, unsigned int);
1211 static int parse_list(struct context *, const struct token *,
1212                       const char *, unsigned int,
1213                       void *, unsigned int);
1214 static int parse_isolate(struct context *, const struct token *,
1215                          const char *, unsigned int,
1216                          void *, unsigned int);
1217 static int parse_int(struct context *, const struct token *,
1218                      const char *, unsigned int,
1219                      void *, unsigned int);
1220 static int parse_prefix(struct context *, const struct token *,
1221                         const char *, unsigned int,
1222                         void *, unsigned int);
1223 static int parse_boolean(struct context *, const struct token *,
1224                          const char *, unsigned int,
1225                          void *, unsigned int);
1226 static int parse_string(struct context *, const struct token *,
1227                         const char *, unsigned int,
1228                         void *, unsigned int);
1229 static int parse_hex(struct context *ctx, const struct token *token,
1230                         const char *str, unsigned int len,
1231                         void *buf, unsigned int size);
1232 static int parse_mac_addr(struct context *, const struct token *,
1233                           const char *, unsigned int,
1234                           void *, unsigned int);
1235 static int parse_ipv4_addr(struct context *, const struct token *,
1236                            const char *, unsigned int,
1237                            void *, unsigned int);
1238 static int parse_ipv6_addr(struct context *, const struct token *,
1239                            const char *, unsigned int,
1240                            void *, unsigned int);
1241 static int parse_port(struct context *, const struct token *,
1242                       const char *, unsigned int,
1243                       void *, unsigned int);
1244 static int comp_none(struct context *, const struct token *,
1245                      unsigned int, char *, unsigned int);
1246 static int comp_boolean(struct context *, const struct token *,
1247                         unsigned int, char *, unsigned int);
1248 static int comp_action(struct context *, const struct token *,
1249                        unsigned int, char *, unsigned int);
1250 static int comp_port(struct context *, const struct token *,
1251                      unsigned int, char *, unsigned int);
1252 static int comp_rule_id(struct context *, const struct token *,
1253                         unsigned int, char *, unsigned int);
1254 static int comp_vc_action_rss_type(struct context *, const struct token *,
1255                                    unsigned int, char *, unsigned int);
1256 static int comp_vc_action_rss_queue(struct context *, const struct token *,
1257                                     unsigned int, char *, unsigned int);
1258
1259 /** Token definitions. */
1260 static const struct token token_list[] = {
1261         /* Special tokens. */
1262         [ZERO] = {
1263                 .name = "ZERO",
1264                 .help = "null entry, abused as the entry point",
1265                 .next = NEXT(NEXT_ENTRY(FLOW)),
1266         },
1267         [END] = {
1268                 .name = "",
1269                 .type = "RETURN",
1270                 .help = "command may end here",
1271         },
1272         [START_SET] = {
1273                 .name = "START_SET",
1274                 .help = "null entry, abused as the entry point for set",
1275                 .next = NEXT(NEXT_ENTRY(SET)),
1276         },
1277         [END_SET] = {
1278                 .name = "end_set",
1279                 .type = "RETURN",
1280                 .help = "set command may end here",
1281         },
1282         /* Common tokens. */
1283         [INTEGER] = {
1284                 .name = "{int}",
1285                 .type = "INTEGER",
1286                 .help = "integer value",
1287                 .call = parse_int,
1288                 .comp = comp_none,
1289         },
1290         [UNSIGNED] = {
1291                 .name = "{unsigned}",
1292                 .type = "UNSIGNED",
1293                 .help = "unsigned integer value",
1294                 .call = parse_int,
1295                 .comp = comp_none,
1296         },
1297         [PREFIX] = {
1298                 .name = "{prefix}",
1299                 .type = "PREFIX",
1300                 .help = "prefix length for bit-mask",
1301                 .call = parse_prefix,
1302                 .comp = comp_none,
1303         },
1304         [BOOLEAN] = {
1305                 .name = "{boolean}",
1306                 .type = "BOOLEAN",
1307                 .help = "any boolean value",
1308                 .call = parse_boolean,
1309                 .comp = comp_boolean,
1310         },
1311         [STRING] = {
1312                 .name = "{string}",
1313                 .type = "STRING",
1314                 .help = "fixed string",
1315                 .call = parse_string,
1316                 .comp = comp_none,
1317         },
1318         [HEX] = {
1319                 .name = "{hex}",
1320                 .type = "HEX",
1321                 .help = "fixed string",
1322                 .call = parse_hex,
1323                 .comp = comp_none,
1324         },
1325         [MAC_ADDR] = {
1326                 .name = "{MAC address}",
1327                 .type = "MAC-48",
1328                 .help = "standard MAC address notation",
1329                 .call = parse_mac_addr,
1330                 .comp = comp_none,
1331         },
1332         [IPV4_ADDR] = {
1333                 .name = "{IPv4 address}",
1334                 .type = "IPV4 ADDRESS",
1335                 .help = "standard IPv4 address notation",
1336                 .call = parse_ipv4_addr,
1337                 .comp = comp_none,
1338         },
1339         [IPV6_ADDR] = {
1340                 .name = "{IPv6 address}",
1341                 .type = "IPV6 ADDRESS",
1342                 .help = "standard IPv6 address notation",
1343                 .call = parse_ipv6_addr,
1344                 .comp = comp_none,
1345         },
1346         [RULE_ID] = {
1347                 .name = "{rule id}",
1348                 .type = "RULE ID",
1349                 .help = "rule identifier",
1350                 .call = parse_int,
1351                 .comp = comp_rule_id,
1352         },
1353         [PORT_ID] = {
1354                 .name = "{port_id}",
1355                 .type = "PORT ID",
1356                 .help = "port identifier",
1357                 .call = parse_port,
1358                 .comp = comp_port,
1359         },
1360         [GROUP_ID] = {
1361                 .name = "{group_id}",
1362                 .type = "GROUP ID",
1363                 .help = "group identifier",
1364                 .call = parse_int,
1365                 .comp = comp_none,
1366         },
1367         [PRIORITY_LEVEL] = {
1368                 .name = "{level}",
1369                 .type = "PRIORITY",
1370                 .help = "priority level",
1371                 .call = parse_int,
1372                 .comp = comp_none,
1373         },
1374         /* Top-level command. */
1375         [FLOW] = {
1376                 .name = "flow",
1377                 .type = "{command} {port_id} [{arg} [...]]",
1378                 .help = "manage ingress/egress flow rules",
1379                 .next = NEXT(NEXT_ENTRY
1380                              (VALIDATE,
1381                               CREATE,
1382                               DESTROY,
1383                               FLUSH,
1384                               LIST,
1385                               QUERY,
1386                               ISOLATE)),
1387                 .call = parse_init,
1388         },
1389         /* Sub-level commands. */
1390         [VALIDATE] = {
1391                 .name = "validate",
1392                 .help = "check whether a flow rule can be created",
1393                 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
1394                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
1395                 .call = parse_vc,
1396         },
1397         [CREATE] = {
1398                 .name = "create",
1399                 .help = "create a flow rule",
1400                 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
1401                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
1402                 .call = parse_vc,
1403         },
1404         [DESTROY] = {
1405                 .name = "destroy",
1406                 .help = "destroy specific flow rules",
1407                 .next = NEXT(NEXT_ENTRY(DESTROY_RULE), NEXT_ENTRY(PORT_ID)),
1408                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
1409                 .call = parse_destroy,
1410         },
1411         [FLUSH] = {
1412                 .name = "flush",
1413                 .help = "destroy all flow rules",
1414                 .next = NEXT(NEXT_ENTRY(PORT_ID)),
1415                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
1416                 .call = parse_flush,
1417         },
1418         [QUERY] = {
1419                 .name = "query",
1420                 .help = "query an existing flow rule",
1421                 .next = NEXT(NEXT_ENTRY(QUERY_ACTION),
1422                              NEXT_ENTRY(RULE_ID),
1423                              NEXT_ENTRY(PORT_ID)),
1424                 .args = ARGS(ARGS_ENTRY(struct buffer, args.query.action.type),
1425                              ARGS_ENTRY(struct buffer, args.query.rule),
1426                              ARGS_ENTRY(struct buffer, port)),
1427                 .call = parse_query,
1428         },
1429         [LIST] = {
1430                 .name = "list",
1431                 .help = "list existing flow rules",
1432                 .next = NEXT(next_list_attr, NEXT_ENTRY(PORT_ID)),
1433                 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
1434                 .call = parse_list,
1435         },
1436         [ISOLATE] = {
1437                 .name = "isolate",
1438                 .help = "restrict ingress traffic to the defined flow rules",
1439                 .next = NEXT(NEXT_ENTRY(BOOLEAN),
1440                              NEXT_ENTRY(PORT_ID)),
1441                 .args = ARGS(ARGS_ENTRY(struct buffer, args.isolate.set),
1442                              ARGS_ENTRY(struct buffer, port)),
1443                 .call = parse_isolate,
1444         },
1445         /* Destroy arguments. */
1446         [DESTROY_RULE] = {
1447                 .name = "rule",
1448                 .help = "specify a rule identifier",
1449                 .next = NEXT(next_destroy_attr, NEXT_ENTRY(RULE_ID)),
1450                 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.destroy.rule)),
1451                 .call = parse_destroy,
1452         },
1453         /* Query arguments. */
1454         [QUERY_ACTION] = {
1455                 .name = "{action}",
1456                 .type = "ACTION",
1457                 .help = "action to query, must be part of the rule",
1458                 .call = parse_action,
1459                 .comp = comp_action,
1460         },
1461         /* List arguments. */
1462         [LIST_GROUP] = {
1463                 .name = "group",
1464                 .help = "specify a group",
1465                 .next = NEXT(next_list_attr, NEXT_ENTRY(GROUP_ID)),
1466                 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.list.group)),
1467                 .call = parse_list,
1468         },
1469         /* Validate/create attributes. */
1470         [GROUP] = {
1471                 .name = "group",
1472                 .help = "specify a group",
1473                 .next = NEXT(next_vc_attr, NEXT_ENTRY(GROUP_ID)),
1474                 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, group)),
1475                 .call = parse_vc,
1476         },
1477         [PRIORITY] = {
1478                 .name = "priority",
1479                 .help = "specify a priority level",
1480                 .next = NEXT(next_vc_attr, NEXT_ENTRY(PRIORITY_LEVEL)),
1481                 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, priority)),
1482                 .call = parse_vc,
1483         },
1484         [INGRESS] = {
1485                 .name = "ingress",
1486                 .help = "affect rule to ingress",
1487                 .next = NEXT(next_vc_attr),
1488                 .call = parse_vc,
1489         },
1490         [EGRESS] = {
1491                 .name = "egress",
1492                 .help = "affect rule to egress",
1493                 .next = NEXT(next_vc_attr),
1494                 .call = parse_vc,
1495         },
1496         [TRANSFER] = {
1497                 .name = "transfer",
1498                 .help = "apply rule directly to endpoints found in pattern",
1499                 .next = NEXT(next_vc_attr),
1500                 .call = parse_vc,
1501         },
1502         /* Validate/create pattern. */
1503         [PATTERN] = {
1504                 .name = "pattern",
1505                 .help = "submit a list of pattern items",
1506                 .next = NEXT(next_item),
1507                 .call = parse_vc,
1508         },
1509         [ITEM_PARAM_IS] = {
1510                 .name = "is",
1511                 .help = "match value perfectly (with full bit-mask)",
1512                 .call = parse_vc_spec,
1513         },
1514         [ITEM_PARAM_SPEC] = {
1515                 .name = "spec",
1516                 .help = "match value according to configured bit-mask",
1517                 .call = parse_vc_spec,
1518         },
1519         [ITEM_PARAM_LAST] = {
1520                 .name = "last",
1521                 .help = "specify upper bound to establish a range",
1522                 .call = parse_vc_spec,
1523         },
1524         [ITEM_PARAM_MASK] = {
1525                 .name = "mask",
1526                 .help = "specify bit-mask with relevant bits set to one",
1527                 .call = parse_vc_spec,
1528         },
1529         [ITEM_PARAM_PREFIX] = {
1530                 .name = "prefix",
1531                 .help = "generate bit-mask from a prefix length",
1532                 .call = parse_vc_spec,
1533         },
1534         [ITEM_NEXT] = {
1535                 .name = "/",
1536                 .help = "specify next pattern item",
1537                 .next = NEXT(next_item),
1538         },
1539         [ITEM_END] = {
1540                 .name = "end",
1541                 .help = "end list of pattern items",
1542                 .priv = PRIV_ITEM(END, 0),
1543                 .next = NEXT(NEXT_ENTRY(ACTIONS)),
1544                 .call = parse_vc,
1545         },
1546         [ITEM_VOID] = {
1547                 .name = "void",
1548                 .help = "no-op pattern item",
1549                 .priv = PRIV_ITEM(VOID, 0),
1550                 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1551                 .call = parse_vc,
1552         },
1553         [ITEM_INVERT] = {
1554                 .name = "invert",
1555                 .help = "perform actions when pattern does not match",
1556                 .priv = PRIV_ITEM(INVERT, 0),
1557                 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1558                 .call = parse_vc,
1559         },
1560         [ITEM_ANY] = {
1561                 .name = "any",
1562                 .help = "match any protocol for the current layer",
1563                 .priv = PRIV_ITEM(ANY, sizeof(struct rte_flow_item_any)),
1564                 .next = NEXT(item_any),
1565                 .call = parse_vc,
1566         },
1567         [ITEM_ANY_NUM] = {
1568                 .name = "num",
1569                 .help = "number of layers covered",
1570                 .next = NEXT(item_any, NEXT_ENTRY(UNSIGNED), item_param),
1571                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_any, num)),
1572         },
1573         [ITEM_PF] = {
1574                 .name = "pf",
1575                 .help = "match traffic from/to the physical function",
1576                 .priv = PRIV_ITEM(PF, 0),
1577                 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1578                 .call = parse_vc,
1579         },
1580         [ITEM_VF] = {
1581                 .name = "vf",
1582                 .help = "match traffic from/to a virtual function ID",
1583                 .priv = PRIV_ITEM(VF, sizeof(struct rte_flow_item_vf)),
1584                 .next = NEXT(item_vf),
1585                 .call = parse_vc,
1586         },
1587         [ITEM_VF_ID] = {
1588                 .name = "id",
1589                 .help = "VF ID",
1590                 .next = NEXT(item_vf, NEXT_ENTRY(UNSIGNED), item_param),
1591                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_vf, id)),
1592         },
1593         [ITEM_PHY_PORT] = {
1594                 .name = "phy_port",
1595                 .help = "match traffic from/to a specific physical port",
1596                 .priv = PRIV_ITEM(PHY_PORT,
1597                                   sizeof(struct rte_flow_item_phy_port)),
1598                 .next = NEXT(item_phy_port),
1599                 .call = parse_vc,
1600         },
1601         [ITEM_PHY_PORT_INDEX] = {
1602                 .name = "index",
1603                 .help = "physical port index",
1604                 .next = NEXT(item_phy_port, NEXT_ENTRY(UNSIGNED), item_param),
1605                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_phy_port, index)),
1606         },
1607         [ITEM_PORT_ID] = {
1608                 .name = "port_id",
1609                 .help = "match traffic from/to a given DPDK port ID",
1610                 .priv = PRIV_ITEM(PORT_ID,
1611                                   sizeof(struct rte_flow_item_port_id)),
1612                 .next = NEXT(item_port_id),
1613                 .call = parse_vc,
1614         },
1615         [ITEM_PORT_ID_ID] = {
1616                 .name = "id",
1617                 .help = "DPDK port ID",
1618                 .next = NEXT(item_port_id, NEXT_ENTRY(UNSIGNED), item_param),
1619                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_port_id, id)),
1620         },
1621         [ITEM_MARK] = {
1622                 .name = "mark",
1623                 .help = "match traffic against value set in previously matched rule",
1624                 .priv = PRIV_ITEM(MARK, sizeof(struct rte_flow_item_mark)),
1625                 .next = NEXT(item_mark),
1626                 .call = parse_vc,
1627         },
1628         [ITEM_MARK_ID] = {
1629                 .name = "id",
1630                 .help = "Integer value to match against",
1631                 .next = NEXT(item_mark, NEXT_ENTRY(UNSIGNED), item_param),
1632                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_mark, id)),
1633         },
1634         [ITEM_RAW] = {
1635                 .name = "raw",
1636                 .help = "match an arbitrary byte string",
1637                 .priv = PRIV_ITEM(RAW, ITEM_RAW_SIZE),
1638                 .next = NEXT(item_raw),
1639                 .call = parse_vc,
1640         },
1641         [ITEM_RAW_RELATIVE] = {
1642                 .name = "relative",
1643                 .help = "look for pattern after the previous item",
1644                 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1645                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1646                                            relative, 1)),
1647         },
1648         [ITEM_RAW_SEARCH] = {
1649                 .name = "search",
1650                 .help = "search pattern from offset (see also limit)",
1651                 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1652                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1653                                            search, 1)),
1654         },
1655         [ITEM_RAW_OFFSET] = {
1656                 .name = "offset",
1657                 .help = "absolute or relative offset for pattern",
1658                 .next = NEXT(item_raw, NEXT_ENTRY(INTEGER), item_param),
1659                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, offset)),
1660         },
1661         [ITEM_RAW_LIMIT] = {
1662                 .name = "limit",
1663                 .help = "search area limit for start of pattern",
1664                 .next = NEXT(item_raw, NEXT_ENTRY(UNSIGNED), item_param),
1665                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, limit)),
1666         },
1667         [ITEM_RAW_PATTERN] = {
1668                 .name = "pattern",
1669                 .help = "byte string to look for",
1670                 .next = NEXT(item_raw,
1671                              NEXT_ENTRY(STRING),
1672                              NEXT_ENTRY(ITEM_PARAM_IS,
1673                                         ITEM_PARAM_SPEC,
1674                                         ITEM_PARAM_MASK)),
1675                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, pattern),
1676                              ARGS_ENTRY(struct rte_flow_item_raw, length),
1677                              ARGS_ENTRY_ARB(sizeof(struct rte_flow_item_raw),
1678                                             ITEM_RAW_PATTERN_SIZE)),
1679         },
1680         [ITEM_ETH] = {
1681                 .name = "eth",
1682                 .help = "match Ethernet header",
1683                 .priv = PRIV_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
1684                 .next = NEXT(item_eth),
1685                 .call = parse_vc,
1686         },
1687         [ITEM_ETH_DST] = {
1688                 .name = "dst",
1689                 .help = "destination MAC",
1690                 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1691                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, dst)),
1692         },
1693         [ITEM_ETH_SRC] = {
1694                 .name = "src",
1695                 .help = "source MAC",
1696                 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1697                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, src)),
1698         },
1699         [ITEM_ETH_TYPE] = {
1700                 .name = "type",
1701                 .help = "EtherType",
1702                 .next = NEXT(item_eth, NEXT_ENTRY(UNSIGNED), item_param),
1703                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, type)),
1704         },
1705         [ITEM_VLAN] = {
1706                 .name = "vlan",
1707                 .help = "match 802.1Q/ad VLAN tag",
1708                 .priv = PRIV_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
1709                 .next = NEXT(item_vlan),
1710                 .call = parse_vc,
1711         },
1712         [ITEM_VLAN_TCI] = {
1713                 .name = "tci",
1714                 .help = "tag control information",
1715                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1716                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tci)),
1717         },
1718         [ITEM_VLAN_PCP] = {
1719                 .name = "pcp",
1720                 .help = "priority code point",
1721                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1722                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1723                                                   tci, "\xe0\x00")),
1724         },
1725         [ITEM_VLAN_DEI] = {
1726                 .name = "dei",
1727                 .help = "drop eligible indicator",
1728                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1729                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1730                                                   tci, "\x10\x00")),
1731         },
1732         [ITEM_VLAN_VID] = {
1733                 .name = "vid",
1734                 .help = "VLAN identifier",
1735                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1736                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1737                                                   tci, "\x0f\xff")),
1738         },
1739         [ITEM_VLAN_INNER_TYPE] = {
1740                 .name = "inner_type",
1741                 .help = "inner EtherType",
1742                 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1743                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan,
1744                                              inner_type)),
1745         },
1746         [ITEM_IPV4] = {
1747                 .name = "ipv4",
1748                 .help = "match IPv4 header",
1749                 .priv = PRIV_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
1750                 .next = NEXT(item_ipv4),
1751                 .call = parse_vc,
1752         },
1753         [ITEM_IPV4_TOS] = {
1754                 .name = "tos",
1755                 .help = "type of service",
1756                 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1757                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1758                                              hdr.type_of_service)),
1759         },
1760         [ITEM_IPV4_TTL] = {
1761                 .name = "ttl",
1762                 .help = "time to live",
1763                 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1764                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1765                                              hdr.time_to_live)),
1766         },
1767         [ITEM_IPV4_PROTO] = {
1768                 .name = "proto",
1769                 .help = "next protocol ID",
1770                 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1771                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1772                                              hdr.next_proto_id)),
1773         },
1774         [ITEM_IPV4_SRC] = {
1775                 .name = "src",
1776                 .help = "source address",
1777                 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1778                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1779                                              hdr.src_addr)),
1780         },
1781         [ITEM_IPV4_DST] = {
1782                 .name = "dst",
1783                 .help = "destination address",
1784                 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1785                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1786                                              hdr.dst_addr)),
1787         },
1788         [ITEM_IPV6] = {
1789                 .name = "ipv6",
1790                 .help = "match IPv6 header",
1791                 .priv = PRIV_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
1792                 .next = NEXT(item_ipv6),
1793                 .call = parse_vc,
1794         },
1795         [ITEM_IPV6_TC] = {
1796                 .name = "tc",
1797                 .help = "traffic class",
1798                 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1799                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1800                                                   hdr.vtc_flow,
1801                                                   "\x0f\xf0\x00\x00")),
1802         },
1803         [ITEM_IPV6_FLOW] = {
1804                 .name = "flow",
1805                 .help = "flow label",
1806                 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1807                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1808                                                   hdr.vtc_flow,
1809                                                   "\x00\x0f\xff\xff")),
1810         },
1811         [ITEM_IPV6_PROTO] = {
1812                 .name = "proto",
1813                 .help = "protocol (next header)",
1814                 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1815                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1816                                              hdr.proto)),
1817         },
1818         [ITEM_IPV6_HOP] = {
1819                 .name = "hop",
1820                 .help = "hop limit",
1821                 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1822                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1823                                              hdr.hop_limits)),
1824         },
1825         [ITEM_IPV6_SRC] = {
1826                 .name = "src",
1827                 .help = "source address",
1828                 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1829                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1830                                              hdr.src_addr)),
1831         },
1832         [ITEM_IPV6_DST] = {
1833                 .name = "dst",
1834                 .help = "destination address",
1835                 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1836                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1837                                              hdr.dst_addr)),
1838         },
1839         [ITEM_ICMP] = {
1840                 .name = "icmp",
1841                 .help = "match ICMP header",
1842                 .priv = PRIV_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
1843                 .next = NEXT(item_icmp),
1844                 .call = parse_vc,
1845         },
1846         [ITEM_ICMP_TYPE] = {
1847                 .name = "type",
1848                 .help = "ICMP packet type",
1849                 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1850                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1851                                              hdr.icmp_type)),
1852         },
1853         [ITEM_ICMP_CODE] = {
1854                 .name = "code",
1855                 .help = "ICMP packet code",
1856                 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1857                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1858                                              hdr.icmp_code)),
1859         },
1860         [ITEM_UDP] = {
1861                 .name = "udp",
1862                 .help = "match UDP header",
1863                 .priv = PRIV_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
1864                 .next = NEXT(item_udp),
1865                 .call = parse_vc,
1866         },
1867         [ITEM_UDP_SRC] = {
1868                 .name = "src",
1869                 .help = "UDP source port",
1870                 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1871                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1872                                              hdr.src_port)),
1873         },
1874         [ITEM_UDP_DST] = {
1875                 .name = "dst",
1876                 .help = "UDP destination port",
1877                 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1878                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1879                                              hdr.dst_port)),
1880         },
1881         [ITEM_TCP] = {
1882                 .name = "tcp",
1883                 .help = "match TCP header",
1884                 .priv = PRIV_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
1885                 .next = NEXT(item_tcp),
1886                 .call = parse_vc,
1887         },
1888         [ITEM_TCP_SRC] = {
1889                 .name = "src",
1890                 .help = "TCP source port",
1891                 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1892                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1893                                              hdr.src_port)),
1894         },
1895         [ITEM_TCP_DST] = {
1896                 .name = "dst",
1897                 .help = "TCP destination port",
1898                 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1899                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1900                                              hdr.dst_port)),
1901         },
1902         [ITEM_TCP_FLAGS] = {
1903                 .name = "flags",
1904                 .help = "TCP flags",
1905                 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1906                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1907                                              hdr.tcp_flags)),
1908         },
1909         [ITEM_SCTP] = {
1910                 .name = "sctp",
1911                 .help = "match SCTP header",
1912                 .priv = PRIV_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
1913                 .next = NEXT(item_sctp),
1914                 .call = parse_vc,
1915         },
1916         [ITEM_SCTP_SRC] = {
1917                 .name = "src",
1918                 .help = "SCTP source port",
1919                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1920                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1921                                              hdr.src_port)),
1922         },
1923         [ITEM_SCTP_DST] = {
1924                 .name = "dst",
1925                 .help = "SCTP destination port",
1926                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1927                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1928                                              hdr.dst_port)),
1929         },
1930         [ITEM_SCTP_TAG] = {
1931                 .name = "tag",
1932                 .help = "validation tag",
1933                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1934                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1935                                              hdr.tag)),
1936         },
1937         [ITEM_SCTP_CKSUM] = {
1938                 .name = "cksum",
1939                 .help = "checksum",
1940                 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1941                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1942                                              hdr.cksum)),
1943         },
1944         [ITEM_VXLAN] = {
1945                 .name = "vxlan",
1946                 .help = "match VXLAN header",
1947                 .priv = PRIV_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
1948                 .next = NEXT(item_vxlan),
1949                 .call = parse_vc,
1950         },
1951         [ITEM_VXLAN_VNI] = {
1952                 .name = "vni",
1953                 .help = "VXLAN identifier",
1954                 .next = NEXT(item_vxlan, NEXT_ENTRY(UNSIGNED), item_param),
1955                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vxlan, vni)),
1956         },
1957         [ITEM_E_TAG] = {
1958                 .name = "e_tag",
1959                 .help = "match E-Tag header",
1960                 .priv = PRIV_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
1961                 .next = NEXT(item_e_tag),
1962                 .call = parse_vc,
1963         },
1964         [ITEM_E_TAG_GRP_ECID_B] = {
1965                 .name = "grp_ecid_b",
1966                 .help = "GRP and E-CID base",
1967                 .next = NEXT(item_e_tag, NEXT_ENTRY(UNSIGNED), item_param),
1968                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_e_tag,
1969                                                   rsvd_grp_ecid_b,
1970                                                   "\x3f\xff")),
1971         },
1972         [ITEM_NVGRE] = {
1973                 .name = "nvgre",
1974                 .help = "match NVGRE header",
1975                 .priv = PRIV_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
1976                 .next = NEXT(item_nvgre),
1977                 .call = parse_vc,
1978         },
1979         [ITEM_NVGRE_TNI] = {
1980                 .name = "tni",
1981                 .help = "virtual subnet ID",
1982                 .next = NEXT(item_nvgre, NEXT_ENTRY(UNSIGNED), item_param),
1983                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_nvgre, tni)),
1984         },
1985         [ITEM_MPLS] = {
1986                 .name = "mpls",
1987                 .help = "match MPLS header",
1988                 .priv = PRIV_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
1989                 .next = NEXT(item_mpls),
1990                 .call = parse_vc,
1991         },
1992         [ITEM_MPLS_LABEL] = {
1993                 .name = "label",
1994                 .help = "MPLS label",
1995                 .next = NEXT(item_mpls, NEXT_ENTRY(UNSIGNED), item_param),
1996                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_mpls,
1997                                                   label_tc_s,
1998                                                   "\xff\xff\xf0")),
1999         },
2000         [ITEM_GRE] = {
2001                 .name = "gre",
2002                 .help = "match GRE header",
2003                 .priv = PRIV_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
2004                 .next = NEXT(item_gre),
2005                 .call = parse_vc,
2006         },
2007         [ITEM_GRE_PROTO] = {
2008                 .name = "protocol",
2009                 .help = "GRE protocol type",
2010                 .next = NEXT(item_gre, NEXT_ENTRY(UNSIGNED), item_param),
2011                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gre,
2012                                              protocol)),
2013         },
2014         [ITEM_GRE_C_RSVD0_VER] = {
2015                 .name = "c_rsvd0_ver",
2016                 .help =
2017                         "checksum (1b), undefined (1b), key bit (1b),"
2018                         " sequence number (1b), reserved 0 (9b),"
2019                         " version (3b)",
2020                 .next = NEXT(item_gre, NEXT_ENTRY(UNSIGNED), item_param),
2021                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gre,
2022                                              c_rsvd0_ver)),
2023         },
2024         [ITEM_GRE_C_BIT] = {
2025                 .name = "c_bit",
2026                 .help = "checksum bit (C)",
2027                 .next = NEXT(item_gre, NEXT_ENTRY(BOOLEAN), item_param),
2028                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_gre,
2029                                                   c_rsvd0_ver,
2030                                                   "\x80\x00\x00\x00")),
2031         },
2032         [ITEM_GRE_S_BIT] = {
2033                 .name = "s_bit",
2034                 .help = "sequence number bit (S)",
2035                 .next = NEXT(item_gre, NEXT_ENTRY(BOOLEAN), item_param),
2036                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_gre,
2037                                                   c_rsvd0_ver,
2038                                                   "\x10\x00\x00\x00")),
2039         },
2040         [ITEM_GRE_K_BIT] = {
2041                 .name = "k_bit",
2042                 .help = "key bit (K)",
2043                 .next = NEXT(item_gre, NEXT_ENTRY(BOOLEAN), item_param),
2044                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_gre,
2045                                                   c_rsvd0_ver,
2046                                                   "\x20\x00\x00\x00")),
2047         },
2048         [ITEM_FUZZY] = {
2049                 .name = "fuzzy",
2050                 .help = "fuzzy pattern match, expect faster than default",
2051                 .priv = PRIV_ITEM(FUZZY,
2052                                 sizeof(struct rte_flow_item_fuzzy)),
2053                 .next = NEXT(item_fuzzy),
2054                 .call = parse_vc,
2055         },
2056         [ITEM_FUZZY_THRESH] = {
2057                 .name = "thresh",
2058                 .help = "match accuracy threshold",
2059                 .next = NEXT(item_fuzzy, NEXT_ENTRY(UNSIGNED), item_param),
2060                 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_fuzzy,
2061                                         thresh)),
2062         },
2063         [ITEM_GTP] = {
2064                 .name = "gtp",
2065                 .help = "match GTP header",
2066                 .priv = PRIV_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
2067                 .next = NEXT(item_gtp),
2068                 .call = parse_vc,
2069         },
2070         [ITEM_GTP_TEID] = {
2071                 .name = "teid",
2072                 .help = "tunnel endpoint identifier",
2073                 .next = NEXT(item_gtp, NEXT_ENTRY(UNSIGNED), item_param),
2074                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gtp, teid)),
2075         },
2076         [ITEM_GTPC] = {
2077                 .name = "gtpc",
2078                 .help = "match GTP header",
2079                 .priv = PRIV_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
2080                 .next = NEXT(item_gtp),
2081                 .call = parse_vc,
2082         },
2083         [ITEM_GTPU] = {
2084                 .name = "gtpu",
2085                 .help = "match GTP header",
2086                 .priv = PRIV_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
2087                 .next = NEXT(item_gtp),
2088                 .call = parse_vc,
2089         },
2090         [ITEM_GENEVE] = {
2091                 .name = "geneve",
2092                 .help = "match GENEVE header",
2093                 .priv = PRIV_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
2094                 .next = NEXT(item_geneve),
2095                 .call = parse_vc,
2096         },
2097         [ITEM_GENEVE_VNI] = {
2098                 .name = "vni",
2099                 .help = "virtual network identifier",
2100                 .next = NEXT(item_geneve, NEXT_ENTRY(UNSIGNED), item_param),
2101                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_geneve, vni)),
2102         },
2103         [ITEM_GENEVE_PROTO] = {
2104                 .name = "protocol",
2105                 .help = "GENEVE protocol type",
2106                 .next = NEXT(item_geneve, NEXT_ENTRY(UNSIGNED), item_param),
2107                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_geneve,
2108                                              protocol)),
2109         },
2110         [ITEM_VXLAN_GPE] = {
2111                 .name = "vxlan-gpe",
2112                 .help = "match VXLAN-GPE header",
2113                 .priv = PRIV_ITEM(VXLAN_GPE,
2114                                   sizeof(struct rte_flow_item_vxlan_gpe)),
2115                 .next = NEXT(item_vxlan_gpe),
2116                 .call = parse_vc,
2117         },
2118         [ITEM_VXLAN_GPE_VNI] = {
2119                 .name = "vni",
2120                 .help = "VXLAN-GPE identifier",
2121                 .next = NEXT(item_vxlan_gpe, NEXT_ENTRY(UNSIGNED), item_param),
2122                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vxlan_gpe,
2123                                              vni)),
2124         },
2125         [ITEM_ARP_ETH_IPV4] = {
2126                 .name = "arp_eth_ipv4",
2127                 .help = "match ARP header for Ethernet/IPv4",
2128                 .priv = PRIV_ITEM(ARP_ETH_IPV4,
2129                                   sizeof(struct rte_flow_item_arp_eth_ipv4)),
2130                 .next = NEXT(item_arp_eth_ipv4),
2131                 .call = parse_vc,
2132         },
2133         [ITEM_ARP_ETH_IPV4_SHA] = {
2134                 .name = "sha",
2135                 .help = "sender hardware address",
2136                 .next = NEXT(item_arp_eth_ipv4, NEXT_ENTRY(MAC_ADDR),
2137                              item_param),
2138                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_arp_eth_ipv4,
2139                                              sha)),
2140         },
2141         [ITEM_ARP_ETH_IPV4_SPA] = {
2142                 .name = "spa",
2143                 .help = "sender IPv4 address",
2144                 .next = NEXT(item_arp_eth_ipv4, NEXT_ENTRY(IPV4_ADDR),
2145                              item_param),
2146                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_arp_eth_ipv4,
2147                                              spa)),
2148         },
2149         [ITEM_ARP_ETH_IPV4_THA] = {
2150                 .name = "tha",
2151                 .help = "target hardware address",
2152                 .next = NEXT(item_arp_eth_ipv4, NEXT_ENTRY(MAC_ADDR),
2153                              item_param),
2154                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_arp_eth_ipv4,
2155                                              tha)),
2156         },
2157         [ITEM_ARP_ETH_IPV4_TPA] = {
2158                 .name = "tpa",
2159                 .help = "target IPv4 address",
2160                 .next = NEXT(item_arp_eth_ipv4, NEXT_ENTRY(IPV4_ADDR),
2161                              item_param),
2162                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_arp_eth_ipv4,
2163                                              tpa)),
2164         },
2165         [ITEM_IPV6_EXT] = {
2166                 .name = "ipv6_ext",
2167                 .help = "match presence of any IPv6 extension header",
2168                 .priv = PRIV_ITEM(IPV6_EXT,
2169                                   sizeof(struct rte_flow_item_ipv6_ext)),
2170                 .next = NEXT(item_ipv6_ext),
2171                 .call = parse_vc,
2172         },
2173         [ITEM_IPV6_EXT_NEXT_HDR] = {
2174                 .name = "next_hdr",
2175                 .help = "next header",
2176                 .next = NEXT(item_ipv6_ext, NEXT_ENTRY(UNSIGNED), item_param),
2177                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6_ext,
2178                                              next_hdr)),
2179         },
2180         [ITEM_ICMP6] = {
2181                 .name = "icmp6",
2182                 .help = "match any ICMPv6 header",
2183                 .priv = PRIV_ITEM(ICMP6, sizeof(struct rte_flow_item_icmp6)),
2184                 .next = NEXT(item_icmp6),
2185                 .call = parse_vc,
2186         },
2187         [ITEM_ICMP6_TYPE] = {
2188                 .name = "type",
2189                 .help = "ICMPv6 type",
2190                 .next = NEXT(item_icmp6, NEXT_ENTRY(UNSIGNED), item_param),
2191                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp6,
2192                                              type)),
2193         },
2194         [ITEM_ICMP6_CODE] = {
2195                 .name = "code",
2196                 .help = "ICMPv6 code",
2197                 .next = NEXT(item_icmp6, NEXT_ENTRY(UNSIGNED), item_param),
2198                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp6,
2199                                              code)),
2200         },
2201         [ITEM_ICMP6_ND_NS] = {
2202                 .name = "icmp6_nd_ns",
2203                 .help = "match ICMPv6 neighbor discovery solicitation",
2204                 .priv = PRIV_ITEM(ICMP6_ND_NS,
2205                                   sizeof(struct rte_flow_item_icmp6_nd_ns)),
2206                 .next = NEXT(item_icmp6_nd_ns),
2207                 .call = parse_vc,
2208         },
2209         [ITEM_ICMP6_ND_NS_TARGET_ADDR] = {
2210                 .name = "target_addr",
2211                 .help = "target address",
2212                 .next = NEXT(item_icmp6_nd_ns, NEXT_ENTRY(IPV6_ADDR),
2213                              item_param),
2214                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp6_nd_ns,
2215                                              target_addr)),
2216         },
2217         [ITEM_ICMP6_ND_NA] = {
2218                 .name = "icmp6_nd_na",
2219                 .help = "match ICMPv6 neighbor discovery advertisement",
2220                 .priv = PRIV_ITEM(ICMP6_ND_NA,
2221                                   sizeof(struct rte_flow_item_icmp6_nd_na)),
2222                 .next = NEXT(item_icmp6_nd_na),
2223                 .call = parse_vc,
2224         },
2225         [ITEM_ICMP6_ND_NA_TARGET_ADDR] = {
2226                 .name = "target_addr",
2227                 .help = "target address",
2228                 .next = NEXT(item_icmp6_nd_na, NEXT_ENTRY(IPV6_ADDR),
2229                              item_param),
2230                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp6_nd_na,
2231                                              target_addr)),
2232         },
2233         [ITEM_ICMP6_ND_OPT] = {
2234                 .name = "icmp6_nd_opt",
2235                 .help = "match presence of any ICMPv6 neighbor discovery"
2236                         " option",
2237                 .priv = PRIV_ITEM(ICMP6_ND_OPT,
2238                                   sizeof(struct rte_flow_item_icmp6_nd_opt)),
2239                 .next = NEXT(item_icmp6_nd_opt),
2240                 .call = parse_vc,
2241         },
2242         [ITEM_ICMP6_ND_OPT_TYPE] = {
2243                 .name = "type",
2244                 .help = "ND option type",
2245                 .next = NEXT(item_icmp6_nd_opt, NEXT_ENTRY(UNSIGNED),
2246                              item_param),
2247                 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp6_nd_opt,
2248                                              type)),
2249         },
2250         [ITEM_ICMP6_ND_OPT_SLA_ETH] = {
2251                 .name = "icmp6_nd_opt_sla_eth",
2252                 .help = "match ICMPv6 neighbor discovery source Ethernet"
2253                         " link-layer address option",
2254                 .priv = PRIV_ITEM
2255                         (ICMP6_ND_OPT_SLA_ETH,
2256                          sizeof(struct rte_flow_item_icmp6_nd_opt_sla_eth)),
2257                 .next = NEXT(item_icmp6_nd_opt_sla_eth),
2258                 .call = parse_vc,
2259         },
2260         [ITEM_ICMP6_ND_OPT_SLA_ETH_SLA] = {
2261                 .name = "sla",
2262                 .help = "source Ethernet LLA",
2263                 .next = NEXT(item_icmp6_nd_opt_sla_eth, NEXT_ENTRY(MAC_ADDR),
2264                              item_param),
2265                 .args = ARGS(ARGS_ENTRY_HTON
2266                              (struct rte_flow_item_icmp6_nd_opt_sla_eth, sla)),
2267         },
2268         [ITEM_ICMP6_ND_OPT_TLA_ETH] = {
2269                 .name = "icmp6_nd_opt_tla_eth",
2270                 .help = "match ICMPv6 neighbor discovery target Ethernet"
2271                         " link-layer address option",
2272                 .priv = PRIV_ITEM
2273                         (ICMP6_ND_OPT_TLA_ETH,
2274                          sizeof(struct rte_flow_item_icmp6_nd_opt_tla_eth)),
2275                 .next = NEXT(item_icmp6_nd_opt_tla_eth),
2276                 .call = parse_vc,
2277         },
2278         [ITEM_ICMP6_ND_OPT_TLA_ETH_TLA] = {
2279                 .name = "tla",
2280                 .help = "target Ethernet LLA",
2281                 .next = NEXT(item_icmp6_nd_opt_tla_eth, NEXT_ENTRY(MAC_ADDR),
2282                              item_param),
2283                 .args = ARGS(ARGS_ENTRY_HTON
2284                              (struct rte_flow_item_icmp6_nd_opt_tla_eth, tla)),
2285         },
2286         [ITEM_META] = {
2287                 .name = "meta",
2288                 .help = "match metadata header",
2289                 .priv = PRIV_ITEM(META, sizeof(struct rte_flow_item_meta)),
2290                 .next = NEXT(item_meta),
2291                 .call = parse_vc,
2292         },
2293         [ITEM_META_DATA] = {
2294                 .name = "data",
2295                 .help = "metadata value",
2296                 .next = NEXT(item_meta, NEXT_ENTRY(UNSIGNED), item_param),
2297                 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_meta,
2298                                                   data, "\xff\xff\xff\xff")),
2299         },
2300         [ITEM_GRE_KEY] = {
2301                 .name = "gre_key",
2302                 .help = "match GRE key",
2303                 .priv = PRIV_ITEM(GRE_KEY, sizeof(rte_be32_t)),
2304                 .next = NEXT(item_gre_key),
2305                 .call = parse_vc,
2306         },
2307         [ITEM_GRE_KEY_VALUE] = {
2308                 .name = "value",
2309                 .help = "key value",
2310                 .next = NEXT(item_gre_key, NEXT_ENTRY(UNSIGNED), item_param),
2311                 .args = ARGS(ARG_ENTRY_HTON(rte_be32_t)),
2312         },
2313
2314         /* Validate/create actions. */
2315         [ACTIONS] = {
2316                 .name = "actions",
2317                 .help = "submit a list of associated actions",
2318                 .next = NEXT(next_action),
2319                 .call = parse_vc,
2320         },
2321         [ACTION_NEXT] = {
2322                 .name = "/",
2323                 .help = "specify next action",
2324                 .next = NEXT(next_action),
2325         },
2326         [ACTION_END] = {
2327                 .name = "end",
2328                 .help = "end list of actions",
2329                 .priv = PRIV_ACTION(END, 0),
2330                 .call = parse_vc,
2331         },
2332         [ACTION_VOID] = {
2333                 .name = "void",
2334                 .help = "no-op action",
2335                 .priv = PRIV_ACTION(VOID, 0),
2336                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2337                 .call = parse_vc,
2338         },
2339         [ACTION_PASSTHRU] = {
2340                 .name = "passthru",
2341                 .help = "let subsequent rule process matched packets",
2342                 .priv = PRIV_ACTION(PASSTHRU, 0),
2343                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2344                 .call = parse_vc,
2345         },
2346         [ACTION_JUMP] = {
2347                 .name = "jump",
2348                 .help = "redirect traffic to a given group",
2349                 .priv = PRIV_ACTION(JUMP, sizeof(struct rte_flow_action_jump)),
2350                 .next = NEXT(action_jump),
2351                 .call = parse_vc,
2352         },
2353         [ACTION_JUMP_GROUP] = {
2354                 .name = "group",
2355                 .help = "group to redirect traffic to",
2356                 .next = NEXT(action_jump, NEXT_ENTRY(UNSIGNED)),
2357                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_jump, group)),
2358                 .call = parse_vc_conf,
2359         },
2360         [ACTION_MARK] = {
2361                 .name = "mark",
2362                 .help = "attach 32 bit value to packets",
2363                 .priv = PRIV_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
2364                 .next = NEXT(action_mark),
2365                 .call = parse_vc,
2366         },
2367         [ACTION_MARK_ID] = {
2368                 .name = "id",
2369                 .help = "32 bit value to return with packets",
2370                 .next = NEXT(action_mark, NEXT_ENTRY(UNSIGNED)),
2371                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_mark, id)),
2372                 .call = parse_vc_conf,
2373         },
2374         [ACTION_FLAG] = {
2375                 .name = "flag",
2376                 .help = "flag packets",
2377                 .priv = PRIV_ACTION(FLAG, 0),
2378                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2379                 .call = parse_vc,
2380         },
2381         [ACTION_QUEUE] = {
2382                 .name = "queue",
2383                 .help = "assign packets to a given queue index",
2384                 .priv = PRIV_ACTION(QUEUE,
2385                                     sizeof(struct rte_flow_action_queue)),
2386                 .next = NEXT(action_queue),
2387                 .call = parse_vc,
2388         },
2389         [ACTION_QUEUE_INDEX] = {
2390                 .name = "index",
2391                 .help = "queue index to use",
2392                 .next = NEXT(action_queue, NEXT_ENTRY(UNSIGNED)),
2393                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_queue, index)),
2394                 .call = parse_vc_conf,
2395         },
2396         [ACTION_DROP] = {
2397                 .name = "drop",
2398                 .help = "drop packets (note: passthru has priority)",
2399                 .priv = PRIV_ACTION(DROP, 0),
2400                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2401                 .call = parse_vc,
2402         },
2403         [ACTION_COUNT] = {
2404                 .name = "count",
2405                 .help = "enable counters for this rule",
2406                 .priv = PRIV_ACTION(COUNT,
2407                                     sizeof(struct rte_flow_action_count)),
2408                 .next = NEXT(action_count),
2409                 .call = parse_vc,
2410         },
2411         [ACTION_COUNT_ID] = {
2412                 .name = "identifier",
2413                 .help = "counter identifier to use",
2414                 .next = NEXT(action_count, NEXT_ENTRY(UNSIGNED)),
2415                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_count, id)),
2416                 .call = parse_vc_conf,
2417         },
2418         [ACTION_COUNT_SHARED] = {
2419                 .name = "shared",
2420                 .help = "shared counter",
2421                 .next = NEXT(action_count, NEXT_ENTRY(BOOLEAN)),
2422                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_count,
2423                                            shared, 1)),
2424                 .call = parse_vc_conf,
2425         },
2426         [ACTION_RSS] = {
2427                 .name = "rss",
2428                 .help = "spread packets among several queues",
2429                 .priv = PRIV_ACTION(RSS, sizeof(struct action_rss_data)),
2430                 .next = NEXT(action_rss),
2431                 .call = parse_vc_action_rss,
2432         },
2433         [ACTION_RSS_FUNC] = {
2434                 .name = "func",
2435                 .help = "RSS hash function to apply",
2436                 .next = NEXT(action_rss,
2437                              NEXT_ENTRY(ACTION_RSS_FUNC_DEFAULT,
2438                                         ACTION_RSS_FUNC_TOEPLITZ,
2439                                         ACTION_RSS_FUNC_SIMPLE_XOR)),
2440         },
2441         [ACTION_RSS_FUNC_DEFAULT] = {
2442                 .name = "default",
2443                 .help = "default hash function",
2444                 .call = parse_vc_action_rss_func,
2445         },
2446         [ACTION_RSS_FUNC_TOEPLITZ] = {
2447                 .name = "toeplitz",
2448                 .help = "Toeplitz hash function",
2449                 .call = parse_vc_action_rss_func,
2450         },
2451         [ACTION_RSS_FUNC_SIMPLE_XOR] = {
2452                 .name = "simple_xor",
2453                 .help = "simple XOR hash function",
2454                 .call = parse_vc_action_rss_func,
2455         },
2456         [ACTION_RSS_LEVEL] = {
2457                 .name = "level",
2458                 .help = "encapsulation level for \"types\"",
2459                 .next = NEXT(action_rss, NEXT_ENTRY(UNSIGNED)),
2460                 .args = ARGS(ARGS_ENTRY_ARB
2461                              (offsetof(struct action_rss_data, conf) +
2462                               offsetof(struct rte_flow_action_rss, level),
2463                               sizeof(((struct rte_flow_action_rss *)0)->
2464                                      level))),
2465         },
2466         [ACTION_RSS_TYPES] = {
2467                 .name = "types",
2468                 .help = "specific RSS hash types",
2469                 .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_TYPE)),
2470         },
2471         [ACTION_RSS_TYPE] = {
2472                 .name = "{type}",
2473                 .help = "RSS hash type",
2474                 .call = parse_vc_action_rss_type,
2475                 .comp = comp_vc_action_rss_type,
2476         },
2477         [ACTION_RSS_KEY] = {
2478                 .name = "key",
2479                 .help = "RSS hash key",
2480                 .next = NEXT(action_rss, NEXT_ENTRY(HEX)),
2481                 .args = ARGS(ARGS_ENTRY_ARB(0, 0),
2482                              ARGS_ENTRY_ARB
2483                              (offsetof(struct action_rss_data, conf) +
2484                               offsetof(struct rte_flow_action_rss, key_len),
2485                               sizeof(((struct rte_flow_action_rss *)0)->
2486                                      key_len)),
2487                              ARGS_ENTRY(struct action_rss_data, key)),
2488         },
2489         [ACTION_RSS_KEY_LEN] = {
2490                 .name = "key_len",
2491                 .help = "RSS hash key length in bytes",
2492                 .next = NEXT(action_rss, NEXT_ENTRY(UNSIGNED)),
2493                 .args = ARGS(ARGS_ENTRY_ARB_BOUNDED
2494                              (offsetof(struct action_rss_data, conf) +
2495                               offsetof(struct rte_flow_action_rss, key_len),
2496                               sizeof(((struct rte_flow_action_rss *)0)->
2497                                      key_len),
2498                               0,
2499                               RSS_HASH_KEY_LENGTH)),
2500         },
2501         [ACTION_RSS_QUEUES] = {
2502                 .name = "queues",
2503                 .help = "queue indices to use",
2504                 .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_QUEUE)),
2505                 .call = parse_vc_conf,
2506         },
2507         [ACTION_RSS_QUEUE] = {
2508                 .name = "{queue}",
2509                 .help = "queue index",
2510                 .call = parse_vc_action_rss_queue,
2511                 .comp = comp_vc_action_rss_queue,
2512         },
2513         [ACTION_PF] = {
2514                 .name = "pf",
2515                 .help = "direct traffic to physical function",
2516                 .priv = PRIV_ACTION(PF, 0),
2517                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2518                 .call = parse_vc,
2519         },
2520         [ACTION_VF] = {
2521                 .name = "vf",
2522                 .help = "direct traffic to a virtual function ID",
2523                 .priv = PRIV_ACTION(VF, sizeof(struct rte_flow_action_vf)),
2524                 .next = NEXT(action_vf),
2525                 .call = parse_vc,
2526         },
2527         [ACTION_VF_ORIGINAL] = {
2528                 .name = "original",
2529                 .help = "use original VF ID if possible",
2530                 .next = NEXT(action_vf, NEXT_ENTRY(BOOLEAN)),
2531                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_vf,
2532                                            original, 1)),
2533                 .call = parse_vc_conf,
2534         },
2535         [ACTION_VF_ID] = {
2536                 .name = "id",
2537                 .help = "VF ID",
2538                 .next = NEXT(action_vf, NEXT_ENTRY(UNSIGNED)),
2539                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_vf, id)),
2540                 .call = parse_vc_conf,
2541         },
2542         [ACTION_PHY_PORT] = {
2543                 .name = "phy_port",
2544                 .help = "direct packets to physical port index",
2545                 .priv = PRIV_ACTION(PHY_PORT,
2546                                     sizeof(struct rte_flow_action_phy_port)),
2547                 .next = NEXT(action_phy_port),
2548                 .call = parse_vc,
2549         },
2550         [ACTION_PHY_PORT_ORIGINAL] = {
2551                 .name = "original",
2552                 .help = "use original port index if possible",
2553                 .next = NEXT(action_phy_port, NEXT_ENTRY(BOOLEAN)),
2554                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_phy_port,
2555                                            original, 1)),
2556                 .call = parse_vc_conf,
2557         },
2558         [ACTION_PHY_PORT_INDEX] = {
2559                 .name = "index",
2560                 .help = "physical port index",
2561                 .next = NEXT(action_phy_port, NEXT_ENTRY(UNSIGNED)),
2562                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_phy_port,
2563                                         index)),
2564                 .call = parse_vc_conf,
2565         },
2566         [ACTION_PORT_ID] = {
2567                 .name = "port_id",
2568                 .help = "direct matching traffic to a given DPDK port ID",
2569                 .priv = PRIV_ACTION(PORT_ID,
2570                                     sizeof(struct rte_flow_action_port_id)),
2571                 .next = NEXT(action_port_id),
2572                 .call = parse_vc,
2573         },
2574         [ACTION_PORT_ID_ORIGINAL] = {
2575                 .name = "original",
2576                 .help = "use original DPDK port ID if possible",
2577                 .next = NEXT(action_port_id, NEXT_ENTRY(BOOLEAN)),
2578                 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_port_id,
2579                                            original, 1)),
2580                 .call = parse_vc_conf,
2581         },
2582         [ACTION_PORT_ID_ID] = {
2583                 .name = "id",
2584                 .help = "DPDK port ID",
2585                 .next = NEXT(action_port_id, NEXT_ENTRY(UNSIGNED)),
2586                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_port_id, id)),
2587                 .call = parse_vc_conf,
2588         },
2589         [ACTION_METER] = {
2590                 .name = "meter",
2591                 .help = "meter the directed packets at given id",
2592                 .priv = PRIV_ACTION(METER,
2593                                     sizeof(struct rte_flow_action_meter)),
2594                 .next = NEXT(action_meter),
2595                 .call = parse_vc,
2596         },
2597         [ACTION_METER_ID] = {
2598                 .name = "mtr_id",
2599                 .help = "meter id to use",
2600                 .next = NEXT(action_meter, NEXT_ENTRY(UNSIGNED)),
2601                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_meter, mtr_id)),
2602                 .call = parse_vc_conf,
2603         },
2604         [ACTION_OF_SET_MPLS_TTL] = {
2605                 .name = "of_set_mpls_ttl",
2606                 .help = "OpenFlow's OFPAT_SET_MPLS_TTL",
2607                 .priv = PRIV_ACTION
2608                         (OF_SET_MPLS_TTL,
2609                          sizeof(struct rte_flow_action_of_set_mpls_ttl)),
2610                 .next = NEXT(action_of_set_mpls_ttl),
2611                 .call = parse_vc,
2612         },
2613         [ACTION_OF_SET_MPLS_TTL_MPLS_TTL] = {
2614                 .name = "mpls_ttl",
2615                 .help = "MPLS TTL",
2616                 .next = NEXT(action_of_set_mpls_ttl, NEXT_ENTRY(UNSIGNED)),
2617                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_of_set_mpls_ttl,
2618                                         mpls_ttl)),
2619                 .call = parse_vc_conf,
2620         },
2621         [ACTION_OF_DEC_MPLS_TTL] = {
2622                 .name = "of_dec_mpls_ttl",
2623                 .help = "OpenFlow's OFPAT_DEC_MPLS_TTL",
2624                 .priv = PRIV_ACTION(OF_DEC_MPLS_TTL, 0),
2625                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2626                 .call = parse_vc,
2627         },
2628         [ACTION_OF_SET_NW_TTL] = {
2629                 .name = "of_set_nw_ttl",
2630                 .help = "OpenFlow's OFPAT_SET_NW_TTL",
2631                 .priv = PRIV_ACTION
2632                         (OF_SET_NW_TTL,
2633                          sizeof(struct rte_flow_action_of_set_nw_ttl)),
2634                 .next = NEXT(action_of_set_nw_ttl),
2635                 .call = parse_vc,
2636         },
2637         [ACTION_OF_SET_NW_TTL_NW_TTL] = {
2638                 .name = "nw_ttl",
2639                 .help = "IP TTL",
2640                 .next = NEXT(action_of_set_nw_ttl, NEXT_ENTRY(UNSIGNED)),
2641                 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_of_set_nw_ttl,
2642                                         nw_ttl)),
2643                 .call = parse_vc_conf,
2644         },
2645         [ACTION_OF_DEC_NW_TTL] = {
2646                 .name = "of_dec_nw_ttl",
2647                 .help = "OpenFlow's OFPAT_DEC_NW_TTL",
2648                 .priv = PRIV_ACTION(OF_DEC_NW_TTL, 0),
2649                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2650                 .call = parse_vc,
2651         },
2652         [ACTION_OF_COPY_TTL_OUT] = {
2653                 .name = "of_copy_ttl_out",
2654                 .help = "OpenFlow's OFPAT_COPY_TTL_OUT",
2655                 .priv = PRIV_ACTION(OF_COPY_TTL_OUT, 0),
2656                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2657                 .call = parse_vc,
2658         },
2659         [ACTION_OF_COPY_TTL_IN] = {
2660                 .name = "of_copy_ttl_in",
2661                 .help = "OpenFlow's OFPAT_COPY_TTL_IN",
2662                 .priv = PRIV_ACTION(OF_COPY_TTL_IN, 0),
2663                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2664                 .call = parse_vc,
2665         },
2666         [ACTION_OF_POP_VLAN] = {
2667                 .name = "of_pop_vlan",
2668                 .help = "OpenFlow's OFPAT_POP_VLAN",
2669                 .priv = PRIV_ACTION(OF_POP_VLAN, 0),
2670                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2671                 .call = parse_vc,
2672         },
2673         [ACTION_OF_PUSH_VLAN] = {
2674                 .name = "of_push_vlan",
2675                 .help = "OpenFlow's OFPAT_PUSH_VLAN",
2676                 .priv = PRIV_ACTION
2677                         (OF_PUSH_VLAN,
2678                          sizeof(struct rte_flow_action_of_push_vlan)),
2679                 .next = NEXT(action_of_push_vlan),
2680                 .call = parse_vc,
2681         },
2682         [ACTION_OF_PUSH_VLAN_ETHERTYPE] = {
2683                 .name = "ethertype",
2684                 .help = "EtherType",
2685                 .next = NEXT(action_of_push_vlan, NEXT_ENTRY(UNSIGNED)),
2686                 .args = ARGS(ARGS_ENTRY_HTON
2687                              (struct rte_flow_action_of_push_vlan,
2688                               ethertype)),
2689                 .call = parse_vc_conf,
2690         },
2691         [ACTION_OF_SET_VLAN_VID] = {
2692                 .name = "of_set_vlan_vid",
2693                 .help = "OpenFlow's OFPAT_SET_VLAN_VID",
2694                 .priv = PRIV_ACTION
2695                         (OF_SET_VLAN_VID,
2696                          sizeof(struct rte_flow_action_of_set_vlan_vid)),
2697                 .next = NEXT(action_of_set_vlan_vid),
2698                 .call = parse_vc,
2699         },
2700         [ACTION_OF_SET_VLAN_VID_VLAN_VID] = {
2701                 .name = "vlan_vid",
2702                 .help = "VLAN id",
2703                 .next = NEXT(action_of_set_vlan_vid, NEXT_ENTRY(UNSIGNED)),
2704                 .args = ARGS(ARGS_ENTRY_HTON
2705                              (struct rte_flow_action_of_set_vlan_vid,
2706                               vlan_vid)),
2707                 .call = parse_vc_conf,
2708         },
2709         [ACTION_OF_SET_VLAN_PCP] = {
2710                 .name = "of_set_vlan_pcp",
2711                 .help = "OpenFlow's OFPAT_SET_VLAN_PCP",
2712                 .priv = PRIV_ACTION
2713                         (OF_SET_VLAN_PCP,
2714                          sizeof(struct rte_flow_action_of_set_vlan_pcp)),
2715                 .next = NEXT(action_of_set_vlan_pcp),
2716                 .call = parse_vc,
2717         },
2718         [ACTION_OF_SET_VLAN_PCP_VLAN_PCP] = {
2719                 .name = "vlan_pcp",
2720                 .help = "VLAN priority",
2721                 .next = NEXT(action_of_set_vlan_pcp, NEXT_ENTRY(UNSIGNED)),
2722                 .args = ARGS(ARGS_ENTRY_HTON
2723                              (struct rte_flow_action_of_set_vlan_pcp,
2724                               vlan_pcp)),
2725                 .call = parse_vc_conf,
2726         },
2727         [ACTION_OF_POP_MPLS] = {
2728                 .name = "of_pop_mpls",
2729                 .help = "OpenFlow's OFPAT_POP_MPLS",
2730                 .priv = PRIV_ACTION(OF_POP_MPLS,
2731                                     sizeof(struct rte_flow_action_of_pop_mpls)),
2732                 .next = NEXT(action_of_pop_mpls),
2733                 .call = parse_vc,
2734         },
2735         [ACTION_OF_POP_MPLS_ETHERTYPE] = {
2736                 .name = "ethertype",
2737                 .help = "EtherType",
2738                 .next = NEXT(action_of_pop_mpls, NEXT_ENTRY(UNSIGNED)),
2739                 .args = ARGS(ARGS_ENTRY_HTON
2740                              (struct rte_flow_action_of_pop_mpls,
2741                               ethertype)),
2742                 .call = parse_vc_conf,
2743         },
2744         [ACTION_OF_PUSH_MPLS] = {
2745                 .name = "of_push_mpls",
2746                 .help = "OpenFlow's OFPAT_PUSH_MPLS",
2747                 .priv = PRIV_ACTION
2748                         (OF_PUSH_MPLS,
2749                          sizeof(struct rte_flow_action_of_push_mpls)),
2750                 .next = NEXT(action_of_push_mpls),
2751                 .call = parse_vc,
2752         },
2753         [ACTION_OF_PUSH_MPLS_ETHERTYPE] = {
2754                 .name = "ethertype",
2755                 .help = "EtherType",
2756                 .next = NEXT(action_of_push_mpls, NEXT_ENTRY(UNSIGNED)),
2757                 .args = ARGS(ARGS_ENTRY_HTON
2758                              (struct rte_flow_action_of_push_mpls,
2759                               ethertype)),
2760                 .call = parse_vc_conf,
2761         },
2762         [ACTION_VXLAN_ENCAP] = {
2763                 .name = "vxlan_encap",
2764                 .help = "VXLAN encapsulation, uses configuration set by \"set"
2765                         " vxlan\"",
2766                 .priv = PRIV_ACTION(VXLAN_ENCAP,
2767                                     sizeof(struct action_vxlan_encap_data)),
2768                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2769                 .call = parse_vc_action_vxlan_encap,
2770         },
2771         [ACTION_VXLAN_DECAP] = {
2772                 .name = "vxlan_decap",
2773                 .help = "Performs a decapsulation action by stripping all"
2774                         " headers of the VXLAN tunnel network overlay from the"
2775                         " matched flow.",
2776                 .priv = PRIV_ACTION(VXLAN_DECAP, 0),
2777                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2778                 .call = parse_vc,
2779         },
2780         [ACTION_NVGRE_ENCAP] = {
2781                 .name = "nvgre_encap",
2782                 .help = "NVGRE encapsulation, uses configuration set by \"set"
2783                         " nvgre\"",
2784                 .priv = PRIV_ACTION(NVGRE_ENCAP,
2785                                     sizeof(struct action_nvgre_encap_data)),
2786                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2787                 .call = parse_vc_action_nvgre_encap,
2788         },
2789         [ACTION_NVGRE_DECAP] = {
2790                 .name = "nvgre_decap",
2791                 .help = "Performs a decapsulation action by stripping all"
2792                         " headers of the NVGRE tunnel network overlay from the"
2793                         " matched flow.",
2794                 .priv = PRIV_ACTION(NVGRE_DECAP, 0),
2795                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2796                 .call = parse_vc,
2797         },
2798         [ACTION_L2_ENCAP] = {
2799                 .name = "l2_encap",
2800                 .help = "l2 encap, uses configuration set by"
2801                         " \"set l2_encap\"",
2802                 .priv = PRIV_ACTION(RAW_ENCAP,
2803                                     sizeof(struct action_raw_encap_data)),
2804                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2805                 .call = parse_vc_action_l2_encap,
2806         },
2807         [ACTION_L2_DECAP] = {
2808                 .name = "l2_decap",
2809                 .help = "l2 decap, uses configuration set by"
2810                         " \"set l2_decap\"",
2811                 .priv = PRIV_ACTION(RAW_DECAP,
2812                                     sizeof(struct action_raw_decap_data)),
2813                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2814                 .call = parse_vc_action_l2_decap,
2815         },
2816         [ACTION_MPLSOGRE_ENCAP] = {
2817                 .name = "mplsogre_encap",
2818                 .help = "mplsogre encapsulation, uses configuration set by"
2819                         " \"set mplsogre_encap\"",
2820                 .priv = PRIV_ACTION(RAW_ENCAP,
2821                                     sizeof(struct action_raw_encap_data)),
2822                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2823                 .call = parse_vc_action_mplsogre_encap,
2824         },
2825         [ACTION_MPLSOGRE_DECAP] = {
2826                 .name = "mplsogre_decap",
2827                 .help = "mplsogre decapsulation, uses configuration set by"
2828                         " \"set mplsogre_decap\"",
2829                 .priv = PRIV_ACTION(RAW_DECAP,
2830                                     sizeof(struct action_raw_decap_data)),
2831                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2832                 .call = parse_vc_action_mplsogre_decap,
2833         },
2834         [ACTION_MPLSOUDP_ENCAP] = {
2835                 .name = "mplsoudp_encap",
2836                 .help = "mplsoudp encapsulation, uses configuration set by"
2837                         " \"set mplsoudp_encap\"",
2838                 .priv = PRIV_ACTION(RAW_ENCAP,
2839                                     sizeof(struct action_raw_encap_data)),
2840                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2841                 .call = parse_vc_action_mplsoudp_encap,
2842         },
2843         [ACTION_MPLSOUDP_DECAP] = {
2844                 .name = "mplsoudp_decap",
2845                 .help = "mplsoudp decapsulation, uses configuration set by"
2846                         " \"set mplsoudp_decap\"",
2847                 .priv = PRIV_ACTION(RAW_DECAP,
2848                                     sizeof(struct action_raw_decap_data)),
2849                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2850                 .call = parse_vc_action_mplsoudp_decap,
2851         },
2852         [ACTION_SET_IPV4_SRC] = {
2853                 .name = "set_ipv4_src",
2854                 .help = "Set a new IPv4 source address in the outermost"
2855                         " IPv4 header",
2856                 .priv = PRIV_ACTION(SET_IPV4_SRC,
2857                         sizeof(struct rte_flow_action_set_ipv4)),
2858                 .next = NEXT(action_set_ipv4_src),
2859                 .call = parse_vc,
2860         },
2861         [ACTION_SET_IPV4_SRC_IPV4_SRC] = {
2862                 .name = "ipv4_addr",
2863                 .help = "new IPv4 source address to set",
2864                 .next = NEXT(action_set_ipv4_src, NEXT_ENTRY(IPV4_ADDR)),
2865                 .args = ARGS(ARGS_ENTRY_HTON
2866                         (struct rte_flow_action_set_ipv4, ipv4_addr)),
2867                 .call = parse_vc_conf,
2868         },
2869         [ACTION_SET_IPV4_DST] = {
2870                 .name = "set_ipv4_dst",
2871                 .help = "Set a new IPv4 destination address in the outermost"
2872                         " IPv4 header",
2873                 .priv = PRIV_ACTION(SET_IPV4_DST,
2874                         sizeof(struct rte_flow_action_set_ipv4)),
2875                 .next = NEXT(action_set_ipv4_dst),
2876                 .call = parse_vc,
2877         },
2878         [ACTION_SET_IPV4_DST_IPV4_DST] = {
2879                 .name = "ipv4_addr",
2880                 .help = "new IPv4 destination address to set",
2881                 .next = NEXT(action_set_ipv4_dst, NEXT_ENTRY(IPV4_ADDR)),
2882                 .args = ARGS(ARGS_ENTRY_HTON
2883                         (struct rte_flow_action_set_ipv4, ipv4_addr)),
2884                 .call = parse_vc_conf,
2885         },
2886         [ACTION_SET_IPV6_SRC] = {
2887                 .name = "set_ipv6_src",
2888                 .help = "Set a new IPv6 source address in the outermost"
2889                         " IPv6 header",
2890                 .priv = PRIV_ACTION(SET_IPV6_SRC,
2891                         sizeof(struct rte_flow_action_set_ipv6)),
2892                 .next = NEXT(action_set_ipv6_src),
2893                 .call = parse_vc,
2894         },
2895         [ACTION_SET_IPV6_SRC_IPV6_SRC] = {
2896                 .name = "ipv6_addr",
2897                 .help = "new IPv6 source address to set",
2898                 .next = NEXT(action_set_ipv6_src, NEXT_ENTRY(IPV6_ADDR)),
2899                 .args = ARGS(ARGS_ENTRY_HTON
2900                         (struct rte_flow_action_set_ipv6, ipv6_addr)),
2901                 .call = parse_vc_conf,
2902         },
2903         [ACTION_SET_IPV6_DST] = {
2904                 .name = "set_ipv6_dst",
2905                 .help = "Set a new IPv6 destination address in the outermost"
2906                         " IPv6 header",
2907                 .priv = PRIV_ACTION(SET_IPV6_DST,
2908                         sizeof(struct rte_flow_action_set_ipv6)),
2909                 .next = NEXT(action_set_ipv6_dst),
2910                 .call = parse_vc,
2911         },
2912         [ACTION_SET_IPV6_DST_IPV6_DST] = {
2913                 .name = "ipv6_addr",
2914                 .help = "new IPv6 destination address to set",
2915                 .next = NEXT(action_set_ipv6_dst, NEXT_ENTRY(IPV6_ADDR)),
2916                 .args = ARGS(ARGS_ENTRY_HTON
2917                         (struct rte_flow_action_set_ipv6, ipv6_addr)),
2918                 .call = parse_vc_conf,
2919         },
2920         [ACTION_SET_TP_SRC] = {
2921                 .name = "set_tp_src",
2922                 .help = "set a new source port number in the outermost"
2923                         " TCP/UDP header",
2924                 .priv = PRIV_ACTION(SET_TP_SRC,
2925                         sizeof(struct rte_flow_action_set_tp)),
2926                 .next = NEXT(action_set_tp_src),
2927                 .call = parse_vc,
2928         },
2929         [ACTION_SET_TP_SRC_TP_SRC] = {
2930                 .name = "port",
2931                 .help = "new source port number to set",
2932                 .next = NEXT(action_set_tp_src, NEXT_ENTRY(UNSIGNED)),
2933                 .args = ARGS(ARGS_ENTRY_HTON
2934                              (struct rte_flow_action_set_tp, port)),
2935                 .call = parse_vc_conf,
2936         },
2937         [ACTION_SET_TP_DST] = {
2938                 .name = "set_tp_dst",
2939                 .help = "set a new destination port number in the outermost"
2940                         " TCP/UDP header",
2941                 .priv = PRIV_ACTION(SET_TP_DST,
2942                         sizeof(struct rte_flow_action_set_tp)),
2943                 .next = NEXT(action_set_tp_dst),
2944                 .call = parse_vc,
2945         },
2946         [ACTION_SET_TP_DST_TP_DST] = {
2947                 .name = "port",
2948                 .help = "new destination port number to set",
2949                 .next = NEXT(action_set_tp_dst, NEXT_ENTRY(UNSIGNED)),
2950                 .args = ARGS(ARGS_ENTRY_HTON
2951                              (struct rte_flow_action_set_tp, port)),
2952                 .call = parse_vc_conf,
2953         },
2954         [ACTION_MAC_SWAP] = {
2955                 .name = "mac_swap",
2956                 .help = "Swap the source and destination MAC addresses"
2957                         " in the outermost Ethernet header",
2958                 .priv = PRIV_ACTION(MAC_SWAP, 0),
2959                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2960                 .call = parse_vc,
2961         },
2962         [ACTION_DEC_TTL] = {
2963                 .name = "dec_ttl",
2964                 .help = "decrease network TTL if available",
2965                 .priv = PRIV_ACTION(DEC_TTL, 0),
2966                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
2967                 .call = parse_vc,
2968         },
2969         [ACTION_SET_TTL] = {
2970                 .name = "set_ttl",
2971                 .help = "set ttl value",
2972                 .priv = PRIV_ACTION(SET_TTL,
2973                         sizeof(struct rte_flow_action_set_ttl)),
2974                 .next = NEXT(action_set_ttl),
2975                 .call = parse_vc,
2976         },
2977         [ACTION_SET_TTL_TTL] = {
2978                 .name = "ttl_value",
2979                 .help = "new ttl value to set",
2980                 .next = NEXT(action_set_ttl, NEXT_ENTRY(UNSIGNED)),
2981                 .args = ARGS(ARGS_ENTRY_HTON
2982                              (struct rte_flow_action_set_ttl, ttl_value)),
2983                 .call = parse_vc_conf,
2984         },
2985         [ACTION_SET_MAC_SRC] = {
2986                 .name = "set_mac_src",
2987                 .help = "set source mac address",
2988                 .priv = PRIV_ACTION(SET_MAC_SRC,
2989                         sizeof(struct rte_flow_action_set_mac)),
2990                 .next = NEXT(action_set_mac_src),
2991                 .call = parse_vc,
2992         },
2993         [ACTION_SET_MAC_SRC_MAC_SRC] = {
2994                 .name = "mac_addr",
2995                 .help = "new source mac address",
2996                 .next = NEXT(action_set_mac_src, NEXT_ENTRY(MAC_ADDR)),
2997                 .args = ARGS(ARGS_ENTRY_HTON
2998                              (struct rte_flow_action_set_mac, mac_addr)),
2999                 .call = parse_vc_conf,
3000         },
3001         [ACTION_SET_MAC_DST] = {
3002                 .name = "set_mac_dst",
3003                 .help = "set destination mac address",
3004                 .priv = PRIV_ACTION(SET_MAC_DST,
3005                         sizeof(struct rte_flow_action_set_mac)),
3006                 .next = NEXT(action_set_mac_dst),
3007                 .call = parse_vc,
3008         },
3009         [ACTION_SET_MAC_DST_MAC_DST] = {
3010                 .name = "mac_addr",
3011                 .help = "new destination mac address to set",
3012                 .next = NEXT(action_set_mac_dst, NEXT_ENTRY(MAC_ADDR)),
3013                 .args = ARGS(ARGS_ENTRY_HTON
3014                              (struct rte_flow_action_set_mac, mac_addr)),
3015                 .call = parse_vc_conf,
3016         },
3017         [ACTION_INC_TCP_SEQ] = {
3018                 .name = "inc_tcp_seq",
3019                 .help = "increase TCP sequence number",
3020                 .priv = PRIV_ACTION(INC_TCP_SEQ, sizeof(rte_be32_t)),
3021                 .next = NEXT(action_inc_tcp_seq),
3022                 .call = parse_vc,
3023         },
3024         [ACTION_INC_TCP_SEQ_VALUE] = {
3025                 .name = "value",
3026                 .help = "the value to increase TCP sequence number by",
3027                 .next = NEXT(action_inc_tcp_seq, NEXT_ENTRY(UNSIGNED)),
3028                 .args = ARGS(ARG_ENTRY_HTON(rte_be32_t)),
3029                 .call = parse_vc_conf,
3030         },
3031         [ACTION_DEC_TCP_SEQ] = {
3032                 .name = "dec_tcp_seq",
3033                 .help = "decrease TCP sequence number",
3034                 .priv = PRIV_ACTION(DEC_TCP_SEQ, sizeof(rte_be32_t)),
3035                 .next = NEXT(action_dec_tcp_seq),
3036                 .call = parse_vc,
3037         },
3038         [ACTION_DEC_TCP_SEQ_VALUE] = {
3039                 .name = "value",
3040                 .help = "the value to decrease TCP sequence number by",
3041                 .next = NEXT(action_dec_tcp_seq, NEXT_ENTRY(UNSIGNED)),
3042                 .args = ARGS(ARG_ENTRY_HTON(rte_be32_t)),
3043                 .call = parse_vc_conf,
3044         },
3045         [ACTION_INC_TCP_ACK] = {
3046                 .name = "inc_tcp_ack",
3047                 .help = "increase TCP acknowledgment number",
3048                 .priv = PRIV_ACTION(INC_TCP_ACK, sizeof(rte_be32_t)),
3049                 .next = NEXT(action_inc_tcp_ack),
3050                 .call = parse_vc,
3051         },
3052         [ACTION_INC_TCP_ACK_VALUE] = {
3053                 .name = "value",
3054                 .help = "the value to increase TCP acknowledgment number by",
3055                 .next = NEXT(action_inc_tcp_ack, NEXT_ENTRY(UNSIGNED)),
3056                 .args = ARGS(ARG_ENTRY_HTON(rte_be32_t)),
3057                 .call = parse_vc_conf,
3058         },
3059         [ACTION_DEC_TCP_ACK] = {
3060                 .name = "dec_tcp_ack",
3061                 .help = "decrease TCP acknowledgment number",
3062                 .priv = PRIV_ACTION(DEC_TCP_ACK, sizeof(rte_be32_t)),
3063                 .next = NEXT(action_dec_tcp_ack),
3064                 .call = parse_vc,
3065         },
3066         [ACTION_DEC_TCP_ACK_VALUE] = {
3067                 .name = "value",
3068                 .help = "the value to decrease TCP acknowledgment number by",
3069                 .next = NEXT(action_dec_tcp_ack, NEXT_ENTRY(UNSIGNED)),
3070                 .args = ARGS(ARG_ENTRY_HTON(rte_be32_t)),
3071                 .call = parse_vc_conf,
3072         },
3073         [ACTION_RAW_ENCAP] = {
3074                 .name = "raw_encap",
3075                 .help = "encapsulation data, defined by set raw_encap",
3076                 .priv = PRIV_ACTION(RAW_ENCAP,
3077                         sizeof(struct rte_flow_action_raw_encap)),
3078                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
3079                 .call = parse_vc_action_raw_encap,
3080         },
3081         [ACTION_RAW_DECAP] = {
3082                 .name = "raw_decap",
3083                 .help = "decapsulation data, defined by set raw_encap",
3084                 .priv = PRIV_ACTION(RAW_DECAP,
3085                         sizeof(struct rte_flow_action_raw_decap)),
3086                 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
3087                 .call = parse_vc_action_raw_decap,
3088         },
3089         /* Top level command. */
3090         [SET] = {
3091                 .name = "set",
3092                 .help = "set raw encap/decap data",
3093                 .type = "set raw_encap|raw_decap <pattern>",
3094                 .next = NEXT(NEXT_ENTRY
3095                              (SET_RAW_ENCAP,
3096                               SET_RAW_DECAP)),
3097                 .call = parse_set_init,
3098         },
3099         /* Sub-level commands. */
3100         [SET_RAW_ENCAP] = {
3101                 .name = "raw_encap",
3102                 .help = "set raw encap data",
3103                 .next = NEXT(next_item),
3104                 .call = parse_set_raw_encap_decap,
3105         },
3106         [SET_RAW_DECAP] = {
3107                 .name = "raw_decap",
3108                 .help = "set raw decap data",
3109                 .next = NEXT(next_item),
3110                 .call = parse_set_raw_encap_decap,
3111         }
3112 };
3113
3114 /** Remove and return last entry from argument stack. */
3115 static const struct arg *
3116 pop_args(struct context *ctx)
3117 {
3118         return ctx->args_num ? ctx->args[--ctx->args_num] : NULL;
3119 }
3120
3121 /** Add entry on top of the argument stack. */
3122 static int
3123 push_args(struct context *ctx, const struct arg *arg)
3124 {
3125         if (ctx->args_num == CTX_STACK_SIZE)
3126                 return -1;
3127         ctx->args[ctx->args_num++] = arg;
3128         return 0;
3129 }
3130
3131 /** Spread value into buffer according to bit-mask. */
3132 static size_t
3133 arg_entry_bf_fill(void *dst, uintmax_t val, const struct arg *arg)
3134 {
3135         uint32_t i = arg->size;
3136         uint32_t end = 0;
3137         int sub = 1;
3138         int add = 0;
3139         size_t len = 0;
3140
3141         if (!arg->mask)
3142                 return 0;
3143 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
3144         if (!arg->hton) {
3145                 i = 0;
3146                 end = arg->size;
3147                 sub = 0;
3148                 add = 1;
3149         }
3150 #endif
3151         while (i != end) {
3152                 unsigned int shift = 0;
3153                 uint8_t *buf = (uint8_t *)dst + arg->offset + (i -= sub);
3154
3155                 for (shift = 0; arg->mask[i] >> shift; ++shift) {
3156                         if (!(arg->mask[i] & (1 << shift)))
3157                                 continue;
3158                         ++len;
3159                         if (!dst)
3160                                 continue;
3161                         *buf &= ~(1 << shift);
3162                         *buf |= (val & 1) << shift;
3163                         val >>= 1;
3164                 }
3165                 i += add;
3166         }
3167         return len;
3168 }
3169
3170 /** Compare a string with a partial one of a given length. */
3171 static int
3172 strcmp_partial(const char *full, const char *partial, size_t partial_len)
3173 {
3174         int r = strncmp(full, partial, partial_len);
3175
3176         if (r)
3177                 return r;
3178         if (strlen(full) <= partial_len)
3179                 return 0;
3180         return full[partial_len];
3181 }
3182
3183 /**
3184  * Parse a prefix length and generate a bit-mask.
3185  *
3186  * Last argument (ctx->args) is retrieved to determine mask size, storage
3187  * location and whether the result must use network byte ordering.
3188  */
3189 static int
3190 parse_prefix(struct context *ctx, const struct token *token,
3191              const char *str, unsigned int len,
3192              void *buf, unsigned int size)
3193 {
3194         const struct arg *arg = pop_args(ctx);
3195         static const uint8_t conv[] = "\x00\x80\xc0\xe0\xf0\xf8\xfc\xfe\xff";
3196         char *end;
3197         uintmax_t u;
3198         unsigned int bytes;
3199         unsigned int extra;
3200
3201         (void)token;
3202         /* Argument is expected. */
3203         if (!arg)
3204                 return -1;
3205         errno = 0;
3206         u = strtoumax(str, &end, 0);
3207         if (errno || (size_t)(end - str) != len)
3208                 goto error;
3209         if (arg->mask) {
3210                 uintmax_t v = 0;
3211
3212                 extra = arg_entry_bf_fill(NULL, 0, arg);
3213                 if (u > extra)
3214                         goto error;
3215                 if (!ctx->object)
3216                         return len;
3217                 extra -= u;
3218                 while (u--)
3219                         (v <<= 1, v |= 1);
3220                 v <<= extra;
3221                 if (!arg_entry_bf_fill(ctx->object, v, arg) ||
3222                     !arg_entry_bf_fill(ctx->objmask, -1, arg))
3223                         goto error;
3224                 return len;
3225         }
3226         bytes = u / 8;
3227         extra = u % 8;
3228         size = arg->size;
3229         if (bytes > size || bytes + !!extra > size)
3230                 goto error;
3231         if (!ctx->object)
3232                 return len;
3233         buf = (uint8_t *)ctx->object + arg->offset;
3234 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
3235         if (!arg->hton) {
3236                 memset((uint8_t *)buf + size - bytes, 0xff, bytes);
3237                 memset(buf, 0x00, size - bytes);
3238                 if (extra)
3239                         ((uint8_t *)buf)[size - bytes - 1] = conv[extra];
3240         } else
3241 #endif
3242         {
3243                 memset(buf, 0xff, bytes);
3244                 memset((uint8_t *)buf + bytes, 0x00, size - bytes);
3245                 if (extra)
3246                         ((uint8_t *)buf)[bytes] = conv[extra];
3247         }
3248         if (ctx->objmask)
3249                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
3250         return len;
3251 error:
3252         push_args(ctx, arg);
3253         return -1;
3254 }
3255
3256 /** Default parsing function for token name matching. */
3257 static int
3258 parse_default(struct context *ctx, const struct token *token,
3259               const char *str, unsigned int len,
3260               void *buf, unsigned int size)
3261 {
3262         (void)ctx;
3263         (void)buf;
3264         (void)size;
3265         if (strcmp_partial(token->name, str, len))
3266                 return -1;
3267         return len;
3268 }
3269
3270 /** Parse flow command, initialize output buffer for subsequent tokens. */
3271 static int
3272 parse_init(struct context *ctx, const struct token *token,
3273            const char *str, unsigned int len,
3274            void *buf, unsigned int size)
3275 {
3276         struct buffer *out = buf;
3277
3278         /* Token name must match. */
3279         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
3280                 return -1;
3281         /* Nothing else to do if there is no buffer. */
3282         if (!out)
3283                 return len;
3284         /* Make sure buffer is large enough. */
3285         if (size < sizeof(*out))
3286                 return -1;
3287         /* Initialize buffer. */
3288         memset(out, 0x00, sizeof(*out));
3289         memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
3290         ctx->objdata = 0;
3291         ctx->object = out;
3292         ctx->objmask = NULL;
3293         return len;
3294 }
3295
3296 /** Parse tokens for validate/create commands. */
3297 static int
3298 parse_vc(struct context *ctx, const struct token *token,
3299          const char *str, unsigned int len,
3300          void *buf, unsigned int size)
3301 {
3302         struct buffer *out = buf;
3303         uint8_t *data;
3304         uint32_t data_size;
3305
3306         /* Token name must match. */
3307         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
3308                 return -1;
3309         /* Nothing else to do if there is no buffer. */
3310         if (!out)
3311                 return len;
3312         if (!out->command) {
3313                 if (ctx->curr != VALIDATE && ctx->curr != CREATE)
3314                         return -1;
3315                 if (sizeof(*out) > size)
3316                         return -1;
3317                 out->command = ctx->curr;
3318                 ctx->objdata = 0;
3319                 ctx->object = out;
3320                 ctx->objmask = NULL;
3321                 out->args.vc.data = (uint8_t *)out + size;
3322                 return len;
3323         }
3324         ctx->objdata = 0;
3325         ctx->object = &out->args.vc.attr;
3326         ctx->objmask = NULL;
3327         switch (ctx->curr) {
3328         case GROUP:
3329         case PRIORITY:
3330                 return len;
3331         case INGRESS:
3332                 out->args.vc.attr.ingress = 1;
3333                 return len;
3334         case EGRESS:
3335                 out->args.vc.attr.egress = 1;
3336                 return len;
3337         case TRANSFER:
3338                 out->args.vc.attr.transfer = 1;
3339                 return len;
3340         case PATTERN:
3341                 out->args.vc.pattern =
3342                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
3343                                                sizeof(double));
3344                 ctx->object = out->args.vc.pattern;
3345                 ctx->objmask = NULL;
3346                 return len;
3347         case ACTIONS:
3348                 out->args.vc.actions =
3349                         (void *)RTE_ALIGN_CEIL((uintptr_t)
3350                                                (out->args.vc.pattern +
3351                                                 out->args.vc.pattern_n),
3352                                                sizeof(double));
3353                 ctx->object = out->args.vc.actions;
3354                 ctx->objmask = NULL;
3355                 return len;
3356         default:
3357                 if (!token->priv)
3358                         return -1;
3359                 break;
3360         }
3361         if (!out->args.vc.actions) {
3362                 const struct parse_item_priv *priv = token->priv;
3363                 struct rte_flow_item *item =
3364                         out->args.vc.pattern + out->args.vc.pattern_n;
3365
3366                 data_size = priv->size * 3; /* spec, last, mask */
3367                 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
3368                                                (out->args.vc.data - data_size),
3369                                                sizeof(double));
3370                 if ((uint8_t *)item + sizeof(*item) > data)
3371                         return -1;
3372                 *item = (struct rte_flow_item){
3373                         .type = priv->type,
3374                 };
3375                 ++out->args.vc.pattern_n;
3376                 ctx->object = item;
3377                 ctx->objmask = NULL;
3378         } else {
3379                 const struct parse_action_priv *priv = token->priv;
3380                 struct rte_flow_action *action =
3381                         out->args.vc.actions + out->args.vc.actions_n;
3382
3383                 data_size = priv->size; /* configuration */
3384                 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
3385                                                (out->args.vc.data - data_size),
3386                                                sizeof(double));
3387                 if ((uint8_t *)action + sizeof(*action) > data)
3388                         return -1;
3389                 *action = (struct rte_flow_action){
3390                         .type = priv->type,
3391                         .conf = data_size ? data : NULL,
3392                 };
3393                 ++out->args.vc.actions_n;
3394                 ctx->object = action;
3395                 ctx->objmask = NULL;
3396         }
3397         memset(data, 0, data_size);
3398         out->args.vc.data = data;
3399         ctx->objdata = data_size;
3400         return len;
3401 }
3402
3403 /** Parse pattern item parameter type. */
3404 static int
3405 parse_vc_spec(struct context *ctx, const struct token *token,
3406               const char *str, unsigned int len,
3407               void *buf, unsigned int size)
3408 {
3409         struct buffer *out = buf;
3410         struct rte_flow_item *item;
3411         uint32_t data_size;
3412         int index;
3413         int objmask = 0;
3414
3415         (void)size;
3416         /* Token name must match. */
3417         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
3418                 return -1;
3419         /* Parse parameter types. */
3420         switch (ctx->curr) {
3421                 static const enum index prefix[] = NEXT_ENTRY(PREFIX);
3422
3423         case ITEM_PARAM_IS:
3424                 index = 0;
3425                 objmask = 1;
3426                 break;
3427         case ITEM_PARAM_SPEC:
3428                 index = 0;
3429                 break;
3430         case ITEM_PARAM_LAST:
3431                 index = 1;
3432                 break;
3433         case ITEM_PARAM_PREFIX:
3434                 /* Modify next token to expect a prefix. */
3435                 if (ctx->next_num < 2)
3436                         return -1;
3437                 ctx->next[ctx->next_num - 2] = prefix;
3438                 /* Fall through. */
3439         case ITEM_PARAM_MASK:
3440                 index = 2;
3441                 break;
3442         default:
3443                 return -1;
3444         }
3445         /* Nothing else to do if there is no buffer. */
3446         if (!out)
3447                 return len;
3448         if (!out->args.vc.pattern_n)
3449                 return -1;
3450         item = &out->args.vc.pattern[out->args.vc.pattern_n - 1];
3451         data_size = ctx->objdata / 3; /* spec, last, mask */
3452         /* Point to selected object. */
3453         ctx->object = out->args.vc.data + (data_size * index);
3454         if (objmask) {
3455                 ctx->objmask = out->args.vc.data + (data_size * 2); /* mask */
3456                 item->mask = ctx->objmask;
3457         } else
3458                 ctx->objmask = NULL;
3459         /* Update relevant item pointer. */
3460         *((const void **[]){ &item->spec, &item->last, &item->mask })[index] =
3461                 ctx->object;
3462         return len;
3463 }
3464
3465 /** Parse action configuration field. */
3466 static int
3467 parse_vc_conf(struct context *ctx, const struct token *token,
3468               const char *str, unsigned int len,
3469               void *buf, unsigned int size)
3470 {
3471         struct buffer *out = buf;
3472
3473         (void)size;
3474         /* Token name must match. */
3475         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
3476                 return -1;
3477         /* Nothing else to do if there is no buffer. */
3478         if (!out)
3479                 return len;
3480         /* Point to selected object. */
3481         ctx->object = out->args.vc.data;
3482         ctx->objmask = NULL;
3483         return len;
3484 }
3485
3486 /** Parse RSS action. */
3487 static int
3488 parse_vc_action_rss(struct context *ctx, const struct token *token,
3489                     const char *str, unsigned int len,
3490                     void *buf, unsigned int size)
3491 {
3492         struct buffer *out = buf;
3493         struct rte_flow_action *action;
3494         struct action_rss_data *action_rss_data;
3495         unsigned int i;
3496         int ret;
3497
3498         ret = parse_vc(ctx, token, str, len, buf, size);
3499         if (ret < 0)
3500                 return ret;
3501         /* Nothing else to do if there is no buffer. */
3502         if (!out)
3503                 return ret;
3504         if (!out->args.vc.actions_n)
3505                 return -1;
3506         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
3507         /* Point to selected object. */
3508         ctx->object = out->args.vc.data;
3509         ctx->objmask = NULL;
3510         /* Set up default configuration. */
3511         action_rss_data = ctx->object;
3512         *action_rss_data = (struct action_rss_data){
3513                 .conf = (struct rte_flow_action_rss){
3514                         .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
3515                         .level = 0,
3516                         .types = rss_hf,
3517                         .key_len = sizeof(action_rss_data->key),
3518                         .queue_num = RTE_MIN(nb_rxq, ACTION_RSS_QUEUE_NUM),
3519                         .key = action_rss_data->key,
3520                         .queue = action_rss_data->queue,
3521                 },
3522                 .key = "testpmd's default RSS hash key, "
3523                         "override it for better balancing",
3524                 .queue = { 0 },
3525         };
3526         for (i = 0; i < action_rss_data->conf.queue_num; ++i)
3527                 action_rss_data->queue[i] = i;
3528         if (!port_id_is_invalid(ctx->port, DISABLED_WARN) &&
3529             ctx->port != (portid_t)RTE_PORT_ALL) {
3530                 struct rte_eth_dev_info info;
3531
3532                 rte_eth_dev_info_get(ctx->port, &info);
3533                 action_rss_data->conf.key_len =
3534                         RTE_MIN(sizeof(action_rss_data->key),
3535                                 info.hash_key_size);
3536         }
3537         action->conf = &action_rss_data->conf;
3538         return ret;
3539 }
3540
3541 /**
3542  * Parse func field for RSS action.
3543  *
3544  * The RTE_ETH_HASH_FUNCTION_* value to assign is derived from the
3545  * ACTION_RSS_FUNC_* index that called this function.
3546  */
3547 static int
3548 parse_vc_action_rss_func(struct context *ctx, const struct token *token,
3549                          const char *str, unsigned int len,
3550                          void *buf, unsigned int size)
3551 {
3552         struct action_rss_data *action_rss_data;
3553         enum rte_eth_hash_function func;
3554
3555         (void)buf;
3556         (void)size;
3557         /* Token name must match. */
3558         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
3559                 return -1;
3560         switch (ctx->curr) {
3561         case ACTION_RSS_FUNC_DEFAULT:
3562                 func = RTE_ETH_HASH_FUNCTION_DEFAULT;
3563                 break;
3564         case ACTION_RSS_FUNC_TOEPLITZ:
3565                 func = RTE_ETH_HASH_FUNCTION_TOEPLITZ;
3566                 break;
3567         case ACTION_RSS_FUNC_SIMPLE_XOR:
3568                 func = RTE_ETH_HASH_FUNCTION_SIMPLE_XOR;
3569                 break;
3570         default:
3571                 return -1;
3572         }
3573         if (!ctx->object)
3574                 return len;
3575         action_rss_data = ctx->object;
3576         action_rss_data->conf.func = func;
3577         return len;
3578 }
3579
3580 /**
3581  * Parse type field for RSS action.
3582  *
3583  * Valid tokens are type field names and the "end" token.
3584  */
3585 static int
3586 parse_vc_action_rss_type(struct context *ctx, const struct token *token,
3587                           const char *str, unsigned int len,
3588                           void *buf, unsigned int size)
3589 {
3590         static const enum index next[] = NEXT_ENTRY(ACTION_RSS_TYPE);
3591         struct action_rss_data *action_rss_data;
3592         unsigned int i;
3593
3594         (void)token;
3595         (void)buf;
3596         (void)size;
3597         if (ctx->curr != ACTION_RSS_TYPE)
3598                 return -1;
3599         if (!(ctx->objdata >> 16) && ctx->object) {
3600                 action_rss_data = ctx->object;
3601                 action_rss_data->conf.types = 0;
3602         }
3603         if (!strcmp_partial("end", str, len)) {
3604                 ctx->objdata &= 0xffff;
3605                 return len;
3606         }
3607         for (i = 0; rss_type_table[i].str; ++i)
3608                 if (!strcmp_partial(rss_type_table[i].str, str, len))
3609                         break;
3610         if (!rss_type_table[i].str)
3611                 return -1;
3612         ctx->objdata = 1 << 16 | (ctx->objdata & 0xffff);
3613         /* Repeat token. */
3614         if (ctx->next_num == RTE_DIM(ctx->next))
3615                 return -1;
3616         ctx->next[ctx->next_num++] = next;
3617         if (!ctx->object)
3618                 return len;
3619         action_rss_data = ctx->object;
3620         action_rss_data->conf.types |= rss_type_table[i].rss_type;
3621         return len;
3622 }
3623
3624 /**
3625  * Parse queue field for RSS action.
3626  *
3627  * Valid tokens are queue indices and the "end" token.
3628  */
3629 static int
3630 parse_vc_action_rss_queue(struct context *ctx, const struct token *token,
3631                           const char *str, unsigned int len,
3632                           void *buf, unsigned int size)
3633 {
3634         static const enum index next[] = NEXT_ENTRY(ACTION_RSS_QUEUE);
3635         struct action_rss_data *action_rss_data;
3636         const struct arg *arg;
3637         int ret;
3638         int i;
3639
3640         (void)token;
3641         (void)buf;
3642         (void)size;
3643         if (ctx->curr != ACTION_RSS_QUEUE)
3644                 return -1;
3645         i = ctx->objdata >> 16;
3646         if (!strcmp_partial("end", str, len)) {
3647                 ctx->objdata &= 0xffff;
3648                 goto end;
3649         }
3650         if (i >= ACTION_RSS_QUEUE_NUM)
3651                 return -1;
3652         arg = ARGS_ENTRY_ARB(offsetof(struct action_rss_data, queue) +
3653                              i * sizeof(action_rss_data->queue[i]),
3654                              sizeof(action_rss_data->queue[i]));
3655         if (push_args(ctx, arg))
3656                 return -1;
3657         ret = parse_int(ctx, token, str, len, NULL, 0);
3658         if (ret < 0) {
3659                 pop_args(ctx);
3660                 return -1;
3661         }
3662         ++i;
3663         ctx->objdata = i << 16 | (ctx->objdata & 0xffff);
3664         /* Repeat token. */
3665         if (ctx->next_num == RTE_DIM(ctx->next))
3666                 return -1;
3667         ctx->next[ctx->next_num++] = next;
3668 end:
3669         if (!ctx->object)
3670                 return len;
3671         action_rss_data = ctx->object;
3672         action_rss_data->conf.queue_num = i;
3673         action_rss_data->conf.queue = i ? action_rss_data->queue : NULL;
3674         return len;
3675 }
3676
3677 /** Parse VXLAN encap action. */
3678 static int
3679 parse_vc_action_vxlan_encap(struct context *ctx, const struct token *token,
3680                             const char *str, unsigned int len,
3681                             void *buf, unsigned int size)
3682 {
3683         struct buffer *out = buf;
3684         struct rte_flow_action *action;
3685         struct action_vxlan_encap_data *action_vxlan_encap_data;
3686         int ret;
3687
3688         ret = parse_vc(ctx, token, str, len, buf, size);
3689         if (ret < 0)
3690                 return ret;
3691         /* Nothing else to do if there is no buffer. */
3692         if (!out)
3693                 return ret;
3694         if (!out->args.vc.actions_n)
3695                 return -1;
3696         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
3697         /* Point to selected object. */
3698         ctx->object = out->args.vc.data;
3699         ctx->objmask = NULL;
3700         /* Set up default configuration. */
3701         action_vxlan_encap_data = ctx->object;
3702         *action_vxlan_encap_data = (struct action_vxlan_encap_data){
3703                 .conf = (struct rte_flow_action_vxlan_encap){
3704                         .definition = action_vxlan_encap_data->items,
3705                 },
3706                 .items = {
3707                         {
3708                                 .type = RTE_FLOW_ITEM_TYPE_ETH,
3709                                 .spec = &action_vxlan_encap_data->item_eth,
3710                                 .mask = &rte_flow_item_eth_mask,
3711                         },
3712                         {
3713                                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
3714                                 .spec = &action_vxlan_encap_data->item_vlan,
3715                                 .mask = &rte_flow_item_vlan_mask,
3716                         },
3717                         {
3718                                 .type = RTE_FLOW_ITEM_TYPE_IPV4,
3719                                 .spec = &action_vxlan_encap_data->item_ipv4,
3720                                 .mask = &rte_flow_item_ipv4_mask,
3721                         },
3722                         {
3723                                 .type = RTE_FLOW_ITEM_TYPE_UDP,
3724                                 .spec = &action_vxlan_encap_data->item_udp,
3725                                 .mask = &rte_flow_item_udp_mask,
3726                         },
3727                         {
3728                                 .type = RTE_FLOW_ITEM_TYPE_VXLAN,
3729                                 .spec = &action_vxlan_encap_data->item_vxlan,
3730                                 .mask = &rte_flow_item_vxlan_mask,
3731                         },
3732                         {
3733                                 .type = RTE_FLOW_ITEM_TYPE_END,
3734                         },
3735                 },
3736                 .item_eth.type = 0,
3737                 .item_vlan = {
3738                         .tci = vxlan_encap_conf.vlan_tci,
3739                         .inner_type = 0,
3740                 },
3741                 .item_ipv4.hdr = {
3742                         .src_addr = vxlan_encap_conf.ipv4_src,
3743                         .dst_addr = vxlan_encap_conf.ipv4_dst,
3744                 },
3745                 .item_udp.hdr = {
3746                         .src_port = vxlan_encap_conf.udp_src,
3747                         .dst_port = vxlan_encap_conf.udp_dst,
3748                 },
3749                 .item_vxlan.flags = 0,
3750         };
3751         memcpy(action_vxlan_encap_data->item_eth.dst.addr_bytes,
3752                vxlan_encap_conf.eth_dst, RTE_ETHER_ADDR_LEN);
3753         memcpy(action_vxlan_encap_data->item_eth.src.addr_bytes,
3754                vxlan_encap_conf.eth_src, RTE_ETHER_ADDR_LEN);
3755         if (!vxlan_encap_conf.select_ipv4) {
3756                 memcpy(&action_vxlan_encap_data->item_ipv6.hdr.src_addr,
3757                        &vxlan_encap_conf.ipv6_src,
3758                        sizeof(vxlan_encap_conf.ipv6_src));
3759                 memcpy(&action_vxlan_encap_data->item_ipv6.hdr.dst_addr,
3760                        &vxlan_encap_conf.ipv6_dst,
3761                        sizeof(vxlan_encap_conf.ipv6_dst));
3762                 action_vxlan_encap_data->items[2] = (struct rte_flow_item){
3763                         .type = RTE_FLOW_ITEM_TYPE_IPV6,
3764                         .spec = &action_vxlan_encap_data->item_ipv6,
3765                         .mask = &rte_flow_item_ipv6_mask,
3766                 };
3767         }
3768         if (!vxlan_encap_conf.select_vlan)
3769                 action_vxlan_encap_data->items[1].type =
3770                         RTE_FLOW_ITEM_TYPE_VOID;
3771         if (vxlan_encap_conf.select_tos_ttl) {
3772                 if (vxlan_encap_conf.select_ipv4) {
3773                         static struct rte_flow_item_ipv4 ipv4_mask_tos;
3774
3775                         memcpy(&ipv4_mask_tos, &rte_flow_item_ipv4_mask,
3776                                sizeof(ipv4_mask_tos));
3777                         ipv4_mask_tos.hdr.type_of_service = 0xff;
3778                         ipv4_mask_tos.hdr.time_to_live = 0xff;
3779                         action_vxlan_encap_data->item_ipv4.hdr.type_of_service =
3780                                         vxlan_encap_conf.ip_tos;
3781                         action_vxlan_encap_data->item_ipv4.hdr.time_to_live =
3782                                         vxlan_encap_conf.ip_ttl;
3783                         action_vxlan_encap_data->items[2].mask =
3784                                                         &ipv4_mask_tos;
3785                 } else {
3786                         static struct rte_flow_item_ipv6 ipv6_mask_tos;
3787
3788                         memcpy(&ipv6_mask_tos, &rte_flow_item_ipv6_mask,
3789                                sizeof(ipv6_mask_tos));
3790                         ipv6_mask_tos.hdr.vtc_flow |=
3791                                 RTE_BE32(0xfful << RTE_IPV6_HDR_TC_SHIFT);
3792                         ipv6_mask_tos.hdr.hop_limits = 0xff;
3793                         action_vxlan_encap_data->item_ipv6.hdr.vtc_flow |=
3794                                 rte_cpu_to_be_32
3795                                         ((uint32_t)vxlan_encap_conf.ip_tos <<
3796                                          RTE_IPV6_HDR_TC_SHIFT);
3797                         action_vxlan_encap_data->item_ipv6.hdr.hop_limits =
3798                                         vxlan_encap_conf.ip_ttl;
3799                         action_vxlan_encap_data->items[2].mask =
3800                                                         &ipv6_mask_tos;
3801                 }
3802         }
3803         memcpy(action_vxlan_encap_data->item_vxlan.vni, vxlan_encap_conf.vni,
3804                RTE_DIM(vxlan_encap_conf.vni));
3805         action->conf = &action_vxlan_encap_data->conf;
3806         return ret;
3807 }
3808
3809 /** Parse NVGRE encap action. */
3810 static int
3811 parse_vc_action_nvgre_encap(struct context *ctx, const struct token *token,
3812                             const char *str, unsigned int len,
3813                             void *buf, unsigned int size)
3814 {
3815         struct buffer *out = buf;
3816         struct rte_flow_action *action;
3817         struct action_nvgre_encap_data *action_nvgre_encap_data;
3818         int ret;
3819
3820         ret = parse_vc(ctx, token, str, len, buf, size);
3821         if (ret < 0)
3822                 return ret;
3823         /* Nothing else to do if there is no buffer. */
3824         if (!out)
3825                 return ret;
3826         if (!out->args.vc.actions_n)
3827                 return -1;
3828         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
3829         /* Point to selected object. */
3830         ctx->object = out->args.vc.data;
3831         ctx->objmask = NULL;
3832         /* Set up default configuration. */
3833         action_nvgre_encap_data = ctx->object;
3834         *action_nvgre_encap_data = (struct action_nvgre_encap_data){
3835                 .conf = (struct rte_flow_action_nvgre_encap){
3836                         .definition = action_nvgre_encap_data->items,
3837                 },
3838                 .items = {
3839                         {
3840                                 .type = RTE_FLOW_ITEM_TYPE_ETH,
3841                                 .spec = &action_nvgre_encap_data->item_eth,
3842                                 .mask = &rte_flow_item_eth_mask,
3843                         },
3844                         {
3845                                 .type = RTE_FLOW_ITEM_TYPE_VLAN,
3846                                 .spec = &action_nvgre_encap_data->item_vlan,
3847                                 .mask = &rte_flow_item_vlan_mask,
3848                         },
3849                         {
3850                                 .type = RTE_FLOW_ITEM_TYPE_IPV4,
3851                                 .spec = &action_nvgre_encap_data->item_ipv4,
3852                                 .mask = &rte_flow_item_ipv4_mask,
3853                         },
3854                         {
3855                                 .type = RTE_FLOW_ITEM_TYPE_NVGRE,
3856                                 .spec = &action_nvgre_encap_data->item_nvgre,
3857                                 .mask = &rte_flow_item_nvgre_mask,
3858                         },
3859                         {
3860                                 .type = RTE_FLOW_ITEM_TYPE_END,
3861                         },
3862                 },
3863                 .item_eth.type = 0,
3864                 .item_vlan = {
3865                         .tci = nvgre_encap_conf.vlan_tci,
3866                         .inner_type = 0,
3867                 },
3868                 .item_ipv4.hdr = {
3869                        .src_addr = nvgre_encap_conf.ipv4_src,
3870                        .dst_addr = nvgre_encap_conf.ipv4_dst,
3871                 },
3872                 .item_nvgre.flow_id = 0,
3873         };
3874         memcpy(action_nvgre_encap_data->item_eth.dst.addr_bytes,
3875                nvgre_encap_conf.eth_dst, RTE_ETHER_ADDR_LEN);
3876         memcpy(action_nvgre_encap_data->item_eth.src.addr_bytes,
3877                nvgre_encap_conf.eth_src, RTE_ETHER_ADDR_LEN);
3878         if (!nvgre_encap_conf.select_ipv4) {
3879                 memcpy(&action_nvgre_encap_data->item_ipv6.hdr.src_addr,
3880                        &nvgre_encap_conf.ipv6_src,
3881                        sizeof(nvgre_encap_conf.ipv6_src));
3882                 memcpy(&action_nvgre_encap_data->item_ipv6.hdr.dst_addr,
3883                        &nvgre_encap_conf.ipv6_dst,
3884                        sizeof(nvgre_encap_conf.ipv6_dst));
3885                 action_nvgre_encap_data->items[2] = (struct rte_flow_item){
3886                         .type = RTE_FLOW_ITEM_TYPE_IPV6,
3887                         .spec = &action_nvgre_encap_data->item_ipv6,
3888                         .mask = &rte_flow_item_ipv6_mask,
3889                 };
3890         }
3891         if (!nvgre_encap_conf.select_vlan)
3892                 action_nvgre_encap_data->items[1].type =
3893                         RTE_FLOW_ITEM_TYPE_VOID;
3894         memcpy(action_nvgre_encap_data->item_nvgre.tni, nvgre_encap_conf.tni,
3895                RTE_DIM(nvgre_encap_conf.tni));
3896         action->conf = &action_nvgre_encap_data->conf;
3897         return ret;
3898 }
3899
3900 /** Parse l2 encap action. */
3901 static int
3902 parse_vc_action_l2_encap(struct context *ctx, const struct token *token,
3903                          const char *str, unsigned int len,
3904                          void *buf, unsigned int size)
3905 {
3906         struct buffer *out = buf;
3907         struct rte_flow_action *action;
3908         struct action_raw_encap_data *action_encap_data;
3909         struct rte_flow_item_eth eth = { .type = 0, };
3910         struct rte_flow_item_vlan vlan = {
3911                 .tci = mplsoudp_encap_conf.vlan_tci,
3912                 .inner_type = 0,
3913         };
3914         uint8_t *header;
3915         int ret;
3916
3917         ret = parse_vc(ctx, token, str, len, buf, size);
3918         if (ret < 0)
3919                 return ret;
3920         /* Nothing else to do if there is no buffer. */
3921         if (!out)
3922                 return ret;
3923         if (!out->args.vc.actions_n)
3924                 return -1;
3925         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
3926         /* Point to selected object. */
3927         ctx->object = out->args.vc.data;
3928         ctx->objmask = NULL;
3929         /* Copy the headers to the buffer. */
3930         action_encap_data = ctx->object;
3931         *action_encap_data = (struct action_raw_encap_data) {
3932                 .conf = (struct rte_flow_action_raw_encap){
3933                         .data = action_encap_data->data,
3934                 },
3935                 .data = {},
3936         };
3937         header = action_encap_data->data;
3938         if (l2_encap_conf.select_vlan)
3939                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN);
3940         else if (l2_encap_conf.select_ipv4)
3941                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
3942         else
3943                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
3944         memcpy(eth.dst.addr_bytes,
3945                l2_encap_conf.eth_dst, RTE_ETHER_ADDR_LEN);
3946         memcpy(eth.src.addr_bytes,
3947                l2_encap_conf.eth_src, RTE_ETHER_ADDR_LEN);
3948         memcpy(header, &eth, sizeof(eth));
3949         header += sizeof(eth);
3950         if (l2_encap_conf.select_vlan) {
3951                 if (l2_encap_conf.select_ipv4)
3952                         vlan.inner_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
3953                 else
3954                         vlan.inner_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
3955                 memcpy(header, &vlan, sizeof(vlan));
3956                 header += sizeof(vlan);
3957         }
3958         action_encap_data->conf.size = header -
3959                 action_encap_data->data;
3960         action->conf = &action_encap_data->conf;
3961         return ret;
3962 }
3963
3964 /** Parse l2 decap action. */
3965 static int
3966 parse_vc_action_l2_decap(struct context *ctx, const struct token *token,
3967                          const char *str, unsigned int len,
3968                          void *buf, unsigned int size)
3969 {
3970         struct buffer *out = buf;
3971         struct rte_flow_action *action;
3972         struct action_raw_decap_data *action_decap_data;
3973         struct rte_flow_item_eth eth = { .type = 0, };
3974         struct rte_flow_item_vlan vlan = {
3975                 .tci = mplsoudp_encap_conf.vlan_tci,
3976                 .inner_type = 0,
3977         };
3978         uint8_t *header;
3979         int ret;
3980
3981         ret = parse_vc(ctx, token, str, len, buf, size);
3982         if (ret < 0)
3983                 return ret;
3984         /* Nothing else to do if there is no buffer. */
3985         if (!out)
3986                 return ret;
3987         if (!out->args.vc.actions_n)
3988                 return -1;
3989         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
3990         /* Point to selected object. */
3991         ctx->object = out->args.vc.data;
3992         ctx->objmask = NULL;
3993         /* Copy the headers to the buffer. */
3994         action_decap_data = ctx->object;
3995         *action_decap_data = (struct action_raw_decap_data) {
3996                 .conf = (struct rte_flow_action_raw_decap){
3997                         .data = action_decap_data->data,
3998                 },
3999                 .data = {},
4000         };
4001         header = action_decap_data->data;
4002         if (l2_decap_conf.select_vlan)
4003                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN);
4004         memcpy(header, &eth, sizeof(eth));
4005         header += sizeof(eth);
4006         if (l2_decap_conf.select_vlan) {
4007                 memcpy(header, &vlan, sizeof(vlan));
4008                 header += sizeof(vlan);
4009         }
4010         action_decap_data->conf.size = header -
4011                 action_decap_data->data;
4012         action->conf = &action_decap_data->conf;
4013         return ret;
4014 }
4015
4016 #define ETHER_TYPE_MPLS_UNICAST 0x8847
4017
4018 /** Parse MPLSOGRE encap action. */
4019 static int
4020 parse_vc_action_mplsogre_encap(struct context *ctx, const struct token *token,
4021                                const char *str, unsigned int len,
4022                                void *buf, unsigned int size)
4023 {
4024         struct buffer *out = buf;
4025         struct rte_flow_action *action;
4026         struct action_raw_encap_data *action_encap_data;
4027         struct rte_flow_item_eth eth = { .type = 0, };
4028         struct rte_flow_item_vlan vlan = {
4029                 .tci = mplsogre_encap_conf.vlan_tci,
4030                 .inner_type = 0,
4031         };
4032         struct rte_flow_item_ipv4 ipv4 = {
4033                 .hdr =  {
4034                         .src_addr = mplsogre_encap_conf.ipv4_src,
4035                         .dst_addr = mplsogre_encap_conf.ipv4_dst,
4036                         .next_proto_id = IPPROTO_GRE,
4037                         .version_ihl = RTE_IPV4_VHL_DEF,
4038                         .time_to_live = IPDEFTTL,
4039                 },
4040         };
4041         struct rte_flow_item_ipv6 ipv6 = {
4042                 .hdr =  {
4043                         .proto = IPPROTO_GRE,
4044                         .hop_limits = IPDEFTTL,
4045                 },
4046         };
4047         struct rte_flow_item_gre gre = {
4048                 .protocol = rte_cpu_to_be_16(ETHER_TYPE_MPLS_UNICAST),
4049         };
4050         struct rte_flow_item_mpls mpls;
4051         uint8_t *header;
4052         int ret;
4053
4054         ret = parse_vc(ctx, token, str, len, buf, size);
4055         if (ret < 0)
4056                 return ret;
4057         /* Nothing else to do if there is no buffer. */
4058         if (!out)
4059                 return ret;
4060         if (!out->args.vc.actions_n)
4061                 return -1;
4062         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
4063         /* Point to selected object. */
4064         ctx->object = out->args.vc.data;
4065         ctx->objmask = NULL;
4066         /* Copy the headers to the buffer. */
4067         action_encap_data = ctx->object;
4068         *action_encap_data = (struct action_raw_encap_data) {
4069                 .conf = (struct rte_flow_action_raw_encap){
4070                         .data = action_encap_data->data,
4071                 },
4072                 .data = {},
4073                 .preserve = {},
4074         };
4075         header = action_encap_data->data;
4076         if (mplsogre_encap_conf.select_vlan)
4077                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN);
4078         else if (mplsogre_encap_conf.select_ipv4)
4079                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
4080         else
4081                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
4082         memcpy(eth.dst.addr_bytes,
4083                mplsogre_encap_conf.eth_dst, RTE_ETHER_ADDR_LEN);
4084         memcpy(eth.src.addr_bytes,
4085                mplsogre_encap_conf.eth_src, RTE_ETHER_ADDR_LEN);
4086         memcpy(header, &eth, sizeof(eth));
4087         header += sizeof(eth);
4088         if (mplsogre_encap_conf.select_vlan) {
4089                 if (mplsogre_encap_conf.select_ipv4)
4090                         vlan.inner_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
4091                 else
4092                         vlan.inner_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
4093                 memcpy(header, &vlan, sizeof(vlan));
4094                 header += sizeof(vlan);
4095         }
4096         if (mplsogre_encap_conf.select_ipv4) {
4097                 memcpy(header, &ipv4, sizeof(ipv4));
4098                 header += sizeof(ipv4);
4099         } else {
4100                 memcpy(&ipv6.hdr.src_addr,
4101                        &mplsogre_encap_conf.ipv6_src,
4102                        sizeof(mplsogre_encap_conf.ipv6_src));
4103                 memcpy(&ipv6.hdr.dst_addr,
4104                        &mplsogre_encap_conf.ipv6_dst,
4105                        sizeof(mplsogre_encap_conf.ipv6_dst));
4106                 memcpy(header, &ipv6, sizeof(ipv6));
4107                 header += sizeof(ipv6);
4108         }
4109         memcpy(header, &gre, sizeof(gre));
4110         header += sizeof(gre);
4111         memcpy(mpls.label_tc_s, mplsogre_encap_conf.label,
4112                RTE_DIM(mplsogre_encap_conf.label));
4113         mpls.label_tc_s[2] |= 0x1;
4114         memcpy(header, &mpls, sizeof(mpls));
4115         header += sizeof(mpls);
4116         action_encap_data->conf.size = header -
4117                 action_encap_data->data;
4118         action->conf = &action_encap_data->conf;
4119         return ret;
4120 }
4121
4122 /** Parse MPLSOGRE decap action. */
4123 static int
4124 parse_vc_action_mplsogre_decap(struct context *ctx, const struct token *token,
4125                                const char *str, unsigned int len,
4126                                void *buf, unsigned int size)
4127 {
4128         struct buffer *out = buf;
4129         struct rte_flow_action *action;
4130         struct action_raw_decap_data *action_decap_data;
4131         struct rte_flow_item_eth eth = { .type = 0, };
4132         struct rte_flow_item_vlan vlan = {.tci = 0};
4133         struct rte_flow_item_ipv4 ipv4 = {
4134                 .hdr =  {
4135                         .next_proto_id = IPPROTO_GRE,
4136                 },
4137         };
4138         struct rte_flow_item_ipv6 ipv6 = {
4139                 .hdr =  {
4140                         .proto = IPPROTO_GRE,
4141                 },
4142         };
4143         struct rte_flow_item_gre gre = {
4144                 .protocol = rte_cpu_to_be_16(ETHER_TYPE_MPLS_UNICAST),
4145         };
4146         struct rte_flow_item_mpls mpls;
4147         uint8_t *header;
4148         int ret;
4149
4150         ret = parse_vc(ctx, token, str, len, buf, size);
4151         if (ret < 0)
4152                 return ret;
4153         /* Nothing else to do if there is no buffer. */
4154         if (!out)
4155                 return ret;
4156         if (!out->args.vc.actions_n)
4157                 return -1;
4158         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
4159         /* Point to selected object. */
4160         ctx->object = out->args.vc.data;
4161         ctx->objmask = NULL;
4162         /* Copy the headers to the buffer. */
4163         action_decap_data = ctx->object;
4164         *action_decap_data = (struct action_raw_decap_data) {
4165                 .conf = (struct rte_flow_action_raw_decap){
4166                         .data = action_decap_data->data,
4167                 },
4168                 .data = {},
4169         };
4170         header = action_decap_data->data;
4171         if (mplsogre_decap_conf.select_vlan)
4172                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN);
4173         else if (mplsogre_encap_conf.select_ipv4)
4174                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
4175         else
4176                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
4177         memcpy(eth.dst.addr_bytes,
4178                mplsogre_encap_conf.eth_dst, RTE_ETHER_ADDR_LEN);
4179         memcpy(eth.src.addr_bytes,
4180                mplsogre_encap_conf.eth_src, RTE_ETHER_ADDR_LEN);
4181         memcpy(header, &eth, sizeof(eth));
4182         header += sizeof(eth);
4183         if (mplsogre_encap_conf.select_vlan) {
4184                 if (mplsogre_encap_conf.select_ipv4)
4185                         vlan.inner_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
4186                 else
4187                         vlan.inner_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
4188                 memcpy(header, &vlan, sizeof(vlan));
4189                 header += sizeof(vlan);
4190         }
4191         if (mplsogre_encap_conf.select_ipv4) {
4192                 memcpy(header, &ipv4, sizeof(ipv4));
4193                 header += sizeof(ipv4);
4194         } else {
4195                 memcpy(header, &ipv6, sizeof(ipv6));
4196                 header += sizeof(ipv6);
4197         }
4198         memcpy(header, &gre, sizeof(gre));
4199         header += sizeof(gre);
4200         memset(&mpls, 0, sizeof(mpls));
4201         memcpy(header, &mpls, sizeof(mpls));
4202         header += sizeof(mpls);
4203         action_decap_data->conf.size = header -
4204                 action_decap_data->data;
4205         action->conf = &action_decap_data->conf;
4206         return ret;
4207 }
4208
4209 /** Parse MPLSOUDP encap action. */
4210 static int
4211 parse_vc_action_mplsoudp_encap(struct context *ctx, const struct token *token,
4212                                const char *str, unsigned int len,
4213                                void *buf, unsigned int size)
4214 {
4215         struct buffer *out = buf;
4216         struct rte_flow_action *action;
4217         struct action_raw_encap_data *action_encap_data;
4218         struct rte_flow_item_eth eth = { .type = 0, };
4219         struct rte_flow_item_vlan vlan = {
4220                 .tci = mplsoudp_encap_conf.vlan_tci,
4221                 .inner_type = 0,
4222         };
4223         struct rte_flow_item_ipv4 ipv4 = {
4224                 .hdr =  {
4225                         .src_addr = mplsoudp_encap_conf.ipv4_src,
4226                         .dst_addr = mplsoudp_encap_conf.ipv4_dst,
4227                         .next_proto_id = IPPROTO_UDP,
4228                         .version_ihl = RTE_IPV4_VHL_DEF,
4229                         .time_to_live = IPDEFTTL,
4230                 },
4231         };
4232         struct rte_flow_item_ipv6 ipv6 = {
4233                 .hdr =  {
4234                         .proto = IPPROTO_UDP,
4235                         .hop_limits = IPDEFTTL,
4236                 },
4237         };
4238         struct rte_flow_item_udp udp = {
4239                 .hdr = {
4240                         .src_port = mplsoudp_encap_conf.udp_src,
4241                         .dst_port = mplsoudp_encap_conf.udp_dst,
4242                 },
4243         };
4244         struct rte_flow_item_mpls mpls;
4245         uint8_t *header;
4246         int ret;
4247
4248         ret = parse_vc(ctx, token, str, len, buf, size);
4249         if (ret < 0)
4250                 return ret;
4251         /* Nothing else to do if there is no buffer. */
4252         if (!out)
4253                 return ret;
4254         if (!out->args.vc.actions_n)
4255                 return -1;
4256         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
4257         /* Point to selected object. */
4258         ctx->object = out->args.vc.data;
4259         ctx->objmask = NULL;
4260         /* Copy the headers to the buffer. */
4261         action_encap_data = ctx->object;
4262         *action_encap_data = (struct action_raw_encap_data) {
4263                 .conf = (struct rte_flow_action_raw_encap){
4264                         .data = action_encap_data->data,
4265                 },
4266                 .data = {},
4267                 .preserve = {},
4268         };
4269         header = action_encap_data->data;
4270         if (mplsoudp_encap_conf.select_vlan)
4271                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN);
4272         else if (mplsoudp_encap_conf.select_ipv4)
4273                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
4274         else
4275                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
4276         memcpy(eth.dst.addr_bytes,
4277                mplsoudp_encap_conf.eth_dst, RTE_ETHER_ADDR_LEN);
4278         memcpy(eth.src.addr_bytes,
4279                mplsoudp_encap_conf.eth_src, RTE_ETHER_ADDR_LEN);
4280         memcpy(header, &eth, sizeof(eth));
4281         header += sizeof(eth);
4282         if (mplsoudp_encap_conf.select_vlan) {
4283                 if (mplsoudp_encap_conf.select_ipv4)
4284                         vlan.inner_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
4285                 else
4286                         vlan.inner_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
4287                 memcpy(header, &vlan, sizeof(vlan));
4288                 header += sizeof(vlan);
4289         }
4290         if (mplsoudp_encap_conf.select_ipv4) {
4291                 memcpy(header, &ipv4, sizeof(ipv4));
4292                 header += sizeof(ipv4);
4293         } else {
4294                 memcpy(&ipv6.hdr.src_addr,
4295                        &mplsoudp_encap_conf.ipv6_src,
4296                        sizeof(mplsoudp_encap_conf.ipv6_src));
4297                 memcpy(&ipv6.hdr.dst_addr,
4298                        &mplsoudp_encap_conf.ipv6_dst,
4299                        sizeof(mplsoudp_encap_conf.ipv6_dst));
4300                 memcpy(header, &ipv6, sizeof(ipv6));
4301                 header += sizeof(ipv6);
4302         }
4303         memcpy(header, &udp, sizeof(udp));
4304         header += sizeof(udp);
4305         memcpy(mpls.label_tc_s, mplsoudp_encap_conf.label,
4306                RTE_DIM(mplsoudp_encap_conf.label));
4307         mpls.label_tc_s[2] |= 0x1;
4308         memcpy(header, &mpls, sizeof(mpls));
4309         header += sizeof(mpls);
4310         action_encap_data->conf.size = header -
4311                 action_encap_data->data;
4312         action->conf = &action_encap_data->conf;
4313         return ret;
4314 }
4315
4316 /** Parse MPLSOUDP decap action. */
4317 static int
4318 parse_vc_action_mplsoudp_decap(struct context *ctx, const struct token *token,
4319                                const char *str, unsigned int len,
4320                                void *buf, unsigned int size)
4321 {
4322         struct buffer *out = buf;
4323         struct rte_flow_action *action;
4324         struct action_raw_decap_data *action_decap_data;
4325         struct rte_flow_item_eth eth = { .type = 0, };
4326         struct rte_flow_item_vlan vlan = {.tci = 0};
4327         struct rte_flow_item_ipv4 ipv4 = {
4328                 .hdr =  {
4329                         .next_proto_id = IPPROTO_UDP,
4330                 },
4331         };
4332         struct rte_flow_item_ipv6 ipv6 = {
4333                 .hdr =  {
4334                         .proto = IPPROTO_UDP,
4335                 },
4336         };
4337         struct rte_flow_item_udp udp = {
4338                 .hdr = {
4339                         .dst_port = rte_cpu_to_be_16(6635),
4340                 },
4341         };
4342         struct rte_flow_item_mpls mpls;
4343         uint8_t *header;
4344         int ret;
4345
4346         ret = parse_vc(ctx, token, str, len, buf, size);
4347         if (ret < 0)
4348                 return ret;
4349         /* Nothing else to do if there is no buffer. */
4350         if (!out)
4351                 return ret;
4352         if (!out->args.vc.actions_n)
4353                 return -1;
4354         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
4355         /* Point to selected object. */
4356         ctx->object = out->args.vc.data;
4357         ctx->objmask = NULL;
4358         /* Copy the headers to the buffer. */
4359         action_decap_data = ctx->object;
4360         *action_decap_data = (struct action_raw_decap_data) {
4361                 .conf = (struct rte_flow_action_raw_decap){
4362                         .data = action_decap_data->data,
4363                 },
4364                 .data = {},
4365         };
4366         header = action_decap_data->data;
4367         if (mplsoudp_decap_conf.select_vlan)
4368                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN);
4369         else if (mplsoudp_encap_conf.select_ipv4)
4370                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
4371         else
4372                 eth.type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
4373         memcpy(eth.dst.addr_bytes,
4374                mplsoudp_encap_conf.eth_dst, RTE_ETHER_ADDR_LEN);
4375         memcpy(eth.src.addr_bytes,
4376                mplsoudp_encap_conf.eth_src, RTE_ETHER_ADDR_LEN);
4377         memcpy(header, &eth, sizeof(eth));
4378         header += sizeof(eth);
4379         if (mplsoudp_encap_conf.select_vlan) {
4380                 if (mplsoudp_encap_conf.select_ipv4)
4381                         vlan.inner_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
4382                 else
4383                         vlan.inner_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
4384                 memcpy(header, &vlan, sizeof(vlan));
4385                 header += sizeof(vlan);
4386         }
4387         if (mplsoudp_encap_conf.select_ipv4) {
4388                 memcpy(header, &ipv4, sizeof(ipv4));
4389                 header += sizeof(ipv4);
4390         } else {
4391                 memcpy(header, &ipv6, sizeof(ipv6));
4392                 header += sizeof(ipv6);
4393         }
4394         memcpy(header, &udp, sizeof(udp));
4395         header += sizeof(udp);
4396         memset(&mpls, 0, sizeof(mpls));
4397         memcpy(header, &mpls, sizeof(mpls));
4398         header += sizeof(mpls);
4399         action_decap_data->conf.size = header -
4400                 action_decap_data->data;
4401         action->conf = &action_decap_data->conf;
4402         return ret;
4403 }
4404
4405 static int
4406 parse_vc_action_raw_encap(struct context *ctx, const struct token *token,
4407                           const char *str, unsigned int len, void *buf,
4408                           unsigned int size)
4409 {
4410         struct buffer *out = buf;
4411         struct rte_flow_action *action;
4412         struct rte_flow_action_raw_encap *action_raw_encap_conf = NULL;
4413         uint8_t *data = NULL;
4414         int ret;
4415
4416         ret = parse_vc(ctx, token, str, len, buf, size);
4417         if (ret < 0)
4418                 return ret;
4419         /* Nothing else to do if there is no buffer. */
4420         if (!out)
4421                 return ret;
4422         if (!out->args.vc.actions_n)
4423                 return -1;
4424         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
4425         /* Point to selected object. */
4426         ctx->object = out->args.vc.data;
4427         ctx->objmask = NULL;
4428         /* Copy the headers to the buffer. */
4429         action_raw_encap_conf = ctx->object;
4430         /* data stored from tail of data buffer */
4431         data = (uint8_t *)&(raw_encap_conf.data) +
4432                 ACTION_RAW_ENCAP_MAX_DATA - raw_encap_conf.size;
4433         action_raw_encap_conf->data = data;
4434         action_raw_encap_conf->preserve = NULL;
4435         action_raw_encap_conf->size = raw_encap_conf.size;
4436         action->conf = action_raw_encap_conf;
4437         return ret;
4438 }
4439
4440 static int
4441 parse_vc_action_raw_decap(struct context *ctx, const struct token *token,
4442                           const char *str, unsigned int len, void *buf,
4443                           unsigned int size)
4444 {
4445         struct buffer *out = buf;
4446         struct rte_flow_action *action;
4447         struct rte_flow_action_raw_decap *action_raw_decap_conf = NULL;
4448         uint8_t *data = NULL;
4449         int ret;
4450
4451         ret = parse_vc(ctx, token, str, len, buf, size);
4452         if (ret < 0)
4453                 return ret;
4454         /* Nothing else to do if there is no buffer. */
4455         if (!out)
4456                 return ret;
4457         if (!out->args.vc.actions_n)
4458                 return -1;
4459         action = &out->args.vc.actions[out->args.vc.actions_n - 1];
4460         /* Point to selected object. */
4461         ctx->object = out->args.vc.data;
4462         ctx->objmask = NULL;
4463         /* Copy the headers to the buffer. */
4464         action_raw_decap_conf = ctx->object;
4465         /* data stored from tail of data buffer */
4466         data = (uint8_t *)&(raw_decap_conf.data) +
4467                 ACTION_RAW_ENCAP_MAX_DATA - raw_decap_conf.size;
4468         action_raw_decap_conf->data = data;
4469         action_raw_decap_conf->size = raw_decap_conf.size;
4470         action->conf = action_raw_decap_conf;
4471         return ret;
4472 }
4473
4474 /** Parse tokens for destroy command. */
4475 static int
4476 parse_destroy(struct context *ctx, const struct token *token,
4477               const char *str, unsigned int len,
4478               void *buf, unsigned int size)
4479 {
4480         struct buffer *out = buf;
4481
4482         /* Token name must match. */
4483         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
4484                 return -1;
4485         /* Nothing else to do if there is no buffer. */
4486         if (!out)
4487                 return len;
4488         if (!out->command) {
4489                 if (ctx->curr != DESTROY)
4490                         return -1;
4491                 if (sizeof(*out) > size)
4492                         return -1;
4493                 out->command = ctx->curr;
4494                 ctx->objdata = 0;
4495                 ctx->object = out;
4496                 ctx->objmask = NULL;
4497                 out->args.destroy.rule =
4498                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
4499                                                sizeof(double));
4500                 return len;
4501         }
4502         if (((uint8_t *)(out->args.destroy.rule + out->args.destroy.rule_n) +
4503              sizeof(*out->args.destroy.rule)) > (uint8_t *)out + size)
4504                 return -1;
4505         ctx->objdata = 0;
4506         ctx->object = out->args.destroy.rule + out->args.destroy.rule_n++;
4507         ctx->objmask = NULL;
4508         return len;
4509 }
4510
4511 /** Parse tokens for flush command. */
4512 static int
4513 parse_flush(struct context *ctx, const struct token *token,
4514             const char *str, unsigned int len,
4515             void *buf, unsigned int size)
4516 {
4517         struct buffer *out = buf;
4518
4519         /* Token name must match. */
4520         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
4521                 return -1;
4522         /* Nothing else to do if there is no buffer. */
4523         if (!out)
4524                 return len;
4525         if (!out->command) {
4526                 if (ctx->curr != FLUSH)
4527                         return -1;
4528                 if (sizeof(*out) > size)
4529                         return -1;
4530                 out->command = ctx->curr;
4531                 ctx->objdata = 0;
4532                 ctx->object = out;
4533                 ctx->objmask = NULL;
4534         }
4535         return len;
4536 }
4537
4538 /** Parse tokens for query command. */
4539 static int
4540 parse_query(struct context *ctx, const struct token *token,
4541             const char *str, unsigned int len,
4542             void *buf, unsigned int size)
4543 {
4544         struct buffer *out = buf;
4545
4546         /* Token name must match. */
4547         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
4548                 return -1;
4549         /* Nothing else to do if there is no buffer. */
4550         if (!out)
4551                 return len;
4552         if (!out->command) {
4553                 if (ctx->curr != QUERY)
4554                         return -1;
4555                 if (sizeof(*out) > size)
4556                         return -1;
4557                 out->command = ctx->curr;
4558                 ctx->objdata = 0;
4559                 ctx->object = out;
4560                 ctx->objmask = NULL;
4561         }
4562         return len;
4563 }
4564
4565 /** Parse action names. */
4566 static int
4567 parse_action(struct context *ctx, const struct token *token,
4568              const char *str, unsigned int len,
4569              void *buf, unsigned int size)
4570 {
4571         struct buffer *out = buf;
4572         const struct arg *arg = pop_args(ctx);
4573         unsigned int i;
4574
4575         (void)size;
4576         /* Argument is expected. */
4577         if (!arg)
4578                 return -1;
4579         /* Parse action name. */
4580         for (i = 0; next_action[i]; ++i) {
4581                 const struct parse_action_priv *priv;
4582
4583                 token = &token_list[next_action[i]];
4584                 if (strcmp_partial(token->name, str, len))
4585                         continue;
4586                 priv = token->priv;
4587                 if (!priv)
4588                         goto error;
4589                 if (out)
4590                         memcpy((uint8_t *)ctx->object + arg->offset,
4591                                &priv->type,
4592                                arg->size);
4593                 return len;
4594         }
4595 error:
4596         push_args(ctx, arg);
4597         return -1;
4598 }
4599
4600 /** Parse tokens for list command. */
4601 static int
4602 parse_list(struct context *ctx, const struct token *token,
4603            const char *str, unsigned int len,
4604            void *buf, unsigned int size)
4605 {
4606         struct buffer *out = buf;
4607
4608         /* Token name must match. */
4609         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
4610                 return -1;
4611         /* Nothing else to do if there is no buffer. */
4612         if (!out)
4613                 return len;
4614         if (!out->command) {
4615                 if (ctx->curr != LIST)
4616                         return -1;
4617                 if (sizeof(*out) > size)
4618                         return -1;
4619                 out->command = ctx->curr;
4620                 ctx->objdata = 0;
4621                 ctx->object = out;
4622                 ctx->objmask = NULL;
4623                 out->args.list.group =
4624                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
4625                                                sizeof(double));
4626                 return len;
4627         }
4628         if (((uint8_t *)(out->args.list.group + out->args.list.group_n) +
4629              sizeof(*out->args.list.group)) > (uint8_t *)out + size)
4630                 return -1;
4631         ctx->objdata = 0;
4632         ctx->object = out->args.list.group + out->args.list.group_n++;
4633         ctx->objmask = NULL;
4634         return len;
4635 }
4636
4637 /** Parse tokens for isolate command. */
4638 static int
4639 parse_isolate(struct context *ctx, const struct token *token,
4640               const char *str, unsigned int len,
4641               void *buf, unsigned int size)
4642 {
4643         struct buffer *out = buf;
4644
4645         /* Token name must match. */
4646         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
4647                 return -1;
4648         /* Nothing else to do if there is no buffer. */
4649         if (!out)
4650                 return len;
4651         if (!out->command) {
4652                 if (ctx->curr != ISOLATE)
4653                         return -1;
4654                 if (sizeof(*out) > size)
4655                         return -1;
4656                 out->command = ctx->curr;
4657                 ctx->objdata = 0;
4658                 ctx->object = out;
4659                 ctx->objmask = NULL;
4660         }
4661         return len;
4662 }
4663
4664 /**
4665  * Parse signed/unsigned integers 8 to 64-bit long.
4666  *
4667  * Last argument (ctx->args) is retrieved to determine integer type and
4668  * storage location.
4669  */
4670 static int
4671 parse_int(struct context *ctx, const struct token *token,
4672           const char *str, unsigned int len,
4673           void *buf, unsigned int size)
4674 {
4675         const struct arg *arg = pop_args(ctx);
4676         uintmax_t u;
4677         char *end;
4678
4679         (void)token;
4680         /* Argument is expected. */
4681         if (!arg)
4682                 return -1;
4683         errno = 0;
4684         u = arg->sign ?
4685                 (uintmax_t)strtoimax(str, &end, 0) :
4686                 strtoumax(str, &end, 0);
4687         if (errno || (size_t)(end - str) != len)
4688                 goto error;
4689         if (arg->bounded &&
4690             ((arg->sign && ((intmax_t)u < (intmax_t)arg->min ||
4691                             (intmax_t)u > (intmax_t)arg->max)) ||
4692              (!arg->sign && (u < arg->min || u > arg->max))))
4693                 goto error;
4694         if (!ctx->object)
4695                 return len;
4696         if (arg->mask) {
4697                 if (!arg_entry_bf_fill(ctx->object, u, arg) ||
4698                     !arg_entry_bf_fill(ctx->objmask, -1, arg))
4699                         goto error;
4700                 return len;
4701         }
4702         buf = (uint8_t *)ctx->object + arg->offset;
4703         size = arg->size;
4704         if (u > RTE_LEN2MASK(size * CHAR_BIT, uint64_t))
4705                 return -1;
4706 objmask:
4707         switch (size) {
4708         case sizeof(uint8_t):
4709                 *(uint8_t *)buf = u;
4710                 break;
4711         case sizeof(uint16_t):
4712                 *(uint16_t *)buf = arg->hton ? rte_cpu_to_be_16(u) : u;
4713                 break;
4714         case sizeof(uint8_t [3]):
4715 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
4716                 if (!arg->hton) {
4717                         ((uint8_t *)buf)[0] = u;
4718                         ((uint8_t *)buf)[1] = u >> 8;
4719                         ((uint8_t *)buf)[2] = u >> 16;
4720                         break;
4721                 }
4722 #endif
4723                 ((uint8_t *)buf)[0] = u >> 16;
4724                 ((uint8_t *)buf)[1] = u >> 8;
4725                 ((uint8_t *)buf)[2] = u;
4726                 break;
4727         case sizeof(uint32_t):
4728                 *(uint32_t *)buf = arg->hton ? rte_cpu_to_be_32(u) : u;
4729                 break;
4730         case sizeof(uint64_t):
4731                 *(uint64_t *)buf = arg->hton ? rte_cpu_to_be_64(u) : u;
4732                 break;
4733         default:
4734                 goto error;
4735         }
4736         if (ctx->objmask && buf != (uint8_t *)ctx->objmask + arg->offset) {
4737                 u = -1;
4738                 buf = (uint8_t *)ctx->objmask + arg->offset;
4739                 goto objmask;
4740         }
4741         return len;
4742 error:
4743         push_args(ctx, arg);
4744         return -1;
4745 }
4746
4747 /**
4748  * Parse a string.
4749  *
4750  * Three arguments (ctx->args) are retrieved from the stack to store data,
4751  * its actual length and address (in that order).
4752  */
4753 static int
4754 parse_string(struct context *ctx, const struct token *token,
4755              const char *str, unsigned int len,
4756              void *buf, unsigned int size)
4757 {
4758         const struct arg *arg_data = pop_args(ctx);
4759         const struct arg *arg_len = pop_args(ctx);
4760         const struct arg *arg_addr = pop_args(ctx);
4761         char tmp[16]; /* Ought to be enough. */
4762         int ret;
4763
4764         /* Arguments are expected. */
4765         if (!arg_data)
4766                 return -1;
4767         if (!arg_len) {
4768                 push_args(ctx, arg_data);
4769                 return -1;
4770         }
4771         if (!arg_addr) {
4772                 push_args(ctx, arg_len);
4773                 push_args(ctx, arg_data);
4774                 return -1;
4775         }
4776         size = arg_data->size;
4777         /* Bit-mask fill is not supported. */
4778         if (arg_data->mask || size < len)
4779                 goto error;
4780         if (!ctx->object)
4781                 return len;
4782         /* Let parse_int() fill length information first. */
4783         ret = snprintf(tmp, sizeof(tmp), "%u", len);
4784         if (ret < 0)
4785                 goto error;
4786         push_args(ctx, arg_len);
4787         ret = parse_int(ctx, token, tmp, ret, NULL, 0);
4788         if (ret < 0) {
4789                 pop_args(ctx);
4790                 goto error;
4791         }
4792         buf = (uint8_t *)ctx->object + arg_data->offset;
4793         /* Output buffer is not necessarily NUL-terminated. */
4794         memcpy(buf, str, len);
4795         memset((uint8_t *)buf + len, 0x00, size - len);
4796         if (ctx->objmask)
4797                 memset((uint8_t *)ctx->objmask + arg_data->offset, 0xff, len);
4798         /* Save address if requested. */
4799         if (arg_addr->size) {
4800                 memcpy((uint8_t *)ctx->object + arg_addr->offset,
4801                        (void *[]){
4802                         (uint8_t *)ctx->object + arg_data->offset
4803                        },
4804                        arg_addr->size);
4805                 if (ctx->objmask)
4806                         memcpy((uint8_t *)ctx->objmask + arg_addr->offset,
4807                                (void *[]){
4808                                 (uint8_t *)ctx->objmask + arg_data->offset
4809                                },
4810                                arg_addr->size);
4811         }
4812         return len;
4813 error:
4814         push_args(ctx, arg_addr);
4815         push_args(ctx, arg_len);
4816         push_args(ctx, arg_data);
4817         return -1;
4818 }
4819
4820 static int
4821 parse_hex_string(const char *src, uint8_t *dst, uint32_t *size)
4822 {
4823         char *c = NULL;
4824         uint32_t i, len;
4825         char tmp[3];
4826
4827         /* Check input parameters */
4828         if ((src == NULL) ||
4829                 (dst == NULL) ||
4830                 (size == NULL) ||
4831                 (*size == 0))
4832                 return -1;
4833
4834         /* Convert chars to bytes */
4835         for (i = 0, len = 0; i < *size; i += 2) {
4836                 snprintf(tmp, 3, "%s", src + i);
4837                 dst[len++] = strtoul(tmp, &c, 16);
4838                 if (*c != 0) {
4839                         len--;
4840                         dst[len] = 0;
4841                         *size = len;
4842                         return -1;
4843                 }
4844         }
4845         dst[len] = 0;
4846         *size = len;
4847
4848         return 0;
4849 }
4850
4851 static int
4852 parse_hex(struct context *ctx, const struct token *token,
4853                 const char *str, unsigned int len,
4854                 void *buf, unsigned int size)
4855 {
4856         const struct arg *arg_data = pop_args(ctx);
4857         const struct arg *arg_len = pop_args(ctx);
4858         const struct arg *arg_addr = pop_args(ctx);
4859         char tmp[16]; /* Ought to be enough. */
4860         int ret;
4861         unsigned int hexlen = len;
4862         unsigned int length = 256;
4863         uint8_t hex_tmp[length];
4864
4865         /* Arguments are expected. */
4866         if (!arg_data)
4867                 return -1;
4868         if (!arg_len) {
4869                 push_args(ctx, arg_data);
4870                 return -1;
4871         }
4872         if (!arg_addr) {
4873                 push_args(ctx, arg_len);
4874                 push_args(ctx, arg_data);
4875                 return -1;
4876         }
4877         size = arg_data->size;
4878         /* Bit-mask fill is not supported. */
4879         if (arg_data->mask)
4880                 goto error;
4881         if (!ctx->object)
4882                 return len;
4883
4884         /* translate bytes string to array. */
4885         if (str[0] == '0' && ((str[1] == 'x') ||
4886                         (str[1] == 'X'))) {
4887                 str += 2;
4888                 hexlen -= 2;
4889         }
4890         if (hexlen > length)
4891                 return -1;
4892         ret = parse_hex_string(str, hex_tmp, &hexlen);
4893         if (ret < 0)
4894                 goto error;
4895         /* Let parse_int() fill length information first. */
4896         ret = snprintf(tmp, sizeof(tmp), "%u", hexlen);
4897         if (ret < 0)
4898                 goto error;
4899         push_args(ctx, arg_len);
4900         ret = parse_int(ctx, token, tmp, ret, NULL, 0);
4901         if (ret < 0) {
4902                 pop_args(ctx);
4903                 goto error;
4904         }
4905         buf = (uint8_t *)ctx->object + arg_data->offset;
4906         /* Output buffer is not necessarily NUL-terminated. */
4907         memcpy(buf, hex_tmp, hexlen);
4908         memset((uint8_t *)buf + hexlen, 0x00, size - hexlen);
4909         if (ctx->objmask)
4910                 memset((uint8_t *)ctx->objmask + arg_data->offset,
4911                                         0xff, hexlen);
4912         /* Save address if requested. */
4913         if (arg_addr->size) {
4914                 memcpy((uint8_t *)ctx->object + arg_addr->offset,
4915                        (void *[]){
4916                         (uint8_t *)ctx->object + arg_data->offset
4917                        },
4918                        arg_addr->size);
4919                 if (ctx->objmask)
4920                         memcpy((uint8_t *)ctx->objmask + arg_addr->offset,
4921                                (void *[]){
4922                                 (uint8_t *)ctx->objmask + arg_data->offset
4923                                },
4924                                arg_addr->size);
4925         }
4926         return len;
4927 error:
4928         push_args(ctx, arg_addr);
4929         push_args(ctx, arg_len);
4930         push_args(ctx, arg_data);
4931         return -1;
4932
4933 }
4934
4935 /**
4936  * Parse a MAC address.
4937  *
4938  * Last argument (ctx->args) is retrieved to determine storage size and
4939  * location.
4940  */
4941 static int
4942 parse_mac_addr(struct context *ctx, const struct token *token,
4943                const char *str, unsigned int len,
4944                void *buf, unsigned int size)
4945 {
4946         const struct arg *arg = pop_args(ctx);
4947         struct rte_ether_addr tmp;
4948         int ret;
4949
4950         (void)token;
4951         /* Argument is expected. */
4952         if (!arg)
4953                 return -1;
4954         size = arg->size;
4955         /* Bit-mask fill is not supported. */
4956         if (arg->mask || size != sizeof(tmp))
4957                 goto error;
4958         /* Only network endian is supported. */
4959         if (!arg->hton)
4960                 goto error;
4961         ret = rte_ether_unformat_addr(str, &tmp);
4962         if (ret < 0)
4963                 goto error;
4964         if (!ctx->object)
4965                 return len;
4966         buf = (uint8_t *)ctx->object + arg->offset;
4967         memcpy(buf, &tmp, size);
4968         if (ctx->objmask)
4969                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
4970         return len;
4971 error:
4972         push_args(ctx, arg);
4973         return -1;
4974 }
4975
4976 /**
4977  * Parse an IPv4 address.
4978  *
4979  * Last argument (ctx->args) is retrieved to determine storage size and
4980  * location.
4981  */
4982 static int
4983 parse_ipv4_addr(struct context *ctx, const struct token *token,
4984                 const char *str, unsigned int len,
4985                 void *buf, unsigned int size)
4986 {
4987         const struct arg *arg = pop_args(ctx);
4988         char str2[len + 1];
4989         struct in_addr tmp;
4990         int ret;
4991
4992         /* Argument is expected. */
4993         if (!arg)
4994                 return -1;
4995         size = arg->size;
4996         /* Bit-mask fill is not supported. */
4997         if (arg->mask || size != sizeof(tmp))
4998                 goto error;
4999         /* Only network endian is supported. */
5000         if (!arg->hton)
5001                 goto error;
5002         memcpy(str2, str, len);
5003         str2[len] = '\0';
5004         ret = inet_pton(AF_INET, str2, &tmp);
5005         if (ret != 1) {
5006                 /* Attempt integer parsing. */
5007                 push_args(ctx, arg);
5008                 return parse_int(ctx, token, str, len, buf, size);
5009         }
5010         if (!ctx->object)
5011                 return len;
5012         buf = (uint8_t *)ctx->object + arg->offset;
5013         memcpy(buf, &tmp, size);
5014         if (ctx->objmask)
5015                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
5016         return len;
5017 error:
5018         push_args(ctx, arg);
5019         return -1;
5020 }
5021
5022 /**
5023  * Parse an IPv6 address.
5024  *
5025  * Last argument (ctx->args) is retrieved to determine storage size and
5026  * location.
5027  */
5028 static int
5029 parse_ipv6_addr(struct context *ctx, const struct token *token,
5030                 const char *str, unsigned int len,
5031                 void *buf, unsigned int size)
5032 {
5033         const struct arg *arg = pop_args(ctx);
5034         char str2[len + 1];
5035         struct in6_addr tmp;
5036         int ret;
5037
5038         (void)token;
5039         /* Argument is expected. */
5040         if (!arg)
5041                 return -1;
5042         size = arg->size;
5043         /* Bit-mask fill is not supported. */
5044         if (arg->mask || size != sizeof(tmp))
5045                 goto error;
5046         /* Only network endian is supported. */
5047         if (!arg->hton)
5048                 goto error;
5049         memcpy(str2, str, len);
5050         str2[len] = '\0';
5051         ret = inet_pton(AF_INET6, str2, &tmp);
5052         if (ret != 1)
5053                 goto error;
5054         if (!ctx->object)
5055                 return len;
5056         buf = (uint8_t *)ctx->object + arg->offset;
5057         memcpy(buf, &tmp, size);
5058         if (ctx->objmask)
5059                 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
5060         return len;
5061 error:
5062         push_args(ctx, arg);
5063         return -1;
5064 }
5065
5066 /** Boolean values (even indices stand for false). */
5067 static const char *const boolean_name[] = {
5068         "0", "1",
5069         "false", "true",
5070         "no", "yes",
5071         "N", "Y",
5072         "off", "on",
5073         NULL,
5074 };
5075
5076 /**
5077  * Parse a boolean value.
5078  *
5079  * Last argument (ctx->args) is retrieved to determine storage size and
5080  * location.
5081  */
5082 static int
5083 parse_boolean(struct context *ctx, const struct token *token,
5084               const char *str, unsigned int len,
5085               void *buf, unsigned int size)
5086 {
5087         const struct arg *arg = pop_args(ctx);
5088         unsigned int i;
5089         int ret;
5090
5091         /* Argument is expected. */
5092         if (!arg)
5093                 return -1;
5094         for (i = 0; boolean_name[i]; ++i)
5095                 if (!strcmp_partial(boolean_name[i], str, len))
5096                         break;
5097         /* Process token as integer. */
5098         if (boolean_name[i])
5099                 str = i & 1 ? "1" : "0";
5100         push_args(ctx, arg);
5101         ret = parse_int(ctx, token, str, strlen(str), buf, size);
5102         return ret > 0 ? (int)len : ret;
5103 }
5104
5105 /** Parse port and update context. */
5106 static int
5107 parse_port(struct context *ctx, const struct token *token,
5108            const char *str, unsigned int len,
5109            void *buf, unsigned int size)
5110 {
5111         struct buffer *out = &(struct buffer){ .port = 0 };
5112         int ret;
5113
5114         if (buf)
5115                 out = buf;
5116         else {
5117                 ctx->objdata = 0;
5118                 ctx->object = out;
5119                 ctx->objmask = NULL;
5120                 size = sizeof(*out);
5121         }
5122         ret = parse_int(ctx, token, str, len, out, size);
5123         if (ret >= 0)
5124                 ctx->port = out->port;
5125         if (!buf)
5126                 ctx->object = NULL;
5127         return ret;
5128 }
5129
5130 /** Parse set command, initialize output buffer for subsequent tokens. */
5131 static int
5132 parse_set_raw_encap_decap(struct context *ctx, const struct token *token,
5133                           const char *str, unsigned int len,
5134                           void *buf, unsigned int size)
5135 {
5136         struct buffer *out = buf;
5137
5138         /* Token name must match. */
5139         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
5140                 return -1;
5141         /* Nothing else to do if there is no buffer. */
5142         if (!out)
5143                 return len;
5144         /* Make sure buffer is large enough. */
5145         if (size < sizeof(*out))
5146                 return -1;
5147         ctx->objdata = 0;
5148         ctx->objmask = NULL;
5149         if (!out->command)
5150                 return -1;
5151         out->command = ctx->curr;
5152         return len;
5153 }
5154
5155 /**
5156  * Parse set raw_encap/raw_decap command,
5157  * initialize output buffer for subsequent tokens.
5158  */
5159 static int
5160 parse_set_init(struct context *ctx, const struct token *token,
5161                const char *str, unsigned int len,
5162                void *buf, unsigned int size)
5163 {
5164         struct buffer *out = buf;
5165
5166         /* Token name must match. */
5167         if (parse_default(ctx, token, str, len, NULL, 0) < 0)
5168                 return -1;
5169         /* Nothing else to do if there is no buffer. */
5170         if (!out)
5171                 return len;
5172         /* Make sure buffer is large enough. */
5173         if (size < sizeof(*out))
5174                 return -1;
5175         /* Initialize buffer. */
5176         memset(out, 0x00, sizeof(*out));
5177         memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
5178         ctx->objdata = 0;
5179         ctx->object = out;
5180         ctx->objmask = NULL;
5181         if (!out->command) {
5182                 if (ctx->curr != SET)
5183                         return -1;
5184                 if (sizeof(*out) > size)
5185                         return -1;
5186                 out->command = ctx->curr;
5187                 out->args.vc.data = (uint8_t *)out + size;
5188                 /* All we need is pattern */
5189                 out->args.vc.pattern =
5190                         (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
5191                                                sizeof(double));
5192                 ctx->object = out->args.vc.pattern;
5193         }
5194         return len;
5195 }
5196
5197 /** No completion. */
5198 static int
5199 comp_none(struct context *ctx, const struct token *token,
5200           unsigned int ent, char *buf, unsigned int size)
5201 {
5202         (void)ctx;
5203         (void)token;
5204         (void)ent;
5205         (void)buf;
5206         (void)size;
5207         return 0;
5208 }
5209
5210 /** Complete boolean values. */
5211 static int
5212 comp_boolean(struct context *ctx, const struct token *token,
5213              unsigned int ent, char *buf, unsigned int size)
5214 {
5215         unsigned int i;
5216
5217         (void)ctx;
5218         (void)token;
5219         for (i = 0; boolean_name[i]; ++i)
5220                 if (buf && i == ent)
5221                         return strlcpy(buf, boolean_name[i], size);
5222         if (buf)
5223                 return -1;
5224         return i;
5225 }
5226
5227 /** Complete action names. */
5228 static int
5229 comp_action(struct context *ctx, const struct token *token,
5230             unsigned int ent, char *buf, unsigned int size)
5231 {
5232         unsigned int i;
5233
5234         (void)ctx;
5235         (void)token;
5236         for (i = 0; next_action[i]; ++i)
5237                 if (buf && i == ent)
5238                         return strlcpy(buf, token_list[next_action[i]].name,
5239                                        size);
5240         if (buf)
5241                 return -1;
5242         return i;
5243 }
5244
5245 /** Complete available ports. */
5246 static int
5247 comp_port(struct context *ctx, const struct token *token,
5248           unsigned int ent, char *buf, unsigned int size)
5249 {
5250         unsigned int i = 0;
5251         portid_t p;
5252
5253         (void)ctx;
5254         (void)token;
5255         RTE_ETH_FOREACH_DEV(p) {
5256                 if (buf && i == ent)
5257                         return snprintf(buf, size, "%u", p);
5258                 ++i;
5259         }
5260         if (buf)
5261                 return -1;
5262         return i;
5263 }
5264
5265 /** Complete available rule IDs. */
5266 static int
5267 comp_rule_id(struct context *ctx, const struct token *token,
5268              unsigned int ent, char *buf, unsigned int size)
5269 {
5270         unsigned int i = 0;
5271         struct rte_port *port;
5272         struct port_flow *pf;
5273
5274         (void)token;
5275         if (port_id_is_invalid(ctx->port, DISABLED_WARN) ||
5276             ctx->port == (portid_t)RTE_PORT_ALL)
5277                 return -1;
5278         port = &ports[ctx->port];
5279         for (pf = port->flow_list; pf != NULL; pf = pf->next) {
5280                 if (buf && i == ent)
5281                         return snprintf(buf, size, "%u", pf->id);
5282                 ++i;
5283         }
5284         if (buf)
5285                 return -1;
5286         return i;
5287 }
5288
5289 /** Complete type field for RSS action. */
5290 static int
5291 comp_vc_action_rss_type(struct context *ctx, const struct token *token,
5292                         unsigned int ent, char *buf, unsigned int size)
5293 {
5294         unsigned int i;
5295
5296         (void)ctx;
5297         (void)token;
5298         for (i = 0; rss_type_table[i].str; ++i)
5299                 ;
5300         if (!buf)
5301                 return i + 1;
5302         if (ent < i)
5303                 return strlcpy(buf, rss_type_table[ent].str, size);
5304         if (ent == i)
5305                 return snprintf(buf, size, "end");
5306         return -1;
5307 }
5308
5309 /** Complete queue field for RSS action. */
5310 static int
5311 comp_vc_action_rss_queue(struct context *ctx, const struct token *token,
5312                          unsigned int ent, char *buf, unsigned int size)
5313 {
5314         (void)ctx;
5315         (void)token;
5316         if (!buf)
5317                 return nb_rxq + 1;
5318         if (ent < nb_rxq)
5319                 return snprintf(buf, size, "%u", ent);
5320         if (ent == nb_rxq)
5321                 return snprintf(buf, size, "end");
5322         return -1;
5323 }
5324
5325 /** Internal context. */
5326 static struct context cmd_flow_context;
5327
5328 /** Global parser instance (cmdline API). */
5329 cmdline_parse_inst_t cmd_flow;
5330 cmdline_parse_inst_t cmd_set_raw;
5331
5332 /** Initialize context. */
5333 static void
5334 cmd_flow_context_init(struct context *ctx)
5335 {
5336         /* A full memset() is not necessary. */
5337         ctx->curr = ZERO;
5338         ctx->prev = ZERO;
5339         ctx->next_num = 0;
5340         ctx->args_num = 0;
5341         ctx->eol = 0;
5342         ctx->last = 0;
5343         ctx->port = 0;
5344         ctx->objdata = 0;
5345         ctx->object = NULL;
5346         ctx->objmask = NULL;
5347 }
5348
5349 /** Parse a token (cmdline API). */
5350 static int
5351 cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result,
5352                unsigned int size)
5353 {
5354         struct context *ctx = &cmd_flow_context;
5355         const struct token *token;
5356         const enum index *list;
5357         int len;
5358         int i;
5359
5360         (void)hdr;
5361         token = &token_list[ctx->curr];
5362         /* Check argument length. */
5363         ctx->eol = 0;
5364         ctx->last = 1;
5365         for (len = 0; src[len]; ++len)
5366                 if (src[len] == '#' || isspace(src[len]))
5367                         break;
5368         if (!len)
5369                 return -1;
5370         /* Last argument and EOL detection. */
5371         for (i = len; src[i]; ++i)
5372                 if (src[i] == '#' || src[i] == '\r' || src[i] == '\n')
5373                         break;
5374                 else if (!isspace(src[i])) {
5375                         ctx->last = 0;
5376                         break;
5377                 }
5378         for (; src[i]; ++i)
5379                 if (src[i] == '\r' || src[i] == '\n') {
5380                         ctx->eol = 1;
5381                         break;
5382                 }
5383         /* Initialize context if necessary. */
5384         if (!ctx->next_num) {
5385                 if (!token->next)
5386                         return 0;
5387                 ctx->next[ctx->next_num++] = token->next[0];
5388         }
5389         /* Process argument through candidates. */
5390         ctx->prev = ctx->curr;
5391         list = ctx->next[ctx->next_num - 1];
5392         for (i = 0; list[i]; ++i) {
5393                 const struct token *next = &token_list[list[i]];
5394                 int tmp;
5395
5396                 ctx->curr = list[i];
5397                 if (next->call)
5398                         tmp = next->call(ctx, next, src, len, result, size);
5399                 else
5400                         tmp = parse_default(ctx, next, src, len, result, size);
5401                 if (tmp == -1 || tmp != len)
5402                         continue;
5403                 token = next;
5404                 break;
5405         }
5406         if (!list[i])
5407                 return -1;
5408         --ctx->next_num;
5409         /* Push subsequent tokens if any. */
5410         if (token->next)
5411                 for (i = 0; token->next[i]; ++i) {
5412                         if (ctx->next_num == RTE_DIM(ctx->next))
5413                                 return -1;
5414                         ctx->next[ctx->next_num++] = token->next[i];
5415                 }
5416         /* Push arguments if any. */
5417         if (token->args)
5418                 for (i = 0; token->args[i]; ++i) {
5419                         if (ctx->args_num == RTE_DIM(ctx->args))
5420                                 return -1;
5421                         ctx->args[ctx->args_num++] = token->args[i];
5422                 }
5423         return len;
5424 }
5425
5426 /** Return number of completion entries (cmdline API). */
5427 static int
5428 cmd_flow_complete_get_nb(cmdline_parse_token_hdr_t *hdr)
5429 {
5430         struct context *ctx = &cmd_flow_context;
5431         const struct token *token = &token_list[ctx->curr];
5432         const enum index *list;
5433         int i;
5434
5435         (void)hdr;
5436         /* Count number of tokens in current list. */
5437         if (ctx->next_num)
5438                 list = ctx->next[ctx->next_num - 1];
5439         else
5440                 list = token->next[0];
5441         for (i = 0; list[i]; ++i)
5442                 ;
5443         if (!i)
5444                 return 0;
5445         /*
5446          * If there is a single token, use its completion callback, otherwise
5447          * return the number of entries.
5448          */
5449         token = &token_list[list[0]];
5450         if (i == 1 && token->comp) {
5451                 /* Save index for cmd_flow_get_help(). */
5452                 ctx->prev = list[0];
5453                 return token->comp(ctx, token, 0, NULL, 0);
5454         }
5455         return i;
5456 }
5457
5458 /** Return a completion entry (cmdline API). */
5459 static int
5460 cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,
5461                           char *dst, unsigned int size)
5462 {
5463         struct context *ctx = &cmd_flow_context;
5464         const struct token *token = &token_list[ctx->curr];
5465         const enum index *list;
5466         int i;
5467
5468         (void)hdr;
5469         /* Count number of tokens in current list. */
5470         if (ctx->next_num)
5471                 list = ctx->next[ctx->next_num - 1];
5472         else
5473                 list = token->next[0];
5474         for (i = 0; list[i]; ++i)
5475                 ;
5476         if (!i)
5477                 return -1;
5478         /* If there is a single token, use its completion callback. */
5479         token = &token_list[list[0]];
5480         if (i == 1 && token->comp) {
5481                 /* Save index for cmd_flow_get_help(). */
5482                 ctx->prev = list[0];
5483                 return token->comp(ctx, token, index, dst, size) < 0 ? -1 : 0;
5484         }
5485         /* Otherwise make sure the index is valid and use defaults. */
5486         if (index >= i)
5487                 return -1;
5488         token = &token_list[list[index]];
5489         strlcpy(dst, token->name, size);
5490         /* Save index for cmd_flow_get_help(). */
5491         ctx->prev = list[index];
5492         return 0;
5493 }
5494
5495 /** Populate help strings for current token (cmdline API). */
5496 static int
5497 cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
5498 {
5499         struct context *ctx = &cmd_flow_context;
5500         const struct token *token = &token_list[ctx->prev];
5501
5502         (void)hdr;
5503         if (!size)
5504                 return -1;
5505         /* Set token type and update global help with details. */
5506         strlcpy(dst, (token->type ? token->type : "TOKEN"), size);
5507         if (token->help)
5508                 cmd_flow.help_str = token->help;
5509         else
5510                 cmd_flow.help_str = token->name;
5511         return 0;
5512 }
5513
5514 /** Token definition template (cmdline API). */
5515 static struct cmdline_token_hdr cmd_flow_token_hdr = {
5516         .ops = &(struct cmdline_token_ops){
5517                 .parse = cmd_flow_parse,
5518                 .complete_get_nb = cmd_flow_complete_get_nb,
5519                 .complete_get_elt = cmd_flow_complete_get_elt,
5520                 .get_help = cmd_flow_get_help,
5521         },
5522         .offset = 0,
5523 };
5524
5525 /** Populate the next dynamic token. */
5526 static void
5527 cmd_flow_tok(cmdline_parse_token_hdr_t **hdr,
5528              cmdline_parse_token_hdr_t **hdr_inst)
5529 {
5530         struct context *ctx = &cmd_flow_context;
5531
5532         /* Always reinitialize context before requesting the first token. */
5533         if (!(hdr_inst - cmd_flow.tokens))
5534                 cmd_flow_context_init(ctx);
5535         /* Return NULL when no more tokens are expected. */
5536         if (!ctx->next_num && ctx->curr) {
5537                 *hdr = NULL;
5538                 return;
5539         }
5540         /* Determine if command should end here. */
5541         if (ctx->eol && ctx->last && ctx->next_num) {
5542                 const enum index *list = ctx->next[ctx->next_num - 1];
5543                 int i;
5544
5545                 for (i = 0; list[i]; ++i) {
5546                         if (list[i] != END)
5547                                 continue;
5548                         *hdr = NULL;
5549                         return;
5550                 }
5551         }
5552         *hdr = &cmd_flow_token_hdr;
5553 }
5554
5555 /** Dispatch parsed buffer to function calls. */
5556 static void
5557 cmd_flow_parsed(const struct buffer *in)
5558 {
5559         switch (in->command) {
5560         case VALIDATE:
5561                 port_flow_validate(in->port, &in->args.vc.attr,
5562                                    in->args.vc.pattern, in->args.vc.actions);
5563                 break;
5564         case CREATE:
5565                 port_flow_create(in->port, &in->args.vc.attr,
5566                                  in->args.vc.pattern, in->args.vc.actions);
5567                 break;
5568         case DESTROY:
5569                 port_flow_destroy(in->port, in->args.destroy.rule_n,
5570                                   in->args.destroy.rule);
5571                 break;
5572         case FLUSH:
5573                 port_flow_flush(in->port);
5574                 break;
5575         case QUERY:
5576                 port_flow_query(in->port, in->args.query.rule,
5577                                 &in->args.query.action);
5578                 break;
5579         case LIST:
5580                 port_flow_list(in->port, in->args.list.group_n,
5581                                in->args.list.group);
5582                 break;
5583         case ISOLATE:
5584                 port_flow_isolate(in->port, in->args.isolate.set);
5585                 break;
5586         default:
5587                 break;
5588         }
5589 }
5590
5591 /** Token generator and output processing callback (cmdline API). */
5592 static void
5593 cmd_flow_cb(void *arg0, struct cmdline *cl, void *arg2)
5594 {
5595         if (cl == NULL)
5596                 cmd_flow_tok(arg0, arg2);
5597         else
5598                 cmd_flow_parsed(arg0);
5599 }
5600
5601 /** Global parser instance (cmdline API). */
5602 cmdline_parse_inst_t cmd_flow = {
5603         .f = cmd_flow_cb,
5604         .data = NULL, /**< Unused. */
5605         .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
5606         .tokens = {
5607                 NULL,
5608         }, /**< Tokens are returned by cmd_flow_tok(). */
5609 };
5610
5611 /** set cmd facility. Reuse cmd flow's infrastructure as much as possible. */
5612
5613 static void
5614 update_fields(uint8_t *buf, struct rte_flow_item *item, uint16_t next_proto)
5615 {
5616         struct rte_flow_item_ipv4 *ipv4;
5617         struct rte_flow_item_eth *eth;
5618         struct rte_flow_item_ipv6 *ipv6;
5619         struct rte_flow_item_vxlan *vxlan;
5620         struct rte_flow_item_vxlan_gpe *gpe;
5621         struct rte_flow_item_nvgre *nvgre;
5622         uint32_t ipv6_vtc_flow;
5623
5624         switch (item->type) {
5625         case RTE_FLOW_ITEM_TYPE_ETH:
5626                 eth = (struct rte_flow_item_eth *)buf;
5627                 if (next_proto)
5628                         eth->type = rte_cpu_to_be_16(next_proto);
5629                 break;
5630         case RTE_FLOW_ITEM_TYPE_IPV4:
5631                 ipv4 = (struct rte_flow_item_ipv4 *)buf;
5632                 ipv4->hdr.version_ihl = 0x45;
5633                 ipv4->hdr.next_proto_id = (uint8_t)next_proto;
5634                 break;
5635         case RTE_FLOW_ITEM_TYPE_IPV6:
5636                 ipv6 = (struct rte_flow_item_ipv6 *)buf;
5637                 ipv6->hdr.proto = (uint8_t)next_proto;
5638                 ipv6_vtc_flow = rte_be_to_cpu_32(ipv6->hdr.vtc_flow);
5639                 ipv6_vtc_flow &= 0x0FFFFFFF; /*< reset version bits. */
5640                 ipv6_vtc_flow |= 0x60000000; /*< set ipv6 version. */
5641                 ipv6->hdr.vtc_flow = rte_cpu_to_be_32(ipv6_vtc_flow);
5642                 break;
5643         case RTE_FLOW_ITEM_TYPE_VXLAN:
5644                 vxlan = (struct rte_flow_item_vxlan *)buf;
5645                 vxlan->flags = 0x08;
5646                 break;
5647         case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
5648                 gpe = (struct rte_flow_item_vxlan_gpe *)buf;
5649                 gpe->flags = 0x0C;
5650                 break;
5651         case RTE_FLOW_ITEM_TYPE_NVGRE:
5652                 nvgre = (struct rte_flow_item_nvgre *)buf;
5653                 nvgre->protocol = rte_cpu_to_be_16(0x6558);
5654                 nvgre->c_k_s_rsvd0_ver = rte_cpu_to_be_16(0x2000);
5655                 break;
5656         default:
5657                 break;
5658         }
5659 }
5660
5661 /** Helper of get item's default mask. */
5662 static const void *
5663 flow_item_default_mask(const struct rte_flow_item *item)
5664 {
5665         const void *mask = NULL;
5666
5667         switch (item->type) {
5668         case RTE_FLOW_ITEM_TYPE_ANY:
5669                 mask = &rte_flow_item_any_mask;
5670                 break;
5671         case RTE_FLOW_ITEM_TYPE_VF:
5672                 mask = &rte_flow_item_vf_mask;
5673                 break;
5674         case RTE_FLOW_ITEM_TYPE_PORT_ID:
5675                 mask = &rte_flow_item_port_id_mask;
5676                 break;
5677         case RTE_FLOW_ITEM_TYPE_RAW:
5678                 mask = &rte_flow_item_raw_mask;
5679                 break;
5680         case RTE_FLOW_ITEM_TYPE_ETH:
5681                 mask = &rte_flow_item_eth_mask;
5682                 break;
5683         case RTE_FLOW_ITEM_TYPE_VLAN:
5684                 mask = &rte_flow_item_vlan_mask;
5685                 break;
5686         case RTE_FLOW_ITEM_TYPE_IPV4:
5687                 mask = &rte_flow_item_ipv4_mask;
5688                 break;
5689         case RTE_FLOW_ITEM_TYPE_IPV6:
5690                 mask = &rte_flow_item_ipv6_mask;
5691                 break;
5692         case RTE_FLOW_ITEM_TYPE_ICMP:
5693                 mask = &rte_flow_item_icmp_mask;
5694                 break;
5695         case RTE_FLOW_ITEM_TYPE_UDP:
5696                 mask = &rte_flow_item_udp_mask;
5697                 break;
5698         case RTE_FLOW_ITEM_TYPE_TCP:
5699                 mask = &rte_flow_item_tcp_mask;
5700                 break;
5701         case RTE_FLOW_ITEM_TYPE_SCTP:
5702                 mask = &rte_flow_item_sctp_mask;
5703                 break;
5704         case RTE_FLOW_ITEM_TYPE_VXLAN:
5705                 mask = &rte_flow_item_vxlan_mask;
5706                 break;
5707         case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
5708                 mask = &rte_flow_item_vxlan_gpe_mask;
5709                 break;
5710         case RTE_FLOW_ITEM_TYPE_E_TAG:
5711                 mask = &rte_flow_item_e_tag_mask;
5712                 break;
5713         case RTE_FLOW_ITEM_TYPE_NVGRE:
5714                 mask = &rte_flow_item_nvgre_mask;
5715                 break;
5716         case RTE_FLOW_ITEM_TYPE_MPLS:
5717                 mask = &rte_flow_item_mpls_mask;
5718                 break;
5719         case RTE_FLOW_ITEM_TYPE_GRE:
5720                 mask = &rte_flow_item_gre_mask;
5721                 break;
5722         case RTE_FLOW_ITEM_TYPE_META:
5723                 mask = &rte_flow_item_meta_mask;
5724                 break;
5725         case RTE_FLOW_ITEM_TYPE_FUZZY:
5726                 mask = &rte_flow_item_fuzzy_mask;
5727                 break;
5728         case RTE_FLOW_ITEM_TYPE_GTP:
5729                 mask = &rte_flow_item_gtp_mask;
5730                 break;
5731         case RTE_FLOW_ITEM_TYPE_ESP:
5732                 mask = &rte_flow_item_esp_mask;
5733                 break;
5734         default:
5735                 break;
5736         }
5737         return mask;
5738 }
5739
5740
5741
5742 /** Dispatch parsed buffer to function calls. */
5743 static void
5744 cmd_set_raw_parsed(const struct buffer *in)
5745 {
5746         uint32_t n = in->args.vc.pattern_n;
5747         int i = 0;
5748         struct rte_flow_item *item = NULL;
5749         size_t size = 0;
5750         uint8_t *data = NULL;
5751         uint8_t *data_tail = NULL;
5752         size_t *total_size = NULL;
5753         uint16_t upper_layer = 0;
5754         uint16_t proto = 0;
5755
5756         RTE_ASSERT(in->command == SET_RAW_ENCAP ||
5757                    in->command == SET_RAW_DECAP);
5758         if (in->command == SET_RAW_ENCAP) {
5759                 total_size = &raw_encap_conf.size;
5760                 data = (uint8_t *)&raw_encap_conf.data;
5761         } else {
5762                 total_size = &raw_decap_conf.size;
5763                 data = (uint8_t *)&raw_decap_conf.data;
5764         }
5765         *total_size = 0;
5766         memset(data, 0x00, ACTION_RAW_ENCAP_MAX_DATA);
5767         /* process hdr from upper layer to low layer (L3/L4 -> L2). */
5768         data_tail = data + ACTION_RAW_ENCAP_MAX_DATA;
5769         for (i = n - 1 ; i >= 0; --i) {
5770                 item = in->args.vc.pattern + i;
5771                 if (item->spec == NULL)
5772                         item->spec = flow_item_default_mask(item);
5773                 switch (item->type) {
5774                 case RTE_FLOW_ITEM_TYPE_ETH:
5775                         size = sizeof(struct rte_flow_item_eth);
5776                         break;
5777                 case RTE_FLOW_ITEM_TYPE_VLAN:
5778                         size = sizeof(struct rte_flow_item_vlan);
5779                         proto = RTE_ETHER_TYPE_VLAN;
5780                         break;
5781                 case RTE_FLOW_ITEM_TYPE_IPV4:
5782                         size = sizeof(struct rte_flow_item_ipv4);
5783                         proto = RTE_ETHER_TYPE_IPV4;
5784                         break;
5785                 case RTE_FLOW_ITEM_TYPE_IPV6:
5786                         size = sizeof(struct rte_flow_item_ipv6);
5787                         proto = RTE_ETHER_TYPE_IPV6;
5788                         break;
5789                 case RTE_FLOW_ITEM_TYPE_UDP:
5790                         size = sizeof(struct rte_flow_item_udp);
5791                         proto = 0x11;
5792                         break;
5793                 case RTE_FLOW_ITEM_TYPE_TCP:
5794                         size = sizeof(struct rte_flow_item_tcp);
5795                         proto = 0x06;
5796                         break;
5797                 case RTE_FLOW_ITEM_TYPE_VXLAN:
5798                         size = sizeof(struct rte_flow_item_vxlan);
5799                         break;
5800                 case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
5801                         size = sizeof(struct rte_flow_item_vxlan_gpe);
5802                         break;
5803                 case RTE_FLOW_ITEM_TYPE_GRE:
5804                         size = sizeof(struct rte_flow_item_gre);
5805                         proto = 0x2F;
5806                         break;
5807                 case RTE_FLOW_ITEM_TYPE_MPLS:
5808                         size = sizeof(struct rte_flow_item_mpls);
5809                         break;
5810                 case RTE_FLOW_ITEM_TYPE_NVGRE:
5811                         size = sizeof(struct rte_flow_item_nvgre);
5812                         proto = 0x2F;
5813                         break;
5814                 default:
5815                         printf("Error - Not supported item\n");
5816                         *total_size = 0;
5817                         memset(data, 0x00, ACTION_RAW_ENCAP_MAX_DATA);
5818                         return;
5819                 }
5820                 *total_size += size;
5821                 rte_memcpy(data_tail - (*total_size), item->spec, size);
5822                 /* update some fields which cannot be set by cmdline */
5823                 update_fields((data_tail - (*total_size)), item,
5824                               upper_layer);
5825                 upper_layer = proto;
5826         }
5827         if (verbose_level & 0x1)
5828                 printf("total data size is %zu\n", (*total_size));
5829         RTE_ASSERT((*total_size) <= ACTION_RAW_ENCAP_MAX_DATA);
5830 }
5831
5832 /** Populate help strings for current token (cmdline API). */
5833 static int
5834 cmd_set_raw_get_help(cmdline_parse_token_hdr_t *hdr, char *dst,
5835                      unsigned int size)
5836 {
5837         struct context *ctx = &cmd_flow_context;
5838         const struct token *token = &token_list[ctx->prev];
5839
5840         (void)hdr;
5841         if (!size)
5842                 return -1;
5843         /* Set token type and update global help with details. */
5844         snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
5845         if (token->help)
5846                 cmd_set_raw.help_str = token->help;
5847         else
5848                 cmd_set_raw.help_str = token->name;
5849         return 0;
5850 }
5851
5852 /** Token definition template (cmdline API). */
5853 static struct cmdline_token_hdr cmd_set_raw_token_hdr = {
5854         .ops = &(struct cmdline_token_ops){
5855                 .parse = cmd_flow_parse,
5856                 .complete_get_nb = cmd_flow_complete_get_nb,
5857                 .complete_get_elt = cmd_flow_complete_get_elt,
5858                 .get_help = cmd_set_raw_get_help,
5859         },
5860         .offset = 0,
5861 };
5862
5863 /** Populate the next dynamic token. */
5864 static void
5865 cmd_set_raw_tok(cmdline_parse_token_hdr_t **hdr,
5866              cmdline_parse_token_hdr_t **hdr_inst)
5867 {
5868         struct context *ctx = &cmd_flow_context;
5869
5870         /* Always reinitialize context before requesting the first token. */
5871         if (!(hdr_inst - cmd_set_raw.tokens)) {
5872                 cmd_flow_context_init(ctx);
5873                 ctx->curr = START_SET;
5874         }
5875         /* Return NULL when no more tokens are expected. */
5876         if (!ctx->next_num && (ctx->curr != START_SET)) {
5877                 *hdr = NULL;
5878                 return;
5879         }
5880         /* Determine if command should end here. */
5881         if (ctx->eol && ctx->last && ctx->next_num) {
5882                 const enum index *list = ctx->next[ctx->next_num - 1];
5883                 int i;
5884
5885                 for (i = 0; list[i]; ++i) {
5886                         if (list[i] != END)
5887                                 continue;
5888                         *hdr = NULL;
5889                         return;
5890                 }
5891         }
5892         *hdr = &cmd_set_raw_token_hdr;
5893 }
5894
5895 /** Token generator and output processing callback (cmdline API). */
5896 static void
5897 cmd_set_raw_cb(void *arg0, struct cmdline *cl, void *arg2)
5898 {
5899         if (cl == NULL)
5900                 cmd_set_raw_tok(arg0, arg2);
5901         else
5902                 cmd_set_raw_parsed(arg0);
5903 }
5904
5905 /** Global parser instance (cmdline API). */
5906 cmdline_parse_inst_t cmd_set_raw = {
5907         .f = cmd_set_raw_cb,
5908         .data = NULL, /**< Unused. */
5909         .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
5910         .tokens = {
5911                 NULL,
5912         }, /**< Tokens are returned by cmd_flow_tok(). */
5913 };