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>
45 #include "rte_virtio_net.h"
47 /* Used to indicate that the device is running on a data core */
48 #define VIRTIO_DEV_RUNNING 1
49 /* Used to indicate that the device is ready to operate */
50 #define VIRTIO_DEV_READY 2
52 /* Backend value set by guest. */
53 #define VIRTIO_DEV_STOPPED -1
55 #define BUF_VECTOR_MAX 256
58 * Structure contains buffer address, length and descriptor index
59 * from vring to do scatter RX.
68 * A structure to hold some fields needed in zero copy code path,
69 * mainly for associating an mbuf with the right desc_idx.
72 struct rte_mbuf *mbuf;
76 TAILQ_ENTRY(zcopy_mbuf) next;
78 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
81 * Structure contains variables relevant to RX/TX virtqueues.
83 struct vhost_virtqueue {
84 struct vring_desc *desc;
85 struct vring_avail *avail;
86 struct vring_used *used;
89 uint16_t last_avail_idx;
90 uint16_t last_used_idx;
91 #define VIRTIO_INVALID_EVENTFD (-1)
92 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
94 /* Backend value to determine if device should started/stopped */
96 /* Used to notify the guest (trigger interrupt) */
98 /* Currently unused as polling mode is enabled */
102 /* Physical address of used ring, for logging */
103 uint64_t log_guest_addr;
107 uint16_t last_zmbuf_idx;
108 struct zcopy_mbuf *zmbufs;
109 struct zcopy_mbuf_list zmbuf_list;
111 struct vring_used_elem *shadow_used_ring;
112 uint16_t shadow_used_idx;
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
123 #define VHOST_MAX_QUEUE_PAIRS 0x80
125 #ifndef VIRTIO_NET_F_MTU
126 #define VIRTIO_NET_F_MTU 3
130 * Define virtio 1.0 for older kernels
132 #ifndef VIRTIO_F_VERSION_1
133 #define VIRTIO_F_VERSION_1 32
136 #define VHOST_USER_F_PROTOCOL_FEATURES 30
138 /* Features supported by this builtin vhost-user net driver. */
139 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
140 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
141 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
142 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
143 (1ULL << VIRTIO_NET_F_MQ) | \
144 (1ULL << VIRTIO_F_VERSION_1) | \
145 (1ULL << VHOST_F_LOG_ALL) | \
146 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
147 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
148 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
149 (1ULL << VIRTIO_NET_F_CSUM) | \
150 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
151 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
152 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
153 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
154 (1ULL << VIRTIO_NET_F_MTU))
158 uint64_t guest_phys_addr;
159 uint64_t host_phys_addr;
164 * Device structure contains all configuration information relating
168 /* Frontend (QEMU) memory and memory region information */
169 struct virtio_memory *mem;
171 uint64_t protocol_features;
175 /* to tell if we need broadcast rarp packet */
176 rte_atomic16_t broadcast_rarp;
178 int dequeue_zero_copy;
179 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
180 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
181 char ifname[IF_NAME_SZ];
185 struct ether_addr mac;
188 struct virtio_net_device_ops const *notify_ops;
190 uint32_t nr_guest_pages;
191 uint32_t max_guest_pages;
192 struct guest_page *guest_pages;
193 } __rte_cache_aligned;
196 * Information relating to memory regions including offsets to
197 * addresses in QEMUs memory file.
199 struct virtio_memory_region {
200 uint64_t guest_phys_addr;
201 uint64_t guest_user_addr;
202 uint64_t host_user_addr;
211 * Memory structure includes region and mapping information.
213 struct virtio_memory {
215 struct virtio_memory_region regions[0];
219 /* Macros for printing using RTE_LOG */
220 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
221 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
223 #ifdef RTE_LIBRTE_VHOST_DEBUG
224 #define VHOST_MAX_PRINT_BUFF 6072
225 #define LOG_LEVEL RTE_LOG_DEBUG
226 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
227 #define PRINT_PACKET(device, addr, size, header) do { \
228 char *pkt_addr = (char *)(addr); \
229 unsigned int index; \
230 char packet[VHOST_MAX_PRINT_BUFF]; \
233 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
235 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
236 for (index = 0; index < (size); index++) { \
237 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
238 "%02hhx ", pkt_addr[index]); \
240 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
242 LOG_DEBUG(VHOST_DATA, "%s", packet); \
245 #define LOG_LEVEL RTE_LOG_INFO
246 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
247 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
250 extern uint64_t VHOST_FEATURES;
251 #define MAX_VHOST_DEVICE 1024
252 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
254 /* Convert guest physical Address to host virtual address */
255 static inline uint64_t __attribute__((always_inline))
256 gpa_to_vva(struct virtio_net *dev, uint64_t gpa)
258 struct virtio_memory_region *reg;
261 for (i = 0; i < dev->mem->nregions; i++) {
262 reg = &dev->mem->regions[i];
263 if (gpa >= reg->guest_phys_addr &&
264 gpa < reg->guest_phys_addr + reg->size) {
265 return gpa - reg->guest_phys_addr +
273 /* Convert guest physical address to host physical address */
274 static inline phys_addr_t __attribute__((always_inline))
275 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
278 struct guest_page *page;
280 for (i = 0; i < dev->nr_guest_pages; i++) {
281 page = &dev->guest_pages[i];
283 if (gpa >= page->guest_phys_addr &&
284 gpa + size < page->guest_phys_addr + page->size) {
285 return gpa - page->guest_phys_addr +
286 page->host_phys_addr;
293 struct virtio_net *get_device(int vid);
295 int vhost_new_device(void);
296 void cleanup_device(struct virtio_net *dev, int destroy);
297 void reset_device(struct virtio_net *dev);
298 void vhost_destroy_device(int);
300 int alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx);
302 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
303 void vhost_enable_dequeue_zero_copy(int vid);
305 struct virtio_net_device_ops const *vhost_driver_callback_get(const char *path);
308 * Backend-specific cleanup.
310 * TODO: fix it; we have one backend now
312 void vhost_backend_cleanup(struct virtio_net *dev);
314 #endif /* _VHOST_NET_CDEV_H_ */