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.
52 * A structure to hold some fields needed in zero copy code path,
53 * mainly for associating an mbuf with the right desc_idx.
56 struct rte_mbuf *mbuf;
60 TAILQ_ENTRY(zcopy_mbuf) next;
62 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
65 * Structure contains the info for each batched memory copy.
67 struct batch_copy_elem {
75 * Structure that contains the info for batched dirty logging.
77 struct log_cache_entry {
83 * Structure contains variables relevant to RX/TX virtqueues.
85 struct vhost_virtqueue {
86 struct vring_desc *desc;
87 struct vring_avail *avail;
88 struct vring_used *used;
91 uint16_t last_avail_idx;
92 uint16_t last_used_idx;
93 /* Last used index we notify to front end. */
94 uint16_t signalled_used;
95 #define VIRTIO_INVALID_EVENTFD (-1)
96 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
98 /* Backend value to determine if device should started/stopped */
102 rte_spinlock_t access_lock;
104 /* Used to notify the guest (trigger interrupt) */
106 /* Currently unused as polling mode is enabled */
109 /* Physical address of used ring, for logging */
110 uint64_t log_guest_addr;
114 uint16_t last_zmbuf_idx;
115 struct zcopy_mbuf *zmbufs;
116 struct zcopy_mbuf_list zmbuf_list;
118 struct vring_used_elem *shadow_used_ring;
119 uint16_t shadow_used_idx;
120 struct vhost_vring_addr ring_addrs;
122 struct batch_copy_elem *batch_copy_elems;
123 uint16_t batch_copy_nb_elems;
125 struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
126 uint16_t log_cache_nb_elem;
128 rte_rwlock_t iotlb_lock;
129 rte_rwlock_t iotlb_pending_lock;
130 struct rte_mempool *iotlb_pool;
131 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
133 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
134 } __rte_cache_aligned;
136 /* Old kernels have no such macros defined */
137 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
138 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
141 #ifndef VIRTIO_NET_F_MQ
142 #define VIRTIO_NET_F_MQ 22
145 #define VHOST_MAX_VRING 0x100
146 #define VHOST_MAX_QUEUE_PAIRS 0x80
148 #ifndef VIRTIO_NET_F_MTU
149 #define VIRTIO_NET_F_MTU 3
152 #ifndef VIRTIO_F_ANY_LAYOUT
153 #define VIRTIO_F_ANY_LAYOUT 27
156 /* Declare IOMMU related bits for older kernels */
157 #ifndef VIRTIO_F_IOMMU_PLATFORM
159 #define VIRTIO_F_IOMMU_PLATFORM 33
161 struct vhost_iotlb_msg {
165 #define VHOST_ACCESS_RO 0x1
166 #define VHOST_ACCESS_WO 0x2
167 #define VHOST_ACCESS_RW 0x3
169 #define VHOST_IOTLB_MISS 1
170 #define VHOST_IOTLB_UPDATE 2
171 #define VHOST_IOTLB_INVALIDATE 3
172 #define VHOST_IOTLB_ACCESS_FAIL 4
176 #define VHOST_IOTLB_MSG 0x1
181 struct vhost_iotlb_msg iotlb;
188 * Define virtio 1.0 for older kernels
190 #ifndef VIRTIO_F_VERSION_1
191 #define VIRTIO_F_VERSION_1 32
194 /* Features supported by this builtin vhost-user net driver. */
195 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
196 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
197 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
198 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
199 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
200 (1ULL << VIRTIO_NET_F_MQ) | \
201 (1ULL << VIRTIO_F_VERSION_1) | \
202 (1ULL << VHOST_F_LOG_ALL) | \
203 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
204 (1ULL << VIRTIO_NET_F_GSO) | \
205 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
206 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
207 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
208 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
209 (1ULL << VIRTIO_NET_F_CSUM) | \
210 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
211 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
212 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
213 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
214 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
215 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
216 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
217 (1ULL << VIRTIO_NET_F_MTU) | \
218 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
222 uint64_t guest_phys_addr;
223 uint64_t host_phys_addr;
228 * function prototype for the vhost backend to handler specific vhost user
229 * messages prior to the master message handling
235 * @param require_reply
236 * If the handler requires sending a reply, this varaible shall be written 1,
239 * If the handler requires skipping the master message handling, this variable
240 * shall be written 1, otherwise 0.
242 * 0 on success, -1 on failure
244 typedef int (*vhost_msg_pre_handle)(int vid, void *msg,
245 uint32_t *require_reply, uint32_t *skip_master);
248 * function prototype for the vhost backend to handler specific vhost user
249 * messages after the master message handling is done
255 * @param require_reply
256 * If the handler requires sending a reply, this varaible shall be written 1,
259 * 0 on success, -1 on failure
261 typedef int (*vhost_msg_post_handle)(int vid, void *msg,
262 uint32_t *require_reply);
265 * pre and post vhost user message handlers
267 struct vhost_user_extern_ops {
268 vhost_msg_pre_handle pre_msg_handle;
269 vhost_msg_post_handle post_msg_handle;
273 * Device structure contains all configuration information relating
277 /* Frontend (QEMU) memory and memory region information */
278 struct rte_vhost_memory *mem;
280 uint64_t protocol_features;
284 /* to tell if we need broadcast rarp packet */
285 rte_atomic16_t broadcast_rarp;
287 int dequeue_zero_copy;
288 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
289 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
290 char ifname[IF_NAME_SZ];
294 struct ether_addr mac;
297 struct vhost_device_ops const *notify_ops;
299 uint32_t nr_guest_pages;
300 uint32_t max_guest_pages;
301 struct guest_page *guest_pages;
304 rte_spinlock_t slave_req_lock;
307 * Device id to identify a specific backend device.
308 * It's set to -1 for the default software implementation.
312 /* private data for virtio device */
314 /* pre and post vhost user message handlers for the device */
315 struct vhost_user_extern_ops extern_ops;
316 } __rte_cache_aligned;
318 #define VHOST_LOG_PAGE 4096
321 * Atomically set a bit in memory.
323 static __rte_always_inline void
324 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
326 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
328 * __sync_ built-ins are deprecated, but __atomic_ ones
329 * are sub-optimized in older GCC versions.
331 __sync_fetch_and_or_1(addr, (1U << nr));
333 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
337 static __rte_always_inline void
338 vhost_log_page(uint8_t *log_base, uint64_t page)
340 vhost_set_bit(page % 8, &log_base[page / 8]);
343 static __rte_always_inline void
344 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
348 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
349 !dev->log_base || !len))
352 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
355 /* To make sure guest memory updates are committed before logging */
358 page = addr / VHOST_LOG_PAGE;
359 while (page * VHOST_LOG_PAGE < addr + len) {
360 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
365 static __rte_always_inline void
366 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
368 unsigned long *log_base;
371 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
375 log_base = (unsigned long *)(uintptr_t)dev->log_base;
378 * It is expected a write memory barrier has been issued
379 * before this function is called.
382 for (i = 0; i < vq->log_cache_nb_elem; i++) {
383 struct log_cache_entry *elem = vq->log_cache + i;
385 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
387 * '__sync' builtins are deprecated, but '__atomic' ones
388 * are sub-optimized in older GCC versions.
390 __sync_fetch_and_or(log_base + elem->offset, elem->val);
392 __atomic_fetch_or(log_base + elem->offset, elem->val,
399 vq->log_cache_nb_elem = 0;
402 static __rte_always_inline void
403 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
406 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
407 uint32_t offset = page / (sizeof(unsigned long) << 3);
410 for (i = 0; i < vq->log_cache_nb_elem; i++) {
411 struct log_cache_entry *elem = vq->log_cache + i;
413 if (elem->offset == offset) {
414 elem->val |= (1UL << bit_nr);
419 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
421 * No more room for a new log cache entry,
422 * so write the dirty log map directly.
425 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
430 vq->log_cache[i].offset = offset;
431 vq->log_cache[i].val = (1UL << bit_nr);
434 static __rte_always_inline void
435 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
436 uint64_t addr, uint64_t len)
440 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
441 !dev->log_base || !len))
444 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
447 page = addr / VHOST_LOG_PAGE;
448 while (page * VHOST_LOG_PAGE < addr + len) {
449 vhost_log_cache_page(dev, vq, page);
454 static __rte_always_inline void
455 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
456 uint64_t offset, uint64_t len)
458 vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
461 static __rte_always_inline void
462 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
463 uint64_t offset, uint64_t len)
465 vhost_log_write(dev, vq->log_guest_addr + offset, len);
468 /* Macros for printing using RTE_LOG */
469 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
470 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
472 #ifdef RTE_LIBRTE_VHOST_DEBUG
473 #define VHOST_MAX_PRINT_BUFF 6072
474 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
475 RTE_LOG(DEBUG, log_type, fmt, ##args)
476 #define PRINT_PACKET(device, addr, size, header) do { \
477 char *pkt_addr = (char *)(addr); \
478 unsigned int index; \
479 char packet[VHOST_MAX_PRINT_BUFF]; \
482 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
484 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
485 for (index = 0; index < (size); index++) { \
486 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
487 "%02hhx ", pkt_addr[index]); \
489 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
491 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
494 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
495 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
498 extern uint64_t VHOST_FEATURES;
499 #define MAX_VHOST_DEVICE 1024
500 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
502 /* Convert guest physical address to host physical address */
503 static __rte_always_inline rte_iova_t
504 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
507 struct guest_page *page;
509 for (i = 0; i < dev->nr_guest_pages; i++) {
510 page = &dev->guest_pages[i];
512 if (gpa >= page->guest_phys_addr &&
513 gpa + size < page->guest_phys_addr + page->size) {
514 return gpa - page->guest_phys_addr +
515 page->host_phys_addr;
522 static __rte_always_inline struct virtio_net *
525 struct virtio_net *dev = vhost_devices[vid];
527 if (unlikely(!dev)) {
528 RTE_LOG(ERR, VHOST_CONFIG,
529 "(%d) device not found.\n", vid);
535 int vhost_new_device(void);
536 void cleanup_device(struct virtio_net *dev, int destroy);
537 void reset_device(struct virtio_net *dev);
538 void vhost_destroy_device(int);
539 void vhost_destroy_device_notify(struct virtio_net *dev);
541 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
542 void free_vq(struct vhost_virtqueue *vq);
544 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
546 void vhost_attach_vdpa_device(int vid, int did);
547 void vhost_detach_vdpa_device(int vid);
549 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
550 void vhost_enable_dequeue_zero_copy(int vid);
551 void vhost_set_builtin_virtio_net(int vid, bool enable);
553 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
556 * Backend-specific cleanup.
558 * TODO: fix it; we have one backend now
560 void vhost_backend_cleanup(struct virtio_net *dev);
562 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
563 uint64_t iova, uint64_t *len, uint8_t perm);
564 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
565 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
567 static __rte_always_inline uint64_t
568 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
569 uint64_t iova, uint64_t *len, uint8_t perm)
571 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
572 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
574 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
577 #define vhost_used_event(vr) \
578 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
581 * The following is used with VIRTIO_RING_F_EVENT_IDX.
582 * Assuming a given event_idx value from the other size, if we have
583 * just incremented index from old to new_idx, should we trigger an
586 static __rte_always_inline int
587 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
589 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
592 static __rte_always_inline void
593 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
595 /* Flush used->idx update before we read avail->flags. */
598 /* Don't kick guest if we don't reach index specified by guest. */
599 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
600 uint16_t old = vq->signalled_used;
601 uint16_t new = vq->last_used_idx;
603 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
605 vhost_used_event(vq),
607 if (vhost_need_event(vhost_used_event(vq), new, old)
608 && (vq->callfd >= 0)) {
609 vq->signalled_used = vq->last_used_idx;
610 eventfd_write(vq->callfd, (eventfd_t) 1);
613 /* Kick the guest if necessary. */
614 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
615 && (vq->callfd >= 0))
616 eventfd_write(vq->callfd, (eventfd_t)1);
620 #endif /* _VHOST_NET_CDEV_H_ */