vhost: hide internal code
[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 /* Enum for virtqueue management. */
55 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
56
57 /**
58  * Device and vring operations.
59  *
60  * Make sure to set VIRTIO_DEV_RUNNING to the device flags in new_device and
61  * remove it in destroy_device.
62  *
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
71 /**
72  *  Disable features in feature_mask. Returns 0 on success.
73  */
74 int rte_vhost_feature_disable(uint64_t feature_mask);
75
76 /**
77  *  Enable features in feature_mask. Returns 0 on success.
78  */
79 int rte_vhost_feature_enable(uint64_t feature_mask);
80
81 /* Returns currently supported vhost features */
82 uint64_t rte_vhost_feature_get(void);
83
84 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
85
86 /* Register vhost driver. dev_name could be different for multiple instance support. */
87 int rte_vhost_driver_register(const char *dev_name);
88
89 /* Unregister vhost driver. This is only meaningful to vhost user. */
90 int rte_vhost_driver_unregister(const char *dev_name);
91
92 /* Register callbacks. */
93 int rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const);
94 /* Start vhost driver session blocking loop. */
95 int rte_vhost_driver_session_start(void);
96
97 /**
98  * Get the numa node from which the virtio net device's memory
99  * is allocated.
100  *
101  * @param vid
102  *  virtio-net device ID
103  *
104  * @return
105  *  The numa node, -1 on failure
106  */
107 int rte_vhost_get_numa_node(int vid);
108
109 /**
110  * Get the number of queues the device supports.
111  *
112  * @param vid
113  *  virtio-net device ID
114  *
115  * @return
116  *  The number of queues, 0 on failure
117  */
118 uint32_t rte_vhost_get_queue_num(int vid);
119
120 /**
121  * Get the virtio net device's ifname. For vhost-cuse, ifname is the
122  * path of the char device. For vhost-user, ifname is the vhost-user
123  * socket file path.
124  *
125  * @param vid
126  *  virtio-net device ID
127  * @param buf
128  *  The buffer to stored the queried ifname
129  * @param len
130  *  The length of buf
131  *
132  * @return
133  *  0 on success, -1 on failure
134  */
135 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
136
137 /**
138  * Get how many avail entries are left in the queue
139  *
140  * @param vid
141  *  virtio-net device ID
142  * @param queue_id
143  *  virtio queue index
144  *
145  * @return
146  *  num of avail entires left
147  */
148 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
149
150 /**
151  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
152  * be received from the physical port or from another virtual device. A packet
153  * count is returned to indicate the number of packets that were succesfully
154  * added to the RX queue.
155  * @param vid
156  *  virtio-net device ID
157  * @param queue_id
158  *  virtio queue index in mq case
159  * @param pkts
160  *  array to contain packets to be enqueued
161  * @param count
162  *  packets num to be enqueued
163  * @return
164  *  num of packets enqueued
165  */
166 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
167         struct rte_mbuf **pkts, uint16_t count);
168
169 /**
170  * This function gets guest buffers from the virtio device TX virtqueue,
171  * construct host mbufs, copies guest buffer content to host mbufs and
172  * store them in pkts to be processed.
173  * @param vid
174  *  virtio-net device
175  * @param queue_id
176  *  virtio queue index in mq case
177  * @param mbuf_pool
178  *  mbuf_pool where host mbuf is allocated.
179  * @param pkts
180  *  array to contain packets to be dequeued
181  * @param count
182  *  packets num to be dequeued
183  * @return
184  *  num of packets dequeued
185  */
186 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
187         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
188
189 #endif /* _VIRTIO_NET_H_ */