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 /* Last used index we notify to front end. */
78 uint16_t signalled_used;
79 #define VIRTIO_INVALID_EVENTFD (-1)
80 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
82 /* Backend value to determine if device should started/stopped */
84 /* Used to notify the guest (trigger interrupt) */
86 /* Currently unused as polling mode is enabled */
91 /* Physical address of used ring, for logging */
92 uint64_t log_guest_addr;
96 uint16_t last_zmbuf_idx;
97 struct zcopy_mbuf *zmbufs;
98 struct zcopy_mbuf_list zmbuf_list;
100 struct vring_used_elem *shadow_used_ring;
101 uint16_t shadow_used_idx;
102 struct vhost_vring_addr ring_addrs;
104 struct batch_copy_elem *batch_copy_elems;
105 uint16_t batch_copy_nb_elems;
107 rte_rwlock_t iotlb_lock;
108 rte_rwlock_t iotlb_pending_lock;
109 struct rte_mempool *iotlb_pool;
110 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
112 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
113 } __rte_cache_aligned;
115 /* Old kernels have no such macros defined */
116 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
117 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
120 #ifndef VIRTIO_NET_F_MQ
121 #define VIRTIO_NET_F_MQ 22
124 #define VHOST_MAX_VRING 0x100
125 #define VHOST_MAX_QUEUE_PAIRS 0x80
127 #ifndef VIRTIO_NET_F_MTU
128 #define VIRTIO_NET_F_MTU 3
131 /* Declare IOMMU related bits for older kernels */
132 #ifndef VIRTIO_F_IOMMU_PLATFORM
134 #define VIRTIO_F_IOMMU_PLATFORM 33
136 struct vhost_iotlb_msg {
140 #define VHOST_ACCESS_RO 0x1
141 #define VHOST_ACCESS_WO 0x2
142 #define VHOST_ACCESS_RW 0x3
144 #define VHOST_IOTLB_MISS 1
145 #define VHOST_IOTLB_UPDATE 2
146 #define VHOST_IOTLB_INVALIDATE 3
147 #define VHOST_IOTLB_ACCESS_FAIL 4
151 #define VHOST_IOTLB_MSG 0x1
156 struct vhost_iotlb_msg iotlb;
163 * Define virtio 1.0 for older kernels
165 #ifndef VIRTIO_F_VERSION_1
166 #define VIRTIO_F_VERSION_1 32
169 #define VHOST_USER_F_PROTOCOL_FEATURES 30
171 /* Features supported by this builtin vhost-user net driver. */
172 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
173 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
174 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
175 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
176 (1ULL << VIRTIO_NET_F_MQ) | \
177 (1ULL << VIRTIO_F_VERSION_1) | \
178 (1ULL << VHOST_F_LOG_ALL) | \
179 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
180 (1ULL << VIRTIO_NET_F_GSO) | \
181 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
182 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
183 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
184 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
185 (1ULL << VIRTIO_NET_F_CSUM) | \
186 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
187 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
188 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
189 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
190 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
191 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
192 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
193 (1ULL << VIRTIO_NET_F_MTU) | \
194 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
198 uint64_t guest_phys_addr;
199 uint64_t host_phys_addr;
204 * Device structure contains all configuration information relating
208 /* Frontend (QEMU) memory and memory region information */
209 struct rte_vhost_memory *mem;
211 uint64_t protocol_features;
215 /* to tell if we need broadcast rarp packet */
216 rte_atomic16_t broadcast_rarp;
218 int dequeue_zero_copy;
219 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
220 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
221 char ifname[IF_NAME_SZ];
225 struct ether_addr mac;
228 struct vhost_device_ops const *notify_ops;
230 uint32_t nr_guest_pages;
231 uint32_t max_guest_pages;
232 struct guest_page *guest_pages;
235 } __rte_cache_aligned;
238 #define VHOST_LOG_PAGE 4096
241 * Atomically set a bit in memory.
243 static __rte_always_inline void
244 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
246 __sync_fetch_and_or_8(addr, (1U << nr));
249 static __rte_always_inline void
250 vhost_log_page(uint8_t *log_base, uint64_t page)
252 vhost_set_bit(page % 8, &log_base[page / 8]);
255 static __rte_always_inline void
256 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
260 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
261 !dev->log_base || !len))
264 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
267 /* To make sure guest memory updates are committed before logging */
270 page = addr / VHOST_LOG_PAGE;
271 while (page * VHOST_LOG_PAGE < addr + len) {
272 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
277 static __rte_always_inline void
278 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
279 uint64_t offset, uint64_t len)
281 vhost_log_write(dev, vq->log_guest_addr + offset, len);
284 /* Macros for printing using RTE_LOG */
285 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
286 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
288 #ifdef RTE_LIBRTE_VHOST_DEBUG
289 #define VHOST_MAX_PRINT_BUFF 6072
290 #define LOG_LEVEL RTE_LOG_DEBUG
291 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
292 #define PRINT_PACKET(device, addr, size, header) do { \
293 char *pkt_addr = (char *)(addr); \
294 unsigned int index; \
295 char packet[VHOST_MAX_PRINT_BUFF]; \
298 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
300 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
301 for (index = 0; index < (size); index++) { \
302 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
303 "%02hhx ", pkt_addr[index]); \
305 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
307 LOG_DEBUG(VHOST_DATA, "%s", packet); \
310 #define LOG_LEVEL RTE_LOG_INFO
311 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
312 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
315 extern uint64_t VHOST_FEATURES;
316 #define MAX_VHOST_DEVICE 1024
317 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
319 /* Convert guest physical address to host physical address */
320 static __rte_always_inline rte_iova_t
321 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
324 struct guest_page *page;
326 for (i = 0; i < dev->nr_guest_pages; i++) {
327 page = &dev->guest_pages[i];
329 if (gpa >= page->guest_phys_addr &&
330 gpa + size < page->guest_phys_addr + page->size) {
331 return gpa - page->guest_phys_addr +
332 page->host_phys_addr;
339 struct virtio_net *get_device(int vid);
341 int vhost_new_device(void);
342 void cleanup_device(struct virtio_net *dev, int destroy);
343 void reset_device(struct virtio_net *dev);
344 void vhost_destroy_device(int);
346 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
347 void free_vq(struct vhost_virtqueue *vq);
349 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
351 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
352 void vhost_enable_dequeue_zero_copy(int vid);
354 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
357 * Backend-specific cleanup.
359 * TODO: fix it; we have one backend now
361 void vhost_backend_cleanup(struct virtio_net *dev);
363 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
364 uint64_t iova, uint64_t size, uint8_t perm);
365 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
366 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
368 static __rte_always_inline uint64_t
369 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
370 uint64_t iova, uint64_t size, uint8_t perm)
372 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
373 return rte_vhost_gpa_to_vva(dev->mem, iova);
375 return __vhost_iova_to_vva(dev, vq, iova, size, perm);
378 #define vhost_used_event(vr) \
379 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
382 * The following is used with VIRTIO_RING_F_EVENT_IDX.
383 * Assuming a given event_idx value from the other size, if we have
384 * just incremented index from old to new_idx, should we trigger an
387 static __rte_always_inline int
388 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
390 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
393 static __rte_always_inline void
394 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
396 /* Flush used->idx update before we read avail->flags. */
399 /* Don't kick guest if we don't reach index specified by guest. */
400 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
401 uint16_t old = vq->signalled_used;
402 uint16_t new = vq->last_used_idx;
404 LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
406 vhost_used_event(vq),
408 if (vhost_need_event(vhost_used_event(vq), new, old)
409 && (vq->callfd >= 0)) {
410 vq->signalled_used = vq->last_used_idx;
411 eventfd_write(vq->callfd, (eventfd_t) 1);
414 /* Kick the guest if necessary. */
415 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
416 && (vq->callfd >= 0))
417 eventfd_write(vq->callfd, (eventfd_t)1);
421 #endif /* _VHOST_NET_CDEV_H_ */