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/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"
24 /* Used to indicate that the device is running on a data core */
25 #define VIRTIO_DEV_RUNNING 1
26 /* Used to indicate that the device is ready to operate */
27 #define VIRTIO_DEV_READY 2
28 /* Used to indicate that the built-in vhost net device backend is enabled */
29 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
31 /* Backend value set by guest. */
32 #define VIRTIO_DEV_STOPPED -1
34 #define BUF_VECTOR_MAX 256
37 * Structure contains buffer address, length and descriptor index
38 * from vring to do scatter RX.
47 * A structure to hold some fields needed in zero copy code path,
48 * mainly for associating an mbuf with the right desc_idx.
51 struct rte_mbuf *mbuf;
55 TAILQ_ENTRY(zcopy_mbuf) next;
57 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
60 * Structure contains the info for each batched memory copy.
62 struct batch_copy_elem {
70 * Structure contains variables relevant to RX/TX virtqueues.
72 struct vhost_virtqueue {
73 struct vring_desc *desc;
74 struct vring_avail *avail;
75 struct vring_used *used;
78 uint16_t last_avail_idx;
79 uint16_t last_used_idx;
80 /* Last used index we notify to front end. */
81 uint16_t signalled_used;
82 #define VIRTIO_INVALID_EVENTFD (-1)
83 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
85 /* Backend value to determine if device should started/stopped */
89 rte_spinlock_t access_lock;
91 /* Used to notify the guest (trigger interrupt) */
93 /* Currently unused as polling mode is enabled */
96 /* Physical address of used ring, for logging */
97 uint64_t log_guest_addr;
101 uint16_t last_zmbuf_idx;
102 struct zcopy_mbuf *zmbufs;
103 struct zcopy_mbuf_list zmbuf_list;
105 struct vring_used_elem *shadow_used_ring;
106 uint16_t shadow_used_idx;
107 struct vhost_vring_addr ring_addrs;
109 struct batch_copy_elem *batch_copy_elems;
110 uint16_t batch_copy_nb_elems;
112 rte_rwlock_t iotlb_lock;
113 rte_rwlock_t iotlb_pending_lock;
114 struct rte_mempool *iotlb_pool;
115 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
117 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
118 } __rte_cache_aligned;
120 /* Old kernels have no such macros defined */
121 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
122 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
125 #ifndef VIRTIO_NET_F_MQ
126 #define VIRTIO_NET_F_MQ 22
129 #define VHOST_MAX_VRING 0x100
130 #define VHOST_MAX_QUEUE_PAIRS 0x80
132 #ifndef VIRTIO_NET_F_MTU
133 #define VIRTIO_NET_F_MTU 3
136 #ifndef VIRTIO_F_ANY_LAYOUT
137 #define VIRTIO_F_ANY_LAYOUT 27
140 /* Declare IOMMU related bits for older kernels */
141 #ifndef VIRTIO_F_IOMMU_PLATFORM
143 #define VIRTIO_F_IOMMU_PLATFORM 33
145 struct vhost_iotlb_msg {
149 #define VHOST_ACCESS_RO 0x1
150 #define VHOST_ACCESS_WO 0x2
151 #define VHOST_ACCESS_RW 0x3
153 #define VHOST_IOTLB_MISS 1
154 #define VHOST_IOTLB_UPDATE 2
155 #define VHOST_IOTLB_INVALIDATE 3
156 #define VHOST_IOTLB_ACCESS_FAIL 4
160 #define VHOST_IOTLB_MSG 0x1
165 struct vhost_iotlb_msg iotlb;
172 * Define virtio 1.0 for older kernels
174 #ifndef VIRTIO_F_VERSION_1
175 #define VIRTIO_F_VERSION_1 32
178 #define VHOST_USER_F_PROTOCOL_FEATURES 30
180 /* Features supported by this builtin vhost-user net driver. */
181 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
182 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
183 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
184 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
185 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
186 (1ULL << VIRTIO_NET_F_MQ) | \
187 (1ULL << VIRTIO_F_VERSION_1) | \
188 (1ULL << VHOST_F_LOG_ALL) | \
189 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
190 (1ULL << VIRTIO_NET_F_GSO) | \
191 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
192 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
193 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
194 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
195 (1ULL << VIRTIO_NET_F_CSUM) | \
196 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
197 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
198 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
199 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
200 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
201 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
202 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
203 (1ULL << VIRTIO_NET_F_MTU) | \
204 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
208 uint64_t guest_phys_addr;
209 uint64_t host_phys_addr;
214 * Device structure contains all configuration information relating
218 /* Frontend (QEMU) memory and memory region information */
219 struct rte_vhost_memory *mem;
221 uint64_t protocol_features;
225 /* to tell if we need broadcast rarp packet */
226 rte_atomic16_t broadcast_rarp;
228 int dequeue_zero_copy;
229 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
230 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
231 char ifname[IF_NAME_SZ];
235 struct ether_addr mac;
238 struct vhost_device_ops const *notify_ops;
240 uint32_t nr_guest_pages;
241 uint32_t max_guest_pages;
242 struct guest_page *guest_pages;
245 } __rte_cache_aligned;
248 #define VHOST_LOG_PAGE 4096
251 * Mark all pages belonging to the same dirty log bitmap byte
252 * as dirty. The goal is to avoid concurrency between different
253 * threads doing atomic read-modify-writes on the same byte.
255 static __rte_always_inline void
256 vhost_log_page(uint8_t *log_base, uint64_t page)
258 log_base[page / 8] = 0xff;
261 static __rte_always_inline void
262 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
266 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
267 !dev->log_base || !len))
270 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
273 /* To make sure guest memory updates are committed before logging */
276 page = addr / VHOST_LOG_PAGE;
277 while (page * VHOST_LOG_PAGE < addr + len) {
278 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
283 static __rte_always_inline void
284 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
285 uint64_t offset, uint64_t len)
287 vhost_log_write(dev, vq->log_guest_addr + offset, len);
290 /* Macros for printing using RTE_LOG */
291 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
292 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
294 #ifdef RTE_LIBRTE_VHOST_DEBUG
295 #define VHOST_MAX_PRINT_BUFF 6072
296 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
297 RTE_LOG(DEBUG, log_type, fmt, ##args)
298 #define PRINT_PACKET(device, addr, size, header) do { \
299 char *pkt_addr = (char *)(addr); \
300 unsigned int index; \
301 char packet[VHOST_MAX_PRINT_BUFF]; \
304 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
306 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
307 for (index = 0; index < (size); index++) { \
308 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
309 "%02hhx ", pkt_addr[index]); \
311 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
313 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
316 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
317 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
320 extern uint64_t VHOST_FEATURES;
321 #define MAX_VHOST_DEVICE 1024
322 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
324 /* Convert guest physical address to host physical address */
325 static __rte_always_inline rte_iova_t
326 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
329 struct guest_page *page;
331 for (i = 0; i < dev->nr_guest_pages; i++) {
332 page = &dev->guest_pages[i];
334 if (gpa >= page->guest_phys_addr &&
335 gpa + size < page->guest_phys_addr + page->size) {
336 return gpa - page->guest_phys_addr +
337 page->host_phys_addr;
344 static __rte_always_inline struct virtio_net *
347 struct virtio_net *dev = vhost_devices[vid];
349 if (unlikely(!dev)) {
350 RTE_LOG(ERR, VHOST_CONFIG,
351 "(%d) device not found.\n", vid);
357 int vhost_new_device(void);
358 void cleanup_device(struct virtio_net *dev, int destroy);
359 void reset_device(struct virtio_net *dev);
360 void vhost_destroy_device(int);
362 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
363 void free_vq(struct vhost_virtqueue *vq);
365 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
367 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
368 void vhost_enable_dequeue_zero_copy(int vid);
369 void vhost_set_builtin_virtio_net(int vid, bool enable);
371 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
374 * Backend-specific cleanup.
376 * TODO: fix it; we have one backend now
378 void vhost_backend_cleanup(struct virtio_net *dev);
380 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
381 uint64_t iova, uint64_t size, uint8_t perm);
382 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
383 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
385 static __rte_always_inline uint64_t
386 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
387 uint64_t iova, uint64_t size, uint8_t perm)
389 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
390 return rte_vhost_gpa_to_vva(dev->mem, iova);
392 return __vhost_iova_to_vva(dev, vq, iova, size, perm);
395 #define vhost_used_event(vr) \
396 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
399 * The following is used with VIRTIO_RING_F_EVENT_IDX.
400 * Assuming a given event_idx value from the other size, if we have
401 * just incremented index from old to new_idx, should we trigger an
404 static __rte_always_inline int
405 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
407 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
410 static __rte_always_inline void
411 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
413 /* Flush used->idx update before we read avail->flags. */
416 /* Don't kick guest if we don't reach index specified by guest. */
417 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
418 uint16_t old = vq->signalled_used;
419 uint16_t new = vq->last_used_idx;
421 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
423 vhost_used_event(vq),
425 if (vhost_need_event(vhost_used_event(vq), new, old)
426 && (vq->callfd >= 0)) {
427 vq->signalled_used = vq->last_used_idx;
428 eventfd_write(vq->callfd, (eventfd_t) 1);
431 /* Kick the guest if necessary. */
432 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
433 && (vq->callfd >= 0))
434 eventfd_write(vq->callfd, (eventfd_t)1);
438 #endif /* _VHOST_NET_CDEV_H_ */