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/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>
38 #include <exec-env/rte_kni_common.h>
43 MODULE_LICENSE("Dual BSD/GPL");
44 MODULE_AUTHOR("Intel Corporation");
45 MODULE_DESCRIPTION("Kernel Module for managing kni devices");
47 #define KNI_RX_LOOP_NUM 1000
49 #define KNI_MAX_DEVICES 32
51 extern const struct pci_device_id ixgbe_pci_tbl[];
52 extern const struct pci_device_id igb_pci_tbl[];
57 /* Kernel thread mode */
58 static char *kthread_mode;
59 static uint32_t multiple_kthread_on;
61 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
63 static int kni_net_id;
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;
74 kni_init_net(struct net *net)
76 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
77 struct kni_net *knet = net_generic(net, kni_net_id);
79 memset(knet, 0, sizeof(*knet));
84 knet = kzalloc(sizeof(struct kni_net), GFP_KERNEL);
91 /* Clear the bit of device in use */
92 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
94 mutex_init(&knet->kni_kthread_lock);
96 init_rwsem(&knet->kni_list_lock);
97 INIT_LIST_HEAD(&knet->kni_list_head);
99 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
102 ret = net_assign_generic(net, kni_net_id, knet);
110 static void __net_exit
111 kni_exit_net(struct net *net)
113 struct kni_net *knet __maybe_unused;
115 knet = net_generic(net, kni_net_id);
116 mutex_destroy(&knet->kni_kthread_lock);
118 #ifndef HAVE_SIMPLIFIED_PERNET_OPERATIONS
123 static struct pernet_operations kni_net_ops = {
124 .init = kni_init_net,
125 .exit = kni_exit_net,
126 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
128 .size = sizeof(struct kni_net),
133 kni_thread_single(void *data)
135 struct kni_net *knet = data;
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) {
144 kni_chk_vhost_rx(dev);
148 kni_net_poll_resp(dev);
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));
163 kni_thread_multiple(void *param)
166 struct kni_dev *dev = (struct kni_dev *)param;
168 while (!kthread_should_stop()) {
169 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
171 kni_chk_vhost_rx(dev);
175 kni_net_poll_resp(dev);
177 #ifdef RTE_KNI_PREEMPT_DEFAULT
178 schedule_timeout_interruptible(
179 usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
187 kni_open(struct inode *inode, struct file *file)
189 struct net *net = current->nsproxy->net_ns;
190 struct kni_net *knet = net_generic(net, kni_net_id);
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))
196 file->private_data = get_net(net);
197 pr_debug("/dev/kni opened\n");
203 kni_dev_remove(struct kni_dev *dev)
209 if (pci_match_id(ixgbe_pci_tbl, dev->pci_dev))
210 ixgbe_kni_remove(dev->pci_dev);
211 else if (pci_match_id(igb_pci_tbl, dev->pci_dev))
212 igb_kni_remove(dev->pci_dev);
216 unregister_netdev(dev->net_dev);
217 free_netdev(dev->net_dev);
224 kni_release(struct inode *inode, struct file *file)
226 struct net *net = file->private_data;
227 struct kni_net *knet = net_generic(net, kni_net_id);
228 struct kni_dev *dev, *n;
230 /* Stop kernel thread for single mode */
231 if (multiple_kthread_on == 0) {
232 mutex_lock(&knet->kni_kthread_lock);
233 /* Stop kernel thread */
234 if (knet->kni_kthread != NULL) {
235 kthread_stop(knet->kni_kthread);
236 knet->kni_kthread = NULL;
238 mutex_unlock(&knet->kni_kthread_lock);
241 down_write(&knet->kni_list_lock);
242 list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
243 /* Stop kernel thread for multiple mode */
244 if (multiple_kthread_on && dev->pthread != NULL) {
245 kthread_stop(dev->pthread);
250 kni_vhost_backend_release(dev);
253 list_del(&dev->list);
255 up_write(&knet->kni_list_lock);
257 /* Clear the bit of device in use */
258 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
261 pr_debug("/dev/kni closed\n");
267 kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
272 /* Check if network name has been used */
273 if (!strncmp(kni->name, dev->name, RTE_KNI_NAMESIZE)) {
274 pr_err("KNI name %s duplicated\n", dev->name);
282 kni_run_thread(struct kni_net *knet, struct kni_dev *kni, uint8_t force_bind)
285 * Create a new kernel thread for multiple mode, set its core affinity,
286 * and finally wake it up.
288 if (multiple_kthread_on) {
289 kni->pthread = kthread_create(kni_thread_multiple,
290 (void *)kni, "kni_%s", kni->name);
291 if (IS_ERR(kni->pthread)) {
297 kthread_bind(kni->pthread, kni->core_id);
298 wake_up_process(kni->pthread);
300 mutex_lock(&knet->kni_kthread_lock);
302 if (knet->kni_kthread == NULL) {
303 knet->kni_kthread = kthread_create(kni_thread_single,
304 (void *)knet, "kni_single");
305 if (IS_ERR(knet->kni_kthread)) {
306 mutex_unlock(&knet->kni_kthread_lock);
312 kthread_bind(knet->kni_kthread, kni->core_id);
313 wake_up_process(knet->kni_kthread);
316 mutex_unlock(&knet->kni_kthread_lock);
323 kni_ioctl_create(struct net *net, uint32_t ioctl_num,
324 unsigned long ioctl_param)
326 struct kni_net *knet = net_generic(net, kni_net_id);
328 struct rte_kni_device_info dev_info;
329 struct pci_dev *pci = NULL;
330 struct pci_dev *found_pci = NULL;
331 struct net_device *net_dev = NULL;
332 struct net_device *lad_dev = NULL;
333 struct kni_dev *kni, *dev, *n;
335 pr_info("Creating kni...\n");
336 /* Check the buffer size, to avoid warning */
337 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
340 /* Copy kni info from user space */
341 ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
343 pr_err("copy_from_user in kni_ioctl_create");
348 * Check if the cpu core id is valid for binding.
350 if (dev_info.force_bind && !cpu_online(dev_info.core_id)) {
351 pr_err("cpu %u is not online\n", dev_info.core_id);
355 /* Check if it has been created */
356 down_read(&knet->kni_list_lock);
357 list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
358 if (kni_check_param(dev, &dev_info) < 0) {
359 up_read(&knet->kni_list_lock);
363 up_read(&knet->kni_list_lock);
365 net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
366 #ifdef NET_NAME_UNKNOWN
370 if (net_dev == NULL) {
371 pr_err("error allocating device \"%s\"\n", dev_info.name);
375 dev_net_set(net_dev, net);
377 kni = netdev_priv(net_dev);
379 kni->net_dev = net_dev;
380 kni->group_id = dev_info.group_id;
381 kni->core_id = dev_info.core_id;
382 strncpy(kni->name, dev_info.name, RTE_KNI_NAMESIZE);
384 /* Translate user space info into kernel space info */
385 kni->tx_q = phys_to_virt(dev_info.tx_phys);
386 kni->rx_q = phys_to_virt(dev_info.rx_phys);
387 kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
388 kni->free_q = phys_to_virt(dev_info.free_phys);
390 kni->req_q = phys_to_virt(dev_info.req_phys);
391 kni->resp_q = phys_to_virt(dev_info.resp_phys);
392 kni->sync_va = dev_info.sync_va;
393 kni->sync_kva = phys_to_virt(dev_info.sync_phys);
396 kni->vhost_queue = NULL;
397 kni->vq_status = BE_STOP;
399 kni->mbuf_size = dev_info.mbuf_size;
401 pr_debug("tx_phys: 0x%016llx, tx_q addr: 0x%p\n",
402 (unsigned long long) dev_info.tx_phys, kni->tx_q);
403 pr_debug("rx_phys: 0x%016llx, rx_q addr: 0x%p\n",
404 (unsigned long long) dev_info.rx_phys, kni->rx_q);
405 pr_debug("alloc_phys: 0x%016llx, alloc_q addr: 0x%p\n",
406 (unsigned long long) dev_info.alloc_phys, kni->alloc_q);
407 pr_debug("free_phys: 0x%016llx, free_q addr: 0x%p\n",
408 (unsigned long long) dev_info.free_phys, kni->free_q);
409 pr_debug("req_phys: 0x%016llx, req_q addr: 0x%p\n",
410 (unsigned long long) dev_info.req_phys, kni->req_q);
411 pr_debug("resp_phys: 0x%016llx, resp_q addr: 0x%p\n",
412 (unsigned long long) dev_info.resp_phys, kni->resp_q);
413 pr_debug("mbuf_size: %u\n", kni->mbuf_size);
415 pr_debug("PCI: %02x:%02x.%02x %04x:%04x\n",
422 pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
424 /* Support Ethtool */
426 pr_debug("pci_bus: %02x:%02x:%02x\n",
428 PCI_SLOT(pci->devfn),
429 PCI_FUNC(pci->devfn));
431 if ((pci->bus->number == dev_info.bus) &&
432 (PCI_SLOT(pci->devfn) == dev_info.devid) &&
433 (PCI_FUNC(pci->devfn) == dev_info.function)) {
436 if (pci_match_id(ixgbe_pci_tbl, found_pci))
437 ret = ixgbe_kni_probe(found_pci, &lad_dev);
438 else if (pci_match_id(igb_pci_tbl, found_pci))
439 ret = igb_kni_probe(found_pci, &lad_dev);
443 pr_debug("PCI found: pci=0x%p, lad_dev=0x%p\n",
446 kni->lad_dev = lad_dev;
447 kni_set_ethtool_ops(kni->net_dev);
449 pr_err("Device not supported by ethtool");
453 kni->pci_dev = found_pci;
454 kni->device_id = dev_info.device_id;
457 pci = pci_get_device(dev_info.vendor_id,
458 dev_info.device_id, pci);
464 ether_addr_copy(net_dev->dev_addr, kni->lad_dev->dev_addr);
467 * Generate random mac address. eth_random_addr() is the newer
468 * version of generating mac address in linux kernel.
470 random_ether_addr(net_dev->dev_addr);
472 ret = register_netdev(net_dev);
474 pr_err("error %i registering device \"%s\"\n",
478 free_netdev(net_dev);
486 ret = kni_run_thread(knet, kni, dev_info.force_bind);
490 down_write(&knet->kni_list_lock);
491 list_add(&kni->list, &knet->kni_list_head);
492 up_write(&knet->kni_list_lock);
498 kni_ioctl_release(struct net *net, uint32_t ioctl_num,
499 unsigned long ioctl_param)
501 struct kni_net *knet = net_generic(net, kni_net_id);
503 struct kni_dev *dev, *n;
504 struct rte_kni_device_info dev_info;
506 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
509 ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
511 pr_err("copy_from_user in kni_ioctl_release");
515 /* Release the network device according to its name */
516 if (strlen(dev_info.name) == 0)
519 down_write(&knet->kni_list_lock);
520 list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
521 if (strncmp(dev->name, dev_info.name, RTE_KNI_NAMESIZE) != 0)
524 if (multiple_kthread_on && dev->pthread != NULL) {
525 kthread_stop(dev->pthread);
530 kni_vhost_backend_release(dev);
533 list_del(&dev->list);
537 up_write(&knet->kni_list_lock);
538 pr_info("%s release kni named %s\n",
539 (ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
545 kni_ioctl(struct inode *inode, uint32_t ioctl_num, unsigned long ioctl_param)
548 struct net *net = current->nsproxy->net_ns;
550 pr_debug("IOCTL num=0x%0x param=0x%0lx\n", ioctl_num, ioctl_param);
553 * Switch according to the ioctl called
555 switch (_IOC_NR(ioctl_num)) {
556 case _IOC_NR(RTE_KNI_IOCTL_TEST):
557 /* For test only, not used */
559 case _IOC_NR(RTE_KNI_IOCTL_CREATE):
560 ret = kni_ioctl_create(net, ioctl_num, ioctl_param);
562 case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
563 ret = kni_ioctl_release(net, ioctl_num, ioctl_param);
566 pr_debug("IOCTL default\n");
574 kni_compat_ioctl(struct inode *inode, uint32_t ioctl_num,
575 unsigned long ioctl_param)
577 /* 32 bits app on 64 bits OS to be supported later */
578 pr_debug("Not implemented.\n");
583 static const struct file_operations kni_fops = {
584 .owner = THIS_MODULE,
586 .release = kni_release,
587 .unlocked_ioctl = (void *)kni_ioctl,
588 .compat_ioctl = (void *)kni_compat_ioctl,
591 static struct miscdevice kni_misc = {
592 .minor = MISC_DYNAMIC_MINOR,
598 kni_parse_kthread_mode(void)
603 if (strcmp(kthread_mode, "single") == 0)
605 else if (strcmp(kthread_mode, "multiple") == 0)
606 multiple_kthread_on = 1;
618 if (kni_parse_kthread_mode() < 0) {
619 pr_err("Invalid parameter for kthread_mode\n");
623 if (multiple_kthread_on == 0)
624 pr_debug("Single kernel thread for all KNI devices\n");
626 pr_debug("Multiple kernel thread mode enabled\n");
628 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
629 rc = register_pernet_subsys(&kni_net_ops);
631 rc = register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
636 rc = misc_register(&kni_misc);
638 pr_err("Misc registration failed\n");
642 /* Configure the lo mode according to the input parameter */
643 kni_net_config_lo_mode(lo_mode);
648 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
649 unregister_pernet_subsys(&kni_net_ops);
651 unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
659 misc_deregister(&kni_misc);
660 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
661 unregister_pernet_subsys(&kni_net_ops);
663 unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
667 module_init(kni_init);
668 module_exit(kni_exit);
670 module_param(lo_mode, charp, S_IRUGO | S_IWUSR);
671 MODULE_PARM_DESC(lo_mode,
672 "KNI loopback mode (default=lo_mode_none):\n"
673 " lo_mode_none Kernel loopback disabled\n"
674 " lo_mode_fifo Enable kernel loopback with fifo\n"
675 " lo_mode_fifo_skb Enable kernel loopback with fifo and skb buffer\n"
679 module_param(kthread_mode, charp, S_IRUGO);
680 MODULE_PARM_DESC(kthread_mode,
681 "Kernel thread mode (default=single):\n"
682 " single Single kernel thread mode enabled.\n"
683 " multiple Multiple kernel thread mode enabled.\n"