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;
62 TAILQ_ENTRY(zcopy_mbuf) next;
64 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
67 * Structure contains the info for each batched memory copy.
69 struct batch_copy_elem {
77 * Structure that contains the info for batched dirty logging.
79 struct log_cache_entry {
84 struct vring_used_elem_packed {
91 * Structure contains variables relevant to RX/TX virtqueues.
93 struct vhost_virtqueue {
95 struct vring_desc *desc;
96 struct vring_packed_desc *desc_packed;
98 struct vring_avail *avail;
99 struct vring_used *used;
102 uint16_t last_avail_idx;
103 uint16_t last_used_idx;
104 /* Last used index we notify to front end. */
105 uint16_t signalled_used;
106 #define VIRTIO_INVALID_EVENTFD (-1)
107 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
109 /* Backend value to determine if device should started/stopped */
113 rte_spinlock_t access_lock;
115 /* Used to notify the guest (trigger interrupt) */
117 /* Currently unused as polling mode is enabled */
120 /* Physical address of used ring, for logging */
121 uint64_t log_guest_addr;
125 uint16_t last_zmbuf_idx;
126 struct zcopy_mbuf *zmbufs;
127 struct zcopy_mbuf_list zmbuf_list;
130 struct vring_used_elem *shadow_used_split;
131 struct vring_used_elem_packed *shadow_used_packed;
133 uint16_t shadow_used_idx;
134 struct vhost_vring_addr ring_addrs;
136 struct batch_copy_elem *batch_copy_elems;
137 uint16_t batch_copy_nb_elems;
138 bool used_wrap_counter;
139 bool avail_wrap_counter;
141 struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
142 uint16_t log_cache_nb_elem;
144 rte_rwlock_t iotlb_lock;
145 rte_rwlock_t iotlb_pending_lock;
146 struct rte_mempool *iotlb_pool;
147 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
149 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
150 } __rte_cache_aligned;
152 /* Old kernels have no such macros defined */
153 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
154 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
157 #ifndef VIRTIO_NET_F_MQ
158 #define VIRTIO_NET_F_MQ 22
161 #define VHOST_MAX_VRING 0x100
162 #define VHOST_MAX_QUEUE_PAIRS 0x80
164 #ifndef VIRTIO_NET_F_MTU
165 #define VIRTIO_NET_F_MTU 3
168 #ifndef VIRTIO_F_ANY_LAYOUT
169 #define VIRTIO_F_ANY_LAYOUT 27
172 /* Declare IOMMU related bits for older kernels */
173 #ifndef VIRTIO_F_IOMMU_PLATFORM
175 #define VIRTIO_F_IOMMU_PLATFORM 33
177 struct vhost_iotlb_msg {
181 #define VHOST_ACCESS_RO 0x1
182 #define VHOST_ACCESS_WO 0x2
183 #define VHOST_ACCESS_RW 0x3
185 #define VHOST_IOTLB_MISS 1
186 #define VHOST_IOTLB_UPDATE 2
187 #define VHOST_IOTLB_INVALIDATE 3
188 #define VHOST_IOTLB_ACCESS_FAIL 4
192 #define VHOST_IOTLB_MSG 0x1
197 struct vhost_iotlb_msg iotlb;
204 * Define virtio 1.0 for older kernels
206 #ifndef VIRTIO_F_VERSION_1
207 #define VIRTIO_F_VERSION_1 32
210 /* Declare packed ring related bits for older kernels */
211 #ifndef VIRTIO_F_RING_PACKED
213 #define VIRTIO_F_RING_PACKED 34
215 #define VRING_DESC_F_NEXT 1
216 #define VRING_DESC_F_WRITE 2
217 #define VRING_DESC_F_INDIRECT 4
219 #define VRING_DESC_F_AVAIL (1ULL << 7)
220 #define VRING_DESC_F_USED (1ULL << 15)
222 struct vring_packed_desc {
231 * Available and used descs are in same order
233 #ifndef VIRTIO_F_IN_ORDER
234 #define VIRTIO_F_IN_ORDER 35
237 /* Features supported by this builtin vhost-user net driver. */
238 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
239 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
240 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
241 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
242 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
243 (1ULL << VIRTIO_NET_F_MQ) | \
244 (1ULL << VIRTIO_F_VERSION_1) | \
245 (1ULL << VHOST_F_LOG_ALL) | \
246 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
247 (1ULL << VIRTIO_NET_F_GSO) | \
248 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
249 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
250 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
251 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
252 (1ULL << VIRTIO_NET_F_CSUM) | \
253 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
254 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
255 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
256 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
257 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
258 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
259 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
260 (1ULL << VIRTIO_NET_F_MTU) | \
261 (1ULL << VIRTIO_F_IN_ORDER) | \
262 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
266 uint64_t guest_phys_addr;
267 uint64_t host_phys_addr;
272 * function prototype for the vhost backend to handler specific vhost user
273 * messages prior to the master message handling
279 * @param require_reply
280 * If the handler requires sending a reply, this varaible shall be written 1,
283 * If the handler requires skipping the master message handling, this variable
284 * shall be written 1, otherwise 0.
286 * 0 on success, -1 on failure
288 typedef int (*vhost_msg_pre_handle)(int vid, void *msg,
289 uint32_t *require_reply, uint32_t *skip_master);
292 * function prototype for the vhost backend to handler specific vhost user
293 * messages after the master message handling is done
299 * @param require_reply
300 * If the handler requires sending a reply, this varaible shall be written 1,
303 * 0 on success, -1 on failure
305 typedef int (*vhost_msg_post_handle)(int vid, void *msg,
306 uint32_t *require_reply);
309 * pre and post vhost user message handlers
311 struct vhost_user_extern_ops {
312 vhost_msg_pre_handle pre_msg_handle;
313 vhost_msg_post_handle post_msg_handle;
317 * Device structure contains all configuration information relating
321 /* Frontend (QEMU) memory and memory region information */
322 struct rte_vhost_memory *mem;
324 uint64_t protocol_features;
328 /* to tell if we need broadcast rarp packet */
329 rte_atomic16_t broadcast_rarp;
331 int dequeue_zero_copy;
332 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
333 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
334 char ifname[IF_NAME_SZ];
338 struct ether_addr mac;
341 struct vhost_device_ops const *notify_ops;
343 uint32_t nr_guest_pages;
344 uint32_t max_guest_pages;
345 struct guest_page *guest_pages;
348 rte_spinlock_t slave_req_lock;
351 * Device id to identify a specific backend device.
352 * It's set to -1 for the default software implementation.
356 /* private data for virtio device */
358 /* pre and post vhost user message handlers for the device */
359 struct vhost_user_extern_ops extern_ops;
360 } __rte_cache_aligned;
362 static __rte_always_inline bool
363 vq_is_packed(struct virtio_net *dev)
365 return dev->features & (1ull << VIRTIO_F_RING_PACKED);
369 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
371 return wrap_counter == !!(desc->flags & VRING_DESC_F_AVAIL) &&
372 wrap_counter != !!(desc->flags & VRING_DESC_F_USED);
375 #define VHOST_LOG_PAGE 4096
378 * Atomically set a bit in memory.
380 static __rte_always_inline void
381 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
383 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
385 * __sync_ built-ins are deprecated, but __atomic_ ones
386 * are sub-optimized in older GCC versions.
388 __sync_fetch_and_or_1(addr, (1U << nr));
390 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
394 static __rte_always_inline void
395 vhost_log_page(uint8_t *log_base, uint64_t page)
397 vhost_set_bit(page % 8, &log_base[page / 8]);
400 static __rte_always_inline void
401 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
405 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
406 !dev->log_base || !len))
409 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
412 /* To make sure guest memory updates are committed before logging */
415 page = addr / VHOST_LOG_PAGE;
416 while (page * VHOST_LOG_PAGE < addr + len) {
417 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
422 static __rte_always_inline void
423 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
425 unsigned long *log_base;
428 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
432 log_base = (unsigned long *)(uintptr_t)dev->log_base;
435 * It is expected a write memory barrier has been issued
436 * before this function is called.
439 for (i = 0; i < vq->log_cache_nb_elem; i++) {
440 struct log_cache_entry *elem = vq->log_cache + i;
442 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
444 * '__sync' builtins are deprecated, but '__atomic' ones
445 * are sub-optimized in older GCC versions.
447 __sync_fetch_and_or(log_base + elem->offset, elem->val);
449 __atomic_fetch_or(log_base + elem->offset, elem->val,
456 vq->log_cache_nb_elem = 0;
459 static __rte_always_inline void
460 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
463 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
464 uint32_t offset = page / (sizeof(unsigned long) << 3);
467 for (i = 0; i < vq->log_cache_nb_elem; i++) {
468 struct log_cache_entry *elem = vq->log_cache + i;
470 if (elem->offset == offset) {
471 elem->val |= (1UL << bit_nr);
476 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
478 * No more room for a new log cache entry,
479 * so write the dirty log map directly.
482 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
487 vq->log_cache[i].offset = offset;
488 vq->log_cache[i].val = (1UL << bit_nr);
489 vq->log_cache_nb_elem++;
492 static __rte_always_inline void
493 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
494 uint64_t addr, uint64_t len)
498 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
499 !dev->log_base || !len))
502 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
505 page = addr / VHOST_LOG_PAGE;
506 while (page * VHOST_LOG_PAGE < addr + len) {
507 vhost_log_cache_page(dev, vq, page);
512 static __rte_always_inline void
513 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
514 uint64_t offset, uint64_t len)
516 vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
519 static __rte_always_inline void
520 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
521 uint64_t offset, uint64_t len)
523 vhost_log_write(dev, vq->log_guest_addr + offset, len);
526 /* Macros for printing using RTE_LOG */
527 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
528 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
530 #ifdef RTE_LIBRTE_VHOST_DEBUG
531 #define VHOST_MAX_PRINT_BUFF 6072
532 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
533 RTE_LOG(DEBUG, log_type, fmt, ##args)
534 #define PRINT_PACKET(device, addr, size, header) do { \
535 char *pkt_addr = (char *)(addr); \
536 unsigned int index; \
537 char packet[VHOST_MAX_PRINT_BUFF]; \
540 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
542 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
543 for (index = 0; index < (size); index++) { \
544 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
545 "%02hhx ", pkt_addr[index]); \
547 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
549 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
552 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
553 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
556 extern uint64_t VHOST_FEATURES;
557 #define MAX_VHOST_DEVICE 1024
558 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
560 /* Convert guest physical address to host physical address */
561 static __rte_always_inline rte_iova_t
562 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
565 struct guest_page *page;
567 for (i = 0; i < dev->nr_guest_pages; i++) {
568 page = &dev->guest_pages[i];
570 if (gpa >= page->guest_phys_addr &&
571 gpa + size < page->guest_phys_addr + page->size) {
572 return gpa - page->guest_phys_addr +
573 page->host_phys_addr;
580 static __rte_always_inline struct virtio_net *
583 struct virtio_net *dev = vhost_devices[vid];
585 if (unlikely(!dev)) {
586 RTE_LOG(ERR, VHOST_CONFIG,
587 "(%d) device not found.\n", vid);
593 int vhost_new_device(void);
594 void cleanup_device(struct virtio_net *dev, int destroy);
595 void reset_device(struct virtio_net *dev);
596 void vhost_destroy_device(int);
597 void vhost_destroy_device_notify(struct virtio_net *dev);
599 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
600 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
602 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
604 void vhost_attach_vdpa_device(int vid, int did);
605 void vhost_detach_vdpa_device(int vid);
607 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
608 void vhost_enable_dequeue_zero_copy(int vid);
609 void vhost_set_builtin_virtio_net(int vid, bool enable);
611 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
614 * Backend-specific cleanup.
616 * TODO: fix it; we have one backend now
618 void vhost_backend_cleanup(struct virtio_net *dev);
620 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
621 uint64_t iova, uint64_t *len, uint8_t perm);
622 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
623 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
625 static __rte_always_inline uint64_t
626 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
627 uint64_t iova, uint64_t *len, uint8_t perm)
629 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
630 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
632 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
635 #define vhost_used_event(vr) \
636 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
639 * The following is used with VIRTIO_RING_F_EVENT_IDX.
640 * Assuming a given event_idx value from the other size, if we have
641 * just incremented index from old to new_idx, should we trigger an
644 static __rte_always_inline int
645 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
647 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
650 static __rte_always_inline void
651 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
653 /* Flush used->idx update before we read avail->flags. */
656 /* Don't kick guest if we don't reach index specified by guest. */
657 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
658 uint16_t old = vq->signalled_used;
659 uint16_t new = vq->last_used_idx;
661 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
663 vhost_used_event(vq),
665 if (vhost_need_event(vhost_used_event(vq), new, old)
666 && (vq->callfd >= 0)) {
667 vq->signalled_used = vq->last_used_idx;
668 eventfd_write(vq->callfd, (eventfd_t) 1);
671 /* Kick the guest if necessary. */
672 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
673 && (vq->callfd >= 0))
674 eventfd_write(vq->callfd, (eventfd_t)1);
678 #endif /* _VHOST_NET_CDEV_H_ */