vhost: protect active rings from async ring changes
[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 /* Declare IOMMU related bits for older kernels */
134 #ifndef VIRTIO_F_IOMMU_PLATFORM
135
136 #define VIRTIO_F_IOMMU_PLATFORM 33
137
138 struct vhost_iotlb_msg {
139         __u64 iova;
140         __u64 size;
141         __u64 uaddr;
142 #define VHOST_ACCESS_RO      0x1
143 #define VHOST_ACCESS_WO      0x2
144 #define VHOST_ACCESS_RW      0x3
145         __u8 perm;
146 #define VHOST_IOTLB_MISS           1
147 #define VHOST_IOTLB_UPDATE         2
148 #define VHOST_IOTLB_INVALIDATE     3
149 #define VHOST_IOTLB_ACCESS_FAIL    4
150         __u8 type;
151 };
152
153 #define VHOST_IOTLB_MSG 0x1
154
155 struct vhost_msg {
156         int type;
157         union {
158                 struct vhost_iotlb_msg iotlb;
159                 __u8 padding[64];
160         };
161 };
162 #endif
163
164 /*
165  * Define virtio 1.0 for older kernels
166  */
167 #ifndef VIRTIO_F_VERSION_1
168  #define VIRTIO_F_VERSION_1 32
169 #endif
170
171 #define VHOST_USER_F_PROTOCOL_FEATURES  30
172
173 /* Features supported by this builtin vhost-user net driver. */
174 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
175                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
176                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
177                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
178                                 (1ULL << VIRTIO_NET_F_MQ)      | \
179                                 (1ULL << VIRTIO_F_VERSION_1)   | \
180                                 (1ULL << VHOST_F_LOG_ALL)      | \
181                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
182                                 (1ULL << VIRTIO_NET_F_GSO) | \
183                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
184                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
185                                 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
186                                 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
187                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
188                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
189                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
190                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
191                                 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
192                                 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
193                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
194                                 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
195                                 (1ULL << VIRTIO_NET_F_MTU) | \
196                                 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
197
198
199 struct guest_page {
200         uint64_t guest_phys_addr;
201         uint64_t host_phys_addr;
202         uint64_t size;
203 };
204
205 /**
206  * Device structure contains all configuration information relating
207  * to the device.
208  */
209 struct virtio_net {
210         /* Frontend (QEMU) memory and memory region information */
211         struct rte_vhost_memory *mem;
212         uint64_t                features;
213         uint64_t                protocol_features;
214         int                     vid;
215         uint32_t                flags;
216         uint16_t                vhost_hlen;
217         /* to tell if we need broadcast rarp packet */
218         rte_atomic16_t          broadcast_rarp;
219         uint32_t                nr_vring;
220         int                     dequeue_zero_copy;
221         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
222 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
223         char                    ifname[IF_NAME_SZ];
224         uint64_t                log_size;
225         uint64_t                log_base;
226         uint64_t                log_addr;
227         struct ether_addr       mac;
228         uint16_t                mtu;
229
230         struct vhost_device_ops const *notify_ops;
231
232         uint32_t                nr_guest_pages;
233         uint32_t                max_guest_pages;
234         struct guest_page       *guest_pages;
235
236         int                     slave_req_fd;
237 } __rte_cache_aligned;
238
239
240 #define VHOST_LOG_PAGE  4096
241
242 /*
243  * Atomically set a bit in memory.
244  */
245 static __rte_always_inline void
246 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
247 {
248         __sync_fetch_and_or_8(addr, (1U << nr));
249 }
250
251 static __rte_always_inline void
252 vhost_log_page(uint8_t *log_base, uint64_t page)
253 {
254         vhost_set_bit(page % 8, &log_base[page / 8]);
255 }
256
257 static __rte_always_inline void
258 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
259 {
260         uint64_t page;
261
262         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
263                    !dev->log_base || !len))
264                 return;
265
266         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
267                 return;
268
269         /* To make sure guest memory updates are committed before logging */
270         rte_smp_wmb();
271
272         page = addr / VHOST_LOG_PAGE;
273         while (page * VHOST_LOG_PAGE < addr + len) {
274                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
275                 page += 1;
276         }
277 }
278
279 static __rte_always_inline void
280 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
281                      uint64_t offset, uint64_t len)
282 {
283         vhost_log_write(dev, vq->log_guest_addr + offset, len);
284 }
285
286 /* Macros for printing using RTE_LOG */
287 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
288 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
289
290 #ifdef RTE_LIBRTE_VHOST_DEBUG
291 #define VHOST_MAX_PRINT_BUFF 6072
292 #define LOG_LEVEL RTE_LOG_DEBUG
293 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
294 #define PRINT_PACKET(device, addr, size, header) do { \
295         char *pkt_addr = (char *)(addr); \
296         unsigned int index; \
297         char packet[VHOST_MAX_PRINT_BUFF]; \
298         \
299         if ((header)) \
300                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
301         else \
302                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
303         for (index = 0; index < (size); index++) { \
304                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
305                         "%02hhx ", pkt_addr[index]); \
306         } \
307         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
308         \
309         LOG_DEBUG(VHOST_DATA, "%s", packet); \
310 } while (0)
311 #else
312 #define LOG_LEVEL RTE_LOG_INFO
313 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
314 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
315 #endif
316
317 extern uint64_t VHOST_FEATURES;
318 #define MAX_VHOST_DEVICE        1024
319 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
320
321 /* Convert guest physical address to host physical address */
322 static __rte_always_inline rte_iova_t
323 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
324 {
325         uint32_t i;
326         struct guest_page *page;
327
328         for (i = 0; i < dev->nr_guest_pages; i++) {
329                 page = &dev->guest_pages[i];
330
331                 if (gpa >= page->guest_phys_addr &&
332                     gpa + size < page->guest_phys_addr + page->size) {
333                         return gpa - page->guest_phys_addr +
334                                page->host_phys_addr;
335                 }
336         }
337
338         return 0;
339 }
340
341 struct virtio_net *get_device(int vid);
342
343 int vhost_new_device(void);
344 void cleanup_device(struct virtio_net *dev, int destroy);
345 void reset_device(struct virtio_net *dev);
346 void vhost_destroy_device(int);
347
348 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
349 void free_vq(struct vhost_virtqueue *vq);
350
351 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
352
353 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
354 void vhost_enable_dequeue_zero_copy(int vid);
355
356 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
357
358 /*
359  * Backend-specific cleanup.
360  *
361  * TODO: fix it; we have one backend now
362  */
363 void vhost_backend_cleanup(struct virtio_net *dev);
364
365 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
366                         uint64_t iova, uint64_t size, uint8_t perm);
367 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
368 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
369
370 static __rte_always_inline uint64_t
371 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
372                         uint64_t iova, uint64_t size, uint8_t perm)
373 {
374         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
375                 return rte_vhost_gpa_to_vva(dev->mem, iova);
376
377         return __vhost_iova_to_vva(dev, vq, iova, size, perm);
378 }
379
380 #define vhost_used_event(vr) \
381         (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
382
383 /*
384  * The following is used with VIRTIO_RING_F_EVENT_IDX.
385  * Assuming a given event_idx value from the other size, if we have
386  * just incremented index from old to new_idx, should we trigger an
387  * event?
388  */
389 static __rte_always_inline int
390 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
391 {
392         return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
393 }
394
395 static __rte_always_inline void
396 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
397 {
398         /* Flush used->idx update before we read avail->flags. */
399         rte_mb();
400
401         /* Don't kick guest if we don't reach index specified by guest. */
402         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
403                 uint16_t old = vq->signalled_used;
404                 uint16_t new = vq->last_used_idx;
405
406                 LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
407                         __func__,
408                         vhost_used_event(vq),
409                         old, new);
410                 if (vhost_need_event(vhost_used_event(vq), new, old)
411                         && (vq->callfd >= 0)) {
412                         vq->signalled_used = vq->last_used_idx;
413                         eventfd_write(vq->callfd, (eventfd_t) 1);
414                 }
415         } else {
416                 /* Kick the guest if necessary. */
417                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
418                                 && (vq->callfd >= 0))
419                         eventfd_write(vq->callfd, (eventfd_t)1);
420         }
421 }
422
423 #endif /* _VHOST_NET_CDEV_H_ */