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"
26 /* Used to indicate that the device is running on a data core */
27 #define VIRTIO_DEV_RUNNING 1
28 /* Used to indicate that the device is ready to operate */
29 #define VIRTIO_DEV_READY 2
30 /* Used to indicate that the built-in vhost net device backend is enabled */
31 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
32 /* Used to indicate that the device has its own data path and configured */
33 #define VIRTIO_DEV_VDPA_CONFIGURED 8
35 /* Backend value set by guest. */
36 #define VIRTIO_DEV_STOPPED -1
38 #define BUF_VECTOR_MAX 256
40 #define VHOST_LOG_CACHE_NR 32
42 #define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \
43 sizeof(struct vring_packed_desc))
44 #define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
46 #ifdef VHOST_GCC_UNROLL_PRAGMA
47 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \
48 for (iter = val; iter < size; iter++)
51 #ifdef VHOST_CLANG_UNROLL_PRAGMA
52 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \
53 for (iter = val; iter < size; iter++)
56 #ifdef VHOST_ICC_UNROLL_PRAGMA
57 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \
58 for (iter = val; iter < size; iter++)
61 #ifndef vhost_for_each_try_unroll
62 #define vhost_for_each_try_unroll(iter, val, num) \
63 for (iter = val; iter < num; iter++)
67 * Structure contains buffer address, length and descriptor index
68 * from vring to do scatter RX.
78 * A structure to hold some fields needed in zero copy code path,
79 * mainly for associating an mbuf with the right desc_idx.
82 struct rte_mbuf *mbuf;
87 TAILQ_ENTRY(zcopy_mbuf) next;
89 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
92 * Structure contains the info for each batched memory copy.
94 struct batch_copy_elem {
102 * Structure that contains the info for batched dirty logging.
104 struct log_cache_entry {
109 struct vring_used_elem_packed {
116 * Structure contains variables relevant to RX/TX virtqueues.
118 struct vhost_virtqueue {
120 struct vring_desc *desc;
121 struct vring_packed_desc *desc_packed;
124 struct vring_avail *avail;
125 struct vring_packed_desc_event *driver_event;
128 struct vring_used *used;
129 struct vring_packed_desc_event *device_event;
133 uint16_t last_avail_idx;
134 uint16_t last_used_idx;
135 /* Last used index we notify to front end. */
136 uint16_t signalled_used;
137 bool signalled_used_valid;
138 #define VIRTIO_INVALID_EVENTFD (-1)
139 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
141 /* Backend value to determine if device should started/stopped */
145 rte_spinlock_t access_lock;
147 /* Used to notify the guest (trigger interrupt) */
149 /* Currently unused as polling mode is enabled */
152 /* Physical address of used ring, for logging */
153 uint64_t log_guest_addr;
155 /* inflight share memory info */
157 struct rte_vhost_inflight_info_split *inflight_split;
158 struct rte_vhost_inflight_info_packed *inflight_packed;
160 struct rte_vhost_resubmit_info *resubmit_inflight;
161 uint64_t global_counter;
165 uint16_t last_zmbuf_idx;
166 struct zcopy_mbuf *zmbufs;
167 struct zcopy_mbuf_list zmbuf_list;
170 struct vring_used_elem *shadow_used_split;
171 struct vring_used_elem_packed *shadow_used_packed;
173 uint16_t shadow_used_idx;
174 struct vhost_vring_addr ring_addrs;
176 struct batch_copy_elem *batch_copy_elems;
177 uint16_t batch_copy_nb_elems;
178 bool used_wrap_counter;
179 bool avail_wrap_counter;
181 struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
182 uint16_t log_cache_nb_elem;
184 rte_rwlock_t iotlb_lock;
185 rte_rwlock_t iotlb_pending_lock;
186 struct rte_mempool *iotlb_pool;
187 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
189 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
190 } __rte_cache_aligned;
192 /* Old kernels have no such macros defined */
193 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
194 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
197 #ifndef VIRTIO_NET_F_MQ
198 #define VIRTIO_NET_F_MQ 22
201 #define VHOST_MAX_VRING 0x100
202 #define VHOST_MAX_QUEUE_PAIRS 0x80
204 #ifndef VIRTIO_NET_F_MTU
205 #define VIRTIO_NET_F_MTU 3
208 #ifndef VIRTIO_F_ANY_LAYOUT
209 #define VIRTIO_F_ANY_LAYOUT 27
212 /* Declare IOMMU related bits for older kernels */
213 #ifndef VIRTIO_F_IOMMU_PLATFORM
215 #define VIRTIO_F_IOMMU_PLATFORM 33
217 struct vhost_iotlb_msg {
221 #define VHOST_ACCESS_RO 0x1
222 #define VHOST_ACCESS_WO 0x2
223 #define VHOST_ACCESS_RW 0x3
225 #define VHOST_IOTLB_MISS 1
226 #define VHOST_IOTLB_UPDATE 2
227 #define VHOST_IOTLB_INVALIDATE 3
228 #define VHOST_IOTLB_ACCESS_FAIL 4
232 #define VHOST_IOTLB_MSG 0x1
237 struct vhost_iotlb_msg iotlb;
244 * Define virtio 1.0 for older kernels
246 #ifndef VIRTIO_F_VERSION_1
247 #define VIRTIO_F_VERSION_1 32
250 /* Declare packed ring related bits for older kernels */
251 #ifndef VIRTIO_F_RING_PACKED
253 #define VIRTIO_F_RING_PACKED 34
255 struct vring_packed_desc {
262 struct vring_packed_desc_event {
269 * Declare below packed ring defines unconditionally
270 * as Kernel header might use different names.
272 #define VRING_DESC_F_AVAIL (1ULL << 7)
273 #define VRING_DESC_F_USED (1ULL << 15)
275 #define VRING_EVENT_F_ENABLE 0x0
276 #define VRING_EVENT_F_DISABLE 0x1
277 #define VRING_EVENT_F_DESC 0x2
280 * Available and used descs are in same order
282 #ifndef VIRTIO_F_IN_ORDER
283 #define VIRTIO_F_IN_ORDER 35
286 /* Features supported by this builtin vhost-user net driver. */
287 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
288 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
289 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
290 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
291 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
292 (1ULL << VIRTIO_NET_F_MQ) | \
293 (1ULL << VIRTIO_F_VERSION_1) | \
294 (1ULL << VHOST_F_LOG_ALL) | \
295 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
296 (1ULL << VIRTIO_NET_F_GSO) | \
297 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
298 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
299 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
300 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
301 (1ULL << VIRTIO_NET_F_CSUM) | \
302 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
303 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
304 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
305 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
306 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
307 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
308 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
309 (1ULL << VIRTIO_NET_F_MTU) | \
310 (1ULL << VIRTIO_F_IN_ORDER) | \
311 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
312 (1ULL << VIRTIO_F_RING_PACKED))
316 uint64_t guest_phys_addr;
317 uint64_t host_phys_addr;
321 struct inflight_mem_info {
328 * Device structure contains all configuration information relating
332 /* Frontend (QEMU) memory and memory region information */
333 struct rte_vhost_memory *mem;
335 uint64_t protocol_features;
339 /* to tell if we need broadcast rarp packet */
340 rte_atomic16_t broadcast_rarp;
342 int dequeue_zero_copy;
345 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
346 struct inflight_mem_info *inflight_info;
347 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
348 char ifname[IF_NAME_SZ];
352 struct rte_ether_addr mac;
355 struct vhost_device_ops const *notify_ops;
357 uint32_t nr_guest_pages;
358 uint32_t max_guest_pages;
359 struct guest_page *guest_pages;
362 rte_spinlock_t slave_req_lock;
365 int postcopy_listening;
368 * Device id to identify a specific backend device.
369 * It's set to -1 for the default software implementation.
373 /* context data for the external message handlers */
375 /* pre and post vhost user message handlers for the device */
376 struct rte_vhost_user_extern_ops extern_ops;
377 } __rte_cache_aligned;
379 static __rte_always_inline bool
380 vq_is_packed(struct virtio_net *dev)
382 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
386 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
388 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
390 return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
391 wrap_counter != !!(flags & VRING_DESC_F_USED);
395 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
397 vq->last_used_idx += num;
398 if (vq->last_used_idx >= vq->size) {
399 vq->used_wrap_counter ^= 1;
400 vq->last_used_idx -= vq->size;
405 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
407 vq->last_avail_idx += num;
408 if (vq->last_avail_idx >= vq->size) {
409 vq->avail_wrap_counter ^= 1;
410 vq->last_avail_idx -= vq->size;
414 void __vhost_log_cache_write(struct virtio_net *dev,
415 struct vhost_virtqueue *vq,
416 uint64_t addr, uint64_t len);
417 void __vhost_log_cache_write_iova(struct virtio_net *dev,
418 struct vhost_virtqueue *vq,
419 uint64_t iova, uint64_t len);
420 void __vhost_log_cache_sync(struct virtio_net *dev,
421 struct vhost_virtqueue *vq);
422 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
423 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
424 uint64_t iova, uint64_t len);
426 static __rte_always_inline void
427 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
429 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
430 __vhost_log_write(dev, addr, len);
433 static __rte_always_inline void
434 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
436 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
437 __vhost_log_cache_sync(dev, vq);
440 static __rte_always_inline void
441 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
442 uint64_t addr, uint64_t len)
444 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
445 __vhost_log_cache_write(dev, vq, addr, len);
448 static __rte_always_inline void
449 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
450 uint64_t offset, uint64_t len)
452 vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
455 static __rte_always_inline void
456 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
457 uint64_t offset, uint64_t len)
459 vhost_log_write(dev, vq->log_guest_addr + offset, len);
462 static __rte_always_inline void
463 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
464 uint64_t iova, uint64_t len)
466 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
469 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
470 __vhost_log_cache_write_iova(dev, vq, iova, len);
472 __vhost_log_cache_write(dev, vq, iova, len);
475 static __rte_always_inline void
476 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
477 uint64_t iova, uint64_t len)
479 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
482 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
483 __vhost_log_write_iova(dev, vq, iova, len);
485 __vhost_log_write(dev, iova, len);
488 /* Macros for printing using RTE_LOG */
489 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
490 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
492 #ifdef RTE_LIBRTE_VHOST_DEBUG
493 #define VHOST_MAX_PRINT_BUFF 6072
494 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
495 RTE_LOG(DEBUG, log_type, fmt, ##args)
496 #define PRINT_PACKET(device, addr, size, header) do { \
497 char *pkt_addr = (char *)(addr); \
498 unsigned int index; \
499 char packet[VHOST_MAX_PRINT_BUFF]; \
502 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
504 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
505 for (index = 0; index < (size); index++) { \
506 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
507 "%02hhx ", pkt_addr[index]); \
509 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
511 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
514 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
515 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
518 extern uint64_t VHOST_FEATURES;
519 #define MAX_VHOST_DEVICE 1024
520 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
522 /* Convert guest physical address to host physical address */
523 static __rte_always_inline rte_iova_t
524 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
527 struct guest_page *page;
529 for (i = 0; i < dev->nr_guest_pages; i++) {
530 page = &dev->guest_pages[i];
532 if (gpa >= page->guest_phys_addr &&
533 gpa + size < page->guest_phys_addr + page->size) {
534 return gpa - page->guest_phys_addr +
535 page->host_phys_addr;
542 static __rte_always_inline uint64_t
543 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
545 struct rte_vhost_mem_region *r;
548 if (unlikely(!dev || !dev->mem))
551 for (i = 0; i < dev->mem->nregions; i++) {
552 r = &dev->mem->regions[i];
554 if (vva >= r->host_user_addr &&
555 vva + len < r->host_user_addr + r->size) {
556 return r->guest_phys_addr + vva - r->host_user_addr;
562 static __rte_always_inline struct virtio_net *
565 struct virtio_net *dev = vhost_devices[vid];
567 if (unlikely(!dev)) {
568 RTE_LOG(ERR, VHOST_CONFIG,
569 "(%d) device not found.\n", vid);
575 int vhost_new_device(void);
576 void cleanup_device(struct virtio_net *dev, int destroy);
577 void reset_device(struct virtio_net *dev);
578 void vhost_destroy_device(int);
579 void vhost_destroy_device_notify(struct virtio_net *dev);
581 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
582 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
583 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
585 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
587 void vhost_attach_vdpa_device(int vid, int did);
589 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
590 void vhost_enable_dequeue_zero_copy(int vid);
591 void vhost_set_builtin_virtio_net(int vid, bool enable);
592 void vhost_enable_extbuf(int vid);
593 void vhost_enable_linearbuf(int vid);
595 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
598 * Backend-specific cleanup.
600 * TODO: fix it; we have one backend now
602 void vhost_backend_cleanup(struct virtio_net *dev);
604 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
605 uint64_t iova, uint64_t *len, uint8_t perm);
606 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
607 struct vhost_virtqueue *vq,
608 uint64_t desc_addr, uint64_t desc_len);
609 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
610 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
612 static __rte_always_inline uint64_t
613 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
614 uint64_t iova, uint64_t *len, uint8_t perm)
616 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
617 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
619 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
622 #define vhost_avail_event(vr) \
623 (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
624 #define vhost_used_event(vr) \
625 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
628 * The following is used with VIRTIO_RING_F_EVENT_IDX.
629 * Assuming a given event_idx value from the other size, if we have
630 * just incremented index from old to new_idx, should we trigger an
633 static __rte_always_inline int
634 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
636 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
639 static __rte_always_inline void
640 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
642 /* Flush used->idx update before we read avail->flags. */
645 /* Don't kick guest if we don't reach index specified by guest. */
646 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
647 uint16_t old = vq->signalled_used;
648 uint16_t new = vq->last_used_idx;
649 bool signalled_used_valid = vq->signalled_used_valid;
651 vq->signalled_used = new;
652 vq->signalled_used_valid = true;
654 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
656 vhost_used_event(vq),
659 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
660 (vq->callfd >= 0)) ||
661 unlikely(!signalled_used_valid)) {
662 eventfd_write(vq->callfd, (eventfd_t) 1);
663 if (dev->notify_ops->guest_notified)
664 dev->notify_ops->guest_notified(dev->vid);
667 /* Kick the guest if necessary. */
668 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
669 && (vq->callfd >= 0)) {
670 eventfd_write(vq->callfd, (eventfd_t)1);
671 if (dev->notify_ops->guest_notified)
672 dev->notify_ops->guest_notified(dev->vid);
677 static __rte_always_inline void
678 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
680 uint16_t old, new, off, off_wrap;
681 bool signalled_used_valid, kick = false;
683 /* Flush used desc update. */
686 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
687 if (vq->driver_event->flags !=
688 VRING_EVENT_F_DISABLE)
693 old = vq->signalled_used;
694 new = vq->last_used_idx;
695 vq->signalled_used = new;
696 signalled_used_valid = vq->signalled_used_valid;
697 vq->signalled_used_valid = true;
699 if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
700 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
705 if (unlikely(!signalled_used_valid)) {
712 off_wrap = vq->driver_event->off_wrap;
713 off = off_wrap & ~(1 << 15);
718 if (vq->used_wrap_counter != off_wrap >> 15)
721 if (vhost_need_event(off, new, old))
725 eventfd_write(vq->callfd, (eventfd_t)1);
726 if (dev->notify_ops->guest_notified)
727 dev->notify_ops->guest_notified(dev->vid);
731 static __rte_always_inline void
732 free_ind_table(void *idesc)
737 static __rte_always_inline void
738 restore_mbuf(struct rte_mbuf *m)
740 uint32_t mbuf_size, priv_size;
743 priv_size = rte_pktmbuf_priv_size(m->pool);
744 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
745 /* start of buffer is after mbuf structure and priv data */
747 m->buf_addr = (char *)m + mbuf_size;
748 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
753 static __rte_always_inline bool
754 mbuf_is_consumed(struct rte_mbuf *m)
757 if (rte_mbuf_refcnt_read(m) > 1)
765 static __rte_always_inline void
766 put_zmbuf(struct zcopy_mbuf *zmbuf)
771 #endif /* _VHOST_NET_CDEV_H_ */