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