Imported Upstream version 16.11
[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 #ifdef RTE_KNI_VHOST
144                                 kni_chk_vhost_rx(dev);
145 #else
146                                 kni_net_rx(dev);
147 #endif
148                                 kni_net_poll_resp(dev);
149                         }
150                 }
151                 up_read(&knet->kni_list_lock);
152 #ifdef RTE_KNI_PREEMPT_DEFAULT
153                 /* reschedule out for a while */
154                 schedule_timeout_interruptible(
155                         usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
156 #endif
157         }
158
159         return 0;
160 }
161
162 static int
163 kni_thread_multiple(void *param)
164 {
165         int j;
166         struct kni_dev *dev = (struct kni_dev *)param;
167
168         while (!kthread_should_stop()) {
169                 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
170 #ifdef RTE_KNI_VHOST
171                         kni_chk_vhost_rx(dev);
172 #else
173                         kni_net_rx(dev);
174 #endif
175                         kni_net_poll_resp(dev);
176                 }
177 #ifdef RTE_KNI_PREEMPT_DEFAULT
178                 schedule_timeout_interruptible(
179                         usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
180 #endif
181         }
182
183         return 0;
184 }
185
186 static int
187 kni_open(struct inode *inode, struct file *file)
188 {
189         struct net *net = current->nsproxy->net_ns;
190         struct kni_net *knet = net_generic(net, kni_net_id);
191
192         /* kni device can be opened by one user only per netns */
193         if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use))
194                 return -EBUSY;
195
196         file->private_data = get_net(net);
197         pr_debug("/dev/kni opened\n");
198
199         return 0;
200 }
201
202 static int
203 kni_dev_remove(struct kni_dev *dev)
204 {
205         if (!dev)
206                 return -ENODEV;
207
208         if (dev->pci_dev) {
209                 if (pci_match_id(ixgbe_pci_tbl, dev->pci_dev))
210                         ixgbe_kni_remove(dev->pci_dev);
211                 else if (pci_match_id(igb_pci_tbl, dev->pci_dev))
212                         igb_kni_remove(dev->pci_dev);
213         }
214
215         if (dev->net_dev) {
216                 unregister_netdev(dev->net_dev);
217                 free_netdev(dev->net_dev);
218         }
219
220         return 0;
221 }
222
223 static int
224 kni_release(struct inode *inode, struct file *file)
225 {
226         struct net *net = file->private_data;
227         struct kni_net *knet = net_generic(net, kni_net_id);
228         struct kni_dev *dev, *n;
229
230         /* Stop kernel thread for single mode */
231         if (multiple_kthread_on == 0) {
232                 mutex_lock(&knet->kni_kthread_lock);
233                 /* Stop kernel thread */
234                 if (knet->kni_kthread != NULL) {
235                         kthread_stop(knet->kni_kthread);
236                         knet->kni_kthread = NULL;
237                 }
238                 mutex_unlock(&knet->kni_kthread_lock);
239         }
240
241         down_write(&knet->kni_list_lock);
242         list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
243                 /* Stop kernel thread for multiple mode */
244                 if (multiple_kthread_on && dev->pthread != NULL) {
245                         kthread_stop(dev->pthread);
246                         dev->pthread = NULL;
247                 }
248
249 #ifdef RTE_KNI_VHOST
250                 kni_vhost_backend_release(dev);
251 #endif
252                 kni_dev_remove(dev);
253                 list_del(&dev->list);
254         }
255         up_write(&knet->kni_list_lock);
256
257         /* Clear the bit of device in use */
258         clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
259
260         put_net(net);
261         pr_debug("/dev/kni closed\n");
262
263         return 0;
264 }
265
266 static int
267 kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
268 {
269         if (!kni || !dev)
270                 return -1;
271
272         /* Check if network name has been used */
273         if (!strncmp(kni->name, dev->name, RTE_KNI_NAMESIZE)) {
274                 pr_err("KNI name %s duplicated\n", dev->name);
275                 return -1;
276         }
277
278         return 0;
279 }
280
281 static int
282 kni_run_thread(struct kni_net *knet, struct kni_dev *kni, uint8_t force_bind)
283 {
284         /**
285          * Create a new kernel thread for multiple mode, set its core affinity,
286          * and finally wake it up.
287          */
288         if (multiple_kthread_on) {
289                 kni->pthread = kthread_create(kni_thread_multiple,
290                         (void *)kni, "kni_%s", kni->name);
291                 if (IS_ERR(kni->pthread)) {
292                         kni_dev_remove(kni);
293                         return -ECANCELED;
294                 }
295
296                 if (force_bind)
297                         kthread_bind(kni->pthread, kni->core_id);
298                 wake_up_process(kni->pthread);
299         } else {
300                 mutex_lock(&knet->kni_kthread_lock);
301
302                 if (knet->kni_kthread == NULL) {
303                         knet->kni_kthread = kthread_create(kni_thread_single,
304                                 (void *)knet, "kni_single");
305                         if (IS_ERR(knet->kni_kthread)) {
306                                 mutex_unlock(&knet->kni_kthread_lock);
307                                 kni_dev_remove(kni);
308                                 return -ECANCELED;
309                         }
310
311                         if (force_bind)
312                                 kthread_bind(knet->kni_kthread, kni->core_id);
313                         wake_up_process(knet->kni_kthread);
314                 }
315
316                 mutex_unlock(&knet->kni_kthread_lock);
317         }
318
319         return 0;
320 }
321
322 static int
323 kni_ioctl_create(struct net *net, uint32_t ioctl_num,
324                 unsigned long ioctl_param)
325 {
326         struct kni_net *knet = net_generic(net, kni_net_id);
327         int ret;
328         struct rte_kni_device_info dev_info;
329         struct pci_dev *pci = NULL;
330         struct pci_dev *found_pci = NULL;
331         struct net_device *net_dev = NULL;
332         struct net_device *lad_dev = NULL;
333         struct kni_dev *kni, *dev, *n;
334
335         pr_info("Creating kni...\n");
336         /* Check the buffer size, to avoid warning */
337         if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
338                 return -EINVAL;
339
340         /* Copy kni info from user space */
341         ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
342         if (ret) {
343                 pr_err("copy_from_user in kni_ioctl_create");
344                 return -EIO;
345         }
346
347         /**
348          * Check if the cpu core id is valid for binding.
349          */
350         if (dev_info.force_bind && !cpu_online(dev_info.core_id)) {
351                 pr_err("cpu %u is not online\n", dev_info.core_id);
352                 return -EINVAL;
353         }
354
355         /* Check if it has been created */
356         down_read(&knet->kni_list_lock);
357         list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
358                 if (kni_check_param(dev, &dev_info) < 0) {
359                         up_read(&knet->kni_list_lock);
360                         return -EINVAL;
361                 }
362         }
363         up_read(&knet->kni_list_lock);
364
365         net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
366 #ifdef NET_NAME_UNKNOWN
367                                                         NET_NAME_UNKNOWN,
368 #endif
369                                                         kni_net_init);
370         if (net_dev == NULL) {
371                 pr_err("error allocating device \"%s\"\n", dev_info.name);
372                 return -EBUSY;
373         }
374
375         dev_net_set(net_dev, net);
376
377         kni = netdev_priv(net_dev);
378
379         kni->net_dev = net_dev;
380         kni->group_id = dev_info.group_id;
381         kni->core_id = dev_info.core_id;
382         strncpy(kni->name, dev_info.name, RTE_KNI_NAMESIZE);
383
384         /* Translate user space info into kernel space info */
385         kni->tx_q = phys_to_virt(dev_info.tx_phys);
386         kni->rx_q = phys_to_virt(dev_info.rx_phys);
387         kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
388         kni->free_q = phys_to_virt(dev_info.free_phys);
389
390         kni->req_q = phys_to_virt(dev_info.req_phys);
391         kni->resp_q = phys_to_virt(dev_info.resp_phys);
392         kni->sync_va = dev_info.sync_va;
393         kni->sync_kva = phys_to_virt(dev_info.sync_phys);
394
395 #ifdef RTE_KNI_VHOST
396         kni->vhost_queue = NULL;
397         kni->vq_status = BE_STOP;
398 #endif
399         kni->mbuf_size = dev_info.mbuf_size;
400
401         pr_debug("tx_phys:      0x%016llx, tx_q addr:      0x%p\n",
402                 (unsigned long long) dev_info.tx_phys, kni->tx_q);
403         pr_debug("rx_phys:      0x%016llx, rx_q addr:      0x%p\n",
404                 (unsigned long long) dev_info.rx_phys, kni->rx_q);
405         pr_debug("alloc_phys:   0x%016llx, alloc_q addr:   0x%p\n",
406                 (unsigned long long) dev_info.alloc_phys, kni->alloc_q);
407         pr_debug("free_phys:    0x%016llx, free_q addr:    0x%p\n",
408                 (unsigned long long) dev_info.free_phys, kni->free_q);
409         pr_debug("req_phys:     0x%016llx, req_q addr:     0x%p\n",
410                 (unsigned long long) dev_info.req_phys, kni->req_q);
411         pr_debug("resp_phys:    0x%016llx, resp_q addr:    0x%p\n",
412                 (unsigned long long) dev_info.resp_phys, kni->resp_q);
413         pr_debug("mbuf_size:    %u\n", kni->mbuf_size);
414
415         pr_debug("PCI: %02x:%02x.%02x %04x:%04x\n",
416                                         dev_info.bus,
417                                         dev_info.devid,
418                                         dev_info.function,
419                                         dev_info.vendor_id,
420                                         dev_info.device_id);
421
422         pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
423
424         /* Support Ethtool */
425         while (pci) {
426                 pr_debug("pci_bus: %02x:%02x:%02x\n",
427                                         pci->bus->number,
428                                         PCI_SLOT(pci->devfn),
429                                         PCI_FUNC(pci->devfn));
430
431                 if ((pci->bus->number == dev_info.bus) &&
432                         (PCI_SLOT(pci->devfn) == dev_info.devid) &&
433                         (PCI_FUNC(pci->devfn) == dev_info.function)) {
434                         found_pci = pci;
435
436                         if (pci_match_id(ixgbe_pci_tbl, found_pci))
437                                 ret = ixgbe_kni_probe(found_pci, &lad_dev);
438                         else if (pci_match_id(igb_pci_tbl, found_pci))
439                                 ret = igb_kni_probe(found_pci, &lad_dev);
440                         else
441                                 ret = -1;
442
443                         pr_debug("PCI found: pci=0x%p, lad_dev=0x%p\n",
444                                                         pci, lad_dev);
445                         if (ret == 0) {
446                                 kni->lad_dev = lad_dev;
447                                 kni_set_ethtool_ops(kni->net_dev);
448                         } else {
449                                 pr_err("Device not supported by ethtool");
450                                 kni->lad_dev = NULL;
451                         }
452
453                         kni->pci_dev = found_pci;
454                         kni->device_id = dev_info.device_id;
455                         break;
456                 }
457                 pci = pci_get_device(dev_info.vendor_id,
458                                 dev_info.device_id, pci);
459         }
460         if (pci)
461                 pci_dev_put(pci);
462
463         if (kni->lad_dev)
464                 ether_addr_copy(net_dev->dev_addr, kni->lad_dev->dev_addr);
465         else
466                 /*
467                  * Generate random mac address. eth_random_addr() is the newer
468                  * version of generating mac address in linux kernel.
469                  */
470                 random_ether_addr(net_dev->dev_addr);
471
472         ret = register_netdev(net_dev);
473         if (ret) {
474                 pr_err("error %i registering device \"%s\"\n",
475                                         ret, dev_info.name);
476                 kni->net_dev = NULL;
477                 kni_dev_remove(kni);
478                 free_netdev(net_dev);
479                 return -ENODEV;
480         }
481
482 #ifdef RTE_KNI_VHOST
483         kni_vhost_init(kni);
484 #endif
485
486         ret = kni_run_thread(knet, kni, dev_info.force_bind);
487         if (ret != 0)
488                 return ret;
489
490         down_write(&knet->kni_list_lock);
491         list_add(&kni->list, &knet->kni_list_head);
492         up_write(&knet->kni_list_lock);
493
494         return 0;
495 }
496
497 static int
498 kni_ioctl_release(struct net *net, uint32_t ioctl_num,
499                 unsigned long ioctl_param)
500 {
501         struct kni_net *knet = net_generic(net, kni_net_id);
502         int ret = -EINVAL;
503         struct kni_dev *dev, *n;
504         struct rte_kni_device_info dev_info;
505
506         if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
507                 return -EINVAL;
508
509         ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
510         if (ret) {
511                 pr_err("copy_from_user in kni_ioctl_release");
512                 return -EIO;
513         }
514
515         /* Release the network device according to its name */
516         if (strlen(dev_info.name) == 0)
517                 return ret;
518
519         down_write(&knet->kni_list_lock);
520         list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
521                 if (strncmp(dev->name, dev_info.name, RTE_KNI_NAMESIZE) != 0)
522                         continue;
523
524                 if (multiple_kthread_on && dev->pthread != NULL) {
525                         kthread_stop(dev->pthread);
526                         dev->pthread = NULL;
527                 }
528
529 #ifdef RTE_KNI_VHOST
530                 kni_vhost_backend_release(dev);
531 #endif
532                 kni_dev_remove(dev);
533                 list_del(&dev->list);
534                 ret = 0;
535                 break;
536         }
537         up_write(&knet->kni_list_lock);
538         pr_info("%s release kni named %s\n",
539                 (ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
540
541         return ret;
542 }
543
544 static int
545 kni_ioctl(struct inode *inode, uint32_t ioctl_num, unsigned long ioctl_param)
546 {
547         int ret = -EINVAL;
548         struct net *net = current->nsproxy->net_ns;
549
550         pr_debug("IOCTL num=0x%0x param=0x%0lx\n", ioctl_num, ioctl_param);
551
552         /*
553          * Switch according to the ioctl called
554          */
555         switch (_IOC_NR(ioctl_num)) {
556         case _IOC_NR(RTE_KNI_IOCTL_TEST):
557                 /* For test only, not used */
558                 break;
559         case _IOC_NR(RTE_KNI_IOCTL_CREATE):
560                 ret = kni_ioctl_create(net, ioctl_num, ioctl_param);
561                 break;
562         case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
563                 ret = kni_ioctl_release(net, ioctl_num, ioctl_param);
564                 break;
565         default:
566                 pr_debug("IOCTL default\n");
567                 break;
568         }
569
570         return ret;
571 }
572
573 static int
574 kni_compat_ioctl(struct inode *inode, uint32_t ioctl_num,
575                 unsigned long ioctl_param)
576 {
577         /* 32 bits app on 64 bits OS to be supported later */
578         pr_debug("Not implemented.\n");
579
580         return -EINVAL;
581 }
582
583 static const struct file_operations kni_fops = {
584         .owner = THIS_MODULE,
585         .open = kni_open,
586         .release = kni_release,
587         .unlocked_ioctl = (void *)kni_ioctl,
588         .compat_ioctl = (void *)kni_compat_ioctl,
589 };
590
591 static struct miscdevice kni_misc = {
592         .minor = MISC_DYNAMIC_MINOR,
593         .name = KNI_DEVICE,
594         .fops = &kni_fops,
595 };
596
597 static int __init
598 kni_parse_kthread_mode(void)
599 {
600         if (!kthread_mode)
601                 return 0;
602
603         if (strcmp(kthread_mode, "single") == 0)
604                 return 0;
605         else if (strcmp(kthread_mode, "multiple") == 0)
606                 multiple_kthread_on = 1;
607         else
608                 return -1;
609
610         return 0;
611 }
612
613 static int __init
614 kni_init(void)
615 {
616         int rc;
617
618         if (kni_parse_kthread_mode() < 0) {
619                 pr_err("Invalid parameter for kthread_mode\n");
620                 return -EINVAL;
621         }
622
623         if (multiple_kthread_on == 0)
624                 pr_debug("Single kernel thread for all KNI devices\n");
625         else
626                 pr_debug("Multiple kernel thread mode enabled\n");
627
628 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
629         rc = register_pernet_subsys(&kni_net_ops);
630 #else
631         rc = register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
632 #endif
633         if (rc)
634                 return -EPERM;
635
636         rc = misc_register(&kni_misc);
637         if (rc != 0) {
638                 pr_err("Misc registration failed\n");
639                 goto out;
640         }
641
642         /* Configure the lo mode according to the input parameter */
643         kni_net_config_lo_mode(lo_mode);
644
645         return 0;
646
647 out:
648 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
649         unregister_pernet_subsys(&kni_net_ops);
650 #else
651         unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
652 #endif
653         return rc;
654 }
655
656 static void __exit
657 kni_exit(void)
658 {
659         misc_deregister(&kni_misc);
660 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
661         unregister_pernet_subsys(&kni_net_ops);
662 #else
663         unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
664 #endif
665 }
666
667 module_init(kni_init);
668 module_exit(kni_exit);
669
670 module_param(lo_mode, charp, S_IRUGO | S_IWUSR);
671 MODULE_PARM_DESC(lo_mode,
672 "KNI loopback mode (default=lo_mode_none):\n"
673 "    lo_mode_none        Kernel loopback disabled\n"
674 "    lo_mode_fifo        Enable kernel loopback with fifo\n"
675 "    lo_mode_fifo_skb    Enable kernel loopback with fifo and skb buffer\n"
676 "\n"
677 );
678
679 module_param(kthread_mode, charp, S_IRUGO);
680 MODULE_PARM_DESC(kthread_mode,
681 "Kernel thread mode (default=single):\n"
682 "    single    Single kernel thread mode enabled.\n"
683 "    multiple  Multiple kernel thread mode enabled.\n"
684 "\n"
685 );