xref: /dpdk/drivers/bus/vdev/vdev.c (revision 52897e7e702b96e4ff6835ead95075b20f65392b)
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 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 
148 	VDEV_LOG(DEBUG, "Search driver %s to probe device %s", name,
149 		rte_vdev_device_name(dev));
150 
151 	if (vdev_parse(name, &driver))
152 		return -1;
153 	ret = driver->probe(dev);
154 	if (ret == 0)
155 		dev->device.driver = &driver->driver;
156 	return ret;
157 }
158 
159 /* The caller shall be responsible for thread-safe */
160 static struct rte_vdev_device *
161 find_vdev(const char *name)
162 {
163 	struct rte_vdev_device *dev;
164 
165 	if (!name)
166 		return NULL;
167 
168 	TAILQ_FOREACH(dev, &vdev_device_list, next) {
169 		const char *devname = rte_vdev_device_name(dev);
170 
171 		if (!strcmp(devname, name))
172 			return dev;
173 	}
174 
175 	return NULL;
176 }
177 
178 static struct rte_devargs *
179 alloc_devargs(const char *name, const char *args)
180 {
181 	struct rte_devargs *devargs;
182 	int ret;
183 
184 	devargs = calloc(1, sizeof(*devargs));
185 	if (!devargs)
186 		return NULL;
187 
188 	devargs->bus = &rte_vdev_bus;
189 	if (args)
190 		devargs->args = strdup(args);
191 	else
192 		devargs->args = strdup("");
193 
194 	ret = snprintf(devargs->name, sizeof(devargs->name), "%s", name);
195 	if (ret < 0 || ret >= (int)sizeof(devargs->name)) {
196 		free(devargs->args);
197 		free(devargs);
198 		return NULL;
199 	}
200 
201 	return devargs;
202 }
203 
204 static int
205 insert_vdev(const char *name, const char *args, struct rte_vdev_device **p_dev)
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.devargs = devargs;
225 	dev->device.numa_node = SOCKET_ID_ANY;
226 	dev->device.name = devargs->name;
227 
228 	if (find_vdev(name)) {
229 		ret = -EEXIST;
230 		goto fail;
231 	}
232 
233 	TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
234 	rte_devargs_insert(devargs);
235 
236 	if (p_dev)
237 		*p_dev = dev;
238 
239 	return 0;
240 fail:
241 	free(devargs->args);
242 	free(devargs);
243 	free(dev);
244 	return ret;
245 }
246 
247 int
248 rte_vdev_init(const char *name, const char *args)
249 {
250 	struct rte_vdev_device *dev;
251 	int ret;
252 
253 	rte_spinlock_recursive_lock(&vdev_device_list_lock);
254 	ret = insert_vdev(name, args, &dev);
255 	if (ret == 0) {
256 		ret = vdev_probe_all_drivers(dev);
257 		if (ret) {
258 			if (ret > 0)
259 				VDEV_LOG(ERR, "no driver found for %s", name);
260 			/* If fails, remove it from vdev list */
261 			TAILQ_REMOVE(&vdev_device_list, dev, next);
262 			rte_devargs_remove(dev->device.devargs);
263 			free(dev);
264 		}
265 	}
266 	rte_spinlock_recursive_unlock(&vdev_device_list_lock);
267 	return ret;
268 }
269 
270 static int
271 vdev_remove_driver(struct rte_vdev_device *dev)
272 {
273 	const char *name = rte_vdev_device_name(dev);
274 	const struct rte_vdev_driver *driver;
275 
276 	if (!dev->device.driver) {
277 		VDEV_LOG(DEBUG, "no driver attach to device %s", name);
278 		return 1;
279 	}
280 
281 	driver = container_of(dev->device.driver, const struct rte_vdev_driver,
282 		driver);
283 	return driver->remove(dev);
284 }
285 
286 int
287 rte_vdev_uninit(const char *name)
288 {
289 	struct rte_vdev_device *dev;
290 	int ret;
291 
292 	if (name == NULL)
293 		return -EINVAL;
294 
295 	rte_spinlock_recursive_lock(&vdev_device_list_lock);
296 
297 	dev = find_vdev(name);
298 	if (!dev) {
299 		ret = -ENOENT;
300 		goto unlock;
301 	}
302 
303 	ret = vdev_remove_driver(dev);
304 	if (ret)
305 		goto unlock;
306 
307 	TAILQ_REMOVE(&vdev_device_list, dev, next);
308 	rte_devargs_remove(dev->device.devargs);
309 	free(dev);
310 
311 unlock:
312 	rte_spinlock_recursive_unlock(&vdev_device_list_lock);
313 	return ret;
314 }
315 
316 struct vdev_param {
317 #define VDEV_SCAN_REQ	1
318 #define VDEV_SCAN_ONE	2
319 #define VDEV_SCAN_REP	3
320 	int type;
321 	int num;
322 	char name[RTE_DEV_NAME_MAX_LEN];
323 };
324 
325 static int vdev_plug(struct rte_device *dev);
326 
327 /**
328  * This function works as the action for both primary and secondary process
329  * for static vdev discovery when a secondary process is booting.
330  *
331  * step 1, secondary process sends a sync request to ask for vdev in primary;
332  * step 2, primary process receives the request, and send vdevs one by one;
333  * step 3, primary process sends back reply, which indicates how many vdevs
334  * are sent.
335  */
336 static int
337 vdev_action(const struct rte_mp_msg *mp_msg, const void *peer)
338 {
339 	struct rte_vdev_device *dev;
340 	struct rte_mp_msg mp_resp;
341 	struct vdev_param *ou = (struct vdev_param *)&mp_resp.param;
342 	const struct vdev_param *in = (const struct vdev_param *)mp_msg->param;
343 	const char *devname;
344 	int num;
345 	int ret;
346 
347 	strlcpy(mp_resp.name, VDEV_MP_KEY, sizeof(mp_resp.name));
348 	mp_resp.len_param = sizeof(*ou);
349 	mp_resp.num_fds = 0;
350 
351 	switch (in->type) {
352 	case VDEV_SCAN_REQ:
353 		ou->type = VDEV_SCAN_ONE;
354 		ou->num = 1;
355 		num = 0;
356 
357 		rte_spinlock_recursive_lock(&vdev_device_list_lock);
358 		TAILQ_FOREACH(dev, &vdev_device_list, next) {
359 			devname = rte_vdev_device_name(dev);
360 			if (strlen(devname) == 0) {
361 				VDEV_LOG(INFO, "vdev with no name is not sent");
362 				continue;
363 			}
364 			VDEV_LOG(INFO, "send vdev, %s", devname);
365 			strlcpy(ou->name, devname, RTE_DEV_NAME_MAX_LEN);
366 			if (rte_mp_sendmsg(&mp_resp) < 0)
367 				VDEV_LOG(ERR, "send vdev, %s, failed, %s",
368 					 devname, strerror(rte_errno));
369 			num++;
370 		}
371 		rte_spinlock_recursive_unlock(&vdev_device_list_lock);
372 
373 		ou->type = VDEV_SCAN_REP;
374 		ou->num = num;
375 		if (rte_mp_reply(&mp_resp, peer) < 0)
376 			VDEV_LOG(ERR, "Failed to reply a scan request");
377 		break;
378 	case VDEV_SCAN_ONE:
379 		VDEV_LOG(INFO, "receive vdev, %s", in->name);
380 		ret = insert_vdev(in->name, NULL, NULL);
381 		if (ret == -EEXIST)
382 			VDEV_LOG(DEBUG, "device already exist, %s", in->name);
383 		else if (ret < 0)
384 			VDEV_LOG(ERR, "failed to add vdev, %s", in->name);
385 		break;
386 	default:
387 		VDEV_LOG(ERR, "vdev cannot recognize this message");
388 	}
389 
390 	return 0;
391 }
392 
393 static int
394 vdev_scan(void)
395 {
396 	struct rte_vdev_device *dev;
397 	struct rte_devargs *devargs;
398 	struct vdev_custom_scan *custom_scan;
399 
400 	if (rte_mp_action_register(VDEV_MP_KEY, vdev_action) < 0 &&
401 	    rte_errno != EEXIST) {
402 		VDEV_LOG(ERR, "Failed to add vdev mp action");
403 		return -1;
404 	}
405 
406 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
407 		struct rte_mp_msg mp_req, *mp_rep;
408 		struct rte_mp_reply mp_reply;
409 		struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
410 		struct vdev_param *req = (struct vdev_param *)mp_req.param;
411 		struct vdev_param *resp;
412 
413 		strlcpy(mp_req.name, VDEV_MP_KEY, sizeof(mp_req.name));
414 		mp_req.len_param = sizeof(*req);
415 		mp_req.num_fds = 0;
416 		req->type = VDEV_SCAN_REQ;
417 		if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0 &&
418 		    mp_reply.nb_received == 1) {
419 			mp_rep = &mp_reply.msgs[0];
420 			resp = (struct vdev_param *)mp_rep->param;
421 			VDEV_LOG(INFO, "Received %d vdevs", resp->num);
422 		} else
423 			VDEV_LOG(ERR, "Failed to request vdev from primary");
424 
425 		/* Fall through to allow private vdevs in secondary process */
426 	}
427 
428 	/* call custom scan callbacks if any */
429 	rte_spinlock_lock(&vdev_custom_scan_lock);
430 	TAILQ_FOREACH(custom_scan, &vdev_custom_scans, next) {
431 		if (custom_scan->callback != NULL)
432 			/*
433 			 * the callback should update devargs list
434 			 * by calling rte_devargs_insert() with
435 			 *     devargs.bus = rte_bus_find_by_name("vdev");
436 			 *     devargs.type = RTE_DEVTYPE_VIRTUAL;
437 			 *     devargs.policy = RTE_DEV_WHITELISTED;
438 			 */
439 			custom_scan->callback(custom_scan->user_arg);
440 	}
441 	rte_spinlock_unlock(&vdev_custom_scan_lock);
442 
443 	/* for virtual devices we scan the devargs_list populated via cmdline */
444 	RTE_EAL_DEVARGS_FOREACH("vdev", devargs) {
445 
446 		dev = calloc(1, sizeof(*dev));
447 		if (!dev)
448 			return -1;
449 
450 		rte_spinlock_recursive_lock(&vdev_device_list_lock);
451 
452 		if (find_vdev(devargs->name)) {
453 			rte_spinlock_recursive_unlock(&vdev_device_list_lock);
454 			free(dev);
455 			continue;
456 		}
457 
458 		dev->device.bus = &rte_vdev_bus;
459 		dev->device.devargs = devargs;
460 		dev->device.numa_node = SOCKET_ID_ANY;
461 		dev->device.name = devargs->name;
462 
463 		TAILQ_INSERT_TAIL(&vdev_device_list, dev, next);
464 
465 		rte_spinlock_recursive_unlock(&vdev_device_list_lock);
466 	}
467 
468 	return 0;
469 }
470 
471 static int
472 vdev_probe(void)
473 {
474 	struct rte_vdev_device *dev;
475 	int ret = 0;
476 
477 	/* call the init function for each virtual device */
478 	TAILQ_FOREACH(dev, &vdev_device_list, next) {
479 		/* we don't use the vdev lock here, as it's only used in DPDK
480 		 * initialization; and we don't want to hold such a lock when
481 		 * we call each driver probe.
482 		 */
483 
484 		if (rte_dev_is_probed(&dev->device))
485 			continue;
486 
487 		if (vdev_probe_all_drivers(dev)) {
488 			VDEV_LOG(ERR, "failed to initialize %s device",
489 				rte_vdev_device_name(dev));
490 			ret = -1;
491 		}
492 	}
493 
494 	return ret;
495 }
496 
497 struct rte_device *
498 rte_vdev_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
499 		     const void *data)
500 {
501 	const struct rte_vdev_device *vstart;
502 	struct rte_vdev_device *dev;
503 
504 	rte_spinlock_recursive_lock(&vdev_device_list_lock);
505 	if (start != NULL) {
506 		vstart = RTE_DEV_TO_VDEV_CONST(start);
507 		dev = TAILQ_NEXT(vstart, next);
508 	} else {
509 		dev = TAILQ_FIRST(&vdev_device_list);
510 	}
511 	while (dev != NULL) {
512 		if (cmp(&dev->device, data) == 0)
513 			break;
514 		dev = TAILQ_NEXT(dev, next);
515 	}
516 	rte_spinlock_recursive_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 = rte_vdev_find_device,
537 	.plug = vdev_plug,
538 	.unplug = vdev_unplug,
539 	.parse = vdev_parse,
540 	.dev_iterate = rte_vdev_dev_iterate,
541 };
542 
543 RTE_REGISTER_BUS(vdev, rte_vdev_bus);
544 
545 RTE_INIT(vdev_init_log)
546 {
547 	vdev_logtype_bus = rte_log_register("bus.vdev");
548 	if (vdev_logtype_bus >= 0)
549 		rte_log_set_level(vdev_logtype_bus, RTE_LOG_NOTICE);
550 }
551