DPDK  23.07.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_compat.h>
18 #include <rte_memory.h>
19 #include <rte_mempool.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #ifndef __cplusplus
26 /* These are not C++-aware. */
27 #include <linux/vhost.h>
28 #include <linux/virtio_ring.h>
29 #include <linux/virtio_net.h>
30 #endif
31 
32 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
33 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
34 #define RTE_VHOST_USER_RESERVED_1 (1ULL << 2)
35 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
36 #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4)
37 /* support mbuf with external buffer attached */
38 #define RTE_VHOST_USER_EXTBUF_SUPPORT (1ULL << 5)
39 /* support only linear buffers (no chained mbufs) */
40 #define RTE_VHOST_USER_LINEARBUF_SUPPORT (1ULL << 6)
41 #define RTE_VHOST_USER_ASYNC_COPY (1ULL << 7)
42 #define RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS (1ULL << 8)
43 #define RTE_VHOST_USER_NET_STATS_ENABLE (1ULL << 9)
44 
45 /* Features. */
46 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
47  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
48 #endif
49 
50 #ifndef VIRTIO_NET_F_MQ
51  #define VIRTIO_NET_F_MQ 22
52 #endif
53 
54 #ifndef VIRTIO_NET_F_MTU
55  #define VIRTIO_NET_F_MTU 3
56 #endif
57 
58 #ifndef VIRTIO_F_ANY_LAYOUT
59  #define VIRTIO_F_ANY_LAYOUT 27
60 #endif
61 
63 #ifndef VHOST_USER_PROTOCOL_F_MQ
64 #define VHOST_USER_PROTOCOL_F_MQ 0
65 #endif
66 
67 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
68 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
69 #endif
70 
71 #ifndef VHOST_USER_PROTOCOL_F_RARP
72 #define VHOST_USER_PROTOCOL_F_RARP 2
73 #endif
74 
75 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
76 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
77 #endif
78 
79 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
80 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
81 #endif
82 
83 #ifndef VHOST_USER_PROTOCOL_F_BACKEND_REQ
84 #define VHOST_USER_PROTOCOL_F_BACKEND_REQ 5
85 #endif
86 
87 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
88 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
89 #endif
90 
91 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
92 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
93 #endif
94 
95 #ifndef VHOST_USER_PROTOCOL_F_CONFIG
96 #define VHOST_USER_PROTOCOL_F_CONFIG 9
97 #endif
98 
99 #ifndef VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD
100 #define VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD 10
101 #endif
102 
103 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
104 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
105 #endif
106 
107 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
108 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
109 #endif
110 
111 #ifndef VHOST_USER_PROTOCOL_F_STATUS
112 #define VHOST_USER_PROTOCOL_F_STATUS 16
113 #endif
114 
116 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
117 #define VHOST_USER_F_PROTOCOL_FEATURES 30
118 #endif
119 
120 #define RTE_MAX_VHOST_DEVICE 1024
121 
122 #define RTE_VHOST_VDPA_DEVICE_TYPE_NET 0
123 #define RTE_VHOST_VDPA_DEVICE_TYPE_BLK 1
124 
125 struct rte_vdpa_device;
126 
132  uint64_t guest_phys_addr;
133  uint64_t guest_user_addr;
134  uint64_t host_user_addr;
135  uint64_t size;
136  void *mmap_addr;
137  uint64_t mmap_size;
138  int fd;
139 };
140 
145  uint32_t nregions;
146  struct rte_vhost_mem_region regions[];
147 };
148 
149 struct rte_vhost_inflight_desc_split {
150  uint8_t inflight;
151  uint8_t padding[5];
152  uint16_t next;
153  uint64_t counter;
154 };
155 
156 struct rte_vhost_inflight_info_split {
157  uint64_t features;
158  uint16_t version;
159  uint16_t desc_num;
160  uint16_t last_inflight_io;
161  uint16_t used_idx;
162  struct rte_vhost_inflight_desc_split desc[];
163 };
164 
165 struct rte_vhost_inflight_desc_packed {
166  uint8_t inflight;
167  uint8_t padding;
168  uint16_t next;
169  uint16_t last;
170  uint16_t num;
171  uint64_t counter;
172  uint16_t id;
173  uint16_t flags;
174  uint32_t len;
175  uint64_t addr;
176 };
177 
178 struct rte_vhost_inflight_info_packed {
179  uint64_t features;
180  uint16_t version;
181  uint16_t desc_num;
182  uint16_t free_head;
183  uint16_t old_free_head;
184  uint16_t used_idx;
185  uint16_t old_used_idx;
186  uint8_t used_wrap_counter;
187  uint8_t old_used_wrap_counter;
188  uint8_t padding[7];
189  struct rte_vhost_inflight_desc_packed desc[];
190 };
191 
192 struct rte_vhost_resubmit_desc {
193  uint16_t index;
194  uint64_t counter;
195 };
196 
197 struct rte_vhost_resubmit_info {
198  struct rte_vhost_resubmit_desc *resubmit_list;
199  uint16_t resubmit_num;
200 };
201 
202 struct rte_vhost_ring_inflight {
203  union {
204  struct rte_vhost_inflight_info_split *inflight_split;
205  struct rte_vhost_inflight_info_packed *inflight_packed;
206  };
207 
208  struct rte_vhost_resubmit_info *resubmit_inflight;
209 };
210 
211 struct rte_vhost_vring {
212  union {
213  struct vring_desc *desc;
214  struct vring_packed_desc *desc_packed;
215  };
216  union {
217  struct vring_avail *avail;
218  struct vring_packed_desc_event *driver_event;
219  };
220  union {
221  struct vring_used *used;
222  struct vring_packed_desc_event *device_event;
223  };
224  uint64_t log_guest_addr;
225 
227  int callfd;
228 
229  int kickfd;
230  uint16_t size;
231 };
232 
237  /* Message handling failed */
238  RTE_VHOST_MSG_RESULT_ERR = -1,
239  /* Message handling successful */
240  RTE_VHOST_MSG_RESULT_OK = 0,
241  /* Message handling successful and reply prepared */
242  RTE_VHOST_MSG_RESULT_REPLY = 1,
243  /* Message not handled */
244  RTE_VHOST_MSG_RESULT_NOT_HANDLED,
245 };
246 
261 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
262 
267  /* Called prior to the frontend message handling. */
268  rte_vhost_msg_handle pre_msg_handle;
269  /* Called after the frontend message handling. */
270  rte_vhost_msg_handle post_msg_handle;
271 };
272 
277  int (*new_device)(int vid);
278  void (*destroy_device)(int vid);
280  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
288  int (*features_changed)(int vid, uint64_t features);
289 
290  int (*new_connection)(int vid);
291  void (*destroy_connection)(int vid);
292 
299  void (*guest_notified)(int vid);
300 
307  bool (*guest_notify)(int vid, uint16_t queue_id);
308 };
309 
315  volatile void *addr;
320  uint64_t val;
322  uint64_t mask;
326  uint8_t size;
332  uint8_t match;
333 };
334 
336 #define RTE_VHOST_STATS_NAME_SIZE 64
337 
347  uint64_t id;
348  uint64_t value;
349 };
350 
359 };
360 
373 static __rte_always_inline uint64_t
375  uint64_t gpa, uint64_t *len)
376 {
377  struct rte_vhost_mem_region *r;
378  uint32_t i;
379 
380  for (i = 0; i < mem->nregions; i++) {
381  r = &mem->regions[i];
382  if (gpa >= r->guest_phys_addr &&
383  gpa < r->guest_phys_addr + r->size) {
384 
385  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
386  *len = r->guest_phys_addr + r->size - gpa;
387 
388  return gpa - r->guest_phys_addr +
389  r->host_user_addr;
390  }
391  }
392  *len = 0;
393 
394  return 0;
395 }
396 
397 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
398 
417 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
418 
437 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
438  uint64_t offset, uint64_t len);
439 
440 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
441 
454 __rte_experimental
455 void rte_vhost_notify_guest(int vid, uint16_t queue_id);
456 
461 int rte_vhost_driver_register(const char *path, uint64_t flags);
462 
463 /* Unregister vhost driver. This is only meaningful to vhost user. */
464 int rte_vhost_driver_unregister(const char *path);
465 
476 int
477 rte_vhost_driver_attach_vdpa_device(const char *path,
478  struct rte_vdpa_device *dev);
479 
488 int
489 rte_vhost_driver_detach_vdpa_device(const char *path);
490 
499 struct rte_vdpa_device *
500 rte_vhost_driver_get_vdpa_device(const char *path);
501 
512 __rte_experimental
513 int
514 rte_vhost_driver_get_vdpa_dev_type(const char *path, uint32_t *type);
515 
526 int rte_vhost_driver_set_features(const char *path, uint64_t features);
527 
543 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
544 
557 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
558 
569 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
570 
581 int
583  uint64_t protocol_features);
584 
595 int
597  uint64_t *protocol_features);
598 
609 int
610 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
611 
625 __rte_experimental
626 int
627 rte_vhost_driver_set_max_queue_num(const char *path, uint32_t max_queue_pairs);
628 
639 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
640 
651 __rte_experimental
652 int
654  uint64_t *protocol_features);
655 
656 /* Register callbacks. */
657 int rte_vhost_driver_callback_register(const char *path,
658  struct rte_vhost_device_ops const * const ops);
659 
671 int rte_vhost_driver_start(const char *path);
672 
686 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
687 
698 int rte_vhost_get_numa_node(int vid);
699 
709 uint16_t rte_vhost_get_vring_num(int vid);
710 
725 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
726 
738 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
739 
740 struct rte_mbuf;
741 struct rte_mempool;
758 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
759  struct rte_mbuf **pkts, uint16_t count);
760 
778 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
779  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
780 
795 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
796 
809 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
810  struct rte_vhost_vring *vring);
811 
824 int
825 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
826  struct rte_vhost_ring_inflight *vring);
827 
843 int
844 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
845  uint16_t idx);
846 
865 int
866 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
867  uint16_t head, uint16_t last, uint16_t *inflight_entry);
868 
881 int
883  uint16_t vring_idx, uint16_t idx);
884 
900 int
902  uint16_t vring_idx, uint16_t head);
903 
918 int
919 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
920  uint16_t last_used_idx, uint16_t idx);
921 
934 int
935 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
936  uint16_t head);
937 
949 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
950 
963 __rte_experimental
964 int rte_vhost_vring_call_nonblock(int vid, uint16_t vring_idx);
965 
976 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
977 
990 __rte_experimental
991 int
992 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
993  struct rte_vhost_power_monitor_cond *pmc);
994 
1007 int
1008 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
1009 
1024 int
1025 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1026  uint16_t *last_avail_idx, uint16_t *last_used_idx);
1027 
1046 int
1048  uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
1049 
1064 int
1065 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1066  uint16_t last_avail_idx, uint16_t last_used_idx);
1067 
1080 int
1082  struct rte_vhost_user_extern_ops const * const ops, void *ctx);
1083 
1092 struct rte_vdpa_device *
1093 rte_vhost_get_vdpa_device(int vid);
1094 
1105 __rte_experimental
1106 int
1107 rte_vhost_backend_config_change(int vid, bool need_reply);
1108 
1132 int
1133 rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id,
1134  struct rte_vhost_stat_name *name, unsigned int size);
1135 
1159 int
1160 rte_vhost_vring_stats_get(int vid, uint16_t queue_id,
1161  struct rte_vhost_stat *stats, unsigned int n);
1162 
1175 int
1176 rte_vhost_vring_stats_reset(int vid, uint16_t queue_id);
1177 
1178 #ifdef __cplusplus
1179 }
1180 #endif
1181 
1182 #endif /* _RTE_VHOST_H_ */
int rte_vhost_driver_start(const char *path)
#define __rte_always_inline
Definition: rte_common.h:255
__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_vring_stats_reset(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)
__rte_experimental int rte_vhost_driver_set_max_queue_num(const char *path, uint32_t max_queue_pairs)
void rte_vhost_log_used_vring(int vid, uint16_t vring_idx, uint64_t offset, uint64_t len)
#define RTE_VHOST_STATS_NAME_SIZE
Definition: rte_vhost.h:336
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)
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)
uint64_t id
Definition: rte_vhost.h:347
int rte_vhost_vring_stats_get(int vid, uint16_t queue_id, struct rte_vhost_stat *stats, unsigned int n)
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:374
#define unlikely(x)
uint16_t rte_vhost_get_vring_num(int vid)
__rte_experimental int rte_vhost_vring_call_nonblock(int vid, uint16_t vring_idx)
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)
__rte_experimental void rte_vhost_notify_guest(int vid, uint16_t queue_id)
int rte_vhost_driver_attach_vdpa_device(const char *path, struct rte_vdpa_device *dev)
__rte_experimental int rte_vhost_driver_get_vdpa_dev_type(const char *path, uint32_t *type)
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:236
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)
int rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id, struct rte_vhost_stat_name *name, unsigned int size)
uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid)
uint64_t value
Definition: rte_vhost.h:348
__rte_experimental int rte_vhost_backend_config_change(int vid, bool need_reply)
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:261
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)