kni: identify device by name
[dpdk.git] / lib / librte_eal / linuxapp / kni / kni_misc.c
old mode 100755 (executable)
new mode 100644 (file)
index dca1ba5..9adf697
@@ -3,24 +3,23 @@
  * 
  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
  * 
- *   This program is free software; you can redistribute it and/or modify 
+ *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of version 2 of the GNU General Public License as
  *   published by the Free Software Foundation.
  * 
- *   This program is distributed in the hope that it will be useful, but 
- *   WITHOUT ANY WARRANTY; without even the implied warranty of 
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+ *   This program is distributed in the hope that it will be useful, but
+ *   WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  *   General Public License for more details.
  * 
- *   You should have received a copy of the GNU General Public License 
- *   along with this program; if not, write to the Free Software 
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
  *   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
- *   The full GNU General Public License is included in this distribution 
+ *   The full GNU General Public License is included in this distribution
  *   in the file called LICENSE.GPL.
  * 
  *   Contact Information:
  *   Intel Corporation
- * 
  */
 
 #include <linux/module.h>
@@ -143,8 +142,6 @@ kni_release(struct inode *inode, struct file *file)
 {
        struct kni_dev *dev, *n;
 
-       KNI_PRINT("Stopping KNI thread...");
-
        /* Stop kernel thread */
        kthread_stop(kni_kthread);
        kni_kthread = NULL;
@@ -184,7 +181,6 @@ kni_thread(void *unused)
        int j;
        struct kni_dev *dev, *n;
 
-       KNI_PRINT("Kernel thread for KNI started\n");
        while (!kthread_should_stop()) {
                down_read(&kni_list_lock);
                for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
@@ -199,7 +195,21 @@ kni_thread(void *unused)
                schedule_timeout_interruptible(usecs_to_jiffies( \
                                KNI_KTHREAD_RESCHEDULE_INTERVAL));
        }
-       KNI_PRINT("Kernel thread for KNI stopped\n");
+
+       return 0;
+}
+
+static int
+kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
+{
+       if (!kni || !dev)
+               return -1;
+
+       /* 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;
+       }
 
        return 0;
 }
@@ -230,10 +240,8 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
        /* Check if it has been created */
        down_read(&kni_list_lock);
        list_for_each_entry_safe(dev, n, &kni_list_head, list) {
-               if (dev->port_id == dev_info.port_id) {
+               if (kni_check_param(dev, &dev_info) < 0) {
                        up_read(&kni_list_lock);
-                       KNI_ERR("Port %d has already been created\n",
-                                               dev_info.port_id);
                        return -EINVAL;
                }
        }
@@ -249,7 +257,8 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
        kni = netdev_priv(net_dev);
 
        kni->net_dev = net_dev;
-       kni->port_id = dev_info.port_id;
+       kni->group_id = dev_info.group_id;
+       strncpy(kni->name, dev_info.name, RTE_KNI_NAMESIZE);
 
        /* Translate user space info into kernel space info */
        kni->tx_q = phys_to_virt(dev_info.tx_phys);
@@ -259,7 +268,6 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
 
        kni->req_q = phys_to_virt(dev_info.req_phys);
        kni->resp_q = phys_to_virt(dev_info.resp_phys);
-
        kni->sync_va = dev_info.sync_va;
        kni->sync_kva = phys_to_virt(dev_info.sync_phys);
 
@@ -268,44 +276,50 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
 
        kni->mbuf_size = dev_info.mbuf_size;
 
-       KNI_PRINT("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",
-                                               (unsigned long long) dev_info.rx_phys, kni->rx_q);
-       KNI_PRINT("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",
-                                       (unsigned long long) dev_info.free_phys, kni->free_q);
-       KNI_PRINT("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",
-                                       (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", dev_info.bus, dev_info.devid,
-                       dev_info.function, dev_info.vendor_id, dev_info.device_id);
+       KNI_PRINT("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",
+               (unsigned long long) dev_info.rx_phys, kni->rx_q);
+       KNI_PRINT("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",
+               (unsigned long long) dev_info.free_phys, kni->free_q);
+       KNI_PRINT("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",
+               (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",
+                                       dev_info.bus,
+                                       dev_info.devid,
+                                       dev_info.function,
+                                       dev_info.vendor_id,
+                                       dev_info.device_id);
 
        pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
 
        /* Support Ethtool */
        while (pci) {
-               KNI_PRINT("pci_bus: %02x:%02x:%02x \n", pci->bus->number,
-                               PCI_SLOT(pci->devfn), PCI_FUNC(pci->devfn));
+               KNI_PRINT("pci_bus: %02x:%02x:%02x \n",
+                                       pci->bus->number,
+                                       PCI_SLOT(pci->devfn),
+                                       PCI_FUNC(pci->devfn));
 
                if ((pci->bus->number == dev_info.bus) &&
                        (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):
+                       #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) \
+                                                       case (dev):
                        #include <rte_pci_dev_ids.h>
                                ret = ixgbe_kni_probe(found_pci, &lad_dev);
                                break;
@@ -314,12 +328,12 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
                                break;
                        }
 
-                       KNI_DBG("PCI found: pci=0x%p, lad_dev=0x%p\n", pci, lad_dev);
+                       KNI_DBG("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 {
+                       } else {
                                KNI_ERR("Device not supported by ethtool");
                                kni->lad_dev = NULL;
                        }
@@ -328,17 +342,16 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
                        kni->device_id = dev_info.device_id;
                        break;
                }
-               pci = pci_get_device(dev_info.vendor_id, dev_info.device_id,
-                                                                       pci);
+               pci = pci_get_device(dev_info.vendor_id,
+                               dev_info.device_id, pci);
        }
-
        if (pci)
                pci_dev_put(pci);
 
        ret = register_netdev(net_dev);
        if (ret) {
-               KNI_ERR("error %i registering device \"%s\"\n", ret,
-                                                       dev_info.name);
+               KNI_ERR("error %i registering device \"%s\"\n",
+                                       ret, dev_info.name);
                free_netdev(net_dev);
                return -ENODEV;
        }
@@ -346,8 +359,6 @@ kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
        down_write(&kni_list_lock);
        list_add(&kni->list, &kni_list_head);
        up_write(&kni_list_lock);
-       printk(KERN_INFO "KNI: Successfully create kni for port %d\n",
-                                               dev_info.port_id);
 
        return 0;
 }
@@ -356,21 +367,25 @@ static int
 kni_ioctl_release(unsigned int ioctl_num, unsigned long ioctl_param)
 {
        int ret = -EINVAL;
-       uint8_t port_id;
        struct kni_dev *dev, *n;
+       struct rte_kni_device_info dev_info;
 
-       if (_IOC_SIZE(ioctl_num) > sizeof(port_id))
+       if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
                        return -EINVAL;
 
-       ret = copy_from_user(&port_id, (void *)ioctl_param, sizeof(port_id));
+       ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
        if (ret) {
                KNI_ERR("copy_from_user in kni_ioctl_release");
                return -EIO;
        }
 
+       /* Release the network device according to its name */
+       if (strlen(dev_info.name) == 0)
+               return ret;
+
        down_write(&kni_list_lock);
        list_for_each_entry_safe(dev, n, &kni_list_head, list) {
-               if (dev->port_id != port_id)
+               if (strncmp(dev->name, dev_info.name, RTE_KNI_NAMESIZE) != 0)
                        continue;
 
                switch (dev->device_id) {
@@ -392,8 +407,8 @@ kni_ioctl_release(unsigned int ioctl_num, unsigned long ioctl_param)
                break;
        }
        up_write(&kni_list_lock);
-       printk(KERN_INFO "KNI: %s release kni for port %d\n",
-               (ret == 0 ? "Successfully" : "Unsuccessfully"), port_id);
+       printk(KERN_INFO "KNI: %s release kni named %s\n",
+               (ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
 
        return ret;
 }