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