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
196 * Available and used descs are in same order
198 #ifndef VIRTIO_F_IN_ORDER
199 #define VIRTIO_F_IN_ORDER 35
202 /* Features supported by this builtin vhost-user net driver. */
203 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
204 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
205 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
206 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
207 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
208 (1ULL << VIRTIO_NET_F_MQ) | \
209 (1ULL << VIRTIO_F_VERSION_1) | \
210 (1ULL << VHOST_F_LOG_ALL) | \
211 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
212 (1ULL << VIRTIO_NET_F_GSO) | \
213 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
214 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
215 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
216 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
217 (1ULL << VIRTIO_NET_F_CSUM) | \
218 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
219 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
220 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
221 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
222 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
223 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
224 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
225 (1ULL << VIRTIO_NET_F_MTU) | \
226 (1ULL << VIRTIO_F_IN_ORDER) | \
227 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
231 uint64_t guest_phys_addr;
232 uint64_t host_phys_addr;
237 * function prototype for the vhost backend to handler specific vhost user
238 * messages prior to the master message handling
244 * @param require_reply
245 * If the handler requires sending a reply, this varaible shall be written 1,
248 * If the handler requires skipping the master message handling, this variable
249 * shall be written 1, otherwise 0.
251 * 0 on success, -1 on failure
253 typedef int (*vhost_msg_pre_handle)(int vid, void *msg,
254 uint32_t *require_reply, uint32_t *skip_master);
257 * function prototype for the vhost backend to handler specific vhost user
258 * messages after the master message handling is done
264 * @param require_reply
265 * If the handler requires sending a reply, this varaible shall be written 1,
268 * 0 on success, -1 on failure
270 typedef int (*vhost_msg_post_handle)(int vid, void *msg,
271 uint32_t *require_reply);
274 * pre and post vhost user message handlers
276 struct vhost_user_extern_ops {
277 vhost_msg_pre_handle pre_msg_handle;
278 vhost_msg_post_handle post_msg_handle;
282 * Device structure contains all configuration information relating
286 /* Frontend (QEMU) memory and memory region information */
287 struct rte_vhost_memory *mem;
289 uint64_t protocol_features;
293 /* to tell if we need broadcast rarp packet */
294 rte_atomic16_t broadcast_rarp;
296 int dequeue_zero_copy;
297 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
298 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
299 char ifname[IF_NAME_SZ];
303 struct ether_addr mac;
306 struct vhost_device_ops const *notify_ops;
308 uint32_t nr_guest_pages;
309 uint32_t max_guest_pages;
310 struct guest_page *guest_pages;
313 rte_spinlock_t slave_req_lock;
316 * Device id to identify a specific backend device.
317 * It's set to -1 for the default software implementation.
321 /* private data for virtio device */
323 /* pre and post vhost user message handlers for the device */
324 struct vhost_user_extern_ops extern_ops;
325 } __rte_cache_aligned;
327 #define VHOST_LOG_PAGE 4096
330 * Atomically set a bit in memory.
332 static __rte_always_inline void
333 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
335 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
337 * __sync_ built-ins are deprecated, but __atomic_ ones
338 * are sub-optimized in older GCC versions.
340 __sync_fetch_and_or_1(addr, (1U << nr));
342 __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
346 static __rte_always_inline void
347 vhost_log_page(uint8_t *log_base, uint64_t page)
349 vhost_set_bit(page % 8, &log_base[page / 8]);
352 static __rte_always_inline void
353 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
357 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
358 !dev->log_base || !len))
361 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
364 /* To make sure guest memory updates are committed before logging */
367 page = addr / VHOST_LOG_PAGE;
368 while (page * VHOST_LOG_PAGE < addr + len) {
369 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
374 static __rte_always_inline void
375 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
377 unsigned long *log_base;
380 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
384 log_base = (unsigned long *)(uintptr_t)dev->log_base;
387 * It is expected a write memory barrier has been issued
388 * before this function is called.
391 for (i = 0; i < vq->log_cache_nb_elem; i++) {
392 struct log_cache_entry *elem = vq->log_cache + i;
394 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
396 * '__sync' builtins are deprecated, but '__atomic' ones
397 * are sub-optimized in older GCC versions.
399 __sync_fetch_and_or(log_base + elem->offset, elem->val);
401 __atomic_fetch_or(log_base + elem->offset, elem->val,
408 vq->log_cache_nb_elem = 0;
411 static __rte_always_inline void
412 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
415 uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
416 uint32_t offset = page / (sizeof(unsigned long) << 3);
419 for (i = 0; i < vq->log_cache_nb_elem; i++) {
420 struct log_cache_entry *elem = vq->log_cache + i;
422 if (elem->offset == offset) {
423 elem->val |= (1UL << bit_nr);
428 if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
430 * No more room for a new log cache entry,
431 * so write the dirty log map directly.
434 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
439 vq->log_cache[i].offset = offset;
440 vq->log_cache[i].val = (1UL << bit_nr);
441 vq->log_cache_nb_elem++;
444 static __rte_always_inline void
445 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
446 uint64_t addr, uint64_t len)
450 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
451 !dev->log_base || !len))
454 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
457 page = addr / VHOST_LOG_PAGE;
458 while (page * VHOST_LOG_PAGE < addr + len) {
459 vhost_log_cache_page(dev, vq, page);
464 static __rte_always_inline void
465 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
466 uint64_t offset, uint64_t len)
468 vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
471 static __rte_always_inline void
472 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
473 uint64_t offset, uint64_t len)
475 vhost_log_write(dev, vq->log_guest_addr + offset, len);
478 /* Macros for printing using RTE_LOG */
479 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
480 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
482 #ifdef RTE_LIBRTE_VHOST_DEBUG
483 #define VHOST_MAX_PRINT_BUFF 6072
484 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
485 RTE_LOG(DEBUG, log_type, fmt, ##args)
486 #define PRINT_PACKET(device, addr, size, header) do { \
487 char *pkt_addr = (char *)(addr); \
488 unsigned int index; \
489 char packet[VHOST_MAX_PRINT_BUFF]; \
492 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
494 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
495 for (index = 0; index < (size); index++) { \
496 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
497 "%02hhx ", pkt_addr[index]); \
499 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
501 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
504 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
505 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
508 extern uint64_t VHOST_FEATURES;
509 #define MAX_VHOST_DEVICE 1024
510 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
512 /* Convert guest physical address to host physical address */
513 static __rte_always_inline rte_iova_t
514 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
517 struct guest_page *page;
519 for (i = 0; i < dev->nr_guest_pages; i++) {
520 page = &dev->guest_pages[i];
522 if (gpa >= page->guest_phys_addr &&
523 gpa + size < page->guest_phys_addr + page->size) {
524 return gpa - page->guest_phys_addr +
525 page->host_phys_addr;
532 static __rte_always_inline struct virtio_net *
535 struct virtio_net *dev = vhost_devices[vid];
537 if (unlikely(!dev)) {
538 RTE_LOG(ERR, VHOST_CONFIG,
539 "(%d) device not found.\n", vid);
545 int vhost_new_device(void);
546 void cleanup_device(struct virtio_net *dev, int destroy);
547 void reset_device(struct virtio_net *dev);
548 void vhost_destroy_device(int);
549 void vhost_destroy_device_notify(struct virtio_net *dev);
551 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
552 void free_vq(struct vhost_virtqueue *vq);
554 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
556 void vhost_attach_vdpa_device(int vid, int did);
557 void vhost_detach_vdpa_device(int vid);
559 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
560 void vhost_enable_dequeue_zero_copy(int vid);
561 void vhost_set_builtin_virtio_net(int vid, bool enable);
563 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
566 * Backend-specific cleanup.
568 * TODO: fix it; we have one backend now
570 void vhost_backend_cleanup(struct virtio_net *dev);
572 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
573 uint64_t iova, uint64_t *len, uint8_t perm);
574 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
575 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
577 static __rte_always_inline uint64_t
578 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
579 uint64_t iova, uint64_t *len, uint8_t perm)
581 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
582 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
584 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
587 #define vhost_used_event(vr) \
588 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
591 * The following is used with VIRTIO_RING_F_EVENT_IDX.
592 * Assuming a given event_idx value from the other size, if we have
593 * just incremented index from old to new_idx, should we trigger an
596 static __rte_always_inline int
597 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
599 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
602 static __rte_always_inline void
603 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
605 /* Flush used->idx update before we read avail->flags. */
608 /* Don't kick guest if we don't reach index specified by guest. */
609 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
610 uint16_t old = vq->signalled_used;
611 uint16_t new = vq->last_used_idx;
613 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
615 vhost_used_event(vq),
617 if (vhost_need_event(vhost_used_event(vq), new, old)
618 && (vq->callfd >= 0)) {
619 vq->signalled_used = vq->last_used_idx;
620 eventfd_write(vq->callfd, (eventfd_t) 1);
623 /* Kick the guest if necessary. */
624 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
625 && (vq->callfd >= 0))
626 eventfd_write(vq->callfd, (eventfd_t)1);
630 #endif /* _VHOST_NET_CDEV_H_ */