xref: /dpdk/drivers/net/virtio/virtio_user_ethdev.c (revision 089e5ed727a15da2729cfee9b63533dd120bd04c)
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 #include "virtio_user/vhost.h"
24 
25 #define virtio_user_get_dev(hw) \
26 	((struct virtio_user_dev *)(hw)->virtio_user_dev)
27 
28 static int
29 virtio_user_server_reconnect(struct virtio_user_dev *dev)
30 {
31 	int ret;
32 	int connectfd;
33 	struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
34 
35 	connectfd = accept(dev->listenfd, NULL, NULL);
36 	if (connectfd < 0)
37 		return -1;
38 
39 	dev->vhostfd = connectfd;
40 	if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
41 				   &dev->device_features) < 0) {
42 		PMD_INIT_LOG(ERR, "get_features failed: %s",
43 			     strerror(errno));
44 		return -1;
45 	}
46 
47 	dev->device_features |= dev->frontend_features;
48 
49 	/* umask vhost-user unsupported features */
50 	dev->device_features &= ~(dev->unsupported_features);
51 
52 	dev->features &= dev->device_features;
53 
54 	ret = virtio_user_start_device(dev);
55 	if (ret < 0)
56 		return -1;
57 
58 	if (dev->queue_pairs > 1) {
59 		ret = virtio_user_handle_mq(dev, dev->queue_pairs);
60 		if (ret != 0) {
61 			PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
62 			return -1;
63 		}
64 	}
65 	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
66 		if (rte_intr_disable(eth_dev->intr_handle) < 0) {
67 			PMD_DRV_LOG(ERR, "interrupt disable failed");
68 			return -1;
69 		}
70 		rte_intr_callback_unregister(eth_dev->intr_handle,
71 					     virtio_interrupt_handler,
72 					     eth_dev);
73 		eth_dev->intr_handle->fd = connectfd;
74 		rte_intr_callback_register(eth_dev->intr_handle,
75 					   virtio_interrupt_handler, eth_dev);
76 
77 		if (rte_intr_enable(eth_dev->intr_handle) < 0) {
78 			PMD_DRV_LOG(ERR, "interrupt enable failed");
79 			return -1;
80 		}
81 	}
82 	PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");
83 	return 0;
84 }
85 
86 static void
87 virtio_user_delayed_handler(void *param)
88 {
89 	struct virtio_hw *hw = (struct virtio_hw *)param;
90 	struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id];
91 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
92 
93 	if (rte_intr_disable(eth_dev->intr_handle) < 0) {
94 		PMD_DRV_LOG(ERR, "interrupt disable failed");
95 		return;
96 	}
97 	rte_intr_callback_unregister(eth_dev->intr_handle,
98 				     virtio_interrupt_handler, eth_dev);
99 	if (dev->is_server) {
100 		if (dev->vhostfd >= 0) {
101 			close(dev->vhostfd);
102 			dev->vhostfd = -1;
103 		}
104 		eth_dev->intr_handle->fd = dev->listenfd;
105 		rte_intr_callback_register(eth_dev->intr_handle,
106 					   virtio_interrupt_handler, eth_dev);
107 		if (rte_intr_enable(eth_dev->intr_handle) < 0) {
108 			PMD_DRV_LOG(ERR, "interrupt enable failed");
109 			return;
110 		}
111 	}
112 }
113 
114 static void
115 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
116 		     void *dst, int length)
117 {
118 	int i;
119 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
120 
121 	if (offset == offsetof(struct virtio_net_config, mac) &&
122 	    length == RTE_ETHER_ADDR_LEN) {
123 		for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
124 			((uint8_t *)dst)[i] = dev->mac_addr[i];
125 		return;
126 	}
127 
128 	if (offset == offsetof(struct virtio_net_config, status)) {
129 		char buf[128];
130 
131 		if (dev->vhostfd >= 0) {
132 			int r;
133 			int flags;
134 
135 			flags = fcntl(dev->vhostfd, F_GETFL);
136 			if (fcntl(dev->vhostfd, F_SETFL,
137 					flags | O_NONBLOCK) == -1) {
138 				PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
139 				return;
140 			}
141 			r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
142 			if (r == 0 || (r < 0 && errno != EAGAIN)) {
143 				dev->status &= (~VIRTIO_NET_S_LINK_UP);
144 				PMD_DRV_LOG(ERR, "virtio-user port %u is down",
145 					    hw->port_id);
146 
147 				/* This function could be called in the process
148 				 * of interrupt handling, callback cannot be
149 				 * unregistered here, set an alarm to do it.
150 				 */
151 				rte_eal_alarm_set(1,
152 						  virtio_user_delayed_handler,
153 						  (void *)hw);
154 			} else {
155 				dev->status |= VIRTIO_NET_S_LINK_UP;
156 			}
157 			if (fcntl(dev->vhostfd, F_SETFL,
158 					flags & ~O_NONBLOCK) == -1) {
159 				PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
160 				return;
161 			}
162 		} else if (dev->is_server) {
163 			dev->status &= (~VIRTIO_NET_S_LINK_UP);
164 			if (virtio_user_server_reconnect(dev) >= 0)
165 				dev->status |= VIRTIO_NET_S_LINK_UP;
166 		}
167 
168 		*(uint16_t *)dst = dev->status;
169 	}
170 
171 	if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
172 		*(uint16_t *)dst = dev->max_queue_pairs;
173 }
174 
175 static void
176 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
177 		      const void *src, int length)
178 {
179 	int i;
180 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
181 
182 	if ((offset == offsetof(struct virtio_net_config, mac)) &&
183 	    (length == RTE_ETHER_ADDR_LEN))
184 		for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
185 			dev->mac_addr[i] = ((const uint8_t *)src)[i];
186 	else
187 		PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
188 			    offset, length);
189 }
190 
191 static void
192 virtio_user_reset(struct virtio_hw *hw)
193 {
194 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
195 
196 	if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
197 		virtio_user_stop_device(dev);
198 }
199 
200 static void
201 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
202 {
203 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
204 
205 	if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
206 		virtio_user_start_device(dev);
207 	else if (status == VIRTIO_CONFIG_STATUS_RESET)
208 		virtio_user_reset(hw);
209 	dev->status = status;
210 }
211 
212 static uint8_t
213 virtio_user_get_status(struct virtio_hw *hw)
214 {
215 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
216 
217 	return dev->status;
218 }
219 
220 static uint64_t
221 virtio_user_get_features(struct virtio_hw *hw)
222 {
223 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
224 
225 	/* unmask feature bits defined in vhost user protocol */
226 	return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
227 }
228 
229 static void
230 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
231 {
232 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
233 
234 	dev->features = features & dev->device_features;
235 }
236 
237 static uint8_t
238 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
239 {
240 	/* rxq interrupts and config interrupt are separated in virtio-user,
241 	 * here we only report config change.
242 	 */
243 	return VIRTIO_PCI_ISR_CONFIG;
244 }
245 
246 static uint16_t
247 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
248 		    uint16_t vec __rte_unused)
249 {
250 	return 0;
251 }
252 
253 static uint16_t
254 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
255 			  struct virtqueue *vq __rte_unused,
256 			  uint16_t vec)
257 {
258 	/* pretend we have done that */
259 	return vec;
260 }
261 
262 /* This function is to get the queue size, aka, number of descs, of a specified
263  * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
264  * max supported queues.
265  */
266 static uint16_t
267 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
268 {
269 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
270 
271 	/* Currently, each queue has same queue size */
272 	return dev->queue_size;
273 }
274 
275 static void
276 virtio_user_setup_queue_packed(struct virtqueue *vq,
277 			       struct virtio_user_dev *dev)
278 {
279 	uint16_t queue_idx = vq->vq_queue_index;
280 	struct vring_packed *vring;
281 	uint64_t desc_addr;
282 	uint64_t avail_addr;
283 	uint64_t used_addr;
284 	uint16_t i;
285 
286 	vring  = &dev->packed_vrings[queue_idx];
287 	desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
288 	avail_addr = desc_addr + vq->vq_nentries *
289 		sizeof(struct vring_packed_desc);
290 	used_addr = RTE_ALIGN_CEIL(avail_addr +
291 			   sizeof(struct vring_packed_desc_event),
292 			   VIRTIO_PCI_VRING_ALIGN);
293 	vring->num = vq->vq_nentries;
294 	vring->desc = (void *)(uintptr_t)desc_addr;
295 	vring->driver = (void *)(uintptr_t)avail_addr;
296 	vring->device = (void *)(uintptr_t)used_addr;
297 	dev->packed_queues[queue_idx].avail_wrap_counter = true;
298 	dev->packed_queues[queue_idx].used_wrap_counter = true;
299 
300 	for (i = 0; i < vring->num; i++)
301 		vring->desc[i].flags = 0;
302 }
303 
304 static void
305 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
306 {
307 	uint16_t queue_idx = vq->vq_queue_index;
308 	uint64_t desc_addr, avail_addr, used_addr;
309 
310 	desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
311 	avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
312 	used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
313 							 ring[vq->vq_nentries]),
314 				   VIRTIO_PCI_VRING_ALIGN);
315 
316 	dev->vrings[queue_idx].num = vq->vq_nentries;
317 	dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
318 	dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
319 	dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
320 }
321 
322 static int
323 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
324 {
325 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
326 
327 	if (vtpci_packed_queue(hw))
328 		virtio_user_setup_queue_packed(vq, dev);
329 	else
330 		virtio_user_setup_queue_split(vq, dev);
331 
332 	return 0;
333 }
334 
335 static void
336 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
337 {
338 	/* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
339 	 * correspondingly stops the ioeventfds, and reset the status of
340 	 * the device.
341 	 * For modern devices, set queue desc, avail, used in PCI bar to 0,
342 	 * not see any more behavior in QEMU.
343 	 *
344 	 * Here we just care about what information to deliver to vhost-user
345 	 * or vhost-kernel. So we just close ioeventfd for now.
346 	 */
347 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
348 
349 	close(dev->callfds[vq->vq_queue_index]);
350 	close(dev->kickfds[vq->vq_queue_index]);
351 }
352 
353 static void
354 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
355 {
356 	uint64_t buf = 1;
357 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
358 
359 	if (hw->cvq && (hw->cvq->vq == vq)) {
360 		if (vtpci_packed_queue(vq->hw))
361 			virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
362 		else
363 			virtio_user_handle_cq(dev, vq->vq_queue_index);
364 		return;
365 	}
366 
367 	if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
368 		PMD_DRV_LOG(ERR, "failed to kick backend: %s",
369 			    strerror(errno));
370 }
371 
372 const struct virtio_pci_ops virtio_user_ops = {
373 	.read_dev_cfg	= virtio_user_read_dev_config,
374 	.write_dev_cfg	= virtio_user_write_dev_config,
375 	.get_status	= virtio_user_get_status,
376 	.set_status	= virtio_user_set_status,
377 	.get_features	= virtio_user_get_features,
378 	.set_features	= virtio_user_set_features,
379 	.get_isr	= virtio_user_get_isr,
380 	.set_config_irq	= virtio_user_set_config_irq,
381 	.set_queue_irq	= virtio_user_set_queue_irq,
382 	.get_queue_num	= virtio_user_get_queue_num,
383 	.setup_queue	= virtio_user_setup_queue,
384 	.del_queue	= virtio_user_del_queue,
385 	.notify_queue	= virtio_user_notify_queue,
386 };
387 
388 static const char *valid_args[] = {
389 #define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
390 	VIRTIO_USER_ARG_QUEUES_NUM,
391 #define VIRTIO_USER_ARG_CQ_NUM         "cq"
392 	VIRTIO_USER_ARG_CQ_NUM,
393 #define VIRTIO_USER_ARG_MAC            "mac"
394 	VIRTIO_USER_ARG_MAC,
395 #define VIRTIO_USER_ARG_PATH           "path"
396 	VIRTIO_USER_ARG_PATH,
397 #define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
398 	VIRTIO_USER_ARG_QUEUE_SIZE,
399 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
400 	VIRTIO_USER_ARG_INTERFACE_NAME,
401 #define VIRTIO_USER_ARG_SERVER_MODE    "server"
402 	VIRTIO_USER_ARG_SERVER_MODE,
403 #define VIRTIO_USER_ARG_MRG_RXBUF      "mrg_rxbuf"
404 	VIRTIO_USER_ARG_MRG_RXBUF,
405 #define VIRTIO_USER_ARG_IN_ORDER       "in_order"
406 	VIRTIO_USER_ARG_IN_ORDER,
407 #define VIRTIO_USER_ARG_PACKED_VQ      "packed_vq"
408 	VIRTIO_USER_ARG_PACKED_VQ,
409 	NULL
410 };
411 
412 #define VIRTIO_USER_DEF_CQ_EN	0
413 #define VIRTIO_USER_DEF_Q_NUM	1
414 #define VIRTIO_USER_DEF_Q_SZ	256
415 #define VIRTIO_USER_DEF_SERVER_MODE	0
416 
417 static int
418 get_string_arg(const char *key __rte_unused,
419 	       const char *value, void *extra_args)
420 {
421 	if (!value || !extra_args)
422 		return -EINVAL;
423 
424 	*(char **)extra_args = strdup(value);
425 
426 	if (!*(char **)extra_args)
427 		return -ENOMEM;
428 
429 	return 0;
430 }
431 
432 static int
433 get_integer_arg(const char *key __rte_unused,
434 		const char *value, void *extra_args)
435 {
436 	if (!value || !extra_args)
437 		return -EINVAL;
438 
439 	*(uint64_t *)extra_args = strtoull(value, NULL, 0);
440 
441 	return 0;
442 }
443 
444 static struct rte_eth_dev *
445 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
446 {
447 	struct rte_eth_dev *eth_dev;
448 	struct rte_eth_dev_data *data;
449 	struct virtio_hw *hw;
450 	struct virtio_user_dev *dev;
451 
452 	eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
453 	if (!eth_dev) {
454 		PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
455 		return NULL;
456 	}
457 
458 	data = eth_dev->data;
459 	hw = eth_dev->data->dev_private;
460 
461 	dev = rte_zmalloc(NULL, sizeof(*dev), 0);
462 	if (!dev) {
463 		PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
464 		rte_eth_dev_release_port(eth_dev);
465 		return NULL;
466 	}
467 
468 	hw->port_id = data->port_id;
469 	dev->port_id = data->port_id;
470 	virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
471 	/*
472 	 * MSIX is required to enable LSC (see virtio_init_device).
473 	 * Here just pretend that we support msix.
474 	 */
475 	hw->use_msix = 1;
476 	hw->modern   = 0;
477 	hw->use_simple_rx = 0;
478 	hw->use_inorder_rx = 0;
479 	hw->use_inorder_tx = 0;
480 	hw->virtio_user_dev = dev;
481 	return eth_dev;
482 }
483 
484 static void
485 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
486 {
487 	struct rte_eth_dev_data *data = eth_dev->data;
488 	struct virtio_hw *hw = data->dev_private;
489 
490 	rte_free(hw->virtio_user_dev);
491 	rte_eth_dev_release_port(eth_dev);
492 }
493 
494 /* Dev initialization routine. Invoked once for each virtio vdev at
495  * EAL init time, see rte_bus_probe().
496  * Returns 0 on success.
497  */
498 static int
499 virtio_user_pmd_probe(struct rte_vdev_device *dev)
500 {
501 	struct rte_kvargs *kvlist = NULL;
502 	struct rte_eth_dev *eth_dev;
503 	struct virtio_hw *hw;
504 	uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
505 	uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
506 	uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
507 	uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
508 	uint64_t mrg_rxbuf = 1;
509 	uint64_t in_order = 1;
510 	uint64_t packed_vq = 0;
511 	char *path = NULL;
512 	char *ifname = NULL;
513 	char *mac_addr = NULL;
514 	int ret = -1;
515 
516 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
517 		const char *name = rte_vdev_device_name(dev);
518 		eth_dev = rte_eth_dev_attach_secondary(name);
519 		if (!eth_dev) {
520 			RTE_LOG(ERR, PMD, "Failed to probe %s\n", name);
521 			return -1;
522 		}
523 
524 		if (eth_virtio_dev_init(eth_dev) < 0) {
525 			PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
526 			rte_eth_dev_release_port(eth_dev);
527 			return -1;
528 		}
529 
530 		eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
531 		eth_dev->device = &dev->device;
532 		rte_eth_dev_probing_finish(eth_dev);
533 		return 0;
534 	}
535 
536 	kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
537 	if (!kvlist) {
538 		PMD_INIT_LOG(ERR, "error when parsing param");
539 		goto end;
540 	}
541 
542 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
543 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
544 				       &get_string_arg, &path) < 0) {
545 			PMD_INIT_LOG(ERR, "error to parse %s",
546 				     VIRTIO_USER_ARG_PATH);
547 			goto end;
548 		}
549 	} else {
550 		PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
551 			     VIRTIO_USER_ARG_PATH);
552 		goto end;
553 	}
554 
555 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
556 		if (is_vhost_user_by_type(path)) {
557 			PMD_INIT_LOG(ERR,
558 				"arg %s applies only to vhost-kernel backend",
559 				VIRTIO_USER_ARG_INTERFACE_NAME);
560 			goto end;
561 		}
562 
563 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
564 				       &get_string_arg, &ifname) < 0) {
565 			PMD_INIT_LOG(ERR, "error to parse %s",
566 				     VIRTIO_USER_ARG_INTERFACE_NAME);
567 			goto end;
568 		}
569 	}
570 
571 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
572 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
573 				       &get_string_arg, &mac_addr) < 0) {
574 			PMD_INIT_LOG(ERR, "error to parse %s",
575 				     VIRTIO_USER_ARG_MAC);
576 			goto end;
577 		}
578 	}
579 
580 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
581 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
582 				       &get_integer_arg, &queue_size) < 0) {
583 			PMD_INIT_LOG(ERR, "error to parse %s",
584 				     VIRTIO_USER_ARG_QUEUE_SIZE);
585 			goto end;
586 		}
587 	}
588 
589 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
590 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
591 				       &get_integer_arg, &queues) < 0) {
592 			PMD_INIT_LOG(ERR, "error to parse %s",
593 				     VIRTIO_USER_ARG_QUEUES_NUM);
594 			goto end;
595 		}
596 	}
597 
598 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
599 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
600 				       &get_integer_arg, &server_mode) < 0) {
601 			PMD_INIT_LOG(ERR, "error to parse %s",
602 				     VIRTIO_USER_ARG_SERVER_MODE);
603 			goto end;
604 		}
605 	}
606 
607 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
608 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
609 				       &get_integer_arg, &cq) < 0) {
610 			PMD_INIT_LOG(ERR, "error to parse %s",
611 				     VIRTIO_USER_ARG_CQ_NUM);
612 			goto end;
613 		}
614 	} else if (queues > 1) {
615 		cq = 1;
616 	}
617 
618 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
619 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
620 				       &get_integer_arg, &packed_vq) < 0) {
621 			PMD_INIT_LOG(ERR, "error to parse %s",
622 				     VIRTIO_USER_ARG_PACKED_VQ);
623 			goto end;
624 		}
625 	}
626 
627 	if (queues > 1 && cq == 0) {
628 		PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
629 		goto end;
630 	}
631 
632 	if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
633 		PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
634 			VIRTIO_USER_ARG_QUEUES_NUM, queues,
635 			VIRTIO_MAX_VIRTQUEUE_PAIRS);
636 		goto end;
637 	}
638 
639 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
640 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
641 				       &get_integer_arg, &mrg_rxbuf) < 0) {
642 			PMD_INIT_LOG(ERR, "error to parse %s",
643 				     VIRTIO_USER_ARG_MRG_RXBUF);
644 			goto end;
645 		}
646 	}
647 
648 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
649 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
650 				       &get_integer_arg, &in_order) < 0) {
651 			PMD_INIT_LOG(ERR, "error to parse %s",
652 				     VIRTIO_USER_ARG_IN_ORDER);
653 			goto end;
654 		}
655 	}
656 
657 	eth_dev = virtio_user_eth_dev_alloc(dev);
658 	if (!eth_dev) {
659 		PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
660 		goto end;
661 	}
662 
663 	hw = eth_dev->data->dev_private;
664 	if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
665 			 queue_size, mac_addr, &ifname, server_mode,
666 			 mrg_rxbuf, in_order, packed_vq) < 0) {
667 		PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
668 		virtio_user_eth_dev_free(eth_dev);
669 		goto end;
670 	}
671 
672 	/* previously called by rte_pci_probe() for physical dev */
673 	if (eth_virtio_dev_init(eth_dev) < 0) {
674 		PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
675 		virtio_user_eth_dev_free(eth_dev);
676 		goto end;
677 	}
678 
679 	rte_eth_dev_probing_finish(eth_dev);
680 	ret = 0;
681 
682 end:
683 	if (kvlist)
684 		rte_kvargs_free(kvlist);
685 	if (path)
686 		free(path);
687 	if (mac_addr)
688 		free(mac_addr);
689 	if (ifname)
690 		free(ifname);
691 	return ret;
692 }
693 
694 static int
695 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
696 {
697 	const char *name;
698 	struct rte_eth_dev *eth_dev;
699 
700 	if (!vdev)
701 		return -EINVAL;
702 
703 	name = rte_vdev_device_name(vdev);
704 	PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
705 	eth_dev = rte_eth_dev_allocated(name);
706 	/* Port has already been released by close. */
707 	if (!eth_dev)
708 		return 0;
709 
710 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
711 		return rte_eth_dev_release_port(eth_dev);
712 
713 	/* make sure the device is stopped, queues freed */
714 	rte_eth_dev_close(eth_dev->data->port_id);
715 
716 	return 0;
717 }
718 
719 static struct rte_vdev_driver virtio_user_driver = {
720 	.probe = virtio_user_pmd_probe,
721 	.remove = virtio_user_pmd_remove,
722 };
723 
724 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
725 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
726 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,
727 	"path=<path> "
728 	"mac=<mac addr> "
729 	"cq=<int> "
730 	"queue_size=<int> "
731 	"queues=<int> "
732 	"iface=<string> "
733 	"server=<0|1> "
734 	"mrg_rxbuf=<0|1> "
735 	"in_order=<0|1> "
736 	"packed_vq=<0|1>");
737