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
43 * Structure contains buffer address, length and descriptor index
44 * from vring to do scatter RX.
54 * A structure to hold some fields needed in zero copy code path,
55 * mainly for associating an mbuf with the right desc_idx.
58 struct rte_mbuf *mbuf;
63 TAILQ_ENTRY(zcopy_mbuf) next;
65 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
68 * Structure contains the info for each batched memory copy.
70 struct batch_copy_elem {
78 * Structure that contains the info for batched dirty logging.
80 struct log_cache_entry {
85 struct vring_used_elem_packed {
92 * Structure contains variables relevant to RX/TX virtqueues.
94 struct vhost_virtqueue {
96 struct vring_desc *desc;
97 struct vring_packed_desc *desc_packed;
100 struct vring_avail *avail;
101 struct vring_packed_desc_event *driver_event;
104 struct vring_used *used;
105 struct vring_packed_desc_event *device_event;
109 uint16_t last_avail_idx;
110 uint16_t last_used_idx;
111 /* Last used index we notify to front end. */
112 uint16_t signalled_used;
113 bool signalled_used_valid;
114 #define VIRTIO_INVALID_EVENTFD (-1)
115 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
117 /* Backend value to determine if device should started/stopped */
121 rte_spinlock_t access_lock;
123 /* Used to notify the guest (trigger interrupt) */
125 /* Currently unused as polling mode is enabled */
128 /* Physical address of used ring, for logging */
129 uint64_t log_guest_addr;
131 /* inflight share memory info */
133 struct rte_vhost_inflight_info_split *inflight_split;
134 struct rte_vhost_inflight_info_packed *inflight_packed;
136 struct rte_vhost_resubmit_info *resubmit_inflight;
137 uint64_t global_counter;
141 uint16_t last_zmbuf_idx;
142 struct zcopy_mbuf *zmbufs;
143 struct zcopy_mbuf_list zmbuf_list;
146 struct vring_used_elem *shadow_used_split;
147 struct vring_used_elem_packed *shadow_used_packed;
149 uint16_t shadow_used_idx;
150 struct vhost_vring_addr ring_addrs;
152 struct batch_copy_elem *batch_copy_elems;
153 uint16_t batch_copy_nb_elems;
154 bool used_wrap_counter;
155 bool avail_wrap_counter;
157 struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
158 uint16_t log_cache_nb_elem;
160 rte_rwlock_t iotlb_lock;
161 rte_rwlock_t iotlb_pending_lock;
162 struct rte_mempool *iotlb_pool;
163 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
165 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
166 } __rte_cache_aligned;
168 /* Old kernels have no such macros defined */
169 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
170 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
173 #ifndef VIRTIO_NET_F_MQ
174 #define VIRTIO_NET_F_MQ 22
177 #define VHOST_MAX_VRING 0x100
178 #define VHOST_MAX_QUEUE_PAIRS 0x80
180 #ifndef VIRTIO_NET_F_MTU
181 #define VIRTIO_NET_F_MTU 3
184 #ifndef VIRTIO_F_ANY_LAYOUT
185 #define VIRTIO_F_ANY_LAYOUT 27
188 /* Declare IOMMU related bits for older kernels */
189 #ifndef VIRTIO_F_IOMMU_PLATFORM
191 #define VIRTIO_F_IOMMU_PLATFORM 33
193 struct vhost_iotlb_msg {
197 #define VHOST_ACCESS_RO 0x1
198 #define VHOST_ACCESS_WO 0x2
199 #define VHOST_ACCESS_RW 0x3
201 #define VHOST_IOTLB_MISS 1
202 #define VHOST_IOTLB_UPDATE 2
203 #define VHOST_IOTLB_INVALIDATE 3
204 #define VHOST_IOTLB_ACCESS_FAIL 4
208 #define VHOST_IOTLB_MSG 0x1
213 struct vhost_iotlb_msg iotlb;
220 * Define virtio 1.0 for older kernels
222 #ifndef VIRTIO_F_VERSION_1
223 #define VIRTIO_F_VERSION_1 32
226 /* Declare packed ring related bits for older kernels */
227 #ifndef VIRTIO_F_RING_PACKED
229 #define VIRTIO_F_RING_PACKED 34
231 struct vring_packed_desc {
238 struct vring_packed_desc_event {
245 * Declare below packed ring defines unconditionally
246 * as Kernel header might use different names.
248 #define VRING_DESC_F_AVAIL (1ULL << 7)
249 #define VRING_DESC_F_USED (1ULL << 15)
251 #define VRING_EVENT_F_ENABLE 0x0
252 #define VRING_EVENT_F_DISABLE 0x1
253 #define VRING_EVENT_F_DESC 0x2
256 * Available and used descs are in same order
258 #ifndef VIRTIO_F_IN_ORDER
259 #define VIRTIO_F_IN_ORDER 35
262 /* Features supported by this builtin vhost-user net driver. */
263 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
264 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
265 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
266 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
267 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
268 (1ULL << VIRTIO_NET_F_MQ) | \
269 (1ULL << VIRTIO_F_VERSION_1) | \
270 (1ULL << VHOST_F_LOG_ALL) | \
271 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
272 (1ULL << VIRTIO_NET_F_GSO) | \
273 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
274 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
275 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
276 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
277 (1ULL << VIRTIO_NET_F_CSUM) | \
278 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
279 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
280 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
281 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
282 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
283 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
284 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
285 (1ULL << VIRTIO_NET_F_MTU) | \
286 (1ULL << VIRTIO_F_IN_ORDER) | \
287 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
288 (1ULL << VIRTIO_F_RING_PACKED))
292 uint64_t guest_phys_addr;
293 uint64_t host_phys_addr;
297 struct inflight_mem_info {
304 * Device structure contains all configuration information relating
308 /* Frontend (QEMU) memory and memory region information */
309 struct rte_vhost_memory *mem;
311 uint64_t protocol_features;
315 /* to tell if we need broadcast rarp packet */
316 rte_atomic16_t broadcast_rarp;
318 int dequeue_zero_copy;
321 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
322 struct inflight_mem_info *inflight_info;
323 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
324 char ifname[IF_NAME_SZ];
328 struct rte_ether_addr mac;
331 struct vhost_device_ops const *notify_ops;
333 uint32_t nr_guest_pages;
334 uint32_t max_guest_pages;
335 struct guest_page *guest_pages;
338 rte_spinlock_t slave_req_lock;
341 int postcopy_listening;
344 * Device id to identify a specific backend device.
345 * It's set to -1 for the default software implementation.
349 /* context data for the external message handlers */
351 /* pre and post vhost user message handlers for the device */
352 struct rte_vhost_user_extern_ops extern_ops;
353 } __rte_cache_aligned;
355 static __rte_always_inline bool
356 vq_is_packed(struct virtio_net *dev)
358 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
362 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
364 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
366 return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
367 wrap_counter != !!(flags & VRING_DESC_F_USED);
371 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
373 vq->last_used_idx += num;
374 if (vq->last_used_idx >= vq->size) {
375 vq->used_wrap_counter ^= 1;
376 vq->last_used_idx -= vq->size;
381 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
383 vq->last_avail_idx += num;
384 if (vq->last_avail_idx >= vq->size) {
385 vq->avail_wrap_counter ^= 1;
386 vq->last_avail_idx -= vq->size;
390 void __vhost_log_cache_write(struct virtio_net *dev,
391 struct vhost_virtqueue *vq,
392 uint64_t addr, uint64_t len);
393 void __vhost_log_cache_write_iova(struct virtio_net *dev,
394 struct vhost_virtqueue *vq,
395 uint64_t iova, uint64_t len);
396 void __vhost_log_cache_sync(struct virtio_net *dev,
397 struct vhost_virtqueue *vq);
398 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
399 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
400 uint64_t iova, uint64_t len);
402 static __rte_always_inline void
403 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
405 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
406 __vhost_log_write(dev, addr, len);
409 static __rte_always_inline void
410 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
412 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
413 __vhost_log_cache_sync(dev, vq);
416 static __rte_always_inline void
417 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
418 uint64_t addr, uint64_t len)
420 if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
421 __vhost_log_cache_write(dev, vq, addr, len);
424 static __rte_always_inline void
425 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
426 uint64_t offset, uint64_t len)
428 vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
431 static __rte_always_inline void
432 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
433 uint64_t offset, uint64_t len)
435 vhost_log_write(dev, vq->log_guest_addr + offset, len);
438 static __rte_always_inline void
439 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
440 uint64_t iova, uint64_t len)
442 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
445 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
446 __vhost_log_cache_write_iova(dev, vq, iova, len);
448 __vhost_log_cache_write(dev, vq, iova, len);
451 static __rte_always_inline void
452 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
453 uint64_t iova, uint64_t len)
455 if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
458 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
459 __vhost_log_write_iova(dev, vq, iova, len);
461 __vhost_log_write(dev, iova, len);
464 /* Macros for printing using RTE_LOG */
465 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
466 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
468 #ifdef RTE_LIBRTE_VHOST_DEBUG
469 #define VHOST_MAX_PRINT_BUFF 6072
470 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
471 RTE_LOG(DEBUG, log_type, fmt, ##args)
472 #define PRINT_PACKET(device, addr, size, header) do { \
473 char *pkt_addr = (char *)(addr); \
474 unsigned int index; \
475 char packet[VHOST_MAX_PRINT_BUFF]; \
478 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
480 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
481 for (index = 0; index < (size); index++) { \
482 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
483 "%02hhx ", pkt_addr[index]); \
485 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
487 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
490 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
491 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
494 extern uint64_t VHOST_FEATURES;
495 #define MAX_VHOST_DEVICE 1024
496 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
498 /* Convert guest physical address to host physical address */
499 static __rte_always_inline rte_iova_t
500 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
503 struct guest_page *page;
505 for (i = 0; i < dev->nr_guest_pages; i++) {
506 page = &dev->guest_pages[i];
508 if (gpa >= page->guest_phys_addr &&
509 gpa + size < page->guest_phys_addr + page->size) {
510 return gpa - page->guest_phys_addr +
511 page->host_phys_addr;
518 static __rte_always_inline uint64_t
519 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
521 struct rte_vhost_mem_region *r;
524 if (unlikely(!dev || !dev->mem))
527 for (i = 0; i < dev->mem->nregions; i++) {
528 r = &dev->mem->regions[i];
530 if (vva >= r->host_user_addr &&
531 vva + len < r->host_user_addr + r->size) {
532 return r->guest_phys_addr + vva - r->host_user_addr;
538 static __rte_always_inline struct virtio_net *
541 struct virtio_net *dev = vhost_devices[vid];
543 if (unlikely(!dev)) {
544 RTE_LOG(ERR, VHOST_CONFIG,
545 "(%d) device not found.\n", vid);
551 int vhost_new_device(void);
552 void cleanup_device(struct virtio_net *dev, int destroy);
553 void reset_device(struct virtio_net *dev);
554 void vhost_destroy_device(int);
555 void vhost_destroy_device_notify(struct virtio_net *dev);
557 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
558 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
559 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
561 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
563 void vhost_attach_vdpa_device(int vid, int did);
565 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
566 void vhost_enable_dequeue_zero_copy(int vid);
567 void vhost_set_builtin_virtio_net(int vid, bool enable);
568 void vhost_enable_extbuf(int vid);
569 void vhost_enable_linearbuf(int vid);
571 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
574 * Backend-specific cleanup.
576 * TODO: fix it; we have one backend now
578 void vhost_backend_cleanup(struct virtio_net *dev);
580 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
581 uint64_t iova, uint64_t *len, uint8_t perm);
582 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
583 struct vhost_virtqueue *vq,
584 uint64_t desc_addr, uint64_t desc_len);
585 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
586 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
588 static __rte_always_inline uint64_t
589 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
590 uint64_t iova, uint64_t *len, uint8_t perm)
592 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
593 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
595 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
598 #define vhost_avail_event(vr) \
599 (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
600 #define vhost_used_event(vr) \
601 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
604 * The following is used with VIRTIO_RING_F_EVENT_IDX.
605 * Assuming a given event_idx value from the other size, if we have
606 * just incremented index from old to new_idx, should we trigger an
609 static __rte_always_inline int
610 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
612 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
615 static __rte_always_inline void
616 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
618 /* Flush used->idx update before we read avail->flags. */
621 /* Don't kick guest if we don't reach index specified by guest. */
622 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
623 uint16_t old = vq->signalled_used;
624 uint16_t new = vq->last_used_idx;
625 bool signalled_used_valid = vq->signalled_used_valid;
627 vq->signalled_used = new;
628 vq->signalled_used_valid = true;
630 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
632 vhost_used_event(vq),
635 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
636 (vq->callfd >= 0)) ||
637 unlikely(!signalled_used_valid)) {
638 eventfd_write(vq->callfd, (eventfd_t) 1);
639 if (dev->notify_ops->guest_notified)
640 dev->notify_ops->guest_notified(dev->vid);
643 /* Kick the guest if necessary. */
644 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
645 && (vq->callfd >= 0)) {
646 eventfd_write(vq->callfd, (eventfd_t)1);
647 if (dev->notify_ops->guest_notified)
648 dev->notify_ops->guest_notified(dev->vid);
653 static __rte_always_inline void
654 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
656 uint16_t old, new, off, off_wrap;
657 bool signalled_used_valid, kick = false;
659 /* Flush used desc update. */
662 if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
663 if (vq->driver_event->flags !=
664 VRING_EVENT_F_DISABLE)
669 old = vq->signalled_used;
670 new = vq->last_used_idx;
671 vq->signalled_used = new;
672 signalled_used_valid = vq->signalled_used_valid;
673 vq->signalled_used_valid = true;
675 if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
676 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
681 if (unlikely(!signalled_used_valid)) {
688 off_wrap = vq->driver_event->off_wrap;
689 off = off_wrap & ~(1 << 15);
694 if (vq->used_wrap_counter != off_wrap >> 15)
697 if (vhost_need_event(off, new, old))
701 eventfd_write(vq->callfd, (eventfd_t)1);
702 if (dev->notify_ops->guest_notified)
703 dev->notify_ops->guest_notified(dev->vid);
707 static __rte_always_inline void
708 free_ind_table(void *idesc)
713 static __rte_always_inline void
714 restore_mbuf(struct rte_mbuf *m)
716 uint32_t mbuf_size, priv_size;
719 priv_size = rte_pktmbuf_priv_size(m->pool);
720 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
721 /* start of buffer is after mbuf structure and priv data */
723 m->buf_addr = (char *)m + mbuf_size;
724 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
729 static __rte_always_inline bool
730 mbuf_is_consumed(struct rte_mbuf *m)
733 if (rte_mbuf_refcnt_read(m) > 1)
741 static __rte_always_inline void
742 put_zmbuf(struct zcopy_mbuf *zmbuf)
747 #endif /* _VHOST_NET_CDEV_H_ */