vhost: remove useless volatile
[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 } __rte_cache_aligned;
109
110 /* Old kernels have no such macro defined */
111 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
112  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
113 #endif
114
115
116 /*
117  * Make an extra wrapper for VIRTIO_NET_F_MQ and
118  * VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX as they are
119  * introduced since kernel v3.8. This makes our
120  * code buildable for older kernel.
121  */
122 #ifdef VIRTIO_NET_F_MQ
123  #define VHOST_MAX_QUEUE_PAIRS  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
124  #define VHOST_SUPPORTS_MQ      (1ULL << VIRTIO_NET_F_MQ)
125 #else
126  #define VHOST_MAX_QUEUE_PAIRS  1
127  #define VHOST_SUPPORTS_MQ      0
128 #endif
129
130 /*
131  * Define virtio 1.0 for older kernels
132  */
133 #ifndef VIRTIO_F_VERSION_1
134  #define VIRTIO_F_VERSION_1 32
135 #endif
136
137 struct guest_page {
138         uint64_t guest_phys_addr;
139         uint64_t host_phys_addr;
140         uint64_t size;
141 };
142
143 /**
144  * Device structure contains all configuration information relating
145  * to the device.
146  */
147 struct virtio_net {
148         /* Frontend (QEMU) memory and memory region information */
149         struct virtio_memory    *mem;
150         uint64_t                features;
151         uint64_t                protocol_features;
152         int                     vid;
153         uint32_t                flags;
154         uint16_t                vhost_hlen;
155         /* to tell if we need broadcast rarp packet */
156         rte_atomic16_t          broadcast_rarp;
157         uint32_t                virt_qp_nb;
158         int                     dequeue_zero_copy;
159         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
160 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
161         char                    ifname[IF_NAME_SZ];
162         uint64_t                log_size;
163         uint64_t                log_base;
164         uint64_t                log_addr;
165         struct ether_addr       mac;
166
167         uint32_t                nr_guest_pages;
168         uint32_t                max_guest_pages;
169         struct guest_page       *guest_pages;
170 } __rte_cache_aligned;
171
172 /**
173  * Information relating to memory regions including offsets to
174  * addresses in QEMUs memory file.
175  */
176 struct virtio_memory_region {
177         uint64_t guest_phys_addr;
178         uint64_t guest_user_addr;
179         uint64_t host_user_addr;
180         uint64_t size;
181         void     *mmap_addr;
182         uint64_t mmap_size;
183         int fd;
184 };
185
186
187 /**
188  * Memory structure includes region and mapping information.
189  */
190 struct virtio_memory {
191         uint32_t nregions;
192         struct virtio_memory_region regions[0];
193 };
194
195
196 /* Macros for printing using RTE_LOG */
197 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
198 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
199
200 #ifdef RTE_LIBRTE_VHOST_DEBUG
201 #define VHOST_MAX_PRINT_BUFF 6072
202 #define LOG_LEVEL RTE_LOG_DEBUG
203 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
204 #define PRINT_PACKET(device, addr, size, header) do { \
205         char *pkt_addr = (char *)(addr); \
206         unsigned int index; \
207         char packet[VHOST_MAX_PRINT_BUFF]; \
208         \
209         if ((header)) \
210                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
211         else \
212                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
213         for (index = 0; index < (size); index++) { \
214                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
215                         "%02hhx ", pkt_addr[index]); \
216         } \
217         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
218         \
219         LOG_DEBUG(VHOST_DATA, "%s", packet); \
220 } while (0)
221 #else
222 #define LOG_LEVEL RTE_LOG_INFO
223 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
224 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
225 #endif
226
227 extern uint64_t VHOST_FEATURES;
228 #define MAX_VHOST_DEVICE        1024
229 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
230
231 /* Convert guest physical Address to host virtual address */
232 static inline uint64_t __attribute__((always_inline))
233 gpa_to_vva(struct virtio_net *dev, uint64_t gpa)
234 {
235         struct virtio_memory_region *reg;
236         uint32_t i;
237
238         for (i = 0; i < dev->mem->nregions; i++) {
239                 reg = &dev->mem->regions[i];
240                 if (gpa >= reg->guest_phys_addr &&
241                     gpa <  reg->guest_phys_addr + reg->size) {
242                         return gpa - reg->guest_phys_addr +
243                                reg->host_user_addr;
244                 }
245         }
246
247         return 0;
248 }
249
250 /* Convert guest physical address to host physical address */
251 static inline phys_addr_t __attribute__((always_inline))
252 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
253 {
254         uint32_t i;
255         struct guest_page *page;
256
257         for (i = 0; i < dev->nr_guest_pages; i++) {
258                 page = &dev->guest_pages[i];
259
260                 if (gpa >= page->guest_phys_addr &&
261                     gpa + size < page->guest_phys_addr + page->size) {
262                         return gpa - page->guest_phys_addr +
263                                page->host_phys_addr;
264                 }
265         }
266
267         return 0;
268 }
269
270 struct virtio_net_device_ops const *notify_ops;
271 struct virtio_net *get_device(int vid);
272
273 int vhost_new_device(void);
274 void cleanup_device(struct virtio_net *dev, int destroy);
275 void reset_device(struct virtio_net *dev);
276 void vhost_destroy_device(int);
277
278 int alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx);
279
280 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
281 void vhost_enable_dequeue_zero_copy(int vid);
282
283 /*
284  * Backend-specific cleanup. Defined by vhost-cuse and vhost-user.
285  */
286 void vhost_backend_cleanup(struct virtio_net *dev);
287
288 #endif /* _VHOST_NET_CDEV_H_ */