eal: fix multiple definition of per lcore thread id
[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 #include <rte_malloc.h>
22
23 #include "rte_vhost.h"
24 #include "rte_vdpa.h"
25 #include "rte_vdpa_dev.h"
26
27 /* Used to indicate that the device is running on a data core */
28 #define VIRTIO_DEV_RUNNING 1
29 /* Used to indicate that the device is ready to operate */
30 #define VIRTIO_DEV_READY 2
31 /* Used to indicate that the built-in vhost net device backend is enabled */
32 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
33 /* Used to indicate that the device has its own data path and configured */
34 #define VIRTIO_DEV_VDPA_CONFIGURED 8
35
36 /* Backend value set by guest. */
37 #define VIRTIO_DEV_STOPPED -1
38
39 #define BUF_VECTOR_MAX 256
40
41 #define VHOST_LOG_CACHE_NR 32
42
43 #define PACKED_DESC_ENQUEUE_USED_FLAG(w)        \
44         ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \
45                 VRING_DESC_F_WRITE)
46 #define PACKED_DESC_DEQUEUE_USED_FLAG(w)        \
47         ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED) : 0x0)
48 #define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \
49                                          VRING_DESC_F_INDIRECT)
50
51 #define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \
52                             sizeof(struct vring_packed_desc))
53 #define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
54
55 #ifdef VHOST_GCC_UNROLL_PRAGMA
56 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \
57         for (iter = val; iter < size; iter++)
58 #endif
59
60 #ifdef VHOST_CLANG_UNROLL_PRAGMA
61 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \
62         for (iter = val; iter < size; iter++)
63 #endif
64
65 #ifdef VHOST_ICC_UNROLL_PRAGMA
66 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \
67         for (iter = val; iter < size; iter++)
68 #endif
69
70 #ifndef vhost_for_each_try_unroll
71 #define vhost_for_each_try_unroll(iter, val, num) \
72         for (iter = val; iter < num; iter++)
73 #endif
74
75 /**
76  * Structure contains buffer address, length and descriptor index
77  * from vring to do scatter RX.
78  */
79 struct buf_vector {
80         uint64_t buf_iova;
81         uint64_t buf_addr;
82         uint32_t buf_len;
83         uint32_t desc_idx;
84 };
85
86 /*
87  * A structure to hold some fields needed in zero copy code path,
88  * mainly for associating an mbuf with the right desc_idx.
89  */
90 struct zcopy_mbuf {
91         struct rte_mbuf *mbuf;
92         uint32_t desc_idx;
93         uint16_t desc_count;
94         uint16_t in_use;
95
96         TAILQ_ENTRY(zcopy_mbuf) next;
97 };
98 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
99
100 /*
101  * Structure contains the info for each batched memory copy.
102  */
103 struct batch_copy_elem {
104         void *dst;
105         void *src;
106         uint32_t len;
107         uint64_t log_addr;
108 };
109
110 /*
111  * Structure that contains the info for batched dirty logging.
112  */
113 struct log_cache_entry {
114         uint32_t offset;
115         unsigned long val;
116 };
117
118 struct vring_used_elem_packed {
119         uint16_t id;
120         uint16_t flags;
121         uint32_t len;
122         uint32_t count;
123 };
124
125 /**
126  * Structure contains variables relevant to RX/TX virtqueues.
127  */
128 struct vhost_virtqueue {
129         union {
130                 struct vring_desc       *desc;
131                 struct vring_packed_desc   *desc_packed;
132         };
133         union {
134                 struct vring_avail      *avail;
135                 struct vring_packed_desc_event *driver_event;
136         };
137         union {
138                 struct vring_used       *used;
139                 struct vring_packed_desc_event *device_event;
140         };
141         uint32_t                size;
142
143         uint16_t                last_avail_idx;
144         uint16_t                last_used_idx;
145         /* Last used index we notify to front end. */
146         uint16_t                signalled_used;
147         bool                    signalled_used_valid;
148 #define VIRTIO_INVALID_EVENTFD          (-1)
149 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
150
151         /* Backend value to determine if device should started/stopped */
152         int                     backend;
153         int                     enabled;
154         int                     access_ok;
155         int                     ready;
156         rte_spinlock_t          access_lock;
157
158         /* Used to notify the guest (trigger interrupt) */
159         int                     callfd;
160         /* Currently unused as polling mode is enabled */
161         int                     kickfd;
162
163         /* Physical address of used ring, for logging */
164         uint64_t                log_guest_addr;
165
166         /* inflight share memory info */
167         union {
168                 struct rte_vhost_inflight_info_split *inflight_split;
169                 struct rte_vhost_inflight_info_packed *inflight_packed;
170         };
171         struct rte_vhost_resubmit_info *resubmit_inflight;
172         uint64_t                global_counter;
173
174         uint16_t                nr_zmbuf;
175         uint16_t                zmbuf_size;
176         uint16_t                last_zmbuf_idx;
177         struct zcopy_mbuf       *zmbufs;
178         struct zcopy_mbuf_list  zmbuf_list;
179
180         union {
181                 struct vring_used_elem  *shadow_used_split;
182                 struct vring_used_elem_packed *shadow_used_packed;
183         };
184         uint16_t                shadow_used_idx;
185         /* Record packed ring enqueue latest desc cache aligned index */
186         uint16_t                shadow_aligned_idx;
187         /* Record packed ring first dequeue desc index */
188         uint16_t                shadow_last_used_idx;
189         struct vhost_vring_addr ring_addrs;
190
191         struct batch_copy_elem  *batch_copy_elems;
192         uint16_t                batch_copy_nb_elems;
193         bool                    used_wrap_counter;
194         bool                    avail_wrap_counter;
195
196         struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
197         uint16_t log_cache_nb_elem;
198
199         rte_rwlock_t    iotlb_lock;
200         rte_rwlock_t    iotlb_pending_lock;
201         struct rte_mempool *iotlb_pool;
202         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
203         int                             iotlb_cache_nr;
204         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
205 } __rte_cache_aligned;
206
207 #define VHOST_MAX_VRING                 0x100
208 #define VHOST_MAX_QUEUE_PAIRS           0x80
209
210 /* Declare IOMMU related bits for older kernels */
211 #ifndef VIRTIO_F_IOMMU_PLATFORM
212
213 #define VIRTIO_F_IOMMU_PLATFORM 33
214
215 struct vhost_iotlb_msg {
216         __u64 iova;
217         __u64 size;
218         __u64 uaddr;
219 #define VHOST_ACCESS_RO      0x1
220 #define VHOST_ACCESS_WO      0x2
221 #define VHOST_ACCESS_RW      0x3
222         __u8 perm;
223 #define VHOST_IOTLB_MISS           1
224 #define VHOST_IOTLB_UPDATE         2
225 #define VHOST_IOTLB_INVALIDATE     3
226 #define VHOST_IOTLB_ACCESS_FAIL    4
227         __u8 type;
228 };
229
230 #define VHOST_IOTLB_MSG 0x1
231
232 struct vhost_msg {
233         int type;
234         union {
235                 struct vhost_iotlb_msg iotlb;
236                 __u8 padding[64];
237         };
238 };
239 #endif
240
241 /*
242  * Define virtio 1.0 for older kernels
243  */
244 #ifndef VIRTIO_F_VERSION_1
245  #define VIRTIO_F_VERSION_1 32
246 #endif
247
248 /* Declare packed ring related bits for older kernels */
249 #ifndef VIRTIO_F_RING_PACKED
250
251 #define VIRTIO_F_RING_PACKED 34
252
253 struct vring_packed_desc {
254         uint64_t addr;
255         uint32_t len;
256         uint16_t id;
257         uint16_t flags;
258 };
259
260 struct vring_packed_desc_event {
261         uint16_t off_wrap;
262         uint16_t flags;
263 };
264 #endif
265
266 /*
267  * Declare below packed ring defines unconditionally
268  * as Kernel header might use different names.
269  */
270 #define VRING_DESC_F_AVAIL      (1ULL << 7)
271 #define VRING_DESC_F_USED       (1ULL << 15)
272
273 #define VRING_EVENT_F_ENABLE 0x0
274 #define VRING_EVENT_F_DISABLE 0x1
275 #define VRING_EVENT_F_DESC 0x2
276
277 /*
278  * Available and used descs are in same order
279  */
280 #ifndef VIRTIO_F_IN_ORDER
281 #define VIRTIO_F_IN_ORDER      35
282 #endif
283
284 /* Features supported by this builtin vhost-user net driver. */
285 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
286                                 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
287                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
288                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
289                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
290                                 (1ULL << VIRTIO_NET_F_MQ)      | \
291                                 (1ULL << VIRTIO_F_VERSION_1)   | \
292                                 (1ULL << VHOST_F_LOG_ALL)      | \
293                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
294                                 (1ULL << VIRTIO_NET_F_GSO) | \
295                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
296                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
297                                 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
298                                 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
299                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
300                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
301                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
302                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
303                                 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
304                                 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
305                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
306                                 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
307                                 (1ULL << VIRTIO_NET_F_MTU)  | \
308                                 (1ULL << VIRTIO_F_IN_ORDER) | \
309                                 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
310                                 (1ULL << VIRTIO_F_RING_PACKED))
311
312
313 struct guest_page {
314         uint64_t guest_phys_addr;
315         uint64_t host_phys_addr;
316         uint64_t size;
317 };
318
319 struct inflight_mem_info {
320         int             fd;
321         void            *addr;
322         uint64_t        size;
323 };
324
325 /**
326  * Device structure contains all configuration information relating
327  * to the device.
328  */
329 struct virtio_net {
330         /* Frontend (QEMU) memory and memory region information */
331         struct rte_vhost_memory *mem;
332         uint64_t                features;
333         uint64_t                protocol_features;
334         int                     vid;
335         uint32_t                flags;
336         uint16_t                vhost_hlen;
337         /* to tell if we need broadcast rarp packet */
338         int16_t                 broadcast_rarp;
339         uint32_t                nr_vring;
340         int                     dequeue_zero_copy;
341         int                     extbuf;
342         int                     linearbuf;
343         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
344         struct inflight_mem_info *inflight_info;
345 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
346         char                    ifname[IF_NAME_SZ];
347         uint64_t                log_size;
348         uint64_t                log_base;
349         uint64_t                log_addr;
350         struct rte_ether_addr   mac;
351         uint16_t                mtu;
352
353         struct vhost_device_ops const *notify_ops;
354
355         uint32_t                nr_guest_pages;
356         uint32_t                max_guest_pages;
357         struct guest_page       *guest_pages;
358
359         int                     slave_req_fd;
360         rte_spinlock_t          slave_req_lock;
361
362         int                     postcopy_ufd;
363         int                     postcopy_listening;
364
365         struct rte_vdpa_device *vdpa_dev;
366
367         /* context data for the external message handlers */
368         void                    *extern_data;
369         /* pre and post vhost user message handlers for the device */
370         struct rte_vhost_user_extern_ops extern_ops;
371 } __rte_cache_aligned;
372
373 static __rte_always_inline bool
374 vq_is_packed(struct virtio_net *dev)
375 {
376         return dev->features & (1ull << VIRTIO_F_RING_PACKED);
377 }
378
379 static inline bool
380 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
381 {
382         uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
383
384         return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
385                 wrap_counter != !!(flags & VRING_DESC_F_USED);
386 }
387
388 static inline void
389 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
390 {
391         vq->last_used_idx += num;
392         if (vq->last_used_idx >= vq->size) {
393                 vq->used_wrap_counter ^= 1;
394                 vq->last_used_idx -= vq->size;
395         }
396 }
397
398 static inline void
399 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
400 {
401         vq->last_avail_idx += num;
402         if (vq->last_avail_idx >= vq->size) {
403                 vq->avail_wrap_counter ^= 1;
404                 vq->last_avail_idx -= vq->size;
405         }
406 }
407
408 void __vhost_log_cache_write(struct virtio_net *dev,
409                 struct vhost_virtqueue *vq,
410                 uint64_t addr, uint64_t len);
411 void __vhost_log_cache_write_iova(struct virtio_net *dev,
412                 struct vhost_virtqueue *vq,
413                 uint64_t iova, uint64_t len);
414 void __vhost_log_cache_sync(struct virtio_net *dev,
415                 struct vhost_virtqueue *vq);
416 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
417 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
418                             uint64_t iova, uint64_t len);
419
420 static __rte_always_inline void
421 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
422 {
423         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
424                 __vhost_log_write(dev, addr, len);
425 }
426
427 static __rte_always_inline void
428 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
429 {
430         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
431                 __vhost_log_cache_sync(dev, vq);
432 }
433
434 static __rte_always_inline void
435 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
436                         uint64_t addr, uint64_t len)
437 {
438         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
439                 __vhost_log_cache_write(dev, vq, addr, len);
440 }
441
442 static __rte_always_inline void
443 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
444                         uint64_t offset, uint64_t len)
445 {
446         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
447                 if (unlikely(vq->log_guest_addr == 0))
448                         return;
449                 __vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset,
450                                         len);
451         }
452 }
453
454 static __rte_always_inline void
455 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
456                      uint64_t offset, uint64_t len)
457 {
458         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) {
459                 if (unlikely(vq->log_guest_addr == 0))
460                         return;
461                 __vhost_log_write(dev, vq->log_guest_addr + offset, len);
462         }
463 }
464
465 static __rte_always_inline void
466 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
467                            uint64_t iova, uint64_t len)
468 {
469         if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
470                 return;
471
472         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
473                 __vhost_log_cache_write_iova(dev, vq, iova, len);
474         else
475                 __vhost_log_cache_write(dev, vq, iova, len);
476 }
477
478 static __rte_always_inline void
479 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
480                            uint64_t iova, uint64_t len)
481 {
482         if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
483                 return;
484
485         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
486                 __vhost_log_write_iova(dev, vq, iova, len);
487         else
488                 __vhost_log_write(dev, iova, len);
489 }
490
491 extern int vhost_config_log_level;
492 extern int vhost_data_log_level;
493
494 #define VHOST_LOG_CONFIG(level, fmt, args...)                   \
495         rte_log(RTE_LOG_ ## level, vhost_config_log_level,      \
496                 "VHOST_CONFIG: " fmt, ##args)
497
498 #define VHOST_LOG_DATA(level, fmt, args...) \
499         (void)((RTE_LOG_ ## level <= RTE_LOG_DP_LEVEL) ?        \
500          rte_log(RTE_LOG_ ## level,  vhost_data_log_level,      \
501                 "VHOST_DATA : " fmt, ##args) :                  \
502          0)
503
504 #ifdef RTE_LIBRTE_VHOST_DEBUG
505 #define VHOST_MAX_PRINT_BUFF 6072
506 #define PRINT_PACKET(device, addr, size, header) do { \
507         char *pkt_addr = (char *)(addr); \
508         unsigned int index; \
509         char packet[VHOST_MAX_PRINT_BUFF]; \
510         \
511         if ((header)) \
512                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
513         else \
514                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
515         for (index = 0; index < (size); index++) { \
516                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
517                         "%02hhx ", pkt_addr[index]); \
518         } \
519         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
520         \
521         VHOST_LOG_DATA(DEBUG, "%s", packet); \
522 } while (0)
523 #else
524 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
525 #endif
526
527 #define MAX_VHOST_DEVICE        1024
528 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
529
530 #define VHOST_BINARY_SEARCH_THRESH 256
531
532 static __rte_always_inline int guest_page_addrcmp(const void *p1,
533                                                 const void *p2)
534 {
535         const struct guest_page *page1 = (const struct guest_page *)p1;
536         const struct guest_page *page2 = (const struct guest_page *)p2;
537
538         if (page1->guest_phys_addr > page2->guest_phys_addr)
539                 return 1;
540         if (page1->guest_phys_addr < page2->guest_phys_addr)
541                 return -1;
542
543         return 0;
544 }
545
546 /* Convert guest physical address to host physical address */
547 static __rte_always_inline rte_iova_t
548 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
549 {
550         uint32_t i;
551         struct guest_page *page;
552         struct guest_page key;
553
554         if (dev->nr_guest_pages >= VHOST_BINARY_SEARCH_THRESH) {
555                 key.guest_phys_addr = gpa;
556                 page = bsearch(&key, dev->guest_pages, dev->nr_guest_pages,
557                                sizeof(struct guest_page), guest_page_addrcmp);
558                 if (page) {
559                         if (gpa + size < page->guest_phys_addr + page->size)
560                                 return gpa - page->guest_phys_addr +
561                                         page->host_phys_addr;
562                 }
563         } else {
564                 for (i = 0; i < dev->nr_guest_pages; i++) {
565                         page = &dev->guest_pages[i];
566
567                         if (gpa >= page->guest_phys_addr &&
568                             gpa + size < page->guest_phys_addr +
569                             page->size)
570                                 return gpa - page->guest_phys_addr +
571                                        page->host_phys_addr;
572                 }
573         }
574
575         return 0;
576 }
577
578 static __rte_always_inline uint64_t
579 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
580 {
581         struct rte_vhost_mem_region *r;
582         uint32_t i;
583
584         if (unlikely(!dev || !dev->mem))
585                 return 0;
586
587         for (i = 0; i < dev->mem->nregions; i++) {
588                 r = &dev->mem->regions[i];
589
590                 if (vva >= r->host_user_addr &&
591                     vva + len <  r->host_user_addr + r->size) {
592                         return r->guest_phys_addr + vva - r->host_user_addr;
593                 }
594         }
595         return 0;
596 }
597
598 static __rte_always_inline struct virtio_net *
599 get_device(int vid)
600 {
601         struct virtio_net *dev = vhost_devices[vid];
602
603         if (unlikely(!dev)) {
604                 VHOST_LOG_CONFIG(ERR,
605                         "(%d) device not found.\n", vid);
606         }
607
608         return dev;
609 }
610
611 int vhost_new_device(void);
612 void cleanup_device(struct virtio_net *dev, int destroy);
613 void reset_device(struct virtio_net *dev);
614 void vhost_destroy_device(int);
615 void vhost_destroy_device_notify(struct virtio_net *dev);
616
617 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
618 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
619 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
620
621 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
622
623 void vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *dev);
624
625 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
626 void vhost_enable_dequeue_zero_copy(int vid);
627 void vhost_set_builtin_virtio_net(int vid, bool enable);
628 void vhost_enable_extbuf(int vid);
629 void vhost_enable_linearbuf(int vid);
630
631 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
632
633 /*
634  * Backend-specific cleanup.
635  *
636  * TODO: fix it; we have one backend now
637  */
638 void vhost_backend_cleanup(struct virtio_net *dev);
639
640 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
641                         uint64_t iova, uint64_t *len, uint8_t perm);
642 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
643                         struct vhost_virtqueue *vq,
644                         uint64_t desc_addr, uint64_t desc_len);
645 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
646 uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
647                 uint64_t log_addr);
648 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
649
650 static __rte_always_inline uint64_t
651 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
652                         uint64_t iova, uint64_t *len, uint8_t perm)
653 {
654         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
655                 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
656
657         return __vhost_iova_to_vva(dev, vq, iova, len, perm);
658 }
659
660 #define vhost_avail_event(vr) \
661         (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
662 #define vhost_used_event(vr) \
663         (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
664
665 /*
666  * The following is used with VIRTIO_RING_F_EVENT_IDX.
667  * Assuming a given event_idx value from the other size, if we have
668  * just incremented index from old to new_idx, should we trigger an
669  * event?
670  */
671 static __rte_always_inline int
672 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
673 {
674         return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
675 }
676
677 static __rte_always_inline void
678 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
679 {
680         /* Flush used->idx update before we read avail->flags. */
681         rte_smp_mb();
682
683         /* Don't kick guest if we don't reach index specified by guest. */
684         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
685                 uint16_t old = vq->signalled_used;
686                 uint16_t new = vq->last_used_idx;
687                 bool signalled_used_valid = vq->signalled_used_valid;
688
689                 vq->signalled_used = new;
690                 vq->signalled_used_valid = true;
691
692                 VHOST_LOG_DATA(DEBUG, "%s: used_event_idx=%d, old=%d, new=%d\n",
693                         __func__,
694                         vhost_used_event(vq),
695                         old, new);
696
697                 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
698                                         (vq->callfd >= 0)) ||
699                                 unlikely(!signalled_used_valid)) {
700                         eventfd_write(vq->callfd, (eventfd_t) 1);
701                         if (dev->notify_ops->guest_notified)
702                                 dev->notify_ops->guest_notified(dev->vid);
703                 }
704         } else {
705                 /* Kick the guest if necessary. */
706                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
707                                 && (vq->callfd >= 0)) {
708                         eventfd_write(vq->callfd, (eventfd_t)1);
709                         if (dev->notify_ops->guest_notified)
710                                 dev->notify_ops->guest_notified(dev->vid);
711                 }
712         }
713 }
714
715 static __rte_always_inline void
716 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
717 {
718         uint16_t old, new, off, off_wrap;
719         bool signalled_used_valid, kick = false;
720
721         /* Flush used desc update. */
722         rte_smp_mb();
723
724         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
725                 if (vq->driver_event->flags !=
726                                 VRING_EVENT_F_DISABLE)
727                         kick = true;
728                 goto kick;
729         }
730
731         old = vq->signalled_used;
732         new = vq->last_used_idx;
733         vq->signalled_used = new;
734         signalled_used_valid = vq->signalled_used_valid;
735         vq->signalled_used_valid = true;
736
737         if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
738                 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
739                         kick = true;
740                 goto kick;
741         }
742
743         if (unlikely(!signalled_used_valid)) {
744                 kick = true;
745                 goto kick;
746         }
747
748         rte_smp_rmb();
749
750         off_wrap = vq->driver_event->off_wrap;
751         off = off_wrap & ~(1 << 15);
752
753         if (new <= old)
754                 old -= vq->size;
755
756         if (vq->used_wrap_counter != off_wrap >> 15)
757                 off -= vq->size;
758
759         if (vhost_need_event(off, new, old))
760                 kick = true;
761 kick:
762         if (kick) {
763                 eventfd_write(vq->callfd, (eventfd_t)1);
764                 if (dev->notify_ops->guest_notified)
765                         dev->notify_ops->guest_notified(dev->vid);
766         }
767 }
768
769 static __rte_always_inline void
770 free_ind_table(void *idesc)
771 {
772         rte_free(idesc);
773 }
774
775 static __rte_always_inline void
776 restore_mbuf(struct rte_mbuf *m)
777 {
778         uint32_t mbuf_size, priv_size;
779
780         while (m) {
781                 priv_size = rte_pktmbuf_priv_size(m->pool);
782                 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
783                 /* start of buffer is after mbuf structure and priv data */
784
785                 m->buf_addr = (char *)m + mbuf_size;
786                 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
787                 m = m->next;
788         }
789 }
790
791 static __rte_always_inline bool
792 mbuf_is_consumed(struct rte_mbuf *m)
793 {
794         while (m) {
795                 if (rte_mbuf_refcnt_read(m) > 1)
796                         return false;
797                 m = m->next;
798         }
799
800         return true;
801 }
802
803 static __rte_always_inline void
804 put_zmbuf(struct zcopy_mbuf *zmbuf)
805 {
806         zmbuf->in_use = 0;
807 }
808
809 #endif /* _VHOST_NET_CDEV_H_ */