DPDK  20.05.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 <stdbool.h>
14 #include <stdint.h>
15 #include <sys/eventfd.h>
16 
17 #include <rte_memory.h>
18 #include <rte_mempool.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /* These are not C++-aware. */
25 #include <linux/vhost.h>
26 #include <linux/virtio_ring.h>
27 #include <linux/virtio_net.h>
28 
29 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
30 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
31 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
32 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
33 #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4)
34 /* support mbuf with external buffer attached */
35 #define RTE_VHOST_USER_EXTBUF_SUPPORT (1ULL << 5)
36 /* support only linear buffers (no chained mbufs) */
37 #define RTE_VHOST_USER_LINEARBUF_SUPPORT (1ULL << 6)
38 
40 #ifndef VHOST_USER_PROTOCOL_F_MQ
41 #define VHOST_USER_PROTOCOL_F_MQ 0
42 #endif
43 
44 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
45 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
46 #endif
47 
48 #ifndef VHOST_USER_PROTOCOL_F_RARP
49 #define VHOST_USER_PROTOCOL_F_RARP 2
50 #endif
51 
52 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
53 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
54 #endif
55 
56 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
57 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
58 #endif
59 
60 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
61 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
62 #endif
63 
64 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
65 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
66 #endif
67 
68 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
69 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
70 #endif
71 
72 #ifndef VHOST_USER_PROTOCOL_F_CONFIG
73 #define VHOST_USER_PROTOCOL_F_CONFIG 9
74 #endif
75 
76 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
77 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
78 #endif
79 
80 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
81 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
82 #endif
83 
84 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
85 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
86 #endif
87 
89 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
90 #define VHOST_USER_F_PROTOCOL_FEATURES 30
91 #endif
92 
93 
99  uint64_t guest_phys_addr;
100  uint64_t guest_user_addr;
101  uint64_t host_user_addr;
102  uint64_t size;
103  void *mmap_addr;
104  uint64_t mmap_size;
105  int fd;
106 };
107 
112  uint32_t nregions;
113  struct rte_vhost_mem_region regions[];
114 };
115 
116 struct rte_vhost_inflight_desc_split {
117  uint8_t inflight;
118  uint8_t padding[5];
119  uint16_t next;
120  uint64_t counter;
121 };
122 
123 struct rte_vhost_inflight_info_split {
124  uint64_t features;
125  uint16_t version;
126  uint16_t desc_num;
127  uint16_t last_inflight_io;
128  uint16_t used_idx;
129  struct rte_vhost_inflight_desc_split desc[0];
130 };
131 
132 struct rte_vhost_inflight_desc_packed {
133  uint8_t inflight;
134  uint8_t padding;
135  uint16_t next;
136  uint16_t last;
137  uint16_t num;
138  uint64_t counter;
139  uint16_t id;
140  uint16_t flags;
141  uint32_t len;
142  uint64_t addr;
143 };
144 
145 struct rte_vhost_inflight_info_packed {
146  uint64_t features;
147  uint16_t version;
148  uint16_t desc_num;
149  uint16_t free_head;
150  uint16_t old_free_head;
151  uint16_t used_idx;
152  uint16_t old_used_idx;
153  uint8_t used_wrap_counter;
154  uint8_t old_used_wrap_counter;
155  uint8_t padding[7];
156  struct rte_vhost_inflight_desc_packed desc[0];
157 };
158 
159 struct rte_vhost_resubmit_desc {
160  uint16_t index;
161  uint64_t counter;
162 };
163 
164 struct rte_vhost_resubmit_info {
165  struct rte_vhost_resubmit_desc *resubmit_list;
166  uint16_t resubmit_num;
167 };
168 
169 struct rte_vhost_ring_inflight {
170  union {
171  struct rte_vhost_inflight_info_split *inflight_split;
172  struct rte_vhost_inflight_info_packed *inflight_packed;
173  };
174 
175  struct rte_vhost_resubmit_info *resubmit_inflight;
176 };
177 
178 struct rte_vhost_vring {
179  union {
180  struct vring_desc *desc;
181  struct vring_packed_desc *desc_packed;
182  };
183  union {
184  struct vring_avail *avail;
185  struct vring_packed_desc_event *driver_event;
186  };
187  union {
188  struct vring_used *used;
189  struct vring_packed_desc_event *device_event;
190  };
191  uint64_t log_guest_addr;
192 
194  int callfd;
195 
196  int kickfd;
197  uint16_t size;
198 };
199 
204  /* Message handling failed */
205  RTE_VHOST_MSG_RESULT_ERR = -1,
206  /* Message handling successful */
207  RTE_VHOST_MSG_RESULT_OK = 0,
208  /* Message handling successful and reply prepared */
209  RTE_VHOST_MSG_RESULT_REPLY = 1,
210  /* Message not handled */
211  RTE_VHOST_MSG_RESULT_NOT_HANDLED,
212 };
213 
228 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
229 
234  /* Called prior to the master message handling. */
235  rte_vhost_msg_handle pre_msg_handle;
236  /* Called after the master message handling. */
237  rte_vhost_msg_handle post_msg_handle;
238 };
239 
244  int (*new_device)(int vid);
245  void (*destroy_device)(int vid);
247  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
255  int (*features_changed)(int vid, uint64_t features);
256 
257  int (*new_connection)(int vid);
258  void (*destroy_connection)(int vid);
259 
266  void (*guest_notified)(int vid);
267 
268  void *reserved[1];
269 };
270 
286 __rte_deprecated
287 static __rte_always_inline uint64_t
288 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
289 {
290  struct rte_vhost_mem_region *reg;
291  uint32_t i;
292 
293  for (i = 0; i < mem->nregions; i++) {
294  reg = &mem->regions[i];
295  if (gpa >= reg->guest_phys_addr &&
296  gpa < reg->guest_phys_addr + reg->size) {
297  return gpa - reg->guest_phys_addr +
298  reg->host_user_addr;
299  }
300  }
301 
302  return 0;
303 }
304 
321 __rte_experimental
322 static __rte_always_inline uint64_t
324  uint64_t gpa, uint64_t *len)
325 {
326  struct rte_vhost_mem_region *r;
327  uint32_t i;
328 
329  for (i = 0; i < mem->nregions; i++) {
330  r = &mem->regions[i];
331  if (gpa >= r->guest_phys_addr &&
332  gpa < r->guest_phys_addr + r->size) {
333 
334  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
335  *len = r->guest_phys_addr + r->size - gpa;
336 
337  return gpa - r->guest_phys_addr +
338  r->host_user_addr;
339  }
340  }
341  *len = 0;
342 
343  return 0;
344 }
345 
346 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
347 
366 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
367 
386 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
387  uint64_t offset, uint64_t len);
388 
389 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
390 
395 int rte_vhost_driver_register(const char *path, uint64_t flags);
396 
397 /* Unregister vhost driver. This is only meaningful to vhost user. */
398 int rte_vhost_driver_unregister(const char *path);
399 
410 __rte_experimental
411 int
412 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
413 
422 __rte_experimental
423 int
424 rte_vhost_driver_detach_vdpa_device(const char *path);
425 
434 __rte_experimental
435 int
436 rte_vhost_driver_get_vdpa_device_id(const char *path);
437 
448 int rte_vhost_driver_set_features(const char *path, uint64_t features);
449 
465 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
466 
479 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
480 
491 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
492 
503 __rte_experimental
504 int
506  uint64_t protocol_features);
507 
518 __rte_experimental
519 int
521  uint64_t *protocol_features);
522 
533 __rte_experimental
534 int
535 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
536 
547 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
548 
549 /* Register callbacks. */
550 int rte_vhost_driver_callback_register(const char *path,
551  struct vhost_device_ops const * const ops);
552 
564 int rte_vhost_driver_start(const char *path);
565 
579 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
580 
591 int rte_vhost_get_numa_node(int vid);
592 
607 __rte_deprecated
608 uint32_t rte_vhost_get_queue_num(int vid);
609 
619 uint16_t rte_vhost_get_vring_num(int vid);
620 
635 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
636 
648 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
649 
650 struct rte_mbuf;
651 struct rte_mempool;
668 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
669  struct rte_mbuf **pkts, uint16_t count);
670 
688 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
689  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
690 
705 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
706 
719 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
720  struct rte_vhost_vring *vring);
721 
734 __rte_experimental
735 int
736 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
737  struct rte_vhost_ring_inflight *vring);
738 
754 __rte_experimental
755 int
756 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
757  uint16_t idx);
758 
777 __rte_experimental
778 int
779 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
780  uint16_t head, uint16_t last, uint16_t *inflight_entry);
781 
794 __rte_experimental
795 int
797  uint16_t vring_idx, uint16_t idx);
798 
814 __rte_experimental
815 int
817  uint16_t vring_idx, uint16_t head);
818 
833 __rte_experimental
834 int
835 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
836  uint16_t last_used_idx, uint16_t idx);
837 
850 __rte_experimental
851 int
852 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
853  uint16_t head);
854 
866 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
867 
878 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
879 
892 __rte_experimental
893 int
894 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
895 
910 __rte_experimental
911 int
912 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
913  uint16_t *last_avail_idx, uint16_t *last_used_idx);
914 
933 __rte_experimental
934 int
936  uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
937 
952 __rte_experimental
953 int
954 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
955  uint16_t last_avail_idx, uint16_t last_used_idx);
956 
969 __rte_experimental
970 int
972  struct rte_vhost_user_extern_ops const * const ops, void *ctx);
973 
982 __rte_experimental
983 int
985 
996 __rte_experimental
997 int
998 rte_vhost_slave_config_change(int vid, bool need_reply);
999 
1000 #ifdef __cplusplus
1001 }
1002 #endif
1003 
1004 #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:288
__rte_experimental int rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx, uint16_t idx)
int rte_vhost_driver_start(const char *path)
#define __rte_always_inline
Definition: rte_common.h:193
void * reserved[1]
Definition: rte_vhost.h:268
int rte_vhost_driver_register(const char *path, uint64_t flags)
uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id)
__rte_experimental int rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx, uint16_t last_used_idx, uint16_t idx)
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)
__rte_experimental int rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx, uint16_t head)
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)
void(* guest_notified)(int vid)
Definition: rte_vhost.h:266
__rte_experimental int rte_vhost_slave_config_change(int vid, bool need_reply)
__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:245
__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)
static __rte_experimental __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:323
int(* features_changed)(int vid, uint64_t features)
Definition: rte_vhost.h:255
__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:203
__rte_experimental int rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx, uint16_t head, uint16_t last, uint16_t *inflight_entry)
__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:244
__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_clr_inflight_desc_packed(int vid, uint16_t vring_idx, uint16_t head)
__rte_experimental int rte_vhost_driver_get_protocol_features(const char *path, uint64_t *protocol_features)
__rte_experimental int rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx, struct rte_vhost_ring_inflight *vring)
__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:247
int rte_vhost_get_ifname(int vid, char *buf, size_t len)
__rte_experimental int rte_vhost_get_vring_base_from_inflight(int vid, uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx)
__rte_experimental int rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx, uint16_t idx)
enum rte_vhost_msg_result(* rte_vhost_msg_handle)(int vid, void *msg)
Definition: rte_vhost.h:228
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)