New upstream version 17.11-rc3
[deb_dpdk.git] / lib / librte_vhost / rte_vhost.h
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
37 /**
38  * @file
39  * Interface to vhost-user
40  */
41
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 #define RTE_VHOST_USER_IOMMU_SUPPORT    (1ULL << 3)
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[];
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 vhost_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         /**
104          * Features could be changed after the feature negotiation.
105          * For example, VHOST_F_LOG_ALL will be set/cleared at the
106          * start/end of live migration, respectively. This callback
107          * is used to inform the application on such change.
108          */
109         int (*features_changed)(int vid, uint64_t features);
110
111         int (*new_connection)(int vid);
112         void (*destroy_connection)(int vid);
113
114         void *reserved[2]; /**< Reserved for future extension */
115 };
116
117 /**
118  * Convert guest physical address to host virtual address
119  *
120  * @param mem
121  *  the guest memory regions
122  * @param gpa
123  *  the guest physical address for querying
124  * @return
125  *  the host virtual address on success, 0 on failure
126  */
127 static __rte_always_inline uint64_t
128 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
129 {
130         struct rte_vhost_mem_region *reg;
131         uint32_t i;
132
133         for (i = 0; i < mem->nregions; i++) {
134                 reg = &mem->regions[i];
135                 if (gpa >= reg->guest_phys_addr &&
136                     gpa <  reg->guest_phys_addr + reg->size) {
137                         return gpa - reg->guest_phys_addr +
138                                reg->host_user_addr;
139                 }
140         }
141
142         return 0;
143 }
144
145 #define RTE_VHOST_NEED_LOG(features)    ((features) & (1ULL << VHOST_F_LOG_ALL))
146
147 /**
148  * Log the memory write start with given address.
149  *
150  * This function only need be invoked when the live migration starts.
151  * Therefore, we won't need call it at all in the most of time. For
152  * making the performance impact be minimum, it's suggested to do a
153  * check before calling it:
154  *
155  *        if (unlikely(RTE_VHOST_NEED_LOG(features)))
156  *                rte_vhost_log_write(vid, addr, len);
157  *
158  * @param vid
159  *  vhost device ID
160  * @param addr
161  *  the starting address for write
162  * @param len
163  *  the length to write
164  */
165 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
166
167 /**
168  * Log the used ring update start at given offset.
169  *
170  * Same as rte_vhost_log_write, it's suggested to do a check before
171  * calling it:
172  *
173  *        if (unlikely(RTE_VHOST_NEED_LOG(features)))
174  *                rte_vhost_log_used_vring(vid, vring_idx, offset, len);
175  *
176  * @param vid
177  *  vhost device ID
178  * @param vring_idx
179  *  the vring index
180  * @param offset
181  *  the offset inside the used ring
182  * @param len
183  *  the length to write
184  */
185 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
186                               uint64_t offset, uint64_t len);
187
188 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
189
190 /**
191  * Register vhost driver. path could be different for multiple
192  * instance support.
193  */
194 int rte_vhost_driver_register(const char *path, uint64_t flags);
195
196 /* Unregister vhost driver. This is only meaningful to vhost user. */
197 int rte_vhost_driver_unregister(const char *path);
198
199 /**
200  * Set the feature bits the vhost-user driver supports.
201  *
202  * @param path
203  *  The vhost-user socket file path
204  * @param features
205  *  Supported features
206  * @return
207  *  0 on success, -1 on failure
208  */
209 int rte_vhost_driver_set_features(const char *path, uint64_t features);
210
211 /**
212  * Enable vhost-user driver features.
213  *
214  * Note that
215  * - the param features should be a subset of the feature bits provided
216  *   by rte_vhost_driver_set_features().
217  * - it must be invoked before vhost-user negotiation starts.
218  *
219  * @param path
220  *  The vhost-user socket file path
221  * @param features
222  *  Features to enable
223  * @return
224  *  0 on success, -1 on failure
225  */
226 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
227
228 /**
229  * Disable vhost-user driver features.
230  *
231  * The two notes at rte_vhost_driver_enable_features() also apply here.
232  *
233  * @param path
234  *  The vhost-user socket file path
235  * @param features
236  *  Features to disable
237  * @return
238  *  0 on success, -1 on failure
239  */
240 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
241
242 /**
243  * Get the feature bits before feature negotiation.
244  *
245  * @param path
246  *  The vhost-user socket file path
247  * @param features
248  *  A pointer to store the queried feature bits
249  * @return
250  *  0 on success, -1 on failure
251  */
252 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
253
254 /**
255  * Get the feature bits after negotiation
256  *
257  * @param vid
258  *  Vhost device ID
259  * @param features
260  *  A pointer to store the queried feature bits
261  * @return
262  *  0 on success, -1 on failure
263  */
264 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
265
266 /* Register callbacks. */
267 int rte_vhost_driver_callback_register(const char *path,
268         struct vhost_device_ops const * const ops);
269
270 /**
271  *
272  * Start the vhost-user driver.
273  *
274  * This function triggers the vhost-user negotiation.
275  *
276  * @param path
277  *  The vhost-user socket file path
278  * @return
279  *  0 on success, -1 on failure
280  */
281 int rte_vhost_driver_start(const char *path);
282
283 /**
284  * Get the MTU value of the device if set in QEMU.
285  *
286  * @param vid
287  *  virtio-net device ID
288  * @param mtu
289  *  The variable to store the MTU value
290  *
291  * @return
292  *  0: success
293  *  -EAGAIN: device not yet started
294  *  -ENOTSUP: device does not support MTU feature
295  */
296 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
297
298 /**
299  * Get the numa node from which the virtio net device's memory
300  * is allocated.
301  *
302  * @param vid
303  *  vhost device ID
304  *
305  * @return
306  *  The numa node, -1 on failure
307  */
308 int rte_vhost_get_numa_node(int vid);
309
310 /**
311  * @deprecated
312  * Get the number of queues the device supports.
313  *
314  * Note this function is deprecated, as it returns a queue pair number,
315  * which is vhost specific. Instead, rte_vhost_get_vring_num should
316  * be used.
317  *
318  * @param vid
319  *  vhost device ID
320  *
321  * @return
322  *  The number of queues, 0 on failure
323  */
324 __rte_deprecated
325 uint32_t rte_vhost_get_queue_num(int vid);
326
327 /**
328  * Get the number of vrings the device supports.
329  *
330  * @param vid
331  *  vhost device ID
332  *
333  * @return
334  *  The number of vrings, 0 on failure
335  */
336 uint16_t rte_vhost_get_vring_num(int vid);
337
338 /**
339  * Get the virtio net device's ifname, which is the vhost-user socket
340  * file path.
341  *
342  * @param vid
343  *  vhost device ID
344  * @param buf
345  *  The buffer to stored the queried ifname
346  * @param len
347  *  The length of buf
348  *
349  * @return
350  *  0 on success, -1 on failure
351  */
352 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
353
354 /**
355  * Get how many avail entries are left in the queue
356  *
357  * @param vid
358  *  vhost device ID
359  * @param queue_id
360  *  virtio queue index
361  *
362  * @return
363  *  num of avail entires left
364  */
365 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
366
367 struct rte_mbuf;
368 struct rte_mempool;
369 /**
370  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
371  * be received from the physical port or from another virtual device. A packet
372  * count is returned to indicate the number of packets that were successfully
373  * added to the RX queue.
374  * @param vid
375  *  vhost device ID
376  * @param queue_id
377  *  virtio queue index in mq case
378  * @param pkts
379  *  array to contain packets to be enqueued
380  * @param count
381  *  packets num to be enqueued
382  * @return
383  *  num of packets enqueued
384  */
385 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
386         struct rte_mbuf **pkts, uint16_t count);
387
388 /**
389  * This function gets guest buffers from the virtio device TX virtqueue,
390  * construct host mbufs, copies guest buffer content to host mbufs and
391  * store them in pkts to be processed.
392  * @param vid
393  *  vhost device ID
394  * @param queue_id
395  *  virtio queue index in mq case
396  * @param mbuf_pool
397  *  mbuf_pool where host mbuf is allocated.
398  * @param pkts
399  *  array to contain packets to be dequeued
400  * @param count
401  *  packets num to be dequeued
402  * @return
403  *  num of packets dequeued
404  */
405 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
406         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
407
408 /**
409  * Get guest mem table: a list of memory regions.
410  *
411  * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
412  * guest memory regions. Application should free it at destroy_device()
413  * callback.
414  *
415  * @param vid
416  *  vhost device ID
417  * @param mem
418  *  To store the returned mem regions
419  * @return
420  *  0 on success, -1 on failure
421  */
422 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
423
424 /**
425  * Get guest vring info, including the vring address, vring size, etc.
426  *
427  * @param vid
428  *  vhost device ID
429  * @param vring_idx
430  *  vring index
431  * @param vring
432  *  the structure to hold the requested vring info
433  * @return
434  *  0 on success, -1 on failure
435  */
436 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
437                               struct rte_vhost_vring *vring);
438
439 /**
440  * Get vhost RX queue avail count.
441  *
442  * @param vid
443  *  vhost device ID
444  * @param qid
445  *  virtio queue index in mq case
446  * @return
447  *  num of desc available
448  */
449 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
450
451 #ifdef __cplusplus
452 }
453 #endif
454
455 #endif /* _RTE_VHOST_H_ */