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. */
74 /* Validate/create pattern. */
107 ITEM_VLAN_INNER_TYPE,
139 ITEM_E_TAG_GRP_ECID_B,
156 /* Validate/create actions. */
172 ACTION_RSS_FUNC_DEFAULT,
173 ACTION_RSS_FUNC_TOEPLITZ,
174 ACTION_RSS_FUNC_SIMPLE_XOR,
189 /** Maximum size for pattern in struct rte_flow_item_raw. */
190 #define ITEM_RAW_PATTERN_SIZE 40
192 /** Storage size for struct rte_flow_item_raw including pattern. */
193 #define ITEM_RAW_SIZE \
194 (sizeof(struct rte_flow_item_raw) + ITEM_RAW_PATTERN_SIZE)
196 /** Maximum number of queue indices in struct rte_flow_action_rss. */
197 #define ACTION_RSS_QUEUE_NUM 32
199 /** Storage for struct rte_flow_action_rss including external data. */
200 struct action_rss_data {
201 struct rte_flow_action_rss conf;
202 uint8_t key[RSS_HASH_KEY_LENGTH];
203 uint16_t queue[ACTION_RSS_QUEUE_NUM];
206 /** Maximum number of subsequent tokens and arguments on the stack. */
207 #define CTX_STACK_SIZE 16
209 /** Parser context. */
211 /** Stack of subsequent token lists to process. */
212 const enum index *next[CTX_STACK_SIZE];
213 /** Arguments for stacked tokens. */
214 const void *args[CTX_STACK_SIZE];
215 enum index curr; /**< Current token index. */
216 enum index prev; /**< Index of the last token seen. */
217 int next_num; /**< Number of entries in next[]. */
218 int args_num; /**< Number of entries in args[]. */
219 uint32_t eol:1; /**< EOL has been detected. */
220 uint32_t last:1; /**< No more arguments. */
221 portid_t port; /**< Current port ID (for completions). */
222 uint32_t objdata; /**< Object-specific data. */
223 void *object; /**< Address of current object for relative offsets. */
224 void *objmask; /**< Object a full mask must be written to. */
227 /** Token argument. */
229 uint32_t hton:1; /**< Use network byte ordering. */
230 uint32_t sign:1; /**< Value is signed. */
231 uint32_t bounded:1; /**< Value is bounded. */
232 uintmax_t min; /**< Minimum value if bounded. */
233 uintmax_t max; /**< Maximum value if bounded. */
234 uint32_t offset; /**< Relative offset from ctx->object. */
235 uint32_t size; /**< Field size. */
236 const uint8_t *mask; /**< Bit-mask to use instead of offset/size. */
239 /** Parser token definition. */
241 /** Type displayed during completion (defaults to "TOKEN"). */
243 /** Help displayed during completion (defaults to token name). */
245 /** Private data used by parser functions. */
248 * Lists of subsequent tokens to push on the stack. Each call to the
249 * parser consumes the last entry of that stack.
251 const enum index *const *next;
252 /** Arguments stack for subsequent tokens that need them. */
253 const struct arg *const *args;
255 * Token-processing callback, returns -1 in case of error, the
256 * length of the matched string otherwise. If NULL, attempts to
257 * match the token name.
259 * If buf is not NULL, the result should be stored in it according
260 * to context. An error is returned if not large enough.
262 int (*call)(struct context *ctx, const struct token *token,
263 const char *str, unsigned int len,
264 void *buf, unsigned int size);
266 * Callback that provides possible values for this token, used for
267 * completion. Returns -1 in case of error, the number of possible
268 * values otherwise. If NULL, the token name is used.
270 * If buf is not NULL, entry index ent is written to buf and the
271 * full length of the entry is returned (same behavior as
274 int (*comp)(struct context *ctx, const struct token *token,
275 unsigned int ent, char *buf, unsigned int size);
276 /** Mandatory token name, no default value. */
280 /** Static initializer for the next field. */
281 #define NEXT(...) (const enum index *const []){ __VA_ARGS__, NULL, }
283 /** Static initializer for a NEXT() entry. */
284 #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, }
286 /** Static initializer for the args field. */
287 #define ARGS(...) (const struct arg *const []){ __VA_ARGS__, NULL, }
289 /** Static initializer for ARGS() to target a field. */
290 #define ARGS_ENTRY(s, f) \
291 (&(const struct arg){ \
292 .offset = offsetof(s, f), \
293 .size = sizeof(((s *)0)->f), \
296 /** Static initializer for ARGS() to target a bit-field. */
297 #define ARGS_ENTRY_BF(s, f, b) \
298 (&(const struct arg){ \
300 .mask = (const void *)&(const s){ .f = (1 << (b)) - 1 }, \
303 /** Static initializer for ARGS() to target an arbitrary bit-mask. */
304 #define ARGS_ENTRY_MASK(s, f, m) \
305 (&(const struct arg){ \
306 .offset = offsetof(s, f), \
307 .size = sizeof(((s *)0)->f), \
308 .mask = (const void *)(m), \
311 /** Same as ARGS_ENTRY_MASK() using network byte ordering for the value. */
312 #define ARGS_ENTRY_MASK_HTON(s, f, m) \
313 (&(const struct arg){ \
315 .offset = offsetof(s, f), \
316 .size = sizeof(((s *)0)->f), \
317 .mask = (const void *)(m), \
320 /** Static initializer for ARGS() to target a pointer. */
321 #define ARGS_ENTRY_PTR(s, f) \
322 (&(const struct arg){ \
323 .size = sizeof(*((s *)0)->f), \
326 /** Static initializer for ARGS() with arbitrary offset and size. */
327 #define ARGS_ENTRY_ARB(o, s) \
328 (&(const struct arg){ \
333 /** Same as ARGS_ENTRY_ARB() with bounded values. */
334 #define ARGS_ENTRY_ARB_BOUNDED(o, s, i, a) \
335 (&(const struct arg){ \
343 /** Same as ARGS_ENTRY() using network byte ordering. */
344 #define ARGS_ENTRY_HTON(s, f) \
345 (&(const struct arg){ \
347 .offset = offsetof(s, f), \
348 .size = sizeof(((s *)0)->f), \
351 /** Parser output buffer layout expected by cmd_flow_parsed(). */
353 enum index command; /**< Flow command. */
354 portid_t port; /**< Affected port ID. */
357 struct rte_flow_attr attr;
358 struct rte_flow_item *pattern;
359 struct rte_flow_action *actions;
363 } vc; /**< Validate/create arguments. */
367 } destroy; /**< Destroy arguments. */
370 enum rte_flow_action_type action;
371 } query; /**< Query arguments. */
375 } list; /**< List arguments. */
378 } isolate; /**< Isolated mode arguments. */
379 } args; /**< Command arguments. */
382 /** Private data for pattern items. */
383 struct parse_item_priv {
384 enum rte_flow_item_type type; /**< Item type. */
385 uint32_t size; /**< Size of item specification structure. */
388 #define PRIV_ITEM(t, s) \
389 (&(const struct parse_item_priv){ \
390 .type = RTE_FLOW_ITEM_TYPE_ ## t, \
394 /** Private data for actions. */
395 struct parse_action_priv {
396 enum rte_flow_action_type type; /**< Action type. */
397 uint32_t size; /**< Size of action configuration structure. */
400 #define PRIV_ACTION(t, s) \
401 (&(const struct parse_action_priv){ \
402 .type = RTE_FLOW_ACTION_TYPE_ ## t, \
406 static const enum index next_vc_attr[] = {
416 static const enum index next_destroy_attr[] = {
422 static const enum index next_list_attr[] = {
428 static const enum index item_param[] = {
437 static const enum index next_item[] = {
467 static const enum index item_fuzzy[] = {
473 static const enum index item_any[] = {
479 static const enum index item_vf[] = {
485 static const enum index item_port[] = {
491 static const enum index item_raw[] = {
501 static const enum index item_eth[] = {
509 static const enum index item_vlan[] = {
514 ITEM_VLAN_INNER_TYPE,
519 static const enum index item_ipv4[] = {
529 static const enum index item_ipv6[] = {
540 static const enum index item_icmp[] = {
547 static const enum index item_udp[] = {
554 static const enum index item_tcp[] = {
562 static const enum index item_sctp[] = {
571 static const enum index item_vxlan[] = {
577 static const enum index item_e_tag[] = {
578 ITEM_E_TAG_GRP_ECID_B,
583 static const enum index item_nvgre[] = {
589 static const enum index item_mpls[] = {
595 static const enum index item_gre[] = {
601 static const enum index item_gtp[] = {
607 static const enum index item_geneve[] = {
614 static const enum index next_action[] = {
630 static const enum index action_mark[] = {
636 static const enum index action_queue[] = {
642 static const enum index action_rss[] = {
653 static const enum index action_vf[] = {
660 static const enum index action_meter[] = {
666 static int parse_init(struct context *, const struct token *,
667 const char *, unsigned int,
668 void *, unsigned int);
669 static int parse_vc(struct context *, const struct token *,
670 const char *, unsigned int,
671 void *, unsigned int);
672 static int parse_vc_spec(struct context *, const struct token *,
673 const char *, unsigned int, void *, unsigned int);
674 static int parse_vc_conf(struct context *, const struct token *,
675 const char *, unsigned int, void *, unsigned int);
676 static int parse_vc_action_rss(struct context *, const struct token *,
677 const char *, unsigned int, void *,
679 static int parse_vc_action_rss_func(struct context *, const struct token *,
680 const char *, unsigned int, void *,
682 static int parse_vc_action_rss_type(struct context *, const struct token *,
683 const char *, unsigned int, void *,
685 static int parse_vc_action_rss_queue(struct context *, const struct token *,
686 const char *, unsigned int, void *,
688 static int parse_destroy(struct context *, const struct token *,
689 const char *, unsigned int,
690 void *, unsigned int);
691 static int parse_flush(struct context *, const struct token *,
692 const char *, unsigned int,
693 void *, unsigned int);
694 static int parse_query(struct context *, const struct token *,
695 const char *, unsigned int,
696 void *, unsigned int);
697 static int parse_action(struct context *, const struct token *,
698 const char *, unsigned int,
699 void *, unsigned int);
700 static int parse_list(struct context *, const struct token *,
701 const char *, unsigned int,
702 void *, unsigned int);
703 static int parse_isolate(struct context *, const struct token *,
704 const char *, unsigned int,
705 void *, unsigned int);
706 static int parse_int(struct context *, const struct token *,
707 const char *, unsigned int,
708 void *, unsigned int);
709 static int parse_prefix(struct context *, const struct token *,
710 const char *, unsigned int,
711 void *, unsigned int);
712 static int parse_boolean(struct context *, const struct token *,
713 const char *, unsigned int,
714 void *, unsigned int);
715 static int parse_string(struct context *, const struct token *,
716 const char *, unsigned int,
717 void *, unsigned int);
718 static int parse_mac_addr(struct context *, const struct token *,
719 const char *, unsigned int,
720 void *, unsigned int);
721 static int parse_ipv4_addr(struct context *, const struct token *,
722 const char *, unsigned int,
723 void *, unsigned int);
724 static int parse_ipv6_addr(struct context *, const struct token *,
725 const char *, unsigned int,
726 void *, unsigned int);
727 static int parse_port(struct context *, const struct token *,
728 const char *, unsigned int,
729 void *, unsigned int);
730 static int comp_none(struct context *, const struct token *,
731 unsigned int, char *, unsigned int);
732 static int comp_boolean(struct context *, const struct token *,
733 unsigned int, char *, unsigned int);
734 static int comp_action(struct context *, const struct token *,
735 unsigned int, char *, unsigned int);
736 static int comp_port(struct context *, const struct token *,
737 unsigned int, char *, unsigned int);
738 static int comp_rule_id(struct context *, const struct token *,
739 unsigned int, char *, unsigned int);
740 static int comp_vc_action_rss_type(struct context *, const struct token *,
741 unsigned int, char *, unsigned int);
742 static int comp_vc_action_rss_queue(struct context *, const struct token *,
743 unsigned int, char *, unsigned int);
745 /** Token definitions. */
746 static const struct token token_list[] = {
747 /* Special tokens. */
750 .help = "null entry, abused as the entry point",
751 .next = NEXT(NEXT_ENTRY(FLOW)),
756 .help = "command may end here",
762 .help = "integer value",
767 .name = "{unsigned}",
769 .help = "unsigned integer value",
776 .help = "prefix length for bit-mask",
777 .call = parse_prefix,
783 .help = "any boolean value",
784 .call = parse_boolean,
785 .comp = comp_boolean,
790 .help = "fixed string",
791 .call = parse_string,
795 .name = "{MAC address}",
797 .help = "standard MAC address notation",
798 .call = parse_mac_addr,
802 .name = "{IPv4 address}",
803 .type = "IPV4 ADDRESS",
804 .help = "standard IPv4 address notation",
805 .call = parse_ipv4_addr,
809 .name = "{IPv6 address}",
810 .type = "IPV6 ADDRESS",
811 .help = "standard IPv6 address notation",
812 .call = parse_ipv6_addr,
818 .help = "rule identifier",
820 .comp = comp_rule_id,
825 .help = "port identifier",
830 .name = "{group_id}",
832 .help = "group identifier",
839 .help = "priority level",
843 /* Top-level command. */
846 .type = "{command} {port_id} [{arg} [...]]",
847 .help = "manage ingress/egress flow rules",
848 .next = NEXT(NEXT_ENTRY
858 /* Sub-level commands. */
861 .help = "check whether a flow rule can be created",
862 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
863 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
868 .help = "create a flow rule",
869 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
870 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
875 .help = "destroy specific flow rules",
876 .next = NEXT(NEXT_ENTRY(DESTROY_RULE), NEXT_ENTRY(PORT_ID)),
877 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
878 .call = parse_destroy,
882 .help = "destroy all flow rules",
883 .next = NEXT(NEXT_ENTRY(PORT_ID)),
884 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
889 .help = "query an existing flow rule",
890 .next = NEXT(NEXT_ENTRY(QUERY_ACTION),
892 NEXT_ENTRY(PORT_ID)),
893 .args = ARGS(ARGS_ENTRY(struct buffer, args.query.action),
894 ARGS_ENTRY(struct buffer, args.query.rule),
895 ARGS_ENTRY(struct buffer, port)),
900 .help = "list existing flow rules",
901 .next = NEXT(next_list_attr, NEXT_ENTRY(PORT_ID)),
902 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
907 .help = "restrict ingress traffic to the defined flow rules",
908 .next = NEXT(NEXT_ENTRY(BOOLEAN),
909 NEXT_ENTRY(PORT_ID)),
910 .args = ARGS(ARGS_ENTRY(struct buffer, args.isolate.set),
911 ARGS_ENTRY(struct buffer, port)),
912 .call = parse_isolate,
914 /* Destroy arguments. */
917 .help = "specify a rule identifier",
918 .next = NEXT(next_destroy_attr, NEXT_ENTRY(RULE_ID)),
919 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.destroy.rule)),
920 .call = parse_destroy,
922 /* Query arguments. */
926 .help = "action to query, must be part of the rule",
927 .call = parse_action,
930 /* List arguments. */
933 .help = "specify a group",
934 .next = NEXT(next_list_attr, NEXT_ENTRY(GROUP_ID)),
935 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.list.group)),
938 /* Validate/create attributes. */
941 .help = "specify a group",
942 .next = NEXT(next_vc_attr, NEXT_ENTRY(GROUP_ID)),
943 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, group)),
948 .help = "specify a priority level",
949 .next = NEXT(next_vc_attr, NEXT_ENTRY(PRIORITY_LEVEL)),
950 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, priority)),
955 .help = "affect rule to ingress",
956 .next = NEXT(next_vc_attr),
961 .help = "affect rule to egress",
962 .next = NEXT(next_vc_attr),
967 .help = "apply rule directly to endpoints found in pattern",
968 .next = NEXT(next_vc_attr),
971 /* Validate/create pattern. */
974 .help = "submit a list of pattern items",
975 .next = NEXT(next_item),
980 .help = "match value perfectly (with full bit-mask)",
981 .call = parse_vc_spec,
983 [ITEM_PARAM_SPEC] = {
985 .help = "match value according to configured bit-mask",
986 .call = parse_vc_spec,
988 [ITEM_PARAM_LAST] = {
990 .help = "specify upper bound to establish a range",
991 .call = parse_vc_spec,
993 [ITEM_PARAM_MASK] = {
995 .help = "specify bit-mask with relevant bits set to one",
996 .call = parse_vc_spec,
998 [ITEM_PARAM_PREFIX] = {
1000 .help = "generate bit-mask from a prefix length",
1001 .call = parse_vc_spec,
1005 .help = "specify next pattern item",
1006 .next = NEXT(next_item),
1010 .help = "end list of pattern items",
1011 .priv = PRIV_ITEM(END, 0),
1012 .next = NEXT(NEXT_ENTRY(ACTIONS)),
1017 .help = "no-op pattern item",
1018 .priv = PRIV_ITEM(VOID, 0),
1019 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1024 .help = "perform actions when pattern does not match",
1025 .priv = PRIV_ITEM(INVERT, 0),
1026 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1031 .help = "match any protocol for the current layer",
1032 .priv = PRIV_ITEM(ANY, sizeof(struct rte_flow_item_any)),
1033 .next = NEXT(item_any),
1038 .help = "number of layers covered",
1039 .next = NEXT(item_any, NEXT_ENTRY(UNSIGNED), item_param),
1040 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_any, num)),
1044 .help = "match packets addressed to the physical function",
1045 .priv = PRIV_ITEM(PF, 0),
1046 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1051 .help = "match packets addressed to a virtual function ID",
1052 .priv = PRIV_ITEM(VF, sizeof(struct rte_flow_item_vf)),
1053 .next = NEXT(item_vf),
1058 .help = "destination VF ID",
1059 .next = NEXT(item_vf, NEXT_ENTRY(UNSIGNED), item_param),
1060 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_vf, id)),
1064 .help = "device-specific physical port index to use",
1065 .priv = PRIV_ITEM(PORT, sizeof(struct rte_flow_item_port)),
1066 .next = NEXT(item_port),
1069 [ITEM_PORT_INDEX] = {
1071 .help = "physical port index",
1072 .next = NEXT(item_port, NEXT_ENTRY(UNSIGNED), item_param),
1073 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_port, index)),
1077 .help = "match an arbitrary byte string",
1078 .priv = PRIV_ITEM(RAW, ITEM_RAW_SIZE),
1079 .next = NEXT(item_raw),
1082 [ITEM_RAW_RELATIVE] = {
1084 .help = "look for pattern after the previous item",
1085 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1086 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1089 [ITEM_RAW_SEARCH] = {
1091 .help = "search pattern from offset (see also limit)",
1092 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1093 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1096 [ITEM_RAW_OFFSET] = {
1098 .help = "absolute or relative offset for pattern",
1099 .next = NEXT(item_raw, NEXT_ENTRY(INTEGER), item_param),
1100 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, offset)),
1102 [ITEM_RAW_LIMIT] = {
1104 .help = "search area limit for start of pattern",
1105 .next = NEXT(item_raw, NEXT_ENTRY(UNSIGNED), item_param),
1106 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, limit)),
1108 [ITEM_RAW_PATTERN] = {
1110 .help = "byte string to look for",
1111 .next = NEXT(item_raw,
1113 NEXT_ENTRY(ITEM_PARAM_IS,
1116 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, pattern),
1117 ARGS_ENTRY(struct rte_flow_item_raw, length),
1118 ARGS_ENTRY_ARB(sizeof(struct rte_flow_item_raw),
1119 ITEM_RAW_PATTERN_SIZE)),
1123 .help = "match Ethernet header",
1124 .priv = PRIV_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
1125 .next = NEXT(item_eth),
1130 .help = "destination MAC",
1131 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1132 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, dst)),
1136 .help = "source MAC",
1137 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1138 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, src)),
1142 .help = "EtherType",
1143 .next = NEXT(item_eth, NEXT_ENTRY(UNSIGNED), item_param),
1144 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, type)),
1148 .help = "match 802.1Q/ad VLAN tag",
1149 .priv = PRIV_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
1150 .next = NEXT(item_vlan),
1155 .help = "tag control information",
1156 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1157 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tci)),
1161 .help = "priority code point",
1162 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1163 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1168 .help = "drop eligible indicator",
1169 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1170 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1175 .help = "VLAN identifier",
1176 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1177 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1180 [ITEM_VLAN_INNER_TYPE] = {
1181 .name = "inner_type",
1182 .help = "inner EtherType",
1183 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1184 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan,
1189 .help = "match IPv4 header",
1190 .priv = PRIV_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
1191 .next = NEXT(item_ipv4),
1196 .help = "type of service",
1197 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1198 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1199 hdr.type_of_service)),
1203 .help = "time to live",
1204 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1205 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1208 [ITEM_IPV4_PROTO] = {
1210 .help = "next protocol ID",
1211 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1212 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1213 hdr.next_proto_id)),
1217 .help = "source address",
1218 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1219 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1224 .help = "destination address",
1225 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1226 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1231 .help = "match IPv6 header",
1232 .priv = PRIV_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
1233 .next = NEXT(item_ipv6),
1238 .help = "traffic class",
1239 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1240 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1242 "\x0f\xf0\x00\x00")),
1244 [ITEM_IPV6_FLOW] = {
1246 .help = "flow label",
1247 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1248 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1250 "\x00\x0f\xff\xff")),
1252 [ITEM_IPV6_PROTO] = {
1254 .help = "protocol (next header)",
1255 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1256 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1261 .help = "hop limit",
1262 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1263 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1268 .help = "source address",
1269 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1270 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1275 .help = "destination address",
1276 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1277 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1282 .help = "match ICMP header",
1283 .priv = PRIV_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
1284 .next = NEXT(item_icmp),
1287 [ITEM_ICMP_TYPE] = {
1289 .help = "ICMP packet type",
1290 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1291 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1294 [ITEM_ICMP_CODE] = {
1296 .help = "ICMP packet code",
1297 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1298 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1303 .help = "match UDP header",
1304 .priv = PRIV_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
1305 .next = NEXT(item_udp),
1310 .help = "UDP source port",
1311 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1312 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1317 .help = "UDP destination port",
1318 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1319 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1324 .help = "match TCP header",
1325 .priv = PRIV_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
1326 .next = NEXT(item_tcp),
1331 .help = "TCP source port",
1332 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1333 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1338 .help = "TCP destination port",
1339 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1340 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1343 [ITEM_TCP_FLAGS] = {
1345 .help = "TCP flags",
1346 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1347 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1352 .help = "match SCTP header",
1353 .priv = PRIV_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
1354 .next = NEXT(item_sctp),
1359 .help = "SCTP source port",
1360 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1361 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1366 .help = "SCTP destination port",
1367 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1368 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1373 .help = "validation tag",
1374 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1375 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1378 [ITEM_SCTP_CKSUM] = {
1381 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1382 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1387 .help = "match VXLAN header",
1388 .priv = PRIV_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
1389 .next = NEXT(item_vxlan),
1392 [ITEM_VXLAN_VNI] = {
1394 .help = "VXLAN identifier",
1395 .next = NEXT(item_vxlan, NEXT_ENTRY(UNSIGNED), item_param),
1396 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vxlan, vni)),
1400 .help = "match E-Tag header",
1401 .priv = PRIV_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
1402 .next = NEXT(item_e_tag),
1405 [ITEM_E_TAG_GRP_ECID_B] = {
1406 .name = "grp_ecid_b",
1407 .help = "GRP and E-CID base",
1408 .next = NEXT(item_e_tag, NEXT_ENTRY(UNSIGNED), item_param),
1409 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_e_tag,
1415 .help = "match NVGRE header",
1416 .priv = PRIV_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
1417 .next = NEXT(item_nvgre),
1420 [ITEM_NVGRE_TNI] = {
1422 .help = "virtual subnet ID",
1423 .next = NEXT(item_nvgre, NEXT_ENTRY(UNSIGNED), item_param),
1424 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_nvgre, tni)),
1428 .help = "match MPLS header",
1429 .priv = PRIV_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
1430 .next = NEXT(item_mpls),
1433 [ITEM_MPLS_LABEL] = {
1435 .help = "MPLS label",
1436 .next = NEXT(item_mpls, NEXT_ENTRY(UNSIGNED), item_param),
1437 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_mpls,
1443 .help = "match GRE header",
1444 .priv = PRIV_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
1445 .next = NEXT(item_gre),
1448 [ITEM_GRE_PROTO] = {
1450 .help = "GRE protocol type",
1451 .next = NEXT(item_gre, NEXT_ENTRY(UNSIGNED), item_param),
1452 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gre,
1457 .help = "fuzzy pattern match, expect faster than default",
1458 .priv = PRIV_ITEM(FUZZY,
1459 sizeof(struct rte_flow_item_fuzzy)),
1460 .next = NEXT(item_fuzzy),
1463 [ITEM_FUZZY_THRESH] = {
1465 .help = "match accuracy threshold",
1466 .next = NEXT(item_fuzzy, NEXT_ENTRY(UNSIGNED), item_param),
1467 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_fuzzy,
1472 .help = "match GTP header",
1473 .priv = PRIV_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
1474 .next = NEXT(item_gtp),
1479 .help = "tunnel endpoint identifier",
1480 .next = NEXT(item_gtp, NEXT_ENTRY(UNSIGNED), item_param),
1481 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gtp, teid)),
1485 .help = "match GTP header",
1486 .priv = PRIV_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
1487 .next = NEXT(item_gtp),
1492 .help = "match GTP header",
1493 .priv = PRIV_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
1494 .next = NEXT(item_gtp),
1499 .help = "match GENEVE header",
1500 .priv = PRIV_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
1501 .next = NEXT(item_geneve),
1504 [ITEM_GENEVE_VNI] = {
1506 .help = "virtual network identifier",
1507 .next = NEXT(item_geneve, NEXT_ENTRY(UNSIGNED), item_param),
1508 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_geneve, vni)),
1510 [ITEM_GENEVE_PROTO] = {
1512 .help = "GENEVE protocol type",
1513 .next = NEXT(item_geneve, NEXT_ENTRY(UNSIGNED), item_param),
1514 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_geneve,
1518 /* Validate/create actions. */
1521 .help = "submit a list of associated actions",
1522 .next = NEXT(next_action),
1527 .help = "specify next action",
1528 .next = NEXT(next_action),
1532 .help = "end list of actions",
1533 .priv = PRIV_ACTION(END, 0),
1538 .help = "no-op action",
1539 .priv = PRIV_ACTION(VOID, 0),
1540 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1543 [ACTION_PASSTHRU] = {
1545 .help = "let subsequent rule process matched packets",
1546 .priv = PRIV_ACTION(PASSTHRU, 0),
1547 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1552 .help = "attach 32 bit value to packets",
1553 .priv = PRIV_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
1554 .next = NEXT(action_mark),
1557 [ACTION_MARK_ID] = {
1559 .help = "32 bit value to return with packets",
1560 .next = NEXT(action_mark, NEXT_ENTRY(UNSIGNED)),
1561 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_mark, id)),
1562 .call = parse_vc_conf,
1566 .help = "flag packets",
1567 .priv = PRIV_ACTION(FLAG, 0),
1568 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1573 .help = "assign packets to a given queue index",
1574 .priv = PRIV_ACTION(QUEUE,
1575 sizeof(struct rte_flow_action_queue)),
1576 .next = NEXT(action_queue),
1579 [ACTION_QUEUE_INDEX] = {
1581 .help = "queue index to use",
1582 .next = NEXT(action_queue, NEXT_ENTRY(UNSIGNED)),
1583 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_queue, index)),
1584 .call = parse_vc_conf,
1588 .help = "drop packets (note: passthru has priority)",
1589 .priv = PRIV_ACTION(DROP, 0),
1590 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1595 .help = "enable counters for this rule",
1596 .priv = PRIV_ACTION(COUNT, 0),
1597 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1602 .help = "spread packets among several queues",
1603 .priv = PRIV_ACTION(RSS, sizeof(struct action_rss_data)),
1604 .next = NEXT(action_rss),
1605 .call = parse_vc_action_rss,
1607 [ACTION_RSS_FUNC] = {
1609 .help = "RSS hash function to apply",
1610 .next = NEXT(action_rss,
1611 NEXT_ENTRY(ACTION_RSS_FUNC_DEFAULT,
1612 ACTION_RSS_FUNC_TOEPLITZ,
1613 ACTION_RSS_FUNC_SIMPLE_XOR)),
1615 [ACTION_RSS_FUNC_DEFAULT] = {
1617 .help = "default hash function",
1618 .call = parse_vc_action_rss_func,
1620 [ACTION_RSS_FUNC_TOEPLITZ] = {
1622 .help = "Toeplitz hash function",
1623 .call = parse_vc_action_rss_func,
1625 [ACTION_RSS_FUNC_SIMPLE_XOR] = {
1626 .name = "simple_xor",
1627 .help = "simple XOR hash function",
1628 .call = parse_vc_action_rss_func,
1630 [ACTION_RSS_LEVEL] = {
1632 .help = "encapsulation level for \"types\"",
1633 .next = NEXT(action_rss, NEXT_ENTRY(UNSIGNED)),
1634 .args = ARGS(ARGS_ENTRY_ARB
1635 (offsetof(struct action_rss_data, conf) +
1636 offsetof(struct rte_flow_action_rss, level),
1637 sizeof(((struct rte_flow_action_rss *)0)->
1640 [ACTION_RSS_TYPES] = {
1642 .help = "specific RSS hash types",
1643 .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_TYPE)),
1645 [ACTION_RSS_TYPE] = {
1647 .help = "RSS hash type",
1648 .call = parse_vc_action_rss_type,
1649 .comp = comp_vc_action_rss_type,
1651 [ACTION_RSS_KEY] = {
1653 .help = "RSS hash key",
1654 .next = NEXT(action_rss, NEXT_ENTRY(STRING)),
1655 .args = ARGS(ARGS_ENTRY_ARB(0, 0),
1657 (offsetof(struct action_rss_data, conf) +
1658 offsetof(struct rte_flow_action_rss, key_len),
1659 sizeof(((struct rte_flow_action_rss *)0)->
1661 ARGS_ENTRY(struct action_rss_data, key)),
1663 [ACTION_RSS_KEY_LEN] = {
1665 .help = "RSS hash key length in bytes",
1666 .next = NEXT(action_rss, NEXT_ENTRY(UNSIGNED)),
1667 .args = ARGS(ARGS_ENTRY_ARB_BOUNDED
1668 (offsetof(struct action_rss_data, conf) +
1669 offsetof(struct rte_flow_action_rss, key_len),
1670 sizeof(((struct rte_flow_action_rss *)0)->
1673 RSS_HASH_KEY_LENGTH)),
1675 [ACTION_RSS_QUEUES] = {
1677 .help = "queue indices to use",
1678 .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_QUEUE)),
1679 .call = parse_vc_conf,
1681 [ACTION_RSS_QUEUE] = {
1683 .help = "queue index",
1684 .call = parse_vc_action_rss_queue,
1685 .comp = comp_vc_action_rss_queue,
1689 .help = "redirect packets to physical device function",
1690 .priv = PRIV_ACTION(PF, 0),
1691 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1696 .help = "redirect packets to virtual device function",
1697 .priv = PRIV_ACTION(VF, sizeof(struct rte_flow_action_vf)),
1698 .next = NEXT(action_vf),
1701 [ACTION_VF_ORIGINAL] = {
1703 .help = "use original VF ID if possible",
1704 .next = NEXT(action_vf, NEXT_ENTRY(BOOLEAN)),
1705 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_vf,
1707 .call = parse_vc_conf,
1711 .help = "VF ID to redirect packets to",
1712 .next = NEXT(action_vf, NEXT_ENTRY(UNSIGNED)),
1713 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_vf, id)),
1714 .call = parse_vc_conf,
1718 .help = "meter the directed packets at given id",
1719 .priv = PRIV_ACTION(METER,
1720 sizeof(struct rte_flow_action_meter)),
1721 .next = NEXT(action_meter),
1724 [ACTION_METER_ID] = {
1726 .help = "meter id to use",
1727 .next = NEXT(action_meter, NEXT_ENTRY(UNSIGNED)),
1728 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_meter, mtr_id)),
1729 .call = parse_vc_conf,
1733 /** Remove and return last entry from argument stack. */
1734 static const struct arg *
1735 pop_args(struct context *ctx)
1737 return ctx->args_num ? ctx->args[--ctx->args_num] : NULL;
1740 /** Add entry on top of the argument stack. */
1742 push_args(struct context *ctx, const struct arg *arg)
1744 if (ctx->args_num == CTX_STACK_SIZE)
1746 ctx->args[ctx->args_num++] = arg;
1750 /** Spread value into buffer according to bit-mask. */
1752 arg_entry_bf_fill(void *dst, uintmax_t val, const struct arg *arg)
1754 uint32_t i = arg->size;
1762 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1771 unsigned int shift = 0;
1772 uint8_t *buf = (uint8_t *)dst + arg->offset + (i -= sub);
1774 for (shift = 0; arg->mask[i] >> shift; ++shift) {
1775 if (!(arg->mask[i] & (1 << shift)))
1780 *buf &= ~(1 << shift);
1781 *buf |= (val & 1) << shift;
1789 /** Compare a string with a partial one of a given length. */
1791 strcmp_partial(const char *full, const char *partial, size_t partial_len)
1793 int r = strncmp(full, partial, partial_len);
1797 if (strlen(full) <= partial_len)
1799 return full[partial_len];
1803 * Parse a prefix length and generate a bit-mask.
1805 * Last argument (ctx->args) is retrieved to determine mask size, storage
1806 * location and whether the result must use network byte ordering.
1809 parse_prefix(struct context *ctx, const struct token *token,
1810 const char *str, unsigned int len,
1811 void *buf, unsigned int size)
1813 const struct arg *arg = pop_args(ctx);
1814 static const uint8_t conv[] = "\x00\x80\xc0\xe0\xf0\xf8\xfc\xfe\xff";
1821 /* Argument is expected. */
1825 u = strtoumax(str, &end, 0);
1826 if (errno || (size_t)(end - str) != len)
1831 extra = arg_entry_bf_fill(NULL, 0, arg);
1840 if (!arg_entry_bf_fill(ctx->object, v, arg) ||
1841 !arg_entry_bf_fill(ctx->objmask, -1, arg))
1848 if (bytes > size || bytes + !!extra > size)
1852 buf = (uint8_t *)ctx->object + arg->offset;
1853 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1855 memset((uint8_t *)buf + size - bytes, 0xff, bytes);
1856 memset(buf, 0x00, size - bytes);
1858 ((uint8_t *)buf)[size - bytes - 1] = conv[extra];
1862 memset(buf, 0xff, bytes);
1863 memset((uint8_t *)buf + bytes, 0x00, size - bytes);
1865 ((uint8_t *)buf)[bytes] = conv[extra];
1868 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
1871 push_args(ctx, arg);
1875 /** Default parsing function for token name matching. */
1877 parse_default(struct context *ctx, const struct token *token,
1878 const char *str, unsigned int len,
1879 void *buf, unsigned int size)
1884 if (strcmp_partial(token->name, str, len))
1889 /** Parse flow command, initialize output buffer for subsequent tokens. */
1891 parse_init(struct context *ctx, const struct token *token,
1892 const char *str, unsigned int len,
1893 void *buf, unsigned int size)
1895 struct buffer *out = buf;
1897 /* Token name must match. */
1898 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1900 /* Nothing else to do if there is no buffer. */
1903 /* Make sure buffer is large enough. */
1904 if (size < sizeof(*out))
1906 /* Initialize buffer. */
1907 memset(out, 0x00, sizeof(*out));
1908 memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
1911 ctx->objmask = NULL;
1915 /** Parse tokens for validate/create commands. */
1917 parse_vc(struct context *ctx, const struct token *token,
1918 const char *str, unsigned int len,
1919 void *buf, unsigned int size)
1921 struct buffer *out = buf;
1925 /* Token name must match. */
1926 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1928 /* Nothing else to do if there is no buffer. */
1931 if (!out->command) {
1932 if (ctx->curr != VALIDATE && ctx->curr != CREATE)
1934 if (sizeof(*out) > size)
1936 out->command = ctx->curr;
1939 ctx->objmask = NULL;
1940 out->args.vc.data = (uint8_t *)out + size;
1944 ctx->object = &out->args.vc.attr;
1945 ctx->objmask = NULL;
1946 switch (ctx->curr) {
1951 out->args.vc.attr.ingress = 1;
1954 out->args.vc.attr.egress = 1;
1957 out->args.vc.attr.transfer = 1;
1960 out->args.vc.pattern =
1961 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
1963 ctx->object = out->args.vc.pattern;
1964 ctx->objmask = NULL;
1967 out->args.vc.actions =
1968 (void *)RTE_ALIGN_CEIL((uintptr_t)
1969 (out->args.vc.pattern +
1970 out->args.vc.pattern_n),
1972 ctx->object = out->args.vc.actions;
1973 ctx->objmask = NULL;
1980 if (!out->args.vc.actions) {
1981 const struct parse_item_priv *priv = token->priv;
1982 struct rte_flow_item *item =
1983 out->args.vc.pattern + out->args.vc.pattern_n;
1985 data_size = priv->size * 3; /* spec, last, mask */
1986 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
1987 (out->args.vc.data - data_size),
1989 if ((uint8_t *)item + sizeof(*item) > data)
1991 *item = (struct rte_flow_item){
1994 ++out->args.vc.pattern_n;
1996 ctx->objmask = NULL;
1998 const struct parse_action_priv *priv = token->priv;
1999 struct rte_flow_action *action =
2000 out->args.vc.actions + out->args.vc.actions_n;
2002 data_size = priv->size; /* configuration */
2003 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
2004 (out->args.vc.data - data_size),
2006 if ((uint8_t *)action + sizeof(*action) > data)
2008 *action = (struct rte_flow_action){
2010 .conf = data_size ? data : NULL,
2012 ++out->args.vc.actions_n;
2013 ctx->object = action;
2014 ctx->objmask = NULL;
2016 memset(data, 0, data_size);
2017 out->args.vc.data = data;
2018 ctx->objdata = data_size;
2022 /** Parse pattern item parameter type. */
2024 parse_vc_spec(struct context *ctx, const struct token *token,
2025 const char *str, unsigned int len,
2026 void *buf, unsigned int size)
2028 struct buffer *out = buf;
2029 struct rte_flow_item *item;
2035 /* Token name must match. */
2036 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2038 /* Parse parameter types. */
2039 switch (ctx->curr) {
2040 static const enum index prefix[] = NEXT_ENTRY(PREFIX);
2046 case ITEM_PARAM_SPEC:
2049 case ITEM_PARAM_LAST:
2052 case ITEM_PARAM_PREFIX:
2053 /* Modify next token to expect a prefix. */
2054 if (ctx->next_num < 2)
2056 ctx->next[ctx->next_num - 2] = prefix;
2058 case ITEM_PARAM_MASK:
2064 /* Nothing else to do if there is no buffer. */
2067 if (!out->args.vc.pattern_n)
2069 item = &out->args.vc.pattern[out->args.vc.pattern_n - 1];
2070 data_size = ctx->objdata / 3; /* spec, last, mask */
2071 /* Point to selected object. */
2072 ctx->object = out->args.vc.data + (data_size * index);
2074 ctx->objmask = out->args.vc.data + (data_size * 2); /* mask */
2075 item->mask = ctx->objmask;
2077 ctx->objmask = NULL;
2078 /* Update relevant item pointer. */
2079 *((const void **[]){ &item->spec, &item->last, &item->mask })[index] =
2084 /** Parse action configuration field. */
2086 parse_vc_conf(struct context *ctx, const struct token *token,
2087 const char *str, unsigned int len,
2088 void *buf, unsigned int size)
2090 struct buffer *out = buf;
2093 /* Token name must match. */
2094 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2096 /* Nothing else to do if there is no buffer. */
2099 /* Point to selected object. */
2100 ctx->object = out->args.vc.data;
2101 ctx->objmask = NULL;
2105 /** Parse RSS action. */
2107 parse_vc_action_rss(struct context *ctx, const struct token *token,
2108 const char *str, unsigned int len,
2109 void *buf, unsigned int size)
2111 struct buffer *out = buf;
2112 struct rte_flow_action *action;
2113 struct action_rss_data *action_rss_data;
2117 ret = parse_vc(ctx, token, str, len, buf, size);
2120 /* Nothing else to do if there is no buffer. */
2123 if (!out->args.vc.actions_n)
2125 action = &out->args.vc.actions[out->args.vc.actions_n - 1];
2126 /* Point to selected object. */
2127 ctx->object = out->args.vc.data;
2128 ctx->objmask = NULL;
2129 /* Set up default configuration. */
2130 action_rss_data = ctx->object;
2131 *action_rss_data = (struct action_rss_data){
2132 .conf = (struct rte_flow_action_rss){
2133 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
2136 .key_len = sizeof(action_rss_data->key),
2137 .queue_num = RTE_MIN(nb_rxq, ACTION_RSS_QUEUE_NUM),
2138 .key = action_rss_data->key,
2139 .queue = action_rss_data->queue,
2141 .key = "testpmd's default RSS hash key",
2144 for (i = 0; i < action_rss_data->conf.queue_num; ++i)
2145 action_rss_data->queue[i] = i;
2146 if (!port_id_is_invalid(ctx->port, DISABLED_WARN) &&
2147 ctx->port != (portid_t)RTE_PORT_ALL) {
2148 struct rte_eth_dev_info info;
2150 rte_eth_dev_info_get(ctx->port, &info);
2151 action_rss_data->conf.key_len =
2152 RTE_MIN(sizeof(action_rss_data->key),
2153 info.hash_key_size);
2155 action->conf = &action_rss_data->conf;
2160 * Parse func field for RSS action.
2162 * The RTE_ETH_HASH_FUNCTION_* value to assign is derived from the
2163 * ACTION_RSS_FUNC_* index that called this function.
2166 parse_vc_action_rss_func(struct context *ctx, const struct token *token,
2167 const char *str, unsigned int len,
2168 void *buf, unsigned int size)
2170 struct action_rss_data *action_rss_data;
2171 enum rte_eth_hash_function func;
2175 /* Token name must match. */
2176 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2178 switch (ctx->curr) {
2179 case ACTION_RSS_FUNC_DEFAULT:
2180 func = RTE_ETH_HASH_FUNCTION_DEFAULT;
2182 case ACTION_RSS_FUNC_TOEPLITZ:
2183 func = RTE_ETH_HASH_FUNCTION_TOEPLITZ;
2185 case ACTION_RSS_FUNC_SIMPLE_XOR:
2186 func = RTE_ETH_HASH_FUNCTION_SIMPLE_XOR;
2193 action_rss_data = ctx->object;
2194 action_rss_data->conf.func = func;
2199 * Parse type field for RSS action.
2201 * Valid tokens are type field names and the "end" token.
2204 parse_vc_action_rss_type(struct context *ctx, const struct token *token,
2205 const char *str, unsigned int len,
2206 void *buf, unsigned int size)
2208 static const enum index next[] = NEXT_ENTRY(ACTION_RSS_TYPE);
2209 struct action_rss_data *action_rss_data;
2215 if (ctx->curr != ACTION_RSS_TYPE)
2217 if (!(ctx->objdata >> 16) && ctx->object) {
2218 action_rss_data = ctx->object;
2219 action_rss_data->conf.types = 0;
2221 if (!strcmp_partial("end", str, len)) {
2222 ctx->objdata &= 0xffff;
2225 for (i = 0; rss_type_table[i].str; ++i)
2226 if (!strcmp_partial(rss_type_table[i].str, str, len))
2228 if (!rss_type_table[i].str)
2230 ctx->objdata = 1 << 16 | (ctx->objdata & 0xffff);
2232 if (ctx->next_num == RTE_DIM(ctx->next))
2234 ctx->next[ctx->next_num++] = next;
2237 action_rss_data = ctx->object;
2238 action_rss_data->conf.types |= rss_type_table[i].rss_type;
2243 * Parse queue field for RSS action.
2245 * Valid tokens are queue indices and the "end" token.
2248 parse_vc_action_rss_queue(struct context *ctx, const struct token *token,
2249 const char *str, unsigned int len,
2250 void *buf, unsigned int size)
2252 static const enum index next[] = NEXT_ENTRY(ACTION_RSS_QUEUE);
2253 struct action_rss_data *action_rss_data;
2260 if (ctx->curr != ACTION_RSS_QUEUE)
2262 i = ctx->objdata >> 16;
2263 if (!strcmp_partial("end", str, len)) {
2264 ctx->objdata &= 0xffff;
2267 if (i >= ACTION_RSS_QUEUE_NUM)
2270 ARGS_ENTRY_ARB(offsetof(struct action_rss_data, queue) +
2271 i * sizeof(action_rss_data->queue[i]),
2272 sizeof(action_rss_data->queue[i]))))
2274 ret = parse_int(ctx, token, str, len, NULL, 0);
2280 ctx->objdata = i << 16 | (ctx->objdata & 0xffff);
2282 if (ctx->next_num == RTE_DIM(ctx->next))
2284 ctx->next[ctx->next_num++] = next;
2287 action_rss_data = ctx->object;
2288 action_rss_data->conf.queue_num = i;
2289 action_rss_data->conf.queue = i ? action_rss_data->queue : NULL;
2293 /** Parse tokens for destroy command. */
2295 parse_destroy(struct context *ctx, const struct token *token,
2296 const char *str, unsigned int len,
2297 void *buf, unsigned int size)
2299 struct buffer *out = buf;
2301 /* Token name must match. */
2302 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2304 /* Nothing else to do if there is no buffer. */
2307 if (!out->command) {
2308 if (ctx->curr != DESTROY)
2310 if (sizeof(*out) > size)
2312 out->command = ctx->curr;
2315 ctx->objmask = NULL;
2316 out->args.destroy.rule =
2317 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
2321 if (((uint8_t *)(out->args.destroy.rule + out->args.destroy.rule_n) +
2322 sizeof(*out->args.destroy.rule)) > (uint8_t *)out + size)
2325 ctx->object = out->args.destroy.rule + out->args.destroy.rule_n++;
2326 ctx->objmask = NULL;
2330 /** Parse tokens for flush command. */
2332 parse_flush(struct context *ctx, const struct token *token,
2333 const char *str, unsigned int len,
2334 void *buf, unsigned int size)
2336 struct buffer *out = buf;
2338 /* Token name must match. */
2339 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2341 /* Nothing else to do if there is no buffer. */
2344 if (!out->command) {
2345 if (ctx->curr != FLUSH)
2347 if (sizeof(*out) > size)
2349 out->command = ctx->curr;
2352 ctx->objmask = NULL;
2357 /** Parse tokens for query command. */
2359 parse_query(struct context *ctx, const struct token *token,
2360 const char *str, unsigned int len,
2361 void *buf, unsigned int size)
2363 struct buffer *out = buf;
2365 /* Token name must match. */
2366 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2368 /* Nothing else to do if there is no buffer. */
2371 if (!out->command) {
2372 if (ctx->curr != QUERY)
2374 if (sizeof(*out) > size)
2376 out->command = ctx->curr;
2379 ctx->objmask = NULL;
2384 /** Parse action names. */
2386 parse_action(struct context *ctx, const struct token *token,
2387 const char *str, unsigned int len,
2388 void *buf, unsigned int size)
2390 struct buffer *out = buf;
2391 const struct arg *arg = pop_args(ctx);
2395 /* Argument is expected. */
2398 /* Parse action name. */
2399 for (i = 0; next_action[i]; ++i) {
2400 const struct parse_action_priv *priv;
2402 token = &token_list[next_action[i]];
2403 if (strcmp_partial(token->name, str, len))
2409 memcpy((uint8_t *)ctx->object + arg->offset,
2415 push_args(ctx, arg);
2419 /** Parse tokens for list command. */
2421 parse_list(struct context *ctx, const struct token *token,
2422 const char *str, unsigned int len,
2423 void *buf, unsigned int size)
2425 struct buffer *out = buf;
2427 /* Token name must match. */
2428 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2430 /* Nothing else to do if there is no buffer. */
2433 if (!out->command) {
2434 if (ctx->curr != LIST)
2436 if (sizeof(*out) > size)
2438 out->command = ctx->curr;
2441 ctx->objmask = NULL;
2442 out->args.list.group =
2443 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
2447 if (((uint8_t *)(out->args.list.group + out->args.list.group_n) +
2448 sizeof(*out->args.list.group)) > (uint8_t *)out + size)
2451 ctx->object = out->args.list.group + out->args.list.group_n++;
2452 ctx->objmask = NULL;
2456 /** Parse tokens for isolate command. */
2458 parse_isolate(struct context *ctx, const struct token *token,
2459 const char *str, unsigned int len,
2460 void *buf, unsigned int size)
2462 struct buffer *out = buf;
2464 /* Token name must match. */
2465 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2467 /* Nothing else to do if there is no buffer. */
2470 if (!out->command) {
2471 if (ctx->curr != ISOLATE)
2473 if (sizeof(*out) > size)
2475 out->command = ctx->curr;
2478 ctx->objmask = NULL;
2484 * Parse signed/unsigned integers 8 to 64-bit long.
2486 * Last argument (ctx->args) is retrieved to determine integer type and
2490 parse_int(struct context *ctx, const struct token *token,
2491 const char *str, unsigned int len,
2492 void *buf, unsigned int size)
2494 const struct arg *arg = pop_args(ctx);
2499 /* Argument is expected. */
2504 (uintmax_t)strtoimax(str, &end, 0) :
2505 strtoumax(str, &end, 0);
2506 if (errno || (size_t)(end - str) != len)
2509 ((arg->sign && ((intmax_t)u < (intmax_t)arg->min ||
2510 (intmax_t)u > (intmax_t)arg->max)) ||
2511 (!arg->sign && (u < arg->min || u > arg->max))))
2516 if (!arg_entry_bf_fill(ctx->object, u, arg) ||
2517 !arg_entry_bf_fill(ctx->objmask, -1, arg))
2521 buf = (uint8_t *)ctx->object + arg->offset;
2525 case sizeof(uint8_t):
2526 *(uint8_t *)buf = u;
2528 case sizeof(uint16_t):
2529 *(uint16_t *)buf = arg->hton ? rte_cpu_to_be_16(u) : u;
2531 case sizeof(uint8_t [3]):
2532 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
2534 ((uint8_t *)buf)[0] = u;
2535 ((uint8_t *)buf)[1] = u >> 8;
2536 ((uint8_t *)buf)[2] = u >> 16;
2540 ((uint8_t *)buf)[0] = u >> 16;
2541 ((uint8_t *)buf)[1] = u >> 8;
2542 ((uint8_t *)buf)[2] = u;
2544 case sizeof(uint32_t):
2545 *(uint32_t *)buf = arg->hton ? rte_cpu_to_be_32(u) : u;
2547 case sizeof(uint64_t):
2548 *(uint64_t *)buf = arg->hton ? rte_cpu_to_be_64(u) : u;
2553 if (ctx->objmask && buf != (uint8_t *)ctx->objmask + arg->offset) {
2555 buf = (uint8_t *)ctx->objmask + arg->offset;
2560 push_args(ctx, arg);
2567 * Three arguments (ctx->args) are retrieved from the stack to store data,
2568 * its actual length and address (in that order).
2571 parse_string(struct context *ctx, const struct token *token,
2572 const char *str, unsigned int len,
2573 void *buf, unsigned int size)
2575 const struct arg *arg_data = pop_args(ctx);
2576 const struct arg *arg_len = pop_args(ctx);
2577 const struct arg *arg_addr = pop_args(ctx);
2578 char tmp[16]; /* Ought to be enough. */
2581 /* Arguments are expected. */
2585 push_args(ctx, arg_data);
2589 push_args(ctx, arg_len);
2590 push_args(ctx, arg_data);
2593 size = arg_data->size;
2594 /* Bit-mask fill is not supported. */
2595 if (arg_data->mask || size < len)
2599 /* Let parse_int() fill length information first. */
2600 ret = snprintf(tmp, sizeof(tmp), "%u", len);
2603 push_args(ctx, arg_len);
2604 ret = parse_int(ctx, token, tmp, ret, NULL, 0);
2609 buf = (uint8_t *)ctx->object + arg_data->offset;
2610 /* Output buffer is not necessarily NUL-terminated. */
2611 memcpy(buf, str, len);
2612 memset((uint8_t *)buf + len, 0x00, size - len);
2614 memset((uint8_t *)ctx->objmask + arg_data->offset, 0xff, len);
2615 /* Save address if requested. */
2616 if (arg_addr->size) {
2617 memcpy((uint8_t *)ctx->object + arg_addr->offset,
2619 (uint8_t *)ctx->object + arg_data->offset
2623 memcpy((uint8_t *)ctx->objmask + arg_addr->offset,
2625 (uint8_t *)ctx->objmask + arg_data->offset
2631 push_args(ctx, arg_addr);
2632 push_args(ctx, arg_len);
2633 push_args(ctx, arg_data);
2638 * Parse a MAC address.
2640 * Last argument (ctx->args) is retrieved to determine storage size and
2644 parse_mac_addr(struct context *ctx, const struct token *token,
2645 const char *str, unsigned int len,
2646 void *buf, unsigned int size)
2648 const struct arg *arg = pop_args(ctx);
2649 struct ether_addr tmp;
2653 /* Argument is expected. */
2657 /* Bit-mask fill is not supported. */
2658 if (arg->mask || size != sizeof(tmp))
2660 /* Only network endian is supported. */
2663 ret = cmdline_parse_etheraddr(NULL, str, &tmp, size);
2664 if (ret < 0 || (unsigned int)ret != len)
2668 buf = (uint8_t *)ctx->object + arg->offset;
2669 memcpy(buf, &tmp, size);
2671 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2674 push_args(ctx, arg);
2679 * Parse an IPv4 address.
2681 * Last argument (ctx->args) is retrieved to determine storage size and
2685 parse_ipv4_addr(struct context *ctx, const struct token *token,
2686 const char *str, unsigned int len,
2687 void *buf, unsigned int size)
2689 const struct arg *arg = pop_args(ctx);
2694 /* Argument is expected. */
2698 /* Bit-mask fill is not supported. */
2699 if (arg->mask || size != sizeof(tmp))
2701 /* Only network endian is supported. */
2704 memcpy(str2, str, len);
2706 ret = inet_pton(AF_INET, str2, &tmp);
2708 /* Attempt integer parsing. */
2709 push_args(ctx, arg);
2710 return parse_int(ctx, token, str, len, buf, size);
2714 buf = (uint8_t *)ctx->object + arg->offset;
2715 memcpy(buf, &tmp, size);
2717 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2720 push_args(ctx, arg);
2725 * Parse an IPv6 address.
2727 * Last argument (ctx->args) is retrieved to determine storage size and
2731 parse_ipv6_addr(struct context *ctx, const struct token *token,
2732 const char *str, unsigned int len,
2733 void *buf, unsigned int size)
2735 const struct arg *arg = pop_args(ctx);
2737 struct in6_addr tmp;
2741 /* Argument is expected. */
2745 /* Bit-mask fill is not supported. */
2746 if (arg->mask || size != sizeof(tmp))
2748 /* Only network endian is supported. */
2751 memcpy(str2, str, len);
2753 ret = inet_pton(AF_INET6, str2, &tmp);
2758 buf = (uint8_t *)ctx->object + arg->offset;
2759 memcpy(buf, &tmp, size);
2761 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2764 push_args(ctx, arg);
2768 /** Boolean values (even indices stand for false). */
2769 static const char *const boolean_name[] = {
2779 * Parse a boolean value.
2781 * Last argument (ctx->args) is retrieved to determine storage size and
2785 parse_boolean(struct context *ctx, const struct token *token,
2786 const char *str, unsigned int len,
2787 void *buf, unsigned int size)
2789 const struct arg *arg = pop_args(ctx);
2793 /* Argument is expected. */
2796 for (i = 0; boolean_name[i]; ++i)
2797 if (!strcmp_partial(boolean_name[i], str, len))
2799 /* Process token as integer. */
2800 if (boolean_name[i])
2801 str = i & 1 ? "1" : "0";
2802 push_args(ctx, arg);
2803 ret = parse_int(ctx, token, str, strlen(str), buf, size);
2804 return ret > 0 ? (int)len : ret;
2807 /** Parse port and update context. */
2809 parse_port(struct context *ctx, const struct token *token,
2810 const char *str, unsigned int len,
2811 void *buf, unsigned int size)
2813 struct buffer *out = &(struct buffer){ .port = 0 };
2821 ctx->objmask = NULL;
2822 size = sizeof(*out);
2824 ret = parse_int(ctx, token, str, len, out, size);
2826 ctx->port = out->port;
2832 /** No completion. */
2834 comp_none(struct context *ctx, const struct token *token,
2835 unsigned int ent, char *buf, unsigned int size)
2845 /** Complete boolean values. */
2847 comp_boolean(struct context *ctx, const struct token *token,
2848 unsigned int ent, char *buf, unsigned int size)
2854 for (i = 0; boolean_name[i]; ++i)
2855 if (buf && i == ent)
2856 return snprintf(buf, size, "%s", boolean_name[i]);
2862 /** Complete action names. */
2864 comp_action(struct context *ctx, const struct token *token,
2865 unsigned int ent, char *buf, unsigned int size)
2871 for (i = 0; next_action[i]; ++i)
2872 if (buf && i == ent)
2873 return snprintf(buf, size, "%s",
2874 token_list[next_action[i]].name);
2880 /** Complete available ports. */
2882 comp_port(struct context *ctx, const struct token *token,
2883 unsigned int ent, char *buf, unsigned int size)
2890 RTE_ETH_FOREACH_DEV(p) {
2891 if (buf && i == ent)
2892 return snprintf(buf, size, "%u", p);
2900 /** Complete available rule IDs. */
2902 comp_rule_id(struct context *ctx, const struct token *token,
2903 unsigned int ent, char *buf, unsigned int size)
2906 struct rte_port *port;
2907 struct port_flow *pf;
2910 if (port_id_is_invalid(ctx->port, DISABLED_WARN) ||
2911 ctx->port == (portid_t)RTE_PORT_ALL)
2913 port = &ports[ctx->port];
2914 for (pf = port->flow_list; pf != NULL; pf = pf->next) {
2915 if (buf && i == ent)
2916 return snprintf(buf, size, "%u", pf->id);
2924 /** Complete type field for RSS action. */
2926 comp_vc_action_rss_type(struct context *ctx, const struct token *token,
2927 unsigned int ent, char *buf, unsigned int size)
2933 for (i = 0; rss_type_table[i].str; ++i)
2938 return snprintf(buf, size, "%s", rss_type_table[ent].str);
2940 return snprintf(buf, size, "end");
2944 /** Complete queue field for RSS action. */
2946 comp_vc_action_rss_queue(struct context *ctx, const struct token *token,
2947 unsigned int ent, char *buf, unsigned int size)
2954 return snprintf(buf, size, "%u", ent);
2956 return snprintf(buf, size, "end");
2960 /** Internal context. */
2961 static struct context cmd_flow_context;
2963 /** Global parser instance (cmdline API). */
2964 cmdline_parse_inst_t cmd_flow;
2966 /** Initialize context. */
2968 cmd_flow_context_init(struct context *ctx)
2970 /* A full memset() is not necessary. */
2980 ctx->objmask = NULL;
2983 /** Parse a token (cmdline API). */
2985 cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result,
2988 struct context *ctx = &cmd_flow_context;
2989 const struct token *token;
2990 const enum index *list;
2995 token = &token_list[ctx->curr];
2996 /* Check argument length. */
2999 for (len = 0; src[len]; ++len)
3000 if (src[len] == '#' || isspace(src[len]))
3004 /* Last argument and EOL detection. */
3005 for (i = len; src[i]; ++i)
3006 if (src[i] == '#' || src[i] == '\r' || src[i] == '\n')
3008 else if (!isspace(src[i])) {
3013 if (src[i] == '\r' || src[i] == '\n') {
3017 /* Initialize context if necessary. */
3018 if (!ctx->next_num) {
3021 ctx->next[ctx->next_num++] = token->next[0];
3023 /* Process argument through candidates. */
3024 ctx->prev = ctx->curr;
3025 list = ctx->next[ctx->next_num - 1];
3026 for (i = 0; list[i]; ++i) {
3027 const struct token *next = &token_list[list[i]];
3030 ctx->curr = list[i];
3032 tmp = next->call(ctx, next, src, len, result, size);
3034 tmp = parse_default(ctx, next, src, len, result, size);
3035 if (tmp == -1 || tmp != len)
3043 /* Push subsequent tokens if any. */
3045 for (i = 0; token->next[i]; ++i) {
3046 if (ctx->next_num == RTE_DIM(ctx->next))
3048 ctx->next[ctx->next_num++] = token->next[i];
3050 /* Push arguments if any. */
3052 for (i = 0; token->args[i]; ++i) {
3053 if (ctx->args_num == RTE_DIM(ctx->args))
3055 ctx->args[ctx->args_num++] = token->args[i];
3060 /** Return number of completion entries (cmdline API). */
3062 cmd_flow_complete_get_nb(cmdline_parse_token_hdr_t *hdr)
3064 struct context *ctx = &cmd_flow_context;
3065 const struct token *token = &token_list[ctx->curr];
3066 const enum index *list;
3070 /* Count number of tokens in current list. */
3072 list = ctx->next[ctx->next_num - 1];
3074 list = token->next[0];
3075 for (i = 0; list[i]; ++i)
3080 * If there is a single token, use its completion callback, otherwise
3081 * return the number of entries.
3083 token = &token_list[list[0]];
3084 if (i == 1 && token->comp) {
3085 /* Save index for cmd_flow_get_help(). */
3086 ctx->prev = list[0];
3087 return token->comp(ctx, token, 0, NULL, 0);
3092 /** Return a completion entry (cmdline API). */
3094 cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,
3095 char *dst, unsigned int size)
3097 struct context *ctx = &cmd_flow_context;
3098 const struct token *token = &token_list[ctx->curr];
3099 const enum index *list;
3103 /* Count number of tokens in current list. */
3105 list = ctx->next[ctx->next_num - 1];
3107 list = token->next[0];
3108 for (i = 0; list[i]; ++i)
3112 /* If there is a single token, use its completion callback. */
3113 token = &token_list[list[0]];
3114 if (i == 1 && token->comp) {
3115 /* Save index for cmd_flow_get_help(). */
3116 ctx->prev = list[0];
3117 return token->comp(ctx, token, index, dst, size) < 0 ? -1 : 0;
3119 /* Otherwise make sure the index is valid and use defaults. */
3122 token = &token_list[list[index]];
3123 snprintf(dst, size, "%s", token->name);
3124 /* Save index for cmd_flow_get_help(). */
3125 ctx->prev = list[index];
3129 /** Populate help strings for current token (cmdline API). */
3131 cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
3133 struct context *ctx = &cmd_flow_context;
3134 const struct token *token = &token_list[ctx->prev];
3139 /* Set token type and update global help with details. */
3140 snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
3142 cmd_flow.help_str = token->help;
3144 cmd_flow.help_str = token->name;
3148 /** Token definition template (cmdline API). */
3149 static struct cmdline_token_hdr cmd_flow_token_hdr = {
3150 .ops = &(struct cmdline_token_ops){
3151 .parse = cmd_flow_parse,
3152 .complete_get_nb = cmd_flow_complete_get_nb,
3153 .complete_get_elt = cmd_flow_complete_get_elt,
3154 .get_help = cmd_flow_get_help,
3159 /** Populate the next dynamic token. */
3161 cmd_flow_tok(cmdline_parse_token_hdr_t **hdr,
3162 cmdline_parse_token_hdr_t **hdr_inst)
3164 struct context *ctx = &cmd_flow_context;
3166 /* Always reinitialize context before requesting the first token. */
3167 if (!(hdr_inst - cmd_flow.tokens))
3168 cmd_flow_context_init(ctx);
3169 /* Return NULL when no more tokens are expected. */
3170 if (!ctx->next_num && ctx->curr) {
3174 /* Determine if command should end here. */
3175 if (ctx->eol && ctx->last && ctx->next_num) {
3176 const enum index *list = ctx->next[ctx->next_num - 1];
3179 for (i = 0; list[i]; ++i) {
3186 *hdr = &cmd_flow_token_hdr;
3189 /** Dispatch parsed buffer to function calls. */
3191 cmd_flow_parsed(const struct buffer *in)
3193 switch (in->command) {
3195 port_flow_validate(in->port, &in->args.vc.attr,
3196 in->args.vc.pattern, in->args.vc.actions);
3199 port_flow_create(in->port, &in->args.vc.attr,
3200 in->args.vc.pattern, in->args.vc.actions);
3203 port_flow_destroy(in->port, in->args.destroy.rule_n,
3204 in->args.destroy.rule);
3207 port_flow_flush(in->port);
3210 port_flow_query(in->port, in->args.query.rule,
3211 in->args.query.action);
3214 port_flow_list(in->port, in->args.list.group_n,
3215 in->args.list.group);
3218 port_flow_isolate(in->port, in->args.isolate.set);
3225 /** Token generator and output processing callback (cmdline API). */
3227 cmd_flow_cb(void *arg0, struct cmdline *cl, void *arg2)
3230 cmd_flow_tok(arg0, arg2);
3232 cmd_flow_parsed(arg0);
3235 /** Global parser instance (cmdline API). */
3236 cmdline_parse_inst_t cmd_flow = {
3238 .data = NULL, /**< Unused. */
3239 .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
3242 }, /**< Tokens are returned by cmd_flow_tok(). */