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. */
60 /* Top-level command. */
64 /** Maximum number of subsequent tokens and arguments on the stack. */
65 #define CTX_STACK_SIZE 16
67 /** Parser context. */
69 /** Stack of subsequent token lists to process. */
70 const enum index *next[CTX_STACK_SIZE];
71 /** Arguments for stacked tokens. */
72 const void *args[CTX_STACK_SIZE];
73 enum index curr; /**< Current token index. */
74 enum index prev; /**< Index of the last token seen. */
75 int next_num; /**< Number of entries in next[]. */
76 int args_num; /**< Number of entries in args[]. */
77 uint32_t reparse:1; /**< Start over from the beginning. */
78 uint32_t eol:1; /**< EOL has been detected. */
79 uint32_t last:1; /**< No more arguments. */
80 void *object; /**< Address of current object for relative offsets. */
83 /** Token argument. */
85 uint32_t hton:1; /**< Use network byte ordering. */
86 uint32_t sign:1; /**< Value is signed. */
87 uint32_t offset; /**< Relative offset from ctx->object. */
88 uint32_t size; /**< Field size. */
91 /** Parser token definition. */
93 /** Type displayed during completion (defaults to "TOKEN"). */
95 /** Help displayed during completion (defaults to token name). */
98 * Lists of subsequent tokens to push on the stack. Each call to the
99 * parser consumes the last entry of that stack.
101 const enum index *const *next;
102 /** Arguments stack for subsequent tokens that need them. */
103 const struct arg *const *args;
105 * Token-processing callback, returns -1 in case of error, the
106 * length of the matched string otherwise. If NULL, attempts to
107 * match the token name.
109 * If buf is not NULL, the result should be stored in it according
110 * to context. An error is returned if not large enough.
112 int (*call)(struct context *ctx, const struct token *token,
113 const char *str, unsigned int len,
114 void *buf, unsigned int size);
116 * Callback that provides possible values for this token, used for
117 * completion. Returns -1 in case of error, the number of possible
118 * values otherwise. If NULL, the token name is used.
120 * If buf is not NULL, entry index ent is written to buf and the
121 * full length of the entry is returned (same behavior as
124 int (*comp)(struct context *ctx, const struct token *token,
125 unsigned int ent, char *buf, unsigned int size);
126 /** Mandatory token name, no default value. */
130 /** Static initializer for the next field. */
131 #define NEXT(...) (const enum index *const []){ __VA_ARGS__, NULL, }
133 /** Static initializer for a NEXT() entry. */
134 #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, }
136 /** Static initializer for the args field. */
137 #define ARGS(...) (const struct arg *const []){ __VA_ARGS__, NULL, }
139 /** Static initializer for ARGS() to target a field. */
140 #define ARGS_ENTRY(s, f) \
141 (&(const struct arg){ \
142 .offset = offsetof(s, f), \
143 .size = sizeof(((s *)0)->f), \
146 /** Static initializer for ARGS() to target a pointer. */
147 #define ARGS_ENTRY_PTR(s, f) \
148 (&(const struct arg){ \
149 .size = sizeof(*((s *)0)->f), \
152 /** Parser output buffer layout expected by cmd_flow_parsed(). */
154 enum index command; /**< Flow command. */
155 uint16_t port; /**< Affected port ID. */
158 static int parse_init(struct context *, const struct token *,
159 const char *, unsigned int,
160 void *, unsigned int);
161 static int parse_int(struct context *, const struct token *,
162 const char *, unsigned int,
163 void *, unsigned int);
164 static int comp_none(struct context *, const struct token *,
165 unsigned int, char *, unsigned int);
167 /** Token definitions. */
168 static const struct token token_list[] = {
169 /* Special tokens. */
172 .help = "null entry, abused as the entry point",
173 .next = NEXT(NEXT_ENTRY(FLOW)),
178 .help = "command may end here",
184 .help = "integer value",
189 .name = "{unsigned}",
191 .help = "unsigned integer value",
195 /* Top-level command. */
198 .type = "{command} {port_id} [{arg} [...]]",
199 .help = "manage ingress/egress flow rules",
204 /** Remove and return last entry from argument stack. */
205 static const struct arg *
206 pop_args(struct context *ctx)
208 return ctx->args_num ? ctx->args[--ctx->args_num] : NULL;
211 /** Add entry on top of the argument stack. */
213 push_args(struct context *ctx, const struct arg *arg)
215 if (ctx->args_num == CTX_STACK_SIZE)
217 ctx->args[ctx->args_num++] = arg;
221 /** Default parsing function for token name matching. */
223 parse_default(struct context *ctx, const struct token *token,
224 const char *str, unsigned int len,
225 void *buf, unsigned int size)
230 if (strncmp(str, token->name, len))
235 /** Parse flow command, initialize output buffer for subsequent tokens. */
237 parse_init(struct context *ctx, const struct token *token,
238 const char *str, unsigned int len,
239 void *buf, unsigned int size)
241 struct buffer *out = buf;
243 /* Token name must match. */
244 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
246 /* Nothing else to do if there is no buffer. */
249 /* Make sure buffer is large enough. */
250 if (size < sizeof(*out))
252 /* Initialize buffer. */
253 memset(out, 0x00, sizeof(*out));
254 memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
260 * Parse signed/unsigned integers 8 to 64-bit long.
262 * Last argument (ctx->args) is retrieved to determine integer type and
266 parse_int(struct context *ctx, const struct token *token,
267 const char *str, unsigned int len,
268 void *buf, unsigned int size)
270 const struct arg *arg = pop_args(ctx);
275 /* Argument is expected. */
280 (uintmax_t)strtoimax(str, &end, 0) :
281 strtoumax(str, &end, 0);
282 if (errno || (size_t)(end - str) != len)
286 buf = (uint8_t *)ctx->object + arg->offset;
289 case sizeof(uint8_t):
292 case sizeof(uint16_t):
293 *(uint16_t *)buf = arg->hton ? rte_cpu_to_be_16(u) : u;
295 case sizeof(uint32_t):
296 *(uint32_t *)buf = arg->hton ? rte_cpu_to_be_32(u) : u;
298 case sizeof(uint64_t):
299 *(uint64_t *)buf = arg->hton ? rte_cpu_to_be_64(u) : u;
310 /** No completion. */
312 comp_none(struct context *ctx, const struct token *token,
313 unsigned int ent, char *buf, unsigned int size)
323 /** Internal context. */
324 static struct context cmd_flow_context;
326 /** Global parser instance (cmdline API). */
327 cmdline_parse_inst_t cmd_flow;
329 /** Initialize context. */
331 cmd_flow_context_init(struct context *ctx)
333 /* A full memset() is not necessary. */
344 /** Parse a token (cmdline API). */
346 cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result,
349 struct context *ctx = &cmd_flow_context;
350 const struct token *token;
351 const enum index *list;
356 /* Restart as requested. */
358 cmd_flow_context_init(ctx);
359 token = &token_list[ctx->curr];
360 /* Check argument length. */
363 for (len = 0; src[len]; ++len)
364 if (src[len] == '#' || isspace(src[len]))
368 /* Last argument and EOL detection. */
369 for (i = len; src[i]; ++i)
370 if (src[i] == '#' || src[i] == '\r' || src[i] == '\n')
372 else if (!isspace(src[i])) {
377 if (src[i] == '\r' || src[i] == '\n') {
381 /* Initialize context if necessary. */
382 if (!ctx->next_num) {
385 ctx->next[ctx->next_num++] = token->next[0];
387 /* Process argument through candidates. */
388 ctx->prev = ctx->curr;
389 list = ctx->next[ctx->next_num - 1];
390 for (i = 0; list[i]; ++i) {
391 const struct token *next = &token_list[list[i]];
396 tmp = next->call(ctx, next, src, len, result, size);
398 tmp = parse_default(ctx, next, src, len, result, size);
399 if (tmp == -1 || tmp != len)
407 /* Push subsequent tokens if any. */
409 for (i = 0; token->next[i]; ++i) {
410 if (ctx->next_num == RTE_DIM(ctx->next))
412 ctx->next[ctx->next_num++] = token->next[i];
414 /* Push arguments if any. */
416 for (i = 0; token->args[i]; ++i) {
417 if (ctx->args_num == RTE_DIM(ctx->args))
419 ctx->args[ctx->args_num++] = token->args[i];
424 /** Return number of completion entries (cmdline API). */
426 cmd_flow_complete_get_nb(cmdline_parse_token_hdr_t *hdr)
428 struct context *ctx = &cmd_flow_context;
429 const struct token *token = &token_list[ctx->curr];
430 const enum index *list;
434 /* Tell cmd_flow_parse() that context must be reinitialized. */
436 /* Count number of tokens in current list. */
438 list = ctx->next[ctx->next_num - 1];
440 list = token->next[0];
441 for (i = 0; list[i]; ++i)
446 * If there is a single token, use its completion callback, otherwise
447 * return the number of entries.
449 token = &token_list[list[0]];
450 if (i == 1 && token->comp) {
451 /* Save index for cmd_flow_get_help(). */
453 return token->comp(ctx, token, 0, NULL, 0);
458 /** Return a completion entry (cmdline API). */
460 cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,
461 char *dst, unsigned int size)
463 struct context *ctx = &cmd_flow_context;
464 const struct token *token = &token_list[ctx->curr];
465 const enum index *list;
469 /* Tell cmd_flow_parse() that context must be reinitialized. */
471 /* Count number of tokens in current list. */
473 list = ctx->next[ctx->next_num - 1];
475 list = token->next[0];
476 for (i = 0; list[i]; ++i)
480 /* If there is a single token, use its completion callback. */
481 token = &token_list[list[0]];
482 if (i == 1 && token->comp) {
483 /* Save index for cmd_flow_get_help(). */
485 return token->comp(ctx, token, index, dst, size) < 0 ? -1 : 0;
487 /* Otherwise make sure the index is valid and use defaults. */
490 token = &token_list[list[index]];
491 snprintf(dst, size, "%s", token->name);
492 /* Save index for cmd_flow_get_help(). */
493 ctx->prev = list[index];
497 /** Populate help strings for current token (cmdline API). */
499 cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
501 struct context *ctx = &cmd_flow_context;
502 const struct token *token = &token_list[ctx->prev];
505 /* Tell cmd_flow_parse() that context must be reinitialized. */
509 /* Set token type and update global help with details. */
510 snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
512 cmd_flow.help_str = token->help;
514 cmd_flow.help_str = token->name;
518 /** Token definition template (cmdline API). */
519 static struct cmdline_token_hdr cmd_flow_token_hdr = {
520 .ops = &(struct cmdline_token_ops){
521 .parse = cmd_flow_parse,
522 .complete_get_nb = cmd_flow_complete_get_nb,
523 .complete_get_elt = cmd_flow_complete_get_elt,
524 .get_help = cmd_flow_get_help,
529 /** Populate the next dynamic token. */
531 cmd_flow_tok(cmdline_parse_token_hdr_t **hdr,
532 cmdline_parse_token_hdr_t *(*hdrs)[])
534 struct context *ctx = &cmd_flow_context;
536 /* Always reinitialize context before requesting the first token. */
538 cmd_flow_context_init(ctx);
539 /* Return NULL when no more tokens are expected. */
540 if (!ctx->next_num && ctx->curr) {
544 /* Determine if command should end here. */
545 if (ctx->eol && ctx->last && ctx->next_num) {
546 const enum index *list = ctx->next[ctx->next_num - 1];
549 for (i = 0; list[i]; ++i) {
556 *hdr = &cmd_flow_token_hdr;
559 /** Dispatch parsed buffer to function calls. */
561 cmd_flow_parsed(const struct buffer *in)
563 switch (in->command) {
569 /** Token generator and output processing callback (cmdline API). */
571 cmd_flow_cb(void *arg0, struct cmdline *cl, void *arg2)
574 cmd_flow_tok(arg0, arg2);
576 cmd_flow_parsed(arg0);
579 /** Global parser instance (cmdline API). */
580 cmdline_parse_inst_t cmd_flow = {
582 .data = NULL, /**< Unused. */
583 .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
586 }, /**< Tokens are returned by cmd_flow_tok(). */