Imported Upstream version 16.07-rc1
[deb_dpdk.git] / lib / librte_kni / rte_kni.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 _RTE_KNI_H_
35 #define _RTE_KNI_H_
36
37 /**
38  * @file
39  * RTE KNI
40  *
41  * The KNI library provides the ability to create and destroy kernel NIC
42  * interfaces that may be used by the RTE application to receive/transmit
43  * packets from/to Linux kernel net interfaces.
44  *
45  * This library provide two APIs to burst receive packets from KNI interfaces,
46  * and burst transmit packets to KNI interfaces.
47  */
48
49 #include <rte_pci.h>
50 #include <rte_memory.h>
51 #include <rte_mempool.h>
52
53 #include <exec-env/rte_kni_common.h>
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59 struct rte_kni;
60 struct rte_mbuf;
61
62 /**
63  * Structure which has the function pointers for KNI interface.
64  */
65 struct rte_kni_ops {
66         uint8_t port_id; /* Port ID */
67
68         /* Pointer to function of changing MTU */
69         int (*change_mtu)(uint8_t port_id, unsigned new_mtu);
70
71         /* Pointer to function of configuring network interface */
72         int (*config_network_if)(uint8_t port_id, uint8_t if_up);
73 };
74
75 /**
76  * Structure for configuring KNI device.
77  */
78 struct rte_kni_conf {
79         /*
80          * KNI name which will be used in relevant network device.
81          * Let the name as short as possible, as it will be part of
82          * memzone name.
83          */
84         char name[RTE_KNI_NAMESIZE];
85         uint32_t core_id;   /* Core ID to bind kernel thread on */
86         uint16_t group_id;  /* Group ID */
87         unsigned mbuf_size; /* mbuf size */
88         struct rte_pci_addr addr;
89         struct rte_pci_id id;
90
91         uint8_t force_bind : 1; /* Flag to bind kernel thread */
92 };
93
94 /**
95  * Initialize and preallocate KNI subsystem
96  *
97  * This function is to be executed on the MASTER lcore only, after EAL
98  * initialization and before any KNI interface is attempted to be
99  * allocated
100  *
101  * @param max_kni_ifaces
102  *  The maximum number of KNI interfaces that can coexist concurrently
103  */
104 void rte_kni_init(unsigned int max_kni_ifaces);
105
106
107 /**
108  * Allocate KNI interface according to the port id, mbuf size, mbuf pool,
109  * configurations and callbacks for kernel requests.The KNI interface created
110  * in the kernel space is the net interface the traditional Linux application
111  * talking to.
112  *
113  * The rte_kni_alloc shall not be called before rte_kni_init() has been
114  * called. rte_kni_alloc is thread safe.
115  *
116  * The mempool should have capacity of more than "2 x KNI_FIFO_COUNT_MAX"
117  * elements for each KNI interface allocated.
118  *
119  * @param pktmbuf_pool
120  *  The mempool for allocting mbufs for packets.
121  * @param conf
122  *  The pointer to the configurations of the KNI device.
123  * @param ops
124  *  The pointer to the callbacks for the KNI kernel requests.
125  *
126  * @return
127  *  - The pointer to the context of a KNI interface.
128  *  - NULL indicate error.
129  */
130 struct rte_kni *rte_kni_alloc(struct rte_mempool *pktmbuf_pool,
131                 const struct rte_kni_conf *conf, struct rte_kni_ops *ops);
132
133 /**
134  * Release KNI interface according to the context. It will also release the
135  * paired KNI interface in kernel space. All processing on the specific KNI
136  * context need to be stopped before calling this interface.
137  *
138  * rte_kni_release is thread safe.
139  *
140  * @param kni
141  *  The pointer to the context of an existent KNI interface.
142  *
143  * @return
144  *  - 0 indicates success.
145  *  - negative value indicates failure.
146  */
147 int rte_kni_release(struct rte_kni *kni);
148
149 /**
150  * It is used to handle the request mbufs sent from kernel space.
151  * Then analyzes it and calls the specific actions for the specific requests.
152  * Finally constructs the response mbuf and puts it back to the resp_q.
153  *
154  * @param kni
155  *  The pointer to the context of an existent KNI interface.
156  *
157  * @return
158  *  - 0
159  *  - negative value indicates failure.
160  */
161 int rte_kni_handle_request(struct rte_kni *kni);
162
163 /**
164  * Retrieve a burst of packets from a KNI interface. The retrieved packets are
165  * stored in rte_mbuf structures whose pointers are supplied in the array of
166  * mbufs, and the maximum number is indicated by num. It handles allocating
167  * the mbufs for KNI interface alloc queue.
168  *
169  * @param kni
170  *  The KNI interface context.
171  * @param mbufs
172  *  The array to store the pointers of mbufs.
173  * @param num
174  *  The maximum number per burst.
175  *
176  * @return
177  *  The actual number of packets retrieved.
178  */
179 unsigned rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs,
180                 unsigned num);
181
182 /**
183  * Send a burst of packets to a KNI interface. The packets to be sent out are
184  * stored in rte_mbuf structures whose pointers are supplied in the array of
185  * mbufs, and the maximum number is indicated by num. It handles the freeing of
186  * the mbufs in the free queue of KNI interface.
187  *
188  * @param kni
189  *  The KNI interface context.
190  * @param mbufs
191  *  The array to store the pointers of mbufs.
192  * @param num
193  *  The maximum number per burst.
194  *
195  * @return
196  *  The actual number of packets sent.
197  */
198 unsigned rte_kni_tx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs,
199                 unsigned num);
200
201 /**
202  * Get the KNI context of its name.
203  *
204  * @param name
205  *  pointer to the KNI device name.
206  *
207  * @return
208  *  On success: Pointer to KNI interface.
209  *  On failure: NULL.
210  */
211 struct rte_kni *rte_kni_get(const char *name);
212
213 /**
214  * Get the name given to a KNI device
215  *
216  * @param kni
217  *   The KNI instance to query
218  * @return
219  *   The pointer to the KNI name
220  */
221 const char *rte_kni_get_name(const struct rte_kni *kni);
222
223 /**
224  * Register KNI request handling for a specified port,and it can
225  * be called by master process or slave process.
226  *
227  * @param kni
228  *  pointer to struct rte_kni.
229  * @param ops
230  *  ponter to struct rte_kni_ops.
231  *
232  * @return
233  *  On success: 0
234  *  On failure: -1
235  */
236 int rte_kni_register_handlers(struct rte_kni *kni, struct rte_kni_ops *ops);
237
238 /**
239  *  Unregister KNI request handling for a specified port.
240  *
241  *  @param kni
242  *   pointer to struct rte_kni.
243  *
244  *  @return
245  *   On success: 0
246  *   On failure: -1
247  */
248 int rte_kni_unregister_handlers(struct rte_kni *kni);
249
250 /**
251  *  Close KNI device.
252  */
253 void rte_kni_close(void);
254
255 #ifdef __cplusplus
256 }
257 #endif
258
259 #endif /* _RTE_KNI_H_ */