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