1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #ifndef _VHOST_NET_CDEV_H_
6 #define _VHOST_NET_CDEV_H_
10 #include <sys/queue.h>
12 #include <linux/vhost.h>
13 #include <linux/virtio_net.h>
14 #include <sys/socket.h>
18 #include <rte_ether.h>
19 #include <rte_rwlock.h>
21 #include "rte_vhost.h"
23 /* Used to indicate that the device is running on a data core */
24 #define VIRTIO_DEV_RUNNING 1
25 /* Used to indicate that the device is ready to operate */
26 #define VIRTIO_DEV_READY 2
28 /* Backend value set by guest. */
29 #define VIRTIO_DEV_STOPPED -1
31 #define BUF_VECTOR_MAX 256
34 * Structure contains buffer address, length and descriptor index
35 * from vring to do scatter RX.
44 * A structure to hold some fields needed in zero copy code path,
45 * mainly for associating an mbuf with the right desc_idx.
48 struct rte_mbuf *mbuf;
52 TAILQ_ENTRY(zcopy_mbuf) next;
54 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
57 * Structure contains the info for each batched memory copy.
59 struct batch_copy_elem {
67 * Structure contains variables relevant to RX/TX virtqueues.
69 struct vhost_virtqueue {
70 struct vring_desc *desc;
71 struct vring_avail *avail;
72 struct vring_used *used;
75 uint16_t last_avail_idx;
76 uint16_t last_used_idx;
77 #define VIRTIO_INVALID_EVENTFD (-1)
78 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
80 /* Backend value to determine if device should started/stopped */
82 /* Used to notify the guest (trigger interrupt) */
84 /* Currently unused as polling mode is enabled */
89 /* Physical address of used ring, for logging */
90 uint64_t log_guest_addr;
94 uint16_t last_zmbuf_idx;
95 struct zcopy_mbuf *zmbufs;
96 struct zcopy_mbuf_list zmbuf_list;
98 struct vring_used_elem *shadow_used_ring;
99 uint16_t shadow_used_idx;
100 struct vhost_vring_addr ring_addrs;
102 struct batch_copy_elem *batch_copy_elems;
103 uint16_t batch_copy_nb_elems;
105 rte_rwlock_t iotlb_lock;
106 rte_rwlock_t iotlb_pending_lock;
107 struct rte_mempool *iotlb_pool;
108 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
110 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
111 } __rte_cache_aligned;
113 /* Old kernels have no such macros defined */
114 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
115 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
118 #ifndef VIRTIO_NET_F_MQ
119 #define VIRTIO_NET_F_MQ 22
122 #define VHOST_MAX_VRING 0x100
123 #define VHOST_MAX_QUEUE_PAIRS 0x80
125 #ifndef VIRTIO_NET_F_MTU
126 #define VIRTIO_NET_F_MTU 3
129 /* Declare IOMMU related bits for older kernels */
130 #ifndef VIRTIO_F_IOMMU_PLATFORM
132 #define VIRTIO_F_IOMMU_PLATFORM 33
134 struct vhost_iotlb_msg {
138 #define VHOST_ACCESS_RO 0x1
139 #define VHOST_ACCESS_WO 0x2
140 #define VHOST_ACCESS_RW 0x3
142 #define VHOST_IOTLB_MISS 1
143 #define VHOST_IOTLB_UPDATE 2
144 #define VHOST_IOTLB_INVALIDATE 3
145 #define VHOST_IOTLB_ACCESS_FAIL 4
149 #define VHOST_IOTLB_MSG 0x1
154 struct vhost_iotlb_msg iotlb;
161 * Define virtio 1.0 for older kernels
163 #ifndef VIRTIO_F_VERSION_1
164 #define VIRTIO_F_VERSION_1 32
167 #define VHOST_USER_F_PROTOCOL_FEATURES 30
169 /* Features supported by this builtin vhost-user net driver. */
170 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
171 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
172 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
173 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
174 (1ULL << VIRTIO_NET_F_MQ) | \
175 (1ULL << VIRTIO_F_VERSION_1) | \
176 (1ULL << VHOST_F_LOG_ALL) | \
177 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
178 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
179 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
180 (1ULL << VIRTIO_NET_F_CSUM) | \
181 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
182 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
183 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
184 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
185 (1ULL << VIRTIO_NET_F_MTU) | \
186 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
190 uint64_t guest_phys_addr;
191 uint64_t host_phys_addr;
196 * Device structure contains all configuration information relating
200 /* Frontend (QEMU) memory and memory region information */
201 struct rte_vhost_memory *mem;
203 uint64_t protocol_features;
207 /* to tell if we need broadcast rarp packet */
208 rte_atomic16_t broadcast_rarp;
210 int dequeue_zero_copy;
211 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
212 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
213 char ifname[IF_NAME_SZ];
217 struct ether_addr mac;
220 struct vhost_device_ops const *notify_ops;
222 uint32_t nr_guest_pages;
223 uint32_t max_guest_pages;
224 struct guest_page *guest_pages;
227 } __rte_cache_aligned;
230 #define VHOST_LOG_PAGE 4096
233 * Atomically set a bit in memory.
235 static __rte_always_inline void
236 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
238 __sync_fetch_and_or_8(addr, (1U << nr));
241 static __rte_always_inline void
242 vhost_log_page(uint8_t *log_base, uint64_t page)
244 vhost_set_bit(page % 8, &log_base[page / 8]);
247 static __rte_always_inline void
248 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
252 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
253 !dev->log_base || !len))
256 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
259 /* To make sure guest memory updates are committed before logging */
262 page = addr / VHOST_LOG_PAGE;
263 while (page * VHOST_LOG_PAGE < addr + len) {
264 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
269 static __rte_always_inline void
270 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
271 uint64_t offset, uint64_t len)
273 vhost_log_write(dev, vq->log_guest_addr + offset, len);
276 /* Macros for printing using RTE_LOG */
277 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
278 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
280 #ifdef RTE_LIBRTE_VHOST_DEBUG
281 #define VHOST_MAX_PRINT_BUFF 6072
282 #define LOG_LEVEL RTE_LOG_DEBUG
283 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
284 #define PRINT_PACKET(device, addr, size, header) do { \
285 char *pkt_addr = (char *)(addr); \
286 unsigned int index; \
287 char packet[VHOST_MAX_PRINT_BUFF]; \
290 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
292 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
293 for (index = 0; index < (size); index++) { \
294 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
295 "%02hhx ", pkt_addr[index]); \
297 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
299 LOG_DEBUG(VHOST_DATA, "%s", packet); \
302 #define LOG_LEVEL RTE_LOG_INFO
303 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
304 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
307 extern uint64_t VHOST_FEATURES;
308 #define MAX_VHOST_DEVICE 1024
309 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
311 /* Convert guest physical address to host physical address */
312 static __rte_always_inline rte_iova_t
313 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
316 struct guest_page *page;
318 for (i = 0; i < dev->nr_guest_pages; i++) {
319 page = &dev->guest_pages[i];
321 if (gpa >= page->guest_phys_addr &&
322 gpa + size < page->guest_phys_addr + page->size) {
323 return gpa - page->guest_phys_addr +
324 page->host_phys_addr;
331 struct virtio_net *get_device(int vid);
333 int vhost_new_device(void);
334 void cleanup_device(struct virtio_net *dev, int destroy);
335 void reset_device(struct virtio_net *dev);
336 void vhost_destroy_device(int);
338 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
340 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
341 void vhost_enable_dequeue_zero_copy(int vid);
343 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
346 * Backend-specific cleanup.
348 * TODO: fix it; we have one backend now
350 void vhost_backend_cleanup(struct virtio_net *dev);
352 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
353 uint64_t iova, uint64_t size, uint8_t perm);
354 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
355 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
357 static __rte_always_inline uint64_t
358 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
359 uint64_t iova, uint64_t size, uint8_t perm)
361 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
362 return rte_vhost_gpa_to_vva(dev->mem, iova);
364 return __vhost_iova_to_vva(dev, vq, iova, size, perm);
367 #endif /* _VHOST_NET_CDEV_H_ */