4 * Copyright 2016 6WIND S.A.
5 * Copyright 2016 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <arpa/inet.h>
42 #include <sys/socket.h>
44 #include <rte_common.h>
45 #include <rte_ethdev.h>
46 #include <rte_byteorder.h>
47 #include <cmdline_parse.h>
48 #include <cmdline_parse_etheraddr.h>
53 /** Parser token indices. */
73 /* Top-level command. */
76 /* Sub-level commands. */
85 /* Destroy arguments. */
88 /* Query arguments. */
94 /* Validate/create arguments. */
100 /* Validate/create pattern. */
165 ITEM_E_TAG_GRP_ECID_B,
179 /* Validate/create actions. */
205 /** Size of pattern[] field in struct rte_flow_item_raw. */
206 #define ITEM_RAW_PATTERN_SIZE 36
208 /** Storage size for struct rte_flow_item_raw including pattern. */
209 #define ITEM_RAW_SIZE \
210 (offsetof(struct rte_flow_item_raw, pattern) + ITEM_RAW_PATTERN_SIZE)
212 /** Number of queue[] entries in struct rte_flow_action_rss. */
213 #define ACTION_RSS_NUM 32
215 /** Storage size for struct rte_flow_action_rss including queues. */
216 #define ACTION_RSS_SIZE \
217 (offsetof(struct rte_flow_action_rss, queue) + \
218 sizeof(*((struct rte_flow_action_rss *)0)->queue) * ACTION_RSS_NUM)
220 /** Maximum number of subsequent tokens and arguments on the stack. */
221 #define CTX_STACK_SIZE 16
223 /** Parser context. */
225 /** Stack of subsequent token lists to process. */
226 const enum index *next[CTX_STACK_SIZE];
227 /** Arguments for stacked tokens. */
228 const void *args[CTX_STACK_SIZE];
229 enum index curr; /**< Current token index. */
230 enum index prev; /**< Index of the last token seen. */
231 int next_num; /**< Number of entries in next[]. */
232 int args_num; /**< Number of entries in args[]. */
233 uint32_t eol:1; /**< EOL has been detected. */
234 uint32_t last:1; /**< No more arguments. */
235 portid_t port; /**< Current port ID (for completions). */
236 uint32_t objdata; /**< Object-specific data. */
237 void *object; /**< Address of current object for relative offsets. */
238 void *objmask; /**< Object a full mask must be written to. */
241 /** Token argument. */
243 uint32_t hton:1; /**< Use network byte ordering. */
244 uint32_t sign:1; /**< Value is signed. */
245 uint32_t offset; /**< Relative offset from ctx->object. */
246 uint32_t size; /**< Field size. */
247 const uint8_t *mask; /**< Bit-mask to use instead of offset/size. */
250 /** Parser token definition. */
252 /** Type displayed during completion (defaults to "TOKEN"). */
254 /** Help displayed during completion (defaults to token name). */
256 /** Private data used by parser functions. */
259 * Lists of subsequent tokens to push on the stack. Each call to the
260 * parser consumes the last entry of that stack.
262 const enum index *const *next;
263 /** Arguments stack for subsequent tokens that need them. */
264 const struct arg *const *args;
266 * Token-processing callback, returns -1 in case of error, the
267 * length of the matched string otherwise. If NULL, attempts to
268 * match the token name.
270 * If buf is not NULL, the result should be stored in it according
271 * to context. An error is returned if not large enough.
273 int (*call)(struct context *ctx, const struct token *token,
274 const char *str, unsigned int len,
275 void *buf, unsigned int size);
277 * Callback that provides possible values for this token, used for
278 * completion. Returns -1 in case of error, the number of possible
279 * values otherwise. If NULL, the token name is used.
281 * If buf is not NULL, entry index ent is written to buf and the
282 * full length of the entry is returned (same behavior as
285 int (*comp)(struct context *ctx, const struct token *token,
286 unsigned int ent, char *buf, unsigned int size);
287 /** Mandatory token name, no default value. */
291 /** Static initializer for the next field. */
292 #define NEXT(...) (const enum index *const []){ __VA_ARGS__, NULL, }
294 /** Static initializer for a NEXT() entry. */
295 #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, }
297 /** Static initializer for the args field. */
298 #define ARGS(...) (const struct arg *const []){ __VA_ARGS__, NULL, }
300 /** Static initializer for ARGS() to target a field. */
301 #define ARGS_ENTRY(s, f) \
302 (&(const struct arg){ \
303 .offset = offsetof(s, f), \
304 .size = sizeof(((s *)0)->f), \
307 /** Static initializer for ARGS() to target a bit-field. */
308 #define ARGS_ENTRY_BF(s, f, b) \
309 (&(const struct arg){ \
311 .mask = (const void *)&(const s){ .f = (1 << (b)) - 1 }, \
314 /** Static initializer for ARGS() to target an arbitrary bit-mask. */
315 #define ARGS_ENTRY_MASK(s, f, m) \
316 (&(const struct arg){ \
317 .offset = offsetof(s, f), \
318 .size = sizeof(((s *)0)->f), \
319 .mask = (const void *)(m), \
322 /** Same as ARGS_ENTRY_MASK() using network byte ordering for the value. */
323 #define ARGS_ENTRY_MASK_HTON(s, f, m) \
324 (&(const struct arg){ \
326 .offset = offsetof(s, f), \
327 .size = sizeof(((s *)0)->f), \
328 .mask = (const void *)(m), \
331 /** Static initializer for ARGS() to target a pointer. */
332 #define ARGS_ENTRY_PTR(s, f) \
333 (&(const struct arg){ \
334 .size = sizeof(*((s *)0)->f), \
337 /** Static initializer for ARGS() with arbitrary size. */
338 #define ARGS_ENTRY_USZ(s, f, sz) \
339 (&(const struct arg){ \
340 .offset = offsetof(s, f), \
344 /** Same as ARGS_ENTRY() using network byte ordering. */
345 #define ARGS_ENTRY_HTON(s, f) \
346 (&(const struct arg){ \
348 .offset = offsetof(s, f), \
349 .size = sizeof(((s *)0)->f), \
352 /** Parser output buffer layout expected by cmd_flow_parsed(). */
354 enum index command; /**< Flow command. */
355 portid_t port; /**< Affected port ID. */
358 struct rte_flow_attr attr;
359 struct rte_flow_item *pattern;
360 struct rte_flow_action *actions;
364 } vc; /**< Validate/create arguments. */
368 } destroy; /**< Destroy arguments. */
371 enum rte_flow_action_type action;
372 } query; /**< Query arguments. */
376 } list; /**< List arguments. */
379 } isolate; /**< Isolated mode arguments. */
380 } args; /**< Command arguments. */
383 /** Private data for pattern items. */
384 struct parse_item_priv {
385 enum rte_flow_item_type type; /**< Item type. */
386 uint32_t size; /**< Size of item specification structure. */
389 #define PRIV_ITEM(t, s) \
390 (&(const struct parse_item_priv){ \
391 .type = RTE_FLOW_ITEM_TYPE_ ## t, \
395 /** Private data for actions. */
396 struct parse_action_priv {
397 enum rte_flow_action_type type; /**< Action type. */
398 uint32_t size; /**< Size of action configuration structure. */
401 #define PRIV_ACTION(t, s) \
402 (&(const struct parse_action_priv){ \
403 .type = RTE_FLOW_ACTION_TYPE_ ## t, \
407 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[] = {
466 static const enum index item_fuzzy[] = {
472 static const enum index item_any[] = {
478 static const enum index item_vf[] = {
484 static const enum index item_port[] = {
490 static const enum index item_raw[] = {
500 static const enum index item_eth[] = {
508 static const enum index item_vlan[] = {
518 static const enum index item_ipv4[] = {
528 static const enum index item_ipv6[] = {
539 static const enum index item_icmp[] = {
546 static const enum index item_udp[] = {
553 static const enum index item_tcp[] = {
561 static const enum index item_sctp[] = {
570 static const enum index item_vxlan[] = {
576 static const enum index item_e_tag[] = {
577 ITEM_E_TAG_GRP_ECID_B,
582 static const enum index item_nvgre[] = {
588 static const enum index item_mpls[] = {
594 static const enum index item_gre[] = {
600 static const enum index item_gtp[] = {
606 static const enum index next_action[] = {
623 static const enum index action_mark[] = {
629 static const enum index action_queue[] = {
635 static const enum index action_dup[] = {
641 static const enum index action_rss[] = {
647 static const enum index action_vf[] = {
654 static const enum index action_meter[] = {
660 static int parse_init(struct context *, const struct token *,
661 const char *, unsigned int,
662 void *, unsigned int);
663 static int parse_vc(struct context *, const struct token *,
664 const char *, unsigned int,
665 void *, unsigned int);
666 static int parse_vc_spec(struct context *, const struct token *,
667 const char *, unsigned int, void *, unsigned int);
668 static int parse_vc_conf(struct context *, const struct token *,
669 const char *, unsigned int, void *, unsigned int);
670 static int parse_vc_action_rss_queue(struct context *, const struct token *,
671 const char *, unsigned int, void *,
673 static int parse_destroy(struct context *, const struct token *,
674 const char *, unsigned int,
675 void *, unsigned int);
676 static int parse_flush(struct context *, const struct token *,
677 const char *, unsigned int,
678 void *, unsigned int);
679 static int parse_query(struct context *, const struct token *,
680 const char *, unsigned int,
681 void *, unsigned int);
682 static int parse_action(struct context *, const struct token *,
683 const char *, unsigned int,
684 void *, unsigned int);
685 static int parse_list(struct context *, const struct token *,
686 const char *, unsigned int,
687 void *, unsigned int);
688 static int parse_isolate(struct context *, const struct token *,
689 const char *, unsigned int,
690 void *, unsigned int);
691 static int parse_int(struct context *, const struct token *,
692 const char *, unsigned int,
693 void *, unsigned int);
694 static int parse_prefix(struct context *, const struct token *,
695 const char *, unsigned int,
696 void *, unsigned int);
697 static int parse_boolean(struct context *, const struct token *,
698 const char *, unsigned int,
699 void *, unsigned int);
700 static int parse_string(struct context *, const struct token *,
701 const char *, unsigned int,
702 void *, unsigned int);
703 static int parse_mac_addr(struct context *, const struct token *,
704 const char *, unsigned int,
705 void *, unsigned int);
706 static int parse_ipv4_addr(struct context *, const struct token *,
707 const char *, unsigned int,
708 void *, unsigned int);
709 static int parse_ipv6_addr(struct context *, const struct token *,
710 const char *, unsigned int,
711 void *, unsigned int);
712 static int parse_port(struct context *, const struct token *,
713 const char *, unsigned int,
714 void *, unsigned int);
715 static int comp_none(struct context *, const struct token *,
716 unsigned int, char *, unsigned int);
717 static int comp_boolean(struct context *, const struct token *,
718 unsigned int, char *, unsigned int);
719 static int comp_action(struct context *, const struct token *,
720 unsigned int, char *, unsigned int);
721 static int comp_port(struct context *, const struct token *,
722 unsigned int, char *, unsigned int);
723 static int comp_rule_id(struct context *, const struct token *,
724 unsigned int, char *, unsigned int);
725 static int comp_vc_action_rss_queue(struct context *, const struct token *,
726 unsigned int, char *, unsigned int);
728 /** Token definitions. */
729 static const struct token token_list[] = {
730 /* Special tokens. */
733 .help = "null entry, abused as the entry point",
734 .next = NEXT(NEXT_ENTRY(FLOW)),
739 .help = "command may end here",
745 .help = "integer value",
750 .name = "{unsigned}",
752 .help = "unsigned integer value",
759 .help = "prefix length for bit-mask",
760 .call = parse_prefix,
766 .help = "any boolean value",
767 .call = parse_boolean,
768 .comp = comp_boolean,
773 .help = "fixed string",
774 .call = parse_string,
778 .name = "{MAC address}",
780 .help = "standard MAC address notation",
781 .call = parse_mac_addr,
785 .name = "{IPv4 address}",
786 .type = "IPV4 ADDRESS",
787 .help = "standard IPv4 address notation",
788 .call = parse_ipv4_addr,
792 .name = "{IPv6 address}",
793 .type = "IPV6 ADDRESS",
794 .help = "standard IPv6 address notation",
795 .call = parse_ipv6_addr,
801 .help = "rule identifier",
803 .comp = comp_rule_id,
808 .help = "port identifier",
813 .name = "{group_id}",
815 .help = "group identifier",
822 .help = "priority level",
826 /* Top-level command. */
829 .type = "{command} {port_id} [{arg} [...]]",
830 .help = "manage ingress/egress flow rules",
831 .next = NEXT(NEXT_ENTRY
841 /* Sub-level commands. */
844 .help = "check whether a flow rule can be created",
845 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
846 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
851 .help = "create a flow rule",
852 .next = NEXT(next_vc_attr, NEXT_ENTRY(PORT_ID)),
853 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
858 .help = "destroy specific flow rules",
859 .next = NEXT(NEXT_ENTRY(DESTROY_RULE), NEXT_ENTRY(PORT_ID)),
860 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
861 .call = parse_destroy,
865 .help = "destroy all flow rules",
866 .next = NEXT(NEXT_ENTRY(PORT_ID)),
867 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
872 .help = "query an existing flow rule",
873 .next = NEXT(NEXT_ENTRY(QUERY_ACTION),
875 NEXT_ENTRY(PORT_ID)),
876 .args = ARGS(ARGS_ENTRY(struct buffer, args.query.action),
877 ARGS_ENTRY(struct buffer, args.query.rule),
878 ARGS_ENTRY(struct buffer, port)),
883 .help = "list existing flow rules",
884 .next = NEXT(next_list_attr, NEXT_ENTRY(PORT_ID)),
885 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
890 .help = "restrict ingress traffic to the defined flow rules",
891 .next = NEXT(NEXT_ENTRY(BOOLEAN),
892 NEXT_ENTRY(PORT_ID)),
893 .args = ARGS(ARGS_ENTRY(struct buffer, args.isolate.set),
894 ARGS_ENTRY(struct buffer, port)),
895 .call = parse_isolate,
897 /* Destroy arguments. */
900 .help = "specify a rule identifier",
901 .next = NEXT(next_destroy_attr, NEXT_ENTRY(RULE_ID)),
902 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.destroy.rule)),
903 .call = parse_destroy,
905 /* Query arguments. */
909 .help = "action to query, must be part of the rule",
910 .call = parse_action,
913 /* List arguments. */
916 .help = "specify a group",
917 .next = NEXT(next_list_attr, NEXT_ENTRY(GROUP_ID)),
918 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.list.group)),
921 /* Validate/create attributes. */
924 .help = "specify a group",
925 .next = NEXT(next_vc_attr, NEXT_ENTRY(GROUP_ID)),
926 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, group)),
931 .help = "specify a priority level",
932 .next = NEXT(next_vc_attr, NEXT_ENTRY(PRIORITY_LEVEL)),
933 .args = ARGS(ARGS_ENTRY(struct rte_flow_attr, priority)),
938 .help = "affect rule to ingress",
939 .next = NEXT(next_vc_attr),
944 .help = "affect rule to egress",
945 .next = NEXT(next_vc_attr),
948 /* Validate/create pattern. */
951 .help = "submit a list of pattern items",
952 .next = NEXT(next_item),
957 .help = "match value perfectly (with full bit-mask)",
958 .call = parse_vc_spec,
960 [ITEM_PARAM_SPEC] = {
962 .help = "match value according to configured bit-mask",
963 .call = parse_vc_spec,
965 [ITEM_PARAM_LAST] = {
967 .help = "specify upper bound to establish a range",
968 .call = parse_vc_spec,
970 [ITEM_PARAM_MASK] = {
972 .help = "specify bit-mask with relevant bits set to one",
973 .call = parse_vc_spec,
975 [ITEM_PARAM_PREFIX] = {
977 .help = "generate bit-mask from a prefix length",
978 .call = parse_vc_spec,
982 .help = "specify next pattern item",
983 .next = NEXT(next_item),
987 .help = "end list of pattern items",
988 .priv = PRIV_ITEM(END, 0),
989 .next = NEXT(NEXT_ENTRY(ACTIONS)),
994 .help = "no-op pattern item",
995 .priv = PRIV_ITEM(VOID, 0),
996 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1001 .help = "perform actions when pattern does not match",
1002 .priv = PRIV_ITEM(INVERT, 0),
1003 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1008 .help = "match any protocol for the current layer",
1009 .priv = PRIV_ITEM(ANY, sizeof(struct rte_flow_item_any)),
1010 .next = NEXT(item_any),
1015 .help = "number of layers covered",
1016 .next = NEXT(item_any, NEXT_ENTRY(UNSIGNED), item_param),
1017 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_any, num)),
1021 .help = "match packets addressed to the physical function",
1022 .priv = PRIV_ITEM(PF, 0),
1023 .next = NEXT(NEXT_ENTRY(ITEM_NEXT)),
1028 .help = "match packets addressed to a virtual function ID",
1029 .priv = PRIV_ITEM(VF, sizeof(struct rte_flow_item_vf)),
1030 .next = NEXT(item_vf),
1035 .help = "destination VF ID",
1036 .next = NEXT(item_vf, NEXT_ENTRY(UNSIGNED), item_param),
1037 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_vf, id)),
1041 .help = "device-specific physical port index to use",
1042 .priv = PRIV_ITEM(PORT, sizeof(struct rte_flow_item_port)),
1043 .next = NEXT(item_port),
1046 [ITEM_PORT_INDEX] = {
1048 .help = "physical port index",
1049 .next = NEXT(item_port, NEXT_ENTRY(UNSIGNED), item_param),
1050 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_port, index)),
1054 .help = "match an arbitrary byte string",
1055 .priv = PRIV_ITEM(RAW, ITEM_RAW_SIZE),
1056 .next = NEXT(item_raw),
1059 [ITEM_RAW_RELATIVE] = {
1061 .help = "look for pattern after the previous item",
1062 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1063 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1066 [ITEM_RAW_SEARCH] = {
1068 .help = "search pattern from offset (see also limit)",
1069 .next = NEXT(item_raw, NEXT_ENTRY(BOOLEAN), item_param),
1070 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_item_raw,
1073 [ITEM_RAW_OFFSET] = {
1075 .help = "absolute or relative offset for pattern",
1076 .next = NEXT(item_raw, NEXT_ENTRY(INTEGER), item_param),
1077 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, offset)),
1079 [ITEM_RAW_LIMIT] = {
1081 .help = "search area limit for start of pattern",
1082 .next = NEXT(item_raw, NEXT_ENTRY(UNSIGNED), item_param),
1083 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, limit)),
1085 [ITEM_RAW_PATTERN] = {
1087 .help = "byte string to look for",
1088 .next = NEXT(item_raw,
1090 NEXT_ENTRY(ITEM_PARAM_IS,
1093 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_raw, length),
1094 ARGS_ENTRY_USZ(struct rte_flow_item_raw,
1096 ITEM_RAW_PATTERN_SIZE)),
1100 .help = "match Ethernet header",
1101 .priv = PRIV_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
1102 .next = NEXT(item_eth),
1107 .help = "destination MAC",
1108 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1109 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, dst)),
1113 .help = "source MAC",
1114 .next = NEXT(item_eth, NEXT_ENTRY(MAC_ADDR), item_param),
1115 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, src)),
1119 .help = "EtherType",
1120 .next = NEXT(item_eth, NEXT_ENTRY(UNSIGNED), item_param),
1121 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_eth, type)),
1125 .help = "match 802.1Q/ad VLAN tag",
1126 .priv = PRIV_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
1127 .next = NEXT(item_vlan),
1130 [ITEM_VLAN_TPID] = {
1132 .help = "tag protocol identifier",
1133 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1134 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tpid)),
1138 .help = "tag control information",
1139 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1140 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vlan, tci)),
1144 .help = "priority code point",
1145 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1146 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1151 .help = "drop eligible indicator",
1152 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1153 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1158 .help = "VLAN identifier",
1159 .next = NEXT(item_vlan, NEXT_ENTRY(UNSIGNED), item_param),
1160 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_vlan,
1165 .help = "match IPv4 header",
1166 .priv = PRIV_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
1167 .next = NEXT(item_ipv4),
1172 .help = "type of service",
1173 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1174 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1175 hdr.type_of_service)),
1179 .help = "time to live",
1180 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1181 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1184 [ITEM_IPV4_PROTO] = {
1186 .help = "next protocol ID",
1187 .next = NEXT(item_ipv4, NEXT_ENTRY(UNSIGNED), item_param),
1188 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1189 hdr.next_proto_id)),
1193 .help = "source address",
1194 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1195 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1200 .help = "destination address",
1201 .next = NEXT(item_ipv4, NEXT_ENTRY(IPV4_ADDR), item_param),
1202 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv4,
1207 .help = "match IPv6 header",
1208 .priv = PRIV_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
1209 .next = NEXT(item_ipv6),
1214 .help = "traffic class",
1215 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1216 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1218 "\x0f\xf0\x00\x00")),
1220 [ITEM_IPV6_FLOW] = {
1222 .help = "flow label",
1223 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1224 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_ipv6,
1226 "\x00\x0f\xff\xff")),
1228 [ITEM_IPV6_PROTO] = {
1230 .help = "protocol (next header)",
1231 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1232 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1237 .help = "hop limit",
1238 .next = NEXT(item_ipv6, NEXT_ENTRY(UNSIGNED), item_param),
1239 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1244 .help = "source address",
1245 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1246 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1251 .help = "destination address",
1252 .next = NEXT(item_ipv6, NEXT_ENTRY(IPV6_ADDR), item_param),
1253 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_ipv6,
1258 .help = "match ICMP header",
1259 .priv = PRIV_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
1260 .next = NEXT(item_icmp),
1263 [ITEM_ICMP_TYPE] = {
1265 .help = "ICMP packet type",
1266 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1267 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1270 [ITEM_ICMP_CODE] = {
1272 .help = "ICMP packet code",
1273 .next = NEXT(item_icmp, NEXT_ENTRY(UNSIGNED), item_param),
1274 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_icmp,
1279 .help = "match UDP header",
1280 .priv = PRIV_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
1281 .next = NEXT(item_udp),
1286 .help = "UDP source port",
1287 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1288 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1293 .help = "UDP destination port",
1294 .next = NEXT(item_udp, NEXT_ENTRY(UNSIGNED), item_param),
1295 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_udp,
1300 .help = "match TCP header",
1301 .priv = PRIV_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
1302 .next = NEXT(item_tcp),
1307 .help = "TCP source port",
1308 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1309 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1314 .help = "TCP destination port",
1315 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1316 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1319 [ITEM_TCP_FLAGS] = {
1321 .help = "TCP flags",
1322 .next = NEXT(item_tcp, NEXT_ENTRY(UNSIGNED), item_param),
1323 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_tcp,
1328 .help = "match SCTP header",
1329 .priv = PRIV_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
1330 .next = NEXT(item_sctp),
1335 .help = "SCTP source port",
1336 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1337 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1342 .help = "SCTP destination port",
1343 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1344 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1349 .help = "validation tag",
1350 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1351 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1354 [ITEM_SCTP_CKSUM] = {
1357 .next = NEXT(item_sctp, NEXT_ENTRY(UNSIGNED), item_param),
1358 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_sctp,
1363 .help = "match VXLAN header",
1364 .priv = PRIV_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
1365 .next = NEXT(item_vxlan),
1368 [ITEM_VXLAN_VNI] = {
1370 .help = "VXLAN identifier",
1371 .next = NEXT(item_vxlan, NEXT_ENTRY(UNSIGNED), item_param),
1372 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_vxlan, vni)),
1376 .help = "match E-Tag header",
1377 .priv = PRIV_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
1378 .next = NEXT(item_e_tag),
1381 [ITEM_E_TAG_GRP_ECID_B] = {
1382 .name = "grp_ecid_b",
1383 .help = "GRP and E-CID base",
1384 .next = NEXT(item_e_tag, NEXT_ENTRY(UNSIGNED), item_param),
1385 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_e_tag,
1391 .help = "match NVGRE header",
1392 .priv = PRIV_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
1393 .next = NEXT(item_nvgre),
1396 [ITEM_NVGRE_TNI] = {
1398 .help = "virtual subnet ID",
1399 .next = NEXT(item_nvgre, NEXT_ENTRY(UNSIGNED), item_param),
1400 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_nvgre, tni)),
1404 .help = "match MPLS header",
1405 .priv = PRIV_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
1406 .next = NEXT(item_mpls),
1409 [ITEM_MPLS_LABEL] = {
1411 .help = "MPLS label",
1412 .next = NEXT(item_mpls, NEXT_ENTRY(UNSIGNED), item_param),
1413 .args = ARGS(ARGS_ENTRY_MASK_HTON(struct rte_flow_item_mpls,
1419 .help = "match GRE header",
1420 .priv = PRIV_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
1421 .next = NEXT(item_gre),
1424 [ITEM_GRE_PROTO] = {
1426 .help = "GRE protocol type",
1427 .next = NEXT(item_gre, NEXT_ENTRY(UNSIGNED), item_param),
1428 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gre,
1433 .help = "fuzzy pattern match, expect faster than default",
1434 .priv = PRIV_ITEM(FUZZY,
1435 sizeof(struct rte_flow_item_fuzzy)),
1436 .next = NEXT(item_fuzzy),
1439 [ITEM_FUZZY_THRESH] = {
1441 .help = "match accuracy threshold",
1442 .next = NEXT(item_fuzzy, NEXT_ENTRY(UNSIGNED), item_param),
1443 .args = ARGS(ARGS_ENTRY(struct rte_flow_item_fuzzy,
1448 .help = "match GTP header",
1449 .priv = PRIV_ITEM(GTP, sizeof(struct rte_flow_item_gtp)),
1450 .next = NEXT(item_gtp),
1455 .help = "tunnel endpoint identifier",
1456 .next = NEXT(item_gtp, NEXT_ENTRY(UNSIGNED), item_param),
1457 .args = ARGS(ARGS_ENTRY_HTON(struct rte_flow_item_gtp, teid)),
1461 .help = "match GTP header",
1462 .priv = PRIV_ITEM(GTPC, sizeof(struct rte_flow_item_gtp)),
1463 .next = NEXT(item_gtp),
1468 .help = "match GTP header",
1469 .priv = PRIV_ITEM(GTPU, sizeof(struct rte_flow_item_gtp)),
1470 .next = NEXT(item_gtp),
1474 /* Validate/create actions. */
1477 .help = "submit a list of associated actions",
1478 .next = NEXT(next_action),
1483 .help = "specify next action",
1484 .next = NEXT(next_action),
1488 .help = "end list of actions",
1489 .priv = PRIV_ACTION(END, 0),
1494 .help = "no-op action",
1495 .priv = PRIV_ACTION(VOID, 0),
1496 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1499 [ACTION_PASSTHRU] = {
1501 .help = "let subsequent rule process matched packets",
1502 .priv = PRIV_ACTION(PASSTHRU, 0),
1503 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1508 .help = "attach 32 bit value to packets",
1509 .priv = PRIV_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
1510 .next = NEXT(action_mark),
1513 [ACTION_MARK_ID] = {
1515 .help = "32 bit value to return with packets",
1516 .next = NEXT(action_mark, NEXT_ENTRY(UNSIGNED)),
1517 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_mark, id)),
1518 .call = parse_vc_conf,
1522 .help = "flag packets",
1523 .priv = PRIV_ACTION(FLAG, 0),
1524 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1529 .help = "assign packets to a given queue index",
1530 .priv = PRIV_ACTION(QUEUE,
1531 sizeof(struct rte_flow_action_queue)),
1532 .next = NEXT(action_queue),
1535 [ACTION_QUEUE_INDEX] = {
1537 .help = "queue index to use",
1538 .next = NEXT(action_queue, NEXT_ENTRY(UNSIGNED)),
1539 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_queue, index)),
1540 .call = parse_vc_conf,
1544 .help = "drop packets (note: passthru has priority)",
1545 .priv = PRIV_ACTION(DROP, 0),
1546 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1551 .help = "enable counters for this rule",
1552 .priv = PRIV_ACTION(COUNT, 0),
1553 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1558 .help = "duplicate packets to a given queue index",
1559 .priv = PRIV_ACTION(DUP, sizeof(struct rte_flow_action_dup)),
1560 .next = NEXT(action_dup),
1563 [ACTION_DUP_INDEX] = {
1565 .help = "queue index to duplicate packets to",
1566 .next = NEXT(action_dup, NEXT_ENTRY(UNSIGNED)),
1567 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_dup, index)),
1568 .call = parse_vc_conf,
1572 .help = "spread packets among several queues",
1573 .priv = PRIV_ACTION(RSS, ACTION_RSS_SIZE),
1574 .next = NEXT(action_rss),
1577 [ACTION_RSS_QUEUES] = {
1579 .help = "queue indices to use",
1580 .next = NEXT(action_rss, NEXT_ENTRY(ACTION_RSS_QUEUE)),
1581 .call = parse_vc_conf,
1583 [ACTION_RSS_QUEUE] = {
1585 .help = "queue index",
1586 .call = parse_vc_action_rss_queue,
1587 .comp = comp_vc_action_rss_queue,
1591 .help = "redirect packets to physical device function",
1592 .priv = PRIV_ACTION(PF, 0),
1593 .next = NEXT(NEXT_ENTRY(ACTION_NEXT)),
1598 .help = "redirect packets to virtual device function",
1599 .priv = PRIV_ACTION(VF, sizeof(struct rte_flow_action_vf)),
1600 .next = NEXT(action_vf),
1603 [ACTION_VF_ORIGINAL] = {
1605 .help = "use original VF ID if possible",
1606 .next = NEXT(action_vf, NEXT_ENTRY(BOOLEAN)),
1607 .args = ARGS(ARGS_ENTRY_BF(struct rte_flow_action_vf,
1609 .call = parse_vc_conf,
1613 .help = "VF ID to redirect packets to",
1614 .next = NEXT(action_vf, NEXT_ENTRY(UNSIGNED)),
1615 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_vf, id)),
1616 .call = parse_vc_conf,
1620 .help = "meter the directed packets at given id",
1621 .priv = PRIV_ACTION(METER,
1622 sizeof(struct rte_flow_action_meter)),
1623 .next = NEXT(action_meter),
1626 [ACTION_METER_ID] = {
1628 .help = "meter id to use",
1629 .next = NEXT(action_meter, NEXT_ENTRY(UNSIGNED)),
1630 .args = ARGS(ARGS_ENTRY(struct rte_flow_action_meter, mtr_id)),
1631 .call = parse_vc_conf,
1635 /** Remove and return last entry from argument stack. */
1636 static const struct arg *
1637 pop_args(struct context *ctx)
1639 return ctx->args_num ? ctx->args[--ctx->args_num] : NULL;
1642 /** Add entry on top of the argument stack. */
1644 push_args(struct context *ctx, const struct arg *arg)
1646 if (ctx->args_num == CTX_STACK_SIZE)
1648 ctx->args[ctx->args_num++] = arg;
1652 /** Spread value into buffer according to bit-mask. */
1654 arg_entry_bf_fill(void *dst, uintmax_t val, const struct arg *arg)
1656 uint32_t i = arg->size;
1664 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1673 unsigned int shift = 0;
1674 uint8_t *buf = (uint8_t *)dst + arg->offset + (i -= sub);
1676 for (shift = 0; arg->mask[i] >> shift; ++shift) {
1677 if (!(arg->mask[i] & (1 << shift)))
1682 *buf &= ~(1 << shift);
1683 *buf |= (val & 1) << shift;
1691 /** Compare a string with a partial one of a given length. */
1693 strcmp_partial(const char *full, const char *partial, size_t partial_len)
1695 int r = strncmp(full, partial, partial_len);
1699 if (strlen(full) <= partial_len)
1701 return full[partial_len];
1705 * Parse a prefix length and generate a bit-mask.
1707 * Last argument (ctx->args) is retrieved to determine mask size, storage
1708 * location and whether the result must use network byte ordering.
1711 parse_prefix(struct context *ctx, const struct token *token,
1712 const char *str, unsigned int len,
1713 void *buf, unsigned int size)
1715 const struct arg *arg = pop_args(ctx);
1716 static const uint8_t conv[] = "\x00\x80\xc0\xe0\xf0\xf8\xfc\xfe\xff";
1723 /* Argument is expected. */
1727 u = strtoumax(str, &end, 0);
1728 if (errno || (size_t)(end - str) != len)
1733 extra = arg_entry_bf_fill(NULL, 0, arg);
1742 if (!arg_entry_bf_fill(ctx->object, v, arg) ||
1743 !arg_entry_bf_fill(ctx->objmask, -1, arg))
1750 if (bytes > size || bytes + !!extra > size)
1754 buf = (uint8_t *)ctx->object + arg->offset;
1755 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
1757 memset((uint8_t *)buf + size - bytes, 0xff, bytes);
1758 memset(buf, 0x00, size - bytes);
1760 ((uint8_t *)buf)[size - bytes - 1] = conv[extra];
1764 memset(buf, 0xff, bytes);
1765 memset((uint8_t *)buf + bytes, 0x00, size - bytes);
1767 ((uint8_t *)buf)[bytes] = conv[extra];
1770 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
1773 push_args(ctx, arg);
1777 /** Default parsing function for token name matching. */
1779 parse_default(struct context *ctx, const struct token *token,
1780 const char *str, unsigned int len,
1781 void *buf, unsigned int size)
1786 if (strcmp_partial(token->name, str, len))
1791 /** Parse flow command, initialize output buffer for subsequent tokens. */
1793 parse_init(struct context *ctx, const struct token *token,
1794 const char *str, unsigned int len,
1795 void *buf, unsigned int size)
1797 struct buffer *out = buf;
1799 /* Token name must match. */
1800 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1802 /* Nothing else to do if there is no buffer. */
1805 /* Make sure buffer is large enough. */
1806 if (size < sizeof(*out))
1808 /* Initialize buffer. */
1809 memset(out, 0x00, sizeof(*out));
1810 memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
1813 ctx->objmask = NULL;
1817 /** Parse tokens for validate/create commands. */
1819 parse_vc(struct context *ctx, const struct token *token,
1820 const char *str, unsigned int len,
1821 void *buf, unsigned int size)
1823 struct buffer *out = buf;
1827 /* Token name must match. */
1828 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1830 /* Nothing else to do if there is no buffer. */
1833 if (!out->command) {
1834 if (ctx->curr != VALIDATE && ctx->curr != CREATE)
1836 if (sizeof(*out) > size)
1838 out->command = ctx->curr;
1841 ctx->objmask = NULL;
1842 out->args.vc.data = (uint8_t *)out + size;
1846 ctx->object = &out->args.vc.attr;
1847 ctx->objmask = NULL;
1848 switch (ctx->curr) {
1853 out->args.vc.attr.ingress = 1;
1856 out->args.vc.attr.egress = 1;
1859 out->args.vc.pattern =
1860 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
1862 ctx->object = out->args.vc.pattern;
1863 ctx->objmask = NULL;
1866 out->args.vc.actions =
1867 (void *)RTE_ALIGN_CEIL((uintptr_t)
1868 (out->args.vc.pattern +
1869 out->args.vc.pattern_n),
1871 ctx->object = out->args.vc.actions;
1872 ctx->objmask = NULL;
1879 if (!out->args.vc.actions) {
1880 const struct parse_item_priv *priv = token->priv;
1881 struct rte_flow_item *item =
1882 out->args.vc.pattern + out->args.vc.pattern_n;
1884 data_size = priv->size * 3; /* spec, last, mask */
1885 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
1886 (out->args.vc.data - data_size),
1888 if ((uint8_t *)item + sizeof(*item) > data)
1890 *item = (struct rte_flow_item){
1893 ++out->args.vc.pattern_n;
1895 ctx->objmask = NULL;
1897 const struct parse_action_priv *priv = token->priv;
1898 struct rte_flow_action *action =
1899 out->args.vc.actions + out->args.vc.actions_n;
1901 data_size = priv->size; /* configuration */
1902 data = (void *)RTE_ALIGN_FLOOR((uintptr_t)
1903 (out->args.vc.data - data_size),
1905 if ((uint8_t *)action + sizeof(*action) > data)
1907 *action = (struct rte_flow_action){
1910 ++out->args.vc.actions_n;
1911 ctx->object = action;
1912 ctx->objmask = NULL;
1914 memset(data, 0, data_size);
1915 out->args.vc.data = data;
1916 ctx->objdata = data_size;
1920 /** Parse pattern item parameter type. */
1922 parse_vc_spec(struct context *ctx, const struct token *token,
1923 const char *str, unsigned int len,
1924 void *buf, unsigned int size)
1926 struct buffer *out = buf;
1927 struct rte_flow_item *item;
1933 /* Token name must match. */
1934 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1936 /* Parse parameter types. */
1937 switch (ctx->curr) {
1938 static const enum index prefix[] = NEXT_ENTRY(PREFIX);
1944 case ITEM_PARAM_SPEC:
1947 case ITEM_PARAM_LAST:
1950 case ITEM_PARAM_PREFIX:
1951 /* Modify next token to expect a prefix. */
1952 if (ctx->next_num < 2)
1954 ctx->next[ctx->next_num - 2] = prefix;
1956 case ITEM_PARAM_MASK:
1962 /* Nothing else to do if there is no buffer. */
1965 if (!out->args.vc.pattern_n)
1967 item = &out->args.vc.pattern[out->args.vc.pattern_n - 1];
1968 data_size = ctx->objdata / 3; /* spec, last, mask */
1969 /* Point to selected object. */
1970 ctx->object = out->args.vc.data + (data_size * index);
1972 ctx->objmask = out->args.vc.data + (data_size * 2); /* mask */
1973 item->mask = ctx->objmask;
1975 ctx->objmask = NULL;
1976 /* Update relevant item pointer. */
1977 *((const void **[]){ &item->spec, &item->last, &item->mask })[index] =
1982 /** Parse action configuration field. */
1984 parse_vc_conf(struct context *ctx, const struct token *token,
1985 const char *str, unsigned int len,
1986 void *buf, unsigned int size)
1988 struct buffer *out = buf;
1989 struct rte_flow_action *action;
1992 /* Token name must match. */
1993 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
1995 /* Nothing else to do if there is no buffer. */
1998 if (!out->args.vc.actions_n)
2000 action = &out->args.vc.actions[out->args.vc.actions_n - 1];
2001 /* Point to selected object. */
2002 ctx->object = out->args.vc.data;
2003 ctx->objmask = NULL;
2004 /* Update configuration pointer. */
2005 action->conf = ctx->object;
2010 * Parse queue field for RSS action.
2012 * Valid tokens are queue indices and the "end" token.
2015 parse_vc_action_rss_queue(struct context *ctx, const struct token *token,
2016 const char *str, unsigned int len,
2017 void *buf, unsigned int size)
2019 static const enum index next[] = NEXT_ENTRY(ACTION_RSS_QUEUE);
2026 if (ctx->curr != ACTION_RSS_QUEUE)
2028 i = ctx->objdata >> 16;
2029 if (!strcmp_partial("end", str, len)) {
2030 ctx->objdata &= 0xffff;
2033 if (i >= ACTION_RSS_NUM)
2035 if (push_args(ctx, ARGS_ENTRY(struct rte_flow_action_rss, queue[i])))
2037 ret = parse_int(ctx, token, str, len, NULL, 0);
2043 ctx->objdata = i << 16 | (ctx->objdata & 0xffff);
2045 if (ctx->next_num == RTE_DIM(ctx->next))
2047 ctx->next[ctx->next_num++] = next;
2050 ((struct rte_flow_action_rss *)ctx->object)->num = i;
2054 /** Parse tokens for destroy command. */
2056 parse_destroy(struct context *ctx, const struct token *token,
2057 const char *str, unsigned int len,
2058 void *buf, unsigned int size)
2060 struct buffer *out = buf;
2062 /* Token name must match. */
2063 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2065 /* Nothing else to do if there is no buffer. */
2068 if (!out->command) {
2069 if (ctx->curr != DESTROY)
2071 if (sizeof(*out) > size)
2073 out->command = ctx->curr;
2076 ctx->objmask = NULL;
2077 out->args.destroy.rule =
2078 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
2082 if (((uint8_t *)(out->args.destroy.rule + out->args.destroy.rule_n) +
2083 sizeof(*out->args.destroy.rule)) > (uint8_t *)out + size)
2086 ctx->object = out->args.destroy.rule + out->args.destroy.rule_n++;
2087 ctx->objmask = NULL;
2091 /** Parse tokens for flush command. */
2093 parse_flush(struct context *ctx, const struct token *token,
2094 const char *str, unsigned int len,
2095 void *buf, unsigned int size)
2097 struct buffer *out = buf;
2099 /* Token name must match. */
2100 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2102 /* Nothing else to do if there is no buffer. */
2105 if (!out->command) {
2106 if (ctx->curr != FLUSH)
2108 if (sizeof(*out) > size)
2110 out->command = ctx->curr;
2113 ctx->objmask = NULL;
2118 /** Parse tokens for query command. */
2120 parse_query(struct context *ctx, const struct token *token,
2121 const char *str, unsigned int len,
2122 void *buf, unsigned int size)
2124 struct buffer *out = buf;
2126 /* Token name must match. */
2127 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2129 /* Nothing else to do if there is no buffer. */
2132 if (!out->command) {
2133 if (ctx->curr != QUERY)
2135 if (sizeof(*out) > size)
2137 out->command = ctx->curr;
2140 ctx->objmask = NULL;
2145 /** Parse action names. */
2147 parse_action(struct context *ctx, const struct token *token,
2148 const char *str, unsigned int len,
2149 void *buf, unsigned int size)
2151 struct buffer *out = buf;
2152 const struct arg *arg = pop_args(ctx);
2156 /* Argument is expected. */
2159 /* Parse action name. */
2160 for (i = 0; next_action[i]; ++i) {
2161 const struct parse_action_priv *priv;
2163 token = &token_list[next_action[i]];
2164 if (strcmp_partial(token->name, str, len))
2170 memcpy((uint8_t *)ctx->object + arg->offset,
2176 push_args(ctx, arg);
2180 /** Parse tokens for list command. */
2182 parse_list(struct context *ctx, const struct token *token,
2183 const char *str, unsigned int len,
2184 void *buf, unsigned int size)
2186 struct buffer *out = buf;
2188 /* Token name must match. */
2189 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2191 /* Nothing else to do if there is no buffer. */
2194 if (!out->command) {
2195 if (ctx->curr != LIST)
2197 if (sizeof(*out) > size)
2199 out->command = ctx->curr;
2202 ctx->objmask = NULL;
2203 out->args.list.group =
2204 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
2208 if (((uint8_t *)(out->args.list.group + out->args.list.group_n) +
2209 sizeof(*out->args.list.group)) > (uint8_t *)out + size)
2212 ctx->object = out->args.list.group + out->args.list.group_n++;
2213 ctx->objmask = NULL;
2217 /** Parse tokens for isolate command. */
2219 parse_isolate(struct context *ctx, const struct token *token,
2220 const char *str, unsigned int len,
2221 void *buf, unsigned int size)
2223 struct buffer *out = buf;
2225 /* Token name must match. */
2226 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
2228 /* Nothing else to do if there is no buffer. */
2231 if (!out->command) {
2232 if (ctx->curr != ISOLATE)
2234 if (sizeof(*out) > size)
2236 out->command = ctx->curr;
2239 ctx->objmask = NULL;
2245 * Parse signed/unsigned integers 8 to 64-bit long.
2247 * Last argument (ctx->args) is retrieved to determine integer type and
2251 parse_int(struct context *ctx, const struct token *token,
2252 const char *str, unsigned int len,
2253 void *buf, unsigned int size)
2255 const struct arg *arg = pop_args(ctx);
2260 /* Argument is expected. */
2265 (uintmax_t)strtoimax(str, &end, 0) :
2266 strtoumax(str, &end, 0);
2267 if (errno || (size_t)(end - str) != len)
2272 if (!arg_entry_bf_fill(ctx->object, u, arg) ||
2273 !arg_entry_bf_fill(ctx->objmask, -1, arg))
2277 buf = (uint8_t *)ctx->object + arg->offset;
2281 case sizeof(uint8_t):
2282 *(uint8_t *)buf = u;
2284 case sizeof(uint16_t):
2285 *(uint16_t *)buf = arg->hton ? rte_cpu_to_be_16(u) : u;
2287 case sizeof(uint8_t [3]):
2288 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
2290 ((uint8_t *)buf)[0] = u;
2291 ((uint8_t *)buf)[1] = u >> 8;
2292 ((uint8_t *)buf)[2] = u >> 16;
2296 ((uint8_t *)buf)[0] = u >> 16;
2297 ((uint8_t *)buf)[1] = u >> 8;
2298 ((uint8_t *)buf)[2] = u;
2300 case sizeof(uint32_t):
2301 *(uint32_t *)buf = arg->hton ? rte_cpu_to_be_32(u) : u;
2303 case sizeof(uint64_t):
2304 *(uint64_t *)buf = arg->hton ? rte_cpu_to_be_64(u) : u;
2309 if (ctx->objmask && buf != (uint8_t *)ctx->objmask + arg->offset) {
2311 buf = (uint8_t *)ctx->objmask + arg->offset;
2316 push_args(ctx, arg);
2323 * Two arguments (ctx->args) are retrieved from the stack to store data and
2324 * its length (in that order).
2327 parse_string(struct context *ctx, const struct token *token,
2328 const char *str, unsigned int len,
2329 void *buf, unsigned int size)
2331 const struct arg *arg_data = pop_args(ctx);
2332 const struct arg *arg_len = pop_args(ctx);
2333 char tmp[16]; /* Ought to be enough. */
2336 /* Arguments are expected. */
2340 push_args(ctx, arg_data);
2343 size = arg_data->size;
2344 /* Bit-mask fill is not supported. */
2345 if (arg_data->mask || size < len)
2349 /* Let parse_int() fill length information first. */
2350 ret = snprintf(tmp, sizeof(tmp), "%u", len);
2353 push_args(ctx, arg_len);
2354 ret = parse_int(ctx, token, tmp, ret, NULL, 0);
2359 buf = (uint8_t *)ctx->object + arg_data->offset;
2360 /* Output buffer is not necessarily NUL-terminated. */
2361 memcpy(buf, str, len);
2362 memset((uint8_t *)buf + len, 0x55, size - len);
2364 memset((uint8_t *)ctx->objmask + arg_data->offset, 0xff, len);
2367 push_args(ctx, arg_len);
2368 push_args(ctx, arg_data);
2373 * Parse a MAC address.
2375 * Last argument (ctx->args) is retrieved to determine storage size and
2379 parse_mac_addr(struct context *ctx, const struct token *token,
2380 const char *str, unsigned int len,
2381 void *buf, unsigned int size)
2383 const struct arg *arg = pop_args(ctx);
2384 struct ether_addr tmp;
2388 /* Argument is expected. */
2392 /* Bit-mask fill is not supported. */
2393 if (arg->mask || size != sizeof(tmp))
2395 /* Only network endian is supported. */
2398 ret = cmdline_parse_etheraddr(NULL, str, &tmp, size);
2399 if (ret < 0 || (unsigned int)ret != len)
2403 buf = (uint8_t *)ctx->object + arg->offset;
2404 memcpy(buf, &tmp, size);
2406 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2409 push_args(ctx, arg);
2414 * Parse an IPv4 address.
2416 * Last argument (ctx->args) is retrieved to determine storage size and
2420 parse_ipv4_addr(struct context *ctx, const struct token *token,
2421 const char *str, unsigned int len,
2422 void *buf, unsigned int size)
2424 const struct arg *arg = pop_args(ctx);
2429 /* Argument is expected. */
2433 /* Bit-mask fill is not supported. */
2434 if (arg->mask || size != sizeof(tmp))
2436 /* Only network endian is supported. */
2439 memcpy(str2, str, len);
2441 ret = inet_pton(AF_INET, str2, &tmp);
2443 /* Attempt integer parsing. */
2444 push_args(ctx, arg);
2445 return parse_int(ctx, token, str, len, buf, size);
2449 buf = (uint8_t *)ctx->object + arg->offset;
2450 memcpy(buf, &tmp, size);
2452 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2455 push_args(ctx, arg);
2460 * Parse an IPv6 address.
2462 * Last argument (ctx->args) is retrieved to determine storage size and
2466 parse_ipv6_addr(struct context *ctx, const struct token *token,
2467 const char *str, unsigned int len,
2468 void *buf, unsigned int size)
2470 const struct arg *arg = pop_args(ctx);
2472 struct in6_addr tmp;
2476 /* Argument is expected. */
2480 /* Bit-mask fill is not supported. */
2481 if (arg->mask || size != sizeof(tmp))
2483 /* Only network endian is supported. */
2486 memcpy(str2, str, len);
2488 ret = inet_pton(AF_INET6, str2, &tmp);
2493 buf = (uint8_t *)ctx->object + arg->offset;
2494 memcpy(buf, &tmp, size);
2496 memset((uint8_t *)ctx->objmask + arg->offset, 0xff, size);
2499 push_args(ctx, arg);
2503 /** Boolean values (even indices stand for false). */
2504 static const char *const boolean_name[] = {
2513 * Parse a boolean value.
2515 * Last argument (ctx->args) is retrieved to determine storage size and
2519 parse_boolean(struct context *ctx, const struct token *token,
2520 const char *str, unsigned int len,
2521 void *buf, unsigned int size)
2523 const struct arg *arg = pop_args(ctx);
2527 /* Argument is expected. */
2530 for (i = 0; boolean_name[i]; ++i)
2531 if (!strcmp_partial(boolean_name[i], str, len))
2533 /* Process token as integer. */
2534 if (boolean_name[i])
2535 str = i & 1 ? "1" : "0";
2536 push_args(ctx, arg);
2537 ret = parse_int(ctx, token, str, strlen(str), buf, size);
2538 return ret > 0 ? (int)len : ret;
2541 /** Parse port and update context. */
2543 parse_port(struct context *ctx, const struct token *token,
2544 const char *str, unsigned int len,
2545 void *buf, unsigned int size)
2547 struct buffer *out = &(struct buffer){ .port = 0 };
2555 ctx->objmask = NULL;
2556 size = sizeof(*out);
2558 ret = parse_int(ctx, token, str, len, out, size);
2560 ctx->port = out->port;
2566 /** No completion. */
2568 comp_none(struct context *ctx, const struct token *token,
2569 unsigned int ent, char *buf, unsigned int size)
2579 /** Complete boolean values. */
2581 comp_boolean(struct context *ctx, const struct token *token,
2582 unsigned int ent, char *buf, unsigned int size)
2588 for (i = 0; boolean_name[i]; ++i)
2589 if (buf && i == ent)
2590 return snprintf(buf, size, "%s", boolean_name[i]);
2596 /** Complete action names. */
2598 comp_action(struct context *ctx, const struct token *token,
2599 unsigned int ent, char *buf, unsigned int size)
2605 for (i = 0; next_action[i]; ++i)
2606 if (buf && i == ent)
2607 return snprintf(buf, size, "%s",
2608 token_list[next_action[i]].name);
2614 /** Complete available ports. */
2616 comp_port(struct context *ctx, const struct token *token,
2617 unsigned int ent, char *buf, unsigned int size)
2624 RTE_ETH_FOREACH_DEV(p) {
2625 if (buf && i == ent)
2626 return snprintf(buf, size, "%u", p);
2634 /** Complete available rule IDs. */
2636 comp_rule_id(struct context *ctx, const struct token *token,
2637 unsigned int ent, char *buf, unsigned int size)
2640 struct rte_port *port;
2641 struct port_flow *pf;
2644 if (port_id_is_invalid(ctx->port, DISABLED_WARN) ||
2645 ctx->port == (portid_t)RTE_PORT_ALL)
2647 port = &ports[ctx->port];
2648 for (pf = port->flow_list; pf != NULL; pf = pf->next) {
2649 if (buf && i == ent)
2650 return snprintf(buf, size, "%u", pf->id);
2658 /** Complete queue field for RSS action. */
2660 comp_vc_action_rss_queue(struct context *ctx, const struct token *token,
2661 unsigned int ent, char *buf, unsigned int size)
2663 static const char *const str[] = { "", "end", NULL };
2668 for (i = 0; str[i] != NULL; ++i)
2669 if (buf && i == ent)
2670 return snprintf(buf, size, "%s", str[i]);
2676 /** Internal context. */
2677 static struct context cmd_flow_context;
2679 /** Global parser instance (cmdline API). */
2680 cmdline_parse_inst_t cmd_flow;
2682 /** Initialize context. */
2684 cmd_flow_context_init(struct context *ctx)
2686 /* A full memset() is not necessary. */
2696 ctx->objmask = NULL;
2699 /** Parse a token (cmdline API). */
2701 cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result,
2704 struct context *ctx = &cmd_flow_context;
2705 const struct token *token;
2706 const enum index *list;
2711 token = &token_list[ctx->curr];
2712 /* Check argument length. */
2715 for (len = 0; src[len]; ++len)
2716 if (src[len] == '#' || isspace(src[len]))
2720 /* Last argument and EOL detection. */
2721 for (i = len; src[i]; ++i)
2722 if (src[i] == '#' || src[i] == '\r' || src[i] == '\n')
2724 else if (!isspace(src[i])) {
2729 if (src[i] == '\r' || src[i] == '\n') {
2733 /* Initialize context if necessary. */
2734 if (!ctx->next_num) {
2737 ctx->next[ctx->next_num++] = token->next[0];
2739 /* Process argument through candidates. */
2740 ctx->prev = ctx->curr;
2741 list = ctx->next[ctx->next_num - 1];
2742 for (i = 0; list[i]; ++i) {
2743 const struct token *next = &token_list[list[i]];
2746 ctx->curr = list[i];
2748 tmp = next->call(ctx, next, src, len, result, size);
2750 tmp = parse_default(ctx, next, src, len, result, size);
2751 if (tmp == -1 || tmp != len)
2759 /* Push subsequent tokens if any. */
2761 for (i = 0; token->next[i]; ++i) {
2762 if (ctx->next_num == RTE_DIM(ctx->next))
2764 ctx->next[ctx->next_num++] = token->next[i];
2766 /* Push arguments if any. */
2768 for (i = 0; token->args[i]; ++i) {
2769 if (ctx->args_num == RTE_DIM(ctx->args))
2771 ctx->args[ctx->args_num++] = token->args[i];
2776 /** Return number of completion entries (cmdline API). */
2778 cmd_flow_complete_get_nb(cmdline_parse_token_hdr_t *hdr)
2780 struct context *ctx = &cmd_flow_context;
2781 const struct token *token = &token_list[ctx->curr];
2782 const enum index *list;
2786 /* Count number of tokens in current list. */
2788 list = ctx->next[ctx->next_num - 1];
2790 list = token->next[0];
2791 for (i = 0; list[i]; ++i)
2796 * If there is a single token, use its completion callback, otherwise
2797 * return the number of entries.
2799 token = &token_list[list[0]];
2800 if (i == 1 && token->comp) {
2801 /* Save index for cmd_flow_get_help(). */
2802 ctx->prev = list[0];
2803 return token->comp(ctx, token, 0, NULL, 0);
2808 /** Return a completion entry (cmdline API). */
2810 cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,
2811 char *dst, unsigned int size)
2813 struct context *ctx = &cmd_flow_context;
2814 const struct token *token = &token_list[ctx->curr];
2815 const enum index *list;
2819 /* Count number of tokens in current list. */
2821 list = ctx->next[ctx->next_num - 1];
2823 list = token->next[0];
2824 for (i = 0; list[i]; ++i)
2828 /* If there is a single token, use its completion callback. */
2829 token = &token_list[list[0]];
2830 if (i == 1 && token->comp) {
2831 /* Save index for cmd_flow_get_help(). */
2832 ctx->prev = list[0];
2833 return token->comp(ctx, token, index, dst, size) < 0 ? -1 : 0;
2835 /* Otherwise make sure the index is valid and use defaults. */
2838 token = &token_list[list[index]];
2839 snprintf(dst, size, "%s", token->name);
2840 /* Save index for cmd_flow_get_help(). */
2841 ctx->prev = list[index];
2845 /** Populate help strings for current token (cmdline API). */
2847 cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
2849 struct context *ctx = &cmd_flow_context;
2850 const struct token *token = &token_list[ctx->prev];
2855 /* Set token type and update global help with details. */
2856 snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
2858 cmd_flow.help_str = token->help;
2860 cmd_flow.help_str = token->name;
2864 /** Token definition template (cmdline API). */
2865 static struct cmdline_token_hdr cmd_flow_token_hdr = {
2866 .ops = &(struct cmdline_token_ops){
2867 .parse = cmd_flow_parse,
2868 .complete_get_nb = cmd_flow_complete_get_nb,
2869 .complete_get_elt = cmd_flow_complete_get_elt,
2870 .get_help = cmd_flow_get_help,
2875 /** Populate the next dynamic token. */
2877 cmd_flow_tok(cmdline_parse_token_hdr_t **hdr,
2878 cmdline_parse_token_hdr_t **hdr_inst)
2880 struct context *ctx = &cmd_flow_context;
2882 /* Always reinitialize context before requesting the first token. */
2883 if (!(hdr_inst - cmd_flow.tokens))
2884 cmd_flow_context_init(ctx);
2885 /* Return NULL when no more tokens are expected. */
2886 if (!ctx->next_num && ctx->curr) {
2890 /* Determine if command should end here. */
2891 if (ctx->eol && ctx->last && ctx->next_num) {
2892 const enum index *list = ctx->next[ctx->next_num - 1];
2895 for (i = 0; list[i]; ++i) {
2902 *hdr = &cmd_flow_token_hdr;
2905 /** Dispatch parsed buffer to function calls. */
2907 cmd_flow_parsed(const struct buffer *in)
2909 switch (in->command) {
2911 port_flow_validate(in->port, &in->args.vc.attr,
2912 in->args.vc.pattern, in->args.vc.actions);
2915 port_flow_create(in->port, &in->args.vc.attr,
2916 in->args.vc.pattern, in->args.vc.actions);
2919 port_flow_destroy(in->port, in->args.destroy.rule_n,
2920 in->args.destroy.rule);
2923 port_flow_flush(in->port);
2926 port_flow_query(in->port, in->args.query.rule,
2927 in->args.query.action);
2930 port_flow_list(in->port, in->args.list.group_n,
2931 in->args.list.group);
2934 port_flow_isolate(in->port, in->args.isolate.set);
2941 /** Token generator and output processing callback (cmdline API). */
2943 cmd_flow_cb(void *arg0, struct cmdline *cl, void *arg2)
2946 cmd_flow_tok(arg0, arg2);
2948 cmd_flow_parsed(arg0);
2951 /** Global parser instance (cmdline API). */
2952 cmdline_parse_inst_t cmd_flow = {
2954 .data = NULL, /**< Unused. */
2955 .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
2958 }, /**< Tokens are returned by cmd_flow_tok(). */