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.
40 #include <rte_common.h>
41 #include <rte_ethdev.h>
42 #include <cmdline_parse.h>
47 /** Parser token indices. */
53 /* Top-level command. */
57 /** Maximum number of subsequent tokens and arguments on the stack. */
58 #define CTX_STACK_SIZE 16
60 /** Parser context. */
62 /** Stack of subsequent token lists to process. */
63 const enum index *next[CTX_STACK_SIZE];
64 enum index curr; /**< Current token index. */
65 enum index prev; /**< Index of the last token seen. */
66 int next_num; /**< Number of entries in next[]. */
67 uint32_t reparse:1; /**< Start over from the beginning. */
68 uint32_t eol:1; /**< EOL has been detected. */
69 uint32_t last:1; /**< No more arguments. */
72 /** Parser token definition. */
74 /** Type displayed during completion (defaults to "TOKEN"). */
76 /** Help displayed during completion (defaults to token name). */
79 * Lists of subsequent tokens to push on the stack. Each call to the
80 * parser consumes the last entry of that stack.
82 const enum index *const *next;
84 * Token-processing callback, returns -1 in case of error, the
85 * length of the matched string otherwise. If NULL, attempts to
86 * match the token name.
88 * If buf is not NULL, the result should be stored in it according
89 * to context. An error is returned if not large enough.
91 int (*call)(struct context *ctx, const struct token *token,
92 const char *str, unsigned int len,
93 void *buf, unsigned int size);
95 * Callback that provides possible values for this token, used for
96 * completion. Returns -1 in case of error, the number of possible
97 * values otherwise. If NULL, the token name is used.
99 * If buf is not NULL, entry index ent is written to buf and the
100 * full length of the entry is returned (same behavior as
103 int (*comp)(struct context *ctx, const struct token *token,
104 unsigned int ent, char *buf, unsigned int size);
105 /** Mandatory token name, no default value. */
109 /** Static initializer for the next field. */
110 #define NEXT(...) (const enum index *const []){ __VA_ARGS__, NULL, }
112 /** Static initializer for a NEXT() entry. */
113 #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, }
115 /** Parser output buffer layout expected by cmd_flow_parsed(). */
117 enum index command; /**< Flow command. */
118 uint16_t port; /**< Affected port ID. */
121 static int parse_init(struct context *, const struct token *,
122 const char *, unsigned int,
123 void *, unsigned int);
125 /** Token definitions. */
126 static const struct token token_list[] = {
127 /* Special tokens. */
130 .help = "null entry, abused as the entry point",
131 .next = NEXT(NEXT_ENTRY(FLOW)),
136 .help = "command may end here",
138 /* Top-level command. */
141 .type = "{command} {port_id} [{arg} [...]]",
142 .help = "manage ingress/egress flow rules",
147 /** Default parsing function for token name matching. */
149 parse_default(struct context *ctx, const struct token *token,
150 const char *str, unsigned int len,
151 void *buf, unsigned int size)
156 if (strncmp(str, token->name, len))
161 /** Parse flow command, initialize output buffer for subsequent tokens. */
163 parse_init(struct context *ctx, const struct token *token,
164 const char *str, unsigned int len,
165 void *buf, unsigned int size)
167 struct buffer *out = buf;
169 /* Token name must match. */
170 if (parse_default(ctx, token, str, len, NULL, 0) < 0)
172 /* Nothing else to do if there is no buffer. */
175 /* Make sure buffer is large enough. */
176 if (size < sizeof(*out))
178 /* Initialize buffer. */
179 memset(out, 0x00, sizeof(*out));
180 memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out));
184 /** Internal context. */
185 static struct context cmd_flow_context;
187 /** Global parser instance (cmdline API). */
188 cmdline_parse_inst_t cmd_flow;
190 /** Initialize context. */
192 cmd_flow_context_init(struct context *ctx)
194 /* A full memset() is not necessary. */
203 /** Parse a token (cmdline API). */
205 cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result,
208 struct context *ctx = &cmd_flow_context;
209 const struct token *token;
210 const enum index *list;
215 /* Restart as requested. */
217 cmd_flow_context_init(ctx);
218 token = &token_list[ctx->curr];
219 /* Check argument length. */
222 for (len = 0; src[len]; ++len)
223 if (src[len] == '#' || isspace(src[len]))
227 /* Last argument and EOL detection. */
228 for (i = len; src[i]; ++i)
229 if (src[i] == '#' || src[i] == '\r' || src[i] == '\n')
231 else if (!isspace(src[i])) {
236 if (src[i] == '\r' || src[i] == '\n') {
240 /* Initialize context if necessary. */
241 if (!ctx->next_num) {
244 ctx->next[ctx->next_num++] = token->next[0];
246 /* Process argument through candidates. */
247 ctx->prev = ctx->curr;
248 list = ctx->next[ctx->next_num - 1];
249 for (i = 0; list[i]; ++i) {
250 const struct token *next = &token_list[list[i]];
255 tmp = next->call(ctx, next, src, len, result, size);
257 tmp = parse_default(ctx, next, src, len, result, size);
258 if (tmp == -1 || tmp != len)
266 /* Push subsequent tokens if any. */
268 for (i = 0; token->next[i]; ++i) {
269 if (ctx->next_num == RTE_DIM(ctx->next))
271 ctx->next[ctx->next_num++] = token->next[i];
276 /** Return number of completion entries (cmdline API). */
278 cmd_flow_complete_get_nb(cmdline_parse_token_hdr_t *hdr)
280 struct context *ctx = &cmd_flow_context;
281 const struct token *token = &token_list[ctx->curr];
282 const enum index *list;
286 /* Tell cmd_flow_parse() that context must be reinitialized. */
288 /* Count number of tokens in current list. */
290 list = ctx->next[ctx->next_num - 1];
292 list = token->next[0];
293 for (i = 0; list[i]; ++i)
298 * If there is a single token, use its completion callback, otherwise
299 * return the number of entries.
301 token = &token_list[list[0]];
302 if (i == 1 && token->comp) {
303 /* Save index for cmd_flow_get_help(). */
305 return token->comp(ctx, token, 0, NULL, 0);
310 /** Return a completion entry (cmdline API). */
312 cmd_flow_complete_get_elt(cmdline_parse_token_hdr_t *hdr, int index,
313 char *dst, unsigned int size)
315 struct context *ctx = &cmd_flow_context;
316 const struct token *token = &token_list[ctx->curr];
317 const enum index *list;
321 /* Tell cmd_flow_parse() that context must be reinitialized. */
323 /* Count number of tokens in current list. */
325 list = ctx->next[ctx->next_num - 1];
327 list = token->next[0];
328 for (i = 0; list[i]; ++i)
332 /* If there is a single token, use its completion callback. */
333 token = &token_list[list[0]];
334 if (i == 1 && token->comp) {
335 /* Save index for cmd_flow_get_help(). */
337 return token->comp(ctx, token, index, dst, size) < 0 ? -1 : 0;
339 /* Otherwise make sure the index is valid and use defaults. */
342 token = &token_list[list[index]];
343 snprintf(dst, size, "%s", token->name);
344 /* Save index for cmd_flow_get_help(). */
345 ctx->prev = list[index];
349 /** Populate help strings for current token (cmdline API). */
351 cmd_flow_get_help(cmdline_parse_token_hdr_t *hdr, char *dst, unsigned int size)
353 struct context *ctx = &cmd_flow_context;
354 const struct token *token = &token_list[ctx->prev];
357 /* Tell cmd_flow_parse() that context must be reinitialized. */
361 /* Set token type and update global help with details. */
362 snprintf(dst, size, "%s", (token->type ? token->type : "TOKEN"));
364 cmd_flow.help_str = token->help;
366 cmd_flow.help_str = token->name;
370 /** Token definition template (cmdline API). */
371 static struct cmdline_token_hdr cmd_flow_token_hdr = {
372 .ops = &(struct cmdline_token_ops){
373 .parse = cmd_flow_parse,
374 .complete_get_nb = cmd_flow_complete_get_nb,
375 .complete_get_elt = cmd_flow_complete_get_elt,
376 .get_help = cmd_flow_get_help,
381 /** Populate the next dynamic token. */
383 cmd_flow_tok(cmdline_parse_token_hdr_t **hdr,
384 cmdline_parse_token_hdr_t *(*hdrs)[])
386 struct context *ctx = &cmd_flow_context;
388 /* Always reinitialize context before requesting the first token. */
390 cmd_flow_context_init(ctx);
391 /* Return NULL when no more tokens are expected. */
392 if (!ctx->next_num && ctx->curr) {
396 /* Determine if command should end here. */
397 if (ctx->eol && ctx->last && ctx->next_num) {
398 const enum index *list = ctx->next[ctx->next_num - 1];
401 for (i = 0; list[i]; ++i) {
408 *hdr = &cmd_flow_token_hdr;
411 /** Dispatch parsed buffer to function calls. */
413 cmd_flow_parsed(const struct buffer *in)
415 switch (in->command) {
421 /** Token generator and output processing callback (cmdline API). */
423 cmd_flow_cb(void *arg0, struct cmdline *cl, void *arg2)
426 cmd_flow_tok(arg0, arg2);
428 cmd_flow_parsed(arg0);
431 /** Global parser instance (cmdline API). */
432 cmdline_parse_inst_t cmd_flow = {
434 .data = NULL, /**< Unused. */
435 .help_str = NULL, /**< Updated by cmd_flow_get_help(). */
438 }, /**< Tokens are returned by cmd_flow_tok(). */