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