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