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