kni: update kernel logging
[dpdk.git] / lib / librte_eal / linuxapp / kni / kni_net.c
index e02edcb..6328e1e 100644 (file)
 
 #include <exec-env/rte_kni_common.h>
 #include <kni_fifo.h>
+
+#include "compat.h"
 #include "kni_dev.h"
 
 #define WD_TIMEOUT 5 /*jiffies */
 
-#define MBUF_BURST_SZ 32
-
 #define KNI_WAIT_RESPONSE_TIMEOUT 300 /* 3 seconds */
 
 /* typedef for rx function */
@@ -59,6 +59,44 @@ static int kni_net_process_request(struct kni_dev *kni,
 /* kni rx function pointer, with default to normal rx */
 static kni_net_rx_t kni_net_rx_func = kni_net_rx_normal;
 
+/* physical address to kernel virtual address */
+static void *
+pa2kva(void *pa)
+{
+       return phys_to_virt((unsigned long)pa);
+}
+
+/* physical address to virtual address */
+static void *
+pa2va(void *pa, struct rte_kni_mbuf *m)
+{
+       void *va;
+
+       va = (void *)((unsigned long)pa +
+                       (unsigned long)m->buf_addr -
+                       (unsigned long)m->buf_physaddr);
+       return va;
+}
+
+/* mbuf data kernel virtual address from mbuf kernel virtual address */
+static void *
+kva2data_kva(struct rte_kni_mbuf *m)
+{
+       return phys_to_virt(m->buf_physaddr + m->data_off);
+}
+
+/* virtual address to physical address */
+static void *
+va2pa(void *va, struct rte_kni_mbuf *m)
+{
+       void *pa;
+
+       pa = (void *)((unsigned long)va -
+                       ((unsigned long)m->buf_addr -
+                        (unsigned long)m->buf_physaddr));
+       return pa;
+}
+
 /*
  * Open and close
  */
@@ -69,15 +107,6 @@ kni_net_open(struct net_device *dev)
        struct rte_kni_request req;
        struct kni_dev *kni = netdev_priv(dev);
 
-       if (kni->lad_dev)
-               memcpy(dev->dev_addr, kni->lad_dev->dev_addr, ETH_ALEN);
-       else
-               /*
-                * Generate random mac address. eth_random_addr() is the newer
-                * version of generating mac address in linux kernel.
-                */
-               random_ether_addr(dev->dev_addr);
-
        netif_start_queue(dev);
 
        memset(&req, 0, sizeof(req));
@@ -128,13 +157,11 @@ kni_net_config(struct net_device *dev, struct ifmap *map)
 static void
 kni_net_rx_normal(struct kni_dev *kni)
 {
-       unsigned ret;
+       unsigned int ret;
        uint32_t len;
-       unsigned i, num_rx, num_fq;
+       unsigned int i, num_rx, num_fq;
        struct rte_kni_mbuf *kva;
-       struct rte_kni_mbuf *va[MBUF_BURST_SZ];
-       void * data_kva;
-
+       void *data_kva;
        struct sk_buff *skb;
        struct net_device *dev = kni->net_dev;
 
@@ -146,48 +173,66 @@ kni_net_rx_normal(struct kni_dev *kni)
        }
 
        /* Calculate the number of entries to dequeue from rx_q */
-       num_rx = min(num_fq, (unsigned)MBUF_BURST_SZ);
+       num_rx = min_t(unsigned int, num_fq, MBUF_BURST_SZ);
 
        /* Burst dequeue from rx_q */
-       num_rx = kni_fifo_get(kni->rx_q, (void **)va, num_rx);
+       num_rx = kni_fifo_get(kni->rx_q, kni->pa, num_rx);
        if (num_rx == 0)
                return;
 
        /* Transfer received packets to netif */
        for (i = 0; i < num_rx; i++) {
-               kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
-               len = kva->data_len;
-               data_kva = kva->buf_addr + kva->data_off - kni->mbuf_va
-                               + kni->mbuf_kva;
+               kva = pa2kva(kni->pa[i]);
+               len = kva->pkt_len;
+               data_kva = kva2data_kva(kva);
+               kni->va[i] = pa2va(kni->pa[i], kva);
 
                skb = dev_alloc_skb(len + 2);
                if (!skb) {
-                       KNI_ERR("Out of mem, dropping pkts\n");
+                       pr_err("Out of mem, dropping pkts\n");
                        /* Update statistics */
                        kni->stats.rx_dropped++;
+                       continue;
                }
-               else {
-                       /* Align IP on 16B boundary */
-                       skb_reserve(skb, 2);
+
+               /* Align IP on 16B boundary */
+               skb_reserve(skb, 2);
+
+               if (kva->nb_segs == 1) {
                        memcpy(skb_put(skb, len), data_kva, len);
-                       skb->dev = dev;
-                       skb->protocol = eth_type_trans(skb, dev);
-                       skb->ip_summed = CHECKSUM_UNNECESSARY;
+               } else {
+                       int nb_segs;
+                       int kva_nb_segs = kva->nb_segs;
 
-                       /* Call netif interface */
-                       netif_rx(skb);
+                       for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
+                               memcpy(skb_put(skb, kva->data_len),
+                                       data_kva, kva->data_len);
 
-                       /* Update statistics */
-                       kni->stats.rx_bytes += len;
-                       kni->stats.rx_packets++;
+                               if (!kva->next)
+                                       break;
+
+                               kva = pa2kva(va2pa(kva->next, kva));
+                               data_kva = kva2data_kva(kva);
+                       }
                }
+
+               skb->dev = dev;
+               skb->protocol = eth_type_trans(skb, dev);
+               skb->ip_summed = CHECKSUM_UNNECESSARY;
+
+               /* Call netif interface */
+               netif_rx_ni(skb);
+
+               /* Update statistics */
+               kni->stats.rx_bytes += len;
+               kni->stats.rx_packets++;
        }
 
        /* Burst enqueue mbufs into free_q */
-       ret = kni_fifo_put(kni->free_q, (void **)va, num_rx);
+       ret = kni_fifo_put(kni->free_q, kni->va, num_rx);
        if (ret != num_rx)
                /* Failing should not happen */
-               KNI_ERR("Fail to enqueue entries into free_q\n");
+               pr_err("Fail to enqueue entries into free_q\n");
 }
 
 /*
@@ -196,15 +241,12 @@ kni_net_rx_normal(struct kni_dev *kni)
 static void
 kni_net_rx_lo_fifo(struct kni_dev *kni)
 {
-       unsigned ret;
+       unsigned int ret;
        uint32_t len;
-       unsigned i, num, num_rq, num_tq, num_aq, num_fq;
+       unsigned int i, num, num_rq, num_tq, num_aq, num_fq;
        struct rte_kni_mbuf *kva;
-       struct rte_kni_mbuf *va[MBUF_BURST_SZ];
-       void * data_kva;
-
+       void *data_kva;
        struct rte_kni_mbuf *alloc_kva;
-       struct rte_kni_mbuf *alloc_va[MBUF_BURST_SZ];
        void *alloc_data_kva;
 
        /* Get the number of entries in rx_q */
