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