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_rwlock.h>
21 #include <rte_malloc.h>
23 #include "rte_vhost.h"
25 #include "rte_vdpa_dev.h"
27 #include "rte_vhost_async.h"
29 /* Used to indicate that the device is running on a data core */
30 #define VIRTIO_DEV_RUNNING 1
31 /* Used to indicate that the device is ready to operate */
32 #define VIRTIO_DEV_READY 2
33 /* Used to indicate that the built-in vhost net device backend is enabled */
34 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
35 /* Used to indicate that the device has its own data path and configured */
36 #define VIRTIO_DEV_VDPA_CONFIGURED 8
37 /* Used to indicate that the feature negotiation failed */
38 #define VIRTIO_DEV_FEATURES_FAILED 16
40 /* Backend value set by guest. */
41 #define VIRTIO_DEV_STOPPED -1
43 #define BUF_VECTOR_MAX 256
45 #define VHOST_LOG_CACHE_NR 32
47 #define MAX_PKT_BURST 32
49 #define ASYNC_MAX_POLL_SEG 255
51 #define VHOST_MAX_ASYNC_IT (MAX_PKT_BURST * 2)
52 #define VHOST_MAX_ASYNC_VEC (BUF_VECTOR_MAX * 2)
54 #define PACKED_DESC_ENQUEUE_USED_FLAG(w) \
55 ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | 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)
62 #define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \
63 sizeof(struct vring_packed_desc))
64 #define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
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++)
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++)
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++)
81 #ifndef vhost_for_each_try_unroll
82 #define vhost_for_each_try_unroll(iter, val, num) \
83 for (iter = val; iter < num; iter++)
87 * Structure contains buffer address, length and descriptor index
88 * from vring to do scatter RX.
98 * A structure to hold some fields needed in zero copy code path,
99 * mainly for associating an mbuf with the right desc_idx.
102 struct rte_mbuf *mbuf;
107 TAILQ_ENTRY(zcopy_mbuf) next;
109 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
112 * Structure contains the info for each batched memory copy.
114 struct batch_copy_elem {
122 * Structure that contains the info for batched dirty logging.
124 struct log_cache_entry {
129 struct vring_used_elem_packed {
137 * Structure contains variables relevant to RX/TX virtqueues.
139 struct vhost_virtqueue {
141 struct vring_desc *desc;
142 struct vring_packed_desc *desc_packed;
145 struct vring_avail *avail;
146 struct vring_packed_desc_event *driver_event;
149 struct vring_used *used;
150 struct vring_packed_desc_event *device_event;
154 uint16_t last_avail_idx;
155 uint16_t last_used_idx;
156 /* Last used index we notify to front end. */
157 uint16_t signalled_used;
158 bool signalled_used_valid;
159 #define VIRTIO_INVALID_EVENTFD (-1)
160 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
162 /* Backend value to determine if device should started/stopped */
168 #define VIRTIO_UNINITIALIZED_NOTIF (-1)
170 rte_spinlock_t access_lock;
172 /* Used to notify the guest (trigger interrupt) */
174 /* Currently unused as polling mode is enabled */
177 /* Physical address of used ring, for logging */
178 uint64_t log_guest_addr;
180 /* inflight share memory info */
182 struct rte_vhost_inflight_info_split *inflight_split;
183 struct rte_vhost_inflight_info_packed *inflight_packed;
185 struct rte_vhost_resubmit_info *resubmit_inflight;
186 uint64_t global_counter;
190 uint16_t last_zmbuf_idx;
191 struct zcopy_mbuf *zmbufs;
192 struct zcopy_mbuf_list zmbuf_list;
195 struct vring_used_elem *shadow_used_split;
196 struct vring_used_elem_packed *shadow_used_packed;
198 uint16_t shadow_used_idx;
199 /* Record packed ring enqueue latest desc cache aligned index */
200 uint16_t shadow_aligned_idx;
201 /* Record packed ring first dequeue desc index */
202 uint16_t shadow_last_used_idx;
203 struct vhost_vring_addr ring_addrs;
205 struct batch_copy_elem *batch_copy_elems;
206 uint16_t batch_copy_nb_elems;
207 bool used_wrap_counter;
208 bool avail_wrap_counter;
210 struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
211 uint16_t log_cache_nb_elem;
213 rte_rwlock_t iotlb_lock;
214 rte_rwlock_t iotlb_pending_lock;
215 struct rte_mempool *iotlb_pool;
216 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
218 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
220 /* operation callbacks for async dma */
221 struct rte_vhost_async_channel_ops async_ops;
223 struct rte_vhost_iov_iter it_pool[VHOST_MAX_ASYNC_IT];
224 struct iovec vec_pool[VHOST_MAX_ASYNC_VEC];
226 /* async data transfer status */
227 uintptr_t **async_pkts_pending;
228 #define ASYNC_PENDING_INFO_N_MSK 0xFFFF
229 #define ASYNC_PENDING_INFO_N_SFT 16
230 uint64_t *async_pending_info;
231 uint16_t async_pkts_idx;
232 uint16_t async_pkts_inflight_n;
233 uint16_t async_last_seg_n;
235 /* vq async features */
237 bool async_registered;
238 uint16_t async_threshold;
239 } __rte_cache_aligned;
241 /* Virtio device status as per Virtio specification */
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
249 #define VHOST_MAX_VRING 0x100
250 #define VHOST_MAX_QUEUE_PAIRS 0x80
252 /* Declare IOMMU related bits for older kernels */
253 #ifndef VIRTIO_F_IOMMU_PLATFORM
255 #define VIRTIO_F_IOMMU_PLATFORM 33
257 struct vhost_iotlb_msg {
261 #define VHOST_ACCESS_RO 0x1
262 #define VHOST_ACCESS_WO 0x2
263 #define VHOST_ACCESS_RW 0x3
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
272 #define VHOST_IOTLB_MSG 0x1
277 struct vhost_iotlb_msg iotlb;
284 * Define virtio 1.0 for older kernels
286 #ifndef VIRTIO_F_VERSION_1
287 #define VIRTIO_F_VERSION_1 32
290 /* Declare packed ring related bits for older kernels */
291 #ifndef VIRTIO_F_RING_PACKED
293 #define VIRTIO_F_RING_PACKED 34
295 struct vring_packed_desc {
302 struct vring_packed_desc_event {
309 * Declare below packed ring defines unconditionally
310 * as Kernel header might use different names.
312 #define VRING_DESC_F_AVAIL (1ULL << 7)
313 #define VRING_DESC_F_USED (1ULL << 15)
315 #define VRING_EVENT_F_ENABLE 0x0
316 #define VRING_EVENT_F_DISABLE 0x1
317 #define VRING_EVENT_F_DESC 0x2
320 * Available and used descs are in same order
322 #ifndef VIRTIO_F_IN_ORDER
323 #define VIRTIO_F_IN_ORDER 35
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))
356 uint64_t guest_phys_addr;
357 uint64_t host_phys_addr;
361 struct inflight_mem_info {
368 * Device structure contains all configuration information relating
372 /* Frontend (QEMU) memory and memory region information */
373 struct rte_vhost_memory *mem;
375 uint64_t protocol_features;
379 /* to tell if we need broadcast rarp packet */
380 int16_t broadcast_rarp;
382 int dequeue_zero_copy;
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];
393 struct rte_ether_addr mac;
397 struct vhost_device_ops const *notify_ops;
399 uint32_t nr_guest_pages;
400 uint32_t max_guest_pages;
401 struct guest_page *guest_pages;
404 rte_spinlock_t slave_req_lock;
407 int postcopy_listening;
409 struct rte_vdpa_device *vdpa_dev;
411 /* context data for the external message handlers */
413 /* pre and post vhost user message handlers for the device */
414 struct rte_vhost_user_extern_ops extern_ops;
415 } __rte_cache_aligned;
417 static __rte_always_inline bool
418 vq_is_packed(struct virtio_net *dev)
420 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
424 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
426 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
428 return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
429 wrap_counter != !!(flags & VRING_DESC_F_USED);
433 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
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;
443 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
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;
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);
464 static __rte_always_inline void
465 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
467 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
468 __vhost_log_write(dev, addr, len);
471 static __rte_always_inline void
472 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
474 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
475 __vhost_log_cache_sync(dev, vq);
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)
482 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
483 __vhost_log_cache_write(dev, vq, addr, len);
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)
490 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
491 if (unlikely(vq->log_guest_addr == 0))
493 __vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset,
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)
502 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
503 if (unlikely(vq->log_guest_addr == 0))
505 __vhost_log_write(dev, vq->log_guest_addr + offset, len);
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)
513 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
516 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
517 __vhost_log_cache_write_iova(dev, vq, iova, len);
519 __vhost_log_cache_write(dev, vq, iova, len);
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)
526 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
529 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
530 __vhost_log_write_iova(dev, vq, iova, len);
532 __vhost_log_write(dev, iova, len);
535 extern int vhost_config_log_level;
536 extern int vhost_data_log_level;
538 #define VHOST_LOG_CONFIG(level, fmt, args...) \
539 rte_log(RTE_LOG_ ## level, vhost_config_log_level, \
540 "VHOST_CONFIG: " fmt, ##args)
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) : \
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]; \
556 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
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]); \
563 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
565 VHOST_LOG_DATA(DEBUG, "%s", packet); \
568 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
571 #define MAX_VHOST_DEVICE 1024
572 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
574 #define VHOST_BINARY_SEARCH_THRESH 256
576 static __rte_always_inline int guest_page_addrcmp(const void *p1,
579 const struct guest_page *page1 = (const struct guest_page *)p1;
580 const struct guest_page *page2 = (const struct guest_page *)p2;
582 if (page1->guest_phys_addr > page2->guest_phys_addr)
584 if (page1->guest_phys_addr < page2->guest_phys_addr)
590 /* Convert guest physical address to host physical address */
591 static __rte_always_inline rte_iova_t
592 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
595 struct guest_page *page;
596 struct guest_page key;
598 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
599 key.guest_phys_addr = gpa;
600 page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
601 sizeof(struct guest_page), guest_page_addrcmp);
603 if (gpa + size < page->guest_phys_addr + page->size)
604 return gpa - page->guest_phys_addr +
605 page->host_phys_addr;
608 for (i = 0; i < dev->nr_guest_pages; i++) {
609 page = &dev->guest_pages[i];
611 if (gpa >= page->guest_phys_addr &&
612 gpa + size < page->guest_phys_addr +
614 return gpa - page->guest_phys_addr +
615 page->host_phys_addr;
622 static __rte_always_inline rte_iova_t
623 gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa,
624 uint64_t gpa_size, uint64_t *hpa_size)
627 struct guest_page *page;
628 struct guest_page key;
630 *hpa_size = gpa_size;
631 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
632 key.guest_phys_addr = gpa & ~(dev->guest_pages[0].size - 1);
633 page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
634 sizeof(struct guest_page), guest_page_addrcmp);
636 if (gpa + gpa_size <=
637 page->guest_phys_addr + page->size) {
638 return gpa - page->guest_phys_addr +
639 page->host_phys_addr;
640 } else if (gpa < page->guest_phys_addr +
642 *hpa_size = page->guest_phys_addr +
644 return gpa - page->guest_phys_addr +
645 page->host_phys_addr;
649 for (i = 0; i < dev->nr_guest_pages; i++) {
650 page = &dev->guest_pages[i];
652 if (gpa >= page->guest_phys_addr) {
653 if (gpa + gpa_size <=
654 page->guest_phys_addr + page->size) {
655 return gpa - page->guest_phys_addr +
656 page->host_phys_addr;
657 } else if (gpa < page->guest_phys_addr +
659 *hpa_size = page->guest_phys_addr +
661 return gpa - page->guest_phys_addr +
662 page->host_phys_addr;
672 static __rte_always_inline uint64_t
673 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
675 struct rte_vhost_mem_region *r;
678 if (unlikely(!dev || !dev->mem))
681 for (i = 0; i < dev->mem->nregions; i++) {
682 r = &dev->mem->regions[i];
684 if (vva >= r->host_user_addr &&
685 vva + len < r->host_user_addr + r->size) {
686 return r->guest_phys_addr + vva - r->host_user_addr;
692 static __rte_always_inline struct virtio_net *
695 struct virtio_net *dev = vhost_devices[vid];
697 if (unlikely(!dev)) {
698 VHOST_LOG_CONFIG(ERR,
699 "(%d) device not found.\n", vid);
705 int vhost_new_device(void);
706 void cleanup_device(struct virtio_net *dev, int destroy);
707 void reset_device(struct virtio_net *dev);
708 void vhost_destroy_device(int);
709 void vhost_destroy_device_notify(struct virtio_net *dev);
711 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
712 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
713 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
715 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
717 void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev);
719 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
720 void vhost_enable_dequeue_zero_copy(int vid);
721 void vhost_set_builtin_virtio_net(int vid, bool enable);
722 void vhost_enable_extbuf(int vid);
723 void vhost_enable_linearbuf(int vid);
724 int vhost_enable_guest_notification(struct virtio_net *dev,
725 struct vhost_virtqueue *vq, int enable);
727 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
730 * Backend-specific cleanup.
732 * TODO: fix it; we have one backend now
734 void vhost_backend_cleanup(struct virtio_net *dev);
736 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
737 uint64_t iova, uint64_t *len, uint8_t perm);
738 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
739 struct vhost_virtqueue *vq,
740 uint64_t desc_addr, uint64_t desc_len);
741 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
742 uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
744 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
746 static __rte_always_inline uint64_t
747 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
748 uint64_t iova, uint64_t *len, uint8_t perm)
750 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
751 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
753 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
756 #define vhost_avail_event(vr) \
757 (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
758 #define vhost_used_event(vr) \
759 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
762 * The following is used with VIRTIO_RING_F_EVENT_IDX.
763 * Assuming a given event_idx value from the other size, if we have
764 * just incremented index from old to new_idx, should we trigger an
767 static __rte_always_inline int
768 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
770 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
773 static __rte_always_inline void
774 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
776 /* Flush used->idx update before we read avail->flags. */
779 /* Don't kick guest if we don't reach index specified by guest. */
780 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
781 uint16_t old = vq->signalled_used;
782 uint16_t new = vq->async_pkts_inflight_n ?
783 vq->used->idx:vq->last_used_idx;
784 bool signalled_used_valid = vq->signalled_used_valid;
786 vq->signalled_used = new;
787 vq->signalled_used_valid = true;
789 VHOST_LOG_DATA(DEBUG, "%s: used_event_idx=%d, old=%d, new=%d\n",
791 vhost_used_event(vq),
794 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
795 (vq->callfd >= 0)) ||
796 unlikely(!signalled_used_valid)) {
797 eventfd_write(vq->callfd, (eventfd_t) 1);
798 if (dev->notify_ops->guest_notified)
799 dev->notify_ops->guest_notified(dev->vid);
802 /* Kick the guest if necessary. */
803 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
804 && (vq->callfd >= 0)) {
805 eventfd_write(vq->callfd, (eventfd_t)1);
806 if (dev->notify_ops->guest_notified)
807 dev->notify_ops->guest_notified(dev->vid);
812 static __rte_always_inline void
813 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
815 uint16_t old, new, off, off_wrap;
816 bool signalled_used_valid, kick = false;
818 /* Flush used desc update. */
821 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
822 if (vq->driver_event->flags !=
823 VRING_EVENT_F_DISABLE)
828 old = vq->signalled_used;
829 new = vq->last_used_idx;
830 vq->signalled_used = new;
831 signalled_used_valid = vq->signalled_used_valid;
832 vq->signalled_used_valid = true;
834 if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
835 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
840 if (unlikely(!signalled_used_valid)) {
847 off_wrap = vq->driver_event->off_wrap;
848 off = off_wrap & ~(1 << 15);
853 if (vq->used_wrap_counter != off_wrap >> 15)
856 if (vhost_need_event(off, new, old))
860 eventfd_write(vq->callfd, (eventfd_t)1);
861 if (dev->notify_ops->guest_notified)
862 dev->notify_ops->guest_notified(dev->vid);
866 static __rte_always_inline void
867 free_ind_table(void *idesc)
872 static __rte_always_inline void
873 restore_mbuf(struct rte_mbuf *m)
875 uint32_t mbuf_size, priv_size;
878 priv_size = rte_pktmbuf_priv_size(m->pool);
879 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
880 /* start of buffer is after mbuf structure and priv data */
882 m->buf_addr = (char *)m + mbuf_size;
883 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
888 static __rte_always_inline bool
889 mbuf_is_consumed(struct rte_mbuf *m)
892 if (rte_mbuf_refcnt_read(m) > 1)
900 static __rte_always_inline void
901 put_zmbuf(struct zcopy_mbuf *zmbuf)
906 #endif /* _VHOST_NET_CDEV_H_ */