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>
40 #include <linux/vhost.h>
44 #include "rte_virtio_net.h"
46 /* Used to indicate that the device is running on a data core */
47 #define VIRTIO_DEV_RUNNING 1
49 /* Backend value set by guest. */
50 #define VIRTIO_DEV_STOPPED -1
52 #define BUF_VECTOR_MAX 256
55 * Structure contains buffer address, length and descriptor index
56 * from vring to do scatter RX.
65 * Structure contains variables relevant to RX/TX virtqueues.
67 struct vhost_virtqueue {
68 struct vring_desc *desc;
69 struct vring_avail *avail;
70 struct vring_used *used;
74 /* Last index used on the available ring */
75 volatile uint16_t last_used_idx;
76 /* Used for multiple devices reserving buffers */
77 volatile uint16_t last_used_idx_res;
78 #define VIRTIO_INVALID_EVENTFD (-1)
79 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
81 /* Backend value to determine if device should started/stopped */
83 /* Used to notify the guest (trigger interrupt) */
85 /* Currently unused as polling mode is enabled */
89 /* Physical address of used ring, for logging */
90 uint64_t log_guest_addr;
91 uint64_t reserved[15];
92 struct buf_vector buf_vec[BUF_VECTOR_MAX];
93 } __rte_cache_aligned;
95 /* Old kernels have no such macro defined */
96 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
97 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
102 * Make an extra wrapper for VIRTIO_NET_F_MQ and
103 * VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX as they are
104 * introduced since kernel v3.8. This makes our
105 * code buildable for older kernel.
107 #ifdef VIRTIO_NET_F_MQ
108 #define VHOST_MAX_QUEUE_PAIRS VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
109 #define VHOST_SUPPORTS_MQ (1ULL << VIRTIO_NET_F_MQ)
111 #define VHOST_MAX_QUEUE_PAIRS 1
112 #define VHOST_SUPPORTS_MQ 0
116 * Define virtio 1.0 for older kernels
118 #ifndef VIRTIO_F_VERSION_1
119 #define VIRTIO_F_VERSION_1 32
123 * Device structure contains all configuration information relating
127 /* Frontend (QEMU) memory and memory region information */
128 struct virtio_memory *mem;
130 uint64_t protocol_features;
133 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
134 char ifname[IF_NAME_SZ];
139 struct ether_addr mac;
141 /* to tell if we need broadcast rarp packet */
142 rte_atomic16_t broadcast_rarp;
143 uint64_t reserved[61];
144 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
145 } __rte_cache_aligned;
148 * Information relating to memory regions including offsets to
149 * addresses in QEMUs memory file.
151 struct virtio_memory_regions {
152 uint64_t guest_phys_address;
153 uint64_t guest_phys_address_end;
154 uint64_t memory_size;
155 uint64_t userspace_address;
156 uint64_t address_offset;
161 * Memory structure includes region and mapping information.
163 struct virtio_memory {
164 /* Base QEMU userspace address of the memory file. */
165 uint64_t base_address;
166 uint64_t mapped_address;
167 uint64_t mapped_size;
169 struct virtio_memory_regions regions[0];
173 /* Macros for printing using RTE_LOG */
174 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
175 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
177 #ifdef RTE_LIBRTE_VHOST_DEBUG
178 #define VHOST_MAX_PRINT_BUFF 6072
179 #define LOG_LEVEL RTE_LOG_DEBUG
180 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
181 #define PRINT_PACKET(device, addr, size, header) do { \
182 char *pkt_addr = (char *)(addr); \
183 unsigned int index; \
184 char packet[VHOST_MAX_PRINT_BUFF]; \
187 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
189 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
190 for (index = 0; index < (size); index++) { \
191 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
192 "%02hhx ", pkt_addr[index]); \
194 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
196 LOG_DEBUG(VHOST_DATA, "%s", packet); \
199 #define LOG_LEVEL RTE_LOG_INFO
200 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
201 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
205 * Function to convert guest physical addresses to vhost virtual addresses.
206 * This is used to convert guest virtio buffer addresses.
208 static inline uint64_t __attribute__((always_inline))
209 gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa)
211 struct virtio_memory_regions *region;
213 uint64_t vhost_va = 0;
215 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
216 region = &dev->mem->regions[regionidx];
217 if ((guest_pa >= region->guest_phys_address) &&
218 (guest_pa <= region->guest_phys_address_end)) {
219 vhost_va = region->address_offset + guest_pa;
226 int vhost_new_device(void);
227 void vhost_destroy_device(int);
229 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
231 int vhost_get_features(int, uint64_t *);
232 int vhost_set_features(int, uint64_t *);
234 int vhost_set_vring_num(int, struct vhost_vring_state *);
235 int vhost_set_vring_addr(int, struct vhost_vring_addr *);
236 int vhost_set_vring_base(int, struct vhost_vring_state *);
237 int vhost_get_vring_base(int, uint32_t, struct vhost_vring_state *);
239 int vhost_set_vring_kick(int, struct vhost_vring_file *);
240 int vhost_set_vring_call(int, struct vhost_vring_file *);
242 int vhost_set_backend(int, struct vhost_vring_file *);
244 int vhost_set_owner(int);
245 int vhost_reset_owner(int);
248 * Backend-specific cleanup. Defined by vhost-cuse and vhost-user.
250 void vhost_backend_cleanup(struct virtio_net *dev);
252 #endif /* _VHOST_NET_CDEV_H_ */