Imported Upstream version 16.11.2
[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 /**
56  * Structure contains buffer address, length and descriptor index
57  * from vring to do scatter RX.
58  */
59 struct buf_vector {
60         uint64_t buf_addr;
61         uint32_t buf_len;
62         uint32_t desc_idx;
63 };
64
65 /*
66  * A structure to hold some fields needed in zero copy code path,
67  * mainly for associating an mbuf with the right desc_idx.
68  */
69 struct zcopy_mbuf {
70         struct rte_mbuf *mbuf;
71         uint32_t desc_idx;
72         uint16_t in_use;
73
74         TAILQ_ENTRY(zcopy_mbuf) next;
75 };
76 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
77
78 /**
79  * Structure contains variables relevant to RX/TX virtqueues.
80  */
81 struct vhost_virtqueue {
82         struct vring_desc       *desc;
83         struct vring_avail      *avail;
84         struct vring_used       *used;
85         uint32_t                size;
86
87         uint16_t                last_avail_idx;
88         uint16_t                last_used_idx;
89 #define VIRTIO_INVALID_EVENTFD          (-1)
90 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
91
92         /* Backend value to determine if device should started/stopped */
93         int                     backend;
94         /* Used to notify the guest (trigger interrupt) */
95         int                     callfd;
96         /* Currently unused as polling mode is enabled */
97         int                     kickfd;
98         int                     enabled;
99
100         /* Physical address of used ring, for logging */
101         uint64_t                log_guest_addr;
102
103         uint16_t                nr_zmbuf;
104         uint16_t                zmbuf_size;
105         uint16_t                last_zmbuf_idx;
106         struct zcopy_mbuf       *zmbufs;
107         struct zcopy_mbuf_list  zmbuf_list;
108
109         struct vring_used_elem  *shadow_used_ring;
110         uint16_t                shadow_used_idx;
111 } __rte_cache_aligned;
112
113 /* Old kernels have no such macros defined */
114 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
115  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
116 #endif
117
118 #ifndef VIRTIO_NET_F_MQ
119  #define VIRTIO_NET_F_MQ                22
120 #endif
121 #define VHOST_MAX_QUEUE_PAIRS           0x80
122
123 /*
124  * Define virtio 1.0 for older kernels
125  */
126 #ifndef VIRTIO_F_VERSION_1
127  #define VIRTIO_F_VERSION_1 32
128 #endif
129
130 struct guest_page {
131         uint64_t guest_phys_addr;
132         uint64_t host_phys_addr;
133         uint64_t size;
134 };
135
136 /**
137  * Device structure contains all configuration information relating
138  * to the device.
139  */
140 struct virtio_net {
141         /* Frontend (QEMU) memory and memory region information */
142         struct virtio_memory    *mem;
143         uint64_t                features;
144         uint64_t                protocol_features;
145         int                     vid;
146         uint32_t                flags;
147         uint16_t                vhost_hlen;
148         /* to tell if we need broadcast rarp packet */
149         rte_atomic16_t          broadcast_rarp;
150         uint32_t                virt_qp_nb;
151         int                     dequeue_zero_copy;
152         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
153 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
154         char                    ifname[IF_NAME_SZ];
155         uint64_t                log_size;
156         uint64_t                log_base;
157         uint64_t                log_addr;
158         struct ether_addr       mac;
159
160         uint32_t                nr_guest_pages;
161         uint32_t                max_guest_pages;
162         struct guest_page       *guest_pages;
163 } __rte_cache_aligned;
164
165 /**
166  * Information relating to memory regions including offsets to
167  * addresses in QEMUs memory file.
168  */
169 struct virtio_memory_region {
170         uint64_t guest_phys_addr;
171         uint64_t guest_user_addr;
172         uint64_t host_user_addr;
173         uint64_t size;
174         void     *mmap_addr;
175         uint64_t mmap_size;
176         int fd;
177 };
178
179
180 /**
181  * Memory structure includes region and mapping information.
182  */
183 struct virtio_memory {
184         uint32_t nregions;
185         struct virtio_memory_region regions[0];
186 };
187
188
189 /* Macros for printing using RTE_LOG */
190 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
191 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
192
193 #ifdef RTE_LIBRTE_VHOST_DEBUG
194 #define VHOST_MAX_PRINT_BUFF 6072
195 #define LOG_LEVEL RTE_LOG_DEBUG
196 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
197 #define PRINT_PACKET(device, addr, size, header) do { \
198         char *pkt_addr = (char *)(addr); \
199         unsigned int index; \
200         char packet[VHOST_MAX_PRINT_BUFF]; \
201         \
202         if ((header)) \
203                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
204         else \
205                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
206         for (index = 0; index < (size); index++) { \
207                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
208                         "%02hhx ", pkt_addr[index]); \
209         } \
210         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
211         \
212         LOG_DEBUG(VHOST_DATA, "%s", packet); \
213 } while (0)
214 #else
215 #define LOG_LEVEL RTE_LOG_INFO
216 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
217 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
218 #endif
219
220 extern uint64_t VHOST_FEATURES;
221 #define MAX_VHOST_DEVICE        1024
222 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
223
224 /* Convert guest physical Address to host virtual address */
225 static inline uint64_t __attribute__((always_inline))
226 gpa_to_vva(struct virtio_net *dev, uint64_t gpa)
227 {
228         struct virtio_memory_region *reg;
229         uint32_t i;
230
231         for (i = 0; i < dev->mem->nregions; i++) {
232                 reg = &dev->mem->regions[i];
233                 if (gpa >= reg->guest_phys_addr &&
234                     gpa <  reg->guest_phys_addr + reg->size) {
235                         return gpa - reg->guest_phys_addr +
236                                reg->host_user_addr;
237                 }
238         }
239
240         return 0;
241 }
242
243 /* Convert guest physical address to host physical address */
244 static inline phys_addr_t __attribute__((always_inline))
245 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
246 {
247         uint32_t i;
248         struct guest_page *page;
249
250         for (i = 0; i < dev->nr_guest_pages; i++) {
251                 page = &dev->guest_pages[i];
252
253                 if (gpa >= page->guest_phys_addr &&
254                     gpa + size < page->guest_phys_addr + page->size) {
255                         return gpa - page->guest_phys_addr +
256                                page->host_phys_addr;
257                 }
258         }
259
260         return 0;
261 }
262
263 struct virtio_net_device_ops const *notify_ops;
264 struct virtio_net *get_device(int vid);
265
266 int vhost_new_device(void);
267 void cleanup_device(struct virtio_net *dev, int destroy);
268 void reset_device(struct virtio_net *dev);
269 void vhost_destroy_device(int);
270
271 int alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx);
272
273 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
274 void vhost_enable_dequeue_zero_copy(int vid);
275
276 /*
277  * Backend-specific cleanup.
278  *
279  * TODO: fix it; we have one backend now
280  */
281 void vhost_backend_cleanup(struct virtio_net *dev);
282
283 #endif /* _VHOST_NET_CDEV_H_ */