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