DPDK  19.08.2
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 #include <linux/virtio_net.h>
27 
28 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
29 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
30 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
31 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
32 #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4)
33 
35 #ifndef VHOST_USER_PROTOCOL_F_MQ
36 #define VHOST_USER_PROTOCOL_F_MQ 0
37 #endif
38 
39 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
40 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
41 #endif
42 
43 #ifndef VHOST_USER_PROTOCOL_F_RARP
44 #define VHOST_USER_PROTOCOL_F_RARP 2
45 #endif
46 
47 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
48 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
49 #endif
50 
51 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
52 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
53 #endif
54 
55 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
56 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
57 #endif
58 
59 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
60 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
61 #endif
62 
63 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
64 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
65 #endif
66 
67 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
68 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
69 #endif
70 
71 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
72 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
73 #endif
74 
76 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
77 #define VHOST_USER_F_PROTOCOL_FEATURES 30
78 #endif
79 
85  uint64_t guest_phys_addr;
86  uint64_t guest_user_addr;
87  uint64_t host_user_addr;
88  uint64_t size;
89  void *mmap_addr;
90  uint64_t mmap_size;
91  int fd;
92 };
93 
98  uint32_t nregions;
99  struct rte_vhost_mem_region regions[];
100 };
101 
102 struct rte_vhost_vring {
103  struct vring_desc *desc;
104  struct vring_avail *avail;
105  struct vring_used *used;
106  uint64_t log_guest_addr;
107 
109  int callfd;
110 
111  int kickfd;
112  uint16_t size;
113 };
114 
119  /* Message handling failed */
120  RTE_VHOST_MSG_RESULT_ERR = -1,
121  /* Message handling successful */
122  RTE_VHOST_MSG_RESULT_OK = 0,
123  /* Message handling successful and reply prepared */
124  RTE_VHOST_MSG_RESULT_REPLY = 1,
125  /* Message not handled */
126  RTE_VHOST_MSG_RESULT_NOT_HANDLED,
127 };
128 
143 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
144 
149  /* Called prior to the master message handling. */
150  rte_vhost_msg_handle pre_msg_handle;
151  /* Called after the master message handling. */
152  rte_vhost_msg_handle post_msg_handle;
153 };
154 
159  int (*new_device)(int vid);
160  void (*destroy_device)(int vid);
162  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
170  int (*features_changed)(int vid, uint64_t features);
171 
172  int (*new_connection)(int vid);
173  void (*destroy_connection)(int vid);
174 
175  void *reserved[2];
176 };
177 
193 __rte_deprecated
194 static __rte_always_inline uint64_t
195 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
196 {
197  struct rte_vhost_mem_region *reg;
198  uint32_t i;
199 
200  for (i = 0; i < mem->nregions; i++) {
201  reg = &mem->regions[i];
202  if (gpa >= reg->guest_phys_addr &&
203  gpa < reg->guest_phys_addr + reg->size) {
204  return gpa - reg->guest_phys_addr +
205  reg->host_user_addr;
206  }
207  }
208 
209  return 0;
210 }
211 
228 static __rte_always_inline uint64_t
230  uint64_t gpa, uint64_t *len)
231 {
232  struct rte_vhost_mem_region *r;
233  uint32_t i;
234 
235  for (i = 0; i < mem->nregions; i++) {
236  r = &mem->regions[i];
237  if (gpa >= r->guest_phys_addr &&
238  gpa < r->guest_phys_addr + r->size) {
239 
240  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
241  *len = r->guest_phys_addr + r->size - gpa;
242 
243  return gpa - r->guest_phys_addr +
244  r->host_user_addr;
245  }
246  }
247  *len = 0;
248 
249  return 0;
250 }
251 
252 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
253 
272 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
273 
292 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
293  uint64_t offset, uint64_t len);
294 
295 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
296 
301 int rte_vhost_driver_register(const char *path, uint64_t flags);
302 
303 /* Unregister vhost driver. This is only meaningful to vhost user. */
304 int rte_vhost_driver_unregister(const char *path);
305 
316 __rte_experimental
317 int
318 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
319 
328 __rte_experimental
329 int
330 rte_vhost_driver_detach_vdpa_device(const char *path);
331 
340 __rte_experimental
341 int
342 rte_vhost_driver_get_vdpa_device_id(const char *path);
343 
354 int rte_vhost_driver_set_features(const char *path, uint64_t features);
355 
371 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
372 
385 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
386 
397 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
398 
409 __rte_experimental
410 int
412  uint64_t protocol_features);
413 
424 __rte_experimental
425 int
427  uint64_t *protocol_features);
428 
439 __rte_experimental
440 int
441 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
442 
453 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
454 
455 /* Register callbacks. */
456 int rte_vhost_driver_callback_register(const char *path,
457  struct vhost_device_ops const * const ops);
458 
470 int rte_vhost_driver_start(const char *path);
471 
485 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
486 
497 int rte_vhost_get_numa_node(int vid);
498 
513 __rte_deprecated
514 uint32_t rte_vhost_get_queue_num(int vid);
515 
525 uint16_t rte_vhost_get_vring_num(int vid);
526 
541 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
542 
554 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
555 
556 struct rte_mbuf;
557 struct rte_mempool;
574 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
575  struct rte_mbuf **pkts, uint16_t count);
576 
594 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
595  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
596 
611 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
612 
625 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
626  struct rte_vhost_vring *vring);
627 
639 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
640 
651 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
652 
665 __rte_experimental
666 int
667 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
668 
683 __rte_experimental
684 int
685 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
686  uint16_t *last_avail_idx, uint16_t *last_used_idx);
687 
702 __rte_experimental
703 int
704 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
705  uint16_t last_avail_idx, uint16_t last_used_idx);
706 
719 __rte_experimental
720 int
722  struct rte_vhost_user_extern_ops const * const ops, void *ctx);
723 
732 __rte_experimental
733 int
735 
736 #ifdef __cplusplus
737 }
738 #endif
739 
740 #endif /* _RTE_VHOST_H_ */
static __rte_deprecated __rte_always_inline uint64_t rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
Definition: rte_vhost.h:195
int rte_vhost_driver_start(const char *path)
#define __rte_always_inline
Definition: rte_common.h:153
int rte_vhost_driver_register(const char *path, uint64_t flags)
uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id)
int rte_vhost_driver_disable_features(const char *path, uint64_t features)
void rte_vhost_log_used_vring(int vid, uint16_t vring_idx, uint64_t offset, uint64_t len)
__rte_experimental int rte_vhost_extern_callback_register(int vid, struct rte_vhost_user_extern_ops const *const ops, void *ctx)
int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx, struct rte_vhost_vring *vring)
uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id, struct rte_mbuf **pkts, uint16_t count)
__rte_deprecated uint32_t rte_vhost_get_queue_num(int vid)
__rte_experimental int rte_vhost_get_vring_base(int vid, uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx)
__rte_experimental int rte_vhost_set_vring_base(int vid, uint16_t queue_id, uint16_t last_avail_idx, uint16_t last_used_idx)
int rte_vhost_get_mtu(int vid, uint16_t *mtu)
__rte_experimental int rte_vhost_driver_set_protocol_features(const char *path, uint64_t protocol_features)
void(* destroy_device)(int vid)
Definition: rte_vhost.h:160
void * reserved[2]
Definition: rte_vhost.h:175
static __rte_always_inline uint64_t rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem, uint64_t gpa, uint64_t *len)
Definition: rte_vhost.h:229
__rte_experimental int rte_vhost_driver_detach_vdpa_device(const char *path)
#define unlikely(x)
uint16_t rte_vhost_get_vring_num(int vid)
int rte_vhost_get_numa_node(int vid)
int(* features_changed)(int vid, uint64_t features)
Definition: rte_vhost.h:170
__rte_experimental int rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
__rte_experimental int rte_vhost_get_vdpa_device_id(int vid)
int rte_vhost_vring_call(int vid, uint16_t vring_idx)
int rte_vhost_get_negotiated_features(int vid, uint64_t *features)
rte_vhost_msg_result
Definition: rte_vhost.h:118
__rte_experimental int rte_vhost_driver_get_vdpa_device_id(const char *path)
uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid)
int(* new_device)(int vid)
Definition: rte_vhost.h:159
__rte_experimental int rte_vhost_driver_attach_vdpa_device(const char *path, int did)
uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id, struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
__rte_experimental int rte_vhost_driver_get_protocol_features(const char *path, uint64_t *protocol_features)
__rte_experimental int rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size)
int rte_vhost_driver_get_features(const char *path, uint64_t *features)
int rte_vhost_driver_set_features(const char *path, uint64_t features)
int(* vring_state_changed)(int vid, uint16_t queue_id, int enable)
Definition: rte_vhost.h:162
int rte_vhost_get_ifname(int vid, char *buf, size_t len)
enum rte_vhost_msg_result(* rte_vhost_msg_handle)(int vid, void *msg)
Definition: rte_vhost.h:143
int rte_vhost_driver_enable_features(const char *path, uint64_t features)
void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)