vhost: fix build with old kernels
[dpdk.git] / lib / librte_vhost / vhost.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _VHOST_NET_CDEV_H_
6 #define _VHOST_NET_CDEV_H_
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/queue.h>
11 #include <unistd.h>
12 #include <linux/vhost.h>
13 #include <linux/virtio_net.h>
14 #include <sys/socket.h>
15 #include <linux/if.h>
16
17 #include <rte_log.h>
18 #include <rte_ether.h>
19 #include <rte_rwlock.h>
20
21 #include "rte_vhost.h"
22
23 /* Used to indicate that the device is running on a data core */
24 #define VIRTIO_DEV_RUNNING 1
25 /* Used to indicate that the device is ready to operate */
26 #define VIRTIO_DEV_READY 2
27
28 /* Backend value set by guest. */
29 #define VIRTIO_DEV_STOPPED -1
30
31 #define BUF_VECTOR_MAX 256
32
33 /**
34  * Structure contains buffer address, length and descriptor index
35  * from vring to do scatter RX.
36  */
37 struct buf_vector {
38         uint64_t buf_addr;
39         uint32_t buf_len;
40         uint32_t desc_idx;
41 };
42
43 /*
44  * A structure to hold some fields needed in zero copy code path,
45  * mainly for associating an mbuf with the right desc_idx.
46  */
47 struct zcopy_mbuf {
48         struct rte_mbuf *mbuf;
49         uint32_t desc_idx;
50         uint16_t in_use;
51
52         TAILQ_ENTRY(zcopy_mbuf) next;
53 };
54 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
55
56 /*
57  * Structure contains the info for each batched memory copy.
58  */
59 struct batch_copy_elem {
60         void *dst;
61         void *src;
62         uint32_t len;
63         uint64_t log_addr;
64 };
65
66 /**
67  * Structure contains variables relevant to RX/TX virtqueues.
68  */
69 struct vhost_virtqueue {
70         struct vring_desc       *desc;
71         struct vring_avail      *avail;
72         struct vring_used       *used;
73         uint32_t                size;
74
75         uint16_t                last_avail_idx;
76         uint16_t                last_used_idx;
77         /* Last used index we notify to front end. */
78         uint16_t                signalled_used;
79 #define VIRTIO_INVALID_EVENTFD          (-1)
80 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
81
82         /* Backend value to determine if device should started/stopped */
83         int                     backend;
84         int                     enabled;
85         int                     access_ok;
86         rte_spinlock_t          access_lock;
87
88         /* Used to notify the guest (trigger interrupt) */
89         int                     callfd;
90         /* Currently unused as polling mode is enabled */
91         int                     kickfd;
92
93         /* Physical address of used ring, for logging */
94         uint64_t                log_guest_addr;
95
96         uint16_t                nr_zmbuf;
97         uint16_t                zmbuf_size;
98         uint16_t                last_zmbuf_idx;
99         struct zcopy_mbuf       *zmbufs;
100         struct zcopy_mbuf_list  zmbuf_list;
101
102         struct vring_used_elem  *shadow_used_ring;
103         uint16_t                shadow_used_idx;
104         struct vhost_vring_addr ring_addrs;
105
106         struct batch_copy_elem  *batch_copy_elems;
107         uint16_t                batch_copy_nb_elems;
108
109         rte_rwlock_t    iotlb_lock;
110         rte_rwlock_t    iotlb_pending_lock;
111         struct rte_mempool *iotlb_pool;
112         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
113         int                             iotlb_cache_nr;
114         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
115 } __rte_cache_aligned;
116
117 /* Old kernels have no such macros defined */
118 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
119  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
120 #endif
121
122 #ifndef VIRTIO_NET_F_MQ
123  #define VIRTIO_NET_F_MQ                22
124 #endif
125
126 #define VHOST_MAX_VRING                 0x100
127 #define VHOST_MAX_QUEUE_PAIRS           0x80
128
129 #ifndef VIRTIO_NET_F_MTU
130  #define VIRTIO_NET_F_MTU 3
131 #endif
132
133 #ifndef VIRTIO_F_ANY_LAYOUT
134  #define VIRTIO_F_ANY_LAYOUT            27
135 #endif
136
137 /* Declare IOMMU related bits for older kernels */
138 #ifndef VIRTIO_F_IOMMU_PLATFORM
139
140 #define VIRTIO_F_IOMMU_PLATFORM 33
141
142 struct vhost_iotlb_msg {
143         __u64 iova;
144         __u64 size;
145         __u64 uaddr;
146 #define VHOST_ACCESS_RO      0x1
147 #define VHOST_ACCESS_WO      0x2
148 #define VHOST_ACCESS_RW      0x3
149         __u8 perm;
150 #define VHOST_IOTLB_MISS           1
151 #define VHOST_IOTLB_UPDATE         2
152 #define VHOST_IOTLB_INVALIDATE     3
153 #define VHOST_IOTLB_ACCESS_FAIL    4
154         __u8 type;
155 };
156
157 #define VHOST_IOTLB_MSG 0x1
158
159 struct vhost_msg {
160         int type;
161         union {
162                 struct vhost_iotlb_msg iotlb;
163                 __u8 padding[64];
164         };
165 };
166 #endif
167
168 /*
169  * Define virtio 1.0 for older kernels
170  */
171 #ifndef VIRTIO_F_VERSION_1
172  #define VIRTIO_F_VERSION_1 32
173 #endif
174
175 #define VHOST_USER_F_PROTOCOL_FEATURES  30
176
177 /* Features supported by this builtin vhost-user net driver. */
178 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
179                                 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
180                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
181                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
182                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
183                                 (1ULL << VIRTIO_NET_F_MQ)      | \
184                                 (1ULL << VIRTIO_F_VERSION_1)   | \
185                                 (1ULL << VHOST_F_LOG_ALL)      | \
186                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
187                                 (1ULL << VIRTIO_NET_F_GSO) | \
188                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
189                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
190                                 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
191                                 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
192                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
193                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
194                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
195                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
196                                 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
197                                 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
198                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
199                                 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
200                                 (1ULL << VIRTIO_NET_F_MTU) | \
201                                 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
202
203
204 struct guest_page {
205         uint64_t guest_phys_addr;
206         uint64_t host_phys_addr;
207         uint64_t size;
208 };
209
210 /**
211  * Device structure contains all configuration information relating
212  * to the device.
213  */
214 struct virtio_net {
215         /* Frontend (QEMU) memory and memory region information */
216         struct rte_vhost_memory *mem;
217         uint64_t                features;
218         uint64_t                protocol_features;
219         int                     vid;
220         uint32_t                flags;
221         uint16_t                vhost_hlen;
222         /* to tell if we need broadcast rarp packet */
223         rte_atomic16_t          broadcast_rarp;
224         uint32_t                nr_vring;
225         int                     dequeue_zero_copy;
226         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
227 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
228         char                    ifname[IF_NAME_SZ];
229         uint64_t                log_size;
230         uint64_t                log_base;
231         uint64_t                log_addr;
232         struct ether_addr       mac;
233         uint16_t                mtu;
234
235         struct vhost_device_ops const *notify_ops;
236
237         uint32_t                nr_guest_pages;
238         uint32_t                max_guest_pages;
239         struct guest_page       *guest_pages;
240
241         int                     slave_req_fd;
242 } __rte_cache_aligned;
243
244
245 #define VHOST_LOG_PAGE  4096
246
247 /*
248  * Atomically set a bit in memory.
249  */
250 static __rte_always_inline void
251 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
252 {
253         __sync_fetch_and_or_8(addr, (1U << nr));
254 }
255
256 static __rte_always_inline void
257 vhost_log_page(uint8_t *log_base, uint64_t page)
258 {
259         vhost_set_bit(page % 8, &log_base[page / 8]);
260 }
261
262 static __rte_always_inline void
263 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
264 {
265         uint64_t page;
266
267         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
268                    !dev->log_base || !len))
269                 return;
270
271         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
272                 return;
273
274         /* To make sure guest memory updates are committed before logging */
275         rte_smp_wmb();
276
277         page = addr / VHOST_LOG_PAGE;
278         while (page * VHOST_LOG_PAGE < addr + len) {
279                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
280                 page += 1;
281         }
282 }
283
284 static __rte_always_inline void
285 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
286                      uint64_t offset, uint64_t len)
287 {
288         vhost_log_write(dev, vq->log_guest_addr + offset, len);
289 }
290
291 /* Macros for printing using RTE_LOG */
292 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
293 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
294
295 #ifdef RTE_LIBRTE_VHOST_DEBUG
296 #define VHOST_MAX_PRINT_BUFF 6072
297 #define LOG_LEVEL RTE_LOG_DEBUG
298 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
299 #define PRINT_PACKET(device, addr, size, header) do { \
300         char *pkt_addr = (char *)(addr); \
301         unsigned int index; \
302         char packet[VHOST_MAX_PRINT_BUFF]; \
303         \
304         if ((header)) \
305                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
306         else \
307                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
308         for (index = 0; index < (size); index++) { \
309                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
310                         "%02hhx ", pkt_addr[index]); \
311         } \
312         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
313         \
314         LOG_DEBUG(VHOST_DATA, "%s", packet); \
315 } while (0)
316 #else
317 #define LOG_LEVEL RTE_LOG_INFO
318 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
319 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
320 #endif
321
322 extern uint64_t VHOST_FEATURES;
323 #define MAX_VHOST_DEVICE        1024
324 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
325
326 /* Convert guest physical address to host physical address */
327 static __rte_always_inline rte_iova_t
328 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
329 {
330         uint32_t i;
331         struct guest_page *page;
332
333         for (i = 0; i < dev->nr_guest_pages; i++) {
334                 page = &dev->guest_pages[i];
335
336                 if (gpa >= page->guest_phys_addr &&
337                     gpa + size < page->guest_phys_addr + page->size) {
338                         return gpa - page->guest_phys_addr +
339                                page->host_phys_addr;
340                 }
341         }
342
343         return 0;
344 }
345
346 struct virtio_net *get_device(int vid);
347
348 int vhost_new_device(void);
349 void cleanup_device(struct virtio_net *dev, int destroy);
350 void reset_device(struct virtio_net *dev);
351 void vhost_destroy_device(int);
352
353 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
354 void free_vq(struct vhost_virtqueue *vq);
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 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
371                         uint64_t iova, uint64_t size, uint8_t perm);
372 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
373 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
374
375 static __rte_always_inline uint64_t
376 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
377                         uint64_t iova, uint64_t size, uint8_t perm)
378 {
379         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
380                 return rte_vhost_gpa_to_vva(dev->mem, iova);
381
382         return __vhost_iova_to_vva(dev, vq, iova, size, perm);
383 }
384
385 #define vhost_used_event(vr) \
386         (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
387
388 /*
389  * The following is used with VIRTIO_RING_F_EVENT_IDX.
390  * Assuming a given event_idx value from the other size, if we have
391  * just incremented index from old to new_idx, should we trigger an
392  * event?
393  */
394 static __rte_always_inline int
395 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
396 {
397         return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
398 }
399
400 static __rte_always_inline void
401 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
402 {
403         /* Flush used->idx update before we read avail->flags. */
404         rte_mb();
405
406         /* Don't kick guest if we don't reach index specified by guest. */
407         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
408                 uint16_t old = vq->signalled_used;
409                 uint16_t new = vq->last_used_idx;
410
411                 LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
412                         __func__,
413                         vhost_used_event(vq),
414                         old, new);
415                 if (vhost_need_event(vhost_used_event(vq), new, old)
416                         && (vq->callfd >= 0)) {
417                         vq->signalled_used = vq->last_used_idx;
418                         eventfd_write(vq->callfd, (eventfd_t) 1);
419                 }
420         } else {
421                 /* Kick the guest if necessary. */
422                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
423                                 && (vq->callfd >= 0))
424                         eventfd_write(vq->callfd, (eventfd_t)1);
425         }
426 }
427
428 #endif /* _VHOST_NET_CDEV_H_ */