xref: /dpdk/lib/vhost/vhost_user.c (revision 7917b0d38e92e8b9ec5a870415b791420e10f11a)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4 
5 /* Security model
6  * --------------
7  * The vhost-user protocol connection is an external interface, so it must be
8  * robust against invalid inputs.
9  *
10  * This is important because the vhost-user frontend is only one step removed
11  * from the guest.  Malicious guests that have escaped will then launch further
12  * attacks from the vhost-user frontend.
13  *
14  * Even in deployments where guests are trusted, a bug in the vhost-user frontend
15  * can still cause invalid messages to be sent.  Such messages must not
16  * compromise the stability of the DPDK application by causing crashes, memory
17  * corruption, or other problematic behavior.
18  *
19  * Do not assume received VhostUserMsg fields contain sensible values!
20  */
21 
22 #include <assert.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/ioctl.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <sys/syscall.h>
33 #ifdef RTE_LIBRTE_VHOST_NUMA
34 #include <numaif.h>
35 #endif
36 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
37 #include <linux/userfaultfd.h>
38 #endif
39 #ifdef F_ADD_SEALS /* if file sealing is supported, so is memfd */
40 #include <linux/memfd.h>
41 #define MEMFD_SUPPORTED
42 #endif
43 
44 #include <rte_common.h>
45 #include <rte_malloc.h>
46 #include <rte_log.h>
47 #include <rte_vfio.h>
48 #include <rte_errno.h>
49 
50 #include "iotlb.h"
51 #include "vhost.h"
52 #include "vhost_user.h"
53 
54 #define VIRTIO_MIN_MTU 68
55 #define VIRTIO_MAX_MTU 65535
56 
57 #define INFLIGHT_ALIGNMENT	64
58 #define INFLIGHT_VERSION	0x1
59 
60 typedef const struct vhost_message_handler {
61 	const char *description;
62 	int (*callback)(struct virtio_net **pdev, struct vhu_msg_context *ctx,
63 		int main_fd);
64 	bool accepts_fd;
65 	bool lock_all_qps;
66 } vhost_message_handler_t;
67 static vhost_message_handler_t vhost_message_handlers[];
68 
69 #define VHOST_MESSAGE_HANDLERS \
70 VHOST_MESSAGE_HANDLER(VHOST_USER_NONE, NULL, false, false) \
71 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_FEATURES, vhost_user_get_features, false, false) \
72 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_FEATURES, vhost_user_set_features, false, true) \
73 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_OWNER, vhost_user_set_owner, false, true) \
74 VHOST_MESSAGE_HANDLER(VHOST_USER_RESET_OWNER, vhost_user_reset_owner, false, false) \
75 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_MEM_TABLE, vhost_user_set_mem_table, true, true) \
76 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_BASE, vhost_user_set_log_base, true, true) \
77 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_LOG_FD, vhost_user_set_log_fd, true, true) \
78 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_NUM, vhost_user_set_vring_num, false, true) \
79 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ADDR, vhost_user_set_vring_addr, false, true) \
80 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_BASE, vhost_user_set_vring_base, false, true) \
81 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_VRING_BASE, vhost_user_get_vring_base, false, false) \
82 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_KICK, vhost_user_set_vring_kick, true, true) \
83 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_CALL, vhost_user_set_vring_call, true, true) \
84 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ERR, vhost_user_set_vring_err, true, true) \
85 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_PROTOCOL_FEATURES, vhost_user_get_protocol_features, \
86 	false, false) \
87 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_PROTOCOL_FEATURES, vhost_user_set_protocol_features, \
88 	false, true) \
89 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_QUEUE_NUM, vhost_user_get_queue_num, false, false) \
90 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_VRING_ENABLE, vhost_user_set_vring_enable, false, true) \
91 VHOST_MESSAGE_HANDLER(VHOST_USER_SEND_RARP, vhost_user_send_rarp, false, true) \
92 VHOST_MESSAGE_HANDLER(VHOST_USER_NET_SET_MTU, vhost_user_net_set_mtu, false, true) \
93 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_BACKEND_REQ_FD, vhost_user_set_req_fd, true, true) \
94 VHOST_MESSAGE_HANDLER(VHOST_USER_IOTLB_MSG, vhost_user_iotlb_msg, false, false) \
95 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_CONFIG, vhost_user_get_config, false, false) \
96 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_CONFIG, vhost_user_set_config, false, false) \
97 VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_ADVISE, vhost_user_set_postcopy_advise, false, false) \
98 VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_LISTEN, vhost_user_set_postcopy_listen, false, false) \
99 VHOST_MESSAGE_HANDLER(VHOST_USER_POSTCOPY_END, vhost_user_postcopy_end, false, false) \
100 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_INFLIGHT_FD, vhost_user_get_inflight_fd, false, false) \
101 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_INFLIGHT_FD, vhost_user_set_inflight_fd, true, false) \
102 VHOST_MESSAGE_HANDLER(VHOST_USER_SET_STATUS, vhost_user_set_status, false, false) \
103 VHOST_MESSAGE_HANDLER(VHOST_USER_GET_STATUS, vhost_user_get_status, false, false)
104 
105 #define VHOST_MESSAGE_HANDLER(id, handler, accepts_fd, lock_all_qps) \
106 	id ## _LOCK_ALL_QPS = lock_all_qps,
107 enum {
108 	VHOST_MESSAGE_HANDLERS
109 };
110 #undef VHOST_MESSAGE_HANDLER
111 
112 /* vhost_user_msg_handler() locks all qps based on a handler's lock_all_qps.
113  * Later, a handler may need to ensure the vq has been locked (for example,
114  * when calling lock annotated helpers).
115  *
116  * Note: unfortunately, static_assert() does not see an array content as a
117  * constant expression. Because of this, we can't simply check for
118  * vhost_user_msg_handler[].lock_all_qps.
119  * Instead, define an enum for each handler.
120  */
121 #define VHOST_USER_ASSERT_LOCK(dev, vq, id) do { \
122 	static_assert(id ## _LOCK_ALL_QPS == true, \
123 		#id " handler is not declared as locking all queue pairs"); \
124 	vq_assert_lock(dev, vq); \
125 } while (0)
126 
127 static int send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx);
128 static int read_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx);
129 
130 static void
131 close_msg_fds(struct vhu_msg_context *ctx)
132 {
133 	int i;
134 
135 	for (i = 0; i < ctx->fd_num; i++) {
136 		int fd = ctx->fds[i];
137 
138 		if (fd == -1)
139 			continue;
140 
141 		ctx->fds[i] = -1;
142 		close(fd);
143 	}
144 }
145 
146 /*
147  * Ensure the expected number of FDs is received,
148  * close all FDs and return an error if this is not the case.
149  */
150 static int
151 validate_msg_fds(struct virtio_net *dev, struct vhu_msg_context *ctx, int expected_fds)
152 {
153 	if (ctx->fd_num == expected_fds)
154 		return 0;
155 
156 	VHOST_CONFIG_LOG(dev->ifname, ERR,
157 		"expect %d FDs for request %s, received %d",
158 		expected_fds, vhost_message_handlers[ctx->msg.request.frontend].description,
159 		ctx->fd_num);
160 
161 	close_msg_fds(ctx);
162 
163 	return -1;
164 }
165 
166 static uint64_t
167 get_blk_size(int fd)
168 {
169 	struct stat stat;
170 	int ret;
171 
172 	ret = fstat(fd, &stat);
173 	return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
174 }
175 
176 static void
177 async_dma_map(struct virtio_net *dev, bool do_map)
178 {
179 	int ret = 0;
180 	uint32_t i;
181 	struct guest_page *page;
182 
183 	if (do_map) {
184 		for (i = 0; i < dev->nr_guest_pages; i++) {
185 			page = &dev->guest_pages[i];
186 			ret = rte_vfio_container_dma_map(RTE_VFIO_DEFAULT_CONTAINER_FD,
187 							 page->host_user_addr,
188 							 page->host_iova,
189 							 page->size);
190 			if (ret) {
191 				/*
192 				 * DMA device may bind with kernel driver, in this case,
193 				 * we don't need to program IOMMU manually. However, if no
194 				 * device is bound with vfio/uio in DPDK, and vfio kernel
195 				 * module is loaded, the API will still be called and return
196 				 * with ENODEV.
197 				 *
198 				 * DPDK vfio only returns ENODEV in very similar situations
199 				 * (vfio either unsupported, or supported but no devices found).
200 				 * Either way, no mappings could be performed. We treat it as
201 				 * normal case in async path. This is a workaround.
202 				 */
203 				if (rte_errno == ENODEV)
204 					return;
205 
206 				/* DMA mapping errors won't stop VHOST_USER_SET_MEM_TABLE. */
207 				VHOST_CONFIG_LOG(dev->ifname, ERR, "DMA engine map failed");
208 			}
209 		}
210 
211 	} else {
212 		for (i = 0; i < dev->nr_guest_pages; i++) {
213 			page = &dev->guest_pages[i];
214 			ret = rte_vfio_container_dma_unmap(RTE_VFIO_DEFAULT_CONTAINER_FD,
215 							   page->host_user_addr,
216 							   page->host_iova,
217 							   page->size);
218 			if (ret) {
219 				/* like DMA map, ignore the kernel driver case when unmap. */
220 				if (rte_errno == EINVAL)
221 					return;
222 
223 				VHOST_CONFIG_LOG(dev->ifname, ERR, "DMA engine unmap failed");
224 			}
225 		}
226 	}
227 }
228 
229 static void
230 free_mem_region(struct virtio_net *dev)
231 {
232 	uint32_t i;
233 	struct rte_vhost_mem_region *reg;
234 
235 	if (!dev || !dev->mem)
236 		return;
237 
238 	if (dev->async_copy && rte_vfio_is_enabled("vfio"))
239 		async_dma_map(dev, false);
240 
241 	for (i = 0; i < dev->mem->nregions; i++) {
242 		reg = &dev->mem->regions[i];
243 		if (reg->host_user_addr) {
244 			munmap(reg->mmap_addr, reg->mmap_size);
245 			close(reg->fd);
246 		}
247 	}
248 }
249 
250 void
251 vhost_backend_cleanup(struct virtio_net *dev)
252 {
253 	struct rte_vdpa_device *vdpa_dev;
254 
255 	vdpa_dev = dev->vdpa_dev;
256 	if (vdpa_dev && vdpa_dev->ops->dev_cleanup != NULL)
257 		vdpa_dev->ops->dev_cleanup(dev->vid);
258 
259 	if (dev->mem) {
260 		free_mem_region(dev);
261 		rte_free(dev->mem);
262 		dev->mem = NULL;
263 	}
264 
265 	rte_free(dev->guest_pages);
266 	dev->guest_pages = NULL;
267 
268 	if (dev->log_addr) {
269 		munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
270 		dev->log_addr = 0;
271 	}
272 
273 	if (dev->inflight_info) {
274 		if (dev->inflight_info->addr) {
275 			munmap(dev->inflight_info->addr,
276 			       dev->inflight_info->size);
277 			dev->inflight_info->addr = NULL;
278 		}
279 
280 		if (dev->inflight_info->fd >= 0) {
281 			close(dev->inflight_info->fd);
282 			dev->inflight_info->fd = -1;
283 		}
284 
285 		rte_free(dev->inflight_info);
286 		dev->inflight_info = NULL;
287 	}
288 
289 	if (dev->backend_req_fd >= 0) {
290 		close(dev->backend_req_fd);
291 		dev->backend_req_fd = -1;
292 	}
293 
294 	if (dev->postcopy_ufd >= 0) {
295 		close(dev->postcopy_ufd);
296 		dev->postcopy_ufd = -1;
297 	}
298 
299 	dev->postcopy_listening = 0;
300 
301 	vhost_user_iotlb_destroy(dev);
302 }
303 
304 static void
305 vhost_user_notify_queue_state(struct virtio_net *dev, struct vhost_virtqueue *vq,
306 	int enable)
307 {
308 	struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
309 
310 	/* Configure guest notifications on enable */
311 	if (enable && vq->notif_enable != VIRTIO_UNINITIALIZED_NOTIF)
312 		vhost_enable_guest_notification(dev, vq, vq->notif_enable);
313 
314 	if (vdpa_dev && vdpa_dev->ops->set_vring_state)
315 		vdpa_dev->ops->set_vring_state(dev->vid, vq->index, enable);
316 
317 	if (dev->notify_ops->vring_state_changed)
318 		dev->notify_ops->vring_state_changed(dev->vid, vq->index, enable);
319 }
320 
321 /*
322  * This function just returns success at the moment unless
323  * the device hasn't been initialised.
324  */
325 static int
326 vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
327 			struct vhu_msg_context *ctx __rte_unused,
328 			int main_fd __rte_unused)
329 {
330 	return RTE_VHOST_MSG_RESULT_OK;
331 }
332 
333 static int
334 vhost_user_reset_owner(struct virtio_net **pdev,
335 			struct vhu_msg_context *ctx __rte_unused,
336 			int main_fd __rte_unused)
337 {
338 	struct virtio_net *dev = *pdev;
339 
340 	vhost_destroy_device_notify(dev);
341 
342 	cleanup_device(dev, 0);
343 	reset_device(dev);
344 	return RTE_VHOST_MSG_RESULT_OK;
345 }
346 
347 /*
348  * The features that we support are requested.
349  */
350 static int
351 vhost_user_get_features(struct virtio_net **pdev,
352 			struct vhu_msg_context *ctx,
353 			int main_fd __rte_unused)
354 {
355 	struct virtio_net *dev = *pdev;
356 	uint64_t features = 0;
357 
358 	rte_vhost_driver_get_features(dev->ifname, &features);
359 
360 	ctx->msg.payload.u64 = features;
361 	ctx->msg.size = sizeof(ctx->msg.payload.u64);
362 	ctx->fd_num = 0;
363 
364 	return RTE_VHOST_MSG_RESULT_REPLY;
365 }
366 
367 /*
368  * The queue number that we support are requested.
369  */
370 static int
371 vhost_user_get_queue_num(struct virtio_net **pdev,
372 			struct vhu_msg_context *ctx,
373 			int main_fd __rte_unused)
374 {
375 	struct virtio_net *dev = *pdev;
376 	uint32_t queue_num = 0;
377 
378 	rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
379 
380 	ctx->msg.payload.u64 = (uint64_t)queue_num;
381 	ctx->msg.size = sizeof(ctx->msg.payload.u64);
382 	ctx->fd_num = 0;
383 
384 	return RTE_VHOST_MSG_RESULT_REPLY;
385 }
386 
387 /*
388  * We receive the negotiated features supported by us and the virtio device.
389  */
390 static int
391 vhost_user_set_features(struct virtio_net **pdev,
392 			struct vhu_msg_context *ctx,
393 			int main_fd __rte_unused)
394 {
395 	struct virtio_net *dev = *pdev;
396 	uint64_t features = ctx->msg.payload.u64;
397 	uint64_t vhost_features = 0;
398 	struct rte_vdpa_device *vdpa_dev;
399 
400 	rte_vhost_driver_get_features(dev->ifname, &vhost_features);
401 	if (features & ~vhost_features) {
402 		VHOST_CONFIG_LOG(dev->ifname, ERR, "received invalid negotiated features.");
403 		dev->flags |= VIRTIO_DEV_FEATURES_FAILED;
404 		dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
405 
406 		return RTE_VHOST_MSG_RESULT_ERR;
407 	}
408 
409 	if (dev->flags & VIRTIO_DEV_RUNNING) {
410 		if (dev->features == features)
411 			return RTE_VHOST_MSG_RESULT_OK;
412 
413 		/*
414 		 * Error out if frontend tries to change features while device is
415 		 * in running state. The exception being VHOST_F_LOG_ALL, which
416 		 * is enabled when the live-migration starts.
417 		 */
418 		if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
419 			VHOST_CONFIG_LOG(dev->ifname, ERR,
420 				"features changed while device is running.");
421 			return RTE_VHOST_MSG_RESULT_ERR;
422 		}
423 
424 		if (dev->notify_ops->features_changed)
425 			dev->notify_ops->features_changed(dev->vid, features);
426 	}
427 
428 	dev->features = features;
429 	if (dev->features &
430 		((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
431 		 (1ULL << VIRTIO_F_VERSION_1) |
432 		 (1ULL << VIRTIO_F_RING_PACKED))) {
433 		dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
434 	} else {
435 		dev->vhost_hlen = sizeof(struct virtio_net_hdr);
436 	}
437 	VHOST_CONFIG_LOG(dev->ifname, INFO,
438 		"negotiated Virtio features: 0x%" PRIx64,
439 		dev->features);
440 	VHOST_CONFIG_LOG(dev->ifname, DEBUG,
441 		"mergeable RX buffers %s, virtio 1 %s",
442 		(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
443 		(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
444 
445 	if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) &&
446 	    !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
447 		/*
448 		 * Remove all but first queue pair if MQ hasn't been
449 		 * negotiated. This is safe because the device is not
450 		 * running at this stage.
451 		 */
452 		while (dev->nr_vring > 2) {
453 			struct vhost_virtqueue *vq;
454 
455 			vq = dev->virtqueue[--dev->nr_vring];
456 			if (!vq)
457 				continue;
458 
459 			dev->virtqueue[dev->nr_vring] = NULL;
460 			cleanup_vq(vq, 1);
461 			cleanup_vq_inflight(dev, vq);
462 			/* vhost_user_lock_all_queue_pairs locked all qps */
463 			VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_FEATURES);
464 			rte_rwlock_write_unlock(&vq->access_lock);
465 			free_vq(dev, vq);
466 		}
467 	}
468 
469 	vdpa_dev = dev->vdpa_dev;
470 	if (vdpa_dev)
471 		vdpa_dev->ops->set_features(dev->vid);
472 
473 	dev->flags &= ~VIRTIO_DEV_FEATURES_FAILED;
474 	return RTE_VHOST_MSG_RESULT_OK;
475 }
476 
477 /*
478  * The virtio device sends us the size of the descriptor ring.
479  */
480 static int
481 vhost_user_set_vring_num(struct virtio_net **pdev,
482 			struct vhu_msg_context *ctx,
483 			int main_fd __rte_unused)
484 {
485 	struct virtio_net *dev = *pdev;
486 	struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
487 
488 	if (ctx->msg.payload.state.num > 32768) {
489 		VHOST_CONFIG_LOG(dev->ifname, ERR,
490 			"invalid virtqueue size %u",
491 			ctx->msg.payload.state.num);
492 		return RTE_VHOST_MSG_RESULT_ERR;
493 	}
494 
495 	vq->size = ctx->msg.payload.state.num;
496 
497 	/* VIRTIO 1.0, 2.4 Virtqueues says:
498 	 *
499 	 *   Queue Size value is always a power of 2. The maximum Queue Size
500 	 *   value is 32768.
501 	 *
502 	 * VIRTIO 1.1 2.7 Virtqueues says:
503 	 *
504 	 *   Packed virtqueues support up to 2^15 entries each.
505 	 */
506 	if (!vq_is_packed(dev)) {
507 		if (vq->size & (vq->size - 1)) {
508 			VHOST_CONFIG_LOG(dev->ifname, ERR,
509 				"invalid virtqueue size %u",
510 				vq->size);
511 			return RTE_VHOST_MSG_RESULT_ERR;
512 		}
513 	}
514 
515 	if (vq_is_packed(dev)) {
516 		rte_free(vq->shadow_used_packed);
517 		vq->shadow_used_packed = rte_malloc_socket(NULL,
518 				vq->size *
519 				sizeof(struct vring_used_elem_packed),
520 				RTE_CACHE_LINE_SIZE, vq->numa_node);
521 		if (!vq->shadow_used_packed) {
522 			VHOST_CONFIG_LOG(dev->ifname, ERR,
523 				"failed to allocate memory for shadow used ring.");
524 			return RTE_VHOST_MSG_RESULT_ERR;
525 		}
526 
527 	} else {
528 		rte_free(vq->shadow_used_split);
529 
530 		vq->shadow_used_split = rte_malloc_socket(NULL,
531 				vq->size * sizeof(struct vring_used_elem),
532 				RTE_CACHE_LINE_SIZE, vq->numa_node);
533 
534 		if (!vq->shadow_used_split) {
535 			VHOST_CONFIG_LOG(dev->ifname, ERR,
536 				"failed to allocate memory for vq internal data.");
537 			return RTE_VHOST_MSG_RESULT_ERR;
538 		}
539 	}
540 
541 	rte_free(vq->batch_copy_elems);
542 	vq->batch_copy_elems = rte_malloc_socket(NULL,
543 				vq->size * sizeof(struct batch_copy_elem),
544 				RTE_CACHE_LINE_SIZE, vq->numa_node);
545 	if (!vq->batch_copy_elems) {
546 		VHOST_CONFIG_LOG(dev->ifname, ERR,
547 			"failed to allocate memory for batching copy.");
548 		return RTE_VHOST_MSG_RESULT_ERR;
549 	}
550 
551 	return RTE_VHOST_MSG_RESULT_OK;
552 }
553 
554 /*
555  * Reallocate virtio_dev, vhost_virtqueue and related data structures to
556  * make them on the same numa node as the memory of vring descriptor.
557  */
558 #ifdef RTE_LIBRTE_VHOST_NUMA
559 static void
560 numa_realloc(struct virtio_net **pdev, struct vhost_virtqueue **pvq)
561 {
562 	int node, dev_node;
563 	struct virtio_net *dev;
564 	struct vhost_virtqueue *vq;
565 	struct batch_copy_elem *bce;
566 	struct guest_page *gp;
567 	struct rte_vhost_memory *mem;
568 	size_t mem_size;
569 	int ret;
570 
571 	dev = *pdev;
572 	vq = *pvq;
573 
574 	/*
575 	 * If VQ is ready, it is too late to reallocate, it certainly already
576 	 * happened anyway on VHOST_USER_SET_VRING_ADRR.
577 	 */
578 	if (vq->ready)
579 		return;
580 
581 	ret = get_mempolicy(&node, NULL, 0, vq->desc, MPOL_F_NODE | MPOL_F_ADDR);
582 	if (ret) {
583 		VHOST_CONFIG_LOG(dev->ifname, ERR,
584 			"unable to get virtqueue %d numa information.",
585 			vq->index);
586 		return;
587 	}
588 
589 	if (node == vq->numa_node)
590 		goto out_dev_realloc;
591 
592 	vq = rte_realloc_socket(*pvq, sizeof(**pvq), 0, node);
593 	if (!vq) {
594 		VHOST_CONFIG_LOG(dev->ifname, ERR,
595 			"failed to realloc virtqueue %d on node %d",
596 			(*pvq)->index, node);
597 		return;
598 	}
599 	*pvq = vq;
600 
601 	if (vq != dev->virtqueue[vq->index]) {
602 		VHOST_CONFIG_LOG(dev->ifname, INFO, "reallocated virtqueue on node %d", node);
603 		dev->virtqueue[vq->index] = vq;
604 	}
605 
606 	if (vq_is_packed(dev)) {
607 		struct vring_used_elem_packed *sup;
608 
609 		sup = rte_realloc_socket(vq->shadow_used_packed, vq->size * sizeof(*sup),
610 				RTE_CACHE_LINE_SIZE, node);
611 		if (!sup) {
612 			VHOST_CONFIG_LOG(dev->ifname, ERR,
613 				"failed to realloc shadow packed on node %d",
614 				node);
615 			return;
616 		}
617 		vq->shadow_used_packed = sup;
618 	} else {
619 		struct vring_used_elem *sus;
620 
621 		sus = rte_realloc_socket(vq->shadow_used_split, vq->size * sizeof(*sus),
622 				RTE_CACHE_LINE_SIZE, node);
623 		if (!sus) {
624 			VHOST_CONFIG_LOG(dev->ifname, ERR,
625 				"failed to realloc shadow split on node %d",
626 				node);
627 			return;
628 		}
629 		vq->shadow_used_split = sus;
630 	}
631 
632 	bce = rte_realloc_socket(vq->batch_copy_elems, vq->size * sizeof(*bce),
633 			RTE_CACHE_LINE_SIZE, node);
634 	if (!bce) {
635 		VHOST_CONFIG_LOG(dev->ifname, ERR,
636 			"failed to realloc batch copy elem on node %d",
637 			node);
638 		return;
639 	}
640 	vq->batch_copy_elems = bce;
641 
642 	if (vq->log_cache) {
643 		struct log_cache_entry *lc;
644 
645 		lc = rte_realloc_socket(vq->log_cache, sizeof(*lc) * VHOST_LOG_CACHE_NR, 0, node);
646 		if (!lc) {
647 			VHOST_CONFIG_LOG(dev->ifname, ERR,
648 				"failed to realloc log cache on node %d",
649 				node);
650 			return;
651 		}
652 		vq->log_cache = lc;
653 	}
654 
655 	if (vq->resubmit_inflight) {
656 		struct rte_vhost_resubmit_info *ri;
657 
658 		ri = rte_realloc_socket(vq->resubmit_inflight, sizeof(*ri), 0, node);
659 		if (!ri) {
660 			VHOST_CONFIG_LOG(dev->ifname, ERR,
661 				"failed to realloc resubmit inflight on node %d",
662 				node);
663 			return;
664 		}
665 		vq->resubmit_inflight = ri;
666 
667 		if (ri->resubmit_list) {
668 			struct rte_vhost_resubmit_desc *rd;
669 
670 			rd = rte_realloc_socket(ri->resubmit_list, sizeof(*rd) * ri->resubmit_num,
671 					0, node);
672 			if (!rd) {
673 				VHOST_CONFIG_LOG(dev->ifname, ERR,
674 					"failed to realloc resubmit list on node %d",
675 					node);
676 				return;
677 			}
678 			ri->resubmit_list = rd;
679 		}
680 	}
681 
682 	vq->numa_node = node;
683 
684 out_dev_realloc:
685 
686 	if (dev->flags & VIRTIO_DEV_RUNNING)
687 		return;
688 
689 	ret = get_mempolicy(&dev_node, NULL, 0, dev, MPOL_F_NODE | MPOL_F_ADDR);
690 	if (ret) {
691 		VHOST_CONFIG_LOG(dev->ifname, ERR, "unable to get numa information.");
692 		return;
693 	}
694 
695 	if (dev_node == node)
696 		return;
697 
698 	dev = rte_realloc_socket(*pdev, sizeof(**pdev), 0, node);
699 	if (!dev) {
700 		VHOST_CONFIG_LOG((*pdev)->ifname, ERR, "failed to realloc dev on node %d", node);
701 		return;
702 	}
703 	*pdev = dev;
704 
705 	VHOST_CONFIG_LOG(dev->ifname, INFO, "reallocated device on node %d", node);
706 	vhost_devices[dev->vid] = dev;
707 
708 	mem_size = sizeof(struct rte_vhost_memory) +
709 		sizeof(struct rte_vhost_mem_region) * dev->mem->nregions;
710 	mem = rte_realloc_socket(dev->mem, mem_size, 0, node);
711 	if (!mem) {
712 		VHOST_CONFIG_LOG(dev->ifname, ERR,
713 			"failed to realloc mem table on node %d",
714 			node);
715 		return;
716 	}
717 	dev->mem = mem;
718 
719 	gp = rte_realloc_socket(dev->guest_pages, dev->max_guest_pages * sizeof(*gp),
720 			RTE_CACHE_LINE_SIZE, node);
721 	if (!gp) {
722 		VHOST_CONFIG_LOG(dev->ifname, ERR,
723 			"failed to realloc guest pages on node %d",
724 			node);
725 		return;
726 	}
727 	dev->guest_pages = gp;
728 
729 	vhost_user_iotlb_init(dev);
730 }
731 #else
732 static void
733 numa_realloc(struct virtio_net **pdev, struct vhost_virtqueue **pvq)
734 {
735 	RTE_SET_USED(pdev);
736 	RTE_SET_USED(pvq);
737 }
738 #endif
739 
740 /* Converts QEMU virtual address to Vhost virtual address. */
741 static uint64_t
742 qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
743 {
744 	struct rte_vhost_mem_region *r;
745 	uint32_t i;
746 
747 	if (unlikely(!dev || !dev->mem))
748 		goto out_error;
749 
750 	/* Find the region where the address lives. */
751 	for (i = 0; i < dev->mem->nregions; i++) {
752 		r = &dev->mem->regions[i];
753 
754 		if (qva >= r->guest_user_addr &&
755 		    qva <  r->guest_user_addr + r->size) {
756 
757 			if (unlikely(*len > r->guest_user_addr + r->size - qva))
758 				*len = r->guest_user_addr + r->size - qva;
759 
760 			return qva - r->guest_user_addr +
761 			       r->host_user_addr;
762 		}
763 	}
764 out_error:
765 	*len = 0;
766 
767 	return 0;
768 }
769 
770 
771 /*
772  * Converts ring address to Vhost virtual address.
773  * If IOMMU is enabled, the ring address is a guest IO virtual address,
774  * else it is a QEMU virtual address.
775  */
776 static uint64_t
777 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
778 		uint64_t ra, uint64_t *size)
779 {
780 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
781 		uint64_t vva;
782 
783 		vhost_user_iotlb_rd_lock(vq);
784 		vva = vhost_iova_to_vva(dev, vq, ra,
785 					size, VHOST_ACCESS_RW);
786 		vhost_user_iotlb_rd_unlock(vq);
787 
788 		return vva;
789 	}
790 
791 	return qva_to_vva(dev, ra, size);
792 }
793 
794 static uint64_t
795 log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq)
796 {
797 	uint64_t log_gpa;
798 
799 	vhost_user_iotlb_rd_lock(vq);
800 	log_gpa = translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr);
801 	vhost_user_iotlb_rd_unlock(vq);
802 
803 	return log_gpa;
804 }
805 
806 static uint64_t
807 hua_to_alignment(struct rte_vhost_memory *mem, void *ptr)
808 {
809 	struct rte_vhost_mem_region *r;
810 	uint32_t i;
811 	uintptr_t hua = (uintptr_t)ptr;
812 
813 	for (i = 0; i < mem->nregions; i++) {
814 		r = &mem->regions[i];
815 		if (hua >= r->host_user_addr &&
816 			hua < r->host_user_addr + r->size) {
817 			return get_blk_size(r->fd);
818 		}
819 	}
820 
821 	/* If region isn't found, don't align at all */
822 	return 1;
823 }
824 
825 void
826 mem_set_dump(struct virtio_net *dev, void *ptr, size_t size, bool enable, uint64_t pagesz)
827 {
828 #ifdef MADV_DONTDUMP
829 	void *start = RTE_PTR_ALIGN_FLOOR(ptr, pagesz);
830 	uintptr_t end = RTE_ALIGN_CEIL((uintptr_t)ptr + size, pagesz);
831 	size_t len = end - (uintptr_t)start;
832 
833 	if (madvise(start, len, enable ? MADV_DODUMP : MADV_DONTDUMP) == -1) {
834 		VHOST_CONFIG_LOG(dev->ifname, INFO,
835 			"could not set coredump preference (%s).", strerror(errno));
836 	}
837 #endif
838 }
839 
840 static void
841 translate_ring_addresses(struct virtio_net **pdev, struct vhost_virtqueue **pvq)
842 {
843 	struct vhost_virtqueue *vq;
844 	struct virtio_net *dev;
845 	uint64_t len, expected_len;
846 
847 	dev = *pdev;
848 	vq = *pvq;
849 
850 	vq_assert_lock(dev, vq);
851 
852 	if (vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG)) {
853 		vq->log_guest_addr =
854 			log_addr_to_gpa(dev, vq);
855 		if (vq->log_guest_addr == 0) {
856 			VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map log_guest_addr.");
857 			return;
858 		}
859 	}
860 
861 	if (vq_is_packed(dev)) {
862 		len = sizeof(struct vring_packed_desc) * vq->size;
863 		vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
864 			ring_addr_to_vva(dev, vq, vq->ring_addrs.desc_user_addr, &len);
865 		if (vq->desc_packed == NULL ||
866 				len != sizeof(struct vring_packed_desc) *
867 				vq->size) {
868 			VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map desc_packed ring.");
869 			return;
870 		}
871 
872 		mem_set_dump(dev, vq->desc_packed, len, true,
873 			hua_to_alignment(dev->mem, vq->desc_packed));
874 		numa_realloc(&dev, &vq);
875 		*pdev = dev;
876 		*pvq = vq;
877 
878 		len = sizeof(struct vring_packed_desc_event);
879 		vq->driver_event = (struct vring_packed_desc_event *)
880 					(uintptr_t)ring_addr_to_vva(dev,
881 					vq, vq->ring_addrs.avail_user_addr, &len);
882 		if (vq->driver_event == NULL ||
883 				len != sizeof(struct vring_packed_desc_event)) {
884 			VHOST_CONFIG_LOG(dev->ifname, DEBUG,
885 				"failed to find driver area address.");
886 			return;
887 		}
888 
889 		mem_set_dump(dev, vq->driver_event, len, true,
890 			hua_to_alignment(dev->mem, vq->driver_event));
891 		len = sizeof(struct vring_packed_desc_event);
892 		vq->device_event = (struct vring_packed_desc_event *)
893 					(uintptr_t)ring_addr_to_vva(dev,
894 					vq, vq->ring_addrs.used_user_addr, &len);
895 		if (vq->device_event == NULL ||
896 				len != sizeof(struct vring_packed_desc_event)) {
897 			VHOST_CONFIG_LOG(dev->ifname, DEBUG,
898 				"failed to find device area address.");
899 			return;
900 		}
901 
902 		mem_set_dump(dev, vq->device_event, len, true,
903 			hua_to_alignment(dev->mem, vq->device_event));
904 		vq->access_ok = true;
905 		return;
906 	}
907 
908 	/* The addresses are converted from QEMU virtual to Vhost virtual. */
909 	if (vq->desc && vq->avail && vq->used)
910 		return;
911 
912 	len = sizeof(struct vring_desc) * vq->size;
913 	vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
914 			vq, vq->ring_addrs.desc_user_addr, &len);
915 	if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
916 		VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map desc ring.");
917 		return;
918 	}
919 
920 	mem_set_dump(dev, vq->desc, len, true, hua_to_alignment(dev->mem, vq->desc));
921 	numa_realloc(&dev, &vq);
922 	*pdev = dev;
923 	*pvq = vq;
924 
925 	len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
926 	if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
927 		len += sizeof(uint16_t);
928 	expected_len = len;
929 	vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
930 			vq, vq->ring_addrs.avail_user_addr, &len);
931 	if (vq->avail == 0 || len != expected_len) {
932 		VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map avail ring.");
933 		return;
934 	}
935 
936 	mem_set_dump(dev, vq->avail, len, true, hua_to_alignment(dev->mem, vq->avail));
937 	len = sizeof(struct vring_used) +
938 		sizeof(struct vring_used_elem) * vq->size;
939 	if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
940 		len += sizeof(uint16_t);
941 	expected_len = len;
942 	vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
943 			vq, vq->ring_addrs.used_user_addr, &len);
944 	if (vq->used == 0 || len != expected_len) {
945 		VHOST_CONFIG_LOG(dev->ifname, DEBUG, "failed to map used ring.");
946 		return;
947 	}
948 
949 	mem_set_dump(dev, vq->used, len, true, hua_to_alignment(dev->mem, vq->used));
950 
951 	if (vq->last_used_idx != vq->used->idx) {
952 		VHOST_CONFIG_LOG(dev->ifname, WARNING,
953 			"last_used_idx (%u) and vq->used->idx (%u) mismatches;",
954 			vq->last_used_idx, vq->used->idx);
955 		vq->last_used_idx  = vq->used->idx;
956 		vq->last_avail_idx = vq->used->idx;
957 		vhost_virtqueue_reconnect_log_split(vq);
958 		VHOST_CONFIG_LOG(dev->ifname, WARNING,
959 			"some packets maybe resent for Tx and dropped for Rx");
960 	}
961 
962 	vq->access_ok = true;
963 
964 	VHOST_CONFIG_LOG(dev->ifname, DEBUG, "mapped address desc: %p", vq->desc);
965 	VHOST_CONFIG_LOG(dev->ifname, DEBUG, "mapped address avail: %p", vq->avail);
966 	VHOST_CONFIG_LOG(dev->ifname, DEBUG, "mapped address used: %p", vq->used);
967 	VHOST_CONFIG_LOG(dev->ifname, DEBUG, "log_guest_addr: %" PRIx64, vq->log_guest_addr);
968 }
969 
970 /*
971  * The virtio device sends us the desc, used and avail ring addresses.
972  * This function then converts these to our address space.
973  */
974 static int
975 vhost_user_set_vring_addr(struct virtio_net **pdev,
976 			struct vhu_msg_context *ctx,
977 			int main_fd __rte_unused)
978 {
979 	struct virtio_net *dev = *pdev;
980 	struct vhost_virtqueue *vq;
981 	struct vhost_vring_addr *addr = &ctx->msg.payload.addr;
982 	bool access_ok;
983 
984 	if (dev->mem == NULL)
985 		return RTE_VHOST_MSG_RESULT_ERR;
986 
987 	/* addr->index refers to the queue index. The txq 1, rxq is 0. */
988 	vq = dev->virtqueue[ctx->msg.payload.addr.index];
989 
990 	/*
991 	 * Rings addresses should not be interpreted as long as the ring is not
992 	 * started and enabled
993 	 */
994 	memcpy(&vq->ring_addrs, addr, sizeof(*addr));
995 
996 	if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)
997 		goto out;
998 
999 	/* vhost_user_lock_all_queue_pairs locked all qps */
1000 	VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_VRING_ADDR);
1001 
1002 	access_ok = vq->access_ok;
1003 
1004 	vring_invalidate(dev, vq);
1005 
1006 	if ((vq->enabled && (dev->features &
1007 				(1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) ||
1008 			access_ok) {
1009 		translate_ring_addresses(&dev, &vq);
1010 		*pdev = dev;
1011 	}
1012 
1013 out:
1014 	return RTE_VHOST_MSG_RESULT_OK;
1015 }
1016 
1017 /*
1018  * The virtio device sends us the available ring last used index.
1019  */
1020 static int
1021 vhost_user_set_vring_base(struct virtio_net **pdev,
1022 			struct vhu_msg_context *ctx,
1023 			int main_fd __rte_unused)
1024 {
1025 	struct virtio_net *dev = *pdev;
1026 	struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
1027 	uint64_t val = ctx->msg.payload.state.num;
1028 
1029 	if (vq_is_packed(dev)) {
1030 		/*
1031 		 * Bit[0:14]: avail index
1032 		 * Bit[15]: avail wrap counter
1033 		 */
1034 		vq->last_avail_idx = val & 0x7fff;
1035 		vq->avail_wrap_counter = !!(val & (0x1 << 15));
1036 		/*
1037 		 * Set used index to same value as available one, as
1038 		 * their values should be the same since ring processing
1039 		 * was stopped at get time.
1040 		 */
1041 		vq->last_used_idx = vq->last_avail_idx;
1042 		vq->used_wrap_counter = vq->avail_wrap_counter;
1043 		vhost_virtqueue_reconnect_log_packed(vq);
1044 	} else {
1045 		vq->last_used_idx = ctx->msg.payload.state.num;
1046 		vq->last_avail_idx = ctx->msg.payload.state.num;
1047 		vhost_virtqueue_reconnect_log_split(vq);
1048 	}
1049 
1050 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1051 		"vring base idx:%u last_used_idx:%u last_avail_idx:%u.",
1052 		ctx->msg.payload.state.index, vq->last_used_idx, vq->last_avail_idx);
1053 
1054 	return RTE_VHOST_MSG_RESULT_OK;
1055 }
1056 
1057 static int
1058 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
1059 		   uint64_t host_iova, uint64_t host_user_addr, uint64_t size)
1060 {
1061 	struct guest_page *page, *last_page;
1062 	struct guest_page *old_pages;
1063 
1064 	if (dev->nr_guest_pages == dev->max_guest_pages) {
1065 		dev->max_guest_pages *= 2;
1066 		old_pages = dev->guest_pages;
1067 		dev->guest_pages = rte_realloc(dev->guest_pages,
1068 					dev->max_guest_pages * sizeof(*page),
1069 					RTE_CACHE_LINE_SIZE);
1070 		if (dev->guest_pages == NULL) {
1071 			VHOST_CONFIG_LOG(dev->ifname, ERR, "cannot realloc guest_pages");
1072 			rte_free(old_pages);
1073 			return -1;
1074 		}
1075 	}
1076 
1077 	if (dev->nr_guest_pages > 0) {
1078 		last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
1079 		/* merge if the two pages are continuous */
1080 		if (host_iova == last_page->host_iova + last_page->size &&
1081 		    guest_phys_addr == last_page->guest_phys_addr + last_page->size &&
1082 		    host_user_addr == last_page->host_user_addr + last_page->size) {
1083 			last_page->size += size;
1084 			return 0;
1085 		}
1086 	}
1087 
1088 	page = &dev->guest_pages[dev->nr_guest_pages++];
1089 	page->guest_phys_addr = guest_phys_addr;
1090 	page->host_iova  = host_iova;
1091 	page->host_user_addr = host_user_addr;
1092 	page->size = size;
1093 
1094 	return 0;
1095 }
1096 
1097 static int
1098 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
1099 		uint64_t page_size)
1100 {
1101 	uint64_t reg_size = reg->size;
1102 	uint64_t host_user_addr  = reg->host_user_addr;
1103 	uint64_t guest_phys_addr = reg->guest_phys_addr;
1104 	uint64_t host_iova;
1105 	uint64_t size;
1106 
1107 	host_iova = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
1108 	size = page_size - (guest_phys_addr & (page_size - 1));
1109 	size = RTE_MIN(size, reg_size);
1110 
1111 	if (add_one_guest_page(dev, guest_phys_addr, host_iova,
1112 			       host_user_addr, size) < 0)
1113 		return -1;
1114 
1115 	host_user_addr  += size;
1116 	guest_phys_addr += size;
1117 	reg_size -= size;
1118 
1119 	while (reg_size > 0) {
1120 		size = RTE_MIN(reg_size, page_size);
1121 		host_iova = rte_mem_virt2iova((void *)(uintptr_t)
1122 						  host_user_addr);
1123 		if (add_one_guest_page(dev, guest_phys_addr, host_iova,
1124 				       host_user_addr, size) < 0)
1125 			return -1;
1126 
1127 		host_user_addr  += size;
1128 		guest_phys_addr += size;
1129 		reg_size -= size;
1130 	}
1131 
1132 	/* sort guest page array if over binary search threshold */
1133 	if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
1134 		qsort((void *)dev->guest_pages, dev->nr_guest_pages,
1135 			sizeof(struct guest_page), guest_page_addrcmp);
1136 	}
1137 
1138 	return 0;
1139 }
1140 
1141 #ifdef RTE_LIBRTE_VHOST_DEBUG
1142 /* TODO: enable it only in debug mode? */
1143 static void
1144 dump_guest_pages(struct virtio_net *dev)
1145 {
1146 	uint32_t i;
1147 	struct guest_page *page;
1148 
1149 	for (i = 0; i < dev->nr_guest_pages; i++) {
1150 		page = &dev->guest_pages[i];
1151 
1152 		VHOST_CONFIG_LOG(dev->ifname, INFO, "guest physical page region %u", i);
1153 		VHOST_CONFIG_LOG(dev->ifname, INFO, "\tguest_phys_addr: %" PRIx64,
1154 			page->guest_phys_addr);
1155 		VHOST_CONFIG_LOG(dev->ifname, INFO, "\thost_iova : %" PRIx64,
1156 			page->host_iova);
1157 		VHOST_CONFIG_LOG(dev->ifname, INFO, "\tsize           : %" PRIx64,
1158 			page->size);
1159 	}
1160 }
1161 #else
1162 #define dump_guest_pages(dev)
1163 #endif
1164 
1165 static bool
1166 vhost_memory_changed(struct VhostUserMemory *new,
1167 		     struct rte_vhost_memory *old)
1168 {
1169 	uint32_t i;
1170 
1171 	if (new->nregions != old->nregions)
1172 		return true;
1173 
1174 	for (i = 0; i < new->nregions; ++i) {
1175 		VhostUserMemoryRegion *new_r = &new->regions[i];
1176 		struct rte_vhost_mem_region *old_r = &old->regions[i];
1177 
1178 		if (new_r->guest_phys_addr != old_r->guest_phys_addr)
1179 			return true;
1180 		if (new_r->memory_size != old_r->size)
1181 			return true;
1182 		if (new_r->userspace_addr != old_r->guest_user_addr)
1183 			return true;
1184 	}
1185 
1186 	return false;
1187 }
1188 
1189 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
1190 static int
1191 vhost_user_postcopy_region_register(struct virtio_net *dev,
1192 		struct rte_vhost_mem_region *reg)
1193 {
1194 	struct uffdio_register reg_struct;
1195 
1196 	/*
1197 	 * Let's register all the mmapped area to ensure
1198 	 * alignment on page boundary.
1199 	 */
1200 	reg_struct.range.start = (uint64_t)(uintptr_t)reg->mmap_addr;
1201 	reg_struct.range.len = reg->mmap_size;
1202 	reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
1203 
1204 	if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
1205 				&reg_struct)) {
1206 		VHOST_CONFIG_LOG(dev->ifname, ERR,
1207 			"failed to register ufd for region "
1208 			"%" PRIx64 " - %" PRIx64 " (ufd = %d) %s",
1209 			(uint64_t)reg_struct.range.start,
1210 			(uint64_t)reg_struct.range.start +
1211 			(uint64_t)reg_struct.range.len - 1,
1212 			dev->postcopy_ufd,
1213 			strerror(errno));
1214 		return -1;
1215 	}
1216 
1217 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1218 		"\t userfaultfd registered for range : %" PRIx64 " - %" PRIx64,
1219 		(uint64_t)reg_struct.range.start,
1220 		(uint64_t)reg_struct.range.start +
1221 		(uint64_t)reg_struct.range.len - 1);
1222 
1223 	return 0;
1224 }
1225 #else
1226 static int
1227 vhost_user_postcopy_region_register(struct virtio_net *dev __rte_unused,
1228 		struct rte_vhost_mem_region *reg __rte_unused)
1229 {
1230 	return -1;
1231 }
1232 #endif
1233 
1234 static int
1235 vhost_user_postcopy_register(struct virtio_net *dev, int main_fd,
1236 		struct vhu_msg_context *ctx)
1237 {
1238 	struct VhostUserMemory *memory;
1239 	struct rte_vhost_mem_region *reg;
1240 	struct vhu_msg_context ack_ctx;
1241 	uint32_t i;
1242 
1243 	if (!dev->postcopy_listening)
1244 		return 0;
1245 
1246 	/*
1247 	 * We haven't a better way right now than sharing
1248 	 * DPDK's virtual address with Qemu, so that Qemu can
1249 	 * retrieve the region offset when handling userfaults.
1250 	 */
1251 	memory = &ctx->msg.payload.memory;
1252 	for (i = 0; i < memory->nregions; i++) {
1253 		reg = &dev->mem->regions[i];
1254 		memory->regions[i].userspace_addr = reg->host_user_addr;
1255 	}
1256 
1257 	/* Send the addresses back to qemu */
1258 	ctx->fd_num = 0;
1259 	send_vhost_reply(dev, main_fd, ctx);
1260 
1261 	/* Wait for qemu to acknowledge it got the addresses
1262 	 * we've got to wait before we're allowed to generate faults.
1263 	 */
1264 	if (read_vhost_message(dev, main_fd, &ack_ctx) <= 0) {
1265 		VHOST_CONFIG_LOG(dev->ifname, ERR,
1266 			"failed to read qemu ack on postcopy set-mem-table");
1267 		return -1;
1268 	}
1269 
1270 	if (validate_msg_fds(dev, &ack_ctx, 0) != 0)
1271 		return -1;
1272 
1273 	if (ack_ctx.msg.request.frontend != VHOST_USER_SET_MEM_TABLE) {
1274 		VHOST_CONFIG_LOG(dev->ifname, ERR,
1275 			"bad qemu ack on postcopy set-mem-table (%d)",
1276 			ack_ctx.msg.request.frontend);
1277 		return -1;
1278 	}
1279 
1280 	/* Now userfault register and we can use the memory */
1281 	for (i = 0; i < memory->nregions; i++) {
1282 		reg = &dev->mem->regions[i];
1283 		if (vhost_user_postcopy_region_register(dev, reg) < 0)
1284 			return -1;
1285 	}
1286 
1287 	return 0;
1288 }
1289 
1290 static int
1291 vhost_user_mmap_region(struct virtio_net *dev,
1292 		struct rte_vhost_mem_region *region,
1293 		uint64_t mmap_offset)
1294 {
1295 	void *mmap_addr;
1296 	uint64_t mmap_size;
1297 	uint64_t alignment;
1298 	int populate;
1299 
1300 	/* Check for memory_size + mmap_offset overflow */
1301 	if (mmap_offset >= -region->size) {
1302 		VHOST_CONFIG_LOG(dev->ifname, ERR,
1303 			"mmap_offset (%#"PRIx64") and memory_size (%#"PRIx64") overflow",
1304 			mmap_offset, region->size);
1305 		return -1;
1306 	}
1307 
1308 	mmap_size = region->size + mmap_offset;
1309 
1310 	/* mmap() without flag of MAP_ANONYMOUS, should be called with length
1311 	 * argument aligned with hugepagesz at older longterm version Linux,
1312 	 * like 2.6.32 and 3.2.72, or mmap() will fail with EINVAL.
1313 	 *
1314 	 * To avoid failure, make sure in caller to keep length aligned.
1315 	 */
1316 	alignment = get_blk_size(region->fd);
1317 	if (alignment == (uint64_t)-1) {
1318 		VHOST_CONFIG_LOG(dev->ifname, ERR, "couldn't get hugepage size through fstat");
1319 		return -1;
1320 	}
1321 	mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
1322 	if (mmap_size == 0) {
1323 		/*
1324 		 * It could happen if initial mmap_size + alignment overflows
1325 		 * the sizeof uint64, which could happen if either mmap_size or
1326 		 * alignment value is wrong.
1327 		 *
1328 		 * mmap() kernel implementation would return an error, but
1329 		 * better catch it before and provide useful info in the logs.
1330 		 */
1331 		VHOST_CONFIG_LOG(dev->ifname, ERR,
1332 			"mmap size (0x%" PRIx64 ") or alignment (0x%" PRIx64 ") is invalid",
1333 			region->size + mmap_offset, alignment);
1334 		return -1;
1335 	}
1336 
1337 	populate = dev->async_copy ? MAP_POPULATE : 0;
1338 	mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
1339 			MAP_SHARED | populate, region->fd, 0);
1340 
1341 	if (mmap_addr == MAP_FAILED) {
1342 		VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap failed (%s).", strerror(errno));
1343 		return -1;
1344 	}
1345 
1346 	region->mmap_addr = mmap_addr;
1347 	region->mmap_size = mmap_size;
1348 	region->host_user_addr = (uint64_t)(uintptr_t)mmap_addr + mmap_offset;
1349 	mem_set_dump(dev, mmap_addr, mmap_size, false, alignment);
1350 
1351 	if (dev->async_copy) {
1352 		if (add_guest_pages(dev, region, alignment) < 0) {
1353 			VHOST_CONFIG_LOG(dev->ifname, ERR,
1354 				"adding guest pages to region failed.");
1355 			return -1;
1356 		}
1357 	}
1358 
1359 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1360 		"guest memory region size: 0x%" PRIx64,
1361 		region->size);
1362 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1363 		"\t guest physical addr: 0x%" PRIx64,
1364 		region->guest_phys_addr);
1365 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1366 		"\t guest virtual  addr: 0x%" PRIx64,
1367 		region->guest_user_addr);
1368 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1369 		"\t host  virtual  addr: 0x%" PRIx64,
1370 		region->host_user_addr);
1371 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1372 		"\t mmap addr : 0x%" PRIx64,
1373 		(uint64_t)(uintptr_t)mmap_addr);
1374 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1375 		"\t mmap size : 0x%" PRIx64,
1376 		mmap_size);
1377 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1378 		"\t mmap align: 0x%" PRIx64,
1379 		alignment);
1380 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1381 		"\t mmap off  : 0x%" PRIx64,
1382 		mmap_offset);
1383 
1384 	return 0;
1385 }
1386 
1387 static int
1388 vhost_user_set_mem_table(struct virtio_net **pdev,
1389 			struct vhu_msg_context *ctx,
1390 			int main_fd)
1391 {
1392 	struct virtio_net *dev = *pdev;
1393 	struct VhostUserMemory *memory = &ctx->msg.payload.memory;
1394 	struct rte_vhost_mem_region *reg;
1395 	int numa_node = SOCKET_ID_ANY;
1396 	uint64_t mmap_offset;
1397 	uint32_t i;
1398 	bool async_notify = false;
1399 
1400 	if (validate_msg_fds(dev, ctx, memory->nregions) != 0)
1401 		return RTE_VHOST_MSG_RESULT_ERR;
1402 
1403 	if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
1404 		VHOST_CONFIG_LOG(dev->ifname, ERR,
1405 			"too many memory regions (%u)",
1406 			memory->nregions);
1407 		goto close_msg_fds;
1408 	}
1409 
1410 	if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
1411 		VHOST_CONFIG_LOG(dev->ifname, INFO, "memory regions not changed");
1412 
1413 		close_msg_fds(ctx);
1414 
1415 		return RTE_VHOST_MSG_RESULT_OK;
1416 	}
1417 
1418 	if (dev->mem) {
1419 		if (dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) {
1420 			struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
1421 
1422 			if (vdpa_dev && vdpa_dev->ops->dev_close)
1423 				vdpa_dev->ops->dev_close(dev->vid);
1424 			dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1425 		}
1426 
1427 		/* notify the vhost application to stop DMA transfers */
1428 		if (dev->async_copy && dev->notify_ops->vring_state_changed) {
1429 			for (i = 0; i < dev->nr_vring; i++) {
1430 				dev->notify_ops->vring_state_changed(dev->vid,
1431 						i, 0);
1432 			}
1433 			async_notify = true;
1434 		}
1435 
1436 		/* Flush IOTLB cache as previous HVAs are now invalid */
1437 		if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1438 			vhost_user_iotlb_flush_all(dev);
1439 
1440 		free_mem_region(dev);
1441 		rte_free(dev->mem);
1442 		dev->mem = NULL;
1443 	}
1444 
1445 	/*
1446 	 * If VQ 0 has already been allocated, try to allocate on the same
1447 	 * NUMA node. It can be reallocated later in numa_realloc().
1448 	 */
1449 	if (dev->nr_vring > 0)
1450 		numa_node = dev->virtqueue[0]->numa_node;
1451 
1452 	dev->nr_guest_pages = 0;
1453 	if (dev->guest_pages == NULL) {
1454 		dev->max_guest_pages = 8;
1455 		dev->guest_pages = rte_zmalloc_socket(NULL,
1456 					dev->max_guest_pages *
1457 					sizeof(struct guest_page),
1458 					RTE_CACHE_LINE_SIZE,
1459 					numa_node);
1460 		if (dev->guest_pages == NULL) {
1461 			VHOST_CONFIG_LOG(dev->ifname, ERR,
1462 				"failed to allocate memory for dev->guest_pages");
1463 			goto close_msg_fds;
1464 		}
1465 	}
1466 
1467 	dev->mem = rte_zmalloc_socket("vhost-mem-table", sizeof(struct rte_vhost_memory) +
1468 		sizeof(struct rte_vhost_mem_region) * memory->nregions, 0, numa_node);
1469 	if (dev->mem == NULL) {
1470 		VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to allocate memory for dev->mem");
1471 		goto free_guest_pages;
1472 	}
1473 
1474 	for (i = 0; i < memory->nregions; i++) {
1475 		reg = &dev->mem->regions[i];
1476 
1477 		reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
1478 		reg->guest_user_addr = memory->regions[i].userspace_addr;
1479 		reg->size            = memory->regions[i].memory_size;
1480 		reg->fd              = ctx->fds[i];
1481 
1482 		/*
1483 		 * Assign invalid file descriptor value to avoid double
1484 		 * closing on error path.
1485 		 */
1486 		ctx->fds[i] = -1;
1487 
1488 		mmap_offset = memory->regions[i].mmap_offset;
1489 
1490 		if (vhost_user_mmap_region(dev, reg, mmap_offset) < 0) {
1491 			VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap region %u", i);
1492 			goto free_mem_table;
1493 		}
1494 
1495 		dev->mem->nregions++;
1496 	}
1497 
1498 	if (dev->async_copy && rte_vfio_is_enabled("vfio"))
1499 		async_dma_map(dev, true);
1500 
1501 	if (vhost_user_postcopy_register(dev, main_fd, ctx) < 0)
1502 		goto free_mem_table;
1503 
1504 	for (i = 0; i < dev->nr_vring; i++) {
1505 		struct vhost_virtqueue *vq = dev->virtqueue[i];
1506 
1507 		if (!vq)
1508 			continue;
1509 
1510 		if (vq->desc || vq->avail || vq->used) {
1511 			/* vhost_user_lock_all_queue_pairs locked all qps */
1512 			VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_MEM_TABLE);
1513 
1514 			/*
1515 			 * If the memory table got updated, the ring addresses
1516 			 * need to be translated again as virtual addresses have
1517 			 * changed.
1518 			 */
1519 			vring_invalidate(dev, vq);
1520 
1521 			translate_ring_addresses(&dev, &vq);
1522 			*pdev = dev;
1523 		}
1524 	}
1525 
1526 	dump_guest_pages(dev);
1527 
1528 	if (async_notify) {
1529 		for (i = 0; i < dev->nr_vring; i++)
1530 			dev->notify_ops->vring_state_changed(dev->vid, i, 1);
1531 	}
1532 
1533 	return RTE_VHOST_MSG_RESULT_OK;
1534 
1535 free_mem_table:
1536 	free_mem_region(dev);
1537 	rte_free(dev->mem);
1538 	dev->mem = NULL;
1539 
1540 free_guest_pages:
1541 	rte_free(dev->guest_pages);
1542 	dev->guest_pages = NULL;
1543 close_msg_fds:
1544 	close_msg_fds(ctx);
1545 	return RTE_VHOST_MSG_RESULT_ERR;
1546 }
1547 
1548 static bool
1549 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
1550 {
1551 	bool rings_ok;
1552 
1553 	if (!vq)
1554 		return false;
1555 
1556 	if (vq_is_packed(dev))
1557 		rings_ok = vq->desc_packed && vq->driver_event &&
1558 			vq->device_event;
1559 	else
1560 		rings_ok = vq->desc && vq->avail && vq->used;
1561 
1562 	return rings_ok &&
1563 	       vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1564 	       vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD &&
1565 	       vq->enabled;
1566 }
1567 
1568 #define VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY 2u
1569 #define VIRTIO_BLK_NUM_VQS_TO_BE_READY 1u
1570 
1571 static int
1572 virtio_is_ready(struct virtio_net *dev)
1573 {
1574 	struct rte_vdpa_device *vdpa_dev;
1575 	struct vhost_virtqueue *vq;
1576 	uint32_t vdpa_type;
1577 	uint32_t i, nr_vring = dev->nr_vring;
1578 
1579 	if (dev->flags & VIRTIO_DEV_READY)
1580 		return 1;
1581 
1582 	if (!dev->nr_vring)
1583 		return 0;
1584 
1585 	vdpa_dev = dev->vdpa_dev;
1586 	if (vdpa_dev)
1587 		vdpa_type = vdpa_dev->type;
1588 	else
1589 		vdpa_type = -1;
1590 
1591 	if (vdpa_type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) {
1592 		nr_vring = VIRTIO_BLK_NUM_VQS_TO_BE_READY;
1593 	} else {
1594 		if (dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET)
1595 			nr_vring = VIRTIO_BUILTIN_NUM_VQS_TO_BE_READY;
1596 	}
1597 
1598 	if (dev->nr_vring < nr_vring)
1599 		return 0;
1600 
1601 	for (i = 0; i < nr_vring; i++) {
1602 		vq = dev->virtqueue[i];
1603 
1604 		if (!vq_is_ready(dev, vq))
1605 			return 0;
1606 	}
1607 
1608 	/* If supported, ensure the frontend is really done with config */
1609 	if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS))
1610 		if (!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK))
1611 			return 0;
1612 
1613 	dev->flags |= VIRTIO_DEV_READY;
1614 
1615 	if (!(dev->flags & VIRTIO_DEV_RUNNING))
1616 		VHOST_CONFIG_LOG(dev->ifname, INFO, "virtio is now ready for processing.");
1617 	return 1;
1618 }
1619 
1620 static void *
1621 inflight_mem_alloc(struct virtio_net *dev, const char *name, size_t size, int *fd)
1622 {
1623 	void *ptr;
1624 	int mfd = -1;
1625 	uint64_t alignment;
1626 	char fname[20] = "/tmp/memfd-XXXXXX";
1627 
1628 	*fd = -1;
1629 #ifdef MEMFD_SUPPORTED
1630 	mfd = memfd_create(name, MFD_CLOEXEC);
1631 #else
1632 	RTE_SET_USED(name);
1633 #endif
1634 	if (mfd == -1) {
1635 		mfd = mkstemp(fname);
1636 		if (mfd == -1) {
1637 			VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to get inflight buffer fd");
1638 			return NULL;
1639 		}
1640 
1641 		unlink(fname);
1642 	}
1643 
1644 	if (ftruncate(mfd, size) == -1) {
1645 		VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc inflight buffer");
1646 		close(mfd);
1647 		return NULL;
1648 	}
1649 
1650 	ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
1651 	if (ptr == MAP_FAILED) {
1652 		VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap inflight buffer");
1653 		close(mfd);
1654 		return NULL;
1655 	}
1656 
1657 	alignment = get_blk_size(mfd);
1658 	mem_set_dump(dev, ptr, size, false, alignment);
1659 	*fd = mfd;
1660 	return ptr;
1661 }
1662 
1663 static uint32_t
1664 get_pervq_shm_size_split(uint16_t queue_size)
1665 {
1666 	return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_split) *
1667 				  queue_size + sizeof(uint64_t) +
1668 				  sizeof(uint16_t) * 4, INFLIGHT_ALIGNMENT);
1669 }
1670 
1671 static uint32_t
1672 get_pervq_shm_size_packed(uint16_t queue_size)
1673 {
1674 	return RTE_ALIGN_MUL_CEIL(sizeof(struct rte_vhost_inflight_desc_packed)
1675 				  * queue_size + sizeof(uint64_t) +
1676 				  sizeof(uint16_t) * 6 + sizeof(uint8_t) * 9,
1677 				  INFLIGHT_ALIGNMENT);
1678 }
1679 
1680 static int
1681 vhost_user_get_inflight_fd(struct virtio_net **pdev,
1682 			   struct vhu_msg_context *ctx,
1683 			   int main_fd __rte_unused)
1684 {
1685 	struct rte_vhost_inflight_info_packed *inflight_packed;
1686 	uint64_t pervq_inflight_size, mmap_size;
1687 	uint16_t num_queues, queue_size;
1688 	struct virtio_net *dev = *pdev;
1689 	int fd, i, j;
1690 	int numa_node = SOCKET_ID_ANY;
1691 	void *addr;
1692 
1693 	if (ctx->msg.size != sizeof(ctx->msg.payload.inflight)) {
1694 		VHOST_CONFIG_LOG(dev->ifname, ERR,
1695 			"invalid get_inflight_fd message size is %d",
1696 			ctx->msg.size);
1697 		return RTE_VHOST_MSG_RESULT_ERR;
1698 	}
1699 
1700 	/*
1701 	 * If VQ 0 has already been allocated, try to allocate on the same
1702 	 * NUMA node. It can be reallocated later in numa_realloc().
1703 	 */
1704 	if (dev->nr_vring > 0)
1705 		numa_node = dev->virtqueue[0]->numa_node;
1706 
1707 	if (dev->inflight_info == NULL) {
1708 		dev->inflight_info = rte_zmalloc_socket("inflight_info",
1709 				sizeof(struct inflight_mem_info), 0, numa_node);
1710 		if (!dev->inflight_info) {
1711 			VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc dev inflight area");
1712 			return RTE_VHOST_MSG_RESULT_ERR;
1713 		}
1714 		dev->inflight_info->fd = -1;
1715 	}
1716 
1717 	num_queues = ctx->msg.payload.inflight.num_queues;
1718 	queue_size = ctx->msg.payload.inflight.queue_size;
1719 
1720 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1721 		"get_inflight_fd num_queues: %u",
1722 		ctx->msg.payload.inflight.num_queues);
1723 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1724 		"get_inflight_fd queue_size: %u",
1725 		ctx->msg.payload.inflight.queue_size);
1726 
1727 	if (vq_is_packed(dev))
1728 		pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1729 	else
1730 		pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1731 
1732 	mmap_size = num_queues * pervq_inflight_size;
1733 	addr = inflight_mem_alloc(dev, "vhost-inflight", mmap_size, &fd);
1734 	if (!addr) {
1735 		VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc vhost inflight area");
1736 			ctx->msg.payload.inflight.mmap_size = 0;
1737 		return RTE_VHOST_MSG_RESULT_ERR;
1738 	}
1739 	memset(addr, 0, mmap_size);
1740 
1741 	if (dev->inflight_info->addr) {
1742 		munmap(dev->inflight_info->addr, dev->inflight_info->size);
1743 		dev->inflight_info->addr = NULL;
1744 	}
1745 
1746 	if (dev->inflight_info->fd >= 0) {
1747 		close(dev->inflight_info->fd);
1748 		dev->inflight_info->fd = -1;
1749 	}
1750 
1751 	dev->inflight_info->addr = addr;
1752 	dev->inflight_info->size = ctx->msg.payload.inflight.mmap_size = mmap_size;
1753 	dev->inflight_info->fd = ctx->fds[0] = fd;
1754 	ctx->msg.payload.inflight.mmap_offset = 0;
1755 	ctx->fd_num = 1;
1756 
1757 	if (vq_is_packed(dev)) {
1758 		for (i = 0; i < num_queues; i++) {
1759 			inflight_packed =
1760 				(struct rte_vhost_inflight_info_packed *)addr;
1761 			inflight_packed->used_wrap_counter = 1;
1762 			inflight_packed->old_used_wrap_counter = 1;
1763 			for (j = 0; j < queue_size; j++)
1764 				inflight_packed->desc[j].next = j + 1;
1765 			addr = (void *)((char *)addr + pervq_inflight_size);
1766 		}
1767 	}
1768 
1769 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1770 		"send inflight mmap_size: %"PRIu64,
1771 		ctx->msg.payload.inflight.mmap_size);
1772 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1773 		"send inflight mmap_offset: %"PRIu64,
1774 		ctx->msg.payload.inflight.mmap_offset);
1775 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1776 		"send inflight fd: %d", ctx->fds[0]);
1777 
1778 	return RTE_VHOST_MSG_RESULT_REPLY;
1779 }
1780 
1781 static int
1782 vhost_user_set_inflight_fd(struct virtio_net **pdev,
1783 			   struct vhu_msg_context *ctx,
1784 			   int main_fd __rte_unused)
1785 {
1786 	uint64_t mmap_size, mmap_offset;
1787 	uint16_t num_queues, queue_size;
1788 	struct virtio_net *dev = *pdev;
1789 	uint32_t pervq_inflight_size;
1790 	struct vhost_virtqueue *vq;
1791 	void *addr;
1792 	int fd, i;
1793 	int numa_node = SOCKET_ID_ANY;
1794 
1795 	if (validate_msg_fds(dev, ctx, 1) != 0)
1796 		return RTE_VHOST_MSG_RESULT_ERR;
1797 
1798 	fd = ctx->fds[0];
1799 	if (ctx->msg.size != sizeof(ctx->msg.payload.inflight) || fd < 0) {
1800 		VHOST_CONFIG_LOG(dev->ifname, ERR,
1801 			"invalid set_inflight_fd message size is %d,fd is %d",
1802 			ctx->msg.size, fd);
1803 		return RTE_VHOST_MSG_RESULT_ERR;
1804 	}
1805 
1806 	mmap_size = ctx->msg.payload.inflight.mmap_size;
1807 	mmap_offset = ctx->msg.payload.inflight.mmap_offset;
1808 	num_queues = ctx->msg.payload.inflight.num_queues;
1809 	queue_size = ctx->msg.payload.inflight.queue_size;
1810 
1811 	if (vq_is_packed(dev))
1812 		pervq_inflight_size = get_pervq_shm_size_packed(queue_size);
1813 	else
1814 		pervq_inflight_size = get_pervq_shm_size_split(queue_size);
1815 
1816 	VHOST_CONFIG_LOG(dev->ifname, INFO, "set_inflight_fd mmap_size: %"PRIu64, mmap_size);
1817 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1818 		"set_inflight_fd mmap_offset: %"PRIu64,
1819 		mmap_offset);
1820 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1821 		"set_inflight_fd num_queues: %u",
1822 		num_queues);
1823 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1824 		"set_inflight_fd queue_size: %u",
1825 		queue_size);
1826 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1827 		"set_inflight_fd fd: %d",
1828 		fd);
1829 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1830 		"set_inflight_fd pervq_inflight_size: %d",
1831 		pervq_inflight_size);
1832 
1833 	/*
1834 	 * If VQ 0 has already been allocated, try to allocate on the same
1835 	 * NUMA node. It can be reallocated later in numa_realloc().
1836 	 */
1837 	if (dev->nr_vring > 0)
1838 		numa_node = dev->virtqueue[0]->numa_node;
1839 
1840 	if (!dev->inflight_info) {
1841 		dev->inflight_info = rte_zmalloc_socket("inflight_info",
1842 				sizeof(struct inflight_mem_info), 0, numa_node);
1843 		if (dev->inflight_info == NULL) {
1844 			VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc dev inflight area");
1845 			return RTE_VHOST_MSG_RESULT_ERR;
1846 		}
1847 		dev->inflight_info->fd = -1;
1848 	}
1849 
1850 	if (dev->inflight_info->addr) {
1851 		munmap(dev->inflight_info->addr, dev->inflight_info->size);
1852 		dev->inflight_info->addr = NULL;
1853 	}
1854 
1855 	addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
1856 		    fd, mmap_offset);
1857 	if (addr == MAP_FAILED) {
1858 		VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to mmap share memory.");
1859 		return RTE_VHOST_MSG_RESULT_ERR;
1860 	}
1861 
1862 	if (dev->inflight_info->fd >= 0) {
1863 		close(dev->inflight_info->fd);
1864 		dev->inflight_info->fd = -1;
1865 	}
1866 
1867 	mem_set_dump(dev, addr, mmap_size, false, get_blk_size(fd));
1868 	dev->inflight_info->fd = fd;
1869 	dev->inflight_info->addr = addr;
1870 	dev->inflight_info->size = mmap_size;
1871 
1872 	for (i = 0; i < num_queues; i++) {
1873 		vq = dev->virtqueue[i];
1874 		if (!vq)
1875 			continue;
1876 
1877 		cleanup_vq_inflight(dev, vq);
1878 		if (vq_is_packed(dev)) {
1879 			vq->inflight_packed = addr;
1880 			vq->inflight_packed->desc_num = queue_size;
1881 		} else {
1882 			vq->inflight_split = addr;
1883 			vq->inflight_split->desc_num = queue_size;
1884 		}
1885 		addr = (void *)((char *)addr + pervq_inflight_size);
1886 	}
1887 
1888 	return RTE_VHOST_MSG_RESULT_OK;
1889 }
1890 
1891 static int
1892 vhost_user_set_vring_call(struct virtio_net **pdev,
1893 			struct vhu_msg_context *ctx,
1894 			int main_fd __rte_unused)
1895 {
1896 	struct virtio_net *dev = *pdev;
1897 	struct vhost_vring_file file;
1898 	struct vhost_virtqueue *vq;
1899 	int expected_fds;
1900 
1901 	expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1902 	if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1903 		return RTE_VHOST_MSG_RESULT_ERR;
1904 
1905 	file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
1906 	if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1907 		file.fd = VIRTIO_INVALID_EVENTFD;
1908 	else
1909 		file.fd = ctx->fds[0];
1910 	VHOST_CONFIG_LOG(dev->ifname, INFO,
1911 		"vring call idx:%d file:%d",
1912 		file.index, file.fd);
1913 
1914 	vq = dev->virtqueue[file.index];
1915 
1916 	if (vq->ready) {
1917 		vq->ready = false;
1918 		vhost_user_notify_queue_state(dev, vq, 0);
1919 	}
1920 
1921 	if (vq->callfd >= 0)
1922 		close(vq->callfd);
1923 
1924 	vq->callfd = file.fd;
1925 
1926 	return RTE_VHOST_MSG_RESULT_OK;
1927 }
1928 
1929 static int vhost_user_set_vring_err(struct virtio_net **pdev,
1930 			struct vhu_msg_context *ctx,
1931 			int main_fd __rte_unused)
1932 {
1933 	struct virtio_net *dev = *pdev;
1934 	int expected_fds;
1935 
1936 	expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
1937 	if (validate_msg_fds(dev, ctx, expected_fds) != 0)
1938 		return RTE_VHOST_MSG_RESULT_ERR;
1939 
1940 	if (!(ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1941 		close(ctx->fds[0]);
1942 	VHOST_CONFIG_LOG(dev->ifname, DEBUG, "not implemented");
1943 
1944 	return RTE_VHOST_MSG_RESULT_OK;
1945 }
1946 
1947 static int
1948 resubmit_desc_compare(const void *a, const void *b)
1949 {
1950 	const struct rte_vhost_resubmit_desc *desc0 = a;
1951 	const struct rte_vhost_resubmit_desc *desc1 = b;
1952 
1953 	if (desc1->counter > desc0->counter)
1954 		return 1;
1955 
1956 	return -1;
1957 }
1958 
1959 static int
1960 vhost_check_queue_inflights_split(struct virtio_net *dev,
1961 				  struct vhost_virtqueue *vq)
1962 {
1963 	uint16_t i;
1964 	uint16_t resubmit_num = 0, last_io, num;
1965 	struct vring_used *used = vq->used;
1966 	struct rte_vhost_resubmit_info *resubmit;
1967 	struct rte_vhost_inflight_info_split *inflight_split;
1968 
1969 	if (!(dev->protocol_features &
1970 	    (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
1971 		return RTE_VHOST_MSG_RESULT_OK;
1972 
1973 	/* The frontend may still not support the inflight feature
1974 	 * although we negotiate the protocol feature.
1975 	 */
1976 	if ((!vq->inflight_split))
1977 		return RTE_VHOST_MSG_RESULT_OK;
1978 
1979 	if (!vq->inflight_split->version) {
1980 		vq->inflight_split->version = INFLIGHT_VERSION;
1981 		return RTE_VHOST_MSG_RESULT_OK;
1982 	}
1983 
1984 	if (vq->resubmit_inflight)
1985 		return RTE_VHOST_MSG_RESULT_OK;
1986 
1987 	inflight_split = vq->inflight_split;
1988 	vq->global_counter = 0;
1989 	last_io = inflight_split->last_inflight_io;
1990 
1991 	if (inflight_split->used_idx != used->idx) {
1992 		inflight_split->desc[last_io].inflight = 0;
1993 		rte_atomic_thread_fence(rte_memory_order_seq_cst);
1994 		inflight_split->used_idx = used->idx;
1995 	}
1996 
1997 	for (i = 0; i < inflight_split->desc_num; i++) {
1998 		if (inflight_split->desc[i].inflight == 1)
1999 			resubmit_num++;
2000 	}
2001 
2002 	vq->last_avail_idx += resubmit_num;
2003 	vhost_virtqueue_reconnect_log_split(vq);
2004 
2005 	if (resubmit_num) {
2006 		resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
2007 				0, vq->numa_node);
2008 		if (!resubmit) {
2009 			VHOST_CONFIG_LOG(dev->ifname, ERR,
2010 				"failed to allocate memory for resubmit info.");
2011 			return RTE_VHOST_MSG_RESULT_ERR;
2012 		}
2013 
2014 		resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
2015 				resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
2016 				0, vq->numa_node);
2017 		if (!resubmit->resubmit_list) {
2018 			VHOST_CONFIG_LOG(dev->ifname, ERR,
2019 					"failed to allocate memory for inflight desc.");
2020 			rte_free(resubmit);
2021 			return RTE_VHOST_MSG_RESULT_ERR;
2022 		}
2023 
2024 		num = 0;
2025 		for (i = 0; i < vq->inflight_split->desc_num; i++) {
2026 			if (vq->inflight_split->desc[i].inflight == 1) {
2027 				resubmit->resubmit_list[num].index = i;
2028 				resubmit->resubmit_list[num].counter =
2029 					inflight_split->desc[i].counter;
2030 				num++;
2031 			}
2032 		}
2033 		resubmit->resubmit_num = num;
2034 
2035 		if (resubmit->resubmit_num > 1)
2036 			qsort(resubmit->resubmit_list, resubmit->resubmit_num,
2037 			      sizeof(struct rte_vhost_resubmit_desc),
2038 			      resubmit_desc_compare);
2039 
2040 		vq->global_counter = resubmit->resubmit_list[0].counter + 1;
2041 		vq->resubmit_inflight = resubmit;
2042 	}
2043 
2044 	return RTE_VHOST_MSG_RESULT_OK;
2045 }
2046 
2047 static int
2048 vhost_check_queue_inflights_packed(struct virtio_net *dev,
2049 				   struct vhost_virtqueue *vq)
2050 {
2051 	uint16_t i;
2052 	uint16_t resubmit_num = 0, old_used_idx, num;
2053 	struct rte_vhost_resubmit_info *resubmit;
2054 	struct rte_vhost_inflight_info_packed *inflight_packed;
2055 
2056 	if (!(dev->protocol_features &
2057 	    (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
2058 		return RTE_VHOST_MSG_RESULT_OK;
2059 
2060 	/* The frontend may still not support the inflight feature
2061 	 * although we negotiate the protocol feature.
2062 	 */
2063 	if ((!vq->inflight_packed))
2064 		return RTE_VHOST_MSG_RESULT_OK;
2065 
2066 	if (!vq->inflight_packed->version) {
2067 		vq->inflight_packed->version = INFLIGHT_VERSION;
2068 		return RTE_VHOST_MSG_RESULT_OK;
2069 	}
2070 
2071 	if (vq->resubmit_inflight)
2072 		return RTE_VHOST_MSG_RESULT_OK;
2073 
2074 	inflight_packed = vq->inflight_packed;
2075 	vq->global_counter = 0;
2076 	old_used_idx = inflight_packed->old_used_idx;
2077 
2078 	if (inflight_packed->used_idx != old_used_idx) {
2079 		if (inflight_packed->desc[old_used_idx].inflight == 0) {
2080 			inflight_packed->old_used_idx =
2081 				inflight_packed->used_idx;
2082 			inflight_packed->old_used_wrap_counter =
2083 				inflight_packed->used_wrap_counter;
2084 			inflight_packed->old_free_head =
2085 				inflight_packed->free_head;
2086 		} else {
2087 			inflight_packed->used_idx =
2088 				inflight_packed->old_used_idx;
2089 			inflight_packed->used_wrap_counter =
2090 				inflight_packed->old_used_wrap_counter;
2091 			inflight_packed->free_head =
2092 				inflight_packed->old_free_head;
2093 		}
2094 	}
2095 
2096 	for (i = 0; i < inflight_packed->desc_num; i++) {
2097 		if (inflight_packed->desc[i].inflight == 1)
2098 			resubmit_num++;
2099 	}
2100 
2101 	if (resubmit_num) {
2102 		resubmit = rte_zmalloc_socket("resubmit", sizeof(struct rte_vhost_resubmit_info),
2103 				0, vq->numa_node);
2104 		if (resubmit == NULL) {
2105 			VHOST_CONFIG_LOG(dev->ifname, ERR,
2106 				"failed to allocate memory for resubmit info.");
2107 			return RTE_VHOST_MSG_RESULT_ERR;
2108 		}
2109 
2110 		resubmit->resubmit_list = rte_zmalloc_socket("resubmit_list",
2111 				resubmit_num * sizeof(struct rte_vhost_resubmit_desc),
2112 				0, vq->numa_node);
2113 		if (resubmit->resubmit_list == NULL) {
2114 			VHOST_CONFIG_LOG(dev->ifname, ERR,
2115 				"failed to allocate memory for resubmit desc.");
2116 			rte_free(resubmit);
2117 			return RTE_VHOST_MSG_RESULT_ERR;
2118 		}
2119 
2120 		num = 0;
2121 		for (i = 0; i < inflight_packed->desc_num; i++) {
2122 			if (vq->inflight_packed->desc[i].inflight == 1) {
2123 				resubmit->resubmit_list[num].index = i;
2124 				resubmit->resubmit_list[num].counter =
2125 					inflight_packed->desc[i].counter;
2126 				num++;
2127 			}
2128 		}
2129 		resubmit->resubmit_num = num;
2130 
2131 		if (resubmit->resubmit_num > 1)
2132 			qsort(resubmit->resubmit_list, resubmit->resubmit_num,
2133 			      sizeof(struct rte_vhost_resubmit_desc),
2134 			      resubmit_desc_compare);
2135 
2136 		vq->global_counter = resubmit->resubmit_list[0].counter + 1;
2137 		vq->resubmit_inflight = resubmit;
2138 	}
2139 
2140 	return RTE_VHOST_MSG_RESULT_OK;
2141 }
2142 
2143 static int
2144 vhost_user_set_vring_kick(struct virtio_net **pdev,
2145 			struct vhu_msg_context *ctx,
2146 			int main_fd __rte_unused)
2147 {
2148 	struct virtio_net *dev = *pdev;
2149 	struct vhost_vring_file file;
2150 	struct vhost_virtqueue *vq;
2151 	int expected_fds;
2152 
2153 	expected_fds = (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
2154 	if (validate_msg_fds(dev, ctx, expected_fds) != 0)
2155 		return RTE_VHOST_MSG_RESULT_ERR;
2156 
2157 	file.index = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
2158 	if (ctx->msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)
2159 		file.fd = VIRTIO_INVALID_EVENTFD;
2160 	else
2161 		file.fd = ctx->fds[0];
2162 	VHOST_CONFIG_LOG(dev->ifname, INFO,
2163 		"vring kick idx:%d file:%d",
2164 		file.index, file.fd);
2165 
2166 	/* Interpret ring addresses only when ring is started. */
2167 	vq = dev->virtqueue[file.index];
2168 	translate_ring_addresses(&dev, &vq);
2169 	*pdev = dev;
2170 
2171 	/*
2172 	 * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
2173 	 * the ring starts already enabled. Otherwise, it is enabled via
2174 	 * the SET_VRING_ENABLE message.
2175 	 */
2176 	if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
2177 		vq->enabled = true;
2178 	}
2179 
2180 	if (vq->ready) {
2181 		vq->ready = false;
2182 		vhost_user_notify_queue_state(dev, vq, 0);
2183 	}
2184 
2185 	if (vq->kickfd >= 0)
2186 		close(vq->kickfd);
2187 	vq->kickfd = file.fd;
2188 
2189 	if (vq_is_packed(dev)) {
2190 		if (vhost_check_queue_inflights_packed(dev, vq)) {
2191 			VHOST_CONFIG_LOG(dev->ifname, ERR,
2192 				"failed to inflights for vq: %d",
2193 				file.index);
2194 			return RTE_VHOST_MSG_RESULT_ERR;
2195 		}
2196 	} else {
2197 		if (vhost_check_queue_inflights_split(dev, vq)) {
2198 			VHOST_CONFIG_LOG(dev->ifname, ERR,
2199 				"failed to inflights for vq: %d",
2200 				file.index);
2201 			return RTE_VHOST_MSG_RESULT_ERR;
2202 		}
2203 	}
2204 
2205 	return RTE_VHOST_MSG_RESULT_OK;
2206 }
2207 
2208 /*
2209  * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
2210  */
2211 static int
2212 vhost_user_get_vring_base(struct virtio_net **pdev,
2213 			struct vhu_msg_context *ctx,
2214 			int main_fd __rte_unused)
2215 {
2216 	struct virtio_net *dev = *pdev;
2217 	struct vhost_virtqueue *vq = dev->virtqueue[ctx->msg.payload.state.index];
2218 	uint64_t val;
2219 
2220 	/* We have to stop the queue (virtio) if it is running. */
2221 	vhost_destroy_device_notify(dev);
2222 
2223 	dev->flags &= ~VIRTIO_DEV_READY;
2224 	dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
2225 
2226 	/* Here we are safe to get the indexes */
2227 	if (vq_is_packed(dev)) {
2228 		/*
2229 		 * Bit[0:14]: avail index
2230 		 * Bit[15]: avail wrap counter
2231 		 */
2232 		val = vq->last_avail_idx & 0x7fff;
2233 		val |= vq->avail_wrap_counter << 15;
2234 		ctx->msg.payload.state.num = val;
2235 	} else {
2236 		ctx->msg.payload.state.num = vq->last_avail_idx;
2237 	}
2238 
2239 	VHOST_CONFIG_LOG(dev->ifname, INFO,
2240 		"vring base idx:%d file:%d",
2241 		ctx->msg.payload.state.index, ctx->msg.payload.state.num);
2242 	/*
2243 	 * Based on current qemu vhost-user implementation, this message is
2244 	 * sent and only sent in vhost_vring_stop.
2245 	 * TODO: cleanup the vring, it isn't usable since here.
2246 	 */
2247 	if (vq->kickfd >= 0)
2248 		close(vq->kickfd);
2249 
2250 	vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
2251 
2252 	if (vq->callfd >= 0)
2253 		close(vq->callfd);
2254 
2255 	vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
2256 
2257 	vq->signalled_used_valid = false;
2258 
2259 	if (vq_is_packed(dev)) {
2260 		rte_free(vq->shadow_used_packed);
2261 		vq->shadow_used_packed = NULL;
2262 	} else {
2263 		rte_free(vq->shadow_used_split);
2264 		vq->shadow_used_split = NULL;
2265 	}
2266 
2267 	rte_free(vq->batch_copy_elems);
2268 	vq->batch_copy_elems = NULL;
2269 
2270 	rte_free(vq->log_cache);
2271 	vq->log_cache = NULL;
2272 
2273 	ctx->msg.size = sizeof(ctx->msg.payload.state);
2274 	ctx->fd_num = 0;
2275 
2276 	vhost_user_iotlb_flush_all(dev);
2277 
2278 	rte_rwlock_write_lock(&vq->access_lock);
2279 	vring_invalidate(dev, vq);
2280 	rte_rwlock_write_unlock(&vq->access_lock);
2281 
2282 	return RTE_VHOST_MSG_RESULT_REPLY;
2283 }
2284 
2285 /*
2286  * when virtio queues are ready to work, qemu will send us to
2287  * enable the virtio queue pair.
2288  */
2289 static int
2290 vhost_user_set_vring_enable(struct virtio_net **pdev,
2291 			struct vhu_msg_context *ctx,
2292 			int main_fd __rte_unused)
2293 {
2294 	struct virtio_net *dev = *pdev;
2295 	struct vhost_virtqueue *vq;
2296 	bool enable = !!ctx->msg.payload.state.num;
2297 	int index = (int)ctx->msg.payload.state.index;
2298 
2299 	VHOST_CONFIG_LOG(dev->ifname, INFO,
2300 		"set queue enable: %d to qp idx: %d",
2301 		enable, index);
2302 
2303 	vq = dev->virtqueue[index];
2304 	if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
2305 		/* vhost_user_lock_all_queue_pairs locked all qps */
2306 		VHOST_USER_ASSERT_LOCK(dev, vq, VHOST_USER_SET_VRING_ENABLE);
2307 		if (enable && vq->async && vq->async->pkts_inflight_n) {
2308 			VHOST_CONFIG_LOG(dev->ifname, ERR,
2309 				"failed to enable vring. Inflight packets must be completed first");
2310 			return RTE_VHOST_MSG_RESULT_ERR;
2311 		}
2312 	}
2313 
2314 	vq->enabled = enable;
2315 
2316 	return RTE_VHOST_MSG_RESULT_OK;
2317 }
2318 
2319 static int
2320 vhost_user_get_protocol_features(struct virtio_net **pdev,
2321 			struct vhu_msg_context *ctx,
2322 			int main_fd __rte_unused)
2323 {
2324 	struct virtio_net *dev = *pdev;
2325 	uint64_t protocol_features;
2326 
2327 	rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
2328 
2329 	ctx->msg.payload.u64 = protocol_features;
2330 	ctx->msg.size = sizeof(ctx->msg.payload.u64);
2331 	ctx->fd_num = 0;
2332 
2333 	return RTE_VHOST_MSG_RESULT_REPLY;
2334 }
2335 
2336 static int
2337 vhost_user_set_protocol_features(struct virtio_net **pdev,
2338 			struct vhu_msg_context *ctx,
2339 			int main_fd __rte_unused)
2340 {
2341 	struct virtio_net *dev = *pdev;
2342 	uint64_t protocol_features = ctx->msg.payload.u64;
2343 	uint64_t backend_protocol_features = 0;
2344 
2345 	rte_vhost_driver_get_protocol_features(dev->ifname,
2346 			&backend_protocol_features);
2347 	if (protocol_features & ~backend_protocol_features) {
2348 		VHOST_CONFIG_LOG(dev->ifname, ERR, "received invalid protocol features.");
2349 		return RTE_VHOST_MSG_RESULT_ERR;
2350 	}
2351 
2352 	dev->protocol_features = protocol_features;
2353 	VHOST_CONFIG_LOG(dev->ifname, INFO,
2354 		"negotiated Vhost-user protocol features: 0x%" PRIx64,
2355 		dev->protocol_features);
2356 
2357 	return RTE_VHOST_MSG_RESULT_OK;
2358 }
2359 
2360 static int
2361 vhost_user_set_log_base(struct virtio_net **pdev,
2362 			struct vhu_msg_context *ctx,
2363 			int main_fd __rte_unused)
2364 {
2365 	struct virtio_net *dev = *pdev;
2366 	int fd = ctx->fds[0];
2367 	uint64_t size, off;
2368 	uint64_t alignment;
2369 	void *addr;
2370 	uint32_t i;
2371 
2372 	if (validate_msg_fds(dev, ctx, 1) != 0)
2373 		return RTE_VHOST_MSG_RESULT_ERR;
2374 
2375 	if (fd < 0) {
2376 		VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid log fd: %d", fd);
2377 		return RTE_VHOST_MSG_RESULT_ERR;
2378 	}
2379 
2380 	if (ctx->msg.size != sizeof(VhostUserLog)) {
2381 		VHOST_CONFIG_LOG(dev->ifname, ERR,
2382 			"invalid log base msg size: %"PRId32" != %d",
2383 			ctx->msg.size, (int)sizeof(VhostUserLog));
2384 		goto close_msg_fds;
2385 	}
2386 
2387 	size = ctx->msg.payload.log.mmap_size;
2388 	off  = ctx->msg.payload.log.mmap_offset;
2389 
2390 	/* Check for mmap size and offset overflow. */
2391 	if (off >= -size) {
2392 		VHOST_CONFIG_LOG(dev->ifname, ERR,
2393 			"log offset %#"PRIx64" and log size %#"PRIx64" overflow",
2394 			off, size);
2395 		goto close_msg_fds;
2396 	}
2397 
2398 	VHOST_CONFIG_LOG(dev->ifname, INFO,
2399 		"log mmap size: %"PRId64", offset: %"PRId64,
2400 		size, off);
2401 
2402 	/*
2403 	 * mmap from 0 to workaround a hugepage mmap bug: mmap will
2404 	 * fail when offset is not page size aligned.
2405 	 */
2406 	addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, off);
2407 	alignment = get_blk_size(fd);
2408 	close(fd);
2409 	if (addr == MAP_FAILED) {
2410 		VHOST_CONFIG_LOG(dev->ifname, ERR, "mmap log base failed!");
2411 		return RTE_VHOST_MSG_RESULT_ERR;
2412 	}
2413 
2414 	/*
2415 	 * Free previously mapped log memory on occasionally
2416 	 * multiple VHOST_USER_SET_LOG_BASE.
2417 	 */
2418 	if (dev->log_addr) {
2419 		munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
2420 	}
2421 	dev->log_addr = (uint64_t)(uintptr_t)addr;
2422 	dev->log_base = dev->log_addr + off;
2423 	dev->log_size = size;
2424 	mem_set_dump(dev, addr, size + off, false, alignment);
2425 
2426 	for (i = 0; i < dev->nr_vring; i++) {
2427 		struct vhost_virtqueue *vq = dev->virtqueue[i];
2428 
2429 		rte_free(vq->log_cache);
2430 		vq->log_cache = NULL;
2431 		vq->log_cache_nb_elem = 0;
2432 		vq->log_cache = rte_malloc_socket("vq log cache",
2433 				sizeof(struct log_cache_entry) * VHOST_LOG_CACHE_NR,
2434 				0, vq->numa_node);
2435 		/*
2436 		 * If log cache alloc fail, don't fail migration, but no
2437 		 * caching will be done, which will impact performance
2438 		 */
2439 		if (!vq->log_cache)
2440 			VHOST_CONFIG_LOG(dev->ifname, ERR,
2441 				"failed to allocate VQ logging cache");
2442 	}
2443 
2444 	/*
2445 	 * The spec is not clear about it (yet), but QEMU doesn't expect
2446 	 * any payload in the reply.
2447 	 */
2448 	ctx->msg.size = 0;
2449 	ctx->fd_num = 0;
2450 
2451 	return RTE_VHOST_MSG_RESULT_REPLY;
2452 
2453 close_msg_fds:
2454 	close_msg_fds(ctx);
2455 	return RTE_VHOST_MSG_RESULT_ERR;
2456 }
2457 
2458 static int vhost_user_set_log_fd(struct virtio_net **pdev,
2459 			struct vhu_msg_context *ctx,
2460 			int main_fd __rte_unused)
2461 {
2462 	struct virtio_net *dev = *pdev;
2463 
2464 	if (validate_msg_fds(dev, ctx, 1) != 0)
2465 		return RTE_VHOST_MSG_RESULT_ERR;
2466 
2467 	close(ctx->fds[0]);
2468 	VHOST_CONFIG_LOG(dev->ifname, DEBUG, "not implemented.");
2469 
2470 	return RTE_VHOST_MSG_RESULT_OK;
2471 }
2472 
2473 /*
2474  * An rarp packet is constructed and broadcasted to notify switches about
2475  * the new location of the migrated VM, so that packets from outside will
2476  * not be lost after migration.
2477  *
2478  * However, we don't actually "send" a rarp packet here, instead, we set
2479  * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
2480  */
2481 static int
2482 vhost_user_send_rarp(struct virtio_net **pdev,
2483 			struct vhu_msg_context *ctx,
2484 			int main_fd __rte_unused)
2485 {
2486 	struct virtio_net *dev = *pdev;
2487 	uint8_t *mac = (uint8_t *)&ctx->msg.payload.u64;
2488 	struct rte_vdpa_device *vdpa_dev;
2489 
2490 	VHOST_CONFIG_LOG(dev->ifname, DEBUG,
2491 		"MAC: " RTE_ETHER_ADDR_PRT_FMT,
2492 		mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
2493 	memcpy(dev->mac.addr_bytes, mac, 6);
2494 
2495 	/*
2496 	 * Set the flag to inject a RARP broadcast packet at
2497 	 * rte_vhost_dequeue_burst().
2498 	 *
2499 	 * rte_memory_order_release ordering is for making sure the mac is
2500 	 * copied before the flag is set.
2501 	 */
2502 	rte_atomic_store_explicit(&dev->broadcast_rarp, 1, rte_memory_order_release);
2503 	vdpa_dev = dev->vdpa_dev;
2504 	if (vdpa_dev && vdpa_dev->ops->migration_done)
2505 		vdpa_dev->ops->migration_done(dev->vid);
2506 
2507 	return RTE_VHOST_MSG_RESULT_OK;
2508 }
2509 
2510 static int
2511 vhost_user_net_set_mtu(struct virtio_net **pdev,
2512 			struct vhu_msg_context *ctx,
2513 			int main_fd __rte_unused)
2514 {
2515 	struct virtio_net *dev = *pdev;
2516 
2517 	if (ctx->msg.payload.u64 < VIRTIO_MIN_MTU ||
2518 			ctx->msg.payload.u64 > VIRTIO_MAX_MTU) {
2519 		VHOST_CONFIG_LOG(dev->ifname, ERR,
2520 			"invalid MTU size (%"PRIu64")",
2521 			ctx->msg.payload.u64);
2522 
2523 		return RTE_VHOST_MSG_RESULT_ERR;
2524 	}
2525 
2526 	dev->mtu = ctx->msg.payload.u64;
2527 
2528 	return RTE_VHOST_MSG_RESULT_OK;
2529 }
2530 
2531 static int
2532 vhost_user_set_req_fd(struct virtio_net **pdev,
2533 			struct vhu_msg_context *ctx,
2534 			int main_fd __rte_unused)
2535 {
2536 	struct virtio_net *dev = *pdev;
2537 	int fd = ctx->fds[0];
2538 
2539 	if (validate_msg_fds(dev, ctx, 1) != 0)
2540 		return RTE_VHOST_MSG_RESULT_ERR;
2541 
2542 	if (fd < 0) {
2543 		VHOST_CONFIG_LOG(dev->ifname, ERR,
2544 			"invalid file descriptor for backend channel (%d)", fd);
2545 		return RTE_VHOST_MSG_RESULT_ERR;
2546 	}
2547 
2548 	if (dev->backend_req_fd >= 0)
2549 		close(dev->backend_req_fd);
2550 
2551 	dev->backend_req_fd = fd;
2552 
2553 	return RTE_VHOST_MSG_RESULT_OK;
2554 }
2555 
2556 static int
2557 is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2558 {
2559 	struct vhost_vring_addr *ra;
2560 	uint64_t start, end, len;
2561 
2562 	start = imsg->iova;
2563 	end = start + imsg->size;
2564 
2565 	ra = &vq->ring_addrs;
2566 	len = sizeof(struct vring_desc) * vq->size;
2567 	if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2568 		return 1;
2569 
2570 	len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
2571 	if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2572 		return 1;
2573 
2574 	len = sizeof(struct vring_used) +
2575 	       sizeof(struct vring_used_elem) * vq->size;
2576 	if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2577 		return 1;
2578 
2579 	if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2580 		len = sizeof(uint64_t);
2581 		if (ra->log_guest_addr < end &&
2582 		    (ra->log_guest_addr + len) > start)
2583 			return 1;
2584 	}
2585 
2586 	return 0;
2587 }
2588 
2589 static int
2590 is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
2591 {
2592 	struct vhost_vring_addr *ra;
2593 	uint64_t start, end, len;
2594 
2595 	start = imsg->iova;
2596 	end = start + imsg->size;
2597 
2598 	ra = &vq->ring_addrs;
2599 	len = sizeof(struct vring_packed_desc) * vq->size;
2600 	if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
2601 		return 1;
2602 
2603 	len = sizeof(struct vring_packed_desc_event);
2604 	if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
2605 		return 1;
2606 
2607 	len = sizeof(struct vring_packed_desc_event);
2608 	if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
2609 		return 1;
2610 
2611 	if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
2612 		len = sizeof(uint64_t);
2613 		if (ra->log_guest_addr < end &&
2614 		    (ra->log_guest_addr + len) > start)
2615 			return 1;
2616 	}
2617 
2618 	return 0;
2619 }
2620 
2621 static int is_vring_iotlb(struct virtio_net *dev,
2622 			  struct vhost_virtqueue *vq,
2623 			  struct vhost_iotlb_msg *imsg)
2624 {
2625 	if (vq_is_packed(dev))
2626 		return is_vring_iotlb_packed(vq, imsg);
2627 	else
2628 		return is_vring_iotlb_split(vq, imsg);
2629 }
2630 
2631 static int
2632 vhost_user_get_config(struct virtio_net **pdev,
2633 			struct vhu_msg_context *ctx,
2634 			int main_fd __rte_unused)
2635 {
2636 	struct virtio_net *dev = *pdev;
2637 	struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
2638 	int ret = 0;
2639 
2640 	if (validate_msg_fds(dev, ctx, 0) != 0)
2641 		return RTE_VHOST_MSG_RESULT_ERR;
2642 
2643 	if (!vdpa_dev) {
2644 		VHOST_CONFIG_LOG(dev->ifname, ERR, "is not vDPA device!");
2645 		return RTE_VHOST_MSG_RESULT_ERR;
2646 	}
2647 
2648 	if (vdpa_dev->ops->get_config) {
2649 		ret = vdpa_dev->ops->get_config(dev->vid,
2650 					   ctx->msg.payload.cfg.region,
2651 					   ctx->msg.payload.cfg.size);
2652 		if (ret != 0) {
2653 			ctx->msg.size = 0;
2654 			VHOST_CONFIG_LOG(dev->ifname, ERR, "get_config() return error!");
2655 		}
2656 	} else {
2657 		VHOST_CONFIG_LOG(dev->ifname, ERR, "get_config() not supported!");
2658 	}
2659 
2660 	return RTE_VHOST_MSG_RESULT_REPLY;
2661 }
2662 
2663 static int
2664 vhost_user_set_config(struct virtio_net **pdev,
2665 			struct vhu_msg_context *ctx,
2666 			int main_fd __rte_unused)
2667 {
2668 	struct virtio_net *dev = *pdev;
2669 	struct rte_vdpa_device *vdpa_dev = dev->vdpa_dev;
2670 	int ret = 0;
2671 
2672 	if (validate_msg_fds(dev, ctx, 0) != 0)
2673 		return RTE_VHOST_MSG_RESULT_ERR;
2674 
2675 	if (ctx->msg.payload.cfg.size > VHOST_USER_MAX_CONFIG_SIZE) {
2676 		VHOST_CONFIG_LOG(dev->ifname, ERR,
2677 			"vhost_user_config size: %"PRIu32", should not be larger than %d",
2678 			ctx->msg.payload.cfg.size, VHOST_USER_MAX_CONFIG_SIZE);
2679 		goto out;
2680 	}
2681 
2682 	if (!vdpa_dev) {
2683 		VHOST_CONFIG_LOG(dev->ifname, ERR, "is not vDPA device!");
2684 		goto out;
2685 	}
2686 
2687 	if (vdpa_dev->ops->set_config) {
2688 		ret = vdpa_dev->ops->set_config(dev->vid,
2689 			ctx->msg.payload.cfg.region,
2690 			ctx->msg.payload.cfg.offset,
2691 			ctx->msg.payload.cfg.size,
2692 			ctx->msg.payload.cfg.flags);
2693 		if (ret)
2694 			VHOST_CONFIG_LOG(dev->ifname, ERR, "set_config() return error!");
2695 	} else {
2696 		VHOST_CONFIG_LOG(dev->ifname, ERR, "set_config() not supported!");
2697 	}
2698 
2699 	return RTE_VHOST_MSG_RESULT_OK;
2700 
2701 out:
2702 	return RTE_VHOST_MSG_RESULT_ERR;
2703 }
2704 
2705 static int
2706 vhost_user_iotlb_msg(struct virtio_net **pdev,
2707 			struct vhu_msg_context *ctx,
2708 			int main_fd __rte_unused)
2709 {
2710 	struct virtio_net *dev = *pdev;
2711 	struct vhost_iotlb_msg *imsg = &ctx->msg.payload.iotlb;
2712 	uint16_t i;
2713 	uint64_t vva, len, pg_sz;
2714 
2715 	switch (imsg->type) {
2716 	case VHOST_IOTLB_UPDATE:
2717 		len = imsg->size;
2718 		vva = qva_to_vva(dev, imsg->uaddr, &len);
2719 		if (!vva)
2720 			return RTE_VHOST_MSG_RESULT_ERR;
2721 
2722 		pg_sz = hua_to_alignment(dev->mem, (void *)(uintptr_t)vva);
2723 
2724 		vhost_user_iotlb_cache_insert(dev, imsg->iova, vva, 0, len, pg_sz, imsg->perm);
2725 
2726 		for (i = 0; i < dev->nr_vring; i++) {
2727 			struct vhost_virtqueue *vq = dev->virtqueue[i];
2728 
2729 			if (!vq)
2730 				continue;
2731 
2732 			if (is_vring_iotlb(dev, vq, imsg)) {
2733 				rte_rwlock_write_lock(&vq->access_lock);
2734 				translate_ring_addresses(&dev, &vq);
2735 				*pdev = dev;
2736 				rte_rwlock_write_unlock(&vq->access_lock);
2737 			}
2738 		}
2739 		break;
2740 	case VHOST_IOTLB_INVALIDATE:
2741 		vhost_user_iotlb_cache_remove(dev, imsg->iova, imsg->size);
2742 
2743 		for (i = 0; i < dev->nr_vring; i++) {
2744 			struct vhost_virtqueue *vq = dev->virtqueue[i];
2745 
2746 			if (!vq)
2747 				continue;
2748 
2749 			if (is_vring_iotlb(dev, vq, imsg)) {
2750 				rte_rwlock_write_lock(&vq->access_lock);
2751 				vring_invalidate(dev, vq);
2752 				rte_rwlock_write_unlock(&vq->access_lock);
2753 			}
2754 		}
2755 		break;
2756 	default:
2757 		VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid IOTLB message type (%d)",
2758 			imsg->type);
2759 		return RTE_VHOST_MSG_RESULT_ERR;
2760 	}
2761 
2762 	return RTE_VHOST_MSG_RESULT_OK;
2763 }
2764 
2765 static int
2766 vhost_user_set_postcopy_advise(struct virtio_net **pdev,
2767 			struct vhu_msg_context *ctx,
2768 			int main_fd __rte_unused)
2769 {
2770 	struct virtio_net *dev = *pdev;
2771 #ifdef RTE_LIBRTE_VHOST_POSTCOPY
2772 	struct uffdio_api api_struct;
2773 
2774 	dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
2775 
2776 	if (dev->postcopy_ufd == -1) {
2777 		VHOST_CONFIG_LOG(dev->ifname, ERR,
2778 			"userfaultfd not available: %s",
2779 			strerror(errno));
2780 		return RTE_VHOST_MSG_RESULT_ERR;
2781 	}
2782 	api_struct.api = UFFD_API;
2783 	api_struct.features = 0;
2784 	if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
2785 		VHOST_CONFIG_LOG(dev->ifname, ERR,
2786 			"UFFDIO_API ioctl failure: %s",
2787 			strerror(errno));
2788 		close(dev->postcopy_ufd);
2789 		dev->postcopy_ufd = -1;
2790 		return RTE_VHOST_MSG_RESULT_ERR;
2791 	}
2792 	ctx->fds[0] = dev->postcopy_ufd;
2793 	ctx->fd_num = 1;
2794 
2795 	return RTE_VHOST_MSG_RESULT_REPLY;
2796 #else
2797 	dev->postcopy_ufd = -1;
2798 	ctx->fd_num = 0;
2799 
2800 	return RTE_VHOST_MSG_RESULT_ERR;
2801 #endif
2802 }
2803 
2804 static int
2805 vhost_user_set_postcopy_listen(struct virtio_net **pdev,
2806 			struct vhu_msg_context *ctx __rte_unused,
2807 			int main_fd __rte_unused)
2808 {
2809 	struct virtio_net *dev = *pdev;
2810 
2811 	if (dev->mem && dev->mem->nregions) {
2812 		VHOST_CONFIG_LOG(dev->ifname, ERR,
2813 			"regions already registered at postcopy-listen");
2814 		return RTE_VHOST_MSG_RESULT_ERR;
2815 	}
2816 	dev->postcopy_listening = 1;
2817 
2818 	return RTE_VHOST_MSG_RESULT_OK;
2819 }
2820 
2821 static int
2822 vhost_user_postcopy_end(struct virtio_net **pdev,
2823 			struct vhu_msg_context *ctx,
2824 			int main_fd __rte_unused)
2825 {
2826 	struct virtio_net *dev = *pdev;
2827 
2828 	dev->postcopy_listening = 0;
2829 	if (dev->postcopy_ufd >= 0) {
2830 		close(dev->postcopy_ufd);
2831 		dev->postcopy_ufd = -1;
2832 	}
2833 
2834 	ctx->msg.payload.u64 = 0;
2835 	ctx->msg.size = sizeof(ctx->msg.payload.u64);
2836 	ctx->fd_num = 0;
2837 
2838 	return RTE_VHOST_MSG_RESULT_REPLY;
2839 }
2840 
2841 static int
2842 vhost_user_get_status(struct virtio_net **pdev,
2843 		      struct vhu_msg_context *ctx,
2844 		      int main_fd __rte_unused)
2845 {
2846 	struct virtio_net *dev = *pdev;
2847 
2848 	ctx->msg.payload.u64 = dev->status;
2849 	ctx->msg.size = sizeof(ctx->msg.payload.u64);
2850 	ctx->fd_num = 0;
2851 
2852 	return RTE_VHOST_MSG_RESULT_REPLY;
2853 }
2854 
2855 static int
2856 vhost_user_set_status(struct virtio_net **pdev,
2857 			struct vhu_msg_context *ctx,
2858 			int main_fd __rte_unused)
2859 {
2860 	struct virtio_net *dev = *pdev;
2861 
2862 	/* As per Virtio specification, the device status is 8bits long */
2863 	if (ctx->msg.payload.u64 > UINT8_MAX) {
2864 		VHOST_CONFIG_LOG(dev->ifname, ERR,
2865 			"invalid VHOST_USER_SET_STATUS payload 0x%" PRIx64,
2866 			ctx->msg.payload.u64);
2867 		return RTE_VHOST_MSG_RESULT_ERR;
2868 	}
2869 
2870 	dev->status = ctx->msg.payload.u64;
2871 
2872 	if ((dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK) &&
2873 	    (dev->flags & VIRTIO_DEV_FEATURES_FAILED)) {
2874 		VHOST_CONFIG_LOG(dev->ifname, ERR,
2875 			"FEATURES_OK bit is set but feature negotiation failed");
2876 		/*
2877 		 * Clear the bit to let the driver know about the feature
2878 		 * negotiation failure
2879 		 */
2880 		dev->status &= ~VIRTIO_DEVICE_STATUS_FEATURES_OK;
2881 	}
2882 
2883 	VHOST_CONFIG_LOG(dev->ifname, INFO, "new device status(0x%08x):", dev->status);
2884 	VHOST_CONFIG_LOG(dev->ifname, INFO,
2885 		"\t-RESET: %u",
2886 		(dev->status == VIRTIO_DEVICE_STATUS_RESET));
2887 	VHOST_CONFIG_LOG(dev->ifname, INFO,
2888 		"\t-ACKNOWLEDGE: %u",
2889 		!!(dev->status & VIRTIO_DEVICE_STATUS_ACK));
2890 	VHOST_CONFIG_LOG(dev->ifname, INFO,
2891 		"\t-DRIVER: %u",
2892 		!!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER));
2893 	VHOST_CONFIG_LOG(dev->ifname, INFO,
2894 		"\t-FEATURES_OK: %u",
2895 		!!(dev->status & VIRTIO_DEVICE_STATUS_FEATURES_OK));
2896 	VHOST_CONFIG_LOG(dev->ifname, INFO,
2897 		"\t-DRIVER_OK: %u",
2898 		!!(dev->status & VIRTIO_DEVICE_STATUS_DRIVER_OK));
2899 	VHOST_CONFIG_LOG(dev->ifname, INFO,
2900 		"\t-DEVICE_NEED_RESET: %u",
2901 		!!(dev->status & VIRTIO_DEVICE_STATUS_DEV_NEED_RESET));
2902 	VHOST_CONFIG_LOG(dev->ifname, INFO,
2903 		"\t-FAILED: %u",
2904 		!!(dev->status & VIRTIO_DEVICE_STATUS_FAILED));
2905 
2906 	return RTE_VHOST_MSG_RESULT_OK;
2907 }
2908 
2909 #define VHOST_MESSAGE_HANDLER(id, handler, accepts_fd, lock_all_qps) \
2910 	[id] = { #id, handler, accepts_fd, id ## _LOCK_ALL_QPS },
2911 static vhost_message_handler_t vhost_message_handlers[] = {
2912 	VHOST_MESSAGE_HANDLERS
2913 };
2914 #undef VHOST_MESSAGE_HANDLER
2915 
2916 /* return bytes# of read on success or negative val on failure. */
2917 static int
2918 read_vhost_message(struct virtio_net *dev, int sockfd, struct  vhu_msg_context *ctx)
2919 {
2920 	int ret;
2921 
2922 	ret = read_fd_message(dev->ifname, sockfd, (char *)&ctx->msg, VHOST_USER_HDR_SIZE,
2923 		ctx->fds, VHOST_MEMORY_MAX_NREGIONS, &ctx->fd_num);
2924 	if (ret <= 0)
2925 		goto out;
2926 
2927 	if (ret != VHOST_USER_HDR_SIZE) {
2928 		VHOST_CONFIG_LOG(dev->ifname, ERR, "Unexpected header size read");
2929 		ret = -1;
2930 		goto out;
2931 	}
2932 
2933 	if (ctx->msg.size) {
2934 		if (ctx->msg.size > sizeof(ctx->msg.payload)) {
2935 			VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid msg size: %d",
2936 				ctx->msg.size);
2937 			ret = -1;
2938 			goto out;
2939 		}
2940 		ret = read(sockfd, &ctx->msg.payload, ctx->msg.size);
2941 		if (ret <= 0)
2942 			goto out;
2943 		if (ret != (int)ctx->msg.size) {
2944 			VHOST_CONFIG_LOG(dev->ifname, ERR, "read control message failed");
2945 			ret = -1;
2946 			goto out;
2947 		}
2948 	}
2949 
2950 out:
2951 	if (ret <= 0)
2952 		close_msg_fds(ctx);
2953 
2954 	return ret;
2955 }
2956 
2957 static int
2958 send_vhost_message(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2959 {
2960 	if (!ctx)
2961 		return 0;
2962 
2963 	return send_fd_message(dev->ifname, sockfd, (char *)&ctx->msg,
2964 		VHOST_USER_HDR_SIZE + ctx->msg.size, ctx->fds, ctx->fd_num);
2965 }
2966 
2967 static int
2968 send_vhost_reply(struct virtio_net *dev, int sockfd, struct vhu_msg_context *ctx)
2969 {
2970 	if (!ctx)
2971 		return 0;
2972 
2973 	ctx->msg.flags &= ~VHOST_USER_VERSION_MASK;
2974 	ctx->msg.flags &= ~VHOST_USER_NEED_REPLY;
2975 	ctx->msg.flags |= VHOST_USER_VERSION;
2976 	ctx->msg.flags |= VHOST_USER_REPLY_MASK;
2977 
2978 	return send_vhost_message(dev, sockfd, ctx);
2979 }
2980 
2981 static int
2982 send_vhost_backend_message(struct virtio_net *dev, struct vhu_msg_context *ctx)
2983 {
2984 	return send_vhost_message(dev, dev->backend_req_fd, ctx);
2985 }
2986 
2987 static int
2988 send_vhost_backend_message_process_reply(struct virtio_net *dev, struct vhu_msg_context *ctx)
2989 {
2990 	struct vhu_msg_context msg_reply;
2991 	int ret;
2992 
2993 	rte_spinlock_lock(&dev->backend_req_lock);
2994 	ret = send_vhost_backend_message(dev, ctx);
2995 	if (ret < 0) {
2996 		VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to send config change (%d)", ret);
2997 		goto out;
2998 	}
2999 
3000 	ret = read_vhost_message(dev, dev->backend_req_fd, &msg_reply);
3001 	if (ret <= 0) {
3002 		if (ret < 0)
3003 			VHOST_CONFIG_LOG(dev->ifname, ERR,
3004 				"vhost read backend message reply failed");
3005 		else
3006 			VHOST_CONFIG_LOG(dev->ifname, INFO, "vhost peer closed");
3007 		ret = -1;
3008 		goto out;
3009 	}
3010 
3011 	if (msg_reply.msg.request.backend != ctx->msg.request.backend) {
3012 		VHOST_CONFIG_LOG(dev->ifname, ERR,
3013 			"received unexpected msg type (%u), expected %u",
3014 			msg_reply.msg.request.backend, ctx->msg.request.backend);
3015 		ret = -1;
3016 		goto out;
3017 	}
3018 
3019 	ret = msg_reply.msg.payload.u64 ? -1 : 0;
3020 out:
3021 	rte_spinlock_unlock(&dev->backend_req_lock);
3022 	return ret;
3023 }
3024 
3025 /*
3026  * Allocate a queue pair if it hasn't been allocated yet
3027  */
3028 static int
3029 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev,
3030 			struct vhu_msg_context *ctx)
3031 {
3032 	uint32_t vring_idx;
3033 
3034 	switch (ctx->msg.request.frontend) {
3035 	case VHOST_USER_SET_VRING_KICK:
3036 	case VHOST_USER_SET_VRING_CALL:
3037 	case VHOST_USER_SET_VRING_ERR:
3038 		vring_idx = ctx->msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
3039 		break;
3040 	case VHOST_USER_SET_VRING_NUM:
3041 	case VHOST_USER_SET_VRING_BASE:
3042 	case VHOST_USER_GET_VRING_BASE:
3043 	case VHOST_USER_SET_VRING_ENABLE:
3044 		vring_idx = ctx->msg.payload.state.index;
3045 		break;
3046 	case VHOST_USER_SET_VRING_ADDR:
3047 		vring_idx = ctx->msg.payload.addr.index;
3048 		break;
3049 	case VHOST_USER_SET_INFLIGHT_FD:
3050 		vring_idx = ctx->msg.payload.inflight.num_queues - 1;
3051 		break;
3052 	default:
3053 		return 0;
3054 	}
3055 
3056 	if (vring_idx >= VHOST_MAX_VRING) {
3057 		VHOST_CONFIG_LOG(dev->ifname, ERR, "invalid vring index: %u", vring_idx);
3058 		return -1;
3059 	}
3060 
3061 	if (dev->virtqueue[vring_idx])
3062 		return 0;
3063 
3064 	return alloc_vring_queue(dev, vring_idx);
3065 }
3066 
3067 static void
3068 vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
3069 	__rte_no_thread_safety_analysis
3070 {
3071 	unsigned int i = 0;
3072 	unsigned int vq_num = 0;
3073 
3074 	while (vq_num < dev->nr_vring) {
3075 		struct vhost_virtqueue *vq = dev->virtqueue[i];
3076 
3077 		if (vq) {
3078 			rte_rwlock_write_lock(&vq->access_lock);
3079 			vq_num++;
3080 		}
3081 		i++;
3082 	}
3083 }
3084 
3085 static void
3086 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
3087 	__rte_no_thread_safety_analysis
3088 {
3089 	unsigned int i = 0;
3090 	unsigned int vq_num = 0;
3091 
3092 	while (vq_num < dev->nr_vring) {
3093 		struct vhost_virtqueue *vq = dev->virtqueue[i];
3094 
3095 		if (vq) {
3096 			rte_rwlock_write_unlock(&vq->access_lock);
3097 			vq_num++;
3098 		}
3099 		i++;
3100 	}
3101 }
3102 
3103 int
3104 vhost_user_msg_handler(int vid, int fd)
3105 {
3106 	struct virtio_net *dev;
3107 	struct vhu_msg_context ctx;
3108 	vhost_message_handler_t *msg_handler;
3109 	struct rte_vdpa_device *vdpa_dev;
3110 	int msg_result = RTE_VHOST_MSG_RESULT_OK;
3111 	int ret;
3112 	int unlock_required = 0;
3113 	bool handled;
3114 	uint32_t request;
3115 	uint32_t i;
3116 	uint16_t blk_call_fd;
3117 
3118 	dev = get_device(vid);
3119 	if (dev == NULL)
3120 		return -1;
3121 
3122 	if (!dev->notify_ops) {
3123 		dev->notify_ops = vhost_driver_callback_get(dev->ifname);
3124 		if (!dev->notify_ops) {
3125 			VHOST_CONFIG_LOG(dev->ifname, ERR,
3126 				"failed to get callback ops for driver");
3127 			return -1;
3128 		}
3129 	}
3130 
3131 	ctx.msg.request.frontend = VHOST_USER_NONE;
3132 	ret = read_vhost_message(dev, fd, &ctx);
3133 	if (ret == 0) {
3134 		VHOST_CONFIG_LOG(dev->ifname, INFO, "vhost peer closed");
3135 		return -1;
3136 	}
3137 
3138 	request = ctx.msg.request.frontend;
3139 	if (request > VHOST_USER_NONE && request < RTE_DIM(vhost_message_handlers))
3140 		msg_handler = &vhost_message_handlers[request];
3141 	else
3142 		msg_handler = NULL;
3143 
3144 	if (ret < 0) {
3145 		VHOST_CONFIG_LOG(dev->ifname, ERR, "vhost read message %s%s%sfailed",
3146 				msg_handler != NULL ? "for " : "",
3147 				msg_handler != NULL ? msg_handler->description : "",
3148 				msg_handler != NULL ? " " : "");
3149 		return -1;
3150 	}
3151 
3152 	if (msg_handler != NULL && msg_handler->description != NULL) {
3153 		if (request != VHOST_USER_IOTLB_MSG)
3154 			VHOST_CONFIG_LOG(dev->ifname, INFO,
3155 				"read message %s",
3156 				msg_handler->description);
3157 		else
3158 			VHOST_CONFIG_LOG(dev->ifname, DEBUG,
3159 				"read message %s",
3160 				msg_handler->description);
3161 	} else {
3162 		VHOST_CONFIG_LOG(dev->ifname, DEBUG, "external request %d", request);
3163 	}
3164 
3165 	ret = vhost_user_check_and_alloc_queue_pair(dev, &ctx);
3166 	if (ret < 0) {
3167 		VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to alloc queue");
3168 		return -1;
3169 	}
3170 
3171 	/*
3172 	 * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
3173 	 * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
3174 	 * and device is destroyed. destroy_device waits for queues to be
3175 	 * inactive, so it is safe. Otherwise taking the access_lock
3176 	 * would cause a dead lock.
3177 	 */
3178 	if (msg_handler != NULL && msg_handler->lock_all_qps) {
3179 		if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3180 			vhost_user_lock_all_queue_pairs(dev);
3181 			unlock_required = 1;
3182 		}
3183 	}
3184 
3185 	handled = false;
3186 	if (dev->extern_ops.pre_msg_handle) {
3187 		RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
3188 		msg_result = (*dev->extern_ops.pre_msg_handle)(dev->vid, &ctx);
3189 		switch (msg_result) {
3190 		case RTE_VHOST_MSG_RESULT_REPLY:
3191 			send_vhost_reply(dev, fd, &ctx);
3192 			/* Fall-through */
3193 		case RTE_VHOST_MSG_RESULT_ERR:
3194 		case RTE_VHOST_MSG_RESULT_OK:
3195 			handled = true;
3196 			goto skip_to_post_handle;
3197 		case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3198 		default:
3199 			break;
3200 		}
3201 	}
3202 
3203 	if (msg_handler == NULL || msg_handler->callback == NULL)
3204 		goto skip_to_post_handle;
3205 
3206 	if (!msg_handler->accepts_fd && validate_msg_fds(dev, &ctx, 0) != 0) {
3207 		msg_result = RTE_VHOST_MSG_RESULT_ERR;
3208 	} else {
3209 		msg_result = msg_handler->callback(&dev, &ctx, fd);
3210 	}
3211 
3212 	switch (msg_result) {
3213 	case RTE_VHOST_MSG_RESULT_ERR:
3214 		VHOST_CONFIG_LOG(dev->ifname, ERR,
3215 			"processing %s failed.",
3216 			msg_handler->description);
3217 		handled = true;
3218 		break;
3219 	case RTE_VHOST_MSG_RESULT_OK:
3220 		VHOST_CONFIG_LOG(dev->ifname, DEBUG,
3221 			"processing %s succeeded.",
3222 			msg_handler->description);
3223 		handled = true;
3224 		break;
3225 	case RTE_VHOST_MSG_RESULT_REPLY:
3226 		VHOST_CONFIG_LOG(dev->ifname, DEBUG,
3227 			"processing %s succeeded and needs reply.",
3228 			msg_handler->description);
3229 		send_vhost_reply(dev, fd, &ctx);
3230 		handled = true;
3231 		break;
3232 	default:
3233 		break;
3234 	}
3235 
3236 skip_to_post_handle:
3237 	if (msg_result != RTE_VHOST_MSG_RESULT_ERR &&
3238 			dev->extern_ops.post_msg_handle) {
3239 		RTE_BUILD_BUG_ON(offsetof(struct vhu_msg_context, msg) != 0);
3240 		msg_result = (*dev->extern_ops.post_msg_handle)(dev->vid, &ctx);
3241 		switch (msg_result) {
3242 		case RTE_VHOST_MSG_RESULT_REPLY:
3243 			send_vhost_reply(dev, fd, &ctx);
3244 			/* Fall-through */
3245 		case RTE_VHOST_MSG_RESULT_ERR:
3246 		case RTE_VHOST_MSG_RESULT_OK:
3247 			handled = true;
3248 		case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
3249 		default:
3250 			break;
3251 		}
3252 	}
3253 
3254 	/* If message was not handled at this stage, treat it as an error */
3255 	if (!handled) {
3256 		VHOST_CONFIG_LOG(dev->ifname, ERR,
3257 			"vhost message (req: %d) was not handled.",
3258 			request);
3259 		close_msg_fds(&ctx);
3260 		msg_result = RTE_VHOST_MSG_RESULT_ERR;
3261 	}
3262 
3263 	/*
3264 	 * If the request required a reply that was already sent,
3265 	 * this optional reply-ack won't be sent as the
3266 	 * VHOST_USER_NEED_REPLY was cleared in send_vhost_reply().
3267 	 */
3268 	if (ctx.msg.flags & VHOST_USER_NEED_REPLY) {
3269 		ctx.msg.payload.u64 = msg_result == RTE_VHOST_MSG_RESULT_ERR;
3270 		ctx.msg.size = sizeof(ctx.msg.payload.u64);
3271 		ctx.fd_num = 0;
3272 		send_vhost_reply(dev, fd, &ctx);
3273 	} else if (msg_result == RTE_VHOST_MSG_RESULT_ERR) {
3274 		VHOST_CONFIG_LOG(dev->ifname, ERR, "vhost message handling failed.");
3275 		ret = -1;
3276 		goto unlock;
3277 	}
3278 
3279 	for (i = 0; i < dev->nr_vring; i++) {
3280 		struct vhost_virtqueue *vq = dev->virtqueue[i];
3281 		bool cur_ready = vq_is_ready(dev, vq);
3282 
3283 		if (cur_ready != (vq && vq->ready)) {
3284 			vq->ready = cur_ready;
3285 			vhost_user_notify_queue_state(dev, vq, cur_ready);
3286 		}
3287 	}
3288 
3289 unlock:
3290 	if (unlock_required)
3291 		vhost_user_unlock_all_queue_pairs(dev);
3292 
3293 	if (ret != 0 || !virtio_is_ready(dev))
3294 		goto out;
3295 
3296 	/*
3297 	 * Virtio is now ready. If not done already, it is time
3298 	 * to notify the application it can process the rings and
3299 	 * configure the vDPA device if present.
3300 	 */
3301 
3302 	if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
3303 		if (dev->notify_ops->new_device(dev->vid) == 0)
3304 			dev->flags |= VIRTIO_DEV_RUNNING;
3305 	}
3306 
3307 	vdpa_dev = dev->vdpa_dev;
3308 	if (!vdpa_dev)
3309 		goto out;
3310 
3311 	if (vdpa_dev->type == RTE_VHOST_VDPA_DEVICE_TYPE_BLK) {
3312 		if (request == VHOST_USER_SET_VRING_CALL) {
3313 			blk_call_fd = ctx.msg.payload.u64 & VHOST_USER_VRING_IDX_MASK;
3314 			if (blk_call_fd != dev->nr_vring - 1)
3315 				goto out;
3316 		} else {
3317 			goto out;
3318 		}
3319 	}
3320 
3321 	if (!(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED)) {
3322 		if (vdpa_dev->ops->dev_conf(dev->vid))
3323 			VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to configure vDPA device");
3324 		else
3325 			dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
3326 	}
3327 
3328 out:
3329 	return ret;
3330 }
3331 
3332 static int
3333 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
3334 {
3335 	int ret;
3336 	struct vhu_msg_context ctx = {
3337 		.msg = {
3338 			.request.backend = VHOST_USER_BACKEND_IOTLB_MSG,
3339 			.flags = VHOST_USER_VERSION,
3340 			.size = sizeof(ctx.msg.payload.iotlb),
3341 			.payload.iotlb = {
3342 				.iova = iova,
3343 				.perm = perm,
3344 				.type = VHOST_IOTLB_MISS,
3345 			},
3346 		},
3347 	};
3348 
3349 	ret = send_vhost_message(dev, dev->backend_req_fd, &ctx);
3350 	if (ret < 0) {
3351 		VHOST_CONFIG_LOG(dev->ifname, ERR,
3352 			"failed to send IOTLB miss message (%d)",
3353 			ret);
3354 		return ret;
3355 	}
3356 
3357 	return 0;
3358 }
3359 
3360 int
3361 rte_vhost_backend_config_change(int vid, bool need_reply)
3362 {
3363 	struct vhu_msg_context ctx = {
3364 		.msg = {
3365 			.request.backend = VHOST_USER_BACKEND_CONFIG_CHANGE_MSG,
3366 			.flags = VHOST_USER_VERSION,
3367 			.size = 0,
3368 		}
3369 	};
3370 	struct virtio_net *dev;
3371 	int ret;
3372 
3373 	dev = get_device(vid);
3374 	if (!dev)
3375 		return -ENODEV;
3376 
3377 	if (!need_reply) {
3378 		ret = send_vhost_backend_message(dev, &ctx);
3379 	} else {
3380 		ctx.msg.flags |= VHOST_USER_NEED_REPLY;
3381 		ret = send_vhost_backend_message_process_reply(dev, &ctx);
3382 	}
3383 
3384 	if (ret < 0)
3385 		VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to send config change (%d)", ret);
3386 	return ret;
3387 }
3388 
3389 static int vhost_user_backend_set_vring_host_notifier(struct virtio_net *dev,
3390 						    int index, int fd,
3391 						    uint64_t offset,
3392 						    uint64_t size)
3393 {
3394 	int ret;
3395 	struct vhu_msg_context ctx = {
3396 		.msg = {
3397 			.request.backend = VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG,
3398 			.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
3399 			.size = sizeof(ctx.msg.payload.area),
3400 			.payload.area = {
3401 				.u64 = index & VHOST_USER_VRING_IDX_MASK,
3402 				.size = size,
3403 				.offset = offset,
3404 			},
3405 		},
3406 	};
3407 
3408 	if (fd < 0)
3409 		ctx.msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
3410 	else {
3411 		ctx.fds[0] = fd;
3412 		ctx.fd_num = 1;
3413 	}
3414 
3415 	ret = send_vhost_backend_message_process_reply(dev, &ctx);
3416 	if (ret < 0)
3417 		VHOST_CONFIG_LOG(dev->ifname, ERR, "failed to set host notifier (%d)", ret);
3418 
3419 	return ret;
3420 }
3421 
3422 int rte_vhost_host_notifier_ctrl(int vid, uint16_t qid, bool enable)
3423 {
3424 	struct virtio_net *dev;
3425 	struct rte_vdpa_device *vdpa_dev;
3426 	int vfio_device_fd, ret = 0;
3427 	uint64_t offset, size;
3428 	unsigned int i, q_start, q_last;
3429 
3430 	dev = get_device(vid);
3431 	if (!dev)
3432 		return -ENODEV;
3433 
3434 	vdpa_dev = dev->vdpa_dev;
3435 	if (vdpa_dev == NULL)
3436 		return -ENODEV;
3437 
3438 	if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
3439 	    !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
3440 	    !(dev->protocol_features &
3441 			(1ULL << VHOST_USER_PROTOCOL_F_BACKEND_REQ)) ||
3442 	    !(dev->protocol_features &
3443 			(1ULL << VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD)) ||
3444 	    !(dev->protocol_features &
3445 			(1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
3446 		return -ENOTSUP;
3447 
3448 	if (qid == RTE_VHOST_QUEUE_ALL) {
3449 		q_start = 0;
3450 		q_last = dev->nr_vring - 1;
3451 	} else {
3452 		if (qid >= dev->nr_vring)
3453 			return -EINVAL;
3454 		q_start = qid;
3455 		q_last = qid;
3456 	}
3457 
3458 	if (vdpa_dev->ops->get_vfio_device_fd == NULL)
3459 		return -ENOTSUP;
3460 	if (vdpa_dev->ops->get_notify_area == NULL)
3461 		return -ENOTSUP;
3462 
3463 	vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
3464 	if (vfio_device_fd < 0)
3465 		return -ENOTSUP;
3466 
3467 	if (enable) {
3468 		for (i = q_start; i <= q_last; i++) {
3469 			if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
3470 					&size) < 0) {
3471 				ret = -ENOTSUP;
3472 				goto disable;
3473 			}
3474 
3475 			if (vhost_user_backend_set_vring_host_notifier(dev, i,
3476 					vfio_device_fd, offset, size) < 0) {
3477 				ret = -EFAULT;
3478 				goto disable;
3479 			}
3480 		}
3481 	} else {
3482 disable:
3483 		for (i = q_start; i <= q_last; i++) {
3484 			vhost_user_backend_set_vring_host_notifier(dev, i, -1,
3485 					0, 0);
3486 		}
3487 	}
3488 
3489 	return ret;
3490 }
3491 
3492 static int
3493 vhost_user_inject_irq(struct virtio_net *dev __rte_unused, struct vhost_virtqueue *vq)
3494 {
3495 	if (vq->callfd < 0)
3496 		return -1;
3497 
3498 	return eventfd_write(vq->callfd, (eventfd_t)1);
3499 }
3500 
3501 static struct vhost_backend_ops vhost_user_backend_ops = {
3502 	.iotlb_miss = vhost_user_iotlb_miss,
3503 	.inject_irq = vhost_user_inject_irq,
3504 };
3505 
3506 int
3507 vhost_user_new_device(void)
3508 {
3509 	return vhost_new_device(&vhost_user_backend_ops);
3510 }
3511