DPDK  22.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_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 #define RTE_VHOST_USER_NET_STATS_ENABLE (1ULL << 9)
43 
44 /* Features. */
45 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
46  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
47 #endif
48 
49 #ifndef VIRTIO_NET_F_MQ
50  #define VIRTIO_NET_F_MQ 22
51 #endif
52 
53 #ifndef VIRTIO_NET_F_MTU
54  #define VIRTIO_NET_F_MTU 3
55 #endif
56 
57 #ifndef VIRTIO_F_ANY_LAYOUT
58  #define VIRTIO_F_ANY_LAYOUT 27
59 #endif
60 
62 #ifndef VHOST_USER_PROTOCOL_F_MQ
63 #define VHOST_USER_PROTOCOL_F_MQ 0
64 #endif
65 
66 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
67 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
68 #endif
69 
70 #ifndef VHOST_USER_PROTOCOL_F_RARP
71 #define VHOST_USER_PROTOCOL_F_RARP 2
72 #endif
73 
74 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
75 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
76 #endif
77 
78 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
79 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
80 #endif
81 
82 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
83 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
84 #endif
85 
86 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
87 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
88 #endif
89 
90 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
91 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
92 #endif
93 
94 #ifndef VHOST_USER_PROTOCOL_F_CONFIG
95 #define VHOST_USER_PROTOCOL_F_CONFIG 9
96 #endif
97 
98 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
99 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
100 #endif
101 
102 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
103 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
104 #endif
105 
106 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
107 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
108 #endif
109 
110 #ifndef VHOST_USER_PROTOCOL_F_STATUS
111 #define VHOST_USER_PROTOCOL_F_STATUS 16
112 #endif
113 
115 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
116 #define VHOST_USER_F_PROTOCOL_FEATURES 30
117 #endif
118 
119 #define RTE_MAX_VHOST_DEVICE 1024
120 
121 #define RTE_VHOST_VDPA_DEVICE_TYPE_NET 0
122 #define RTE_VHOST_VDPA_DEVICE_TYPE_BLK 1
123 
124 struct rte_vdpa_device;
125 
131  uint64_t guest_phys_addr;
132  uint64_t guest_user_addr;
133  uint64_t host_user_addr;
134  uint64_t size;
135  void *mmap_addr;
136  uint64_t mmap_size;
137  int fd;
138 };
139 
144  uint32_t nregions;
145  struct rte_vhost_mem_region regions[];
146 };
147 
148 struct rte_vhost_inflight_desc_split {
149  uint8_t inflight;
150  uint8_t padding[5];
151  uint16_t next;
152  uint64_t counter;
153 };
154 
155 struct rte_vhost_inflight_info_split {
156  uint64_t features;
157  uint16_t version;
158  uint16_t desc_num;
159  uint16_t last_inflight_io;
160  uint16_t used_idx;
161  struct rte_vhost_inflight_desc_split desc[];
162 };
163 
164 struct rte_vhost_inflight_desc_packed {
165  uint8_t inflight;
166  uint8_t padding;
167  uint16_t next;
168  uint16_t last;
169  uint16_t num;
170  uint64_t counter;
171  uint16_t id;
172  uint16_t flags;
173  uint32_t len;
174  uint64_t addr;
175 };
176 
177 struct rte_vhost_inflight_info_packed {
178  uint64_t features;
179  uint16_t version;
180  uint16_t desc_num;
181  uint16_t free_head;
182  uint16_t old_free_head;
183  uint16_t used_idx;
184  uint16_t old_used_idx;
185  uint8_t used_wrap_counter;
186  uint8_t old_used_wrap_counter;
187  uint8_t padding[7];
188  struct rte_vhost_inflight_desc_packed desc[];
189 };
190 
191 struct rte_vhost_resubmit_desc {
192  uint16_t index;
193  uint64_t counter;
194 };
195 
196 struct rte_vhost_resubmit_info {
197  struct rte_vhost_resubmit_desc *resubmit_list;
198  uint16_t resubmit_num;
199 };
200 
201 struct rte_vhost_ring_inflight {
202  union {
203  struct rte_vhost_inflight_info_split *inflight_split;
204  struct rte_vhost_inflight_info_packed *inflight_packed;
205  };
206 
207  struct rte_vhost_resubmit_info *resubmit_inflight;
208 };
209 
210 struct rte_vhost_vring {
211  union {
212  struct vring_desc *desc;
213  struct vring_packed_desc *desc_packed;
214  };
215  union {
216  struct vring_avail *avail;
217  struct vring_packed_desc_event *driver_event;
218  };
219  union {
220  struct vring_used *used;
221  struct vring_packed_desc_event *device_event;
222  };
223  uint64_t log_guest_addr;
224 
226  int callfd;
227 
228  int kickfd;
229  uint16_t size;
230 };
231 
236  /* Message handling failed */
237  RTE_VHOST_MSG_RESULT_ERR = -1,
238  /* Message handling successful */
239  RTE_VHOST_MSG_RESULT_OK = 0,
240  /* Message handling successful and reply prepared */
241  RTE_VHOST_MSG_RESULT_REPLY = 1,
242  /* Message not handled */
243  RTE_VHOST_MSG_RESULT_NOT_HANDLED,
244 };
245 
260 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
261 
266  /* Called prior to the master message handling. */
267  rte_vhost_msg_handle pre_msg_handle;
268  /* Called after the master message handling. */
269  rte_vhost_msg_handle post_msg_handle;
270 };
271 
276  int (*new_device)(int vid);
277  void (*destroy_device)(int vid);
279  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
287  int (*features_changed)(int vid, uint64_t features);
288 
289  int (*new_connection)(int vid);
290  void (*destroy_connection)(int vid);
291 
298  void (*guest_notified)(int vid);
299 
300  void *reserved[1];
301 };
302 
308  volatile void *addr;
313  uint64_t val;
315  uint64_t mask;
319  uint8_t size;
325  uint8_t match;
326 };
327 
329 #define RTE_VHOST_STATS_NAME_SIZE 64
330 
340  uint64_t id;
341  uint64_t value;
342 };
343 
352 };
353 
369 __rte_deprecated
370 static __rte_always_inline uint64_t
371 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
372 {
373  struct rte_vhost_mem_region *reg;
374  uint32_t i;
375 
376  for (i = 0; i < mem->nregions; i++) {
377  reg = &mem->regions[i];
378  if (gpa >= reg->guest_phys_addr &&
379  gpa < reg->guest_phys_addr + reg->size) {
380  return gpa - reg->guest_phys_addr +
381  reg->host_user_addr;
382  }
383  }
384 
385  return 0;
386 }
387 
404 static __rte_always_inline uint64_t
406  uint64_t gpa, uint64_t *len)
407 {
408  struct rte_vhost_mem_region *r;
409  uint32_t i;
410 
411  for (i = 0; i < mem->nregions; i++) {
412  r = &mem->regions[i];
413  if (gpa >= r->guest_phys_addr &&
414  gpa < r->guest_phys_addr + r->size) {
415 
416  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
417  *len = r->guest_phys_addr + r->size - gpa;
418 
419  return gpa - r->guest_phys_addr +
420  r->host_user_addr;
421  }
422  }
423  *len = 0;
424 
425  return 0;
426 }
427 
428 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
429 
448 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
449 
468 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
469  uint64_t offset, uint64_t len);
470 
471 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
472 
477 int rte_vhost_driver_register(const char *path, uint64_t flags);
478 
479 /* Unregister vhost driver. This is only meaningful to vhost user. */
480 int rte_vhost_driver_unregister(const char *path);
481 
492 int
493 rte_vhost_driver_attach_vdpa_device(const char *path,
494  struct rte_vdpa_device *dev);
495 
504 int
505 rte_vhost_driver_detach_vdpa_device(const char *path);
506 
515 struct rte_vdpa_device *
516 rte_vhost_driver_get_vdpa_device(const char *path);
517 
528 __rte_experimental
529 int
530 rte_vhost_driver_get_vdpa_dev_type(const char *path, uint32_t *type);
531 
542 int rte_vhost_driver_set_features(const char *path, uint64_t features);
543 
559 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
560 
573 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
574 
585 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
586 
597 int
599  uint64_t protocol_features);
600 
611 int
613  uint64_t *protocol_features);
614 
625 int
626 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
627 
638 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
639 
650 __rte_experimental
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 
713 __rte_deprecated
714 uint32_t rte_vhost_get_queue_num(int vid);
715 
725 uint16_t rte_vhost_get_vring_num(int vid);
726 
741 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
742 
754 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
755 
756 struct rte_mbuf;
757 struct rte_mempool;
774 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
775  struct rte_mbuf **pkts, uint16_t count);
776 
794 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
795  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
796 
811 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
812 
825 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
826  struct rte_vhost_vring *vring);
827 
840 int
841 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
842  struct rte_vhost_ring_inflight *vring);
843 
859 int
860 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
861  uint16_t idx);
862 
881 int
882 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
883  uint16_t head, uint16_t last, uint16_t *inflight_entry);
884 
897 int
899  uint16_t vring_idx, uint16_t idx);
900 
916 int
918  uint16_t vring_idx, uint16_t head);
919 
934 int
935 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
936  uint16_t last_used_idx, uint16_t idx);
937 
950 int
951 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
952  uint16_t head);
953 
965 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
966 
977 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
978 
991 __rte_experimental
992 int
993 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
994  struct rte_vhost_power_monitor_cond *pmc);
995 
1008 int
1009 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
1010 
1025 int
1026 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1027  uint16_t *last_avail_idx, uint16_t *last_used_idx);
1028 
1047 int
1049  uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
1050 
1065 int
1066 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1067  uint16_t last_avail_idx, uint16_t last_used_idx);
1068 
1081 int
1083  struct rte_vhost_user_extern_ops const * const ops, void *ctx);
1084 
1093 struct rte_vdpa_device *
1094 rte_vhost_get_vdpa_device(int vid);
1095 
1106 __rte_experimental
1107 int
1108 rte_vhost_slave_config_change(int vid, bool need_reply);
1109 
1133 __rte_experimental
1134 int
1135 rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id,
1136  struct rte_vhost_stat_name *name, unsigned int size);
1137 
1161 __rte_experimental
1162 int
1163 rte_vhost_vring_stats_get(int vid, uint16_t queue_id,
1164  struct rte_vhost_stat *stats, unsigned int n);
1165 
1178 __rte_experimental
1179 int
1180 rte_vhost_vring_stats_reset(int vid, uint16_t queue_id);
1181 
1182 #ifdef __cplusplus
1183 }
1184 #endif
1185 
1186 #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:371
int rte_vhost_driver_start(const char *path)
#define __rte_always_inline
Definition: rte_common.h:258
__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)
#define RTE_VHOST_STATS_NAME_SIZE
Definition: rte_vhost.h:329
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)
uint64_t id
Definition: rte_vhost.h:340
__rte_experimental 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:405
#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)
__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:235
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)
uint64_t value
Definition: rte_vhost.h:341
__rte_experimental int rte_vhost_vring_stats_reset(int vid, uint16_t queue_id)
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)
__rte_experimental 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_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:260
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)