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>
22 #include "rte_vhost.h"
25 /* Used to indicate that the device is running on a data core */
26 #define VIRTIO_DEV_RUNNING 1
27 /* Used to indicate that the device is ready to operate */
28 #define VIRTIO_DEV_READY 2
29 /* Used to indicate that the built-in vhost net device backend is enabled */
30 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
31 /* Used to indicate that the device has its own data path and configured */
32 #define VIRTIO_DEV_VDPA_CONFIGURED 8
34 /* Backend value set by guest. */
35 #define VIRTIO_DEV_STOPPED -1
37 #define BUF_VECTOR_MAX 256
39 #define VHOST_LOG_CACHE_NR 32
42 * Structure contains buffer address, length and descriptor index
43 * from vring to do scatter RX.
53 * A structure to hold some fields needed in zero copy code path,
54 * mainly for associating an mbuf with the right desc_idx.
57 struct rte_mbuf *mbuf;
61 TAILQ_ENTRY(zcopy_mbuf) next;
63 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
66 * Structure contains the info for each batched memory copy.
68 struct batch_copy_elem {
76 * Structure that contains the info for batched dirty logging.
78 struct log_cache_entry {
83 struct vring_used_elem_packed {
90 * Structure contains variables relevant to RX/TX virtqueues.
92 struct vhost_virtqueue {
94 struct vring_desc *desc;
95 struct vring_packed_desc *desc_packed;
97 struct vring_avail *avail;
98 struct vring_used *used;
101 uint16_t last_avail_idx;
102 uint16_t last_used_idx;
103 /* Last used index we notify to front end. */
104 uint16_t signalled_used;
105 #define VIRTIO_INVALID_EVENTFD (-1)
106 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
108 /* Backend value to determine if device should started/stopped */
112 rte_spinlock_t access_lock;
114 /* Used to notify the guest (trigger interrupt) */
116 /* Currently unused as polling mode is enabled */
119 /* Physical address of used ring, for logging */
120 uint64_t log_guest_addr;
124 uint16_t last_zmbuf_idx;
125 struct zcopy_mbuf *zmbufs;
126 struct zcopy_mbuf_list zmbuf_list;
129 struct vring_used_elem *shadow_used_split;
130 struct vring_used_elem_packed *shadow_used_packed;
132 uint16_t shadow_used_idx;
133 struct vhost_vring_addr ring_addrs;
135 struct batch_copy_elem *batch_copy_elems;
136 uint16_t batch_copy_nb_elems;
137 bool used_wrap_counter;
138 bool avail_wrap_counter;
140 struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
141 uint16_t log_cache_nb_elem;
143 rte_rwlock_t iotlb_lock;
144 rte_rwlock_t iotlb_pending_lock;
145 struct rte_mempool *iotlb_pool;
146 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
148 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
149 } __rte_cache_aligned;
151 /* Old kernels have no such macros defined */
152 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
153 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
156 #ifndef VIRTIO_NET_F_MQ
157 #define VIRTIO_NET_F_MQ 22
160 #define VHOST_MAX_VRING 0x100
161 #define VHOST_MAX_QUEUE_PAIRS 0x80
163 #ifndef VIRTIO_NET_F_MTU
164 #define VIRTIO_NET_F_MTU 3
167 #ifndef VIRTIO_F_ANY_LAYOUT
168 #define VIRTIO_F_ANY_LAYOUT 27
171 /* Declare IOMMU related bits for older kernels */
172 #ifndef VIRTIO_F_IOMMU_PLATFORM
174 #define VIRTIO_F_IOMMU_PLATFORM 33
176 struct vhost_iotlb_msg {
180 #define VHOST_ACCESS_RO 0x1
181 #define VHOST_ACCESS_WO 0x2
182 #define VHOST_ACCESS_RW 0x3
184 #define VHOST_IOTLB_MISS 1
185 #define VHOST_IOTLB_UPDATE 2
186 #define VHOST_IOTLB_INVALIDATE 3
187 #define VHOST_IOTLB_ACCESS_FAIL 4
191 #define VHOST_IOTLB_MSG 0x1
196 struct vhost_iotlb_msg iotlb;
203 * Define virtio 1.0 for older kernels
205 #ifndef VIRTIO_F_VERSION_1
206 #define VIRTIO_F_VERSION_1 32
209 /* Declare packed ring related bits for older kernels */
210 #ifndef VIRTIO_F_RING_PACKED
212 #define VIRTIO_F_RING_PACKED 34
214 #define VRING_DESC_F_NEXT 1
215 #define VRING_DESC_F_WRITE 2
216 #define VRING_DESC_F_INDIRECT 4
218 #define VRING_DESC_F_AVAIL (1ULL << 7)
219 #define VRING_DESC_F_USED (1ULL << 15)
221 struct vring_packed_desc {
230 * Available and used descs are in same order
232 #ifndef VIRTIO_F_IN_ORDER
233 #define VIRTIO_F_IN_ORDER 35
236 /* Features supported by this builtin vhost-user net driver. */
237 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
238 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
239 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
240 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
241 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
242 (1ULL << VIRTIO_NET_F_MQ) | \
243 (1ULL << VIRTIO_F_VERSION_1) | \
244 (1ULL << VHOST_F_LOG_ALL) | \
245 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
246 (1ULL << VIRTIO_NET_F_GSO) | \
247 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
248 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
249 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
250 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
251 (1ULL << VIRTIO_NET_F_CSUM) | \
252 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
253 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
254 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
255 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
256 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
257 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
258 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
259 (1ULL << VIRTIO_NET_F_MTU) | \
260 (1ULL << VIRTIO_F_IN_ORDER) | \
261 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
265 uint64_t guest_phys_addr;
266 uint64_t host_phys_addr;
271 * function prototype for the vhost backend to handler specific vhost user
272 * messages prior to the master message handling
278 * @param require_reply
279 * If the handler requires sending a reply, this varaible shall be written 1,
282 * If the handler requires skipping the master message handling, this variable
283 * shall be written 1, otherwise 0.
285 * 0 on success, -1 on failure
287 typedef int (*vhost_msg_pre_handle)(int vid, void *msg,
288 uint32_t *require_reply, uint32_t *skip_master);
291 * function prototype for the vhost backend to handler specific vhost user
292 * messages after the master message handling is done
298 * @param require_reply
299 * If the handler requires sending a reply, this varaible shall be written 1,
302 * 0 on success, -1 on failure
304 typedef int (*vhost_msg_post_handle)(int vid, void *msg,
305 uint32_t *require_reply);
308 * pre and post vhost user message handlers
310 struct vhost_user_extern_ops {
311 vhost_msg_pre_handle pre_msg_handle;
312 vhost_msg_post_handle post_msg_handle;
316 * Device structure contains all configuration information relating
320 /* Frontend (QEMU) memory and memory region information */
321 struct rte_vhost_memory *mem;
323 uint64_t protocol_features;
327 /* to tell if we need broadcast rarp packet */
328 rte_atomic16_t broadcast_rarp;
330 int dequeue_zero_copy;
331 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
332 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
333 char ifname[IF_NAME_SZ];
337 struct ether_addr mac;
340 struct vhost_device_ops const *notify_ops;
342 uint32_t nr_guest_pages;
343 uint32_t max_guest_pages;
344 struct guest_page *guest_pages;
347 rte_spinlock_t slave_req_lock;
350 * Device id to identify a specific backend device.
351 * It's set to -1 for the default software implementation.
355 /* private data for virtio device */
357 /* pre and post vhost user message handlers for the device */
358 struct vhost_user_extern_ops extern_ops;
359 } __rte_cache_aligned;
361 static __rte_always_inline bool
362 vq_is_packed(struct virtio_net *dev)
364 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
368 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
370 return wrap_counter == !!(desc->flags & VRING_DESC_F_AVAIL) &&
371 wrap_counter != !!(desc->flags & VRING_DESC_F_USED);
374 #define VHOST_LOG_PAGE 4096
377 * Atomically set a bit in memory.
379 static __rte_always_inline void
380 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
382 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
384 * __sync_ built-ins are deprecated, but __atomic_ ones
385 * are sub-optimized in older GCC versions.
387 __sync_fetch_and_or_1(addr, (1U << nr));
389 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
393 static __rte_always_inline void
394 vhost_log_page(uint8_t *log_base, uint64_t page)
396 vhost_set_bit(page % 8, &log_base[page / 8]);
399 static __rte_always_inline void
400 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
404 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
405 !dev->log_base || !len))
408 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
411 /* To make sure guest memory updates are committed before logging */
414 page = addr / VHOST_LOG_PAGE;
415 while (page * VHOST_LOG_PAGE < addr + len) {
416 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
421 static __rte_always_inline void
422 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
424 unsigned long *log_base;
427 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
431 log_base = (unsigned long *)(uintptr_t)dev->log_base;
434 * It is expected a write memory barrier has been issued
435 * before this function is called.
438 for (i = 0; i < vq->log_cache_nb_elem; i++) {
439 struct log_cache_entry *elem = vq->log_cache + i;
441 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
443 * '__sync' builtins are deprecated, but '__atomic' ones
444 * are sub-optimized in older GCC versions.
446 __sync_fetch_and_or(log_base + elem->offset, elem->val);
448 __atomic_fetch_or(log_base + elem->offset, elem->val,
455 vq->log_cache_nb_elem = 0;
458 static __rte_always_inline void
459 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
462 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
463 uint32_t offset = page / (sizeof(unsigned long) << 3);
466 for (i = 0; i < vq->log_cache_nb_elem; i++) {
467 struct log_cache_entry *elem = vq->log_cache + i;
469 if (elem->offset == offset) {
470 elem->val |= (1UL << bit_nr);
475 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
477 * No more room for a new log cache entry,
478 * so write the dirty log map directly.
481 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
486 vq->log_cache[i].offset = offset;
487 vq->log_cache[i].val = (1UL << bit_nr);
488 vq->log_cache_nb_elem++;
491 static __rte_always_inline void
492 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
493 uint64_t addr, uint64_t len)
497 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
498 !dev->log_base || !len))
501 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
504 page = addr / VHOST_LOG_PAGE;
505 while (page * VHOST_LOG_PAGE < addr + len) {
506 vhost_log_cache_page(dev, vq, page);
511 static __rte_always_inline void
512 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
513 uint64_t offset, uint64_t len)
515 vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
518 static __rte_always_inline void
519 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
520 uint64_t offset, uint64_t len)
522 vhost_log_write(dev, vq->log_guest_addr + offset, len);
525 /* Macros for printing using RTE_LOG */
526 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
527 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
529 #ifdef RTE_LIBRTE_VHOST_DEBUG
530 #define VHOST_MAX_PRINT_BUFF 6072
531 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
532 RTE_LOG(DEBUG, log_type, fmt, ##args)
533 #define PRINT_PACKET(device, addr, size, header) do { \
534 char *pkt_addr = (char *)(addr); \
535 unsigned int index; \
536 char packet[VHOST_MAX_PRINT_BUFF]; \
539 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
541 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
542 for (index = 0; index < (size); index++) { \
543 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
544 "%02hhx ", pkt_addr[index]); \
546 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
548 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
551 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
552 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
555 extern uint64_t VHOST_FEATURES;
556 #define MAX_VHOST_DEVICE 1024
557 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
559 /* Convert guest physical address to host physical address */
560 static __rte_always_inline rte_iova_t
561 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
564 struct guest_page *page;
566 for (i = 0; i < dev->nr_guest_pages; i++) {
567 page = &dev->guest_pages[i];
569 if (gpa >= page->guest_phys_addr &&
570 gpa + size < page->guest_phys_addr + page->size) {
571 return gpa - page->guest_phys_addr +
572 page->host_phys_addr;
579 static __rte_always_inline struct virtio_net *
582 struct virtio_net *dev = vhost_devices[vid];
584 if (unlikely(!dev)) {
585 RTE_LOG(ERR, VHOST_CONFIG,
586 "(%d) device not found.\n", vid);
592 int vhost_new_device(void);
593 void cleanup_device(struct virtio_net *dev, int destroy);
594 void reset_device(struct virtio_net *dev);
595 void vhost_destroy_device(int);
596 void vhost_destroy_device_notify(struct virtio_net *dev);
598 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
599 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
601 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
603 void vhost_attach_vdpa_device(int vid, int did);
604 void vhost_detach_vdpa_device(int vid);
606 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
607 void vhost_enable_dequeue_zero_copy(int vid);
608 void vhost_set_builtin_virtio_net(int vid, bool enable);
610 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
613 * Backend-specific cleanup.
615 * TODO: fix it; we have one backend now
617 void vhost_backend_cleanup(struct virtio_net *dev);
619 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
620 uint64_t iova, uint64_t *len, uint8_t perm);
621 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
622 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
624 static __rte_always_inline uint64_t
625 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
626 uint64_t iova, uint64_t *len, uint8_t perm)
628 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
629 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
631 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
634 #define vhost_used_event(vr) \
635 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
638 * The following is used with VIRTIO_RING_F_EVENT_IDX.
639 * Assuming a given event_idx value from the other size, if we have
640 * just incremented index from old to new_idx, should we trigger an
643 static __rte_always_inline int
644 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
646 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
649 static __rte_always_inline void
650 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
652 /* Flush used->idx update before we read avail->flags. */
655 /* Don't kick guest if we don't reach index specified by guest. */
656 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
657 uint16_t old = vq->signalled_used;
658 uint16_t new = vq->last_used_idx;
660 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
662 vhost_used_event(vq),
664 if (vhost_need_event(vhost_used_event(vq), new, old)
665 && (vq->callfd >= 0)) {
666 vq->signalled_used = vq->last_used_idx;
667 eventfd_write(vq->callfd, (eventfd_t) 1);
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);
677 #endif /* _VHOST_NET_CDEV_H_ */