7590f1fdc17b506eec0a93f463500cc845b82977
[deb_dpdk.git] / lib / librte_eal / linuxapp / kni / kni_misc.c
1 /*-
2  * GPL LICENSE SUMMARY
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of version 2 of the GNU General Public License as
8  *   published by the Free Software Foundation.
9  *
10  *   This program is distributed in the hope that it will be useful, but
11  *   WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *   General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18  *   The full GNU General Public License is included in this distribution
19  *   in the file called LICENSE.GPL.
20  *
21  *   Contact Information:
22  *   Intel Corporation
23  */
24
25 #include <linux/version.h>
26 #include <linux/module.h>
27 #include <linux/miscdevice.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/pci.h>
31 #include <linux/kthread.h>
32 #include <linux/rwsem.h>
33 #include <linux/mutex.h>
34 #include <linux/nsproxy.h>
35 #include <net/net_namespace.h>
36 #include <net/netns/generic.h>
37
38 #include <exec-env/rte_kni_common.h>
39
40 #include "compat.h"
41 #include "kni_dev.h"
42
43 MODULE_LICENSE("Dual BSD/GPL");
44 MODULE_AUTHOR("Intel Corporation");
45 MODULE_DESCRIPTION("Kernel Module for managing kni devices");
46
47 #define KNI_RX_LOOP_NUM 1000
48
49 #define KNI_MAX_DEVICES 32
50
51 extern const struct pci_device_id ixgbe_pci_tbl[];
52 extern const struct pci_device_id igb_pci_tbl[];
53
54 /* loopback mode */
55 static char *lo_mode;
56
57 /* Kernel thread mode */
58 static char *kthread_mode;
59 static uint32_t multiple_kthread_on;
60
61 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
62
63 static int kni_net_id;
64
65 struct kni_net {
66         unsigned long device_in_use; /* device in use flag */
67         struct mutex kni_kthread_lock;
68         struct task_struct *kni_kthread;
69         struct rw_semaphore kni_list_lock;
70         struct list_head kni_list_head;
71 };
72
73 static int __net_init
74 kni_init_net(struct net *net)
75 {
76 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
77         struct kni_net *knet = net_generic(net, kni_net_id);
78
79         memset(knet, 0, sizeof(*knet));
80 #else
81         struct kni_net *knet;
82         int ret;
83
84         knet = kzalloc(sizeof(struct kni_net), GFP_KERNEL);
85         if (!knet) {
86                 ret = -ENOMEM;
87                 return ret;
88         }
89 #endif
90
91         /* Clear the bit of device in use */
92         clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
93
94         mutex_init(&knet->kni_kthread_lock);
95
96         init_rwsem(&knet->kni_list_lock);
97         INIT_LIST_HEAD(&knet->kni_list_head);
98
99 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
100         return 0;
101 #else
102         ret = net_assign_generic(net, kni_net_id, knet);
103         if (ret < 0)
104                 kfree(knet);
105
106         return ret;
107 #endif
108 }
109
110 static void __net_exit
111 kni_exit_net(struct net *net)
112 {
113         struct kni_net *knet __maybe_unused;
114
115         knet = net_generic(net, kni_net_id);
116         mutex_destroy(&knet->kni_kthread_lock);
117
118 #ifndef HAVE_SIMPLIFIED_PERNET_OPERATIONS
119         kfree(knet);
120 #endif
121 }
122
123 static struct pernet_operations kni_net_ops = {
124         .init = kni_init_net,
125         .exit = kni_exit_net,
126 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
127         .id   = &kni_net_id,
128         .size = sizeof(struct kni_net),
129 #endif
130 };
131
132 static int
133 kni_thread_single(void *data)
134 {
135         struct kni_net *knet = data;
136         int j;
137         struct kni_dev *dev;
138
139         while (!kthread_should_stop()) {
140                 down_read(&knet->kni_list_lock);
141                 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
142                         list_for_each_entry(dev, &knet->kni_list_head, list) {
143                                 kni_net_rx(dev);
144                                 kni_net_poll_resp(dev);
145                         }
146                 }
147                 up_read(&knet->kni_list_lock);
148 #ifdef RTE_KNI_PREEMPT_DEFAULT
149                 /* reschedule out for a while */
150                 schedule_timeout_interruptible(
151                         usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
152 #endif
153         }
154
155         return 0;
156 }
157
158 static int
159 kni_thread_multiple(void *param)
160 {
161         int j;
162         struct kni_dev *dev = param;
163
164         while (!kthread_should_stop()) {
165                 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
166                         kni_net_rx(dev);
167                         kni_net_poll_resp(dev);
168                 }
169 #ifdef RTE_KNI_PREEMPT_DEFAULT
170                 schedule_timeout_interruptible(
171                         usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
172 #endif
173         }
174
175         return 0;
176 }
177
178 static int
179 kni_open(struct inode *inode, struct file *file)
180 {
181         struct net *net = current->nsproxy->net_ns;
182         struct kni_net *knet = net_generic(net, kni_net_id);
183
184         /* kni device can be opened by one user only per netns */
185         if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use))
186                 return -EBUSY;
187
188         file->private_data = get_net(net);
189         pr_debug("/dev/kni opened\n");
190
191         return 0;
192 }
193
194 static int
195 kni_dev_remove(struct kni_dev *dev)
196 {
197         if (!dev)
198                 return -ENODEV;
199
200 #ifdef RTE_KNI_KMOD_ETHTOOL
201         if (dev->pci_dev) {
202                 if (pci_match_id(ixgbe_pci_tbl, dev->pci_dev))
203                         ixgbe_kni_remove(dev->pci_dev);
204                 else if (pci_match_id(igb_pci_tbl, dev->pci_dev))
205                         igb_kni_remove(dev->pci_dev);
206         }
207 #endif
208
209         if (dev->net_dev) {
210                 unregister_netdev(dev->net_dev);
211                 free_netdev(dev->net_dev);
212         }
213
214         return 0;
215 }
216
217 static int
218 kni_release(struct inode *inode, struct file *file)
219 {
220         struct net *net = file->private_data;
221         struct kni_net *knet = net_generic(net, kni_net_id);
222         struct kni_dev *dev, *n;
223
224         /* Stop kernel thread for single mode */
225         if (multiple_kthread_on == 0) {
226                 mutex_lock(&knet->kni_kthread_lock);
227                 /* Stop kernel thread */
228                 if (knet->kni_kthread != NULL) {
229                         kthread_stop(knet->kni_kthread);
230                         knet->kni_kthread = NULL;
231                 }
232                 mutex_unlock(&knet->kni_kthread_lock);
233         }
234
235         down_write(&knet->kni_list_lock);
236         list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
237                 /* Stop kernel thread for multiple mode */
238                 if (multiple_kthread_on && dev->pthread != NULL) {
239                         kthread_stop(dev->pthread);
240                         dev->pthread = NULL;
241                 }
242
243                 kni_dev_remove(dev);
244                 list_del(&dev->list);
245         }
246         up_write(&knet->kni_list_lock);
247
248         /* Clear the bit of device in use */
249         clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
250
251         put_net(net);
252         pr_debug("/dev/kni closed\n");
253
254         return 0;
255 }
256
257 static int
258 kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
259 {
260         if (!kni || !dev)
261                 return -1;
262
263         /* Check if network name has been used */
264         if (!strncmp(kni->name, dev->name, RTE_KNI_NAMESIZE)) {
265                 pr_err("KNI name %s duplicated\n", dev->name);
266                 return -1;
267         }
268
269         return 0;
270 }
271
272 static int
273 kni_run_thread(struct kni_net *knet, struct kni_dev *kni, uint8_t force_bind)
274 {
275         /**
276          * Create a new kernel thread for multiple mode, set its core affinity,
277          * and finally wake it up.
278          */
279         if (multiple_kthread_on) {
280                 kni->pthread = kthread_create(kni_thread_multiple,
281                         (void *)kni, "kni_%s", kni->name);
282                 if (IS_ERR(kni->pthread)) {
283                         kni_dev_remove(kni);
284                         return -ECANCELED;
285                 }
286
287                 if (force_bind)
288                         kthread_bind(kni->pthread, kni->core_id);
289                 wake_up_process(kni->pthread);
290         } else {
291                 mutex_lock(&knet->kni_kthread_lock);
292
293                 if (knet->kni_kthread == NULL) {
294                         knet->kni_kthread = kthread_create(kni_thread_single,
295                                 (void *)knet, "kni_single");
296                         if (IS_ERR(knet->kni_kthread)) {
297                                 mutex_unlock(&knet->kni_kthread_lock);
298                                 kni_dev_remove(kni);
299                                 return -ECANCELED;
300                         }
301
302                         if (force_bind)
303                                 kthread_bind(knet->kni_kthread, kni->core_id);
304                         wake_up_process(knet->kni_kthread);
305                 }
306
307                 mutex_unlock(&knet->kni_kthread_lock);
308         }
309
310         return 0;
311 }
312
313 static int
314 kni_ioctl_create(struct net *net, uint32_t ioctl_num,
315                 unsigned long ioctl_param)
316 {
317         struct kni_net *knet = net_generic(net, kni_net_id);
318         int ret;
319         struct rte_kni_device_info dev_info;
320         struct net_device *net_dev = NULL;
321         struct kni_dev *kni, *dev, *n;
322 #ifdef RTE_KNI_KMOD_ETHTOOL
323         struct pci_dev *found_pci = NULL;
324         struct net_device *lad_dev = NULL;
325         struct pci_dev *pci = NULL;
326 #endif
327
328         pr_info("Creating kni...\n");
329         /* Check the buffer size, to avoid warning */
330         if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
331                 return -EINVAL;
332
333         /* Copy kni info from user space */
334         ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
335         if (ret) {
336                 pr_err("copy_from_user in kni_ioctl_create");
337                 return -EIO;
338         }
339
340         /* Check if name is zero-ended */
341         if (strnlen(dev_info.name, sizeof(dev_info.name)) == sizeof(dev_info.name)) {
342                 pr_err("kni.name not zero-terminated");
343                 return -EINVAL;
344         }
345
346         /**
347          * Check if the cpu core id is valid for binding.
348          */
349         if (dev_info.force_bind && !cpu_online(dev_info.core_id)) {
350                 pr_err("cpu %u is not online\n", dev_info.core_id);
351                 return -EINVAL;
352         }
353
354         /* Check if it has been created */
355         down_read(&knet->kni_list_lock);
356         list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
357                 if (kni_check_param(dev, &dev_info) < 0) {
358                         up_read(&knet->kni_list_lock);
359                         return -EINVAL;
360                 }
361         }
362         up_read(&knet->kni_list_lock);
363
364         net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
365 #ifdef NET_NAME_USER
366                                                         NET_NAME_USER,
367 #endif
368                                                         kni_net_init);
369         if (net_dev == NULL) {
370                 pr_err("error allocating device \"%s\"\n", dev_info.name);
371                 return -EBUSY;
372         }
373
374         dev_net_set(net_dev, net);
375
376         kni = netdev_priv(net_dev);
377
378         kni->net_dev = net_dev;
379         kni->group_id = dev_info.group_id;
380         kni->core_id = dev_info.core_id;
381         strncpy(kni->name, dev_info.name, RTE_KNI_NAMESIZE);
382
383         /* Translate user space info into kernel space info */
384         kni->tx_q = phys_to_virt(dev_info.tx_phys);
385         kni->rx_q = phys_to_virt(dev_info.rx_phys);
386         kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
387         kni->free_q = phys_to_virt(dev_info.free_phys);
388
389         kni->req_q = phys_to_virt(dev_info.req_phys);
390         kni->resp_q = phys_to_virt(dev_info.resp_phys);
391         kni->sync_va = dev_info.sync_va;
392         kni->sync_kva = phys_to_virt(dev_info.sync_phys);
393
394         kni->mbuf_size = dev_info.mbuf_size;
395
396         pr_debug("tx_phys:      0x%016llx, tx_q addr:      0x%p\n",
397                 (unsigned long long) dev_info.tx_phys, kni->tx_q);
398         pr_debug("rx_phys:      0x%016llx, rx_q addr:      0x%p\n",
399                 (unsigned long long) dev_info.rx_phys, kni->rx_q);
400         pr_debug("alloc_phys:   0x%016llx, alloc_q addr:   0x%p\n",
401                 (unsigned long long) dev_info.alloc_phys, kni->alloc_q);
402         pr_debug("free_phys:    0x%016llx, free_q addr:    0x%p\n",
403                 (unsigned long long) dev_info.free_phys, kni->free_q);
404         pr_debug("req_phys:     0x%016llx, req_q addr:     0x%p\n",
405                 (unsigned long long) dev_info.req_phys, kni->req_q);
406         pr_debug("resp_phys:    0x%016llx, resp_q addr:    0x%p\n",
407                 (unsigned long long) dev_info.resp_phys, kni->resp_q);
408         pr_debug("mbuf_size:    %u\n", kni->mbuf_size);
409
410         pr_debug("PCI: %02x:%02x.%02x %04x:%04x\n",
411                                         dev_info.bus,
412                                         dev_info.devid,
413                                         dev_info.function,
414                                         dev_info.vendor_id,
415                                         dev_info.device_id);
416 #ifdef RTE_KNI_KMOD_ETHTOOL
417         pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
418
419         /* Support Ethtool */
420         while (pci) {
421                 pr_debug("pci_bus: %02x:%02x:%02x\n",
422                                         pci->bus->number,
423                                         PCI_SLOT(pci->devfn),
424                                         PCI_FUNC(pci->devfn));
425
426                 if ((pci->bus->number == dev_info.bus) &&
427                         (PCI_SLOT(pci->devfn) == dev_info.devid) &&
428                         (PCI_FUNC(pci->devfn) == dev_info.function)) {
429                         found_pci = pci;
430
431                         if (pci_match_id(ixgbe_pci_tbl, found_pci))
432                                 ret = ixgbe_kni_probe(found_pci, &lad_dev);
433                         else if (pci_match_id(igb_pci_tbl, found_pci))
434                                 ret = igb_kni_probe(found_pci, &lad_dev);
435                         else
436                                 ret = -1;
437
438                         pr_debug("PCI found: pci=0x%p, lad_dev=0x%p\n",
439                                                         pci, lad_dev);
440                         if (ret == 0) {
441                                 kni->lad_dev = lad_dev;
442                                 kni_set_ethtool_ops(kni->net_dev);
443                         } else {
444                                 pr_err("Device not supported by ethtool");
445                                 kni->lad_dev = NULL;
446                         }
447
448                         kni->pci_dev = found_pci;
449                         kni->device_id = dev_info.device_id;
450                         break;
451                 }
452                 pci = pci_get_device(dev_info.vendor_id,
453                                 dev_info.device_id, pci);
454         }
455         if (pci)
456                 pci_dev_put(pci);
457 #endif
458
459         if (kni->lad_dev)
460                 ether_addr_copy(net_dev->dev_addr, kni->lad_dev->dev_addr);
461         else
462                 /*
463                  * Generate random mac address. eth_random_addr() is the newer
464                  * version of generating mac address in linux kernel.
465                  */
466                 random_ether_addr(net_dev->dev_addr);
467
468         ret = register_netdev(net_dev);
469         if (ret) {
470                 pr_err("error %i registering device \"%s\"\n",
471                                         ret, dev_info.name);
472                 kni->net_dev = NULL;
473                 kni_dev_remove(kni);
474                 free_netdev(net_dev);
475                 return -ENODEV;
476         }
477
478         ret = kni_run_thread(knet, kni, dev_info.force_bind);
479         if (ret != 0)
480                 return ret;
481
482         down_write(&knet->kni_list_lock);
483         list_add(&kni->list, &knet->kni_list_head);
484         up_write(&knet->kni_list_lock);
485
486         return 0;
487 }
488
489 static int
490 kni_ioctl_release(struct net *net, uint32_t ioctl_num,
491                 unsigned long ioctl_param)
492 {
493         struct kni_net *knet = net_generic(net, kni_net_id);
494         int ret = -EINVAL;
495         struct kni_dev *dev, *n;
496         struct rte_kni_device_info dev_info;
497
498         if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
499                 return -EINVAL;
500
501         ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
502         if (ret) {
503                 pr_err("copy_from_user in kni_ioctl_release");
504                 return -EIO;
505         }
506
507         /* Release the network device according to its name */
508         if (strlen(dev_info.name) == 0)
509                 return ret;
510
511         down_write(&knet->kni_list_lock);
512         list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
513                 if (strncmp(dev->name, dev_info.name, RTE_KNI_NAMESIZE) != 0)
514                         continue;
515
516                 if (multiple_kthread_on && dev->pthread != NULL) {
517                         kthread_stop(dev->pthread);
518                         dev->pthread = NULL;
519                 }
520
521                 kni_dev_remove(dev);
522                 list_del(&dev->list);
523                 ret = 0;
524                 break;
525         }
526         up_write(&knet->kni_list_lock);
527         pr_info("%s release kni named %s\n",
528                 (ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
529
530         return ret;
531 }
532
533 static int
534 kni_ioctl(struct inode *inode, uint32_t ioctl_num, unsigned long ioctl_param)
535 {
536         int ret = -EINVAL;
537         struct net *net = current->nsproxy->net_ns;
538
539         pr_debug("IOCTL num=0x%0x param=0x%0lx\n", ioctl_num, ioctl_param);
540
541         /*
542          * Switch according to the ioctl called
543          */
544         switch (_IOC_NR(ioctl_num)) {
545         case _IOC_NR(RTE_KNI_IOCTL_TEST):
546                 /* For test only, not used */
547                 break;
548         case _IOC_NR(RTE_KNI_IOCTL_CREATE):
549                 ret = kni_ioctl_create(net, ioctl_num, ioctl_param);
550                 break;
551         case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
552                 ret = kni_ioctl_release(net, ioctl_num, ioctl_param);
553                 break;
554         default:
555                 pr_debug("IOCTL default\n");
556                 break;
557         }
558
559         return ret;
560 }
561
562 static int
563 kni_compat_ioctl(struct inode *inode, uint32_t ioctl_num,
564                 unsigned long ioctl_param)
565 {
566         /* 32 bits app on 64 bits OS to be supported later */
567         pr_debug("Not implemented.\n");
568
569         return -EINVAL;
570 }
571
572 static const struct file_operations kni_fops = {
573         .owner = THIS_MODULE,
574         .open = kni_open,
575         .release = kni_release,
576         .unlocked_ioctl = (void *)kni_ioctl,
577         .compat_ioctl = (void *)kni_compat_ioctl,
578 };
579
580 static struct miscdevice kni_misc = {
581         .minor = MISC_DYNAMIC_MINOR,
582         .name = KNI_DEVICE,
583         .fops = &kni_fops,
584 };
585
586 static int __init
587 kni_parse_kthread_mode(void)
588 {
589         if (!kthread_mode)
590                 return 0;
591
592         if (strcmp(kthread_mode, "single") == 0)
593                 return 0;
594         else if (strcmp(kthread_mode, "multiple") == 0)
595                 multiple_kthread_on = 1;
596         else
597                 return -1;
598
599         return 0;
600 }
601
602 static int __init
603 kni_init(void)
604 {
605         int rc;
606
607         if (kni_parse_kthread_mode() < 0) {
608                 pr_err("Invalid parameter for kthread_mode\n");
609                 return -EINVAL;
610         }
611
612         if (multiple_kthread_on == 0)
613                 pr_debug("Single kernel thread for all KNI devices\n");
614         else
615                 pr_debug("Multiple kernel thread mode enabled\n");
616
617 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
618         rc = register_pernet_subsys(&kni_net_ops);
619 #else
620         rc = register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
621 #endif
622         if (rc)
623                 return -EPERM;
624
625         rc = misc_register(&kni_misc);
626         if (rc != 0) {
627                 pr_err("Misc registration failed\n");
628                 goto out;
629         }
630
631         /* Configure the lo mode according to the input parameter */
632         kni_net_config_lo_mode(lo_mode);
633
634         return 0;
635
636 out:
637 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
638         unregister_pernet_subsys(&kni_net_ops);
639 #else
640         unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
641 #endif
642         return rc;
643 }
644
645 static void __exit
646 kni_exit(void)
647 {
648         misc_deregister(&kni_misc);
649 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
650         unregister_pernet_subsys(&kni_net_ops);
651 #else
652         unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
653 #endif
654 }
655
656 module_init(kni_init);
657 module_exit(kni_exit);
658
659 module_param(lo_mode, charp, S_IRUGO | S_IWUSR);
660 MODULE_PARM_DESC(lo_mode,
661 "KNI loopback mode (default=lo_mode_none):\n"
662 "    lo_mode_none        Kernel loopback disabled\n"
663 "    lo_mode_fifo        Enable kernel loopback with fifo\n"
664 "    lo_mode_fifo_skb    Enable kernel loopback with fifo and skb buffer\n"
665 "\n"
666 );
667
668 module_param(kthread_mode, charp, S_IRUGO);
669 MODULE_PARM_DESC(kthread_mode,
670 "Kernel thread mode (default=single):\n"
671 "    single    Single kernel thread mode enabled.\n"
672 "    multiple  Multiple kernel thread mode enabled.\n"
673 "\n"
674 );