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 mutex_lock(&kni->sync_lock);
119 memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
120 num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
122 pr_err("Cannot send to req_q\n");
127 ret_val = wait_event_interruptible_timeout(kni->wq,
128 kni_fifo_count(kni->resp_q), 3 * HZ);
129 if (signal_pending(current) || ret_val <= 0) {
133 num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
134 if (num != 1 || resp_va != kni->sync_va) {
135 /* This should never happen */
136 pr_err("No data in resp_q\n");
141 memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
145 mutex_unlock(&kni->sync_lock);
153 kni_net_open(struct net_device *dev)
156 struct rte_kni_request req;
158 netif_start_queue(dev);
159 if (kni_dflt_carrier == 1)
160 netif_carrier_on(dev);
162 netif_carrier_off(dev);
164 memset(&req, 0, sizeof(req));
165 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
167 /* Setting if_up to non-zero means up */
169 ret = kni_net_process_request(dev, &req);
171 return (ret == 0) ? req.result : ret;
175 kni_net_release(struct net_device *dev)
178 struct rte_kni_request req;
180 netif_stop_queue(dev); /* can't transmit any more */
181 netif_carrier_off(dev);
183 memset(&req, 0, sizeof(req));
184 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
186 /* Setting if_up to 0 means down */
188 ret = kni_net_process_request(dev, &req);
190 return (ret == 0) ? req.result : ret;
194 kni_fifo_trans_pa2va(struct kni_dev *kni,
195 struct rte_kni_fifo *src_pa, struct rte_kni_fifo *dst_va)
197 uint32_t ret, i, num_dst, num_rx;
198 struct rte_kni_mbuf *kva, *prev_kva;
203 num_dst = kni_fifo_free_count(dst_va);
207 num_rx = min_t(uint32_t, num_dst, MBUF_BURST_SZ);
209 num_rx = kni_fifo_get(src_pa, kni->pa, num_rx);
213 for (i = 0; i < num_rx; i++) {
214 kva = get_kva(kni, kni->pa[i]);
215 kni->va[i] = pa2va(kni->pa[i], kva);
217 kva_nb_segs = kva->nb_segs;
218 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
223 kva = pa2kva(kva->next);
224 /* Convert physical address to virtual address */
225 prev_kva->next = pa2va(prev_kva->next, kva);
229 ret = kni_fifo_put(dst_va, kni->va, num_rx);
231 /* Failing should not happen */
232 pr_err("Fail to enqueue entries into dst_va\n");
238 /* Try to release mbufs when kni release */
239 void kni_net_release_fifo_phy(struct kni_dev *kni)
241 /* release rx_q first, because it can't release in userspace */
242 kni_fifo_trans_pa2va(kni, kni->rx_q, kni->free_q);
243 /* release alloc_q for speeding up kni release in userspace */
244 kni_fifo_trans_pa2va(kni, kni->alloc_q, kni->free_q);
248 * Configuration changes (passed on by ifconfig)
251 kni_net_config(struct net_device *dev, struct ifmap *map)
253 if (dev->flags & IFF_UP) /* can't act on a running interface */
256 /* ignore other fields */
261 * Transmit a packet (called by the kernel)
264 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
268 struct kni_dev *kni = netdev_priv(dev);
269 struct rte_kni_mbuf *pkt_kva = NULL;
273 /* save the timestamp */
274 #ifdef HAVE_TRANS_START_HELPER
275 netif_trans_update(dev);
277 dev->trans_start = jiffies;
280 /* Check if the length of skb is less than mbuf size */
281 if (skb->len > kni->mbuf_size)
285 * Check if it has at least one free entry in tx_q and
286 * one entry in alloc_q.
288 if (kni_fifo_free_count(kni->tx_q) == 0 ||
289 kni_fifo_count(kni->alloc_q) == 0) {
291 * If no free entry in tx_q or no entry in alloc_q,
292 * drops skb and goes out.
297 /* dequeue a mbuf from alloc_q */
298 ret = kni_fifo_get(kni->alloc_q, &pkt_pa, 1);
299 if (likely(ret == 1)) {
302 pkt_kva = get_kva(kni, pkt_pa);
303 data_kva = get_data_kva(kni, pkt_kva);
304 pkt_va = pa2va(pkt_pa, pkt_kva);
307 memcpy(data_kva, skb->data, len);
308 if (unlikely(len < ETH_ZLEN)) {
309 memset(data_kva + len, 0, ETH_ZLEN - len);
312 pkt_kva->pkt_len = len;
313 pkt_kva->data_len = len;
315 /* enqueue mbuf into tx_q */
316 ret = kni_fifo_put(kni->tx_q, &pkt_va, 1);
317 if (unlikely(ret != 1)) {
318 /* Failing should not happen */
319 pr_err("Fail to enqueue mbuf into tx_q\n");
323 /* Failing should not happen */
324 pr_err("Fail to dequeue mbuf from alloc_q\n");
328 /* Free skb and update statistics */
330 dev->stats.tx_bytes += len;
331 dev->stats.tx_packets++;
336 /* Free skb and update statistics */
338 dev->stats.tx_dropped++;
344 * RX: normal working mode
347 kni_net_rx_normal(struct kni_dev *kni)
351 uint32_t i, num_rx, num_fq;
352 struct rte_kni_mbuf *kva, *prev_kva;
355 struct net_device *dev = kni->net_dev;
357 /* Get the number of free entries in free_q */
358 num_fq = kni_fifo_free_count(kni->free_q);
360 /* No room on the free_q, bail out */
364 /* Calculate the number of entries to dequeue from rx_q */
365 num_rx = min_t(uint32_t, num_fq, MBUF_BURST_SZ);
367 /* Burst dequeue from rx_q */
368 num_rx = kni_fifo_get(kni->rx_q, kni->pa, num_rx);
372 /* Transfer received packets to netif */
373 for (i = 0; i < num_rx; i++) {
374 kva = get_kva(kni, kni->pa[i]);
376 data_kva = get_data_kva(kni, kva);
377 kni->va[i] = pa2va(kni->pa[i], kva);
379 skb = netdev_alloc_skb(dev, len);
381 /* Update statistics */
382 dev->stats.rx_dropped++;
386 if (kva->nb_segs == 1) {
387 memcpy(skb_put(skb, len), data_kva, len);
390 int kva_nb_segs = kva->nb_segs;
392 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
393 memcpy(skb_put(skb, kva->data_len),
394 data_kva, kva->data_len);
400 kva = pa2kva(kva->next);
401 data_kva = kva2data_kva(kva);
402 /* Convert physical address to virtual address */
403 prev_kva->next = pa2va(prev_kva->next, kva);
407 skb->protocol = eth_type_trans(skb, dev);
408 skb->ip_summed = CHECKSUM_UNNECESSARY;
410 /* Call netif interface */
413 /* Update statistics */
414 dev->stats.rx_bytes += len;
415 dev->stats.rx_packets++;
418 /* Burst enqueue mbufs into free_q */
419 ret = kni_fifo_put(kni->free_q, kni->va, num_rx);
421 /* Failing should not happen */
422 pr_err("Fail to enqueue entries into free_q\n");
426 * RX: loopback with enqueue/dequeue fifos.
429 kni_net_rx_lo_fifo(struct kni_dev *kni)
433 uint32_t i, num, num_rq, num_tq, num_aq, num_fq;
434 struct rte_kni_mbuf *kva, *next_kva;
436 struct rte_kni_mbuf *alloc_kva;
437 void *alloc_data_kva;
438 struct net_device *dev = kni->net_dev;
440 /* Get the number of entries in rx_q */
441 num_rq = kni_fifo_count(kni->rx_q);
443 /* Get the number of free entries in tx_q */
444 num_tq = kni_fifo_free_count(kni->tx_q);
446 /* Get the number of entries in alloc_q */
447 num_aq = kni_fifo_count(kni->alloc_q);
449 /* Get the number of free entries in free_q */
450 num_fq = kni_fifo_free_count(kni->free_q);
452 /* Calculate the number of entries to be dequeued from rx_q */
453 num = min(num_rq, num_tq);
454 num = min(num, num_aq);
455 num = min(num, num_fq);
456 num = min_t(uint32_t, num, MBUF_BURST_SZ);
458 /* Return if no entry to dequeue from rx_q */
462 /* Burst dequeue from rx_q */
463 ret = kni_fifo_get(kni->rx_q, kni->pa, num);
465 return; /* Failing should not happen */
467 /* Dequeue entries from alloc_q */
468 ret = kni_fifo_get(kni->alloc_q, kni->alloc_pa, num);
472 for (i = 0; i < num; i++) {
473 kva = get_kva(kni, kni->pa[i]);
475 data_kva = get_data_kva(kni, kva);
476 kni->va[i] = pa2va(kni->pa[i], kva);
479 next_kva = pa2kva(kva->next);
480 /* Convert physical address to virtual address */
481 kva->next = pa2va(kva->next, next_kva);
485 alloc_kva = get_kva(kni, kni->alloc_pa[i]);
486 alloc_data_kva = get_data_kva(kni, alloc_kva);
487 kni->alloc_va[i] = pa2va(kni->alloc_pa[i], alloc_kva);
489 memcpy(alloc_data_kva, data_kva, len);
490 alloc_kva->pkt_len = len;
491 alloc_kva->data_len = len;
493 dev->stats.tx_bytes += len;
494 dev->stats.rx_bytes += len;
497 /* Burst enqueue mbufs into tx_q */
498 ret = kni_fifo_put(kni->tx_q, kni->alloc_va, num);
500 /* Failing should not happen */
501 pr_err("Fail to enqueue mbufs into tx_q\n");
504 /* Burst enqueue mbufs into free_q */
505 ret = kni_fifo_put(kni->free_q, kni->va, num);
507 /* Failing should not happen */
508 pr_err("Fail to enqueue mbufs into free_q\n");
511 * Update statistic, and enqueue/dequeue failure is impossible,
512 * as all queues are checked at first.
514 dev->stats.tx_packets += num;
515 dev->stats.rx_packets += num;
519 * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
522 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
526 uint32_t i, num_rq, num_fq, num;
527 struct rte_kni_mbuf *kva, *prev_kva;
530 struct net_device *dev = kni->net_dev;
532 /* Get the number of entries in rx_q */
533 num_rq = kni_fifo_count(kni->rx_q);
535 /* Get the number of free entries in free_q */
536 num_fq = kni_fifo_free_count(kni->free_q);
538 /* Calculate the number of entries to dequeue from rx_q */
539 num = min(num_rq, num_fq);
540 num = min_t(uint32_t, num, MBUF_BURST_SZ);
542 /* Return if no entry to dequeue from rx_q */
546 /* Burst dequeue mbufs from rx_q */
547 ret = kni_fifo_get(kni->rx_q, kni->pa, num);
551 /* Copy mbufs to sk buffer and then call tx interface */
552 for (i = 0; i < num; i++) {
553 kva = get_kva(kni, kni->pa[i]);
555 data_kva = get_data_kva(kni, kva);
556 kni->va[i] = pa2va(kni->pa[i], kva);
558 skb = netdev_alloc_skb(dev, len);
560 memcpy(skb_put(skb, len), data_kva, len);
561 skb->ip_summed = CHECKSUM_UNNECESSARY;
565 /* Simulate real usage, allocate/copy skb twice */
566 skb = netdev_alloc_skb(dev, len);
568 dev->stats.rx_dropped++;
572 if (kva->nb_segs == 1) {
573 memcpy(skb_put(skb, len), data_kva, len);
576 int kva_nb_segs = kva->nb_segs;
578 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
579 memcpy(skb_put(skb, kva->data_len),
580 data_kva, kva->data_len);
586 kva = get_kva(kni, kva->next);
587 data_kva = get_data_kva(kni, kva);
588 /* Convert physical address to virtual address */
589 prev_kva->next = pa2va(prev_kva->next, kva);
593 skb->ip_summed = CHECKSUM_UNNECESSARY;
595 dev->stats.rx_bytes += len;
596 dev->stats.rx_packets++;
598 /* call tx interface */
599 kni_net_tx(skb, dev);
602 /* enqueue all the mbufs from rx_q into free_q */
603 ret = kni_fifo_put(kni->free_q, kni->va, num);
605 /* Failing should not happen */
606 pr_err("Fail to enqueue mbufs into free_q\n");
611 kni_net_rx(struct kni_dev *kni)
614 * It doesn't need to check if it is NULL pointer,
615 * as it has a default value
617 (*kni_net_rx_func)(kni);
621 * Deal with a transmit timeout.
623 #ifdef HAVE_TX_TIMEOUT_TXQUEUE
625 kni_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
628 kni_net_tx_timeout(struct net_device *dev)
631 pr_debug("Transmit timeout at %ld, latency %ld\n", jiffies,
632 jiffies - dev_trans_start(dev));
634 dev->stats.tx_errors++;
635 netif_wake_queue(dev);
639 kni_net_change_mtu(struct net_device *dev, int new_mtu)
642 struct rte_kni_request req;
644 pr_debug("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
646 memset(&req, 0, sizeof(req));
647 req.req_id = RTE_KNI_REQ_CHANGE_MTU;
648 req.new_mtu = new_mtu;
649 ret = kni_net_process_request(dev, &req);
650 if (ret == 0 && req.result == 0)
653 return (ret == 0) ? req.result : ret;
657 kni_net_change_rx_flags(struct net_device *netdev, int flags)
659 struct rte_kni_request req;
661 memset(&req, 0, sizeof(req));
663 if (flags & IFF_ALLMULTI) {
664 req.req_id = RTE_KNI_REQ_CHANGE_ALLMULTI;
666 if (netdev->flags & IFF_ALLMULTI)
672 if (flags & IFF_PROMISC) {
673 req.req_id = RTE_KNI_REQ_CHANGE_PROMISC;
675 if (netdev->flags & IFF_PROMISC)
676 req.promiscusity = 1;
678 req.promiscusity = 0;
681 kni_net_process_request(netdev, &req);
685 * Checks if the user space application provided the resp message
688 kni_net_poll_resp(struct kni_dev *kni)
690 if (kni_fifo_count(kni->resp_q))
691 wake_up_interruptible(&kni->wq);
695 * Fill the eth header
698 kni_net_header(struct sk_buff *skb, struct net_device *dev,
699 unsigned short type, const void *daddr,
700 const void *saddr, uint32_t len)
702 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
704 memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
705 memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
706 eth->h_proto = htons(type);
708 return dev->hard_header_len;
712 * Re-fill the eth header
714 #ifdef HAVE_REBUILD_HEADER
716 kni_net_rebuild_header(struct sk_buff *skb)
718 struct net_device *dev = skb->dev;
719 struct ethhdr *eth = (struct ethhdr *) skb->data;
721 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
722 memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
729 * kni_net_set_mac - Change the Ethernet Address of the KNI NIC
730 * @netdev: network interface device structure
731 * @p: pointer to an address structure
733 * Returns 0 on success, negative on failure
736 kni_net_set_mac(struct net_device *netdev, void *p)
739 struct rte_kni_request req;
740 struct sockaddr *addr = p;
742 memset(&req, 0, sizeof(req));
743 req.req_id = RTE_KNI_REQ_CHANGE_MAC_ADDR;
745 if (!is_valid_ether_addr((unsigned char *)(addr->sa_data)))
746 return -EADDRNOTAVAIL;
748 memcpy(req.mac_addr, addr->sa_data, netdev->addr_len);
749 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
751 ret = kni_net_process_request(netdev, &req);
753 return (ret == 0 ? req.result : ret);
756 #ifdef HAVE_CHANGE_CARRIER_CB
758 kni_net_change_carrier(struct net_device *dev, bool new_carrier)
761 netif_carrier_on(dev);
763 netif_carrier_off(dev);
768 static const struct header_ops kni_net_header_ops = {
769 .create = kni_net_header,
770 .parse = eth_header_parse,
771 #ifdef HAVE_REBUILD_HEADER
772 .rebuild = kni_net_rebuild_header,
774 .cache = NULL, /* disable caching */
777 static const struct net_device_ops kni_net_netdev_ops = {
778 .ndo_open = kni_net_open,
779 .ndo_stop = kni_net_release,
780 .ndo_set_config = kni_net_config,
781 .ndo_change_rx_flags = kni_net_change_rx_flags,
782 .ndo_start_xmit = kni_net_tx,
783 .ndo_change_mtu = kni_net_change_mtu,
784 .ndo_tx_timeout = kni_net_tx_timeout,
785 .ndo_set_mac_address = kni_net_set_mac,
786 #ifdef HAVE_CHANGE_CARRIER_CB
787 .ndo_change_carrier = kni_net_change_carrier,
791 static void kni_get_drvinfo(struct net_device *dev,
792 struct ethtool_drvinfo *info)
794 strlcpy(info->version, KNI_VERSION, sizeof(info->version));
795 strlcpy(info->driver, "kni", sizeof(info->driver));
798 static const struct ethtool_ops kni_net_ethtool_ops = {
799 .get_drvinfo = kni_get_drvinfo,
800 .get_link = ethtool_op_get_link,
804 kni_net_init(struct net_device *dev)
806 struct kni_dev *kni = netdev_priv(dev);
808 init_waitqueue_head(&kni->wq);
809 mutex_init(&kni->sync_lock);
811 ether_setup(dev); /* assign some of the fields */
812 dev->netdev_ops = &kni_net_netdev_ops;
813 dev->header_ops = &kni_net_header_ops;
814 dev->ethtool_ops = &kni_net_ethtool_ops;
815 dev->watchdog_timeo = WD_TIMEOUT;
819 kni_net_config_lo_mode(char *lo_str)
822 pr_debug("loopback disabled");
826 if (!strcmp(lo_str, "lo_mode_none"))
827 pr_debug("loopback disabled");
828 else if (!strcmp(lo_str, "lo_mode_fifo")) {
829 pr_debug("loopback mode=lo_mode_fifo enabled");
830 kni_net_rx_func = kni_net_rx_lo_fifo;
831 } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
832 pr_debug("loopback mode=lo_mode_fifo_skb enabled");
833 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
835 pr_debug("Unknown loopback parameter, disabled");