@@ -223,33 +265,32 @@ kni_net_rx_lo_fifo(struct kni_dev *kni)
        num = min(num_rq, num_tq);
        num = min(num, num_aq);
        num = min(num, num_fq);
-       num = min(num, (unsigned)MBUF_BURST_SZ);
+       num = min_t(unsigned int, num, MBUF_BURST_SZ);
 
        /* Return if no entry to dequeue from rx_q */
        if (num == 0)
                return;
 
        /* Burst dequeue from rx_q */
-       ret = kni_fifo_get(kni->rx_q, (void **)va, num);
+       ret = kni_fifo_get(kni->rx_q, kni->pa, num);
        if (ret == 0)
                return; /* Failing should not happen */
 
        /* Dequeue entries from alloc_q */
-       ret = kni_fifo_get(kni->alloc_q, (void **)alloc_va, num);
+       ret = kni_fifo_get(kni->alloc_q, kni->alloc_pa, num);
        if (ret) {
                num = ret;
                /* Copy mbufs */
                for (i = 0; i < num; i++) {
-                       kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
+                       kva = pa2kva(kni->pa[i]);
                        len = kva->pkt_len;
-                       data_kva = kva->buf_addr + kva->data_off -
-                                       kni->mbuf_va + kni->mbuf_kva;
-
-                       alloc_kva = (void *)alloc_va[i] - kni->mbuf_va +
-                                                       kni->mbuf_kva;
-                       alloc_data_kva = alloc_kva->buf_addr +
-                                       alloc_kva->data_off - kni->mbuf_va +
-                                                       kni->mbuf_kva;
+                       data_kva = kva2data_kva(kva);
+                       kni->va[i] = pa2va(kni->pa[i], kva);
+
+                       alloc_kva = pa2kva(kni->alloc_pa[i]);
+                       alloc_data_kva = kva2data_kva(alloc_kva);
+                       kni->alloc_va[i] = pa2va(kni->alloc_pa[i], alloc_kva);
+
                        memcpy(alloc_data_kva, data_kva, len);
                        alloc_kva->pkt_len = len;
                        alloc_kva->data_len = len;
@@ -259,17 +300,17 @@ kni_net_rx_lo_fifo(struct kni_dev *kni)
                }
 
                /* Burst enqueue mbufs into tx_q */
-               ret = kni_fifo_put(kni->tx_q, (void **)alloc_va, num);
+               ret = kni_fifo_put(kni->tx_q, kni->alloc_va, num);
                if (ret != num)
                        /* Failing should not happen */
-                       KNI_ERR("Fail to enqueue mbufs into tx_q\n");
+                       pr_err("Fail to enqueue mbufs into tx_q\n");
        }
 
        /* Burst enqueue mbufs into free_q */
