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_vhost.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 the info for each batched memory copy.
87 struct batch_copy_elem {
95 * Structure contains variables relevant to RX/TX virtqueues.
97 struct vhost_virtqueue {
98 struct vring_desc *desc;
99 struct vring_avail *avail;
100 struct vring_used *used;
103 uint16_t last_avail_idx;
104 uint16_t last_used_idx;
105 #define VIRTIO_INVALID_EVENTFD (-1)
106 #define VIRTIO_UNINITIALIZED_EVENTFD (-2)
108 /* Backend value to determine if device should started/stopped */
110 /* Used to notify the guest (trigger interrupt) */
112 /* Currently unused as polling mode is enabled */
116 /* Physical address of used ring, for logging */
117 uint64_t log_guest_addr;
121 uint16_t last_zmbuf_idx;
122 struct zcopy_mbuf *zmbufs;
123 struct zcopy_mbuf_list zmbuf_list;
125 struct vring_used_elem *shadow_used_ring;
126 uint16_t shadow_used_idx;
128 struct batch_copy_elem *batch_copy_elems;
129 uint16_t batch_copy_nb_elems;
130 } __rte_cache_aligned;
132 /* Old kernels have no such macros defined */
133 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
134 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
137 #ifndef VIRTIO_NET_F_MQ
138 #define VIRTIO_NET_F_MQ 22
141 #define VHOST_MAX_VRING 0x100
142 #define VHOST_MAX_QUEUE_PAIRS 0x80
144 #ifndef VIRTIO_NET_F_MTU
145 #define VIRTIO_NET_F_MTU 3
149 * Define virtio 1.0 for older kernels
151 #ifndef VIRTIO_F_VERSION_1
152 #define VIRTIO_F_VERSION_1 32
155 #define VHOST_USER_F_PROTOCOL_FEATURES 30
157 /* Features supported by this builtin vhost-user net driver. */
158 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
159 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
160 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
161 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
162 (1ULL << VIRTIO_NET_F_MQ) | \
163 (1ULL << VIRTIO_F_VERSION_1) | \
164 (1ULL << VHOST_F_LOG_ALL) | \
165 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
166 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
167 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
168 (1ULL << VIRTIO_NET_F_CSUM) | \
169 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
170 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
171 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
172 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
173 (1ULL << VIRTIO_NET_F_MTU))
177 uint64_t guest_phys_addr;
178 uint64_t host_phys_addr;
183 * Device structure contains all configuration information relating
187 /* Frontend (QEMU) memory and memory region information */
188 struct rte_vhost_memory *mem;
190 uint64_t protocol_features;
194 /* to tell if we need broadcast rarp packet */
195 rte_atomic16_t broadcast_rarp;
197 int dequeue_zero_copy;
198 struct vhost_virtqueue *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
199 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
200 char ifname[IF_NAME_SZ];
204 struct ether_addr mac;
207 struct vhost_device_ops const *notify_ops;
209 uint32_t nr_guest_pages;
210 uint32_t max_guest_pages;
211 struct guest_page *guest_pages;
212 } __rte_cache_aligned;
215 #define VHOST_LOG_PAGE 4096
218 * Atomically set a bit in memory.
220 static __rte_always_inline void
221 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
223 __sync_fetch_and_or_8(addr, (1U << nr));
226 static __rte_always_inline void
227 vhost_log_page(uint8_t *log_base, uint64_t page)
229 vhost_set_bit(page % 8, &log_base[page / 8]);
232 static __rte_always_inline void
233 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
237 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
238 !dev->log_base || !len))
241 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
244 /* To make sure guest memory updates are committed before logging */
247 page = addr / VHOST_LOG_PAGE;
248 while (page * VHOST_LOG_PAGE < addr + len) {
249 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
254 static __rte_always_inline void
255 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
256 uint64_t offset, uint64_t len)
258 vhost_log_write(dev, vq->log_guest_addr + offset, len);
261 /* Macros for printing using RTE_LOG */
262 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
263 #define RTE_LOGTYPE_VHOST_DATA RTE_LOGTYPE_USER1
265 #ifdef RTE_LIBRTE_VHOST_DEBUG
266 #define VHOST_MAX_PRINT_BUFF 6072
267 #define LOG_LEVEL RTE_LOG_DEBUG
268 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
269 #define PRINT_PACKET(device, addr, size, header) do { \
270 char *pkt_addr = (char *)(addr); \
271 unsigned int index; \
272 char packet[VHOST_MAX_PRINT_BUFF]; \
275 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
277 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
278 for (index = 0; index < (size); index++) { \
279 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
280 "%02hhx ", pkt_addr[index]); \
282 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
284 LOG_DEBUG(VHOST_DATA, "%s", packet); \
287 #define LOG_LEVEL RTE_LOG_INFO
288 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
289 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
292 extern uint64_t VHOST_FEATURES;
293 #define MAX_VHOST_DEVICE 1024
294 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
296 /* Convert guest physical address to host physical address */
297 static __rte_always_inline phys_addr_t
298 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
301 struct guest_page *page;
303 for (i = 0; i < dev->nr_guest_pages; i++) {
304 page = &dev->guest_pages[i];
306 if (gpa >= page->guest_phys_addr &&
307 gpa + size < page->guest_phys_addr + page->size) {
308 return gpa - page->guest_phys_addr +
309 page->host_phys_addr;
316 struct virtio_net *get_device(int vid);
318 int vhost_new_device(void);
319 void cleanup_device(struct virtio_net *dev, int destroy);
320 void reset_device(struct virtio_net *dev);
321 void vhost_destroy_device(int);
323 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
325 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
326 void vhost_enable_dequeue_zero_copy(int vid);
328 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
331 * Backend-specific cleanup.
333 * TODO: fix it; we have one backend now
335 void vhost_backend_cleanup(struct virtio_net *dev);
337 #endif /* _VHOST_NET_CDEV_H_ */