vhost: introduce API to fetch negotiated features
[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 /**
84  * Device and vring operations.
85  */
86 struct virtio_net_device_ops {
87         int (*new_device)(int vid);             /**< Add device. */
88         void (*destroy_device)(int vid);        /**< Remove device. */
89
90         int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);     /**< triggered when a vring is enabled or disabled */
91
92         void *reserved[5]; /**< Reserved for future extension */
93 };
94
95 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
96
97 /**
98  * Register vhost driver. path could be different for multiple
99  * instance support.
100  */
101 int rte_vhost_driver_register(const char *path, uint64_t flags);
102
103 /* Unregister vhost driver. This is only meaningful to vhost user. */
104 int rte_vhost_driver_unregister(const char *path);
105
106 /**
107  * Set the feature bits the vhost-user driver supports.
108  *
109  * @param path
110  *  The vhost-user socket file path
111  * @param features
112  *  Supported features
113  * @return
114  *  0 on success, -1 on failure
115  */
116 int rte_vhost_driver_set_features(const char *path, uint64_t features);
117
118 /**
119  * Enable vhost-user driver features.
120  *
121  * Note that
122  * - the param features should be a subset of the feature bits provided
123  *   by rte_vhost_driver_set_features().
124  * - it must be invoked before vhost-user negotiation starts.
125  *
126  * @param path
127  *  The vhost-user socket file path
128  * @param features
129  *  Features to enable
130  * @return
131  *  0 on success, -1 on failure
132  */
133 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
134
135 /**
136  * Disable vhost-user driver features.
137  *
138  * The two notes at rte_vhost_driver_enable_features() also apply here.
139  *
140  * @param path
141  *  The vhost-user socket file path
142  * @param features
143  *  Features to disable
144  * @return
145  *  0 on success, -1 on failure
146  */
147 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
148
149 /**
150  * Get the feature bits before feature negotiation.
151  *
152  * @param path
153  *  The vhost-user socket file path
154  * @param features
155  *  A pointer to store the queried feature bits
156  * @return
157  *  0 on success, -1 on failure
158  */
159 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
160
161 /**
162  * Get the feature bits after negotiation
163  *
164  * @param vid
165  *  Vhost device ID
166  * @param features
167  *  A pointer to store the queried feature bits
168  * @return
169  *  0 on success, -1 on failure
170  */
171 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
172
173 /* Register callbacks. */
174 int rte_vhost_driver_callback_register(const char *path,
175         struct virtio_net_device_ops const * const ops);
176 /* Start vhost driver session blocking loop. */
177 int rte_vhost_driver_session_start(void);
178
179 /**
180  * Get the MTU value of the device if set in QEMU.
181  *
182  * @param vid
183  *  virtio-net device ID
184  * @param mtu
185  *  The variable to store the MTU value
186  *
187  * @return
188  *  0: success
189  *  -EAGAIN: device not yet started
190  *  -ENOTSUP: device does not support MTU feature
191  */
192 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
193
194 /**
195  * Get the numa node from which the virtio net device's memory
196  * is allocated.
197  *
198  * @param vid
199  *  virtio-net device ID
200  *
201  * @return
202  *  The numa node, -1 on failure
203  */
204 int rte_vhost_get_numa_node(int vid);
205
206 /**
207  * Get the number of queues the device supports.
208  *
209  * @param vid
210  *  virtio-net device ID
211  *
212  * @return
213  *  The number of queues, 0 on failure
214  */
215 uint32_t rte_vhost_get_queue_num(int vid);
216
217 /**
218  * Get the virtio net device's ifname, which is the vhost-user socket
219  * file path.
220  *
221  * @param vid
222  *  virtio-net device ID
223  * @param buf
224  *  The buffer to stored the queried ifname
225  * @param len
226  *  The length of buf
227  *
228  * @return
229  *  0 on success, -1 on failure
230  */
231 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
232
233 /**
234  * Get how many avail entries are left in the queue
235  *
236  * @param vid
237  *  virtio-net device ID
238  * @param queue_id
239  *  virtio queue index
240  *
241  * @return
242  *  num of avail entires left
243  */
244 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
245
246 /**
247  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
248  * be received from the physical port or from another virtual device. A packet
249  * count is returned to indicate the number of packets that were succesfully
250  * added to the RX queue.
251  * @param vid
252  *  virtio-net device ID
253  * @param queue_id
254  *  virtio queue index in mq case
255  * @param pkts
256  *  array to contain packets to be enqueued
257  * @param count
258  *  packets num to be enqueued
259  * @return
260  *  num of packets enqueued
261  */
262 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
263         struct rte_mbuf **pkts, uint16_t count);
264
265 /**
266  * This function gets guest buffers from the virtio device TX virtqueue,
267  * construct host mbufs, copies guest buffer content to host mbufs and
268  * store them in pkts to be processed.
269  * @param vid
270  *  virtio-net device
271  * @param queue_id
272  *  virtio queue index in mq case
273  * @param mbuf_pool
274  *  mbuf_pool where host mbuf is allocated.
275  * @param pkts
276  *  array to contain packets to be dequeued
277  * @param count
278  *  packets num to be dequeued
279  * @return
280  *  num of packets dequeued
281  */
282 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
283         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
284
285 /**
286  * Get guest mem table: a list of memory regions.
287  *
288  * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
289  * guest memory regions. Application should free it at destroy_device()
290  * callback.
291  *
292  * @param vid
293  *  vhost device ID
294  * @param mem
295  *  To store the returned mem regions
296  * @return
297  *  0 on success, -1 on failure
298  */
299 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
300
301 #endif /* _VIRTIO_NET_H_ */