vhost: get guest/host physical address mappings
[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 <unistd.h>
40 #include <linux/vhost.h>
41
42 #include <rte_log.h>
43
44 #include "rte_virtio_net.h"
45
46 /* Used to indicate that the device is running on a data core */
47 #define VIRTIO_DEV_RUNNING 1
48
49 /* Backend value set by guest. */
50 #define VIRTIO_DEV_STOPPED -1
51
52 #define BUF_VECTOR_MAX 256
53
54 /**
55  * Structure contains buffer address, length and descriptor index
56  * from vring to do scatter RX.
57  */
58 struct buf_vector {
59         uint64_t buf_addr;
60         uint32_t buf_len;
61         uint32_t desc_idx;
62 };
63
64 /**
65  * Structure contains variables relevant to RX/TX virtqueues.
66  */
67 struct vhost_virtqueue {
68         struct vring_desc       *desc;
69         struct vring_avail      *avail;
70         struct vring_used       *used;
71         uint32_t                size;
72
73         /* Last index used on the available ring */
74         volatile uint16_t       last_used_idx;
75 #define VIRTIO_INVALID_EVENTFD          (-1)
76 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
77
78         /* Backend value to determine if device should started/stopped */
79         int                     backend;
80         /* Used to notify the guest (trigger interrupt) */
81         int                     callfd;
82         /* Currently unused as polling mode is enabled */
83         int                     kickfd;
84         int                     enabled;
85
86         /* Physical address of used ring, for logging */
87         uint64_t                log_guest_addr;
88 } __rte_cache_aligned;
89
90 /* Old kernels have no such macro defined */
91 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
92  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
93 #endif
94
95
96 /*
97  * Make an extra wrapper for VIRTIO_NET_F_MQ and
98  * VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX as they are
99  * introduced since kernel v3.8. This makes our
100  * code buildable for older kernel.
101  */
102 #ifdef VIRTIO_NET_F_MQ
103  #define VHOST_MAX_QUEUE_PAIRS  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
104  #define VHOST_SUPPORTS_MQ      (1ULL << VIRTIO_NET_F_MQ)
105 #else
106  #define VHOST_MAX_QUEUE_PAIRS  1
107  #define VHOST_SUPPORTS_MQ      0
108 #endif
109
110 /*
111  * Define virtio 1.0 for older kernels
112  */
113 #ifndef VIRTIO_F_VERSION_1
114  #define VIRTIO_F_VERSION_1 32
115 #endif
116
117 struct guest_page {
118         uint64_t guest_phys_addr;
119         uint64_t host_phys_addr;
120         uint64_t size;
121 };
122
123 /**
124  * Device structure contains all configuration information relating
125  * to the device.
126  */
127 struct virtio_net {
128         /* Frontend (QEMU) memory and memory region information */
129         struct virtio_memory    *mem;
130         uint64_t                features;
131         uint64_t                protocol_features;
132         int                     vid;
133         uint32_t                flags;
134         uint16_t                vhost_hlen;
135         /* to tell if we need broadcast rarp packet */
136         rte_atomic16_t          broadcast_rarp;
137         uint32_t                virt_qp_nb;
138         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
139 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
140         char                    ifname[IF_NAME_SZ];
141         uint64_t                log_size;
142         uint64_t                log_base;
143         uint64_t                log_addr;
144         struct ether_addr       mac;
145
146         uint32_t                nr_guest_pages;
147         uint32_t                max_guest_pages;
148         struct guest_page       *guest_pages;
149
150 } __rte_cache_aligned;
151
152 /**
153  * Information relating to memory regions including offsets to
154  * addresses in QEMUs memory file.
155  */
156 struct virtio_memory_region {
157         uint64_t guest_phys_addr;
158         uint64_t guest_user_addr;
159         uint64_t host_user_addr;
160         uint64_t size;
161         void     *mmap_addr;
162         uint64_t mmap_size;
163         int fd;
164 };
165
166
167 /**
168  * Memory structure includes region and mapping information.
169  */
170 struct virtio_memory {
171         uint32_t nregions;
172         struct virtio_memory_region regions[0];
173 };
174
175
176 /* Macros for printing using RTE_LOG */
177 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
178 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
179
180 #ifdef RTE_LIBRTE_VHOST_DEBUG
181 #define VHOST_MAX_PRINT_BUFF 6072
182 #define LOG_LEVEL RTE_LOG_DEBUG
183 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
184 #define PRINT_PACKET(device, addr, size, header) do { \
185         char *pkt_addr = (char *)(addr); \
186         unsigned int index; \
187         char packet[VHOST_MAX_PRINT_BUFF]; \
188         \
189         if ((header)) \
190                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
191         else \
192                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
193         for (index = 0; index < (size); index++) { \
194                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
195                         "%02hhx ", pkt_addr[index]); \
196         } \
197         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
198         \
199         LOG_DEBUG(VHOST_DATA, "%s", packet); \
200 } while (0)
201 #else
202 #define LOG_LEVEL RTE_LOG_INFO
203 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
204 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
205 #endif
206
207 extern uint64_t VHOST_FEATURES;
208 #define MAX_VHOST_DEVICE        1024
209 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
210
211 /* Convert guest physical Address to host virtual address */
212 static inline uint64_t __attribute__((always_inline))
213 gpa_to_vva(struct virtio_net *dev, uint64_t gpa)
214 {
215         struct virtio_memory_region *reg;
216         uint32_t i;
217
218         for (i = 0; i < dev->mem->nregions; i++) {
219                 reg = &dev->mem->regions[i];
220                 if (gpa >= reg->guest_phys_addr &&
221                     gpa <  reg->guest_phys_addr + reg->size) {
222                         return gpa - reg->guest_phys_addr +
223                                reg->host_user_addr;
224                 }
225         }
226
227         return 0;
228 }
229
230 /* Convert guest physical address to host physical address */
231 static inline phys_addr_t __attribute__((always_inline))
232 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
233 {
234         uint32_t i;
235         struct guest_page *page;
236
237         for (i = 0; i < dev->nr_guest_pages; i++) {
238                 page = &dev->guest_pages[i];
239
240                 if (gpa >= page->guest_phys_addr &&
241                     gpa + size < page->guest_phys_addr + page->size) {
242                         return gpa - page->guest_phys_addr +
243                                page->host_phys_addr;
244                 }
245         }
246
247         return 0;
248 }
249
250 struct virtio_net_device_ops const *notify_ops;
251 struct virtio_net *get_device(int vid);
252
253 int vhost_new_device(void);
254 void cleanup_device(struct virtio_net *dev, int destroy);
255 void reset_device(struct virtio_net *dev);
256 void vhost_destroy_device(int);
257
258 int alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx);
259
260 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
261
262 /*
263  * Backend-specific cleanup. Defined by vhost-cuse and vhost-user.
264  */
265 void vhost_backend_cleanup(struct virtio_net *dev);
266
267 #endif /* _VHOST_NET_CDEV_H_ */