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 {
84 * Structure contains variables relevant to RX/TX virtqueues.
86 struct vhost_virtqueue {
87 struct vring_desc *desc;
88 struct vring_avail *avail;
89 struct vring_used *used;
92 uint16_t last_avail_idx;
93 uint16_t last_used_idx;
94 /* Last used index we notify to front end. */
95 uint16_t signalled_used;
96 #define VIRTIO_INVALID_EVENTFD (-1)
97 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
99 /* Backend value to determine if device should started/stopped */
103 rte_spinlock_t access_lock;
105 /* Used to notify the guest (trigger interrupt) */
107 /* Currently unused as polling mode is enabled */
110 /* Physical address of used ring, for logging */
111 uint64_t log_guest_addr;
115 uint16_t last_zmbuf_idx;
116 struct zcopy_mbuf *zmbufs;
117 struct zcopy_mbuf_list zmbuf_list;
119 struct vring_used_elem *shadow_used_ring;
120 uint16_t shadow_used_idx;
121 struct vhost_vring_addr ring_addrs;
123 struct batch_copy_elem *batch_copy_elems;
124 uint16_t batch_copy_nb_elems;
126 struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
127 uint16_t log_cache_nb_elem;
129 rte_rwlock_t iotlb_lock;
130 rte_rwlock_t iotlb_pending_lock;
131 struct rte_mempool *iotlb_pool;
132 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
134 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
135 } __rte_cache_aligned;
137 /* Old kernels have no such macros defined */
138 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
139 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
142 #ifndef VIRTIO_NET_F_MQ
143 #define VIRTIO_NET_F_MQ 22
146 #define VHOST_MAX_VRING 0x100
147 #define VHOST_MAX_QUEUE_PAIRS 0x80
149 #ifndef VIRTIO_NET_F_MTU
150 #define VIRTIO_NET_F_MTU 3
153 #ifndef VIRTIO_F_ANY_LAYOUT
154 #define VIRTIO_F_ANY_LAYOUT 27
157 /* Declare IOMMU related bits for older kernels */
158 #ifndef VIRTIO_F_IOMMU_PLATFORM
160 #define VIRTIO_F_IOMMU_PLATFORM 33
162 struct vhost_iotlb_msg {
166 #define VHOST_ACCESS_RO 0x1
167 #define VHOST_ACCESS_WO 0x2
168 #define VHOST_ACCESS_RW 0x3
170 #define VHOST_IOTLB_MISS 1
171 #define VHOST_IOTLB_UPDATE 2
172 #define VHOST_IOTLB_INVALIDATE 3
173 #define VHOST_IOTLB_ACCESS_FAIL 4
177 #define VHOST_IOTLB_MSG 0x1
182 struct vhost_iotlb_msg iotlb;
189 * Define virtio 1.0 for older kernels
191 #ifndef VIRTIO_F_VERSION_1
192 #define VIRTIO_F_VERSION_1 32
195 /* Declare packed ring related bits for older kernels */
196 #ifndef VIRTIO_F_RING_PACKED
198 #define VIRTIO_F_RING_PACKED 34
200 #define VRING_DESC_F_NEXT 1
201 #define VRING_DESC_F_WRITE 2
202 #define VRING_DESC_F_INDIRECT 4
204 #define VRING_DESC_F_AVAIL (1ULL << 7)
205 #define VRING_DESC_F_USED (1ULL << 15)
207 struct vring_packed_desc {
216 * Available and used descs are in same order
218 #ifndef VIRTIO_F_IN_ORDER
219 #define VIRTIO_F_IN_ORDER 35
222 /* Features supported by this builtin vhost-user net driver. */
223 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
224 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
225 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
226 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
227 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
228 (1ULL << VIRTIO_NET_F_MQ) | \
229 (1ULL << VIRTIO_F_VERSION_1) | \
230 (1ULL << VHOST_F_LOG_ALL) | \
231 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
232 (1ULL << VIRTIO_NET_F_GSO) | \
233 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
234 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
235 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
236 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
237 (1ULL << VIRTIO_NET_F_CSUM) | \
238 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
239 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
240 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
241 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
242 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
243 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
244 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
245 (1ULL << VIRTIO_NET_F_MTU) | \
246 (1ULL << VIRTIO_F_IN_ORDER) | \
247 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
251 uint64_t guest_phys_addr;
252 uint64_t host_phys_addr;
257 * function prototype for the vhost backend to handler specific vhost user
258 * messages prior to the master message handling
264 * @param require_reply
265 * If the handler requires sending a reply, this varaible shall be written 1,
268 * If the handler requires skipping the master message handling, this variable
269 * shall be written 1, otherwise 0.
271 * 0 on success, -1 on failure
273 typedef int (*vhost_msg_pre_handle)(int vid, void *msg,
274 uint32_t *require_reply, uint32_t *skip_master);
277 * function prototype for the vhost backend to handler specific vhost user
278 * messages after the master message handling is done
284 * @param require_reply
285 * If the handler requires sending a reply, this varaible shall be written 1,
288 * 0 on success, -1 on failure
290 typedef int (*vhost_msg_post_handle)(int vid, void *msg,
291 uint32_t *require_reply);
294 * pre and post vhost user message handlers
296 struct vhost_user_extern_ops {
297 vhost_msg_pre_handle pre_msg_handle;
298 vhost_msg_post_handle post_msg_handle;
302 * Device structure contains all configuration information relating
306 /* Frontend (QEMU) memory and memory region information */
307 struct rte_vhost_memory *mem;
309 uint64_t protocol_features;
313 /* to tell if we need broadcast rarp packet */
314 rte_atomic16_t broadcast_rarp;
316 int dequeue_zero_copy;
317 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
318 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
319 char ifname[IF_NAME_SZ];
323 struct ether_addr mac;
326 struct vhost_device_ops const *notify_ops;
328 uint32_t nr_guest_pages;
329 uint32_t max_guest_pages;
330 struct guest_page *guest_pages;
333 rte_spinlock_t slave_req_lock;
336 * Device id to identify a specific backend device.
337 * It's set to -1 for the default software implementation.
341 /* private data for virtio device */
343 /* pre and post vhost user message handlers for the device */
344 struct vhost_user_extern_ops extern_ops;
345 } __rte_cache_aligned;
347 static __rte_always_inline bool
348 vq_is_packed(struct virtio_net *dev)
350 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
354 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
356 return wrap_counter == !!(desc->flags & VRING_DESC_F_AVAIL) &&
357 wrap_counter != !!(desc->flags & VRING_DESC_F_USED);
360 #define VHOST_LOG_PAGE 4096
363 * Atomically set a bit in memory.
365 static __rte_always_inline void
366 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
368 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
370 * __sync_ built-ins are deprecated, but __atomic_ ones
371 * are sub-optimized in older GCC versions.
373 __sync_fetch_and_or_1(addr, (1U << nr));
375 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
379 static __rte_always_inline void
380 vhost_log_page(uint8_t *log_base, uint64_t page)
382 vhost_set_bit(page % 8, &log_base[page / 8]);
385 static __rte_always_inline void
386 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
390 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
391 !dev->log_base || !len))
394 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
397 /* To make sure guest memory updates are committed before logging */
400 page = addr / VHOST_LOG_PAGE;
401 while (page * VHOST_LOG_PAGE < addr + len) {
402 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
407 static __rte_always_inline void
408 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
410 unsigned long *log_base;
413 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
417 log_base = (unsigned long *)(uintptr_t)dev->log_base;
420 * It is expected a write memory barrier has been issued
421 * before this function is called.
424 for (i = 0; i < vq->log_cache_nb_elem; i++) {
425 struct log_cache_entry *elem = vq->log_cache + i;
427 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
429 * '__sync' builtins are deprecated, but '__atomic' ones
430 * are sub-optimized in older GCC versions.
432 __sync_fetch_and_or(log_base + elem->offset, elem->val);
434 __atomic_fetch_or(log_base + elem->offset, elem->val,
441 vq->log_cache_nb_elem = 0;
444 static __rte_always_inline void
445 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
448 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
449 uint32_t offset = page / (sizeof(unsigned long) << 3);
452 for (i = 0; i < vq->log_cache_nb_elem; i++) {
453 struct log_cache_entry *elem = vq->log_cache + i;
455 if (elem->offset == offset) {
456 elem->val |= (1UL << bit_nr);
461 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
463 * No more room for a new log cache entry,
464 * so write the dirty log map directly.
467 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
472 vq->log_cache[i].offset = offset;
473 vq->log_cache[i].val = (1UL << bit_nr);
474 vq->log_cache_nb_elem++;
477 static __rte_always_inline void
478 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
479 uint64_t addr, uint64_t len)
483 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
484 !dev->log_base || !len))
487 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
490 page = addr / VHOST_LOG_PAGE;
491 while (page * VHOST_LOG_PAGE < addr + len) {
492 vhost_log_cache_page(dev, vq, page);
497 static __rte_always_inline void
498 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
499 uint64_t offset, uint64_t len)
501 vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
504 static __rte_always_inline void
505 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
506 uint64_t offset, uint64_t len)
508 vhost_log_write(dev, vq->log_guest_addr + offset, len);
511 /* Macros for printing using RTE_LOG */
512 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
513 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
515 #ifdef RTE_LIBRTE_VHOST_DEBUG
516 #define VHOST_MAX_PRINT_BUFF 6072
517 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
518 RTE_LOG(DEBUG, log_type, fmt, ##args)
519 #define PRINT_PACKET(device, addr, size, header) do { \
520 char *pkt_addr = (char *)(addr); \
521 unsigned int index; \
522 char packet[VHOST_MAX_PRINT_BUFF]; \
525 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
527 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
528 for (index = 0; index < (size); index++) { \
529 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
530 "%02hhx ", pkt_addr[index]); \
532 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
534 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
537 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
538 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
541 extern uint64_t VHOST_FEATURES;
542 #define MAX_VHOST_DEVICE 1024
543 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
545 /* Convert guest physical address to host physical address */
546 static __rte_always_inline rte_iova_t
547 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
550 struct guest_page *page;
552 for (i = 0; i < dev->nr_guest_pages; i++) {
553 page = &dev->guest_pages[i];
555 if (gpa >= page->guest_phys_addr &&
556 gpa + size < page->guest_phys_addr + page->size) {
557 return gpa - page->guest_phys_addr +
558 page->host_phys_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 free_vq(struct vhost_virtqueue *vq);
587 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
589 void vhost_attach_vdpa_device(int vid, int did);
590 void vhost_detach_vdpa_device(int vid);
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);
596 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
599 * Backend-specific cleanup.
601 * TODO: fix it; we have one backend now
603 void vhost_backend_cleanup(struct virtio_net *dev);
605 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
606 uint64_t iova, uint64_t *len, uint8_t perm);
607 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
608 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
610 static __rte_always_inline uint64_t
611 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
612 uint64_t iova, uint64_t *len, uint8_t perm)
614 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
615 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
617 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
620 #define vhost_used_event(vr) \
621 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
624 * The following is used with VIRTIO_RING_F_EVENT_IDX.
625 * Assuming a given event_idx value from the other size, if we have
626 * just incremented index from old to new_idx, should we trigger an
629 static __rte_always_inline int
630 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
632 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
635 static __rte_always_inline void
636 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
638 /* Flush used->idx update before we read avail->flags. */
641 /* Don't kick guest if we don't reach index specified by guest. */
642 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
643 uint16_t old = vq->signalled_used;
644 uint16_t new = vq->last_used_idx;
646 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
648 vhost_used_event(vq),
650 if (vhost_need_event(vhost_used_event(vq), new, old)
651 && (vq->callfd >= 0)) {
652 vq->signalled_used = vq->last_used_idx;
653 eventfd_write(vq->callfd, (eventfd_t) 1);
656 /* Kick the guest if necessary. */
657 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
658 && (vq->callfd >= 0))
659 eventfd_write(vq->callfd, (eventfd_t)1);
663 #endif /* _VHOST_NET_CDEV_H_ */