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
27 /* Used to indicate that the built-in vhost net device backend is enabled */
28 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
30 /* Backend value set by guest. */
31 #define VIRTIO_DEV_STOPPED -1
33 #define BUF_VECTOR_MAX 256
36 * Structure contains buffer address, length and descriptor index
37 * from vring to do scatter RX.
46 * A structure to hold some fields needed in zero copy code path,
47 * mainly for associating an mbuf with the right desc_idx.
50 struct rte_mbuf *mbuf;
54 TAILQ_ENTRY(zcopy_mbuf) next;
56 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
59 * Structure contains the info for each batched memory copy.
61 struct batch_copy_elem {
69 * Structure contains variables relevant to RX/TX virtqueues.
71 struct vhost_virtqueue {
72 struct vring_desc *desc;
73 struct vring_avail *avail;
74 struct vring_used *used;
77 uint16_t last_avail_idx;
78 uint16_t last_used_idx;
79 /* Last used index we notify to front end. */
80 uint16_t signalled_used;
81 #define VIRTIO_INVALID_EVENTFD (-1)
82 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
84 /* Backend value to determine if device should started/stopped */
88 rte_spinlock_t access_lock;
90 /* Used to notify the guest (trigger interrupt) */
92 /* Currently unused as polling mode is enabled */
95 /* Physical address of used ring, for logging */
96 uint64_t log_guest_addr;
100 uint16_t last_zmbuf_idx;
101 struct zcopy_mbuf *zmbufs;
102 struct zcopy_mbuf_list zmbuf_list;
104 struct vring_used_elem *shadow_used_ring;
105 uint16_t shadow_used_idx;
106 struct vhost_vring_addr ring_addrs;
108 struct batch_copy_elem *batch_copy_elems;
109 uint16_t batch_copy_nb_elems;
111 rte_rwlock_t iotlb_lock;
112 rte_rwlock_t iotlb_pending_lock;
113 struct rte_mempool *iotlb_pool;
114 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
116 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
117 } __rte_cache_aligned;
119 /* Old kernels have no such macros defined */
120 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
121 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
124 #ifndef VIRTIO_NET_F_MQ
125 #define VIRTIO_NET_F_MQ 22
128 #define VHOST_MAX_VRING 0x100
129 #define VHOST_MAX_QUEUE_PAIRS 0x80
131 #ifndef VIRTIO_NET_F_MTU
132 #define VIRTIO_NET_F_MTU 3
135 #ifndef VIRTIO_F_ANY_LAYOUT
136 #define VIRTIO_F_ANY_LAYOUT 27
139 /* Declare IOMMU related bits for older kernels */
140 #ifndef VIRTIO_F_IOMMU_PLATFORM
142 #define VIRTIO_F_IOMMU_PLATFORM 33
144 struct vhost_iotlb_msg {
148 #define VHOST_ACCESS_RO 0x1
149 #define VHOST_ACCESS_WO 0x2
150 #define VHOST_ACCESS_RW 0x3
152 #define VHOST_IOTLB_MISS 1
153 #define VHOST_IOTLB_UPDATE 2
154 #define VHOST_IOTLB_INVALIDATE 3
155 #define VHOST_IOTLB_ACCESS_FAIL 4
159 #define VHOST_IOTLB_MSG 0x1
164 struct vhost_iotlb_msg iotlb;
171 * Define virtio 1.0 for older kernels
173 #ifndef VIRTIO_F_VERSION_1
174 #define VIRTIO_F_VERSION_1 32
177 #define VHOST_USER_F_PROTOCOL_FEATURES 30
179 /* Features supported by this builtin vhost-user net driver. */
180 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
181 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
182 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
183 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
184 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
185 (1ULL << VIRTIO_NET_F_MQ) | \
186 (1ULL << VIRTIO_F_VERSION_1) | \
187 (1ULL << VHOST_F_LOG_ALL) | \
188 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
189 (1ULL << VIRTIO_NET_F_GSO) | \
190 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
191 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
192 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
193 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
194 (1ULL << VIRTIO_NET_F_CSUM) | \
195 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
196 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
197 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
198 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
199 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
200 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
201 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
202 (1ULL << VIRTIO_NET_F_MTU) | \
203 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
207 uint64_t guest_phys_addr;
208 uint64_t host_phys_addr;
213 * Device structure contains all configuration information relating
217 /* Frontend (QEMU) memory and memory region information */
218 struct rte_vhost_memory *mem;
220 uint64_t protocol_features;
224 /* to tell if we need broadcast rarp packet */
225 rte_atomic16_t broadcast_rarp;
227 int dequeue_zero_copy;
228 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
229 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
230 char ifname[IF_NAME_SZ];
234 struct ether_addr mac;
237 struct vhost_device_ops const *notify_ops;
239 uint32_t nr_guest_pages;
240 uint32_t max_guest_pages;
241 struct guest_page *guest_pages;
244 } __rte_cache_aligned;
247 #define VHOST_LOG_PAGE 4096
250 * Atomically set a bit in memory.
252 static __rte_always_inline void
253 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
255 __sync_fetch_and_or_8(addr, (1U << nr));
258 static __rte_always_inline void
259 vhost_log_page(uint8_t *log_base, uint64_t page)
261 vhost_set_bit(page % 8, &log_base[page / 8]);
264 static __rte_always_inline void
265 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
269 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
270 !dev->log_base || !len))
273 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
276 /* To make sure guest memory updates are committed before logging */
279 page = addr / VHOST_LOG_PAGE;
280 while (page * VHOST_LOG_PAGE < addr + len) {
281 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
286 static __rte_always_inline void
287 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
288 uint64_t offset, uint64_t len)
290 vhost_log_write(dev, vq->log_guest_addr + offset, len);
293 /* Macros for printing using RTE_LOG */
294 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
295 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
297 #ifdef RTE_LIBRTE_VHOST_DEBUG
298 #define VHOST_MAX_PRINT_BUFF 6072
299 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
300 RTE_LOG(DEBUG, log_type, fmt, ##args)
301 #define PRINT_PACKET(device, addr, size, header) do { \
302 char *pkt_addr = (char *)(addr); \
303 unsigned int index; \
304 char packet[VHOST_MAX_PRINT_BUFF]; \
307 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
309 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
310 for (index = 0; index < (size); index++) { \
311 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
312 "%02hhx ", pkt_addr[index]); \
314 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
316 VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
319 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
320 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
323 extern uint64_t VHOST_FEATURES;
324 #define MAX_VHOST_DEVICE 1024
325 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
327 /* Convert guest physical address to host physical address */
328 static __rte_always_inline rte_iova_t
329 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
332 struct guest_page *page;
334 for (i = 0; i < dev->nr_guest_pages; i++) {
335 page = &dev->guest_pages[i];
337 if (gpa >= page->guest_phys_addr &&
338 gpa + size < page->guest_phys_addr + page->size) {
339 return gpa - page->guest_phys_addr +
340 page->host_phys_addr;
347 static __rte_always_inline struct virtio_net *
350 struct virtio_net *dev = vhost_devices[vid];
352 if (unlikely(!dev)) {
353 RTE_LOG(ERR, VHOST_CONFIG,
354 "(%d) device not found.\n", vid);
360 int vhost_new_device(void);
361 void cleanup_device(struct virtio_net *dev, int destroy);
362 void reset_device(struct virtio_net *dev);
363 void vhost_destroy_device(int);
365 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
366 void free_vq(struct vhost_virtqueue *vq);
368 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
370 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
371 void vhost_enable_dequeue_zero_copy(int vid);
372 void vhost_set_builtin_virtio_net(int vid, bool enable);
374 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
377 * Backend-specific cleanup.
379 * TODO: fix it; we have one backend now
381 void vhost_backend_cleanup(struct virtio_net *dev);
383 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
384 uint64_t iova, uint64_t size, uint8_t perm);
385 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
386 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
388 static __rte_always_inline uint64_t
389 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
390 uint64_t iova, uint64_t size, uint8_t perm)
392 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
393 return rte_vhost_gpa_to_vva(dev->mem, iova);
395 return __vhost_iova_to_vva(dev, vq, iova, size, perm);
398 #define vhost_used_event(vr) \
399 (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
402 * The following is used with VIRTIO_RING_F_EVENT_IDX.
403 * Assuming a given event_idx value from the other size, if we have
404 * just incremented index from old to new_idx, should we trigger an
407 static __rte_always_inline int
408 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
410 return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
413 static __rte_always_inline void
414 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
416 /* Flush used->idx update before we read avail->flags. */
419 /* Don't kick guest if we don't reach index specified by guest. */
420 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
421 uint16_t old = vq->signalled_used;
422 uint16_t new = vq->last_used_idx;
424 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
426 vhost_used_event(vq),
428 if (vhost_need_event(vhost_used_event(vq), new, old)
429 && (vq->callfd >= 0)) {
430 vq->signalled_used = vq->last_used_idx;
431 eventfd_write(vq->callfd, (eventfd_t) 1);
434 /* Kick the guest if necessary. */
435 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
436 && (vq->callfd >= 0))
437 eventfd_write(vq->callfd, (eventfd_t)1);
441 #endif /* _VHOST_NET_CDEV_H_ */