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"
46 #include "vhost_user.h"
48 /* Used to indicate that the device is running on a data core */
49 #define VIRTIO_DEV_RUNNING 1
51 /* Backend value set by guest. */
52 #define VIRTIO_DEV_STOPPED -1
54 #define BUF_VECTOR_MAX 256
57 * Structure contains buffer address, length and descriptor index
58 * from vring to do scatter RX.
67 * A structure to hold some fields needed in zero copy code path,
68 * mainly for associating an mbuf with the right desc_idx.
71 struct rte_mbuf
*mbuf
;
75 TAILQ_ENTRY(zcopy_mbuf
) next
;
77 TAILQ_HEAD(zcopy_mbuf_list
, zcopy_mbuf
);
80 * Structure contains variables relevant to RX/TX virtqueues.
82 struct vhost_virtqueue
{
83 struct vring_desc
*desc
;
84 struct vring_avail
*avail
;
85 struct vring_used
*used
;
88 uint16_t last_avail_idx
;
89 uint16_t last_used_idx
;
90 #define VIRTIO_INVALID_EVENTFD (-1)
91 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
93 /* Backend value to determine if device should started/stopped */
95 /* Used to notify the guest (trigger interrupt) */
97 /* Currently unused as polling mode is enabled */
101 /* Physical address of used ring, for logging */
102 uint64_t log_guest_addr
;
106 uint16_t last_zmbuf_idx
;
107 struct zcopy_mbuf
*zmbufs
;
108 struct zcopy_mbuf_list zmbuf_list
;
110 struct vring_used_elem
*shadow_used_ring
;
111 uint16_t shadow_used_idx
;
112 } __rte_cache_aligned
;
114 /* Old kernels have no such macro defined */
115 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
116 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
121 * Make an extra wrapper for VIRTIO_NET_F_MQ and
122 * VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX as they are
123 * introduced since kernel v3.8. This makes our
124 * code buildable for older kernel.
126 #ifdef VIRTIO_NET_F_MQ
127 #define VHOST_MAX_QUEUE_PAIRS VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
128 #define VHOST_SUPPORTS_MQ (1ULL << VIRTIO_NET_F_MQ)
130 #define VHOST_MAX_QUEUE_PAIRS 1
131 #define VHOST_SUPPORTS_MQ 0
135 * Define virtio 1.0 for older kernels
137 #ifndef VIRTIO_F_VERSION_1
138 #define VIRTIO_F_VERSION_1 32
142 uint64_t guest_phys_addr
;
143 uint64_t host_phys_addr
;
148 * Device structure contains all configuration information relating
152 /* Frontend (QEMU) memory and memory region information */
153 struct virtio_memory
*mem
;
155 uint64_t protocol_features
;
159 /* to tell if we need broadcast rarp packet */
160 rte_atomic16_t broadcast_rarp
;
163 int dequeue_zero_copy
;
164 struct vhost_virtqueue
*virtqueue
[VHOST_MAX_QUEUE_PAIRS
* 2];
165 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
166 char ifname
[IF_NAME_SZ
];
170 struct ether_addr mac
;
172 uint32_t nr_guest_pages
;
173 uint32_t max_guest_pages
;
174 struct guest_page
*guest_pages
;
175 int has_new_mem_table
;
176 struct VhostUserMemory mem_table
;
177 int mem_table_fds
[VHOST_MEMORY_MAX_NREGIONS
];
178 } __rte_cache_aligned
;
181 * Information relating to memory regions including offsets to
182 * addresses in QEMUs memory file.
184 struct virtio_memory_region
{
185 uint64_t guest_phys_addr
;
186 uint64_t guest_user_addr
;
187 uint64_t host_user_addr
;
196 * Memory structure includes region and mapping information.
198 struct virtio_memory
{
200 struct virtio_memory_region regions
[0];
204 /* Macros for printing using RTE_LOG */
205 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
206 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
208 #ifdef RTE_LIBRTE_VHOST_DEBUG
209 #define VHOST_MAX_PRINT_BUFF 6072
210 #define LOG_LEVEL RTE_LOG_DEBUG
211 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
212 #define PRINT_PACKET(device, addr, size, header) do { \
213 char *pkt_addr = (char *)(addr); \
214 unsigned int index; \
215 char packet[VHOST_MAX_PRINT_BUFF]; \
218 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
220 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
221 for (index = 0; index < (size); index++) { \
222 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
223 "%02hhx ", pkt_addr[index]); \
225 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
227 LOG_DEBUG(VHOST_DATA, "%s", packet); \
230 #define LOG_LEVEL RTE_LOG_INFO
231 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
232 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
235 extern uint64_t VHOST_FEATURES
;
236 #define MAX_VHOST_DEVICE 1024
237 extern struct virtio_net
*vhost_devices
[MAX_VHOST_DEVICE
];
239 /* Convert guest physical Address to host virtual address */
240 static inline uint64_t __attribute__((always_inline
))
241 gpa_to_vva(struct virtio_net
*dev
, uint64_t gpa
)
243 struct virtio_memory_region
*reg
;
246 for (i
= 0; i
< dev
->mem
->nregions
; i
++) {
247 reg
= &dev
->mem
->regions
[i
];
248 if (gpa
>= reg
->guest_phys_addr
&&
249 gpa
< reg
->guest_phys_addr
+ reg
->size
) {
250 return gpa
- reg
->guest_phys_addr
+
258 /* Convert guest physical address to host physical address */
259 static inline phys_addr_t
__attribute__((always_inline
))
260 gpa_to_hpa(struct virtio_net
*dev
, uint64_t gpa
, uint64_t size
)
263 struct guest_page
*page
;
265 for (i
= 0; i
< dev
->nr_guest_pages
; i
++) {
266 page
= &dev
->guest_pages
[i
];
268 if (gpa
>= page
->guest_phys_addr
&&
269 gpa
+ size
< page
->guest_phys_addr
+ page
->size
) {
270 return gpa
- page
->guest_phys_addr
+
271 page
->host_phys_addr
;
278 extern struct virtio_net_device_ops
const *notify_ops
;
279 struct virtio_net
*get_device(int vid
);
281 int vhost_new_device(void);
282 void cleanup_device(struct virtio_net
*dev
, int destroy
);
283 void reset_device(struct virtio_net
*dev
);
284 void vhost_destroy_device(int);
286 int alloc_vring_queue_pair(struct virtio_net
*dev
, uint32_t qp_idx
);
288 void vhost_set_ifname(int, const char *if_name
, unsigned int if_len
);
289 void vhost_enable_dequeue_zero_copy(int vid
);
292 * Backend-specific cleanup.
294 * TODO: fix it; we have one backend now
296 void vhost_backend_cleanup(struct virtio_net
*dev
);
298 #endif /* _VHOST_NET_CDEV_H_ */