4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifndef RTE_EXEC_ENV_LINUXAPP
35 #error "KNI is not supported"
41 #include <sys/ioctl.h>
43 #include <rte_spinlock.h>
44 #include <rte_string_fns.h>
45 #include <rte_ethdev.h>
46 #include <rte_malloc.h>
49 #include <rte_memzone.h>
50 #include <exec-env/rte_kni_common.h>
51 #include "rte_kni_fifo.h"
53 #define MAX_MBUF_BURST_NUM 32
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))
60 #define KNI_REQUEST_MBUF_NUM_MAX 32
62 #define KNI_MEM_CHECK(cond) do { if (cond) goto kni_fail; } while (0)
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 */
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 */
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 */
84 struct rte_kni_ops ops; /**< operations for request */
85 uint8_t in_use : 1; /**< kni in use */
89 KNI_REQ_NO_REGISTER = 0,
94 * KNI memzone pool slot
96 struct rte_kni_memzone_slot {
98 uint8_t in_use : 1; /**< slot in use */
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;
110 /* Free linked list */
111 struct rte_kni_memzone_slot *next; /**< Next slot link.list */
117 struct rte_kni_memzone_pool {
118 uint8_t initialized : 1; /**< Global KNI pool init flag */
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 */
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 */
130 static void kni_free_mbufs(struct rte_kni *kni);
131 static void kni_allocate_mbufs(struct rte_kni *kni);
133 static volatile int kni_fd = -1;
134 static struct rte_kni_memzone_pool kni_memzone_pool = {
138 static const struct rte_memzone *
139 kni_memzone_reserve(const char *name, size_t len, int socket_id,
142 const struct rte_memzone *mz = rte_memzone_lookup(name);
145 mz = rte_memzone_reserve(name, len, socket_id, flags);
151 static struct rte_kni_memzone_slot*
152 kni_memzone_pool_alloc(void)
154 struct rte_kni_memzone_slot *slot;
156 rte_spinlock_lock(&kni_memzone_pool.mutex);
158 if (!kni_memzone_pool.free) {
159 rte_spinlock_unlock(&kni_memzone_pool.mutex);
163 slot = kni_memzone_pool.free;
164 kni_memzone_pool.free = slot->next;
167 if (!kni_memzone_pool.free)
168 kni_memzone_pool.free_tail = NULL;
170 rte_spinlock_unlock(&kni_memzone_pool.mutex);
176 kni_memzone_pool_release(struct rte_kni_memzone_slot *slot)
178 rte_spinlock_lock(&kni_memzone_pool.mutex);
180 if (kni_memzone_pool.free)
181 kni_memzone_pool.free_tail->next = slot;
183 kni_memzone_pool.free = slot;
185 kni_memzone_pool.free_tail = slot;
189 rte_spinlock_unlock(&kni_memzone_pool.mutex);
193 /* Shall be called before any allocation happens */
195 rte_kni_init(unsigned int max_kni_ifaces)
198 struct rte_kni_memzone_slot *it;
199 const struct rte_memzone *mz;
201 char obj_name[OBJNAMSIZ];
202 char mz_name[RTE_MEMZONE_NAMESIZE];
204 /* Immediately return if KNI is already initialized */
205 if (kni_memzone_pool.initialized) {
206 RTE_LOG(WARNING, KNI, "Double call to rte_kni_init()");
210 if (max_kni_ifaces == 0) {
211 RTE_LOG(ERR, KNI, "Invalid number of max_kni_ifaces %d\n",
213 rte_panic("Unable to initialize KNI\n");
216 /* Check FD and open */
218 kni_fd = open("/dev/" KNI_DEVICE, O_RDWR);
220 rte_panic("Can not open /dev/%s\n", KNI_DEVICE);
223 /* Allocate slot objects */
224 kni_memzone_pool.slots = (struct rte_kni_memzone_slot *)
226 sizeof(struct rte_kni_memzone_slot) *
229 KNI_MEM_CHECK(kni_memzone_pool.slots == NULL);
231 /* Initialize general pool variables */
232 kni_memzone_pool.initialized = 1;
233 kni_memzone_pool.max_ifaces = max_kni_ifaces;
234 kni_memzone_pool.free = &kni_memzone_pool.slots[0];
235 rte_spinlock_init(&kni_memzone_pool.mutex);
237 /* Pre-allocate all memzones of all the slots; panic on error */
238 for (i = 0; i < max_kni_ifaces; i++) {
240 /* Recover current slot */
241 it = &kni_memzone_pool.slots[i];
244 /* Allocate KNI context */
245 snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%d", i);
246 mz = kni_memzone_reserve(mz_name, sizeof(struct rte_kni),
248 KNI_MEM_CHECK(mz == NULL);
252 snprintf(obj_name, OBJNAMSIZ, "kni_tx_%d", i);
253 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
255 KNI_MEM_CHECK(mz == NULL);
259 snprintf(obj_name, OBJNAMSIZ, "kni_rx_%d", i);
260 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
262 KNI_MEM_CHECK(mz == NULL);
266 snprintf(obj_name, OBJNAMSIZ, "kni_alloc_%d", i);
267 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
269 KNI_MEM_CHECK(mz == NULL);
273 snprintf(obj_name, OBJNAMSIZ, "kni_free_%d", i);
274 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
276 KNI_MEM_CHECK(mz == NULL);
280 snprintf(obj_name, OBJNAMSIZ, "kni_req_%d", i);
281 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
283 KNI_MEM_CHECK(mz == NULL);
287 snprintf(obj_name, OBJNAMSIZ, "kni_resp_%d", i);
288 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
290 KNI_MEM_CHECK(mz == NULL);
293 /* Req/Resp sync mem area */
294 snprintf(obj_name, OBJNAMSIZ, "kni_sync_%d", i);
295 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
297 KNI_MEM_CHECK(mz == NULL);
298 it->m_sync_addr = mz;
300 if ((i+1) == max_kni_ifaces) {
302 kni_memzone_pool.free_tail = it;
304 it->next = &kni_memzone_pool.slots[i+1];
310 rte_panic("Unable to allocate memory for max_kni_ifaces:%d. Increase the amount of hugepages memory\n",
316 rte_kni_alloc(struct rte_mempool *pktmbuf_pool,
317 const struct rte_kni_conf *conf,
318 struct rte_kni_ops *ops)
321 struct rte_kni_device_info dev_info;
323 char intf_name[RTE_KNI_NAMESIZE];
324 const struct rte_memzone *mz;
325 struct rte_kni_memzone_slot *slot = NULL;
327 if (!pktmbuf_pool || !conf || !conf->name[0])
330 /* Check if KNI subsystem has been initialized */
331 if (kni_memzone_pool.initialized != 1) {
332 RTE_LOG(ERR, KNI, "KNI subsystem has not been initialized. Invoke rte_kni_init() first\n");
336 /* Get an available slot from the pool */
337 slot = kni_memzone_pool_alloc();
339 RTE_LOG(ERR, KNI, "Cannot allocate more KNI interfaces; increase the number of max_kni_ifaces(current %d) or release unusued ones.\n",
340 kni_memzone_pool.max_ifaces);
345 ctx = slot->m_ctx->addr;
346 snprintf(intf_name, RTE_KNI_NAMESIZE, "%s", conf->name);
349 RTE_LOG(ERR, KNI, "KNI %s is in use\n", ctx->name);
352 memset(ctx, 0, sizeof(struct rte_kni));
354 memcpy(&ctx->ops, ops, sizeof(struct rte_kni_ops));
356 memset(&dev_info, 0, sizeof(dev_info));
357 dev_info.bus = conf->addr.bus;
358 dev_info.devid = conf->addr.devid;
359 dev_info.function = conf->addr.function;
360 dev_info.vendor_id = conf->id.vendor_id;
361 dev_info.device_id = conf->id.device_id;
362 dev_info.core_id = conf->core_id;
363 dev_info.force_bind = conf->force_bind;
364 dev_info.group_id = conf->group_id;
365 dev_info.mbuf_size = conf->mbuf_size;
367 snprintf(ctx->name, RTE_KNI_NAMESIZE, "%s", intf_name);
368 snprintf(dev_info.name, RTE_KNI_NAMESIZE, "%s", intf_name);
370 RTE_LOG(INFO, KNI, "pci: %02x:%02x:%02x \t %02x:%02x\n",
371 dev_info.bus, dev_info.devid, dev_info.function,
372 dev_info.vendor_id, dev_info.device_id);
375 ctx->tx_q = mz->addr;
376 kni_fifo_init(ctx->tx_q, KNI_FIFO_COUNT_MAX);
377 dev_info.tx_phys = mz->phys_addr;
381 ctx->rx_q = mz->addr;
382 kni_fifo_init(ctx->rx_q, KNI_FIFO_COUNT_MAX);
383 dev_info.rx_phys = mz->phys_addr;
386 mz = slot->m_alloc_q;
387 ctx->alloc_q = mz->addr;
388 kni_fifo_init(ctx->alloc_q, KNI_FIFO_COUNT_MAX);
389 dev_info.alloc_phys = mz->phys_addr;
393 ctx->free_q = mz->addr;
394 kni_fifo_init(ctx->free_q, KNI_FIFO_COUNT_MAX);
395 dev_info.free_phys = mz->phys_addr;
399 ctx->req_q = mz->addr;
400 kni_fifo_init(ctx->req_q, KNI_FIFO_COUNT_MAX);
401 dev_info.req_phys = mz->phys_addr;
405 ctx->resp_q = mz->addr;
406 kni_fifo_init(ctx->resp_q, KNI_FIFO_COUNT_MAX);
407 dev_info.resp_phys = mz->phys_addr;
409 /* Req/Resp sync mem area */
410 mz = slot->m_sync_addr;
411 ctx->sync_addr = mz->addr;
412 dev_info.sync_va = mz->addr;
413 dev_info.sync_phys = mz->phys_addr;
417 /* KNI currently requires to have only one memory chunk */
418 if (pktmbuf_pool->nb_mem_chunks != 1)
421 dev_info.mbuf_va = STAILQ_FIRST(&pktmbuf_pool->mem_list)->addr;
422 dev_info.mbuf_phys = STAILQ_FIRST(&pktmbuf_pool->mem_list)->phys_addr;
423 ctx->pktmbuf_pool = pktmbuf_pool;
424 ctx->group_id = conf->group_id;
425 ctx->slot_id = slot->id;
426 ctx->mbuf_size = conf->mbuf_size;
428 ret = ioctl(kni_fd, RTE_KNI_IOCTL_CREATE, &dev_info);
429 KNI_MEM_CHECK(ret < 0);
433 /* Allocate mbufs and then put them into alloc_q */
434 kni_allocate_mbufs(ctx);
440 kni_memzone_pool_release(&kni_memzone_pool.slots[slot->id]);
446 kni_free_fifo(struct rte_kni_fifo *fifo)
449 struct rte_mbuf *pkt;
452 ret = kni_fifo_get(fifo, (void **)&pkt, 1);
454 rte_pktmbuf_free(pkt);
459 rte_kni_release(struct rte_kni *kni)
461 struct rte_kni_device_info dev_info;
464 if (!kni || !kni->in_use)
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");
473 /* mbufs in all fifo should be released, except request/response */
474 kni_free_fifo(kni->tx_q);
475 kni_free_fifo(kni->rx_q);
476 kni_free_fifo(kni->alloc_q);
477 kni_free_fifo(kni->free_q);
479 slot_id = kni->slot_id;
481 /* Memset the KNI struct */
482 memset(kni, 0, sizeof(struct rte_kni));
484 /* Release memzone */
485 if (slot_id > kni_memzone_pool.max_ifaces) {
486 rte_panic("KNI pool: corrupted slot ID: %d, max: %d\n",
487 slot_id, kni_memzone_pool.max_ifaces);
489 kni_memzone_pool_release(&kni_memzone_pool.slots[slot_id]);
495 rte_kni_handle_request(struct rte_kni *kni)
498 struct rte_kni_request *req;
503 /* Get request mbuf */
504 ret = kni_fifo_get(kni->req_q, (void **)&req, 1);
506 return 0; /* It is OK of can not getting the request mbuf */
508 if (req != kni->sync_addr) {
509 rte_panic("Wrong req pointer %p\n", req);
512 /* Analyze the request and call the relevant actions for it */
513 switch (req->req_id) {
514 case RTE_KNI_REQ_CHANGE_MTU: /* Change MTU */
515 if (kni->ops.change_mtu)
516 req->result = kni->ops.change_mtu(kni->ops.port_id,
519 case RTE_KNI_REQ_CFG_NETWORK_IF: /* Set network interface up/down */
520 if (kni->ops.config_network_if)
521 req->result = kni->ops.config_network_if(\
522 kni->ops.port_id, req->if_up);
525 RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id);
526 req->result = -EINVAL;
530 /* Construct response mbuf and put it back to resp_q */
531 ret = kni_fifo_put(kni->resp_q, (void **)&req, 1);
533 RTE_LOG(ERR, KNI, "Fail to put the muf back to resp_q\n");
534 return -1; /* It is an error of can't putting the mbuf back */
541 rte_kni_tx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
543 unsigned ret = kni_fifo_put(kni->rx_q, (void **)mbufs, num);
545 /* Get mbufs from free_q and then free them */
552 rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
554 unsigned ret = kni_fifo_get(kni->tx_q, (void **)mbufs, num);
556 /* If buffers removed, allocate mbufs and then put them into alloc_q */
558 kni_allocate_mbufs(kni);
564 kni_free_mbufs(struct rte_kni *kni)
567 struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
569 ret = kni_fifo_get(kni->free_q, (void **)pkts, MAX_MBUF_BURST_NUM);
570 if (likely(ret > 0)) {
571 for (i = 0; i < ret; i++)
572 rte_pktmbuf_free(pkts[i]);
577 kni_allocate_mbufs(struct rte_kni *kni)
580 struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
582 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pool) !=
583 offsetof(struct rte_kni_mbuf, pool));
584 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, buf_addr) !=
585 offsetof(struct rte_kni_mbuf, buf_addr));
586 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, next) !=
587 offsetof(struct rte_kni_mbuf, next));
588 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_off) !=
589 offsetof(struct rte_kni_mbuf, data_off));
590 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_len) !=
591 offsetof(struct rte_kni_mbuf, data_len));
592 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pkt_len) !=
593 offsetof(struct rte_kni_mbuf, pkt_len));
594 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, ol_flags) !=
595 offsetof(struct rte_kni_mbuf, ol_flags));
597 /* Check if pktmbuf pool has been configured */
598 if (kni->pktmbuf_pool == NULL) {
599 RTE_LOG(ERR, KNI, "No valid mempool for allocating mbufs\n");
603 for (i = 0; i < MAX_MBUF_BURST_NUM; i++) {
604 pkts[i] = rte_pktmbuf_alloc(kni->pktmbuf_pool);
605 if (unlikely(pkts[i] == NULL)) {
607 RTE_LOG(ERR, KNI, "Out of memory\n");
612 /* No pkt mbuf alocated */
616 ret = kni_fifo_put(kni->alloc_q, (void **)pkts, i);
618 /* Check if any mbufs not put into alloc_q, and then free them */
619 if (ret >= 0 && ret < i && ret < MAX_MBUF_BURST_NUM) {
622 for (j = ret; j < i; j++)
623 rte_pktmbuf_free(pkts[j]);
628 rte_kni_get(const char *name)
631 struct rte_kni_memzone_slot *it;
634 /* Note: could be improved perf-wise if necessary */
635 for (i = 0; i < kni_memzone_pool.max_ifaces; i++) {
636 it = &kni_memzone_pool.slots[i];
639 kni = it->m_ctx->addr;
640 if (strncmp(kni->name, name, RTE_KNI_NAMESIZE) == 0)
648 rte_kni_get_name(const struct rte_kni *kni)
653 static enum kni_ops_status
654 kni_check_request_register(struct rte_kni_ops *ops)
656 /* check if KNI request ops has been registered*/
658 return KNI_REQ_NO_REGISTER;
660 if((NULL == ops->change_mtu) && (NULL == ops->config_network_if))
661 return KNI_REQ_NO_REGISTER;
663 return KNI_REQ_REGISTERED;
667 rte_kni_register_handlers(struct rte_kni *kni,struct rte_kni_ops *ops)
669 enum kni_ops_status req_status;
672 RTE_LOG(ERR, KNI, "Invalid KNI request operation.\n");
677 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
681 req_status = kni_check_request_register(&kni->ops);
682 if ( KNI_REQ_REGISTERED == req_status) {
683 RTE_LOG(ERR, KNI, "The KNI request operation has already registered.\n");
687 memcpy(&kni->ops, ops, sizeof(struct rte_kni_ops));
692 rte_kni_unregister_handlers(struct rte_kni *kni)
695 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
699 kni->ops.change_mtu = NULL;
700 kni->ops.config_network_if = NULL;