aboutsummaryrefslogtreecommitdiffstats
path: root/examples/multi_process/hotplug_mp/commands.c
blob: b068593939d8c9f1526fd9ffc8c3c389a34afbf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018 Intel Corporation.
 */

#include <cmdline_rdline.h>
#include <cmdline_parse.h>
#include <cmdline_parse_ipaddr.h>
#include <cmdline_parse_num.h>
#include <cmdline_parse_string.h>
#include <cmdline.h>
#include <rte_ethdev.h>

/**********************************************************/

struct cmd_help_result {
	cmdline_fixed_string_t help;
};

static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
			    struct cmdline *cl,
			    __attribute__((unused)) void *data)
{
	cmdline_printf(cl,
		       "commands:\n"
		       "- attach <devargs>\n"
		       "- detach <devargs>\n"
		       "- list\n\n");
}

cmdline_parse_token_string_t cmd_help_help =
	TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");

cmdline_parse_inst_t cmd_help = {
	.f = cmd_help_parsed,  /* function to call */
	.data = NULL,      /* 2nd arg of func */
	.help_str = "show help",
	.tokens = {        /* token list, NULL terminated */
		(void *)&cmd_help_help,
		NULL,
	},
};

/**********************************************************/

struct cmd_quit_result {
	cmdline_fixed_string_t quit;
};

static void cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
			    struct cmdline *cl,
			    __attribute__((unused)) void *data)
{
	cmdline_quit(cl);
}

cmdline_parse_token_string_t cmd_quit_quit =
	TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");

cmdline_parse_inst_t cmd_quit = {
	.f = cmd_quit_parsed,  /* function to call */
	.data = NULL,      /* 2nd arg of func */
	.help_str = "quit",
	.tokens = {        /* token list, NULL terminated */
		(void *)&cmd_quit_quit,
		NULL,
	},
};

/**********************************************************/

struct cmd_list_result {
	cmdline_fixed_string_t list;
};

static void cmd_list_parsed(__attribute__((unused)) void *parsed_result,
			    struct cmdline *cl,
			    __attribute__((unused)) void *data)
{
	uint16_t port_id;
	char dev_name[RTE_DEV_NAME_MAX_LEN];

	cmdline_printf(cl, "list all etherdev\n");

	RTE_ETH_FOREACH_DEV(port_id) {
		rte_eth_dev_get_name_by_port(port_id, dev_name);
		if (strlen(dev_name) > 0)
			cmdline_printf(cl, "%d\t%s\n", port_id, dev_name);
		else
			printf("empty dev_name is not expected!\n");
	}
}

cmdline_parse_token_string_t cmd_list_list =
	TOKEN_STRING_INITIALIZER(struct cmd_list_result, list, "list");

cmdline_parse_inst_t cmd_list = {
	.f = cmd_list_parsed,  /* function to call */
	.data = NULL,      /* 2nd arg of func */
	.help_str = "list all devices",
	.tokens = {        /* token list, NULL terminated */
		(void *)&cmd_list_list,
		NULL,
	},
};

/**********************************************************/

struct cmd_dev_attach_result {
	cmdline_fixed_string_t attach;
	cmdline_fixed_string_t devargs;
};

static void cmd_dev_attach_parsed(void *parsed_result,
				  struct cmdline *cl,
				  __attribute__((unused)) void *data)
{
	struct cmd_dev_attach_result *res = parsed_result;
	struct rte_devargs da;

	memset(&da, 0, sizeof(da));

	if (rte_devargs_parsef(&da, "%s", res->devargs)) {
		cmdline_printf(cl, "cannot parse devargs\n");
		if (da.args)
			free(da.args);
		return;
	}

	if (!rte_eal_hotplug_add(da.bus->name, da.name, da.args))
		cmdline_printf(cl, "attached device %s\n", da.name);
	else
		cmdline_printf(cl, "failed to attached device %s\n",
				da.name);
}

cmdline_parse_token_string_t cmd_dev_attach_attach =
	TOKEN_STRING_INITIALIZER(struct cmd_dev_attach_result, attach,
				 "attach");
cmdline_parse_token_string_t cmd_dev_attach_devargs =
	TOKEN_STRING_INITIALIZER(struct cmd_dev_attach_result, devargs, NULL);

cmdline_parse_inst_t cmd_attach_device = {
	.f = cmd_dev_attach_parsed,  /* function to call */
	.data = NULL,      /* 2nd arg of func */
	.help_str = "attach a device",
	.tokens = {        /* token list, NULL terminated */
		(void *)&cmd_dev_attach_attach,
		(void *)&cmd_dev_attach_devargs,
		NULL,
	},
};

/**********************************************************/

struct cmd_dev_detach_result {
	cmdline_fixed_string_t detach;
	cmdline_fixed_string_t devargs;
};

static void cmd_dev_detach_parsed(void *parsed_result,
				   struct cmdline *cl,
				   __attribute__((unused)) void *data)
{
	struct cmd_dev_detach_result *res = parsed_result;
	struct rte_devargs da;

	memset(&da, 0, sizeof(da));

	if (rte_devargs_parsef(&da, "%s", res->devargs)) {
		cmdline_printf(cl, "cannot parse devargs\n");
		if (da.args)
			free(da.args);
		return;
	}

	printf("detaching...\n");
	if (!rte_eal_hotplug_remove(da.bus->name, da.name))
		cmdline_printf(cl, "detached device %s\n",
			da.name);
	else
		cmdline_printf(cl, "failed to dettach device %s\n",
			da.name);
}

cmdline_parse_token_string_t cmd_dev_detach_detach =
	TOKEN_STRING_INITIALIZER(struct cmd_dev_detach_result, detach,
				 "detach");

cmdline_parse_token_string_t cmd_dev_detach_devargs =
	TOKEN_STRING_INITIALIZER(struct cmd_dev_detach_result, devargs, NULL);

cmdline_parse_inst_t cmd_detach_device = {
	.f = cmd_dev_detach_parsed,  /* function to call */
	.data = NULL,      /* 2nd arg of func */
	.help_str = "detach a device",
	.tokens = {        /* token list, NULL terminated */
		(void *)&cmd_dev_detach_detach,
		(void *)&cmd_dev_detach_devargs,
		NULL,
	},
};

/**********************************************************/
/**********************************************************/
/****** CONTEXT (list of instruction) */

cmdline_parse_ctx_t main_ctx[] = {
	(cmdline_parse_inst_t *)&cmd_help,
	(cmdline_parse_inst_t *)&cmd_quit,
	(cmdline_parse_inst_t *)&cmd_list,
	(cmdline_parse_inst_t *)&cmd_attach_device,
	(cmdline_parse_inst_t *)&cmd_detach_device,
	NULL,
};