vhost: advertise support in-order feature
[dpdk.git] / lib / librte_vhost / vhost.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 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 <stdbool.h>
10 #include <sys/types.h>
11 #include <sys/queue.h>
12 #include <unistd.h>
13 #include <linux/vhost.h>
14 #include <linux/virtio_net.h>
15 #include <sys/socket.h>
16 #include <linux/if.h>
17
18 #include <rte_log.h>
19 #include <rte_ether.h>
20 #include <rte_rwlock.h>
21
22 #include "rte_vhost.h"
23 #include "rte_vdpa.h"
24
25 /* Used to indicate that the device is running on a data core */
26 #define VIRTIO_DEV_RUNNING 1
27 /* Used to indicate that the device is ready to operate */
28 #define VIRTIO_DEV_READY 2
29 /* Used to indicate that the built-in vhost net device backend is enabled */
30 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
31 /* Used to indicate that the device has its own data path and configured */
32 #define VIRTIO_DEV_VDPA_CONFIGURED 8
33
34 /* Backend value set by guest. */
35 #define VIRTIO_DEV_STOPPED -1
36
37 #define BUF_VECTOR_MAX 256
38
39 #define VHOST_LOG_CACHE_NR 32
40
41 /**
42  * Structure contains buffer address, length and descriptor index
43  * from vring to do scatter RX.
44  */
45 struct buf_vector {
46         uint64_t buf_addr;
47         uint32_t buf_len;
48         uint32_t desc_idx;
49 };
50
51 /*
52  * A structure to hold some fields needed in zero copy code path,
53  * mainly for associating an mbuf with the right desc_idx.
54  */
55 struct zcopy_mbuf {
56         struct rte_mbuf *mbuf;
57         uint32_t desc_idx;
58         uint16_t in_use;
59
60         TAILQ_ENTRY(zcopy_mbuf) next;
61 };
62 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
63
64 /*
65  * Structure contains the info for each batched memory copy.
66  */
67 struct batch_copy_elem {
68         void *dst;
69         void *src;
70         uint32_t len;
71         uint64_t log_addr;
72 };
73
74 /*
75  * Structure that contains the info for batched dirty logging.
76  */
77 struct log_cache_entry {
78         uint32_t offset;
79         unsigned long val;
80 };
81
82 /**
83  * Structure contains variables relevant to RX/TX virtqueues.
84  */
85 struct vhost_virtqueue {
86         struct vring_desc       *desc;
87         struct vring_avail      *avail;
88         struct vring_used       *used;
89         uint32_t                size;
90
91         uint16_t                last_avail_idx;
92         uint16_t                last_used_idx;
93         /* Last used index we notify to front end. */
94         uint16_t                signalled_used;
95 #define VIRTIO_INVALID_EVENTFD          (-1)
96 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
97
98         /* Backend value to determine if device should started/stopped */
99         int                     backend;
100         int                     enabled;
101         int                     access_ok;
102         rte_spinlock_t          access_lock;
103
104         /* Used to notify the guest (trigger interrupt) */
105         int                     callfd;
106         /* Currently unused as polling mode is enabled */
107         int                     kickfd;
108
109         /* Physical address of used ring, for logging */
110         uint64_t                log_guest_addr;
111
112         uint16_t                nr_zmbuf;
113         uint16_t                zmbuf_size;
114         uint16_t                last_zmbuf_idx;
115         struct zcopy_mbuf       *zmbufs;
116         struct zcopy_mbuf_list  zmbuf_list;
117
118         struct vring_used_elem  *shadow_used_ring;
119         uint16_t                shadow_used_idx;
120         struct vhost_vring_addr ring_addrs;
121
122         struct batch_copy_elem  *batch_copy_elems;
123         uint16_t                batch_copy_nb_elems;
124
125         struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
126         uint16_t log_cache_nb_elem;
127
128         rte_rwlock_t    iotlb_lock;
129         rte_rwlock_t    iotlb_pending_lock;
130         struct rte_mempool *iotlb_pool;
131         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
132         int                             iotlb_cache_nr;
133         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
134 } __rte_cache_aligned;
135
136 /* Old kernels have no such macros defined */
137 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
138  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
139 #endif
140
141 #ifndef VIRTIO_NET_F_MQ
142  #define VIRTIO_NET_F_MQ                22
143 #endif
144
145 #define VHOST_MAX_VRING                 0x100
146 #define VHOST_MAX_QUEUE_PAIRS           0x80
147
148 #ifndef VIRTIO_NET_F_MTU
149  #define VIRTIO_NET_F_MTU 3
150 #endif
151
152 #ifndef VIRTIO_F_ANY_LAYOUT
153  #define VIRTIO_F_ANY_LAYOUT            27
154 #endif
155
156 /* Declare IOMMU related bits for older kernels */
157 #ifndef VIRTIO_F_IOMMU_PLATFORM
158
159 #define VIRTIO_F_IOMMU_PLATFORM 33
160
161 struct vhost_iotlb_msg {
162         __u64 iova;
163         __u64 size;
164         __u64 uaddr;
165 #define VHOST_ACCESS_RO      0x1
166 #define VHOST_ACCESS_WO      0x2
167 #define VHOST_ACCESS_RW      0x3
168         __u8 perm;
169 #define VHOST_IOTLB_MISS           1
170 #define VHOST_IOTLB_UPDATE         2
171 #define VHOST_IOTLB_INVALIDATE     3
172 #define VHOST_IOTLB_ACCESS_FAIL    4
173         __u8 type;
174 };
175
176 #define VHOST_IOTLB_MSG 0x1
177
178 struct vhost_msg {
179         int type;
180         union {
181                 struct vhost_iotlb_msg iotlb;
182                 __u8 padding[64];
183         };
184 };
185 #endif
186
187 /*
188  * Define virtio 1.0 for older kernels
189  */
190 #ifndef VIRTIO_F_VERSION_1
191  #define VIRTIO_F_VERSION_1 32
192 #endif
193
194 /*
195  * Available and used descs are in same order
196  */
197 #ifndef VIRTIO_F_IN_ORDER
198 #define VIRTIO_F_IN_ORDER      35
199 #endif
200
201 /* Features supported by this builtin vhost-user net driver. */
202 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
203                                 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
204                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
205                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
206                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
207                                 (1ULL << VIRTIO_NET_F_MQ)      | \
208                                 (1ULL << VIRTIO_F_VERSION_1)   | \
209                                 (1ULL << VHOST_F_LOG_ALL)      | \
210                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
211                                 (1ULL << VIRTIO_NET_F_GSO) | \
212                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
213                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
214                                 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
215                                 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
216                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
217                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
218                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
219                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
220                                 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
221                                 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
222                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
223                                 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
224                                 (1ULL << VIRTIO_NET_F_MTU)  | \
225                                 (1ULL << VIRTIO_F_IN_ORDER) | \
226                                 (1ULL << VIRTIO_F_IOMMU_PLATFORM))
227
228
229 struct guest_page {
230         uint64_t guest_phys_addr;
231         uint64_t host_phys_addr;
232         uint64_t size;
233 };
234
235 /**
236  * function prototype for the vhost backend to handler specific vhost user
237  * messages prior to the master message handling
238  *
239  * @param vid
240  *  vhost device id
241  * @param msg
242  *  Message pointer.
243  * @param require_reply
244  *  If the handler requires sending a reply, this varaible shall be written 1,
245  *  otherwise 0.
246  * @param skip_master
247  *  If the handler requires skipping the master message handling, this variable
248  *  shall be written 1, otherwise 0.
249  * @return
250  *  0 on success, -1 on failure
251  */
252 typedef int (*vhost_msg_pre_handle)(int vid, void *msg,
253                 uint32_t *require_reply, uint32_t *skip_master);
254
255 /**
256  * function prototype for the vhost backend to handler specific vhost user
257  * messages after the master message handling is done
258  *
259  * @param vid
260  *  vhost device id
261  * @param msg
262  *  Message pointer.
263  * @param require_reply
264  *  If the handler requires sending a reply, this varaible shall be written 1,
265  *  otherwise 0.
266  * @return
267  *  0 on success, -1 on failure
268  */
269 typedef int (*vhost_msg_post_handle)(int vid, void *msg,
270                 uint32_t *require_reply);
271
272 /**
273  * pre and post vhost user message handlers
274  */
275 struct vhost_user_extern_ops {
276         vhost_msg_pre_handle pre_msg_handle;
277         vhost_msg_post_handle post_msg_handle;
278 };
279
280 /**
281  * Device structure contains all configuration information relating
282  * to the device.
283  */
284 struct virtio_net {
285         /* Frontend (QEMU) memory and memory region information */
286         struct rte_vhost_memory *mem;
287         uint64_t                features;
288         uint64_t                protocol_features;
289         int                     vid;
290         uint32_t                flags;
291         uint16_t                vhost_hlen;
292         /* to tell if we need broadcast rarp packet */
293         rte_atomic16_t          broadcast_rarp;
294         uint32_t                nr_vring;
295         int                     dequeue_zero_copy;
296         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
297 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
298         char                    ifname[IF_NAME_SZ];
299         uint64_t                log_size;
300         uint64_t                log_base;
301         uint64_t                log_addr;
302         struct ether_addr       mac;
303         uint16_t                mtu;
304
305         struct vhost_device_ops const *notify_ops;
306
307         uint32_t                nr_guest_pages;
308         uint32_t                max_guest_pages;
309         struct guest_page       *guest_pages;
310
311         int                     slave_req_fd;
312         rte_spinlock_t          slave_req_lock;
313
314         /*
315          * Device id to identify a specific backend device.
316          * It's set to -1 for the default software implementation.
317          */
318         int                     vdpa_dev_id;
319
320         /* private data for virtio device */
321         void                    *extern_data;
322         /* pre and post vhost user message handlers for the device */
323         struct vhost_user_extern_ops extern_ops;
324 } __rte_cache_aligned;
325
326 #define VHOST_LOG_PAGE  4096
327
328 /*
329  * Atomically set a bit in memory.
330  */
331 static __rte_always_inline void
332 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
333 {
334 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
335         /*
336          * __sync_ built-ins are deprecated, but __atomic_ ones
337          * are sub-optimized in older GCC versions.
338          */
339         __sync_fetch_and_or_1(addr, (1U << nr));
340 #else
341         __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
342 #endif
343 }
344
345 static __rte_always_inline void
346 vhost_log_page(uint8_t *log_base, uint64_t page)
347 {
348         vhost_set_bit(page % 8, &log_base[page / 8]);
349 }
350
351 static __rte_always_inline void
352 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
353 {
354         uint64_t page;
355
356         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
357                    !dev->log_base || !len))
358                 return;
359
360         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
361                 return;
362
363         /* To make sure guest memory updates are committed before logging */
364         rte_smp_wmb();
365
366         page = addr / VHOST_LOG_PAGE;
367         while (page * VHOST_LOG_PAGE < addr + len) {
368                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
369                 page += 1;
370         }
371 }
372
373 static __rte_always_inline void
374 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
375 {
376         unsigned long *log_base;
377         int i;
378
379         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
380                    !dev->log_base))
381                 return;
382
383         log_base = (unsigned long *)(uintptr_t)dev->log_base;
384
385         /*
386          * It is expected a write memory barrier has been issued
387          * before this function is called.
388          */
389
390         for (i = 0; i < vq->log_cache_nb_elem; i++) {
391                 struct log_cache_entry *elem = vq->log_cache + i;
392
393 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
394                 /*
395                  * '__sync' builtins are deprecated, but '__atomic' ones
396                  * are sub-optimized in older GCC versions.
397                  */
398                 __sync_fetch_and_or(log_base + elem->offset, elem->val);
399 #else
400                 __atomic_fetch_or(log_base + elem->offset, elem->val,
401                                 __ATOMIC_RELAXED);
402 #endif
403         }
404
405         rte_smp_wmb();
406
407         vq->log_cache_nb_elem = 0;
408 }
409
410 static __rte_always_inline void
411 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
412                         uint64_t page)
413 {
414         uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
415         uint32_t offset = page / (sizeof(unsigned long) << 3);
416         int i;
417
418         for (i = 0; i < vq->log_cache_nb_elem; i++) {
419                 struct log_cache_entry *elem = vq->log_cache + i;
420
421                 if (elem->offset == offset) {
422                         elem->val |= (1UL << bit_nr);
423                         return;
424                 }
425         }
426
427         if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
428                 /*
429                  * No more room for a new log cache entry,
430                  * so write the dirty log map directly.
431                  */
432                 rte_smp_wmb();
433                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
434
435                 return;
436         }
437
438         vq->log_cache[i].offset = offset;
439         vq->log_cache[i].val = (1UL << bit_nr);
440         vq->log_cache_nb_elem++;
441 }
442
443 static __rte_always_inline void
444 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
445                         uint64_t addr, uint64_t len)
446 {
447         uint64_t page;
448
449         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
450                    !dev->log_base || !len))
451                 return;
452
453         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
454                 return;
455
456         page = addr / VHOST_LOG_PAGE;
457         while (page * VHOST_LOG_PAGE < addr + len) {
458                 vhost_log_cache_page(dev, vq, page);
459                 page += 1;
460         }
461 }
462
463 static __rte_always_inline void
464 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
465                         uint64_t offset, uint64_t len)
466 {
467         vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
468 }
469
470 static __rte_always_inline void
471 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
472                      uint64_t offset, uint64_t len)
473 {
474         vhost_log_write(dev, vq->log_guest_addr + offset, len);
475 }
476
477 /* Macros for printing using RTE_LOG */
478 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
479 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
480
481 #ifdef RTE_LIBRTE_VHOST_DEBUG
482 #define VHOST_MAX_PRINT_BUFF 6072
483 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
484         RTE_LOG(DEBUG, log_type, fmt, ##args)
485 #define PRINT_PACKET(device, addr, size, header) do { \
486         char *pkt_addr = (char *)(addr); \
487         unsigned int index; \
488         char packet[VHOST_MAX_PRINT_BUFF]; \
489         \
490         if ((header)) \
491                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
492         else \
493                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
494         for (index = 0; index < (size); index++) { \
495                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
496                         "%02hhx ", pkt_addr[index]); \
497         } \
498         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
499         \
500         VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
501 } while (0)
502 #else
503 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
504 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
505 #endif
506
507 extern uint64_t VHOST_FEATURES;
508 #define MAX_VHOST_DEVICE        1024
509 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
510
511 /* Convert guest physical address to host physical address */
512 static __rte_always_inline rte_iova_t
513 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
514 {
515         uint32_t i;
516         struct guest_page *page;
517
518         for (i = 0; i < dev->nr_guest_pages; i++) {
519                 page = &dev->guest_pages[i];
520
521                 if (gpa >= page->guest_phys_addr &&
522                     gpa + size < page->guest_phys_addr + page->size) {
523                         return gpa - page->guest_phys_addr +
524                                page->host_phys_addr;
525                 }
526         }
527
528         return 0;
529 }
530
531 static __rte_always_inline struct virtio_net *
532 get_device(int vid)
533 {
534         struct virtio_net *dev = vhost_devices[vid];
535
536         if (unlikely(!dev)) {
537                 RTE_LOG(ERR, VHOST_CONFIG,
538                         "(%d) device not found.\n", vid);
539         }
540
541         return dev;
542 }
543
544 int vhost_new_device(void);
545 void cleanup_device(struct virtio_net *dev, int destroy);
546 void reset_device(struct virtio_net *dev);
547 void vhost_destroy_device(int);
548 void vhost_destroy_device_notify(struct virtio_net *dev);
549
550 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
551 void free_vq(struct vhost_virtqueue *vq);
552
553 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
554
555 void vhost_attach_vdpa_device(int vid, int did);
556 void vhost_detach_vdpa_device(int vid);
557
558 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
559 void vhost_enable_dequeue_zero_copy(int vid);
560 void vhost_set_builtin_virtio_net(int vid, bool enable);
561
562 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
563
564 /*
565  * Backend-specific cleanup.
566  *
567  * TODO: fix it; we have one backend now
568  */
569 void vhost_backend_cleanup(struct virtio_net *dev);
570
571 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
572                         uint64_t iova, uint64_t *len, uint8_t perm);
573 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
574 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
575
576 static __rte_always_inline uint64_t
577 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
578                         uint64_t iova, uint64_t *len, uint8_t perm)
579 {
580         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
581                 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
582
583         return __vhost_iova_to_vva(dev, vq, iova, len, perm);
584 }
585
586 #define vhost_used_event(vr) \
587         (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
588
589 /*
590  * The following is used with VIRTIO_RING_F_EVENT_IDX.
591  * Assuming a given event_idx value from the other size, if we have
592  * just incremented index from old to new_idx, should we trigger an
593  * event?
594  */
595 static __rte_always_inline int
596 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
597 {
598         return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
599 }
600
601 static __rte_always_inline void
602 vhost_vring_call(struct virtio_net *dev, struct vhost_virtqueue *vq)
603 {
604         /* Flush used->idx update before we read avail->flags. */
605         rte_smp_mb();
606
607         /* Don't kick guest if we don't reach index specified by guest. */
608         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
609                 uint16_t old = vq->signalled_used;
610                 uint16_t new = vq->last_used_idx;
611
612                 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
613                         __func__,
614                         vhost_used_event(vq),
615                         old, new);
616                 if (vhost_need_event(vhost_used_event(vq), new, old)
617                         && (vq->callfd >= 0)) {
618                         vq->signalled_used = vq->last_used_idx;
619                         eventfd_write(vq->callfd, (eventfd_t) 1);
620                 }
621         } else {
622                 /* Kick the guest if necessary. */
623                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
624                                 && (vq->callfd >= 0))
625                         eventfd_write(vq->callfd, (eventfd_t)1);
626         }
627 }
628
629 #endif /* _VHOST_NET_CDEV_H_ */