1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(c) 2010-2014 Intel Corporation.
7 * This code is inspired from the book "Linux Device Drivers" by
8 * Alessandro Rubini and Jonathan Corbet, published by O'Reilly & Associates
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/version.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h> /* eth_type_trans */
16 #include <linux/ethtool.h>
17 #include <linux/skbuff.h>
18 #include <linux/kthread.h>
19 #include <linux/delay.h>
20 #include <linux/rtnetlink.h>
22 #include <rte_kni_common.h>
28 #define WD_TIMEOUT 5 /*jiffies */
30 #define KNI_WAIT_RESPONSE_TIMEOUT 300 /* 3 seconds */
32 /* typedef for rx function */
33 typedef void (*kni_net_rx_t)(struct kni_dev *kni);
35 static void kni_net_rx_normal(struct kni_dev *kni);
37 /* kni rx function pointer, with default to normal rx */
38 static kni_net_rx_t kni_net_rx_func = kni_net_rx_normal;
40 #ifdef HAVE_IOVA_TO_KVA_MAPPING_SUPPORT
41 /* iova to kernel virtual address */
43 iova2kva(struct kni_dev *kni, void *iova)
45 return phys_to_virt(iova_to_phys(kni->usr_tsk, (unsigned long)iova));
49 iova2data_kva(struct kni_dev *kni, struct rte_kni_mbuf *m)
51 return phys_to_virt(iova_to_phys(kni->usr_tsk, m->buf_iova) +
56 /* physical address to kernel virtual address */
60 return phys_to_virt((unsigned long)pa);
63 /* physical address to virtual address */
65 pa2va(void *pa, struct rte_kni_mbuf *m)
69 va = (void *)((unsigned long)pa +
70 (unsigned long)m->buf_addr -
71 (unsigned long)m->buf_iova);
75 /* mbuf data kernel virtual address from mbuf kernel virtual address */
77 kva2data_kva(struct rte_kni_mbuf *m)
79 return phys_to_virt(m->buf_iova + m->data_off);
83 get_kva(struct kni_dev *kni, void *pa)
85 #ifdef HAVE_IOVA_TO_KVA_MAPPING_SUPPORT
86 if (kni->iova_mode == 1)
87 return iova2kva(kni, pa);
93 get_data_kva(struct kni_dev *kni, void *pkt_kva)
95 #ifdef HAVE_IOVA_TO_KVA_MAPPING_SUPPORT
96 if (kni->iova_mode == 1)
97 return iova2data_kva(kni, pkt_kva);
99 return kva2data_kva(pkt_kva);
103 * It can be called to process the request.
106 kni_net_process_request(struct net_device *dev, struct rte_kni_request *req)
108 struct kni_dev *kni = netdev_priv(dev);
116 /* If we need to wait and RTNL mutex is held
117 * drop the mutex and hold reference to keep device
119 if (req->async == 0) {
124 mutex_lock(&kni->sync_lock);
127 memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
128 num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
130 pr_err("Cannot send to req_q\n");
135 /* No result available since request is handled
136 * asynchronously. set response to success.
138 if (req->async != 0) {
143 ret_val = wait_event_interruptible_timeout(kni->wq,
144 kni_fifo_count(kni->resp_q), 3 * HZ);
145 if (signal_pending(current) || ret_val <= 0) {
149 num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
150 if (num != 1 || resp_va != kni->sync_va) {
151 /* This should never happen */
152 pr_err("No data in resp_q\n");
157 memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
162 mutex_unlock(&kni->sync_lock);
163 if (req->async == 0) {
174 kni_net_open(struct net_device *dev)
177 struct rte_kni_request req;
179 netif_start_queue(dev);
180 if (kni_dflt_carrier == 1)
181 netif_carrier_on(dev);
183 netif_carrier_off(dev);
185 memset(&req, 0, sizeof(req));
186 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
188 /* Setting if_up to non-zero means up */
190 ret = kni_net_process_request(dev, &req);
192 return (ret == 0) ? req.result : ret;
196 kni_net_release(struct net_device *dev)
199 struct rte_kni_request req;
201 netif_stop_queue(dev); /* can't transmit any more */
202 netif_carrier_off(dev);
204 memset(&req, 0, sizeof(req));
205 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
207 /* Setting if_up to 0 means down */
210 /* request async because of the deadlock problem */
213 ret = kni_net_process_request(dev, &req);
215 return (ret == 0) ? req.result : ret;
219 kni_fifo_trans_pa2va(struct kni_dev *kni,
220 struct rte_kni_fifo *src_pa, struct rte_kni_fifo *dst_va)
222 uint32_t ret, i, num_dst, num_rx;
223 struct rte_kni_mbuf *kva, *prev_kva;
228 num_dst = kni_fifo_free_count(dst_va);
232 num_rx = min_t(uint32_t, num_dst, MBUF_BURST_SZ);
234 num_rx = kni_fifo_get(src_pa, kni->pa, num_rx);
238 for (i = 0; i < num_rx; i++) {
239 kva = get_kva(kni, kni->pa[i]);
240 kni->va[i] = pa2va(kni->pa[i], kva);
242 kva_nb_segs = kva->nb_segs;
243 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
248 kva = pa2kva(kva->next);
249 /* Convert physical address to virtual address */
250 prev_kva->next = pa2va(prev_kva->next, kva);
254 ret = kni_fifo_put(dst_va, kni->va, num_rx);
256 /* Failing should not happen */
257 pr_err("Fail to enqueue entries into dst_va\n");
263 /* Try to release mbufs when kni release */
264 void kni_net_release_fifo_phy(struct kni_dev *kni)
266 /* release rx_q first, because it can't release in userspace */
267 kni_fifo_trans_pa2va(kni, kni->rx_q, kni->free_q);
268 /* release alloc_q for speeding up kni release in userspace */
269 kni_fifo_trans_pa2va(kni, kni->alloc_q, kni->free_q);
273 * Configuration changes (passed on by ifconfig)
276 kni_net_config(struct net_device *dev, struct ifmap *map)
278 if (dev->flags & IFF_UP) /* can't act on a running interface */
281 /* ignore other fields */
286 * Transmit a packet (called by the kernel)
289 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
293 struct kni_dev *kni = netdev_priv(dev);
294 struct rte_kni_mbuf *pkt_kva = NULL;
298 /* save the timestamp */
299 #ifdef HAVE_TRANS_START_HELPER
300 netif_trans_update(dev);
302 dev->trans_start = jiffies;
305 /* Check if the length of skb is less than mbuf size */
306 if (skb->len > kni->mbuf_size)
310 * Check if it has at least one free entry in tx_q and
311 * one entry in alloc_q.
313 if (kni_fifo_free_count(kni->tx_q) == 0 ||
314 kni_fifo_count(kni->alloc_q) == 0) {
316 * If no free entry in tx_q or no entry in alloc_q,
317 * drops skb and goes out.
322 /* dequeue a mbuf from alloc_q */
323 ret = kni_fifo_get(kni->alloc_q, &pkt_pa, 1);
324 if (likely(ret == 1)) {
327 pkt_kva = get_kva(kni, pkt_pa);
328 data_kva = get_data_kva(kni, pkt_kva);
329 pkt_va = pa2va(pkt_pa, pkt_kva);
332 memcpy(data_kva, skb->data, len);
333 if (unlikely(len < ETH_ZLEN)) {
334 memset(data_kva + len, 0, ETH_ZLEN - len);
337 pkt_kva->pkt_len = len;
338 pkt_kva->data_len = len;
340 /* enqueue mbuf into tx_q */
341 ret = kni_fifo_put(kni->tx_q, &pkt_va, 1);
342 if (unlikely(ret != 1)) {
343 /* Failing should not happen */
344 pr_err("Fail to enqueue mbuf into tx_q\n");
348 /* Failing should not happen */
349 pr_err("Fail to dequeue mbuf from alloc_q\n");
353 /* Free skb and update statistics */
355 dev->stats.tx_bytes += len;
356 dev->stats.tx_packets++;
361 /* Free skb and update statistics */
363 dev->stats.tx_dropped++;
369 * RX: normal working mode
372 kni_net_rx_normal(struct kni_dev *kni)
376 uint32_t i, num_rx, num_fq;
377 struct rte_kni_mbuf *kva, *prev_kva;
380 struct net_device *dev = kni->net_dev;
382 /* Get the number of free entries in free_q */
383 num_fq = kni_fifo_free_count(kni->free_q);
385 /* No room on the free_q, bail out */
389 /* Calculate the number of entries to dequeue from rx_q */
390 num_rx = min_t(uint32_t, num_fq, MBUF_BURST_SZ);
392 /* Burst dequeue from rx_q */
393 num_rx = kni_fifo_get(kni->rx_q, kni->pa, num_rx);
397 /* Transfer received packets to netif */
398 for (i = 0; i < num_rx; i++) {
399 kva = get_kva(kni, kni->pa[i]);
401 data_kva = get_data_kva(kni, kva);
402 kni->va[i] = pa2va(kni->pa[i], kva);
404 skb = netdev_alloc_skb(dev, len);
406 /* Update statistics */
407 dev->stats.rx_dropped++;
411 if (kva->nb_segs == 1) {
412 memcpy(skb_put(skb, len), data_kva, len);
415 int kva_nb_segs = kva->nb_segs;
417 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
418 memcpy(skb_put(skb, kva->data_len),
419 data_kva, kva->data_len);
425 kva = pa2kva(kva->next);
426 data_kva = kva2data_kva(kva);
427 /* Convert physical address to virtual address */
428 prev_kva->next = pa2va(prev_kva->next, kva);
432 skb->protocol = eth_type_trans(skb, dev);
433 skb->ip_summed = CHECKSUM_UNNECESSARY;
435 /* Call netif interface */
438 /* Update statistics */
439 dev->stats.rx_bytes += len;
440 dev->stats.rx_packets++;
443 /* Burst enqueue mbufs into free_q */
444 ret = kni_fifo_put(kni->free_q, kni->va, num_rx);
446 /* Failing should not happen */
447 pr_err("Fail to enqueue entries into free_q\n");
451 * RX: loopback with enqueue/dequeue fifos.
454 kni_net_rx_lo_fifo(struct kni_dev *kni)
458 uint32_t i, num, num_rq, num_tq, num_aq, num_fq;
459 struct rte_kni_mbuf *kva, *next_kva;
461 struct rte_kni_mbuf *alloc_kva;
462 void *alloc_data_kva;
463 struct net_device *dev = kni->net_dev;
465 /* Get the number of entries in rx_q */
466 num_rq = kni_fifo_count(kni->rx_q);
468 /* Get the number of free entries in tx_q */
469 num_tq = kni_fifo_free_count(kni->tx_q);
471 /* Get the number of entries in alloc_q */
472 num_aq = kni_fifo_count(kni->alloc_q);
474 /* Get the number of free entries in free_q */
475 num_fq = kni_fifo_free_count(kni->free_q);
477 /* Calculate the number of entries to be dequeued from rx_q */
478 num = min(num_rq, num_tq);
479 num = min(num, num_aq);
480 num = min(num, num_fq);
481 num = min_t(uint32_t, num, MBUF_BURST_SZ);
483 /* Return if no entry to dequeue from rx_q */
487 /* Burst dequeue from rx_q */
488 ret = kni_fifo_get(kni->rx_q, kni->pa, num);
490 return; /* Failing should not happen */
492 /* Dequeue entries from alloc_q */
493 ret = kni_fifo_get(kni->alloc_q, kni->alloc_pa, num);
497 for (i = 0; i < num; i++) {
498 kva = get_kva(kni, kni->pa[i]);
500 data_kva = get_data_kva(kni, kva);
501 kni->va[i] = pa2va(kni->pa[i], kva);
504 next_kva = pa2kva(kva->next);
505 /* Convert physical address to virtual address */
506 kva->next = pa2va(kva->next, next_kva);
510 alloc_kva = get_kva(kni, kni->alloc_pa[i]);
511 alloc_data_kva = get_data_kva(kni, alloc_kva);
512 kni->alloc_va[i] = pa2va(kni->alloc_pa[i], alloc_kva);
514 memcpy(alloc_data_kva, data_kva, len);
515 alloc_kva->pkt_len = len;
516 alloc_kva->data_len = len;
518 dev->stats.tx_bytes += len;
519 dev->stats.rx_bytes += len;
522 /* Burst enqueue mbufs into tx_q */
523 ret = kni_fifo_put(kni->tx_q, kni->alloc_va, num);
525 /* Failing should not happen */
526 pr_err("Fail to enqueue mbufs into tx_q\n");
529 /* Burst enqueue mbufs into free_q */
530 ret = kni_fifo_put(kni->free_q, kni->va, num);
532 /* Failing should not happen */
533 pr_err("Fail to enqueue mbufs into free_q\n");
536 * Update statistic, and enqueue/dequeue failure is impossible,
537 * as all queues are checked at first.
539 dev->stats.tx_packets += num;
540 dev->stats.rx_packets += num;
544 * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
547 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
551 uint32_t i, num_rq, num_fq, num;
552 struct rte_kni_mbuf *kva, *prev_kva;
555 struct net_device *dev = kni->net_dev;
557 /* Get the number of entries in rx_q */
558 num_rq = kni_fifo_count(kni->rx_q);
560 /* Get the number of free entries in free_q */
561 num_fq = kni_fifo_free_count(kni->free_q);
563 /* Calculate the number of entries to dequeue from rx_q */
564 num = min(num_rq, num_fq);
565 num = min_t(uint32_t, num, MBUF_BURST_SZ);
567 /* Return if no entry to dequeue from rx_q */
571 /* Burst dequeue mbufs from rx_q */
572 ret = kni_fifo_get(kni->rx_q, kni->pa, num);
576 /* Copy mbufs to sk buffer and then call tx interface */
577 for (i = 0; i < num; i++) {
578 kva = get_kva(kni, kni->pa[i]);
580 data_kva = get_data_kva(kni, kva);
581 kni->va[i] = pa2va(kni->pa[i], kva);
583 skb = netdev_alloc_skb(dev, len);
585 memcpy(skb_put(skb, len), data_kva, len);
586 skb->ip_summed = CHECKSUM_UNNECESSARY;
590 /* Simulate real usage, allocate/copy skb twice */
591 skb = netdev_alloc_skb(dev, len);
593 dev->stats.rx_dropped++;
597 if (kva->nb_segs == 1) {
598 memcpy(skb_put(skb, len), data_kva, len);
601 int kva_nb_segs = kva->nb_segs;
603 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
604 memcpy(skb_put(skb, kva->data_len),
605 data_kva, kva->data_len);
611 kva = get_kva(kni, kva->next);
612 data_kva = get_data_kva(kni, kva);
613 /* Convert physical address to virtual address */
614 prev_kva->next = pa2va(prev_kva->next, kva);
618 skb->ip_summed = CHECKSUM_UNNECESSARY;
620 dev->stats.rx_bytes += len;
621 dev->stats.rx_packets++;
623 /* call tx interface */
624 kni_net_tx(skb, dev);
627 /* enqueue all the mbufs from rx_q into free_q */
628 ret = kni_fifo_put(kni->free_q, kni->va, num);
630 /* Failing should not happen */
631 pr_err("Fail to enqueue mbufs into free_q\n");
636 kni_net_rx(struct kni_dev *kni)
639 * It doesn't need to check if it is NULL pointer,
640 * as it has a default value
642 (*kni_net_rx_func)(kni);
646 * Deal with a transmit timeout.
648 #ifdef HAVE_TX_TIMEOUT_TXQUEUE
650 kni_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
653 kni_net_tx_timeout(struct net_device *dev)
656 pr_debug("Transmit timeout at %ld, latency %ld\n", jiffies,
657 jiffies - dev_trans_start(dev));
659 dev->stats.tx_errors++;
660 netif_wake_queue(dev);
664 kni_net_change_mtu(struct net_device *dev, int new_mtu)
667 struct rte_kni_request req;
669 pr_debug("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
671 memset(&req, 0, sizeof(req));
672 req.req_id = RTE_KNI_REQ_CHANGE_MTU;
673 req.new_mtu = new_mtu;
674 ret = kni_net_process_request(dev, &req);
675 if (ret == 0 && req.result == 0)
678 return (ret == 0) ? req.result : ret;
682 kni_net_change_rx_flags(struct net_device *netdev, int flags)
684 struct rte_kni_request req;
686 memset(&req, 0, sizeof(req));
688 if (flags & IFF_ALLMULTI) {
689 req.req_id = RTE_KNI_REQ_CHANGE_ALLMULTI;
691 if (netdev->flags & IFF_ALLMULTI)
697 if (flags & IFF_PROMISC) {
698 req.req_id = RTE_KNI_REQ_CHANGE_PROMISC;
700 if (netdev->flags & IFF_PROMISC)
701 req.promiscusity = 1;
703 req.promiscusity = 0;
706 kni_net_process_request(netdev, &req);
710 * Checks if the user space application provided the resp message
713 kni_net_poll_resp(struct kni_dev *kni)
715 if (kni_fifo_count(kni->resp_q))
716 wake_up_interruptible(&kni->wq);
720 * Fill the eth header
723 kni_net_header(struct sk_buff *skb, struct net_device *dev,
724 unsigned short type, const void *daddr,
725 const void *saddr, uint32_t len)
727 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
729 memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
730 memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
731 eth->h_proto = htons(type);
733 return dev->hard_header_len;
737 * Re-fill the eth header
739 #ifdef HAVE_REBUILD_HEADER
741 kni_net_rebuild_header(struct sk_buff *skb)
743 struct net_device *dev = skb->dev;
744 struct ethhdr *eth = (struct ethhdr *) skb->data;
746 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
747 memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
754 * kni_net_set_mac - Change the Ethernet Address of the KNI NIC
755 * @netdev: network interface device structure
756 * @p: pointer to an address structure
758 * Returns 0 on success, negative on failure
761 kni_net_set_mac(struct net_device *netdev, void *p)
764 struct rte_kni_request req;
765 struct sockaddr *addr = p;
767 memset(&req, 0, sizeof(req));
768 req.req_id = RTE_KNI_REQ_CHANGE_MAC_ADDR;
770 if (!is_valid_ether_addr((unsigned char *)(addr->sa_data)))
771 return -EADDRNOTAVAIL;
773 memcpy(req.mac_addr, addr->sa_data, netdev->addr_len);
774 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
776 ret = kni_net_process_request(netdev, &req);
778 return (ret == 0 ? req.result : ret);
781 #ifdef HAVE_CHANGE_CARRIER_CB
783 kni_net_change_carrier(struct net_device *dev, bool new_carrier)
786 netif_carrier_on(dev);
788 netif_carrier_off(dev);
793 static const struct header_ops kni_net_header_ops = {
794 .create = kni_net_header,
795 .parse = eth_header_parse,
796 #ifdef HAVE_REBUILD_HEADER
797 .rebuild = kni_net_rebuild_header,
799 .cache = NULL, /* disable caching */
802 static const struct net_device_ops kni_net_netdev_ops = {
803 .ndo_open = kni_net_open,
804 .ndo_stop = kni_net_release,
805 .ndo_set_config = kni_net_config,
806 .ndo_change_rx_flags = kni_net_change_rx_flags,
807 .ndo_start_xmit = kni_net_tx,
808 .ndo_change_mtu = kni_net_change_mtu,
809 .ndo_tx_timeout = kni_net_tx_timeout,
810 .ndo_set_mac_address = kni_net_set_mac,
811 #ifdef HAVE_CHANGE_CARRIER_CB
812 .ndo_change_carrier = kni_net_change_carrier,
816 static void kni_get_drvinfo(struct net_device *dev,
817 struct ethtool_drvinfo *info)
819 strlcpy(info->version, KNI_VERSION, sizeof(info->version));
820 strlcpy(info->driver, "kni", sizeof(info->driver));
823 static const struct ethtool_ops kni_net_ethtool_ops = {
824 .get_drvinfo = kni_get_drvinfo,
825 .get_link = ethtool_op_get_link,
829 kni_net_init(struct net_device *dev)
831 struct kni_dev *kni = netdev_priv(dev);
833 init_waitqueue_head(&kni->wq);
834 mutex_init(&kni->sync_lock);
836 ether_setup(dev); /* assign some of the fields */
837 dev->netdev_ops = &kni_net_netdev_ops;
838 dev->header_ops = &kni_net_header_ops;
839 dev->ethtool_ops = &kni_net_ethtool_ops;
840 dev->watchdog_timeo = WD_TIMEOUT;
844 kni_net_config_lo_mode(char *lo_str)
847 pr_debug("loopback disabled");
851 if (!strcmp(lo_str, "lo_mode_none"))
852 pr_debug("loopback disabled");
853 else if (!strcmp(lo_str, "lo_mode_fifo")) {
854 pr_debug("loopback mode=lo_mode_fifo enabled");
855 kni_net_rx_func = kni_net_rx_lo_fifo;
856 } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
857 pr_debug("loopback mode=lo_mode_fifo_skb enabled");
858 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
860 pr_debug("Unknown loopback parameter, disabled");