1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
10 #include <rte_common.h>
17 #ifndef CMD_MAX_TOKENS
18 #define CMD_MAX_TOKENS 256
21 #define MSG_OUT_OF_MEMORY "Not enough memory.\n"
22 #define MSG_CMD_UNKNOWN "Unknown command \"%s\".\n"
23 #define MSG_CMD_UNIMPLEM "Command \"%s\" not implemented.\n"
24 #define MSG_ARG_NOT_ENOUGH "Not enough arguments for command \"%s\".\n"
25 #define MSG_ARG_TOO_MANY "Too many arguments for command \"%s\".\n"
26 #define MSG_ARG_MISMATCH "Wrong number of arguments for command \"%s\".\n"
27 #define MSG_ARG_NOT_FOUND "Argument \"%s\" not found.\n"
28 #define MSG_ARG_INVALID "Invalid value for argument \"%s\".\n"
29 #define MSG_FILE_ERR "Error in file \"%s\" at line %u.\n"
30 #define MSG_CMD_FAIL "Command \"%s\" failed.\n"
35 if ((strlen(in) && index("!#%;", in[0])) ||
36 (strncmp(in, "//", 2) == 0) ||
37 (strncmp(in, "--", 2) == 0))
44 * mempool <mempool_name>
45 * buffer <buffer_size>
51 cmd_mempool(char **tokens,
56 struct mempool_params p;
58 struct mempool *mempool;
61 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
67 if (strcmp(tokens[2], "buffer") != 0) {
68 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "buffer");
72 if (parser_read_uint32(&p.buffer_size, tokens[3]) != 0) {
73 snprintf(out, out_size, MSG_ARG_INVALID, "buffer_size");
77 if (strcmp(tokens[4], "pool") != 0) {
78 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pool");
82 if (parser_read_uint32(&p.pool_size, tokens[5]) != 0) {
83 snprintf(out, out_size, MSG_ARG_INVALID, "pool_size");
87 if (strcmp(tokens[6], "cache") != 0) {
88 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cache");
92 if (parser_read_uint32(&p.cache_size, tokens[7]) != 0) {
93 snprintf(out, out_size, MSG_ARG_INVALID, "cache_size");
97 if (strcmp(tokens[8], "cpu") != 0) {
98 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
102 if (parser_read_uint32(&p.cpu_id, tokens[9]) != 0) {
103 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
107 mempool = mempool_create(name, &p);
108 if (mempool == NULL) {
109 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
116 * dev <device_name> | port <port_id>
117 * rxq <n_queues> <queue_size> <mempool_name>
118 * txq <n_queues> <queue_size>
119 * promiscuous on | off
120 * [rss <qid_0> ... <qid_n>]
123 cmd_link(char **tokens,
128 struct link_params p;
129 struct link_params_rss rss;
133 if ((n_tokens < 13) || (n_tokens > 14 + LINK_RXQ_RSS_MAX)) {
134 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
139 if (strcmp(tokens[2], "dev") == 0)
140 p.dev_name = tokens[3];
141 else if (strcmp(tokens[2], "port") == 0) {
144 if (parser_read_uint16(&p.port_id, tokens[3]) != 0) {
145 snprintf(out, out_size, MSG_ARG_INVALID, "port_id");
149 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "dev or port");
153 if (strcmp(tokens[4], "rxq") != 0) {
154 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rxq");
158 if (parser_read_uint32(&p.rx.n_queues, tokens[5]) != 0) {
159 snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
162 if (parser_read_uint32(&p.rx.queue_size, tokens[6]) != 0) {
163 snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
167 p.rx.mempool_name = tokens[7];
169 if (strcmp(tokens[8], "txq") != 0) {
170 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "txq");
174 if (parser_read_uint32(&p.tx.n_queues, tokens[9]) != 0) {
175 snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
179 if (parser_read_uint32(&p.tx.queue_size, tokens[10]) != 0) {
180 snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
184 if (strcmp(tokens[11], "promiscuous") != 0) {
185 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "promiscuous");
189 if (strcmp(tokens[12], "on") == 0)
191 else if (strcmp(tokens[12], "off") == 0)
194 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "on or off");
201 uint32_t queue_id, i;
203 if (strcmp(tokens[13], "rss") != 0) {
204 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rss");
211 for (i = 14; i < n_tokens; i++) {
212 if (parser_read_uint32(&queue_id, tokens[i]) != 0) {
213 snprintf(out, out_size, MSG_ARG_INVALID,
218 rss.queue_id[rss.n_queues] = queue_id;
223 link = link_create(name, &p);
225 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
231 cli_process(char *in, char *out, size_t out_size)
233 char *tokens[CMD_MAX_TOKENS];
234 uint32_t n_tokens = RTE_DIM(tokens);
240 status = parse_tokenize_string(in, tokens, &n_tokens);
242 snprintf(out, out_size, MSG_ARG_TOO_MANY, "");
249 if (strcmp(tokens[0], "mempool") == 0) {
250 cmd_mempool(tokens, n_tokens, out, out_size);
254 if (strcmp(tokens[0], "link") == 0) {
255 cmd_link(tokens, n_tokens, out, out_size);
259 snprintf(out, out_size, MSG_CMD_UNKNOWN, tokens[0]);
263 cli_script_process(const char *file_name,
264 size_t msg_in_len_max,
265 size_t msg_out_len_max)
267 char *msg_in = NULL, *msg_out = NULL;
270 /* Check input arguments */
271 if ((file_name == NULL) ||
272 (strlen(file_name) == 0) ||
273 (msg_in_len_max == 0) ||
274 (msg_out_len_max == 0))
277 msg_in = malloc(msg_in_len_max + 1);
278 msg_out = malloc(msg_out_len_max + 1);
279 if ((msg_in == NULL) ||
286 /* Open input file */
287 f = fopen(file_name, "r");
296 if (fgets(msg_in, msg_in_len_max + 1, f) == NULL)
299 printf("%s", msg_in);
307 printf("%s", msg_out);