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,
186 ACTION_PHY_PORT_ORIGINAL,
187 ACTION_PHY_PORT_INDEX,
192 /** Maximum size for pattern in struct rte_flow_item_raw. */
193 #define ITEM_RAW_PATTERN_SIZE 40
195 /** Storage size for struct rte_flow_item_raw including pattern. */
196 #define ITEM_RAW_SIZE \
197 (sizeof(struct rte_flow_item_raw) + ITEM_RAW_PATTERN_SIZE)
199 /** Maximum number of queue indices in struct rte_flow_action_rss. */
200 #define ACTION_RSS_QUEUE_NUM 32
202 /** Storage for struct rte_flow_action_rss including external data. */
203 struct action_rss_data {
204 struct rte_flow_action_rss conf;
205 uint8_t key[RSS_HASH_KEY_LENGTH];
206 uint16_t queue[ACTION_RSS_QUEUE_NUM];
209 /** Maximum number of subsequent tokens and arguments on the stack. */
210 #define CTX_STACK_SIZE 16
212 /** Parser context. */
214 /** Stack of subsequent token lists to process. */
215 const enum index *next[CTX_STACK_SIZE];
216 /** Arguments for stacked tokens. */
217 const void *args[CTX_STACK_SIZE];
218 enum index curr; /**< Current token index. */
219 enum index prev; /**< Index of the last token seen. */
220 int next_num; /**< Number of entries in next[]. */
221 int args_num; /**< Number of entries in args[]. */
222 uint32_t eol:1; /**< EOL has been detected. */
223 uint32_t last:1; /**< No more arguments. */
224 portid_t port; /**< Current port ID (for completions). */
225 uint32_t objdata; /**< Object-specific data. */
226 void *object; /**< Address of current object for relative offsets. */
227 void *objmask; /**< Object a full mask must be written to. */
230 /** Token argument. */
232 uint32_t hton:1; /**< Use network byte ordering. */
233 uint32_t sign:1; /**< Value is signed. */
234 uint32_t bounded:1; /**< Value is bounded. */
235 uintmax_t min; /**< Minimum value if bounded. */
236 uintmax_t max; /**< Maximum value if bounded. */
237 uint32_t offset; /**< Relative offset from ctx->object. */
238 uint32_t size; /**< Field size. */
239 const uint8_t *mask; /**< Bit-mask to use instead of offset/size. */
242 /** Parser token definition. */
244 /** Type displayed during completion (defaults to "TOKEN"). */
246 /** Help displayed during completion (defaults to token name). */
248 /** Private data used by parser functions. */
251 * Lists of subsequent tokens to push on the stack. Each call to the
252 * parser consumes the last entry of that stack.
254 const enum index *const *next;
255 /** Arguments stack for subsequent tokens that need them. */
256 const struct arg *const *args;
258 * Token-processing callback, returns -1 in case of error, the
259 * length of the matched string otherwise. If NULL, attempts to
260 * match the token name.
262 * If buf is not NULL, the result should be stored in it according
263 * to context. An error is returned if not large enough.
265 int (*call)(struct context *ctx, const struct token *token,
266 const char *str, unsigned int len,
267 void *buf, unsigned int size);
269 * Callback that provides possible values for this token, used for
270 * completion. Returns -1 in case of error, the number of possible
271 * values otherwise. If NULL, the token name is used.
273 * If buf is not NULL, entry index ent is written to buf and the
274 * full length of the entry is returned (same behavior as
277 int (*comp)(struct context *ctx, const struct token *token,
278 unsigned int ent, char *buf, unsigned int size);
279 /** Mandatory token name, no default value. */
283 /** Static initializer for the next field. */
284 #define NEXT(...) (const enum index *const []){ __VA_ARGS__, NULL, }
286 /** Static initializer for a NEXT() entry. */
287 #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, }
289 /** Static initializer for the args field. */
290 #define ARGS(...) (const struct arg *const []){ __VA_ARGS__, NULL, }
292 /** Static initializer for ARGS() to target a field. */
293 #define ARGS_ENTRY(s, f) \
294 (&(const struct arg){ \
295 .offset = offsetof(s, f), \
296 .size = sizeof(((s *)0)->f), \
299 /** Static initializer for ARGS() to target a bit-field. */
300 #define ARGS_ENTRY_BF(s, f, b) \
301 (&(const struct arg){ \
303 .mask = (const void *)&(const s){ .f = (1 << (b)) - 1 }, \
306 /** Static initializer for ARGS() to target an arbitrary bit-mask. */
307 #define ARGS_ENTRY_MASK(s, f, m) \
308 (&(const struct arg){ \
309 .offset = offsetof(s, f), \
310 .size = sizeof(((s *)0)->f), \
311 .mask = (const void *)(m), \
314 /** Same as ARGS_ENTRY_MASK() using network byte ordering for the value. */
315 #define ARGS_ENTRY_MASK_HTON(s, f, m) \
316 (&(const struct arg){ \
318 .offset = offsetof(s, f), \
319 .size = sizeof(((s *)0)->f), \
320 .mask = (const void *)(m), \
323 /** Static initializer for ARGS() to target a pointer. */
324 #define ARGS_ENTRY_PTR(s, f) \
325 (&(const struct arg){ \
326 .size = sizeof(*((s *)0)->f), \
329 /** Static initializer for ARGS() with arbitrary offset and size. */
330 #define ARGS_ENTRY_ARB(o, s) \
331 (&(const struct arg){ \
336 /** Same as ARGS_ENTRY_ARB() with bounded values. */
337 #define ARGS_ENTRY_ARB_BOUNDED(o, s, i, a) \
338 (&(const struct arg){ \
346 /** Same as ARGS_ENTRY() using network byte ordering. */
347 #define ARGS_ENTRY_HTON(s, f) \
348 (&(const struct arg){ \
350 .offset = offsetof(s, f), \
351 .size = sizeof(((s *)0)->f), \
354 /** Parser output buffer layout expected by cmd_flow_parsed(). */
356 enum index command; /**< Flow command. */
357 portid_t port; /**< Affected port ID. */
360 struct rte_flow_attr attr;
361 struct rte_flow_item *pattern;
362 struct rte_flow_action *actions;
366 } vc; /**< Validate/create arguments. */
370 } destroy; /**< Destroy arguments. */
373 enum rte_flow_action_type action;
374 } query; /**< Query arguments. */
378 } list; /**< List arguments. */
381 } isolate; /**< Isolated mode arguments. */
382 } args; /**< Command arguments. */
385 /** Private data for pattern items. */
386 struct parse_item_priv {
387 enum rte_flow_item_type type; /**< Item type. */
388 uint32_t size; /**< Size of item specification structure. */
391 #define PRIV_ITEM(t, s) \
392 (&(const struct parse_item_priv){ \
393 .type = RTE_FLOW_ITEM_TYPE_ ## t, \
397 /** Private data for actions. */
398 struct parse_action_priv {
399 enum rte_flow_action_type type; /**< Action type. */
400 uint32_t size; /**< Size of action configuration structure. */
403 #define PRIV_ACTION(t, s) \
404 (&(const struct parse_action_priv){ \
405 .type = RTE_FLOW_ACTION_TYPE_ ## t, \
409 static const enum index next_vc_attr[] = {
419 static const enum index next_destroy_attr[] = {
425 static const enum index next_list_attr[] = {
431 static const enum index item_param[] = {
440 static const enum index next_item[] = {
470 static const enum index item_fuzzy[] = {
476 static const enum index item_any[] = {
482 static const enum index item_vf[] = {
488 static const enum index item_phy_port[] = {
494 static const enum index item_raw[] = {
504 static const enum index item_eth[] = {
512 static const enum index item_vlan[] = {
517 ITEM_VLAN_INNER_TYPE,
522 static const enum index item_ipv4[] = {
532 static const enum index item_ipv6[] = {
543 static const enum index item_icmp[] = {
550 static const enum index item_udp[] = {
557 static const enum index item_tcp[] = {
565 static const enum index item_sctp[] = {
574 static const enum index item_vxlan[] = {
580 static const enum index item_e_tag[] = {
581 ITEM_E_TAG_GRP_ECID_B,
586 static const enum index item_nvgre[] = {
592 static const enum index item_mpls[] = {
598 static const enum index item_gre[] = {
604 static const enum index item_gtp[] = {
610 static const enum index item_geneve[] = {
617 static const enum index next_action[] = {
634 static const enum index action_mark[] = {
640 static const enum index action_queue[] = {
646 static const enum index action_rss[] = {
657 static const enum index action_vf[] = {
664 static const enum index action_phy_port[] = {
665 ACTION_PHY_PORT_ORIGINAL,
666 ACTION_PHY_PORT_INDEX,
671 static const enum index action_meter[] = {
677 static int parse_init(struct context *, const struct token *,
678 const char *, unsigned int,
679 void *, unsigned int);
680 static int parse_vc(struct context *, const struct token *,
681 const char *, unsigned int,
682 void *, unsigned int);
683 static int parse_vc_spec(struct context *, const struct token *,
684 const char *, unsigned int, void *, unsigned int);
685 static int parse_vc_conf(struct context *, const struct token *,
686 const char *, unsigned int, void *, unsigned int);
687 static int parse_vc_action_rss(struct context *, const struct token *,
688 const char *, unsigned int, void *,
690 static int parse_vc_action_rss_func(struct context *, const struct token *,
691 const char *, unsigned int, void *,
693 static int parse_vc_action_rss_type(struct context *, const struct token *,
694 const char *, unsigned int, void *,
696 static int parse_vc_action_rss_queue(struct context *, const struct token *,
697 const char *, unsigned int, void *,
699 static int parse_destroy(struct context *, const struct token *,
700 const char *, unsigned int,
701 void *, unsigned int);
702 static int parse_flush(struct context *, const struct token *,
703 const char *, unsigned int,
704 void *, unsigned int);
705 static int parse_query(struct context *, const struct token *,
706 const char *, unsigned int,
707 void *, unsigned int);
708 static int parse_action(struct context *, const struct token *,
709 const char *, unsigned int,
710 void *, unsigned int);
711 static int parse_list(struct context *, const struct token *,
712 const char *, unsigned int,
713 void *, unsigned int);
714 static int parse_isolate(struct context *, const struct token *,
715 const char *, unsigned int,
716 void *, unsigned int);
717 static int parse_int(struct context *, const struct token *,
718 const char *, unsigned int,
719 void *, unsigned int);
720 static int parse_prefix(struct context *, const struct token *,
721 const char *, unsigned int,
722 void *, unsigned int);
723 static int parse_boolean(struct context *, const struct token *,
724 const char *, unsigned int,
725 void *, unsigned int);
726 static int parse_string(struct context *, const struct token *,
727 const char *, unsigned int,
728 void *, unsigned int);
729 static int parse_mac_addr(struct context *, const struct token *,
730 const char *, unsigned int,
731 void *, unsigned int);
732 static int parse_ipv4_addr(struct context *, const struct token *,
733 const char *, unsigned int,
734 void *, unsigned int);
735 static int parse_ipv6_addr(struct context *, const struct token *,
736 const char *, unsigned int,
737 void *, unsigned int);
738 static int parse_port(struct context *, const struct token *,
739 const char *, unsigned int,
740 void *, unsigned int);
741 static int comp_none(struct context *, const struct token *,
742 unsigned int, char *, unsigned int);
743 static int comp_boolean(struct context *, const struct token *,
744 unsigned int, char *, unsigned int);
745 static int comp_action(struct context *, const struct token *,
746 unsigned int, char *, unsigned int);
747 static int comp_port(struct context *, const struct token *,
748 unsigned int, char *, unsigned int);
749 static int comp_rule_id(struct context *, const struct token *,
750 unsigned int, char *, unsigned int);
751 static int comp_vc_action_rss_type(struct context *, const struct token *,
752 unsigned int, char *, unsigned int);
753 static int comp_vc_action_rss_queue(struct context *, const struct token *,
754 unsigned int, char *, unsigned int);
756 /** Token definitions. */
757 static const struct token token_list[] = {
758 /* Special tokens. */
761 .help = "null entry, abused as the entry point",
762 .next = NEXT(NEXT_ENTRY(FLOW)),
767 .help = "command may end here",
773 .help = "integer value",
778 .name = "{unsigned}",
780 .help = "unsigned integer value",
787 .help = "prefix length for bit-mask",
788 .call = parse_prefix,
794 .help = "any boolean value",
795 .call = parse_boolean,
796 .comp = comp_boolean,
801 .help = "fixed string",
802 .call = parse_string,
806 .name = "{MAC address}",
808 .help = "standard MAC address notation",
809 .call = parse_mac_addr,
813 .name = "{IPv4 address}",
814 .type = "IPV4 ADDRESS",
815 .help = "standard IPv4 address notation",
816 .call = parse_ipv4_addr,
820 .name = "{IPv6 address}",
821 .type = "IPV6 ADDRESS",
822 .help = "standard IPv6 address notation",
823 .call = parse_ipv6_addr,
829 .help = "rule identifier",
831 .comp = comp_rule_id,
836 .help = "port identifier",
841 .name = "{group_id}",
843 .help = "group identifier",
850 .help = "priority level",
854 /* Top-level command. */
857 .type = "{command} {port_id} [{arg} [...]]",
858 .help = "manage ingress/egress flow rules",
859 .next = NEXT(NEXT_ENTRY
869 /* Sub-level commands. */
872 .help = "check whether a flow rule can be created",
873 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
874 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
879 .help = "create a flow rule",
880 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
881 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
886 .help = "destroy specific flow rules",
887 .next = NEXT(NEXT_ENTRY(DESTROY_RULE), NEXT_ENTRY(PORT_ID)),
888 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
889 .call = parse_destroy,
893 .help = "destroy all flow rules",
894 .next = NEXT(NEXT_ENTRY(PORT_ID)),
895 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
900 .help = "query an existing flow rule",
901 .next = NEXT(NEXT_ENTRY(QUERY_ACTION),
903 NEXT_ENTRY(PORT_ID)),
904 .args = ARGS(ARGS_ENTRY(struct buffer, args.query.action),
905 ARGS_ENTRY(struct buffer, args.query.rule),
906 ARGS_ENTRY(struct buffer, port)),
911 .help = "list existing flow rules",
912 .next = NEXT(next_list_attr, NEXT_ENTRY(PORT_ID)),
913 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
918 .help = "restrict ingress traffic to the defined flow rules",
919 .next = NEXT(NEXT_ENTRY(BOOLEAN),
920 NEXT_ENTRY(PORT_ID)),
921 .args = ARGS(ARGS_ENTRY(struct buffer, args.isolate.set),
922 ARGS_ENTRY(struct buffer, port)),
923 .call = parse_isolate,
925 /* Destroy arguments. */
928 .help = "specify a rule identifier",
929 .next = NEXT(next_destroy_attr, NEXT_ENTRY(RULE_ID)),
930 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.destroy.rule)),
931 .call = parse_destroy,
933 /* Query arguments. */
937 .help = "action to query, must be part of the rule",
938 .call = parse_action,
941 /* List arguments. */
944 .help = "specify a group",
945 .next = NEXT(next_list_attr, NEXT_ENTRY(GROUP_ID)),
946 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.list.group)),
949 /* Validate/create attributes. */
952 .help = "specify a group",
953 .next = NEXT(next_vc_attr, NEXT_ENTRY(GROUP_ID)),
954 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, group)),
959 .help = "specify a priority level",
960 .next = NEXT(next_vc_attr, NEXT_ENTRY(PRIORITY_LEVEL)),
961 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, priority)),
966 .help = "affect rule to ingress",
967 .next = NEXT(next_vc_attr),
972 .help = "affect rule to egress",
973 .next = NEXT(next_vc_attr),
978 .help = "apply rule directly to endpoints found in pattern",
979 .next = NEXT(next_vc_attr),
982 /* Validate/create pattern. */
985 .help = "submit a list of pattern items",
986 .next = NEXT(next_item),
991 .help = "match value perfectly (with full bit-mask)",
992 .call = parse_vc_spec,
994 [ITEM_PARAM_SPEC] = {
996 .help = "match value according to configured bit-mask",
997 .call = parse_vc_spec,
999 [ITEM_PARAM_LAST] = {
1001 .help = "specify upper bound to establish a range",
1002 .call = parse_vc_spec,
1004 [ITEM_PARAM_MASK] = {
1006 .help = "specify bit-mask with relevant bits set to one",
1007 .call = parse_vc_spec,
1009 [ITEM_PARAM_PREFIX] = {
1011 .help = "generate bit-mask from a prefix length",
1012 .call = parse_vc_spec,
1016 .help = "specify next pattern item",
1017 .next = NEXT(next_item),
1021 .help = "end list of pattern items",
1022 .priv = PRIV_ITEM(END, 0),
1023 .next = NEXT(NEXT_ENTRY(ACTIONS)),
1028 .help = "no-op pattern item",
1029 .priv = PRIV_ITEM(VOID, 0),
1030 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1035 .help = "perform actions when pattern does not match",
1036 .priv = PRIV_ITEM(INVERT, 0),
1037 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1042 .help = "match any protocol for the current layer",
1043 .priv = PRIV_ITEM(ANY, sizeof(struct rte_flow_item_any)),
1044 .next = NEXT(item_any),
1049 .help = "number of layers covered",
1050 .next = NEXT(item_any, NEXT_ENTRY(UNSIGNED), item_param),
1051 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_any, num)),
1055 .help = "match traffic from/to the physical function",
1056 .priv = PRIV_ITEM(PF, 0),
1057 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1062 .help = "match traffic from/to a virtual function ID",
1063 .priv = PRIV_ITEM(VF, sizeof(struct rte_flow_item_vf)),
1064 .next = NEXT(item_vf),
1070 .next = NEXT(item_vf, NEXT_ENTRY(UNSIGNED), item_param),
1071 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_vf, id)),
1075 .help = "match traffic from/to a specific physical port",
1076 .priv = PRIV_ITEM(PHY_PORT,
1077 sizeof(struct rte_flow_item_phy_port)),
1078 .next = NEXT(item_phy_port),
1081 [ITEM_PHY_PORT_INDEX] = {
1083 .help = "physical port index",
1084 .next = NEXT(item_phy_port, NEXT_ENTRY(UNSIGNED), item_param),
1085 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_phy_port, index)),
1089 .help = "match an arbitrary byte string",
1090 .priv = PRIV_ITEM(RAW, ITEM_RAW_SIZE),
1091 .next = NEXT(item_raw),
1094 [ITEM_RAW_RELATIVE] = {
1096 .help = "look for pattern after the previous item",
1097 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1098 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1101 [ITEM_RAW_SEARCH] = {
1103 .help = "search pattern from offset (see also limit)",
1104 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1105 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1108 [ITEM_RAW_OFFSET] = {
1110 .help = "absolute or relative offset for pattern",
1111 .next = NEXT(item_raw, NEXT_ENTRY(INTEGER), item_param),
1112 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, offset)),
1114 [ITEM_RAW_LIMIT] = {
1116 .help = "search area limit for start of pattern",
1117 .next = NEXT(item_raw, NEXT_ENTRY(UNSIGNED), item_param),
1118 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, limit)),
1120 [ITEM_RAW_PATTERN] = {
1122 .help = "byte string to look for",
1123 .next = NEXT(item_raw,
1125 NEXT_ENTRY(ITEM_PARAM_IS,
1128 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, pattern),
1129 ARGS_ENTRY(struct rte_flow_item_raw, length),
1130 ARGS_ENTRY_ARB(sizeof(struct rte_flow_item_raw),
1131 ITEM_RAW_PATTERN_SIZE)),
1135 .help = "match Ethernet header",
1136 .priv = PRIV_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
1137 .next = NEXT(item_eth),
1142 .help = "destination MAC",
1143 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1144 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, dst)),
1148 .help = "source MAC",
1149 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1150 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, src)),
1154 .help = "EtherType",
1155 .next = NEXT(item_eth, NEXT_ENTRY(UNSIGNED), item_param),
1156 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, type)),
1160 .help = "match 802.1Q/ad VLAN tag",
1161 .priv = PRIV_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
1162 .next = NEXT(item_vlan),
1167 .help = "tag control information",
1168 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1169 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tci)),
1173 .help = "priority code point",
1174 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1175 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1180 .help = "drop eligible indicator",
1181 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1182 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1187 .help = "VLAN identifier",
1188 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1189 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1192 [ITEM_VLAN_INNER_TYPE] = {
1193 .name = "inner_type",
1194 .help = "inner EtherType",
1195 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1196 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan,
1201 .help = "match IPv4 header",
1202 .priv = PRIV_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
1203 .next = NEXT(item_ipv4),
1208 .help = "type of service",
1209 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1210 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1211 hdr.type_of_service)),
1215 .help = "time to live",
1216 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1217 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1220 [ITEM_IPV4_PROTO] = {
1222 .help = "next protocol ID",
1223 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1224 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1225 hdr.next_proto_id)),
1229 .help = "source address",
1230 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1231 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1236 .help = "destination address",
1237 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1238 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1243 .help = "match IPv6 header",
1244 .priv = PRIV_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
1245 .next = NEXT(item_ipv6),
1250 .help = "traffic class",
1251 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1252 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1254 "\x0f\xf0\x00\x00")),
1256 [ITEM_IPV6_FLOW] = {
1258 .help = "flow label",
1259 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1260 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1262 "\x00\x0f\xff\xff")),
1264 [ITEM_IPV6_PROTO] = {
1266 .help = "protocol (next header)",
1267 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1268 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1273 .help = "hop limit",
1274 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1275 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1280 .help = "source address",
1281 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1282 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1287 .help = "destination address",
1288 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1289 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1294 .help = "match ICMP header",
1295 .priv = PRIV_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
1296 .next = NEXT(item_icmp),
1299 [ITEM_ICMP_TYPE] = {
1301 .help = "ICMP packet type",
1302 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1303 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1306 [ITEM_ICMP_CODE] = {
1308 .help = "ICMP packet code",
1309 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1310 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1315 .help = "match UDP header",
1316 .priv = PRIV_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
1317 .next = NEXT(item_udp),
1322 .help = "UDP source port",
1323 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1324 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1329 .help = "UDP destination port",
1330 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1331 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1336 .help = "match TCP header",
1337 .priv = PRIV_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
1338 .next = NEXT(item_tcp),
1343 .help = "TCP source port",
1344 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1345 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1350 .help = "TCP destination port",
1351 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1352 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1355 [ITEM_TCP_FLAGS] = {
1357 .help = "TCP flags",
1358 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1359 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1364 .help = "match SCTP header",
1365 .priv = PRIV_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
1366 .next = NEXT(item_sctp),
1371 .help = "SCTP source port",
1372 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1373 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1378 .help = "SCTP destination port",
1379 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1380 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1385 .help = "validation tag",
1386 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1387 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1390 [ITEM_SCTP_CKSUM] = {
1393 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1394 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1399 .help = "match VXLAN header",
1400 .priv = PRIV_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
1401 .next = NEXT(item_vxlan),
1404 [ITEM_VXLAN_VNI] = {
1406 .help = "VXLAN identifier",
1407 .next = NEXT(item_vxlan, NEXT_ENTRY(UNSIGNED), item_param),
1408 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vxlan, vni)),
1412 .help = "match E-Tag header",
1413 .priv = PRIV_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
1414 .next = NEXT(item_e_tag),
1417 [ITEM_E_TAG_GRP_ECID_B] = {
1418 .name = "grp_ecid_b",
1419 .help = "GRP and E-CID base",
1420 .next = NEXT(item_e_tag, NEXT_ENTRY(UNSIGNED), item_param),
1421 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_e_tag,
1427 .help = "match NVGRE header",
1428 .priv = PRIV_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
1429 .next = NEXT(item_nvgre),
1432 [ITEM_NVGRE_TNI] = {
1434 .help = "virtual subnet ID",
1435 .next = NEXT(item_nvgre, NEXT_ENTRY(UNSIGNED), item_param),
1436 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_nvgre, tni)),
1440 .help = "match MPLS header",
1441 .priv = PRIV_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
1442 .next = NEXT(item_mpls),
1445 [ITEM_MPLS_LABEL] = {
1447 .help = "MPLS label",
1448 .next = NEXT(item_mpls, NEXT_ENTRY(UNSIGNED), item_param),
1449 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_mpls,
1455 .help = "match GRE header",
1456 .priv = PRIV_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
1457 .next = NEXT(item_gre),
1460 [ITEM_GRE_PROTO] = {
1462 .help = "GRE protocol type",
1463 .next = NEXT(item_gre, NEXT_ENTRY(UNSIGNED), item_param),
1464 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gre,
1469 .help = "fuzzy pattern match, expect faster than default",
1470 .priv = PRIV_ITEM(FUZZY,
1471 sizeof(struct rte_flow_item_fuzzy)),
1472 .next = NEXT(item_fuzzy),
1475 [ITEM_FUZZY_THRESH] = {
1477 .help = "match accuracy threshold",
1478 .next = NEXT(item_fuzzy, NEXT_ENTRY(UNSIGNED), item_param),
1479 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_fuzzy,
1484 .help = "match GTP header",
1485 .priv = PRIV_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
1486 .next = NEXT(item_gtp),
1491 .help = "tunnel endpoint identifier",
1492 .next = NEXT(item_gtp, NEXT_ENTRY(UNSIGNED), item_param),
1493 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gtp, teid)),
1497 .help = "match GTP header",
1498 .priv = PRIV_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
1499 .next = NEXT(item_gtp),
1504 .help = "match GTP header",
1505 .priv = PRIV_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
1506 .next = NEXT(item_gtp),
1511 .help = "match GENEVE header",
1512 .priv = PRIV_ITEM(GENEVE, sizeof(struct rte_flow_item_geneve)),
1513 .next = NEXT(item_geneve),
1516 [ITEM_GENEVE_VNI] = {
1518 .help = "virtual network identifier",
1519 .next = NEXT(item_geneve, NEXT_ENTRY(UNSIGNED), item_param),
1520 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_geneve, vni)),
1522 [ITEM_GENEVE_PROTO] = {
1524 .help = "GENEVE protocol type",
1525 .next = NEXT(item_geneve, NEXT_ENTRY(UNSIGNED), item_param),
1526 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_geneve,
1530 /* Validate/create actions. */
1533 .help = "submit a list of associated actions",
1534 .next = NEXT(next_action),
1539 .help = "specify next action",
1540 .next = NEXT(next_action),
1544 .help = "end list of actions",
1545 .priv = PRIV_ACTION(END, 0),
1550 .help = "no-op action",
1551 .priv = PRIV_ACTION(VOID, 0),
1552 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1555 [ACTION_PASSTHRU] = {
1557 .help = "let subsequent rule process matched packets",
1558 .priv = PRIV_ACTION(PASSTHRU, 0),
1559 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1564 .help = "attach 32 bit value to packets",
1565 .priv = PRIV_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
1566 .next = NEXT(action_mark),
1569 [ACTION_MARK_ID] = {
1571 .help = "32 bit value to return with packets",
1572 .next = NEXT(action_mark, NEXT_ENTRY(UNSIGNED)),
1573 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_mark, id)),
1574 .call = parse_vc_conf,
1578 .help = "flag packets",
1579 .priv = PRIV_ACTION(FLAG, 0),
1580 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1585 .help = "assign packets to a given queue index",
1586 .priv = PRIV_ACTION(QUEUE,
1587 sizeof(struct rte_flow_action_queue)),
1588 .next = NEXT(action_queue),
1591 [ACTION_QUEUE_INDEX] = {
1593 .help = "queue index to use",
1594 .next = NEXT(action_queue, NEXT_ENTRY(UNSIGNED)),
1595 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_queue, index)),
1596 .call = parse_vc_conf,
1600 .help = "drop packets (note: passthru has priority)",
1601 .priv = PRIV_ACTION(DROP, 0),
1602 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1607 .help = "enable counters for this rule",
1608 .priv = PRIV_ACTION(COUNT, 0),
1609 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1614 .help = "spread packets among several queues",
1615 .priv = PRIV_ACTION(RSS, sizeof(struct action_rss_data)),
1616 .next = NEXT(action_rss),
1617 .call = parse_vc_action_rss,
1619 [ACTION_RSS_FUNC] = {
1621 .help = "RSS hash function to apply",
1622 .next = NEXT(action_rss,
1623 NEXT_ENTRY(ACTION_RSS_FUNC_DEFAULT,
1624 ACTION_RSS_FUNC_TOEPLITZ,
1625 ACTION_RSS_FUNC_SIMPLE_XOR)),
1627 [ACTION_RSS_FUNC_DEFAULT] = {
1629 .help = "default hash function",
1630 .call = parse_vc_action_rss_func,
1632 [ACTION_RSS_FUNC_TOEPLITZ] = {
1634 .help = "Toeplitz hash function",
1635 .call = parse_vc_action_rss_func,
1637 [ACTION_RSS_FUNC_SIMPLE_XOR] = {
1638 .name = "simple_xor",
1639 .help = "simple XOR hash function",
1640 .call = parse_vc_action_rss_func,
1642 [ACTION_RSS_LEVEL] = {
1644 .help = "encapsulation level for \"types\"",
1645 .next = NEXT(action_rss, NEXT_ENTRY(UNSIGNED)),
1646 .args = ARGS(ARGS_ENTRY_ARB
1647 (offsetof(struct action_rss_data, conf) +
1648 offsetof(struct rte_flow_action_rss, level),
1649 sizeof(((struct rte_flow_action_rss *)0)->
1652 [ACTION_RSS_TYPES] = {
1654 .help = "specific RSS hash types",
1655 .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_TYPE)),
1657 [ACTION_RSS_TYPE] = {
1659 .help = "RSS hash type",
1660 .call = parse_vc_action_rss_type,
1661 .comp = comp_vc_action_rss_type,
1663 [ACTION_RSS_KEY] = {
1665 .help = "RSS hash key",
1666 .next = NEXT(action_rss, NEXT_ENTRY(STRING)),
1667 .args = ARGS(ARGS_ENTRY_ARB(0, 0),
1669 (offsetof(struct action_rss_data, conf) +
1670 offsetof(struct rte_flow_action_rss, key_len),
1671 sizeof(((struct rte_flow_action_rss *)0)->
1673 ARGS_ENTRY(struct action_rss_data, key)),
1675 [ACTION_RSS_KEY_LEN] = {
1677 .help = "RSS hash key length in bytes",
1678 .next = NEXT(action_rss, NEXT_ENTRY(UNSIGNED)),
1679 .args = ARGS(ARGS_ENTRY_ARB_BOUNDED
1680 (offsetof(struct action_rss_data, conf) +
1681 offsetof(struct rte_flow_action_rss, key_len),
1682 sizeof(((struct rte_flow_action_rss *)0)->
1685 RSS_HASH_KEY_LENGTH)),
1687 [ACTION_RSS_QUEUES] = {
1689 .help = "queue indices to use",
1690 .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_QUEUE)),
1691 .call = parse_vc_conf,
1693 [ACTION_RSS_QUEUE] = {
1695 .help = "queue index",
1696 .call = parse_vc_action_rss_queue,
1697 .comp = comp_vc_action_rss_queue,
1701 .help = "direct traffic to physical function",
1702 .priv = PRIV_ACTION(PF, 0),
1703 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1708 .help = "direct traffic to a virtual function ID",
1709 .priv = PRIV_ACTION(VF, sizeof(struct rte_flow_action_vf)),
1710 .next = NEXT(action_vf),
1713 [ACTION_VF_ORIGINAL] = {
1715 .help = "use original VF ID if possible",
1716 .next = NEXT(action_vf, NEXT_ENTRY(BOOLEAN)),
1717 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_vf,
1719 .call = parse_vc_conf,
1724 .next = NEXT(action_vf, NEXT_ENTRY(UNSIGNED)),
1725 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_vf, id)),
1726 .call = parse_vc_conf,
1728 [ACTION_PHY_PORT] = {
1730 .help = "direct packets to physical port index",
1731 .priv = PRIV_ACTION(PHY_PORT,
1732 sizeof(struct rte_flow_action_phy_port)),
1733 .next = NEXT(action_phy_port),
1736 [ACTION_PHY_PORT_ORIGINAL] = {
1738 .help = "use original port index if possible",
1739 .next = NEXT(action_phy_port, NEXT_ENTRY(BOOLEAN)),
1740 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_phy_port,
1742 .call = parse_vc_conf,
1744 [ACTION_PHY_PORT_INDEX] = {
1746 .help = "physical port index",
1747 .next = NEXT(action_phy_port, NEXT_ENTRY(UNSIGNED)),
1748 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_phy_port,
1750 .call = parse_vc_conf,
1754 .help = "meter the directed packets at given id",
1755 .priv = PRIV_ACTION(METER,
1756 sizeof(struct rte_flow_action_meter)),
1757 .next = NEXT(action_meter),
1760 [ACTION_METER_ID] = {
1762 .help = "meter id to use",
1763 .next = NEXT(action_meter, NEXT_ENTRY(UNSIGNED)),
1764 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_meter, mtr_id)),
1765 .call = parse_vc_conf,
1769 /** Remove and return last entry from argument stack. */
1770 static const struct arg *
1771 pop_args(struct context *ctx)
1773 return ctx->args_num ? ctx->args[--ctx->args_num] : NULL;
1776 /** Add entry on top of the argument stack. */
1778 push_args(struct context *ctx, const struct arg *arg)
1780 if (ctx->args_num == CTX_STACK_SIZE)
1782 ctx->args[ctx->args_num++] = arg;
1786 /** Spread value into buffer according to bit-mask. */
1788 arg_entry_bf_fill(void *dst, uintmax_t val, const struct arg *arg)
1790 uint32_t i = arg->size;
1798 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1807 unsigned int shift = 0;
1808 uint8_t *buf = (uint8_t *)dst + arg->offset + (i -= sub);
1810 for (shift = 0; arg->mask[i] >> shift; ++shift) {
1811 if (!(arg->mask[i] & (1 << shift)))
1816 *buf &= ~(1 << shift);
1817 *buf |= (val & 1) << shift;
1825 /** Compare a string with a partial one of a given length. */
1827 strcmp_partial(const char *full, const char *partial, size_t partial_len)
1829 int r = strncmp(full, partial, partial_len);
1833 if (strlen(full) <= partial_len)
1835 return full[partial_len];
1839 * Parse a prefix length and generate a bit-mask.
1841 * Last argument (ctx->args) is retrieved to determine mask size, storage
1842 * location and whether the result must use network byte ordering.
1845 parse_prefix(struct context *ctx, const struct token *token,
1846 const char *str, unsigned int len,
1847 void *buf, unsigned int size)
1849 const struct arg *arg = pop_args(ctx);
1850 static const uint8_t conv[] = "\x00\x80\xc0\xe0\xf0\xf8\xfc\xfe\xff";
1857 /* Argument is expected. */
1861 u = strtoumax(str, &end, 0);
1862 if (errno || (size_t)(end - str) != len)
1867 extra = arg_entry_bf_fill(NULL, 0, arg);
1876 if (!arg_entry_bf_fill(ctx->object, v, arg) ||
1877 !arg_entry_bf_fill(ctx->objmask, -1, arg))
1884 if (bytes > size || bytes + !!extra > size)
1888 buf = (uint8_t *)ctx->object + arg->offset;
1889 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1891 memset((uint8_t *)buf + size - bytes, 0xff, bytes);
1892 memset(buf, 0x00, size - bytes);
1894 ((uint8_t *)buf)[size - bytes - 1] = conv[extra];
1898 memset(buf, 0xff, bytes);
1899 memset((uint8_t *)buf + bytes, 0x00, size - bytes);
1901 ((uint8_t *)buf)[bytes] = conv[extra];
1904 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
1907 push_args(ctx, arg);
1911 /** Default parsing function for token name matching. */
1913 parse_default(struct context *ctx, const struct token *token,
1914 const char *str, unsigned int len,
1915 void *buf, unsigned int size)
1920 if (strcmp_partial(token->name, str, len))
1925 /** Parse flow command, initialize output buffer for subsequent tokens. */
1927 parse_init(struct context *ctx, const struct token *token,
1928 const char *str, unsigned int len,
1929 void *buf, unsigned int size)
1931 struct buffer *out = buf;
1933 /* Token name must match. */
1934 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1936 /* Nothing else to do if there is no buffer. */
1939 /* Make sure buffer is large enough. */
1940 if (size < sizeof(*out))
1942 /* Initialize buffer. */
1943 memset(out, 0x00, sizeof(*out));
1944 memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
1947 ctx->objmask = NULL;
1951 /** Parse tokens for validate/create commands. */
1953 parse_vc(struct context *ctx, const struct token *token,
1954 const char *str, unsigned int len,
1955 void *buf, unsigned int size)
1957 struct buffer *out = buf;
1961 /* Token name must match. */
1962 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1964 /* Nothing else to do if there is no buffer. */
1967 if (!out->command) {
1968 if (ctx->curr != VALIDATE && ctx->curr != CREATE)
1970 if (sizeof(*out) > size)
1972 out->command = ctx->curr;
1975 ctx->objmask = NULL;
1976 out->args.vc.data = (uint8_t *)out + size;
1980 ctx->object = &out->args.vc.attr;
1981 ctx->objmask = NULL;
1982 switch (ctx->curr) {
1987 out->args.vc.attr.ingress = 1;
1990 out->args.vc.attr.egress = 1;
1993 out->args.vc.attr.transfer = 1;
1996 out->args.vc.pattern =
1997 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
1999 ctx->object = out->args.vc.pattern;
2000 ctx->objmask = NULL;
2003 out->args.vc.actions =
2004 (void *)RTE_ALIGN_CEIL((uintptr_t)
2005 (out->args.vc.pattern +
2006 out->args.vc.pattern_n),
2008 ctx->object = out->args.vc.actions;
2009 ctx->objmask = NULL;
2016 if (!out->args.vc.actions) {
2017 const struct parse_item_priv *priv = token->priv;
2018 struct rte_flow_item *item =
2019 out->args.vc.pattern + out->args.vc.pattern_n;
2021 data_size = priv->size * 3; /* spec, last, mask */
2022 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
2023 (out->args.vc.data - data_size),
2025 if ((uint8_t *)item + sizeof(*item) > data)
2027 *item = (struct rte_flow_item){
2030 ++out->args.vc.pattern_n;
2032 ctx->objmask = NULL;
2034 const struct parse_action_priv *priv = token->priv;
2035 struct rte_flow_action *action =
2036 out->args.vc.actions + out->args.vc.actions_n;
2038 data_size = priv->size; /* configuration */
2039 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
2040 (out->args.vc.data - data_size),
2042 if ((uint8_t *)action + sizeof(*action) > data)
2044 *action = (struct rte_flow_action){
2046 .conf = data_size ? data : NULL,
2048 ++out->args.vc.actions_n;
2049 ctx->object = action;
2050 ctx->objmask = NULL;
2052 memset(data, 0, data_size);
2053 out->args.vc.data = data;
2054 ctx->objdata = data_size;
2058 /** Parse pattern item parameter type. */
2060 parse_vc_spec(struct context *ctx, const struct token *token,
2061 const char *str, unsigned int len,
2062 void *buf, unsigned int size)
2064 struct buffer *out = buf;
2065 struct rte_flow_item *item;
2071 /* Token name must match. */
2072 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2074 /* Parse parameter types. */
2075 switch (ctx->curr) {
2076 static const enum index prefix[] = NEXT_ENTRY(PREFIX);
2082 case ITEM_PARAM_SPEC:
2085 case ITEM_PARAM_LAST:
2088 case ITEM_PARAM_PREFIX:
2089 /* Modify next token to expect a prefix. */
2090 if (ctx->next_num < 2)
2092 ctx->next[ctx->next_num - 2] = prefix;
2094 case ITEM_PARAM_MASK:
2100 /* Nothing else to do if there is no buffer. */
2103 if (!out->args.vc.pattern_n)
2105 item = &out->args.vc.pattern[out->args.vc.pattern_n - 1];
2106 data_size = ctx->objdata / 3; /* spec, last, mask */
2107 /* Point to selected object. */
2108 ctx->object = out->args.vc.data + (data_size * index);
2110 ctx->objmask = out->args.vc.data + (data_size * 2); /* mask */
2111 item->mask = ctx->objmask;
2113 ctx->objmask = NULL;
2114 /* Update relevant item pointer. */
2115 *((const void **[]){ &item->spec, &item->last, &item->mask })[index] =
2120 /** Parse action configuration field. */
2122 parse_vc_conf(struct context *ctx, const struct token *token,
2123 const char *str, unsigned int len,
2124 void *buf, unsigned int size)
2126 struct buffer *out = buf;
2129 /* Token name must match. */
2130 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2132 /* Nothing else to do if there is no buffer. */
2135 /* Point to selected object. */
2136 ctx->object = out->args.vc.data;
2137 ctx->objmask = NULL;
2141 /** Parse RSS action. */
2143 parse_vc_action_rss(struct context *ctx, const struct token *token,
2144 const char *str, unsigned int len,
2145 void *buf, unsigned int size)
2147 struct buffer *out = buf;
2148 struct rte_flow_action *action;
2149 struct action_rss_data *action_rss_data;
2153 ret = parse_vc(ctx, token, str, len, buf, size);
2156 /* Nothing else to do if there is no buffer. */
2159 if (!out->args.vc.actions_n)
2161 action = &out->args.vc.actions[out->args.vc.actions_n - 1];
2162 /* Point to selected object. */
2163 ctx->object = out->args.vc.data;
2164 ctx->objmask = NULL;
2165 /* Set up default configuration. */
2166 action_rss_data = ctx->object;
2167 *action_rss_data = (struct action_rss_data){
2168 .conf = (struct rte_flow_action_rss){
2169 .func = RTE_ETH_HASH_FUNCTION_DEFAULT,
2172 .key_len = sizeof(action_rss_data->key),
2173 .queue_num = RTE_MIN(nb_rxq, ACTION_RSS_QUEUE_NUM),
2174 .key = action_rss_data->key,
2175 .queue = action_rss_data->queue,
2177 .key = "testpmd's default RSS hash key",
2180 for (i = 0; i < action_rss_data->conf.queue_num; ++i)
2181 action_rss_data->queue[i] = i;
2182 if (!port_id_is_invalid(ctx->port, DISABLED_WARN) &&
2183 ctx->port != (portid_t)RTE_PORT_ALL) {
2184 struct rte_eth_dev_info info;
2186 rte_eth_dev_info_get(ctx->port, &info);
2187 action_rss_data->conf.key_len =
2188 RTE_MIN(sizeof(action_rss_data->key),
2189 info.hash_key_size);
2191 action->conf = &action_rss_data->conf;
2196 * Parse func field for RSS action.
2198 * The RTE_ETH_HASH_FUNCTION_* value to assign is derived from the
2199 * ACTION_RSS_FUNC_* index that called this function.
2202 parse_vc_action_rss_func(struct context *ctx, const struct token *token,
2203 const char *str, unsigned int len,
2204 void *buf, unsigned int size)
2206 struct action_rss_data *action_rss_data;
2207 enum rte_eth_hash_function func;
2211 /* Token name must match. */
2212 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2214 switch (ctx->curr) {
2215 case ACTION_RSS_FUNC_DEFAULT:
2216 func = RTE_ETH_HASH_FUNCTION_DEFAULT;
2218 case ACTION_RSS_FUNC_TOEPLITZ:
2219 func = RTE_ETH_HASH_FUNCTION_TOEPLITZ;
2221 case ACTION_RSS_FUNC_SIMPLE_XOR:
2222 func = RTE_ETH_HASH_FUNCTION_SIMPLE_XOR;
2229 action_rss_data = ctx->object;
2230 action_rss_data->conf.func = func;
2235 * Parse type field for RSS action.
2237 * Valid tokens are type field names and the "end" token.
2240 parse_vc_action_rss_type(struct context *ctx, const struct token *token,
2241 const char *str, unsigned int len,
2242 void *buf, unsigned int size)
2244 static const enum index next[] = NEXT_ENTRY(ACTION_RSS_TYPE);
2245 struct action_rss_data *action_rss_data;
2251 if (ctx->curr != ACTION_RSS_TYPE)
2253 if (!(ctx->objdata >> 16) && ctx->object) {
2254 action_rss_data = ctx->object;
2255 action_rss_data->conf.types = 0;
2257 if (!strcmp_partial("end", str, len)) {
2258 ctx->objdata &= 0xffff;
2261 for (i = 0; rss_type_table[i].str; ++i)
2262 if (!strcmp_partial(rss_type_table[i].str, str, len))
2264 if (!rss_type_table[i].str)
2266 ctx->objdata = 1 << 16 | (ctx->objdata & 0xffff);
2268 if (ctx->next_num == RTE_DIM(ctx->next))
2270 ctx->next[ctx->next_num++] = next;
2273 action_rss_data = ctx->object;
2274 action_rss_data->conf.types |= rss_type_table[i].rss_type;
2279 * Parse queue field for RSS action.
2281 * Valid tokens are queue indices and the "end" token.
2284 parse_vc_action_rss_queue(struct context *ctx, const struct token *token,
2285 const char *str, unsigned int len,
2286 void *buf, unsigned int size)
2288 static const enum index next[] = NEXT_ENTRY(ACTION_RSS_QUEUE);
2289 struct action_rss_data *action_rss_data;
2296 if (ctx->curr != ACTION_RSS_QUEUE)
2298 i = ctx->objdata >> 16;
2299 if (!strcmp_partial("end", str, len)) {
2300 ctx->objdata &= 0xffff;
2303 if (i >= ACTION_RSS_QUEUE_NUM)
2306 ARGS_ENTRY_ARB(offsetof(struct action_rss_data, queue) +
2307 i * sizeof(action_rss_data->queue[i]),
2308 sizeof(action_rss_data->queue[i]))))
2310 ret = parse_int(ctx, token, str, len, NULL, 0);
2316 ctx->objdata = i << 16 | (ctx->objdata & 0xffff);
2318 if (ctx->next_num == RTE_DIM(ctx->next))
2320 ctx->next[ctx->next_num++] = next;
2323 action_rss_data = ctx->object;
2324 action_rss_data->conf.queue_num = i;
2325 action_rss_data->conf.queue = i ? action_rss_data->queue : NULL;
2329 /** Parse tokens for destroy command. */
2331 parse_destroy(struct context *ctx, const struct token *token,
2332 const char *str, unsigned int len,
2333 void *buf, unsigned int size)
2335 struct buffer *out = buf;
2337 /* Token name must match. */
2338 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2340 /* Nothing else to do if there is no buffer. */
2343 if (!out->command) {
2344 if (ctx->curr != DESTROY)
2346 if (sizeof(*out) > size)
2348 out->command = ctx->curr;
2351 ctx->objmask = NULL;
2352 out->args.destroy.rule =
2353 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
2357 if (((uint8_t *)(out->args.destroy.rule + out->args.destroy.rule_n) +
2358 sizeof(*out->args.destroy.rule)) > (uint8_t *)out + size)
2361 ctx->object = out->args.destroy.rule + out->args.destroy.rule_n++;
2362 ctx->objmask = NULL;
2366 /** Parse tokens for flush command. */
2368 parse_flush(struct context *ctx, const struct token *token,
2369 const char *str, unsigned int len,
2370 void *buf, unsigned int size)
2372 struct buffer *out = buf;
2374 /* Token name must match. */
2375 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2377 /* Nothing else to do if there is no buffer. */
2380 if (!out->command) {
2381 if (ctx->curr != FLUSH)
2383 if (sizeof(*out) > size)
2385 out->command = ctx->curr;
2388 ctx->objmask = NULL;
2393 /** Parse tokens for query command. */
2395 parse_query(struct context *ctx, const struct token *token,
2396 const char *str, unsigned int len,
2397 void *buf, unsigned int size)
2399 struct buffer *out = buf;
2401 /* Token name must match. */
2402 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2404 /* Nothing else to do if there is no buffer. */
2407 if (!out->command) {
2408 if (ctx->curr != QUERY)
2410 if (sizeof(*out) > size)
2412 out->command = ctx->curr;
2415 ctx->objmask = NULL;
2420 /** Parse action names. */
2422 parse_action(struct context *ctx, const struct token *token,
2423 const char *str, unsigned int len,
2424 void *buf, unsigned int size)
2426 struct buffer *out = buf;
2427 const struct arg *arg = pop_args(ctx);
2431 /* Argument is expected. */
2434 /* Parse action name. */
2435 for (i = 0; next_action[i]; ++i) {
2436 const struct parse_action_priv *priv;
2438 token = &token_list[next_action[i]];
2439 if (strcmp_partial(token->name, str, len))
2445 memcpy((uint8_t *)ctx->object + arg->offset,
2451 push_args(ctx, arg);
2455 /** Parse tokens for list command. */
2457 parse_list(struct context *ctx, const struct token *token,
2458 const char *str, unsigned int len,
2459 void *buf, unsigned int size)
2461 struct buffer *out = buf;
2463 /* Token name must match. */
2464 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2466 /* Nothing else to do if there is no buffer. */
2469 if (!out->command) {
2470 if (ctx->curr != LIST)
2472 if (sizeof(*out) > size)
2474 out->command = ctx->curr;
2477 ctx->objmask = NULL;
2478 out->args.list.group =
2479 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
2483 if (((uint8_t *)(out->args.list.group + out->args.list.group_n) +
2484 sizeof(*out->args.list.group)) > (uint8_t *)out + size)
2487 ctx->object = out->args.list.group + out->args.list.group_n++;
2488 ctx->objmask = NULL;
2492 /** Parse tokens for isolate command. */
2494 parse_isolate(struct context *ctx, const struct token *token,
2495 const char *str, unsigned int len,
2496 void *buf, unsigned int size)
2498 struct buffer *out = buf;
2500 /* Token name must match. */
2501 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2503 /* Nothing else to do if there is no buffer. */
2506 if (!out->command) {
2507 if (ctx->curr != ISOLATE)
2509 if (sizeof(*out) > size)
2511 out->command = ctx->curr;
2514 ctx->objmask = NULL;
2520 * Parse signed/unsigned integers 8 to 64-bit long.
2522 * Last argument (ctx->args) is retrieved to determine integer type and
2526 parse_int(struct context *ctx, const struct token *token,
2527 const char *str, unsigned int len,
2528 void *buf, unsigned int size)
2530 const struct arg *arg = pop_args(ctx);
2535 /* Argument is expected. */
2540 (uintmax_t)strtoimax(str, &end, 0) :
2541 strtoumax(str, &end, 0);
2542 if (errno || (size_t)(end - str) != len)
2545 ((arg->sign && ((intmax_t)u < (intmax_t)arg->min ||
2546 (intmax_t)u > (intmax_t)arg->max)) ||
2547 (!arg->sign && (u < arg->min || u > arg->max))))
2552 if (!arg_entry_bf_fill(ctx->object, u, arg) ||
2553 !arg_entry_bf_fill(ctx->objmask, -1, arg))
2557 buf = (uint8_t *)ctx->object + arg->offset;
2561 case sizeof(uint8_t):
2562 *(uint8_t *)buf = u;
2564 case sizeof(uint16_t):
2565 *(uint16_t *)buf = arg->hton ? rte_cpu_to_be_16(u) : u;
2567 case sizeof(uint8_t [3]):
2568 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
2570 ((uint8_t *)buf)[0] = u;
2571 ((uint8_t *)buf)[1] = u >> 8;
2572 ((uint8_t *)buf)[2] = u >> 16;
2576 ((uint8_t *)buf)[0] = u >> 16;
2577 ((uint8_t *)buf)[1] = u >> 8;
2578 ((uint8_t *)buf)[2] = u;
2580 case sizeof(uint32_t):
2581 *(uint32_t *)buf = arg->hton ? rte_cpu_to_be_32(u) : u;
2583 case sizeof(uint64_t):
2584 *(uint64_t *)buf = arg->hton ? rte_cpu_to_be_64(u) : u;
2589 if (ctx->objmask && buf != (uint8_t *)ctx->objmask + arg->offset) {
2591 buf = (uint8_t *)ctx->objmask + arg->offset;
2596 push_args(ctx, arg);
2603 * Three arguments (ctx->args) are retrieved from the stack to store data,
2604 * its actual length and address (in that order).
2607 parse_string(struct context *ctx, const struct token *token,
2608 const char *str, unsigned int len,
2609 void *buf, unsigned int size)
2611 const struct arg *arg_data = pop_args(ctx);
2612 const struct arg *arg_len = pop_args(ctx);
2613 const struct arg *arg_addr = pop_args(ctx);
2614 char tmp[16]; /* Ought to be enough. */
2617 /* Arguments are expected. */
2621 push_args(ctx, arg_data);
2625 push_args(ctx, arg_len);
2626 push_args(ctx, arg_data);
2629 size = arg_data->size;
2630 /* Bit-mask fill is not supported. */
2631 if (arg_data->mask || size < len)
2635 /* Let parse_int() fill length information first. */
2636 ret = snprintf(tmp, sizeof(tmp), "%u", len);
2639 push_args(ctx, arg_len);
2640 ret = parse_int(ctx, token, tmp, ret, NULL, 0);
2645 buf = (uint8_t *)ctx->object + arg_data->offset;
2646 /* Output buffer is not necessarily NUL-terminated. */
2647 memcpy(buf, str, len);
2648 memset((uint8_t *)buf + len, 0x00, size - len);
2650 memset((uint8_t *)ctx->objmask + arg_data->offset, 0xff, len);
2651 /* Save address if requested. */
2652 if (arg_addr->size) {
2653 memcpy((uint8_t *)ctx->object + arg_addr->offset,
2655 (uint8_t *)ctx->object + arg_data->offset
2659 memcpy((uint8_t *)ctx->objmask + arg_addr->offset,
2661 (uint8_t *)ctx->objmask + arg_data->offset
2667 push_args(ctx, arg_addr);
2668 push_args(ctx, arg_len);
2669 push_args(ctx, arg_data);
2674 * Parse a MAC address.
2676 * Last argument (ctx->args) is retrieved to determine storage size and
2680 parse_mac_addr(struct context *ctx, const struct token *token,
2681 const char *str, unsigned int len,
2682 void *buf, unsigned int size)
2684 const struct arg *arg = pop_args(ctx);
2685 struct ether_addr tmp;
2689 /* Argument is expected. */
2693 /* Bit-mask fill is not supported. */
2694 if (arg->mask || size != sizeof(tmp))
2696 /* Only network endian is supported. */
2699 ret = cmdline_parse_etheraddr(NULL, str, &tmp, size);
2700 if (ret < 0 || (unsigned int)ret != len)
2704 buf = (uint8_t *)ctx->object + arg->offset;
2705 memcpy(buf, &tmp, size);
2707 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2710 push_args(ctx, arg);
2715 * Parse an IPv4 address.
2717 * Last argument (ctx->args) is retrieved to determine storage size and
2721 parse_ipv4_addr(struct context *ctx, const struct token *token,
2722 const char *str, unsigned int len,
2723 void *buf, unsigned int size)
2725 const struct arg *arg = pop_args(ctx);
2730 /* Argument is expected. */
2734 /* Bit-mask fill is not supported. */
2735 if (arg->mask || size != sizeof(tmp))
2737 /* Only network endian is supported. */
2740 memcpy(str2, str, len);
2742 ret = inet_pton(AF_INET, str2, &tmp);
2744 /* Attempt integer parsing. */
2745 push_args(ctx, arg);
2746 return parse_int(ctx, token, str, len, buf, size);
2750 buf = (uint8_t *)ctx->object + arg->offset;
2751 memcpy(buf, &tmp, size);
2753 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2756 push_args(ctx, arg);
2761 * Parse an IPv6 address.
2763 * Last argument (ctx->args) is retrieved to determine storage size and
2767 parse_ipv6_addr(struct context *ctx, const struct token *token,
2768 const char *str, unsigned int len,
2769 void *buf, unsigned int size)
2771 const struct arg *arg = pop_args(ctx);
2773 struct in6_addr tmp;
2777 /* Argument is expected. */
2781 /* Bit-mask fill is not supported. */
2782 if (arg->mask || size != sizeof(tmp))
2784 /* Only network endian is supported. */
2787 memcpy(str2, str, len);
2789 ret = inet_pton(AF_INET6, str2, &tmp);
2794 buf = (uint8_t *)ctx->object + arg->offset;
2795 memcpy(buf, &tmp, size);
2797 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2800 push_args(ctx, arg);
2804 /** Boolean values (even indices stand for false). */
2805 static const char *const boolean_name[] = {
2815 * Parse a boolean value.
2817 * Last argument (ctx->args) is retrieved to determine storage size and
2821 parse_boolean(struct context *ctx, const struct token *token,
2822 const char *str, unsigned int len,
2823 void *buf, unsigned int size)
2825 const struct arg *arg = pop_args(ctx);
2829 /* Argument is expected. */
2832 for (i = 0; boolean_name[i]; ++i)
2833 if (!strcmp_partial(boolean_name[i], str, len))
2835 /* Process token as integer. */
2836 if (boolean_name[i])
2837 str = i & 1 ? "1" : "0";
2838 push_args(ctx, arg);
2839 ret = parse_int(ctx, token, str, strlen(str), buf, size);
2840 return ret > 0 ? (int)len : ret;
2843 /** Parse port and update context. */
2845 parse_port(struct context *ctx, const struct token *token,
2846 const char *str, unsigned int len,
2847 void *buf, unsigned int size)
2849 struct buffer *out = &(struct buffer){ .port = 0 };
2857 ctx->objmask = NULL;
2858 size = sizeof(*out);
2860 ret = parse_int(ctx, token, str, len, out, size);
2862 ctx->port = out->port;
2868 /** No completion. */
2870 comp_none(struct context *ctx, const struct token *token,
2871 unsigned int ent, char *buf, unsigned int size)
2881 /** Complete boolean values. */
2883 comp_boolean(struct context *ctx, const struct token *token,
2884 unsigned int ent, char *buf, unsigned int size)
2890 for (i = 0; boolean_name[i]; ++i)
2891 if (buf && i == ent)
2892 return snprintf(buf, size, "%s", boolean_name[i]);
2898 /** Complete action names. */
2900 comp_action(struct context *ctx, const struct token *token,
2901 unsigned int ent, char *buf, unsigned int size)
2907 for (i = 0; next_action[i]; ++i)
2908 if (buf && i == ent)
2909 return snprintf(buf, size, "%s",
2910 token_list[next_action[i]].name);
2916 /** Complete available ports. */
2918 comp_port(struct context *ctx, const struct token *token,
2919 unsigned int ent, char *buf, unsigned int size)
2926 RTE_ETH_FOREACH_DEV(p) {
2927 if (buf && i == ent)
2928 return snprintf(buf, size, "%u", p);
2936 /** Complete available rule IDs. */
2938 comp_rule_id(struct context *ctx, const struct token *token,
2939 unsigned int ent, char *buf, unsigned int size)
2942 struct rte_port *port;
2943 struct port_flow *pf;
2946 if (port_id_is_invalid(ctx->port, DISABLED_WARN) ||
2947 ctx->port == (portid_t)RTE_PORT_ALL)
2949 port = &ports[ctx->port];
2950 for (pf = port->flow_list; pf != NULL; pf = pf->next) {
2951 if (buf && i == ent)
2952 return snprintf(buf, size, "%u", pf->id);
2960 /** Complete type field for RSS action. */
2962 comp_vc_action_rss_type(struct context *ctx, const struct token *token,
2963 unsigned int ent, char *buf, unsigned int size)
2969 for (i = 0; rss_type_table[i].str; ++i)
2974 return snprintf(buf, size, "%s", rss_type_table[ent].str);
2976 return snprintf(buf, size, "end");
2980 /** Complete queue field for RSS action. */
2982 comp_vc_action_rss_queue(struct context *ctx, const struct token *token,
2983 unsigned int ent, char *buf, unsigned int size)
2990 return snprintf(buf, size, "%u", ent);
2992 return snprintf(buf, size, "end");
2996 /** Internal context. */
2997 static struct context cmd_flow_context;
2999 /** Global parser instance (cmdline API). */
3000 cmdline_parse_inst_t cmd_flow;
3002 /** Initialize context. */
3004 cmd_flow_context_init(struct context *ctx)
3006 /* A full memset() is not necessary. */
3016 ctx->objmask = NULL;
3019 /** Parse a token (cmdline API). */
3021 cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result,
3024 struct context *ctx = &cmd_flow_context;
3025 const struct token *token;
3026 const enum index *list;
3031 token = &token_list[ctx->curr];
3032 /* Check argument length. */
3035 for (len = 0; src[len]; ++len)
3036 if (src[len] == '#' || isspace(src[len]))
3040 /* Last argument and EOL detection. */
3041 for (i = len; src[i]; ++i)
3042 if (src[i] == '#' || src[i] == '\r' || src[i] == '\n')
3044 else if (!isspace(src[i])) {
3049 if (src[i] == '\r' || src[i] == '\n') {
3053 /* Initialize context if necessary. */
3054 if (!ctx->next_num) {
3057 ctx->next[ctx->next_num++] = token->next[0];
3059 /* Process argument through candidates. */
3060 ctx->prev = ctx->curr;
3061 list = ctx->next[ctx->next_num - 1];
3062 for (i = 0; list[i]; ++i) {
3063 const struct token *next = &token_list[list[i]];
3066 ctx->curr = list[i];
3068 tmp = next->call(ctx, next, src, len, result, size);
3070 tmp = parse_default(ctx, next, src, len, result, size);
3071 if (tmp == -1 || tmp != len)
3079 /* Push subsequent tokens if any. */
3081 for (i = 0; token->next[i]; ++i) {
3082 if (ctx->next_num == RTE_DIM(ctx->next))
3084 ctx->next[ctx->next_num++] = token->next[i];
3086 /* Push arguments if any. */
3088 for (i = 0; token->args[i]; ++i) {
3089 if (ctx->args_num == RTE_DIM(ctx->args))
3091 ctx->args[ctx->args_num++] = token->args[i];
3096 /** Return number of completion entries (cmdline API). */
3098 cmd_flow_complete_get_nb(cmdline_parse_token_hdr_t *hdr)
3100 struct context *ctx = &cmd_flow_context;
3101 const struct token *token = &token_list[ctx->curr];
3102 const enum index *list;
3106 /* Count number of tokens in current list. */
3108 list = ctx->next[ctx->next_num - 1];
3110 list = token->next[0];
3111 for (i = 0; list[i]; ++i)
3116 * If there is a single token, use its completion callback, otherwise
3117 * return the number of entries.
3119 token = &token_list[list[0]];
3120 if (i == 1 && token->comp) {
3121 /* Save index for cmd_flow_get_help(). */
3122 ctx->prev = list[0];
3123 return token->comp(ctx, token, 0, NULL, 0);
3128 /** Return a completion entry (cmdline API). */
3130 cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,
3131 char *dst, unsigned int size)
3133 struct context *ctx = &cmd_flow_context;
3134 const struct token *token = &token_list[ctx->curr];
3135 const enum index *list;
3139 /* Count number of tokens in current list. */
3141 list = ctx->next[ctx->next_num - 1];
3143 list = token->next[0];
3144 for (i = 0; list[i]; ++i)
3148 /* If there is a single token, use its completion callback. */
3149 token = &token_list[list[0]];
3150 if (i == 1 && token->comp) {
3151 /* Save index for cmd_flow_get_help(). */
3152 ctx->prev = list[0];
3153 return token->comp(ctx, token, index, dst, size) < 0 ? -1 : 0;
3155 /* Otherwise make sure the index is valid and use defaults. */
3158 token = &token_list[list[index]];
3159 snprintf(dst, size, "%s", token->name);
3160 /* Save index for cmd_flow_get_help(). */
3161 ctx->prev = list[index];
3165 /** Populate help strings for current token (cmdline API). */
3167 cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
3169 struct context *ctx = &cmd_flow_context;
3170 const struct token *token = &token_list[ctx->prev];
3175 /* Set token type and update global help with details. */
3176 snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
3178 cmd_flow.help_str = token->help;
3180 cmd_flow.help_str = token->name;
3184 /** Token definition template (cmdline API). */
3185 static struct cmdline_token_hdr cmd_flow_token_hdr = {
3186 .ops = &(struct cmdline_token_ops){
3187 .parse = cmd_flow_parse,
3188 .complete_get_nb = cmd_flow_complete_get_nb,
3189 .complete_get_elt = cmd_flow_complete_get_elt,
3190 .get_help = cmd_flow_get_help,
3195 /** Populate the next dynamic token. */
3197 cmd_flow_tok(cmdline_parse_token_hdr_t **hdr,
3198 cmdline_parse_token_hdr_t **hdr_inst)
3200 struct context *ctx = &cmd_flow_context;
3202 /* Always reinitialize context before requesting the first token. */
3203 if (!(hdr_inst - cmd_flow.tokens))
3204 cmd_flow_context_init(ctx);
3205 /* Return NULL when no more tokens are expected. */
3206 if (!ctx->next_num && ctx->curr) {
3210 /* Determine if command should end here. */
3211 if (ctx->eol && ctx->last && ctx->next_num) {
3212 const enum index *list = ctx->next[ctx->next_num - 1];
3215 for (i = 0; list[i]; ++i) {
3222 *hdr = &cmd_flow_token_hdr;
3225 /** Dispatch parsed buffer to function calls. */
3227 cmd_flow_parsed(const struct buffer *in)
3229 switch (in->command) {
3231 port_flow_validate(in->port, &in->args.vc.attr,
3232 in->args.vc.pattern, in->args.vc.actions);
3235 port_flow_create(in->port, &in->args.vc.attr,
3236 in->args.vc.pattern, in->args.vc.actions);
3239 port_flow_destroy(in->port, in->args.destroy.rule_n,
3240 in->args.destroy.rule);
3243 port_flow_flush(in->port);
3246 port_flow_query(in->port, in->args.query.rule,
3247 in->args.query.action);
3250 port_flow_list(in->port, in->args.list.group_n,
3251 in->args.list.group);
3254 port_flow_isolate(in->port, in->args.isolate.set);
3261 /** Token generator and output processing callback (cmdline API). */
3263 cmd_flow_cb(void *arg0, struct cmdline *cl, void *arg2)
3266 cmd_flow_tok(arg0, arg2);
3268 cmd_flow_parsed(arg0);
3271 /** Global parser instance (cmdline API). */
3272 cmdline_parse_inst_t cmd_flow = {
3274 .data = NULL, /**< Unused. */
3275 .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
3278 }, /**< Tokens are returned by cmd_flow_tok(). */