-       ret = kni_fifo_put(kni->free_q, (void **)va, num);
+       ret = kni_fifo_put(kni->free_q, kni->va, num);
        if (ret != num)
                /* Failing should not happen */
-               KNI_ERR("Fail to enqueue mbufs into free_q\n");
+               pr_err("Fail to enqueue mbufs into free_q\n");
 
        /**
         * Update statistic, and enqueue/dequeue failure is impossible,
@@ -285,13 +326,11 @@ kni_net_rx_lo_fifo(struct kni_dev *kni)
 static void
 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
 {
-       unsigned ret;
+       unsigned int ret;
        uint32_t len;
-       unsigned i, num_rq, num_fq, num;
+       unsigned int i, num_rq, num_fq, num;
        struct rte_kni_mbuf *kva;
-       struct rte_kni_mbuf *va[MBUF_BURST_SZ];
-       void * data_kva;
-
+       void *data_kva;
        struct sk_buff *skb;
        struct net_device *dev = kni->net_dev;
 
@@ -303,27 +342,27 @@ kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
 
        /* Calculate the number of entries to dequeue from rx_q */
        num = min(num_rq, num_fq);
-       num = min(num, (unsigned)MBUF_BURST_SZ);
+       num = min_t(unsigned int, num, MBUF_BURST_SZ);
 
        /* Return if no entry to dequeue from rx_q */
        if (num == 0)
                return;
 
        /* Burst dequeue mbufs from rx_q */
-       ret = kni_fifo_get(kni->rx_q, (void **)va, num);
+       ret = kni_fifo_get(kni->rx_q, kni->pa, num);
        if (ret == 0)
                return;
 
        /* Copy mbufs to sk buffer and then call tx interface */
        for (i = 0; i < num; i++) {
-               kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
-               len = kva->data_len;
-               data_kva = kva->buf_addr + kva->data_off - kni->mbuf_va +
-                               kni->mbuf_kva;
+               kva = pa2kva(kni->pa[i]);
+               len = kva->pkt_len;
+               data_kva = kva2data_kva(kva);
+               kni->va[i] = pa2va(kni->pa[i], kva);
 
                skb = dev_alloc_skb(len + 2);
                if (skb == NULL)
-                       KNI_ERR("Out of mem, dropping pkts\n");
+                       pr_err("Out of mem, dropping pkts\n");
                else {
                        /* Align IP on 16B boundary */
                        skb_reserve(skb, 2);
@@ -336,29 +375,47 @@ kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
                /* Simulate real usage, allocate/copy skb twice */
                skb = dev_alloc_skb(len + 2);
                if (skb == NULL) {
-                       KNI_ERR("Out of mem, dropping pkts\n");
+                       pr_err("Out of mem, dropping pkts\n");
                        kni->stats.rx_dropped++;
+                       continue;
                }
-               else {
-                       /* Align IP on 16B boundary */
-                       skb_reserve(skb, 2);
+
+               /* Align IP on 16B boundary */
+               skb_reserve(skb, 2);
+
+               if (kva->nb_segs == 1) {
                        memcpy(skb_put(skb, len), data_kva, len);
-                       skb->dev = dev;
-                       skb->ip_summed = CHECKSUM_UNNECESSARY;
+               } else {
+                       int nb_segs;
+                       int kva_nb_segs = kva->nb_segs;
 
-                       kni->stats.rx_bytes += len;
-                       kni->stats.rx_packets++;
+                       for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
+                               memcpy(skb_put(skb, kva->data_len),
+                                       data_kva, kva->data_len);
+
+                               if (!kva->next)
+                                       break;
 
-                       /* call tx interface */
-                       kni_net_tx(skb, dev);
+                               kva = pa2kva(va2pa(kva->next, kva));
+                               data_kva = kva2data_kva(kva);
+                       }
                }
+
+               skb->dev = dev;
+               skb->ip_summed = CHECKSUM_UNNECESSARY;
+
+               kni->stats.rx_bytes += len;
+               kni->stats.rx_packets++;
+
+               /* call tx interface */
+               kni_net_tx(skb, dev);
        }
 
        /* enqueue all the mbufs from rx_q into free_q */
-       ret = kni_fifo_put(kni->free_q, (void **)&va, num);
+       ret = kni_fifo_put(kni->free_q, kni->va, num);
        if (ret != num)
                /* Failing should not happen */
-               KNI_ERR("Fail to enqueue mbufs into free_q\n");
+               pr_err("Fail to enqueue mbufs into free_q\n");
 }
 
 /* rx interface */
