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