xref: /dpdk/drivers/net/virtio/virtio_user_ethdev.c (revision 7be724856315a0dab9645c20a617fba276607294)
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 	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 && (virtnet_cq_to_vq(hw->cvq) == vq)) {
263 		if (virtio_with_packed_queue(vq->hw))
264 			virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
265 		else
266 			virtio_user_handle_cq(dev, vq->vq_queue_index);
267 		return;
268 	}
269 
270 	if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
271 		PMD_DRV_LOG(ERR, "failed to kick backend: %s",
272 			    strerror(errno));
273 }
274 
275 static int
276 virtio_user_dev_close(struct virtio_hw *hw)
277 {
278 	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
279 
280 	virtio_user_dev_uninit(dev);
281 
282 	return 0;
283 }
284 
285 const struct virtio_ops virtio_user_ops = {
286 	.read_dev_cfg	= virtio_user_read_dev_config,
287 	.write_dev_cfg	= virtio_user_write_dev_config,
288 	.get_status	= virtio_user_get_status,
289 	.set_status	= virtio_user_set_status,
290 	.get_features	= virtio_user_get_features,
291 	.set_features	= virtio_user_set_features,
292 	.features_ok	= virtio_user_features_ok,
293 	.get_isr	= virtio_user_get_isr,
294 	.set_config_irq	= virtio_user_set_config_irq,
295 	.set_queue_irq	= virtio_user_set_queue_irq,
296 	.get_queue_num	= virtio_user_get_queue_num,
297 	.setup_queue	= virtio_user_setup_queue,
298 	.del_queue	= virtio_user_del_queue,
299 	.notify_queue	= virtio_user_notify_queue,
300 	.dev_close	= virtio_user_dev_close,
301 };
302 
303 static const char *valid_args[] = {
304 #define VIRTIO_USER_ARG_QUEUES_NUM     "queues"
305 	VIRTIO_USER_ARG_QUEUES_NUM,
306 #define VIRTIO_USER_ARG_CQ_NUM         "cq"
307 	VIRTIO_USER_ARG_CQ_NUM,
308 #define VIRTIO_USER_ARG_MAC            "mac"
309 	VIRTIO_USER_ARG_MAC,
310 #define VIRTIO_USER_ARG_PATH           "path"
311 	VIRTIO_USER_ARG_PATH,
312 #define VIRTIO_USER_ARG_QUEUE_SIZE     "queue_size"
313 	VIRTIO_USER_ARG_QUEUE_SIZE,
314 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
315 	VIRTIO_USER_ARG_INTERFACE_NAME,
316 #define VIRTIO_USER_ARG_SERVER_MODE    "server"
317 	VIRTIO_USER_ARG_SERVER_MODE,
318 #define VIRTIO_USER_ARG_MRG_RXBUF      "mrg_rxbuf"
319 	VIRTIO_USER_ARG_MRG_RXBUF,
320 #define VIRTIO_USER_ARG_IN_ORDER       "in_order"
321 	VIRTIO_USER_ARG_IN_ORDER,
322 #define VIRTIO_USER_ARG_PACKED_VQ      "packed_vq"
323 	VIRTIO_USER_ARG_PACKED_VQ,
324 #define VIRTIO_USER_ARG_SPEED          "speed"
325 	VIRTIO_USER_ARG_SPEED,
326 #define VIRTIO_USER_ARG_VECTORIZED     "vectorized"
327 	VIRTIO_USER_ARG_VECTORIZED,
328 	NULL
329 };
330 
331 #define VIRTIO_USER_DEF_CQ_EN	0
332 #define VIRTIO_USER_DEF_Q_NUM	1
333 #define VIRTIO_USER_DEF_Q_SZ	256
334 #define VIRTIO_USER_DEF_SERVER_MODE	0
335 
336 static int
337 get_string_arg(const char *key __rte_unused,
338 	       const char *value, void *extra_args)
339 {
340 	if (!value || !extra_args)
341 		return -EINVAL;
342 
343 	*(char **)extra_args = strdup(value);
344 
345 	if (!*(char **)extra_args)
346 		return -ENOMEM;
347 
348 	return 0;
349 }
350 
351 static int
352 get_integer_arg(const char *key __rte_unused,
353 		const char *value, void *extra_args)
354 {
355 	uint64_t integer = 0;
356 	if (!value || !extra_args)
357 		return -EINVAL;
358 	errno = 0;
359 	integer = strtoull(value, NULL, 0);
360 	/* extra_args keeps default value, it should be replaced
361 	 * only in case of successful parsing of the 'value' arg
362 	 */
363 	if (errno == 0)
364 		*(uint64_t *)extra_args = integer;
365 	return -errno;
366 }
367 
368 static uint32_t
369 vdpa_dynamic_major_num(void)
370 {
371 	FILE *fp;
372 	char *line = NULL;
373 	size_t size = 0;
374 	char name[11];
375 	bool found = false;
376 	uint32_t num;
377 
378 	fp = fopen("/proc/devices", "r");
379 	if (fp == NULL) {
380 		PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s",
381 			     strerror(errno));
382 		return UNNAMED_MAJOR;
383 	}
384 
385 	while (getline(&line, &size, fp) > 0) {
386 		char *stripped = line + strspn(line, " ");
387 		if ((sscanf(stripped, "%u %10s", &num, name) == 2) &&
388 		    (strncmp(name, "vhost-vdpa", 10) == 0)) {
389 			found = true;
390 			break;
391 		}
392 	}
393 	free(line);
394 	fclose(fp);
395 	return found ? num : UNNAMED_MAJOR;
396 }
397 
398 static enum virtio_user_backend_type
399 virtio_user_backend_type(const char *path)
400 {
401 	struct stat sb;
402 
403 	if (stat(path, &sb) == -1) {
404 		if (errno == ENOENT)
405 			return VIRTIO_USER_BACKEND_VHOST_USER;
406 
407 		PMD_INIT_LOG(ERR, "Stat fails: %s (%s)", path,
408 			     strerror(errno));
409 		return VIRTIO_USER_BACKEND_UNKNOWN;
410 	}
411 
412 	if (S_ISSOCK(sb.st_mode)) {
413 		return VIRTIO_USER_BACKEND_VHOST_USER;
414 	} else if (S_ISCHR(sb.st_mode)) {
415 		if (major(sb.st_rdev) == MISC_MAJOR)
416 			return VIRTIO_USER_BACKEND_VHOST_KERNEL;
417 		if (major(sb.st_rdev) == vdpa_dynamic_major_num())
418 			return VIRTIO_USER_BACKEND_VHOST_VDPA;
419 	}
420 	return VIRTIO_USER_BACKEND_UNKNOWN;
421 }
422 
423 static struct rte_eth_dev *
424 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
425 {
426 	struct rte_eth_dev *eth_dev;
427 	struct rte_eth_dev_data *data;
428 	struct virtio_hw *hw;
429 	struct virtio_user_dev *dev;
430 
431 	eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*dev));
432 	if (!eth_dev) {
433 		PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
434 		return NULL;
435 	}
436 
437 	data = eth_dev->data;
438 	dev = eth_dev->data->dev_private;
439 	hw = &dev->hw;
440 
441 	hw->port_id = data->port_id;
442 	VIRTIO_OPS(hw) = &virtio_user_ops;
443 
444 	hw->intr_lsc = 1;
445 	hw->use_vec_rx = 0;
446 	hw->use_vec_tx = 0;
447 	hw->use_inorder_rx = 0;
448 	hw->use_inorder_tx = 0;
449 
450 	return eth_dev;
451 }
452 
453 static void
454 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
455 {
456 	rte_eth_dev_release_port(eth_dev);
457 }
458 
459 /* Dev initialization routine. Invoked once for each virtio vdev at
460  * EAL init time, see rte_bus_probe().
461  * Returns 0 on success.
462  */
463 static int
464 virtio_user_pmd_probe(struct rte_vdev_device *vdev)
465 {
466 	struct rte_kvargs *kvlist = NULL;
467 	struct rte_eth_dev *eth_dev;
468 	struct virtio_hw *hw;
469 	struct virtio_user_dev *dev;
470 	enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN;
471 	uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
472 	uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
473 	uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
474 	uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
475 	uint64_t mrg_rxbuf = 1;
476 	uint64_t in_order = 1;
477 	uint64_t packed_vq = 0;
478 	uint64_t vectorized = 0;
479 	char *path = NULL;
480 	char *ifname = NULL;
481 	char *mac_addr = NULL;
482 	int ret = -1;
483 
484 	RTE_BUILD_BUG_ON(offsetof(struct virtio_user_dev, hw) != 0);
485 
486 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
487 		const char *name = rte_vdev_device_name(vdev);
488 		eth_dev = rte_eth_dev_attach_secondary(name);
489 		if (!eth_dev) {
490 			PMD_INIT_LOG(ERR, "Failed to probe %s", name);
491 			return -1;
492 		}
493 
494 		dev = eth_dev->data->dev_private;
495 		hw = &dev->hw;
496 		VIRTIO_OPS(hw) = &virtio_user_ops;
497 
498 		if (eth_virtio_dev_init(eth_dev) < 0) {
499 			PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
500 			rte_eth_dev_release_port(eth_dev);
501 			return -1;
502 		}
503 
504 		eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
505 		eth_dev->device = &vdev->device;
506 		rte_eth_dev_probing_finish(eth_dev);
507 		return 0;
508 	}
509 
510 	kvlist = rte_kvargs_parse(rte_vdev_device_args(vdev), valid_args);
511 	if (!kvlist) {
512 		PMD_INIT_LOG(ERR, "error when parsing param");
513 		goto end;
514 	}
515 
516 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
517 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
518 				       &get_string_arg, &path) < 0) {
519 			PMD_INIT_LOG(ERR, "error to parse %s",
520 				     VIRTIO_USER_ARG_PATH);
521 			goto end;
522 		}
523 	} else {
524 		PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
525 			     VIRTIO_USER_ARG_PATH);
526 		goto end;
527 	}
528 
529 	backend_type = virtio_user_backend_type(path);
530 	if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) {
531 		PMD_INIT_LOG(ERR,
532 			     "unable to determine backend type for path %s",
533 			path);
534 		goto end;
535 	}
536 	PMD_INIT_LOG(INFO, "Backend type detected: %s",
537 		     virtio_user_backend_strings[backend_type]);
538 
539 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
540 		if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) {
541 			PMD_INIT_LOG(ERR,
542 				"arg %s applies only to vhost-kernel backend",
543 				VIRTIO_USER_ARG_INTERFACE_NAME);
544 			goto end;
545 		}
546 
547 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
548 				       &get_string_arg, &ifname) < 0) {
549 			PMD_INIT_LOG(ERR, "error to parse %s",
550 				     VIRTIO_USER_ARG_INTERFACE_NAME);
551 			goto end;
552 		}
553 	}
554 
555 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
556 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
557 				       &get_string_arg, &mac_addr) < 0) {
558 			PMD_INIT_LOG(ERR, "error to parse %s",
559 				     VIRTIO_USER_ARG_MAC);
560 			goto end;
561 		}
562 	}
563 
564 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
565 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
566 				       &get_integer_arg, &queue_size) < 0) {
567 			PMD_INIT_LOG(ERR, "error to parse %s",
568 				     VIRTIO_USER_ARG_QUEUE_SIZE);
569 			goto end;
570 		}
571 	}
572 
573 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
574 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
575 				       &get_integer_arg, &queues) < 0) {
576 			PMD_INIT_LOG(ERR, "error to parse %s",
577 				     VIRTIO_USER_ARG_QUEUES_NUM);
578 			goto end;
579 		}
580 	}
581 
582 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
583 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
584 				       &get_integer_arg, &server_mode) < 0) {
585 			PMD_INIT_LOG(ERR, "error to parse %s",
586 				     VIRTIO_USER_ARG_SERVER_MODE);
587 			goto end;
588 		}
589 	}
590 
591 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
592 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
593 				       &get_integer_arg, &cq) < 0) {
594 			PMD_INIT_LOG(ERR, "error to parse %s",
595 				     VIRTIO_USER_ARG_CQ_NUM);
596 			goto end;
597 		}
598 	}
599 
600 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
601 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
602 				       &get_integer_arg, &packed_vq) < 0) {
603 			PMD_INIT_LOG(ERR, "error to parse %s",
604 				     VIRTIO_USER_ARG_PACKED_VQ);
605 			goto end;
606 		}
607 	}
608 
609 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) {
610 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED,
611 				       &get_integer_arg, &vectorized) < 0) {
612 			PMD_INIT_LOG(ERR, "error to parse %s",
613 				     VIRTIO_USER_ARG_VECTORIZED);
614 			goto end;
615 		}
616 	}
617 
618 	if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
619 		PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
620 			VIRTIO_USER_ARG_QUEUES_NUM, queues,
621 			VIRTIO_MAX_VIRTQUEUE_PAIRS);
622 		goto end;
623 	}
624 
625 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
626 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
627 				       &get_integer_arg, &mrg_rxbuf) < 0) {
628 			PMD_INIT_LOG(ERR, "error to parse %s",
629 				     VIRTIO_USER_ARG_MRG_RXBUF);
630 			goto end;
631 		}
632 	}
633 
634 	if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
635 		if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
636 				       &get_integer_arg, &in_order) < 0) {
637 			PMD_INIT_LOG(ERR, "error to parse %s",
638 				     VIRTIO_USER_ARG_IN_ORDER);
639 			goto end;
640 		}
641 	}
642 
643 	eth_dev = virtio_user_eth_dev_alloc(vdev);
644 	if (!eth_dev) {
645 		PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
646 		goto end;
647 	}
648 
649 	dev = eth_dev->data->dev_private;
650 	hw = &dev->hw;
651 	if (virtio_user_dev_init(dev, path, (uint16_t)queues, cq,
652 			 queue_size, mac_addr, &ifname, server_mode,
653 			 mrg_rxbuf, in_order, packed_vq, backend_type) < 0) {
654 		PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
655 		virtio_user_eth_dev_free(eth_dev);
656 		goto end;
657 	}
658 
659 	/*
660 	 * Virtio-user requires using virtual addresses for the descriptors
661 	 * buffers, whatever other devices require
662 	 */
663 	hw->use_va = true;
664 
665 	/* previously called by pci probing for physical dev */
666 	if (eth_virtio_dev_init(eth_dev) < 0) {
667 		PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
668 		virtio_user_dev_uninit(dev);
669 		virtio_user_eth_dev_free(eth_dev);
670 		goto end;
671 	}
672 
673 	if (vectorized) {
674 		if (packed_vq) {
675 #if defined(CC_AVX512_SUPPORT) || defined(RTE_ARCH_ARM)
676 			hw->use_vec_rx = 1;
677 			hw->use_vec_tx = 1;
678 #else
679 			PMD_INIT_LOG(INFO,
680 				"building environment do not support packed ring vectorized");
681 #endif
682 		} else {
683 			hw->use_vec_rx = 1;
684 		}
685 	}
686 
687 	rte_eth_dev_probing_finish(eth_dev);
688 	ret = 0;
689 
690 end:
691 	rte_kvargs_free(kvlist);
692 	free(path);
693 	free(mac_addr);
694 	free(ifname);
695 	return ret;
696 }
697 
698 static int
699 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
700 {
701 	const char *name;
702 	struct rte_eth_dev *eth_dev;
703 
704 	if (!vdev)
705 		return -EINVAL;
706 
707 	name = rte_vdev_device_name(vdev);
708 	PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
709 	eth_dev = rte_eth_dev_allocated(name);
710 	/* Port has already been released by close. */
711 	if (!eth_dev)
712 		return 0;
713 
714 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
715 		return rte_eth_dev_release_port(eth_dev);
716 
717 	/* make sure the device is stopped, queues freed */
718 	return rte_eth_dev_close(eth_dev->data->port_id);
719 }
720 
721 static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr,
722 		uint64_t iova, size_t len)
723 {
724 	const char *name;
725 	struct rte_eth_dev *eth_dev;
726 	struct virtio_user_dev *dev;
727 
728 	if (!vdev)
729 		return -EINVAL;
730 
731 	name = rte_vdev_device_name(vdev);
732 	eth_dev = rte_eth_dev_allocated(name);
733 	/* Port has already been released by close. */
734 	if (!eth_dev)
735 		return 0;
736 
737 	dev = eth_dev->data->dev_private;
738 
739 	if (dev->ops->dma_map)
740 		return dev->ops->dma_map(dev, addr, iova, len);
741 
742 	return 0;
743 }
744 
745 static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr,
746 		uint64_t iova, size_t len)
747 {
748 	const char *name;
749 	struct rte_eth_dev *eth_dev;
750 	struct virtio_user_dev *dev;
751 
752 	if (!vdev)
753 		return -EINVAL;
754 
755 	name = rte_vdev_device_name(vdev);
756 	eth_dev = rte_eth_dev_allocated(name);
757 	/* Port has already been released by close. */
758 	if (!eth_dev)
759 		return 0;
760 
761 	dev = eth_dev->data->dev_private;
762 
763 	if (dev->ops->dma_unmap)
764 		return dev->ops->dma_unmap(dev, addr, iova, len);
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 	.dma_map = virtio_user_pmd_dma_map,
773 	.dma_unmap = virtio_user_pmd_dma_unmap,
774 };
775 
776 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
777 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
778 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,
779 	"path=<path> "
780 	"mac=<mac addr> "
781 	"cq=<int> "
782 	"queue_size=<int> "
783 	"queues=<int> "
784 	"iface=<string> "
785 	"server=<0|1> "
786 	"mrg_rxbuf=<0|1> "
787 	"in_order=<0|1> "
788 	"packed_vq=<0|1> "
789 	"speed=<int> "
790 	"vectorized=<0|1>");
791