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