- rte_eth_dev_info_get(port_id, ð_dev_info);
- RTE_LOG(INFO, KNI, "pci: %02x:%02x:%02x \t %02x:%02x\n",
- eth_dev_info.pci_dev->addr.bus,
- eth_dev_info.pci_dev->addr.devid,
- eth_dev_info.pci_dev->addr.function,
- eth_dev_info.pci_dev->id.vendor_id,
- eth_dev_info.pci_dev->id.device_id);
- dev_info.bus = eth_dev_info.pci_dev->addr.bus;
- dev_info.devid = eth_dev_info.pci_dev->addr.devid;
- dev_info.function = eth_dev_info.pci_dev->addr.function;
- dev_info.vendor_id = eth_dev_info.pci_dev->id.vendor_id;
- dev_info.device_id = eth_dev_info.pci_dev->id.device_id;
- dev_info.port_id = port_id;
-
- rte_snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%d", port_id);
- mz = kni_memzone_reserve(mz_name, sizeof(struct rte_kni),
- SOCKET_ID_ANY, 0);
- KNI_MZ_CHECK(mz == NULL);
- ctx = mz->addr;
-
- if (ctx->port_in_use != 0) {
- RTE_LOG(ERR, KNI, "Port %d has been used\n", port_id);
- goto fail;
+ /* Allocate slot objects */
+ kni_memzone_pool.slots = (struct rte_kni_memzone_slot *)
+ rte_malloc(NULL,
+ sizeof(struct rte_kni_memzone_slot) *
+ max_kni_ifaces,
+ 0);
+ KNI_MEM_CHECK(kni_memzone_pool.slots == NULL);
+
+ /* Initialize general pool variables */
+ kni_memzone_pool.initialized = 1;
+ kni_memzone_pool.max_ifaces = max_kni_ifaces;
+ kni_memzone_pool.free = &kni_memzone_pool.slots[0];
+ rte_spinlock_init(&kni_memzone_pool.mutex);
+
+ /* Pre-allocate all memzones of all the slots; panic on error */
+ for (i = 0; i < max_kni_ifaces; i++) {
+
+ /* Recover current slot */
+ it = &kni_memzone_pool.slots[i];
+ it->id = i;
+
+ /* Allocate KNI context */
+ snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%d", i);
+ mz = kni_memzone_reserve(mz_name, sizeof(struct rte_kni),
+ SOCKET_ID_ANY, 0);
+ KNI_MEM_CHECK(mz == NULL);
+ it->m_ctx = mz;
+
+ /* TX RING */
+ snprintf(obj_name, OBJNAMSIZ, "kni_tx_%d", i);
+ mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
+ SOCKET_ID_ANY, 0);
+ KNI_MEM_CHECK(mz == NULL);
+ it->m_tx_q = mz;
+
+ /* RX RING */
+ snprintf(obj_name, OBJNAMSIZ, "kni_rx_%d", i);
+ mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
+ SOCKET_ID_ANY, 0);
+ KNI_MEM_CHECK(mz == NULL);
+ it->m_rx_q = mz;
+
+ /* ALLOC RING */
+ snprintf(obj_name, OBJNAMSIZ, "kni_alloc_%d", i);
+ mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
+ SOCKET_ID_ANY, 0);
+ KNI_MEM_CHECK(mz == NULL);
+ it->m_alloc_q = mz;
+
+ /* FREE RING */
+ snprintf(obj_name, OBJNAMSIZ, "kni_free_%d", i);
+ mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
+ SOCKET_ID_ANY, 0);
+ KNI_MEM_CHECK(mz == NULL);
+ it->m_free_q = mz;
+
+ /* Request RING */
+ snprintf(obj_name, OBJNAMSIZ, "kni_req_%d", i);
+ mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
+ SOCKET_ID_ANY, 0);
+ KNI_MEM_CHECK(mz == NULL);
+ it->m_req_q = mz;
+
+ /* Response RING */
+ snprintf(obj_name, OBJNAMSIZ, "kni_resp_%d", i);
+ mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
+ SOCKET_ID_ANY, 0);
+ KNI_MEM_CHECK(mz == NULL);
+ it->m_resp_q = mz;
+
+ /* Req/Resp sync mem area */
+ snprintf(obj_name, OBJNAMSIZ, "kni_sync_%d", i);
+ mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE,
+ SOCKET_ID_ANY, 0);
+ KNI_MEM_CHECK(mz == NULL);
+ it->m_sync_addr = mz;
+
+ if ((i+1) == max_kni_ifaces) {
+ it->next = NULL;
+ kni_memzone_pool.free_tail = it;
+ } else
+ it->next = &kni_memzone_pool.slots[i+1];
+ }
+
+ return;
+
+kni_fail:
+ RTE_LOG(ERR, KNI, "Unable to allocate memory for max_kni_ifaces:%d."
+ "Increase the amount of hugepages memory\n", max_kni_ifaces);
+}
+
+
+struct rte_kni *
+rte_kni_alloc(struct rte_mempool *pktmbuf_pool,
+ const struct rte_kni_conf *conf,
+ struct rte_kni_ops *ops)
+{
+ int ret;
+ struct rte_kni_device_info dev_info;
+ struct rte_kni *ctx;
+ char intf_name[RTE_KNI_NAMESIZE];
+ const struct rte_memzone *mz;
+ struct rte_kni_memzone_slot *slot = NULL;
+
+ if (!pktmbuf_pool || !conf || !conf->name[0])
+ return NULL;
+
+ /* Check if KNI subsystem has been initialized */
+ if (kni_memzone_pool.initialized != 1) {
+ RTE_LOG(ERR, KNI, "KNI subsystem has not been initialized. Invoke rte_kni_init() first\n");
+ return NULL;
+ }
+
+ /* Get an available slot from the pool */
+ slot = kni_memzone_pool_alloc();
+ if (!slot) {
+ RTE_LOG(ERR, KNI, "Cannot allocate more KNI interfaces; increase the number of max_kni_ifaces(current %d) or release unusued ones.\n",
+ kni_memzone_pool.max_ifaces);
+ return NULL;
+ }
+
+ /* Recover ctx */
+ ctx = slot->m_ctx->addr;
+ snprintf(intf_name, RTE_KNI_NAMESIZE, "%s", conf->name);
+
+ if (ctx->in_use) {
+ RTE_LOG(ERR, KNI, "KNI %s is in use\n", ctx->name);
+ return NULL;