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.
42 #include <rte_common.h>
43 #include <rte_ethdev.h>
44 #include <rte_byteorder.h>
45 #include <cmdline_parse.h>
50 /** Parser token indices. */
63 /* Top-level command. */
66 /* Sub-level commands. */
71 /* Destroy arguments. */
78 /** Maximum number of subsequent tokens and arguments on the stack. */
79 #define CTX_STACK_SIZE 16
81 /** Parser context. */
83 /** Stack of subsequent token lists to process. */
84 const enum index *next[CTX_STACK_SIZE];
85 /** Arguments for stacked tokens. */
86 const void *args[CTX_STACK_SIZE];
87 enum index curr; /**< Current token index. */
88 enum index prev; /**< Index of the last token seen. */
89 int next_num; /**< Number of entries in next[]. */
90 int args_num; /**< Number of entries in args[]. */
91 uint32_t reparse:1; /**< Start over from the beginning. */
92 uint32_t eol:1; /**< EOL has been detected. */
93 uint32_t last:1; /**< No more arguments. */
94 uint16_t port; /**< Current port ID (for completions). */
95 void *object; /**< Address of current object for relative offsets. */
98 /** Token argument. */
100 uint32_t hton:1; /**< Use network byte ordering. */
101 uint32_t sign:1; /**< Value is signed. */
102 uint32_t offset; /**< Relative offset from ctx->object. */
103 uint32_t size; /**< Field size. */
106 /** Parser token definition. */
108 /** Type displayed during completion (defaults to "TOKEN"). */
110 /** Help displayed during completion (defaults to token name). */
113 * Lists of subsequent tokens to push on the stack. Each call to the
114 * parser consumes the last entry of that stack.
116 const enum index *const *next;
117 /** Arguments stack for subsequent tokens that need them. */
118 const struct arg *const *args;
120 * Token-processing callback, returns -1 in case of error, the
121 * length of the matched string otherwise. If NULL, attempts to
122 * match the token name.
124 * If buf is not NULL, the result should be stored in it according
125 * to context. An error is returned if not large enough.
127 int (*call)(struct context *ctx, const struct token *token,
128 const char *str, unsigned int len,
129 void *buf, unsigned int size);
131 * Callback that provides possible values for this token, used for
132 * completion. Returns -1 in case of error, the number of possible
133 * values otherwise. If NULL, the token name is used.
135 * If buf is not NULL, entry index ent is written to buf and the
136 * full length of the entry is returned (same behavior as
139 int (*comp)(struct context *ctx, const struct token *token,
140 unsigned int ent, char *buf, unsigned int size);
141 /** Mandatory token name, no default value. */
145 /** Static initializer for the next field. */
146 #define NEXT(...) (const enum index *const []){ __VA_ARGS__, NULL, }
148 /** Static initializer for a NEXT() entry. */
149 #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, }
151 /** Static initializer for the args field. */
152 #define ARGS(...) (const struct arg *const []){ __VA_ARGS__, NULL, }
154 /** Static initializer for ARGS() to target a field. */
155 #define ARGS_ENTRY(s, f) \
156 (&(const struct arg){ \
157 .offset = offsetof(s, f), \
158 .size = sizeof(((s *)0)->f), \
161 /** Static initializer for ARGS() to target a pointer. */
162 #define ARGS_ENTRY_PTR(s, f) \
163 (&(const struct arg){ \
164 .size = sizeof(*((s *)0)->f), \
167 /** Parser output buffer layout expected by cmd_flow_parsed(). */
169 enum index command; /**< Flow command. */
170 uint16_t port; /**< Affected port ID. */
175 } destroy; /**< Destroy arguments. */
179 } list; /**< List arguments. */
180 } args; /**< Command arguments. */
183 static const enum index next_destroy_attr[] = {
189 static const enum index next_list_attr[] = {
195 static int parse_init(struct context *, const struct token *,
196 const char *, unsigned int,
197 void *, unsigned int);
198 static int parse_destroy(struct context *, const struct token *,
199 const char *, unsigned int,
200 void *, unsigned int);
201 static int parse_flush(struct context *, const struct token *,
202 const char *, unsigned int,
203 void *, unsigned int);
204 static int parse_list(struct context *, const struct token *,
205 const char *, unsigned int,
206 void *, unsigned int);
207 static int parse_int(struct context *, const struct token *,
208 const char *, unsigned int,
209 void *, unsigned int);
210 static int parse_port(struct context *, const struct token *,
211 const char *, unsigned int,
212 void *, unsigned int);
213 static int comp_none(struct context *, const struct token *,
214 unsigned int, char *, unsigned int);
215 static int comp_port(struct context *, const struct token *,
216 unsigned int, char *, unsigned int);
217 static int comp_rule_id(struct context *, const struct token *,
218 unsigned int, char *, unsigned int);
220 /** Token definitions. */
221 static const struct token token_list[] = {
222 /* Special tokens. */
225 .help = "null entry, abused as the entry point",
226 .next = NEXT(NEXT_ENTRY(FLOW)),
231 .help = "command may end here",
237 .help = "integer value",
242 .name = "{unsigned}",
244 .help = "unsigned integer value",
251 .help = "rule identifier",
253 .comp = comp_rule_id,
258 .help = "port identifier",
263 .name = "{group_id}",
265 .help = "group identifier",
269 /* Top-level command. */
272 .type = "{command} {port_id} [{arg} [...]]",
273 .help = "manage ingress/egress flow rules",
274 .next = NEXT(NEXT_ENTRY
280 /* Sub-level commands. */
283 .help = "destroy specific flow rules",
284 .next = NEXT(NEXT_ENTRY(DESTROY_RULE), NEXT_ENTRY(PORT_ID)),
285 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
286 .call = parse_destroy,
290 .help = "destroy all flow rules",
291 .next = NEXT(NEXT_ENTRY(PORT_ID)),
292 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
297 .help = "list existing flow rules",
298 .next = NEXT(next_list_attr, NEXT_ENTRY(PORT_ID)),
299 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
302 /* Destroy arguments. */
305 .help = "specify a rule identifier",
306 .next = NEXT(next_destroy_attr, NEXT_ENTRY(RULE_ID)),
307 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.destroy.rule)),
308 .call = parse_destroy,
310 /* List arguments. */
313 .help = "specify a group",
314 .next = NEXT(next_list_attr, NEXT_ENTRY(GROUP_ID)),
315 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.list.group)),
320 /** Remove and return last entry from argument stack. */
321 static const struct arg *
322 pop_args(struct context *ctx)
324 return ctx->args_num ? ctx->args[--ctx->args_num] : NULL;
327 /** Add entry on top of the argument stack. */
329 push_args(struct context *ctx, const struct arg *arg)
331 if (ctx->args_num == CTX_STACK_SIZE)
333 ctx->args[ctx->args_num++] = arg;
337 /** Default parsing function for token name matching. */
339 parse_default(struct context *ctx, const struct token *token,
340 const char *str, unsigned int len,
341 void *buf, unsigned int size)
346 if (strncmp(str, token->name, len))
351 /** Parse flow command, initialize output buffer for subsequent tokens. */
353 parse_init(struct context *ctx, const struct token *token,
354 const char *str, unsigned int len,
355 void *buf, unsigned int size)
357 struct buffer *out = buf;
359 /* Token name must match. */
360 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
362 /* Nothing else to do if there is no buffer. */
365 /* Make sure buffer is large enough. */
366 if (size < sizeof(*out))
368 /* Initialize buffer. */
369 memset(out, 0x00, sizeof(*out));
370 memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
375 /** Parse tokens for destroy command. */
377 parse_destroy(struct context *ctx, const struct token *token,
378 const char *str, unsigned int len,
379 void *buf, unsigned int size)
381 struct buffer *out = buf;
383 /* Token name must match. */
384 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
386 /* Nothing else to do if there is no buffer. */
390 if (ctx->curr != DESTROY)
392 if (sizeof(*out) > size)
394 out->command = ctx->curr;
396 out->args.destroy.rule =
397 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
401 if (((uint8_t *)(out->args.destroy.rule + out->args.destroy.rule_n) +
402 sizeof(*out->args.destroy.rule)) > (uint8_t *)out + size)
404 ctx->object = out->args.destroy.rule + out->args.destroy.rule_n++;
408 /** Parse tokens for flush command. */
410 parse_flush(struct context *ctx, const struct token *token,
411 const char *str, unsigned int len,
412 void *buf, unsigned int size)
414 struct buffer *out = buf;
416 /* Token name must match. */
417 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
419 /* Nothing else to do if there is no buffer. */
423 if (ctx->curr != FLUSH)
425 if (sizeof(*out) > size)
427 out->command = ctx->curr;
433 /** Parse tokens for list command. */
435 parse_list(struct context *ctx, const struct token *token,
436 const char *str, unsigned int len,
437 void *buf, unsigned int size)
439 struct buffer *out = buf;
441 /* Token name must match. */
442 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
444 /* Nothing else to do if there is no buffer. */
448 if (ctx->curr != LIST)
450 if (sizeof(*out) > size)
452 out->command = ctx->curr;
454 out->args.list.group =
455 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
459 if (((uint8_t *)(out->args.list.group + out->args.list.group_n) +
460 sizeof(*out->args.list.group)) > (uint8_t *)out + size)
462 ctx->object = out->args.list.group + out->args.list.group_n++;
467 * Parse signed/unsigned integers 8 to 64-bit long.
469 * Last argument (ctx->args) is retrieved to determine integer type and
473 parse_int(struct context *ctx, const struct token *token,
474 const char *str, unsigned int len,
475 void *buf, unsigned int size)
477 const struct arg *arg = pop_args(ctx);
482 /* Argument is expected. */
487 (uintmax_t)strtoimax(str, &end, 0) :
488 strtoumax(str, &end, 0);
489 if (errno || (size_t)(end - str) != len)
493 buf = (uint8_t *)ctx->object + arg->offset;
496 case sizeof(uint8_t):
499 case sizeof(uint16_t):
500 *(uint16_t *)buf = arg->hton ? rte_cpu_to_be_16(u) : u;
502 case sizeof(uint32_t):
503 *(uint32_t *)buf = arg->hton ? rte_cpu_to_be_32(u) : u;
505 case sizeof(uint64_t):
506 *(uint64_t *)buf = arg->hton ? rte_cpu_to_be_64(u) : u;
517 /** Parse port and update context. */
519 parse_port(struct context *ctx, const struct token *token,
520 const char *str, unsigned int len,
521 void *buf, unsigned int size)
523 struct buffer *out = &(struct buffer){ .port = 0 };
532 ret = parse_int(ctx, token, str, len, out, size);
534 ctx->port = out->port;
540 /** No completion. */
542 comp_none(struct context *ctx, const struct token *token,
543 unsigned int ent, char *buf, unsigned int size)
553 /** Complete available ports. */
555 comp_port(struct context *ctx, const struct token *token,
556 unsigned int ent, char *buf, unsigned int size)
563 FOREACH_PORT(p, ports) {
565 return snprintf(buf, size, "%u", p);
573 /** Complete available rule IDs. */
575 comp_rule_id(struct context *ctx, const struct token *token,
576 unsigned int ent, char *buf, unsigned int size)
579 struct rte_port *port;
580 struct port_flow *pf;
583 if (port_id_is_invalid(ctx->port, DISABLED_WARN) ||
584 ctx->port == (uint16_t)RTE_PORT_ALL)
586 port = &ports[ctx->port];
587 for (pf = port->flow_list; pf != NULL; pf = pf->next) {
589 return snprintf(buf, size, "%u", pf->id);
597 /** Internal context. */
598 static struct context cmd_flow_context;
600 /** Global parser instance (cmdline API). */
601 cmdline_parse_inst_t cmd_flow;
603 /** Initialize context. */
605 cmd_flow_context_init(struct context *ctx)
607 /* A full memset() is not necessary. */
619 /** Parse a token (cmdline API). */
621 cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result,
624 struct context *ctx = &cmd_flow_context;
625 const struct token *token;
626 const enum index *list;
631 /* Restart as requested. */
633 cmd_flow_context_init(ctx);
634 token = &token_list[ctx->curr];
635 /* Check argument length. */
638 for (len = 0; src[len]; ++len)
639 if (src[len] == '#' || isspace(src[len]))
643 /* Last argument and EOL detection. */
644 for (i = len; src[i]; ++i)
645 if (src[i] == '#' || src[i] == '\r' || src[i] == '\n')
647 else if (!isspace(src[i])) {
652 if (src[i] == '\r' || src[i] == '\n') {
656 /* Initialize context if necessary. */
657 if (!ctx->next_num) {
660 ctx->next[ctx->next_num++] = token->next[0];
662 /* Process argument through candidates. */
663 ctx->prev = ctx->curr;
664 list = ctx->next[ctx->next_num - 1];
665 for (i = 0; list[i]; ++i) {
666 const struct token *next = &token_list[list[i]];
671 tmp = next->call(ctx, next, src, len, result, size);
673 tmp = parse_default(ctx, next, src, len, result, size);
674 if (tmp == -1 || tmp != len)
682 /* Push subsequent tokens if any. */
684 for (i = 0; token->next[i]; ++i) {
685 if (ctx->next_num == RTE_DIM(ctx->next))
687 ctx->next[ctx->next_num++] = token->next[i];
689 /* Push arguments if any. */
691 for (i = 0; token->args[i]; ++i) {
692 if (ctx->args_num == RTE_DIM(ctx->args))
694 ctx->args[ctx->args_num++] = token->args[i];
699 /** Return number of completion entries (cmdline API). */
701 cmd_flow_complete_get_nb(cmdline_parse_token_hdr_t *hdr)
703 struct context *ctx = &cmd_flow_context;
704 const struct token *token = &token_list[ctx->curr];
705 const enum index *list;
709 /* Tell cmd_flow_parse() that context must be reinitialized. */
711 /* Count number of tokens in current list. */
713 list = ctx->next[ctx->next_num - 1];
715 list = token->next[0];
716 for (i = 0; list[i]; ++i)
721 * If there is a single token, use its completion callback, otherwise
722 * return the number of entries.
724 token = &token_list[list[0]];
725 if (i == 1 && token->comp) {
726 /* Save index for cmd_flow_get_help(). */
728 return token->comp(ctx, token, 0, NULL, 0);
733 /** Return a completion entry (cmdline API). */
735 cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,
736 char *dst, unsigned int size)
738 struct context *ctx = &cmd_flow_context;
739 const struct token *token = &token_list[ctx->curr];
740 const enum index *list;
744 /* Tell cmd_flow_parse() that context must be reinitialized. */
746 /* Count number of tokens in current list. */
748 list = ctx->next[ctx->next_num - 1];
750 list = token->next[0];
751 for (i = 0; list[i]; ++i)
755 /* If there is a single token, use its completion callback. */
756 token = &token_list[list[0]];
757 if (i == 1 && token->comp) {
758 /* Save index for cmd_flow_get_help(). */
760 return token->comp(ctx, token, index, dst, size) < 0 ? -1 : 0;
762 /* Otherwise make sure the index is valid and use defaults. */
765 token = &token_list[list[index]];
766 snprintf(dst, size, "%s", token->name);
767 /* Save index for cmd_flow_get_help(). */
768 ctx->prev = list[index];
772 /** Populate help strings for current token (cmdline API). */
774 cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
776 struct context *ctx = &cmd_flow_context;
777 const struct token *token = &token_list[ctx->prev];
780 /* Tell cmd_flow_parse() that context must be reinitialized. */
784 /* Set token type and update global help with details. */
785 snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
787 cmd_flow.help_str = token->help;
789 cmd_flow.help_str = token->name;
793 /** Token definition template (cmdline API). */
794 static struct cmdline_token_hdr cmd_flow_token_hdr = {
795 .ops = &(struct cmdline_token_ops){
796 .parse = cmd_flow_parse,
797 .complete_get_nb = cmd_flow_complete_get_nb,
798 .complete_get_elt = cmd_flow_complete_get_elt,
799 .get_help = cmd_flow_get_help,
804 /** Populate the next dynamic token. */
806 cmd_flow_tok(cmdline_parse_token_hdr_t **hdr,
807 cmdline_parse_token_hdr_t *(*hdrs)[])
809 struct context *ctx = &cmd_flow_context;
811 /* Always reinitialize context before requesting the first token. */
813 cmd_flow_context_init(ctx);
814 /* Return NULL when no more tokens are expected. */
815 if (!ctx->next_num && ctx->curr) {
819 /* Determine if command should end here. */
820 if (ctx->eol && ctx->last && ctx->next_num) {
821 const enum index *list = ctx->next[ctx->next_num - 1];
824 for (i = 0; list[i]; ++i) {
831 *hdr = &cmd_flow_token_hdr;
834 /** Dispatch parsed buffer to function calls. */
836 cmd_flow_parsed(const struct buffer *in)
838 switch (in->command) {
840 port_flow_destroy(in->port, in->args.destroy.rule_n,
841 in->args.destroy.rule);
844 port_flow_flush(in->port);
847 port_flow_list(in->port, in->args.list.group_n,
848 in->args.list.group);
855 /** Token generator and output processing callback (cmdline API). */
857 cmd_flow_cb(void *arg0, struct cmdline *cl, void *arg2)
860 cmd_flow_tok(arg0, arg2);
862 cmd_flow_parsed(arg0);
865 /** Global parser instance (cmdline API). */
866 cmdline_parse_inst_t cmd_flow = {
868 .data = NULL, /**< Unused. */
869 .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
872 }, /**< Tokens are returned by cmd_flow_tok(). */