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