1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
10 #include <rte_common.h>
21 #ifndef CMD_MAX_TOKENS
22 #define CMD_MAX_TOKENS 256
25 #define MSG_OUT_OF_MEMORY "Not enough memory.\n"
26 #define MSG_CMD_UNKNOWN "Unknown command \"%s\".\n"
27 #define MSG_CMD_UNIMPLEM "Command \"%s\" not implemented.\n"
28 #define MSG_ARG_NOT_ENOUGH "Not enough arguments for command \"%s\".\n"
29 #define MSG_ARG_TOO_MANY "Too many arguments for command \"%s\".\n"
30 #define MSG_ARG_MISMATCH "Wrong number of arguments for command \"%s\".\n"
31 #define MSG_ARG_NOT_FOUND "Argument \"%s\" not found.\n"
32 #define MSG_ARG_INVALID "Invalid value for argument \"%s\".\n"
33 #define MSG_FILE_ERR "Error in file \"%s\" at line %u.\n"
34 #define MSG_CMD_FAIL "Command \"%s\" failed.\n"
39 if ((strlen(in) && index("!#%;", in[0])) ||
40 (strncmp(in, "//", 2) == 0) ||
41 (strncmp(in, "--", 2) == 0))
48 * mempool <mempool_name>
49 * buffer <buffer_size>
55 cmd_mempool(char **tokens,
60 struct mempool_params p;
62 struct mempool *mempool;
65 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
71 if (strcmp(tokens[2], "buffer") != 0) {
72 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "buffer");
76 if (parser_read_uint32(&p.buffer_size, tokens[3]) != 0) {
77 snprintf(out, out_size, MSG_ARG_INVALID, "buffer_size");
81 if (strcmp(tokens[4], "pool") != 0) {
82 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pool");
86 if (parser_read_uint32(&p.pool_size, tokens[5]) != 0) {
87 snprintf(out, out_size, MSG_ARG_INVALID, "pool_size");
91 if (strcmp(tokens[6], "cache") != 0) {
92 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cache");
96 if (parser_read_uint32(&p.cache_size, tokens[7]) != 0) {
97 snprintf(out, out_size, MSG_ARG_INVALID, "cache_size");
101 if (strcmp(tokens[8], "cpu") != 0) {
102 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
106 if (parser_read_uint32(&p.cpu_id, tokens[9]) != 0) {
107 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
111 mempool = mempool_create(name, &p);
112 if (mempool == NULL) {
113 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
120 * dev <device_name> | port <port_id>
121 * rxq <n_queues> <queue_size> <mempool_name>
122 * txq <n_queues> <queue_size>
123 * promiscuous on | off
124 * [rss <qid_0> ... <qid_n>]
127 cmd_link(char **tokens,
132 struct link_params p;
133 struct link_params_rss rss;
137 if ((n_tokens < 13) || (n_tokens > 14 + LINK_RXQ_RSS_MAX)) {
138 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
143 if (strcmp(tokens[2], "dev") == 0)
144 p.dev_name = tokens[3];
145 else if (strcmp(tokens[2], "port") == 0) {
148 if (parser_read_uint16(&p.port_id, tokens[3]) != 0) {
149 snprintf(out, out_size, MSG_ARG_INVALID, "port_id");
153 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "dev or port");
157 if (strcmp(tokens[4], "rxq") != 0) {
158 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rxq");
162 if (parser_read_uint32(&p.rx.n_queues, tokens[5]) != 0) {
163 snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
166 if (parser_read_uint32(&p.rx.queue_size, tokens[6]) != 0) {
167 snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
171 p.rx.mempool_name = tokens[7];
173 if (strcmp(tokens[8], "txq") != 0) {
174 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "txq");
178 if (parser_read_uint32(&p.tx.n_queues, tokens[9]) != 0) {
179 snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
183 if (parser_read_uint32(&p.tx.queue_size, tokens[10]) != 0) {
184 snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
188 if (strcmp(tokens[11], "promiscuous") != 0) {
189 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "promiscuous");
193 if (strcmp(tokens[12], "on") == 0)
195 else if (strcmp(tokens[12], "off") == 0)
198 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "on or off");
205 uint32_t queue_id, i;
207 if (strcmp(tokens[13], "rss") != 0) {
208 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rss");
215 for (i = 14; i < n_tokens; i++) {
216 if (parser_read_uint32(&queue_id, tokens[i]) != 0) {
217 snprintf(out, out_size, MSG_ARG_INVALID,
222 rss.queue_id[rss.n_queues] = queue_id;
227 link = link_create(name, &p);
229 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
240 cmd_swq(char **tokens,
250 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
256 if (strcmp(tokens[2], "size") != 0) {
257 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "size");
261 if (parser_read_uint32(&p.size, tokens[3]) != 0) {
262 snprintf(out, out_size, MSG_ARG_INVALID, "size");
266 if (strcmp(tokens[4], "cpu") != 0) {
267 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
271 if (parser_read_uint32(&p.cpu_id, tokens[5]) != 0) {
272 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
276 swq = swq_create(name, &p);
278 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
284 * tmgr subport profile
285 * <tb_rate> <tb_size>
286 * <tc0_rate> <tc1_rate> <tc2_rate> <tc3_rate>
290 cmd_tmgr_subport_profile(char **tokens,
295 struct rte_sched_subport_params p;
298 if (n_tokens != 10) {
299 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
303 if (parser_read_uint32(&p.tb_rate, tokens[3]) != 0) {
304 snprintf(out, out_size, MSG_ARG_INVALID, "tb_rate");
308 if (parser_read_uint32(&p.tb_size, tokens[4]) != 0) {
309 snprintf(out, out_size, MSG_ARG_INVALID, "tb_size");
313 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
314 if (parser_read_uint32(&p.tc_rate[i], tokens[5 + i]) != 0) {
315 snprintf(out, out_size, MSG_ARG_INVALID, "tc_rate");
319 if (parser_read_uint32(&p.tc_period, tokens[9]) != 0) {
320 snprintf(out, out_size, MSG_ARG_INVALID, "tc_period");
324 status = tmgr_subport_profile_add(&p);
326 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
333 * <tb_rate> <tb_size>
334 * <tc0_rate> <tc1_rate> <tc2_rate> <tc3_rate>
340 cmd_tmgr_pipe_profile(char **tokens,
345 struct rte_sched_pipe_params p;
348 if (n_tokens != 27) {
349 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
353 if (parser_read_uint32(&p.tb_rate, tokens[3]) != 0) {
354 snprintf(out, out_size, MSG_ARG_INVALID, "tb_rate");
358 if (parser_read_uint32(&p.tb_size, tokens[4]) != 0) {
359 snprintf(out, out_size, MSG_ARG_INVALID, "tb_size");
363 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
364 if (parser_read_uint32(&p.tc_rate[i], tokens[5 + i]) != 0) {
365 snprintf(out, out_size, MSG_ARG_INVALID, "tc_rate");
369 if (parser_read_uint32(&p.tc_period, tokens[9]) != 0) {
370 snprintf(out, out_size, MSG_ARG_INVALID, "tc_period");
374 #ifdef RTE_SCHED_SUBPORT_TC_OV
375 if (parser_read_uint8(&p.tc_ov_weight, tokens[10]) != 0) {
376 snprintf(out, out_size, MSG_ARG_INVALID, "tc_ov_weight");
381 for (i = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++)
382 if (parser_read_uint8(&p.wrr_weights[i], tokens[11 + i]) != 0) {
383 snprintf(out, out_size, MSG_ARG_INVALID, "wrr_weights");
387 status = tmgr_pipe_profile_add(&p);
389 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
397 * spp <n_subports_per_port>
398 * pps <n_pipes_per_subport>
399 * qsize <qsize_tc0> <qsize_tc1> <qsize_tc2> <qsize_tc3>
400 * fo <frame_overhead>
405 cmd_tmgr(char **tokens,
410 struct tmgr_port_params p;
412 struct tmgr_port *tmgr_port;
415 if (n_tokens != 19) {
416 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
422 if (strcmp(tokens[2], "rate") != 0) {
423 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rate");
427 if (parser_read_uint32(&p.rate, tokens[3]) != 0) {
428 snprintf(out, out_size, MSG_ARG_INVALID, "rate");
432 if (strcmp(tokens[4], "spp") != 0) {
433 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "spp");
437 if (parser_read_uint32(&p.n_subports_per_port, tokens[5]) != 0) {
438 snprintf(out, out_size, MSG_ARG_INVALID, "n_subports_per_port");
442 if (strcmp(tokens[6], "pps") != 0) {
443 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pps");
447 if (parser_read_uint32(&p.n_pipes_per_subport, tokens[7]) != 0) {
448 snprintf(out, out_size, MSG_ARG_INVALID, "n_pipes_per_subport");
452 if (strcmp(tokens[8], "qsize") != 0) {
453 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "qsize");
457 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
458 if (parser_read_uint16(&p.qsize[i], tokens[9 + i]) != 0) {
459 snprintf(out, out_size, MSG_ARG_INVALID, "qsize");
463 if (strcmp(tokens[13], "fo") != 0) {
464 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "fo");
468 if (parser_read_uint32(&p.frame_overhead, tokens[14]) != 0) {
469 snprintf(out, out_size, MSG_ARG_INVALID, "frame_overhead");
473 if (strcmp(tokens[15], "mtu") != 0) {
474 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "mtu");
478 if (parser_read_uint32(&p.mtu, tokens[16]) != 0) {
479 snprintf(out, out_size, MSG_ARG_INVALID, "mtu");
483 if (strcmp(tokens[17], "cpu") != 0) {
484 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
488 if (parser_read_uint32(&p.cpu_id, tokens[18]) != 0) {
489 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
493 tmgr_port = tmgr_port_create(name, &p);
494 if (tmgr_port == NULL) {
495 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
501 * tmgr <tmgr_name> subport <subport_id>
502 * profile <subport_profile_id>
505 cmd_tmgr_subport(char **tokens,
510 uint32_t subport_id, subport_profile_id;
515 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
521 if (parser_read_uint32(&subport_id, tokens[3]) != 0) {
522 snprintf(out, out_size, MSG_ARG_INVALID, "subport_id");
526 if (parser_read_uint32(&subport_profile_id, tokens[5]) != 0) {
527 snprintf(out, out_size, MSG_ARG_INVALID, "subport_profile_id");
531 status = tmgr_subport_config(name, subport_id, subport_profile_id);
533 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
539 * tmgr <tmgr_name> subport <subport_id> pipe
540 * from <pipe_id_first> to <pipe_id_last>
541 * profile <pipe_profile_id>
544 cmd_tmgr_subport_pipe(char **tokens,
549 uint32_t subport_id, pipe_id_first, pipe_id_last, pipe_profile_id;
553 if (n_tokens != 11) {
554 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
560 if (parser_read_uint32(&subport_id, tokens[3]) != 0) {
561 snprintf(out, out_size, MSG_ARG_INVALID, "subport_id");
565 if (strcmp(tokens[4], "pipe") != 0) {
566 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pipe");
570 if (strcmp(tokens[5], "from") != 0) {
571 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "from");
575 if (parser_read_uint32(&pipe_id_first, tokens[6]) != 0) {
576 snprintf(out, out_size, MSG_ARG_INVALID, "pipe_id_first");
580 if (strcmp(tokens[7], "to") != 0) {
581 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "to");
585 if (parser_read_uint32(&pipe_id_last, tokens[8]) != 0) {
586 snprintf(out, out_size, MSG_ARG_INVALID, "pipe_id_last");
590 if (strcmp(tokens[9], "profile") != 0) {
591 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "profile");
595 if (parser_read_uint32(&pipe_profile_id, tokens[10]) != 0) {
596 snprintf(out, out_size, MSG_ARG_INVALID, "pipe_profile_id");
600 status = tmgr_pipe_config(name, subport_id, pipe_id_first,
601 pipe_id_last, pipe_profile_id);
603 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
612 cmd_tap(char **tokens,
621 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
627 tap = tap_create(name);
629 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
637 * mempool <mempool_name>
638 * [thread <thread_id>]
641 cmd_kni(char **tokens,
650 if ((n_tokens != 6) && (n_tokens != 8)) {
651 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
657 if (strcmp(tokens[2], "link") != 0) {
658 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "link");
662 p.link_name = tokens[3];
664 if (strcmp(tokens[4], "mempool") != 0) {
665 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "mempool");
669 p.mempool_name = tokens[5];
672 if (strcmp(tokens[6], "thread") != 0) {
673 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "thread");
677 if (parser_read_uint32(&p.thread_id, tokens[7]) != 0) {
678 snprintf(out, out_size, MSG_ARG_INVALID, "thread_id");
686 kni = kni_create(name, &p);
688 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
694 cli_process(char *in, char *out, size_t out_size)
696 char *tokens[CMD_MAX_TOKENS];
697 uint32_t n_tokens = RTE_DIM(tokens);
703 status = parse_tokenize_string(in, tokens, &n_tokens);
705 snprintf(out, out_size, MSG_ARG_TOO_MANY, "");
712 if (strcmp(tokens[0], "mempool") == 0) {
713 cmd_mempool(tokens, n_tokens, out, out_size);
717 if (strcmp(tokens[0], "link") == 0) {
718 cmd_link(tokens, n_tokens, out, out_size);
722 if (strcmp(tokens[0], "swq") == 0) {
723 cmd_swq(tokens, n_tokens, out, out_size);
727 if (strcmp(tokens[0], "tmgr") == 0) {
728 if ((n_tokens >= 3) &&
729 (strcmp(tokens[1], "subport") == 0) &&
730 (strcmp(tokens[2], "profile") == 0)) {
731 cmd_tmgr_subport_profile(tokens, n_tokens,
736 if ((n_tokens >= 3) &&
737 (strcmp(tokens[1], "pipe") == 0) &&
738 (strcmp(tokens[2], "profile") == 0)) {
739 cmd_tmgr_pipe_profile(tokens, n_tokens, out, out_size);
743 if ((n_tokens >= 5) &&
744 (strcmp(tokens[2], "subport") == 0) &&
745 (strcmp(tokens[4], "profile") == 0)) {
746 cmd_tmgr_subport(tokens, n_tokens, out, out_size);
750 if ((n_tokens >= 5) &&
751 (strcmp(tokens[2], "subport") == 0) &&
752 (strcmp(tokens[4], "pipe") == 0)) {
753 cmd_tmgr_subport_pipe(tokens, n_tokens, out, out_size);
757 cmd_tmgr(tokens, n_tokens, out, out_size);
761 if (strcmp(tokens[0], "tap") == 0) {
762 cmd_tap(tokens, n_tokens, out, out_size);
766 if (strcmp(tokens[0], "kni") == 0) {
767 cmd_kni(tokens, n_tokens, out, out_size);
771 snprintf(out, out_size, MSG_CMD_UNKNOWN, tokens[0]);
775 cli_script_process(const char *file_name,
776 size_t msg_in_len_max,
777 size_t msg_out_len_max)
779 char *msg_in = NULL, *msg_out = NULL;
782 /* Check input arguments */
783 if ((file_name == NULL) ||
784 (strlen(file_name) == 0) ||
785 (msg_in_len_max == 0) ||
786 (msg_out_len_max == 0))
789 msg_in = malloc(msg_in_len_max + 1);
790 msg_out = malloc(msg_out_len_max + 1);
791 if ((msg_in == NULL) ||
798 /* Open input file */
799 f = fopen(file_name, "r");
808 if (fgets(msg_in, msg_in_len_max + 1, f) == NULL)
811 printf("%s", msg_in);
819 printf("%s", msg_out);