kni: optimize Rx burst
[dpdk.git] / lib / librte_kni / rte_kni.c
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_EXEC_ENV_LINUXAPP
35 #error "KNI is not supported"
36 #endif
37
38 #include <string.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <sys/ioctl.h>
42
43 #include <rte_spinlock.h>
44 #include <rte_string_fns.h>
45 #include <rte_ethdev.h>
46 #include <rte_malloc.h>
47 #include <rte_log.h>
48 #include <rte_kni.h>
49 #include <rte_memzone.h>
50 #include <exec-env/rte_kni_common.h>
51 #include "rte_kni_fifo.h"
52
53 #define MAX_MBUF_BURST_NUM            32
54
55 /* Maximum number of ring entries */
56 #define KNI_FIFO_COUNT_MAX     1024
57 #define KNI_FIFO_SIZE          (KNI_FIFO_COUNT_MAX * sizeof(void *) + \
58                                         sizeof(struct rte_kni_fifo))
59
60 #define KNI_REQUEST_MBUF_NUM_MAX      32
61
62 #define KNI_MEM_CHECK(cond) do { if (cond) goto kni_fail; } while (0)
63
64 /**
65  * KNI context
66  */
67 struct rte_kni {
68         char name[RTE_KNI_NAMESIZE];        /**< KNI interface name */
69         uint16_t group_id;                  /**< Group ID of KNI devices */
70         uint32_t slot_id;                   /**< KNI pool slot ID */
71         struct rte_mempool *pktmbuf_pool;   /**< pkt mbuf mempool */
72         unsigned mbuf_size;                 /**< mbuf size */
73
74         struct rte_kni_fifo *tx_q;          /**< TX queue */
75         struct rte_kni_fifo *rx_q;          /**< RX queue */
76         struct rte_kni_fifo *alloc_q;       /**< Allocated mbufs queue */
77         struct rte_kni_fifo *free_q;        /**< To be freed mbufs queue */
78
79         /* For request & response */
80         struct rte_kni_fifo *req_q;         /**< Request queue */
81         struct rte_kni_fifo *resp_q;        /**< Response queue */
82         void * sync_addr;                   /**< Req/Resp Mem address */
83
84         struct rte_kni_ops ops;             /**< operations for request */
85         uint8_t in_use : 1;                 /**< kni in use */
86 };
87
88 enum kni_ops_status {
89         KNI_REQ_NO_REGISTER = 0,
90         KNI_REQ_REGISTERED,
91 };
92
93 /**
94  * KNI memzone pool slot
95  */
96 struct rte_kni_memzone_slot {
97         uint32_t id;
98         uint8_t in_use : 1;                    /**< slot in use */
99
100         /* Memzones */
101         const struct rte_memzone *m_ctx;       /**< KNI ctx */
102         const struct rte_memzone *m_tx_q;      /**< TX queue */
103         const struct rte_memzone *m_rx_q;      /**< RX queue */
104         const struct rte_memzone *m_alloc_q;   /**< Allocated mbufs queue */
105         const struct rte_memzone *m_free_q;    /**< To be freed mbufs queue */
106         const struct rte_memzone *m_req_q;     /**< Request queue */
107         const struct rte_memzone *m_resp_q;    /**< Response queue */
108         const struct rte_memzone *m_sync_addr;
109
110         /* Free linked list */
111         struct rte_kni_memzone_slot *next;     /**< Next slot link.list */
112 };
113
114 /**
115  * KNI memzone pool
116  */
117 struct rte_kni_memzone_pool {
118         uint8_t initialized : 1;            /**< Global KNI pool init flag */
119
120         uint32_t max_ifaces;                /**< Max. num of KNI ifaces */
121         struct rte_kni_memzone_slot *slots;        /**< Pool slots */
122         rte_spinlock_t mutex;               /**< alloc/relase mutex */
123
124         /* Free memzone slots linked-list */
125         struct rte_kni_memzone_slot *free;         /**< First empty slot */
126         struct rte_kni_memzone_slot *free_tail;    /**< Last empty slot */
127 };
128
129
130 static void kni_free_mbufs(struct rte_kni *kni);
131 static void kni_allocate_mbufs(struct rte_kni *kni);
132
133 static volatile int kni_fd = -1;
134 static struct rte_kni_memzone_pool kni_memzone_pool = {
135         .initialized = 0,
136 };
137
138 static const struct rte_memzone *
139 kni_memzone_reserve(const char *name, size_t len, int socket_id,
140                                                 unsigned flags)
141 {
142         const struct rte_memzone *mz = rte_memzone_lookup(name);
143
144         if (mz == NULL)
145                 mz = rte_memzone_reserve(name, len, socket_id, flags);
146
147         return mz;
148 }
149
150 /* Pool mgmt */
151 static struct rte_kni_memzone_slot*
152 kni_memzone_pool_alloc(void)
153 {
154         struct rte_kni_memzone_slot *slot;
155
156         rte_spinlock_lock(&kni_memzone_pool.mutex);
157
158         if (!kni_memzone_pool.free) {
159                 rte_spinlock_unlock(&kni_memzone_pool.mutex);
160                 return NULL;
161         }
162
163         slot = kni_memzone_pool.free;
164         kni_memzone_pool.free = slot->next;
165         slot->in_use = 1;
166
167         if (!kni_memzone_pool.free)
168                 kni_memzone_pool.free_tail = NULL;
169
170         rte_spinlock_unlock(&kni_memzone_pool.mutex);
171
172         return slot;
173 }
174
175 static void
176 kni_memzone_pool_release(struct rte_kni_memzone_slot *slot)
177 {
178         rte_spinlock_lock(&kni_memzone_pool.mutex);
179
180         if (kni_memzone_pool.free)
181                 kni_memzone_pool.free_tail->next = slot;
182         else
183                 kni_memzone_pool.free = slot;
184
185         kni_memzone_pool.free_tail = slot;
186         slot->next = NULL;
187         slot->in_use = 0;
188
189         rte_spinlock_unlock(&kni_memzone_pool.mutex);
190 }
191
192
193 /* Shall be called before any allocation happens */
194 void
195 rte_kni_init(unsigned int max_kni_ifaces)
196 {
197         uint32_t i;
198         struct rte_kni_memzone_slot *it;
199         const struct rte_memzone *mz;
200 #define OBJNAMSIZ 32
201         char obj_name[OBJNAMSIZ];
202         char mz_name[RTE_MEMZONE_NAMESIZE];
203
204         if (max_kni_ifaces == 0) {
205                 RTE_LOG(ERR, KNI, "Invalid number of max_kni_ifaces %d\n",
206                                                         max_kni_ifaces);
207                 rte_panic("Unable to initialize KNI\n");
208         }
209
210         /* Check FD and open */
211         if (kni_fd < 0) {
212                 kni_fd = open("/dev/" KNI_DEVICE, O_RDWR);
213                 if (kni_fd < 0)
214                         rte_panic("Can not open /dev/%s\n", KNI_DEVICE);
215         }
216
217         /* Allocate slot objects */
218         kni_memzone_pool.slots = (struct rte_kni_memzone_slot *)
219                                         rte_malloc(NULL,
220                                         sizeof(struct rte_kni_memzone_slot) *
221                                         max_kni_ifaces,
222                                         0);
223         KNI_MEM_CHECK(kni_memzone_pool.slots == NULL);
224
225         /* Initialize general pool variables */
226         kni_memzone_pool.initialized = 1;
227         kni_memzone_pool.max_ifaces = max_kni_ifaces;
228         kni_memzone_pool.free = &kni_memzone_pool.slots[0];
229         rte_spinlock_init(&kni_memzone_pool.mutex);
230
231         /* Pre-allocate all memzones of all the slots; panic on error */
232         for (i = 0; i < max_kni_ifaces; i++) {
233
234                 /* Recover current slot */
235                 it = &kni_memzone_pool.slots[i];
236                 it->id = i;
237
238                 /* Allocate KNI context */
239                 snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%d", i);
240                 mz = kni_memzone_reserve(mz_name, sizeof(struct rte_kni),
241                                         SOCKET_ID_ANY, 0);
242                 KNI_MEM_CHECK(mz == NULL);
243                 it->m_ctx = mz;
244
245                 /* TX RING */
246                 snprintf(obj_name, OBJNAMSIZ, "kni_tx_%d", i);
247                 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
248                                                         SOCKET_ID_ANY, 0);
249                 KNI_MEM_CHECK(mz == NULL);
250                 it->m_tx_q = mz;
251
252                 /* RX RING */
253                 snprintf(obj_name, OBJNAMSIZ, "kni_rx_%d", i);
254                 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
255                                                         SOCKET_ID_ANY, 0);
256                 KNI_MEM_CHECK(mz == NULL);
257                 it->m_rx_q = mz;
258
259                 /* ALLOC RING */
260                 snprintf(obj_name, OBJNAMSIZ, "kni_alloc_%d", i);
261                 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
262                                                         SOCKET_ID_ANY, 0);
263                 KNI_MEM_CHECK(mz == NULL);
264                 it->m_alloc_q = mz;
265
266                 /* FREE RING */
267                 snprintf(obj_name, OBJNAMSIZ, "kni_free_%d", i);
268                 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
269                                                         SOCKET_ID_ANY, 0);
270                 KNI_MEM_CHECK(mz == NULL);
271                 it->m_free_q = mz;
272
273                 /* Request RING */
274                 snprintf(obj_name, OBJNAMSIZ, "kni_req_%d", i);
275                 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
276                                                         SOCKET_ID_ANY, 0);
277                 KNI_MEM_CHECK(mz == NULL);
278                 it->m_req_q = mz;
279
280                 /* Response RING */
281                 snprintf(obj_name, OBJNAMSIZ, "kni_resp_%d", i);
282                 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
283                                                         SOCKET_ID_ANY, 0);
284                 KNI_MEM_CHECK(mz == NULL);
285                 it->m_resp_q = mz;
286
287                 /* Req/Resp sync mem area */
288                 snprintf(obj_name, OBJNAMSIZ, "kni_sync_%d", i);
289                 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
290                                                         SOCKET_ID_ANY, 0);
291                 KNI_MEM_CHECK(mz == NULL);
292                 it->m_sync_addr = mz;
293
294                 if ((i+1) == max_kni_ifaces) {
295                         it->next = NULL;
296                         kni_memzone_pool.free_tail = it;
297                 } else
298                         it->next = &kni_memzone_pool.slots[i+1];
299         }
300
301         return;
302
303 kni_fail:
304         rte_panic("Unable to allocate memory for max_kni_ifaces:%d. Increase the amount of hugepages memory\n",
305                          max_kni_ifaces);
306 }
307
308 /* It is deprecated and just for backward compatibility */
309 struct rte_kni *
310 rte_kni_create(uint8_t port_id,
311                unsigned mbuf_size,
312                struct rte_mempool *pktmbuf_pool,
313                struct rte_kni_ops *ops)
314 {
315         struct rte_kni_conf conf;
316         struct rte_eth_dev_info info;
317
318         memset(&info, 0, sizeof(info));
319         memset(&conf, 0, sizeof(conf));
320         rte_eth_dev_info_get(port_id, &info);
321
322         snprintf(conf.name, sizeof(conf.name), "vEth%u", port_id);
323         conf.addr = info.pci_dev->addr;
324         conf.id = info.pci_dev->id;
325         conf.group_id = (uint16_t)port_id;
326         conf.mbuf_size = mbuf_size;
327
328         /* Save the port id for request handling */
329         ops->port_id = port_id;
330
331         return rte_kni_alloc(pktmbuf_pool, &conf, ops);
332 }
333
334 struct rte_kni *
335 rte_kni_alloc(struct rte_mempool *pktmbuf_pool,
336               const struct rte_kni_conf *conf,
337               struct rte_kni_ops *ops)
338 {
339         int ret;
340         struct rte_kni_device_info dev_info;
341         struct rte_kni *ctx;
342         char intf_name[RTE_KNI_NAMESIZE];
343         char mz_name[RTE_MEMZONE_NAMESIZE];
344         const struct rte_memzone *mz;
345         struct rte_kni_memzone_slot *slot = NULL;
346
347         if (!pktmbuf_pool || !conf || !conf->name[0])
348                 return NULL;
349
350         /* Check if KNI subsystem has been initialized */
351         if (kni_memzone_pool.initialized != 1) {
352                 RTE_LOG(ERR, KNI, "KNI subsystem has not been initialized. Invoke rte_kni_init() first\n");
353                 return NULL;
354         }
355
356         /* Get an available slot from the pool */
357         slot = kni_memzone_pool_alloc();
358         if (!slot) {
359                 RTE_LOG(ERR, KNI, "Cannot allocate more KNI interfaces; increase the number of max_kni_ifaces(current %d) or release unusued ones.\n",
360                         kni_memzone_pool.max_ifaces);
361                 return NULL;
362         }
363
364         /* Recover ctx */
365         ctx = slot->m_ctx->addr;
366         snprintf(intf_name, RTE_KNI_NAMESIZE, "%s", conf->name);
367
368         if (ctx->in_use) {
369                 RTE_LOG(ERR, KNI, "KNI %s is in use\n", ctx->name);
370                 return NULL;
371         }
372         memset(ctx, 0, sizeof(struct rte_kni));
373         if (ops)
374                 memcpy(&ctx->ops, ops, sizeof(struct rte_kni_ops));
375
376         memset(&dev_info, 0, sizeof(dev_info));
377         dev_info.bus = conf->addr.bus;
378         dev_info.devid = conf->addr.devid;
379         dev_info.function = conf->addr.function;
380         dev_info.vendor_id = conf->id.vendor_id;
381         dev_info.device_id = conf->id.device_id;
382         dev_info.core_id = conf->core_id;
383         dev_info.force_bind = conf->force_bind;
384         dev_info.group_id = conf->group_id;
385         dev_info.mbuf_size = conf->mbuf_size;
386
387         snprintf(ctx->name, RTE_KNI_NAMESIZE, "%s", intf_name);
388         snprintf(dev_info.name, RTE_KNI_NAMESIZE, "%s", intf_name);
389
390         RTE_LOG(INFO, KNI, "pci: %02x:%02x:%02x \t %02x:%02x\n",
391                 dev_info.bus, dev_info.devid, dev_info.function,
392                         dev_info.vendor_id, dev_info.device_id);
393         /* TX RING */
394         mz = slot->m_tx_q;
395         ctx->tx_q = mz->addr;
396         kni_fifo_init(ctx->tx_q, KNI_FIFO_COUNT_MAX);
397         dev_info.tx_phys = mz->phys_addr;
398
399         /* RX RING */
400         mz = slot->m_rx_q;
401         ctx->rx_q = mz->addr;
402         kni_fifo_init(ctx->rx_q, KNI_FIFO_COUNT_MAX);
403         dev_info.rx_phys = mz->phys_addr;
404
405         /* ALLOC RING */
406         mz = slot->m_alloc_q;
407         ctx->alloc_q = mz->addr;
408         kni_fifo_init(ctx->alloc_q, KNI_FIFO_COUNT_MAX);
409         dev_info.alloc_phys = mz->phys_addr;
410
411         /* FREE RING */
412         mz = slot->m_free_q;
413         ctx->free_q = mz->addr;
414         kni_fifo_init(ctx->free_q, KNI_FIFO_COUNT_MAX);
415         dev_info.free_phys = mz->phys_addr;
416
417         /* Request RING */
418         mz = slot->m_req_q;
419         ctx->req_q = mz->addr;
420         kni_fifo_init(ctx->req_q, KNI_FIFO_COUNT_MAX);
421         dev_info.req_phys = mz->phys_addr;
422
423         /* Response RING */
424         mz = slot->m_resp_q;
425         ctx->resp_q = mz->addr;
426         kni_fifo_init(ctx->resp_q, KNI_FIFO_COUNT_MAX);
427         dev_info.resp_phys = mz->phys_addr;
428
429         /* Req/Resp sync mem area */
430         mz = slot->m_sync_addr;
431         ctx->sync_addr = mz->addr;
432         dev_info.sync_va = mz->addr;
433         dev_info.sync_phys = mz->phys_addr;
434
435
436         /* MBUF mempool */
437         snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_OBJ_NAME,
438                 pktmbuf_pool->name);
439         mz = rte_memzone_lookup(mz_name);
440         KNI_MEM_CHECK(mz == NULL);
441         dev_info.mbuf_va = mz->addr;
442         dev_info.mbuf_phys = mz->phys_addr;
443         ctx->pktmbuf_pool = pktmbuf_pool;
444         ctx->group_id = conf->group_id;
445         ctx->slot_id = slot->id;
446         ctx->mbuf_size = conf->mbuf_size;
447
448         ret = ioctl(kni_fd, RTE_KNI_IOCTL_CREATE, &dev_info);
449         KNI_MEM_CHECK(ret < 0);
450
451         ctx->in_use = 1;
452
453         /* Allocate mbufs and then put them into alloc_q */
454         kni_allocate_mbufs(ctx);
455
456         return ctx;
457
458 kni_fail:
459         if (slot)
460                 kni_memzone_pool_release(&kni_memzone_pool.slots[slot->id]);
461
462         return NULL;
463 }
464
465 static void
466 kni_free_fifo(struct rte_kni_fifo *fifo)
467 {
468         int ret;
469         struct rte_mbuf *pkt;
470
471         do {
472                 ret = kni_fifo_get(fifo, (void **)&pkt, 1);
473                 if (ret)
474                         rte_pktmbuf_free(pkt);
475         } while (ret);
476 }
477
478 int
479 rte_kni_release(struct rte_kni *kni)
480 {
481         struct rte_kni_device_info dev_info;
482         uint32_t slot_id;
483
484         if (!kni || !kni->in_use)
485                 return -1;
486
487         snprintf(dev_info.name, sizeof(dev_info.name), "%s", kni->name);
488         if (ioctl(kni_fd, RTE_KNI_IOCTL_RELEASE, &dev_info) < 0) {
489                 RTE_LOG(ERR, KNI, "Fail to release kni device\n");
490                 return -1;
491         }
492
493         /* mbufs in all fifo should be released, except request/response */
494         kni_free_fifo(kni->tx_q);
495         kni_free_fifo(kni->rx_q);
496         kni_free_fifo(kni->alloc_q);
497         kni_free_fifo(kni->free_q);
498
499         slot_id = kni->slot_id;
500
501         /* Memset the KNI struct */
502         memset(kni, 0, sizeof(struct rte_kni));
503
504         /* Release memzone */
505         if (slot_id > kni_memzone_pool.max_ifaces) {
506                 rte_panic("KNI pool: corrupted slot ID: %d, max: %d\n",
507                         slot_id, kni_memzone_pool.max_ifaces);
508         }
509         kni_memzone_pool_release(&kni_memzone_pool.slots[slot_id]);
510
511         return 0;
512 }
513
514 int
515 rte_kni_handle_request(struct rte_kni *kni)
516 {
517         unsigned ret;
518         struct rte_kni_request *req;
519
520         if (kni == NULL)
521                 return -1;
522
523         /* Get request mbuf */
524         ret = kni_fifo_get(kni->req_q, (void **)&req, 1);
525         if (ret != 1)
526                 return 0; /* It is OK of can not getting the request mbuf */
527
528         if (req != kni->sync_addr) {
529                 rte_panic("Wrong req pointer %p\n", req);
530         }
531
532         /* Analyze the request and call the relevant actions for it */
533         switch (req->req_id) {
534         case RTE_KNI_REQ_CHANGE_MTU: /* Change MTU */
535                 if (kni->ops.change_mtu)
536                         req->result = kni->ops.change_mtu(kni->ops.port_id,
537                                                         req->new_mtu);
538                 break;
539         case RTE_KNI_REQ_CFG_NETWORK_IF: /* Set network interface up/down */
540                 if (kni->ops.config_network_if)
541                         req->result = kni->ops.config_network_if(\
542                                         kni->ops.port_id, req->if_up);
543                 break;
544         default:
545                 RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id);
546                 req->result = -EINVAL;
547                 break;
548         }
549
550         /* Construct response mbuf and put it back to resp_q */
551         ret = kni_fifo_put(kni->resp_q, (void **)&req, 1);
552         if (ret != 1) {
553                 RTE_LOG(ERR, KNI, "Fail to put the muf back to resp_q\n");
554                 return -1; /* It is an error of can't putting the mbuf back */
555         }
556
557         return 0;
558 }
559
560 unsigned
561 rte_kni_tx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
562 {
563         unsigned ret = kni_fifo_put(kni->rx_q, (void **)mbufs, num);
564
565         /* Get mbufs from free_q and then free them */
566         kni_free_mbufs(kni);
567
568         return ret;
569 }
570
571 unsigned
572 rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
573 {
574         unsigned ret = kni_fifo_get(kni->tx_q, (void **)mbufs, num);
575
576         /* If buffers removed, allocate mbufs and then put them into alloc_q */
577         if (ret)
578                 kni_allocate_mbufs(kni);
579
580         return ret;
581 }
582
583 static void
584 kni_free_mbufs(struct rte_kni *kni)
585 {
586         int i, ret;
587         struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
588
589         ret = kni_fifo_get(kni->free_q, (void **)pkts, MAX_MBUF_BURST_NUM);
590         if (likely(ret > 0)) {
591                 for (i = 0; i < ret; i++)
592                         rte_pktmbuf_free(pkts[i]);
593         }
594 }
595
596 static void
597 kni_allocate_mbufs(struct rte_kni *kni)
598 {
599         int i, ret;
600         struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
601
602         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pool) !=
603                          offsetof(struct rte_kni_mbuf, pool));
604         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, buf_addr) !=
605                          offsetof(struct rte_kni_mbuf, buf_addr));
606         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, next) !=
607                          offsetof(struct rte_kni_mbuf, next));
608         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_off) !=
609                          offsetof(struct rte_kni_mbuf, data_off));
610         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_len) !=
611                          offsetof(struct rte_kni_mbuf, data_len));
612         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pkt_len) !=
613                          offsetof(struct rte_kni_mbuf, pkt_len));
614         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, ol_flags) !=
615                          offsetof(struct rte_kni_mbuf, ol_flags));
616
617         /* Check if pktmbuf pool has been configured */
618         if (kni->pktmbuf_pool == NULL) {
619                 RTE_LOG(ERR, KNI, "No valid mempool for allocating mbufs\n");
620                 return;
621         }
622
623         for (i = 0; i < MAX_MBUF_BURST_NUM; i++) {
624                 pkts[i] = rte_pktmbuf_alloc(kni->pktmbuf_pool);
625                 if (unlikely(pkts[i] == NULL)) {
626                         /* Out of memory */
627                         RTE_LOG(ERR, KNI, "Out of memory\n");
628                         break;
629                 }
630         }
631
632         /* No pkt mbuf alocated */
633         if (i <= 0)
634                 return;
635
636         ret = kni_fifo_put(kni->alloc_q, (void **)pkts, i);
637
638         /* Check if any mbufs not put into alloc_q, and then free them */
639         if (ret >= 0 && ret < i && ret < MAX_MBUF_BURST_NUM) {
640                 int j;
641
642                 for (j = ret; j < i; j++)
643                         rte_pktmbuf_free(pkts[j]);
644         }
645 }
646
647 /* It is deprecated and just for backward compatibility */
648 uint8_t
649 rte_kni_get_port_id(struct rte_kni *kni)
650 {
651         if (!kni)
652                 return ~0x0;
653
654         return kni->ops.port_id;
655 }
656
657 struct rte_kni *
658 rte_kni_get(const char *name)
659 {
660         uint32_t i;
661         struct rte_kni_memzone_slot *it;
662         struct rte_kni *kni;
663
664         /* Note: could be improved perf-wise if necessary */
665         for (i = 0; i < kni_memzone_pool.max_ifaces; i++) {
666                 it = &kni_memzone_pool.slots[i];
667                 if (it->in_use == 0)
668                         continue;
669                 kni = it->m_ctx->addr;
670                 if (strncmp(kni->name, name, RTE_KNI_NAMESIZE) == 0)
671                         return kni;
672         }
673
674         return NULL;
675 }
676
677 /*
678  * It is deprecated and just for backward compatibility.
679  */
680 struct rte_kni *
681 rte_kni_info_get(uint8_t port_id)
682 {
683         char name[RTE_MEMZONE_NAMESIZE];
684
685         if (port_id >= RTE_MAX_ETHPORTS)
686                 return NULL;
687
688         snprintf(name, RTE_MEMZONE_NAMESIZE, "vEth%u", port_id);
689
690         return rte_kni_get(name);
691 }
692
693 static enum kni_ops_status
694 kni_check_request_register(struct rte_kni_ops *ops)
695 {
696         /* check if KNI request ops has been registered*/
697         if( NULL == ops )
698                 return KNI_REQ_NO_REGISTER;
699
700         if((NULL == ops->change_mtu) && (NULL == ops->config_network_if))
701                 return KNI_REQ_NO_REGISTER;
702
703         return KNI_REQ_REGISTERED;
704 }
705
706 int
707 rte_kni_register_handlers(struct rte_kni *kni,struct rte_kni_ops *ops)
708 {
709         enum kni_ops_status req_status;
710
711         if (NULL == ops) {
712                 RTE_LOG(ERR, KNI, "Invalid KNI request operation.\n");
713                 return -1;
714         }
715
716         if (NULL == kni) {
717                 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
718                 return -1;
719         }
720
721         req_status = kni_check_request_register(&kni->ops);
722         if ( KNI_REQ_REGISTERED == req_status) {
723                 RTE_LOG(ERR, KNI, "The KNI request operation has already registered.\n");
724                 return -1;
725         }
726
727         memcpy(&kni->ops, ops, sizeof(struct rte_kni_ops));
728         return 0;
729 }
730
731 int
732 rte_kni_unregister_handlers(struct rte_kni *kni)
733 {
734         if (NULL == kni) {
735                 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
736                 return -1;
737         }
738
739         kni->ops.change_mtu = NULL;
740         kni->ops.config_network_if = NULL;
741         return 0;
742 }
743 void
744 rte_kni_close(void)
745 {
746         if (kni_fd < 0)
747                 return;
748
749         close(kni_fd);
750         kni_fd = -1;
751 }