Imported Upstream version 16.11
[deb_dpdk.git] / lib / librte_eal / linuxapp / kni / kni_misc.c
index 59d15ca..497db9b 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,52 +48,15 @@ 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);
-
-static int kni_open(struct inode *inode, struct file *file);
-static int kni_release(struct inode *inode, struct file *file);
-static int kni_ioctl(struct inode *inode, unsigned int ioctl_num,
-                                       unsigned long ioctl_param);
-static int kni_compat_ioctl(struct inode *inode, unsigned int ioctl_num,
-                                               unsigned long ioctl_param);
-static int kni_dev_remove(struct kni_dev *dev);
-
-static int __init kni_parse_kthread_mode(void);
-
-/* KNI processing for single kernel thread mode */
-static int kni_thread_single(void *unused);
-/* KNI processing for multiple kernel thread mode */
-static int kni_thread_multiple(void *param);
-
-static struct file_operations kni_fops = {
-       .owner = THIS_MODULE,
-       .open = kni_open,
-       .release = kni_release,
-       .unlocked_ioctl = (void *)kni_ioctl,
-       .compat_ioctl = (void *)kni_compat_ioctl,
-};
-
-static struct miscdevice kni_misc = {
-       .minor = MISC_DYNAMIC_MINOR,
-       .name = KNI_DEVICE,
-       .fops = &kni_fops,
-};
+extern const struct pci_device_id ixgbe_pci_tbl[];
+extern const struct pci_device_id igb_pci_tbl[];
 
 /* 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 uint32_t multiple_kthread_on;
 
 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
 
@@ -100,20 +64,24 @@ 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)
+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;
@@ -123,6 +91,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);
 
@@ -137,11 +107,15 @@ static int __net_init kni_init_net(struct net *net)
 #endif
 }
 
-static void __net_exit kni_exit_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);
+       struct kni_net *knet __maybe_unused;
+
+       knet = net_generic(net, kni_net_id);
+       mutex_destroy(&knet->kni_kthread_lock);
 
+#ifndef HAVE_SIMPLIFIED_PERNET_OPERATIONS
        kfree(knet);
 #endif
 }
@@ -155,72 +129,56 @@ static struct pernet_operations kni_net_ops = {
 #endif
 };
 
-static int __init
-kni_init(void)
+static int
+kni_thread_single(void *data)
 {
-       int rc;
-
-       KNI_PRINT("######## DPDK kni module loading ########\n");
-
-       if (kni_parse_kthread_mode() < 0) {
-               KNI_ERR("Invalid parameter for kthread_mode\n");
-               return -EINVAL;
-       }
+       struct kni_net *knet = data;
+       int j;
+       struct kni_dev *dev;
 
-#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
-       rc = register_pernet_subsys(&kni_net_ops);
+       while (!kthread_should_stop()) {
+               down_read(&knet->kni_list_lock);
+               for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
+                       list_for_each_entry(dev, &knet->kni_list_head, list) {
+#ifdef RTE_KNI_VHOST
+                               kni_chk_vhost_rx(dev);
 #else
-       rc = register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
+                               kni_net_rx(dev);
+#endif
+                               kni_net_poll_resp(dev);
+                       }
+               }
+               up_read(&knet->kni_list_lock);
+#ifdef RTE_KNI_PREEMPT_DEFAULT
+               /* reschedule out for a while */
+               schedule_timeout_interruptible(
+                       usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
 #endif
-       if (rc)
-               return -EPERM;
-
-       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);
-
-       KNI_PRINT("######## DPDK kni module loaded  ########\n");
-
        return 0;
-
-out:
-#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
-       unregister_pernet_subsys(&kni_net_ops);
-#else
-       register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
-#endif
-       return rc;
 }
 
-static void __exit
-kni_exit(void)
+static int
+kni_thread_multiple(void *param)
 {
-       misc_deregister(&kni_misc);
-#ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
-       unregister_pernet_subsys(&kni_net_ops);
+       int j;
+       struct kni_dev *dev = (struct kni_dev *)param;
+
+       while (!kthread_should_stop()) {
+               for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
+#ifdef RTE_KNI_VHOST
+                       kni_chk_vhost_rx(dev);
 #else
-       register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
+                       kni_net_rx(dev);
 #endif
-       KNI_PRINT("####### DPDK kni module unloaded  #######\n");
-}
-
-static int __init
-kni_parse_kthread_mode(void)
-{
-       if (!kthread_mode)
-               return 0;
-
-       if (strcmp(kthread_mode, "single") == 0)
-               return 0;
-       else if (strcmp(kthread_mode, "multiple") == 0)
-               multiple_kthread_on = 1;
-       else
-               return -1;
+                       kni_net_poll_resp(dev);
+               }
+#ifdef RTE_KNI_PREEMPT_DEFAULT
+               schedule_timeout_interruptible(
+                       usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
+#endif
+       }
 
        return 0;
 }
