6fe72aeb61df2ac977c633f46a4d425aa7294e2c
[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 #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 variables relevant to RX/TX virtqueues.
86  */
87 struct vhost_virtqueue {
88         struct vring_desc       *desc;
89         struct vring_avail      *avail;
90         struct vring_used       *used;
91         uint32_t                size;
92
93         uint16_t                last_avail_idx;
94         uint16_t                last_used_idx;
95 #define VIRTIO_INVALID_EVENTFD          (-1)
96 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
97
98         /* Backend value to determine if device should started/stopped */
99         int                     backend;
100         /* Used to notify the guest (trigger interrupt) */
101         int                     callfd;
102         /* Currently unused as polling mode is enabled */
103         int                     kickfd;
104         int                     enabled;
105
106         /* Physical address of used ring, for logging */
107         uint64_t                log_guest_addr;
108
109         uint16_t                nr_zmbuf;
110         uint16_t                zmbuf_size;
111         uint16_t                last_zmbuf_idx;
112         struct zcopy_mbuf       *zmbufs;
113         struct zcopy_mbuf_list  zmbuf_list;
114
115         struct vring_used_elem  *shadow_used_ring;
116         uint16_t                shadow_used_idx;
117 } __rte_cache_aligned;
118
119 /* Old kernels have no such macros defined */
120 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
121  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
122 #endif
123
124 #ifndef VIRTIO_NET_F_MQ
125  #define VIRTIO_NET_F_MQ                22
126 #endif
127
128 #define VHOST_MAX_VRING                 0x100
129 #define VHOST_MAX_QUEUE_PAIRS           0x80
130
131 #ifndef VIRTIO_NET_F_MTU
132  #define VIRTIO_NET_F_MTU 3
133 #endif
134
135 /*
136  * Define virtio 1.0 for older kernels
137  */
138 #ifndef VIRTIO_F_VERSION_1
139  #define VIRTIO_F_VERSION_1 32
140 #endif
141
142 #define VHOST_USER_F_PROTOCOL_FEATURES  30
143
144 /* Features supported by this builtin vhost-user net driver. */
145 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
146                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
147                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
148                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
149                                 (1ULL << VIRTIO_NET_F_MQ)      | \
150                                 (1ULL << VIRTIO_F_VERSION_1)   | \
151                                 (1ULL << VHOST_F_LOG_ALL)      | \
152                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
153                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
154                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
155                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
156                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
157                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
158                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
159                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
160                                 (1ULL << VIRTIO_NET_F_MTU))
161
162
163 struct guest_page {
164         uint64_t guest_phys_addr;
165         uint64_t host_phys_addr;
166         uint64_t size;
167 };
168
169 /**
170  * Device structure contains all configuration information relating
171  * to the device.
172  */
173 struct virtio_net {
174         /* Frontend (QEMU) memory and memory region information */
175         struct rte_vhost_memory *mem;
176         uint64_t                features;
177         uint64_t                protocol_features;
178         int                     vid;
179         uint32_t                flags;
180         uint16_t                vhost_hlen;
181         /* to tell if we need broadcast rarp packet */
182         rte_atomic16_t          broadcast_rarp;
183         uint32_t                nr_vring;
184         int                     dequeue_zero_copy;
185         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
186 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
187         char                    ifname[IF_NAME_SZ];
188         uint64_t                log_size;
189         uint64_t                log_base;
190         uint64_t                log_addr;
191         struct ether_addr       mac;
192         uint16_t                mtu;
193
194         struct vhost_device_ops const *notify_ops;
195
196         uint32_t                nr_guest_pages;
197         uint32_t                max_guest_pages;
198         struct guest_page       *guest_pages;
199 } __rte_cache_aligned;
200
201
202 #define VHOST_LOG_PAGE  4096
203
204 /*
205  * Atomically set a bit in memory.
206  */
207 static __rte_always_inline void
208 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
209 {
210         __sync_fetch_and_or_8(addr, (1U << nr));
211 }
212
213 static __rte_always_inline void
214 vhost_log_page(uint8_t *log_base, uint64_t page)
215 {
216         vhost_set_bit(page % 8, &log_base[page / 8]);
217 }
218
219 static __rte_always_inline void
220 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
221 {
222         uint64_t page;
223
224         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
225                    !dev->log_base || !len))
226                 return;
227
228         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
229                 return;
230
231         /* To make sure guest memory updates are committed before logging */
232         rte_smp_wmb();
233
234         page = addr / VHOST_LOG_PAGE;
235         while (page * VHOST_LOG_PAGE < addr + len) {
236                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
237                 page += 1;
238         }
239 }
240
241 static __rte_always_inline void
242 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
243                      uint64_t offset, uint64_t len)
244 {
245         vhost_log_write(dev, vq->log_guest_addr + offset, len);
246 }
247
248 /* Macros for printing using RTE_LOG */
249 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
250 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
251
252 #ifdef RTE_LIBRTE_VHOST_DEBUG
253 #define VHOST_MAX_PRINT_BUFF 6072
254 #define LOG_LEVEL RTE_LOG_DEBUG
255 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
256 #define PRINT_PACKET(device, addr, size, header) do { \
257         char *pkt_addr = (char *)(addr); \
258         unsigned int index; \
259         char packet[VHOST_MAX_PRINT_BUFF]; \
260         \
261         if ((header)) \
262                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
263         else \
264                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
265         for (index = 0; index < (size); index++) { \
266                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
267                         "%02hhx ", pkt_addr[index]); \
268         } \
269         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
270         \
271         LOG_DEBUG(VHOST_DATA, "%s", packet); \
272 } while (0)
273 #else
274 #define LOG_LEVEL RTE_LOG_INFO
275 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
276 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
277 #endif
278
279 extern uint64_t VHOST_FEATURES;
280 #define MAX_VHOST_DEVICE        1024
281 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
282
283 /* Convert guest physical address to host physical address */
284 static __rte_always_inline phys_addr_t
285 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
286 {
287         uint32_t i;
288         struct guest_page *page;
289
290         for (i = 0; i < dev->nr_guest_pages; i++) {
291                 page = &dev->guest_pages[i];
292
293                 if (gpa >= page->guest_phys_addr &&
294                     gpa + size < page->guest_phys_addr + page->size) {
295                         return gpa - page->guest_phys_addr +
296                                page->host_phys_addr;
297                 }
298         }
299
300         return 0;
301 }
302
303 struct virtio_net *get_device(int vid);
304
305 int vhost_new_device(void);
306 void cleanup_device(struct virtio_net *dev, int destroy);
307 void reset_device(struct virtio_net *dev);
308 void vhost_destroy_device(int);
309
310 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
311
312 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
313 void vhost_enable_dequeue_zero_copy(int vid);
314
315 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
316
317 /*
318  * Backend-specific cleanup.
319  *
320  * TODO: fix it; we have one backend now
321  */
322 void vhost_backend_cleanup(struct virtio_net *dev);
323
324 #endif /* _VHOST_NET_CDEV_H_ */