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