@@ -235,21 +193,29 @@ 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");
+       pr_debug("/dev/kni opened\n");
+
+       return 0;
+}
+
+static int
+kni_dev_remove(struct kni_dev *dev)
+{
+       if (!dev)
+               return -ENODEV;
+
+       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) {
+               unregister_netdev(dev->net_dev);
+               free_netdev(dev->net_dev);
+       }
 
        return 0;
 }
@@ -263,9 +229,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);
@@ -288,110 +258,70 @@ kni_release(struct inode *inode, struct file *file)
        clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
 
        put_net(net);
-       KNI_PRINT("/dev/kni closed\n");
+       pr_debug("/dev/kni closed\n");
 
        return 0;
 }
 
 static int
-kni_thread_single(void *data)
+kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
 {
-       struct kni_net *knet = data;
-       int j;
-       struct kni_dev *dev;
+       if (!kni || !dev)
+               return -1;
 
-       while (!kthread_should_stop()) {
-               down_read(&knet->kni_list_lock);
-               for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
-                       list_for_each_entry(dev, &knet->kni_list_head, list) {
-#ifdef RTE_KNI_VHOST
-                               kni_chk_vhost_rx(dev);
-#else
-                               kni_net_rx(dev);
-#endif
-                               kni_net_poll_resp(dev);
-                       }
-               }
-               up_read(&knet->kni_list_lock);
-#ifdef RTE_KNI_PREEMPT_DEFAULT
-               /* reschedule out for a while */
-               schedule_timeout_interruptible(usecs_to_jiffies( \
-                               KNI_KTHREAD_RESCHEDULE_INTERVAL));
-#endif
+       /* Check if network name has been used */
+       if (!strncmp(kni->name, dev->name, RTE_KNI_NAMESIZE)) {
+               pr_err("KNI name %s duplicated\n", dev->name);
+               return -1;
        }
 
        return 0;
 }
 
 static int
