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>
23 #include "vdpa_blk_compact.h"
25 #define MAX_PATH_LEN 128
26 #define MAX_VDPA_SAMPLE_PORTS 1024
27 #define RTE_LOGTYPE_VDPA RTE_LOGTYPE_USER1
30 char ifname[MAX_PATH_LEN];
31 struct rte_vdpa_device *dev;
35 struct rte_vdpa_stat_name *stats_names;
36 struct rte_vdpa_stat *stats;
39 static struct vdpa_port vports[MAX_VDPA_SAMPLE_PORTS];
41 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 rte_vhost_device_ops vdpa_sample_devops = {
158 .new_device = new_device,
159 .destroy_device = destroy_device,
163 vdpa_blk_device_set_features_and_protocol(const char *path)
165 uint64_t protocol_features = 0;
168 ret = rte_vhost_driver_set_features(path, VHOST_BLK_FEATURES);
171 "rte_vhost_driver_set_features for %s failed.\n",
176 ret = rte_vhost_driver_disable_features(path,
177 VHOST_BLK_DISABLED_FEATURES);
180 "rte_vhost_driver_disable_features for %s failed.\n",
185 ret = rte_vhost_driver_get_protocol_features(path, &protocol_features);
188 "rte_vhost_driver_get_protocol_features for %s failed.\n",
193 protocol_features |= VHOST_BLK_PROTOCOL_FEATURES;
195 ret = rte_vhost_driver_set_protocol_features(path, protocol_features);
198 "rte_vhost_driver_set_protocol_features for %s failed.\n",
207 start_vdpa(struct vdpa_port *vport)
209 uint32_t device_type = 0;
211 char *socket_path = vport->ifname;
214 vport->flags |= RTE_VHOST_USER_CLIENT;
216 if (access(socket_path, F_OK) != -1 && !client_mode) {
218 "%s exists, please remove it or specify another file and try again.\n",
222 ret = rte_vhost_driver_register(socket_path, vport->flags);
224 rte_exit(EXIT_FAILURE,
225 "register driver failed: %s\n",
228 ret = rte_vhost_driver_callback_register(socket_path,
229 &vdpa_sample_devops);
231 rte_exit(EXIT_FAILURE,
232 "register driver ops failed: %s\n",
235 ret = rte_vhost_driver_attach_vdpa_device(socket_path, vport->dev);
237 rte_exit(EXIT_FAILURE,
238 "attach vdpa device failed: %s\n",
241 ret = rte_vhost_driver_get_vdpa_dev_type(socket_path, &device_type);
242 if (ret == 0 && device_type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) {
243 RTE_LOG(NOTICE, VDPA, "%s is a blk device\n", socket_path);
244 ret = vdpa_blk_device_set_features_and_protocol(socket_path);
246 rte_exit(EXIT_FAILURE,
247 "set vhost blk driver features and protocol features failed: %s\n",
251 if (rte_vhost_driver_start(socket_path) < 0)
252 rte_exit(EXIT_FAILURE,
253 "start vhost driver failed: %s\n",
259 close_vdpa(struct vdpa_port *vport)
262 char *socket_path = vport->ifname;
264 ret = rte_vhost_driver_detach_vdpa_device(socket_path);
267 "detach vdpa device failed: %s\n",
270 ret = rte_vhost_driver_unregister(socket_path);
273 "Fail to unregister vhost driver for %s.\n",
275 if (vport->stats_names) {
276 rte_free(vport->stats_names);
277 vport->stats_names = NULL;
282 vdpa_sample_quit(void)
285 for (i = 0; i < RTE_MIN(MAX_VDPA_SAMPLE_PORTS, devcnt); i++) {
286 if (vports[i].ifname[0] != '\0')
287 close_vdpa(&vports[i]);
292 signal_handler(int signum)
294 if (signum == SIGINT || signum == SIGTERM) {
295 printf("\nSignal %d received, preparing to exit...\n", signum);
301 /* interactive cmds */
303 /* *** Help command with introduction. *** */
304 struct cmd_help_result {
305 cmdline_fixed_string_t help;
308 static void cmd_help_parsed(__rte_unused void *parsed_result,
310 __rte_unused void *data)
315 "The following commands are currently available:\n\n"
317 " help : Show interactive instructions.\n"
318 " list : list all available vdpa devices.\n"
319 " create <socket file> <vdev addr> : create a new vdpa port.\n"
320 " stats <device ID> <virtio queue ID> : show statistics of virtio queue, 0xffff for all.\n"
321 " quit : exit vdpa sample app.\n"
325 cmdline_parse_token_string_t cmd_help_help =
326 TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
328 cmdline_parse_inst_t cmd_help = {
329 .f = cmd_help_parsed,
331 .help_str = "show help",
333 (void *)&cmd_help_help,
338 /* *** List all available vdpa devices *** */
339 struct cmd_list_result {
340 cmdline_fixed_string_t action;
343 static void cmd_list_vdpa_devices_parsed(
344 __rte_unused void *parsed_result,
346 __rte_unused void *data)
350 struct rte_vdpa_device *vdev;
351 struct rte_device *dev;
352 struct rte_dev_iterator dev_iter;
354 cmdline_printf(cl, "device name\tqueue num\tsupported features\n");
355 RTE_DEV_FOREACH(dev, "class=vdpa", &dev_iter) {
356 vdev = rte_vdpa_find_device_by_name(dev->name);
359 if (rte_vdpa_get_queue_num(vdev, &queue_num) < 0) {
361 "failed to get vdpa queue number "
362 "for device %s.\n", dev->name);
365 if (rte_vdpa_get_features(vdev, &features) < 0) {
367 "failed to get vdpa features "
368 "for device %s.\n", dev->name);
371 cmdline_printf(cl, "%s\t\t%" PRIu32 "\t\t0x%" PRIx64 "\n",
372 dev->name, queue_num, features);
376 cmdline_parse_token_string_t cmd_action_list =
377 TOKEN_STRING_INITIALIZER(struct cmd_list_result, action, "list");
379 cmdline_parse_inst_t cmd_list_vdpa_devices = {
380 .f = cmd_list_vdpa_devices_parsed,
382 .help_str = "list all available vdpa devices",
384 (void *)&cmd_action_list,
389 /* *** Create new vdpa port *** */
390 struct cmd_create_result {
391 cmdline_fixed_string_t action;
392 cmdline_fixed_string_t socket_path;
393 cmdline_fixed_string_t bdf;
396 static void cmd_create_vdpa_port_parsed(void *parsed_result,
398 __rte_unused void *data)
400 struct rte_vdpa_device *dev;
401 struct cmd_create_result *res = parsed_result;
403 rte_strscpy(vports[devcnt].ifname, res->socket_path, MAX_PATH_LEN);
404 dev = rte_vdpa_find_device_by_name(res->bdf);
406 cmdline_printf(cl, "Unable to find vdpa device id for %s.\n",
411 vports[devcnt].dev = dev;
413 if (start_vdpa(&vports[devcnt]) == 0)
417 cmdline_parse_token_string_t cmd_action_create =
418 TOKEN_STRING_INITIALIZER(struct cmd_create_result, action, "create");
419 cmdline_parse_token_string_t cmd_socket_path =
420 TOKEN_STRING_INITIALIZER(struct cmd_create_result, socket_path, NULL);
421 cmdline_parse_token_string_t cmd_bdf =
422 TOKEN_STRING_INITIALIZER(struct cmd_create_result, bdf, NULL);
424 cmdline_parse_inst_t cmd_create_vdpa_port = {
425 .f = cmd_create_vdpa_port_parsed,
427 .help_str = "create a new vdpa port",
429 (void *)&cmd_action_create,
430 (void *)&cmd_socket_path,
437 struct cmd_stats_result {
438 cmdline_fixed_string_t stats;
439 cmdline_fixed_string_t bdf;
443 static void cmd_device_stats_parsed(void *parsed_result, struct cmdline *cl,
444 __rte_unused void *data)
446 struct cmd_stats_result *res = parsed_result;
447 struct rte_vdpa_device *vdev = rte_vdpa_find_device_by_name(res->bdf);
448 struct vdpa_port *vport = NULL;
449 uint32_t first, last;
453 RTE_LOG(ERR, VDPA, "Invalid device: %s.\n",
457 for (i = 0; i < RTE_MIN(MAX_VDPA_SAMPLE_PORTS, devcnt); i++) {
458 if (vports[i].dev == vdev) {
464 RTE_LOG(ERR, VDPA, "Device %s was not created.\n", res->bdf);
467 if (res->qid == 0xFFFF) {
469 last = rte_vhost_get_vring_num(vport->vid);
471 RTE_LOG(ERR, VDPA, "Failed to get num of actual virtqs"
472 " for device %s.\n", res->bdf);
480 if (!vport->stats_names) {
481 vport->stats_n = rte_vdpa_get_stats_names(vport->dev, NULL, 0);
482 if (vport->stats_n <= 0) {
483 RTE_LOG(ERR, VDPA, "Failed to get names number of "
484 "device %s stats.\n", res->bdf);
487 vport->stats_names = rte_zmalloc(NULL,
488 (sizeof(*vport->stats_names) + sizeof(*vport->stats)) *
490 if (!vport->stats_names) {
491 RTE_LOG(ERR, VDPA, "Failed to allocate memory for stat"
492 " names of device %s.\n", res->bdf);
495 i = rte_vdpa_get_stats_names(vport->dev, vport->stats_names,
497 if (vport->stats_n != i) {
498 RTE_LOG(ERR, VDPA, "Failed to get names of device %s "
499 "stats.\n", res->bdf);
502 vport->stats = (struct rte_vdpa_stat *)
503 (vport->stats_names + vport->stats_n);
505 cmdline_printf(cl, "\nDevice %s:\n", res->bdf);
506 for (; first <= last; first++) {
507 memset(vport->stats, 0, sizeof(*vport->stats) * vport->stats_n);
508 if (rte_vdpa_get_stats(vport->dev, (int)first, vport->stats,
509 vport->stats_n) <= 0) {
510 RTE_LOG(ERR, VDPA, "Failed to get vdpa queue statistics"
511 " for device %s qid %d.\n", res->bdf,
515 cmdline_printf(cl, "\tVirtq %" PRIu32 ":\n", first);
516 for (i = 0; i < vport->stats_n; ++i) {
517 cmdline_printf(cl, "\t\t%-*s %-16" PRIu64 "\n",
518 RTE_VDPA_STATS_NAME_SIZE,
519 vport->stats_names[vport->stats[i].id].name,
520 vport->stats[i].value);
525 cmdline_parse_token_string_t cmd_device_stats_ =
526 TOKEN_STRING_INITIALIZER(struct cmd_stats_result, stats, "stats");
527 cmdline_parse_token_string_t cmd_device_bdf =
528 TOKEN_STRING_INITIALIZER(struct cmd_stats_result, bdf, NULL);
529 cmdline_parse_token_num_t cmd_queue_id =
530 TOKEN_NUM_INITIALIZER(struct cmd_stats_result, qid, RTE_UINT32);
532 cmdline_parse_inst_t cmd_device_stats = {
533 .f = cmd_device_stats_parsed,
535 .help_str = "stats: show device statistics",
537 (void *)&cmd_device_stats_,
538 (void *)&cmd_device_bdf,
539 (void *)&cmd_queue_id,
545 struct cmd_quit_result {
546 cmdline_fixed_string_t quit;
549 static void cmd_quit_parsed(__rte_unused void *parsed_result,
551 __rte_unused void *data)
557 cmdline_parse_token_string_t cmd_quit_quit =
558 TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
560 cmdline_parse_inst_t cmd_quit = {
561 .f = cmd_quit_parsed,
563 .help_str = "quit: exit application",
565 (void *)&cmd_quit_quit,
569 cmdline_parse_ctx_t main_ctx[] = {
570 (cmdline_parse_inst_t *)&cmd_help,
571 (cmdline_parse_inst_t *)&cmd_list_vdpa_devices,
572 (cmdline_parse_inst_t *)&cmd_create_vdpa_port,
573 (cmdline_parse_inst_t *)&cmd_device_stats,
574 (cmdline_parse_inst_t *)&cmd_quit,
579 main(int argc, char *argv[])
584 struct rte_vdpa_device *vdev;
585 struct rte_device *dev;
586 struct rte_dev_iterator dev_iter;
588 ret = rte_eal_init(argc, argv);
590 rte_exit(EXIT_FAILURE, "eal init failed\n");
594 signal(SIGINT, signal_handler);
595 signal(SIGTERM, signal_handler);
597 ret = parse_args(argc, argv);
599 rte_exit(EXIT_FAILURE, "invalid argument\n");
601 if (interactive == 1) {
602 cl = cmdline_stdin_new(main_ctx, "vdpa> ");
604 rte_panic("Cannot create cmdline instance\n");
605 cmdline_interact(cl);
606 cmdline_stdin_exit(cl);
608 RTE_DEV_FOREACH(dev, "class=vdpa", &dev_iter) {
609 vdev = rte_vdpa_find_device_by_name(dev->name);
611 rte_panic("Failed to find vDPA dev for %s\n",
614 vports[devcnt].dev = vdev;
615 snprintf(vports[devcnt].ifname, MAX_PATH_LEN, "%s%d",
618 start_vdpa(&vports[devcnt]);
622 printf("enter \'q\' to quit\n");
623 while (scanf("%c", &ch)) {
627 if (scanf("%c", &ch))
630 printf("enter \'q\' to quit\n");
635 /* clean up the EAL */