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