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>
38 #include <rte_config.h>
40 MODULE_LICENSE("Dual BSD/GPL");
41 MODULE_AUTHOR("Intel Corporation");
42 MODULE_DESCRIPTION("Kernel Module for managing kni devices");
44 #define KNI_RX_LOOP_NUM 1000
46 #define KNI_MAX_DEVICES 32
48 extern void kni_net_rx(struct kni_dev *kni);
49 extern void kni_net_init(struct net_device *dev);
50 extern void kni_net_config_lo_mode(char *lo_str);
51 extern void kni_net_poll_resp(struct kni_dev *kni);
52 extern void kni_set_ethtool_ops(struct net_device *netdev);
54 extern int ixgbe_kni_probe(struct pci_dev *pdev, struct net_device **lad_dev);
55 extern void ixgbe_kni_remove(struct pci_dev *pdev);
56 extern int igb_kni_probe(struct pci_dev *pdev, struct net_device **lad_dev);
57 extern void igb_kni_remove(struct pci_dev *pdev);
59 static int kni_open(struct inode *inode, struct file *file);
60 static int kni_release(struct inode *inode, struct file *file);
61 static int kni_ioctl(struct inode *inode, unsigned int ioctl_num,
62 unsigned long ioctl_param);
63 static int kni_compat_ioctl(struct inode *inode, unsigned int ioctl_num,
64 unsigned long ioctl_param);
65 static int kni_dev_remove(struct kni_dev *dev);
67 static int __init kni_parse_kthread_mode(void);
69 /* KNI processing for single kernel thread mode */
70 static int kni_thread_single(void *unused);
71 /* KNI processing for multiple kernel thread mode */
72 static int kni_thread_multiple(void *param);
74 static struct file_operations kni_fops = {
77 .release = kni_release,
78 .unlocked_ioctl = (void *)kni_ioctl,
79 .compat_ioctl = (void *)kni_compat_ioctl,
82 static struct miscdevice kni_misc = {
83 .minor = MISC_DYNAMIC_MINOR,
89 static char *lo_mode = NULL;
91 /* Kernel thread mode */
92 static char *kthread_mode = NULL;
93 static unsigned multiple_kthread_on = 0;
95 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
97 static int kni_net_id;
100 unsigned long device_in_use; /* device in use flag */
101 struct task_struct *kni_kthread;
102 struct rw_semaphore kni_list_lock;
103 struct list_head kni_list_head;
106 static int __net_init kni_init_net(struct net *net)
108 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
109 struct kni_net *knet = net_generic(net, kni_net_id);
111 struct kni_net *knet;
114 knet = kmalloc(sizeof(struct kni_net), GFP_KERNEL);
119 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
121 /* Clear the bit of device in use */
122 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
124 init_rwsem(&knet->kni_list_lock);
125 INIT_LIST_HEAD(&knet->kni_list_head);
127 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
130 ret = net_assign_generic(net, kni_net_id, knet);
135 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
138 static void __net_exit kni_exit_net(struct net *net)
140 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
141 struct kni_net *knet = net_generic(net, kni_net_id);
144 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
147 static struct pernet_operations kni_net_ops = {
148 .init = kni_init_net,
149 .exit = kni_exit_net,
150 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
152 .size = sizeof(struct kni_net),
153 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
161 KNI_PRINT("######## DPDK kni module loading ########\n");
163 if (kni_parse_kthread_mode() < 0) {
164 KNI_ERR("Invalid parameter for kthread_mode\n");
168 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
169 rc = register_pernet_subsys(&kni_net_ops);
171 rc = register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
172 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
176 rc = misc_register(&kni_misc);
178 KNI_ERR("Misc registration failed\n");
182 /* Configure the lo mode according to the input parameter */
183 kni_net_config_lo_mode(lo_mode);
185 KNI_PRINT("######## DPDK kni module loaded ########\n");
190 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
191 unregister_pernet_subsys(&kni_net_ops);
193 register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
194 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
201 misc_deregister(&kni_misc);
202 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32)
203 unregister_pernet_subsys(&kni_net_ops);
205 register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
206 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 32) */
207 KNI_PRINT("####### DPDK kni module unloaded #######\n");
211 kni_parse_kthread_mode(void)
216 if (strcmp(kthread_mode, "single") == 0)
218 else if (strcmp(kthread_mode, "multiple") == 0)
219 multiple_kthread_on = 1;
227 kni_open(struct inode *inode, struct file *file)
229 struct net *net = current->nsproxy->net_ns;
230 struct kni_net *knet = net_generic(net, kni_net_id);
232 /* kni device can be opened by one user only per netns */
233 if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use))
236 /* Create kernel thread for single mode */
237 if (multiple_kthread_on == 0) {
238 KNI_PRINT("Single kernel thread for all KNI devices\n");
239 /* Create kernel thread for RX */
240 knet->kni_kthread = kthread_run(kni_thread_single, (void *)knet,
242 if (IS_ERR(knet->kni_kthread)) {
243 KNI_ERR("Unable to create kernel threaed\n");
244 return PTR_ERR(knet->kni_kthread);
247 KNI_PRINT("Multiple kernel thread mode enabled\n");
249 file->private_data = get_net(net);
250 KNI_PRINT("/dev/kni opened\n");
256 kni_release(struct inode *inode, struct file *file)
258 struct net *net = file->private_data;
259 struct kni_net *knet = net_generic(net, kni_net_id);
260 struct kni_dev *dev, *n;
262 /* Stop kernel thread for single mode */
263 if (multiple_kthread_on == 0) {
264 /* Stop kernel thread */
265 kthread_stop(knet->kni_kthread);
266 knet->kni_kthread = NULL;
269 down_write(&knet->kni_list_lock);
270 list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
271 /* Stop kernel thread for multiple mode */
272 if (multiple_kthread_on && dev->pthread != NULL) {
273 kthread_stop(dev->pthread);
278 kni_vhost_backend_release(dev);
281 list_del(&dev->list);
283 up_write(&knet->kni_list_lock);
285 /* Clear the bit of device in use */
286 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
289 KNI_PRINT("/dev/kni closed\n");
295 kni_thread_single(void *data)
297 struct kni_net *knet = data;
301 while (!kthread_should_stop()) {
302 down_read(&knet->kni_list_lock);
303 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
304 list_for_each_entry(dev, &knet->kni_list_head, list) {
306 kni_chk_vhost_rx(dev);
310 kni_net_poll_resp(dev);
313 up_read(&knet->kni_list_lock);
314 #ifdef RTE_KNI_PREEMPT_DEFAULT
315 /* reschedule out for a while */
316 schedule_timeout_interruptible(usecs_to_jiffies( \
317 KNI_KTHREAD_RESCHEDULE_INTERVAL));
325 kni_thread_multiple(void *param)
328 struct kni_dev *dev = (struct kni_dev *)param;
330 while (!kthread_should_stop()) {
331 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
333 kni_chk_vhost_rx(dev);
337 kni_net_poll_resp(dev);
339 #ifdef RTE_KNI_PREEMPT_DEFAULT
340 schedule_timeout_interruptible(usecs_to_jiffies( \
341 KNI_KTHREAD_RESCHEDULE_INTERVAL));
349 kni_dev_remove(struct kni_dev *dev)
354 switch (dev->device_id) {
355 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
356 #include <rte_pci_dev_ids.h>
357 igb_kni_remove(dev->pci_dev);
359 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
360 #include <rte_pci_dev_ids.h>
361 ixgbe_kni_remove(dev->pci_dev);
368 unregister_netdev(dev->net_dev);
369 free_netdev(dev->net_dev);
376 kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
381 /* Check if network name has been used */
382 if (!strncmp(kni->name, dev->name, RTE_KNI_NAMESIZE)) {
383 KNI_ERR("KNI name %s duplicated\n", dev->name);
391 kni_ioctl_create(struct net *net,
392 unsigned int ioctl_num, unsigned long ioctl_param)
394 struct kni_net *knet = net_generic(net, kni_net_id);
396 struct rte_kni_device_info dev_info;
397 struct pci_dev *pci = NULL;
398 struct pci_dev *found_pci = NULL;
399 struct net_device *net_dev = NULL;
400 struct net_device *lad_dev = NULL;
401 struct kni_dev *kni, *dev, *n;
403 printk(KERN_INFO "KNI: Creating kni...\n");
404 /* Check the buffer size, to avoid warning */
405 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
408 /* Copy kni info from user space */
409 ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
411 KNI_ERR("copy_from_user in kni_ioctl_create");
416 * Check if the cpu core id is valid for binding,
417 * for multiple kernel thread mode.
419 if (multiple_kthread_on && dev_info.force_bind &&
420 !cpu_online(dev_info.core_id)) {
421 KNI_ERR("cpu %u is not online\n", dev_info.core_id);
425 /* Check if it has been created */
426 down_read(&knet->kni_list_lock);
427 list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
428 if (kni_check_param(dev, &dev_info) < 0) {
429 up_read(&knet->kni_list_lock);
433 up_read(&knet->kni_list_lock);
435 net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
436 #ifdef NET_NAME_UNKNOWN
440 if (net_dev == NULL) {
441 KNI_ERR("error allocating device \"%s\"\n", dev_info.name);
445 dev_net_set(net_dev, net);
447 kni = netdev_priv(net_dev);
449 kni->net_dev = net_dev;
450 kni->group_id = dev_info.group_id;
451 kni->core_id = dev_info.core_id;
452 strncpy(kni->name, dev_info.name, RTE_KNI_NAMESIZE);
454 /* Translate user space info into kernel space info */
455 kni->tx_q = phys_to_virt(dev_info.tx_phys);
456 kni->rx_q = phys_to_virt(dev_info.rx_phys);
457 kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
458 kni->free_q = phys_to_virt(dev_info.free_phys);
460 kni->req_q = phys_to_virt(dev_info.req_phys);
461 kni->resp_q = phys_to_virt(dev_info.resp_phys);
462 kni->sync_va = dev_info.sync_va;
463 kni->sync_kva = phys_to_virt(dev_info.sync_phys);
465 kni->mbuf_kva = phys_to_virt(dev_info.mbuf_phys);
466 kni->mbuf_va = dev_info.mbuf_va;
469 kni->vhost_queue = NULL;
470 kni->vq_status = BE_STOP;
472 kni->mbuf_size = dev_info.mbuf_size;
474 KNI_PRINT("tx_phys: 0x%016llx, tx_q addr: 0x%p\n",
475 (unsigned long long) dev_info.tx_phys, kni->tx_q);
476 KNI_PRINT("rx_phys: 0x%016llx, rx_q addr: 0x%p\n",
477 (unsigned long long) dev_info.rx_phys, kni->rx_q);
478 KNI_PRINT("alloc_phys: 0x%016llx, alloc_q addr: 0x%p\n",
479 (unsigned long long) dev_info.alloc_phys, kni->alloc_q);
480 KNI_PRINT("free_phys: 0x%016llx, free_q addr: 0x%p\n",
481 (unsigned long long) dev_info.free_phys, kni->free_q);
482 KNI_PRINT("req_phys: 0x%016llx, req_q addr: 0x%p\n",
483 (unsigned long long) dev_info.req_phys, kni->req_q);
484 KNI_PRINT("resp_phys: 0x%016llx, resp_q addr: 0x%p\n",
485 (unsigned long long) dev_info.resp_phys, kni->resp_q);
486 KNI_PRINT("mbuf_phys: 0x%016llx, mbuf_kva: 0x%p\n",
487 (unsigned long long) dev_info.mbuf_phys, kni->mbuf_kva);
488 KNI_PRINT("mbuf_va: 0x%p\n", dev_info.mbuf_va);
489 KNI_PRINT("mbuf_size: %u\n", kni->mbuf_size);
491 KNI_DBG("PCI: %02x:%02x.%02x %04x:%04x\n",
498 pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
500 /* Support Ethtool */
502 KNI_PRINT("pci_bus: %02x:%02x:%02x \n",
504 PCI_SLOT(pci->devfn),
505 PCI_FUNC(pci->devfn));
507 if ((pci->bus->number == dev_info.bus) &&
508 (PCI_SLOT(pci->devfn) == dev_info.devid) &&
509 (PCI_FUNC(pci->devfn) == dev_info.function)) {
511 switch (dev_info.device_id) {
512 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
513 #include <rte_pci_dev_ids.h>
514 ret = igb_kni_probe(found_pci, &lad_dev);
516 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) \
518 #include <rte_pci_dev_ids.h>
519 ret = ixgbe_kni_probe(found_pci, &lad_dev);
526 KNI_DBG("PCI found: pci=0x%p, lad_dev=0x%p\n",
529 kni->lad_dev = lad_dev;
530 kni_set_ethtool_ops(kni->net_dev);
532 KNI_ERR("Device not supported by ethtool");
536 kni->pci_dev = found_pci;
537 kni->device_id = dev_info.device_id;
540 pci = pci_get_device(dev_info.vendor_id,
541 dev_info.device_id, pci);
546 ret = register_netdev(net_dev);
548 KNI_ERR("error %i registering device \"%s\"\n",
559 * Create a new kernel thread for multiple mode, set its core affinity,
560 * and finally wake it up.
562 if (multiple_kthread_on) {
563 kni->pthread = kthread_create(kni_thread_multiple,
565 "kni_%s", kni->name);
566 if (IS_ERR(kni->pthread)) {
570 if (dev_info.force_bind)
571 kthread_bind(kni->pthread, kni->core_id);
572 wake_up_process(kni->pthread);
575 down_write(&knet->kni_list_lock);
576 list_add(&kni->list, &knet->kni_list_head);
577 up_write(&knet->kni_list_lock);
583 kni_ioctl_release(struct net *net,
584 unsigned int ioctl_num, unsigned long ioctl_param)
586 struct kni_net *knet = net_generic(net, kni_net_id);
588 struct kni_dev *dev, *n;
589 struct rte_kni_device_info dev_info;
591 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
594 ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
596 KNI_ERR("copy_from_user in kni_ioctl_release");
600 /* Release the network device according to its name */
601 if (strlen(dev_info.name) == 0)
604 down_write(&knet->kni_list_lock);
605 list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
606 if (strncmp(dev->name, dev_info.name, RTE_KNI_NAMESIZE) != 0)
609 if (multiple_kthread_on && dev->pthread != NULL) {
610 kthread_stop(dev->pthread);
615 kni_vhost_backend_release(dev);
618 list_del(&dev->list);
622 up_write(&knet->kni_list_lock);
623 printk(KERN_INFO "KNI: %s release kni named %s\n",
624 (ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
630 kni_ioctl(struct inode *inode,
631 unsigned int ioctl_num,
632 unsigned long ioctl_param)
635 struct net *net = current->nsproxy->net_ns;
637 KNI_DBG("IOCTL num=0x%0x param=0x%0lx\n", ioctl_num, ioctl_param);
640 * Switch according to the ioctl called
642 switch (_IOC_NR(ioctl_num)) {
643 case _IOC_NR(RTE_KNI_IOCTL_TEST):
644 /* For test only, not used */
646 case _IOC_NR(RTE_KNI_IOCTL_CREATE):
647 ret = kni_ioctl_create(net, ioctl_num, ioctl_param);
649 case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
650 ret = kni_ioctl_release(net, ioctl_num, ioctl_param);
653 KNI_DBG("IOCTL default\n");
661 kni_compat_ioctl(struct inode *inode,
662 unsigned int ioctl_num,
663 unsigned long ioctl_param)
665 /* 32 bits app on 64 bits OS to be supported later */
666 KNI_PRINT("Not implemented.\n");
671 module_init(kni_init);
672 module_exit(kni_exit);
674 module_param(lo_mode, charp, S_IRUGO | S_IWUSR);
675 MODULE_PARM_DESC(lo_mode,
676 "KNI loopback mode (default=lo_mode_none):\n"
677 " lo_mode_none Kernel loopback disabled\n"
678 " lo_mode_fifo Enable kernel loopback with fifo\n"
679 " lo_mode_fifo_skb Enable kernel loopback with fifo and skb buffer\n"
683 module_param(kthread_mode, charp, S_IRUGO);
684 MODULE_PARM_DESC(kthread_mode,
685 "Kernel thread mode (default=single):\n"
686 " single Single kernel thread mode enabled.\n"
687 " multiple Multiple kernel thread mode enabled.\n"