DPDK  22.03.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 #ifndef __cplusplus
25 /* These are not C++-aware. */
26 #include <linux/vhost.h>
27 #include <linux/virtio_ring.h>
28 #include <linux/virtio_net.h>
29 #endif
30 
31 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
32 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
33 #define RTE_VHOST_USER_RESERVED_1 (1ULL << 2)
34 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
35 #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4)
36 /* support mbuf with external buffer attached */
37 #define RTE_VHOST_USER_EXTBUF_SUPPORT (1ULL << 5)
38 /* support only linear buffers (no chained mbufs) */
39 #define RTE_VHOST_USER_LINEARBUF_SUPPORT (1ULL << 6)
40 #define RTE_VHOST_USER_ASYNC_COPY (1ULL << 7)
41 #define RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS (1ULL << 8)
42 
43 /* Features. */
44 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
45  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
46 #endif
47 
48 #ifndef VIRTIO_NET_F_MQ
49  #define VIRTIO_NET_F_MQ 22
50 #endif
51 
52 #ifndef VIRTIO_NET_F_MTU
53  #define VIRTIO_NET_F_MTU 3
54 #endif
55 
56 #ifndef VIRTIO_F_ANY_LAYOUT
57  #define VIRTIO_F_ANY_LAYOUT 27
58 #endif
59 
61 #ifndef VHOST_USER_PROTOCOL_F_MQ
62 #define VHOST_USER_PROTOCOL_F_MQ 0
63 #endif
64 
65 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
66 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
67 #endif
68 
69 #ifndef VHOST_USER_PROTOCOL_F_RARP
70 #define VHOST_USER_PROTOCOL_F_RARP 2
71 #endif
72 
73 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
74 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
75 #endif
76 
77 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
78 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
79 #endif
80 
81 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
82 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
83 #endif
84 
85 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
86 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
87 #endif
88 
89 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
90 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
91 #endif
92 
93 #ifndef VHOST_USER_PROTOCOL_F_CONFIG
94 #define VHOST_USER_PROTOCOL_F_CONFIG 9
95 #endif
96 
97 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
98 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
99 #endif
100 
101 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
102 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
103 #endif
104 
105 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
106 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
107 #endif
108 
109 #ifndef VHOST_USER_PROTOCOL_F_STATUS
110 #define VHOST_USER_PROTOCOL_F_STATUS 16
111 #endif
112 
114 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
115 #define VHOST_USER_F_PROTOCOL_FEATURES 30
116 #endif
117 
118 #define RTE_MAX_VHOST_DEVICE 1024
119 
120 struct rte_vdpa_device;
121 
127  uint64_t guest_phys_addr;
128  uint64_t guest_user_addr;
129  uint64_t host_user_addr;
130  uint64_t size;
131  void *mmap_addr;
132  uint64_t mmap_size;
133  int fd;
134 };
135 
140  uint32_t nregions;
141  struct rte_vhost_mem_region regions[];
142 };
143 
144 struct rte_vhost_inflight_desc_split {
145  uint8_t inflight;
146  uint8_t padding[5];
147  uint16_t next;
148  uint64_t counter;
149 };
150 
151 struct rte_vhost_inflight_info_split {
152  uint64_t features;
153  uint16_t version;
154  uint16_t desc_num;
155  uint16_t last_inflight_io;
156  uint16_t used_idx;
157  struct rte_vhost_inflight_desc_split desc[0];
158 };
159 
160 struct rte_vhost_inflight_desc_packed {
161  uint8_t inflight;
162  uint8_t padding;
163  uint16_t next;
164  uint16_t last;
165  uint16_t num;
166  uint64_t counter;
167  uint16_t id;
168  uint16_t flags;
169  uint32_t len;
170  uint64_t addr;
171 };
172 
173 struct rte_vhost_inflight_info_packed {
174  uint64_t features;
175  uint16_t version;
176  uint16_t desc_num;
177  uint16_t free_head;
178  uint16_t old_free_head;
179  uint16_t used_idx;
180  uint16_t old_used_idx;
181  uint8_t used_wrap_counter;
182  uint8_t old_used_wrap_counter;
183  uint8_t padding[7];
184  struct rte_vhost_inflight_desc_packed desc[0];
185 };
186 
187 struct rte_vhost_resubmit_desc {
188  uint16_t index;
189  uint64_t counter;
190 };
191 
192 struct rte_vhost_resubmit_info {
193  struct rte_vhost_resubmit_desc *resubmit_list;
194  uint16_t resubmit_num;
195 };
196 
197 struct rte_vhost_ring_inflight {
198  union {
199  struct rte_vhost_inflight_info_split *inflight_split;
200  struct rte_vhost_inflight_info_packed *inflight_packed;
201  };
202 
203  struct rte_vhost_resubmit_info *resubmit_inflight;
204 };
205 
206 struct rte_vhost_vring {
207  union {
208  struct vring_desc *desc;
209  struct vring_packed_desc *desc_packed;
210  };
211  union {
212  struct vring_avail *avail;
213  struct vring_packed_desc_event *driver_event;
214  };
215  union {
216  struct vring_used *used;
217  struct vring_packed_desc_event *device_event;
218  };
219  uint64_t log_guest_addr;
220 
222  int callfd;
223 
224  int kickfd;
225  uint16_t size;
226 };
227 
232  /* Message handling failed */
233  RTE_VHOST_MSG_RESULT_ERR = -1,
234  /* Message handling successful */
235  RTE_VHOST_MSG_RESULT_OK = 0,
236  /* Message handling successful and reply prepared */
237  RTE_VHOST_MSG_RESULT_REPLY = 1,
238  /* Message not handled */
239  RTE_VHOST_MSG_RESULT_NOT_HANDLED,
240 };
241 
256 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
257 
262  /* Called prior to the master message handling. */
263  rte_vhost_msg_handle pre_msg_handle;
264  /* Called after the master message handling. */
265  rte_vhost_msg_handle post_msg_handle;
266 };
267 
272  int (*new_device)(int vid);
273  void (*destroy_device)(int vid);
275  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
283  int (*features_changed)(int vid, uint64_t features);
284 
285  int (*new_connection)(int vid);
286  void (*destroy_connection)(int vid);
287 
294  void (*guest_notified)(int vid);
295 
296  void *reserved[1];
297 };
298 
304  volatile void *addr;
309  uint64_t val;
311  uint64_t mask;
315  uint8_t size;
321  uint8_t match;
322 };
323 
339 __rte_deprecated
340 static __rte_always_inline uint64_t
341 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
342 {
343  struct rte_vhost_mem_region *reg;
344  uint32_t i;
345 
346  for (i = 0; i < mem->nregions; i++) {
347  reg = &mem->regions[i];
348  if (gpa >= reg->guest_phys_addr &&
349  gpa < reg->guest_phys_addr + reg->size) {
350  return gpa - reg->guest_phys_addr +
351  reg->host_user_addr;
352  }
353  }
354 
355  return 0;
356 }
357 
374 static __rte_always_inline uint64_t
376  uint64_t gpa, uint64_t *len)
377 {
378  struct rte_vhost_mem_region *r;
379  uint32_t i;
380 
381  for (i = 0; i < mem->nregions; i++) {
382  r = &mem->regions[i];
383  if (gpa >= r->guest_phys_addr &&
384  gpa < r->guest_phys_addr + r->size) {
385 
386  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
387  *len = r->guest_phys_addr + r->size - gpa;
388 
389  return gpa - r->guest_phys_addr +
390  r->host_user_addr;
391  }
392  }
393  *len = 0;
394 
395  return 0;
396 }
397 
398 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
399 
418 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
419 
438 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
439  uint64_t offset, uint64_t len);
440 
441 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
442 
447 int rte_vhost_driver_register(const char *path, uint64_t flags);
448 
449 /* Unregister vhost driver. This is only meaningful to vhost user. */
450 int rte_vhost_driver_unregister(const char *path);
451 
462 int
463 rte_vhost_driver_attach_vdpa_device(const char *path,
464  struct rte_vdpa_device *dev);
465 
474 int
475 rte_vhost_driver_detach_vdpa_device(const char *path);
476 
485 struct rte_vdpa_device *
486 rte_vhost_driver_get_vdpa_device(const char *path);
487 
498 int rte_vhost_driver_set_features(const char *path, uint64_t features);
499 
515 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
516 
529 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
530 
541 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
542 
553 int
555  uint64_t protocol_features);
556 
567 int
569  uint64_t *protocol_features);
570 
581 int
582 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
583 
594 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
595 
606 __rte_experimental
607 int
609  uint64_t *protocol_features);
610 
611 /* Register callbacks. */
612 int rte_vhost_driver_callback_register(const char *path,
613  struct rte_vhost_device_ops const * const ops);
614 
626 int rte_vhost_driver_start(const char *path);
627 
641 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
642 
653 int rte_vhost_get_numa_node(int vid);
654 
669 __rte_deprecated
670 uint32_t rte_vhost_get_queue_num(int vid);
671 
681 uint16_t rte_vhost_get_vring_num(int vid);
682 
697 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
698 
710 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
711 
712 struct rte_mbuf;
713 struct rte_mempool;
730 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
731  struct rte_mbuf **pkts, uint16_t count);
732 
750 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
751  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
752 
767 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
768 
781 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
782  struct rte_vhost_vring *vring);
783 
796 int
797 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
798  struct rte_vhost_ring_inflight *vring);
799 
815 int
816 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
817  uint16_t idx);
818 
837 int
838 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
839  uint16_t head, uint16_t last, uint16_t *inflight_entry);
840 
853 int
855  uint16_t vring_idx, uint16_t idx);
856 
872 int
874  uint16_t vring_idx, uint16_t head);
875 
890 int
891 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
892  uint16_t last_used_idx, uint16_t idx);
893 
906 int
907 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
908  uint16_t head);
909 
921 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
922 
933 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
934 
947 __rte_experimental
948 int
949 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
950  struct rte_vhost_power_monitor_cond *pmc);
951 
964 int
965 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
966 
981 int
982 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
983  uint16_t *last_avail_idx, uint16_t *last_used_idx);
984 
1003 int
1005  uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
1006 
1021 int
1022 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1023  uint16_t last_avail_idx, uint16_t last_used_idx);
1024 
1037 int
1039  struct rte_vhost_user_extern_ops const * const ops, void *ctx);
1040 
1049 struct rte_vdpa_device *
1050 rte_vhost_get_vdpa_device(int vid);
1051 
1062 __rte_experimental
1063 int
1064 rte_vhost_slave_config_change(int vid, bool need_reply);
1065 
1066 #ifdef __cplusplus
1067 }
1068 #endif
1069 
1070 #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:341
int rte_vhost_driver_start(const char *path)
#define __rte_always_inline
Definition: rte_common.h:228
__rte_experimental int rte_vhost_get_negotiated_protocol_features(int vid, uint64_t *protocol_features)
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)
struct rte_vdpa_device * rte_vhost_get_vdpa_device(int vid)
void rte_vhost_log_used_vring(int vid, uint16_t vring_idx, uint64_t offset, uint64_t len)
int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx, struct rte_vhost_vring *vring)
int rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx, uint16_t head, uint16_t last, uint16_t *inflight_entry)
int rte_vhost_clr_inflight_desc_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)
int rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx, struct rte_vhost_ring_inflight *vring)
int rte_vhost_driver_get_protocol_features(const char *path, uint64_t *protocol_features)
int rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx, uint16_t head)
int rte_vhost_get_mtu(int vid, uint16_t *mtu)
int rte_vhost_driver_detach_vdpa_device(const char *path)
int rte_vhost_driver_set_protocol_features(const char *path, uint64_t protocol_features)
__rte_experimental int rte_vhost_slave_config_change(int vid, bool need_reply)
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:375
#define unlikely(x)
uint16_t rte_vhost_get_vring_num(int vid)
int rte_vhost_extern_callback_register(int vid, struct rte_vhost_user_extern_ops const *const ops, void *ctx)
__rte_experimental int rte_vhost_get_monitor_addr(int vid, uint16_t queue_id, struct rte_vhost_power_monitor_cond *pmc)
int rte_vhost_get_numa_node(int vid)
int rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx, uint16_t idx)
int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
int rte_vhost_driver_attach_vdpa_device(const char *path, struct rte_vdpa_device *dev)
int rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx, uint16_t idx)
int rte_vhost_vring_call(int vid, uint16_t vring_idx)
int rte_vhost_get_negotiated_features(int vid, uint64_t *features)
struct rte_vdpa_device * rte_vhost_driver_get_vdpa_device(const char *path)
rte_vhost_msg_result
Definition: rte_vhost.h:231
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_clr_inflight_desc_split(int vid, uint16_t vring_idx, uint16_t last_used_idx, uint16_t idx)
int rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size)
uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid)
uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id, struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
int rte_vhost_get_vring_base(int vid, uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx)
int rte_vhost_driver_get_features(const char *path, uint64_t *features)
int rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
int rte_vhost_driver_set_features(const char *path, uint64_t features)
int rte_vhost_get_ifname(int vid, char *buf, size_t len)
int rte_vhost_get_vring_base_from_inflight(int vid, uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx)
enum rte_vhost_msg_result(* rte_vhost_msg_handle)(int vid, void *msg)
Definition: rte_vhost.h:256
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)