0f230bcdc0951bff850d1cb80c0a3e262264ec98
[dpdk.git] / lib / librte_kni / rte_kni.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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
35 #ifndef _RTE_KNI_H_
36 #define _RTE_KNI_H_
37
38 /**
39  * @file
40  * RTE KNI
41  *
42  * The KNI library provides the ability to create and destroy kernel NIC
43  * interfaces that may be used by the RTE application to receive/transmit
44  * packets from/to Linux kernel net interfaces.
45  *
46  * This library provide two APIs to burst receive packets from KNI interfaces,
47  * and burst transmit packets to KNI interfaces.
48  */
49
50 #include <rte_mbuf.h>
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56 struct rte_kni;
57
58 /**
59  * Structure which has the function pointers for KNI interface.
60  */
61 struct rte_kni_ops {
62         /* Pointer to function of changing MTU */
63         int (*change_mtu)(uint8_t port_id, unsigned new_mtu);
64
65         /* Pointer to function of configuring network interface */
66         int (*config_network_if)(uint8_t port_id, uint8_t if_up);
67 };
68
69 /**
70  * Create kni interface according to the port id. It will create a paired KNI
71  * interface in the kernel space for each NIC port. The KNI interface created
72  * in the kernel space is the net interface the traditional Linux application
73  * talking to.
74  *
75  * @param port_id
76  *  The port id.
77  * @param pktmbuf_pool
78  *  The mempool for allocting mbufs for packets.
79  * @param mbuf_size
80  *  The mbuf size to store a packet.
81  *
82  * @return
83  *  - The pointer to the context of a kni interface.
84  *  - NULL indicate error.
85  */
86 extern struct rte_kni *rte_kni_create(uint8_t port_id, unsigned mbuf_size,
87                 struct rte_mempool *pktmbuf_pool, struct rte_kni_ops *ops);
88
89 /**
90  * Release kni interface according to the context. It will also release the
91  * paired KNI interface in kernel space. All processing on the specific kni
92  * context need to be stopped before calling this interface.
93  *
94  * @param kni
95  *  The pointer to the context of an existant kni interface.
96  *
97  * @return
98  *  - 0 indicates success.
99  *  - negative value indicates failure.
100  */
101 extern int rte_kni_release(struct rte_kni *kni);
102
103 /**
104  * It is used to handle the request mbufs sent from kernel space. 
105  * Then analyzes it and calls the specific actions for the specific requests.
106  * Finally constructs the response mbuf and puts it back to the resp_q.
107  *
108  * @param kni
109  *  The pointer to the context of an existant kni interface.
110  *
111  * @return
112  *  - 0 
113  *  - negative value indicates failure.
114  */
115 extern int rte_kni_handle_request(struct rte_kni *kni);
116
117 /**
118  * Retrieve a burst of packets from a kni interface. The retrieved packets are
119  * stored in rte_mbuf structures whose pointers are supplied in the array of
120  * mbufs, and the maximum number is indicated by num. It handles the freeing of
121  * the mbufs in the free queue of kni interface.
122  *
123  * @param kni
124  *  The kni interface context.
125  * @param mbufs
126  *  The array to store the pointers of mbufs.
127  * @param num
128  *  The maximum number per burst.
129  *
130  * @return
131  *  The actual number of packets retrieved.
132  */
133 extern unsigned rte_kni_rx_burst(struct rte_kni *kni,
134                 struct rte_mbuf **mbufs, unsigned num);
135
136 /**
137  * Send a burst of packets to a kni interface. The packets to be sent out are
138  * stored in rte_mbuf structures whose pointers are supplied in the array of
139  * mbufs, and the maximum number is indicated by num. It handles allocating
140  * the mbufs for kni interface alloc queue.
141  *
142  * @param kni
143  *  The kni interface context.
144  * @param mbufs
145  *  The array to store the pointers of mbufs.
146  * @param num
147  *  The maximum number per burst.
148  *
149  * @return
150  *  The actual number of packets sent.
151  */
152 extern unsigned rte_kni_tx_burst(struct rte_kni *kni,
153                 struct rte_mbuf **mbufs, unsigned num);
154
155 /**
156  * Get the port id from kni interface.
157  *
158  * @param kni
159  *  The kni interface context.
160  *
161  * @return
162  *  On success: The port id.
163  *  On failure: ~0x0
164  */
165 extern uint8_t rte_kni_get_port_id(struct rte_kni *kni);
166
167 /**
168  * Get kni context information of the port.  
169  *
170  * @port_id
171  *  the port id.
172  *
173  * @return 
174  *  On success: Pointer to kni interface.
175  *  On failure: NULL
176  */
177 extern struct rte_kni * rte_kni_info_get(uint8_t port_id);
178
179 /**
180  * Register kni request handling for a specified port,and it can
181  * be called by master process or slave process.
182  *
183  * @param kni 
184  *  pointer to struct rte_kni. 
185  * @param ops 
186  *  ponter to struct rte_kni_ops.
187  *
188  * @return
189  *  On success: 0
190  *  On failure: -1
191  */
192 extern int rte_kni_register_handlers(struct rte_kni *kni,
193                         struct rte_kni_ops *ops);
194
195 /**
196  *  Unregister kni request handling for a specified port.
197  * 
198  *  @param kni 
199  *   pointer to struct rte_kni. 
200  *
201  *  @return
202  *   On success: 0
203  *   On failure: -1
204  */
205 extern int rte_kni_unregister_handlers(struct rte_kni *kni);
206
207 #ifdef __cplusplus
208 }
209 #endif
210
211 #endif /* _RTE_KNI_H_ */
212