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