1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #ifndef RTE_EXEC_ENV_LINUXAPP
6 #error "KNI is not supported"
12 #include <sys/ioctl.h>
14 #include <rte_spinlock.h>
15 #include <rte_string_fns.h>
16 #include <rte_ethdev.h>
17 #include <rte_malloc.h>
20 #include <rte_memzone.h>
21 #include <rte_tailq.h>
22 #include <rte_rwlock.h>
23 #include <rte_eal_memconfig.h>
24 #include <exec-env/rte_kni_common.h>
25 #include "rte_kni_fifo.h"
27 #define MAX_MBUF_BURST_NUM 32
29 /* Maximum number of ring entries */
30 #define KNI_FIFO_COUNT_MAX 1024
31 #define KNI_FIFO_SIZE (KNI_FIFO_COUNT_MAX * sizeof(void *) + \
32 sizeof(struct rte_kni_fifo))
34 #define KNI_REQUEST_MBUF_NUM_MAX 32
36 #define KNI_MEM_CHECK(cond, fail) do { if (cond) goto fail; } while (0)
38 #define KNI_MZ_NAME_FMT "kni_info_%s"
39 #define KNI_TX_Q_MZ_NAME_FMT "kni_tx_%s"
40 #define KNI_RX_Q_MZ_NAME_FMT "kni_rx_%s"
41 #define KNI_ALLOC_Q_MZ_NAME_FMT "kni_alloc_%s"
42 #define KNI_FREE_Q_MZ_NAME_FMT "kni_free_%s"
43 #define KNI_REQ_Q_MZ_NAME_FMT "kni_req_%s"
44 #define KNI_RESP_Q_MZ_NAME_FMT "kni_resp_%s"
45 #define KNI_SYNC_ADDR_MZ_NAME_FMT "kni_sync_%s"
47 TAILQ_HEAD(rte_kni_list, rte_tailq_entry);
49 static struct rte_tailq_elem rte_kni_tailq = {
52 EAL_REGISTER_TAILQ(rte_kni_tailq)
58 char name[RTE_KNI_NAMESIZE]; /**< KNI interface name */
59 uint16_t group_id; /**< Group ID of KNI devices */
60 uint32_t slot_id; /**< KNI pool slot ID */
61 struct rte_mempool *pktmbuf_pool; /**< pkt mbuf mempool */
62 unsigned mbuf_size; /**< mbuf size */
64 const struct rte_memzone *m_tx_q; /**< TX queue memzone */
65 const struct rte_memzone *m_rx_q; /**< RX queue memzone */
66 const struct rte_memzone *m_alloc_q;/**< Alloc queue memzone */
67 const struct rte_memzone *m_free_q; /**< Free queue memzone */
69 struct rte_kni_fifo *tx_q; /**< TX queue */
70 struct rte_kni_fifo *rx_q; /**< RX queue */
71 struct rte_kni_fifo *alloc_q; /**< Allocated mbufs queue */
72 struct rte_kni_fifo *free_q; /**< To be freed mbufs queue */
74 const struct rte_memzone *m_req_q; /**< Request queue memzone */
75 const struct rte_memzone *m_resp_q; /**< Response queue memzone */
76 const struct rte_memzone *m_sync_addr;/**< Sync addr memzone */
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 */
83 struct rte_kni_ops ops; /**< operations for request */
87 KNI_REQ_NO_REGISTER = 0,
91 static void kni_free_mbufs(struct rte_kni *kni);
92 static void kni_allocate_mbufs(struct rte_kni *kni);
94 static volatile int kni_fd = -1;
96 /* Shall be called before any allocation happens */
98 rte_kni_init(unsigned int max_kni_ifaces __rte_unused)
100 /* Check FD and open */
102 kni_fd = open("/dev/" KNI_DEVICE, O_RDWR);
105 "Can not open /dev/%s\n", KNI_DEVICE);
113 static struct rte_kni *
114 __rte_kni_get(const char *name)
117 struct rte_tailq_entry *te;
118 struct rte_kni_list *kni_list;
120 kni_list = RTE_TAILQ_CAST(rte_kni_tailq.head, rte_kni_list);
122 TAILQ_FOREACH(te, kni_list, next) {
124 if (strncmp(name, kni->name, RTE_KNI_NAMESIZE) == 0)
135 kni_reserve_mz(struct rte_kni *kni)
137 char mz_name[RTE_MEMZONE_NAMESIZE];
139 snprintf(mz_name, RTE_MEMZONE_NAMESIZE, KNI_TX_Q_MZ_NAME_FMT, kni->name);
140 kni->m_tx_q = rte_memzone_reserve(mz_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
141 KNI_MEM_CHECK(kni->m_tx_q == NULL, tx_q_fail);
143 snprintf(mz_name, RTE_MEMZONE_NAMESIZE, KNI_RX_Q_MZ_NAME_FMT, kni->name);
144 kni->m_rx_q = rte_memzone_reserve(mz_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
145 KNI_MEM_CHECK(kni->m_rx_q == NULL, rx_q_fail);
147 snprintf(mz_name, RTE_MEMZONE_NAMESIZE, KNI_ALLOC_Q_MZ_NAME_FMT, kni->name);
148 kni->m_alloc_q = rte_memzone_reserve(mz_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
149 KNI_MEM_CHECK(kni->m_alloc_q == NULL, alloc_q_fail);
151 snprintf(mz_name, RTE_MEMZONE_NAMESIZE, KNI_FREE_Q_MZ_NAME_FMT, kni->name);
152 kni->m_free_q = rte_memzone_reserve(mz_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
153 KNI_MEM_CHECK(kni->m_free_q == NULL, free_q_fail);
155 snprintf(mz_name, RTE_MEMZONE_NAMESIZE, KNI_REQ_Q_MZ_NAME_FMT, kni->name);
156 kni->m_req_q = rte_memzone_reserve(mz_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
157 KNI_MEM_CHECK(kni->m_req_q == NULL, req_q_fail);
159 snprintf(mz_name, RTE_MEMZONE_NAMESIZE, KNI_RESP_Q_MZ_NAME_FMT, kni->name);
160 kni->m_resp_q = rte_memzone_reserve(mz_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
161 KNI_MEM_CHECK(kni->m_resp_q == NULL, resp_q_fail);
163 snprintf(mz_name, RTE_MEMZONE_NAMESIZE, KNI_SYNC_ADDR_MZ_NAME_FMT, kni->name);
164 kni->m_sync_addr = rte_memzone_reserve(mz_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
165 KNI_MEM_CHECK(kni->m_sync_addr == NULL, sync_addr_fail);
170 rte_memzone_free(kni->m_resp_q);
172 rte_memzone_free(kni->m_req_q);
174 rte_memzone_free(kni->m_free_q);
176 rte_memzone_free(kni->m_alloc_q);
178 rte_memzone_free(kni->m_rx_q);
180 rte_memzone_free(kni->m_tx_q);
186 kni_release_mz(struct rte_kni *kni)
188 rte_memzone_free(kni->m_tx_q);
189 rte_memzone_free(kni->m_rx_q);
190 rte_memzone_free(kni->m_alloc_q);
191 rte_memzone_free(kni->m_free_q);
192 rte_memzone_free(kni->m_req_q);
193 rte_memzone_free(kni->m_resp_q);
194 rte_memzone_free(kni->m_sync_addr);
198 rte_kni_alloc(struct rte_mempool *pktmbuf_pool,
199 const struct rte_kni_conf *conf,
200 struct rte_kni_ops *ops)
203 struct rte_kni_device_info dev_info;
205 struct rte_tailq_entry *te;
206 struct rte_kni_list *kni_list;
208 if (!pktmbuf_pool || !conf || !conf->name[0])
211 /* Check if KNI subsystem has been initialized */
213 RTE_LOG(ERR, KNI, "KNI subsystem has not been initialized. Invoke rte_kni_init() first\n");
217 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
219 kni = __rte_kni_get(conf->name);
221 RTE_LOG(ERR, KNI, "KNI already exists\n");
225 te = rte_zmalloc("KNI_TAILQ_ENTRY", sizeof(*te), 0);
227 RTE_LOG(ERR, KNI, "Failed to allocate tailq entry\n");
231 kni = rte_zmalloc("KNI", sizeof(struct rte_kni), RTE_CACHE_LINE_SIZE);
233 RTE_LOG(ERR, KNI, "KNI memory allocation failed\n");
237 snprintf(kni->name, RTE_KNI_NAMESIZE, "%s", conf->name);
240 memcpy(&kni->ops, ops, sizeof(struct rte_kni_ops));
242 kni->ops.port_id = UINT16_MAX;
244 memset(&dev_info, 0, sizeof(dev_info));
245 dev_info.bus = conf->addr.bus;
246 dev_info.devid = conf->addr.devid;
247 dev_info.function = conf->addr.function;
248 dev_info.vendor_id = conf->id.vendor_id;
249 dev_info.device_id = conf->id.device_id;
250 dev_info.core_id = conf->core_id;
251 dev_info.force_bind = conf->force_bind;
252 dev_info.group_id = conf->group_id;
253 dev_info.mbuf_size = conf->mbuf_size;
254 dev_info.mtu = conf->mtu;
256 memcpy(dev_info.mac_addr, conf->mac_addr, ETHER_ADDR_LEN);
258 snprintf(dev_info.name, RTE_KNI_NAMESIZE, "%s", conf->name);
260 RTE_LOG(INFO, KNI, "pci: %02x:%02x:%02x \t %02x:%02x\n",
261 dev_info.bus, dev_info.devid, dev_info.function,
262 dev_info.vendor_id, dev_info.device_id);
264 ret = kni_reserve_mz(kni);
269 kni->tx_q = kni->m_tx_q->addr;
270 kni_fifo_init(kni->tx_q, KNI_FIFO_COUNT_MAX);
271 dev_info.tx_phys = kni->m_tx_q->phys_addr;
274 kni->rx_q = kni->m_rx_q->addr;
275 kni_fifo_init(kni->rx_q, KNI_FIFO_COUNT_MAX);
276 dev_info.rx_phys = kni->m_rx_q->phys_addr;
279 kni->alloc_q = kni->m_alloc_q->addr;
280 kni_fifo_init(kni->alloc_q, KNI_FIFO_COUNT_MAX);
281 dev_info.alloc_phys = kni->m_alloc_q->phys_addr;
284 kni->free_q = kni->m_free_q->addr;
285 kni_fifo_init(kni->free_q, KNI_FIFO_COUNT_MAX);
286 dev_info.free_phys = kni->m_free_q->phys_addr;
289 kni->req_q = kni->m_req_q->addr;
290 kni_fifo_init(kni->req_q, KNI_FIFO_COUNT_MAX);
291 dev_info.req_phys = kni->m_req_q->phys_addr;
294 kni->resp_q = kni->m_resp_q->addr;
295 kni_fifo_init(kni->resp_q, KNI_FIFO_COUNT_MAX);
296 dev_info.resp_phys = kni->m_resp_q->phys_addr;
298 /* Req/Resp sync mem area */
299 kni->sync_addr = kni->m_sync_addr->addr;
300 dev_info.sync_va = kni->m_sync_addr->addr;
301 dev_info.sync_phys = kni->m_sync_addr->phys_addr;
303 kni->pktmbuf_pool = pktmbuf_pool;
304 kni->group_id = conf->group_id;
305 kni->mbuf_size = conf->mbuf_size;
307 ret = ioctl(kni_fd, RTE_KNI_IOCTL_CREATE, &dev_info);
313 kni_list = RTE_TAILQ_CAST(rte_kni_tailq.head, rte_kni_list);
314 TAILQ_INSERT_TAIL(kni_list, te, next);
316 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
318 /* Allocate mbufs and then put them into alloc_q */
319 kni_allocate_mbufs(kni);
330 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
336 kni_free_fifo(struct rte_kni_fifo *fifo)
339 struct rte_mbuf *pkt;
342 ret = kni_fifo_get(fifo, (void **)&pkt, 1);
344 rte_pktmbuf_free(pkt);
349 va2pa(struct rte_mbuf *m)
351 return (void *)((unsigned long)m -
352 ((unsigned long)m->buf_addr -
353 (unsigned long)m->buf_iova));
357 obj_free(struct rte_mempool *mp __rte_unused, void *opaque, void *obj,
358 unsigned obj_idx __rte_unused)
360 struct rte_mbuf *m = obj;
361 void *mbuf_phys = opaque;
363 if (va2pa(m) == mbuf_phys)
368 kni_free_fifo_phy(struct rte_mempool *mp, struct rte_kni_fifo *fifo)
374 ret = kni_fifo_get(fifo, &mbuf_phys, 1);
376 rte_mempool_obj_iter(mp, obj_free, mbuf_phys);
381 rte_kni_release(struct rte_kni *kni)
383 struct rte_tailq_entry *te;
384 struct rte_kni_list *kni_list;
385 struct rte_kni_device_info dev_info;
391 kni_list = RTE_TAILQ_CAST(rte_kni_tailq.head, rte_kni_list);
393 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
395 TAILQ_FOREACH(te, kni_list, next) {
403 snprintf(dev_info.name, sizeof(dev_info.name), "%s", kni->name);
404 if (ioctl(kni_fd, RTE_KNI_IOCTL_RELEASE, &dev_info) < 0) {
405 RTE_LOG(ERR, KNI, "Fail to release kni device\n");
409 TAILQ_REMOVE(kni_list, te, next);
411 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
413 /* mbufs in all fifo should be released, except request/response */
415 /* wait until all rxq packets processed by kernel */
416 while (kni_fifo_count(kni->rx_q) && retry--)
419 if (kni_fifo_count(kni->rx_q))
420 RTE_LOG(ERR, KNI, "Fail to free all Rx-q items\n");
422 kni_free_fifo_phy(kni->pktmbuf_pool, kni->alloc_q);
423 kni_free_fifo(kni->tx_q);
424 kni_free_fifo(kni->free_q);
435 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
440 /* default callback for request of configuring device mac address */
442 kni_config_mac_address(uint16_t port_id, uint8_t mac_addr[])
446 if (!rte_eth_dev_is_valid_port(port_id)) {
447 RTE_LOG(ERR, KNI, "Invalid port id %d\n", port_id);
451 RTE_LOG(INFO, KNI, "Configure mac address of %d", port_id);
453 ret = rte_eth_dev_default_mac_addr_set(port_id,
454 (struct ether_addr *)mac_addr);
456 RTE_LOG(ERR, KNI, "Failed to config mac_addr for port %d\n",
462 /* default callback for request of configuring promiscuous mode */
464 kni_config_promiscusity(uint16_t port_id, uint8_t to_on)
466 if (!rte_eth_dev_is_valid_port(port_id)) {
467 RTE_LOG(ERR, KNI, "Invalid port id %d\n", port_id);
471 RTE_LOG(INFO, KNI, "Configure promiscuous mode of %d to %d\n",
475 rte_eth_promiscuous_enable(port_id);
477 rte_eth_promiscuous_disable(port_id);
483 rte_kni_handle_request(struct rte_kni *kni)
486 struct rte_kni_request *req = NULL;
491 /* Get request mbuf */
492 ret = kni_fifo_get(kni->req_q, (void **)&req, 1);
494 return 0; /* It is OK of can not getting the request mbuf */
496 if (req != kni->sync_addr) {
497 RTE_LOG(ERR, KNI, "Wrong req pointer %p\n", req);
501 /* Analyze the request and call the relevant actions for it */
502 switch (req->req_id) {
503 case RTE_KNI_REQ_CHANGE_MTU: /* Change MTU */
504 if (kni->ops.change_mtu)
505 req->result = kni->ops.change_mtu(kni->ops.port_id,
508 case RTE_KNI_REQ_CFG_NETWORK_IF: /* Set network interface up/down */
509 if (kni->ops.config_network_if)
510 req->result = kni->ops.config_network_if(\
511 kni->ops.port_id, req->if_up);
513 case RTE_KNI_REQ_CHANGE_MAC_ADDR: /* Change MAC Address */
514 if (kni->ops.config_mac_address)
515 req->result = kni->ops.config_mac_address(
516 kni->ops.port_id, req->mac_addr);
517 else if (kni->ops.port_id != UINT16_MAX)
518 req->result = kni_config_mac_address(
519 kni->ops.port_id, req->mac_addr);
521 case RTE_KNI_REQ_CHANGE_PROMISC: /* Change PROMISCUOUS MODE */
522 if (kni->ops.config_promiscusity)
523 req->result = kni->ops.config_promiscusity(
524 kni->ops.port_id, req->promiscusity);
525 else if (kni->ops.port_id != UINT16_MAX)
526 req->result = kni_config_promiscusity(
527 kni->ops.port_id, req->promiscusity);
530 RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id);
531 req->result = -EINVAL;
535 /* Construct response mbuf and put it back to resp_q */
536 ret = kni_fifo_put(kni->resp_q, (void **)&req, 1);
538 RTE_LOG(ERR, KNI, "Fail to put the muf back to resp_q\n");
539 return -1; /* It is an error of can't putting the mbuf back */
546 rte_kni_tx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
548 void *phy_mbufs[num];
552 for (i = 0; i < num; i++)
553 phy_mbufs[i] = va2pa(mbufs[i]);
555 ret = kni_fifo_put(kni->rx_q, phy_mbufs, num);
557 /* Get mbufs from free_q and then free them */
564 rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
566 unsigned ret = kni_fifo_get(kni->tx_q, (void **)mbufs, num);
568 /* If buffers removed, allocate mbufs and then put them into alloc_q */
570 kni_allocate_mbufs(kni);
576 kni_free_mbufs(struct rte_kni *kni)
579 struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
581 ret = kni_fifo_get(kni->free_q, (void **)pkts, MAX_MBUF_BURST_NUM);
582 if (likely(ret > 0)) {
583 for (i = 0; i < ret; i++)
584 rte_pktmbuf_free(pkts[i]);
589 kni_allocate_mbufs(struct rte_kni *kni)
592 struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
593 void *phys[MAX_MBUF_BURST_NUM];
596 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pool) !=
597 offsetof(struct rte_kni_mbuf, pool));
598 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, buf_addr) !=
599 offsetof(struct rte_kni_mbuf, buf_addr));
600 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, next) !=
601 offsetof(struct rte_kni_mbuf, next));
602 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_off) !=
603 offsetof(struct rte_kni_mbuf, data_off));
604 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_len) !=
605 offsetof(struct rte_kni_mbuf, data_len));
606 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pkt_len) !=
607 offsetof(struct rte_kni_mbuf, pkt_len));
608 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, ol_flags) !=
609 offsetof(struct rte_kni_mbuf, ol_flags));
611 /* Check if pktmbuf pool has been configured */
612 if (kni->pktmbuf_pool == NULL) {
613 RTE_LOG(ERR, KNI, "No valid mempool for allocating mbufs\n");
617 allocq_free = (kni->alloc_q->read - kni->alloc_q->write - 1) \
618 & (MAX_MBUF_BURST_NUM - 1);
619 for (i = 0; i < allocq_free; i++) {
620 pkts[i] = rte_pktmbuf_alloc(kni->pktmbuf_pool);
621 if (unlikely(pkts[i] == NULL)) {
623 RTE_LOG(ERR, KNI, "Out of memory\n");
626 phys[i] = va2pa(pkts[i]);
629 /* No pkt mbuf allocated */
633 ret = kni_fifo_put(kni->alloc_q, phys, i);
635 /* Check if any mbufs not put into alloc_q, and then free them */
636 if (ret >= 0 && ret < i && ret < MAX_MBUF_BURST_NUM) {
639 for (j = ret; j < i; j++)
640 rte_pktmbuf_free(pkts[j]);
645 rte_kni_get(const char *name)
649 if (name == NULL || name[0] == '\0')
652 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
654 kni = __rte_kni_get(name);
656 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
662 rte_kni_get_name(const struct rte_kni *kni)
667 static enum kni_ops_status
668 kni_check_request_register(struct rte_kni_ops *ops)
670 /* check if KNI request ops has been registered*/
672 return KNI_REQ_NO_REGISTER;
674 if ((ops->change_mtu == NULL)
675 && (ops->config_network_if == NULL)
676 && (ops->config_mac_address == NULL)
677 && (ops->config_promiscusity == NULL))
678 return KNI_REQ_NO_REGISTER;
680 return KNI_REQ_REGISTERED;
684 rte_kni_register_handlers(struct rte_kni *kni,struct rte_kni_ops *ops)
686 enum kni_ops_status req_status;
689 RTE_LOG(ERR, KNI, "Invalid KNI request operation.\n");
694 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
698 req_status = kni_check_request_register(&kni->ops);
699 if ( KNI_REQ_REGISTERED == req_status) {
700 RTE_LOG(ERR, KNI, "The KNI request operation has already registered.\n");
704 memcpy(&kni->ops, ops, sizeof(struct rte_kni_ops));
709 rte_kni_unregister_handlers(struct rte_kni *kni)
712 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
716 memset(&kni->ops, 0, sizeof(struct rte_kni_ops));
721 int __rte_experimental
722 rte_kni_update_link(struct rte_kni *kni, unsigned int linkup)
726 const char *new_carrier;
733 snprintf(path, sizeof(path), "/sys/devices/virtual/net/%s/carrier",
736 fd = open(path, O_RDWR);
738 RTE_LOG(ERR, KNI, "Failed to open file: %s.\n", path);
742 ret = read(fd, old_carrier, 2);
747 old_linkup = (old_carrier[0] == '1');
749 new_carrier = linkup ? "1" : "0";
750 ret = write(fd, new_carrier, 1);
752 RTE_LOG(ERR, KNI, "Failed to write file: %s.\n", path);