New upstream version 18.02
[deb_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 /* Used to indicate that the built-in vhost net device backend is enabled */
28 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
29
30 /* Backend value set by guest. */
31 #define VIRTIO_DEV_STOPPED -1
32
33 #define BUF_VECTOR_MAX 256
34
35 /**
36  * Structure contains buffer address, length and descriptor index
37  * from vring to do scatter RX.
38  */
39 struct buf_vector {
40         uint64_t buf_addr;
41         uint32_t buf_len;
42         uint32_t desc_idx;
43 };
44
45 /*
46  * A structure to hold some fields needed in zero copy code path,
47  * mainly for associating an mbuf with the right desc_idx.
48  */
49 struct zcopy_mbuf {
50         struct rte_mbuf *mbuf;
51         uint32_t desc_idx;
52         uint16_t in_use;
53
54         TAILQ_ENTRY(zcopy_mbuf) next;
55 };
56 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
57
58 /*
59  * Structure contains the info for each batched memory copy.
60  */
61 struct batch_copy_elem {
62         void *dst;
63         void *src;
64         uint32_t len;
65         uint64_t log_addr;
66 };
67
68 /**
69  * Structure contains variables relevant to RX/TX virtqueues.
70  */
71 struct vhost_virtqueue {
72         struct vring_desc       *desc;
73         struct vring_avail      *avail;
74         struct vring_used       *used;
75         uint32_t                size;
76
77         uint16_t                last_avail_idx;
78         uint16_t                last_used_idx;
79         /* Last used index we notify to front end. */
80         uint16_t                signalled_used;
81 #define VIRTIO_INVALID_EVENTFD          (-1)
82 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
83
84         /* Backend value to determine if device should started/stopped */
85         int                     backend;
86         int                     enabled;
87         int                     access_ok;
88         rte_spinlock_t          access_lock;
89
90         /* Used to notify the guest (trigger interrupt) */
91         int                     callfd;
92         /* Currently unused as polling mode is enabled */
93         int                     kickfd;
94
95         /* Physical address of used ring, for logging */
96         uint64_t                log_guest_addr;
97
98         uint16_t                nr_zmbuf;
99         uint16_t                zmbuf_size;
100         uint16_t                last_zmbuf_idx;
101         struct zcopy_mbuf       *zmbufs;
102         struct zcopy_mbuf_list  zmbuf_list;
103
104         struct vring_used_elem  *shadow_used_ring;
105         uint16_t                shadow_used_idx;
106         struct vhost_vring_addr ring_addrs;
107
108         struct batch_copy_elem  *batch_copy_elems;
109         uint16_t                batch_copy_nb_elems;
110
111         rte_rwlock_t    iotlb_lock;
112         rte_rwlock_t    iotlb_pending_lock;
113         struct rte_mempool *iotlb_pool;
114         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
115         int                             iotlb_cache_nr;
116         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
117 } __rte_cache_aligned;
118
119 /* Old kernels have no such macros defined */
120 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
121  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
122 #endif
123
124 #ifndef VIRTIO_NET_F_MQ
125  #define VIRTIO_NET_F_MQ                22
126 #endif
127
128 #define VHOST_MAX_VRING                 0x100
129 #define VHOST_MAX_QUEUE_PAIRS           0x80
130
131 #ifndef VIRTIO_NET_F_MTU
132  #define VIRTIO_NET_F_MTU 3
133 #endif
134
135 #ifndef VIRTIO_F_ANY_LAYOUT
136  #define VIRTIO_F_ANY_LAYOUT            27
137 #endif
138
139 /* Declare IOMMU related bits for older kernels */
140 #ifndef VIRTIO_F_IOMMU_PLATFORM
141
142 #define VIRTIO_F_IOMMU_PLATFORM 33
143
144 struct vhost_iotlb_msg {
145         __u64 iova;
146         __u64 size;
147         __u64 uaddr;
148 #define VHOST_ACCESS_RO      0x1
149 #define VHOST_ACCESS_WO      0x2
150 #define VHOST_ACCESS_RW      0x3
151         __u8 perm;
152 #define VHOST_IOTLB_MISS           1
153 #define VHOST_IOTLB_UPDATE         2
154 #define VHOST_IOTLB_INVALIDATE     3
155 #define VHOST_IOTLB_ACCESS_FAIL    4
156         __u8 type;
157 };
158
159 #define VHOST_IOTLB_MSG 0x1
160
161 struct vhost_msg {
162         int type;
163         union {
164                 struct vhost_iotlb_msg iotlb;
165                 __u8 padding[64];
166         };
167 };
168 #endif
169
170 /*
171  * Define virtio 1.0 for older kernels
172  */
173 #ifndef VIRTIO_F_VERSION_1
174  #define VIRTIO_F_VERSION_1 32
175 #endif
176
177 #define VHOST_USER_F_PROTOCOL_FEATURES  30
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 } __rte_cache_aligned;
245
246
247 #define VHOST_LOG_PAGE  4096
248
249 /*
250  * Atomically set a bit in memory.
251  */
252 static __rte_always_inline void
253 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
254 {
255         __sync_fetch_and_or_8(addr, (1U << nr));
256 }
257
258 static __rte_always_inline void
259 vhost_log_page(uint8_t *log_base, uint64_t page)
260 {
261         vhost_set_bit(page % 8, &log_base[page / 8]);
262 }
263
264 static __rte_always_inline void
265 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
266 {
267         uint64_t page;
268
269         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
270                    !dev->log_base || !len))
271                 return;
272
273         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
274                 return;
275
276         /* To make sure guest memory updates are committed before logging */
277         rte_smp_wmb();
278
279         page = addr / VHOST_LOG_PAGE;
280         while (page * VHOST_LOG_PAGE < addr + len) {
281                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
282                 page += 1;
283         }
284 }
285
286 static __rte_always_inline void
287 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
288                      uint64_t offset, uint64_t len)
289 {
290         vhost_log_write(dev, vq->log_guest_addr + offset, len);
291 }
292
293 /* Macros for printing using RTE_LOG */
294 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
295 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
296
297 #ifdef RTE_LIBRTE_VHOST_DEBUG
298 #define VHOST_MAX_PRINT_BUFF 6072
299 #define LOG_LEVEL RTE_LOG_DEBUG
300 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
301 #define PRINT_PACKET(device, addr, size, header) do { \
302         char *pkt_addr = (char *)(addr); \
303         unsigned int index; \
304         char packet[VHOST_MAX_PRINT_BUFF]; \
305         \
306         if ((header)) \
307                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
308         else \
309                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
310         for (index = 0; index < (size); index++) { \
311                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
312                         "%02hhx ", pkt_addr[index]); \
313         } \
314         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
315         \
316         LOG_DEBUG(VHOST_DATA, "%s", packet); \
317 } while (0)
318 #else
319 #define LOG_LEVEL RTE_LOG_INFO
320 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
321 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
322 #endif
323
324 extern uint64_t VHOST_FEATURES;
325 #define MAX_VHOST_DEVICE        1024
326 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
327
328 /* Convert guest physical address to host physical address */
329 static __rte_always_inline rte_iova_t
330 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
331 {
332         uint32_t i;
333         struct guest_page *page;
334
335         for (i = 0; i < dev->nr_guest_pages; i++) {
336                 page = &dev->guest_pages[i];
337
338                 if (gpa >= page->guest_phys_addr &&
339                     gpa + size < page->guest_phys_addr + page->size) {
340                         return gpa - page->guest_phys_addr +
341                                page->host_phys_addr;
342                 }
343         }
344
345         return 0;
346 }
347
348 struct virtio_net *get_device(int vid);
349
350 int vhost_new_device(void);
351 void cleanup_device(struct virtio_net *dev, int destroy);
352 void reset_device(struct virtio_net *dev);
353 void vhost_destroy_device(int);
354
355 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
356 void free_vq(struct vhost_virtqueue *vq);
357
358 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
359
360 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
361 void vhost_enable_dequeue_zero_copy(int vid);
362 void vhost_set_builtin_virtio_net(int vid, bool enable);
363
364 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
365
366 /*
367  * Backend-specific cleanup.
368  *
369  * TODO: fix it; we have one backend now
370  */
371 void vhost_backend_cleanup(struct virtio_net *dev);
372
373 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
374                         uint64_t iova, uint64_t size, uint8_t perm);
375 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
376 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
377
378 static __rte_always_inline uint64_t
379 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
380                         uint64_t iova, uint64_t size, uint8_t perm)
381 {
382         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
383                 return rte_vhost_gpa_to_vva(dev->mem, iova);
384
385         return __vhost_iova_to_vva(dev, vq, iova, size, perm);
386 }
387
388 #define vhost_used_event(vr) \
389         (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
390
391 /*
392  * The following is used with VIRTIO_RING_F_EVENT_IDX.
393  * Assuming a given event_idx value from the other size, if we have
394  * just incremented index from old to new_idx, should we trigger an
395  * event?
396  */
397 static __rte_always_inline int
398 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
399 {
400         return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
401 }
402
403 static __rte_always_inline void
404 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
405 {
406         /* Flush used->idx update before we read avail->flags. */
407         rte_mb();
408
409         /* Don't kick guest if we don't reach index specified by guest. */
410         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
411                 uint16_t old = vq->signalled_used;
412                 uint16_t new = vq->last_used_idx;
413
414                 LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
415                         __func__,
416                         vhost_used_event(vq),
417                         old, new);
418                 if (vhost_need_event(vhost_used_event(vq), new, old)
419                         && (vq->callfd >= 0)) {
420                         vq->signalled_used = vq->last_used_idx;
421                         eventfd_write(vq->callfd, (eventfd_t) 1);
422                 }
423         } else {
424                 /* Kick the guest if necessary. */
425                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
426                                 && (vq->callfd >= 0))
427                         eventfd_write(vq->callfd, (eventfd_t)1);
428         }
429 }
430
431 #endif /* _VHOST_NET_CDEV_H_ */