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