1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation.
5 #include <cmdline_rdline.h>
6 #include <cmdline_parse.h>
7 #include <cmdline_parse_ipaddr.h>
8 #include <cmdline_parse_num.h>
9 #include <cmdline_parse_string.h>
11 #include <rte_ethdev.h>
13 /**********************************************************/
15 struct cmd_help_result {
16 cmdline_fixed_string_t help;
19 static void cmd_help_parsed(__rte_unused void *parsed_result,
21 __rte_unused void *data)
25 "- attach <devargs>\n"
26 "- detach <devargs>\n"
30 cmdline_parse_token_string_t cmd_help_help =
31 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
33 cmdline_parse_inst_t cmd_help = {
34 .f = cmd_help_parsed, /* function to call */
35 .data = NULL, /* 2nd arg of func */
36 .help_str = "show help",
37 .tokens = { /* token list, NULL terminated */
38 (void *)&cmd_help_help,
43 /**********************************************************/
45 struct cmd_quit_result {
46 cmdline_fixed_string_t quit;
49 static void cmd_quit_parsed(__rte_unused void *parsed_result,
51 __rte_unused void *data)
56 cmdline_parse_token_string_t cmd_quit_quit =
57 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
59 cmdline_parse_inst_t cmd_quit = {
60 .f = cmd_quit_parsed, /* function to call */
61 .data = NULL, /* 2nd arg of func */
63 .tokens = { /* token list, NULL terminated */
64 (void *)&cmd_quit_quit,
69 /**********************************************************/
71 struct cmd_list_result {
72 cmdline_fixed_string_t list;
75 static void cmd_list_parsed(__rte_unused void *parsed_result,
77 __rte_unused void *data)
80 char dev_name[RTE_DEV_NAME_MAX_LEN];
82 cmdline_printf(cl, "list all etherdev\n");
84 RTE_ETH_FOREACH_DEV(port_id) {
85 rte_eth_dev_get_name_by_port(port_id, dev_name);
86 if (strlen(dev_name) > 0)
87 cmdline_printf(cl, "%d\t%s\n", port_id, dev_name);
89 printf("empty dev_name is not expected!\n");
93 cmdline_parse_token_string_t cmd_list_list =
94 TOKEN_STRING_INITIALIZER(struct cmd_list_result, list, "list");
96 cmdline_parse_inst_t cmd_list = {
97 .f = cmd_list_parsed, /* function to call */
98 .data = NULL, /* 2nd arg of func */
99 .help_str = "list all devices",
100 .tokens = { /* token list, NULL terminated */
101 (void *)&cmd_list_list,
106 /**********************************************************/
108 struct cmd_dev_attach_result {
109 cmdline_fixed_string_t attach;
110 cmdline_fixed_string_t devargs;
113 static void cmd_dev_attach_parsed(void *parsed_result,
115 __rte_unused void *data)
117 struct cmd_dev_attach_result *res = parsed_result;
118 struct rte_devargs da;
120 memset(&da, 0, sizeof(da));
122 if (rte_devargs_parsef(&da, "%s", res->devargs)) {
123 cmdline_printf(cl, "cannot parse devargs\n");
129 if (!rte_eal_hotplug_add(da.bus->name, da.name, da.args))
130 cmdline_printf(cl, "attached device %s\n", da.name);
132 cmdline_printf(cl, "failed to attached device %s\n",
136 cmdline_parse_token_string_t cmd_dev_attach_attach =
137 TOKEN_STRING_INITIALIZER(struct cmd_dev_attach_result, attach,
139 cmdline_parse_token_string_t cmd_dev_attach_devargs =
140 TOKEN_STRING_INITIALIZER(struct cmd_dev_attach_result, devargs, NULL);
142 cmdline_parse_inst_t cmd_attach_device = {
143 .f = cmd_dev_attach_parsed, /* function to call */
144 .data = NULL, /* 2nd arg of func */
145 .help_str = "attach a device",
146 .tokens = { /* token list, NULL terminated */
147 (void *)&cmd_dev_attach_attach,
148 (void *)&cmd_dev_attach_devargs,
153 /**********************************************************/
155 struct cmd_dev_detach_result {
156 cmdline_fixed_string_t detach;
157 cmdline_fixed_string_t devargs;
160 static void cmd_dev_detach_parsed(void *parsed_result,
162 __rte_unused void *data)
164 struct cmd_dev_detach_result *res = parsed_result;
165 struct rte_devargs da;
167 memset(&da, 0, sizeof(da));
169 if (rte_devargs_parsef(&da, "%s", res->devargs)) {
170 cmdline_printf(cl, "cannot parse devargs\n");
176 printf("detaching...\n");
177 if (!rte_eal_hotplug_remove(da.bus->name, da.name))
178 cmdline_printf(cl, "detached device %s\n",
181 cmdline_printf(cl, "failed to dettach device %s\n",
185 cmdline_parse_token_string_t cmd_dev_detach_detach =
186 TOKEN_STRING_INITIALIZER(struct cmd_dev_detach_result, detach,
189 cmdline_parse_token_string_t cmd_dev_detach_devargs =
190 TOKEN_STRING_INITIALIZER(struct cmd_dev_detach_result, devargs, NULL);
192 cmdline_parse_inst_t cmd_detach_device = {
193 .f = cmd_dev_detach_parsed, /* function to call */
194 .data = NULL, /* 2nd arg of func */
195 .help_str = "detach a device",
196 .tokens = { /* token list, NULL terminated */
197 (void *)&cmd_dev_detach_detach,
198 (void *)&cmd_dev_detach_devargs,
203 /**********************************************************/
204 /**********************************************************/
205 /****** CONTEXT (list of instruction) */
207 cmdline_parse_ctx_t main_ctx[] = {
208 (cmdline_parse_inst_t *)&cmd_help,
209 (cmdline_parse_inst_t *)&cmd_quit,
210 (cmdline_parse_inst_t *)&cmd_list,
211 (cmdline_parse_inst_t *)&cmd_attach_device,
212 (cmdline_parse_inst_t *)&cmd_detach_device,