1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox Technologies, Ltd
13 #include <arpa/inet.h>
14 #include <sys/socket.h>
16 #include <rte_common.h>
17 #include <rte_eth_ctrl.h>
18 #include <rte_ethdev.h>
19 #include <rte_byteorder.h>
20 #include <cmdline_parse.h>
21 #include <cmdline_parse_etheraddr.h>
26 /** Parser token indices. */
46 /* Top-level command. */
49 /* Sub-level commands. */
58 /* Destroy arguments. */
61 /* Query arguments. */
67 /* Validate/create arguments. */
73 /* Validate/create pattern. */
138 ITEM_E_TAG_GRP_ECID_B,
155 /* Validate/create actions. */
171 ACTION_RSS_FUNC_DEFAULT,
172 ACTION_RSS_FUNC_TOEPLITZ,
173 ACTION_RSS_FUNC_SIMPLE_XOR,
188 /** Maximum size for pattern in struct rte_flow_item_raw. */
189 #define ITEM_RAW_PATTERN_SIZE 40
191 /** Storage size for struct rte_flow_item_raw including pattern. */
192 #define ITEM_RAW_SIZE \
193 (sizeof(struct rte_flow_item_raw) + ITEM_RAW_PATTERN_SIZE)
195 /** Maximum number of queue indices in struct rte_flow_action_rss. */
196 #define ACTION_RSS_QUEUE_NUM 32
198 /** Storage for struct rte_flow_action_rss including external data. */
199 struct action_rss_data {
200 struct rte_flow_action_rss conf;
201 uint8_t key[RSS_HASH_KEY_LENGTH];
202 uint16_t queue[ACTION_RSS_QUEUE_NUM];
205 /** Maximum number of subsequent tokens and arguments on the stack. */
206 #define CTX_STACK_SIZE 16
208 /** Parser context. */
210 /** Stack of subsequent token lists to process. */
211 const enum index *next[CTX_STACK_SIZE];
212 /** Arguments for stacked tokens. */
213 const void *args[CTX_STACK_SIZE];
214 enum index curr; /**< Current token index. */
215 enum index prev; /**< Index of the last token seen. */
216 int next_num; /**< Number of entries in next[]. */
217 int args_num; /**< Number of entries in args[]. */
218 uint32_t eol:1; /**< EOL has been detected. */
219 uint32_t last:1; /**< No more arguments. */
220 portid_t port; /**< Current port ID (for completions). */
221 uint32_t objdata; /**< Object-specific data. */
222 void *object; /**< Address of current object for relative offsets. */
223 void *objmask; /**< Object a full mask must be written to. */
226 /** Token argument. */
228 uint32_t hton:1; /**< Use network byte ordering. */
229 uint32_t sign:1; /**< Value is signed. */
230 uint32_t bounded:1; /**< Value is bounded. */
231 uintmax_t min; /**< Minimum value if bounded. */
232 uintmax_t max; /**< Maximum value if bounded. */
233 uint32_t offset; /**< Relative offset from ctx->object. */
234 uint32_t size; /**< Field size. */
235 const uint8_t *mask; /**< Bit-mask to use instead of offset/size. */
238 /** Parser token definition. */
240 /** Type displayed during completion (defaults to "TOKEN"). */
242 /** Help displayed during completion (defaults to token name). */
244 /** Private data used by parser functions. */
247 * Lists of subsequent tokens to push on the stack. Each call to the
248 * parser consumes the last entry of that stack.
250 const enum index *const *next;
251 /** Arguments stack for subsequent tokens that need them. */
252 const struct arg *const *args;
254 * Token-processing callback, returns -1 in case of error, the
255 * length of the matched string otherwise. If NULL, attempts to
256 * match the token name.
258 * If buf is not NULL, the result should be stored in it according
259 * to context. An error is returned if not large enough.
261 int (*call)(struct context *ctx, const struct token *token,
262 const char *str, unsigned int len,
263 void *buf, unsigned int size);
265 * Callback that provides possible values for this token, used for
266 * completion. Returns -1 in case of error, the number of possible
267 * values otherwise. If NULL, the token name is used.
269 * If buf is not NULL, entry index ent is written to buf and the
270 * full length of the entry is returned (same behavior as
273 int (*comp)(struct context *ctx, const struct token *token,
274 unsigned int ent, char *buf, unsigned int size);
275 /** Mandatory token name, no default value. */
279 /** Static initializer for the next field. */
280 #define NEXT(...) (const enum index *const []){ __VA_ARGS__, NULL, }
282 /** Static initializer for a NEXT() entry. */
283 #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, }
285 /** Static initializer for the args field. */
286 #define ARGS(...) (const struct arg *const []){ __VA_ARGS__, NULL, }
288 /** Static initializer for ARGS() to target a field. */
289 #define ARGS_ENTRY(s, f) \
290 (&(const struct arg){ \
291 .offset = offsetof(s, f), \
292 .size = sizeof(((s *)0)->f), \
295 /** Static initializer for ARGS() to target a bit-field. */
296 #define ARGS_ENTRY_BF(s, f, b) \
297 (&(const struct arg){ \
299 .mask = (const void *)&(const s){ .f = (1 << (b)) - 1 }, \
302 /** Static initializer for ARGS() to target an arbitrary bit-mask. */
303 #define ARGS_ENTRY_MASK(s, f, m) \
304 (&(const struct arg){ \
305 .offset = offsetof(s, f), \
306 .size = sizeof(((s *)0)->f), \
307 .mask = (const void *)(m), \
310 /** Same as ARGS_ENTRY_MASK() using network byte ordering for the value. */
311 #define ARGS_ENTRY_MASK_HTON(s, f, m) \
312 (&(const struct arg){ \
314 .offset = offsetof(s, f), \
315 .size = sizeof(((s *)0)->f), \
316 .mask = (const void *)(m), \
319 /** Static initializer for ARGS() to target a pointer. */
320 #define ARGS_ENTRY_PTR(s, f) \
321 (&(const struct arg){ \
322 .size = sizeof(*((s *)0)->f), \
325 /** Static initializer for ARGS() with arbitrary offset and size. */
326 #define ARGS_ENTRY_ARB(o, s) \
327 (&(const struct arg){ \
332 /** Same as ARGS_ENTRY_ARB() with bounded values. */
333 #define ARGS_ENTRY_ARB_BOUNDED(o, s, i, a) \
334 (&(const struct arg){ \
342 /** Same as ARGS_ENTRY() using network byte ordering. */
343 #define ARGS_ENTRY_HTON(s, f) \
344 (&(const struct arg){ \
346 .offset = offsetof(s, f), \
347 .size = sizeof(((s *)0)->f), \
350 /** Parser output buffer layout expected by cmd_flow_parsed(). */
352 enum index command; /**< Flow command. */
353 portid_t port; /**< Affected port ID. */
356 struct rte_flow_attr attr;
357 struct rte_flow_item *pattern;
358 struct rte_flow_action *actions;
362 } vc; /**< Validate/create arguments. */
366 } destroy; /**< Destroy arguments. */
369 enum rte_flow_action_type action;
370 } query; /**< Query arguments. */
374 } list; /**< List arguments. */
377 } isolate; /**< Isolated mode arguments. */
378 } args; /**< Command arguments. */
381 /** Private data for pattern items. */
382 struct parse_item_priv {
383 enum rte_flow_item_type type; /**< Item type. */
384 uint32_t size; /**< Size of item specification structure. */
387 #define PRIV_ITEM(t, s) \
388 (&(const struct parse_item_priv){ \
389 .type = RTE_FLOW_ITEM_TYPE_ ## t, \
393 /** Private data for actions. */
394 struct parse_action_priv {
395 enum rte_flow_action_type type; /**< Action type. */
396 uint32_t size; /**< Size of action configuration structure. */
399 #define PRIV_ACTION(t, s) \
400 (&(const struct parse_action_priv){ \
401 .type = RTE_FLOW_ACTION_TYPE_ ## t, \
405 static const enum index next_vc_attr[] = {
414 static const enum index next_destroy_attr[] = {
420 static const enum index next_list_attr[] = {
426 static const enum index item_param[] = {
435 static const enum index next_item[] = {
465 static const enum index item_fuzzy[] = {
471 static const enum index item_any[] = {
477 static const enum index item_vf[] = {
483 static const enum index item_port[] = {
489 static const enum index item_raw[] = {
499 static const enum index item_eth[] = {
507 static const enum index item_vlan[] = {
517 static const enum index item_ipv4[] = {
527 static const enum index item_ipv6[] = {
538 static const enum index item_icmp[] = {
545 static const enum index item_udp[] = {
552 static const enum index item_tcp[] = {
560 static const enum index item_sctp[] = {
569 static const enum index item_vxlan[] = {
575 static const enum index item_e_tag[] = {
576 ITEM_E_TAG_GRP_ECID_B,
581 static const enum index item_nvgre[] = {
587 static const enum index item_mpls[] = {
593 static const enum index item_gre[] = {
599 static const enum index item_gtp[] = {
605 static const enum index item_geneve[] = {
612 static const enum index next_action[] = {
628 static const enum index action_mark[] = {
634 static const enum index action_queue[] = {
640 static const enum index action_rss[] = {
651 static const enum index action_vf[] = {
658 static const enum index action_meter[] = {
664 static int parse_init(struct context *, const struct token *,
665 const char *, unsigned int,
666 void *, unsigned int);
667 static int parse_vc(struct context *, const struct token *,
668 const char *, unsigned int,
669 void *, unsigned int);
670 static int parse_vc_spec(struct context *, const struct token *,
671 const char *, unsigned int, void *, unsigned int);
672 static int parse_vc_conf(struct context *, const struct token *,
673 const char *, unsigned int, void *, unsigned int);
674 static int parse_vc_action_rss(struct context *, const struct token *,
675 const char *, unsigned int, void *,
677 static int parse_vc_action_rss_func(struct context *, const struct token *,
678 const char *, unsigned int, void *,
680 static int parse_vc_action_rss_type(struct context *, const struct token *,
681 const char *, unsigned int, void *,
683 static int parse_vc_action_rss_queue(struct context *, const struct token *,
684 const char *, unsigned int, void *,
686 static int parse_destroy(struct context *, const struct token *,
687 const char *, unsigned int,
688 void *, unsigned int);
689 static int parse_flush(struct context *, const struct token *,
690 const char *, unsigned int,
691 void *, unsigned int);
692 static int parse_query(struct context *, const struct token *,
693 const char *, unsigned int,
694 void *, unsigned int);
695 static int parse_action(struct context *, const struct token *,
696 const char *, unsigned int,
697 void *, unsigned int);
698 static int parse_list(struct context *, const struct token *,
699 const char *, unsigned int,
700 void *, unsigned int);
701 static int parse_isolate(struct context *, const struct token *,
702 const char *, unsigned int,
703 void *, unsigned int);
704 static int parse_int(struct context *, const struct token *,
705 const char *, unsigned int,
706 void *, unsigned int);
707 static int parse_prefix(struct context *, const struct token *,
708 const char *, unsigned int,
709 void *, unsigned int);
710 static int parse_boolean(struct context *, const struct token *,
711 const char *, unsigned int,
712 void *, unsigned int);
713 static int parse_string(struct context *, const struct token *,
714 const char *, unsigned int,
715 void *, unsigned int);
716 static int parse_mac_addr(struct context *, const struct token *,
717 const char *, unsigned int,
718 void *, unsigned int);
719 static int parse_ipv4_addr(struct context *, const struct token *,
720 const char *, unsigned int,
721 void *, unsigned int);
722 static int parse_ipv6_addr(struct context *, const struct token *,
723 const char *, unsigned int,
724 void *, unsigned int);
725 static int parse_port(struct context *, const struct token *,
726 const char *, unsigned int,
727 void *, unsigned int);
728 static int comp_none(struct context *, const struct token *,
729 unsigned int, char *, unsigned int);
730 static int comp_boolean(struct context *, const struct token *,
731 unsigned int, char *, unsigned int);
732 static int comp_action(struct context *, const struct token *,
733 unsigned int, char *, unsigned int);
734 static int comp_port(struct context *, const struct token *,
735 unsigned int, char *, unsigned int);
736 static int comp_rule_id(struct context *, const struct token *,
737 unsigned int, char *, unsigned int);
738 static int comp_vc_action_rss_type(struct context *, const struct token *,
739 unsigned int, char *, unsigned int);
740 static int comp_vc_action_rss_queue(struct context *, const struct token *,
741 unsigned int, char *, unsigned int);
743 /** Token definitions. */
744 static const struct token token_list[] = {
745 /* Special tokens. */
748 .help = "null entry, abused as the entry point",
749 .next = NEXT(NEXT_ENTRY(FLOW)),
754 .help = "command may end here",
760 .help = "integer value",
765 .name = "{unsigned}",
767 .help = "unsigned integer value",
774 .help = "prefix length for bit-mask",
775 .call = parse_prefix,
781 .help = "any boolean value",
782 .call = parse_boolean,
783 .comp = comp_boolean,
788 .help = "fixed string",
789 .call = parse_string,
793 .name = "{MAC address}",
795 .help = "standard MAC address notation",
796 .call = parse_mac_addr,
800 .name = "{IPv4 address}",
801 .type = "IPV4 ADDRESS",
802 .help = "standard IPv4 address notation",
803 .call = parse_ipv4_addr,
807 .name = "{IPv6 address}",
808 .type = "IPV6 ADDRESS",
809 .help = "standard IPv6 address notation",
810 .call = parse_ipv6_addr,
816 .help = "rule identifier",
818 .comp = comp_rule_id,
823 .help = "port identifier",
828 .name = "{group_id}",
830 .help = "group identifier",
837 .help = "priority level",
841 /* Top-level command. */
844 .type = "{command} {port_id} [{arg} [...]]",
845 .help = "manage ingress/egress flow rules",
846 .next = NEXT(NEXT_ENTRY
856 /* Sub-level commands. */
859 .help = "check whether a flow rule can be created",
860 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
861 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
866 .help = "create a flow rule",
867 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
868 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
873 .help = "destroy specific flow rules",
874 .next = NEXT(NEXT_ENTRY(DESTROY_RULE), NEXT_ENTRY(PORT_ID)),
875 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
876 .call = parse_destroy,
880 .help = "destroy all flow rules",
881 .next = NEXT(NEXT_ENTRY(PORT_ID)),
882 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
887 .help = "query an existing flow rule",
888 .next = NEXT(NEXT_ENTRY(QUERY_ACTION),
890 NEXT_ENTRY(PORT_ID)),
891 .args = ARGS(ARGS_ENTRY(struct buffer, args.query.action),
892 ARGS_ENTRY(struct buffer, args.query.rule),
893 ARGS_ENTRY(struct buffer, port)),
898 .help = "list existing flow rules",
899 .next = NEXT(next_list_attr, NEXT_ENTRY(PORT_ID)),
900 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
905 .help = "restrict ingress traffic to the defined flow rules",
906 .next = NEXT(NEXT_ENTRY(BOOLEAN),
907 NEXT_ENTRY(PORT_ID)),
908 .args = ARGS(ARGS_ENTRY(struct buffer, args.isolate.set),
909 ARGS_ENTRY(struct buffer, port)),
910 .call = parse_isolate,
912 /* Destroy arguments. */
915 .help = "specify a rule identifier",
916 .next = NEXT(next_destroy_attr, NEXT_ENTRY(RULE_ID)),
917 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.destroy.rule)),
918 .call = parse_destroy,
920 /* Query arguments. */
924 .help = "action to query, must be part of the rule",
925 .call = parse_action,
928 /* List arguments. */
931 .help = "specify a group",
932 .next = NEXT(next_list_attr, NEXT_ENTRY(GROUP_ID)),
933 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.list.group)),
936 /* Validate/create attributes. */
939 .help = "specify a group",
940 .next = NEXT(next_vc_attr, NEXT_ENTRY(GROUP_ID)),
941 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, group)),
946 .help = "specify a priority level",
947 .next = NEXT(next_vc_attr, NEXT_ENTRY(PRIORITY_LEVEL)),
948 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, priority)),
953 .help = "affect rule to ingress",
954 .next = NEXT(next_vc_attr),
959 .help = "affect rule to egress",
960 .next = NEXT(next_vc_attr),
963 /* Validate/create pattern. */
966 .help = "submit a list of pattern items",
967 .next = NEXT(next_item),
972 .help = "match value perfectly (with full bit-mask)",
973 .call = parse_vc_spec,
975 [ITEM_PARAM_SPEC] = {
977 .help = "match value according to configured bit-mask",
978 .call = parse_vc_spec,
980 [ITEM_PARAM_LAST] = {
982 .help = "specify upper bound to establish a range",
983 .call = parse_vc_spec,
985 [ITEM_PARAM_MASK] = {
987 .help = "specify bit-mask with relevant bits set to one",
988 .call = parse_vc_spec,
990 [ITEM_PARAM_PREFIX] = {
992 .help = "generate bit-mask from a prefix length",
993 .call = parse_vc_spec,
997 .help = "specify next pattern item",
998 .next = NEXT(next_item),
1002 .help = "end list of pattern items",
1003 .priv = PRIV_ITEM(END, 0),
1004 .next = NEXT(NEXT_ENTRY(ACTIONS)),
1009 .help = "no-op pattern item",
1010 .priv = PRIV_ITEM(VOID, 0),
1011 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1016 .help = "perform actions when pattern does not match",
1017 .priv = PRIV_ITEM(INVERT, 0),
1018 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1023 .help = "match any protocol for the current layer",
1024 .priv = PRIV_ITEM(ANY, sizeof(struct rte_flow_item_any)),
1025 .next = NEXT(item_any),
1030 .help = "number of layers covered",
1031 .next = NEXT(item_any, NEXT_ENTRY(UNSIGNED), item_param),
1032 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_any, num)),
1036 .help = "match packets addressed to the physical function",
1037 .priv = PRIV_ITEM(PF, 0),
1038 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1043 .help = "match packets addressed to a virtual function ID",
1044 .priv = PRIV_ITEM(VF, sizeof(struct rte_flow_item_vf)),
1045 .next = NEXT(item_vf),
1050 .help = "destination VF ID",
1051 .next = NEXT(item_vf, NEXT_ENTRY(UNSIGNED), item_param),
1052 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_vf, id)),
1056 .help = "device-specific physical port index to use",
1057 .priv = PRIV_ITEM(PORT, sizeof(struct rte_flow_item_port)),
1058 .next = NEXT(item_port),
1061 [ITEM_PORT_INDEX] = {
1063 .help = "physical port index",
1064 .next = NEXT(item_port, NEXT_ENTRY(UNSIGNED), item_param),
1065 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_port, index)),
1069 .help = "match an arbitrary byte string",
1070 .priv = PRIV_ITEM(RAW, ITEM_RAW_SIZE),
1071 .next = NEXT(item_raw),
1074 [ITEM_RAW_RELATIVE] = {
1076 .help = "look for pattern after the previous item",
1077 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1078 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1081 [ITEM_RAW_SEARCH] = {
1083 .help = "search pattern from offset (see also limit)",
1084 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1085 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1088 [ITEM_RAW_OFFSET] = {
1090 .help = "absolute or relative offset for pattern",
1091 .next = NEXT(item_raw, NEXT_ENTRY(INTEGER), item_param),
1092 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, offset)),
1094 [ITEM_RAW_LIMIT] = {
1096 .help = "search area limit for start of pattern",
1097 .next = NEXT(item_raw, NEXT_ENTRY(UNSIGNED), item_param),
1098 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, limit)),
1100 [ITEM_RAW_PATTERN] = {
1102 .help = "byte string to look for",
1103 .next = NEXT(item_raw,
1105 NEXT_ENTRY(ITEM_PARAM_IS,
1108 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, pattern),
1109 ARGS_ENTRY(struct rte_flow_item_raw, length),
1110 ARGS_ENTRY_ARB(sizeof(struct rte_flow_item_raw),
1111 ITEM_RAW_PATTERN_SIZE)),
1115 .help = "match Ethernet header",
1116 .priv = PRIV_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
1117 .next = NEXT(item_eth),
1122 .help = "destination MAC",
1123 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1124 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, dst)),
1128 .help = "source MAC",
1129 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1130 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, src)),
1134 .help = "EtherType",
1135 .next = NEXT(item_eth, NEXT_ENTRY(UNSIGNED), item_param),
1136 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, type)),
1140 .help = "match 802.1Q/ad VLAN tag",
1141 .priv = PRIV_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
1142 .next = NEXT(item_vlan),
1145 [ITEM_VLAN_TPID] = {
1147 .help = "tag protocol identifier",
1148 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1149 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tpid)),
1153 .help = "tag control information",
1154 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1155 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tci)),
1159 .help = "priority code point",
1160 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1161 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1166 .help = "drop eligible indicator",
1167 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1168 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1173 .help = "VLAN identifier",
1174 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1175 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1180 .help = "match IPv4 header",
1181 .priv = PRIV_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
1182 .next = NEXT(item_ipv4),
1187 .help = "type of service",
1188 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1189 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1190 hdr.type_of_service)),
1194 .help = "time to live",
1195 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1196 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1199 [ITEM_IPV4_PROTO] = {
1201 .help = "next protocol ID",
1202 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1203 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1204 hdr.next_proto_id)),
1208 .help = "source address",
1209 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1210 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1215 .help = "destination address",
1216 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1217 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1222 .help = "match IPv6 header",
1223 .priv = PRIV_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
1224 .next = NEXT(item_ipv6),
1229 .help = "traffic class",
1230 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1231 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1233 "\x0f\xf0\x00\x00")),
1235 [ITEM_IPV6_FLOW] = {
1237 .help = "flow label",
1238 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1239 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1241 "\x00\x0f\xff\xff")),
1243 [ITEM_IPV6_PROTO] = {
1245 .help = "protocol (next header)",
1246 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1247 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1252 .help = "hop limit",
1253 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1254 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1259 .help = "source address",
1260 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1261 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1266 .help = "destination address",
1267 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1268 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1273 .help = "match ICMP header",
1274 .priv = PRIV_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
1275 .next = NEXT(item_icmp),
1278 [ITEM_ICMP_TYPE] = {
1280 .help = "ICMP packet type",
1281 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1282 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1285 [ITEM_ICMP_CODE] = {
1287 .help = "ICMP packet code",
1288 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1289 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1294 .help = "match UDP header",
1295 .priv = PRIV_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
1296 .next = NEXT(item_udp),
1301 .help = "UDP source port",
1302 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1303 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1308 .help = "UDP destination port",
1309 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1310 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1315 .help = "match TCP header",
1316 .priv = PRIV_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
1317 .next = NEXT(item_tcp),
1322 .help = "TCP source port",
1323 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1324 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1329 .help = "TCP destination port",
1330 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1331 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1334 [ITEM_TCP_FLAGS] = {
1336 .help = "TCP flags",
1337 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1338 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1343 .help = "match SCTP header",
1344 .priv = PRIV_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
1345 .next = NEXT(item_sctp),
1350 .help = "SCTP source port",
1351 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1352 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1357 .help = "SCTP destination port",
1358 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1359 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1364 .help = "validation tag",
1365 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1366 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1369 [ITEM_SCTP_CKSUM] = {
1372 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1373 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1378 .help = "match VXLAN header",
1379 .priv = PRIV_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
1380 .next = NEXT(item_vxlan),
1383 [ITEM_VXLAN_VNI] = {
1385 .help = "VXLAN identifier",
1386 .next = NEXT(item_vxlan, NEXT_ENTRY(UNSIGNED), item_param),
1387 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vxlan, vni)),
1391 .help = "match E-Tag header",
1392 .priv = PRIV_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
1393 .next = NEXT(item_e_tag),
1396 [ITEM_E_TAG_GRP_ECID_B] = {
1397 .name = "grp_ecid_b",
1398 .help = "GRP and E-CID base",
1399 .next = NEXT(item_e_tag, NEXT_ENTRY(UNSIGNED), item_param),
1400 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_e_tag,
1406 .help = "match NVGRE header",
1407 .priv = PRIV_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
1408 .next = NEXT(item_nvgre),
1411 [ITEM_NVGRE_TNI] = {
1413 .help = "virtual subnet ID",
1414 .next = NEXT(item_nvgre, NEXT_ENTRY(UNSIGNED), item_param),
1415 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_nvgre, tni)),
1419 .help = "match MPLS header",
1420 .priv = PRIV_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
1421 .next = NEXT(item_mpls),
1424 [ITEM_MPLS_LABEL] = {
1426 .help = "MPLS label",
1427 .next = NEXT(item_mpls, NEXT_ENTRY(UNSIGNED), item_param),
1428 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_mpls,
1434 .help = "match GRE header",
1435 .priv = PRIV_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
1436 .next = NEXT(item_gre),
1439 [ITEM_GRE_PROTO] = {
1441 .help = "GRE protocol type",
1442 .next = NEXT(item_gre, NEXT_ENTRY(UNSIGNED), item_param),
1443 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gre,
1448 .help = "fuzzy pattern match, expect faster than default",
1449 .priv = PRIV_ITEM(FUZZY,
1450 sizeof(struct rte_flow_item_fuzzy)),
1451 .next = NEXT(item_fuzzy),
1454 [ITEM_FUZZY_THRESH] = {
1456 .help = "match accuracy threshold",
1457 .next = NEXT(item_fuzzy, NEXT_ENTRY(UNSIGNED), item_param),
1458 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_fuzzy,
1463 .help = "match GTP header",
1464 .priv = PRIV_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
1465 .next = NEXT(item_gtp),
1470 .help = "tunnel endpoint identifier",
1471 .next = NEXT(item_gtp, NEXT_ENTRY(UNSIGNED), item_param),
1472 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gtp, teid)),
1476 .help = "match GTP header",
1477 .priv = PRIV_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
1478 .next = NEXT(item_gtp),
1483 .help = "match GTP header",
1484 .priv = PRIV_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
1485 .next = NEXT(item_gtp),
1490 .help = "match GENEVE header",
1491 .priv = PRIV_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
1492 .next = NEXT(item_geneve),
1495 [ITEM_GENEVE_VNI] = {
1497 .help = "virtual network identifier",
1498 .next = NEXT(item_geneve, NEXT_ENTRY(UNSIGNED), item_param),
1499 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_geneve, vni)),
1501 [ITEM_GENEVE_PROTO] = {
1503 .help = "GENEVE protocol type",
1504 .next = NEXT(item_geneve, NEXT_ENTRY(UNSIGNED), item_param),
1505 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_geneve,
1509 /* Validate/create actions. */
1512 .help = "submit a list of associated actions",
1513 .next = NEXT(next_action),
1518 .help = "specify next action",
1519 .next = NEXT(next_action),
1523 .help = "end list of actions",
1524 .priv = PRIV_ACTION(END, 0),
1529 .help = "no-op action",
1530 .priv = PRIV_ACTION(VOID, 0),
1531 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1534 [ACTION_PASSTHRU] = {
1536 .help = "let subsequent rule process matched packets",
1537 .priv = PRIV_ACTION(PASSTHRU, 0),
1538 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1543 .help = "attach 32 bit value to packets",
1544 .priv = PRIV_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
1545 .next = NEXT(action_mark),
1548 [ACTION_MARK_ID] = {
1550 .help = "32 bit value to return with packets",
1551 .next = NEXT(action_mark, NEXT_ENTRY(UNSIGNED)),
1552 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_mark, id)),
1553 .call = parse_vc_conf,
1557 .help = "flag packets",
1558 .priv = PRIV_ACTION(FLAG, 0),
1559 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1564 .help = "assign packets to a given queue index",
1565 .priv = PRIV_ACTION(QUEUE,
1566 sizeof(struct rte_flow_action_queue)),
1567 .next = NEXT(action_queue),
1570 [ACTION_QUEUE_INDEX] = {
1572 .help = "queue index to use",
1573 .next = NEXT(action_queue, NEXT_ENTRY(UNSIGNED)),
1574 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_queue, index)),
1575 .call = parse_vc_conf,
1579 .help = "drop packets (note: passthru has priority)",
1580 .priv = PRIV_ACTION(DROP, 0),
1581 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1586 .help = "enable counters for this rule",
1587 .priv = PRIV_ACTION(COUNT, 0),
1588 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1593 .help = "spread packets among several queues",
1594 .priv = PRIV_ACTION(RSS, sizeof(struct action_rss_data)),
1595 .next = NEXT(action_rss),
1596 .call = parse_vc_action_rss,
1598 [ACTION_RSS_FUNC] = {
1600 .help = "RSS hash function to apply",
1601 .next = NEXT(action_rss,
1602 NEXT_ENTRY(ACTION_RSS_FUNC_DEFAULT,
1603 ACTION_RSS_FUNC_TOEPLITZ,
1604 ACTION_RSS_FUNC_SIMPLE_XOR)),
1606 [ACTION_RSS_FUNC_DEFAULT] = {
1608 .help = "default hash function",
1609 .call = parse_vc_action_rss_func,
1611 [ACTION_RSS_FUNC_TOEPLITZ] = {
1613 .help = "Toeplitz hash function",
1614 .call = parse_vc_action_rss_func,
1616 [ACTION_RSS_FUNC_SIMPLE_XOR] = {
1617 .name = "simple_xor",
1618 .help = "simple XOR hash function",
1619 .call = parse_vc_action_rss_func,
1621 [ACTION_RSS_LEVEL] = {
1623 .help = "encapsulation level for \"types\"",
1624 .next = NEXT(action_rss, NEXT_ENTRY(UNSIGNED)),
1625 .args = ARGS(ARGS_ENTRY_ARB
1626 (offsetof(struct action_rss_data, conf) +
1627 offsetof(struct rte_flow_action_rss, level),
1628 sizeof(((struct rte_flow_action_rss *)0)->
1631 [ACTION_RSS_TYPES] = {
1633 .help = "specific RSS hash types",
1634 .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_TYPE)),
1636 [ACTION_RSS_TYPE] = {
1638 .help = "RSS hash type",
1639 .call = parse_vc_action_rss_type,
1640 .comp = comp_vc_action_rss_type,
1642 [ACTION_RSS_KEY] = {
1644 .help = "RSS hash key",
1645 .next = NEXT(action_rss, NEXT_ENTRY(STRING)),
1646 .args = ARGS(ARGS_ENTRY_ARB(0, 0),
1648 (offsetof(struct action_rss_data, conf) +
1649 offsetof(struct rte_flow_action_rss, key_len),
1650 sizeof(((struct rte_flow_action_rss *)0)->
1652 ARGS_ENTRY(struct action_rss_data, key)),
1654 [ACTION_RSS_KEY_LEN] = {
1656 .help = "RSS hash key length in bytes",
1657 .next = NEXT(action_rss, NEXT_ENTRY(UNSIGNED)),
1658 .args = ARGS(ARGS_ENTRY_ARB_BOUNDED
1659 (offsetof(struct action_rss_data, conf) +
1660 offsetof(struct rte_flow_action_rss, key_len),
1661 sizeof(((struct rte_flow_action_rss *)0)->
1664 RSS_HASH_KEY_LENGTH)),
1666 [ACTION_RSS_QUEUES] = {
1668 .help = "queue indices to use",
1669 .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_QUEUE)),
1670 .call = parse_vc_conf,
1672 [ACTION_RSS_QUEUE] = {
1674 .help = "queue index",
1675 .call = parse_vc_action_rss_queue,
1676 .comp = comp_vc_action_rss_queue,
1680 .help = "redirect packets to physical device function",
1681 .priv = PRIV_ACTION(PF, 0),
1682 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1687 .help = "redirect packets to virtual device function",
1688 .priv = PRIV_ACTION(VF, sizeof(struct rte_flow_action_vf)),
1689 .next = NEXT(action_vf),
1692 [ACTION_VF_ORIGINAL] = {
1694 .help = "use original VF ID if possible",
1695 .next = NEXT(action_vf, NEXT_ENTRY(BOOLEAN)),
1696 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_vf,
1698 .call = parse_vc_conf,
1702 .help = "VF ID to redirect packets to",
1703 .next = NEXT(action_vf, NEXT_ENTRY(UNSIGNED)),
1704 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_vf, id)),
1705 .call = parse_vc_conf,
1709 .help = "meter the directed packets at given id",
1710 .priv = PRIV_ACTION(METER,
1711 sizeof(struct rte_flow_action_meter)),
1712 .next = NEXT(action_meter),
1715 [ACTION_METER_ID] = {
1717 .help = "meter id to use",
1718 .next = NEXT(action_meter, NEXT_ENTRY(UNSIGNED)),
1719 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_meter, mtr_id)),
1720 .call = parse_vc_conf,
1724 /** Remove and return last entry from argument stack. */
1725 static const struct arg *
1726 pop_args(struct context *ctx)
1728 return ctx->args_num ? ctx->args[--ctx->args_num] : NULL;
1731 /** Add entry on top of the argument stack. */
1733 push_args(struct context *ctx, const struct arg *arg)
1735 if (ctx->args_num == CTX_STACK_SIZE)
1737 ctx->args[ctx->args_num++] = arg;
1741 /** Spread value into buffer according to bit-mask. */
1743 arg_entry_bf_fill(void *dst, uintmax_t val, const struct arg *arg)
1745 uint32_t i = arg->size;
1753 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1762 unsigned int shift = 0;
1763 uint8_t *buf = (uint8_t *)dst + arg->offset + (i -= sub);
1765 for (shift = 0; arg->mask[i] >> shift; ++shift) {
1766 if (!(arg->mask[i] & (1 << shift)))
1771 *buf &= ~(1 << shift);
1772 *buf |= (val & 1) << shift;
1780 /** Compare a string with a partial one of a given length. */
1782 strcmp_partial(const char *full, const char *partial, size_t partial_len)
1784 int r = strncmp(full, partial, partial_len);
1788 if (strlen(full) <= partial_len)
1790 return full[partial_len];
1794 * Parse a prefix length and generate a bit-mask.
1796 * Last argument (ctx->args) is retrieved to determine mask size, storage
1797 * location and whether the result must use network byte ordering.
1800 parse_prefix(struct context *ctx, const struct token *token,
1801 const char *str, unsigned int len,
1802 void *buf, unsigned int size)
1804 const struct arg *arg = pop_args(ctx);
1805 static const uint8_t conv[] = "\x00\x80\xc0\xe0\xf0\xf8\xfc\xfe\xff";
1812 /* Argument is expected. */
1816 u = strtoumax(str, &end, 0);
1817 if (errno || (size_t)(end - str) != len)
1822 extra = arg_entry_bf_fill(NULL, 0, arg);
1831 if (!arg_entry_bf_fill(ctx->object, v, arg) ||
1832 !arg_entry_bf_fill(ctx->objmask, -1, arg))
1839 if (bytes > size || bytes + !!extra > size)
1843 buf = (uint8_t *)ctx->object + arg->offset;
1844 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1846 memset((uint8_t *)buf + size - bytes, 0xff, bytes);
1847 memset(buf, 0x00, size - bytes);
1849 ((uint8_t *)buf)[size - bytes - 1] = conv[extra];
1853 memset(buf, 0xff, bytes);
1854 memset((uint8_t *)buf + bytes, 0x00, size - bytes);
1856 ((uint8_t *)buf)[bytes] = conv[extra];
1859 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
1862 push_args(ctx, arg);
1866 /** Default parsing function for token name matching. */
1868 parse_default(struct context *ctx, const struct token *token,
1869 const char *str, unsigned int len,
1870 void *buf, unsigned int size)
1875 if (strcmp_partial(token->name, str, len))
1880 /** Parse flow command, initialize output buffer for subsequent tokens. */
1882 parse_init(struct context *ctx, const struct token *token,
1883 const char *str, unsigned int len,
1884 void *buf, unsigned int size)
1886 struct buffer *out = buf;
1888 /* Token name must match. */
1889 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1891 /* Nothing else to do if there is no buffer. */
1894 /* Make sure buffer is large enough. */
1895 if (size < sizeof(*out))
1897 /* Initialize buffer. */
1898 memset(out, 0x00, sizeof(*out));
1899 memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
1902 ctx->objmask = NULL;
1906 /** Parse tokens for validate/create commands. */
1908 parse_vc(struct context *ctx, const struct token *token,
1909 const char *str, unsigned int len,
1910 void *buf, unsigned int size)
1912 struct buffer *out = buf;
1916 /* Token name must match. */
1917 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1919 /* Nothing else to do if there is no buffer. */
1922 if (!out->command) {
1923 if (ctx->curr != VALIDATE && ctx->curr != CREATE)
1925 if (sizeof(*out) > size)
1927 out->command = ctx->curr;
1930 ctx->objmask = NULL;
1931 out->args.vc.data = (uint8_t *)out + size;
1935 ctx->object = &out->args.vc.attr;
1936 ctx->objmask = NULL;
1937 switch (ctx->curr) {
1942 out->args.vc.attr.ingress = 1;
1945 out->args.vc.attr.egress = 1;
1948 out->args.vc.pattern =
1949 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
1951 ctx->object = out->args.vc.pattern;
1952 ctx->objmask = NULL;
1955 out->args.vc.actions =
1956 (void *)RTE_ALIGN_CEIL((uintptr_t)
1957 (out->args.vc.pattern +
1958 out->args.vc.pattern_n),
1960 ctx->object = out->args.vc.actions;
1961 ctx->objmask = NULL;
1968 if (!out->args.vc.actions) {
1969 const struct parse_item_priv *priv = token->priv;
1970 struct rte_flow_item *item =
1971 out->args.vc.pattern + out->args.vc.pattern_n;
1973 data_size = priv->size * 3; /* spec, last, mask */
1974 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
1975 (out->args.vc.data - data_size),
1977 if ((uint8_t *)item + sizeof(*item) > data)
1979 *item = (struct rte_flow_item){
1982 ++out->args.vc.pattern_n;
1984 ctx->objmask = NULL;
1986 const struct parse_action_priv *priv = token->priv;
1987 struct rte_flow_action *action =
1988 out->args.vc.actions + out->args.vc.actions_n;
1990 data_size = priv->size; /* configuration */
1991 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
1992 (out->args.vc.data - data_size),
1994 if ((uint8_t *)action + sizeof(*action) > data)
1996 *action = (struct rte_flow_action){
1998 .conf = data_size ? data : NULL,
2000 ++out->args.vc.actions_n;
2001 ctx->object = action;
2002 ctx->objmask = NULL;
2004 memset(data, 0, data_size);
2005 out->args.vc.data = data;
2006 ctx->objdata = data_size;
2010 /** Parse pattern item parameter type. */
2012 parse_vc_spec(struct context *ctx, const struct token *token,
2013 const char *str, unsigned int len,
2014 void *buf, unsigned int size)
2016 struct buffer *out = buf;
2017 struct rte_flow_item *item;
2023 /* Token name must match. */
2024 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2026 /* Parse parameter types. */
2027 switch (ctx->curr) {
2028 static const enum index prefix[] = NEXT_ENTRY(PREFIX);
2034 case ITEM_PARAM_SPEC:
2037 case ITEM_PARAM_LAST:
2040 case ITEM_PARAM_PREFIX:
2041 /* Modify next token to expect a prefix. */
2042 if (ctx->next_num < 2)
2044 ctx->next[ctx->next_num - 2] = prefix;
2046 case ITEM_PARAM_MASK:
2052 /* Nothing else to do if there is no buffer. */
2055 if (!out->args.vc.pattern_n)
2057 item = &out->args.vc.pattern[out->args.vc.pattern_n - 1];
2058 data_size = ctx->objdata / 3; /* spec, last, mask */
2059 /* Point to selected object. */
2060 ctx->object = out->args.vc.data + (data_size * index);
2062 ctx->objmask = out->args.vc.data + (data_size * 2); /* mask */
2063 item->mask = ctx->objmask;
2065 ctx->objmask = NULL;
2066 /* Update relevant item pointer. */
2067 *((const void **[]){ &item->spec, &item->last, &item->mask })[index] =
2072 /** Parse action configuration field. */
2074 parse_vc_conf(struct context *ctx, const struct token *token,
2075 const char *str, unsigned int len,
2076 void *buf, unsigned int size)
2078 struct buffer *out = buf;
2081 /* Token name must match. */
2082 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2084 /* Nothing else to do if there is no buffer. */
2087 /* Point to selected object. */
2088 ctx->object = out->args.vc.data;
2089 ctx->objmask = NULL;
2093 /** Parse RSS action. */
2095 parse_vc_action_rss(struct context *ctx, const struct token *token,
2096 const char *str, unsigned int len,
2097 void *buf, unsigned int size)
2099 struct buffer *out = buf;
2100 struct rte_flow_action *action;
2101 struct action_rss_data *action_rss_data;
2105 ret = parse_vc(ctx, token, str, len, buf, size);
2108 /* Nothing else to do if there is no buffer. */
2111 if (!out->args.vc.actions_n)
2113 action = &out->args.vc.actions[out->args.vc.actions_n - 1];
2114 /* Point to selected object. */
2115 ctx->object = out->args.vc.data;
2116 ctx->objmask = NULL;
2117 /* Set up default configuration. */
2118 action_rss_data = ctx->object;
2119 *action_rss_data = (struct action_rss_data){
2120 .conf = (struct rte_flow_action_rss){
2121 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
2124 .key_len = sizeof(action_rss_data->key),
2125 .queue_num = RTE_MIN(nb_rxq, ACTION_RSS_QUEUE_NUM),
2126 .key = action_rss_data->key,
2127 .queue = action_rss_data->queue,
2129 .key = "testpmd's default RSS hash key",
2132 for (i = 0; i < action_rss_data->conf.queue_num; ++i)
2133 action_rss_data->queue[i] = i;
2134 if (!port_id_is_invalid(ctx->port, DISABLED_WARN) &&
2135 ctx->port != (portid_t)RTE_PORT_ALL) {
2136 struct rte_eth_dev_info info;
2138 rte_eth_dev_info_get(ctx->port, &info);
2139 action_rss_data->conf.key_len =
2140 RTE_MIN(sizeof(action_rss_data->key),
2141 info.hash_key_size);
2143 action->conf = &action_rss_data->conf;
2148 * Parse func field for RSS action.
2150 * The RTE_ETH_HASH_FUNCTION_* value to assign is derived from the
2151 * ACTION_RSS_FUNC_* index that called this function.
2154 parse_vc_action_rss_func(struct context *ctx, const struct token *token,
2155 const char *str, unsigned int len,
2156 void *buf, unsigned int size)
2158 struct action_rss_data *action_rss_data;
2159 enum rte_eth_hash_function func;
2163 /* Token name must match. */
2164 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2166 switch (ctx->curr) {
2167 case ACTION_RSS_FUNC_DEFAULT:
2168 func = RTE_ETH_HASH_FUNCTION_DEFAULT;
2170 case ACTION_RSS_FUNC_TOEPLITZ:
2171 func = RTE_ETH_HASH_FUNCTION_TOEPLITZ;
2173 case ACTION_RSS_FUNC_SIMPLE_XOR:
2174 func = RTE_ETH_HASH_FUNCTION_SIMPLE_XOR;
2181 action_rss_data = ctx->object;
2182 action_rss_data->conf.func = func;
2187 * Parse type field for RSS action.
2189 * Valid tokens are type field names and the "end" token.
2192 parse_vc_action_rss_type(struct context *ctx, const struct token *token,
2193 const char *str, unsigned int len,
2194 void *buf, unsigned int size)
2196 static const enum index next[] = NEXT_ENTRY(ACTION_RSS_TYPE);
2197 struct action_rss_data *action_rss_data;
2203 if (ctx->curr != ACTION_RSS_TYPE)
2205 if (!(ctx->objdata >> 16) && ctx->object) {
2206 action_rss_data = ctx->object;
2207 action_rss_data->conf.types = 0;
2209 if (!strcmp_partial("end", str, len)) {
2210 ctx->objdata &= 0xffff;
2213 for (i = 0; rss_type_table[i].str; ++i)
2214 if (!strcmp_partial(rss_type_table[i].str, str, len))
2216 if (!rss_type_table[i].str)
2218 ctx->objdata = 1 << 16 | (ctx->objdata & 0xffff);
2220 if (ctx->next_num == RTE_DIM(ctx->next))
2222 ctx->next[ctx->next_num++] = next;
2225 action_rss_data = ctx->object;
2226 action_rss_data->conf.types |= rss_type_table[i].rss_type;
2231 * Parse queue field for RSS action.
2233 * Valid tokens are queue indices and the "end" token.
2236 parse_vc_action_rss_queue(struct context *ctx, const struct token *token,
2237 const char *str, unsigned int len,
2238 void *buf, unsigned int size)
2240 static const enum index next[] = NEXT_ENTRY(ACTION_RSS_QUEUE);
2241 struct action_rss_data *action_rss_data;
2248 if (ctx->curr != ACTION_RSS_QUEUE)
2250 i = ctx->objdata >> 16;
2251 if (!strcmp_partial("end", str, len)) {
2252 ctx->objdata &= 0xffff;
2255 if (i >= ACTION_RSS_QUEUE_NUM)
2258 ARGS_ENTRY_ARB(offsetof(struct action_rss_data, queue) +
2259 i * sizeof(action_rss_data->queue[i]),
2260 sizeof(action_rss_data->queue[i]))))
2262 ret = parse_int(ctx, token, str, len, NULL, 0);
2268 ctx->objdata = i << 16 | (ctx->objdata & 0xffff);
2270 if (ctx->next_num == RTE_DIM(ctx->next))
2272 ctx->next[ctx->next_num++] = next;
2275 action_rss_data = ctx->object;
2276 action_rss_data->conf.queue_num = i;
2277 action_rss_data->conf.queue = i ? action_rss_data->queue : NULL;
2281 /** Parse tokens for destroy command. */
2283 parse_destroy(struct context *ctx, const struct token *token,
2284 const char *str, unsigned int len,
2285 void *buf, unsigned int size)
2287 struct buffer *out = buf;
2289 /* Token name must match. */
2290 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2292 /* Nothing else to do if there is no buffer. */
2295 if (!out->command) {
2296 if (ctx->curr != DESTROY)
2298 if (sizeof(*out) > size)
2300 out->command = ctx->curr;
2303 ctx->objmask = NULL;
2304 out->args.destroy.rule =
2305 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
2309 if (((uint8_t *)(out->args.destroy.rule + out->args.destroy.rule_n) +
2310 sizeof(*out->args.destroy.rule)) > (uint8_t *)out + size)
2313 ctx->object = out->args.destroy.rule + out->args.destroy.rule_n++;
2314 ctx->objmask = NULL;
2318 /** Parse tokens for flush command. */
2320 parse_flush(struct context *ctx, const struct token *token,
2321 const char *str, unsigned int len,
2322 void *buf, unsigned int size)
2324 struct buffer *out = buf;
2326 /* Token name must match. */
2327 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2329 /* Nothing else to do if there is no buffer. */
2332 if (!out->command) {
2333 if (ctx->curr != FLUSH)
2335 if (sizeof(*out) > size)
2337 out->command = ctx->curr;
2340 ctx->objmask = NULL;
2345 /** Parse tokens for query command. */
2347 parse_query(struct context *ctx, const struct token *token,
2348 const char *str, unsigned int len,
2349 void *buf, unsigned int size)
2351 struct buffer *out = buf;
2353 /* Token name must match. */
2354 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2356 /* Nothing else to do if there is no buffer. */
2359 if (!out->command) {
2360 if (ctx->curr != QUERY)
2362 if (sizeof(*out) > size)
2364 out->command = ctx->curr;
2367 ctx->objmask = NULL;
2372 /** Parse action names. */
2374 parse_action(struct context *ctx, const struct token *token,
2375 const char *str, unsigned int len,
2376 void *buf, unsigned int size)
2378 struct buffer *out = buf;
2379 const struct arg *arg = pop_args(ctx);
2383 /* Argument is expected. */
2386 /* Parse action name. */
2387 for (i = 0; next_action[i]; ++i) {
2388 const struct parse_action_priv *priv;
2390 token = &token_list[next_action[i]];
2391 if (strcmp_partial(token->name, str, len))
2397 memcpy((uint8_t *)ctx->object + arg->offset,
2403 push_args(ctx, arg);
2407 /** Parse tokens for list command. */
2409 parse_list(struct context *ctx, const struct token *token,
2410 const char *str, unsigned int len,
2411 void *buf, unsigned int size)
2413 struct buffer *out = buf;
2415 /* Token name must match. */
2416 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2418 /* Nothing else to do if there is no buffer. */
2421 if (!out->command) {
2422 if (ctx->curr != LIST)
2424 if (sizeof(*out) > size)
2426 out->command = ctx->curr;
2429 ctx->objmask = NULL;
2430 out->args.list.group =
2431 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
2435 if (((uint8_t *)(out->args.list.group + out->args.list.group_n) +
2436 sizeof(*out->args.list.group)) > (uint8_t *)out + size)
2439 ctx->object = out->args.list.group + out->args.list.group_n++;
2440 ctx->objmask = NULL;
2444 /** Parse tokens for isolate command. */
2446 parse_isolate(struct context *ctx, const struct token *token,
2447 const char *str, unsigned int len,
2448 void *buf, unsigned int size)
2450 struct buffer *out = buf;
2452 /* Token name must match. */
2453 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2455 /* Nothing else to do if there is no buffer. */
2458 if (!out->command) {
2459 if (ctx->curr != ISOLATE)
2461 if (sizeof(*out) > size)
2463 out->command = ctx->curr;
2466 ctx->objmask = NULL;
2472 * Parse signed/unsigned integers 8 to 64-bit long.
2474 * Last argument (ctx->args) is retrieved to determine integer type and
2478 parse_int(struct context *ctx, const struct token *token,
2479 const char *str, unsigned int len,
2480 void *buf, unsigned int size)
2482 const struct arg *arg = pop_args(ctx);
2487 /* Argument is expected. */
2492 (uintmax_t)strtoimax(str, &end, 0) :
2493 strtoumax(str, &end, 0);
2494 if (errno || (size_t)(end - str) != len)
2497 ((arg->sign && ((intmax_t)u < (intmax_t)arg->min ||
2498 (intmax_t)u > (intmax_t)arg->max)) ||
2499 (!arg->sign && (u < arg->min || u > arg->max))))
2504 if (!arg_entry_bf_fill(ctx->object, u, arg) ||
2505 !arg_entry_bf_fill(ctx->objmask, -1, arg))
2509 buf = (uint8_t *)ctx->object + arg->offset;
2513 case sizeof(uint8_t):
2514 *(uint8_t *)buf = u;
2516 case sizeof(uint16_t):
2517 *(uint16_t *)buf = arg->hton ? rte_cpu_to_be_16(u) : u;
2519 case sizeof(uint8_t [3]):
2520 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
2522 ((uint8_t *)buf)[0] = u;
2523 ((uint8_t *)buf)[1] = u >> 8;
2524 ((uint8_t *)buf)[2] = u >> 16;
2528 ((uint8_t *)buf)[0] = u >> 16;
2529 ((uint8_t *)buf)[1] = u >> 8;
2530 ((uint8_t *)buf)[2] = u;
2532 case sizeof(uint32_t):
2533 *(uint32_t *)buf = arg->hton ? rte_cpu_to_be_32(u) : u;
2535 case sizeof(uint64_t):
2536 *(uint64_t *)buf = arg->hton ? rte_cpu_to_be_64(u) : u;
2541 if (ctx->objmask && buf != (uint8_t *)ctx->objmask + arg->offset) {
2543 buf = (uint8_t *)ctx->objmask + arg->offset;
2548 push_args(ctx, arg);
2555 * Three arguments (ctx->args) are retrieved from the stack to store data,
2556 * its actual length and address (in that order).
2559 parse_string(struct context *ctx, const struct token *token,
2560 const char *str, unsigned int len,
2561 void *buf, unsigned int size)
2563 const struct arg *arg_data = pop_args(ctx);
2564 const struct arg *arg_len = pop_args(ctx);
2565 const struct arg *arg_addr = pop_args(ctx);
2566 char tmp[16]; /* Ought to be enough. */
2569 /* Arguments are expected. */
2573 push_args(ctx, arg_data);
2577 push_args(ctx, arg_len);
2578 push_args(ctx, arg_data);
2581 size = arg_data->size;
2582 /* Bit-mask fill is not supported. */
2583 if (arg_data->mask || size < len)
2587 /* Let parse_int() fill length information first. */
2588 ret = snprintf(tmp, sizeof(tmp), "%u", len);
2591 push_args(ctx, arg_len);
2592 ret = parse_int(ctx, token, tmp, ret, NULL, 0);
2597 buf = (uint8_t *)ctx->object + arg_data->offset;
2598 /* Output buffer is not necessarily NUL-terminated. */
2599 memcpy(buf, str, len);
2600 memset((uint8_t *)buf + len, 0x00, size - len);
2602 memset((uint8_t *)ctx->objmask + arg_data->offset, 0xff, len);
2603 /* Save address if requested. */
2604 if (arg_addr->size) {
2605 memcpy((uint8_t *)ctx->object + arg_addr->offset,
2607 (uint8_t *)ctx->object + arg_data->offset
2611 memcpy((uint8_t *)ctx->objmask + arg_addr->offset,
2613 (uint8_t *)ctx->objmask + arg_data->offset
2619 push_args(ctx, arg_addr);
2620 push_args(ctx, arg_len);
2621 push_args(ctx, arg_data);
2626 * Parse a MAC address.
2628 * Last argument (ctx->args) is retrieved to determine storage size and
2632 parse_mac_addr(struct context *ctx, const struct token *token,
2633 const char *str, unsigned int len,
2634 void *buf, unsigned int size)
2636 const struct arg *arg = pop_args(ctx);
2637 struct ether_addr tmp;
2641 /* Argument is expected. */
2645 /* Bit-mask fill is not supported. */
2646 if (arg->mask || size != sizeof(tmp))
2648 /* Only network endian is supported. */
2651 ret = cmdline_parse_etheraddr(NULL, str, &tmp, size);
2652 if (ret < 0 || (unsigned int)ret != len)
2656 buf = (uint8_t *)ctx->object + arg->offset;
2657 memcpy(buf, &tmp, size);
2659 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2662 push_args(ctx, arg);
2667 * Parse an IPv4 address.
2669 * Last argument (ctx->args) is retrieved to determine storage size and
2673 parse_ipv4_addr(struct context *ctx, const struct token *token,
2674 const char *str, unsigned int len,
2675 void *buf, unsigned int size)
2677 const struct arg *arg = pop_args(ctx);
2682 /* Argument is expected. */
2686 /* Bit-mask fill is not supported. */
2687 if (arg->mask || size != sizeof(tmp))
2689 /* Only network endian is supported. */
2692 memcpy(str2, str, len);
2694 ret = inet_pton(AF_INET, str2, &tmp);
2696 /* Attempt integer parsing. */
2697 push_args(ctx, arg);
2698 return parse_int(ctx, token, str, len, buf, size);
2702 buf = (uint8_t *)ctx->object + arg->offset;
2703 memcpy(buf, &tmp, size);
2705 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2708 push_args(ctx, arg);
2713 * Parse an IPv6 address.
2715 * Last argument (ctx->args) is retrieved to determine storage size and
2719 parse_ipv6_addr(struct context *ctx, const struct token *token,
2720 const char *str, unsigned int len,
2721 void *buf, unsigned int size)
2723 const struct arg *arg = pop_args(ctx);
2725 struct in6_addr tmp;
2729 /* Argument is expected. */
2733 /* Bit-mask fill is not supported. */
2734 if (arg->mask || size != sizeof(tmp))
2736 /* Only network endian is supported. */
2739 memcpy(str2, str, len);
2741 ret = inet_pton(AF_INET6, str2, &tmp);
2746 buf = (uint8_t *)ctx->object + arg->offset;
2747 memcpy(buf, &tmp, size);
2749 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2752 push_args(ctx, arg);
2756 /** Boolean values (even indices stand for false). */
2757 static const char *const boolean_name[] = {
2767 * Parse a boolean value.
2769 * Last argument (ctx->args) is retrieved to determine storage size and
2773 parse_boolean(struct context *ctx, const struct token *token,
2774 const char *str, unsigned int len,
2775 void *buf, unsigned int size)
2777 const struct arg *arg = pop_args(ctx);
2781 /* Argument is expected. */
2784 for (i = 0; boolean_name[i]; ++i)
2785 if (!strcmp_partial(boolean_name[i], str, len))
2787 /* Process token as integer. */
2788 if (boolean_name[i])
2789 str = i & 1 ? "1" : "0";
2790 push_args(ctx, arg);
2791 ret = parse_int(ctx, token, str, strlen(str), buf, size);
2792 return ret > 0 ? (int)len : ret;
2795 /** Parse port and update context. */
2797 parse_port(struct context *ctx, const struct token *token,
2798 const char *str, unsigned int len,
2799 void *buf, unsigned int size)
2801 struct buffer *out = &(struct buffer){ .port = 0 };
2809 ctx->objmask = NULL;
2810 size = sizeof(*out);
2812 ret = parse_int(ctx, token, str, len, out, size);
2814 ctx->port = out->port;
2820 /** No completion. */
2822 comp_none(struct context *ctx, const struct token *token,
2823 unsigned int ent, char *buf, unsigned int size)
2833 /** Complete boolean values. */
2835 comp_boolean(struct context *ctx, const struct token *token,
2836 unsigned int ent, char *buf, unsigned int size)
2842 for (i = 0; boolean_name[i]; ++i)
2843 if (buf && i == ent)
2844 return snprintf(buf, size, "%s", boolean_name[i]);
2850 /** Complete action names. */
2852 comp_action(struct context *ctx, const struct token *token,
2853 unsigned int ent, char *buf, unsigned int size)
2859 for (i = 0; next_action[i]; ++i)
2860 if (buf && i == ent)
2861 return snprintf(buf, size, "%s",
2862 token_list[next_action[i]].name);
2868 /** Complete available ports. */
2870 comp_port(struct context *ctx, const struct token *token,
2871 unsigned int ent, char *buf, unsigned int size)
2878 RTE_ETH_FOREACH_DEV(p) {
2879 if (buf && i == ent)
2880 return snprintf(buf, size, "%u", p);
2888 /** Complete available rule IDs. */
2890 comp_rule_id(struct context *ctx, const struct token *token,
2891 unsigned int ent, char *buf, unsigned int size)
2894 struct rte_port *port;
2895 struct port_flow *pf;
2898 if (port_id_is_invalid(ctx->port, DISABLED_WARN) ||
2899 ctx->port == (portid_t)RTE_PORT_ALL)
2901 port = &ports[ctx->port];
2902 for (pf = port->flow_list; pf != NULL; pf = pf->next) {
2903 if (buf && i == ent)
2904 return snprintf(buf, size, "%u", pf->id);
2912 /** Complete type field for RSS action. */
2914 comp_vc_action_rss_type(struct context *ctx, const struct token *token,
2915 unsigned int ent, char *buf, unsigned int size)
2921 for (i = 0; rss_type_table[i].str; ++i)
2926 return snprintf(buf, size, "%s", rss_type_table[ent].str);
2928 return snprintf(buf, size, "end");
2932 /** Complete queue field for RSS action. */
2934 comp_vc_action_rss_queue(struct context *ctx, const struct token *token,
2935 unsigned int ent, char *buf, unsigned int size)
2942 return snprintf(buf, size, "%u", ent);
2944 return snprintf(buf, size, "end");
2948 /** Internal context. */
2949 static struct context cmd_flow_context;
2951 /** Global parser instance (cmdline API). */
2952 cmdline_parse_inst_t cmd_flow;
2954 /** Initialize context. */
2956 cmd_flow_context_init(struct context *ctx)
2958 /* A full memset() is not necessary. */
2968 ctx->objmask = NULL;
2971 /** Parse a token (cmdline API). */
2973 cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result,
2976 struct context *ctx = &cmd_flow_context;
2977 const struct token *token;
2978 const enum index *list;
2983 token = &token_list[ctx->curr];
2984 /* Check argument length. */
2987 for (len = 0; src[len]; ++len)
2988 if (src[len] == '#' || isspace(src[len]))
2992 /* Last argument and EOL detection. */
2993 for (i = len; src[i]; ++i)
2994 if (src[i] == '#' || src[i] == '\r' || src[i] == '\n')
2996 else if (!isspace(src[i])) {
3001 if (src[i] == '\r' || src[i] == '\n') {
3005 /* Initialize context if necessary. */
3006 if (!ctx->next_num) {
3009 ctx->next[ctx->next_num++] = token->next[0];
3011 /* Process argument through candidates. */
3012 ctx->prev = ctx->curr;
3013 list = ctx->next[ctx->next_num - 1];
3014 for (i = 0; list[i]; ++i) {
3015 const struct token *next = &token_list[list[i]];
3018 ctx->curr = list[i];
3020 tmp = next->call(ctx, next, src, len, result, size);
3022 tmp = parse_default(ctx, next, src, len, result, size);
3023 if (tmp == -1 || tmp != len)
3031 /* Push subsequent tokens if any. */
3033 for (i = 0; token->next[i]; ++i) {
3034 if (ctx->next_num == RTE_DIM(ctx->next))
3036 ctx->next[ctx->next_num++] = token->next[i];
3038 /* Push arguments if any. */
3040 for (i = 0; token->args[i]; ++i) {
3041 if (ctx->args_num == RTE_DIM(ctx->args))
3043 ctx->args[ctx->args_num++] = token->args[i];
3048 /** Return number of completion entries (cmdline API). */
3050 cmd_flow_complete_get_nb(cmdline_parse_token_hdr_t *hdr)
3052 struct context *ctx = &cmd_flow_context;
3053 const struct token *token = &token_list[ctx->curr];
3054 const enum index *list;
3058 /* Count number of tokens in current list. */
3060 list = ctx->next[ctx->next_num - 1];
3062 list = token->next[0];
3063 for (i = 0; list[i]; ++i)
3068 * If there is a single token, use its completion callback, otherwise
3069 * return the number of entries.
3071 token = &token_list[list[0]];
3072 if (i == 1 && token->comp) {
3073 /* Save index for cmd_flow_get_help(). */
3074 ctx->prev = list[0];
3075 return token->comp(ctx, token, 0, NULL, 0);
3080 /** Return a completion entry (cmdline API). */
3082 cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,
3083 char *dst, unsigned int size)
3085 struct context *ctx = &cmd_flow_context;
3086 const struct token *token = &token_list[ctx->curr];
3087 const enum index *list;
3091 /* Count number of tokens in current list. */
3093 list = ctx->next[ctx->next_num - 1];
3095 list = token->next[0];
3096 for (i = 0; list[i]; ++i)
3100 /* If there is a single token, use its completion callback. */
3101 token = &token_list[list[0]];
3102 if (i == 1 && token->comp) {
3103 /* Save index for cmd_flow_get_help(). */
3104 ctx->prev = list[0];
3105 return token->comp(ctx, token, index, dst, size) < 0 ? -1 : 0;
3107 /* Otherwise make sure the index is valid and use defaults. */
3110 token = &token_list[list[index]];
3111 snprintf(dst, size, "%s", token->name);
3112 /* Save index for cmd_flow_get_help(). */
3113 ctx->prev = list[index];
3117 /** Populate help strings for current token (cmdline API). */
3119 cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
3121 struct context *ctx = &cmd_flow_context;
3122 const struct token *token = &token_list[ctx->prev];
3127 /* Set token type and update global help with details. */
3128 snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
3130 cmd_flow.help_str = token->help;
3132 cmd_flow.help_str = token->name;
3136 /** Token definition template (cmdline API). */
3137 static struct cmdline_token_hdr cmd_flow_token_hdr = {
3138 .ops = &(struct cmdline_token_ops){
3139 .parse = cmd_flow_parse,
3140 .complete_get_nb = cmd_flow_complete_get_nb,
3141 .complete_get_elt = cmd_flow_complete_get_elt,
3142 .get_help = cmd_flow_get_help,
3147 /** Populate the next dynamic token. */
3149 cmd_flow_tok(cmdline_parse_token_hdr_t **hdr,
3150 cmdline_parse_token_hdr_t **hdr_inst)
3152 struct context *ctx = &cmd_flow_context;
3154 /* Always reinitialize context before requesting the first token. */
3155 if (!(hdr_inst - cmd_flow.tokens))
3156 cmd_flow_context_init(ctx);
3157 /* Return NULL when no more tokens are expected. */
3158 if (!ctx->next_num && ctx->curr) {
3162 /* Determine if command should end here. */
3163 if (ctx->eol && ctx->last && ctx->next_num) {
3164 const enum index *list = ctx->next[ctx->next_num - 1];
3167 for (i = 0; list[i]; ++i) {
3174 *hdr = &cmd_flow_token_hdr;
3177 /** Dispatch parsed buffer to function calls. */
3179 cmd_flow_parsed(const struct buffer *in)
3181 switch (in->command) {
3183 port_flow_validate(in->port, &in->args.vc.attr,
3184 in->args.vc.pattern, in->args.vc.actions);
3187 port_flow_create(in->port, &in->args.vc.attr,
3188 in->args.vc.pattern, in->args.vc.actions);
3191 port_flow_destroy(in->port, in->args.destroy.rule_n,
3192 in->args.destroy.rule);
3195 port_flow_flush(in->port);
3198 port_flow_query(in->port, in->args.query.rule,
3199 in->args.query.action);
3202 port_flow_list(in->port, in->args.list.group_n,
3203 in->args.list.group);
3206 port_flow_isolate(in->port, in->args.isolate.set);
3213 /** Token generator and output processing callback (cmdline API). */
3215 cmd_flow_cb(void *arg0, struct cmdline *cl, void *arg2)
3218 cmd_flow_tok(arg0, arg2);
3220 cmd_flow_parsed(arg0);
3223 /** Global parser instance (cmdline API). */
3224 cmdline_parse_inst_t cmd_flow = {
3226 .data = NULL, /**< Unused. */
3227 .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
3230 }, /**< Tokens are returned by cmd_flow_tok(). */