kni: uninitialize global variables
[dpdk.git] / lib / librte_eal / linuxapp / kni / kni_misc.c
index 2c411e2..f051595 100644 (file)
  *   Intel Corporation
  */
 
+#include <linux/version.h>
 #include <linux/module.h>
 #include <linux/miscdevice.h>
 #include <linux/netdevice.h>
+#include <linux/etherdevice.h>
 #include <linux/pci.h>
 #include <linux/kthread.h>
 #include <linux/rwsem.h>
+#include <linux/mutex.h>
+#include <linux/nsproxy.h>
+#include <net/net_namespace.h>
+#include <net/netns/generic.h>
 
 #include <exec-env/rte_kni_common.h>
+
+#include "compat.h"
 #include "kni_dev.h"
-#include <rte_config.h>
 
 MODULE_LICENSE("Dual BSD/GPL");
 MODULE_AUTHOR("Intel Corporation");
@@ -41,16 +48,8 @@ MODULE_DESCRIPTION("Kernel Module for managing kni devices");
 
 #define KNI_MAX_DEVICES 32
 
-extern void kni_net_rx(struct kni_dev *kni);
-extern void kni_net_init(struct net_device *dev);
-extern void kni_net_config_lo_mode(char *lo_str);
-extern void kni_net_poll_resp(struct kni_dev *kni);
-extern void kni_set_ethtool_ops(struct net_device *netdev);
-
-extern int ixgbe_kni_probe(struct pci_dev *pdev, struct net_device **lad_dev);
-extern void ixgbe_kni_remove(struct pci_dev *pdev);
-extern int igb_kni_probe(struct pci_dev *pdev, struct net_device **lad_dev);
-extern void igb_kni_remove(struct pci_dev *pdev);
+extern const struct pci_device_id ixgbe_pci_tbl[];
+extern const struct pci_device_id igb_pci_tbl[];
 
 static int kni_open(struct inode *inode, struct file *file);
 static int kni_release(struct inode *inode, struct file *file);
@@ -82,26 +81,82 @@ static struct miscdevice kni_misc = {
 };
 
 /* loopback mode */
-static char *lo_mode = NULL;
+static char *lo_mode;
 
 /* Kernel thread mode */
-static char *kthread_mode = NULL;
-static unsigned multiple_kthread_on = 0;
+static char *kthread_mode;
+static unsigned int multiple_kthread_on;
 
 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
 
