vhost: add IOMMU-related macros for old kernels
[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 #include <linux/virtio_net.h>
43 #include <sys/socket.h>
44 #include <linux/if.h>
45
46 #include <rte_log.h>
47 #include <rte_ether.h>
48
49 #include "rte_vhost.h"
50
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
55
56 /* Backend value set by guest. */
57 #define VIRTIO_DEV_STOPPED -1
58
59 #define BUF_VECTOR_MAX 256
60
61 /**
62  * Structure contains buffer address, length and descriptor index
63  * from vring to do scatter RX.
64  */
65 struct buf_vector {
66         uint64_t buf_addr;
67         uint32_t buf_len;
68         uint32_t desc_idx;
69 };
70
71 /*
72  * A structure to hold some fields needed in zero copy code path,
73  * mainly for associating an mbuf with the right desc_idx.
74  */
75 struct zcopy_mbuf {
76         struct rte_mbuf *mbuf;
77         uint32_t desc_idx;
78         uint16_t in_use;
79
80         TAILQ_ENTRY(zcopy_mbuf) next;
81 };
82 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
83
84 /*
85  * Structure contains the info for each batched memory copy.
86  */
87 struct batch_copy_elem {
88         void *dst;
89         void *src;
90         uint32_t len;
91         uint64_t log_addr;
92 };
93
94 /**
95  * Structure contains variables relevant to RX/TX virtqueues.
96  */
97 struct vhost_virtqueue {
98         struct vring_desc       *desc;
99         struct vring_avail      *avail;
100         struct vring_used       *used;
101         uint32_t                size;
102
103         uint16_t                last_avail_idx;
104         uint16_t                last_used_idx;
105 #define VIRTIO_INVALID_EVENTFD          (-1)
106 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
107
108         /* Backend value to determine if device should started/stopped */
109         int                     backend;
110         /* Used to notify the guest (trigger interrupt) */
111         int                     callfd;
112         /* Currently unused as polling mode is enabled */
113         int                     kickfd;
114         int                     enabled;
115
116         /* Physical address of used ring, for logging */
117         uint64_t                log_guest_addr;
118
119         uint16_t                nr_zmbuf;
120         uint16_t                zmbuf_size;
121         uint16_t                last_zmbuf_idx;
122         struct zcopy_mbuf       *zmbufs;
123         struct zcopy_mbuf_list  zmbuf_list;
124
125         struct vring_used_elem  *shadow_used_ring;
126         uint16_t                shadow_used_idx;
127
128         struct batch_copy_elem  *batch_copy_elems;
129         uint16_t                batch_copy_nb_elems;
130 } __rte_cache_aligned;
131
132 /* Old kernels have no such macros defined */
133 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
134  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
135 #endif
136
137 #ifndef VIRTIO_NET_F_MQ
138  #define VIRTIO_NET_F_MQ                22
139 #endif
140
141 #define VHOST_MAX_VRING                 0x100
142 #define VHOST_MAX_QUEUE_PAIRS           0x80
143
144 #ifndef VIRTIO_NET_F_MTU
145  #define VIRTIO_NET_F_MTU 3
146 #endif
147
148 /* Declare IOMMU related bits for older kernels */
149 #ifndef VIRTIO_F_IOMMU_PLATFORM
150
151 #define VIRTIO_F_IOMMU_PLATFORM 33
152
153 struct vhost_iotlb_msg {
154         __u64 iova;
155         __u64 size;
156         __u64 uaddr;
157 #define VHOST_ACCESS_RO      0x1
158 #define VHOST_ACCESS_WO      0x2
159 #define VHOST_ACCESS_RW      0x3
160         __u8 perm;
161 #define VHOST_IOTLB_MISS           1
162 #define VHOST_IOTLB_UPDATE         2
163 #define VHOST_IOTLB_INVALIDATE     3
164 #define VHOST_IOTLB_ACCESS_FAIL    4
165         __u8 type;
166 };
167
168 #define VHOST_IOTLB_MSG 0x1
169
170 struct vhost_msg {
171         int type;
172         union {
173                 struct vhost_iotlb_msg iotlb;
174                 __u8 padding[64];
175         };
176 };
177 #endif
178
179 /*
180  * Define virtio 1.0 for older kernels
181  */
182 #ifndef VIRTIO_F_VERSION_1
183  #define VIRTIO_F_VERSION_1 32
184 #endif
185
186 #define VHOST_USER_F_PROTOCOL_FEATURES  30
187
188 /* Features supported by this builtin vhost-user net driver. */
189 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
190                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
191                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
192                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
193                                 (1ULL << VIRTIO_NET_F_MQ)      | \
194                                 (1ULL << VIRTIO_F_VERSION_1)   | \
195                                 (1ULL << VHOST_F_LOG_ALL)      | \
196                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
197                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
198                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
199                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
200                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
201                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
202                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
203                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
204                                 (1ULL << VIRTIO_NET_F_MTU))
205
206
207 struct guest_page {
208         uint64_t guest_phys_addr;
209         uint64_t host_phys_addr;
210         uint64_t size;
211 };
212
213 /**
214  * Device structure contains all configuration information relating
215  * to the device.
216  */
217 struct virtio_net {
218         /* Frontend (QEMU) memory and memory region information */
219         struct rte_vhost_memory *mem;
220         uint64_t                features;
221         uint64_t                protocol_features;
222         int                     vid;
223         uint32_t                flags;
224         uint16_t                vhost_hlen;
225         /* to tell if we need broadcast rarp packet */
226         rte_atomic16_t          broadcast_rarp;
227         uint32_t                nr_vring;
228         int                     dequeue_zero_copy;
229         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
230 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
231         char                    ifname[IF_NAME_SZ];
232         uint64_t                log_size;
233         uint64_t                log_base;
234         uint64_t                log_addr;
235         struct ether_addr       mac;
236         uint16_t                mtu;
237
238         struct vhost_device_ops const *notify_ops;
239
240         uint32_t                nr_guest_pages;
241         uint32_t                max_guest_pages;
242         struct guest_page       *guest_pages;
243
244         int                     slave_req_fd;
245 } __rte_cache_aligned;
246
247
248 #define VHOST_LOG_PAGE  4096
249
250 /*
251  * Atomically set a bit in memory.
252  */
253 static __rte_always_inline void
254 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
255 {
256         __sync_fetch_and_or_8(addr, (1U << nr));
257 }
258
259 static __rte_always_inline void
260 vhost_log_page(uint8_t *log_base, uint64_t page)
261 {
262         vhost_set_bit(page % 8, &log_base[page / 8]);
263 }
264
265 static __rte_always_inline void
266 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
267 {
268         uint64_t page;
269
270         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
271                    !dev->log_base || !len))
272                 return;
273
274         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
275                 return;
276
277         /* To make sure guest memory updates are committed before logging */
278         rte_smp_wmb();
279
280         page = addr / VHOST_LOG_PAGE;
281         while (page * VHOST_LOG_PAGE < addr + len) {
282                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
283                 page += 1;
284         }
285 }
286
287 static __rte_always_inline void
288 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
289                      uint64_t offset, uint64_t len)
290 {
291         vhost_log_write(dev, vq->log_guest_addr + offset, len);
292 }
293
294 /* Macros for printing using RTE_LOG */
295 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
296 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
297
298 #ifdef RTE_LIBRTE_VHOST_DEBUG
299 #define VHOST_MAX_PRINT_BUFF 6072
300 #define LOG_LEVEL RTE_LOG_DEBUG
301 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
302 #define PRINT_PACKET(device, addr, size, header) do { \
303         char *pkt_addr = (char *)(addr); \
304         unsigned int index; \
305         char packet[VHOST_MAX_PRINT_BUFF]; \
306         \
307         if ((header)) \
308                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
309         else \
310                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
311         for (index = 0; index < (size); index++) { \
312                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
313                         "%02hhx ", pkt_addr[index]); \
314         } \
315         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
316         \
317         LOG_DEBUG(VHOST_DATA, "%s", packet); \
318 } while (0)
319 #else
320 #define LOG_LEVEL RTE_LOG_INFO
321 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
322 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
323 #endif
324
325 extern uint64_t VHOST_FEATURES;
326 #define MAX_VHOST_DEVICE        1024
327 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
328
329 /* Convert guest physical address to host physical address */
330 static __rte_always_inline phys_addr_t
331 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
332 {
333         uint32_t i;
334         struct guest_page *page;
335
336         for (i = 0; i < dev->nr_guest_pages; i++) {
337                 page = &dev->guest_pages[i];
338
339                 if (gpa >= page->guest_phys_addr &&
340                     gpa + size < page->guest_phys_addr + page->size) {
341                         return gpa - page->guest_phys_addr +
342                                page->host_phys_addr;
343                 }
344         }
345
346         return 0;
347 }
348
349 struct virtio_net *get_device(int vid);
350
351 int vhost_new_device(void);
352 void cleanup_device(struct virtio_net *dev, int destroy);
353 void reset_device(struct virtio_net *dev);
354 void vhost_destroy_device(int);
355
356 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
357
358 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
359 void vhost_enable_dequeue_zero_copy(int vid);
360
361 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
362
363 /*
364  * Backend-specific cleanup.
365  *
366  * TODO: fix it; we have one backend now
367  */
368 void vhost_backend_cleanup(struct virtio_net *dev);
369
370 #endif /* _VHOST_NET_CDEV_H_ */