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