1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
5 #ifndef _VHOST_NET_CDEV_H_
6 #define _VHOST_NET_CDEV_H_
10 #include <sys/types.h>
11 #include <sys/queue.h>
13 #include <linux/vhost.h>
14 #include <linux/virtio_net.h>
15 #include <sys/socket.h>
19 #include <rte_ether.h>
20 #include <rte_malloc.h>
21 #include <rte_dmadev.h>
23 #include "rte_vhost.h"
24 #include "vdpa_driver.h"
26 #include "rte_vhost_async.h"
28 /* Used to indicate that the device is running on a data core */
29 #define VIRTIO_DEV_RUNNING ((uint32_t)1 << 0)
30 /* Used to indicate that the device is ready to operate */
31 #define VIRTIO_DEV_READY ((uint32_t)1 << 1)
32 /* Used to indicate that the built-in vhost net device backend is enabled */
33 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET ((uint32_t)1 << 2)
34 /* Used to indicate that the device has its own data path and configured */
35 #define VIRTIO_DEV_VDPA_CONFIGURED ((uint32_t)1 << 3)
36 /* Used to indicate that the feature negotiation failed */
37 #define VIRTIO_DEV_FEATURES_FAILED ((uint32_t)1 << 4)
38 /* Used to indicate that the virtio_net tx code should fill TX ol_flags */
39 #define VIRTIO_DEV_LEGACY_OL_FLAGS ((uint32_t)1 << 5)
41 /* Backend value set by guest. */
42 #define VIRTIO_DEV_STOPPED -1
44 #define BUF_VECTOR_MAX 256
46 #define VHOST_LOG_CACHE_NR 32
48 #define MAX_PKT_BURST 32
50 #define VHOST_MAX_ASYNC_IT (MAX_PKT_BURST)
51 #define VHOST_MAX_ASYNC_VEC 2048
52 #define VIRTIO_MAX_RX_PKTLEN 9728U
53 #define VHOST_DMA_MAX_COPY_COMPLETE ((VIRTIO_MAX_RX_PKTLEN / RTE_MBUF_DEFAULT_DATAROOM) \
56 #define PACKED_DESC_ENQUEUE_USED_FLAG(w) \
57 ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \
59 #define PACKED_DESC_DEQUEUE_USED_FLAG(w) \
60 ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED) : 0x0)
61 #define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \
62 VRING_DESC_F_INDIRECT)
64 #define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \
65 sizeof(struct vring_packed_desc))
66 #define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
68 #ifdef VHOST_GCC_UNROLL_PRAGMA
69 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \
70 for (iter = val; iter < size; iter++)
73 #ifdef VHOST_CLANG_UNROLL_PRAGMA
74 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \
75 for (iter = val; iter < size; iter++)
78 #ifdef VHOST_ICC_UNROLL_PRAGMA
79 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \
80 for (iter = val; iter < size; iter++)
83 #ifndef vhost_for_each_try_unroll
84 #define vhost_for_each_try_unroll(iter, val, num) \
85 for (iter = val; iter < num; iter++)
89 * Structure contains buffer address, length and descriptor index
90 * from vring to do scatter RX.
100 * Structure contains the info for each batched memory copy.
102 struct batch_copy_elem {
110 * Structure that contains the info for batched dirty logging.
112 struct log_cache_entry {
117 struct vring_used_elem_packed {
136 struct vhost_iov_iter {
137 /** pointer to the iovec array */
138 struct vhost_iovec *iov;
139 /** number of iovec in this iterator */
140 unsigned long nr_segs;
143 struct async_dma_vchan_info {
144 /* circular array to track if packet copy completes */
145 bool **pkts_cmpl_flag_addr;
147 /* max elements in 'pkts_cmpl_flag_addr' */
149 /* ring index mask for 'pkts_cmpl_flag_addr' */
153 * DMA virtual channel lock. Although it is able to bind DMA
154 * virtual channels to data plane threads, vhost control plane
155 * thread could call data plane functions too, thus causing
156 * DMA device contention.
158 * For example, in VM exit case, vhost control plane thread needs
159 * to clear in-flight packets before disable vring, but there could
160 * be anotther data plane thread is enqueuing packets to the same
161 * vring with the same DMA virtual channel. As dmadev PMD functions
162 * are lock-free, the control plane and data plane threads could
163 * operate the same DMA virtual channel at the same time.
165 rte_spinlock_t dma_lock;
168 struct async_dma_info {
169 struct async_dma_vchan_info *vchans;
170 /* number of registered virtual channels */
174 extern struct async_dma_info dma_copy_track[RTE_DMADEV_DEFAULT_MAX];
177 * inflight async packet information
179 struct async_inflight_info {
180 struct rte_mbuf *mbuf;
181 uint16_t descs; /* num of descs inflight */
182 uint16_t nr_buffers; /* num of buffers inflight for packed ring */
186 struct vhost_iov_iter iov_iter[VHOST_MAX_ASYNC_IT];
187 struct vhost_iovec iovec[VHOST_MAX_ASYNC_VEC];
191 /* data transfer status */
192 struct async_inflight_info *pkts_info;
194 * Packet reorder array. "true" indicates that DMA device
195 * completes all copies for the packet.
197 * Note that this array could be written by multiple threads
198 * simultaneously. For example, in the case of thread0 and
199 * thread1 RX packets from NIC and then enqueue packets to
200 * vring0 and vring1 with own DMA device DMA0 and DMA1, it's
201 * possible for thread0 to get completed copies belonging to
202 * vring1 from DMA0, while thread0 is calling rte_vhost_poll
203 * _enqueue_completed() for vring0 and thread1 is calling
204 * rte_vhost_submit_enqueue_burst() for vring1. In this case,
205 * vq->access_lock cannot protect pkts_cmpl_flag of vring1.
207 * However, since offloading is per-packet basis, each packet
208 * flag will only be written by one thread. And single byte
209 * write is atomic, so no lock for pkts_cmpl_flag is needed.
211 bool *pkts_cmpl_flag;
213 uint16_t pkts_inflight_n;
215 struct vring_used_elem *descs_split;
216 struct vring_used_elem_packed *buffers_packed;
219 uint16_t desc_idx_split;
220 uint16_t buffer_idx_packed;
223 uint16_t last_desc_idx_split;
224 uint16_t last_buffer_idx_packed;
229 * Structure contains variables relevant to RX/TX virtqueues.
231 struct vhost_virtqueue {
233 struct vring_desc *desc;
234 struct vring_packed_desc *desc_packed;
237 struct vring_avail *avail;
238 struct vring_packed_desc_event *driver_event;
241 struct vring_used *used;
242 struct vring_packed_desc_event *device_event;
246 uint16_t last_avail_idx;
247 uint16_t last_used_idx;
248 /* Last used index we notify to front end. */
249 uint16_t signalled_used;
250 bool signalled_used_valid;
251 #define VIRTIO_INVALID_EVENTFD (-1)
252 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
258 rte_spinlock_t access_lock;
262 struct vring_used_elem *shadow_used_split;
263 struct vring_used_elem_packed *shadow_used_packed;
265 uint16_t shadow_used_idx;
266 /* Record packed ring enqueue latest desc cache aligned index */
267 uint16_t shadow_aligned_idx;
268 /* Record packed ring first dequeue desc index */
269 uint16_t shadow_last_used_idx;
271 uint16_t batch_copy_nb_elems;
272 struct batch_copy_elem *batch_copy_elems;
274 bool used_wrap_counter;
275 bool avail_wrap_counter;
277 /* Physical address of used ring, for logging */
278 uint16_t log_cache_nb_elem;
279 uint64_t log_guest_addr;
280 struct log_cache_entry *log_cache;
282 rte_rwlock_t iotlb_lock;
283 rte_rwlock_t iotlb_pending_lock;
284 struct rte_mempool *iotlb_pool;
285 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
286 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
289 /* Used to notify the guest (trigger interrupt) */
291 /* Currently unused as polling mode is enabled */
294 /* inflight share memory info */
296 struct rte_vhost_inflight_info_split *inflight_split;
297 struct rte_vhost_inflight_info_packed *inflight_packed;
299 struct rte_vhost_resubmit_info *resubmit_inflight;
300 uint64_t global_counter;
302 struct vhost_async *async;
305 #define VIRTIO_UNINITIALIZED_NOTIF (-1)
307 struct vhost_vring_addr ring_addrs;
308 } __rte_cache_aligned;
310 /* Virtio device status as per Virtio specification */
311 #define VIRTIO_DEVICE_STATUS_RESET 0x00
312 #define VIRTIO_DEVICE_STATUS_ACK 0x01
313 #define VIRTIO_DEVICE_STATUS_DRIVER 0x02
314 #define VIRTIO_DEVICE_STATUS_DRIVER_OK 0x04
315 #define VIRTIO_DEVICE_STATUS_FEATURES_OK 0x08
316 #define VIRTIO_DEVICE_STATUS_DEV_NEED_RESET 0x40
317 #define VIRTIO_DEVICE_STATUS_FAILED 0x80
319 #define VHOST_MAX_VRING 0x100
320 #define VHOST_MAX_QUEUE_PAIRS 0x80
322 /* Declare IOMMU related bits for older kernels */
323 #ifndef VIRTIO_F_IOMMU_PLATFORM
325 #define VIRTIO_F_IOMMU_PLATFORM 33
327 struct vhost_iotlb_msg {
331 #define VHOST_ACCESS_RO 0x1
332 #define VHOST_ACCESS_WO 0x2
333 #define VHOST_ACCESS_RW 0x3
335 #define VHOST_IOTLB_MISS 1
336 #define VHOST_IOTLB_UPDATE 2
337 #define VHOST_IOTLB_INVALIDATE 3
338 #define VHOST_IOTLB_ACCESS_FAIL 4
342 #define VHOST_IOTLB_MSG 0x1
347 struct vhost_iotlb_msg iotlb;
354 * Define virtio 1.0 for older kernels
356 #ifndef VIRTIO_F_VERSION_1
357 #define VIRTIO_F_VERSION_1 32
360 /* Declare packed ring related bits for older kernels */
361 #ifndef VIRTIO_F_RING_PACKED
363 #define VIRTIO_F_RING_PACKED 34
365 struct vring_packed_desc {
372 struct vring_packed_desc_event {
379 * Declare below packed ring defines unconditionally
380 * as Kernel header might use different names.
382 #define VRING_DESC_F_AVAIL (1ULL << 7)
383 #define VRING_DESC_F_USED (1ULL << 15)
385 #define VRING_EVENT_F_ENABLE 0x0
386 #define VRING_EVENT_F_DISABLE 0x1
387 #define VRING_EVENT_F_DESC 0x2
390 * Available and used descs are in same order
392 #ifndef VIRTIO_F_IN_ORDER
393 #define VIRTIO_F_IN_ORDER 35
396 /* Features supported by this builtin vhost-user net driver. */
397 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
398 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
399 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
400 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
401 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
402 (1ULL << VIRTIO_NET_F_MQ) | \
403 (1ULL << VIRTIO_F_VERSION_1) | \
404 (1ULL << VHOST_F_LOG_ALL) | \
405 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
406 (1ULL << VIRTIO_NET_F_GSO) | \
407 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
408 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
409 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
410 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
411 (1ULL << VIRTIO_NET_F_CSUM) | \
412 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
413 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
414 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
415 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
416 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
417 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
418 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
419 (1ULL << VIRTIO_NET_F_MTU) | \
420 (1ULL << VIRTIO_F_IN_ORDER) | \
421 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
422 (1ULL << VIRTIO_F_RING_PACKED))
426 uint64_t guest_phys_addr;
428 uint64_t host_user_addr;
432 struct inflight_mem_info {
439 * Device structure contains all configuration information relating
443 /* Frontend (QEMU) memory and memory region information */
444 struct rte_vhost_memory *mem;
446 uint64_t protocol_features;
450 /* to tell if we need broadcast rarp packet */
451 int16_t broadcast_rarp;
457 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
458 struct inflight_mem_info *inflight_info;
459 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
460 char ifname[IF_NAME_SZ];
464 struct rte_ether_addr mac;
468 struct rte_vhost_device_ops const *notify_ops;
470 uint32_t nr_guest_pages;
471 uint32_t max_guest_pages;
472 struct guest_page *guest_pages;
475 rte_spinlock_t slave_req_lock;
478 int postcopy_listening;
480 struct rte_vdpa_device *vdpa_dev;
482 /* context data for the external message handlers */
484 /* pre and post vhost user message handlers for the device */
485 struct rte_vhost_user_extern_ops extern_ops;
486 } __rte_cache_aligned;
488 static __rte_always_inline bool
489 vq_is_packed(struct virtio_net *dev)
491 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
495 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
497 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
499 return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
500 wrap_counter != !!(flags & VRING_DESC_F_USED);
504 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
506 vq->last_used_idx += num;
507 if (vq->last_used_idx >= vq->size) {
508 vq->used_wrap_counter ^= 1;
509 vq->last_used_idx -= vq->size;
514 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
516 vq->last_avail_idx += num;
517 if (vq->last_avail_idx >= vq->size) {
518 vq->avail_wrap_counter ^= 1;
519 vq->last_avail_idx -= vq->size;
523 void __vhost_log_cache_write(struct virtio_net *dev,
524 struct vhost_virtqueue *vq,
525 uint64_t addr, uint64_t len);
526 void __vhost_log_cache_write_iova(struct virtio_net *dev,
527 struct vhost_virtqueue *vq,
528 uint64_t iova, uint64_t len);
529 void __vhost_log_cache_sync(struct virtio_net *dev,
530 struct vhost_virtqueue *vq);
531 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
532 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
533 uint64_t iova, uint64_t len);
535 static __rte_always_inline void
536 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
538 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
539 __vhost_log_write(dev, addr, len);
542 static __rte_always_inline void
543 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
545 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
546 __vhost_log_cache_sync(dev, vq);
549 static __rte_always_inline void
550 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
551 uint64_t addr, uint64_t len)
553 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
554 __vhost_log_cache_write(dev, vq, addr, len);
557 static __rte_always_inline void
558 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
559 uint64_t offset, uint64_t len)
561 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
562 if (unlikely(vq->log_guest_addr == 0))
564 __vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset,
569 static __rte_always_inline void
570 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
571 uint64_t offset, uint64_t len)
573 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
574 if (unlikely(vq->log_guest_addr == 0))
576 __vhost_log_write(dev, vq->log_guest_addr + offset, len);
580 static __rte_always_inline void
581 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
582 uint64_t iova, uint64_t len)
584 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
587 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
588 __vhost_log_cache_write_iova(dev, vq, iova, len);
590 __vhost_log_cache_write(dev, vq, iova, len);
593 static __rte_always_inline void
594 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
595 uint64_t iova, uint64_t len)
597 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
600 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
601 __vhost_log_write_iova(dev, vq, iova, len);
603 __vhost_log_write(dev, iova, len);
606 extern int vhost_config_log_level;
607 extern int vhost_data_log_level;
609 #define VHOST_LOG_CONFIG(level, fmt, args...) \
610 rte_log(RTE_LOG_ ## level, vhost_config_log_level, \
611 "VHOST_CONFIG: " fmt, ##args)
613 #define VHOST_LOG_DATA(level, fmt, args...) \
614 (void)((RTE_LOG_ ## level <= RTE_LOG_DP_LEVEL) ? \
615 rte_log(RTE_LOG_ ## level, vhost_data_log_level, \
616 "VHOST_DATA : " fmt, ##args) : \
619 #ifdef RTE_LIBRTE_VHOST_DEBUG
620 #define VHOST_MAX_PRINT_BUFF 6072
621 #define PRINT_PACKET(device, addr, size, header) do { \
622 char *pkt_addr = (char *)(addr); \
623 unsigned int index; \
624 char packet[VHOST_MAX_PRINT_BUFF]; \
627 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
629 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
630 for (index = 0; index < (size); index++) { \
631 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
632 "%02hhx ", pkt_addr[index]); \
634 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
636 VHOST_LOG_DATA(DEBUG, "%s", packet); \
639 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
642 extern struct virtio_net *vhost_devices[RTE_MAX_VHOST_DEVICE];
644 #define VHOST_BINARY_SEARCH_THRESH 256
646 static __rte_always_inline int guest_page_addrcmp(const void *p1,
649 const struct guest_page *page1 = (const struct guest_page *)p1;
650 const struct guest_page *page2 = (const struct guest_page *)p2;
652 if (page1->guest_phys_addr > page2->guest_phys_addr)
654 if (page1->guest_phys_addr < page2->guest_phys_addr)
660 static __rte_always_inline int guest_page_rangecmp(const void *p1, const void *p2)
662 const struct guest_page *page1 = (const struct guest_page *)p1;
663 const struct guest_page *page2 = (const struct guest_page *)p2;
665 if (page1->guest_phys_addr >= page2->guest_phys_addr) {
666 if (page1->guest_phys_addr < page2->guest_phys_addr + page2->size)
674 static __rte_always_inline rte_iova_t
675 gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa,
676 uint64_t gpa_size, uint64_t *hpa_size)
679 struct guest_page *page;
680 struct guest_page key;
682 *hpa_size = gpa_size;
683 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
684 key.guest_phys_addr = gpa;
685 page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
686 sizeof(struct guest_page), guest_page_rangecmp);
688 if (gpa + gpa_size <=
689 page->guest_phys_addr + page->size) {
690 return gpa - page->guest_phys_addr +
692 } else if (gpa < page->guest_phys_addr +
694 *hpa_size = page->guest_phys_addr +
696 return gpa - page->guest_phys_addr +
701 for (i = 0; i < dev->nr_guest_pages; i++) {
702 page = &dev->guest_pages[i];
704 if (gpa >= page->guest_phys_addr) {
705 if (gpa + gpa_size <=
706 page->guest_phys_addr + page->size) {
707 return gpa - page->guest_phys_addr +
709 } else if (gpa < page->guest_phys_addr +
711 *hpa_size = page->guest_phys_addr +
713 return gpa - page->guest_phys_addr +
724 /* Convert guest physical address to host physical address */
725 static __rte_always_inline rte_iova_t
726 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
731 hpa = gpa_to_first_hpa(dev, gpa, size, &hpa_size);
732 return hpa_size == size ? hpa : 0;
735 static __rte_always_inline uint64_t
736 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
738 struct rte_vhost_mem_region *r;
741 if (unlikely(!dev || !dev->mem))
744 for (i = 0; i < dev->mem->nregions; i++) {
745 r = &dev->mem->regions[i];
747 if (vva >= r->host_user_addr &&
748 vva + len < r->host_user_addr + r->size) {
749 return r->guest_phys_addr + vva - r->host_user_addr;
755 static __rte_always_inline struct virtio_net *
758 struct virtio_net *dev = vhost_devices[vid];
760 if (unlikely(!dev)) {
761 VHOST_LOG_CONFIG(ERR,
762 "(%d) device not found.\n", vid);
768 int vhost_new_device(void);
769 void cleanup_device(struct virtio_net *dev, int destroy);
770 void reset_device(struct virtio_net *dev);
771 void vhost_destroy_device(int);
772 void vhost_destroy_device_notify(struct virtio_net *dev);
774 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
775 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
776 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
778 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
780 void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev);
782 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
783 void vhost_setup_virtio_net(int vid, bool enable, bool legacy_ol_flags);
784 void vhost_enable_extbuf(int vid);
785 void vhost_enable_linearbuf(int vid);
786 int vhost_enable_guest_notification(struct virtio_net *dev,
787 struct vhost_virtqueue *vq, int enable);
789 struct rte_vhost_device_ops const *vhost_driver_callback_get(const char *path);
792 * Backend-specific cleanup.
794 * TODO: fix it; we have one backend now
796 void vhost_backend_cleanup(struct virtio_net *dev);
798 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
799 uint64_t iova, uint64_t *len, uint8_t perm);
800 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
801 struct vhost_virtqueue *vq,
802 uint64_t desc_addr, uint64_t desc_len);
803 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
804 uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
806 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
808 static __rte_always_inline uint64_t
809 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
810 uint64_t iova, uint64_t *len, uint8_t perm)
812 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
813 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
815 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
818 #define vhost_avail_event(vr) \
819 (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
820 #define vhost_used_event(vr) \
821 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
824 * The following is used with VIRTIO_RING_F_EVENT_IDX.
825 * Assuming a given event_idx value from the other size, if we have
826 * just incremented index from old to new_idx, should we trigger an
829 static __rte_always_inline int
830 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
832 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
835 static __rte_always_inline void
836 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
838 /* Flush used->idx update before we read avail->flags. */
839 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
841 /* Don't kick guest if we don't reach index specified by guest. */
842 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
843 uint16_t old = vq->signalled_used;
844 uint16_t new = vq->last_used_idx;
845 bool signalled_used_valid = vq->signalled_used_valid;
847 vq->signalled_used = new;
848 vq->signalled_used_valid = true;
850 VHOST_LOG_DATA(DEBUG, "%s: used_event_idx=%d, old=%d, new=%d\n",
852 vhost_used_event(vq),
855 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
856 (vq->callfd >= 0)) ||
857 unlikely(!signalled_used_valid)) {
858 eventfd_write(vq->callfd, (eventfd_t) 1);
859 if (dev->notify_ops->guest_notified)
860 dev->notify_ops->guest_notified(dev->vid);
863 /* Kick the guest if necessary. */
864 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
865 && (vq->callfd >= 0)) {
866 eventfd_write(vq->callfd, (eventfd_t)1);
867 if (dev->notify_ops->guest_notified)
868 dev->notify_ops->guest_notified(dev->vid);
873 static __rte_always_inline void
874 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
876 uint16_t old, new, off, off_wrap;
877 bool signalled_used_valid, kick = false;
879 /* Flush used desc update. */
880 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
882 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
883 if (vq->driver_event->flags !=
884 VRING_EVENT_F_DISABLE)
889 old = vq->signalled_used;
890 new = vq->last_used_idx;
891 vq->signalled_used = new;
892 signalled_used_valid = vq->signalled_used_valid;
893 vq->signalled_used_valid = true;
895 if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
896 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
901 if (unlikely(!signalled_used_valid)) {
906 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
908 off_wrap = vq->driver_event->off_wrap;
909 off = off_wrap & ~(1 << 15);
914 if (vq->used_wrap_counter != off_wrap >> 15)
917 if (vhost_need_event(off, new, old))
921 eventfd_write(vq->callfd, (eventfd_t)1);
922 if (dev->notify_ops->guest_notified)
923 dev->notify_ops->guest_notified(dev->vid);
927 static __rte_always_inline void
928 free_ind_table(void *idesc)
933 static __rte_always_inline void
934 restore_mbuf(struct rte_mbuf *m)
936 uint32_t mbuf_size, priv_size;
939 priv_size = rte_pktmbuf_priv_size(m->pool);
940 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
941 /* start of buffer is after mbuf structure and priv data */
943 m->buf_addr = (char *)m + mbuf_size;
944 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
949 static __rte_always_inline bool
950 mbuf_is_consumed(struct rte_mbuf *m)
953 if (rte_mbuf_refcnt_read(m) > 1)
961 #endif /* _VHOST_NET_CDEV_H_ */