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