vhost: turn queue pair to vring
[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 /* Used to indicate that the device is ready to operate */
50 #define VIRTIO_DEV_READY 2
51
52 /* Backend value set by guest. */
53 #define VIRTIO_DEV_STOPPED -1
54
55 #define BUF_VECTOR_MAX 256
56
57 /**
58  * Structure contains buffer address, length and descriptor index
59  * from vring to do scatter RX.
60  */
61 struct buf_vector {
62         uint64_t buf_addr;
63         uint32_t buf_len;
64         uint32_t desc_idx;
65 };
66
67 /*
68  * A structure to hold some fields needed in zero copy code path,
69  * mainly for associating an mbuf with the right desc_idx.
70  */
71 struct zcopy_mbuf {
72         struct rte_mbuf *mbuf;
73         uint32_t desc_idx;
74         uint16_t in_use;
75
76         TAILQ_ENTRY(zcopy_mbuf) next;
77 };
78 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
79
80 /**
81  * Structure contains variables relevant to RX/TX virtqueues.
82  */
83 struct vhost_virtqueue {
84         struct vring_desc       *desc;
85         struct vring_avail      *avail;
86         struct vring_used       *used;
87         uint32_t                size;
88
89         uint16_t                last_avail_idx;
90         uint16_t                last_used_idx;
91 #define VIRTIO_INVALID_EVENTFD          (-1)
92 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
93
94         /* Backend value to determine if device should started/stopped */
95         int                     backend;
96         /* Used to notify the guest (trigger interrupt) */
97         int                     callfd;
98         /* Currently unused as polling mode is enabled */
99         int                     kickfd;
100         int                     enabled;
101
102         /* Physical address of used ring, for logging */
103         uint64_t                log_guest_addr;
104
105         uint16_t                nr_zmbuf;
106         uint16_t                zmbuf_size;
107         uint16_t                last_zmbuf_idx;
108         struct zcopy_mbuf       *zmbufs;
109         struct zcopy_mbuf_list  zmbuf_list;
110
111         struct vring_used_elem  *shadow_used_ring;
112         uint16_t                shadow_used_idx;
113 } __rte_cache_aligned;
114
115 /* Old kernels have no such macros defined */
116 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
117  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
118 #endif
119
120 #ifndef VIRTIO_NET_F_MQ
121  #define VIRTIO_NET_F_MQ                22
122 #endif
123
124 #define VHOST_MAX_VRING                 0x100
125 #define VHOST_MAX_QUEUE_PAIRS           0x80
126
127 #ifndef VIRTIO_NET_F_MTU
128  #define VIRTIO_NET_F_MTU 3
129 #endif
130
131 /*
132  * Define virtio 1.0 for older kernels
133  */
134 #ifndef VIRTIO_F_VERSION_1
135  #define VIRTIO_F_VERSION_1 32
136 #endif
137
138 #define VHOST_USER_F_PROTOCOL_FEATURES  30
139
140 /* Features supported by this builtin vhost-user net driver. */
141 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
142                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
143                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
144                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
145                                 (1ULL << VIRTIO_NET_F_MQ)      | \
146                                 (1ULL << VIRTIO_F_VERSION_1)   | \
147                                 (1ULL << VHOST_F_LOG_ALL)      | \
148                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
149                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
150                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
151                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
152                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
153                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
154                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
155                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
156                                 (1ULL << VIRTIO_NET_F_MTU))
157
158
159 struct guest_page {
160         uint64_t guest_phys_addr;
161         uint64_t host_phys_addr;
162         uint64_t size;
163 };
164
165 /**
166  * Device structure contains all configuration information relating
167  * to the device.
168  */
169 struct virtio_net {
170         /* Frontend (QEMU) memory and memory region information */
171         struct rte_vhost_memory *mem;
172         uint64_t                features;
173         uint64_t                protocol_features;
174         int                     vid;
175         uint32_t                flags;
176         uint16_t                vhost_hlen;
177         /* to tell if we need broadcast rarp packet */
178         rte_atomic16_t          broadcast_rarp;
179         uint32_t                nr_vring;
180         int                     dequeue_zero_copy;
181         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
182 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
183         char                    ifname[IF_NAME_SZ];
184         uint64_t                log_size;
185         uint64_t                log_base;
186         uint64_t                log_addr;
187         struct ether_addr       mac;
188         uint16_t                mtu;
189
190         struct virtio_net_device_ops const *notify_ops;
191
192         uint32_t                nr_guest_pages;
193         uint32_t                max_guest_pages;
194         struct guest_page       *guest_pages;
195 } __rte_cache_aligned;
196
197 /* Macros for printing using RTE_LOG */
198 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
199 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
200
201 #ifdef RTE_LIBRTE_VHOST_DEBUG
202 #define VHOST_MAX_PRINT_BUFF 6072
203 #define LOG_LEVEL RTE_LOG_DEBUG
204 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
205 #define PRINT_PACKET(device, addr, size, header) do { \
206         char *pkt_addr = (char *)(addr); \
207         unsigned int index; \
208         char packet[VHOST_MAX_PRINT_BUFF]; \
209         \
210         if ((header)) \
211                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
212         else \
213                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
214         for (index = 0; index < (size); index++) { \
215                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
216                         "%02hhx ", pkt_addr[index]); \
217         } \
218         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
219         \
220         LOG_DEBUG(VHOST_DATA, "%s", packet); \
221 } while (0)
222 #else
223 #define LOG_LEVEL RTE_LOG_INFO
224 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
225 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
226 #endif
227
228 extern uint64_t VHOST_FEATURES;
229 #define MAX_VHOST_DEVICE        1024
230 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
231
232 /* Convert guest physical address to host physical address */
233 static inline phys_addr_t __attribute__((always_inline))
234 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
235 {
236         uint32_t i;
237         struct guest_page *page;
238
239         for (i = 0; i < dev->nr_guest_pages; i++) {
240                 page = &dev->guest_pages[i];
241
242                 if (gpa >= page->guest_phys_addr &&
243                     gpa + size < page->guest_phys_addr + page->size) {
244                         return gpa - page->guest_phys_addr +
245                                page->host_phys_addr;
246                 }
247         }
248
249         return 0;
250 }
251
252 struct virtio_net *get_device(int vid);
253
254 int vhost_new_device(void);
255 void cleanup_device(struct virtio_net *dev, int destroy);
256 void reset_device(struct virtio_net *dev);
257 void vhost_destroy_device(int);
258
259 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
260
261 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
262 void vhost_enable_dequeue_zero_copy(int vid);
263
264 struct virtio_net_device_ops const *vhost_driver_callback_get(const char *path);
265
266 /*
267  * Backend-specific cleanup.
268  *
269  * TODO: fix it; we have one backend now
270  */
271 void vhost_backend_cleanup(struct virtio_net *dev);
272
273 #endif /* _VHOST_NET_CDEV_H_ */