vhost: export vhost feature definitions
[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 /* Features supported by this builtin vhost-user net driver. */
179 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
180                                 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
181                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
182                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
183                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
184                                 (1ULL << VIRTIO_NET_F_MQ)      | \
185                                 (1ULL << VIRTIO_F_VERSION_1)   | \
186                                 (1ULL << VHOST_F_LOG_ALL)      | \
187                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
188                                 (1ULL << VIRTIO_NET_F_GSO) | \
189                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
190                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
191                                 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
192                                 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
193                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
194                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
195                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
196                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
197                                 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
198                                 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
199                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
200                                 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
201                                 (1ULL << VIRTIO_NET_F_MTU) | \
202                                 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
203
204
205 struct guest_page {
206         uint64_t guest_phys_addr;
207         uint64_t host_phys_addr;
208         uint64_t size;
209 };
210
211 /**
212  * Device structure contains all configuration information relating
213  * to the device.
214  */
215 struct virtio_net {
216         /* Frontend (QEMU) memory and memory region information */
217         struct rte_vhost_memory *mem;
218         uint64_t                features;
219         uint64_t                protocol_features;
220         int                     vid;
221         uint32_t                flags;
222         uint16_t                vhost_hlen;
223         /* to tell if we need broadcast rarp packet */
224         rte_atomic16_t          broadcast_rarp;
225         uint32_t                nr_vring;
226         int                     dequeue_zero_copy;
227         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
228 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
229         char                    ifname[IF_NAME_SZ];
230         uint64_t                log_size;
231         uint64_t                log_base;
232         uint64_t                log_addr;
233         struct ether_addr       mac;
234         uint16_t                mtu;
235
236         struct vhost_device_ops const *notify_ops;
237
238         uint32_t                nr_guest_pages;
239         uint32_t                max_guest_pages;
240         struct guest_page       *guest_pages;
241
242         int                     slave_req_fd;
243 } __rte_cache_aligned;
244
245
246 #define VHOST_LOG_PAGE  4096
247
248 /*
249  * Mark all pages belonging to the same dirty log bitmap byte
250  * as dirty. The goal is to avoid concurrency between different
251  * threads doing atomic read-modify-writes on the same byte.
252  */
253 static __rte_always_inline void
254 vhost_log_page(uint8_t *log_base, uint64_t page)
255 {
256         log_base[page / 8] = 0xff;
257 }
258
259 static __rte_always_inline void
260 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
261 {
262         uint64_t page;
263
264         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
265                    !dev->log_base || !len))
266                 return;
267
268         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
269                 return;
270
271         /* To make sure guest memory updates are committed before logging */
272         rte_smp_wmb();
273
274         page = addr / VHOST_LOG_PAGE;
275         while (page * VHOST_LOG_PAGE < addr + len) {
276                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
277                 page += 1;
278         }
279 }
280
281 static __rte_always_inline void
282 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
283                      uint64_t offset, uint64_t len)
284 {
285         vhost_log_write(dev, vq->log_guest_addr + offset, len);
286 }
287
288 /* Macros for printing using RTE_LOG */
289 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
290 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
291
292 #ifdef RTE_LIBRTE_VHOST_DEBUG
293 #define VHOST_MAX_PRINT_BUFF 6072
294 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
295         RTE_LOG(DEBUG, log_type, fmt, ##args)
296 #define PRINT_PACKET(device, addr, size, header) do { \
297         char *pkt_addr = (char *)(addr); \
298         unsigned int index; \
299         char packet[VHOST_MAX_PRINT_BUFF]; \
300         \
301         if ((header)) \
302                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
303         else \
304                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
305         for (index = 0; index < (size); index++) { \
306                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
307                         "%02hhx ", pkt_addr[index]); \
308         } \
309         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
310         \
311         VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
312 } while (0)
313 #else
314 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
315 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
316 #endif
317
318 extern uint64_t VHOST_FEATURES;
319 #define MAX_VHOST_DEVICE        1024
320 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
321
322 /* Convert guest physical address to host physical address */
323 static __rte_always_inline rte_iova_t
324 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
325 {
326         uint32_t i;
327         struct guest_page *page;
328
329         for (i = 0; i < dev->nr_guest_pages; i++) {
330                 page = &dev->guest_pages[i];
331
332                 if (gpa >= page->guest_phys_addr &&
333                     gpa + size < page->guest_phys_addr + page->size) {
334                         return gpa - page->guest_phys_addr +
335                                page->host_phys_addr;
336                 }
337         }
338
339         return 0;
340 }
341
342 static __rte_always_inline struct virtio_net *
343 get_device(int vid)
344 {
345         struct virtio_net *dev = vhost_devices[vid];
346
347         if (unlikely(!dev)) {
348                 RTE_LOG(ERR, VHOST_CONFIG,
349                         "(%d) device not found.\n", vid);
350         }
351
352         return dev;
353 }
354
355 int vhost_new_device(void);
356 void cleanup_device(struct virtio_net *dev, int destroy);
357 void reset_device(struct virtio_net *dev);
358 void vhost_destroy_device(int);
359
360 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
361 void free_vq(struct vhost_virtqueue *vq);
362
363 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
364
365 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
366 void vhost_enable_dequeue_zero_copy(int vid);
367 void vhost_set_builtin_virtio_net(int vid, bool enable);
368
369 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
370
371 /*
372  * Backend-specific cleanup.
373  *
374  * TODO: fix it; we have one backend now
375  */
376 void vhost_backend_cleanup(struct virtio_net *dev);
377
378 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
379                         uint64_t iova, uint64_t size, uint8_t perm);
380 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
381 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
382
383 static __rte_always_inline uint64_t
384 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
385                         uint64_t iova, uint64_t size, uint8_t perm)
386 {
387         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
388                 return rte_vhost_gpa_to_vva(dev->mem, iova);
389
390         return __vhost_iova_to_vva(dev, vq, iova, size, perm);
391 }
392
393 #define vhost_used_event(vr) \
394         (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
395
396 /*
397  * The following is used with VIRTIO_RING_F_EVENT_IDX.
398  * Assuming a given event_idx value from the other size, if we have
399  * just incremented index from old to new_idx, should we trigger an
400  * event?
401  */
402 static __rte_always_inline int
403 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
404 {
405         return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
406 }
407
408 static __rte_always_inline void
409 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
410 {
411         /* Flush used->idx update before we read avail->flags. */
412         rte_mb();
413
414         /* Don't kick guest if we don't reach index specified by guest. */
415         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
416                 uint16_t old = vq->signalled_used;
417                 uint16_t new = vq->last_used_idx;
418
419                 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
420                         __func__,
421                         vhost_used_event(vq),
422                         old, new);
423                 if (vhost_need_event(vhost_used_event(vq), new, old)
424                         && (vq->callfd >= 0)) {
425                         vq->signalled_used = vq->last_used_idx;
426                         eventfd_write(vq->callfd, (eventfd_t) 1);
427                 }
428         } else {
429                 /* Kick the guest if necessary. */
430                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
431                                 && (vq->callfd >= 0))
432                         eventfd_write(vq->callfd, (eventfd_t)1);
433         }
434 }
435
436 #endif /* _VHOST_NET_CDEV_H_ */