1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4 
5 #include <stdint.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <sys/socket.h>
10 
11 #include <rte_malloc.h>
12 #include <rte_kvargs.h>
13 #include <rte_ethdev_vdev.h>
14 #include <rte_bus_vdev.h>
15 #include <rte_alarm.h>
16 
17 #include "virtio_ethdev.h"
18 #include "virtio_logs.h"
19 #include "virtio_pci.h"
20 #include "virtqueue.h"
21 #include "virtio_rxtx.h"
22 #include "virtio_user/virtio_user_dev.h"
23 
24 #define virtio_user_get_dev(hw) \
25 	((struct virtio_user_dev *)(hw)->virtio_user_dev)
26 
27 static void
28 virtio_user_delayed_handler(void *param)
29 {
30 	struct virtio_hw *hw = (struct virtio_hw *)param;
31 	struct rte_eth_dev *dev = &rte_eth_devices[hw->port_id];
32 
33 	rte_intr_callback_unregister(dev->intr_handle,
34 				     virtio_interrupt_handler,
35 				     dev);
36 }
37 
38 static void
39 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
40 		     void *dst, int length)
41 {
42 	int i;
43 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
44 
45 	if (offset == offsetof(struct virtio_net_config, mac) &&
46 	    length == ETHER_ADDR_LEN) {
47 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
48 			((uint8_t *)dst)[i] = dev->mac_addr[i];
49 		return;
50 	}
51 
52 	if (offset == offsetof(struct virtio_net_config, status)) {
53 		char buf[128];
54 
55 		if (dev->vhostfd >= 0) {
56 			int r;
57 			int flags;
58 
59 			flags = fcntl(dev->vhostfd, F_GETFL);
60 			if (fcntl(dev->vhostfd, F_SETFL,
61 					flags | O_NONBLOCK) == -1) {
62 				PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
63 				return;
64 			}
65 			r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
66 			if (r == 0 || (r < 0 && errno != EAGAIN)) {
67 				dev->status &= (~VIRTIO_NET_S_LINK_UP);
68 				PMD_DRV_LOG(ERR, "virtio-user port %u is down",
69 					    hw->port_id);
70 				/* Only client mode is available now. Once the
71 				 * connection is broken, it can never be up
72 				 * again. Besides, this function could be called
73 				 * in the process of interrupt handling,
74 				 * callback cannot be unregistered here, set an
75 				 * alarm to do it.
76 				 */
77 				rte_eal_alarm_set(1,
78 						  virtio_user_delayed_handler,
79 						  (void *)hw);
80 			} else {
81 				dev->status |= VIRTIO_NET_S_LINK_UP;
82 			}
83 			if (fcntl(dev->vhostfd, F_SETFL,
84 					flags & ~O_NONBLOCK) == -1) {
85 				PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
86 				return;
87 			}
88 		}
89 		*(uint16_t *)dst = dev->status;
90 	}
91 
92 	if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
93 		*(uint16_t *)dst = dev->max_queue_pairs;
94 }
95 
96 static void
97 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
98 		      const void *src, int length)
99 {
100 	int i;
101 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
102 
103 	if ((offset == offsetof(struct virtio_net_config, mac)) &&
104 	    (length == ETHER_ADDR_LEN))
105 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
106 			dev->mac_addr[i] = ((const uint8_t *)src)[i];
107 	else
108 		PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
109 			    offset, length);
110 }
111 
112 static void
113 virtio_user_reset(struct virtio_hw *hw)
114 {
115 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
116 
117 	if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
118 		virtio_user_stop_device(dev);
119 }
120 
121 static void
122 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
123 {
124 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
125 
126 	if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
127 		virtio_user_start_device(dev);
128 	else if (status == VIRTIO_CONFIG_STATUS_RESET)
129 		virtio_user_reset(hw);
130 	dev->status = status;
131 }
132 
133 static uint8_t
134 virtio_user_get_status(struct virtio_hw *hw)
135 {
136 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
137 
138 	return dev->status;
139 }
140 
141 static uint64_t
142 virtio_user_get_features(struct virtio_hw *hw)
143 {
144 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
145 
146 	/* unmask feature bits defined in vhost user protocol */
147 	return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
148 }
149 
150 static void
151 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
152 {
153 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
154 
155 	dev->features = features & dev->device_features;
156 }
157 
158 static uint8_t
159 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
160 {
161 	/* rxq interrupts and config interrupt are separated in virtio-user,
162 	 * here we only report config change.
163 	 */
164 	return VIRTIO_PCI_ISR_CONFIG;
165 }
166 
167 static uint16_t
168 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
169 		    uint16_t vec __rte_unused)
170 {
171 	return 0;
172 }
173 
174 static uint16_t
175 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
176 			  struct virtqueue *vq __rte_unused,
177 			  uint16_t vec)
178 {
179 	/* pretend we have done that */
180 	return vec;
181 }
182 
183 /* This function is to get the queue size, aka, number of descs, of a specified
184  * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
185  * max supported queues.
186  */
187 static uint16_t
188 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
189 {
190 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
191 
192 	/* Currently, each queue has same queue size */
193 	return dev->queue_size;
194 }
195 
196 static int
197 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
198 {
199 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
200 	uint16_t queue_idx = vq->vq_queue_index;
201 	uint64_t desc_addr, avail_addr, used_addr;
202 
203 	desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
204 	avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
205 	used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
206 							 ring[vq->vq_nentries]),
207 				   VIRTIO_PCI_VRING_ALIGN);
208 
209 	dev->vrings[queue_idx].num = vq->vq_nentries;
210 	dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
211 	dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
212 	dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
213 
214 	return 0;
215 }
216 
217 static void
218 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
219 {
220 	/* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
221 	 * correspondingly stops the ioeventfds, and reset the status of
222 	 * the device.
223 	 * For modern devices, set queue desc, avail, used in PCI bar to 0,
224 	 * not see any more behavior in QEMU.
225 	 *
226 	 * Here we just care about what information to deliver to vhost-user
227 	 * or vhost-kernel. So we just close ioeventfd for now.
228 	 */
229 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
230 
231 	close(dev->callfds[vq->vq_queue_index]);
232 	close(dev->kickfds[vq->vq_queue_index]);
233 }
234 
235 static void
236 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
237 {
238 	uint64_t buf = 1;
239 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
240 
241 	if (hw->cvq && (hw->cvq->vq == vq)) {
242 		virtio_user_handle_cq(dev, vq->vq_queue_index);
243 		return;
244 	}
245 
246 	if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
247 		PMD_DRV_LOG(ERR, "failed to kick backend: %s",
248 			    strerror(errno));
249 }
250 
251 const struct virtio_pci_ops virtio_user_ops = {
252 	.read_dev_cfg	= virtio_user_read_dev_config,
253 	.write_dev_cfg	= virtio_user_write_dev_config,
254 	.reset		= virtio_user_reset,
255 	.get_status	= virtio_user_get_status,
256 	.set_status	= virtio_user_set_status,
257 	.get_features	= virtio_user_get_features,
258 	.set_features	= virtio_user_set_features,
259 	.get_isr	= virtio_user_get_isr,
260 	.set_config_irq	= virtio_user_set_config_irq,
261 	.set_queue_irq	= virtio_user_set_queue_irq,
262 	.get_queue_num	= virtio_user_get_queue_num,
263 	.setup_queue	= virtio_user_setup_queue,
264 	.del_queue	= virtio_user_del_queue,
265 	.notify_queue	= virtio_user_notify_queue,
266 };
267 
268 static const char *valid_args[] = {
269 #define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
270 	VIRTIO_USER_ARG_QUEUES_NUM,
271 #define VIRTIO_USER_ARG_CQ_NUM         "cq"
272 	VIRTIO_USER_ARG_CQ_NUM,
273 #define VIRTIO_USER_ARG_MAC            "mac"
274 	VIRTIO_USER_ARG_MAC,
275 #define VIRTIO_USER_ARG_PATH           "path"
276 	VIRTIO_USER_ARG_PATH,
277 #define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
278 	VIRTIO_USER_ARG_QUEUE_SIZE,
279 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
280 	VIRTIO_USER_ARG_INTERFACE_NAME,
281 	NULL
282 };
283 
284 #define VIRTIO_USER_DEF_CQ_EN	0
285 #define VIRTIO_USER_DEF_Q_NUM	1
286 #define VIRTIO_USER_DEF_Q_SZ	256
287 
288 static int
289 get_string_arg(const char *key __rte_unused,
290 	       const char *value, void *extra_args)
291 {
292 	if (!value || !extra_args)
293 		return -EINVAL;
294 
295 	*(char **)extra_args = strdup(value);
296 
297 	if (!*(char **)extra_args)
298 		return -ENOMEM;
299 
300 	return 0;
301 }
302 
303 static int
304 get_integer_arg(const char *key __rte_unused,
305 		const char *value, void *extra_args)
306 {
307 	if (!value || !extra_args)
308 		return -EINVAL;
309 
310 	*(uint64_t *)extra_args = strtoull(value, NULL, 0);
311 
312 	return 0;
313 }
314 
315 static struct rte_vdev_driver virtio_user_driver;
316 
317 static struct rte_eth_dev *
318 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
319 {
320 	struct rte_eth_dev *eth_dev;
321 	struct rte_eth_dev_data *data;
322 	struct virtio_hw *hw;
323 	struct virtio_user_dev *dev;
324 
325 	eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
326 	if (!eth_dev) {
327 		PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
328 		return NULL;
329 	}
330 
331 	data = eth_dev->data;
332 	hw = eth_dev->data->dev_private;
333 
334 	dev = rte_zmalloc(NULL, sizeof(*dev), 0);
335 	if (!dev) {
336 		PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
337 		rte_eth_dev_release_port(eth_dev);
338 		rte_free(hw);
339 		return NULL;
340 	}
341 
342 	hw->port_id = data->port_id;
343 	dev->port_id = data->port_id;
344 	virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
345 	/*
346 	 * MSIX is required to enable LSC (see virtio_init_device).
347 	 * Here just pretend that we support msix.
348 	 */
349 	hw->use_msix = 1;
350 	hw->modern   = 0;
351 	hw->use_simple_rx = 0;
352 	hw->use_simple_tx = 0;
353 	hw->virtio_user_dev = dev;
354 	return eth_dev;
355 }
356 
357 static void
358 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
359 {
360 	struct rte_eth_dev_data *data = eth_dev->data;
361 	struct virtio_hw *hw = data->dev_private;
362 
363 	rte_free(hw->virtio_user_dev);
364 	rte_free(hw);
365 	rte_eth_dev_release_port(eth_dev);
366 }
367 
368 /* Dev initialization routine. Invoked once for each virtio vdev at
369  * EAL init time, see rte_bus_probe().
370  * Returns 0 on success.
371  */
372 static int
373 virtio_user_pmd_probe(struct rte_vdev_device *dev)
374 {
375 	struct rte_kvargs *kvlist = NULL;
376 	struct rte_eth_dev *eth_dev;
377 	struct virtio_hw *hw;
378 	uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
379 	uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
380 	uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
381 	char *path = NULL;
382 	char *ifname = NULL;
383 	char *mac_addr = NULL;
384 	int ret = -1;
385 
386 	kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
387 	if (!kvlist) {
388 		PMD_INIT_LOG(ERR, "error when parsing param");
389 		goto end;
390 	}
391 
392 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
393 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
394 				       &get_string_arg, &path) < 0) {
395 			PMD_INIT_LOG(ERR, "error to parse %s",
396 				     VIRTIO_USER_ARG_PATH);
397 			goto end;
398 		}
399 	} else {
400 		PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
401 			  VIRTIO_USER_ARG_QUEUE_SIZE);
402 		goto end;
403 	}
404 
405 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
406 		if (is_vhost_user_by_type(path)) {
407 			PMD_INIT_LOG(ERR,
408 				"arg %s applies only to vhost-kernel backend",
409 				VIRTIO_USER_ARG_INTERFACE_NAME);
410 			goto end;
411 		}
412 
413 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
414 				       &get_string_arg, &ifname) < 0) {
415 			PMD_INIT_LOG(ERR, "error to parse %s",
416 				     VIRTIO_USER_ARG_INTERFACE_NAME);
417 			goto end;
418 		}
419 	}
420 
421 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
422 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
423 				       &get_string_arg, &mac_addr) < 0) {
424 			PMD_INIT_LOG(ERR, "error to parse %s",
425 				     VIRTIO_USER_ARG_MAC);
426 			goto end;
427 		}
428 	}
429 
430 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
431 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
432 				       &get_integer_arg, &queue_size) < 0) {
433 			PMD_INIT_LOG(ERR, "error to parse %s",
434 				     VIRTIO_USER_ARG_QUEUE_SIZE);
435 			goto end;
436 		}
437 	}
438 
439 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
440 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
441 				       &get_integer_arg, &queues) < 0) {
442 			PMD_INIT_LOG(ERR, "error to parse %s",
443 				     VIRTIO_USER_ARG_QUEUES_NUM);
444 			goto end;
445 		}
446 	}
447 
448 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
449 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
450 				       &get_integer_arg, &cq) < 0) {
451 			PMD_INIT_LOG(ERR, "error to parse %s",
452 				     VIRTIO_USER_ARG_CQ_NUM);
453 			goto end;
454 		}
455 	} else if (queues > 1) {
456 		cq = 1;
457 	}
458 
459 	if (queues > 1 && cq == 0) {
460 		PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
461 		goto end;
462 	}
463 
464 	if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
465 		PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
466 			VIRTIO_USER_ARG_QUEUES_NUM, queues,
467 			VIRTIO_MAX_VIRTQUEUE_PAIRS);
468 		goto end;
469 	}
470 
471 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
472 		eth_dev = virtio_user_eth_dev_alloc(dev);
473 		if (!eth_dev) {
474 			PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
475 			goto end;
476 		}
477 
478 		hw = eth_dev->data->dev_private;
479 		if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
480 				 queue_size, mac_addr, &ifname) < 0) {
481 			PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
482 			virtio_user_eth_dev_free(eth_dev);
483 			goto end;
484 		}
485 	} else {
486 		eth_dev = rte_eth_dev_attach_secondary(rte_vdev_device_name(dev));
487 		if (!eth_dev)
488 			goto end;
489 	}
490 
491 	/* previously called by rte_pci_probe() for physical dev */
492 	if (eth_virtio_dev_init(eth_dev) < 0) {
493 		PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
494 		virtio_user_eth_dev_free(eth_dev);
495 		goto end;
496 	}
497 	ret = 0;
498 
499 end:
500 	if (kvlist)
501 		rte_kvargs_free(kvlist);
502 	if (path)
503 		free(path);
504 	if (mac_addr)
505 		free(mac_addr);
506 	if (ifname)
507 		free(ifname);
508 	return ret;
509 }
510 
511 /** Called by rte_eth_dev_detach() */
512 static int
513 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
514 {
515 	const char *name;
516 	struct rte_eth_dev *eth_dev;
517 	struct virtio_hw *hw;
518 	struct virtio_user_dev *dev;
519 
520 	if (!vdev)
521 		return -EINVAL;
522 
523 	name = rte_vdev_device_name(vdev);
524 	PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
525 	eth_dev = rte_eth_dev_allocated(name);
526 	if (!eth_dev)
527 		return -ENODEV;
528 
529 	/* make sure the device is stopped, queues freed */
530 	rte_eth_dev_close(eth_dev->data->port_id);
531 
532 	hw = eth_dev->data->dev_private;
533 	dev = hw->virtio_user_dev;
534 	virtio_user_dev_uninit(dev);
535 
536 	rte_free(eth_dev->data->dev_private);
537 	rte_eth_dev_release_port(eth_dev);
538 
539 	return 0;
540 }
541 
542 static struct rte_vdev_driver virtio_user_driver = {
543 	.probe = virtio_user_pmd_probe,
544 	.remove = virtio_user_pmd_remove,
545 };
546 
547 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
548 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
549 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,
550 	"path=<path> "
551 	"mac=<mac addr> "
552 	"cq=<int> "
553 	"queue_size=<int> "
554 	"queues=<int> "
555 	"iface=<string>");
556