1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
11 #include <rte_ethdev.h>
12 #include <rte_malloc.h>
13 #include <rte_vhost.h>
16 #include <rte_string_fns.h>
18 #include <cmdline_parse.h>
19 #include <cmdline_socket.h>
20 #include <cmdline_parse_string.h>
21 #include <cmdline_parse_num.h>
24 #define MAX_PATH_LEN 128
25 #define MAX_VDPA_SAMPLE_PORTS 1024
26 #define RTE_LOGTYPE_VDPA RTE_LOGTYPE_USER1
29 char ifname[MAX_PATH_LEN];
30 struct rte_vdpa_device *dev;
34 struct rte_vdpa_stat_name *stats_names;
35 struct rte_vdpa_stat *stats;
38 static struct vdpa_port vports[MAX_VDPA_SAMPLE_PORTS];
40 static char iface[MAX_PATH_LEN];
43 static int interactive;
44 static int client_mode;
48 vdpa_usage(const char *prgname)
50 printf("Usage: %s [EAL options] -- "
51 " --interactive|-i: run in interactive mode.\n"
52 " --iface <path>: specify the path prefix of the socket files, e.g. /tmp/vhost-user-.\n"
53 " --client: register a vhost-user socket as client mode.\n",
58 parse_args(int argc, char **argv)
60 static const char *short_option = "i";
61 static struct option long_option[] = {
62 {"iface", required_argument, NULL, 0},
63 {"interactive", no_argument, &interactive, 1},
64 {"client", no_argument, &client_mode, 1},
68 char *prgname = argv[0];
70 while ((opt = getopt_long(argc, argv, short_option, long_option, &idx))
74 printf("Interactive-mode selected\n");
79 if (strncmp(long_option[idx].name, "iface",
81 rte_strscpy(iface, optarg, MAX_PATH_LEN);
82 printf("iface %s\n", iface);
84 if (!strcmp(long_option[idx].name, "interactive")) {
85 printf("Interactive-mode selected\n");
96 if (iface[0] == '\0' && interactive == 0) {
107 char ifname[MAX_PATH_LEN];
108 struct rte_device *dev;
111 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
112 for (i = 0; i < MAX_VDPA_SAMPLE_PORTS; i++) {
113 if (strncmp(ifname, vports[i].ifname, MAX_PATH_LEN))
116 dev = rte_vdpa_get_rte_device(vports[i].dev);
119 "Failed to get generic device for port %d\n", i);
122 printf("\nnew port %s, device : %s\n", ifname, dev->name);
127 if (i >= MAX_VDPA_SAMPLE_PORTS)
134 destroy_device(int vid)
136 struct rte_device *dev;
137 char ifname[MAX_PATH_LEN];
140 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
141 for (i = 0; i < MAX_VDPA_SAMPLE_PORTS; i++) {
142 if (strncmp(ifname, vports[i].ifname, MAX_PATH_LEN))
145 dev = rte_vdpa_get_rte_device(vports[i].dev);
148 "Failed to get generic device for port %d\n", i);
152 printf("\ndestroy port %s, device: %s\n", ifname, dev->name);
157 static const struct vhost_device_ops vdpa_sample_devops = {
158 .new_device = new_device,
159 .destroy_device = destroy_device,
163 start_vdpa(struct vdpa_port *vport)
166 char *socket_path = vport->ifname;
169 vport->flags |= RTE_VHOST_USER_CLIENT;
171 if (access(socket_path, F_OK) != -1 && !client_mode) {
173 "%s exists, please remove it or specify another file and try again.\n",
177 ret = rte_vhost_driver_register(socket_path, vport->flags);
179 rte_exit(EXIT_FAILURE,
180 "register driver failed: %s\n",
183 ret = rte_vhost_driver_callback_register(socket_path,
184 &vdpa_sample_devops);
186 rte_exit(EXIT_FAILURE,
187 "register driver ops failed: %s\n",
190 ret = rte_vhost_driver_attach_vdpa_device(socket_path, vport->dev);
192 rte_exit(EXIT_FAILURE,
193 "attach vdpa device failed: %s\n",
196 if (rte_vhost_driver_start(socket_path) < 0)
197 rte_exit(EXIT_FAILURE,
198 "start vhost driver failed: %s\n",
204 close_vdpa(struct vdpa_port *vport)
207 char *socket_path = vport->ifname;
209 ret = rte_vhost_driver_detach_vdpa_device(socket_path);
212 "detach vdpa device failed: %s\n",
215 ret = rte_vhost_driver_unregister(socket_path);
218 "Fail to unregister vhost driver for %s.\n",
220 if (vport->stats_names) {
221 rte_free(vport->stats_names);
222 vport->stats_names = NULL;
227 vdpa_sample_quit(void)
230 for (i = 0; i < RTE_MIN(MAX_VDPA_SAMPLE_PORTS, dev_total); i++) {
231 if (vports[i].ifname[0] != '\0')
232 close_vdpa(&vports[i]);
237 signal_handler(int signum)
239 if (signum == SIGINT || signum == SIGTERM) {
240 printf("\nSignal %d received, preparing to exit...\n", signum);
246 /* interactive cmds */
248 /* *** Help command with introduction. *** */
249 struct cmd_help_result {
250 cmdline_fixed_string_t help;
253 static void cmd_help_parsed(__rte_unused void *parsed_result,
255 __rte_unused void *data)
260 "The following commands are currently available:\n\n"
262 " help : Show interactive instructions.\n"
263 " list : list all available vdpa devices.\n"
264 " create <socket file> <vdev addr> : create a new vdpa port.\n"
265 " stats <device ID> <virtio queue ID> : show statistics of virtio queue, 0xffff for all.\n"
266 " quit : exit vdpa sample app.\n"
270 cmdline_parse_token_string_t cmd_help_help =
271 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
273 cmdline_parse_inst_t cmd_help = {
274 .f = cmd_help_parsed,
276 .help_str = "show help",
278 (void *)&cmd_help_help,
283 /* *** List all available vdpa devices *** */
284 struct cmd_list_result {
285 cmdline_fixed_string_t action;
288 static void cmd_list_vdpa_devices_parsed(
289 __rte_unused void *parsed_result,
291 __rte_unused void *data)
295 struct rte_vdpa_device *vdev;
296 struct rte_device *dev;
297 struct rte_dev_iterator dev_iter;
299 cmdline_printf(cl, "device name\tqueue num\tsupported features\n");
300 RTE_DEV_FOREACH(dev, "class=vdpa", &dev_iter) {
301 vdev = rte_vdpa_find_device_by_name(dev->name);
304 if (rte_vdpa_get_queue_num(vdev, &queue_num) < 0) {
306 "failed to get vdpa queue number "
307 "for device %s.\n", dev->name);
310 if (rte_vdpa_get_features(vdev, &features) < 0) {
312 "failed to get vdpa features "
313 "for device %s.\n", dev->name);
316 cmdline_printf(cl, "%s\t\t%" PRIu32 "\t\t0x%" PRIx64 "\n",
317 dev->name, queue_num, features);
321 cmdline_parse_token_string_t cmd_action_list =
322 TOKEN_STRING_INITIALIZER(struct cmd_list_result, action, "list");
324 cmdline_parse_inst_t cmd_list_vdpa_devices = {
325 .f = cmd_list_vdpa_devices_parsed,
327 .help_str = "list all available vdpa devices",
329 (void *)&cmd_action_list,
334 /* *** Create new vdpa port *** */
335 struct cmd_create_result {
336 cmdline_fixed_string_t action;
337 cmdline_fixed_string_t socket_path;
338 cmdline_fixed_string_t bdf;
341 static void cmd_create_vdpa_port_parsed(void *parsed_result,
343 __rte_unused void *data)
345 struct rte_vdpa_device *dev;
346 struct cmd_create_result *res = parsed_result;
348 rte_strscpy(vports[devcnt].ifname, res->socket_path, MAX_PATH_LEN);
349 dev = rte_vdpa_find_device_by_name(res->bdf);
351 cmdline_printf(cl, "Unable to find vdpa device id for %s.\n",
356 vports[devcnt].dev = dev;
358 if (start_vdpa(&vports[devcnt]) == 0)
362 cmdline_parse_token_string_t cmd_action_create =
363 TOKEN_STRING_INITIALIZER(struct cmd_create_result, action, "create");
364 cmdline_parse_token_string_t cmd_socket_path =
365 TOKEN_STRING_INITIALIZER(struct cmd_create_result, socket_path, NULL);
366 cmdline_parse_token_string_t cmd_bdf =
367 TOKEN_STRING_INITIALIZER(struct cmd_create_result, bdf, NULL);
369 cmdline_parse_inst_t cmd_create_vdpa_port = {
370 .f = cmd_create_vdpa_port_parsed,
372 .help_str = "create a new vdpa port",
374 (void *)&cmd_action_create,
375 (void *)&cmd_socket_path,
382 struct cmd_stats_result {
383 cmdline_fixed_string_t stats;
384 cmdline_fixed_string_t bdf;
388 static void cmd_device_stats_parsed(void *parsed_result, struct cmdline *cl,
389 __rte_unused void *data)
391 struct cmd_stats_result *res = parsed_result;
392 struct rte_vdpa_device *vdev = rte_vdpa_find_device_by_name(res->bdf);
393 struct vdpa_port *vport = NULL;
394 uint32_t first, last;
398 RTE_LOG(ERR, VDPA, "Invalid device: %s.\n",
402 for (i = 0; i < RTE_MIN(MAX_VDPA_SAMPLE_PORTS, dev_total); i++) {
403 if (vports[i].dev == vdev) {
409 RTE_LOG(ERR, VDPA, "Device %s was not created.\n", res->bdf);
412 if (res->qid == 0xFFFF) {
414 last = rte_vhost_get_vring_num(vport->vid);
416 RTE_LOG(ERR, VDPA, "Failed to get num of actual virtqs"
417 " for device %s.\n", res->bdf);
425 if (!vport->stats_names) {
426 vport->stats_n = rte_vdpa_get_stats_names(vport->dev, NULL, 0);
427 if (vport->stats_n <= 0) {
428 RTE_LOG(ERR, VDPA, "Failed to get names number of "
429 "device %s stats.\n", res->bdf);
432 vport->stats_names = rte_zmalloc(NULL,
433 (sizeof(*vport->stats_names) + sizeof(*vport->stats)) *
435 if (!vport->stats_names) {
436 RTE_LOG(ERR, VDPA, "Failed to allocate memory for stat"
437 " names of device %s.\n", res->bdf);
440 i = rte_vdpa_get_stats_names(vport->dev, vport->stats_names,
442 if (vport->stats_n != i) {
443 RTE_LOG(ERR, VDPA, "Failed to get names of device %s "
444 "stats.\n", res->bdf);
447 vport->stats = (struct rte_vdpa_stat *)
448 (vport->stats_names + vport->stats_n);
450 cmdline_printf(cl, "\nDevice %s:\n", res->bdf);
451 for (; first <= last; first++) {
452 memset(vport->stats, 0, sizeof(*vport->stats) * vport->stats_n);
453 if (rte_vdpa_get_stats(vport->dev, (int)first, vport->stats,
454 vport->stats_n) <= 0) {
455 RTE_LOG(ERR, VDPA, "Failed to get vdpa queue statistics"
456 " for device %s qid %d.\n", res->bdf,
460 cmdline_printf(cl, "\tVirtq %" PRIu32 ":\n", first);
461 for (i = 0; i < vport->stats_n; ++i) {
462 cmdline_printf(cl, "\t\t%-*s %-16" PRIu64 "\n",
463 RTE_VDPA_STATS_NAME_SIZE,
464 vport->stats_names[vport->stats[i].id].name,
465 vport->stats[i].value);
470 cmdline_parse_token_string_t cmd_device_stats_ =
471 TOKEN_STRING_INITIALIZER(struct cmd_stats_result, stats, "stats");
472 cmdline_parse_token_string_t cmd_device_bdf =
473 TOKEN_STRING_INITIALIZER(struct cmd_stats_result, bdf, NULL);
474 cmdline_parse_token_num_t cmd_queue_id =
475 TOKEN_NUM_INITIALIZER(struct cmd_stats_result, qid, UINT32);
477 cmdline_parse_inst_t cmd_device_stats = {
478 .f = cmd_device_stats_parsed,
480 .help_str = "stats: show device statistics",
482 (void *)&cmd_device_stats_,
483 (void *)&cmd_device_bdf,
484 (void *)&cmd_queue_id,
490 struct cmd_quit_result {
491 cmdline_fixed_string_t quit;
494 static void cmd_quit_parsed(__rte_unused void *parsed_result,
496 __rte_unused void *data)
502 cmdline_parse_token_string_t cmd_quit_quit =
503 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
505 cmdline_parse_inst_t cmd_quit = {
506 .f = cmd_quit_parsed,
508 .help_str = "quit: exit application",
510 (void *)&cmd_quit_quit,
514 cmdline_parse_ctx_t main_ctx[] = {
515 (cmdline_parse_inst_t *)&cmd_help,
516 (cmdline_parse_inst_t *)&cmd_list_vdpa_devices,
517 (cmdline_parse_inst_t *)&cmd_create_vdpa_port,
518 (cmdline_parse_inst_t *)&cmd_device_stats,
519 (cmdline_parse_inst_t *)&cmd_quit,
524 main(int argc, char *argv[])
529 struct rte_vdpa_device *vdev;
530 struct rte_device *dev;
531 struct rte_dev_iterator dev_iter;
533 ret = rte_eal_init(argc, argv);
535 rte_exit(EXIT_FAILURE, "eal init failed\n");
539 dev_total = rte_vdpa_get_device_num();
541 rte_exit(EXIT_FAILURE, "No available vdpa device found\n");
543 signal(SIGINT, signal_handler);
544 signal(SIGTERM, signal_handler);
546 ret = parse_args(argc, argv);
548 rte_exit(EXIT_FAILURE, "invalid argument\n");
550 if (interactive == 1) {
551 cl = cmdline_stdin_new(main_ctx, "vdpa> ");
553 rte_panic("Cannot create cmdline instance\n");
554 cmdline_interact(cl);
555 cmdline_stdin_exit(cl);
557 RTE_DEV_FOREACH(dev, "class=vdpa", &dev_iter) {
558 vdev = rte_vdpa_find_device_by_name(dev->name);
560 rte_panic("Failed to find vDPA dev for %s\n",
563 vports[devcnt].dev = vdev;
564 snprintf(vports[devcnt].ifname, MAX_PATH_LEN, "%s%d",
567 start_vdpa(&vports[devcnt]);
571 printf("enter \'q\' to quit\n");
572 while (scanf("%c", &ch)) {
576 if (scanf("%c", &ch))
579 printf("enter \'q\' to quit\n");