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");
127 if (!rte_eal_hotplug_add(da.bus->name, da.name, da.args))
128 cmdline_printf(cl, "attached device %s\n", da.name);
130 cmdline_printf(cl, "failed to attached device %s\n",
132 rte_devargs_reset(&da);
135 cmdline_parse_token_string_t cmd_dev_attach_attach =
136 TOKEN_STRING_INITIALIZER(struct cmd_dev_attach_result, attach,
138 cmdline_parse_token_string_t cmd_dev_attach_devargs =
139 TOKEN_STRING_INITIALIZER(struct cmd_dev_attach_result, devargs, NULL);
141 cmdline_parse_inst_t cmd_attach_device = {
142 .f = cmd_dev_attach_parsed, /* function to call */
143 .data = NULL, /* 2nd arg of func */
144 .help_str = "attach a device",
145 .tokens = { /* token list, NULL terminated */
146 (void *)&cmd_dev_attach_attach,
147 (void *)&cmd_dev_attach_devargs,
152 /**********************************************************/
154 struct cmd_dev_detach_result {
155 cmdline_fixed_string_t detach;
156 cmdline_fixed_string_t devargs;
159 static void cmd_dev_detach_parsed(void *parsed_result,
161 __rte_unused void *data)
163 struct cmd_dev_detach_result *res = parsed_result;
164 struct rte_devargs da;
166 memset(&da, 0, sizeof(da));
168 if (rte_devargs_parsef(&da, "%s", res->devargs)) {
169 cmdline_printf(cl, "cannot parse devargs\n");
173 printf("detaching...\n");
174 if (!rte_eal_hotplug_remove(da.bus->name, da.name))
175 cmdline_printf(cl, "detached device %s\n",
178 cmdline_printf(cl, "failed to detach device %s\n",
180 rte_devargs_reset(&da);
183 cmdline_parse_token_string_t cmd_dev_detach_detach =
184 TOKEN_STRING_INITIALIZER(struct cmd_dev_detach_result, detach,
187 cmdline_parse_token_string_t cmd_dev_detach_devargs =
188 TOKEN_STRING_INITIALIZER(struct cmd_dev_detach_result, devargs, NULL);
190 cmdline_parse_inst_t cmd_detach_device = {
191 .f = cmd_dev_detach_parsed, /* function to call */
192 .data = NULL, /* 2nd arg of func */
193 .help_str = "detach a device",
194 .tokens = { /* token list, NULL terminated */
195 (void *)&cmd_dev_detach_detach,
196 (void *)&cmd_dev_detach_devargs,
201 /**********************************************************/
202 /**********************************************************/
203 /****** CONTEXT (list of instruction) */
205 cmdline_parse_ctx_t main_ctx[] = {
206 (cmdline_parse_inst_t *)&cmd_help,
207 (cmdline_parse_inst_t *)&cmd_quit,
208 (cmdline_parse_inst_t *)&cmd_list,
209 (cmdline_parse_inst_t *)&cmd_attach_device,
210 (cmdline_parse_inst_t *)&cmd_detach_device,