DPDK  19.02.0
rte_vhost.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4 
5 #ifndef _RTE_VHOST_H_
6 #define _RTE_VHOST_H_
7 
13 #include <stdint.h>
14 #include <sys/eventfd.h>
15 
16 #include <rte_memory.h>
17 #include <rte_mempool.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /* These are not C++-aware. */
24 #include <linux/vhost.h>
25 #include <linux/virtio_ring.h>
26 
27 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
28 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
29 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
30 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
31 #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4)
32 
34 #ifndef VHOST_USER_PROTOCOL_F_MQ
35 #define VHOST_USER_PROTOCOL_F_MQ 0
36 #endif
37 
38 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
39 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
40 #endif
41 
42 #ifndef VHOST_USER_PROTOCOL_F_RARP
43 #define VHOST_USER_PROTOCOL_F_RARP 2
44 #endif
45 
46 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
47 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
48 #endif
49 
50 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
51 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
52 #endif
53 
54 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
55 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
56 #endif
57 
58 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
59 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
60 #endif
61 
62 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
63 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
64 #endif
65 
66 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
67 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
68 #endif
69 
70 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
71 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
72 #endif
73 
75 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
76 #define VHOST_USER_F_PROTOCOL_FEATURES 30
77 #endif
78 
84  uint64_t guest_phys_addr;
85  uint64_t guest_user_addr;
86  uint64_t host_user_addr;
87  uint64_t size;
88  void *mmap_addr;
89  uint64_t mmap_size;
90  int fd;
91 };
92 
97  uint32_t nregions;
98  struct rte_vhost_mem_region regions[];
99 };
100 
101 struct rte_vhost_vring {
102  struct vring_desc *desc;
103  struct vring_avail *avail;
104  struct vring_used *used;
105  uint64_t log_guest_addr;
106 
108  int callfd;
109 
110  int kickfd;
111  uint16_t size;
112 };
113 
118  int (*new_device)(int vid);
119  void (*destroy_device)(int vid);
121  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
129  int (*features_changed)(int vid, uint64_t features);
130 
131  int (*new_connection)(int vid);
132  void (*destroy_connection)(int vid);
133 
134  void *reserved[2];
135 };
136 
152 __rte_deprecated
153 static __rte_always_inline uint64_t
154 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
155 {
156  struct rte_vhost_mem_region *reg;
157  uint32_t i;
158 
159  for (i = 0; i < mem->nregions; i++) {
160  reg = &mem->regions[i];
161  if (gpa >= reg->guest_phys_addr &&
162  gpa < reg->guest_phys_addr + reg->size) {
163  return gpa - reg->guest_phys_addr +
164  reg->host_user_addr;
165  }
166  }
167 
168  return 0;
169 }
170 
187 static __rte_always_inline uint64_t
189  uint64_t gpa, uint64_t *len)
190 {
191  struct rte_vhost_mem_region *r;
192  uint32_t i;
193 
194  for (i = 0; i < mem->nregions; i++) {
195  r = &mem->regions[i];
196  if (gpa >= r->guest_phys_addr &&
197  gpa < r->guest_phys_addr + r->size) {
198 
199  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
200  *len = r->guest_phys_addr + r->size - gpa;
201 
202  return gpa - r->guest_phys_addr +
203  r->host_user_addr;
204  }
205  }
206  *len = 0;
207 
208  return 0;
209 }
210 
211 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
212 
231 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
232 
251 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
252  uint64_t offset, uint64_t len);
253 
254 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
255 
260 int rte_vhost_driver_register(const char *path, uint64_t flags);
261 
262 /* Unregister vhost driver. This is only meaningful to vhost user. */
263 int rte_vhost_driver_unregister(const char *path);
264 
275 int __rte_experimental
276 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
277 
286 int __rte_experimental
287 rte_vhost_driver_detach_vdpa_device(const char *path);
288 
297 int __rte_experimental
298 rte_vhost_driver_get_vdpa_device_id(const char *path);
299 
310 int rte_vhost_driver_set_features(const char *path, uint64_t features);
311 
327 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
328 
341 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
342 
353 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
354 
365 int __rte_experimental
367  uint64_t *protocol_features);
368 
379 int __rte_experimental
380 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
381 
392 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
393 
394 /* Register callbacks. */
395 int rte_vhost_driver_callback_register(const char *path,
396  struct vhost_device_ops const * const ops);
397 
409 int rte_vhost_driver_start(const char *path);
410 
424 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
425 
436 int rte_vhost_get_numa_node(int vid);
437 
452 __rte_deprecated
453 uint32_t rte_vhost_get_queue_num(int vid);
454 
464 uint16_t rte_vhost_get_vring_num(int vid);
465 
480 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
481 
493 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
494 
495 struct rte_mbuf;
496 struct rte_mempool;
513 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
514  struct rte_mbuf **pkts, uint16_t count);
515 
533 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
534  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
535 
550 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
551 
564 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
565  struct rte_vhost_vring *vring);
566 
578 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
579 
590 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
591 
604 int __rte_experimental
605 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
606 
621 int __rte_experimental
622 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
623  uint16_t *last_avail_idx, uint16_t *last_used_idx);
624 
639 int __rte_experimental
640 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
641  uint16_t last_avail_idx, uint16_t last_used_idx);
642 
651 int __rte_experimental
653 
654 #ifdef __cplusplus
655 }
656 #endif
657 
658 #endif /* _RTE_VHOST_H_ */