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