DPDK  24.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 #define RTE_VHOST_USER_ASYNC_CONNECT (1ULL << 10)
45 
46 /* Features. */
47 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
48  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
49 #endif
50 
51 #ifndef VIRTIO_NET_F_MQ
52  #define VIRTIO_NET_F_MQ 22
53 #endif
54 
55 #ifndef VIRTIO_NET_F_MTU
56  #define VIRTIO_NET_F_MTU 3
57 #endif
58 
59 #ifndef VIRTIO_F_ANY_LAYOUT
60  #define VIRTIO_F_ANY_LAYOUT 27
61 #endif
62 
64 #ifndef VHOST_USER_PROTOCOL_F_MQ
65 #define VHOST_USER_PROTOCOL_F_MQ 0
66 #endif
67 
68 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
69 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
70 #endif
71 
72 #ifndef VHOST_USER_PROTOCOL_F_RARP
73 #define VHOST_USER_PROTOCOL_F_RARP 2
74 #endif
75 
76 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
77 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
78 #endif
79 
80 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
81 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
82 #endif
83 
84 #ifndef VHOST_USER_PROTOCOL_F_BACKEND_REQ
85 #define VHOST_USER_PROTOCOL_F_BACKEND_REQ 5
86 #endif
87 
88 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
89 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
90 #endif
91 
92 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
93 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
94 #endif
95 
96 #ifndef VHOST_USER_PROTOCOL_F_CONFIG
97 #define VHOST_USER_PROTOCOL_F_CONFIG 9
98 #endif
99 
100 #ifndef VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD
101 #define VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD 10
102 #endif
103 
104 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
105 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
106 #endif
107 
108 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
109 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
110 #endif
111 
112 #ifndef VHOST_USER_PROTOCOL_F_STATUS
113 #define VHOST_USER_PROTOCOL_F_STATUS 16
114 #endif
115 
117 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
118 #define VHOST_USER_F_PROTOCOL_FEATURES 30
119 #endif
120 
121 #define RTE_MAX_VHOST_DEVICE 1024
122 
123 #define RTE_VHOST_VDPA_DEVICE_TYPE_NET 0
124 #define RTE_VHOST_VDPA_DEVICE_TYPE_BLK 1
125 
126 struct rte_vdpa_device;
127 
133  uint64_t guest_phys_addr;
134  uint64_t guest_user_addr;
135  uint64_t host_user_addr;
136  uint64_t size;
137  void *mmap_addr;
138  uint64_t mmap_size;
139  int fd;
140 };
141 
146  uint32_t nregions;
147  struct rte_vhost_mem_region regions[];
148 };
149 
150 struct rte_vhost_inflight_desc_split {
151  uint8_t inflight;
152  uint8_t padding[5];
153  uint16_t next;
154  uint64_t counter;
155 };
156 
157 struct rte_vhost_inflight_info_split {
158  uint64_t features;
159  uint16_t version;
160  uint16_t desc_num;
161  uint16_t last_inflight_io;
162  uint16_t used_idx;
163  struct rte_vhost_inflight_desc_split desc[];
164 };
165 
166 struct rte_vhost_inflight_desc_packed {
167  uint8_t inflight;
168  uint8_t padding;
169  uint16_t next;
170  uint16_t last;
171  uint16_t num;
172  uint64_t counter;
173  uint16_t id;
174  uint16_t flags;
175  uint32_t len;
176  uint64_t addr;
177 };
178 
179 struct rte_vhost_inflight_info_packed {
180  uint64_t features;
181  uint16_t version;
182  uint16_t desc_num;
183  uint16_t free_head;
184  uint16_t old_free_head;
185  uint16_t used_idx;
186  uint16_t old_used_idx;
187  uint8_t used_wrap_counter;
188  uint8_t old_used_wrap_counter;
189  uint8_t padding[7];
190  struct rte_vhost_inflight_desc_packed desc[];
191 };
192 
193 struct rte_vhost_resubmit_desc {
194  uint16_t index;
195  uint64_t counter;
196 };
197 
198 struct rte_vhost_resubmit_info {
199  struct rte_vhost_resubmit_desc *resubmit_list;
200  uint16_t resubmit_num;
201 };
202 
203 struct rte_vhost_ring_inflight {
204  union {
205  struct rte_vhost_inflight_info_split *inflight_split;
206  struct rte_vhost_inflight_info_packed *inflight_packed;
207  };
208 
209  struct rte_vhost_resubmit_info *resubmit_inflight;
210 };
211 
212 struct rte_vhost_vring {
213  union {
214  struct vring_desc *desc;
215  struct vring_packed_desc *desc_packed;
216  };
217  union {
218  struct vring_avail *avail;
219  struct vring_packed_desc_event *driver_event;
220  };
221  union {
222  struct vring_used *used;
223  struct vring_packed_desc_event *device_event;
224  };
225  uint64_t log_guest_addr;
226 
228  int callfd;
229 
230  int kickfd;
231  uint16_t size;
232 };
233 
238  /* Message handling failed */
239  RTE_VHOST_MSG_RESULT_ERR = -1,
240  /* Message handling successful */
241  RTE_VHOST_MSG_RESULT_OK = 0,
242  /* Message handling successful and reply prepared */
243  RTE_VHOST_MSG_RESULT_REPLY = 1,
244  /* Message not handled */
245  RTE_VHOST_MSG_RESULT_NOT_HANDLED,
246 };
247 
262 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
263 
268  /* Called prior to the frontend message handling. */
269  rte_vhost_msg_handle pre_msg_handle;
270  /* Called after the frontend message handling. */
271  rte_vhost_msg_handle post_msg_handle;
272 };
273 
278  int (*new_device)(int vid);
279  void (*destroy_device)(int vid);
281  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
289  int (*features_changed)(int vid, uint64_t features);
290 
291  int (*new_connection)(int vid);
292  void (*destroy_connection)(int vid);
293 
300  void (*guest_notified)(int vid);
301 
308  bool (*guest_notify)(int vid, uint16_t queue_id);
309 };
310 
316  volatile void *addr;
321  uint64_t val;
323  uint64_t mask;
327  uint8_t size;
333  uint8_t match;
334 };
335 
337 #define RTE_VHOST_STATS_NAME_SIZE 64
338 
348  uint64_t id;
349  uint64_t value;
350 };
351 
360 };
361 
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 
455 __rte_experimental
456 void rte_vhost_notify_guest(int vid, uint16_t queue_id);
457 
462 int rte_vhost_driver_register(const char *path, uint64_t flags);
463 
464 /* Unregister vhost driver. This is only meaningful to vhost user. */
465 int rte_vhost_driver_unregister(const char *path);
466 
477 int
478 rte_vhost_driver_attach_vdpa_device(const char *path,
479  struct rte_vdpa_device *dev);
480 
489 int
490 rte_vhost_driver_detach_vdpa_device(const char *path);
491 
500 struct rte_vdpa_device *
501 rte_vhost_driver_get_vdpa_device(const char *path);
502 
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 int
653  uint64_t *protocol_features);
654 
655 /* Register callbacks. */
656 int rte_vhost_driver_callback_register(const char *path,
657  struct rte_vhost_device_ops const * const ops);
658 
670 int rte_vhost_driver_start(const char *path);
671 
685 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
686 
697 int rte_vhost_get_numa_node(int vid);
698 
708 uint16_t rte_vhost_get_vring_num(int vid);
709 
724 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
725 
737 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
738 
739 struct rte_mbuf;
740 struct rte_mempool;
757 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
758  struct rte_mbuf **pkts, uint16_t count);
759 
777 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
778  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
779 
794 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
795 
808 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
809  struct rte_vhost_vring *vring);
810 
823 int
824 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
825  struct rte_vhost_ring_inflight *vring);
826 
842 int
843 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
844  uint16_t idx);
845 
864 int
865 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
866  uint16_t head, uint16_t last, uint16_t *inflight_entry);
867 
880 int
882  uint16_t vring_idx, uint16_t idx);
883 
899 int
901  uint16_t vring_idx, uint16_t head);
902 
917 int
918 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
919  uint16_t last_used_idx, uint16_t idx);
920 
933 int
934 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
935  uint16_t head);
936 
948 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
949 
962 int rte_vhost_vring_call_nonblock(int vid, uint16_t vring_idx);
963 
974 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
975 
988 int
989 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
990  struct rte_vhost_power_monitor_cond *pmc);
991 
1004 int
1005 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
1006 
1021 int
1022 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1023  uint16_t *last_avail_idx, uint16_t *last_used_idx);
1024 
1043 int
1045  uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
1046 
1061 int
1062 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1063  uint16_t last_avail_idx, uint16_t last_used_idx);
1064 
1077 int
1079  struct rte_vhost_user_extern_ops const * const ops, void *ctx);
1080 
1089 struct rte_vdpa_device *
1090 rte_vhost_get_vdpa_device(int vid);
1091 
1102 int
1103 rte_vhost_backend_config_change(int vid, bool need_reply);
1104 
1128 int
1129 rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id,
1130  struct rte_vhost_stat_name *name, unsigned int size);
1131 
1155 int
1156 rte_vhost_vring_stats_get(int vid, uint16_t queue_id,
1157  struct rte_vhost_stat *stats, unsigned int n);
1158 
1171 int
1172 rte_vhost_vring_stats_reset(int vid, uint16_t queue_id);
1173 
1174 #ifdef __cplusplus
1175 }
1176 #endif
1177 
1178 #endif /* _RTE_VHOST_H_ */
int rte_vhost_driver_start(const char *path)
#define __rte_always_inline
Definition: rte_common.h:370
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:337
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_backend_config_change(int vid, bool need_reply)
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:348
int rte_vhost_driver_get_vdpa_dev_type(const char *path, uint32_t *type)
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: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)
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)
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:237
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)
int rte_vhost_vring_call_nonblock(int vid, uint16_t vring_idx)
uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid)
int rte_vhost_get_negotiated_protocol_features(int vid, uint64_t *protocol_features)
uint64_t value
Definition: rte_vhost.h:349
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_get_monitor_addr(int vid, uint16_t queue_id, struct rte_vhost_power_monitor_cond *pmc)
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:262
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)