xref: /dpdk/lib/vhost/vhost.h (revision a01070da)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4 
5 #ifndef _VHOST_NET_CDEV_H_
6 #define _VHOST_NET_CDEV_H_
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdbool.h>
10 #include <sys/types.h>
11 #include <sys/queue.h>
12 #include <unistd.h>
13 #include <linux/vhost.h>
14 #include <linux/virtio_net.h>
15 #include <sys/socket.h>
16 #include <linux/if.h>
17 
18 #include <rte_log.h>
19 #include <rte_ether.h>
20 #include <rte_rwlock.h>
21 #include <rte_malloc.h>
22 
23 #include "rte_vhost.h"
24 #include "rte_vdpa.h"
25 #include "vdpa_driver.h"
26 
27 #include "rte_vhost_async.h"
28 
29 /* Used to indicate that the device is running on a data core */
30 #define VIRTIO_DEV_RUNNING ((uint32_t)1 << 0)
31 /* Used to indicate that the device is ready to operate */
32 #define VIRTIO_DEV_READY ((uint32_t)1 << 1)
33 /* Used to indicate that the built-in vhost net device backend is enabled */
34 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET ((uint32_t)1 << 2)
35 /* Used to indicate that the device has its own data path and configured */
36 #define VIRTIO_DEV_VDPA_CONFIGURED ((uint32_t)1 << 3)
37 /* Used to indicate that the feature negotiation failed */
38 #define VIRTIO_DEV_FEATURES_FAILED ((uint32_t)1 << 4)
39 /* Used to indicate that the virtio_net tx code should fill TX ol_flags */
40 #define VIRTIO_DEV_LEGACY_OL_FLAGS ((uint32_t)1 << 5)
41 
42 /* Backend value set by guest. */
43 #define VIRTIO_DEV_STOPPED -1
44 
45 #define BUF_VECTOR_MAX 256
46 
47 #define VHOST_LOG_CACHE_NR 32
48 
49 #define MAX_PKT_BURST 32
50 
51 #define VHOST_MAX_ASYNC_IT (MAX_PKT_BURST)
52 #define VHOST_MAX_ASYNC_VEC 2048
53 
54 #define PACKED_DESC_ENQUEUE_USED_FLAG(w)	\
55 	((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \
56 		VRING_DESC_F_WRITE)
57 #define PACKED_DESC_DEQUEUE_USED_FLAG(w)	\
58 	((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED) : 0x0)
59 #define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \
60 					 VRING_DESC_F_INDIRECT)
61 
62 #define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \
63 			    sizeof(struct vring_packed_desc))
64 #define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
65 
66 #ifdef VHOST_GCC_UNROLL_PRAGMA
67 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \
68 	for (iter = val; iter < size; iter++)
69 #endif
70 
71 #ifdef VHOST_CLANG_UNROLL_PRAGMA
72 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \
73 	for (iter = val; iter < size; iter++)
74 #endif
75 
76 #ifdef VHOST_ICC_UNROLL_PRAGMA
77 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \
78 	for (iter = val; iter < size; iter++)
79 #endif
80 
81 #ifndef vhost_for_each_try_unroll
82 #define vhost_for_each_try_unroll(iter, val, num) \
83 	for (iter = val; iter < num; iter++)
84 #endif
85 
86 /**
87  * Structure contains buffer address, length and descriptor index
88  * from vring to do scatter RX.
89  */
90 struct buf_vector {
91 	uint64_t buf_iova;
92 	uint64_t buf_addr;
93 	uint32_t buf_len;
94 	uint32_t desc_idx;
95 };
96 
97 /*
98  * Structure contains the info for each batched memory copy.
99  */
100 struct batch_copy_elem {
101 	void *dst;
102 	void *src;
103 	uint32_t len;
104 	uint64_t log_addr;
105 };
106 
107 /*
108  * Structure that contains the info for batched dirty logging.
109  */
110 struct log_cache_entry {
111 	uint32_t offset;
112 	unsigned long val;
113 };
114 
115 struct vring_used_elem_packed {
116 	uint16_t id;
117 	uint16_t flags;
118 	uint32_t len;
119 	uint32_t count;
120 };
121 
122 /**
123  * inflight async packet information
124  */
125 struct async_inflight_info {
126 	struct rte_mbuf *mbuf;
127 	uint16_t descs; /* num of descs inflight */
128 	uint16_t nr_buffers; /* num of buffers inflight for packed ring */
129 };
130 
131 struct vhost_async {
132 	/* operation callbacks for DMA */
133 	struct rte_vhost_async_channel_ops ops;
134 
135 	struct rte_vhost_iov_iter iov_iter[VHOST_MAX_ASYNC_IT];
136 	struct rte_vhost_iovec iovec[VHOST_MAX_ASYNC_VEC];
137 	uint16_t iter_idx;
138 	uint16_t iovec_idx;
139 
140 	/* data transfer status */
141 	struct async_inflight_info *pkts_info;
142 	uint16_t pkts_idx;
143 	uint16_t pkts_inflight_n;
144 	union {
145 		struct vring_used_elem  *descs_split;
146 		struct vring_used_elem_packed *buffers_packed;
147 	};
148 	union {
149 		uint16_t desc_idx_split;
150 		uint16_t buffer_idx_packed;
151 	};
152 	union {
153 		uint16_t last_desc_idx_split;
154 		uint16_t last_buffer_idx_packed;
155 	};
156 };
157 
158 /**
159  * Structure contains variables relevant to RX/TX virtqueues.
160  */
161 struct vhost_virtqueue {
162 	union {
163 		struct vring_desc	*desc;
164 		struct vring_packed_desc   *desc_packed;
165 	};
166 	union {
167 		struct vring_avail	*avail;
168 		struct vring_packed_desc_event *driver_event;
169 	};
170 	union {
171 		struct vring_used	*used;
172 		struct vring_packed_desc_event *device_event;
173 	};
174 	uint16_t		size;
175 
176 	uint16_t		last_avail_idx;
177 	uint16_t		last_used_idx;
178 	/* Last used index we notify to front end. */
179 	uint16_t		signalled_used;
180 	bool			signalled_used_valid;
181 #define VIRTIO_INVALID_EVENTFD		(-1)
182 #define VIRTIO_UNINITIALIZED_EVENTFD	(-2)
183 
184 	bool			enabled;
185 	bool			access_ok;
186 	bool			ready;
187 
188 	rte_spinlock_t		access_lock;
189 
190 
191 	union {
192 		struct vring_used_elem  *shadow_used_split;
193 		struct vring_used_elem_packed *shadow_used_packed;
194 	};
195 	uint16_t                shadow_used_idx;
196 	/* Record packed ring enqueue latest desc cache aligned index */
197 	uint16_t		shadow_aligned_idx;
198 	/* Record packed ring first dequeue desc index */
199 	uint16_t		shadow_last_used_idx;
200 
201 	uint16_t		batch_copy_nb_elems;
202 	struct batch_copy_elem	*batch_copy_elems;
203 	int			numa_node;
204 	bool			used_wrap_counter;
205 	bool			avail_wrap_counter;
206 
207 	/* Physical address of used ring, for logging */
208 	uint16_t		log_cache_nb_elem;
209 	uint64_t		log_guest_addr;
210 	struct log_cache_entry	*log_cache;
211 
212 	rte_rwlock_t	iotlb_lock;
213 	rte_rwlock_t	iotlb_pending_lock;
214 	struct rte_mempool *iotlb_pool;
215 	TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
216 	TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
217 	int				iotlb_cache_nr;
218 
219 	/* Used to notify the guest (trigger interrupt) */
220 	int			callfd;
221 	/* Currently unused as polling mode is enabled */
222 	int			kickfd;
223 
224 	/* inflight share memory info */
225 	union {
226 		struct rte_vhost_inflight_info_split *inflight_split;
227 		struct rte_vhost_inflight_info_packed *inflight_packed;
228 	};
229 	struct rte_vhost_resubmit_info *resubmit_inflight;
230 	uint64_t		global_counter;
231 
232 	struct vhost_async	*async;
233 
234 	int			notif_enable;
235 #define VIRTIO_UNINITIALIZED_NOTIF	(-1)
236 
237 	struct vhost_vring_addr ring_addrs;
238 } __rte_cache_aligned;
239 
240 /* Virtio device status as per Virtio specification */
241 #define VIRTIO_DEVICE_STATUS_RESET		0x00
242 #define VIRTIO_DEVICE_STATUS_ACK		0x01
243 #define VIRTIO_DEVICE_STATUS_DRIVER		0x02
244 #define VIRTIO_DEVICE_STATUS_DRIVER_OK		0x04
245 #define VIRTIO_DEVICE_STATUS_FEATURES_OK	0x08
246 #define VIRTIO_DEVICE_STATUS_DEV_NEED_RESET	0x40
247 #define VIRTIO_DEVICE_STATUS_FAILED		0x80
248 
249 #define VHOST_MAX_VRING			0x100
250 #define VHOST_MAX_QUEUE_PAIRS		0x80
251 
252 /* Declare IOMMU related bits for older kernels */
253 #ifndef VIRTIO_F_IOMMU_PLATFORM
254 
255 #define VIRTIO_F_IOMMU_PLATFORM 33
256 
257 struct vhost_iotlb_msg {
258 	__u64 iova;
259 	__u64 size;
260 	__u64 uaddr;
261 #define VHOST_ACCESS_RO      0x1
262 #define VHOST_ACCESS_WO      0x2
263 #define VHOST_ACCESS_RW      0x3
264 	__u8 perm;
265 #define VHOST_IOTLB_MISS           1
266 #define VHOST_IOTLB_UPDATE         2
267 #define VHOST_IOTLB_INVALIDATE     3
268 #define VHOST_IOTLB_ACCESS_FAIL    4
269 	__u8 type;
270 };
271 
272 #define VHOST_IOTLB_MSG 0x1
273 
274 struct vhost_msg {
275 	int type;
276 	union {
277 		struct vhost_iotlb_msg iotlb;
278 		__u8 padding[64];
279 	};
280 };
281 #endif
282 
283 /*
284  * Define virtio 1.0 for older kernels
285  */
286 #ifndef VIRTIO_F_VERSION_1
287  #define VIRTIO_F_VERSION_1 32
288 #endif
289 
290 /* Declare packed ring related bits for older kernels */
291 #ifndef VIRTIO_F_RING_PACKED
292 
293 #define VIRTIO_F_RING_PACKED 34
294 
295 struct vring_packed_desc {
296 	uint64_t addr;
297 	uint32_t len;
298 	uint16_t id;
299 	uint16_t flags;
300 };
301 
302 struct vring_packed_desc_event {
303 	uint16_t off_wrap;
304 	uint16_t flags;
305 };
306 #endif
307 
308 /*
309  * Declare below packed ring defines unconditionally
310  * as Kernel header might use different names.
311  */
312 #define VRING_DESC_F_AVAIL	(1ULL << 7)
313 #define VRING_DESC_F_USED	(1ULL << 15)
314 
315 #define VRING_EVENT_F_ENABLE 0x0
316 #define VRING_EVENT_F_DISABLE 0x1
317 #define VRING_EVENT_F_DESC 0x2
318 
319 /*
320  * Available and used descs are in same order
321  */
322 #ifndef VIRTIO_F_IN_ORDER
323 #define VIRTIO_F_IN_ORDER      35
324 #endif
325 
326 /* Features supported by this builtin vhost-user net driver. */
327 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
328 				(1ULL << VIRTIO_F_ANY_LAYOUT) | \
329 				(1ULL << VIRTIO_NET_F_CTRL_VQ) | \
330 				(1ULL << VIRTIO_NET_F_CTRL_RX) | \
331 				(1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
332 				(1ULL << VIRTIO_NET_F_MQ)      | \
333 				(1ULL << VIRTIO_F_VERSION_1)   | \
334 				(1ULL << VHOST_F_LOG_ALL)      | \
335 				(1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
336 				(1ULL << VIRTIO_NET_F_GSO) | \
337 				(1ULL << VIRTIO_NET_F_HOST_TSO4) | \
338 				(1ULL << VIRTIO_NET_F_HOST_TSO6) | \
339 				(1ULL << VIRTIO_NET_F_HOST_UFO) | \
340 				(1ULL << VIRTIO_NET_F_HOST_ECN) | \
341 				(1ULL << VIRTIO_NET_F_CSUM)    | \
342 				(1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
343 				(1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
344 				(1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
345 				(1ULL << VIRTIO_NET_F_GUEST_UFO) | \
346 				(1ULL << VIRTIO_NET_F_GUEST_ECN) | \
347 				(1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
348 				(1ULL << VIRTIO_RING_F_EVENT_IDX) | \
349 				(1ULL << VIRTIO_NET_F_MTU)  | \
350 				(1ULL << VIRTIO_F_IN_ORDER) | \
351 				(1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
352 				(1ULL << VIRTIO_F_RING_PACKED))
353 
354 
355 struct guest_page {
356 	uint64_t guest_phys_addr;
357 	uint64_t host_phys_addr;
358 	uint64_t size;
359 };
360 
361 struct inflight_mem_info {
362 	int		fd;
363 	void		*addr;
364 	uint64_t	size;
365 };
366 
367 /**
368  * Device structure contains all configuration information relating
369  * to the device.
370  */
371 struct virtio_net {
372 	/* Frontend (QEMU) memory and memory region information */
373 	struct rte_vhost_memory	*mem;
374 	uint64_t		features;
375 	uint64_t		protocol_features;
376 	int			vid;
377 	uint32_t		flags;
378 	uint16_t		vhost_hlen;
379 	/* to tell if we need broadcast rarp packet */
380 	int16_t			broadcast_rarp;
381 	uint32_t		nr_vring;
382 	int			async_copy;
383 
384 	int			extbuf;
385 	int			linearbuf;
386 	struct vhost_virtqueue	*virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
387 	struct inflight_mem_info *inflight_info;
388 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
389 	char			ifname[IF_NAME_SZ];
390 	uint64_t		log_size;
391 	uint64_t		log_base;
392 	uint64_t		log_addr;
393 	struct rte_ether_addr	mac;
394 	uint16_t		mtu;
395 	uint8_t			status;
396 
397 	struct rte_vhost_device_ops const *notify_ops;
398 
399 	uint32_t		nr_guest_pages;
400 	uint32_t		max_guest_pages;
401 	struct guest_page       *guest_pages;
402 
403 	int			slave_req_fd;
404 	rte_spinlock_t		slave_req_lock;
405 
406 	int			postcopy_ufd;
407 	int			postcopy_listening;
408 
409 	struct rte_vdpa_device *vdpa_dev;
410 
411 	/* context data for the external message handlers */
412 	void			*extern_data;
413 	/* pre and post vhost user message handlers for the device */
414 	struct rte_vhost_user_extern_ops extern_ops;
415 } __rte_cache_aligned;
416 
417 static __rte_always_inline bool
418 vq_is_packed(struct virtio_net *dev)
419 {
420 	return dev->features & (1ull << VIRTIO_F_RING_PACKED);
421 }
422 
423 static inline bool
424 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
425 {
426 	uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
427 
428 	return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
429 		wrap_counter != !!(flags & VRING_DESC_F_USED);
430 }
431 
432 static inline void
433 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
434 {
435 	vq->last_used_idx += num;
436 	if (vq->last_used_idx >= vq->size) {
437 		vq->used_wrap_counter ^= 1;
438 		vq->last_used_idx -= vq->size;
439 	}
440 }
441 
442 static inline void
443 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
444 {
445 	vq->last_avail_idx += num;
446 	if (vq->last_avail_idx >= vq->size) {
447 		vq->avail_wrap_counter ^= 1;
448 		vq->last_avail_idx -= vq->size;
449 	}
450 }
451 
452 void __vhost_log_cache_write(struct virtio_net *dev,
453 		struct vhost_virtqueue *vq,
454 		uint64_t addr, uint64_t len);
455 void __vhost_log_cache_write_iova(struct virtio_net *dev,
456 		struct vhost_virtqueue *vq,
457 		uint64_t iova, uint64_t len);
458 void __vhost_log_cache_sync(struct virtio_net *dev,
459 		struct vhost_virtqueue *vq);
460 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
461 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
462 			    uint64_t iova, uint64_t len);
463 
464 static __rte_always_inline void
465 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
466 {
467 	if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
468 		__vhost_log_write(dev, addr, len);
469 }
470 
471 static __rte_always_inline void
472 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
473 {
474 	if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
475 		__vhost_log_cache_sync(dev, vq);
476 }
477 
478 static __rte_always_inline void
479 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
480 			uint64_t addr, uint64_t len)
481 {
482 	if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
483 		__vhost_log_cache_write(dev, vq, addr, len);
484 }
485 
486 static __rte_always_inline void
487 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
488 			uint64_t offset, uint64_t len)
489 {
490 	if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
491 		if (unlikely(vq->log_guest_addr == 0))
492 			return;
493 		__vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset,
494 					len);
495 	}
496 }
497 
498 static __rte_always_inline void
499 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
500 		     uint64_t offset, uint64_t len)
501 {
502 	if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
503 		if (unlikely(vq->log_guest_addr == 0))
504 			return;
505 		__vhost_log_write(dev, vq->log_guest_addr + offset, len);
506 	}
507 }
508 
509 static __rte_always_inline void
510 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
511 			   uint64_t iova, uint64_t len)
512 {
513 	if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
514 		return;
515 
516 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
517 		__vhost_log_cache_write_iova(dev, vq, iova, len);
518 	else
519 		__vhost_log_cache_write(dev, vq, iova, len);
520 }
521 
522 static __rte_always_inline void
523 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
524 			   uint64_t iova, uint64_t len)
525 {
526 	if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
527 		return;
528 
529 	if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
530 		__vhost_log_write_iova(dev, vq, iova, len);
531 	else
532 		__vhost_log_write(dev, iova, len);
533 }
534 
535 extern int vhost_config_log_level;
536 extern int vhost_data_log_level;
537 
538 #define VHOST_LOG_CONFIG(level, fmt, args...)			\
539 	rte_log(RTE_LOG_ ## level, vhost_config_log_level,	\
540 		"VHOST_CONFIG: " fmt, ##args)
541 
542 #define VHOST_LOG_DATA(level, fmt, args...) \
543 	(void)((RTE_LOG_ ## level <= RTE_LOG_DP_LEVEL) ?	\
544 	 rte_log(RTE_LOG_ ## level,  vhost_data_log_level,	\
545 		"VHOST_DATA : " fmt, ##args) :			\
546 	 0)
547 
548 #ifdef RTE_LIBRTE_VHOST_DEBUG
549 #define VHOST_MAX_PRINT_BUFF 6072
550 #define PRINT_PACKET(device, addr, size, header) do { \
551 	char *pkt_addr = (char *)(addr); \
552 	unsigned int index; \
553 	char packet[VHOST_MAX_PRINT_BUFF]; \
554 	\
555 	if ((header)) \
556 		snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
557 	else \
558 		snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
559 	for (index = 0; index < (size); index++) { \
560 		snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
561 			"%02hhx ", pkt_addr[index]); \
562 	} \
563 	snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
564 	\
565 	VHOST_LOG_DATA(DEBUG, "%s", packet); \
566 } while (0)
567 #else
568 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
569 #endif
570 
571 #define MAX_VHOST_DEVICE	1024
572 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
573 
574 #define VHOST_BINARY_SEARCH_THRESH 256
575 
576 static __rte_always_inline int guest_page_addrcmp(const void *p1,
577 						const void *p2)
578 {
579 	const struct guest_page *page1 = (const struct guest_page *)p1;
580 	const struct guest_page *page2 = (const struct guest_page *)p2;
581 
582 	if (page1->guest_phys_addr > page2->guest_phys_addr)
583 		return 1;
584 	if (page1->guest_phys_addr < page2->guest_phys_addr)
585 		return -1;
586 
587 	return 0;
588 }
589 
590 static __rte_always_inline int guest_page_rangecmp(const void *p1, const void *p2)
591 {
592 	const struct guest_page *page1 = (const struct guest_page *)p1;
593 	const struct guest_page *page2 = (const struct guest_page *)p2;
594 
595 	if (page1->guest_phys_addr >= page2->guest_phys_addr) {
596 		if (page1->guest_phys_addr < page2->guest_phys_addr + page2->size)
597 			return 0;
598 		else
599 			return 1;
600 	} else
601 		return -1;
602 }
603 
604 static __rte_always_inline rte_iova_t
605 gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa,
606 	uint64_t gpa_size, uint64_t *hpa_size)
607 {
608 	uint32_t i;
609 	struct guest_page *page;
610 	struct guest_page key;
611 
612 	*hpa_size = gpa_size;
613 	if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
614 		key.guest_phys_addr = gpa;
615 		page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
616 			       sizeof(struct guest_page), guest_page_rangecmp);
617 		if (page) {
618 			if (gpa + gpa_size <=
619 					page->guest_phys_addr + page->size) {
620 				return gpa - page->guest_phys_addr +
621 					page->host_phys_addr;
622 			} else if (gpa < page->guest_phys_addr +
623 						page->size) {
624 				*hpa_size = page->guest_phys_addr +
625 					page->size - gpa;
626 				return gpa - page->guest_phys_addr +
627 					page->host_phys_addr;
628 			}
629 		}
630 	} else {
631 		for (i = 0; i < dev->nr_guest_pages; i++) {
632 			page = &dev->guest_pages[i];
633 
634 			if (gpa >= page->guest_phys_addr) {
635 				if (gpa + gpa_size <=
636 					page->guest_phys_addr + page->size) {
637 					return gpa - page->guest_phys_addr +
638 						page->host_phys_addr;
639 				} else if (gpa < page->guest_phys_addr +
640 							page->size) {
641 					*hpa_size = page->guest_phys_addr +
642 						page->size - gpa;
643 					return gpa - page->guest_phys_addr +
644 						page->host_phys_addr;
645 				}
646 			}
647 		}
648 	}
649 
650 	*hpa_size = 0;
651 	return 0;
652 }
653 
654 /* Convert guest physical address to host physical address */
655 static __rte_always_inline rte_iova_t
656 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
657 {
658 	rte_iova_t hpa;
659 	uint64_t hpa_size;
660 
661 	hpa = gpa_to_first_hpa(dev, gpa, size, &hpa_size);
662 	return hpa_size == size ? hpa : 0;
663 }
664 
665 static __rte_always_inline uint64_t
666 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
667 {
668 	struct rte_vhost_mem_region *r;
669 	uint32_t i;
670 
671 	if (unlikely(!dev || !dev->mem))
672 		return 0;
673 
674 	for (i = 0; i < dev->mem->nregions; i++) {
675 		r = &dev->mem->regions[i];
676 
677 		if (vva >= r->host_user_addr &&
678 		    vva + len <  r->host_user_addr + r->size) {
679 			return r->guest_phys_addr + vva - r->host_user_addr;
680 		}
681 	}
682 	return 0;
683 }
684 
685 static __rte_always_inline struct virtio_net *
686 get_device(int vid)
687 {
688 	struct virtio_net *dev = vhost_devices[vid];
689 
690 	if (unlikely(!dev)) {
691 		VHOST_LOG_CONFIG(ERR,
692 			"(%d) device not found.\n", vid);
693 	}
694 
695 	return dev;
696 }
697 
698 int vhost_new_device(void);
699 void cleanup_device(struct virtio_net *dev, int destroy);
700 void reset_device(struct virtio_net *dev);
701 void vhost_destroy_device(int);
702 void vhost_destroy_device_notify(struct virtio_net *dev);
703 
704 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
705 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
706 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
707 
708 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
709 
710 void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev);
711 
712 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
713 void vhost_setup_virtio_net(int vid, bool enable, bool legacy_ol_flags);
714 void vhost_enable_extbuf(int vid);
715 void vhost_enable_linearbuf(int vid);
716 int vhost_enable_guest_notification(struct virtio_net *dev,
717 		struct vhost_virtqueue *vq, int enable);
718 
719 struct rte_vhost_device_ops const *vhost_driver_callback_get(const char *path);
720 
721 /*
722  * Backend-specific cleanup.
723  *
724  * TODO: fix it; we have one backend now
725  */
726 void vhost_backend_cleanup(struct virtio_net *dev);
727 
728 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
729 			uint64_t iova, uint64_t *len, uint8_t perm);
730 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
731 			struct vhost_virtqueue *vq,
732 			uint64_t desc_addr, uint64_t desc_len);
733 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
734 uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
735 		uint64_t log_addr);
736 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
737 
738 static __rte_always_inline uint64_t
739 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
740 			uint64_t iova, uint64_t *len, uint8_t perm)
741 {
742 	if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
743 		return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
744 
745 	return __vhost_iova_to_vva(dev, vq, iova, len, perm);
746 }
747 
748 #define vhost_avail_event(vr) \
749 	(*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
750 #define vhost_used_event(vr) \
751 	(*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
752 
753 /*
754  * The following is used with VIRTIO_RING_F_EVENT_IDX.
755  * Assuming a given event_idx value from the other size, if we have
756  * just incremented index from old to new_idx, should we trigger an
757  * event?
758  */
759 static __rte_always_inline int
760 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
761 {
762 	return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
763 }
764 
765 static __rte_always_inline void
766 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
767 {
768 	/* Flush used->idx update before we read avail->flags. */
769 	rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
770 
771 	/* Don't kick guest if we don't reach index specified by guest. */
772 	if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
773 		uint16_t old = vq->signalled_used;
774 		uint16_t new = vq->last_used_idx;
775 		bool signalled_used_valid = vq->signalled_used_valid;
776 
777 		vq->signalled_used = new;
778 		vq->signalled_used_valid = true;
779 
780 		VHOST_LOG_DATA(DEBUG, "%s: used_event_idx=%d, old=%d, new=%d\n",
781 			__func__,
782 			vhost_used_event(vq),
783 			old, new);
784 
785 		if ((vhost_need_event(vhost_used_event(vq), new, old) &&
786 					(vq->callfd >= 0)) ||
787 				unlikely(!signalled_used_valid)) {
788 			eventfd_write(vq->callfd, (eventfd_t) 1);
789 			if (dev->notify_ops->guest_notified)
790 				dev->notify_ops->guest_notified(dev->vid);
791 		}
792 	} else {
793 		/* Kick the guest if necessary. */
794 		if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
795 				&& (vq->callfd >= 0)) {
796 			eventfd_write(vq->callfd, (eventfd_t)1);
797 			if (dev->notify_ops->guest_notified)
798 				dev->notify_ops->guest_notified(dev->vid);
799 		}
800 	}
801 }
802 
803 static __rte_always_inline void
804 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
805 {
806 	uint16_t old, new, off, off_wrap;
807 	bool signalled_used_valid, kick = false;
808 
809 	/* Flush used desc update. */
810 	rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
811 
812 	if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
813 		if (vq->driver_event->flags !=
814 				VRING_EVENT_F_DISABLE)
815 			kick = true;
816 		goto kick;
817 	}
818 
819 	old = vq->signalled_used;
820 	new = vq->last_used_idx;
821 	vq->signalled_used = new;
822 	signalled_used_valid = vq->signalled_used_valid;
823 	vq->signalled_used_valid = true;
824 
825 	if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
826 		if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
827 			kick = true;
828 		goto kick;
829 	}
830 
831 	if (unlikely(!signalled_used_valid)) {
832 		kick = true;
833 		goto kick;
834 	}
835 
836 	rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
837 
838 	off_wrap = vq->driver_event->off_wrap;
839 	off = off_wrap & ~(1 << 15);
840 
841 	if (new <= old)
842 		old -= vq->size;
843 
844 	if (vq->used_wrap_counter != off_wrap >> 15)
845 		off -= vq->size;
846 
847 	if (vhost_need_event(off, new, old))
848 		kick = true;
849 kick:
850 	if (kick) {
851 		eventfd_write(vq->callfd, (eventfd_t)1);
852 		if (dev->notify_ops->guest_notified)
853 			dev->notify_ops->guest_notified(dev->vid);
854 	}
855 }
856 
857 static __rte_always_inline void
858 free_ind_table(void *idesc)
859 {
860 	rte_free(idesc);
861 }
862 
863 static __rte_always_inline void
864 restore_mbuf(struct rte_mbuf *m)
865 {
866 	uint32_t mbuf_size, priv_size;
867 
868 	while (m) {
869 		priv_size = rte_pktmbuf_priv_size(m->pool);
870 		mbuf_size = sizeof(struct rte_mbuf) + priv_size;
871 		/* start of buffer is after mbuf structure and priv data */
872 
873 		m->buf_addr = (char *)m + mbuf_size;
874 		m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
875 		m = m->next;
876 	}
877 }
878 
879 static __rte_always_inline bool
880 mbuf_is_consumed(struct rte_mbuf *m)
881 {
882 	while (m) {
883 		if (rte_mbuf_refcnt_read(m) > 1)
884 			return false;
885 		m = m->next;
886 	}
887 
888 	return true;
889 }
890 
891 #endif /* _VHOST_NET_CDEV_H_ */
892