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