4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
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.
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.
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.
21 * Contact Information:
25 #include <linux/version.h>
26 #include <linux/module.h>
27 #include <linux/miscdevice.h>
28 #include <linux/netdevice.h>
29 #include <linux/pci.h>
30 #include <linux/kthread.h>
31 #include <linux/rwsem.h>
32 #include <linux/nsproxy.h>
33 #include <net/net_namespace.h>
34 #include <net/netns/generic.h>
36 #include <exec-env/rte_kni_common.h>
39 MODULE_LICENSE("Dual BSD/GPL");
40 MODULE_AUTHOR("Intel Corporation");
41 MODULE_DESCRIPTION("Kernel Module for managing kni devices");
43 #define KNI_RX_LOOP_NUM 1000
45 #define KNI_MAX_DEVICES 32
47 extern void kni_net_rx(struct kni_dev *kni);
48 extern void kni_net_init(struct net_device *dev);
49 extern void kni_net_config_lo_mode(char *lo_str);
50 extern void kni_net_poll_resp(struct kni_dev *kni);
51 extern void kni_set_ethtool_ops(struct net_device *netdev);
53 extern int ixgbe_kni_probe(struct pci_dev *pdev, struct net_device **lad_dev);
54 extern void ixgbe_kni_remove(struct pci_dev *pdev);
55 extern int igb_kni_probe(struct pci_dev *pdev, struct net_device **lad_dev);
56 extern void igb_kni_remove(struct pci_dev *pdev);
58 static int kni_open(struct inode *inode, struct file *file);
59 static int kni_release(struct inode *inode, struct file *file);
60 static int kni_ioctl(struct inode *inode, unsigned int ioctl_num,
61 unsigned long ioctl_param);
62 static int kni_compat_ioctl(struct inode *inode, unsigned int ioctl_num,
63 unsigned long ioctl_param);
64 static int kni_dev_remove(struct kni_dev *dev);
66 static int __init kni_parse_kthread_mode(void);
68 /* KNI processing for single kernel thread mode */
69 static int kni_thread_single(void *unused);
70 /* KNI processing for multiple kernel thread mode */
71 static int kni_thread_multiple(void *param);
73 static struct file_operations kni_fops = {
76 .release = kni_release,
77 .unlocked_ioctl = (void *)kni_ioctl,
78 .compat_ioctl = (void *)kni_compat_ioctl,
81 static struct miscdevice kni_misc = {
82 .minor = MISC_DYNAMIC_MINOR,
88 static char *lo_mode = NULL;
90 /* Kernel thread mode */
91 static char *kthread_mode = NULL;
92 static unsigned multiple_kthread_on = 0;
94 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
96 static int kni_net_id;
99 unsigned long device_in_use; /* device in use flag */
100 struct task_struct *kni_kthread;
101 struct rw_semaphore kni_list_lock;
102 struct list_head kni_list_head;
105 static int __net_init kni_init_net(struct net *net)
107 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
108 struct kni_net *knet = net_generic(net, kni_net_id);
110 struct kni_net *knet;
113 knet = kmalloc(sizeof(struct kni_net), GFP_KERNEL);
118 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
120 /* Clear the bit of device in use */
121 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
123 init_rwsem(&knet->kni_list_lock);
124 INIT_LIST_HEAD(&knet->kni_list_head);
126 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
129 ret = net_assign_generic(net, kni_net_id, knet);
134 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
137 static void __net_exit kni_exit_net(struct net *net)
139 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
140 struct kni_net *knet = net_generic(net, kni_net_id);
143 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
146 static struct pernet_operations kni_net_ops = {
147 .init = kni_init_net,
148 .exit = kni_exit_net,
149 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
151 .size = sizeof(struct kni_net),
152 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
160 KNI_PRINT("######## DPDK kni module loading ########\n");
162 if (kni_parse_kthread_mode() < 0) {
163 KNI_ERR("Invalid parameter for kthread_mode\n");
167 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
168 rc = register_pernet_subsys(&kni_net_ops);
170 rc = register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
171 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
175 rc = misc_register(&kni_misc);
177 KNI_ERR("Misc registration failed\n");
181 /* Configure the lo mode according to the input parameter */
182 kni_net_config_lo_mode(lo_mode);
184 KNI_PRINT("######## DPDK kni module loaded ########\n");
189 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
190 unregister_pernet_subsys(&kni_net_ops);
192 register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
193 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
200 misc_deregister(&kni_misc);
201 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
202 unregister_pernet_subsys(&kni_net_ops);
204 register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
205 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
206 KNI_PRINT("####### DPDK kni module unloaded #######\n");
210 kni_parse_kthread_mode(void)
215 if (strcmp(kthread_mode, "single") == 0)
217 else if (strcmp(kthread_mode, "multiple") == 0)
218 multiple_kthread_on = 1;
226 kni_open(struct inode *inode, struct file *file)
228 struct net *net = current->nsproxy->net_ns;
229 struct kni_net *knet = net_generic(net, kni_net_id);
231 /* kni device can be opened by one user only per netns */
232 if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use))
235 /* Create kernel thread for single mode */
236 if (multiple_kthread_on == 0) {
237 KNI_PRINT("Single kernel thread for all KNI devices\n");
238 /* Create kernel thread for RX */
239 knet->kni_kthread = kthread_run(kni_thread_single, (void *)knet,
241 if (IS_ERR(knet->kni_kthread)) {
242 KNI_ERR("Unable to create kernel threaed\n");
243 return PTR_ERR(knet->kni_kthread);
246 KNI_PRINT("Multiple kernel thread mode enabled\n");
248 file->private_data = get_net(net);
249 KNI_PRINT("/dev/kni opened\n");
255 kni_release(struct inode *inode, struct file *file)
257 struct net *net = file->private_data;
258 struct kni_net *knet = net_generic(net, kni_net_id);
259 struct kni_dev *dev, *n;
261 /* Stop kernel thread for single mode */
262 if (multiple_kthread_on == 0) {
263 /* Stop kernel thread */
264 kthread_stop(knet->kni_kthread);
265 knet->kni_kthread = NULL;
268 down_write(&knet->kni_list_lock);
269 list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
270 /* Stop kernel thread for multiple mode */
271 if (multiple_kthread_on && dev->pthread != NULL) {
272 kthread_stop(dev->pthread);
277 kni_vhost_backend_release(dev);
280 list_del(&dev->list);
282 up_write(&knet->kni_list_lock);
284 /* Clear the bit of device in use */
285 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
288 KNI_PRINT("/dev/kni closed\n");
294 kni_thread_single(void *data)
296 struct kni_net *knet = data;
300 while (!kthread_should_stop()) {
301 down_read(&knet->kni_list_lock);
302 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
303 list_for_each_entry(dev, &knet->kni_list_head, list) {
305 kni_chk_vhost_rx(dev);
309 kni_net_poll_resp(dev);
312 up_read(&knet->kni_list_lock);
313 #ifdef RTE_KNI_PREEMPT_DEFAULT
314 /* reschedule out for a while */
315 schedule_timeout_interruptible(usecs_to_jiffies( \
316 KNI_KTHREAD_RESCHEDULE_INTERVAL));
324 kni_thread_multiple(void *param)
327 struct kni_dev *dev = (struct kni_dev *)param;
329 while (!kthread_should_stop()) {
330 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
332 kni_chk_vhost_rx(dev);
336 kni_net_poll_resp(dev);
338 #ifdef RTE_KNI_PREEMPT_DEFAULT
339 schedule_timeout_interruptible(usecs_to_jiffies( \
340 KNI_KTHREAD_RESCHEDULE_INTERVAL));
348 kni_dev_remove(struct kni_dev *dev)
353 switch (dev->device_id) {
354 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
355 #include <rte_pci_dev_ids.h>
356 igb_kni_remove(dev->pci_dev);
358 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
359 #include <rte_pci_dev_ids.h>
360 ixgbe_kni_remove(dev->pci_dev);
367 unregister_netdev(dev->net_dev);
368 free_netdev(dev->net_dev);
375 kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
380 /* Check if network name has been used */
381 if (!strncmp(kni->name, dev->name, RTE_KNI_NAMESIZE)) {
382 KNI_ERR("KNI name %s duplicated\n", dev->name);
390 kni_ioctl_create(struct net *net,
391 unsigned int ioctl_num, unsigned long ioctl_param)
393 struct kni_net *knet = net_generic(net, kni_net_id);
395 struct rte_kni_device_info dev_info;
396 struct pci_dev *pci = NULL;
397 struct pci_dev *found_pci = NULL;
398 struct net_device *net_dev = NULL;
399 struct net_device *lad_dev = NULL;
400 struct kni_dev *kni, *dev, *n;
402 printk(KERN_INFO "KNI: Creating kni...\n");
403 /* Check the buffer size, to avoid warning */
404 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
407 /* Copy kni info from user space */
408 ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
410 KNI_ERR("copy_from_user in kni_ioctl_create");
415 * Check if the cpu core id is valid for binding,
416 * for multiple kernel thread mode.
418 if (multiple_kthread_on && dev_info.force_bind &&
419 !cpu_online(dev_info.core_id)) {
420 KNI_ERR("cpu %u is not online\n", dev_info.core_id);
424 /* Check if it has been created */
425 down_read(&knet->kni_list_lock);
426 list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
427 if (kni_check_param(dev, &dev_info) < 0) {
428 up_read(&knet->kni_list_lock);
432 up_read(&knet->kni_list_lock);
434 net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
435 #ifdef NET_NAME_UNKNOWN
439 if (net_dev == NULL) {
440 KNI_ERR("error allocating device \"%s\"\n", dev_info.name);
444 dev_net_set(net_dev, net);
446 kni = netdev_priv(net_dev);
448 kni->net_dev = net_dev;
449 kni->group_id = dev_info.group_id;
450 kni->core_id = dev_info.core_id;
451 strncpy(kni->name, dev_info.name, RTE_KNI_NAMESIZE);
453 /* Translate user space info into kernel space info */
454 kni->tx_q = phys_to_virt(dev_info.tx_phys);
455 kni->rx_q = phys_to_virt(dev_info.rx_phys);
456 kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
457 kni->free_q = phys_to_virt(dev_info.free_phys);
459 kni->req_q = phys_to_virt(dev_info.req_phys);
460 kni->resp_q = phys_to_virt(dev_info.resp_phys);
461 kni->sync_va = dev_info.sync_va;
462 kni->sync_kva = phys_to_virt(dev_info.sync_phys);
464 kni->mbuf_kva = phys_to_virt(dev_info.mbuf_phys);
465 kni->mbuf_va = dev_info.mbuf_va;
468 kni->vhost_queue = NULL;
469 kni->vq_status = BE_STOP;
471 kni->mbuf_size = dev_info.mbuf_size;
473 KNI_PRINT("tx_phys: 0x%016llx, tx_q addr: 0x%p\n",
474 (unsigned long long) dev_info.tx_phys, kni->tx_q);
475 KNI_PRINT("rx_phys: 0x%016llx, rx_q addr: 0x%p\n",
476 (unsigned long long) dev_info.rx_phys, kni->rx_q);
477 KNI_PRINT("alloc_phys: 0x%016llx, alloc_q addr: 0x%p\n",
478 (unsigned long long) dev_info.alloc_phys, kni->alloc_q);
479 KNI_PRINT("free_phys: 0x%016llx, free_q addr: 0x%p\n",
480 (unsigned long long) dev_info.free_phys, kni->free_q);
481 KNI_PRINT("req_phys: 0x%016llx, req_q addr: 0x%p\n",
482 (unsigned long long) dev_info.req_phys, kni->req_q);
483 KNI_PRINT("resp_phys: 0x%016llx, resp_q addr: 0x%p\n",
484 (unsigned long long) dev_info.resp_phys, kni->resp_q);
485 KNI_PRINT("mbuf_phys: 0x%016llx, mbuf_kva: 0x%p\n",
486 (unsigned long long) dev_info.mbuf_phys, kni->mbuf_kva);
487 KNI_PRINT("mbuf_va: 0x%p\n", dev_info.mbuf_va);
488 KNI_PRINT("mbuf_size: %u\n", kni->mbuf_size);
490 KNI_DBG("PCI: %02x:%02x.%02x %04x:%04x\n",
497 pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
499 /* Support Ethtool */
501 KNI_PRINT("pci_bus: %02x:%02x:%02x \n",
503 PCI_SLOT(pci->devfn),
504 PCI_FUNC(pci->devfn));
506 if ((pci->bus->number == dev_info.bus) &&
507 (PCI_SLOT(pci->devfn) == dev_info.devid) &&
508 (PCI_FUNC(pci->devfn) == dev_info.function)) {
510 switch (dev_info.device_id) {
511 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
512 #include <rte_pci_dev_ids.h>
513 ret = igb_kni_probe(found_pci, &lad_dev);
515 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) \
517 #include <rte_pci_dev_ids.h>
518 ret = ixgbe_kni_probe(found_pci, &lad_dev);
525 KNI_DBG("PCI found: pci=0x%p, lad_dev=0x%p\n",
528 kni->lad_dev = lad_dev;
529 kni_set_ethtool_ops(kni->net_dev);
531 KNI_ERR("Device not supported by ethtool");
535 kni->pci_dev = found_pci;
536 kni->device_id = dev_info.device_id;
539 pci = pci_get_device(dev_info.vendor_id,
540 dev_info.device_id, pci);
545 ret = register_netdev(net_dev);
547 KNI_ERR("error %i registering device \"%s\"\n",
558 * Create a new kernel thread for multiple mode, set its core affinity,
559 * and finally wake it up.
561 if (multiple_kthread_on) {
562 kni->pthread = kthread_create(kni_thread_multiple,
564 "kni_%s", kni->name);
565 if (IS_ERR(kni->pthread)) {
569 if (dev_info.force_bind)
570 kthread_bind(kni->pthread, kni->core_id);
571 wake_up_process(kni->pthread);
574 down_write(&knet->kni_list_lock);
575 list_add(&kni->list, &knet->kni_list_head);
576 up_write(&knet->kni_list_lock);
582 kni_ioctl_release(struct net *net,
583 unsigned int ioctl_num, unsigned long ioctl_param)
585 struct kni_net *knet = net_generic(net, kni_net_id);
587 struct kni_dev *dev, *n;
588 struct rte_kni_device_info dev_info;
590 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
593 ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
595 KNI_ERR("copy_from_user in kni_ioctl_release");
599 /* Release the network device according to its name */
600 if (strlen(dev_info.name) == 0)
603 down_write(&knet->kni_list_lock);
604 list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
605 if (strncmp(dev->name, dev_info.name, RTE_KNI_NAMESIZE) != 0)
608 if (multiple_kthread_on && dev->pthread != NULL) {
609 kthread_stop(dev->pthread);
614 kni_vhost_backend_release(dev);
617 list_del(&dev->list);
621 up_write(&knet->kni_list_lock);
622 printk(KERN_INFO "KNI: %s release kni named %s\n",
623 (ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
629 kni_ioctl(struct inode *inode,
630 unsigned int ioctl_num,
631 unsigned long ioctl_param)
634 struct net *net = current->nsproxy->net_ns;
636 KNI_DBG("IOCTL num=0x%0x param=0x%0lx\n", ioctl_num, ioctl_param);
639 * Switch according to the ioctl called
641 switch (_IOC_NR(ioctl_num)) {
642 case _IOC_NR(RTE_KNI_IOCTL_TEST):
643 /* For test only, not used */
645 case _IOC_NR(RTE_KNI_IOCTL_CREATE):
646 ret = kni_ioctl_create(net, ioctl_num, ioctl_param);
648 case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
649 ret = kni_ioctl_release(net, ioctl_num, ioctl_param);
652 KNI_DBG("IOCTL default\n");
660 kni_compat_ioctl(struct inode *inode,
661 unsigned int ioctl_num,
662 unsigned long ioctl_param)
664 /* 32 bits app on 64 bits OS to be supported later */
665 KNI_PRINT("Not implemented.\n");
670 module_init(kni_init);
671 module_exit(kni_exit);
673 module_param(lo_mode, charp, S_IRUGO | S_IWUSR);
674 MODULE_PARM_DESC(lo_mode,
675 "KNI loopback mode (default=lo_mode_none):\n"
676 " lo_mode_none Kernel loopback disabled\n"
677 " lo_mode_fifo Enable kernel loopback with fifo\n"
678 " lo_mode_fifo_skb Enable kernel loopback with fifo and skb buffer\n"
682 module_param(kthread_mode, charp, S_IRUGO);
683 MODULE_PARM_DESC(kthread_mode,
684 "Kernel thread mode (default=single):\n"
685 " single Single kernel thread mode enabled.\n"
686 " multiple Multiple kernel thread mode enabled.\n"