DPDK  18.05.1
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 
33 #ifndef VHOST_USER_PROTOCOL_F_MQ
34 #define VHOST_USER_PROTOCOL_F_MQ 0
35 #endif
36 
37 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
38 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
39 #endif
40 
41 #ifndef VHOST_USER_PROTOCOL_F_RARP
42 #define VHOST_USER_PROTOCOL_F_RARP 2
43 #endif
44 
45 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
46 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
47 #endif
48 
49 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
50 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
51 #endif
52 
53 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
54 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
55 #endif
56 
57 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
58 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
59 #endif
60 
62 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
63 #define VHOST_USER_F_PROTOCOL_FEATURES 30
64 #endif
65 
71  uint64_t guest_phys_addr;
72  uint64_t guest_user_addr;
73  uint64_t host_user_addr;
74  uint64_t size;
75  void *mmap_addr;
76  uint64_t mmap_size;
77  int fd;
78 };
79 
84  uint32_t nregions;
85  struct rte_vhost_mem_region regions[];
86 };
87 
88 struct rte_vhost_vring {
89  struct vring_desc *desc;
90  struct vring_avail *avail;
91  struct vring_used *used;
92  uint64_t log_guest_addr;
93 
95  int callfd;
96 
97  int kickfd;
98  uint16_t size;
99 };
100 
105  int (*new_device)(int vid);
106  void (*destroy_device)(int vid);
108  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
116  int (*features_changed)(int vid, uint64_t features);
117 
118  int (*new_connection)(int vid);
119  void (*destroy_connection)(int vid);
120 
121  void *reserved[2];
122 };
123 
139 __rte_deprecated
140 static __rte_always_inline uint64_t
141 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
142 {
143  struct rte_vhost_mem_region *reg;
144  uint32_t i;
145 
146  for (i = 0; i < mem->nregions; i++) {
147  reg = &mem->regions[i];
148  if (gpa >= reg->guest_phys_addr &&
149  gpa < reg->guest_phys_addr + reg->size) {
150  return gpa - reg->guest_phys_addr +
151  reg->host_user_addr;
152  }
153  }
154 
155  return 0;
156 }
157 
174 static __rte_always_inline uint64_t
176  uint64_t gpa, uint64_t *len)
177 {
178  struct rte_vhost_mem_region *r;
179  uint32_t i;
180 
181  for (i = 0; i < mem->nregions; i++) {
182  r = &mem->regions[i];
183  if (gpa >= r->guest_phys_addr &&
184  gpa < r->guest_phys_addr + r->size) {
185 
186  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
187  *len = r->guest_phys_addr + r->size - gpa;
188 
189  return gpa - r->guest_phys_addr +
190  r->host_user_addr;
191  }
192  }
193  *len = 0;
194 
195  return 0;
196 }
197 
198 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
199 
218 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
219 
238 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
239  uint64_t offset, uint64_t len);
240 
241 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
242 
247 int rte_vhost_driver_register(const char *path, uint64_t flags);
248 
249 /* Unregister vhost driver. This is only meaningful to vhost user. */
250 int rte_vhost_driver_unregister(const char *path);
251 
262 int __rte_experimental
263 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
264 
273 int __rte_experimental
274 rte_vhost_driver_detach_vdpa_device(const char *path);
275 
284 int __rte_experimental
285 rte_vhost_driver_get_vdpa_device_id(const char *path);
286 
297 int rte_vhost_driver_set_features(const char *path, uint64_t features);
298 
314 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
315 
328 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
329 
340 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
341 
352 int __rte_experimental
354  uint64_t *protocol_features);
355 
366 int __rte_experimental
367 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
368 
379 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
380 
381 /* Register callbacks. */
382 int rte_vhost_driver_callback_register(const char *path,
383  struct vhost_device_ops const * const ops);
384 
396 int rte_vhost_driver_start(const char *path);
397 
411 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
412 
423 int rte_vhost_get_numa_node(int vid);
424 
439 __rte_deprecated
440 uint32_t rte_vhost_get_queue_num(int vid);
441 
451 uint16_t rte_vhost_get_vring_num(int vid);
452 
467 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
468 
480 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
481 
482 struct rte_mbuf;
483 struct rte_mempool;
500 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
501  struct rte_mbuf **pkts, uint16_t count);
502 
520 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
521  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
522 
537 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
538 
551 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
552  struct rte_vhost_vring *vring);
553 
565 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
566 
577 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
578 
591 int __rte_experimental
592 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
593 
608 int __rte_experimental
609 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
610  uint16_t *last_avail_idx, uint16_t *last_used_idx);
611 
626 int __rte_experimental
627 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
628  uint16_t last_avail_idx, uint16_t last_used_idx);
629 
638 int __rte_experimental
640 
641 #ifdef __cplusplus
642 }
643 #endif
644 
645 #endif /* _RTE_VHOST_H_ */