DPDK  20.11.3
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_RESERVED_1 (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 #define RTE_VHOST_USER_ASYNC_COPY (1ULL << 7)
39 #define RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS (1ULL << 8)
40 
41 /* Features. */
42 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
43  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
44 #endif
45 
46 #ifndef VIRTIO_NET_F_MQ
47  #define VIRTIO_NET_F_MQ 22
48 #endif
49 
50 #ifndef VIRTIO_NET_F_MTU
51  #define VIRTIO_NET_F_MTU 3
52 #endif
53 
54 #ifndef VIRTIO_F_ANY_LAYOUT
55  #define VIRTIO_F_ANY_LAYOUT 27
56 #endif
57 
59 #ifndef VHOST_USER_PROTOCOL_F_MQ
60 #define VHOST_USER_PROTOCOL_F_MQ 0
61 #endif
62 
63 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
64 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
65 #endif
66 
67 #ifndef VHOST_USER_PROTOCOL_F_RARP
68 #define VHOST_USER_PROTOCOL_F_RARP 2
69 #endif
70 
71 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
72 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
73 #endif
74 
75 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
76 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
77 #endif
78 
79 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
80 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
81 #endif
82 
83 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
84 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
85 #endif
86 
87 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
88 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
89 #endif
90 
91 #ifndef VHOST_USER_PROTOCOL_F_CONFIG
92 #define VHOST_USER_PROTOCOL_F_CONFIG 9
93 #endif
94 
95 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
96 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
97 #endif
98 
99 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
100 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
101 #endif
102 
103 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
104 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
105 #endif
106 
107 #ifndef VHOST_USER_PROTOCOL_F_STATUS
108 #define VHOST_USER_PROTOCOL_F_STATUS 16
109 #endif
110 
112 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
113 #define VHOST_USER_F_PROTOCOL_FEATURES 30
114 #endif
115 
116 struct rte_vdpa_device;
117 
123  uint64_t guest_phys_addr;
124  uint64_t guest_user_addr;
125  uint64_t host_user_addr;
126  uint64_t size;
127  void *mmap_addr;
128  uint64_t mmap_size;
129  int fd;
130 };
131 
136  uint32_t nregions;
137  struct rte_vhost_mem_region regions[];
138 };
139 
140 struct rte_vhost_inflight_desc_split {
141  uint8_t inflight;
142  uint8_t padding[5];
143  uint16_t next;
144  uint64_t counter;
145 };
146 
147 struct rte_vhost_inflight_info_split {
148  uint64_t features;
149  uint16_t version;
150  uint16_t desc_num;
151  uint16_t last_inflight_io;
152  uint16_t used_idx;
153  struct rte_vhost_inflight_desc_split desc[0];
154 };
155 
156 struct rte_vhost_inflight_desc_packed {
157  uint8_t inflight;
158  uint8_t padding;
159  uint16_t next;
160  uint16_t last;
161  uint16_t num;
162  uint64_t counter;
163  uint16_t id;
164  uint16_t flags;
165  uint32_t len;
166  uint64_t addr;
167 };
168 
169 struct rte_vhost_inflight_info_packed {
170  uint64_t features;
171  uint16_t version;
172  uint16_t desc_num;
173  uint16_t free_head;
174  uint16_t old_free_head;
175  uint16_t used_idx;
176  uint16_t old_used_idx;
177  uint8_t used_wrap_counter;
178  uint8_t old_used_wrap_counter;
179  uint8_t padding[7];
180  struct rte_vhost_inflight_desc_packed desc[0];
181 };
182 
183 struct rte_vhost_resubmit_desc {
184  uint16_t index;
185  uint64_t counter;
186 };
187 
188 struct rte_vhost_resubmit_info {
189  struct rte_vhost_resubmit_desc *resubmit_list;
190  uint16_t resubmit_num;
191 };
192 
193 struct rte_vhost_ring_inflight {
194  union {
195  struct rte_vhost_inflight_info_split *inflight_split;
196  struct rte_vhost_inflight_info_packed *inflight_packed;
197  };
198 
199  struct rte_vhost_resubmit_info *resubmit_inflight;
200 };
201 
202 struct rte_vhost_vring {
203  union {
204  struct vring_desc *desc;
205  struct vring_packed_desc *desc_packed;
206  };
207  union {
208  struct vring_avail *avail;
209  struct vring_packed_desc_event *driver_event;
210  };
211  union {
212  struct vring_used *used;
213  struct vring_packed_desc_event *device_event;
214  };
215  uint64_t log_guest_addr;
216 
218  int callfd;
219 
220  int kickfd;
221  uint16_t size;
222 };
223 
228  /* Message handling failed */
229  RTE_VHOST_MSG_RESULT_ERR = -1,
230  /* Message handling successful */
231  RTE_VHOST_MSG_RESULT_OK = 0,
232  /* Message handling successful and reply prepared */
233  RTE_VHOST_MSG_RESULT_REPLY = 1,
234  /* Message not handled */
235  RTE_VHOST_MSG_RESULT_NOT_HANDLED,
236 };
237 
252 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
253 
258  /* Called prior to the master message handling. */
259  rte_vhost_msg_handle pre_msg_handle;
260  /* Called after the master message handling. */
261  rte_vhost_msg_handle post_msg_handle;
262 };
263 
268  int (*new_device)(int vid);
269  void (*destroy_device)(int vid);
271  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
279  int (*features_changed)(int vid, uint64_t features);
280 
281  int (*new_connection)(int vid);
282  void (*destroy_connection)(int vid);
283 
290  void (*guest_notified)(int vid);
291 
292  void *reserved[1];
293 };
294 
310 __rte_deprecated
311 static __rte_always_inline uint64_t
312 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
313 {
314  struct rte_vhost_mem_region *reg;
315  uint32_t i;
316 
317  for (i = 0; i < mem->nregions; i++) {
318  reg = &mem->regions[i];
319  if (gpa >= reg->guest_phys_addr &&
320  gpa < reg->guest_phys_addr + reg->size) {
321  return gpa - reg->guest_phys_addr +
322  reg->host_user_addr;
323  }
324  }
325 
326  return 0;
327 }
328 
345 __rte_experimental
346 static __rte_always_inline uint64_t
348  uint64_t gpa, uint64_t *len)
349 {
350  struct rte_vhost_mem_region *r;
351  uint32_t i;
352 
353  for (i = 0; i < mem->nregions; i++) {
354  r = &mem->regions[i];
355  if (gpa >= r->guest_phys_addr &&
356  gpa < r->guest_phys_addr + r->size) {
357 
358  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
359  *len = r->guest_phys_addr + r->size - gpa;
360 
361  return gpa - r->guest_phys_addr +
362  r->host_user_addr;
363  }
364  }
365  *len = 0;
366 
367  return 0;
368 }
369 
370 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
371 
390 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
391 
410 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
411  uint64_t offset, uint64_t len);
412 
413 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
414 
419 int rte_vhost_driver_register(const char *path, uint64_t flags);
420 
421 /* Unregister vhost driver. This is only meaningful to vhost user. */
422 int rte_vhost_driver_unregister(const char *path);
423 
434 int
435 rte_vhost_driver_attach_vdpa_device(const char *path,
436  struct rte_vdpa_device *dev);
437 
446 int
447 rte_vhost_driver_detach_vdpa_device(const char *path);
448 
457 struct rte_vdpa_device *
458 rte_vhost_driver_get_vdpa_device(const char *path);
459 
470 int rte_vhost_driver_set_features(const char *path, uint64_t features);
471 
487 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
488 
501 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
502 
513 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
514 
525 __rte_experimental
526 int
528  uint64_t protocol_features);
529 
540 __rte_experimental
541 int
543  uint64_t *protocol_features);
544 
555 __rte_experimental
556 int
557 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
558 
569 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
570 
571 /* Register callbacks. */
572 int rte_vhost_driver_callback_register(const char *path,
573  struct vhost_device_ops const * const ops);
574 
586 int rte_vhost_driver_start(const char *path);
587 
601 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
602 
613 int rte_vhost_get_numa_node(int vid);
614 
629 __rte_deprecated
630 uint32_t rte_vhost_get_queue_num(int vid);
631 
641 uint16_t rte_vhost_get_vring_num(int vid);
642 
657 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
658 
670 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
671 
672 struct rte_mbuf;
673 struct rte_mempool;
690 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
691  struct rte_mbuf **pkts, uint16_t count);
692 
710 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
711  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
712 
727 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
728 
741 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
742  struct rte_vhost_vring *vring);
743 
756 __rte_experimental
757 int
758 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
759  struct rte_vhost_ring_inflight *vring);
760 
776 __rte_experimental
777 int
778 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
779  uint16_t idx);
780 
799 __rte_experimental
800 int
801 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
802  uint16_t head, uint16_t last, uint16_t *inflight_entry);
803 
816 __rte_experimental
817 int
819  uint16_t vring_idx, uint16_t idx);
820 
836 __rte_experimental
837 int
839  uint16_t vring_idx, uint16_t head);
840 
855 __rte_experimental
856 int
857 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
858  uint16_t last_used_idx, uint16_t idx);
859 
872 __rte_experimental
873 int
874 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
875  uint16_t head);
876 
888 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
889 
900 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
901 
914 int
915 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
916 
931 int
932 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
933  uint16_t *last_avail_idx, uint16_t *last_used_idx);
934 
953 __rte_experimental
954 int
956  uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
957 
972 int
973 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
974  uint16_t last_avail_idx, uint16_t last_used_idx);
975 
988 __rte_experimental
989 int
991  struct rte_vhost_user_extern_ops const * const ops, void *ctx);
992 
1001 struct rte_vdpa_device *
1002 rte_vhost_get_vdpa_device(int vid);
1003 
1014 __rte_experimental
1015 int
1016 rte_vhost_slave_config_change(int vid, bool need_reply);
1017 
1018 #ifdef __cplusplus
1019 }
1020 #endif
1021 
1022 #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:312
__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:226
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)
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)
__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)
int rte_vhost_get_mtu(int vid, uint16_t *mtu)
int rte_vhost_driver_detach_vdpa_device(const char *path)
__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)
#define unlikely(x)
uint16_t rte_vhost_get_vring_num(int vid)
int rte_vhost_get_numa_node(int vid)
struct rte_vdpa_dev_ops * ops
Definition: rte_vdpa_dev.h:78
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:347
__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)
int rte_vhost_driver_attach_vdpa_device(const char *path, struct rte_vdpa_device *dev)
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:227
int rte_vhost_set_vring_base(int vid, uint16_t queue_id, uint16_t last_avail_idx, uint16_t last_used_idx)
__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)
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)
__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)
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 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:252
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)