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 ((uint32_t)1 << 0)
31 /* Used to indicate that the device is ready to operate */
32 #define VIRTIO_DEV_READY ((uint32_t)1 << 1)
33 /* Used to indicate that the built-in vhost net device backend is enabled */
34 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET ((uint32_t)1 << 2)
35 /* Used to indicate that the device has its own data path and configured */
36 #define VIRTIO_DEV_VDPA_CONFIGURED ((uint32_t)1 << 3)
37 /* Used to indicate that the feature negotiation failed */
38 #define VIRTIO_DEV_FEATURES_FAILED ((uint32_t)1 << 4)
39 /* Used to indicate that the virtio_net tx code should fill TX ol_flags */
40 #define VIRTIO_DEV_LEGACY_OL_FLAGS ((uint32_t)1 << 5)
42 /* Backend value set by guest. */
43 #define VIRTIO_DEV_STOPPED -1
45 #define BUF_VECTOR_MAX 256
47 #define VHOST_LOG_CACHE_NR 32
49 #define MAX_PKT_BURST 32
51 #define VHOST_MAX_ASYNC_IT (MAX_PKT_BURST)
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 * Structure contains the info for each batched memory copy.
100 struct batch_copy_elem {
108 * Structure that contains the info for batched dirty logging.
110 struct log_cache_entry {
115 struct vring_used_elem_packed {
123 * inflight async packet information
125 struct async_inflight_info {
126 struct rte_mbuf *mbuf;
127 uint16_t descs; /* num of descs inflight */
128 uint16_t nr_buffers; /* num of buffers inflight for packed ring */
132 /* operation callbacks for DMA */
133 struct rte_vhost_async_channel_ops ops;
135 struct rte_vhost_iov_iter iov_iter[VHOST_MAX_ASYNC_IT];
136 struct rte_vhost_iovec iovec[VHOST_MAX_ASYNC_VEC];
138 /* data transfer status */
139 struct async_inflight_info *pkts_info;
141 uint16_t pkts_inflight_n;
142 uint16_t last_pkts_n;
144 struct vring_used_elem *descs_split;
145 struct vring_used_elem_packed *buffers_packed;
148 uint16_t desc_idx_split;
149 uint16_t buffer_idx_packed;
152 uint16_t last_desc_idx_split;
153 uint16_t last_buffer_idx_packed;
158 * Structure contains variables relevant to RX/TX virtqueues.
160 struct vhost_virtqueue {
162 struct vring_desc *desc;
163 struct vring_packed_desc *desc_packed;
166 struct vring_avail *avail;
167 struct vring_packed_desc_event *driver_event;
170 struct vring_used *used;
171 struct vring_packed_desc_event *device_event;
175 uint16_t last_avail_idx;
176 uint16_t last_used_idx;
177 /* Last used index we notify to front end. */
178 uint16_t signalled_used;
179 bool signalled_used_valid;
180 #define VIRTIO_INVALID_EVENTFD (-1)
181 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
187 rte_spinlock_t access_lock;
191 struct vring_used_elem *shadow_used_split;
192 struct vring_used_elem_packed *shadow_used_packed;
194 uint16_t shadow_used_idx;
195 /* Record packed ring enqueue latest desc cache aligned index */
196 uint16_t shadow_aligned_idx;
197 /* Record packed ring first dequeue desc index */
198 uint16_t shadow_last_used_idx;
200 uint16_t batch_copy_nb_elems;
201 struct batch_copy_elem *batch_copy_elems;
203 bool used_wrap_counter;
204 bool avail_wrap_counter;
206 /* Physical address of used ring, for logging */
207 uint16_t log_cache_nb_elem;
208 uint64_t log_guest_addr;
209 struct log_cache_entry *log_cache;
211 rte_rwlock_t iotlb_lock;
212 rte_rwlock_t iotlb_pending_lock;
213 struct rte_mempool *iotlb_pool;
214 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
215 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
218 /* Used to notify the guest (trigger interrupt) */
220 /* Currently unused as polling mode is enabled */
223 /* inflight share memory info */
225 struct rte_vhost_inflight_info_split *inflight_split;
226 struct rte_vhost_inflight_info_packed *inflight_packed;
228 struct rte_vhost_resubmit_info *resubmit_inflight;
229 uint64_t global_counter;
231 struct vhost_async *async;
234 #define VIRTIO_UNINITIALIZED_NOTIF (-1)
236 struct vhost_vring_addr ring_addrs;
237 } __rte_cache_aligned;
239 /* Virtio device status as per Virtio specification */
240 #define VIRTIO_DEVICE_STATUS_RESET 0x00
241 #define VIRTIO_DEVICE_STATUS_ACK 0x01
242 #define VIRTIO_DEVICE_STATUS_DRIVER 0x02
243 #define VIRTIO_DEVICE_STATUS_DRIVER_OK 0x04
244 #define VIRTIO_DEVICE_STATUS_FEATURES_OK 0x08
245 #define VIRTIO_DEVICE_STATUS_DEV_NEED_RESET 0x40
246 #define VIRTIO_DEVICE_STATUS_FAILED 0x80
248 #define VHOST_MAX_VRING 0x100
249 #define VHOST_MAX_QUEUE_PAIRS 0x80
251 /* Declare IOMMU related bits for older kernels */
252 #ifndef VIRTIO_F_IOMMU_PLATFORM
254 #define VIRTIO_F_IOMMU_PLATFORM 33
256 struct vhost_iotlb_msg {
260 #define VHOST_ACCESS_RO 0x1
261 #define VHOST_ACCESS_WO 0x2
262 #define VHOST_ACCESS_RW 0x3
264 #define VHOST_IOTLB_MISS 1
265 #define VHOST_IOTLB_UPDATE 2
266 #define VHOST_IOTLB_INVALIDATE 3
267 #define VHOST_IOTLB_ACCESS_FAIL 4
271 #define VHOST_IOTLB_MSG 0x1
276 struct vhost_iotlb_msg iotlb;
283 * Define virtio 1.0 for older kernels
285 #ifndef VIRTIO_F_VERSION_1
286 #define VIRTIO_F_VERSION_1 32
289 /* Declare packed ring related bits for older kernels */
290 #ifndef VIRTIO_F_RING_PACKED
292 #define VIRTIO_F_RING_PACKED 34
294 struct vring_packed_desc {
301 struct vring_packed_desc_event {
308 * Declare below packed ring defines unconditionally
309 * as Kernel header might use different names.
311 #define VRING_DESC_F_AVAIL (1ULL << 7)
312 #define VRING_DESC_F_USED (1ULL << 15)
314 #define VRING_EVENT_F_ENABLE 0x0
315 #define VRING_EVENT_F_DISABLE 0x1
316 #define VRING_EVENT_F_DESC 0x2
319 * Available and used descs are in same order
321 #ifndef VIRTIO_F_IN_ORDER
322 #define VIRTIO_F_IN_ORDER 35
325 /* Features supported by this builtin vhost-user net driver. */
326 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
327 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
328 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
329 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
330 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
331 (1ULL << VIRTIO_NET_F_MQ) | \
332 (1ULL << VIRTIO_F_VERSION_1) | \
333 (1ULL << VHOST_F_LOG_ALL) | \
334 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
335 (1ULL << VIRTIO_NET_F_GSO) | \
336 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
337 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
338 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
339 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
340 (1ULL << VIRTIO_NET_F_CSUM) | \
341 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
342 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
343 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
344 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
345 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
346 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
347 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
348 (1ULL << VIRTIO_NET_F_MTU) | \
349 (1ULL << VIRTIO_F_IN_ORDER) | \
350 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
351 (1ULL << VIRTIO_F_RING_PACKED))
355 uint64_t guest_phys_addr;
356 uint64_t host_phys_addr;
360 struct inflight_mem_info {
367 * Device structure contains all configuration information relating
371 /* Frontend (QEMU) memory and memory region information */
372 struct rte_vhost_memory *mem;
374 uint64_t protocol_features;
378 /* to tell if we need broadcast rarp packet */
379 int16_t broadcast_rarp;
385 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
386 struct inflight_mem_info *inflight_info;
387 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
388 char ifname[IF_NAME_SZ];
392 struct rte_ether_addr mac;
396 struct vhost_device_ops const *notify_ops;
398 uint32_t nr_guest_pages;
399 uint32_t max_guest_pages;
400 struct guest_page *guest_pages;
403 rte_spinlock_t slave_req_lock;
406 int postcopy_listening;
408 struct rte_vdpa_device *vdpa_dev;
410 /* context data for the external message handlers */
412 /* pre and post vhost user message handlers for the device */
413 struct rte_vhost_user_extern_ops extern_ops;
414 } __rte_cache_aligned;
416 static __rte_always_inline bool
417 vq_is_packed(struct virtio_net *dev)
419 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
423 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
425 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
427 return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
428 wrap_counter != !!(flags & VRING_DESC_F_USED);
432 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
434 vq->last_used_idx += num;
435 if (vq->last_used_idx >= vq->size) {
436 vq->used_wrap_counter ^= 1;
437 vq->last_used_idx -= vq->size;
442 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
444 vq->last_avail_idx += num;
445 if (vq->last_avail_idx >= vq->size) {
446 vq->avail_wrap_counter ^= 1;
447 vq->last_avail_idx -= vq->size;
451 void __vhost_log_cache_write(struct virtio_net *dev,
452 struct vhost_virtqueue *vq,
453 uint64_t addr, uint64_t len);
454 void __vhost_log_cache_write_iova(struct virtio_net *dev,
455 struct vhost_virtqueue *vq,
456 uint64_t iova, uint64_t len);
457 void __vhost_log_cache_sync(struct virtio_net *dev,
458 struct vhost_virtqueue *vq);
459 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
460 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
461 uint64_t iova, uint64_t len);
463 static __rte_always_inline void
464 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
466 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
467 __vhost_log_write(dev, addr, len);
470 static __rte_always_inline void
471 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
473 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
474 __vhost_log_cache_sync(dev, vq);
477 static __rte_always_inline void
478 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
479 uint64_t addr, uint64_t len)
481 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
482 __vhost_log_cache_write(dev, vq, addr, len);
485 static __rte_always_inline void
486 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
487 uint64_t offset, uint64_t len)
489 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
490 if (unlikely(vq->log_guest_addr == 0))
492 __vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset,
497 static __rte_always_inline void
498 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
499 uint64_t offset, uint64_t len)
501 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
502 if (unlikely(vq->log_guest_addr == 0))
504 __vhost_log_write(dev, vq->log_guest_addr + offset, len);
508 static __rte_always_inline void
509 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
510 uint64_t iova, uint64_t len)
512 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
515 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
516 __vhost_log_cache_write_iova(dev, vq, iova, len);
518 __vhost_log_cache_write(dev, vq, iova, len);
521 static __rte_always_inline void
522 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
523 uint64_t iova, uint64_t len)
525 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
528 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
529 __vhost_log_write_iova(dev, vq, iova, len);
531 __vhost_log_write(dev, iova, len);
534 extern int vhost_config_log_level;
535 extern int vhost_data_log_level;
537 #define VHOST_LOG_CONFIG(level, fmt, args...) \
538 rte_log(RTE_LOG_ ## level, vhost_config_log_level, \
539 "VHOST_CONFIG: " fmt, ##args)
541 #define VHOST_LOG_DATA(level, fmt, args...) \
542 (void)((RTE_LOG_ ## level <= RTE_LOG_DP_LEVEL) ? \
543 rte_log(RTE_LOG_ ## level, vhost_data_log_level, \
544 "VHOST_DATA : " fmt, ##args) : \
547 #ifdef RTE_LIBRTE_VHOST_DEBUG
548 #define VHOST_MAX_PRINT_BUFF 6072
549 #define PRINT_PACKET(device, addr, size, header) do { \
550 char *pkt_addr = (char *)(addr); \
551 unsigned int index; \
552 char packet[VHOST_MAX_PRINT_BUFF]; \
555 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
557 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
558 for (index = 0; index < (size); index++) { \
559 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
560 "%02hhx ", pkt_addr[index]); \
562 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
564 VHOST_LOG_DATA(DEBUG, "%s", packet); \
567 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
570 #define MAX_VHOST_DEVICE 1024
571 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
573 #define VHOST_BINARY_SEARCH_THRESH 256
575 static __rte_always_inline int guest_page_addrcmp(const void *p1,
578 const struct guest_page *page1 = (const struct guest_page *)p1;
579 const struct guest_page *page2 = (const struct guest_page *)p2;
581 if (page1->guest_phys_addr > page2->guest_phys_addr)
583 if (page1->guest_phys_addr < page2->guest_phys_addr)
589 static __rte_always_inline rte_iova_t
590 gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa,
591 uint64_t gpa_size, uint64_t *hpa_size)
594 struct guest_page *page;
595 struct guest_page key;
597 *hpa_size = gpa_size;
598 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
599 key.guest_phys_addr = gpa & ~(dev->guest_pages[0].size - 1);
600 page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
601 sizeof(struct guest_page), guest_page_addrcmp);
603 if (gpa + gpa_size <=
604 page->guest_phys_addr + page->size) {
605 return gpa - page->guest_phys_addr +
606 page->host_phys_addr;
607 } else if (gpa < page->guest_phys_addr +
609 *hpa_size = page->guest_phys_addr +
611 return gpa - page->guest_phys_addr +
612 page->host_phys_addr;
616 for (i = 0; i < dev->nr_guest_pages; i++) {
617 page = &dev->guest_pages[i];
619 if (gpa >= page->guest_phys_addr) {
620 if (gpa + gpa_size <=
621 page->guest_phys_addr + page->size) {
622 return gpa - page->guest_phys_addr +
623 page->host_phys_addr;
624 } else if (gpa < page->guest_phys_addr +
626 *hpa_size = page->guest_phys_addr +
628 return gpa - page->guest_phys_addr +
629 page->host_phys_addr;
639 /* Convert guest physical address to host physical address */
640 static __rte_always_inline rte_iova_t
641 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
646 hpa = gpa_to_first_hpa(dev, gpa, size, &hpa_size);
647 return hpa_size == size ? hpa : 0;
650 static __rte_always_inline uint64_t
651 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
653 struct rte_vhost_mem_region *r;
656 if (unlikely(!dev || !dev->mem))
659 for (i = 0; i < dev->mem->nregions; i++) {
660 r = &dev->mem->regions[i];
662 if (vva >= r->host_user_addr &&
663 vva + len < r->host_user_addr + r->size) {
664 return r->guest_phys_addr + vva - r->host_user_addr;
670 static __rte_always_inline struct virtio_net *
673 struct virtio_net *dev = vhost_devices[vid];
675 if (unlikely(!dev)) {
676 VHOST_LOG_CONFIG(ERR,
677 "(%d) device not found.\n", vid);
683 int vhost_new_device(void);
684 void cleanup_device(struct virtio_net *dev, int destroy);
685 void reset_device(struct virtio_net *dev);
686 void vhost_destroy_device(int);
687 void vhost_destroy_device_notify(struct virtio_net *dev);
689 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
690 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
691 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
693 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
695 void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev);
697 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
698 void vhost_setup_virtio_net(int vid, bool enable, bool legacy_ol_flags);
699 void vhost_enable_extbuf(int vid);
700 void vhost_enable_linearbuf(int vid);
701 int vhost_enable_guest_notification(struct virtio_net *dev,
702 struct vhost_virtqueue *vq, int enable);
704 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
707 * Backend-specific cleanup.
709 * TODO: fix it; we have one backend now
711 void vhost_backend_cleanup(struct virtio_net *dev);
713 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
714 uint64_t iova, uint64_t *len, uint8_t perm);
715 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
716 struct vhost_virtqueue *vq,
717 uint64_t desc_addr, uint64_t desc_len);
718 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
719 uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
721 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
723 static __rte_always_inline uint64_t
724 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
725 uint64_t iova, uint64_t *len, uint8_t perm)
727 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
728 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
730 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
733 #define vhost_avail_event(vr) \
734 (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
735 #define vhost_used_event(vr) \
736 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
739 * The following is used with VIRTIO_RING_F_EVENT_IDX.
740 * Assuming a given event_idx value from the other size, if we have
741 * just incremented index from old to new_idx, should we trigger an
744 static __rte_always_inline int
745 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
747 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
750 static __rte_always_inline void
751 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
753 /* Flush used->idx update before we read avail->flags. */
754 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
756 /* Don't kick guest if we don't reach index specified by guest. */
757 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
758 uint16_t old = vq->signalled_used;
759 uint16_t new = vq->last_used_idx;
760 bool signalled_used_valid = vq->signalled_used_valid;
762 vq->signalled_used = new;
763 vq->signalled_used_valid = true;
765 VHOST_LOG_DATA(DEBUG, "%s: used_event_idx=%d, old=%d, new=%d\n",
767 vhost_used_event(vq),
770 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
771 (vq->callfd >= 0)) ||
772 unlikely(!signalled_used_valid)) {
773 eventfd_write(vq->callfd, (eventfd_t) 1);
774 if (dev->notify_ops->guest_notified)
775 dev->notify_ops->guest_notified(dev->vid);
778 /* Kick the guest if necessary. */
779 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
780 && (vq->callfd >= 0)) {
781 eventfd_write(vq->callfd, (eventfd_t)1);
782 if (dev->notify_ops->guest_notified)
783 dev->notify_ops->guest_notified(dev->vid);
788 static __rte_always_inline void
789 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
791 uint16_t old, new, off, off_wrap;
792 bool signalled_used_valid, kick = false;
794 /* Flush used desc update. */
795 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
797 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
798 if (vq->driver_event->flags !=
799 VRING_EVENT_F_DISABLE)
804 old = vq->signalled_used;
805 new = vq->last_used_idx;
806 vq->signalled_used = new;
807 signalled_used_valid = vq->signalled_used_valid;
808 vq->signalled_used_valid = true;
810 if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
811 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
816 if (unlikely(!signalled_used_valid)) {
821 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
823 off_wrap = vq->driver_event->off_wrap;
824 off = off_wrap & ~(1 << 15);
829 if (vq->used_wrap_counter != off_wrap >> 15)
832 if (vhost_need_event(off, new, old))
836 eventfd_write(vq->callfd, (eventfd_t)1);
837 if (dev->notify_ops->guest_notified)
838 dev->notify_ops->guest_notified(dev->vid);
842 static __rte_always_inline void
843 free_ind_table(void *idesc)
848 static __rte_always_inline void
849 restore_mbuf(struct rte_mbuf *m)
851 uint32_t mbuf_size, priv_size;
854 priv_size = rte_pktmbuf_priv_size(m->pool);
855 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
856 /* start of buffer is after mbuf structure and priv data */
858 m->buf_addr = (char *)m + mbuf_size;
859 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
864 static __rte_always_inline bool
865 mbuf_is_consumed(struct rte_mbuf *m)
868 if (rte_mbuf_refcnt_read(m) > 1)
876 #endif /* _VHOST_NET_CDEV_H_ */