1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(c) 2010-2014 Intel Corporation.
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>
19 #include <rte_kni_common.h>
24 MODULE_VERSION(KNI_VERSION);
25 MODULE_LICENSE("Dual BSD/GPL");
26 MODULE_AUTHOR("Intel Corporation");
27 MODULE_DESCRIPTION("Kernel Module for managing kni devices");
29 #define KNI_RX_LOOP_NUM 1000
31 #define KNI_MAX_DEVICES 32
36 /* Kernel thread mode */
37 static char *kthread_mode;
38 static uint32_t multiple_kthread_on;
40 /* Default carrier state for created KNI network interfaces */
42 uint32_t kni_dflt_carrier;
44 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
46 static int kni_net_id;
49 unsigned long device_in_use; /* device in use flag */
50 struct mutex kni_kthread_lock;
51 struct task_struct *kni_kthread;
52 struct rw_semaphore kni_list_lock;
53 struct list_head kni_list_head;
57 kni_init_net(struct net *net)
59 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
60 struct kni_net *knet = net_generic(net, kni_net_id);
62 memset(knet, 0, sizeof(*knet));
67 knet = kzalloc(sizeof(struct kni_net), GFP_KERNEL);
74 /* Clear the bit of device in use */
75 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
77 mutex_init(&knet->kni_kthread_lock);
79 init_rwsem(&knet->kni_list_lock);
80 INIT_LIST_HEAD(&knet->kni_list_head);
82 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
85 ret = net_assign_generic(net, kni_net_id, knet);
93 static void __net_exit
94 kni_exit_net(struct net *net)
96 struct kni_net *knet __maybe_unused;
98 knet = net_generic(net, kni_net_id);
99 mutex_destroy(&knet->kni_kthread_lock);
101 #ifndef HAVE_SIMPLIFIED_PERNET_OPERATIONS
106 static struct pernet_operations kni_net_ops = {
107 .init = kni_init_net,
108 .exit = kni_exit_net,
109 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
111 .size = sizeof(struct kni_net),
116 kni_thread_single(void *data)
118 struct kni_net *knet = data;
122 while (!kthread_should_stop()) {
123 down_read(&knet->kni_list_lock);
124 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
125 list_for_each_entry(dev, &knet->kni_list_head, list) {
127 kni_net_poll_resp(dev);
130 up_read(&knet->kni_list_lock);
131 #ifdef RTE_KNI_PREEMPT_DEFAULT
132 /* reschedule out for a while */
133 schedule_timeout_interruptible(
134 usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
142 kni_thread_multiple(void *param)
145 struct kni_dev *dev = param;
147 while (!kthread_should_stop()) {
148 for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
150 kni_net_poll_resp(dev);
152 #ifdef RTE_KNI_PREEMPT_DEFAULT
153 schedule_timeout_interruptible(
154 usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
162 kni_open(struct inode *inode, struct file *file)
164 struct net *net = current->nsproxy->net_ns;
165 struct kni_net *knet = net_generic(net, kni_net_id);
167 /* kni device can be opened by one user only per netns */
168 if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use))
171 file->private_data = get_net(net);
172 pr_debug("/dev/kni opened\n");
178 kni_dev_remove(struct kni_dev *dev)
184 unregister_netdev(dev->net_dev);
185 free_netdev(dev->net_dev);
188 kni_net_release_fifo_phy(dev);
194 kni_release(struct inode *inode, struct file *file)
196 struct net *net = file->private_data;
197 struct kni_net *knet = net_generic(net, kni_net_id);
198 struct kni_dev *dev, *n;
200 /* Stop kernel thread for single mode */
201 if (multiple_kthread_on == 0) {
202 mutex_lock(&knet->kni_kthread_lock);
203 /* Stop kernel thread */
204 if (knet->kni_kthread != NULL) {
205 kthread_stop(knet->kni_kthread);
206 knet->kni_kthread = NULL;
208 mutex_unlock(&knet->kni_kthread_lock);
211 down_write(&knet->kni_list_lock);
212 list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
213 /* Stop kernel thread for multiple mode */
214 if (multiple_kthread_on && dev->pthread != NULL) {
215 kthread_stop(dev->pthread);
220 list_del(&dev->list);
222 up_write(&knet->kni_list_lock);
224 /* Clear the bit of device in use */
225 clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
228 pr_debug("/dev/kni closed\n");
234 kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
239 /* Check if network name has been used */
240 if (!strncmp(kni->name, dev->name, RTE_KNI_NAMESIZE)) {
241 pr_err("KNI name %s duplicated\n", dev->name);
249 kni_run_thread(struct kni_net *knet, struct kni_dev *kni, uint8_t force_bind)
252 * Create a new kernel thread for multiple mode, set its core affinity,
253 * and finally wake it up.
255 if (multiple_kthread_on) {
256 kni->pthread = kthread_create(kni_thread_multiple,
257 (void *)kni, "kni_%s", kni->name);
258 if (IS_ERR(kni->pthread)) {
264 kthread_bind(kni->pthread, kni->core_id);
265 wake_up_process(kni->pthread);
267 mutex_lock(&knet->kni_kthread_lock);
269 if (knet->kni_kthread == NULL) {
270 knet->kni_kthread = kthread_create(kni_thread_single,
271 (void *)knet, "kni_single");
272 if (IS_ERR(knet->kni_kthread)) {
273 mutex_unlock(&knet->kni_kthread_lock);
279 kthread_bind(knet->kni_kthread, kni->core_id);
280 wake_up_process(knet->kni_kthread);
283 mutex_unlock(&knet->kni_kthread_lock);
290 kni_ioctl_create(struct net *net, uint32_t ioctl_num,
291 unsigned long ioctl_param)
293 struct kni_net *knet = net_generic(net, kni_net_id);
295 struct rte_kni_device_info dev_info;
296 struct net_device *net_dev = NULL;
297 struct kni_dev *kni, *dev, *n;
299 pr_info("Creating kni...\n");
300 /* Check the buffer size, to avoid warning */
301 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
304 /* Copy kni info from user space */
305 if (copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info)))
308 /* Check if name is zero-ended */
309 if (strnlen(dev_info.name, sizeof(dev_info.name)) == sizeof(dev_info.name)) {
310 pr_err("kni.name not zero-terminated");
315 * Check if the cpu core id is valid for binding.
317 if (dev_info.force_bind && !cpu_online(dev_info.core_id)) {
318 pr_err("cpu %u is not online\n", dev_info.core_id);
322 /* Check if it has been created */
323 down_read(&knet->kni_list_lock);
324 list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
325 if (kni_check_param(dev, &dev_info) < 0) {
326 up_read(&knet->kni_list_lock);
330 up_read(&knet->kni_list_lock);
332 net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
337 if (net_dev == NULL) {
338 pr_err("error allocating device \"%s\"\n", dev_info.name);
342 dev_net_set(net_dev, net);
344 kni = netdev_priv(net_dev);
346 kni->net_dev = net_dev;
347 kni->core_id = dev_info.core_id;
348 strncpy(kni->name, dev_info.name, RTE_KNI_NAMESIZE);
350 /* Translate user space info into kernel space info */
351 if (dev_info.iova_mode) {
352 #ifdef HAVE_IOVA_TO_KVA_MAPPING_SUPPORT
353 kni->tx_q = iova_to_kva(current, dev_info.tx_phys);
354 kni->rx_q = iova_to_kva(current, dev_info.rx_phys);
355 kni->alloc_q = iova_to_kva(current, dev_info.alloc_phys);
356 kni->free_q = iova_to_kva(current, dev_info.free_phys);
358 kni->req_q = iova_to_kva(current, dev_info.req_phys);
359 kni->resp_q = iova_to_kva(current, dev_info.resp_phys);
360 kni->sync_va = dev_info.sync_va;
361 kni->sync_kva = iova_to_kva(current, dev_info.sync_phys);
362 kni->usr_tsk = current;
365 pr_err("KNI module does not support IOVA to VA translation\n");
370 kni->tx_q = phys_to_virt(dev_info.tx_phys);
371 kni->rx_q = phys_to_virt(dev_info.rx_phys);
372 kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
373 kni->free_q = phys_to_virt(dev_info.free_phys);
375 kni->req_q = phys_to_virt(dev_info.req_phys);
376 kni->resp_q = phys_to_virt(dev_info.resp_phys);
377 kni->sync_va = dev_info.sync_va;
378 kni->sync_kva = phys_to_virt(dev_info.sync_phys);
382 kni->mbuf_size = dev_info.mbuf_size;
384 pr_debug("tx_phys: 0x%016llx, tx_q addr: 0x%p\n",
385 (unsigned long long) dev_info.tx_phys, kni->tx_q);
386 pr_debug("rx_phys: 0x%016llx, rx_q addr: 0x%p\n",
387 (unsigned long long) dev_info.rx_phys, kni->rx_q);
388 pr_debug("alloc_phys: 0x%016llx, alloc_q addr: 0x%p\n",
389 (unsigned long long) dev_info.alloc_phys, kni->alloc_q);
390 pr_debug("free_phys: 0x%016llx, free_q addr: 0x%p\n",
391 (unsigned long long) dev_info.free_phys, kni->free_q);
392 pr_debug("req_phys: 0x%016llx, req_q addr: 0x%p\n",
393 (unsigned long long) dev_info.req_phys, kni->req_q);
394 pr_debug("resp_phys: 0x%016llx, resp_q addr: 0x%p\n",
395 (unsigned long long) dev_info.resp_phys, kni->resp_q);
396 pr_debug("mbuf_size: %u\n", kni->mbuf_size);
398 /* if user has provided a valid mac address */
399 if (is_valid_ether_addr(dev_info.mac_addr))
400 memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
403 * Generate random mac address. eth_random_addr() is the
404 * newer version of generating mac address in kernel.
406 random_ether_addr(net_dev->dev_addr);
409 net_dev->mtu = dev_info.mtu;
410 #ifdef HAVE_MAX_MTU_PARAM
411 net_dev->max_mtu = net_dev->mtu;
413 if (dev_info.min_mtu)
414 net_dev->min_mtu = dev_info.min_mtu;
416 if (dev_info.max_mtu)
417 net_dev->max_mtu = dev_info.max_mtu;
420 ret = register_netdev(net_dev);
422 pr_err("error %i registering device \"%s\"\n",
426 free_netdev(net_dev);
430 netif_carrier_off(net_dev);
432 ret = kni_run_thread(knet, kni, dev_info.force_bind);
436 down_write(&knet->kni_list_lock);
437 list_add(&kni->list, &knet->kni_list_head);
438 up_write(&knet->kni_list_lock);
444 kni_ioctl_release(struct net *net, uint32_t ioctl_num,
445 unsigned long ioctl_param)
447 struct kni_net *knet = net_generic(net, kni_net_id);
449 struct kni_dev *dev, *n;
450 struct rte_kni_device_info dev_info;
452 if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
455 if (copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info)))
458 /* Release the network device according to its name */
459 if (strlen(dev_info.name) == 0)
462 down_write(&knet->kni_list_lock);
463 list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
464 if (strncmp(dev->name, dev_info.name, RTE_KNI_NAMESIZE) != 0)
467 if (multiple_kthread_on && dev->pthread != NULL) {
468 kthread_stop(dev->pthread);
473 list_del(&dev->list);
477 up_write(&knet->kni_list_lock);
478 pr_info("%s release kni named %s\n",
479 (ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
485 kni_ioctl(struct inode *inode, uint32_t ioctl_num, unsigned long ioctl_param)
488 struct net *net = current->nsproxy->net_ns;
490 pr_debug("IOCTL num=0x%0x param=0x%0lx\n", ioctl_num, ioctl_param);
493 * Switch according to the ioctl called
495 switch (_IOC_NR(ioctl_num)) {
496 case _IOC_NR(RTE_KNI_IOCTL_TEST):
497 /* For test only, not used */
499 case _IOC_NR(RTE_KNI_IOCTL_CREATE):
500 ret = kni_ioctl_create(net, ioctl_num, ioctl_param);
502 case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
503 ret = kni_ioctl_release(net, ioctl_num, ioctl_param);
506 pr_debug("IOCTL default\n");
514 kni_compat_ioctl(struct inode *inode, uint32_t ioctl_num,
515 unsigned long ioctl_param)
517 /* 32 bits app on 64 bits OS to be supported later */
518 pr_debug("Not implemented.\n");
523 static const struct file_operations kni_fops = {
524 .owner = THIS_MODULE,
526 .release = kni_release,
527 .unlocked_ioctl = (void *)kni_ioctl,
528 .compat_ioctl = (void *)kni_compat_ioctl,
531 static struct miscdevice kni_misc = {
532 .minor = MISC_DYNAMIC_MINOR,
538 kni_parse_kthread_mode(void)
543 if (strcmp(kthread_mode, "single") == 0)
545 else if (strcmp(kthread_mode, "multiple") == 0)
546 multiple_kthread_on = 1;
554 kni_parse_carrier_state(void)
557 kni_dflt_carrier = 0;
561 if (strcmp(carrier, "off") == 0)
562 kni_dflt_carrier = 0;
563 else if (strcmp(carrier, "on") == 0)
564 kni_dflt_carrier = 1;
576 if (kni_parse_kthread_mode() < 0) {
577 pr_err("Invalid parameter for kthread_mode\n");
581 if (multiple_kthread_on == 0)
582 pr_debug("Single kernel thread for all KNI devices\n");
584 pr_debug("Multiple kernel thread mode enabled\n");
586 if (kni_parse_carrier_state() < 0) {
587 pr_err("Invalid parameter for carrier\n");
591 if (kni_dflt_carrier == 0)
592 pr_debug("Default carrier state set to off.\n");
594 pr_debug("Default carrier state set to on.\n");
596 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
597 rc = register_pernet_subsys(&kni_net_ops);
599 rc = register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
604 rc = misc_register(&kni_misc);
606 pr_err("Misc registration failed\n");
610 /* Configure the lo mode according to the input parameter */
611 kni_net_config_lo_mode(lo_mode);
616 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
617 unregister_pernet_subsys(&kni_net_ops);
619 unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
627 misc_deregister(&kni_misc);
628 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
629 unregister_pernet_subsys(&kni_net_ops);
631 unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
635 module_init(kni_init);
636 module_exit(kni_exit);
638 module_param(lo_mode, charp, 0644);
639 MODULE_PARM_DESC(lo_mode,
640 "KNI loopback mode (default=lo_mode_none):\n"
641 "\t\tlo_mode_none Kernel loopback disabled\n"
642 "\t\tlo_mode_fifo Enable kernel loopback with fifo\n"
643 "\t\tlo_mode_fifo_skb Enable kernel loopback with fifo and skb buffer\n"
647 module_param(kthread_mode, charp, 0644);
648 MODULE_PARM_DESC(kthread_mode,
649 "Kernel thread mode (default=single):\n"
650 "\t\tsingle Single kernel thread mode enabled.\n"
651 "\t\tmultiple Multiple kernel thread mode enabled.\n"
655 module_param(carrier, charp, 0644);
656 MODULE_PARM_DESC(carrier,
657 "Default carrier state for KNI interface (default=off):\n"
658 "\t\toff Interfaces will be created with carrier state set to off.\n"
659 "\t\ton Interfaces will be created with carrier state set to on.\n"