DPDK  18.08.1
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 <stdint.h>
14 #include <sys/eventfd.h>
15 
16 #include <rte_memory.h>
17 #include <rte_mempool.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /* These are not C++-aware. */
24 #include <linux/vhost.h>
25 #include <linux/virtio_ring.h>
26 
27 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
28 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
29 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
30 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
31 
33 #ifndef VHOST_USER_PROTOCOL_F_MQ
34 #define VHOST_USER_PROTOCOL_F_MQ 0
35 #endif
36 
37 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
38 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
39 #endif
40 
41 #ifndef VHOST_USER_PROTOCOL_F_RARP
42 #define VHOST_USER_PROTOCOL_F_RARP 2
43 #endif
44 
45 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
46 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
47 #endif
48 
49 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
50 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
51 #endif
52 
53 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
54 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
55 #endif
56 
57 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
58 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
59 #endif
60 
61 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
62 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
63 #endif
64 
65 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
66 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
67 #endif
68 
70 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
71 #define VHOST_USER_F_PROTOCOL_FEATURES 30
72 #endif
73 
79  uint64_t guest_phys_addr;
80  uint64_t guest_user_addr;
81  uint64_t host_user_addr;
82  uint64_t size;
83  void *mmap_addr;
84  uint64_t mmap_size;
85  int fd;
86 };
87 
92  uint32_t nregions;
93  struct rte_vhost_mem_region regions[];
94 };
95 
96 struct rte_vhost_vring {
97  struct vring_desc *desc;
98  struct vring_avail *avail;
99  struct vring_used *used;
100  uint64_t log_guest_addr;
101 
103  int callfd;
104 
105  int kickfd;
106  uint16_t size;
107 };
108 
113  int (*new_device)(int vid);
114  void (*destroy_device)(int vid);
116  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
124  int (*features_changed)(int vid, uint64_t features);
125 
126  int (*new_connection)(int vid);
127  void (*destroy_connection)(int vid);
128 
129  void *reserved[2];
130 };
131 
147 __rte_deprecated
148 static __rte_always_inline uint64_t
149 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
150 {
151  struct rte_vhost_mem_region *reg;
152  uint32_t i;
153 
154  for (i = 0; i < mem->nregions; i++) {
155  reg = &mem->regions[i];
156  if (gpa >= reg->guest_phys_addr &&
157  gpa < reg->guest_phys_addr + reg->size) {
158  return gpa - reg->guest_phys_addr +
159  reg->host_user_addr;
160  }
161  }
162 
163  return 0;
164 }
165 
182 static __rte_always_inline uint64_t
184  uint64_t gpa, uint64_t *len)
185 {
186  struct rte_vhost_mem_region *r;
187  uint32_t i;
188 
189  for (i = 0; i < mem->nregions; i++) {
190  r = &mem->regions[i];
191  if (gpa >= r->guest_phys_addr &&
192  gpa < r->guest_phys_addr + r->size) {
193 
194  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
195  *len = r->guest_phys_addr + r->size - gpa;
196 
197  return gpa - r->guest_phys_addr +
198  r->host_user_addr;
199  }
200  }
201  *len = 0;
202 
203  return 0;
204 }
205 
206 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
207 
226 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
227 
246 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
247  uint64_t offset, uint64_t len);
248 
249 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
250 
255 int rte_vhost_driver_register(const char *path, uint64_t flags);
256 
257 /* Unregister vhost driver. This is only meaningful to vhost user. */
258 int rte_vhost_driver_unregister(const char *path);
259 
270 int __rte_experimental
271 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
272 
281 int __rte_experimental
282 rte_vhost_driver_detach_vdpa_device(const char *path);
283 
292 int __rte_experimental
293 rte_vhost_driver_get_vdpa_device_id(const char *path);
294 
305 int rte_vhost_driver_set_features(const char *path, uint64_t features);
306 
322 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
323 
336 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
337 
348 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
349 
360 int __rte_experimental
362  uint64_t *protocol_features);
363 
374 int __rte_experimental
375 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
376 
387 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
388 
389 /* Register callbacks. */
390 int rte_vhost_driver_callback_register(const char *path,
391  struct vhost_device_ops const * const ops);
392 
404 int rte_vhost_driver_start(const char *path);
405 
419 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
420 
431 int rte_vhost_get_numa_node(int vid);
432 
447 __rte_deprecated
448 uint32_t rte_vhost_get_queue_num(int vid);
449 
459 uint16_t rte_vhost_get_vring_num(int vid);
460 
475 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
476 
488 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
489 
490 struct rte_mbuf;
491 struct rte_mempool;
508 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
509  struct rte_mbuf **pkts, uint16_t count);
510 
528 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
529  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
530 
545 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
546 
559 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
560  struct rte_vhost_vring *vring);
561 
573 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
574 
585 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
586 
599 int __rte_experimental
600 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
601 
616 int __rte_experimental
617 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
618  uint16_t *last_avail_idx, uint16_t *last_used_idx);
619 
634 int __rte_experimental
635 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
636  uint16_t last_avail_idx, uint16_t last_used_idx);
637 
646 int __rte_experimental
648 
649 #ifdef __cplusplus
650 }
651 #endif
652 
653 #endif /* _RTE_VHOST_H_ */