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];
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];
110 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
111 for (i = 0; i < MAX_VDPA_SAMPLE_PORTS; i++) {
112 if (strncmp(ifname, vports[i].ifname, MAX_PATH_LEN) == 0) {
113 printf("\nnew port %s, did: %d\n",
114 ifname, vports[i].did);
120 if (i >= MAX_VDPA_SAMPLE_PORTS)
127 destroy_device(int vid)
129 char ifname[MAX_PATH_LEN];
132 rte_vhost_get_ifname(vid, ifname, sizeof(ifname));
133 for (i = 0; i < MAX_VDPA_SAMPLE_PORTS; i++) {
134 if (strcmp(ifname, vports[i].ifname) == 0) {
135 printf("\ndestroy port %s, did: %d\n",
136 ifname, vports[i].did);
142 static const struct vhost_device_ops vdpa_sample_devops = {
143 .new_device = new_device,
144 .destroy_device = destroy_device,
148 start_vdpa(struct vdpa_port *vport)
151 char *socket_path = vport->ifname;
152 int did = vport->did;
155 vport->flags |= RTE_VHOST_USER_CLIENT;
157 if (access(socket_path, F_OK) != -1 && !client_mode) {
159 "%s exists, please remove it or specify another file and try again.\n",
163 ret = rte_vhost_driver_register(socket_path, vport->flags);
165 rte_exit(EXIT_FAILURE,
166 "register driver failed: %s\n",
169 ret = rte_vhost_driver_callback_register(socket_path,
170 &vdpa_sample_devops);
172 rte_exit(EXIT_FAILURE,
173 "register driver ops failed: %s\n",
176 ret = rte_vhost_driver_attach_vdpa_device(socket_path, did);
178 rte_exit(EXIT_FAILURE,
179 "attach vdpa device failed: %s\n",
182 if (rte_vhost_driver_start(socket_path) < 0)
183 rte_exit(EXIT_FAILURE,
184 "start vhost driver failed: %s\n",
190 close_vdpa(struct vdpa_port *vport)
193 char *socket_path = vport->ifname;
195 ret = rte_vhost_driver_detach_vdpa_device(socket_path);
198 "detach vdpa device failed: %s\n",
201 ret = rte_vhost_driver_unregister(socket_path);
204 "Fail to unregister vhost driver for %s.\n",
206 if (vport->stats_names) {
207 rte_free(vport->stats_names);
208 vport->stats_names = NULL;
213 vdpa_sample_quit(void)
216 for (i = 0; i < RTE_MIN(MAX_VDPA_SAMPLE_PORTS, dev_total); i++) {
217 if (vports[i].ifname[0] != '\0')
218 close_vdpa(&vports[i]);
223 signal_handler(int signum)
225 if (signum == SIGINT || signum == SIGTERM) {
226 printf("\nSignal %d received, preparing to exit...\n", signum);
232 /* interactive cmds */
234 /* *** Help command with introduction. *** */
235 struct cmd_help_result {
236 cmdline_fixed_string_t help;
239 static void cmd_help_parsed(__rte_unused void *parsed_result,
241 __rte_unused void *data)
246 "The following commands are currently available:\n\n"
248 " help : Show interactive instructions.\n"
249 " list : list all available vdpa devices.\n"
250 " create <socket file> <vdev addr> : create a new vdpa port.\n"
251 " stats <device ID> <virtio queue ID> : show statistics of virtio queue, 0xffff for all.\n"
252 " quit : exit vdpa sample app.\n"
256 cmdline_parse_token_string_t cmd_help_help =
257 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
259 cmdline_parse_inst_t cmd_help = {
260 .f = cmd_help_parsed,
262 .help_str = "show help",
264 (void *)&cmd_help_help,
269 /* *** List all available vdpa devices *** */
270 struct cmd_list_result {
271 cmdline_fixed_string_t action;
274 static void cmd_list_vdpa_devices_parsed(
275 __rte_unused void *parsed_result,
277 __rte_unused void *data)
282 struct rte_vdpa_device *vdev;
283 struct rte_pci_addr addr;
285 cmdline_printf(cl, "device id\tdevice address\tqueue num\tsupported features\n");
286 for (did = 0; did < dev_total; did++) {
287 vdev = rte_vdpa_get_device(did);
290 if (vdev->ops->get_queue_num(did, &queue_num) < 0) {
292 "failed to get vdpa queue number "
293 "for device id %d.\n", did);
296 if (vdev->ops->get_features(did, &features) < 0) {
298 "failed to get vdpa features "
299 "for device id %d.\n", did);
302 addr = vdev->addr.pci_addr;
304 "%d\t\t" PCI_PRI_FMT "\t%" PRIu32 "\t\t0x%" PRIx64 "\n",
305 did, addr.domain, addr.bus, addr.devid,
306 addr.function, queue_num, features);
310 cmdline_parse_token_string_t cmd_action_list =
311 TOKEN_STRING_INITIALIZER(struct cmd_list_result, action, "list");
313 cmdline_parse_inst_t cmd_list_vdpa_devices = {
314 .f = cmd_list_vdpa_devices_parsed,
316 .help_str = "list all available vdpa devices",
318 (void *)&cmd_action_list,
323 /* *** Create new vdpa port *** */
324 struct cmd_create_result {
325 cmdline_fixed_string_t action;
326 cmdline_fixed_string_t socket_path;
327 cmdline_fixed_string_t bdf;
330 static void cmd_create_vdpa_port_parsed(void *parsed_result,
332 __rte_unused void *data)
335 struct cmd_create_result *res = parsed_result;
336 struct rte_vdpa_dev_addr addr;
338 rte_strscpy(vports[devcnt].ifname, res->socket_path, MAX_PATH_LEN);
339 if (rte_pci_addr_parse(res->bdf, &addr.pci_addr) != 0) {
340 cmdline_printf(cl, "Unable to parse the given bdf.\n");
343 addr.type = VDPA_ADDR_PCI;
344 did = rte_vdpa_find_device_id(&addr);
346 cmdline_printf(cl, "Unable to find vdpa device id.\n");
350 vports[devcnt].did = did;
352 if (start_vdpa(&vports[devcnt]) == 0)
356 cmdline_parse_token_string_t cmd_action_create =
357 TOKEN_STRING_INITIALIZER(struct cmd_create_result, action, "create");
358 cmdline_parse_token_string_t cmd_socket_path =
359 TOKEN_STRING_INITIALIZER(struct cmd_create_result, socket_path, NULL);
360 cmdline_parse_token_string_t cmd_bdf =
361 TOKEN_STRING_INITIALIZER(struct cmd_create_result, bdf, NULL);
363 cmdline_parse_inst_t cmd_create_vdpa_port = {
364 .f = cmd_create_vdpa_port_parsed,
366 .help_str = "create a new vdpa port",
368 (void *)&cmd_action_create,
369 (void *)&cmd_socket_path,
376 struct cmd_stats_result {
377 cmdline_fixed_string_t stats;
382 static void cmd_device_stats_parsed(void *parsed_result, struct cmdline *cl,
383 __rte_unused void *data)
385 struct cmd_stats_result *res = parsed_result;
386 struct rte_vdpa_device *vdev = rte_vdpa_get_device(res->did);
387 struct vdpa_port *vport = NULL;
388 uint32_t first, last;
392 RTE_LOG(ERR, VDPA, "Invalid device id %" PRIu16 ".\n",
396 for (i = 0; i < RTE_MIN(MAX_VDPA_SAMPLE_PORTS, dev_total); i++) {
397 if (vports[i].did == res->did) {
403 RTE_LOG(ERR, VDPA, "Device id %" PRIu16 " was not created.\n",
407 if (res->qid == 0xFFFF) {
409 last = rte_vhost_get_vring_num(vport->vid);
411 RTE_LOG(ERR, VDPA, "Failed to get num of actual virtqs"
412 " for device id %d.\n", (int)res->did);
420 if (!vport->stats_names) {
421 vport->stats_n = rte_vdpa_get_stats_names(vport->did, NULL, 0);
422 if (vport->stats_n <= 0) {
423 RTE_LOG(ERR, VDPA, "Failed to get names number of "
424 "device %d stats.\n", (int)res->did);
427 vport->stats_names = rte_zmalloc(NULL,
428 (sizeof(*vport->stats_names) + sizeof(*vport->stats)) *
430 if (!vport->stats_names) {
431 RTE_LOG(ERR, VDPA, "Failed to allocate memory for stat"
432 " names of device %d.\n", (int)res->did);
435 i = rte_vdpa_get_stats_names(vport->did, vport->stats_names,
437 if (vport->stats_n != i) {
438 RTE_LOG(ERR, VDPA, "Failed to get names of device %d "
439 "stats.\n", (int)res->did);
442 vport->stats = (struct rte_vdpa_stat *)
443 (vport->stats_names + vport->stats_n);
445 cmdline_printf(cl, "\nDevice %d:\n", (int)res->did);
446 for (; first <= last; first++) {
447 memset(vport->stats, 0, sizeof(*vport->stats) * vport->stats_n);
448 if (rte_vdpa_get_stats(vport->did, (int)first, vport->stats,
449 vport->stats_n) <= 0) {
450 RTE_LOG(ERR, VDPA, "Failed to get vdpa queue statistics"
451 " for device id %d qid %d.\n", (int)res->did,
455 cmdline_printf(cl, "\tVirtq %" PRIu32 ":\n", first);
456 for (i = 0; i < vport->stats_n; ++i) {
457 cmdline_printf(cl, "\t\t%-*s %-16" PRIu64 "\n",
458 RTE_VDPA_STATS_NAME_SIZE,
459 vport->stats_names[vport->stats[i].id].name,
460 vport->stats[i].value);
465 cmdline_parse_token_string_t cmd_device_stats_ =
466 TOKEN_STRING_INITIALIZER(struct cmd_stats_result, stats, "stats");
467 cmdline_parse_token_num_t cmd_device_id =
468 TOKEN_NUM_INITIALIZER(struct cmd_stats_result, did, UINT32);
469 cmdline_parse_token_num_t cmd_queue_id =
470 TOKEN_NUM_INITIALIZER(struct cmd_stats_result, qid, UINT32);
472 cmdline_parse_inst_t cmd_device_stats = {
473 .f = cmd_device_stats_parsed,
475 .help_str = "stats: show device statistics",
477 (void *)&cmd_device_stats_,
478 (void *)&cmd_device_id,
479 (void *)&cmd_queue_id,
485 struct cmd_quit_result {
486 cmdline_fixed_string_t quit;
489 static void cmd_quit_parsed(__rte_unused void *parsed_result,
491 __rte_unused void *data)
497 cmdline_parse_token_string_t cmd_quit_quit =
498 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
500 cmdline_parse_inst_t cmd_quit = {
501 .f = cmd_quit_parsed,
503 .help_str = "quit: exit application",
505 (void *)&cmd_quit_quit,
509 cmdline_parse_ctx_t main_ctx[] = {
510 (cmdline_parse_inst_t *)&cmd_help,
511 (cmdline_parse_inst_t *)&cmd_list_vdpa_devices,
512 (cmdline_parse_inst_t *)&cmd_create_vdpa_port,
513 (cmdline_parse_inst_t *)&cmd_device_stats,
514 (cmdline_parse_inst_t *)&cmd_quit,
519 main(int argc, char *argv[])
526 ret = rte_eal_init(argc, argv);
528 rte_exit(EXIT_FAILURE, "eal init failed\n");
532 dev_total = rte_vdpa_get_device_num();
534 rte_exit(EXIT_FAILURE, "No available vdpa device found\n");
536 signal(SIGINT, signal_handler);
537 signal(SIGTERM, signal_handler);
539 ret = parse_args(argc, argv);
541 rte_exit(EXIT_FAILURE, "invalid argument\n");
543 if (interactive == 1) {
544 cl = cmdline_stdin_new(main_ctx, "vdpa> ");
546 rte_panic("Cannot create cmdline instance\n");
547 cmdline_interact(cl);
548 cmdline_stdin_exit(cl);
550 for (i = 0; i < RTE_MIN(MAX_VDPA_SAMPLE_PORTS, dev_total);
553 snprintf(vports[i].ifname, MAX_PATH_LEN, "%s%d",
556 start_vdpa(&vports[i]);
559 printf("enter \'q\' to quit\n");
560 while (scanf("%c", &ch)) {
564 if (scanf("%c", &ch))
567 printf("enter \'q\' to quit\n");