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 /* Features supported by this builtin vhost-user net driver. */
179 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
180 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
181 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
182 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
183 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
184 (1ULL << VIRTIO_NET_F_MQ) | \
185 (1ULL << VIRTIO_F_VERSION_1) | \
186 (1ULL << VHOST_F_LOG_ALL) | \
187 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
188 (1ULL << VIRTIO_NET_F_GSO) | \
189 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
190 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
191 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
192 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
193 (1ULL << VIRTIO_NET_F_CSUM) | \
194 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
195 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
196 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
197 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
198 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
199 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
200 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
201 (1ULL << VIRTIO_NET_F_MTU) | \
202 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
206 uint64_t guest_phys_addr;
207 uint64_t host_phys_addr;
212 * Device structure contains all configuration information relating
216 /* Frontend (QEMU) memory and memory region information */
217 struct rte_vhost_memory *mem;
219 uint64_t protocol_features;
223 /* to tell if we need broadcast rarp packet */
224 rte_atomic16_t broadcast_rarp;
226 int dequeue_zero_copy;
227 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
228 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
229 char ifname[IF_NAME_SZ];
233 struct ether_addr mac;
236 struct vhost_device_ops const *notify_ops;
238 uint32_t nr_guest_pages;
239 uint32_t max_guest_pages;
240 struct guest_page *guest_pages;
243 } __rte_cache_aligned;
246 #define VHOST_LOG_PAGE 4096
249 * Mark all pages belonging to the same dirty log bitmap byte
250 * as dirty. The goal is to avoid concurrency between different
251 * threads doing atomic read-modify-writes on the same byte.
253 static __rte_always_inline void
254 vhost_log_page(uint8_t *log_base, uint64_t page)
256 log_base[page / 8] = 0xff;
259 static __rte_always_inline void
260 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
264 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
265 !dev->log_base || !len))
268 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
271 /* To make sure guest memory updates are committed before logging */
274 page = addr / VHOST_LOG_PAGE;
275 while (page * VHOST_LOG_PAGE < addr + len) {
276 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
281 static __rte_always_inline void
282 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
283 uint64_t offset, uint64_t len)
285 vhost_log_write(dev, vq->log_guest_addr + offset, len);
288 /* Macros for printing using RTE_LOG */
289 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
290 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
292 #ifdef RTE_LIBRTE_VHOST_DEBUG
293 #define VHOST_MAX_PRINT_BUFF 6072
294 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
295 RTE_LOG(DEBUG, log_type, fmt, ##args)
296 #define PRINT_PACKET(device, addr, size, header) do { \
297 char *pkt_addr = (char *)(addr); \
298 unsigned int index; \
299 char packet[VHOST_MAX_PRINT_BUFF]; \
302 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
304 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
305 for (index = 0; index < (size); index++) { \
306 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
307 "%02hhx ", pkt_addr[index]); \
309 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
311 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
314 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
315 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
318 extern uint64_t VHOST_FEATURES;
319 #define MAX_VHOST_DEVICE 1024
320 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
322 /* Convert guest physical address to host physical address */
323 static __rte_always_inline rte_iova_t
324 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
327 struct guest_page *page;
329 for (i = 0; i < dev->nr_guest_pages; i++) {
330 page = &dev->guest_pages[i];
332 if (gpa >= page->guest_phys_addr &&
333 gpa + size < page->guest_phys_addr + page->size) {
334 return gpa - page->guest_phys_addr +
335 page->host_phys_addr;
342 static __rte_always_inline struct virtio_net *
345 struct virtio_net *dev = vhost_devices[vid];
347 if (unlikely(!dev)) {
348 RTE_LOG(ERR, VHOST_CONFIG,
349 "(%d) device not found.\n", vid);
355 int vhost_new_device(void);
356 void cleanup_device(struct virtio_net *dev, int destroy);
357 void reset_device(struct virtio_net *dev);
358 void vhost_destroy_device(int);
360 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
361 void free_vq(struct vhost_virtqueue *vq);
363 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
365 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
366 void vhost_enable_dequeue_zero_copy(int vid);
367 void vhost_set_builtin_virtio_net(int vid, bool enable);
369 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
372 * Backend-specific cleanup.
374 * TODO: fix it; we have one backend now
376 void vhost_backend_cleanup(struct virtio_net *dev);
378 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
379 uint64_t iova, uint64_t size, uint8_t perm);
380 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
381 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
383 static __rte_always_inline uint64_t
384 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
385 uint64_t iova, uint64_t size, uint8_t perm)
387 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
388 return rte_vhost_gpa_to_vva(dev->mem, iova);
390 return __vhost_iova_to_vva(dev, vq, iova, size, perm);
393 #define vhost_used_event(vr) \
394 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
397 * The following is used with VIRTIO_RING_F_EVENT_IDX.
398 * Assuming a given event_idx value from the other size, if we have
399 * just incremented index from old to new_idx, should we trigger an
402 static __rte_always_inline int
403 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
405 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
408 static __rte_always_inline void
409 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
411 /* Flush used->idx update before we read avail->flags. */
414 /* Don't kick guest if we don't reach index specified by guest. */
415 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
416 uint16_t old = vq->signalled_used;
417 uint16_t new = vq->last_used_idx;
419 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
421 vhost_used_event(vq),
423 if (vhost_need_event(vhost_used_event(vq), new, old)
424 && (vq->callfd >= 0)) {
425 vq->signalled_used = vq->last_used_idx;
426 eventfd_write(vq->callfd, (eventfd_t) 1);
429 /* Kick the guest if necessary. */
430 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
431 && (vq->callfd >= 0))
432 eventfd_write(vq->callfd, (eventfd_t)1);
436 #endif /* _VHOST_NET_CDEV_H_ */