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 * 2)
52 #define VHOST_MAX_ASYNC_VEC (BUF_VECTOR_MAX * 4)
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 * Structure contains variables relevant to RX/TX virtqueues.
125 struct vhost_virtqueue {
127 struct vring_desc *desc;
128 struct vring_packed_desc *desc_packed;
131 struct vring_avail *avail;
132 struct vring_packed_desc_event *driver_event;
135 struct vring_used *used;
136 struct vring_packed_desc_event *device_event;
140 uint16_t last_avail_idx;
141 uint16_t last_used_idx;
142 /* Last used index we notify to front end. */
143 uint16_t signalled_used;
144 bool signalled_used_valid;
145 #define VIRTIO_INVALID_EVENTFD (-1)
146 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
152 rte_spinlock_t access_lock;
156 struct vring_used_elem *shadow_used_split;
157 struct vring_used_elem_packed *shadow_used_packed;
159 uint16_t shadow_used_idx;
160 /* Record packed ring enqueue latest desc cache aligned index */
161 uint16_t shadow_aligned_idx;
162 /* Record packed ring first dequeue desc index */
163 uint16_t shadow_last_used_idx;
165 uint16_t batch_copy_nb_elems;
166 struct batch_copy_elem *batch_copy_elems;
168 bool used_wrap_counter;
169 bool avail_wrap_counter;
171 /* Physical address of used ring, for logging */
172 uint16_t log_cache_nb_elem;
173 uint64_t log_guest_addr;
174 struct log_cache_entry *log_cache;
176 rte_rwlock_t iotlb_lock;
177 rte_rwlock_t iotlb_pending_lock;
178 struct rte_mempool *iotlb_pool;
179 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
180 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
183 /* Used to notify the guest (trigger interrupt) */
185 /* Currently unused as polling mode is enabled */
188 /* inflight share memory info */
190 struct rte_vhost_inflight_info_split *inflight_split;
191 struct rte_vhost_inflight_info_packed *inflight_packed;
193 struct rte_vhost_resubmit_info *resubmit_inflight;
194 uint64_t global_counter;
196 /* operation callbacks for async dma */
197 struct rte_vhost_async_channel_ops async_ops;
199 struct rte_vhost_iov_iter *it_pool;
200 struct iovec *vec_pool;
202 /* async data transfer status */
203 struct async_inflight_info *async_pkts_info;
204 uint16_t async_pkts_idx;
205 uint16_t async_pkts_inflight_n;
206 uint16_t async_last_pkts_n;
208 struct vring_used_elem *async_descs_split;
209 struct vring_used_elem_packed *async_buffers_packed;
212 uint16_t async_desc_idx_split;
213 uint16_t async_buffer_idx_packed;
216 uint16_t last_async_desc_idx_split;
217 uint16_t last_async_buffer_idx_packed;
220 /* vq async features */
221 bool async_registered;
222 uint32_t async_threshold;
225 #define VIRTIO_UNINITIALIZED_NOTIF (-1)
227 struct vhost_vring_addr ring_addrs;
228 } __rte_cache_aligned;
230 /* Virtio device status as per Virtio specification */
231 #define VIRTIO_DEVICE_STATUS_RESET 0x00
232 #define VIRTIO_DEVICE_STATUS_ACK 0x01
233 #define VIRTIO_DEVICE_STATUS_DRIVER 0x02
234 #define VIRTIO_DEVICE_STATUS_DRIVER_OK 0x04
235 #define VIRTIO_DEVICE_STATUS_FEATURES_OK 0x08
236 #define VIRTIO_DEVICE_STATUS_DEV_NEED_RESET 0x40
237 #define VIRTIO_DEVICE_STATUS_FAILED 0x80
239 #define VHOST_MAX_VRING 0x100
240 #define VHOST_MAX_QUEUE_PAIRS 0x80
242 /* Declare IOMMU related bits for older kernels */
243 #ifndef VIRTIO_F_IOMMU_PLATFORM
245 #define VIRTIO_F_IOMMU_PLATFORM 33
247 struct vhost_iotlb_msg {
251 #define VHOST_ACCESS_RO 0x1
252 #define VHOST_ACCESS_WO 0x2
253 #define VHOST_ACCESS_RW 0x3
255 #define VHOST_IOTLB_MISS 1
256 #define VHOST_IOTLB_UPDATE 2
257 #define VHOST_IOTLB_INVALIDATE 3
258 #define VHOST_IOTLB_ACCESS_FAIL 4
262 #define VHOST_IOTLB_MSG 0x1
267 struct vhost_iotlb_msg iotlb;
274 * Define virtio 1.0 for older kernels
276 #ifndef VIRTIO_F_VERSION_1
277 #define VIRTIO_F_VERSION_1 32
280 /* Declare packed ring related bits for older kernels */
281 #ifndef VIRTIO_F_RING_PACKED
283 #define VIRTIO_F_RING_PACKED 34
285 struct vring_packed_desc {
292 struct vring_packed_desc_event {
299 * Declare below packed ring defines unconditionally
300 * as Kernel header might use different names.
302 #define VRING_DESC_F_AVAIL (1ULL << 7)
303 #define VRING_DESC_F_USED (1ULL << 15)
305 #define VRING_EVENT_F_ENABLE 0x0
306 #define VRING_EVENT_F_DISABLE 0x1
307 #define VRING_EVENT_F_DESC 0x2
310 * Available and used descs are in same order
312 #ifndef VIRTIO_F_IN_ORDER
313 #define VIRTIO_F_IN_ORDER 35
316 /* Features supported by this builtin vhost-user net driver. */
317 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
318 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
319 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
320 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
321 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
322 (1ULL << VIRTIO_NET_F_MQ) | \
323 (1ULL << VIRTIO_F_VERSION_1) | \
324 (1ULL << VHOST_F_LOG_ALL) | \
325 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
326 (1ULL << VIRTIO_NET_F_GSO) | \
327 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
328 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
329 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
330 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
331 (1ULL << VIRTIO_NET_F_CSUM) | \
332 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
333 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
334 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
335 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
336 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
337 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
338 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
339 (1ULL << VIRTIO_NET_F_MTU) | \
340 (1ULL << VIRTIO_F_IN_ORDER) | \
341 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
342 (1ULL << VIRTIO_F_RING_PACKED))
346 uint64_t guest_phys_addr;
347 uint64_t host_phys_addr;
351 struct inflight_mem_info {
358 * Device structure contains all configuration information relating
362 /* Frontend (QEMU) memory and memory region information */
363 struct rte_vhost_memory *mem;
365 uint64_t protocol_features;
369 /* to tell if we need broadcast rarp packet */
370 int16_t broadcast_rarp;
375 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
376 struct inflight_mem_info *inflight_info;
377 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
378 char ifname[IF_NAME_SZ];
382 struct rte_ether_addr mac;
386 struct vhost_device_ops const *notify_ops;
388 uint32_t nr_guest_pages;
389 uint32_t max_guest_pages;
390 struct guest_page *guest_pages;
393 rte_spinlock_t slave_req_lock;
396 int postcopy_listening;
398 struct rte_vdpa_device *vdpa_dev;
400 /* context data for the external message handlers */
402 /* pre and post vhost user message handlers for the device */
403 struct rte_vhost_user_extern_ops extern_ops;
404 } __rte_cache_aligned;
406 static __rte_always_inline bool
407 vq_is_packed(struct virtio_net *dev)
409 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
413 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
415 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
417 return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
418 wrap_counter != !!(flags & VRING_DESC_F_USED);
422 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
424 vq->last_used_idx += num;
425 if (vq->last_used_idx >= vq->size) {
426 vq->used_wrap_counter ^= 1;
427 vq->last_used_idx -= vq->size;
432 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
434 vq->last_avail_idx += num;
435 if (vq->last_avail_idx >= vq->size) {
436 vq->avail_wrap_counter ^= 1;
437 vq->last_avail_idx -= vq->size;
441 void __vhost_log_cache_write(struct virtio_net *dev,
442 struct vhost_virtqueue *vq,
443 uint64_t addr, uint64_t len);
444 void __vhost_log_cache_write_iova(struct virtio_net *dev,
445 struct vhost_virtqueue *vq,
446 uint64_t iova, uint64_t len);
447 void __vhost_log_cache_sync(struct virtio_net *dev,
448 struct vhost_virtqueue *vq);
449 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
450 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
451 uint64_t iova, uint64_t len);
453 static __rte_always_inline void
454 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
456 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
457 __vhost_log_write(dev, addr, len);
460 static __rte_always_inline void
461 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
463 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
464 __vhost_log_cache_sync(dev, vq);
467 static __rte_always_inline void
468 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
469 uint64_t addr, uint64_t len)
471 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
472 __vhost_log_cache_write(dev, vq, addr, len);
475 static __rte_always_inline void
476 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
477 uint64_t offset, uint64_t len)
479 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
480 if (unlikely(vq->log_guest_addr == 0))
482 __vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset,
487 static __rte_always_inline void
488 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
489 uint64_t offset, uint64_t len)
491 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
492 if (unlikely(vq->log_guest_addr == 0))
494 __vhost_log_write(dev, vq->log_guest_addr + offset, len);
498 static __rte_always_inline void
499 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
500 uint64_t iova, uint64_t len)
502 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
505 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
506 __vhost_log_cache_write_iova(dev, vq, iova, len);
508 __vhost_log_cache_write(dev, vq, iova, len);
511 static __rte_always_inline void
512 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
513 uint64_t iova, uint64_t len)
515 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
518 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
519 __vhost_log_write_iova(dev, vq, iova, len);
521 __vhost_log_write(dev, iova, len);
524 extern int vhost_config_log_level;
525 extern int vhost_data_log_level;
527 #define VHOST_LOG_CONFIG(level, fmt, args...) \
528 rte_log(RTE_LOG_ ## level, vhost_config_log_level, \
529 "VHOST_CONFIG: " fmt, ##args)
531 #define VHOST_LOG_DATA(level, fmt, args...) \
532 (void)((RTE_LOG_ ## level <= RTE_LOG_DP_LEVEL) ? \
533 rte_log(RTE_LOG_ ## level, vhost_data_log_level, \
534 "VHOST_DATA : " fmt, ##args) : \
537 #ifdef RTE_LIBRTE_VHOST_DEBUG
538 #define VHOST_MAX_PRINT_BUFF 6072
539 #define PRINT_PACKET(device, addr, size, header) do { \
540 char *pkt_addr = (char *)(addr); \
541 unsigned int index; \
542 char packet[VHOST_MAX_PRINT_BUFF]; \
545 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
547 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
548 for (index = 0; index < (size); index++) { \
549 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
550 "%02hhx ", pkt_addr[index]); \
552 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
554 VHOST_LOG_DATA(DEBUG, "%s", packet); \
557 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
560 #define MAX_VHOST_DEVICE 1024
561 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
563 #define VHOST_BINARY_SEARCH_THRESH 256
565 static __rte_always_inline int guest_page_addrcmp(const void *p1,
568 const struct guest_page *page1 = (const struct guest_page *)p1;
569 const struct guest_page *page2 = (const struct guest_page *)p2;
571 if (page1->guest_phys_addr > page2->guest_phys_addr)
573 if (page1->guest_phys_addr < page2->guest_phys_addr)
579 static __rte_always_inline rte_iova_t
580 gpa_to_first_hpa(struct virtio_net *dev, uint64_t gpa,
581 uint64_t gpa_size, uint64_t *hpa_size)
584 struct guest_page *page;
585 struct guest_page key;
587 *hpa_size = gpa_size;
588 if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
589 key.guest_phys_addr = gpa & ~(dev->guest_pages[0].size - 1);
590 page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
591 sizeof(struct guest_page), guest_page_addrcmp);
593 if (gpa + gpa_size <=
594 page->guest_phys_addr + page->size) {
595 return gpa - page->guest_phys_addr +
596 page->host_phys_addr;
597 } else if (gpa < page->guest_phys_addr +
599 *hpa_size = page->guest_phys_addr +
601 return gpa - page->guest_phys_addr +
602 page->host_phys_addr;
606 for (i = 0; i < dev->nr_guest_pages; i++) {
607 page = &dev->guest_pages[i];
609 if (gpa >= page->guest_phys_addr) {
610 if (gpa + gpa_size <=
611 page->guest_phys_addr + page->size) {
612 return gpa - page->guest_phys_addr +
613 page->host_phys_addr;
614 } else if (gpa < page->guest_phys_addr +
616 *hpa_size = page->guest_phys_addr +
618 return gpa - page->guest_phys_addr +
619 page->host_phys_addr;
629 /* Convert guest physical address to host physical address */
630 static __rte_always_inline rte_iova_t
631 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
636 hpa = gpa_to_first_hpa(dev, gpa, size, &hpa_size);
637 return hpa_size == size ? hpa : 0;
640 static __rte_always_inline uint64_t
641 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
643 struct rte_vhost_mem_region *r;
646 if (unlikely(!dev || !dev->mem))
649 for (i = 0; i < dev->mem->nregions; i++) {
650 r = &dev->mem->regions[i];
652 if (vva >= r->host_user_addr &&
653 vva + len < r->host_user_addr + r->size) {
654 return r->guest_phys_addr + vva - r->host_user_addr;
660 static __rte_always_inline struct virtio_net *
663 struct virtio_net *dev = vhost_devices[vid];
665 if (unlikely(!dev)) {
666 VHOST_LOG_CONFIG(ERR,
667 "(%d) device not found.\n", vid);
673 int vhost_new_device(void);
674 void cleanup_device(struct virtio_net *dev, int destroy);
675 void reset_device(struct virtio_net *dev);
676 void vhost_destroy_device(int);
677 void vhost_destroy_device_notify(struct virtio_net *dev);
679 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
680 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
681 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
683 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
685 void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev);
687 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
688 void vhost_setup_virtio_net(int vid, bool enable, bool legacy_ol_flags);
689 void vhost_enable_extbuf(int vid);
690 void vhost_enable_linearbuf(int vid);
691 int vhost_enable_guest_notification(struct virtio_net *dev,
692 struct vhost_virtqueue *vq, int enable);
694 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
697 * Backend-specific cleanup.
699 * TODO: fix it; we have one backend now
701 void vhost_backend_cleanup(struct virtio_net *dev);
703 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
704 uint64_t iova, uint64_t *len, uint8_t perm);
705 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
706 struct vhost_virtqueue *vq,
707 uint64_t desc_addr, uint64_t desc_len);
708 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
709 uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
711 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
713 static __rte_always_inline uint64_t
714 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
715 uint64_t iova, uint64_t *len, uint8_t perm)
717 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
718 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
720 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
723 #define vhost_avail_event(vr) \
724 (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
725 #define vhost_used_event(vr) \
726 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
729 * The following is used with VIRTIO_RING_F_EVENT_IDX.
730 * Assuming a given event_idx value from the other size, if we have
731 * just incremented index from old to new_idx, should we trigger an
734 static __rte_always_inline int
735 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
737 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
740 static __rte_always_inline void
741 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
743 /* Flush used->idx update before we read avail->flags. */
744 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
746 /* Don't kick guest if we don't reach index specified by guest. */
747 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
748 uint16_t old = vq->signalled_used;
749 uint16_t new = vq->last_used_idx;
750 bool signalled_used_valid = vq->signalled_used_valid;
752 vq->signalled_used = new;
753 vq->signalled_used_valid = true;
755 VHOST_LOG_DATA(DEBUG, "%s: used_event_idx=%d, old=%d, new=%d\n",
757 vhost_used_event(vq),
760 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
761 (vq->callfd >= 0)) ||
762 unlikely(!signalled_used_valid)) {
763 eventfd_write(vq->callfd, (eventfd_t) 1);
764 if (dev->notify_ops->guest_notified)
765 dev->notify_ops->guest_notified(dev->vid);
768 /* Kick the guest if necessary. */
769 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
770 && (vq->callfd >= 0)) {
771 eventfd_write(vq->callfd, (eventfd_t)1);
772 if (dev->notify_ops->guest_notified)
773 dev->notify_ops->guest_notified(dev->vid);
778 static __rte_always_inline void
779 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
781 uint16_t old, new, off, off_wrap;
782 bool signalled_used_valid, kick = false;
784 /* Flush used desc update. */
785 rte_atomic_thread_fence(__ATOMIC_SEQ_CST);
787 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
788 if (vq->driver_event->flags !=
789 VRING_EVENT_F_DISABLE)
794 old = vq->signalled_used;
795 new = vq->last_used_idx;
796 vq->signalled_used = new;
797 signalled_used_valid = vq->signalled_used_valid;
798 vq->signalled_used_valid = true;
800 if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
801 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
806 if (unlikely(!signalled_used_valid)) {
811 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
813 off_wrap = vq->driver_event->off_wrap;
814 off = off_wrap & ~(1 << 15);
819 if (vq->used_wrap_counter != off_wrap >> 15)
822 if (vhost_need_event(off, new, old))
826 eventfd_write(vq->callfd, (eventfd_t)1);
827 if (dev->notify_ops->guest_notified)
828 dev->notify_ops->guest_notified(dev->vid);
832 static __rte_always_inline void
833 free_ind_table(void *idesc)
838 static __rte_always_inline void
839 restore_mbuf(struct rte_mbuf *m)
841 uint32_t mbuf_size, priv_size;
844 priv_size = rte_pktmbuf_priv_size(m->pool);
845 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
846 /* start of buffer is after mbuf structure and priv data */
848 m->buf_addr = (char *)m + mbuf_size;
849 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
854 static __rte_always_inline bool
855 mbuf_is_consumed(struct rte_mbuf *m)
858 if (rte_mbuf_refcnt_read(m) > 1)
866 #endif /* _VHOST_NET_CDEV_H_ */