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