@@ -391,12 +448,18 @@ static int
 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
 {
        int len = 0;
-       unsigned ret;
+       unsigned int ret;
        struct kni_dev *kni = netdev_priv(dev);
        struct rte_kni_mbuf *pkt_kva = NULL;
-       struct rte_kni_mbuf *pkt_va = NULL;
+       void *pkt_pa = NULL;
+       void *pkt_va = NULL;
 
-       dev->trans_start = jiffies; /* save the timestamp */
+       /* save the timestamp */
+#ifdef HAVE_TRANS_START_HELPER
+       netif_trans_update(dev);
+#else
+       dev->trans_start = jiffies;
+#endif
 
        /* Check if the length of skb is less than mbuf size */
        if (skb->len > kni->mbuf_size)
@@ -416,13 +479,13 @@ kni_net_tx(struct sk_buff *skb, struct net_device *dev)
        }
 
        /* dequeue a mbuf from alloc_q */
-       ret = kni_fifo_get(kni->alloc_q, (void **)&pkt_va, 1);
+       ret = kni_fifo_get(kni->alloc_q, &pkt_pa, 1);
        if (likely(ret == 1)) {
                void *data_kva;
 
-               pkt_kva = (void *)pkt_va - kni->mbuf_va + kni->mbuf_kva;
-               data_kva = pkt_kva->buf_addr + pkt_kva->data_off - kni->mbuf_va
-                               + kni->mbuf_kva;
+               pkt_kva = pa2kva(pkt_pa);
+               data_kva = kva2data_kva(pkt_kva);
+               pkt_va = pa2va(pkt_pa, pkt_kva);
 
                len = skb->len;
                memcpy(data_kva, skb->data, len);
@@ -434,15 +497,15 @@ kni_net_tx(struct sk_buff *skb, struct net_device *dev)
                pkt_kva->data_len = len;
 
                /* enqueue mbuf into tx_q */
-               ret = kni_fifo_put(kni->tx_q, (void **)&pkt_va, 1);
+               ret = kni_fifo_put(kni->tx_q, &pkt_va, 1);
                if (unlikely(ret != 1)) {
                        /* Failing should not happen */
-                       KNI_ERR("Fail to enqueue mbuf into tx_q\n");
+                       pr_err("Fail to enqueue mbuf into tx_q\n");
                        goto drop;
                }
        } else {
                /* Failing should not happen */
-               KNI_ERR("Fail to dequeue mbuf from alloc_q\n");
+               pr_err("Fail to dequeue mbuf from alloc_q\n");
                goto drop;
        }
 
@@ -466,16 +529,15 @@ drop:
  * Deal with a transmit timeout.
  */
 static void
-kni_net_tx_timeout (struct net_device *dev)
+kni_net_tx_timeout(struct net_device *dev)
 {
        struct kni_dev *kni = netdev_priv(dev);
 
        KNI_DBG("Transmit timeout at %ld, latency %ld\n", jiffies,
-                       jiffies - dev->trans_start);
+                       jiffies - dev_trans_start(dev));
 
        kni->stats.tx_errors++;
        netif_wake_queue(dev);
