vhost: remove unused log constant
[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_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
300 #define PRINT_PACKET(device, addr, size, header) do { \
301         char *pkt_addr = (char *)(addr); \
302         unsigned int index; \
303         char packet[VHOST_MAX_PRINT_BUFF]; \
304         \
305         if ((header)) \
306                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
307         else \
308                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
309         for (index = 0; index < (size); index++) { \
310                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
311                         "%02hhx ", pkt_addr[index]); \
312         } \
313         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
314         \
315         LOG_DEBUG(VHOST_DATA, "%s", packet); \
316 } while (0)
317 #else
318 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
319 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
320 #endif
321
322 extern uint64_t VHOST_FEATURES;
323 #define MAX_VHOST_DEVICE        1024
324 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
325
326 /* Convert guest physical address to host physical address */
327 static __rte_always_inline rte_iova_t
328 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
329 {
330         uint32_t i;
331         struct guest_page *page;
332
333         for (i = 0; i < dev->nr_guest_pages; i++) {
334                 page = &dev->guest_pages[i];
335
336                 if (gpa >= page->guest_phys_addr &&
337                     gpa + size < page->guest_phys_addr + page->size) {
338                         return gpa - page->guest_phys_addr +
339                                page->host_phys_addr;
340                 }
341         }
342
343         return 0;
344 }
345
346 struct virtio_net *get_device(int vid);
347
348 int vhost_new_device(void);
349 void cleanup_device(struct virtio_net *dev, int destroy);
350 void reset_device(struct virtio_net *dev);
351 void vhost_destroy_device(int);
352
353 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
354 void free_vq(struct vhost_virtqueue *vq);
355
356 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
357
358 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
359 void vhost_enable_dequeue_zero_copy(int vid);
360 void vhost_set_builtin_virtio_net(int vid, bool enable);
361
362 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
363
364 /*
365  * Backend-specific cleanup.
366  *
367  * TODO: fix it; we have one backend now
368  */
369 void vhost_backend_cleanup(struct virtio_net *dev);
370
371 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
372                         uint64_t iova, uint64_t size, uint8_t perm);
373 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
374 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
375
376 static __rte_always_inline uint64_t
377 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
378                         uint64_t iova, uint64_t size, uint8_t perm)
379 {
380         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
381                 return rte_vhost_gpa_to_vva(dev->mem, iova);
382
383         return __vhost_iova_to_vva(dev, vq, iova, size, perm);
384 }
385
386 #define vhost_used_event(vr) \
387         (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
388
389 /*
390  * The following is used with VIRTIO_RING_F_EVENT_IDX.
391  * Assuming a given event_idx value from the other size, if we have
392  * just incremented index from old to new_idx, should we trigger an
393  * event?
394  */
395 static __rte_always_inline int
396 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
397 {
398         return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
399 }
400
401 static __rte_always_inline void
402 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
403 {
404         /* Flush used->idx update before we read avail->flags. */
405         rte_mb();
406
407         /* Don't kick guest if we don't reach index specified by guest. */
408         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
409                 uint16_t old = vq->signalled_used;
410                 uint16_t new = vq->last_used_idx;
411
412                 LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
413                         __func__,
414                         vhost_used_event(vq),
415                         old, new);
416                 if (vhost_need_event(vhost_used_event(vq), new, old)
417                         && (vq->callfd >= 0)) {
418                         vq->signalled_used = vq->last_used_idx;
419                         eventfd_write(vq->callfd, (eventfd_t) 1);
420                 }
421         } else {
422                 /* Kick the guest if necessary. */
423                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
424                                 && (vq->callfd >= 0))
425                         eventfd_write(vq->callfd, (eventfd_t)1);
426         }
427 }
428
429 #endif /* _VHOST_NET_CDEV_H_ */