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