0854b215e0a5b886cb98757b61dd351d6111db24
[dpdk.git] / lib / librte_kni / rte_kni.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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_EXEC_ENV_LINUXAPP
36 #error "KNI is not supported"
37 #endif
38
39 #include <string.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <sys/ioctl.h>
43 #include <netinet/in.h>
44 #include <linux/if.h>
45
46 #include <rte_string_fns.h>
47 #include <rte_ethdev.h>
48 #include <rte_malloc.h>
49 #include <rte_log.h>
50 #include <rte_kni.h>
51 #include <rte_memzone.h>
52 #include <exec-env/rte_kni_common.h>
53 #include "rte_kni_fifo.h"
54
55 #define MAX_MBUF_BURST_NUM            32
56
57 /* Maximum number of ring entries */
58 #define KNI_FIFO_COUNT_MAX     1024
59 #define KNI_FIFO_SIZE          (KNI_FIFO_COUNT_MAX * sizeof(void *) + \
60                                         sizeof(struct rte_kni_fifo))
61
62 #define KNI_REQUEST_MBUF_NUM_MAX      32
63
64 /**
65  * KNI context
66  */
67 struct rte_kni {
68         char name[IFNAMSIZ];                /**< KNI interface name */
69         uint8_t port_id;                    /**< Port id KNI associate with */
70         struct rte_mempool *pktmbuf_pool;   /**< pkt mbuf mempool */
71         unsigned mbuf_size;                 /**< mbuf size */
72
73         struct rte_kni_fifo *tx_q;          /**< TX queue */
74         struct rte_kni_fifo *rx_q;          /**< RX queue */
75         struct rte_kni_fifo *alloc_q;       /**< Allocated mbufs queue */
76         struct rte_kni_fifo *free_q;        /**< To be freed mbufs queue */
77
78         /* For request & response */
79         struct rte_kni_fifo *req_q;         /**< Request queue */
80         struct rte_kni_fifo *resp_q;        /**< Response queue */
81         void * sync_addr;                                       /**< Req/Resp Mem address */
82
83         struct rte_kni_ops ops;             /**< operations for request */
84 };
85
86 static void kni_free_mbufs(struct rte_kni *kni);
87 static void kni_allocate_mbufs(struct rte_kni *kni);
88
89 static int kni_fd = -1;
90
91
92 struct rte_kni *
93 rte_kni_create(uint8_t port_id,
94                 unsigned mbuf_size,
95                 struct rte_mempool *pktmbuf_pool,
96                 struct rte_kni_ops *ops)
97 {
98         struct rte_kni_device_info dev_info;
99         struct rte_eth_dev_info eth_dev_info;
100         struct rte_kni *ctx;
101         char itf_name[IFNAMSIZ];
102 #define OBJNAMSIZ 32
103         char obj_name[OBJNAMSIZ];
104         const struct rte_memzone *mz;
105
106         if (port_id >= RTE_MAX_ETHPORTS || pktmbuf_pool == NULL || !ops)
107                 return NULL;
108
109         /* Check FD and open once */
110         if (kni_fd < 0) {
111                 kni_fd = open("/dev/" KNI_DEVICE, O_RDWR);
112                 if (kni_fd < 0) {
113                         RTE_LOG(ERR, KNI, "Can not open /dev/%s\n",
114                                                         KNI_DEVICE);
115                         return NULL;
116                 }
117         }
118
119         rte_eth_dev_info_get(port_id, &eth_dev_info);
120         RTE_LOG(INFO, KNI, "pci: %02x:%02x:%02x \t %02x:%02x\n",
121                                         eth_dev_info.pci_dev->addr.bus,
122                                         eth_dev_info.pci_dev->addr.devid,
123                                         eth_dev_info.pci_dev->addr.function,
124                                         eth_dev_info.pci_dev->id.vendor_id,
125                                         eth_dev_info.pci_dev->id.device_id);
126         dev_info.bus = eth_dev_info.pci_dev->addr.bus;
127         dev_info.devid = eth_dev_info.pci_dev->addr.devid;
128         dev_info.function = eth_dev_info.pci_dev->addr.function;
129         dev_info.vendor_id = eth_dev_info.pci_dev->id.vendor_id;
130         dev_info.device_id = eth_dev_info.pci_dev->id.device_id;
131
132         ctx = rte_zmalloc("kni devs", sizeof(struct rte_kni), 0);
133         if (ctx == NULL)
134                 rte_panic("Cannot allocate memory for kni dev\n");
135         memcpy(&ctx->ops, ops, sizeof(struct rte_kni_ops));
136
137         rte_snprintf(itf_name, IFNAMSIZ, "vEth%u", port_id);
138         rte_snprintf(ctx->name, IFNAMSIZ, itf_name);
139         rte_snprintf(dev_info.name, IFNAMSIZ, itf_name);
140
141         /* TX RING */
142         rte_snprintf(obj_name, OBJNAMSIZ, "kni_tx_%d", port_id);
143         mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
144         if (mz == NULL || mz->addr == NULL)
145                 rte_panic("Cannot create kni_tx_%d queue\n", port_id);
146         ctx->tx_q = mz->addr;
147         kni_fifo_init(ctx->tx_q, KNI_FIFO_COUNT_MAX);
148         dev_info.tx_phys = mz->phys_addr;
149
150         /* RX RING */
151         rte_snprintf(obj_name, OBJNAMSIZ, "kni_rx_%d", port_id);
152         mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
153         if (mz == NULL || mz->addr == NULL)
154                 rte_panic("Cannot create kni_rx_%d queue\n", port_id);
155         ctx->rx_q = mz->addr;
156         kni_fifo_init(ctx->rx_q, KNI_FIFO_COUNT_MAX);
157         dev_info.rx_phys = mz->phys_addr;
158
159         /* ALLOC RING */
160         rte_snprintf(obj_name, OBJNAMSIZ, "kni_alloc_%d", port_id);
161         mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
162         if (mz == NULL || mz->addr == NULL)
163                 rte_panic("Cannot create kni_alloc_%d queue\n", port_id);
164         ctx->alloc_q = mz->addr;
165         kni_fifo_init(ctx->alloc_q, KNI_FIFO_COUNT_MAX);
166         dev_info.alloc_phys = mz->phys_addr;
167
168         /* FREE RING */
169         rte_snprintf(obj_name, OBJNAMSIZ, "kni_free_%d", port_id);
170         mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
171         if (mz == NULL || mz->addr == NULL)
172                 rte_panic("Cannot create kni_free_%d queue\n", port_id);
173         ctx->free_q = mz->addr;
174         kni_fifo_init(ctx->free_q, KNI_FIFO_COUNT_MAX);
175         dev_info.free_phys = mz->phys_addr;
176
177         /* Request RING */
178         rte_snprintf(obj_name, OBJNAMSIZ, "kni_req_%d", port_id);
179         mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
180         if (mz == NULL || mz->addr == NULL)
181                 rte_panic("Cannot create kni_req_%d ring\n", port_id);
182         ctx->req_q = mz->addr;
183         kni_fifo_init(ctx->req_q, KNI_FIFO_COUNT_MAX);
184         dev_info.req_phys = mz->phys_addr;
185
186         /* Response RING */
187         rte_snprintf(obj_name, OBJNAMSIZ, "kni_resp_%d", port_id);
188         mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
189         if (mz == NULL || mz->addr == NULL)
190                 rte_panic("Cannot create kni_resp_%d ring\n", port_id);
191         ctx->resp_q = mz->addr;
192         kni_fifo_init(ctx->resp_q, KNI_FIFO_COUNT_MAX);
193         dev_info.resp_phys = mz->phys_addr;
194
195         /* Req/Resp sync mem area */
196         rte_snprintf(obj_name, OBJNAMSIZ, "kni_sync_%d", port_id);
197         mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
198         if (mz == NULL || mz->addr == NULL)
199                 rte_panic("Cannot create kni_sync_%d mem\n", port_id);
200         ctx->sync_addr = mz->addr;
201         dev_info.sync_va = mz->addr;
202         dev_info.sync_phys = mz->phys_addr;
203
204         /* MBUF mempool */
205         mz = rte_memzone_lookup("MP_mbuf_pool");
206         if (mz == NULL) {
207                 RTE_LOG(ERR, KNI, "Can not find MP_mbuf_pool\n");
208                 goto fail;
209         }
210         dev_info.mbuf_va = mz->addr;
211         dev_info.mbuf_phys = mz->phys_addr;
212         ctx->pktmbuf_pool = pktmbuf_pool;
213         ctx->port_id = port_id;
214         ctx->mbuf_size = mbuf_size;
215
216         /* Configure the buffer size which will be checked in kernel module */
217         dev_info.mbuf_size = ctx->mbuf_size;
218
219         if (ioctl(kni_fd, RTE_KNI_IOCTL_CREATE, &dev_info) < 0) {
220                 RTE_LOG(ERR, KNI, "Fail to create kni device\n");
221                 goto fail;
222         }
223
224         return ctx;
225
226 fail:
227         if (ctx != NULL)
228                 rte_free(ctx);
229
230         return NULL;
231 }
232
233 /**
234  * It is called in the same lcore of receiving packets, and polls the request
235  * mbufs sent from kernel space. Then analyzes it and calls the specific
236  * actions for the specific requests. Finally constructs the response mbuf and
237  * puts it back to the resp_q.
238  */
239 static int
240 kni_request_handler(struct rte_kni *kni)
241 {
242         unsigned ret;
243         struct rte_kni_request *req;
244
245         if (kni == NULL)
246                 return -1;
247
248         /* Get request mbuf */
249         ret = kni_fifo_get(kni->req_q, (void **)&req, 1);
250         if (ret != 1)
251                 return 0; /* It is OK of can not getting the request mbuf */
252
253         if (req != kni->sync_addr) {
254                 rte_panic("Wrong req pointer %p\n", req);
255         }
256
257         /* Analyze the request and call the relevant actions for it */
258         switch (req->req_id) {
259         case RTE_KNI_REQ_CHANGE_MTU: /* Change MTU */
260                 if (kni->ops.change_mtu)
261                         req->result = kni->ops.change_mtu(kni->port_id,
262                                                         req->new_mtu);
263                 break;
264         case RTE_KNI_REQ_CFG_NETWORK_IF: /* Set network interface up/down */
265                 if (kni->ops.config_network_if)
266                         req->result = kni->ops.config_network_if(kni->port_id,
267                                                                 req->if_up);
268                 break;
269         default:
270                 RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id);
271                 req->result = -EINVAL;
272                 break;
273         }
274
275         /* Construct response mbuf and put it back to resp_q */
276         ret = kni_fifo_put(kni->resp_q, (void **)&req, 1);
277         if (ret != 1) {
278                 RTE_LOG(ERR, KNI, "Fail to put the muf back to resp_q\n");
279                 return -1; /* It is an error of can't putting the mbuf back */
280         }
281
282         return 0;
283 }
284
285 unsigned
286 rte_kni_tx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
287 {
288         unsigned ret = kni_fifo_put(kni->rx_q, (void **)mbufs, num);
289
290         /* Get mbufs from free_q and then free them */
291         kni_free_mbufs(kni);
292
293         /* Handle the requests from kernel space */
294         kni_request_handler(kni);
295
296         return ret;
297 }
298
299 unsigned
300 rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
301 {
302         unsigned ret = kni_fifo_get(kni->tx_q, (void **)mbufs, num);
303
304         /* Allocate mbufs and then put them into alloc_q */
305         kni_allocate_mbufs(kni);
306
307         return ret;
308 }
309
310 static void
311 kni_free_mbufs(struct rte_kni *kni)
312 {
313         int i, ret;
314         struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
315
316         ret = kni_fifo_get(kni->free_q, (void **)pkts, MAX_MBUF_BURST_NUM);
317         if (likely(ret > 0)) {
318                 for (i = 0; i < ret; i++)
319                         rte_pktmbuf_free(pkts[i]);
320         }
321 }
322
323 static void
324 kni_allocate_mbufs(struct rte_kni *kni)
325 {
326         int i, ret;
327         struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
328
329         /* Check if pktmbuf pool has been configured */
330         if (kni->pktmbuf_pool == NULL) {
331                 RTE_LOG(ERR, KNI, "No valid mempool for allocating mbufs\n");
332                 return;
333         }
334
335         for (i = 0; i < MAX_MBUF_BURST_NUM; i++) {
336                 pkts[i] = rte_pktmbuf_alloc(kni->pktmbuf_pool);
337                 if (unlikely(pkts[i] == NULL)) {
338                         /* Out of memory */
339                         RTE_LOG(ERR, KNI, "Out of memory\n");
340                         break;
341                 }
342         }
343
344         /* No pkt mbuf alocated */
345         if (i <= 0)
346                 return;
347
348         ret = kni_fifo_put(kni->alloc_q, (void **)pkts, i);
349
350         /* Check if any mbufs not put into alloc_q, and then free them */
351         if (ret >= 0 && ret < i && ret < MAX_MBUF_BURST_NUM) {
352                 int j;
353
354                 for (j = ret; j < i; j++)
355                         rte_pktmbuf_free(pkts[j]);
356         }
357 }
358
359 uint8_t
360 rte_kni_get_port_id(struct rte_kni *kni)
361 {
362         if (kni == NULL)
363                 return ~0x0;
364
365         return kni->port_id;
366 }
367