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;
431 struct inflight_mem_info {
438 * Device structure contains all configuration information relating
442 /* Frontend (QEMU) memory and memory region information */
443 struct rte_vhost_memory *mem;
445 uint64_t protocol_features;
449 /* to tell if we need broadcast rarp packet */
450 int16_t broadcast_rarp;
456 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
457 struct inflight_mem_info *inflight_info;
458 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
459 char ifname[IF_NAME_SZ];
463 struct rte_ether_addr mac;
467 struct rte_vhost_device_ops const *notify_ops;
469 uint32_t nr_guest_pages;
470 uint32_t max_guest_pages;
471 struct guest_page *guest_pages;
474 rte_spinlock_t slave_req_lock;
477 int postcopy_listening;
479 struct rte_vdpa_device *vdpa_dev;
481 /* context data for the external message handlers */
483 /* pre and post vhost user message handlers for the device */
484 struct rte_vhost_user_extern_ops extern_ops;
485 } __rte_cache_aligned;
487 static __rte_always_inline bool
488 vq_is_packed(struct virtio_net *dev)
490 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
494 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
496 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
498 return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
499 wrap_counter != !!(flags & VRING_DESC_F_USED);
503 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
505 vq->last_used_idx += num;
506 if (vq->last_used_idx >= vq->size) {
507 vq->used_wrap_counter ^= 1;
508 vq->last_used_idx -= vq->size;
513 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
515 vq->last_avail_idx += num;
516 if (vq->last_avail_idx >= vq->size) {
517 vq->avail_wrap_counter ^= 1;
518 vq->last_avail_idx -= vq->size;
522 void __vhost_log_cache_write(struct virtio_net *dev,
523 struct vhost_virtqueue *vq,
524 uint64_t addr, uint64_t len);
525 void __vhost_log_cache_write_iova(struct virtio_net *dev,
526 struct vhost_virtqueue *vq,
527 uint64_t iova, uint64_t len);
528 void __vhost_log_cache_sync(struct virtio_net *dev,
529 struct vhost_virtqueue *vq);
530 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
531 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
532 uint64_t iova, uint64_t len);
534 static __rte_always_inline void
535 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
537 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
538 __vhost_log_write(dev, addr, len);
541 static __rte_always_inline void
542 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
544 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
545 __vhost_log_cache_sync(dev, vq);
548 static __rte_always_inline void
549 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
550 uint64_t addr, uint64_t len)
552 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
553 __vhost_log_cache_write(dev, vq, addr, len);
556 static __rte_always_inline void
557 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
558 uint64_t offset, uint64_t len)
560 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
561 if (unlikely(vq->log_guest_addr == 0))
563 __vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset,
568 static __rte_always_inline void
569 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
570 uint64_t offset, uint64_t len)
572 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
573 if (unlikely(vq->log_guest_addr == 0))
575 __vhost_log_write(dev, vq->log_guest_addr + offset, len);
579 static __rte_always_inline void
580 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
581 uint64_t iova, uint64_t len)
583 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
586 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
587 __vhost_log_cache_write_iova(dev, vq, iova, len);
589 __vhost_log_cache_write(dev, vq, iova, len);
592 static __rte_always_inline void
593 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
594 uint64_t iova, uint64_t len)
596 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
599 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
600 __vhost_log_write_iova(dev, vq, iova, len);
602 __vhost_log_write(dev, iova, len);
605 extern int vhost_config_log_level;
606 extern int vhost_data_log_level;
608 #define VHOST_LOG_CONFIG(level, fmt, args...) \
609 rte_log(RTE_LOG_ ## level, vhost_config_log_level, \
610 "VHOST_CONFIG: " fmt, ##args)
612 #define VHOST_LOG_DATA(level, fmt, args...) \
613 (void)((RTE_LOG_ ## level <= RTE_LOG_DP_LEVEL) ? \
614 rte_log(RTE_LOG_ ## level, vhost_data_log_level, \
615 "VHOST_DATA : " fmt, ##args) : \
618 #ifdef RTE_LIBRTE_VHOST_DEBUG
619 #define VHOST_MAX_PRINT_BUFF 6072
620 #define PRINT_PACKET(device, addr, size, header) do { \
621 char *pkt_addr = (char *)(addr); \
622 unsigned int index; \
623 char packet[VHOST_MAX_PRINT_BUFF]; \
626 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
628 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
629 for (index = 0; index < (size); index++) { \
630 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
631 "%02hhx ", pkt_addr[index]); \
633 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
635 VHOST_LOG_DATA(DEBUG, "%s", packet); \
638 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
641 extern struct virtio_net *vhost_devices[RTE_MAX_VHOST_DEVICE];
643 #define VHOST_BINARY_SEARCH_THRESH 256
645 static __rte_always_inline int guest_page_addrcmp(const void *p1,
648 const struct guest_page *page1 = (const struct guest_page *)p1;
649 const struct guest_page *page2 = (const struct guest_page *)p2;
651 if (page1->guest_phys_addr > page2->guest_phys_addr)
653 if (page1->guest_phys_addr < page2->guest_phys_addr)
659 static __rte_always_inline int guest_page_rangecmp(const void *p1, const void *p2)
661 const struct guest_page *page1 = (const struct guest_page *)p1;
662 const struct guest_page *page2 = (const struct guest_page *)p2;
664 if (page1->guest_phys_addr >= page2->guest_phys_addr) {
665 if (page1->guest_phys_addr < page2->guest_phys_addr + page2->size)
673 static __rte_always_inline rte_iova_t
674 gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa,
675 uint64_t gpa_size, uint64_t *hpa_size)
678 struct guest_page *page;
679 struct guest_page key;
681 *hpa_size = gpa_size;
682 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
683 key.guest_phys_addr = gpa;
684 page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
685 sizeof(struct guest_page), guest_page_rangecmp);
687 if (gpa + gpa_size <=
688 page->guest_phys_addr + page->size) {
689 return gpa - page->guest_phys_addr +
691 } else if (gpa < page->guest_phys_addr +
693 *hpa_size = page->guest_phys_addr +
695 return gpa - page->guest_phys_addr +
700 for (i = 0; i < dev->nr_guest_pages; i++) {
701 page = &dev->guest_pages[i];
703 if (gpa >= page->guest_phys_addr) {
704 if (gpa + gpa_size <=
705 page->guest_phys_addr + page->size) {
706 return gpa - page->guest_phys_addr +
708 } else if (gpa < page->guest_phys_addr +
710 *hpa_size = page->guest_phys_addr +
712 return gpa - page->guest_phys_addr +
723 /* Convert guest physical address to host physical address */
724 static __rte_always_inline rte_iova_t
725 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
730 hpa = gpa_to_first_hpa(dev, gpa, size, &hpa_size);
731 return hpa_size == size ? hpa : 0;
734 static __rte_always_inline uint64_t
735 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
737 struct rte_vhost_mem_region *r;
740 if (unlikely(!dev || !dev->mem))
743 for (i = 0; i < dev->mem->nregions; i++) {
744 r = &dev->mem->regions[i];
746 if (vva >= r->host_user_addr &&
747 vva + len < r->host_user_addr + r->size) {
748 return r->guest_phys_addr + vva - r->host_user_addr;
754 static __rte_always_inline struct virtio_net *
757 struct virtio_net *dev = vhost_devices[vid];
759 if (unlikely(!dev)) {
760 VHOST_LOG_CONFIG(ERR,
761 "(%d) device not found.\n", vid);
767 int vhost_new_device(void);
768 void cleanup_device(struct virtio_net *dev, int destroy);
769 void reset_device(struct virtio_net *dev);
770 void vhost_destroy_device(int);
771 void vhost_destroy_device_notify(struct virtio_net *dev);
773 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
774 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
775 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
777 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
779 void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev);
781 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
782 void vhost_setup_virtio_net(int vid, bool enable, bool legacy_ol_flags);
783 void vhost_enable_extbuf(int vid);
784 void vhost_enable_linearbuf(int vid);
785 int vhost_enable_guest_notification(struct virtio_net *dev,
786 struct vhost_virtqueue *vq, int enable);
788 struct rte_vhost_device_ops const *vhost_driver_callback_get(const char *path);
791 * Backend-specific cleanup.
793 * TODO: fix it; we have one backend now
795 void vhost_backend_cleanup(struct virtio_net *dev);
797 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
798 uint64_t iova, uint64_t *len, uint8_t perm);
799 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
800 struct vhost_virtqueue *vq,
801 uint64_t desc_addr, uint64_t desc_len);
802 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
803 uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
805 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
807 static __rte_always_inline uint64_t
808 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
809 uint64_t iova, uint64_t *len, uint8_t perm)
811 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
812 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
814 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
817 #define vhost_avail_event(vr) \
818 (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
819 #define vhost_used_event(vr) \
820 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
823 * The following is used with VIRTIO_RING_F_EVENT_IDX.
824 * Assuming a given event_idx value from the other size, if we have
825 * just incremented index from old to new_idx, should we trigger an
828 static __rte_always_inline int
829 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
831 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
834 static __rte_always_inline void
835 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
837 /* Flush used->idx update before we read avail->flags. */
838 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
840 /* Don't kick guest if we don't reach index specified by guest. */
841 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
842 uint16_t old = vq->signalled_used;
843 uint16_t new = vq->last_used_idx;
844 bool signalled_used_valid = vq->signalled_used_valid;
846 vq->signalled_used = new;
847 vq->signalled_used_valid = true;
849 VHOST_LOG_DATA(DEBUG, "%s: used_event_idx=%d, old=%d, new=%d\n",
851 vhost_used_event(vq),
854 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
855 (vq->callfd >= 0)) ||
856 unlikely(!signalled_used_valid)) {
857 eventfd_write(vq->callfd, (eventfd_t) 1);
858 if (dev->notify_ops->guest_notified)
859 dev->notify_ops->guest_notified(dev->vid);
862 /* Kick the guest if necessary. */
863 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
864 && (vq->callfd >= 0)) {
865 eventfd_write(vq->callfd, (eventfd_t)1);
866 if (dev->notify_ops->guest_notified)
867 dev->notify_ops->guest_notified(dev->vid);
872 static __rte_always_inline void
873 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
875 uint16_t old, new, off, off_wrap;
876 bool signalled_used_valid, kick = false;
878 /* Flush used desc update. */
879 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
881 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
882 if (vq->driver_event->flags !=
883 VRING_EVENT_F_DISABLE)
888 old = vq->signalled_used;
889 new = vq->last_used_idx;
890 vq->signalled_used = new;
891 signalled_used_valid = vq->signalled_used_valid;
892 vq->signalled_used_valid = true;
894 if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
895 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
900 if (unlikely(!signalled_used_valid)) {
905 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
907 off_wrap = vq->driver_event->off_wrap;
908 off = off_wrap & ~(1 << 15);
913 if (vq->used_wrap_counter != off_wrap >> 15)
916 if (vhost_need_event(off, new, old))
920 eventfd_write(vq->callfd, (eventfd_t)1);
921 if (dev->notify_ops->guest_notified)
922 dev->notify_ops->guest_notified(dev->vid);
926 static __rte_always_inline void
927 free_ind_table(void *idesc)
932 static __rte_always_inline void
933 restore_mbuf(struct rte_mbuf *m)
935 uint32_t mbuf_size, priv_size;
938 priv_size = rte_pktmbuf_priv_size(m->pool);
939 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
940 /* start of buffer is after mbuf structure and priv data */
942 m->buf_addr = (char *)m + mbuf_size;
943 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
948 static __rte_always_inline bool
949 mbuf_is_consumed(struct rte_mbuf *m)
952 if (rte_mbuf_refcnt_read(m) > 1)
960 #endif /* _VHOST_NET_CDEV_H_ */