4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef _VHOST_NET_CDEV_H_
35 #define _VHOST_NET_CDEV_H_
38 #include <sys/types.h>
39 #include <sys/queue.h>
41 #include <linux/vhost.h>
42 #include <linux/virtio_net.h>
43 #include <sys/socket.h>
47 #include <rte_ether.h>
48 #include <rte_rwlock.h>
50 #include "rte_vhost.h"
52 /* Used to indicate that the device is running on a data core */
53 #define VIRTIO_DEV_RUNNING 1
54 /* Used to indicate that the device is ready to operate */
55 #define VIRTIO_DEV_READY 2
57 /* Backend value set by guest. */
58 #define VIRTIO_DEV_STOPPED -1
60 #define BUF_VECTOR_MAX 256
63 * Structure contains buffer address, length and descriptor index
64 * from vring to do scatter RX.
73 * A structure to hold some fields needed in zero copy code path,
74 * mainly for associating an mbuf with the right desc_idx.
77 struct rte_mbuf *mbuf;
81 TAILQ_ENTRY(zcopy_mbuf) next;
83 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
86 * Structure contains the info for each batched memory copy.
88 struct batch_copy_elem {
96 * Structure contains variables relevant to RX/TX virtqueues.
98 struct vhost_virtqueue {
99 struct vring_desc *desc;
100 struct vring_avail *avail;
101 struct vring_used *used;
104 uint16_t last_avail_idx;
105 uint16_t last_used_idx;
106 #define VIRTIO_INVALID_EVENTFD (-1)
107 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
109 /* Backend value to determine if device should started/stopped */
113 rte_spinlock_t access_lock;
115 /* Used to notify the guest (trigger interrupt) */
117 /* Currently unused as polling mode is enabled */
120 /* Physical address of used ring, for logging */
121 uint64_t log_guest_addr;
125 uint16_t last_zmbuf_idx;
126 struct zcopy_mbuf *zmbufs;
127 struct zcopy_mbuf_list zmbuf_list;
129 struct vring_used_elem *shadow_used_ring;
130 uint16_t shadow_used_idx;
131 struct vhost_vring_addr ring_addrs;
133 struct batch_copy_elem *batch_copy_elems;
134 uint16_t batch_copy_nb_elems;
136 rte_rwlock_t iotlb_lock;
137 rte_rwlock_t iotlb_pending_lock;
138 struct rte_mempool *iotlb_pool;
139 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
141 TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
142 } __rte_cache_aligned;
144 /* Old kernels have no such macros defined */
145 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
146 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
149 #ifndef VIRTIO_NET_F_MQ
150 #define VIRTIO_NET_F_MQ 22
153 #define VHOST_MAX_VRING 0x100
154 #define VHOST_MAX_QUEUE_PAIRS 0x80
156 #ifndef VIRTIO_NET_F_MTU
157 #define VIRTIO_NET_F_MTU 3
160 /* Declare IOMMU related bits for older kernels */
161 #ifndef VIRTIO_F_IOMMU_PLATFORM
163 #define VIRTIO_F_IOMMU_PLATFORM 33
165 struct vhost_iotlb_msg {
169 #define VHOST_ACCESS_RO 0x1
170 #define VHOST_ACCESS_WO 0x2
171 #define VHOST_ACCESS_RW 0x3
173 #define VHOST_IOTLB_MISS 1
174 #define VHOST_IOTLB_UPDATE 2
175 #define VHOST_IOTLB_INVALIDATE 3
176 #define VHOST_IOTLB_ACCESS_FAIL 4
180 #define VHOST_IOTLB_MSG 0x1
185 struct vhost_iotlb_msg iotlb;
192 * Define virtio 1.0 for older kernels
194 #ifndef VIRTIO_F_VERSION_1
195 #define VIRTIO_F_VERSION_1 32
198 #define VHOST_USER_F_PROTOCOL_FEATURES 30
200 /* Features supported by this builtin vhost-user net driver. */
201 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
202 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
203 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
204 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
205 (1ULL << VIRTIO_NET_F_MQ) | \
206 (1ULL << VIRTIO_F_VERSION_1) | \
207 (1ULL << VHOST_F_LOG_ALL) | \
208 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
209 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
210 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
211 (1ULL << VIRTIO_NET_F_CSUM) | \
212 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
213 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
214 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
215 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
216 (1ULL << VIRTIO_NET_F_MTU) | \
217 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
221 uint64_t guest_phys_addr;
222 uint64_t host_phys_addr;
227 * Device structure contains all configuration information relating
231 /* Frontend (QEMU) memory and memory region information */
232 struct rte_vhost_memory *mem;
234 uint64_t protocol_features;
238 /* to tell if we need broadcast rarp packet */
239 rte_atomic16_t broadcast_rarp;
241 int dequeue_zero_copy;
242 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
243 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
244 char ifname[IF_NAME_SZ];
248 struct ether_addr mac;
251 struct vhost_device_ops const *notify_ops;
253 uint32_t nr_guest_pages;
254 uint32_t max_guest_pages;
255 struct guest_page *guest_pages;
258 } __rte_cache_aligned;
261 #define VHOST_LOG_PAGE 4096
264 * Atomically set a bit in memory.
266 static __rte_always_inline void
267 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
269 __sync_fetch_and_or_8(addr, (1U << nr));
272 static __rte_always_inline void
273 vhost_log_page(uint8_t *log_base, uint64_t page)
275 vhost_set_bit(page % 8, &log_base[page / 8]);
278 static __rte_always_inline void
279 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
283 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
284 !dev->log_base || !len))
287 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
290 /* To make sure guest memory updates are committed before logging */
293 page = addr / VHOST_LOG_PAGE;
294 while (page * VHOST_LOG_PAGE < addr + len) {
295 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
300 static __rte_always_inline void
301 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
302 uint64_t offset, uint64_t len)
304 vhost_log_write(dev, vq->log_guest_addr + offset, len);
307 /* Macros for printing using RTE_LOG */
308 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
309 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
311 #ifdef RTE_LIBRTE_VHOST_DEBUG
312 #define VHOST_MAX_PRINT_BUFF 6072
313 #define LOG_LEVEL RTE_LOG_DEBUG
314 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
315 #define PRINT_PACKET(device, addr, size, header) do { \
316 char *pkt_addr = (char *)(addr); \
317 unsigned int index; \
318 char packet[VHOST_MAX_PRINT_BUFF]; \
321 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
323 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
324 for (index = 0; index < (size); index++) { \
325 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
326 "%02hhx ", pkt_addr[index]); \
328 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
330 LOG_DEBUG(VHOST_DATA, "%s", packet); \
333 #define LOG_LEVEL RTE_LOG_INFO
334 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
335 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
338 extern uint64_t VHOST_FEATURES;
339 #define MAX_VHOST_DEVICE 1024
340 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
342 /* Convert guest physical address to host physical address */
343 static __rte_always_inline rte_iova_t
344 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
347 struct guest_page *page;
349 for (i = 0; i < dev->nr_guest_pages; i++) {
350 page = &dev->guest_pages[i];
352 if (gpa >= page->guest_phys_addr &&
353 gpa + size < page->guest_phys_addr + page->size) {
354 return gpa - page->guest_phys_addr +
355 page->host_phys_addr;
362 struct virtio_net *get_device(int vid);
364 int vhost_new_device(void);
365 void cleanup_device(struct virtio_net *dev, int destroy);
366 void reset_device(struct virtio_net *dev);
367 void vhost_destroy_device(int);
369 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
371 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
372 void vhost_enable_dequeue_zero_copy(int vid);
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 *len, 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 *len, uint8_t perm)
392 if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
393 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
395 return __vhost_iova_to_vva(dev, vq, iova, len, perm);
398 #endif /* _VHOST_NET_CDEV_H_ */