-       return;
 }
 
 /*
@@ -532,11 +594,11 @@ kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
 {
        int ret = -1;
        void *resp_va;
-       unsigned num;
+       unsigned int num;
        int ret_val;
 
        if (!kni || !req) {
-               KNI_ERR("No kni instance or request\n");
+               pr_err("No kni instance or request\n");
                return -EINVAL;
        }
 
@@ -546,7 +608,7 @@ kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
        memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
        num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
        if (num < 1) {
-               KNI_ERR("Cannot send to req_q\n");
+               pr_err("Cannot send to req_q\n");
                ret = -EBUSY;
                goto fail;
        }
@@ -560,7 +622,7 @@ kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
        num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
        if (num != 1 || resp_va != kni->sync_va) {
                /* This should never happen */
-               KNI_ERR("No data in resp_q\n");
+               pr_err("No data in resp_q\n");
                ret = -ENODATA;
                goto fail;
        }
@@ -580,6 +642,7 @@ static struct net_device_stats *
 kni_net_stats(struct net_device *dev)
 {
        struct kni_dev *kni = netdev_priv(dev);
+
        return &kni->stats;
 }
 
@@ -600,11 +663,10 @@ kni_net_header(struct sk_buff *skb, struct net_device *dev,
        return dev->hard_header_len;
 }
 
-
 /*
  * Re-fill the eth header
  */
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0))
+#ifdef HAVE_REBUILD_HEADER
 static int
 kni_net_rebuild_header(struct sk_buff *skb)
 {
@@ -625,17 +687,20 @@ kni_net_rebuild_header(struct sk_buff *skb)
  *
  * Returns 0 on success, negative on failure
  **/
-static int kni_net_set_mac(struct net_device *netdev, void *p)
+static int
+kni_net_set_mac(struct net_device *netdev, void *p)
 {
        struct sockaddr *addr = p;
+
        if (!is_valid_ether_addr((unsigned char *)(addr->sa_data)))
                return -EADDRNOTAVAIL;
        memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
        return 0;
 }
 
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
-static int kni_net_change_carrier(struct net_device *dev, bool new_carrier)
+#ifdef HAVE_CHANGE_CARRIER_CB
+static int
+kni_net_change_carrier(struct net_device *dev, bool new_carrier)
 {
        if (new_carrier)
                netif_carrier_on(dev);
@@ -647,7 +712,7 @@ static int kni_net_change_carrier(struct net_device *dev, bool new_carrier)
 
 static const struct header_ops kni_net_header_ops = {
        .create  = kni_net_header,
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0))
+#ifdef HAVE_REBUILD_HEADER
        .rebuild = kni_net_rebuild_header,
 #endif /* < 4.1.0  */
        .cache   = NULL,  /* disable caching */
@@ -664,7 +729,7 @@ static const struct net_device_ops kni_net_netdev_ops = {
        .ndo_get_stats = kni_net_stats,
        .ndo_tx_timeout = kni_net_tx_timeout,
        .ndo_set_mac_address = kni_net_set_mac,
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
+#ifdef HAVE_CHANGE_CARRIER_CB
        .ndo_change_carrier = kni_net_change_carrier,
 #endif
 };
@@ -689,18 +754,18 @@ void
 kni_net_config_lo_mode(char *lo_str)
 {
        if (!lo_str) {
-               KNI_PRINT("loopback disabled");
+               pr_debug("loopback disabled");
                return;
        }
 
        if (!strcmp(lo_str, "lo_mode_none"))
-               KNI_PRINT("loopback disabled");
+               pr_debug("loopback disabled");
        else if (!strcmp(lo_str, "lo_mode_fifo")) {
-               KNI_PRINT("loopback mode=lo_mode_fifo enabled");
+               pr_debug("loopback mode=lo_mode_fifo enabled");
                kni_net_rx_func = kni_net_rx_lo_fifo;
        } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
-               KNI_PRINT("loopback mode=lo_mode_fifo_skb enabled");
+               pr_debug("loopback mode=lo_mode_fifo_skb enabled");
                kni_net_rx_func = kni_net_rx_lo_fifo_skb;
        } else
-               KNI_PRINT("Incognizant parameter, loopback disabled");
+               pr_debug("Incognizant parameter, loopback disabled");
 }