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