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