-kni_thread_multiple(void *param)
+kni_run_thread(struct kni_net *knet, struct kni_dev *kni, uint8_t force_bind)
 {
-       int j;
-       struct kni_dev *dev = (struct kni_dev *)param;
-
-       while (!kthread_should_stop()) {
-               for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
-#ifdef RTE_KNI_VHOST
-                       kni_chk_vhost_rx(dev);
-#else
-                       kni_net_rx(dev);
-#endif
-                       kni_net_poll_resp(dev);
+       /**
+        * 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;
                }
-#ifdef RTE_KNI_PREEMPT_DEFAULT
-               schedule_timeout_interruptible(usecs_to_jiffies( \
-                               KNI_KTHREAD_RESCHEDULE_INTERVAL));
-#endif
-       }
-
-       return 0;
-}
-
-static int
-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->net_dev) {
-               unregister_netdev(dev->net_dev);
-               free_netdev(dev->net_dev);
-       }
 
-       return 0;
-}
+               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;
+                       }
 
-static int
-kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
-{
-       if (!kni || !dev)
-               return -1;
+                       if (force_bind)
+                               kthread_bind(knet->kni_kthread, kni->core_id);
+                       wake_up_process(knet->kni_kthread);
+               }
 
-       /* Check if network name has been used */
-       if (!strncmp(kni->name, dev->name, RTE_KNI_NAMESIZE)) {
-               KNI_ERR("KNI name %s duplicated\n", dev->name);
-               return -1;
+               mutex_unlock(&knet->kni_kthread_lock);
        }
 
        return 0;
 }
 
 static int
-kni_ioctl_create(struct net *net,
-               unsigned int ioctl_num, unsigned long ioctl_param)
+kni_ioctl_create(struct net *net, uint32_t ioctl_num,
+               unsigned long ioctl_param)
 {
        struct kni_net *knet = net_generic(net, kni_net_id);
        int ret;
@@ -402,7 +332,7 @@ kni_ioctl_create(struct net *net,
        struct net_device *lad_dev = NULL;
        struct kni_dev *kni, *dev, *n;
 
-       printk(KERN_INFO "KNI: Creating kni...\n");
+       pr_info("Creating kni...\n");
        /* Check the buffer size, to avoid warning */
        if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
                return -EINVAL;
@@ -410,17 +340,15 @@ kni_ioctl_create(struct net *net,
        /* Copy kni info from user space */
        ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
        if (ret) {
-               KNI_ERR("copy_from_user in kni_ioctl_create");
+               pr_err("copy_from_user in kni_ioctl_create");
                return -EIO;
        }
 
        /**
-        * 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)) {
-               KNI_ERR("cpu %u is not online\n", dev_info.core_id);
+       if (dev_info.force_bind && !cpu_online(dev_info.core_id)) {
+               pr_err("cpu %u is not online\n", dev_info.core_id);
                return -EINVAL;
        }
 
@@ -440,7 +368,7 @@ kni_ioctl_create(struct net *net,
 #endif
                                                        kni_net_init);
        if (net_dev == NULL) {
-               KNI_ERR("error allocating device \"%s\"\n", dev_info.name);
+               pr_err("error allocating device \"%s\"\n", dev_info.name);
                return -EBUSY;
        }
 
@@ -464,33 +392,27 @@ 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;
 #endif
        kni->mbuf_size = dev_info.mbuf_size;
 
-       KNI_PRINT("tx_phys:      0x%016llx, tx_q addr:      0x%p\n",
+       pr_debug("tx_phys:      0x%016llx, tx_q addr:      0x%p\n",
                (unsigned long long) dev_info.tx_phys, kni->tx_q);
-       KNI_PRINT("rx_phys:      0x%016llx, rx_q addr:      0x%p\n",
+       pr_debug("rx_phys:      0x%016llx, rx_q addr:      0x%p\n",
                (unsigned long long) dev_info.rx_phys, kni->rx_q);
-       KNI_PRINT("alloc_phys:   0x%016llx, alloc_q addr:   0x%p\n",
+       pr_debug("alloc_phys:   0x%016llx, alloc_q addr:   0x%p\n",
                (unsigned long long) dev_info.alloc_phys, kni->alloc_q);
-       KNI_PRINT("free_phys:    0x%016llx, free_q addr:    0x%p\n",
+       pr_debug("free_phys:    0x%016llx, free_q addr:    0x%p\n",
                (unsigned long long) dev_info.free_phys, kni->free_q);
-       KNI_PRINT("req_phys:     0x%016llx, req_q addr:     0x%p\n",
+       pr_debug("req_phys:     0x%016llx, req_q addr:     0x%p\n",
                (unsigned long long) dev_info.req_phys, kni->req_q);
-       KNI_PRINT("resp_phys:    0x%016llx, resp_q addr:    0x%p\n",
+       pr_debug("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);
+       pr_debug("mbuf_size:    %u\n", kni->mbuf_size);
 
-       KNI_DBG("PCI: %02x:%02x.%02x %04x:%04x\n",
+       pr_debug("PCI: %02x:%02x.%02x %04x:%04x\n",
                                        dev_info.bus,
                                        dev_info.devid,
                                        dev_info.function,
@@ -501,7 +423,7 @@ kni_ioctl_create(struct net *net,
 
        /* Support Ethtool */
        while (pci) {
-               KNI_PRINT("pci_bus: %02x:%02x:%02x \n",
+               pr_debug("pci_bus: %02x:%02x:%02x\n",
                                        pci->bus->number,
                                        PCI_SLOT(pci->devfn),
                                        PCI_FUNC(pci->devfn));
@@ -510,28 +432,21 @@ kni_ioctl_create(struct net *net,
                        (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",
+                       pr_debug("PCI found: pci=0x%p, lad_dev=0x%p\n",
                                                        pci, lad_dev);
                        if (ret == 0) {
                                kni->lad_dev = lad_dev;
                                kni_set_ethtool_ops(kni->net_dev);
                        } else {
-                               KNI_ERR("Device not supported by ethtool");
+                               pr_err("Device not supported by ethtool");
                                kni->lad_dev = NULL;
                        }
 
@@ -546,7 +461,7 @@ kni_ioctl_create(struct net *net,
                pci_dev_put(pci);
 
        if (kni->lad_dev)
-               memcpy(net_dev->dev_addr, kni->lad_dev->dev_addr, ETH_ALEN);
+               ether_addr_copy(net_dev->dev_addr, kni->lad_dev->dev_addr);
        else
                /*
                 * Generate random mac address. eth_random_addr() is the newer
@@ -556,9 +471,11 @@ kni_ioctl_create(struct net *net,
 
        ret = register_netdev(net_dev);
        if (ret) {
-               KNI_ERR("error %i registering device \"%s\"\n",
+               pr_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;
        }
 
@@ -566,22 +483,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);
@@ -591,8 +495,8 @@ kni_ioctl_create(struct net *net,
 }
 
 static int
-kni_ioctl_release(struct net *net,
-               unsigned int ioctl_num, unsigned long ioctl_param)
+kni_ioctl_release(struct net *net, uint32_t ioctl_num,
+               unsigned long ioctl_param)
 {
        struct kni_net *knet = net_generic(net, kni_net_id);
        int ret = -EINVAL;
@@ -600,11 +504,11 @@ kni_ioctl_release(struct net *net,
        struct rte_kni_device_info dev_info;
 
        if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
-                       return -EINVAL;
+               return -EINVAL;
 
        ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
        if (ret) {
-               KNI_ERR("copy_from_user in kni_ioctl_release");
+               pr_err("copy_from_user in kni_ioctl_release");
                return -EIO;
        }
 
@@ -631,21 +535,19 @@ kni_ioctl_release(struct net *net,
                break;
        }
        up_write(&knet->kni_list_lock);
-       printk(KERN_INFO "KNI: %s release kni named %s\n",
+       pr_info("%s release kni named %s\n",
                (ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
 
        return ret;
 }
 
 static int
-kni_ioctl(struct inode *inode,
-       unsigned int ioctl_num,
-       unsigned long ioctl_param)
+kni_ioctl(struct inode *inode, uint32_t ioctl_num, 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);
+       pr_debug("IOCTL num=0x%0x param=0x%0lx\n", ioctl_num, ioctl_param);
 
        /*
         * Switch according to the ioctl called
@@ -661,7 +563,7 @@ kni_ioctl(struct inode *inode,
                ret = kni_ioctl_release(net, ioctl_num, ioctl_param);
                break;
        default:
-               KNI_DBG("IOCTL default\n");
+               pr_debug("IOCTL default\n");
                break;
        }
 
@@ -669,16 +571,99 @@ kni_ioctl(struct inode *inode,
 }
 
 static int
-kni_compat_ioctl(struct inode *inode,
-               unsigned int ioctl_num,
+kni_compat_ioctl(struct inode *inode, uint32_t ioctl_num,
                unsigned long ioctl_param)
 {
        /* 32 bits app on 64 bits OS to be supported later */
-       KNI_PRINT("Not implemented.\n");
+       pr_debug("Not implemented.\n");
 
        return -EINVAL;
 }
 
+static const struct file_operations kni_fops = {
+       .owner = THIS_MODULE,
+       .open = kni_open,
+       .release = kni_release,
+       .unlocked_ioctl = (void *)kni_ioctl,
+       .compat_ioctl = (void *)kni_compat_ioctl,
+};
+
+static struct miscdevice kni_misc = {
+       .minor = MISC_DYNAMIC_MINOR,
+       .name = KNI_DEVICE,
+       .fops = &kni_fops,
+};
+
+static int __init
+kni_parse_kthread_mode(void)
+{
+       if (!kthread_mode)
+               return 0;
+
+       if (strcmp(kthread_mode, "single") == 0)
+               return 0;
+       else if (strcmp(kthread_mode, "multiple") == 0)
+               multiple_kthread_on = 1;
+       else
+               return -1;
+
+       return 0;
+}
+
+static int __init
+kni_init(void)
+{
+       int rc;
+
+       if (kni_parse_kthread_mode() < 0) {
+               pr_err("Invalid parameter for kthread_mode\n");
+               return -EINVAL;
+       }
+
+       if (multiple_kthread_on == 0)
+               pr_debug("Single kernel thread for all KNI devices\n");
+       else
+               pr_debug("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;
+
+       rc = misc_register(&kni_misc);
+       if (rc != 0) {
+               pr_err("Misc registration failed\n");
+               goto out;
+       }
+
+       /* Configure the lo mode according to the input parameter */
+       kni_net_config_lo_mode(lo_mode);
+
+       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
+}
+
 module_init(kni_init);
 module_exit(kni_exit);