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