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 {
88 struct vring_desc *desc;
89 struct vring_packed_desc *desc_packed;
91 struct vring_avail *avail;
92 struct vring_used *used;
95 uint16_t last_avail_idx;
96 uint16_t last_used_idx;
97 /* Last used index we notify to front end. */
98 uint16_t signalled_used;
99 #define VIRTIO_INVALID_EVENTFD (-1)
100 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
102 /* Backend value to determine if device should started/stopped */
106 rte_spinlock_t access_lock;
108 /* Used to notify the guest (trigger interrupt) */
110 /* Currently unused as polling mode is enabled */
113 /* Physical address of used ring, for logging */
114 uint64_t log_guest_addr;
118 uint16_t last_zmbuf_idx;
119 struct zcopy_mbuf *zmbufs;
120 struct zcopy_mbuf_list zmbuf_list;
122 struct vring_used_elem *shadow_used_ring;
123 uint16_t shadow_used_idx;
124 struct vhost_vring_addr ring_addrs;
126 struct batch_copy_elem *batch_copy_elems;
127 uint16_t batch_copy_nb_elems;
128 bool used_wrap_counter;
129 bool avail_wrap_counter;
131 struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
132 uint16_t log_cache_nb_elem;
134 rte_rwlock_t iotlb_lock;
135 rte_rwlock_t iotlb_pending_lock;
136 struct rte_mempool *iotlb_pool;
137 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
139 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
140 } __rte_cache_aligned;
142 /* Old kernels have no such macros defined */
143 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
144 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
147 #ifndef VIRTIO_NET_F_MQ
148 #define VIRTIO_NET_F_MQ 22
151 #define VHOST_MAX_VRING 0x100
152 #define VHOST_MAX_QUEUE_PAIRS 0x80
154 #ifndef VIRTIO_NET_F_MTU
155 #define VIRTIO_NET_F_MTU 3
158 #ifndef VIRTIO_F_ANY_LAYOUT
159 #define VIRTIO_F_ANY_LAYOUT 27
162 /* Declare IOMMU related bits for older kernels */
163 #ifndef VIRTIO_F_IOMMU_PLATFORM
165 #define VIRTIO_F_IOMMU_PLATFORM 33
167 struct vhost_iotlb_msg {
171 #define VHOST_ACCESS_RO 0x1
172 #define VHOST_ACCESS_WO 0x2
173 #define VHOST_ACCESS_RW 0x3
175 #define VHOST_IOTLB_MISS 1
176 #define VHOST_IOTLB_UPDATE 2
177 #define VHOST_IOTLB_INVALIDATE 3
178 #define VHOST_IOTLB_ACCESS_FAIL 4
182 #define VHOST_IOTLB_MSG 0x1
187 struct vhost_iotlb_msg iotlb;
194 * Define virtio 1.0 for older kernels
196 #ifndef VIRTIO_F_VERSION_1
197 #define VIRTIO_F_VERSION_1 32
200 /* Declare packed ring related bits for older kernels */
201 #ifndef VIRTIO_F_RING_PACKED
203 #define VIRTIO_F_RING_PACKED 34
205 #define VRING_DESC_F_NEXT 1
206 #define VRING_DESC_F_WRITE 2
207 #define VRING_DESC_F_INDIRECT 4
209 #define VRING_DESC_F_AVAIL (1ULL << 7)
210 #define VRING_DESC_F_USED (1ULL << 15)
212 struct vring_packed_desc {
221 * Available and used descs are in same order
223 #ifndef VIRTIO_F_IN_ORDER
224 #define VIRTIO_F_IN_ORDER 35
227 /* Features supported by this builtin vhost-user net driver. */
228 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
229 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
230 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
231 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
232 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
233 (1ULL << VIRTIO_NET_F_MQ) | \
234 (1ULL << VIRTIO_F_VERSION_1) | \
235 (1ULL << VHOST_F_LOG_ALL) | \
236 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
237 (1ULL << VIRTIO_NET_F_GSO) | \
238 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
239 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
240 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
241 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
242 (1ULL << VIRTIO_NET_F_CSUM) | \
243 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
244 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
245 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
246 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
247 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
248 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
249 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
250 (1ULL << VIRTIO_NET_F_MTU) | \
251 (1ULL << VIRTIO_F_IN_ORDER) | \
252 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
256 uint64_t guest_phys_addr;
257 uint64_t host_phys_addr;
262 * function prototype for the vhost backend to handler specific vhost user
263 * messages prior to the master message handling
269 * @param require_reply
270 * If the handler requires sending a reply, this varaible shall be written 1,
273 * If the handler requires skipping the master message handling, this variable
274 * shall be written 1, otherwise 0.
276 * 0 on success, -1 on failure
278 typedef int (*vhost_msg_pre_handle)(int vid, void *msg,
279 uint32_t *require_reply, uint32_t *skip_master);
282 * function prototype for the vhost backend to handler specific vhost user
283 * messages after the master message handling is done
289 * @param require_reply
290 * If the handler requires sending a reply, this varaible shall be written 1,
293 * 0 on success, -1 on failure
295 typedef int (*vhost_msg_post_handle)(int vid, void *msg,
296 uint32_t *require_reply);
299 * pre and post vhost user message handlers
301 struct vhost_user_extern_ops {
302 vhost_msg_pre_handle pre_msg_handle;
303 vhost_msg_post_handle post_msg_handle;
307 * Device structure contains all configuration information relating
311 /* Frontend (QEMU) memory and memory region information */
312 struct rte_vhost_memory *mem;
314 uint64_t protocol_features;
318 /* to tell if we need broadcast rarp packet */
319 rte_atomic16_t broadcast_rarp;
321 int dequeue_zero_copy;
322 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
323 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
324 char ifname[IF_NAME_SZ];
328 struct 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 * Device id to identify a specific backend device.
342 * It's set to -1 for the default software implementation.
346 /* private data for virtio device */
348 /* pre and post vhost user message handlers for the device */
349 struct vhost_user_extern_ops extern_ops;
350 } __rte_cache_aligned;
352 static __rte_always_inline bool
353 vq_is_packed(struct virtio_net *dev)
355 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
359 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
361 return wrap_counter == !!(desc->flags & VRING_DESC_F_AVAIL) &&
362 wrap_counter != !!(desc->flags & VRING_DESC_F_USED);
365 #define VHOST_LOG_PAGE 4096
368 * Atomically set a bit in memory.
370 static __rte_always_inline void
371 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
373 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
375 * __sync_ built-ins are deprecated, but __atomic_ ones
376 * are sub-optimized in older GCC versions.
378 __sync_fetch_and_or_1(addr, (1U << nr));
380 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
384 static __rte_always_inline void
385 vhost_log_page(uint8_t *log_base, uint64_t page)
387 vhost_set_bit(page % 8, &log_base[page / 8]);
390 static __rte_always_inline void
391 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
395 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
396 !dev->log_base || !len))
399 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
402 /* To make sure guest memory updates are committed before logging */
405 page = addr / VHOST_LOG_PAGE;
406 while (page * VHOST_LOG_PAGE < addr + len) {
407 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
412 static __rte_always_inline void
413 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
415 unsigned long *log_base;
418 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
422 log_base = (unsigned long *)(uintptr_t)dev->log_base;
425 * It is expected a write memory barrier has been issued
426 * before this function is called.
429 for (i = 0; i < vq->log_cache_nb_elem; i++) {
430 struct log_cache_entry *elem = vq->log_cache + i;
432 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
434 * '__sync' builtins are deprecated, but '__atomic' ones
435 * are sub-optimized in older GCC versions.
437 __sync_fetch_and_or(log_base + elem->offset, elem->val);
439 __atomic_fetch_or(log_base + elem->offset, elem->val,
446 vq->log_cache_nb_elem = 0;
449 static __rte_always_inline void
450 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
453 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
454 uint32_t offset = page / (sizeof(unsigned long) << 3);
457 for (i = 0; i < vq->log_cache_nb_elem; i++) {
458 struct log_cache_entry *elem = vq->log_cache + i;
460 if (elem->offset == offset) {
461 elem->val |= (1UL << bit_nr);
466 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
468 * No more room for a new log cache entry,
469 * so write the dirty log map directly.
472 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
477 vq->log_cache[i].offset = offset;
478 vq->log_cache[i].val = (1UL << bit_nr);
479 vq->log_cache_nb_elem++;
482 static __rte_always_inline void
483 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
484 uint64_t addr, uint64_t len)
488 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
489 !dev->log_base || !len))
492 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
495 page = addr / VHOST_LOG_PAGE;
496 while (page * VHOST_LOG_PAGE < addr + len) {
497 vhost_log_cache_page(dev, vq, page);
502 static __rte_always_inline void
503 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
504 uint64_t offset, uint64_t len)
506 vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
509 static __rte_always_inline void
510 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
511 uint64_t offset, uint64_t len)
513 vhost_log_write(dev, vq->log_guest_addr + offset, len);
516 /* Macros for printing using RTE_LOG */
517 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
518 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
520 #ifdef RTE_LIBRTE_VHOST_DEBUG
521 #define VHOST_MAX_PRINT_BUFF 6072
522 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
523 RTE_LOG(DEBUG, log_type, fmt, ##args)
524 #define PRINT_PACKET(device, addr, size, header) do { \
525 char *pkt_addr = (char *)(addr); \
526 unsigned int index; \
527 char packet[VHOST_MAX_PRINT_BUFF]; \
530 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
532 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
533 for (index = 0; index < (size); index++) { \
534 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
535 "%02hhx ", pkt_addr[index]); \
537 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
539 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
542 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
543 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
546 extern uint64_t VHOST_FEATURES;
547 #define MAX_VHOST_DEVICE 1024
548 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
550 /* Convert guest physical address to host physical address */
551 static __rte_always_inline rte_iova_t
552 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
555 struct guest_page *page;
557 for (i = 0; i < dev->nr_guest_pages; i++) {
558 page = &dev->guest_pages[i];
560 if (gpa >= page->guest_phys_addr &&
561 gpa + size < page->guest_phys_addr + page->size) {
562 return gpa - page->guest_phys_addr +
563 page->host_phys_addr;
570 static __rte_always_inline struct virtio_net *
573 struct virtio_net *dev = vhost_devices[vid];
575 if (unlikely(!dev)) {
576 RTE_LOG(ERR, VHOST_CONFIG,
577 "(%d) device not found.\n", vid);
583 int vhost_new_device(void);
584 void cleanup_device(struct virtio_net *dev, int destroy);
585 void reset_device(struct virtio_net *dev);
586 void vhost_destroy_device(int);
587 void vhost_destroy_device_notify(struct virtio_net *dev);
589 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
590 void free_vq(struct vhost_virtqueue *vq);
592 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
594 void vhost_attach_vdpa_device(int vid, int did);
595 void vhost_detach_vdpa_device(int vid);
597 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
598 void vhost_enable_dequeue_zero_copy(int vid);
599 void vhost_set_builtin_virtio_net(int vid, bool enable);
601 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
604 * Backend-specific cleanup.
606 * TODO: fix it; we have one backend now
608 void vhost_backend_cleanup(struct virtio_net *dev);
610 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
611 uint64_t iova, uint64_t *len, uint8_t perm);
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_used_event(vr) \
626 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
629 * The following is used with VIRTIO_RING_F_EVENT_IDX.
630 * Assuming a given event_idx value from the other size, if we have
631 * just incremented index from old to new_idx, should we trigger an
634 static __rte_always_inline int
635 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
637 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
640 static __rte_always_inline void
641 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
643 /* Flush used->idx update before we read avail->flags. */
646 /* Don't kick guest if we don't reach index specified by guest. */
647 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
648 uint16_t old = vq->signalled_used;
649 uint16_t new = vq->last_used_idx;
651 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
653 vhost_used_event(vq),
655 if (vhost_need_event(vhost_used_event(vq), new, old)
656 && (vq->callfd >= 0)) {
657 vq->signalled_used = vq->last_used_idx;
658 eventfd_write(vq->callfd, (eventfd_t) 1);
661 /* Kick the guest if necessary. */
662 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
663 && (vq->callfd >= 0))
664 eventfd_write(vq->callfd, (eventfd_t)1);
668 #endif /* _VHOST_NET_CDEV_H_ */