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