DPDK  17.05.2
rte_vhost.h
Go to the documentation of this file.
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of Intel Corporation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _RTE_VHOST_H_
35 #define _RTE_VHOST_H_
36 
42 #include <stdint.h>
43 #include <sys/eventfd.h>
44 
45 #include <rte_memory.h>
46 #include <rte_mempool.h>
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /* These are not C++-aware. */
53 #include <linux/vhost.h>
54 #include <linux/virtio_ring.h>
55 
56 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
57 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
58 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
59 
65  uint64_t guest_phys_addr;
66  uint64_t guest_user_addr;
67  uint64_t host_user_addr;
68  uint64_t size;
69  void *mmap_addr;
70  uint64_t mmap_size;
71  int fd;
72 };
73 
78  uint32_t nregions;
79  struct rte_vhost_mem_region regions[];
80 };
81 
82 struct rte_vhost_vring {
83  struct vring_desc *desc;
84  struct vring_avail *avail;
85  struct vring_used *used;
86  uint64_t log_guest_addr;
87 
88  int callfd;
89  int kickfd;
90  uint16_t size;
91 };
92 
97  int (*new_device)(int vid);
98  void (*destroy_device)(int vid);
100  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
108  int (*features_changed)(int vid, uint64_t features);
109 
110  void *reserved[4];
111 };
112 
123 static inline uint64_t __attribute__((always_inline))
124 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
125 {
126  struct rte_vhost_mem_region *reg;
127  uint32_t i;
128 
129  for (i = 0; i < mem->nregions; i++) {
130  reg = &mem->regions[i];
131  if (gpa >= reg->guest_phys_addr &&
132  gpa < reg->guest_phys_addr + reg->size) {
133  return gpa - reg->guest_phys_addr +
134  reg->host_user_addr;
135  }
136  }
137 
138  return 0;
139 }
140 
141 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
142 
161 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
162 
181 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
182  uint64_t offset, uint64_t len);
183 
184 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
185 
190 int rte_vhost_driver_register(const char *path, uint64_t flags);
191 
192 /* Unregister vhost driver. This is only meaningful to vhost user. */
193 int rte_vhost_driver_unregister(const char *path);
194 
205 int rte_vhost_driver_set_features(const char *path, uint64_t features);
206 
222 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
223 
236 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
237 
248 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
249 
260 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
261 
262 /* Register callbacks. */
263 int rte_vhost_driver_callback_register(const char *path,
264  struct vhost_device_ops const * const ops);
265 
277 int rte_vhost_driver_start(const char *path);
278 
292 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
293 
304 int rte_vhost_get_numa_node(int vid);
305 
320 __rte_deprecated
321 uint32_t rte_vhost_get_queue_num(int vid);
322 
332 uint16_t rte_vhost_get_vring_num(int vid);
333 
348 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
349 
361 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
362 
363 struct rte_mbuf;
364 struct rte_mempool;
381 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
382  struct rte_mbuf **pkts, uint16_t count);
383 
401 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
402  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
403 
418 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
419 
432 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
433  struct rte_vhost_vring *vring);
434 
435 #ifdef __cplusplus
436 }
437 #endif
438 
439 #endif /* _RTE_VHOST_H_ */