-static volatile unsigned long device_in_use; /* device in use flag */
-static struct task_struct *kni_kthread;
+static int kni_net_id;
+
+struct kni_net {
+       unsigned long device_in_use; /* device in use flag */
+       struct mutex kni_kthread_lock;
+       struct task_struct *kni_kthread;
+       struct rw_semaphore kni_list_lock;
+       struct list_head kni_list_head;
+};
+
+static int __net_init kni_init_net(struct net *net)
+{
+#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
+       struct kni_net *knet = net_generic(net, kni_net_id);
+       memset(knet, 0, sizeof(*knet));
+#else
+       struct kni_net *knet;
+       int ret;
+
+       knet = kzalloc(sizeof(struct kni_net), GFP_KERNEL);
+       if (!knet) {
+               ret = -ENOMEM;
+               return ret;
+       }
+#endif
+
+       /* Clear the bit of device in use */
+       clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
 
-/* kni list lock */
-static DECLARE_RWSEM(kni_list_lock);
+       mutex_init(&knet->kni_kthread_lock);
 
-/* kni list */
-static struct list_head kni_list_head = LIST_HEAD_INIT(kni_list_head);
+       init_rwsem(&knet->kni_list_lock);
+       INIT_LIST_HEAD(&knet->kni_list_head);
+
+#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
+       return 0;
+#else
+       ret = net_assign_generic(net, kni_net_id, knet);
+       if (ret < 0)
+               kfree(knet);
+
+       return ret;
+#endif
+}
+
+static void __net_exit kni_exit_net(struct net *net)
+{
+       struct kni_net *knet = net_generic(net, kni_net_id);
+       mutex_destroy(&knet->kni_kthread_lock);
+#ifndef HAVE_SIMPLIFIED_PERNET_OPERATIONS
+       kfree(knet);
+#endif
+}
+
+static struct pernet_operations kni_net_ops = {
+       .init = kni_init_net,
+       .exit = kni_exit_net,
+#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
+       .id   = &kni_net_id,
+       .size = sizeof(struct kni_net),
+#endif
+};
 
 static int __init
 kni_init(void)
 {
+       int rc;
+
        KNI_PRINT("######## DPDK kni module loading ########\n");
 
        if (kni_parse_kthread_mode() < 0) {
@@ -109,13 +164,24 @@ kni_init(void)
                return -EINVAL;
        }
 
-       if (misc_register(&kni_misc) != 0) {
-               KNI_ERR("Misc registration failed\n");
+       if (multiple_kthread_on == 0)
+               KNI_PRINT("Single kernel thread for all KNI devices\n");
+       else
+               KNI_PRINT("Multiple kernel thread mode enabled\n");
+
+#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
+       rc = register_pernet_subsys(&kni_net_ops);
+#else
+       rc = register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
+#endif
+       if (rc)
                return -EPERM;
-       }
 
-       /* Clear the bit of device in use */
-       clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
+       rc = misc_register(&kni_misc);
+       if (rc != 0) {
+               KNI_ERR("Misc registration failed\n");
+               goto out;
+       }
 
        /* Configure the lo mode according to the input parameter */
        kni_net_config_lo_mode(lo_mode);
@@ -123,12 +189,25 @@ kni_init(void)
        KNI_PRINT("######## DPDK kni module loaded  ########\n");
 
        return 0;
+
+out:
+#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
+       unregister_pernet_subsys(&kni_net_ops);
+#else
+       unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
+#endif
+       return rc;
 }
 
 static void __exit
 kni_exit(void)
 {
        misc_deregister(&kni_misc);
+#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
+       unregister_pernet_subsys(&kni_net_ops);
+#else
+       unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
+#endif
        KNI_PRINT("####### DPDK kni module unloaded  #######\n");
 }
 
@@ -151,23 +230,14 @@ kni_parse_kthread_mode(void)
 static int
 kni_open(struct inode *inode, struct file *file)
 {
-       /* kni device can be opened by one user only, test and set bit */
-       if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use))
-               return -EBUSY;
+       struct net *net = current->nsproxy->net_ns;
+       struct kni_net *knet = net_generic(net, kni_net_id);
 
-       /* Create kernel thread for single mode */
-       if (multiple_kthread_on == 0) {
-               KNI_PRINT("Single kernel thread for all KNI devices\n");
-               /* Create kernel thread for RX */
-               kni_kthread = kthread_run(kni_thread_single, NULL,
-                                               "kni_single");
-               if (IS_ERR(kni_kthread)) {
-                       KNI_ERR("Unable to create kernel threaed\n");
-                       return PTR_ERR(kni_kthread);
-               }
-       } else
-               KNI_PRINT("Multiple kernel thread mode enabled\n");
+       /* kni device can be opened by one user only per netns */
+       if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use))
+               return -EBUSY;
 
+       file->private_data = get_net(net);
        KNI_PRINT("/dev/kni opened\n");
 
        return 0;
