1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
10 #include <rte_common.h>
18 #ifndef CMD_MAX_TOKENS
19 #define CMD_MAX_TOKENS 256
22 #define MSG_OUT_OF_MEMORY "Not enough memory.\n"
23 #define MSG_CMD_UNKNOWN "Unknown command \"%s\".\n"
24 #define MSG_CMD_UNIMPLEM "Command \"%s\" not implemented.\n"
25 #define MSG_ARG_NOT_ENOUGH "Not enough arguments for command \"%s\".\n"
26 #define MSG_ARG_TOO_MANY "Too many arguments for command \"%s\".\n"
27 #define MSG_ARG_MISMATCH "Wrong number of arguments for command \"%s\".\n"
28 #define MSG_ARG_NOT_FOUND "Argument \"%s\" not found.\n"
29 #define MSG_ARG_INVALID "Invalid value for argument \"%s\".\n"
30 #define MSG_FILE_ERR "Error in file \"%s\" at line %u.\n"
31 #define MSG_CMD_FAIL "Command \"%s\" failed.\n"
36 if ((strlen(in) && index("!#%;", in[0])) ||
37 (strncmp(in, "//", 2) == 0) ||
38 (strncmp(in, "--", 2) == 0))
45 * mempool <mempool_name>
46 * buffer <buffer_size>
52 cmd_mempool(char **tokens,
57 struct mempool_params p;
59 struct mempool *mempool;
62 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
68 if (strcmp(tokens[2], "buffer") != 0) {
69 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "buffer");
73 if (parser_read_uint32(&p.buffer_size, tokens[3]) != 0) {
74 snprintf(out, out_size, MSG_ARG_INVALID, "buffer_size");
78 if (strcmp(tokens[4], "pool") != 0) {
79 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pool");
83 if (parser_read_uint32(&p.pool_size, tokens[5]) != 0) {
84 snprintf(out, out_size, MSG_ARG_INVALID, "pool_size");
88 if (strcmp(tokens[6], "cache") != 0) {
89 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cache");
93 if (parser_read_uint32(&p.cache_size, tokens[7]) != 0) {
94 snprintf(out, out_size, MSG_ARG_INVALID, "cache_size");
98 if (strcmp(tokens[8], "cpu") != 0) {
99 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
103 if (parser_read_uint32(&p.cpu_id, tokens[9]) != 0) {
104 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
108 mempool = mempool_create(name, &p);
109 if (mempool == NULL) {
110 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
117 * dev <device_name> | port <port_id>
118 * rxq <n_queues> <queue_size> <mempool_name>
119 * txq <n_queues> <queue_size>
120 * promiscuous on | off
121 * [rss <qid_0> ... <qid_n>]
124 cmd_link(char **tokens,
129 struct link_params p;
130 struct link_params_rss rss;
134 if ((n_tokens < 13) || (n_tokens > 14 + LINK_RXQ_RSS_MAX)) {
135 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
140 if (strcmp(tokens[2], "dev") == 0)
141 p.dev_name = tokens[3];
142 else if (strcmp(tokens[2], "port") == 0) {
145 if (parser_read_uint16(&p.port_id, tokens[3]) != 0) {
146 snprintf(out, out_size, MSG_ARG_INVALID, "port_id");
150 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "dev or port");
154 if (strcmp(tokens[4], "rxq") != 0) {
155 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rxq");
159 if (parser_read_uint32(&p.rx.n_queues, tokens[5]) != 0) {
160 snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
163 if (parser_read_uint32(&p.rx.queue_size, tokens[6]) != 0) {
164 snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
168 p.rx.mempool_name = tokens[7];
170 if (strcmp(tokens[8], "txq") != 0) {
171 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "txq");
175 if (parser_read_uint32(&p.tx.n_queues, tokens[9]) != 0) {
176 snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
180 if (parser_read_uint32(&p.tx.queue_size, tokens[10]) != 0) {
181 snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
185 if (strcmp(tokens[11], "promiscuous") != 0) {
186 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "promiscuous");
190 if (strcmp(tokens[12], "on") == 0)
192 else if (strcmp(tokens[12], "off") == 0)
195 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "on or off");
202 uint32_t queue_id, i;
204 if (strcmp(tokens[13], "rss") != 0) {
205 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rss");
212 for (i = 14; i < n_tokens; i++) {
213 if (parser_read_uint32(&queue_id, tokens[i]) != 0) {
214 snprintf(out, out_size, MSG_ARG_INVALID,
219 rss.queue_id[rss.n_queues] = queue_id;
224 link = link_create(name, &p);
226 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
237 cmd_swq(char **tokens,
247 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
253 if (strcmp(tokens[2], "size") != 0) {
254 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "size");
258 if (parser_read_uint32(&p.size, tokens[3]) != 0) {
259 snprintf(out, out_size, MSG_ARG_INVALID, "size");
263 if (strcmp(tokens[4], "cpu") != 0) {
264 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
268 if (parser_read_uint32(&p.cpu_id, tokens[5]) != 0) {
269 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
273 swq = swq_create(name, &p);
275 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
281 cli_process(char *in, char *out, size_t out_size)
283 char *tokens[CMD_MAX_TOKENS];
284 uint32_t n_tokens = RTE_DIM(tokens);
290 status = parse_tokenize_string(in, tokens, &n_tokens);
292 snprintf(out, out_size, MSG_ARG_TOO_MANY, "");
299 if (strcmp(tokens[0], "mempool") == 0) {
300 cmd_mempool(tokens, n_tokens, out, out_size);
304 if (strcmp(tokens[0], "link") == 0) {
305 cmd_link(tokens, n_tokens, out, out_size);
309 if (strcmp(tokens[0], "swq") == 0) {
310 cmd_swq(tokens, n_tokens, out, out_size);
314 snprintf(out, out_size, MSG_CMD_UNKNOWN, tokens[0]);
318 cli_script_process(const char *file_name,
319 size_t msg_in_len_max,
320 size_t msg_out_len_max)
322 char *msg_in = NULL, *msg_out = NULL;
325 /* Check input arguments */
326 if ((file_name == NULL) ||
327 (strlen(file_name) == 0) ||
328 (msg_in_len_max == 0) ||
329 (msg_out_len_max == 0))
332 msg_in = malloc(msg_in_len_max + 1);
333 msg_out = malloc(msg_out_len_max + 1);
334 if ((msg_in == NULL) ||
341 /* Open input file */
342 f = fopen(file_name, "r");
351 if (fgets(msg_in, msg_in_len_max + 1, f) == NULL)
354 printf("%s", msg_in);
362 printf("%s", msg_out);