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