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