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