kni: uninitialize global variables
[dpdk.git] / lib / librte_eal / linuxapp / kni / kni_misc.c
index 66bf519..f051595 100644 (file)
@@ -30,6 +30,7 @@
 #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>
@@ -47,17 +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 struct pci_device_id ixgbe_pci_tbl[];
-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);
@@ -89,11 +81,11 @@ 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 */
 
@@ -101,6 +93,7 @@ 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;
@@ -110,11 +103,12 @@ 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 = kmalloc(sizeof(struct kni_net), GFP_KERNEL);
+       knet = kzalloc(sizeof(struct kni_net), GFP_KERNEL);
        if (!knet) {
                ret = -ENOMEM;
                return ret;
@@ -124,6 +118,8 @@ static int __net_init kni_init_net(struct net *net)
        /* Clear the bit of device in use */
        clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
 
+       mutex_init(&knet->kni_kthread_lock);
+
        init_rwsem(&knet->kni_list_lock);
        INIT_LIST_HEAD(&knet->kni_list_head);
 
@@ -140,9 +136,9 @@ static int __net_init kni_init_net(struct net *net)
 
 static void __net_exit kni_exit_net(struct net *net)
 {
-#ifndef HAVE_SIMPLIFIED_PERNET_OPERATIONS
        struct kni_net *knet = net_generic(net, kni_net_id);
-
+       mutex_destroy(&knet->kni_kthread_lock);
+#ifndef HAVE_SIMPLIFIED_PERNET_OPERATIONS
        kfree(knet);
 #endif
 }
@@ -168,6 +164,11 @@ kni_init(void)
                return -EINVAL;
        }
 
+       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
@@ -193,7 +194,7 @@ out:
 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
        unregister_pernet_subsys(&kni_net_ops);
 #else
-       register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
+       unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
 #endif
        return rc;
 }
@@ -205,7 +206,7 @@ kni_exit(void)
 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
        unregister_pernet_subsys(&kni_net_ops);
 #else
-       register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
+       unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
 #endif
        KNI_PRINT("####### DPDK kni module unloaded  #######\n");
 }
@@ -236,19 +237,6 @@ kni_open(struct inode *inode, struct file *file)
        if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use))
                return -EBUSY;
 
-       /* 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 */
-               knet->kni_kthread = kthread_run(kni_thread_single, (void *)knet,
-                                               "kni_single");
-               if (IS_ERR(knet->kni_kthread)) {
-                       KNI_ERR("Unable to create kernel threaed\n");
-                       return PTR_ERR(knet->kni_kthread);
-               }
-       } else
-               KNI_PRINT("Multiple kernel thread mode enabled\n");
-
        file->private_data = get_net(net);
        KNI_PRINT("/dev/kni opened\n");
 
@@ -264,9 +252,13 @@ kni_release(struct inode *inode, struct file *file)
 
        /* Stop kernel thread for single mode */
        if (multiple_kthread_on == 0) {
+               mutex_lock(&knet->kni_kthread_lock);
                /* Stop kernel thread */
-               kthread_stop(knet->kni_kthread);
-               knet->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(&knet->kni_list_lock);
@@ -354,16 +346,11 @@ kni_dev_remove(struct kni_dev *dev)
        if (!dev)
                return -ENODEV;
 
-       if (pci_match_id(ixgbe_pci_tbl, dev->pci_dev))
-               ixgbe_kni_remove(dev->pci_dev);
-
-       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;
-       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) {
@@ -389,6 +376,47 @@ kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
        return 0;
 }
 
+static int
+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)
@@ -415,11 +443,9 @@ kni_ioctl_create(struct net *net,
        }
 
        /**
-        * 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;
        }
@@ -464,9 +490,6 @@ kni_ioctl_create(struct net *net,
        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;
@@ -485,9 +508,6 @@ kni_ioctl_create(struct net *net,
                (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",
@@ -513,16 +533,10 @@ kni_ioctl_create(struct net *net,
 
                        if (pci_match_id(ixgbe_pci_tbl, found_pci))
                                ret = ixgbe_kni_probe(found_pci, &lad_dev);
-
-                       switch (dev_info.device_id) {
-                       #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
-                       #include <rte_pci_dev_ids.h>
+                       else if (pci_match_id(igb_pci_tbl, found_pci))
                                ret = igb_kni_probe(found_pci, &lad_dev);
-                               break;
-                       default:
+                       else
                                ret = -1;
-                               break;
-                       }
 
                        KNI_DBG("PCI found: pci=0x%p, lad_dev=0x%p\n",
                                                        pci, lad_dev);
@@ -557,7 +571,9 @@ kni_ioctl_create(struct net *net,
        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;
        }
 
@@ -565,22 +581,9 @@ kni_ioctl_create(struct net *net,
        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(&knet->kni_list_lock);
        list_add(&kni->list, &knet->kni_list_head);