New upstream version 16.11.7
[deb_dpdk.git] / lib / librte_vhost / vhost.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #ifndef _VHOST_NET_CDEV_H_
35 #define _VHOST_NET_CDEV_H_
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <sys/queue.h>
40 #include <unistd.h>
41 #include <linux/vhost.h>
42
43 #include <rte_log.h>
44
45 #include "rte_virtio_net.h"
46
47 /* Used to indicate that the device is running on a data core */
48 #define VIRTIO_DEV_RUNNING 1
49
50 /* Backend value set by guest. */
51 #define VIRTIO_DEV_STOPPED -1
52
53 #define BUF_VECTOR_MAX 256
54
55 #define VHOST_LOG_CACHE_NR 32
56
57 /**
58  * Structure contains buffer address, length and descriptor index
59  * from vring to do scatter RX.
60  */
61 struct buf_vector {
62         uint64_t buf_addr;
63         uint32_t buf_len;
64         uint32_t desc_idx;
65 };
66
67 /*
68  * A structure to hold some fields needed in zero copy code path,
69  * mainly for associating an mbuf with the right desc_idx.
70  */
71 struct zcopy_mbuf {
72         struct rte_mbuf *mbuf;
73         uint32_t desc_idx;
74         uint16_t in_use;
75
76         TAILQ_ENTRY(zcopy_mbuf) next;
77 };
78 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
79
80 /*
81  * Structure that contains the info for batched dirty logging.
82  */
83 struct log_cache_entry {
84         uint32_t offset;
85         unsigned long val;
86 };
87
88 /**
89  * Structure contains variables relevant to RX/TX virtqueues.
90  */
91 struct vhost_virtqueue {
92         struct vring_desc       *desc;
93         struct vring_avail      *avail;
94         struct vring_used       *used;
95         uint32_t                size;
96
97         uint16_t                last_avail_idx;
98         uint16_t                last_used_idx;
99 #define VIRTIO_INVALID_EVENTFD          (-1)
100 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
101
102         /* Backend value to determine if device should started/stopped */
103         int                     backend;
104         rte_spinlock_t          access_lock;
105
106         /* Used to notify the guest (trigger interrupt) */
107         int                     callfd;
108         /* Currently unused as polling mode is enabled */
109         int                     kickfd;
110         int                     enabled;
111
112         /* Physical address of used ring, for logging */
113         uint64_t                log_guest_addr;
114
115         uint16_t                nr_zmbuf;
116         uint16_t                zmbuf_size;
117         uint16_t                last_zmbuf_idx;
118         struct zcopy_mbuf       *zmbufs;
119         struct zcopy_mbuf_list  zmbuf_list;
120
121         struct vring_used_elem  *shadow_used_ring;
122         uint16_t                shadow_used_idx;
123
124         struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
125         uint16_t log_cache_nb_elem;
126 } __rte_cache_aligned;
127
128 /* Old kernels have no such macros defined */
129 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
130  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
131 #endif
132
133 #ifndef VIRTIO_NET_F_MQ
134  #define VIRTIO_NET_F_MQ                22
135 #endif
136 #define VHOST_MAX_QUEUE_PAIRS           0x80
137
138 /*
139  * Define virtio 1.0 for older kernels
140  */
141 #ifndef VIRTIO_F_VERSION_1
142  #define VIRTIO_F_VERSION_1 32
143 #endif
144
145 struct guest_page {
146         uint64_t guest_phys_addr;
147         uint64_t host_phys_addr;
148         uint64_t size;
149 };
150
151 /**
152  * Device structure contains all configuration information relating
153  * to the device.
154  */
155 struct virtio_net {
156         /* Frontend (QEMU) memory and memory region information */
157         struct virtio_memory    *mem;
158         uint64_t                features;
159         uint64_t                protocol_features;
160         int                     vid;
161         uint32_t                flags;
162         uint16_t                vhost_hlen;
163         /* to tell if we need broadcast rarp packet */
164         rte_atomic16_t          broadcast_rarp;
165         uint32_t                virt_qp_nb;
166         int                     dequeue_zero_copy;
167         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
168 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
169         char                    ifname[IF_NAME_SZ];
170         uint64_t                log_size;
171         uint64_t                log_base;
172         uint64_t                log_addr;
173         struct ether_addr       mac;
174
175         uint32_t                nr_guest_pages;
176         uint32_t                max_guest_pages;
177         struct guest_page       *guest_pages;
178 } __rte_cache_aligned;
179
180 /**
181  * Information relating to memory regions including offsets to
182  * addresses in QEMUs memory file.
183  */
184 struct virtio_memory_region {
185         uint64_t guest_phys_addr;
186         uint64_t guest_user_addr;
187         uint64_t host_user_addr;
188         uint64_t size;
189         void     *mmap_addr;
190         uint64_t mmap_size;
191         int fd;
192 };
193
194
195 /**
196  * Memory structure includes region and mapping information.
197  */
198 struct virtio_memory {
199         uint32_t nregions;
200         struct virtio_memory_region regions[0];
201 };
202
203
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
207
208 #ifdef RTE_LIBRTE_VHOST_DEBUG
209 #define VHOST_MAX_PRINT_BUFF 6072
210 #define LOG_LEVEL RTE_LOG_DEBUG
211 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
212         RTE_LOG(DEBUG, log_type, fmt, ##args)
213 #define PRINT_PACKET(device, addr, size, header) do { \
214         char *pkt_addr = (char *)(addr); \
215         unsigned int index; \
216         char packet[VHOST_MAX_PRINT_BUFF]; \
217         \
218         if ((header)) \
219                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
220         else \
221                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
222         for (index = 0; index < (size); index++) { \
223                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
224                         "%02hhx ", pkt_addr[index]); \
225         } \
226         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
227         \
228         VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
229 } while (0)
230 #else
231 #define LOG_LEVEL RTE_LOG_INFO
232 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
233 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
234 #endif
235
236 extern uint64_t VHOST_FEATURES;
237 #define MAX_VHOST_DEVICE        1024
238 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
239
240 /* Convert guest physical Address to host virtual address */
241 static inline uint64_t __attribute__((always_inline))
242 gpa_to_vva(struct virtio_net *dev, uint64_t gpa, uint64_t *len)
243 {
244         struct virtio_memory_region *r;
245         uint32_t i;
246
247         for (i = 0; i < dev->mem->nregions; i++) {
248                 r = &dev->mem->regions[i];
249                 if (gpa >= r->guest_phys_addr &&
250                     gpa <  r->guest_phys_addr + r->size) {
251
252                         if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
253                                 *len = r->guest_phys_addr + r->size - gpa;
254
255                         return gpa - r->guest_phys_addr +
256                                r->host_user_addr;
257                 }
258         }
259         *len = 0;
260
261         return 0;
262 }
263
264 /* Convert guest physical address to host physical address */
265 static inline phys_addr_t __attribute__((always_inline))
266 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
267 {
268         uint32_t i;
269         struct guest_page *page;
270
271         for (i = 0; i < dev->nr_guest_pages; i++) {
272                 page = &dev->guest_pages[i];
273
274                 if (gpa >= page->guest_phys_addr &&
275                     gpa + size < page->guest_phys_addr + page->size) {
276                         return gpa - page->guest_phys_addr +
277                                page->host_phys_addr;
278                 }
279         }
280
281         return 0;
282 }
283
284 struct virtio_net_device_ops const *notify_ops;
285 struct virtio_net *get_device(int vid);
286
287 int vhost_new_device(void);
288 void cleanup_device(struct virtio_net *dev, int destroy);
289 void reset_device(struct virtio_net *dev);
290 void vhost_destroy_device(int);
291
292 int alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx);
293
294 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
295 void vhost_enable_dequeue_zero_copy(int vid);
296
297 /*
298  * Backend-specific cleanup.
299  *
300  * TODO: fix it; we have one backend now
301  */
302 void vhost_backend_cleanup(struct virtio_net *dev);
303
304 #endif /* _VHOST_NET_CDEV_H_ */