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