@@ -176,17 +246,23 @@ kni_open(struct inode *inode, struct file *file)
 static int
 kni_release(struct inode *inode, struct file *file)
 {
+       struct net *net = file->private_data;
+       struct kni_net *knet = net_generic(net, kni_net_id);
        struct kni_dev *dev, *n;
 
        /* Stop kernel thread for single mode */
        if (multiple_kthread_on == 0) {
+               mutex_lock(&knet->kni_kthread_lock);
                /* Stop kernel thread */
-               kthread_stop(kni_kthread);
-               kni_kthread = NULL;
+               if (knet->kni_kthread != NULL) {
+                       kthread_stop(knet->kni_kthread);
+                       knet->kni_kthread = NULL;
+               }
+               mutex_unlock(&knet->kni_kthread_lock);
        }
 
-       down_write(&kni_list_lock);
-       list_for_each_entry_safe(dev, n, &kni_list_head, list) {
+       down_write(&knet->kni_list_lock);
+       list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
                /* Stop kernel thread for multiple mode */
                if (multiple_kthread_on && dev->pthread != NULL) {
                        kthread_stop(dev->pthread);
@@ -199,27 +275,28 @@ kni_release(struct inode *inode, struct file *file)
                kni_dev_remove(dev);
                list_del(&dev->list);
        }
-       up_write(&kni_list_lock);
+       up_write(&knet->kni_list_lock);
 
        /* Clear the bit of device in use */
-       clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
+       clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
 
+       put_net(net);
        KNI_PRINT("/dev/kni closed\n");
 
        return 0;
 }
 
 static int
-kni_thread_single(void *unused)
+kni_thread_single(void *data)
 {
+       struct kni_net *knet = data;
        int j;
-       struct kni_dev *dev, *n;
+       struct kni_dev *dev;
 
        while (!kthread_should_stop()) {
-               down_read(&kni_list_lock);
+               down_read(&knet->kni_list_lock);
                for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
-                       list_for_each_entry_safe(dev, n,
-                                       &kni_list_head, list) {
+                       list_for_each_entry(dev, &knet->kni_list_head, list) {
 #ifdef RTE_KNI_VHOST
                                kni_chk_vhost_rx(dev);
 #else
@@ -228,7 +305,7 @@ kni_thread_single(void *unused)
                                kni_net_poll_resp(dev);
                        }
                }
-               up_read(&kni_list_lock);
+               up_read(&knet->kni_list_lock);
 #ifdef RTE_KNI_PREEMPT_DEFAULT
                /* reschedule out for a while */
                schedule_timeout_interruptible(usecs_to_jiffies( \
@@ -269,17 +346,11 @@ kni_dev_remove(struct kni_dev *dev)
        if (!dev)
                return -ENODEV;
 
-       switch (dev->device_id) {
-       #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
-       #include <rte_pci_dev_ids.h>
-               igb_kni_remove(dev->pci_dev);
-               break;
-       #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
-       #include <rte_pci_dev_ids.h>
-               ixgbe_kni_remove(dev->pci_dev);
-               break;
-       default:
-               break;
+       if (dev->pci_dev) {
+               if (pci_match_id(ixgbe_pci_tbl, dev->pci_dev))
+                       ixgbe_kni_remove(dev->pci_dev);
+               else if (pci_match_id(igb_pci_tbl, dev->pci_dev))
+                       igb_kni_remove(dev->pci_dev);
        }
 
        if (dev->net_dev) {
@@ -306,8 +377,51 @@ kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
 }
 
 static int
-kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
+kni_run_thread(struct kni_net *knet, struct kni_dev *kni, uint8_t force_bind)
 {
+       /**
+        * Create a new kernel thread for multiple mode, set its core affinity,
+        * and finally wake it up.
+        */
+       if (multiple_kthread_on) {
+               kni->pthread = kthread_create(kni_thread_multiple,
+                       (void *)kni, "kni_%s", kni->name);
+               if (IS_ERR(kni->pthread)) {
+                       kni_dev_remove(kni);
+                       return -ECANCELED;
+               }
+
+               if (force_bind)
+                       kthread_bind(kni->pthread, kni->core_id);
+               wake_up_process(kni->pthread);
+       } else {
+               mutex_lock(&knet->kni_kthread_lock);
+
+               if (knet->kni_kthread == NULL) {
+                       knet->kni_kthread = kthread_create(kni_thread_single,
+                               (void *)knet, "kni_single");
+                       if (IS_ERR(knet->kni_kthread)) {
+                               mutex_unlock(&knet->kni_kthread_lock);
+                               kni_dev_remove(kni);
+                               return -ECANCELED;
+                       }
+
+                       if (force_bind)
+                               kthread_bind(knet->kni_kthread, kni->core_id);
+                       wake_up_process(knet->kni_kthread);
+               }
+
+               mutex_unlock(&knet->kni_kthread_lock);
+       }
+
+       return 0;
+}
+
+static int
+kni_ioctl_create(struct net *net,
+               unsigned int ioctl_num, unsigned long ioctl_param)
+{
+       struct kni_net *knet = net_generic(net, kni_net_id);
        int ret;
        struct rte_kni_device_info dev_info;
        struct pci_dev *pci = NULL;
@@ -315,7 +429,6 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
        struct net_device *net_dev = NULL;
        struct net_device *lad_dev = NULL;
        struct kni_dev *kni, *dev, *n;
-       struct net *net;
 
        printk(KERN_INFO "KNI: Creating kni...\n");
        /* Check the buffer size, to avoid warning */
@@ -330,24 +443,22 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
        }
 
        /**
-        * Check if the cpu core id is valid for binding,
-        * for multiple kernel thread mode.
+        * Check if the cpu core id is valid for binding.
         */
-       if (multiple_kthread_on && dev_info.force_bind &&
-                               !cpu_online(dev_info.core_id)) {
+       if (dev_info.force_bind && !cpu_online(dev_info.core_id)) {
                KNI_ERR("cpu %u is not online\n", dev_info.core_id);
                return -EINVAL;
        }
 
        /* Check if it has been created */
-       down_read(&kni_list_lock);
-       list_for_each_entry_safe(dev, n, &kni_list_head, list) {
+       down_read(&knet->kni_list_lock);
+       list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
                if (kni_check_param(dev, &dev_info) < 0) {
-                       up_read(&kni_list_lock);
+                       up_read(&knet->kni_list_lock);
                        return -EINVAL;
                }
        }
-       up_read(&kni_list_lock);
+       up_read(&knet->kni_list_lock);
 
        net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
 #ifdef NET_NAME_UNKNOWN
@@ -359,13 +470,7 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
                return -EBUSY;
        }
 
-       net = get_net_ns_by_pid(current->pid);
-       if (IS_ERR(net)) {
-               free_netdev(net_dev);
-               return PTR_ERR(net);
-       }
        dev_net_set(net_dev, net);
-       put_net(net);
 
        kni = netdev_priv(net_dev);
 
@@ -385,9 +490,6 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
        kni->sync_va = dev_info.sync_va;
        kni->sync_kva = phys_to_virt(dev_info.sync_phys);
 
-       kni->mbuf_kva = phys_to_virt(dev_info.mbuf_phys);
-       kni->mbuf_va = dev_info.mbuf_va;
-
 #ifdef RTE_KNI_VHOST
        kni->vhost_queue = NULL;
        kni->vq_status = BE_STOP;
@@ -406,9 +508,6 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
                (unsigned long long) dev_info.req_phys, kni->req_q);
        KNI_PRINT("resp_phys:    0x%016llx, resp_q addr:    0x%p\n",
                (unsigned long long) dev_info.resp_phys, kni->resp_q);
-       KNI_PRINT("mbuf_phys:    0x%016llx, mbuf_kva:       0x%p\n",
-               (unsigned long long) dev_info.mbuf_phys, kni->mbuf_kva);
-       KNI_PRINT("mbuf_va:      0x%p\n", dev_info.mbuf_va);
        KNI_PRINT("mbuf_size:    %u\n", kni->mbuf_size);
 
        KNI_DBG("PCI: %02x:%02x.%02x %04x:%04x\n",
@@ -431,20 +530,13 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
                        (PCI_SLOT(pci->devfn) == dev_info.devid) &&
                        (PCI_FUNC(pci->devfn) == dev_info.function)) {
                        found_pci = pci;
-                       switch (dev_info.device_id) {
-                       #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
-                       #include <rte_pci_dev_ids.h>
-                               ret = igb_kni_probe(found_pci, &lad_dev);
-                               break;
-                       #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) \
-                                                       case (dev):
-                       #include <rte_pci_dev_ids.h>
+
+                       if (pci_match_id(ixgbe_pci_tbl, found_pci))
                                ret = ixgbe_kni_probe(found_pci, &lad_dev);
-                               break;
-                       default:
+                       else if (pci_match_id(igb_pci_tbl, found_pci))
+                               ret = igb_kni_probe(found_pci, &lad_dev);
+                       else
                                ret = -1;
-                               break;
-                       }
 
                        KNI_DBG("PCI found: pci=0x%p, lad_dev=0x%p\n",
                                                        pci, lad_dev);
@@ -466,11 +558,22 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
        if (pci)
                pci_dev_put(pci);
 
+       if (kni->lad_dev)
+               memcpy(net_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(net_dev->dev_addr);
+
        ret = register_netdev(net_dev);
        if (ret) {
                KNI_ERR("error %i registering device \"%s\"\n",
                                        ret, dev_info.name);
+               kni->net_dev = NULL;
                kni_dev_remove(kni);
+               free_netdev(net_dev);
                return -ENODEV;
        }
 
@@ -478,33 +581,22 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
        kni_vhost_init(kni);
 #endif
 
-       /**
-        * Create a new kernel thread for multiple mode, set its core affinity,
-        * and finally wake it up.
-        */
-       if (multiple_kthread_on) {
-               kni->pthread = kthread_create(kni_thread_multiple,
-                                             (void *)kni,
-                                             "kni_%s", kni->name);
-               if (IS_ERR(kni->pthread)) {
-                       kni_dev_remove(kni);
-                       return -ECANCELED;
-               }
-               if (dev_info.force_bind)
-                       kthread_bind(kni->pthread, kni->core_id);
-               wake_up_process(kni->pthread);
-       }
+       ret = kni_run_thread(knet, kni, dev_info.force_bind);
+       if (ret != 0)
+               return ret;
 
-       down_write(&kni_list_lock);
-       list_add(&kni->list, &kni_list_head);
-       up_write(&kni_list_lock);
+       down_write(&knet->kni_list_lock);
+       list_add(&kni->list, &knet->kni_list_head);
+       up_write(&knet->kni_list_lock);
 
        return 0;
 }
 
 static int
-kni_ioctl_release(unsigned int ioctl_num, unsigned long ioctl_param)
+kni_ioctl_release(struct net *net,
+               unsigned int ioctl_num, unsigned long ioctl_param)
 {
+       struct kni_net *knet = net_generic(net, kni_net_id);
        int ret = -EINVAL;
        struct kni_dev *dev, *n;
        struct rte_kni_device_info dev_info;
@@ -522,8 +614,8 @@ kni_ioctl_release(unsigned int ioctl_num, unsigned long ioctl_param)
        if (strlen(dev_info.name) == 0)
                return ret;
 
-       down_write(&kni_list_lock);
-       list_for_each_entry_safe(dev, n, &kni_list_head, list) {
+       down_write(&knet->kni_list_lock);
+       list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
                if (strncmp(dev->name, dev_info.name, RTE_KNI_NAMESIZE) != 0)
                        continue;
 
@@ -540,7 +632,7 @@ kni_ioctl_release(unsigned int ioctl_num, unsigned long ioctl_param)
                ret = 0;
                break;
        }
-       up_write(&kni_list_lock);
+       up_write(&knet->kni_list_lock);
        printk(KERN_INFO "KNI: %s release kni named %s\n",
                (ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
 
@@ -553,8 +645,9 @@ kni_ioctl(struct inode *inode,
        unsigned long ioctl_param)
 {
        int ret = -EINVAL;
+       struct net *net = current->nsproxy->net_ns;
 
-       KNI_DBG("IOCTL num=0x%0x param=0x%0lx \n", ioctl_num, ioctl_param);
+       KNI_DBG("IOCTL num=0x%0x param=0x%0lx\n", ioctl_num, ioctl_param);
 
        /*
         * Switch according to the ioctl called
@@ -564,13 +657,13 @@ kni_ioctl(struct inode *inode,
                /* For test only, not used */
                break;
        case _IOC_NR(RTE_KNI_IOCTL_CREATE):
-               ret = kni_ioctl_create(ioctl_num, ioctl_param);
+               ret = kni_ioctl_create(net, ioctl_num, ioctl_param);
                break;
        case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
-               ret = kni_ioctl_release(ioctl_num, ioctl_param);
+               ret = kni_ioctl_release(net, ioctl_num, ioctl_param);
                break;
        default:
-               KNI_DBG("IOCTL default \n");
+               KNI_DBG("IOCTL default\n");
                break;
        }