72254117917b3dc5c8d6f07fe1f447c6e37ca1a4
[dpdk.git] / drivers / bus / vdev / vdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 RehiveTech. All rights reserved.
3  */
4
5 #include <string.h>
6 #include <inttypes.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include <sys/queue.h>
12
13 #include <rte_eal.h>
14 #include <rte_dev.h>
15 #include <rte_bus.h>
16 #include <rte_common.h>
17 #include <rte_devargs.h>
18 #include <rte_memory.h>
19 #include <rte_tailq.h>
20 #include <rte_spinlock.h>
21 #include <rte_string_fns.h>
22 #include <rte_errno.h>
23
24 #include "rte_bus_vdev.h"
25 #include "vdev_logs.h"
26 #include "vdev_private.h"
27
28 #define VDEV_MP_KEY     "bus_vdev_mp"
29
30 int vdev_logtype_bus;
31
32 /* Forward declare to access virtual bus name */
33 static struct rte_bus rte_vdev_bus;
34
35 /** Double linked list of virtual device drivers. */
36 TAILQ_HEAD(vdev_device_list, rte_vdev_device);
37
38 static struct vdev_device_list vdev_device_list =
39         TAILQ_HEAD_INITIALIZER(vdev_device_list);
40 /* The lock needs to be recursive because a vdev can manage another vdev. */
41 static rte_spinlock_recursive_t vdev_device_list_lock =
42         RTE_SPINLOCK_RECURSIVE_INITIALIZER;
43
44 static struct vdev_driver_list vdev_driver_list =
45         TAILQ_HEAD_INITIALIZER(vdev_driver_list);
46
47 struct vdev_custom_scan {
48         TAILQ_ENTRY(vdev_custom_scan) next;
49         rte_vdev_scan_callback callback;
50         void *user_arg;
51 };
52 TAILQ_HEAD(vdev_custom_scans, vdev_custom_scan);
53 static struct vdev_custom_scans vdev_custom_scans =
54         TAILQ_HEAD_INITIALIZER(vdev_custom_scans);
55 static rte_spinlock_t vdev_custom_scan_lock = RTE_SPINLOCK_INITIALIZER;
56
57 /* register a driver */
58 void
59 rte_vdev_register(struct rte_vdev_driver *driver)
60 {
61         TAILQ_INSERT_TAIL(&vdev_driver_list, driver, next);
62 }
63
64 /* unregister a driver */
65 void
66 rte_vdev_unregister(struct rte_vdev_driver *driver)
67 {
68         TAILQ_REMOVE(&vdev_driver_list, driver, next);
69 }
70
71 int
72 rte_vdev_add_custom_scan(rte_vdev_scan_callback callback, void *user_arg)
73 {
74         struct vdev_custom_scan *custom_scan;
75
76         rte_spinlock_lock(&vdev_custom_scan_lock);
77
78         /* check if already registered */
79         TAILQ_FOREACH(custom_scan, &vdev_custom_scans, next) {
80                 if (custom_scan->callback == callback &&
81                                 custom_scan->user_arg == user_arg)
82                         break;
83         }
84
85         if (custom_scan == NULL) {
86                 custom_scan = malloc(sizeof(struct vdev_custom_scan));
87                 if (custom_scan != NULL) {
88                         custom_scan->callback = callback;
89                         custom_scan->user_arg = user_arg;
90                         TAILQ_INSERT_TAIL(&vdev_custom_scans, custom_scan, next);
91                 }
92         }
93
94         rte_spinlock_unlock(&vdev_custom_scan_lock);
95
96         return (custom_scan == NULL) ? -1 : 0;
97 }
98
99 int
100 rte_vdev_remove_custom_scan(rte_vdev_scan_callback callback, void *user_arg)
101 {
102         struct vdev_custom_scan *custom_scan, *tmp_scan;
103
104         rte_spinlock_lock(&vdev_custom_scan_lock);
105         TAILQ_FOREACH_SAFE(custom_scan, &vdev_custom_scans, next, tmp_scan) {
106                 if (custom_scan->callback != callback ||
107                                 (custom_scan->user_arg != (void *)-1 &&
108                                 custom_scan->user_arg != user_arg))
109                         continue;
110                 TAILQ_REMOVE(&vdev_custom_scans, custom_scan, next);
111                 free(custom_scan);
112         }
113         rte_spinlock_unlock(&vdev_custom_scan_lock);
114
115         return 0;
116 }
117
118 static int
119 vdev_parse(const char *name, void *addr)
120 {
121         struct rte_vdev_driver **out = addr;
122         struct rte_vdev_driver *driver = NULL;
123
124         TAILQ_FOREACH(driver, &vdev_driver_list, next) {
125                 if (strncmp(driver->driver.name, name,
126                             strlen(driver->driver.name)) == 0)
127                         break;
128                 if (driver->driver.alias &&
129                     strncmp(driver->driver.alias, name,
130                             strlen(driver->driver.alias)) == 0)
131                         break;
132         }
133         if (driver != NULL &&
134             addr != NULL)
135                 *out = driver;
136         return driver == NULL;
137 }
138
139 static int
140 vdev_probe_all_drivers(struct rte_vdev_device *dev)
141 {
142         const char *name;
143         struct rte_vdev_driver *driver;
144         int ret;
145
146         name = rte_vdev_device_name(dev);
147         VDEV_LOG(DEBUG, "Search driver to probe device %s", name);
148
149         if (vdev_parse(name, &driver))
150                 return -1;
151         ret = driver->probe(dev);
152         if (ret == 0)
153                 dev->device.driver = &driver->driver;
154         return ret;
155 }
156
157 /* The caller shall be responsible for thread-safe */
158 static struct rte_vdev_device *
159 find_vdev(const char *name)
160 {
161         struct rte_vdev_device *dev;
162
163         if (!name)
164                 return NULL;
165
166         TAILQ_FOREACH(dev, &vdev_device_list, next) {
167                 const char *devname = rte_vdev_device_name(dev);
168
169                 if (!strcmp(devname, name))
170                         return dev;
171         }
172
173         return NULL;
174 }
175
176 static struct rte_devargs *
177 alloc_devargs(const char *name, const char *args)
178 {
179         struct rte_devargs *devargs;
180         int ret;
181
182         devargs = calloc(1, sizeof(*devargs));
183         if (!devargs)
184                 return NULL;
185
186         devargs->bus = &rte_vdev_bus;
187         if (args)
188                 devargs->args = strdup(args);
189         else
190                 devargs->args = strdup("");
191
192         ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
193         if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
194                 free(devargs->args);
195                 free(devargs);
196                 return NULL;
197         }
198
199         return devargs;
200 }
201
202 static int
203 insert_vdev(const char *name, const char *args,
204                 struct rte_vdev_device **p_dev,
205                 bool init)
206 {
207         struct rte_vdev_device *dev;
208         struct rte_devargs *devargs;
209         int ret;
210
211         if (name == NULL)
212                 return -EINVAL;
213
214         devargs = alloc_devargs(name, args);
215         if (!devargs)
216                 return -ENOMEM;
217
218         dev = calloc(1, sizeof(*dev));
219         if (!dev) {
220                 ret = -ENOMEM;
221                 goto fail;
222         }
223
224         dev->device.bus = &rte_vdev_bus;
225         dev->device.numa_node = SOCKET_ID_ANY;
226         dev->device.name = devargs->name;
227
228         if (find_vdev(name)) {
229                 /*
230                  * A vdev is expected to have only one port.
231                  * So there is no reason to try probing again,
232                  * even with new arguments.
233                  */
234                 ret = -EEXIST;
235                 goto fail;
236         }
237
238         if (init)
239                 rte_devargs_insert(&devargs);
240         dev->device.devargs = devargs;
241         TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
242
243         if (p_dev)
244                 *p_dev = dev;
245
246         return 0;
247 fail:
248         free(devargs->args);
249         free(devargs);
250         free(dev);
251         return ret;
252 }
253
254 int
255 rte_vdev_init(const char *name, const char *args)
256 {
257         struct rte_vdev_device *dev;
258         int ret;
259
260         rte_spinlock_recursive_lock(&vdev_device_list_lock);
261         ret = insert_vdev(name, args, &dev, true);
262         if (ret == 0) {
263                 ret = vdev_probe_all_drivers(dev);
264                 if (ret) {
265                         if (ret > 0)
266                                 VDEV_LOG(ERR, "no driver found for %s", name);
267                         /* If fails, remove it from vdev list */
268                         TAILQ_REMOVE(&vdev_device_list, dev, next);
269                         rte_devargs_remove(dev->device.devargs);
270                         free(dev);
271                 }
272         }
273         rte_spinlock_recursive_unlock(&vdev_device_list_lock);
274         return ret;
275 }
276
277 static int
278 vdev_remove_driver(struct rte_vdev_device *dev)
279 {
280         const char *name = rte_vdev_device_name(dev);
281         const struct rte_vdev_driver *driver;
282
283         if (!dev->device.driver) {
284                 VDEV_LOG(DEBUG, "no driver attach to device %s", name);
285                 return 1;
286         }
287
288         driver = container_of(dev->device.driver, const struct rte_vdev_driver,
289                 driver);
290         return driver->remove(dev);
291 }
292
293 int
294 rte_vdev_uninit(const char *name)
295 {
296         struct rte_vdev_device *dev;
297         int ret;
298
299         if (name == NULL)
300                 return -EINVAL;
301
302         rte_spinlock_recursive_lock(&vdev_device_list_lock);
303
304         dev = find_vdev(name);
305         if (!dev) {
306                 ret = -ENOENT;
307                 goto unlock;
308         }
309
310         ret = vdev_remove_driver(dev);
311         if (ret)
312                 goto unlock;
313
314         TAILQ_REMOVE(&vdev_device_list, dev, next);
315         rte_devargs_remove(dev->device.devargs);
316         free(dev);
317
318 unlock:
319         rte_spinlock_recursive_unlock(&vdev_device_list_lock);
320         return ret;
321 }
322
323 struct vdev_param {
324 #define VDEV_SCAN_REQ   1
325 #define VDEV_SCAN_ONE   2
326 #define VDEV_SCAN_REP   3
327         int type;
328         int num;
329         char name[RTE_DEV_NAME_MAX_LEN];
330 };
331
332 static int vdev_plug(struct rte_device *dev);
333
334 /**
335  * This function works as the action for both primary and secondary process
336  * for static vdev discovery when a secondary process is booting.
337  *
338  * step 1, secondary process sends a sync request to ask for vdev in primary;
339  * step 2, primary process receives the request, and send vdevs one by one;
340  * step 3, primary process sends back reply, which indicates how many vdevs
341  * are sent.
342  */
343 static int
344 vdev_action(const struct rte_mp_msg *mp_msg, const void *peer)
345 {
346         struct rte_vdev_device *dev;
347         struct rte_mp_msg mp_resp;
348         struct vdev_param *ou = (struct vdev_param *)&mp_resp.param;
349         const struct vdev_param *in = (const struct vdev_param *)mp_msg->param;
350         const char *devname;
351         int num;
352         int ret;
353
354         strlcpy(mp_resp.name, VDEV_MP_KEY, sizeof(mp_resp.name));
355         mp_resp.len_param = sizeof(*ou);
356         mp_resp.num_fds = 0;
357
358         switch (in->type) {
359         case VDEV_SCAN_REQ:
360                 ou->type = VDEV_SCAN_ONE;
361                 ou->num = 1;
362                 num = 0;
363
364                 rte_spinlock_recursive_lock(&vdev_device_list_lock);
365                 TAILQ_FOREACH(dev, &vdev_device_list, next) {
366                         devname = rte_vdev_device_name(dev);
367                         if (strlen(devname) == 0) {
368                                 VDEV_LOG(INFO, "vdev with no name is not sent");
369                                 continue;
370                         }
371                         VDEV_LOG(INFO, "send vdev, %s", devname);
372                         strlcpy(ou->name, devname, RTE_DEV_NAME_MAX_LEN);
373                         if (rte_mp_sendmsg(&mp_resp) < 0)
374                                 VDEV_LOG(ERR, "send vdev, %s, failed, %s",
375                                          devname, strerror(rte_errno));
376                         num++;
377                 }
378                 rte_spinlock_recursive_unlock(&vdev_device_list_lock);
379
380                 ou->type = VDEV_SCAN_REP;
381                 ou->num = num;
382                 if (rte_mp_reply(&mp_resp, peer) < 0)
383                         VDEV_LOG(ERR, "Failed to reply a scan request");
384                 break;
385         case VDEV_SCAN_ONE:
386                 VDEV_LOG(INFO, "receive vdev, %s", in->name);
387                 ret = insert_vdev(in->name, NULL, NULL, false);
388                 if (ret == -EEXIST)
389                         VDEV_LOG(DEBUG, "device already exist, %s", in->name);
390                 else if (ret < 0)
391                         VDEV_LOG(ERR, "failed to add vdev, %s", in->name);
392                 break;
393         default:
394                 VDEV_LOG(ERR, "vdev cannot recognize this message");
395         }
396
397         return 0;
398 }
399
400 static int
401 vdev_scan(void)
402 {
403         struct rte_vdev_device *dev;
404         struct rte_devargs *devargs;
405         struct vdev_custom_scan *custom_scan;
406
407         if (rte_mp_action_register(VDEV_MP_KEY, vdev_action) < 0 &&
408             rte_errno != EEXIST) {
409                 VDEV_LOG(ERR, "Failed to add vdev mp action");
410                 return -1;
411         }
412
413         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
414                 struct rte_mp_msg mp_req, *mp_rep;
415                 struct rte_mp_reply mp_reply;
416                 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
417                 struct vdev_param *req = (struct vdev_param *)mp_req.param;
418                 struct vdev_param *resp;
419
420                 strlcpy(mp_req.name, VDEV_MP_KEY, sizeof(mp_req.name));
421                 mp_req.len_param = sizeof(*req);
422                 mp_req.num_fds = 0;
423                 req->type = VDEV_SCAN_REQ;
424                 if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
425                     mp_reply.nb_received == 1) {
426                         mp_rep = &mp_reply.msgs[0];
427                         resp = (struct vdev_param *)mp_rep->param;
428                         VDEV_LOG(INFO, "Received %d vdevs", resp->num);
429                         free(mp_reply.msgs);
430                 } else
431                         VDEV_LOG(ERR, "Failed to request vdev from primary");
432
433                 /* Fall through to allow private vdevs in secondary process */
434         }
435
436         /* call custom scan callbacks if any */
437         rte_spinlock_lock(&vdev_custom_scan_lock);
438         TAILQ_FOREACH(custom_scan, &vdev_custom_scans, next) {
439                 if (custom_scan->callback != NULL)
440                         /*
441                          * the callback should update devargs list
442                          * by calling rte_devargs_insert() with
443                          *     devargs.bus = rte_bus_find_by_name("vdev");
444                          *     devargs.type = RTE_DEVTYPE_VIRTUAL;
445                          *     devargs.policy = RTE_DEV_WHITELISTED;
446                          */
447                         custom_scan->callback(custom_scan->user_arg);
448         }
449         rte_spinlock_unlock(&vdev_custom_scan_lock);
450
451         /* for virtual devices we scan the devargs_list populated via cmdline */
452         RTE_EAL_DEVARGS_FOREACH("vdev", devargs) {
453
454                 dev = calloc(1, sizeof(*dev));
455                 if (!dev)
456                         return -1;
457
458                 rte_spinlock_recursive_lock(&vdev_device_list_lock);
459
460                 if (find_vdev(devargs->name)) {
461                         rte_spinlock_recursive_unlock(&vdev_device_list_lock);
462                         free(dev);
463                         continue;
464                 }
465
466                 dev->device.bus = &rte_vdev_bus;
467                 dev->device.devargs = devargs;
468                 dev->device.numa_node = SOCKET_ID_ANY;
469                 dev->device.name = devargs->name;
470
471                 TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
472
473                 rte_spinlock_recursive_unlock(&vdev_device_list_lock);
474         }
475
476         return 0;
477 }
478
479 static int
480 vdev_probe(void)
481 {
482         struct rte_vdev_device *dev;
483         int ret = 0;
484
485         /* call the init function for each virtual device */
486         TAILQ_FOREACH(dev, &vdev_device_list, next) {
487                 /* we don't use the vdev lock here, as it's only used in DPDK
488                  * initialization; and we don't want to hold such a lock when
489                  * we call each driver probe.
490                  */
491
492                 if (rte_dev_is_probed(&dev->device))
493                         continue;
494
495                 if (vdev_probe_all_drivers(dev)) {
496                         VDEV_LOG(ERR, "failed to initialize %s device",
497                                 rte_vdev_device_name(dev));
498                         ret = -1;
499                 }
500         }
501
502         return ret;
503 }
504
505 struct rte_device *
506 rte_vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
507                      const void *data)
508 {
509         const struct rte_vdev_device *vstart;
510         struct rte_vdev_device *dev;
511
512         rte_spinlock_recursive_lock(&vdev_device_list_lock);
513         if (start != NULL) {
514                 vstart = RTE_DEV_TO_VDEV_CONST(start);
515                 dev = TAILQ_NEXT(vstart, next);
516         } else {
517                 dev = TAILQ_FIRST(&vdev_device_list);
518         }
519         while (dev != NULL) {
520                 if (cmp(&dev->device, data) == 0)
521                         break;
522                 dev = TAILQ_NEXT(dev, next);
523         }
524         rte_spinlock_recursive_unlock(&vdev_device_list_lock);
525
526         return dev ? &dev->device : NULL;
527 }
528
529 static int
530 vdev_plug(struct rte_device *dev)
531 {
532         return vdev_probe_all_drivers(RTE_DEV_TO_VDEV(dev));
533 }
534
535 static int
536 vdev_unplug(struct rte_device *dev)
537 {
538         return rte_vdev_uninit(dev->name);
539 }
540
541 static struct rte_bus rte_vdev_bus = {
542         .scan = vdev_scan,
543         .probe = vdev_probe,
544         .find_device = rte_vdev_find_device,
545         .plug = vdev_plug,
546         .unplug = vdev_unplug,
547         .parse = vdev_parse,
548         .dev_iterate = rte_vdev_dev_iterate,
549 };
550
551 RTE_REGISTER_BUS(vdev, rte_vdev_bus);
552
553 RTE_INIT(vdev_init_log)
554 {
555         vdev_logtype_bus = rte_log_register("bus.vdev");
556         if (vdev_logtype_bus >= 0)
557                 rte_log_set_level(vdev_logtype_bus, RTE_LOG_NOTICE);
558 }