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>
49 #include "rte_virtio_net.h"
51 /* Used to indicate that the device is running on a data core */
52 #define VIRTIO_DEV_RUNNING 1
53 /* Used to indicate that the device is ready to operate */
54 #define VIRTIO_DEV_READY 2
56 /* Backend value set by guest. */
57 #define VIRTIO_DEV_STOPPED -1
59 #define BUF_VECTOR_MAX 256
62 * Structure contains buffer address, length and descriptor index
63 * from vring to do scatter RX.
72 * A structure to hold some fields needed in zero copy code path,
73 * mainly for associating an mbuf with the right desc_idx.
76 struct rte_mbuf *mbuf;
80 TAILQ_ENTRY(zcopy_mbuf) next;
82 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
85 * Structure contains variables relevant to RX/TX virtqueues.
87 struct vhost_virtqueue {
88 struct vring_desc *desc;
89 struct vring_avail *avail;
90 struct vring_used *used;
93 uint16_t last_avail_idx;
94 uint16_t last_used_idx;
95 #define VIRTIO_INVALID_EVENTFD (-1)
96 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
98 /* Backend value to determine if device should started/stopped */
100 /* Used to notify the guest (trigger interrupt) */
102 /* Currently unused as polling mode is enabled */
106 /* Physical address of used ring, for logging */
107 uint64_t log_guest_addr;
111 uint16_t last_zmbuf_idx;
112 struct zcopy_mbuf *zmbufs;
113 struct zcopy_mbuf_list zmbuf_list;
115 struct vring_used_elem *shadow_used_ring;
116 uint16_t shadow_used_idx;
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
136 * Define virtio 1.0 for older kernels
138 #ifndef VIRTIO_F_VERSION_1
139 #define VIRTIO_F_VERSION_1 32
142 #define VHOST_USER_F_PROTOCOL_FEATURES 30
144 /* Features supported by this builtin vhost-user net driver. */
145 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
146 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
147 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
148 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
149 (1ULL << VIRTIO_NET_F_MQ) | \
150 (1ULL << VIRTIO_F_VERSION_1) | \
151 (1ULL << VHOST_F_LOG_ALL) | \
152 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
153 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
154 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
155 (1ULL << VIRTIO_NET_F_CSUM) | \
156 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
157 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
158 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
159 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
160 (1ULL << VIRTIO_NET_F_MTU))
164 uint64_t guest_phys_addr;
165 uint64_t host_phys_addr;
170 * Device structure contains all configuration information relating
174 /* Frontend (QEMU) memory and memory region information */
175 struct rte_vhost_memory *mem;
177 uint64_t protocol_features;
181 /* to tell if we need broadcast rarp packet */
182 rte_atomic16_t broadcast_rarp;
184 int dequeue_zero_copy;
185 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
186 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
187 char ifname[IF_NAME_SZ];
191 struct ether_addr mac;
194 struct virtio_net_device_ops const *notify_ops;
196 uint32_t nr_guest_pages;
197 uint32_t max_guest_pages;
198 struct guest_page *guest_pages;
199 } __rte_cache_aligned;
201 /* Macros for printing using RTE_LOG */
202 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
203 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
205 #ifdef RTE_LIBRTE_VHOST_DEBUG
206 #define VHOST_MAX_PRINT_BUFF 6072
207 #define LOG_LEVEL RTE_LOG_DEBUG
208 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
209 #define PRINT_PACKET(device, addr, size, header) do { \
210 char *pkt_addr = (char *)(addr); \
211 unsigned int index; \
212 char packet[VHOST_MAX_PRINT_BUFF]; \
215 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
217 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
218 for (index = 0; index < (size); index++) { \
219 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
220 "%02hhx ", pkt_addr[index]); \
222 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
224 LOG_DEBUG(VHOST_DATA, "%s", packet); \
227 #define LOG_LEVEL RTE_LOG_INFO
228 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
229 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
232 extern uint64_t VHOST_FEATURES;
233 #define MAX_VHOST_DEVICE 1024
234 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
236 /* Convert guest physical address to host physical address */
237 static inline phys_addr_t __attribute__((always_inline))
238 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
241 struct guest_page *page;
243 for (i = 0; i < dev->nr_guest_pages; i++) {
244 page = &dev->guest_pages[i];
246 if (gpa >= page->guest_phys_addr &&
247 gpa + size < page->guest_phys_addr + page->size) {
248 return gpa - page->guest_phys_addr +
249 page->host_phys_addr;
256 struct virtio_net *get_device(int vid);
258 int vhost_new_device(void);
259 void cleanup_device(struct virtio_net *dev, int destroy);
260 void reset_device(struct virtio_net *dev);
261 void vhost_destroy_device(int);
263 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
265 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
266 void vhost_enable_dequeue_zero_copy(int vid);
268 struct virtio_net_device_ops const *vhost_driver_callback_get(const char *path);
271 * Backend-specific cleanup.
273 * TODO: fix it; we have one backend now
275 void vhost_backend_cleanup(struct virtio_net *dev);
277 #endif /* _VHOST_NET_CDEV_H_ */