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. */
62 /* Top-level command. */
65 /* Sub-level commands. */
72 /** Maximum number of subsequent tokens and arguments on the stack. */
73 #define CTX_STACK_SIZE 16
75 /** Parser context. */
77 /** Stack of subsequent token lists to process. */
78 const enum index *next[CTX_STACK_SIZE];
79 /** Arguments for stacked tokens. */
80 const void *args[CTX_STACK_SIZE];
81 enum index curr; /**< Current token index. */
82 enum index prev; /**< Index of the last token seen. */
83 int next_num; /**< Number of entries in next[]. */
84 int args_num; /**< Number of entries in args[]. */
85 uint32_t reparse:1; /**< Start over from the beginning. */
86 uint32_t eol:1; /**< EOL has been detected. */
87 uint32_t last:1; /**< No more arguments. */
88 uint16_t port; /**< Current port ID (for completions). */
89 void *object; /**< Address of current object for relative offsets. */
92 /** Token argument. */
94 uint32_t hton:1; /**< Use network byte ordering. */
95 uint32_t sign:1; /**< Value is signed. */
96 uint32_t offset; /**< Relative offset from ctx->object. */
97 uint32_t size; /**< Field size. */
100 /** Parser token definition. */
102 /** Type displayed during completion (defaults to "TOKEN"). */
104 /** Help displayed during completion (defaults to token name). */
107 * Lists of subsequent tokens to push on the stack. Each call to the
108 * parser consumes the last entry of that stack.
110 const enum index *const *next;
111 /** Arguments stack for subsequent tokens that need them. */
112 const struct arg *const *args;
114 * Token-processing callback, returns -1 in case of error, the
115 * length of the matched string otherwise. If NULL, attempts to
116 * match the token name.
118 * If buf is not NULL, the result should be stored in it according
119 * to context. An error is returned if not large enough.
121 int (*call)(struct context *ctx, const struct token *token,
122 const char *str, unsigned int len,
123 void *buf, unsigned int size);
125 * Callback that provides possible values for this token, used for
126 * completion. Returns -1 in case of error, the number of possible
127 * values otherwise. If NULL, the token name is used.
129 * If buf is not NULL, entry index ent is written to buf and the
130 * full length of the entry is returned (same behavior as
133 int (*comp)(struct context *ctx, const struct token *token,
134 unsigned int ent, char *buf, unsigned int size);
135 /** Mandatory token name, no default value. */
139 /** Static initializer for the next field. */
140 #define NEXT(...) (const enum index *const []){ __VA_ARGS__, NULL, }
142 /** Static initializer for a NEXT() entry. */
143 #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, }
145 /** Static initializer for the args field. */
146 #define ARGS(...) (const struct arg *const []){ __VA_ARGS__, NULL, }
148 /** Static initializer for ARGS() to target a field. */
149 #define ARGS_ENTRY(s, f) \
150 (&(const struct arg){ \
151 .offset = offsetof(s, f), \
152 .size = sizeof(((s *)0)->f), \
155 /** Static initializer for ARGS() to target a pointer. */
156 #define ARGS_ENTRY_PTR(s, f) \
157 (&(const struct arg){ \
158 .size = sizeof(*((s *)0)->f), \
161 /** Parser output buffer layout expected by cmd_flow_parsed(). */
163 enum index command; /**< Flow command. */
164 uint16_t port; /**< Affected port ID. */
169 } list; /**< List arguments. */
170 } args; /**< Command arguments. */
173 static const enum index next_list_attr[] = {
179 static int parse_init(struct context *, const struct token *,
180 const char *, unsigned int,
181 void *, unsigned int);
182 static int parse_list(struct context *, const struct token *,
183 const char *, unsigned int,
184 void *, unsigned int);
185 static int parse_int(struct context *, const struct token *,
186 const char *, unsigned int,
187 void *, unsigned int);
188 static int parse_port(struct context *, const struct token *,
189 const char *, unsigned int,
190 void *, unsigned int);
191 static int comp_none(struct context *, const struct token *,
192 unsigned int, char *, unsigned int);
193 static int comp_port(struct context *, const struct token *,
194 unsigned int, char *, unsigned int);
196 /** Token definitions. */
197 static const struct token token_list[] = {
198 /* Special tokens. */
201 .help = "null entry, abused as the entry point",
202 .next = NEXT(NEXT_ENTRY(FLOW)),
207 .help = "command may end here",
213 .help = "integer value",
218 .name = "{unsigned}",
220 .help = "unsigned integer value",
227 .help = "port identifier",
232 .name = "{group_id}",
234 .help = "group identifier",
238 /* Top-level command. */
241 .type = "{command} {port_id} [{arg} [...]]",
242 .help = "manage ingress/egress flow rules",
243 .next = NEXT(NEXT_ENTRY(LIST)),
246 /* Sub-level commands. */
249 .help = "list existing flow rules",
250 .next = NEXT(next_list_attr, NEXT_ENTRY(PORT_ID)),
251 .args = ARGS(ARGS_ENTRY(struct buffer, port)),
254 /* List arguments. */
257 .help = "specify a group",
258 .next = NEXT(next_list_attr, NEXT_ENTRY(GROUP_ID)),
259 .args = ARGS(ARGS_ENTRY_PTR(struct buffer, args.list.group)),
264 /** Remove and return last entry from argument stack. */
265 static const struct arg *
266 pop_args(struct context *ctx)
268 return ctx->args_num ? ctx->args[--ctx->args_num] : NULL;
271 /** Add entry on top of the argument stack. */
273 push_args(struct context *ctx, const struct arg *arg)
275 if (ctx->args_num == CTX_STACK_SIZE)
277 ctx->args[ctx->args_num++] = arg;
281 /** Default parsing function for token name matching. */
283 parse_default(struct context *ctx, const struct token *token,
284 const char *str, unsigned int len,
285 void *buf, unsigned int size)
290 if (strncmp(str, token->name, len))
295 /** Parse flow command, initialize output buffer for subsequent tokens. */
297 parse_init(struct context *ctx, const struct token *token,
298 const char *str, unsigned int len,
299 void *buf, unsigned int size)
301 struct buffer *out = buf;
303 /* Token name must match. */
304 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
306 /* Nothing else to do if there is no buffer. */
309 /* Make sure buffer is large enough. */
310 if (size < sizeof(*out))
312 /* Initialize buffer. */
313 memset(out, 0x00, sizeof(*out));
314 memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
319 /** Parse tokens for list command. */
321 parse_list(struct context *ctx, const struct token *token,
322 const char *str, unsigned int len,
323 void *buf, unsigned int size)
325 struct buffer *out = buf;
327 /* Token name must match. */
328 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
330 /* Nothing else to do if there is no buffer. */
334 if (ctx->curr != LIST)
336 if (sizeof(*out) > size)
338 out->command = ctx->curr;
340 out->args.list.group =
341 (void *)RTE_ALIGN_CEIL((uintptr_t)(out + 1),
345 if (((uint8_t *)(out->args.list.group + out->args.list.group_n) +
346 sizeof(*out->args.list.group)) > (uint8_t *)out + size)
348 ctx->object = out->args.list.group + out->args.list.group_n++;
353 * Parse signed/unsigned integers 8 to 64-bit long.
355 * Last argument (ctx->args) is retrieved to determine integer type and
359 parse_int(struct context *ctx, const struct token *token,
360 const char *str, unsigned int len,
361 void *buf, unsigned int size)
363 const struct arg *arg = pop_args(ctx);
368 /* Argument is expected. */
373 (uintmax_t)strtoimax(str, &end, 0) :
374 strtoumax(str, &end, 0);
375 if (errno || (size_t)(end - str) != len)
379 buf = (uint8_t *)ctx->object + arg->offset;
382 case sizeof(uint8_t):
385 case sizeof(uint16_t):
386 *(uint16_t *)buf = arg->hton ? rte_cpu_to_be_16(u) : u;
388 case sizeof(uint32_t):
389 *(uint32_t *)buf = arg->hton ? rte_cpu_to_be_32(u) : u;
391 case sizeof(uint64_t):
392 *(uint64_t *)buf = arg->hton ? rte_cpu_to_be_64(u) : u;
403 /** Parse port and update context. */
405 parse_port(struct context *ctx, const struct token *token,
406 const char *str, unsigned int len,
407 void *buf, unsigned int size)
409 struct buffer *out = &(struct buffer){ .port = 0 };
418 ret = parse_int(ctx, token, str, len, out, size);
420 ctx->port = out->port;
426 /** No completion. */
428 comp_none(struct context *ctx, const struct token *token,
429 unsigned int ent, char *buf, unsigned int size)
439 /** Complete available ports. */
441 comp_port(struct context *ctx, const struct token *token,
442 unsigned int ent, char *buf, unsigned int size)
449 FOREACH_PORT(p, ports) {
451 return snprintf(buf, size, "%u", p);
459 /** Internal context. */
460 static struct context cmd_flow_context;
462 /** Global parser instance (cmdline API). */
463 cmdline_parse_inst_t cmd_flow;
465 /** Initialize context. */
467 cmd_flow_context_init(struct context *ctx)
469 /* A full memset() is not necessary. */
481 /** Parse a token (cmdline API). */
483 cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result,
486 struct context *ctx = &cmd_flow_context;
487 const struct token *token;
488 const enum index *list;
493 /* Restart as requested. */
495 cmd_flow_context_init(ctx);
496 token = &token_list[ctx->curr];
497 /* Check argument length. */
500 for (len = 0; src[len]; ++len)
501 if (src[len] == '#' || isspace(src[len]))
505 /* Last argument and EOL detection. */
506 for (i = len; src[i]; ++i)
507 if (src[i] == '#' || src[i] == '\r' || src[i] == '\n')
509 else if (!isspace(src[i])) {
514 if (src[i] == '\r' || src[i] == '\n') {
518 /* Initialize context if necessary. */
519 if (!ctx->next_num) {
522 ctx->next[ctx->next_num++] = token->next[0];
524 /* Process argument through candidates. */
525 ctx->prev = ctx->curr;
526 list = ctx->next[ctx->next_num - 1];
527 for (i = 0; list[i]; ++i) {
528 const struct token *next = &token_list[list[i]];
533 tmp = next->call(ctx, next, src, len, result, size);
535 tmp = parse_default(ctx, next, src, len, result, size);
536 if (tmp == -1 || tmp != len)
544 /* Push subsequent tokens if any. */
546 for (i = 0; token->next[i]; ++i) {
547 if (ctx->next_num == RTE_DIM(ctx->next))
549 ctx->next[ctx->next_num++] = token->next[i];
551 /* Push arguments if any. */
553 for (i = 0; token->args[i]; ++i) {
554 if (ctx->args_num == RTE_DIM(ctx->args))
556 ctx->args[ctx->args_num++] = token->args[i];
561 /** Return number of completion entries (cmdline API). */
563 cmd_flow_complete_get_nb(cmdline_parse_token_hdr_t *hdr)
565 struct context *ctx = &cmd_flow_context;
566 const struct token *token = &token_list[ctx->curr];
567 const enum index *list;
571 /* Tell cmd_flow_parse() that context must be reinitialized. */
573 /* Count number of tokens in current list. */
575 list = ctx->next[ctx->next_num - 1];
577 list = token->next[0];
578 for (i = 0; list[i]; ++i)
583 * If there is a single token, use its completion callback, otherwise
584 * return the number of entries.
586 token = &token_list[list[0]];
587 if (i == 1 && token->comp) {
588 /* Save index for cmd_flow_get_help(). */
590 return token->comp(ctx, token, 0, NULL, 0);
595 /** Return a completion entry (cmdline API). */
597 cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,
598 char *dst, unsigned int size)
600 struct context *ctx = &cmd_flow_context;
601 const struct token *token = &token_list[ctx->curr];
602 const enum index *list;
606 /* Tell cmd_flow_parse() that context must be reinitialized. */
608 /* Count number of tokens in current list. */
610 list = ctx->next[ctx->next_num - 1];
612 list = token->next[0];
613 for (i = 0; list[i]; ++i)
617 /* If there is a single token, use its completion callback. */
618 token = &token_list[list[0]];
619 if (i == 1 && token->comp) {
620 /* Save index for cmd_flow_get_help(). */
622 return token->comp(ctx, token, index, dst, size) < 0 ? -1 : 0;
624 /* Otherwise make sure the index is valid and use defaults. */
627 token = &token_list[list[index]];
628 snprintf(dst, size, "%s", token->name);
629 /* Save index for cmd_flow_get_help(). */
630 ctx->prev = list[index];
634 /** Populate help strings for current token (cmdline API). */
636 cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
638 struct context *ctx = &cmd_flow_context;
639 const struct token *token = &token_list[ctx->prev];
642 /* Tell cmd_flow_parse() that context must be reinitialized. */
646 /* Set token type and update global help with details. */
647 snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
649 cmd_flow.help_str = token->help;
651 cmd_flow.help_str = token->name;
655 /** Token definition template (cmdline API). */
656 static struct cmdline_token_hdr cmd_flow_token_hdr = {
657 .ops = &(struct cmdline_token_ops){
658 .parse = cmd_flow_parse,
659 .complete_get_nb = cmd_flow_complete_get_nb,
660 .complete_get_elt = cmd_flow_complete_get_elt,
661 .get_help = cmd_flow_get_help,
666 /** Populate the next dynamic token. */
668 cmd_flow_tok(cmdline_parse_token_hdr_t **hdr,
669 cmdline_parse_token_hdr_t *(*hdrs)[])
671 struct context *ctx = &cmd_flow_context;
673 /* Always reinitialize context before requesting the first token. */
675 cmd_flow_context_init(ctx);
676 /* Return NULL when no more tokens are expected. */
677 if (!ctx->next_num && ctx->curr) {
681 /* Determine if command should end here. */
682 if (ctx->eol && ctx->last && ctx->next_num) {
683 const enum index *list = ctx->next[ctx->next_num - 1];
686 for (i = 0; list[i]; ++i) {
693 *hdr = &cmd_flow_token_hdr;
696 /** Dispatch parsed buffer to function calls. */
698 cmd_flow_parsed(const struct buffer *in)
700 switch (in->command) {
702 port_flow_list(in->port, in->args.list.group_n,
703 in->args.list.group);
710 /** Token generator and output processing callback (cmdline API). */
712 cmd_flow_cb(void *arg0, struct cmdline *cl, void *arg2)
715 cmd_flow_tok(arg0, arg2);
717 cmd_flow_parsed(arg0);
720 /** Global parser instance (cmdline API). */
721 cmdline_parse_inst_t cmd_flow = {
723 .data = NULL, /**< Unused. */
724 .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
727 }, /**< Tokens are returned by cmd_flow_tok(). */