6a719e32d872c9d8ee4921425a313b398fd6dfbe
[dpdk.git] / examples / vdpa / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <getopt.h>
6 #include <signal.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <unistd.h>
10
11 #include <rte_ethdev.h>
12 #include <rte_malloc.h>
13 #include <rte_vhost.h>
14 #include <rte_vdpa.h>
15 #include <rte_pci.h>
16 #include <rte_string_fns.h>
17
18 #include <cmdline_parse.h>
19 #include <cmdline_socket.h>
20 #include <cmdline_parse_string.h>
21 #include <cmdline_parse_num.h>
22 #include <cmdline.h>
23
24 #define MAX_PATH_LEN 128
25 #define MAX_VDPA_SAMPLE_PORTS 1024
26 #define RTE_LOGTYPE_VDPA RTE_LOGTYPE_USER1
27
28 struct vdpa_port {
29         char ifname[MAX_PATH_LEN];
30         int did;
31         int vid;
32         uint64_t flags;
33         int stats_n;
34         struct rte_vdpa_stat_name *stats_names;
35         struct rte_vdpa_stat *stats;
36 };
37
38 static struct vdpa_port vports[MAX_VDPA_SAMPLE_PORTS];
39
40 static char iface[MAX_PATH_LEN];
41 static int dev_total;
42 static int devcnt;
43 static int interactive;
44 static int client_mode;
45
46 /* display usage */
47 static void
48 vdpa_usage(const char *prgname)
49 {
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",
54                                  prgname);
55 }
56
57 static int
58 parse_args(int argc, char **argv)
59 {
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},
65                 {NULL, 0, 0, 0},
66         };
67         int opt, idx;
68         char *prgname = argv[0];
69
70         while ((opt = getopt_long(argc, argv, short_option, long_option, &idx))
71                         != EOF) {
72                 switch (opt) {
73                 case 'i':
74                         printf("Interactive-mode selected\n");
75                         interactive = 1;
76                         break;
77                 /* long options */
78                 case 0:
79                         if (strncmp(long_option[idx].name, "iface",
80                                                 MAX_PATH_LEN) == 0) {
81                                 rte_strscpy(iface, optarg, MAX_PATH_LEN);
82                                 printf("iface %s\n", iface);
83                         }
84                         if (!strcmp(long_option[idx].name, "interactive")) {
85                                 printf("Interactive-mode selected\n");
86                                 interactive = 1;
87                         }
88                         break;
89
90                 default:
91                         vdpa_usage(prgname);
92                         return -1;
93                 }
94         }
95
96         if (iface[0] == '\0' && interactive == 0) {
97                 vdpa_usage(prgname);
98                 return -1;
99         }
100
101         return 0;
102 }
103
104 static int
105 new_device(int vid)
106 {
107         char ifname[MAX_PATH_LEN];
108         int i;
109
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);
115                         vports[i].vid = vid;
116                         break;
117                 }
118         }
119
120         if (i >= MAX_VDPA_SAMPLE_PORTS)
121                 return -1;
122
123         return 0;
124 }
125
126 static void
127 destroy_device(int vid)
128 {
129         char ifname[MAX_PATH_LEN];
130         int i;
131
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);
137                         break;
138                 }
139         }
140 }
141
142 static const struct vhost_device_ops vdpa_sample_devops = {
143         .new_device = new_device,
144         .destroy_device = destroy_device,
145 };
146
147 static int
148 start_vdpa(struct vdpa_port *vport)
149 {
150         int ret;
151         char *socket_path = vport->ifname;
152         struct rte_vdpa_device *vdev;
153         int did = vport->did;
154
155         if (client_mode)
156                 vport->flags |= RTE_VHOST_USER_CLIENT;
157
158         if (access(socket_path, F_OK) != -1 && !client_mode) {
159                 RTE_LOG(ERR, VDPA,
160                         "%s exists, please remove it or specify another file and try again.\n",
161                         socket_path);
162                 return -1;
163         }
164         ret = rte_vhost_driver_register(socket_path, vport->flags);
165         if (ret != 0)
166                 rte_exit(EXIT_FAILURE,
167                         "register driver failed: %s\n",
168                         socket_path);
169
170         ret = rte_vhost_driver_callback_register(socket_path,
171                         &vdpa_sample_devops);
172         if (ret != 0)
173                 rte_exit(EXIT_FAILURE,
174                         "register driver ops failed: %s\n",
175                         socket_path);
176
177         vdev = rte_vdpa_get_device(did);
178         if (!vdev)
179                 rte_exit(EXIT_FAILURE,
180                         "vDPA device retrieval failed: %p\n",
181                         vdev);
182
183         ret = rte_vhost_driver_attach_vdpa_device(socket_path, vdev);
184         if (ret != 0)
185                 rte_exit(EXIT_FAILURE,
186                         "attach vdpa device failed: %s\n",
187                         socket_path);
188
189         if (rte_vhost_driver_start(socket_path) < 0)
190                 rte_exit(EXIT_FAILURE,
191                         "start vhost driver failed: %s\n",
192                         socket_path);
193         return 0;
194 }
195
196 static void
197 close_vdpa(struct vdpa_port *vport)
198 {
199         int ret;
200         char *socket_path = vport->ifname;
201
202         ret = rte_vhost_driver_detach_vdpa_device(socket_path);
203         if (ret != 0)
204                 RTE_LOG(ERR, VDPA,
205                                 "detach vdpa device failed: %s\n",
206                                 socket_path);
207
208         ret = rte_vhost_driver_unregister(socket_path);
209         if (ret != 0)
210                 RTE_LOG(ERR, VDPA,
211                                 "Fail to unregister vhost driver for %s.\n",
212                                 socket_path);
213         if (vport->stats_names) {
214                 rte_free(vport->stats_names);
215                 vport->stats_names = NULL;
216         }
217 }
218
219 static void
220 vdpa_sample_quit(void)
221 {
222         int i;
223         for (i = 0; i < RTE_MIN(MAX_VDPA_SAMPLE_PORTS, dev_total); i++) {
224                 if (vports[i].ifname[0] != '\0')
225                         close_vdpa(&vports[i]);
226         }
227 }
228
229 static void
230 signal_handler(int signum)
231 {
232         if (signum == SIGINT || signum == SIGTERM) {
233                 printf("\nSignal %d received, preparing to exit...\n", signum);
234                 vdpa_sample_quit();
235                 exit(0);
236         }
237 }
238
239 /* interactive cmds */
240
241 /* *** Help command with introduction. *** */
242 struct cmd_help_result {
243         cmdline_fixed_string_t help;
244 };
245
246 static void cmd_help_parsed(__rte_unused void *parsed_result,
247                 struct cmdline *cl,
248                 __rte_unused void *data)
249 {
250         cmdline_printf(
251                 cl,
252                 "\n"
253                 "The following commands are currently available:\n\n"
254                 "Control:\n"
255                 "    help                                      : Show interactive instructions.\n"
256                 "    list                                      : list all available vdpa devices.\n"
257                 "    create <socket file> <vdev addr>          : create a new vdpa port.\n"
258                 "    stats <device ID> <virtio queue ID>       : show statistics of virtio queue, 0xffff for all.\n"
259                 "    quit                                      : exit vdpa sample app.\n"
260         );
261 }
262
263 cmdline_parse_token_string_t cmd_help_help =
264         TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
265
266 cmdline_parse_inst_t cmd_help = {
267         .f = cmd_help_parsed,
268         .data = NULL,
269         .help_str = "show help",
270         .tokens = {
271                 (void *)&cmd_help_help,
272                 NULL,
273         },
274 };
275
276 /* *** List all available vdpa devices *** */
277 struct cmd_list_result {
278         cmdline_fixed_string_t action;
279 };
280
281 static void cmd_list_vdpa_devices_parsed(
282                 __rte_unused void *parsed_result,
283                 struct cmdline *cl,
284                 __rte_unused void *data)
285 {
286         int did;
287         uint32_t queue_num;
288         uint64_t features;
289         struct rte_vdpa_device *vdev;
290         struct rte_device *dev;
291         struct rte_dev_iterator dev_iter;
292
293         cmdline_printf(cl, "device id\tdevice name\tqueue num\tsupported features\n");
294         RTE_DEV_FOREACH(dev, "class=vdpa", &dev_iter) {
295                 did = rte_vdpa_find_device_id_by_name(dev->name);
296                 if (did < 0)
297                         continue;
298                 vdev = rte_vdpa_get_device(did);
299                 if (!vdev)
300                         continue;
301                 if (vdev->ops->get_queue_num(vdev, &queue_num) < 0) {
302                         RTE_LOG(ERR, VDPA,
303                                 "failed to get vdpa queue number "
304                                 "for device id %d.\n", did);
305                         continue;
306                 }
307                 if (vdev->ops->get_features(vdev, &features) < 0) {
308                         RTE_LOG(ERR, VDPA,
309                                 "failed to get vdpa features "
310                                 "for device id %d.\n", did);
311                         continue;
312                 }
313                 cmdline_printf(cl, "%d\t\t%s\t\t%" PRIu32 "\t\t0x%" PRIx64 "\n",
314                         did, dev->name, queue_num, features);
315         }
316 }
317
318 cmdline_parse_token_string_t cmd_action_list =
319         TOKEN_STRING_INITIALIZER(struct cmd_list_result, action, "list");
320
321 cmdline_parse_inst_t cmd_list_vdpa_devices = {
322         .f = cmd_list_vdpa_devices_parsed,
323         .data = NULL,
324         .help_str = "list all available vdpa devices",
325         .tokens = {
326                 (void *)&cmd_action_list,
327                 NULL,
328         },
329 };
330
331 /* *** Create new vdpa port *** */
332 struct cmd_create_result {
333         cmdline_fixed_string_t action;
334         cmdline_fixed_string_t socket_path;
335         cmdline_fixed_string_t bdf;
336 };
337
338 static void cmd_create_vdpa_port_parsed(void *parsed_result,
339                 struct cmdline *cl,
340                 __rte_unused void *data)
341 {
342         int did;
343         struct cmd_create_result *res = parsed_result;
344
345         rte_strscpy(vports[devcnt].ifname, res->socket_path, MAX_PATH_LEN);
346         did = rte_vdpa_find_device_id_by_name(res->bdf);
347         if (did < 0) {
348                 cmdline_printf(cl, "Unable to find vdpa device id for %s.\n",
349                                 res->bdf);
350                 return;
351         }
352
353         vports[devcnt].did = did;
354
355         if (start_vdpa(&vports[devcnt]) == 0)
356                 devcnt++;
357 }
358
359 cmdline_parse_token_string_t cmd_action_create =
360         TOKEN_STRING_INITIALIZER(struct cmd_create_result, action, "create");
361 cmdline_parse_token_string_t cmd_socket_path =
362         TOKEN_STRING_INITIALIZER(struct cmd_create_result, socket_path, NULL);
363 cmdline_parse_token_string_t cmd_bdf =
364         TOKEN_STRING_INITIALIZER(struct cmd_create_result, bdf, NULL);
365
366 cmdline_parse_inst_t cmd_create_vdpa_port = {
367         .f = cmd_create_vdpa_port_parsed,
368         .data = NULL,
369         .help_str = "create a new vdpa port",
370         .tokens = {
371                 (void *)&cmd_action_create,
372                 (void *)&cmd_socket_path,
373                 (void *)&cmd_bdf,
374                 NULL,
375         },
376 };
377
378 /* *** STATS *** */
379 struct cmd_stats_result {
380         cmdline_fixed_string_t stats;
381         uint16_t did;
382         uint16_t qid;
383 };
384
385 static void cmd_device_stats_parsed(void *parsed_result, struct cmdline *cl,
386                                     __rte_unused void *data)
387 {
388         struct cmd_stats_result *res = parsed_result;
389         struct rte_vdpa_device *vdev = rte_vdpa_get_device(res->did);
390         struct vdpa_port *vport = NULL;
391         uint32_t first, last;
392         int i;
393
394         if (!vdev) {
395                 RTE_LOG(ERR, VDPA, "Invalid device id %" PRIu16 ".\n",
396                         res->did);
397                 return;
398         }
399         for (i = 0; i < RTE_MIN(MAX_VDPA_SAMPLE_PORTS, dev_total); i++) {
400                 if (vports[i].did == res->did) {
401                         vport = &vports[i];
402                         break;
403                 }
404         }
405         if (!vport) {
406                 RTE_LOG(ERR, VDPA, "Device id %" PRIu16 " was not created.\n",
407                         res->did);
408                 return;
409         }
410         if (res->qid == 0xFFFF) {
411                 first = 0;
412                 last = rte_vhost_get_vring_num(vport->vid);
413                 if (last == 0) {
414                         RTE_LOG(ERR, VDPA, "Failed to get num of actual virtqs"
415                                 " for device id %d.\n", (int)res->did);
416                         return;
417                 }
418                 last--;
419         } else {
420                 first = res->qid;
421                 last = res->qid;
422         }
423         if (!vport->stats_names) {
424                 vport->stats_n = rte_vdpa_get_stats_names(vport->did, NULL, 0);
425                 if (vport->stats_n <= 0) {
426                         RTE_LOG(ERR, VDPA, "Failed to get names number of "
427                                 "device %d stats.\n", (int)res->did);
428                         return;
429                 }
430                 vport->stats_names = rte_zmalloc(NULL,
431                         (sizeof(*vport->stats_names) + sizeof(*vport->stats)) *
432                                                         vport->stats_n, 0);
433                 if (!vport->stats_names) {
434                         RTE_LOG(ERR, VDPA, "Failed to allocate memory for stat"
435                                 " names of device %d.\n", (int)res->did);
436                         return;
437                 }
438                 i = rte_vdpa_get_stats_names(vport->did, vport->stats_names,
439                                                 vport->stats_n);
440                 if (vport->stats_n != i) {
441                         RTE_LOG(ERR, VDPA, "Failed to get names of device %d "
442                                 "stats.\n", (int)res->did);
443                         return;
444                 }
445                 vport->stats = (struct rte_vdpa_stat *)
446                                         (vport->stats_names + vport->stats_n);
447         }
448         cmdline_printf(cl, "\nDevice %d:\n", (int)res->did);
449         for (; first <= last; first++) {
450                 memset(vport->stats, 0, sizeof(*vport->stats) * vport->stats_n);
451                 if (rte_vdpa_get_stats(vport->did, (int)first, vport->stats,
452                                         vport->stats_n) <= 0) {
453                         RTE_LOG(ERR, VDPA, "Failed to get vdpa queue statistics"
454                                 " for device id %d qid %d.\n", (int)res->did,
455                                 (int)first);
456                         return;
457                 }
458                 cmdline_printf(cl, "\tVirtq %" PRIu32 ":\n", first);
459                 for (i = 0; i < vport->stats_n; ++i) {
460                         cmdline_printf(cl, "\t\t%-*s %-16" PRIu64 "\n",
461                                 RTE_VDPA_STATS_NAME_SIZE,
462                                 vport->stats_names[vport->stats[i].id].name,
463                                 vport->stats[i].value);
464                 }
465         }
466 }
467
468 cmdline_parse_token_string_t cmd_device_stats_ =
469         TOKEN_STRING_INITIALIZER(struct cmd_stats_result, stats, "stats");
470 cmdline_parse_token_num_t cmd_device_id =
471         TOKEN_NUM_INITIALIZER(struct cmd_stats_result, did, UINT32);
472 cmdline_parse_token_num_t cmd_queue_id =
473         TOKEN_NUM_INITIALIZER(struct cmd_stats_result, qid, UINT32);
474
475 cmdline_parse_inst_t cmd_device_stats = {
476         .f = cmd_device_stats_parsed,
477         .data = NULL,
478         .help_str = "stats: show device statistics",
479         .tokens = {
480                 (void *)&cmd_device_stats_,
481                 (void *)&cmd_device_id,
482                 (void *)&cmd_queue_id,
483                 NULL,
484         },
485 };
486
487 /* *** QUIT *** */
488 struct cmd_quit_result {
489         cmdline_fixed_string_t quit;
490 };
491
492 static void cmd_quit_parsed(__rte_unused void *parsed_result,
493                 struct cmdline *cl,
494                 __rte_unused void *data)
495 {
496         vdpa_sample_quit();
497         cmdline_quit(cl);
498 }
499
500 cmdline_parse_token_string_t cmd_quit_quit =
501         TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
502
503 cmdline_parse_inst_t cmd_quit = {
504         .f = cmd_quit_parsed,
505         .data = NULL,
506         .help_str = "quit: exit application",
507         .tokens = {
508                 (void *)&cmd_quit_quit,
509                 NULL,
510         },
511 };
512 cmdline_parse_ctx_t main_ctx[] = {
513         (cmdline_parse_inst_t *)&cmd_help,
514         (cmdline_parse_inst_t *)&cmd_list_vdpa_devices,
515         (cmdline_parse_inst_t *)&cmd_create_vdpa_port,
516         (cmdline_parse_inst_t *)&cmd_device_stats,
517         (cmdline_parse_inst_t *)&cmd_quit,
518         NULL,
519 };
520
521 int
522 main(int argc, char *argv[])
523 {
524         char ch;
525         int did;
526         int ret;
527         struct cmdline *cl;
528         struct rte_device *dev;
529         struct rte_dev_iterator dev_iter;
530
531         ret = rte_eal_init(argc, argv);
532         if (ret < 0)
533                 rte_exit(EXIT_FAILURE, "eal init failed\n");
534         argc -= ret;
535         argv += ret;
536
537         dev_total = rte_vdpa_get_device_num();
538         if (dev_total <= 0)
539                 rte_exit(EXIT_FAILURE, "No available vdpa device found\n");
540
541         signal(SIGINT, signal_handler);
542         signal(SIGTERM, signal_handler);
543
544         ret = parse_args(argc, argv);
545         if (ret < 0)
546                 rte_exit(EXIT_FAILURE, "invalid argument\n");
547
548         if (interactive == 1) {
549                 cl = cmdline_stdin_new(main_ctx, "vdpa> ");
550                 if (cl == NULL)
551                         rte_panic("Cannot create cmdline instance\n");
552                 cmdline_interact(cl);
553                 cmdline_stdin_exit(cl);
554         } else {
555                 RTE_DEV_FOREACH(dev, "class=vdpa", &dev_iter) {
556                         did = rte_vdpa_find_device_id_by_name(dev->name);
557                         if (did < 0) {
558                                 rte_panic("Failed to find device id for %s\n",
559                                                 dev->name);
560                         }
561                         vports[devcnt].did = did;
562                         snprintf(vports[devcnt].ifname, MAX_PATH_LEN, "%s%d",
563                                         iface, devcnt);
564
565                         start_vdpa(&vports[devcnt]);
566                         devcnt++;
567                 }
568
569                 printf("enter \'q\' to quit\n");
570                 while (scanf("%c", &ch)) {
571                         if (ch == 'q')
572                                 break;
573                         while (ch != '\n') {
574                                 if (scanf("%c", &ch))
575                                         printf("%c", ch);
576                         }
577                         printf("enter \'q\' to quit\n");
578                 }
579                 vdpa_sample_quit();
580         }
581
582         return 0;
583 }