vhost: export the number of vrings
[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  * @deprecated
247  * Get the number of queues the device supports.
248  *
249  * Note this function is deprecated, as it returns a queue pair number,
250  * which is virtio-net specific. Instead, rte_vhost_get_vring_num should
251  * be used.
252  *
253  * @param vid
254  *  virtio-net device ID
255  *
256  * @return
257  *  The number of queues, 0 on failure
258  */
259 __rte_deprecated
260 uint32_t rte_vhost_get_queue_num(int vid);
261
262 /**
263  * Get the number of vrings the device supports.
264  *
265  * @param vid
266  *  vhost device ID
267  *
268  * @return
269  *  The number of vrings, 0 on failure
270  */
271 uint16_t rte_vhost_get_vring_num(int vid);
272
273 /**
274  * Get the virtio net device's ifname, which is the vhost-user socket
275  * file path.
276  *
277  * @param vid
278  *  virtio-net device ID
279  * @param buf
280  *  The buffer to stored the queried ifname
281  * @param len
282  *  The length of buf
283  *
284  * @return
285  *  0 on success, -1 on failure
286  */
287 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
288
289 /**
290  * Get how many avail entries are left in the queue
291  *
292  * @param vid
293  *  virtio-net device ID
294  * @param queue_id
295  *  virtio queue index
296  *
297  * @return
298  *  num of avail entires left
299  */
300 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
301
302 /**
303  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
304  * be received from the physical port or from another virtual device. A packet
305  * count is returned to indicate the number of packets that were succesfully
306  * added to the RX queue.
307  * @param vid
308  *  virtio-net device ID
309  * @param queue_id
310  *  virtio queue index in mq case
311  * @param pkts
312  *  array to contain packets to be enqueued
313  * @param count
314  *  packets num to be enqueued
315  * @return
316  *  num of packets enqueued
317  */
318 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
319         struct rte_mbuf **pkts, uint16_t count);
320
321 /**
322  * This function gets guest buffers from the virtio device TX virtqueue,
323  * construct host mbufs, copies guest buffer content to host mbufs and
324  * store them in pkts to be processed.
325  * @param vid
326  *  virtio-net device
327  * @param queue_id
328  *  virtio queue index in mq case
329  * @param mbuf_pool
330  *  mbuf_pool where host mbuf is allocated.
331  * @param pkts
332  *  array to contain packets to be dequeued
333  * @param count
334  *  packets num to be dequeued
335  * @return
336  *  num of packets dequeued
337  */
338 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
339         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
340
341 /**
342  * Get guest mem table: a list of memory regions.
343  *
344  * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
345  * guest memory regions. Application should free it at destroy_device()
346  * callback.
347  *
348  * @param vid
349  *  vhost device ID
350  * @param mem
351  *  To store the returned mem regions
352  * @return
353  *  0 on success, -1 on failure
354  */
355 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
356
357 /**
358  * Get guest vring info, including the vring address, vring size, etc.
359  *
360  * @param vid
361  *  vhost device ID
362  * @param vring_idx
363  *  vring index
364  * @param vring
365  *  the structure to hold the requested vring info
366  * @return
367  *  0 on success, -1 on failure
368  */
369 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
370                               struct rte_vhost_vring *vring);
371
372 #endif /* _VIRTIO_NET_H_ */