xref: /dpdk/drivers/net/virtio/virtio_user/virtio_user_dev.c (revision 5ecb687a5698d2d8ec1f3b3b5a7a16bceca3e29c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4 
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <sys/mman.h>
11 #include <unistd.h>
12 #include <sys/eventfd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 
16 #include <rte_string_fns.h>
17 #include <rte_eal_memconfig.h>
18 
19 #include "vhost.h"
20 #include "virtio_user_dev.h"
21 #include "../virtio_ethdev.h"
22 
23 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb"
24 
25 static int
26 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
27 {
28 	/* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
29 	 * firstly because vhost depends on this msg to allocate virtqueue
30 	 * pair.
31 	 */
32 	struct vhost_vring_file file;
33 
34 	file.index = queue_sel;
35 	file.fd = dev->callfds[queue_sel];
36 	dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
37 
38 	return 0;
39 }
40 
41 static int
42 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
43 {
44 	struct vhost_vring_file file;
45 	struct vhost_vring_state state;
46 	struct vring *vring = &dev->vrings[queue_sel];
47 	struct vring_packed *pq_vring = &dev->packed_vrings[queue_sel];
48 	struct vhost_vring_addr addr = {
49 		.index = queue_sel,
50 		.log_guest_addr = 0,
51 		.flags = 0, /* disable log */
52 	};
53 
54 	if (dev->features & (1ULL << VIRTIO_F_RING_PACKED)) {
55 		addr.desc_user_addr =
56 			(uint64_t)(uintptr_t)pq_vring->desc;
57 		addr.avail_user_addr =
58 			(uint64_t)(uintptr_t)pq_vring->driver;
59 		addr.used_user_addr =
60 			(uint64_t)(uintptr_t)pq_vring->device;
61 	} else {
62 		addr.desc_user_addr = (uint64_t)(uintptr_t)vring->desc;
63 		addr.avail_user_addr = (uint64_t)(uintptr_t)vring->avail;
64 		addr.used_user_addr = (uint64_t)(uintptr_t)vring->used;
65 	}
66 
67 	state.index = queue_sel;
68 	state.num = vring->num;
69 	dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
70 
71 	state.index = queue_sel;
72 	state.num = 0; /* no reservation */
73 	if (dev->features & (1ULL << VIRTIO_F_RING_PACKED))
74 		state.num |= (1 << 15);
75 	dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
76 
77 	dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
78 
79 	/* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
80 	 * lastly because vhost depends on this msg to judge if
81 	 * virtio is ready.
82 	 */
83 	file.index = queue_sel;
84 	file.fd = dev->kickfds[queue_sel];
85 	dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
86 
87 	return 0;
88 }
89 
90 static int
91 virtio_user_queue_setup(struct virtio_user_dev *dev,
92 			int (*fn)(struct virtio_user_dev *, uint32_t))
93 {
94 	uint32_t i, queue_sel;
95 
96 	for (i = 0; i < dev->max_queue_pairs; ++i) {
97 		queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
98 		if (fn(dev, queue_sel) < 0) {
99 			PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
100 			return -1;
101 		}
102 	}
103 	for (i = 0; i < dev->max_queue_pairs; ++i) {
104 		queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
105 		if (fn(dev, queue_sel) < 0) {
106 			PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
107 			return -1;
108 		}
109 	}
110 
111 	return 0;
112 }
113 
114 int
115 is_vhost_user_by_type(const char *path)
116 {
117 	struct stat sb;
118 
119 	if (stat(path, &sb) == -1)
120 		return 0;
121 
122 	return S_ISSOCK(sb.st_mode);
123 }
124 
125 int
126 virtio_user_start_device(struct virtio_user_dev *dev)
127 {
128 	struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
129 	uint64_t features;
130 	int ret;
131 
132 	/*
133 	 * XXX workaround!
134 	 *
135 	 * We need to make sure that the locks will be
136 	 * taken in the correct order to avoid deadlocks.
137 	 *
138 	 * Before releasing this lock, this thread should
139 	 * not trigger any memory hotplug events.
140 	 *
141 	 * This is a temporary workaround, and should be
142 	 * replaced when we get proper supports from the
143 	 * memory subsystem in the future.
144 	 */
145 	rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
146 	pthread_mutex_lock(&dev->mutex);
147 
148 	if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0)
149 		goto error;
150 
151 	/* Step 0: tell vhost to create queues */
152 	if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
153 		goto error;
154 
155 	/* Step 1: set features */
156 	features = dev->features;
157 	/* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
158 	features &= ~(1ull << VIRTIO_NET_F_MAC);
159 	/* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
160 	features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
161 	features &= ~(1ull << VIRTIO_NET_F_STATUS);
162 	ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
163 	if (ret < 0)
164 		goto error;
165 	PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
166 
167 	/* Step 2: share memory regions */
168 	ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
169 	if (ret < 0)
170 		goto error;
171 
172 	/* Step 3: kick queues */
173 	if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
174 		goto error;
175 
176 	/* Step 4: enable queues
177 	 * we enable the 1st queue pair by default.
178 	 */
179 	dev->ops->enable_qp(dev, 0, 1);
180 
181 	dev->started = true;
182 	pthread_mutex_unlock(&dev->mutex);
183 	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
184 
185 	return 0;
186 error:
187 	pthread_mutex_unlock(&dev->mutex);
188 	rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
189 	/* TODO: free resource here or caller to check */
190 	return -1;
191 }
192 
193 int virtio_user_stop_device(struct virtio_user_dev *dev)
194 {
195 	struct vhost_vring_state state;
196 	uint32_t i;
197 	int error = 0;
198 
199 	pthread_mutex_lock(&dev->mutex);
200 	if (!dev->started)
201 		goto out;
202 
203 	for (i = 0; i < dev->max_queue_pairs; ++i)
204 		dev->ops->enable_qp(dev, i, 0);
205 
206 	/* Stop the backend. */
207 	for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
208 		state.index = i;
209 		if (dev->ops->send_request(dev, VHOST_USER_GET_VRING_BASE,
210 					   &state) < 0) {
211 			PMD_DRV_LOG(ERR, "get_vring_base failed, index=%u\n",
212 				    i);
213 			error = -1;
214 			goto out;
215 		}
216 	}
217 
218 	dev->started = false;
219 out:
220 	pthread_mutex_unlock(&dev->mutex);
221 
222 	return error;
223 }
224 
225 static inline void
226 parse_mac(struct virtio_user_dev *dev, const char *mac)
227 {
228 	int i, r;
229 	uint32_t tmp[ETHER_ADDR_LEN];
230 
231 	if (!mac)
232 		return;
233 
234 	r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
235 			&tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
236 	if (r == ETHER_ADDR_LEN) {
237 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
238 			dev->mac_addr[i] = (uint8_t)tmp[i];
239 		dev->mac_specified = 1;
240 	} else {
241 		/* ignore the wrong mac, use random mac */
242 		PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
243 	}
244 }
245 
246 static int
247 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
248 {
249 	uint32_t i, j;
250 	int callfd;
251 	int kickfd;
252 
253 	for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
254 		if (i >= dev->max_queue_pairs * 2) {
255 			dev->kickfds[i] = -1;
256 			dev->callfds[i] = -1;
257 			continue;
258 		}
259 
260 		/* May use invalid flag, but some backend uses kickfd and
261 		 * callfd as criteria to judge if dev is alive. so finally we
262 		 * use real event_fd.
263 		 */
264 		callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
265 		if (callfd < 0) {
266 			PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
267 			break;
268 		}
269 		kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
270 		if (kickfd < 0) {
271 			PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
272 			break;
273 		}
274 		dev->callfds[i] = callfd;
275 		dev->kickfds[i] = kickfd;
276 	}
277 
278 	if (i < VIRTIO_MAX_VIRTQUEUES) {
279 		for (j = 0; j <= i; ++j) {
280 			close(dev->callfds[j]);
281 			close(dev->kickfds[j]);
282 		}
283 
284 		return -1;
285 	}
286 
287 	return 0;
288 }
289 
290 static int
291 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
292 {
293 	uint32_t i;
294 	struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
295 
296 	if (!eth_dev->intr_handle) {
297 		eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
298 		if (!eth_dev->intr_handle) {
299 			PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
300 			return -1;
301 		}
302 		memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
303 	}
304 
305 	for (i = 0; i < dev->max_queue_pairs; ++i)
306 		eth_dev->intr_handle->efds[i] = dev->callfds[i];
307 	eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
308 	eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
309 	eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
310 	/* For virtio vdev, no need to read counter for clean */
311 	eth_dev->intr_handle->efd_counter_size = 0;
312 	eth_dev->intr_handle->fd = -1;
313 	if (dev->vhostfd >= 0)
314 		eth_dev->intr_handle->fd = dev->vhostfd;
315 	else if (dev->is_server)
316 		eth_dev->intr_handle->fd = dev->listenfd;
317 
318 	return 0;
319 }
320 
321 static void
322 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
323 						 const void *addr __rte_unused,
324 						 size_t len __rte_unused,
325 						 void *arg)
326 {
327 	struct virtio_user_dev *dev = arg;
328 	struct rte_memseg_list *msl;
329 	uint16_t i;
330 
331 	/* ignore externally allocated memory */
332 	msl = rte_mem_virt2memseg_list(addr);
333 	if (msl->external)
334 		return;
335 
336 	pthread_mutex_lock(&dev->mutex);
337 
338 	if (dev->started == false)
339 		goto exit;
340 
341 	/* Step 1: pause the active queues */
342 	for (i = 0; i < dev->queue_pairs; i++)
343 		dev->ops->enable_qp(dev, i, 0);
344 
345 	/* Step 2: update memory regions */
346 	dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
347 
348 	/* Step 3: resume the active queues */
349 	for (i = 0; i < dev->queue_pairs; i++)
350 		dev->ops->enable_qp(dev, i, 1);
351 
352 exit:
353 	pthread_mutex_unlock(&dev->mutex);
354 }
355 
356 static int
357 virtio_user_dev_setup(struct virtio_user_dev *dev)
358 {
359 	uint32_t q;
360 
361 	dev->vhostfd = -1;
362 	dev->vhostfds = NULL;
363 	dev->tapfds = NULL;
364 
365 	if (dev->is_server) {
366 		if (access(dev->path, F_OK) == 0 &&
367 		    !is_vhost_user_by_type(dev->path)) {
368 			PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!");
369 			return -1;
370 		}
371 		dev->ops = &virtio_ops_user;
372 	} else {
373 		if (is_vhost_user_by_type(dev->path)) {
374 			dev->ops = &virtio_ops_user;
375 		} else {
376 			dev->ops = &virtio_ops_kernel;
377 
378 			dev->vhostfds = malloc(dev->max_queue_pairs *
379 					       sizeof(int));
380 			dev->tapfds = malloc(dev->max_queue_pairs *
381 					     sizeof(int));
382 			if (!dev->vhostfds || !dev->tapfds) {
383 				PMD_INIT_LOG(ERR, "Failed to malloc");
384 				return -1;
385 			}
386 
387 			for (q = 0; q < dev->max_queue_pairs; ++q) {
388 				dev->vhostfds[q] = -1;
389 				dev->tapfds[q] = -1;
390 			}
391 		}
392 	}
393 
394 	if (dev->ops->setup(dev) < 0)
395 		return -1;
396 
397 	if (virtio_user_dev_init_notify(dev) < 0)
398 		return -1;
399 
400 	if (virtio_user_fill_intr_handle(dev) < 0)
401 		return -1;
402 
403 	return 0;
404 }
405 
406 /* Use below macro to filter features from vhost backend */
407 #define VIRTIO_USER_SUPPORTED_FEATURES			\
408 	(1ULL << VIRTIO_NET_F_MAC		|	\
409 	 1ULL << VIRTIO_NET_F_STATUS		|	\
410 	 1ULL << VIRTIO_NET_F_MQ		|	\
411 	 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR	|	\
412 	 1ULL << VIRTIO_NET_F_CTRL_VQ		|	\
413 	 1ULL << VIRTIO_NET_F_CTRL_RX		|	\
414 	 1ULL << VIRTIO_NET_F_CTRL_VLAN		|	\
415 	 1ULL << VIRTIO_NET_F_CSUM		|	\
416 	 1ULL << VIRTIO_NET_F_HOST_TSO4		|	\
417 	 1ULL << VIRTIO_NET_F_HOST_TSO6		|	\
418 	 1ULL << VIRTIO_NET_F_MRG_RXBUF		|	\
419 	 1ULL << VIRTIO_RING_F_INDIRECT_DESC	|	\
420 	 1ULL << VIRTIO_NET_F_GUEST_CSUM	|	\
421 	 1ULL << VIRTIO_NET_F_GUEST_TSO4	|	\
422 	 1ULL << VIRTIO_NET_F_GUEST_TSO6	|	\
423 	 1ULL << VIRTIO_F_IN_ORDER		|	\
424 	 1ULL << VIRTIO_F_VERSION_1		|	\
425 	 1ULL << VIRTIO_F_RING_PACKED)
426 
427 int
428 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
429 		     int cq, int queue_size, const char *mac, char **ifname,
430 		     int server, int mrg_rxbuf, int in_order, int packed_vq)
431 {
432 	pthread_mutex_init(&dev->mutex, NULL);
433 	strlcpy(dev->path, path, PATH_MAX);
434 	dev->started = 0;
435 	dev->max_queue_pairs = queues;
436 	dev->queue_pairs = 1; /* mq disabled by default */
437 	dev->queue_size = queue_size;
438 	dev->is_server = server;
439 	dev->mac_specified = 0;
440 	dev->frontend_features = 0;
441 	dev->unsupported_features = ~VIRTIO_USER_SUPPORTED_FEATURES;
442 	parse_mac(dev, mac);
443 
444 	if (*ifname) {
445 		dev->ifname = *ifname;
446 		*ifname = NULL;
447 	}
448 
449 	if (virtio_user_dev_setup(dev) < 0) {
450 		PMD_INIT_LOG(ERR, "backend set up fails");
451 		return -1;
452 	}
453 
454 	if (!dev->is_server) {
455 		if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER,
456 					   NULL) < 0) {
457 			PMD_INIT_LOG(ERR, "set_owner fails: %s",
458 				     strerror(errno));
459 			return -1;
460 		}
461 
462 		if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
463 					   &dev->device_features) < 0) {
464 			PMD_INIT_LOG(ERR, "get_features failed: %s",
465 				     strerror(errno));
466 			return -1;
467 		}
468 	} else {
469 		/* We just pretend vhost-user can support all these features.
470 		 * Note that this could be problematic that if some feature is
471 		 * negotiated but not supported by the vhost-user which comes
472 		 * later.
473 		 */
474 		dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
475 	}
476 
477 	if (!mrg_rxbuf)
478 		dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
479 
480 	if (!in_order)
481 		dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
482 
483 	if (!packed_vq)
484 		dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED);
485 
486 	if (dev->mac_specified)
487 		dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC);
488 	else
489 		dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
490 
491 	if (cq) {
492 		/* device does not really need to know anything about CQ,
493 		 * so if necessary, we just claim to support CQ
494 		 */
495 		dev->frontend_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
496 	} else {
497 		dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
498 		/* Also disable features that depend on VIRTIO_NET_F_CTRL_VQ */
499 		dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
500 		dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
501 		dev->unsupported_features |=
502 			(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
503 		dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
504 		dev->unsupported_features |=
505 			(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
506 	}
507 
508 	/* The backend will not report this feature, we add it explicitly */
509 	if (is_vhost_user_by_type(dev->path))
510 		dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS);
511 
512 	/*
513 	 * Device features =
514 	 *     (frontend_features | backend_features) & ~unsupported_features;
515 	 */
516 	dev->device_features |= dev->frontend_features;
517 	dev->device_features &= ~dev->unsupported_features;
518 
519 	if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
520 				virtio_user_mem_event_cb, dev)) {
521 		if (rte_errno != ENOTSUP) {
522 			PMD_INIT_LOG(ERR, "Failed to register mem event"
523 					" callback\n");
524 			return -1;
525 		}
526 	}
527 
528 	return 0;
529 }
530 
531 void
532 virtio_user_dev_uninit(struct virtio_user_dev *dev)
533 {
534 	uint32_t i;
535 
536 	virtio_user_stop_device(dev);
537 
538 	rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
539 
540 	for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
541 		close(dev->callfds[i]);
542 		close(dev->kickfds[i]);
543 	}
544 
545 	close(dev->vhostfd);
546 
547 	if (dev->is_server && dev->listenfd >= 0) {
548 		close(dev->listenfd);
549 		dev->listenfd = -1;
550 	}
551 
552 	if (dev->vhostfds) {
553 		for (i = 0; i < dev->max_queue_pairs; ++i)
554 			close(dev->vhostfds[i]);
555 		free(dev->vhostfds);
556 		free(dev->tapfds);
557 	}
558 
559 	free(dev->ifname);
560 
561 	if (dev->is_server)
562 		unlink(dev->path);
563 }
564 
565 uint8_t
566 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
567 {
568 	uint16_t i;
569 	uint8_t ret = 0;
570 
571 	if (q_pairs > dev->max_queue_pairs) {
572 		PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
573 			     q_pairs, dev->max_queue_pairs);
574 		return -1;
575 	}
576 
577 	/* Server mode can't enable queue pairs if vhostfd is invalid,
578 	 * always return 0 in this case.
579 	 */
580 	if (!dev->is_server || dev->vhostfd >= 0) {
581 		for (i = 0; i < q_pairs; ++i)
582 			ret |= dev->ops->enable_qp(dev, i, 1);
583 		for (i = q_pairs; i < dev->max_queue_pairs; ++i)
584 			ret |= dev->ops->enable_qp(dev, i, 0);
585 	}
586 	dev->queue_pairs = q_pairs;
587 
588 	return ret;
589 }
590 
591 static uint32_t
592 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
593 			    uint16_t idx_hdr)
594 {
595 	struct virtio_net_ctrl_hdr *hdr;
596 	virtio_net_ctrl_ack status = ~0;
597 	uint16_t i, idx_data, idx_status;
598 	uint32_t n_descs = 0;
599 
600 	/* locate desc for header, data, and status */
601 	idx_data = vring->desc[idx_hdr].next;
602 	n_descs++;
603 
604 	i = idx_data;
605 	while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
606 		i = vring->desc[i].next;
607 		n_descs++;
608 	}
609 
610 	/* locate desc for status */
611 	idx_status = i;
612 	n_descs++;
613 
614 	hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
615 	if (hdr->class == VIRTIO_NET_CTRL_MQ &&
616 	    hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
617 		uint16_t queues;
618 
619 		queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
620 		status = virtio_user_handle_mq(dev, queues);
621 	}
622 
623 	/* Update status */
624 	*(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
625 
626 	return n_descs;
627 }
628 
629 static inline int
630 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
631 {
632 	uint16_t flags = desc->flags;
633 
634 	return wrap_counter == !!(flags & VRING_PACKED_DESC_F_AVAIL) &&
635 		wrap_counter != !!(flags & VRING_PACKED_DESC_F_USED);
636 }
637 
638 static uint32_t
639 virtio_user_handle_ctrl_msg_packed(struct virtio_user_dev *dev,
640 				   struct vring_packed *vring,
641 				   uint16_t idx_hdr)
642 {
643 	struct virtio_net_ctrl_hdr *hdr;
644 	virtio_net_ctrl_ack status = ~0;
645 	uint16_t idx_data, idx_status;
646 	/* initialize to one, header is first */
647 	uint32_t n_descs = 1;
648 
649 	/* locate desc for header, data, and status */
650 	idx_data = idx_hdr + 1;
651 	if (idx_data >= dev->queue_size)
652 		idx_data -= dev->queue_size;
653 
654 	n_descs++;
655 
656 	idx_status = idx_data;
657 	while (vring->desc[idx_status].flags & VRING_DESC_F_NEXT) {
658 		idx_status++;
659 		if (idx_status >= dev->queue_size)
660 			idx_status -= dev->queue_size;
661 		n_descs++;
662 	}
663 
664 	hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
665 	if (hdr->class == VIRTIO_NET_CTRL_MQ &&
666 	    hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
667 		uint16_t queues;
668 
669 		queues = *(uint16_t *)(uintptr_t)
670 				vring->desc[idx_data].addr;
671 		status = virtio_user_handle_mq(dev, queues);
672 	}
673 
674 	/* Update status */
675 	*(virtio_net_ctrl_ack *)(uintptr_t)
676 		vring->desc[idx_status].addr = status;
677 
678 	/* Update used descriptor */
679 	vring->desc[idx_hdr].id = vring->desc[idx_status].id;
680 	vring->desc[idx_hdr].len = sizeof(status);
681 
682 	return n_descs;
683 }
684 
685 void
686 virtio_user_handle_cq_packed(struct virtio_user_dev *dev, uint16_t queue_idx)
687 {
688 	struct virtio_user_queue *vq = &dev->packed_queues[queue_idx];
689 	struct vring_packed *vring = &dev->packed_vrings[queue_idx];
690 	uint16_t n_descs, flags;
691 
692 	while (desc_is_avail(&vring->desc[vq->used_idx],
693 			     vq->used_wrap_counter)) {
694 
695 		n_descs = virtio_user_handle_ctrl_msg_packed(dev, vring,
696 				vq->used_idx);
697 
698 		flags = VRING_DESC_F_WRITE;
699 		if (vq->used_wrap_counter)
700 			flags |= VRING_PACKED_DESC_F_AVAIL_USED;
701 
702 		rte_smp_wmb();
703 		vring->desc[vq->used_idx].flags = flags;
704 
705 		vq->used_idx += n_descs;
706 		if (vq->used_idx >= dev->queue_size) {
707 			vq->used_idx -= dev->queue_size;
708 			vq->used_wrap_counter ^= 1;
709 		}
710 	}
711 }
712 
713 void
714 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
715 {
716 	uint16_t avail_idx, desc_idx;
717 	struct vring_used_elem *uep;
718 	uint32_t n_descs;
719 	struct vring *vring = &dev->vrings[queue_idx];
720 
721 	/* Consume avail ring, using used ring idx as first one */
722 	while (vring->used->idx != vring->avail->idx) {
723 		avail_idx = (vring->used->idx) & (vring->num - 1);
724 		desc_idx = vring->avail->ring[avail_idx];
725 
726 		n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
727 
728 		/* Update used ring */
729 		uep = &vring->used->ring[avail_idx];
730 		uep->id = desc_idx;
731 		uep->len = n_descs;
732 
733 		vring->used->idx++;
734 	}
735 }
736