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/module.h>
26 #include <linux/miscdevice.h>
27 #include <linux/netdevice.h>
28 #include <linux/pci.h>
29 #include <linux/kthread.h>
30 #include <linux/rwsem.h>
32 #include <exec-env/rte_kni_common.h>
34 #include <rte_config.h>
36 MODULE_LICENSE("Dual BSD/GPL");
37 MODULE_AUTHOR("Intel Corporation");
38 MODULE_DESCRIPTION("Kernel Module for managing kni devices");
40 #define KNI_RX_LOOP_NUM 1000
42 #define KNI_MAX_DEVICES 32
44 extern void kni_net_rx(struct kni_dev *kni);
45 extern void kni_net_init(struct net_device *dev);
46 extern void kni_net_config_lo_mode(char *lo_str);
47 extern void kni_net_poll_resp(struct kni_dev *kni);
48 extern void kni_set_ethtool_ops(struct net_device *netdev);
50 extern int ixgbe_kni_probe(struct pci_dev *pdev, struct net_device **lad_dev);
51 extern void ixgbe_kni_remove(struct pci_dev *pdev);
52 extern int igb_kni_probe(struct pci_dev *pdev, struct net_device **lad_dev);
53 extern void igb_kni_remove(struct pci_dev *pdev);
55 static int kni_open(struct inode *inode, struct file *file);
56 static int kni_release(struct inode *inode, struct file *file);
57 static int kni_ioctl(struct inode *inode, unsigned int ioctl_num,
58 unsigned long ioctl_param);
59 static int kni_compat_ioctl(struct inode *inode, unsigned int ioctl_num,
60 unsigned long ioctl_param);
61 static int kni_dev_remove(struct kni_dev *dev);
63 static int __init kni_parse_kthread_mode(void);
65 /* KNI processing for single kernel thread mode */
66 static int kni_thread_single(void *unused);
67 /* KNI processing for multiple kernel thread mode */
68 static int kni_thread_multiple(void *param);
70 static struct file_operations kni_fops = {
73 .release = kni_release,
74 .unlocked_ioctl = (void *)kni_ioctl,
75 .compat_ioctl = (void *)kni_compat_ioctl,
78 static struct miscdevice kni_misc = {
79 .minor = MISC_DYNAMIC_MINOR,
85 static char *lo_mode = NULL;
87 /* Kernel thread mode */
88 static char *kthread_mode = NULL;
89 static unsigned multiple_kthread_on = 0;
91 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
93 static volatile unsigned long device_in_use; /* device in use flag */
94 static struct task_struct *kni_kthread;
97 static DECLARE_RWSEM(kni_list_lock);
100 static struct list_head kni_list_head = LIST_HEAD_INIT(kni_list_head);
105 KNI_PRINT("######## DPDK kni module loading ########\n");
107 if (kni_parse_kthread_mode() < 0) {
108 KNI_ERR("Invalid parameter for kthread_mode\n");
112 if (misc_register(&kni_misc) != 0) {
113 KNI_ERR("Misc registration failed\n");
117 /* Clear the bit of device in use */
118 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
120 /* Configure the lo mode according to the input parameter */
121 kni_net_config_lo_mode(lo_mode);
123 KNI_PRINT("######## DPDK kni module loaded ########\n");
131 misc_deregister(&kni_misc);
132 KNI_PRINT("####### DPDK kni module unloaded #######\n");
136 kni_parse_kthread_mode(void)
141 if (strcmp(kthread_mode, "single") == 0)
143 else if (strcmp(kthread_mode, "multiple") == 0)
144 multiple_kthread_on = 1;
152 kni_open(struct inode *inode, struct file *file)
154 /* kni device can be opened by one user only, test and set bit */
155 if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use))
158 /* Create kernel thread for single mode */
159 if (multiple_kthread_on == 0) {
160 KNI_PRINT("Single kernel thread for all KNI devices\n");
161 /* Create kernel thread for RX */
162 kni_kthread = kthread_run(kni_thread_single, NULL,
164 if (IS_ERR(kni_kthread)) {
165 KNI_ERR("Unable to create kernel threaed\n");
166 return PTR_ERR(kni_kthread);
169 KNI_PRINT("Multiple kernel thread mode enabled\n");
171 KNI_PRINT("/dev/kni opened\n");
177 kni_release(struct inode *inode, struct file *file)
179 struct kni_dev *dev, *n;
181 /* Stop kernel thread for single mode */
182 if (multiple_kthread_on == 0) {
183 /* Stop kernel thread */
184 kthread_stop(kni_kthread);
188 down_write(&kni_list_lock);
189 list_for_each_entry_safe(dev, n, &kni_list_head, list) {
190 /* Stop kernel thread for multiple mode */
191 if (multiple_kthread_on && dev->pthread != NULL) {
192 kthread_stop(dev->pthread);
197 kni_vhost_backend_release(dev);
200 list_del(&dev->list);
202 up_write(&kni_list_lock);
204 /* Clear the bit of device in use */
205 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &device_in_use);
207 KNI_PRINT("/dev/kni closed\n");
213 kni_thread_single(void *unused)
218 while (!kthread_should_stop()) {
219 down_read(&kni_list_lock);
220 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
221 list_for_each_entry(dev, &kni_list_head, list) {
223 kni_chk_vhost_rx(dev);
227 kni_net_poll_resp(dev);
230 up_read(&kni_list_lock);
231 #ifdef RTE_KNI_PREEMPT_DEFAULT
232 /* reschedule out for a while */
233 schedule_timeout_interruptible(usecs_to_jiffies( \
234 KNI_KTHREAD_RESCHEDULE_INTERVAL));
242 kni_thread_multiple(void *param)
245 struct kni_dev *dev = (struct kni_dev *)param;
247 while (!kthread_should_stop()) {
248 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
250 kni_chk_vhost_rx(dev);
254 kni_net_poll_resp(dev);
256 #ifdef RTE_KNI_PREEMPT_DEFAULT
257 schedule_timeout_interruptible(usecs_to_jiffies( \
258 KNI_KTHREAD_RESCHEDULE_INTERVAL));
266 kni_dev_remove(struct kni_dev *dev)
271 switch (dev->device_id) {
272 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
273 #include <rte_pci_dev_ids.h>
274 igb_kni_remove(dev->pci_dev);
276 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) case (dev):
277 #include <rte_pci_dev_ids.h>
278 ixgbe_kni_remove(dev->pci_dev);
285 unregister_netdev(dev->net_dev);
286 free_netdev(dev->net_dev);
293 kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
298 /* Check if network name has been used */
299 if (!strncmp(kni->name, dev->name, RTE_KNI_NAMESIZE)) {
300 KNI_ERR("KNI name %s duplicated\n", dev->name);
308 kni_ioctl_create(unsigned int ioctl_num, unsigned long ioctl_param)
311 struct rte_kni_device_info dev_info;
312 struct pci_dev *pci = NULL;
313 struct pci_dev *found_pci = NULL;
314 struct net_device *net_dev = NULL;
315 struct net_device *lad_dev = NULL;
316 struct kni_dev *kni, *dev, *n;
319 printk(KERN_INFO "KNI: Creating kni...\n");
320 /* Check the buffer size, to avoid warning */
321 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
324 /* Copy kni info from user space */
325 ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
327 KNI_ERR("copy_from_user in kni_ioctl_create");
332 * Check if the cpu core id is valid for binding,
333 * for multiple kernel thread mode.
335 if (multiple_kthread_on && dev_info.force_bind &&
336 !cpu_online(dev_info.core_id)) {
337 KNI_ERR("cpu %u is not online\n", dev_info.core_id);
341 /* Check if it has been created */
342 down_read(&kni_list_lock);
343 list_for_each_entry_safe(dev, n, &kni_list_head, list) {
344 if (kni_check_param(dev, &dev_info) < 0) {
345 up_read(&kni_list_lock);
349 up_read(&kni_list_lock);
351 net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
352 #ifdef NET_NAME_UNKNOWN
356 if (net_dev == NULL) {
357 KNI_ERR("error allocating device \"%s\"\n", dev_info.name);
361 net = get_net_ns_by_pid(task_pid_vnr(current));
363 free_netdev(net_dev);
366 dev_net_set(net_dev, net);
369 kni = netdev_priv(net_dev);
371 kni->net_dev = net_dev;
372 kni->group_id = dev_info.group_id;
373 kni->core_id = dev_info.core_id;
374 strncpy(kni->name, dev_info.name, RTE_KNI_NAMESIZE);
376 /* Translate user space info into kernel space info */
377 kni->tx_q = phys_to_virt(dev_info.tx_phys);
378 kni->rx_q = phys_to_virt(dev_info.rx_phys);
379 kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
380 kni->free_q = phys_to_virt(dev_info.free_phys);
382 kni->req_q = phys_to_virt(dev_info.req_phys);
383 kni->resp_q = phys_to_virt(dev_info.resp_phys);
384 kni->sync_va = dev_info.sync_va;
385 kni->sync_kva = phys_to_virt(dev_info.sync_phys);
387 kni->mbuf_kva = phys_to_virt(dev_info.mbuf_phys);
388 kni->mbuf_va = dev_info.mbuf_va;
391 kni->vhost_queue = NULL;
392 kni->vq_status = BE_STOP;
394 kni->mbuf_size = dev_info.mbuf_size;
396 KNI_PRINT("tx_phys: 0x%016llx, tx_q addr: 0x%p\n",
397 (unsigned long long) dev_info.tx_phys, kni->tx_q);
398 KNI_PRINT("rx_phys: 0x%016llx, rx_q addr: 0x%p\n",
399 (unsigned long long) dev_info.rx_phys, kni->rx_q);
400 KNI_PRINT("alloc_phys: 0x%016llx, alloc_q addr: 0x%p\n",
401 (unsigned long long) dev_info.alloc_phys, kni->alloc_q);
402 KNI_PRINT("free_phys: 0x%016llx, free_q addr: 0x%p\n",
403 (unsigned long long) dev_info.free_phys, kni->free_q);
404 KNI_PRINT("req_phys: 0x%016llx, req_q addr: 0x%p\n",
405 (unsigned long long) dev_info.req_phys, kni->req_q);
406 KNI_PRINT("resp_phys: 0x%016llx, resp_q addr: 0x%p\n",
407 (unsigned long long) dev_info.resp_phys, kni->resp_q);
408 KNI_PRINT("mbuf_phys: 0x%016llx, mbuf_kva: 0x%p\n",
409 (unsigned long long) dev_info.mbuf_phys, kni->mbuf_kva);
410 KNI_PRINT("mbuf_va: 0x%p\n", dev_info.mbuf_va);
411 KNI_PRINT("mbuf_size: %u\n", kni->mbuf_size);
413 KNI_DBG("PCI: %02x:%02x.%02x %04x:%04x\n",
420 pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
422 /* Support Ethtool */
424 KNI_PRINT("pci_bus: %02x:%02x:%02x \n",
426 PCI_SLOT(pci->devfn),
427 PCI_FUNC(pci->devfn));
429 if ((pci->bus->number == dev_info.bus) &&
430 (PCI_SLOT(pci->devfn) == dev_info.devid) &&
431 (PCI_FUNC(pci->devfn) == dev_info.function)) {
433 switch (dev_info.device_id) {
434 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) case (dev):
435 #include <rte_pci_dev_ids.h>
436 ret = igb_kni_probe(found_pci, &lad_dev);
438 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) \
440 #include <rte_pci_dev_ids.h>
441 ret = ixgbe_kni_probe(found_pci, &lad_dev);
448 KNI_DBG("PCI found: pci=0x%p, lad_dev=0x%p\n",
451 kni->lad_dev = lad_dev;
452 kni_set_ethtool_ops(kni->net_dev);
454 KNI_ERR("Device not supported by ethtool");
458 kni->pci_dev = found_pci;
459 kni->device_id = dev_info.device_id;
462 pci = pci_get_device(dev_info.vendor_id,
463 dev_info.device_id, pci);
468 ret = register_netdev(net_dev);
470 KNI_ERR("error %i registering device \"%s\"\n",
481 * Create a new kernel thread for multiple mode, set its core affinity,
482 * and finally wake it up.
484 if (multiple_kthread_on) {
485 kni->pthread = kthread_create(kni_thread_multiple,
487 "kni_%s", kni->name);
488 if (IS_ERR(kni->pthread)) {
492 if (dev_info.force_bind)
493 kthread_bind(kni->pthread, kni->core_id);
494 wake_up_process(kni->pthread);
497 down_write(&kni_list_lock);
498 list_add(&kni->list, &kni_list_head);
499 up_write(&kni_list_lock);
505 kni_ioctl_release(unsigned int ioctl_num, unsigned long ioctl_param)
508 struct kni_dev *dev, *n;
509 struct rte_kni_device_info dev_info;
511 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
514 ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
516 KNI_ERR("copy_from_user in kni_ioctl_release");
520 /* Release the network device according to its name */
521 if (strlen(dev_info.name) == 0)
524 down_write(&kni_list_lock);
525 list_for_each_entry_safe(dev, n, &kni_list_head, list) {
526 if (strncmp(dev->name, dev_info.name, RTE_KNI_NAMESIZE) != 0)
529 if (multiple_kthread_on && dev->pthread != NULL) {
530 kthread_stop(dev->pthread);
535 kni_vhost_backend_release(dev);
538 list_del(&dev->list);
542 up_write(&kni_list_lock);
543 printk(KERN_INFO "KNI: %s release kni named %s\n",
544 (ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
550 kni_ioctl(struct inode *inode,
551 unsigned int ioctl_num,
552 unsigned long ioctl_param)
556 KNI_DBG("IOCTL num=0x%0x param=0x%0lx \n", ioctl_num, ioctl_param);
559 * Switch according to the ioctl called
561 switch (_IOC_NR(ioctl_num)) {
562 case _IOC_NR(RTE_KNI_IOCTL_TEST):
563 /* For test only, not used */
565 case _IOC_NR(RTE_KNI_IOCTL_CREATE):
566 ret = kni_ioctl_create(ioctl_num, ioctl_param);
568 case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
569 ret = kni_ioctl_release(ioctl_num, ioctl_param);
572 KNI_DBG("IOCTL default \n");
580 kni_compat_ioctl(struct inode *inode,
581 unsigned int ioctl_num,
582 unsigned long ioctl_param)
584 /* 32 bits app on 64 bits OS to be supported later */
585 KNI_PRINT("Not implemented.\n");
590 module_init(kni_init);
591 module_exit(kni_exit);
593 module_param(lo_mode, charp, S_IRUGO | S_IWUSR);
594 MODULE_PARM_DESC(lo_mode,
595 "KNI loopback mode (default=lo_mode_none):\n"
596 " lo_mode_none Kernel loopback disabled\n"
597 " lo_mode_fifo Enable kernel loopback with fifo\n"
598 " lo_mode_fifo_skb Enable kernel loopback with fifo and skb buffer\n"
602 module_param(kthread_mode, charp, S_IRUGO);
603 MODULE_PARM_DESC(kthread_mode,
604 "Kernel thread mode (default=single):\n"
605 " single Single kernel thread mode enabled.\n"
606 " multiple Multiple kernel thread mode enabled.\n"