vhost: export API to translate gpa to vva
[dpdk.git] / lib / librte_vhost / rte_virtio_net.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 _VIRTIO_NET_H_
35 #define _VIRTIO_NET_H_
36
37 /**
38  * @file
39  * Interface to vhost net
40  */
41
42 #include <stdint.h>
43 #include <linux/vhost.h>
44 #include <linux/virtio_ring.h>
45 #include <linux/virtio_net.h>
46 #include <sys/eventfd.h>
47 #include <sys/socket.h>
48 #include <linux/if.h>
49
50 #include <rte_memory.h>
51 #include <rte_mempool.h>
52 #include <rte_ether.h>
53
54 #define RTE_VHOST_USER_CLIENT           (1ULL << 0)
55 #define RTE_VHOST_USER_NO_RECONNECT     (1ULL << 1)
56 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY        (1ULL << 2)
57
58 /* Enum for virtqueue management. */
59 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
60
61 /**
62  * Information relating to memory regions including offsets to
63  * addresses in QEMUs memory file.
64  */
65 struct rte_vhost_mem_region {
66         uint64_t guest_phys_addr;
67         uint64_t guest_user_addr;
68         uint64_t host_user_addr;
69         uint64_t size;
70         void     *mmap_addr;
71         uint64_t mmap_size;
72         int fd;
73 };
74
75 /**
76  * Memory structure includes region and mapping information.
77  */
78 struct rte_vhost_memory {
79         uint32_t nregions;
80         struct rte_vhost_mem_region regions[0];
81 };
82
83 struct rte_vhost_vring {
84         struct vring_desc       *desc;
85         struct vring_avail      *avail;
86         struct vring_used       *used;
87         uint64_t                log_guest_addr;
88
89         int                     callfd;
90         int                     kickfd;
91         uint16_t                size;
92 };
93
94 /**
95  * Device and vring operations.
96  */
97 struct virtio_net_device_ops {
98         int (*new_device)(int vid);             /**< Add device. */
99         void (*destroy_device)(int vid);        /**< Remove device. */
100
101         int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);     /**< triggered when a vring is enabled or disabled */
102
103         void *reserved[5]; /**< Reserved for future extension */
104 };
105
106 /**
107  * Convert guest physical address to host virtual address
108  *
109  * @param mem
110  *  the guest memory regions
111  * @param gpa
112  *  the guest physical address for querying
113  * @return
114  *  the host virtual address on success, 0 on failure
115  */
116 static inline uint64_t __attribute__((always_inline))
117 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
118 {
119         struct rte_vhost_mem_region *reg;
120         uint32_t i;
121
122         for (i = 0; i < mem->nregions; i++) {
123                 reg = &mem->regions[i];
124                 if (gpa >= reg->guest_phys_addr &&
125                     gpa <  reg->guest_phys_addr + reg->size) {
126                         return gpa - reg->guest_phys_addr +
127                                reg->host_user_addr;
128                 }
129         }
130
131         return 0;
132 }
133
134 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
135
136 /**
137  * Register vhost driver. path could be different for multiple
138  * instance support.
139  */
140 int rte_vhost_driver_register(const char *path, uint64_t flags);
141
142 /* Unregister vhost driver. This is only meaningful to vhost user. */
143 int rte_vhost_driver_unregister(const char *path);
144
145 /**
146  * Set the feature bits the vhost-user driver supports.
147  *
148  * @param path
149  *  The vhost-user socket file path
150  * @param features
151  *  Supported features
152  * @return
153  *  0 on success, -1 on failure
154  */
155 int rte_vhost_driver_set_features(const char *path, uint64_t features);
156
157 /**
158  * Enable vhost-user driver features.
159  *
160  * Note that
161  * - the param features should be a subset of the feature bits provided
162  *   by rte_vhost_driver_set_features().
163  * - it must be invoked before vhost-user negotiation starts.
164  *
165  * @param path
166  *  The vhost-user socket file path
167  * @param features
168  *  Features to enable
169  * @return
170  *  0 on success, -1 on failure
171  */
172 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
173
174 /**
175  * Disable vhost-user driver features.
176  *
177  * The two notes at rte_vhost_driver_enable_features() also apply here.
178  *
179  * @param path
180  *  The vhost-user socket file path
181  * @param features
182  *  Features to disable
183  * @return
184  *  0 on success, -1 on failure
185  */
186 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
187
188 /**
189  * Get the feature bits before feature negotiation.
190  *
191  * @param path
192  *  The vhost-user socket file path
193  * @param features
194  *  A pointer to store the queried feature bits
195  * @return
196  *  0 on success, -1 on failure
197  */
198 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
199
200 /**
201  * Get the feature bits after negotiation
202  *
203  * @param vid
204  *  Vhost device ID
205  * @param features
206  *  A pointer to store the queried feature bits
207  * @return
208  *  0 on success, -1 on failure
209  */
210 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
211
212 /* Register callbacks. */
213 int rte_vhost_driver_callback_register(const char *path,
214         struct virtio_net_device_ops const * const ops);
215 /* Start vhost driver session blocking loop. */
216 int rte_vhost_driver_session_start(void);
217
218 /**
219  * Get the MTU value of the device if set in QEMU.
220  *
221  * @param vid
222  *  virtio-net device ID
223  * @param mtu
224  *  The variable to store the MTU value
225  *
226  * @return
227  *  0: success
228  *  -EAGAIN: device not yet started
229  *  -ENOTSUP: device does not support MTU feature
230  */
231 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
232
233 /**
234  * Get the numa node from which the virtio net device's memory
235  * is allocated.
236  *
237  * @param vid
238  *  virtio-net device ID
239  *
240  * @return
241  *  The numa node, -1 on failure
242  */
243 int rte_vhost_get_numa_node(int vid);
244
245 /**
246  * Get the number of queues the device supports.
247  *
248  * @param vid
249  *  virtio-net device ID
250  *
251  * @return
252  *  The number of queues, 0 on failure
253  */
254 uint32_t rte_vhost_get_queue_num(int vid);
255
256 /**
257  * Get the virtio net device's ifname, which is the vhost-user socket
258  * file path.
259  *
260  * @param vid
261  *  virtio-net device ID
262  * @param buf
263  *  The buffer to stored the queried ifname
264  * @param len
265  *  The length of buf
266  *
267  * @return
268  *  0 on success, -1 on failure
269  */
270 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
271
272 /**
273  * Get how many avail entries are left in the queue
274  *
275  * @param vid
276  *  virtio-net device ID
277  * @param queue_id
278  *  virtio queue index
279  *
280  * @return
281  *  num of avail entires left
282  */
283 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
284
285 /**
286  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
287  * be received from the physical port or from another virtual device. A packet
288  * count is returned to indicate the number of packets that were succesfully
289  * added to the RX queue.
290  * @param vid
291  *  virtio-net device ID
292  * @param queue_id
293  *  virtio queue index in mq case
294  * @param pkts
295  *  array to contain packets to be enqueued
296  * @param count
297  *  packets num to be enqueued
298  * @return
299  *  num of packets enqueued
300  */
301 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
302         struct rte_mbuf **pkts, uint16_t count);
303
304 /**
305  * This function gets guest buffers from the virtio device TX virtqueue,
306  * construct host mbufs, copies guest buffer content to host mbufs and
307  * store them in pkts to be processed.
308  * @param vid
309  *  virtio-net device
310  * @param queue_id
311  *  virtio queue index in mq case
312  * @param mbuf_pool
313  *  mbuf_pool where host mbuf is allocated.
314  * @param pkts
315  *  array to contain packets to be dequeued
316  * @param count
317  *  packets num to be dequeued
318  * @return
319  *  num of packets dequeued
320  */
321 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
322         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
323
324 /**
325  * Get guest mem table: a list of memory regions.
326  *
327  * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
328  * guest memory regions. Application should free it at destroy_device()
329  * callback.
330  *
331  * @param vid
332  *  vhost device ID
333  * @param mem
334  *  To store the returned mem regions
335  * @return
336  *  0 on success, -1 on failure
337  */
338 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
339
340 /**
341  * Get guest vring info, including the vring address, vring size, etc.
342  *
343  * @param vid
344  *  vhost device ID
345  * @param vring_idx
346  *  vring index
347  * @param vring
348  *  the structure to hold the requested vring info
349  * @return
350  *  0 on success, -1 on failure
351  */
352 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
353                               struct rte_vhost_vring *vring);
354
355 #endif /* _VIRTIO_NET_H_ */