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 /* No result available since request is handled
128 * asynchronously. set response to success.
130 if (req->async != 0) {
135 ret_val = wait_event_interruptible_timeout(kni->wq,
136 kni_fifo_count(kni->resp_q), 3 * HZ);
137 if (signal_pending(current) || ret_val <= 0) {
141 num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
142 if (num != 1 || resp_va != kni->sync_va) {
143 /* This should never happen */
144 pr_err("No data in resp_q\n");
149 memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
154 mutex_unlock(&kni->sync_lock);
162 kni_net_open(struct net_device *dev)
165 struct rte_kni_request req;
167 netif_start_queue(dev);
168 if (kni_dflt_carrier == 1)
169 netif_carrier_on(dev);
171 netif_carrier_off(dev);
173 memset(&req, 0, sizeof(req));
174 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
176 /* Setting if_up to non-zero means up */
178 ret = kni_net_process_request(dev, &req);
180 return (ret == 0) ? req.result : ret;
184 kni_net_release(struct net_device *dev)
187 struct rte_kni_request req;
189 netif_stop_queue(dev); /* can't transmit any more */
190 netif_carrier_off(dev);
192 memset(&req, 0, sizeof(req));
193 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
195 /* Setting if_up to 0 means down */
197 ret = kni_net_process_request(dev, &req);
199 return (ret == 0) ? req.result : ret;
203 kni_fifo_trans_pa2va(struct kni_dev *kni,
204 struct rte_kni_fifo *src_pa, struct rte_kni_fifo *dst_va)
206 uint32_t ret, i, num_dst, num_rx;
207 struct rte_kni_mbuf *kva, *prev_kva;
212 num_dst = kni_fifo_free_count(dst_va);
216 num_rx = min_t(uint32_t, num_dst, MBUF_BURST_SZ);
218 num_rx = kni_fifo_get(src_pa, kni->pa, num_rx);
222 for (i = 0; i < num_rx; i++) {
223 kva = get_kva(kni, kni->pa[i]);
224 kni->va[i] = pa2va(kni->pa[i], kva);
226 kva_nb_segs = kva->nb_segs;
227 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
232 kva = pa2kva(kva->next);
233 /* Convert physical address to virtual address */
234 prev_kva->next = pa2va(prev_kva->next, kva);
238 ret = kni_fifo_put(dst_va, kni->va, num_rx);
240 /* Failing should not happen */
241 pr_err("Fail to enqueue entries into dst_va\n");
247 /* Try to release mbufs when kni release */
248 void kni_net_release_fifo_phy(struct kni_dev *kni)
250 /* release rx_q first, because it can't release in userspace */
251 kni_fifo_trans_pa2va(kni, kni->rx_q, kni->free_q);
252 /* release alloc_q for speeding up kni release in userspace */
253 kni_fifo_trans_pa2va(kni, kni->alloc_q, kni->free_q);
257 * Configuration changes (passed on by ifconfig)
260 kni_net_config(struct net_device *dev, struct ifmap *map)
262 if (dev->flags & IFF_UP) /* can't act on a running interface */
265 /* ignore other fields */
270 * Transmit a packet (called by the kernel)
273 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
277 struct kni_dev *kni = netdev_priv(dev);
278 struct rte_kni_mbuf *pkt_kva = NULL;
282 /* save the timestamp */
283 #ifdef HAVE_TRANS_START_HELPER
284 netif_trans_update(dev);
286 dev->trans_start = jiffies;
289 /* Check if the length of skb is less than mbuf size */
290 if (skb->len > kni->mbuf_size)
294 * Check if it has at least one free entry in tx_q and
295 * one entry in alloc_q.
297 if (kni_fifo_free_count(kni->tx_q) == 0 ||
298 kni_fifo_count(kni->alloc_q) == 0) {
300 * If no free entry in tx_q or no entry in alloc_q,
301 * drops skb and goes out.
306 /* dequeue a mbuf from alloc_q */
307 ret = kni_fifo_get(kni->alloc_q, &pkt_pa, 1);
308 if (likely(ret == 1)) {
311 pkt_kva = get_kva(kni, pkt_pa);
312 data_kva = get_data_kva(kni, pkt_kva);
313 pkt_va = pa2va(pkt_pa, pkt_kva);
316 memcpy(data_kva, skb->data, len);
317 if (unlikely(len < ETH_ZLEN)) {
318 memset(data_kva + len, 0, ETH_ZLEN - len);
321 pkt_kva->pkt_len = len;
322 pkt_kva->data_len = len;
324 /* enqueue mbuf into tx_q */
325 ret = kni_fifo_put(kni->tx_q, &pkt_va, 1);
326 if (unlikely(ret != 1)) {
327 /* Failing should not happen */
328 pr_err("Fail to enqueue mbuf into tx_q\n");
332 /* Failing should not happen */
333 pr_err("Fail to dequeue mbuf from alloc_q\n");
337 /* Free skb and update statistics */
339 dev->stats.tx_bytes += len;
340 dev->stats.tx_packets++;
345 /* Free skb and update statistics */
347 dev->stats.tx_dropped++;
353 * RX: normal working mode
356 kni_net_rx_normal(struct kni_dev *kni)
360 uint32_t i, num_rx, num_fq;
361 struct rte_kni_mbuf *kva, *prev_kva;
364 struct net_device *dev = kni->net_dev;
366 /* Get the number of free entries in free_q */
367 num_fq = kni_fifo_free_count(kni->free_q);
369 /* No room on the free_q, bail out */
373 /* Calculate the number of entries to dequeue from rx_q */
374 num_rx = min_t(uint32_t, num_fq, MBUF_BURST_SZ);
376 /* Burst dequeue from rx_q */
377 num_rx = kni_fifo_get(kni->rx_q, kni->pa, num_rx);
381 /* Transfer received packets to netif */
382 for (i = 0; i < num_rx; i++) {
383 kva = get_kva(kni, kni->pa[i]);
385 data_kva = get_data_kva(kni, kva);
386 kni->va[i] = pa2va(kni->pa[i], kva);
388 skb = netdev_alloc_skb(dev, len);
390 /* Update statistics */
391 dev->stats.rx_dropped++;
395 if (kva->nb_segs == 1) {
396 memcpy(skb_put(skb, len), data_kva, len);
399 int kva_nb_segs = kva->nb_segs;
401 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
402 memcpy(skb_put(skb, kva->data_len),
403 data_kva, kva->data_len);
409 kva = pa2kva(kva->next);
410 data_kva = kva2data_kva(kva);
411 /* Convert physical address to virtual address */
412 prev_kva->next = pa2va(prev_kva->next, kva);
416 skb->protocol = eth_type_trans(skb, dev);
417 skb->ip_summed = CHECKSUM_UNNECESSARY;
419 /* Call netif interface */
422 /* Update statistics */
423 dev->stats.rx_bytes += len;
424 dev->stats.rx_packets++;
427 /* Burst enqueue mbufs into free_q */
428 ret = kni_fifo_put(kni->free_q, kni->va, num_rx);
430 /* Failing should not happen */
431 pr_err("Fail to enqueue entries into free_q\n");
435 * RX: loopback with enqueue/dequeue fifos.
438 kni_net_rx_lo_fifo(struct kni_dev *kni)
442 uint32_t i, num, num_rq, num_tq, num_aq, num_fq;
443 struct rte_kni_mbuf *kva, *next_kva;
445 struct rte_kni_mbuf *alloc_kva;
446 void *alloc_data_kva;
447 struct net_device *dev = kni->net_dev;
449 /* Get the number of entries in rx_q */
450 num_rq = kni_fifo_count(kni->rx_q);
452 /* Get the number of free entries in tx_q */
453 num_tq = kni_fifo_free_count(kni->tx_q);
455 /* Get the number of entries in alloc_q */
456 num_aq = kni_fifo_count(kni->alloc_q);
458 /* Get the number of free entries in free_q */
459 num_fq = kni_fifo_free_count(kni->free_q);
461 /* Calculate the number of entries to be dequeued from rx_q */
462 num = min(num_rq, num_tq);
463 num = min(num, num_aq);
464 num = min(num, num_fq);
465 num = min_t(uint32_t, num, MBUF_BURST_SZ);
467 /* Return if no entry to dequeue from rx_q */
471 /* Burst dequeue from rx_q */
472 ret = kni_fifo_get(kni->rx_q, kni->pa, num);
474 return; /* Failing should not happen */
476 /* Dequeue entries from alloc_q */
477 ret = kni_fifo_get(kni->alloc_q, kni->alloc_pa, num);
481 for (i = 0; i < num; i++) {
482 kva = get_kva(kni, kni->pa[i]);
484 data_kva = get_data_kva(kni, kva);
485 kni->va[i] = pa2va(kni->pa[i], kva);
488 next_kva = pa2kva(kva->next);
489 /* Convert physical address to virtual address */
490 kva->next = pa2va(kva->next, next_kva);
494 alloc_kva = get_kva(kni, kni->alloc_pa[i]);
495 alloc_data_kva = get_data_kva(kni, alloc_kva);
496 kni->alloc_va[i] = pa2va(kni->alloc_pa[i], alloc_kva);
498 memcpy(alloc_data_kva, data_kva, len);
499 alloc_kva->pkt_len = len;
500 alloc_kva->data_len = len;
502 dev->stats.tx_bytes += len;
503 dev->stats.rx_bytes += len;
506 /* Burst enqueue mbufs into tx_q */
507 ret = kni_fifo_put(kni->tx_q, kni->alloc_va, num);
509 /* Failing should not happen */
510 pr_err("Fail to enqueue mbufs into tx_q\n");
513 /* Burst enqueue mbufs into free_q */
514 ret = kni_fifo_put(kni->free_q, kni->va, num);
516 /* Failing should not happen */
517 pr_err("Fail to enqueue mbufs into free_q\n");
520 * Update statistic, and enqueue/dequeue failure is impossible,
521 * as all queues are checked at first.
523 dev->stats.tx_packets += num;
524 dev->stats.rx_packets += num;
528 * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
531 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
535 uint32_t i, num_rq, num_fq, num;
536 struct rte_kni_mbuf *kva, *prev_kva;
539 struct net_device *dev = kni->net_dev;
541 /* Get the number of entries in rx_q */
542 num_rq = kni_fifo_count(kni->rx_q);
544 /* Get the number of free entries in free_q */
545 num_fq = kni_fifo_free_count(kni->free_q);
547 /* Calculate the number of entries to dequeue from rx_q */
548 num = min(num_rq, num_fq);
549 num = min_t(uint32_t, num, MBUF_BURST_SZ);
551 /* Return if no entry to dequeue from rx_q */
555 /* Burst dequeue mbufs from rx_q */
556 ret = kni_fifo_get(kni->rx_q, kni->pa, num);
560 /* Copy mbufs to sk buffer and then call tx interface */
561 for (i = 0; i < num; i++) {
562 kva = get_kva(kni, kni->pa[i]);
564 data_kva = get_data_kva(kni, kva);
565 kni->va[i] = pa2va(kni->pa[i], kva);
567 skb = netdev_alloc_skb(dev, len);
569 memcpy(skb_put(skb, len), data_kva, len);
570 skb->ip_summed = CHECKSUM_UNNECESSARY;
574 /* Simulate real usage, allocate/copy skb twice */
575 skb = netdev_alloc_skb(dev, len);
577 dev->stats.rx_dropped++;
581 if (kva->nb_segs == 1) {
582 memcpy(skb_put(skb, len), data_kva, len);
585 int kva_nb_segs = kva->nb_segs;
587 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
588 memcpy(skb_put(skb, kva->data_len),
589 data_kva, kva->data_len);
595 kva = get_kva(kni, kva->next);
596 data_kva = get_data_kva(kni, kva);
597 /* Convert physical address to virtual address */
598 prev_kva->next = pa2va(prev_kva->next, kva);
602 skb->ip_summed = CHECKSUM_UNNECESSARY;
604 dev->stats.rx_bytes += len;
605 dev->stats.rx_packets++;
607 /* call tx interface */
608 kni_net_tx(skb, dev);
611 /* enqueue all the mbufs from rx_q into free_q */
612 ret = kni_fifo_put(kni->free_q, kni->va, num);
614 /* Failing should not happen */
615 pr_err("Fail to enqueue mbufs into free_q\n");
620 kni_net_rx(struct kni_dev *kni)
623 * It doesn't need to check if it is NULL pointer,
624 * as it has a default value
626 (*kni_net_rx_func)(kni);
630 * Deal with a transmit timeout.
632 #ifdef HAVE_TX_TIMEOUT_TXQUEUE
634 kni_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
637 kni_net_tx_timeout(struct net_device *dev)
640 pr_debug("Transmit timeout at %ld, latency %ld\n", jiffies,
641 jiffies - dev_trans_start(dev));
643 dev->stats.tx_errors++;
644 netif_wake_queue(dev);
648 kni_net_change_mtu(struct net_device *dev, int new_mtu)
651 struct rte_kni_request req;
653 pr_debug("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
655 memset(&req, 0, sizeof(req));
656 req.req_id = RTE_KNI_REQ_CHANGE_MTU;
657 req.new_mtu = new_mtu;
658 ret = kni_net_process_request(dev, &req);
659 if (ret == 0 && req.result == 0)
662 return (ret == 0) ? req.result : ret;
666 kni_net_change_rx_flags(struct net_device *netdev, int flags)
668 struct rte_kni_request req;
670 memset(&req, 0, sizeof(req));
672 if (flags & IFF_ALLMULTI) {
673 req.req_id = RTE_KNI_REQ_CHANGE_ALLMULTI;
675 if (netdev->flags & IFF_ALLMULTI)
681 if (flags & IFF_PROMISC) {
682 req.req_id = RTE_KNI_REQ_CHANGE_PROMISC;
684 if (netdev->flags & IFF_PROMISC)
685 req.promiscusity = 1;
687 req.promiscusity = 0;
690 kni_net_process_request(netdev, &req);
694 * Checks if the user space application provided the resp message
697 kni_net_poll_resp(struct kni_dev *kni)
699 if (kni_fifo_count(kni->resp_q))
700 wake_up_interruptible(&kni->wq);
704 * Fill the eth header
707 kni_net_header(struct sk_buff *skb, struct net_device *dev,
708 unsigned short type, const void *daddr,
709 const void *saddr, uint32_t len)
711 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
713 memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
714 memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
715 eth->h_proto = htons(type);
717 return dev->hard_header_len;
721 * Re-fill the eth header
723 #ifdef HAVE_REBUILD_HEADER
725 kni_net_rebuild_header(struct sk_buff *skb)
727 struct net_device *dev = skb->dev;
728 struct ethhdr *eth = (struct ethhdr *) skb->data;
730 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
731 memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
738 * kni_net_set_mac - Change the Ethernet Address of the KNI NIC
739 * @netdev: network interface device structure
740 * @p: pointer to an address structure
742 * Returns 0 on success, negative on failure
745 kni_net_set_mac(struct net_device *netdev, void *p)
748 struct rte_kni_request req;
749 struct sockaddr *addr = p;
751 memset(&req, 0, sizeof(req));
752 req.req_id = RTE_KNI_REQ_CHANGE_MAC_ADDR;
754 if (!is_valid_ether_addr((unsigned char *)(addr->sa_data)))
755 return -EADDRNOTAVAIL;
757 memcpy(req.mac_addr, addr->sa_data, netdev->addr_len);
758 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
760 ret = kni_net_process_request(netdev, &req);
762 return (ret == 0 ? req.result : ret);
765 #ifdef HAVE_CHANGE_CARRIER_CB
767 kni_net_change_carrier(struct net_device *dev, bool new_carrier)
770 netif_carrier_on(dev);
772 netif_carrier_off(dev);
777 static const struct header_ops kni_net_header_ops = {
778 .create = kni_net_header,
779 .parse = eth_header_parse,
780 #ifdef HAVE_REBUILD_HEADER
781 .rebuild = kni_net_rebuild_header,
783 .cache = NULL, /* disable caching */
786 static const struct net_device_ops kni_net_netdev_ops = {
787 .ndo_open = kni_net_open,
788 .ndo_stop = kni_net_release,
789 .ndo_set_config = kni_net_config,
790 .ndo_change_rx_flags = kni_net_change_rx_flags,
791 .ndo_start_xmit = kni_net_tx,
792 .ndo_change_mtu = kni_net_change_mtu,
793 .ndo_tx_timeout = kni_net_tx_timeout,
794 .ndo_set_mac_address = kni_net_set_mac,
795 #ifdef HAVE_CHANGE_CARRIER_CB
796 .ndo_change_carrier = kni_net_change_carrier,
800 static void kni_get_drvinfo(struct net_device *dev,
801 struct ethtool_drvinfo *info)
803 strlcpy(info->version, KNI_VERSION, sizeof(info->version));
804 strlcpy(info->driver, "kni", sizeof(info->driver));
807 static const struct ethtool_ops kni_net_ethtool_ops = {
808 .get_drvinfo = kni_get_drvinfo,
809 .get_link = ethtool_op_get_link,
813 kni_net_init(struct net_device *dev)
815 struct kni_dev *kni = netdev_priv(dev);
817 init_waitqueue_head(&kni->wq);
818 mutex_init(&kni->sync_lock);
820 ether_setup(dev); /* assign some of the fields */
821 dev->netdev_ops = &kni_net_netdev_ops;
822 dev->header_ops = &kni_net_header_ops;
823 dev->ethtool_ops = &kni_net_ethtool_ops;
824 dev->watchdog_timeo = WD_TIMEOUT;
828 kni_net_config_lo_mode(char *lo_str)
831 pr_debug("loopback disabled");
835 if (!strcmp(lo_str, "lo_mode_none"))
836 pr_debug("loopback disabled");
837 else if (!strcmp(lo_str, "lo_mode_fifo")) {
838 pr_debug("loopback mode=lo_mode_fifo enabled");
839 kni_net_rx_func = kni_net_rx_lo_fifo;
840 } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
841 pr_debug("loopback mode=lo_mode_fifo_skb enabled");
842 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
844 pr_debug("Unknown loopback parameter, disabled");