1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
10 #include <rte_common.h>
20 #ifndef CMD_MAX_TOKENS
21 #define CMD_MAX_TOKENS 256
24 #define MSG_OUT_OF_MEMORY "Not enough memory.\n"
25 #define MSG_CMD_UNKNOWN "Unknown command \"%s\".\n"
26 #define MSG_CMD_UNIMPLEM "Command \"%s\" not implemented.\n"
27 #define MSG_ARG_NOT_ENOUGH "Not enough arguments for command \"%s\".\n"
28 #define MSG_ARG_TOO_MANY "Too many arguments for command \"%s\".\n"
29 #define MSG_ARG_MISMATCH "Wrong number of arguments for command \"%s\".\n"
30 #define MSG_ARG_NOT_FOUND "Argument \"%s\" not found.\n"
31 #define MSG_ARG_INVALID "Invalid value for argument \"%s\".\n"
32 #define MSG_FILE_ERR "Error in file \"%s\" at line %u.\n"
33 #define MSG_CMD_FAIL "Command \"%s\" failed.\n"
38 if ((strlen(in) && index("!#%;", in[0])) ||
39 (strncmp(in, "//", 2) == 0) ||
40 (strncmp(in, "--", 2) == 0))
47 * mempool <mempool_name>
48 * buffer <buffer_size>
54 cmd_mempool(char **tokens,
59 struct mempool_params p;
61 struct mempool *mempool;
64 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
70 if (strcmp(tokens[2], "buffer") != 0) {
71 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "buffer");
75 if (parser_read_uint32(&p.buffer_size, tokens[3]) != 0) {
76 snprintf(out, out_size, MSG_ARG_INVALID, "buffer_size");
80 if (strcmp(tokens[4], "pool") != 0) {
81 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pool");
85 if (parser_read_uint32(&p.pool_size, tokens[5]) != 0) {
86 snprintf(out, out_size, MSG_ARG_INVALID, "pool_size");
90 if (strcmp(tokens[6], "cache") != 0) {
91 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cache");
95 if (parser_read_uint32(&p.cache_size, tokens[7]) != 0) {
96 snprintf(out, out_size, MSG_ARG_INVALID, "cache_size");
100 if (strcmp(tokens[8], "cpu") != 0) {
101 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
105 if (parser_read_uint32(&p.cpu_id, tokens[9]) != 0) {
106 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
110 mempool = mempool_create(name, &p);
111 if (mempool == NULL) {
112 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
119 * dev <device_name> | port <port_id>
120 * rxq <n_queues> <queue_size> <mempool_name>
121 * txq <n_queues> <queue_size>
122 * promiscuous on | off
123 * [rss <qid_0> ... <qid_n>]
126 cmd_link(char **tokens,
131 struct link_params p;
132 struct link_params_rss rss;
136 if ((n_tokens < 13) || (n_tokens > 14 + LINK_RXQ_RSS_MAX)) {
137 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
142 if (strcmp(tokens[2], "dev") == 0)
143 p.dev_name = tokens[3];
144 else if (strcmp(tokens[2], "port") == 0) {
147 if (parser_read_uint16(&p.port_id, tokens[3]) != 0) {
148 snprintf(out, out_size, MSG_ARG_INVALID, "port_id");
152 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "dev or port");
156 if (strcmp(tokens[4], "rxq") != 0) {
157 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rxq");
161 if (parser_read_uint32(&p.rx.n_queues, tokens[5]) != 0) {
162 snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
165 if (parser_read_uint32(&p.rx.queue_size, tokens[6]) != 0) {
166 snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
170 p.rx.mempool_name = tokens[7];
172 if (strcmp(tokens[8], "txq") != 0) {
173 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "txq");
177 if (parser_read_uint32(&p.tx.n_queues, tokens[9]) != 0) {
178 snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
182 if (parser_read_uint32(&p.tx.queue_size, tokens[10]) != 0) {
183 snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
187 if (strcmp(tokens[11], "promiscuous") != 0) {
188 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "promiscuous");
192 if (strcmp(tokens[12], "on") == 0)
194 else if (strcmp(tokens[12], "off") == 0)
197 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "on or off");
204 uint32_t queue_id, i;
206 if (strcmp(tokens[13], "rss") != 0) {
207 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rss");
214 for (i = 14; i < n_tokens; i++) {
215 if (parser_read_uint32(&queue_id, tokens[i]) != 0) {
216 snprintf(out, out_size, MSG_ARG_INVALID,
221 rss.queue_id[rss.n_queues] = queue_id;
226 link = link_create(name, &p);
228 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
239 cmd_swq(char **tokens,
249 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
255 if (strcmp(tokens[2], "size") != 0) {
256 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "size");
260 if (parser_read_uint32(&p.size, tokens[3]) != 0) {
261 snprintf(out, out_size, MSG_ARG_INVALID, "size");
265 if (strcmp(tokens[4], "cpu") != 0) {
266 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
270 if (parser_read_uint32(&p.cpu_id, tokens[5]) != 0) {
271 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
275 swq = swq_create(name, &p);
277 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
283 * tmgr subport profile
284 * <tb_rate> <tb_size>
285 * <tc0_rate> <tc1_rate> <tc2_rate> <tc3_rate>
289 cmd_tmgr_subport_profile(char **tokens,
294 struct rte_sched_subport_params p;
297 if (n_tokens != 10) {
298 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
302 if (parser_read_uint32(&p.tb_rate, tokens[3]) != 0) {
303 snprintf(out, out_size, MSG_ARG_INVALID, "tb_rate");
307 if (parser_read_uint32(&p.tb_size, tokens[4]) != 0) {
308 snprintf(out, out_size, MSG_ARG_INVALID, "tb_size");
312 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
313 if (parser_read_uint32(&p.tc_rate[i], tokens[5 + i]) != 0) {
314 snprintf(out, out_size, MSG_ARG_INVALID, "tc_rate");
318 if (parser_read_uint32(&p.tc_period, tokens[9]) != 0) {
319 snprintf(out, out_size, MSG_ARG_INVALID, "tc_period");
323 status = tmgr_subport_profile_add(&p);
325 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
332 * <tb_rate> <tb_size>
333 * <tc0_rate> <tc1_rate> <tc2_rate> <tc3_rate>
339 cmd_tmgr_pipe_profile(char **tokens,
344 struct rte_sched_pipe_params p;
347 if (n_tokens != 27) {
348 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
352 if (parser_read_uint32(&p.tb_rate, tokens[3]) != 0) {
353 snprintf(out, out_size, MSG_ARG_INVALID, "tb_rate");
357 if (parser_read_uint32(&p.tb_size, tokens[4]) != 0) {
358 snprintf(out, out_size, MSG_ARG_INVALID, "tb_size");
362 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
363 if (parser_read_uint32(&p.tc_rate[i], tokens[5 + i]) != 0) {
364 snprintf(out, out_size, MSG_ARG_INVALID, "tc_rate");
368 if (parser_read_uint32(&p.tc_period, tokens[9]) != 0) {
369 snprintf(out, out_size, MSG_ARG_INVALID, "tc_period");
373 #ifdef RTE_SCHED_SUBPORT_TC_OV
374 if (parser_read_uint8(&p.tc_ov_weight, tokens[10]) != 0) {
375 snprintf(out, out_size, MSG_ARG_INVALID, "tc_ov_weight");
380 for (i = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++)
381 if (parser_read_uint8(&p.wrr_weights[i], tokens[11 + i]) != 0) {
382 snprintf(out, out_size, MSG_ARG_INVALID, "wrr_weights");
386 status = tmgr_pipe_profile_add(&p);
388 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
396 * spp <n_subports_per_port>
397 * pps <n_pipes_per_subport>
398 * qsize <qsize_tc0> <qsize_tc1> <qsize_tc2> <qsize_tc3>
399 * fo <frame_overhead>
404 cmd_tmgr(char **tokens,
409 struct tmgr_port_params p;
411 struct tmgr_port *tmgr_port;
414 if (n_tokens != 19) {
415 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
421 if (strcmp(tokens[2], "rate") != 0) {
422 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rate");
426 if (parser_read_uint32(&p.rate, tokens[3]) != 0) {
427 snprintf(out, out_size, MSG_ARG_INVALID, "rate");
431 if (strcmp(tokens[4], "spp") != 0) {
432 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "spp");
436 if (parser_read_uint32(&p.n_subports_per_port, tokens[5]) != 0) {
437 snprintf(out, out_size, MSG_ARG_INVALID, "n_subports_per_port");
441 if (strcmp(tokens[6], "pps") != 0) {
442 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pps");
446 if (parser_read_uint32(&p.n_pipes_per_subport, tokens[7]) != 0) {
447 snprintf(out, out_size, MSG_ARG_INVALID, "n_pipes_per_subport");
451 if (strcmp(tokens[8], "qsize") != 0) {
452 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "qsize");
456 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
457 if (parser_read_uint16(&p.qsize[i], tokens[9 + i]) != 0) {
458 snprintf(out, out_size, MSG_ARG_INVALID, "qsize");
462 if (strcmp(tokens[13], "fo") != 0) {
463 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "fo");
467 if (parser_read_uint32(&p.frame_overhead, tokens[14]) != 0) {
468 snprintf(out, out_size, MSG_ARG_INVALID, "frame_overhead");
472 if (strcmp(tokens[15], "mtu") != 0) {
473 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "mtu");
477 if (parser_read_uint32(&p.mtu, tokens[16]) != 0) {
478 snprintf(out, out_size, MSG_ARG_INVALID, "mtu");
482 if (strcmp(tokens[17], "cpu") != 0) {
483 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
487 if (parser_read_uint32(&p.cpu_id, tokens[18]) != 0) {
488 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
492 tmgr_port = tmgr_port_create(name, &p);
493 if (tmgr_port == NULL) {
494 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
500 * tmgr <tmgr_name> subport <subport_id>
501 * profile <subport_profile_id>
504 cmd_tmgr_subport(char **tokens,
509 uint32_t subport_id, subport_profile_id;
514 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
520 if (parser_read_uint32(&subport_id, tokens[3]) != 0) {
521 snprintf(out, out_size, MSG_ARG_INVALID, "subport_id");
525 if (parser_read_uint32(&subport_profile_id, tokens[5]) != 0) {
526 snprintf(out, out_size, MSG_ARG_INVALID, "subport_profile_id");
530 status = tmgr_subport_config(name, subport_id, subport_profile_id);
532 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
538 * tmgr <tmgr_name> subport <subport_id> pipe
539 * from <pipe_id_first> to <pipe_id_last>
540 * profile <pipe_profile_id>
543 cmd_tmgr_subport_pipe(char **tokens,
548 uint32_t subport_id, pipe_id_first, pipe_id_last, pipe_profile_id;
552 if (n_tokens != 11) {
553 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
559 if (parser_read_uint32(&subport_id, tokens[3]) != 0) {
560 snprintf(out, out_size, MSG_ARG_INVALID, "subport_id");
564 if (strcmp(tokens[4], "pipe") != 0) {
565 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pipe");
569 if (strcmp(tokens[5], "from") != 0) {
570 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "from");
574 if (parser_read_uint32(&pipe_id_first, tokens[6]) != 0) {
575 snprintf(out, out_size, MSG_ARG_INVALID, "pipe_id_first");
579 if (strcmp(tokens[7], "to") != 0) {
580 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "to");
584 if (parser_read_uint32(&pipe_id_last, tokens[8]) != 0) {
585 snprintf(out, out_size, MSG_ARG_INVALID, "pipe_id_last");
589 if (strcmp(tokens[9], "profile") != 0) {
590 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "profile");
594 if (parser_read_uint32(&pipe_profile_id, tokens[10]) != 0) {
595 snprintf(out, out_size, MSG_ARG_INVALID, "pipe_profile_id");
599 status = tmgr_pipe_config(name, subport_id, pipe_id_first,
600 pipe_id_last, pipe_profile_id);
602 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
611 cmd_tap(char **tokens,
620 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
626 tap = tap_create(name);
628 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
634 cli_process(char *in, char *out, size_t out_size)
636 char *tokens[CMD_MAX_TOKENS];
637 uint32_t n_tokens = RTE_DIM(tokens);
643 status = parse_tokenize_string(in, tokens, &n_tokens);
645 snprintf(out, out_size, MSG_ARG_TOO_MANY, "");
652 if (strcmp(tokens[0], "mempool") == 0) {
653 cmd_mempool(tokens, n_tokens, out, out_size);
657 if (strcmp(tokens[0], "link") == 0) {
658 cmd_link(tokens, n_tokens, out, out_size);
662 if (strcmp(tokens[0], "swq") == 0) {
663 cmd_swq(tokens, n_tokens, out, out_size);
667 if (strcmp(tokens[0], "tmgr") == 0) {
668 if ((n_tokens >= 3) &&
669 (strcmp(tokens[1], "subport") == 0) &&
670 (strcmp(tokens[2], "profile") == 0)) {
671 cmd_tmgr_subport_profile(tokens, n_tokens,
676 if ((n_tokens >= 3) &&
677 (strcmp(tokens[1], "pipe") == 0) &&
678 (strcmp(tokens[2], "profile") == 0)) {
679 cmd_tmgr_pipe_profile(tokens, n_tokens, out, out_size);
683 if ((n_tokens >= 5) &&
684 (strcmp(tokens[2], "subport") == 0) &&
685 (strcmp(tokens[4], "profile") == 0)) {
686 cmd_tmgr_subport(tokens, n_tokens, out, out_size);
690 if ((n_tokens >= 5) &&
691 (strcmp(tokens[2], "subport") == 0) &&
692 (strcmp(tokens[4], "pipe") == 0)) {
693 cmd_tmgr_subport_pipe(tokens, n_tokens, out, out_size);
697 cmd_tmgr(tokens, n_tokens, out, out_size);
701 if (strcmp(tokens[0], "tap") == 0) {
702 cmd_tap(tokens, n_tokens, out, out_size);
706 snprintf(out, out_size, MSG_CMD_UNKNOWN, tokens[0]);
710 cli_script_process(const char *file_name,
711 size_t msg_in_len_max,
712 size_t msg_out_len_max)
714 char *msg_in = NULL, *msg_out = NULL;
717 /* Check input arguments */
718 if ((file_name == NULL) ||
719 (strlen(file_name) == 0) ||
720 (msg_in_len_max == 0) ||
721 (msg_out_len_max == 0))
724 msg_in = malloc(msg_in_len_max + 1);
725 msg_out = malloc(msg_out_len_max + 1);
726 if ((msg_in == NULL) ||
733 /* Open input file */
734 f = fopen(file_name, "r");
743 if (fgets(msg_in, msg_in_len_max + 1, f) == NULL)
746 printf("%s", msg_in);
754 printf("%s", msg_out);