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_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \
43 VRING_DESC_F_INDIRECT)
45 #define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \
46 sizeof(struct vring_packed_desc))
47 #define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
49 #ifdef VHOST_GCC_UNROLL_PRAGMA
50 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \
51 for (iter = val; iter < size; iter++)
54 #ifdef VHOST_CLANG_UNROLL_PRAGMA
55 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \
56 for (iter = val; iter < size; iter++)
59 #ifdef VHOST_ICC_UNROLL_PRAGMA
60 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \
61 for (iter = val; iter < size; iter++)
64 #ifndef vhost_for_each_try_unroll
65 #define vhost_for_each_try_unroll(iter, val, num) \
66 for (iter = val; iter < num; iter++)
70 * Structure contains buffer address, length and descriptor index
71 * from vring to do scatter RX.
81 * A structure to hold some fields needed in zero copy code path,
82 * mainly for associating an mbuf with the right desc_idx.
85 struct rte_mbuf *mbuf;
90 TAILQ_ENTRY(zcopy_mbuf) next;
92 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
95 * Structure contains the info for each batched memory copy.
97 struct batch_copy_elem {
105 * Structure that contains the info for batched dirty logging.
107 struct log_cache_entry {
112 struct vring_used_elem_packed {
119 * Structure contains variables relevant to RX/TX virtqueues.
121 struct vhost_virtqueue {
123 struct vring_desc *desc;
124 struct vring_packed_desc *desc_packed;
127 struct vring_avail *avail;
128 struct vring_packed_desc_event *driver_event;
131 struct vring_used *used;
132 struct vring_packed_desc_event *device_event;
136 uint16_t last_avail_idx;
137 uint16_t last_used_idx;
138 /* Last used index we notify to front end. */
139 uint16_t signalled_used;
140 bool signalled_used_valid;
141 #define VIRTIO_INVALID_EVENTFD (-1)
142 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
144 /* Backend value to determine if device should started/stopped */
148 rte_spinlock_t access_lock;
150 /* Used to notify the guest (trigger interrupt) */
152 /* Currently unused as polling mode is enabled */
155 /* Physical address of used ring, for logging */
156 uint64_t log_guest_addr;
158 /* inflight share memory info */
160 struct rte_vhost_inflight_info_split *inflight_split;
161 struct rte_vhost_inflight_info_packed *inflight_packed;
163 struct rte_vhost_resubmit_info *resubmit_inflight;
164 uint64_t global_counter;
168 uint16_t last_zmbuf_idx;
169 struct zcopy_mbuf *zmbufs;
170 struct zcopy_mbuf_list zmbuf_list;
173 struct vring_used_elem *shadow_used_split;
174 struct vring_used_elem_packed *shadow_used_packed;
176 uint16_t shadow_used_idx;
177 struct vhost_vring_addr ring_addrs;
179 struct batch_copy_elem *batch_copy_elems;
180 uint16_t batch_copy_nb_elems;
181 bool used_wrap_counter;
182 bool avail_wrap_counter;
184 struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
185 uint16_t log_cache_nb_elem;
187 rte_rwlock_t iotlb_lock;
188 rte_rwlock_t iotlb_pending_lock;
189 struct rte_mempool *iotlb_pool;
190 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
192 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
193 } __rte_cache_aligned;
195 /* Old kernels have no such macros defined */
196 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
197 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
200 #ifndef VIRTIO_NET_F_MQ
201 #define VIRTIO_NET_F_MQ 22
204 #define VHOST_MAX_VRING 0x100
205 #define VHOST_MAX_QUEUE_PAIRS 0x80
207 #ifndef VIRTIO_NET_F_MTU
208 #define VIRTIO_NET_F_MTU 3
211 #ifndef VIRTIO_F_ANY_LAYOUT
212 #define VIRTIO_F_ANY_LAYOUT 27
215 /* Declare IOMMU related bits for older kernels */
216 #ifndef VIRTIO_F_IOMMU_PLATFORM
218 #define VIRTIO_F_IOMMU_PLATFORM 33
220 struct vhost_iotlb_msg {
224 #define VHOST_ACCESS_RO 0x1
225 #define VHOST_ACCESS_WO 0x2
226 #define VHOST_ACCESS_RW 0x3
228 #define VHOST_IOTLB_MISS 1
229 #define VHOST_IOTLB_UPDATE 2
230 #define VHOST_IOTLB_INVALIDATE 3
231 #define VHOST_IOTLB_ACCESS_FAIL 4
235 #define VHOST_IOTLB_MSG 0x1
240 struct vhost_iotlb_msg iotlb;
247 * Define virtio 1.0 for older kernels
249 #ifndef VIRTIO_F_VERSION_1
250 #define VIRTIO_F_VERSION_1 32
253 /* Declare packed ring related bits for older kernels */
254 #ifndef VIRTIO_F_RING_PACKED
256 #define VIRTIO_F_RING_PACKED 34
258 struct vring_packed_desc {
265 struct vring_packed_desc_event {
272 * Declare below packed ring defines unconditionally
273 * as Kernel header might use different names.
275 #define VRING_DESC_F_AVAIL (1ULL << 7)
276 #define VRING_DESC_F_USED (1ULL << 15)
278 #define VRING_EVENT_F_ENABLE 0x0
279 #define VRING_EVENT_F_DISABLE 0x1
280 #define VRING_EVENT_F_DESC 0x2
283 * Available and used descs are in same order
285 #ifndef VIRTIO_F_IN_ORDER
286 #define VIRTIO_F_IN_ORDER 35
289 /* Features supported by this builtin vhost-user net driver. */
290 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
291 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
292 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
293 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
294 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
295 (1ULL << VIRTIO_NET_F_MQ) | \
296 (1ULL << VIRTIO_F_VERSION_1) | \
297 (1ULL << VHOST_F_LOG_ALL) | \
298 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
299 (1ULL << VIRTIO_NET_F_GSO) | \
300 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
301 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
302 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
303 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
304 (1ULL << VIRTIO_NET_F_CSUM) | \
305 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
306 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
307 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
308 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
309 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
310 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
311 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
312 (1ULL << VIRTIO_NET_F_MTU) | \
313 (1ULL << VIRTIO_F_IN_ORDER) | \
314 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
315 (1ULL << VIRTIO_F_RING_PACKED))
319 uint64_t guest_phys_addr;
320 uint64_t host_phys_addr;
324 struct inflight_mem_info {
331 * Device structure contains all configuration information relating
335 /* Frontend (QEMU) memory and memory region information */
336 struct rte_vhost_memory *mem;
338 uint64_t protocol_features;
342 /* to tell if we need broadcast rarp packet */
343 rte_atomic16_t broadcast_rarp;
345 int dequeue_zero_copy;
348 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
349 struct inflight_mem_info *inflight_info;
350 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
351 char ifname[IF_NAME_SZ];
355 struct rte_ether_addr mac;
358 struct vhost_device_ops const *notify_ops;
360 uint32_t nr_guest_pages;
361 uint32_t max_guest_pages;
362 struct guest_page *guest_pages;
365 rte_spinlock_t slave_req_lock;
368 int postcopy_listening;
371 * Device id to identify a specific backend device.
372 * It's set to -1 for the default software implementation.
376 /* context data for the external message handlers */
378 /* pre and post vhost user message handlers for the device */
379 struct rte_vhost_user_extern_ops extern_ops;
380 } __rte_cache_aligned;
382 static __rte_always_inline bool
383 vq_is_packed(struct virtio_net *dev)
385 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
389 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
391 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
393 return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
394 wrap_counter != !!(flags & VRING_DESC_F_USED);
398 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
400 vq->last_used_idx += num;
401 if (vq->last_used_idx >= vq->size) {
402 vq->used_wrap_counter ^= 1;
403 vq->last_used_idx -= vq->size;
408 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
410 vq->last_avail_idx += num;
411 if (vq->last_avail_idx >= vq->size) {
412 vq->avail_wrap_counter ^= 1;
413 vq->last_avail_idx -= vq->size;
417 void __vhost_log_cache_write(struct virtio_net *dev,
418 struct vhost_virtqueue *vq,
419 uint64_t addr, uint64_t len);
420 void __vhost_log_cache_write_iova(struct virtio_net *dev,
421 struct vhost_virtqueue *vq,
422 uint64_t iova, uint64_t len);
423 void __vhost_log_cache_sync(struct virtio_net *dev,
424 struct vhost_virtqueue *vq);
425 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
426 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
427 uint64_t iova, uint64_t len);
429 static __rte_always_inline void
430 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
432 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
433 __vhost_log_write(dev, addr, len);
436 static __rte_always_inline void
437 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
439 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
440 __vhost_log_cache_sync(dev, vq);
443 static __rte_always_inline void
444 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
445 uint64_t addr, uint64_t len)
447 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
448 __vhost_log_cache_write(dev, vq, addr, len);
451 static __rte_always_inline void
452 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
453 uint64_t offset, uint64_t len)
455 vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
458 static __rte_always_inline void
459 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
460 uint64_t offset, uint64_t len)
462 vhost_log_write(dev, vq->log_guest_addr + offset, len);
465 static __rte_always_inline void
466 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
467 uint64_t iova, uint64_t len)
469 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
472 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
473 __vhost_log_cache_write_iova(dev, vq, iova, len);
475 __vhost_log_cache_write(dev, vq, iova, len);
478 static __rte_always_inline void
479 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
480 uint64_t iova, uint64_t len)
482 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
485 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
486 __vhost_log_write_iova(dev, vq, iova, len);
488 __vhost_log_write(dev, iova, len);
491 /* Macros for printing using RTE_LOG */
492 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
493 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
495 #ifdef RTE_LIBRTE_VHOST_DEBUG
496 #define VHOST_MAX_PRINT_BUFF 6072
497 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
498 RTE_LOG(DEBUG, log_type, fmt, ##args)
499 #define PRINT_PACKET(device, addr, size, header) do { \
500 char *pkt_addr = (char *)(addr); \
501 unsigned int index; \
502 char packet[VHOST_MAX_PRINT_BUFF]; \
505 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
507 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
508 for (index = 0; index < (size); index++) { \
509 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
510 "%02hhx ", pkt_addr[index]); \
512 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
514 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
517 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
518 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
521 extern uint64_t VHOST_FEATURES;
522 #define MAX_VHOST_DEVICE 1024
523 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
525 /* Convert guest physical address to host physical address */
526 static __rte_always_inline rte_iova_t
527 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
530 struct guest_page *page;
532 for (i = 0; i < dev->nr_guest_pages; i++) {
533 page = &dev->guest_pages[i];
535 if (gpa >= page->guest_phys_addr &&
536 gpa + size < page->guest_phys_addr + page->size) {
537 return gpa - page->guest_phys_addr +
538 page->host_phys_addr;
545 static __rte_always_inline uint64_t
546 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
548 struct rte_vhost_mem_region *r;
551 if (unlikely(!dev || !dev->mem))
554 for (i = 0; i < dev->mem->nregions; i++) {
555 r = &dev->mem->regions[i];
557 if (vva >= r->host_user_addr &&
558 vva + len < r->host_user_addr + r->size) {
559 return r->guest_phys_addr + vva - r->host_user_addr;
565 static __rte_always_inline struct virtio_net *
568 struct virtio_net *dev = vhost_devices[vid];
570 if (unlikely(!dev)) {
571 RTE_LOG(ERR, VHOST_CONFIG,
572 "(%d) device not found.\n", vid);
578 int vhost_new_device(void);
579 void cleanup_device(struct virtio_net *dev, int destroy);
580 void reset_device(struct virtio_net *dev);
581 void vhost_destroy_device(int);
582 void vhost_destroy_device_notify(struct virtio_net *dev);
584 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
585 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
586 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
588 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
590 void vhost_attach_vdpa_device(int vid, int did);
592 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
593 void vhost_enable_dequeue_zero_copy(int vid);
594 void vhost_set_builtin_virtio_net(int vid, bool enable);
595 void vhost_enable_extbuf(int vid);
596 void vhost_enable_linearbuf(int vid);
598 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
601 * Backend-specific cleanup.
603 * TODO: fix it; we have one backend now
605 void vhost_backend_cleanup(struct virtio_net *dev);
607 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
608 uint64_t iova, uint64_t *len, uint8_t perm);
609 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
610 struct vhost_virtqueue *vq,
611 uint64_t desc_addr, uint64_t desc_len);
612 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
613 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
615 static __rte_always_inline uint64_t
616 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
617 uint64_t iova, uint64_t *len, uint8_t perm)
619 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
620 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
622 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
625 #define vhost_avail_event(vr) \
626 (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
627 #define vhost_used_event(vr) \
628 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
631 * The following is used with VIRTIO_RING_F_EVENT_IDX.
632 * Assuming a given event_idx value from the other size, if we have
633 * just incremented index from old to new_idx, should we trigger an
636 static __rte_always_inline int
637 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
639 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
642 static __rte_always_inline void
643 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
645 /* Flush used->idx update before we read avail->flags. */
648 /* Don't kick guest if we don't reach index specified by guest. */
649 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
650 uint16_t old = vq->signalled_used;
651 uint16_t new = vq->last_used_idx;
652 bool signalled_used_valid = vq->signalled_used_valid;
654 vq->signalled_used = new;
655 vq->signalled_used_valid = true;
657 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
659 vhost_used_event(vq),
662 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
663 (vq->callfd >= 0)) ||
664 unlikely(!signalled_used_valid)) {
665 eventfd_write(vq->callfd, (eventfd_t) 1);
666 if (dev->notify_ops->guest_notified)
667 dev->notify_ops->guest_notified(dev->vid);
670 /* Kick the guest if necessary. */
671 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
672 && (vq->callfd >= 0)) {
673 eventfd_write(vq->callfd, (eventfd_t)1);
674 if (dev->notify_ops->guest_notified)
675 dev->notify_ops->guest_notified(dev->vid);
680 static __rte_always_inline void
681 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
683 uint16_t old, new, off, off_wrap;
684 bool signalled_used_valid, kick = false;
686 /* Flush used desc update. */
689 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
690 if (vq->driver_event->flags !=
691 VRING_EVENT_F_DISABLE)
696 old = vq->signalled_used;
697 new = vq->last_used_idx;
698 vq->signalled_used = new;
699 signalled_used_valid = vq->signalled_used_valid;
700 vq->signalled_used_valid = true;
702 if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
703 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
708 if (unlikely(!signalled_used_valid)) {
715 off_wrap = vq->driver_event->off_wrap;
716 off = off_wrap & ~(1 << 15);
721 if (vq->used_wrap_counter != off_wrap >> 15)
724 if (vhost_need_event(off, new, old))
728 eventfd_write(vq->callfd, (eventfd_t)1);
729 if (dev->notify_ops->guest_notified)
730 dev->notify_ops->guest_notified(dev->vid);
734 static __rte_always_inline void
735 free_ind_table(void *idesc)
740 static __rte_always_inline void
741 restore_mbuf(struct rte_mbuf *m)
743 uint32_t mbuf_size, priv_size;
746 priv_size = rte_pktmbuf_priv_size(m->pool);
747 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
748 /* start of buffer is after mbuf structure and priv data */
750 m->buf_addr = (char *)m + mbuf_size;
751 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
756 static __rte_always_inline bool
757 mbuf_is_consumed(struct rte_mbuf *m)
760 if (rte_mbuf_refcnt_read(m) > 1)
768 static __rte_always_inline void
769 put_zmbuf(struct zcopy_mbuf *zmbuf)
774 #endif /* _VHOST_NET_CDEV_H_ */