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