1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
10 #include <rte_common.h>
19 #ifndef CMD_MAX_TOKENS
20 #define CMD_MAX_TOKENS 256
23 #define MSG_OUT_OF_MEMORY "Not enough memory.\n"
24 #define MSG_CMD_UNKNOWN "Unknown command \"%s\".\n"
25 #define MSG_CMD_UNIMPLEM "Command \"%s\" not implemented.\n"
26 #define MSG_ARG_NOT_ENOUGH "Not enough arguments for command \"%s\".\n"
27 #define MSG_ARG_TOO_MANY "Too many arguments for command \"%s\".\n"
28 #define MSG_ARG_MISMATCH "Wrong number of arguments for command \"%s\".\n"
29 #define MSG_ARG_NOT_FOUND "Argument \"%s\" not found.\n"
30 #define MSG_ARG_INVALID "Invalid value for argument \"%s\".\n"
31 #define MSG_FILE_ERR "Error in file \"%s\" at line %u.\n"
32 #define MSG_CMD_FAIL "Command \"%s\" failed.\n"
37 if ((strlen(in) && index("!#%;", in[0])) ||
38 (strncmp(in, "//", 2) == 0) ||
39 (strncmp(in, "--", 2) == 0))
46 * mempool <mempool_name>
47 * buffer <buffer_size>
53 cmd_mempool(char **tokens,
58 struct mempool_params p;
60 struct mempool *mempool;
63 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
69 if (strcmp(tokens[2], "buffer") != 0) {
70 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "buffer");
74 if (parser_read_uint32(&p.buffer_size, tokens[3]) != 0) {
75 snprintf(out, out_size, MSG_ARG_INVALID, "buffer_size");
79 if (strcmp(tokens[4], "pool") != 0) {
80 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pool");
84 if (parser_read_uint32(&p.pool_size, tokens[5]) != 0) {
85 snprintf(out, out_size, MSG_ARG_INVALID, "pool_size");
89 if (strcmp(tokens[6], "cache") != 0) {
90 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cache");
94 if (parser_read_uint32(&p.cache_size, tokens[7]) != 0) {
95 snprintf(out, out_size, MSG_ARG_INVALID, "cache_size");
99 if (strcmp(tokens[8], "cpu") != 0) {
100 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
104 if (parser_read_uint32(&p.cpu_id, tokens[9]) != 0) {
105 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
109 mempool = mempool_create(name, &p);
110 if (mempool == NULL) {
111 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
118 * dev <device_name> | port <port_id>
119 * rxq <n_queues> <queue_size> <mempool_name>
120 * txq <n_queues> <queue_size>
121 * promiscuous on | off
122 * [rss <qid_0> ... <qid_n>]
125 cmd_link(char **tokens,
130 struct link_params p;
131 struct link_params_rss rss;
135 if ((n_tokens < 13) || (n_tokens > 14 + LINK_RXQ_RSS_MAX)) {
136 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
141 if (strcmp(tokens[2], "dev") == 0)
142 p.dev_name = tokens[3];
143 else if (strcmp(tokens[2], "port") == 0) {
146 if (parser_read_uint16(&p.port_id, tokens[3]) != 0) {
147 snprintf(out, out_size, MSG_ARG_INVALID, "port_id");
151 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "dev or port");
155 if (strcmp(tokens[4], "rxq") != 0) {
156 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rxq");
160 if (parser_read_uint32(&p.rx.n_queues, tokens[5]) != 0) {
161 snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
164 if (parser_read_uint32(&p.rx.queue_size, tokens[6]) != 0) {
165 snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
169 p.rx.mempool_name = tokens[7];
171 if (strcmp(tokens[8], "txq") != 0) {
172 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "txq");
176 if (parser_read_uint32(&p.tx.n_queues, tokens[9]) != 0) {
177 snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
181 if (parser_read_uint32(&p.tx.queue_size, tokens[10]) != 0) {
182 snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
186 if (strcmp(tokens[11], "promiscuous") != 0) {
187 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "promiscuous");
191 if (strcmp(tokens[12], "on") == 0)
193 else if (strcmp(tokens[12], "off") == 0)
196 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "on or off");
203 uint32_t queue_id, i;
205 if (strcmp(tokens[13], "rss") != 0) {
206 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rss");
213 for (i = 14; i < n_tokens; i++) {
214 if (parser_read_uint32(&queue_id, tokens[i]) != 0) {
215 snprintf(out, out_size, MSG_ARG_INVALID,
220 rss.queue_id[rss.n_queues] = queue_id;
225 link = link_create(name, &p);
227 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
238 cmd_swq(char **tokens,
248 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
254 if (strcmp(tokens[2], "size") != 0) {
255 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "size");
259 if (parser_read_uint32(&p.size, tokens[3]) != 0) {
260 snprintf(out, out_size, MSG_ARG_INVALID, "size");
264 if (strcmp(tokens[4], "cpu") != 0) {
265 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
269 if (parser_read_uint32(&p.cpu_id, tokens[5]) != 0) {
270 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
274 swq = swq_create(name, &p);
276 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
282 * tmgr subport profile
283 * <tb_rate> <tb_size>
284 * <tc0_rate> <tc1_rate> <tc2_rate> <tc3_rate>
288 cmd_tmgr_subport_profile(char **tokens,
293 struct rte_sched_subport_params p;
296 if (n_tokens != 10) {
297 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
301 if (parser_read_uint32(&p.tb_rate, tokens[3]) != 0) {
302 snprintf(out, out_size, MSG_ARG_INVALID, "tb_rate");
306 if (parser_read_uint32(&p.tb_size, tokens[4]) != 0) {
307 snprintf(out, out_size, MSG_ARG_INVALID, "tb_size");
311 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
312 if (parser_read_uint32(&p.tc_rate[i], tokens[5 + i]) != 0) {
313 snprintf(out, out_size, MSG_ARG_INVALID, "tc_rate");
317 if (parser_read_uint32(&p.tc_period, tokens[9]) != 0) {
318 snprintf(out, out_size, MSG_ARG_INVALID, "tc_period");
322 status = tmgr_subport_profile_add(&p);
324 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
331 * <tb_rate> <tb_size>
332 * <tc0_rate> <tc1_rate> <tc2_rate> <tc3_rate>
338 cmd_tmgr_pipe_profile(char **tokens,
343 struct rte_sched_pipe_params p;
346 if (n_tokens != 27) {
347 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
351 if (parser_read_uint32(&p.tb_rate, tokens[3]) != 0) {
352 snprintf(out, out_size, MSG_ARG_INVALID, "tb_rate");
356 if (parser_read_uint32(&p.tb_size, tokens[4]) != 0) {
357 snprintf(out, out_size, MSG_ARG_INVALID, "tb_size");
361 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
362 if (parser_read_uint32(&p.tc_rate[i], tokens[5 + i]) != 0) {
363 snprintf(out, out_size, MSG_ARG_INVALID, "tc_rate");
367 if (parser_read_uint32(&p.tc_period, tokens[9]) != 0) {
368 snprintf(out, out_size, MSG_ARG_INVALID, "tc_period");
372 #ifdef RTE_SCHED_SUBPORT_TC_OV
373 if (parser_read_uint8(&p.tc_ov_weight, tokens[10]) != 0) {
374 snprintf(out, out_size, MSG_ARG_INVALID, "tc_ov_weight");
379 for (i = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++)
380 if (parser_read_uint8(&p.wrr_weights[i], tokens[11 + i]) != 0) {
381 snprintf(out, out_size, MSG_ARG_INVALID, "wrr_weights");
385 status = tmgr_pipe_profile_add(&p);
387 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
395 * spp <n_subports_per_port>
396 * pps <n_pipes_per_subport>
397 * qsize <qsize_tc0> <qsize_tc1> <qsize_tc2> <qsize_tc3>
398 * fo <frame_overhead>
403 cmd_tmgr(char **tokens,
408 struct tmgr_port_params p;
410 struct tmgr_port *tmgr_port;
413 if (n_tokens != 19) {
414 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
420 if (strcmp(tokens[2], "rate") != 0) {
421 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rate");
425 if (parser_read_uint32(&p.rate, tokens[3]) != 0) {
426 snprintf(out, out_size, MSG_ARG_INVALID, "rate");
430 if (strcmp(tokens[4], "spp") != 0) {
431 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "spp");
435 if (parser_read_uint32(&p.n_subports_per_port, tokens[5]) != 0) {
436 snprintf(out, out_size, MSG_ARG_INVALID, "n_subports_per_port");
440 if (strcmp(tokens[6], "pps") != 0) {
441 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pps");
445 if (parser_read_uint32(&p.n_pipes_per_subport, tokens[7]) != 0) {
446 snprintf(out, out_size, MSG_ARG_INVALID, "n_pipes_per_subport");
450 if (strcmp(tokens[8], "qsize") != 0) {
451 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "qsize");
455 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
456 if (parser_read_uint16(&p.qsize[i], tokens[9 + i]) != 0) {
457 snprintf(out, out_size, MSG_ARG_INVALID, "qsize");
461 if (strcmp(tokens[13], "fo") != 0) {
462 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "fo");
466 if (parser_read_uint32(&p.frame_overhead, tokens[14]) != 0) {
467 snprintf(out, out_size, MSG_ARG_INVALID, "frame_overhead");
471 if (strcmp(tokens[15], "mtu") != 0) {
472 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "mtu");
476 if (parser_read_uint32(&p.mtu, tokens[16]) != 0) {
477 snprintf(out, out_size, MSG_ARG_INVALID, "mtu");
481 if (strcmp(tokens[17], "cpu") != 0) {
482 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
486 if (parser_read_uint32(&p.cpu_id, tokens[18]) != 0) {
487 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
491 tmgr_port = tmgr_port_create(name, &p);
492 if (tmgr_port == NULL) {
493 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
499 * tmgr <tmgr_name> subport <subport_id>
500 * profile <subport_profile_id>
503 cmd_tmgr_subport(char **tokens,
508 uint32_t subport_id, subport_profile_id;
513 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
519 if (parser_read_uint32(&subport_id, tokens[3]) != 0) {
520 snprintf(out, out_size, MSG_ARG_INVALID, "subport_id");
524 if (parser_read_uint32(&subport_profile_id, tokens[5]) != 0) {
525 snprintf(out, out_size, MSG_ARG_INVALID, "subport_profile_id");
529 status = tmgr_subport_config(name, subport_id, subport_profile_id);
531 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
537 * tmgr <tmgr_name> subport <subport_id> pipe
538 * from <pipe_id_first> to <pipe_id_last>
539 * profile <pipe_profile_id>
542 cmd_tmgr_subport_pipe(char **tokens,
547 uint32_t subport_id, pipe_id_first, pipe_id_last, pipe_profile_id;
551 if (n_tokens != 11) {
552 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
558 if (parser_read_uint32(&subport_id, tokens[3]) != 0) {
559 snprintf(out, out_size, MSG_ARG_INVALID, "subport_id");
563 if (strcmp(tokens[4], "pipe") != 0) {
564 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pipe");
568 if (strcmp(tokens[5], "from") != 0) {
569 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "from");
573 if (parser_read_uint32(&pipe_id_first, tokens[6]) != 0) {
574 snprintf(out, out_size, MSG_ARG_INVALID, "pipe_id_first");
578 if (strcmp(tokens[7], "to") != 0) {
579 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "to");
583 if (parser_read_uint32(&pipe_id_last, tokens[8]) != 0) {
584 snprintf(out, out_size, MSG_ARG_INVALID, "pipe_id_last");
588 if (strcmp(tokens[9], "profile") != 0) {
589 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "profile");
593 if (parser_read_uint32(&pipe_profile_id, tokens[10]) != 0) {
594 snprintf(out, out_size, MSG_ARG_INVALID, "pipe_profile_id");
598 status = tmgr_pipe_config(name, subport_id, pipe_id_first,
599 pipe_id_last, pipe_profile_id);
601 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
607 cli_process(char *in, char *out, size_t out_size)
609 char *tokens[CMD_MAX_TOKENS];
610 uint32_t n_tokens = RTE_DIM(tokens);
616 status = parse_tokenize_string(in, tokens, &n_tokens);
618 snprintf(out, out_size, MSG_ARG_TOO_MANY, "");
625 if (strcmp(tokens[0], "mempool") == 0) {
626 cmd_mempool(tokens, n_tokens, out, out_size);
630 if (strcmp(tokens[0], "link") == 0) {
631 cmd_link(tokens, n_tokens, out, out_size);
635 if (strcmp(tokens[0], "swq") == 0) {
636 cmd_swq(tokens, n_tokens, out, out_size);
640 if (strcmp(tokens[0], "tmgr") == 0) {
641 if ((n_tokens >= 3) &&
642 (strcmp(tokens[1], "subport") == 0) &&
643 (strcmp(tokens[2], "profile") == 0)) {
644 cmd_tmgr_subport_profile(tokens, n_tokens,
649 if ((n_tokens >= 3) &&
650 (strcmp(tokens[1], "pipe") == 0) &&
651 (strcmp(tokens[2], "profile") == 0)) {
652 cmd_tmgr_pipe_profile(tokens, n_tokens, out, out_size);
656 if ((n_tokens >= 5) &&
657 (strcmp(tokens[2], "subport") == 0) &&
658 (strcmp(tokens[4], "profile") == 0)) {
659 cmd_tmgr_subport(tokens, n_tokens, out, out_size);
663 if ((n_tokens >= 5) &&
664 (strcmp(tokens[2], "subport") == 0) &&
665 (strcmp(tokens[4], "pipe") == 0)) {
666 cmd_tmgr_subport_pipe(tokens, n_tokens, out, out_size);
670 cmd_tmgr(tokens, n_tokens, out, out_size);
674 snprintf(out, out_size, MSG_CMD_UNKNOWN, tokens[0]);
678 cli_script_process(const char *file_name,
679 size_t msg_in_len_max,
680 size_t msg_out_len_max)
682 char *msg_in = NULL, *msg_out = NULL;
685 /* Check input arguments */
686 if ((file_name == NULL) ||
687 (strlen(file_name) == 0) ||
688 (msg_in_len_max == 0) ||
689 (msg_out_len_max == 0))
692 msg_in = malloc(msg_in_len_max + 1);
693 msg_out = malloc(msg_out_len_max + 1);
694 if ((msg_in == NULL) ||
701 /* Open input file */
702 f = fopen(file_name, "r");
711 if (fgets(msg_in, msg_in_len_max + 1, f) == NULL)
714 printf("%s", msg_in);
722 printf("%s", msg_out);