xref: /dpdk/kernel/linux/kni/kni_misc.c (revision c7e9729d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright(c) 2010-2014 Intel Corporation.
4  */
5 
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>
18 
19 #include <exec-env/rte_kni_common.h>
20 
21 #include "compat.h"
22 #include "kni_dev.h"
23 
24 MODULE_LICENSE("Dual BSD/GPL");
25 MODULE_AUTHOR("Intel Corporation");
26 MODULE_DESCRIPTION("Kernel Module for managing kni devices");
27 
28 #define KNI_RX_LOOP_NUM 1000
29 
30 #define KNI_MAX_DEVICES 32
31 
32 extern const struct pci_device_id ixgbe_pci_tbl[];
33 extern const struct pci_device_id igb_pci_tbl[];
34 
35 /* loopback mode */
36 static char *lo_mode;
37 
38 /* Kernel thread mode */
39 static char *kthread_mode;
40 static uint32_t multiple_kthread_on;
41 
42 #define KNI_DEV_IN_USE_BIT_NUM 0 /* Bit number for device in use */
43 
44 static int kni_net_id;
45 
46 struct kni_net {
47 	unsigned long device_in_use; /* device in use flag */
48 	struct mutex kni_kthread_lock;
49 	struct task_struct *kni_kthread;
50 	struct rw_semaphore kni_list_lock;
51 	struct list_head kni_list_head;
52 };
53 
54 static int __net_init
55 kni_init_net(struct net *net)
56 {
57 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
58 	struct kni_net *knet = net_generic(net, kni_net_id);
59 
60 	memset(knet, 0, sizeof(*knet));
61 #else
62 	struct kni_net *knet;
63 	int ret;
64 
65 	knet = kzalloc(sizeof(struct kni_net), GFP_KERNEL);
66 	if (!knet) {
67 		ret = -ENOMEM;
68 		return ret;
69 	}
70 #endif
71 
72 	/* Clear the bit of device in use */
73 	clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
74 
75 	mutex_init(&knet->kni_kthread_lock);
76 
77 	init_rwsem(&knet->kni_list_lock);
78 	INIT_LIST_HEAD(&knet->kni_list_head);
79 
80 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
81 	return 0;
82 #else
83 	ret = net_assign_generic(net, kni_net_id, knet);
84 	if (ret < 0)
85 		kfree(knet);
86 
87 	return ret;
88 #endif
89 }
90 
91 static void __net_exit
92 kni_exit_net(struct net *net)
93 {
94 	struct kni_net *knet __maybe_unused;
95 
96 	knet = net_generic(net, kni_net_id);
97 	mutex_destroy(&knet->kni_kthread_lock);
98 
99 #ifndef HAVE_SIMPLIFIED_PERNET_OPERATIONS
100 	kfree(knet);
101 #endif
102 }
103 
104 static struct pernet_operations kni_net_ops = {
105 	.init = kni_init_net,
106 	.exit = kni_exit_net,
107 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
108 	.id   = &kni_net_id,
109 	.size = sizeof(struct kni_net),
110 #endif
111 };
112 
113 static int
114 kni_thread_single(void *data)
115 {
116 	struct kni_net *knet = data;
117 	int j;
118 	struct kni_dev *dev;
119 
120 	while (!kthread_should_stop()) {
121 		down_read(&knet->kni_list_lock);
122 		for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
123 			list_for_each_entry(dev, &knet->kni_list_head, list) {
124 				kni_net_rx(dev);
125 				kni_net_poll_resp(dev);
126 			}
127 		}
128 		up_read(&knet->kni_list_lock);
129 #ifdef RTE_KNI_PREEMPT_DEFAULT
130 		/* reschedule out for a while */
131 		schedule_timeout_interruptible(
132 			usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
133 #endif
134 	}
135 
136 	return 0;
137 }
138 
139 static int
140 kni_thread_multiple(void *param)
141 {
142 	int j;
143 	struct kni_dev *dev = param;
144 
145 	while (!kthread_should_stop()) {
146 		for (j = 0; j < KNI_RX_LOOP_NUM; j++) {
147 			kni_net_rx(dev);
148 			kni_net_poll_resp(dev);
149 		}
150 #ifdef RTE_KNI_PREEMPT_DEFAULT
151 		schedule_timeout_interruptible(
152 			usecs_to_jiffies(KNI_KTHREAD_RESCHEDULE_INTERVAL));
153 #endif
154 	}
155 
156 	return 0;
157 }
158 
159 static int
160 kni_open(struct inode *inode, struct file *file)
161 {
162 	struct net *net = current->nsproxy->net_ns;
163 	struct kni_net *knet = net_generic(net, kni_net_id);
164 
165 	/* kni device can be opened by one user only per netns */
166 	if (test_and_set_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use))
167 		return -EBUSY;
168 
169 	file->private_data = get_net(net);
170 	pr_debug("/dev/kni opened\n");
171 
172 	return 0;
173 }
174 
175 static int
176 kni_dev_remove(struct kni_dev *dev)
177 {
178 	if (!dev)
179 		return -ENODEV;
180 
181 #ifdef RTE_KNI_KMOD_ETHTOOL
182 	if (dev->pci_dev) {
183 		if (pci_match_id(ixgbe_pci_tbl, dev->pci_dev))
184 			ixgbe_kni_remove(dev->pci_dev);
185 		else if (pci_match_id(igb_pci_tbl, dev->pci_dev))
186 			igb_kni_remove(dev->pci_dev);
187 	}
188 #endif
189 
190 	if (dev->net_dev) {
191 		unregister_netdev(dev->net_dev);
192 		free_netdev(dev->net_dev);
193 	}
194 
195 	return 0;
196 }
197 
198 static int
199 kni_release(struct inode *inode, struct file *file)
200 {
201 	struct net *net = file->private_data;
202 	struct kni_net *knet = net_generic(net, kni_net_id);
203 	struct kni_dev *dev, *n;
204 
205 	/* Stop kernel thread for single mode */
206 	if (multiple_kthread_on == 0) {
207 		mutex_lock(&knet->kni_kthread_lock);
208 		/* Stop kernel thread */
209 		if (knet->kni_kthread != NULL) {
210 			kthread_stop(knet->kni_kthread);
211 			knet->kni_kthread = NULL;
212 		}
213 		mutex_unlock(&knet->kni_kthread_lock);
214 	}
215 
216 	down_write(&knet->kni_list_lock);
217 	list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
218 		/* Stop kernel thread for multiple mode */
219 		if (multiple_kthread_on && dev->pthread != NULL) {
220 			kthread_stop(dev->pthread);
221 			dev->pthread = NULL;
222 		}
223 
224 		kni_dev_remove(dev);
225 		list_del(&dev->list);
226 	}
227 	up_write(&knet->kni_list_lock);
228 
229 	/* Clear the bit of device in use */
230 	clear_bit(KNI_DEV_IN_USE_BIT_NUM, &knet->device_in_use);
231 
232 	put_net(net);
233 	pr_debug("/dev/kni closed\n");
234 
235 	return 0;
236 }
237 
238 static int
239 kni_check_param(struct kni_dev *kni, struct rte_kni_device_info *dev)
240 {
241 	if (!kni || !dev)
242 		return -1;
243 
244 	/* Check if network name has been used */
245 	if (!strncmp(kni->name, dev->name, RTE_KNI_NAMESIZE)) {
246 		pr_err("KNI name %s duplicated\n", dev->name);
247 		return -1;
248 	}
249 
250 	return 0;
251 }
252 
253 static int
254 kni_run_thread(struct kni_net *knet, struct kni_dev *kni, uint8_t force_bind)
255 {
256 	/**
257 	 * Create a new kernel thread for multiple mode, set its core affinity,
258 	 * and finally wake it up.
259 	 */
260 	if (multiple_kthread_on) {
261 		kni->pthread = kthread_create(kni_thread_multiple,
262 			(void *)kni, "kni_%s", kni->name);
263 		if (IS_ERR(kni->pthread)) {
264 			kni_dev_remove(kni);
265 			return -ECANCELED;
266 		}
267 
268 		if (force_bind)
269 			kthread_bind(kni->pthread, kni->core_id);
270 		wake_up_process(kni->pthread);
271 	} else {
272 		mutex_lock(&knet->kni_kthread_lock);
273 
274 		if (knet->kni_kthread == NULL) {
275 			knet->kni_kthread = kthread_create(kni_thread_single,
276 				(void *)knet, "kni_single");
277 			if (IS_ERR(knet->kni_kthread)) {
278 				mutex_unlock(&knet->kni_kthread_lock);
279 				kni_dev_remove(kni);
280 				return -ECANCELED;
281 			}
282 
283 			if (force_bind)
284 				kthread_bind(knet->kni_kthread, kni->core_id);
285 			wake_up_process(knet->kni_kthread);
286 		}
287 
288 		mutex_unlock(&knet->kni_kthread_lock);
289 	}
290 
291 	return 0;
292 }
293 
294 static int
295 kni_ioctl_create(struct net *net, uint32_t ioctl_num,
296 		unsigned long ioctl_param)
297 {
298 	struct kni_net *knet = net_generic(net, kni_net_id);
299 	int ret;
300 	struct rte_kni_device_info dev_info;
301 	struct net_device *net_dev = NULL;
302 	struct kni_dev *kni, *dev, *n;
303 #ifdef RTE_KNI_KMOD_ETHTOOL
304 	struct pci_dev *found_pci = NULL;
305 	struct net_device *lad_dev = NULL;
306 	struct pci_dev *pci = NULL;
307 #endif
308 
309 	pr_info("Creating kni...\n");
310 	/* Check the buffer size, to avoid warning */
311 	if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
312 		return -EINVAL;
313 
314 	/* Copy kni info from user space */
315 	ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
316 	if (ret) {
317 		pr_err("copy_from_user in kni_ioctl_create");
318 		return -EIO;
319 	}
320 
321 	/* Check if name is zero-ended */
322 	if (strnlen(dev_info.name, sizeof(dev_info.name)) == sizeof(dev_info.name)) {
323 		pr_err("kni.name not zero-terminated");
324 		return -EINVAL;
325 	}
326 
327 	/**
328 	 * Check if the cpu core id is valid for binding.
329 	 */
330 	if (dev_info.force_bind && !cpu_online(dev_info.core_id)) {
331 		pr_err("cpu %u is not online\n", dev_info.core_id);
332 		return -EINVAL;
333 	}
334 
335 	/* Check if it has been created */
336 	down_read(&knet->kni_list_lock);
337 	list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
338 		if (kni_check_param(dev, &dev_info) < 0) {
339 			up_read(&knet->kni_list_lock);
340 			return -EINVAL;
341 		}
342 	}
343 	up_read(&knet->kni_list_lock);
344 
345 	net_dev = alloc_netdev(sizeof(struct kni_dev), dev_info.name,
346 #ifdef NET_NAME_USER
347 							NET_NAME_USER,
348 #endif
349 							kni_net_init);
350 	if (net_dev == NULL) {
351 		pr_err("error allocating device \"%s\"\n", dev_info.name);
352 		return -EBUSY;
353 	}
354 
355 	dev_net_set(net_dev, net);
356 
357 	kni = netdev_priv(net_dev);
358 
359 	kni->net_dev = net_dev;
360 	kni->group_id = dev_info.group_id;
361 	kni->core_id = dev_info.core_id;
362 	strncpy(kni->name, dev_info.name, RTE_KNI_NAMESIZE);
363 
364 	/* Translate user space info into kernel space info */
365 	kni->tx_q = phys_to_virt(dev_info.tx_phys);
366 	kni->rx_q = phys_to_virt(dev_info.rx_phys);
367 	kni->alloc_q = phys_to_virt(dev_info.alloc_phys);
368 	kni->free_q = phys_to_virt(dev_info.free_phys);
369 
370 	kni->req_q = phys_to_virt(dev_info.req_phys);
371 	kni->resp_q = phys_to_virt(dev_info.resp_phys);
372 	kni->sync_va = dev_info.sync_va;
373 	kni->sync_kva = phys_to_virt(dev_info.sync_phys);
374 
375 	kni->mbuf_size = dev_info.mbuf_size;
376 
377 	pr_debug("tx_phys:      0x%016llx, tx_q addr:      0x%p\n",
378 		(unsigned long long) dev_info.tx_phys, kni->tx_q);
379 	pr_debug("rx_phys:      0x%016llx, rx_q addr:      0x%p\n",
380 		(unsigned long long) dev_info.rx_phys, kni->rx_q);
381 	pr_debug("alloc_phys:   0x%016llx, alloc_q addr:   0x%p\n",
382 		(unsigned long long) dev_info.alloc_phys, kni->alloc_q);
383 	pr_debug("free_phys:    0x%016llx, free_q addr:    0x%p\n",
384 		(unsigned long long) dev_info.free_phys, kni->free_q);
385 	pr_debug("req_phys:     0x%016llx, req_q addr:     0x%p\n",
386 		(unsigned long long) dev_info.req_phys, kni->req_q);
387 	pr_debug("resp_phys:    0x%016llx, resp_q addr:    0x%p\n",
388 		(unsigned long long) dev_info.resp_phys, kni->resp_q);
389 	pr_debug("mbuf_size:    %u\n", kni->mbuf_size);
390 
391 	pr_debug("PCI: %02x:%02x.%02x %04x:%04x\n",
392 					dev_info.bus,
393 					dev_info.devid,
394 					dev_info.function,
395 					dev_info.vendor_id,
396 					dev_info.device_id);
397 #ifdef RTE_KNI_KMOD_ETHTOOL
398 	pci = pci_get_device(dev_info.vendor_id, dev_info.device_id, NULL);
399 
400 	/* Support Ethtool */
401 	while (pci) {
402 		pr_debug("pci_bus: %02x:%02x:%02x\n",
403 					pci->bus->number,
404 					PCI_SLOT(pci->devfn),
405 					PCI_FUNC(pci->devfn));
406 
407 		if ((pci->bus->number == dev_info.bus) &&
408 			(PCI_SLOT(pci->devfn) == dev_info.devid) &&
409 			(PCI_FUNC(pci->devfn) == dev_info.function)) {
410 			found_pci = pci;
411 
412 			if (pci_match_id(ixgbe_pci_tbl, found_pci))
413 				ret = ixgbe_kni_probe(found_pci, &lad_dev);
414 			else if (pci_match_id(igb_pci_tbl, found_pci))
415 				ret = igb_kni_probe(found_pci, &lad_dev);
416 			else
417 				ret = -1;
418 
419 			pr_debug("PCI found: pci=0x%p, lad_dev=0x%p\n",
420 							pci, lad_dev);
421 			if (ret == 0) {
422 				kni->lad_dev = lad_dev;
423 				kni_set_ethtool_ops(kni->net_dev);
424 			} else {
425 				pr_err("Device not supported by ethtool");
426 				kni->lad_dev = NULL;
427 			}
428 
429 			kni->pci_dev = found_pci;
430 			kni->device_id = dev_info.device_id;
431 			break;
432 		}
433 		pci = pci_get_device(dev_info.vendor_id,
434 				dev_info.device_id, pci);
435 	}
436 	if (pci)
437 		pci_dev_put(pci);
438 #endif
439 
440 	if (kni->lad_dev)
441 		ether_addr_copy(net_dev->dev_addr, kni->lad_dev->dev_addr);
442 	else {
443 		/* if user has provided a valid mac address */
444 		if (is_valid_ether_addr((unsigned char *)(dev_info.mac_addr)))
445 			memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
446 		else
447 			/*
448 			 * Generate random mac address. eth_random_addr() is the
449 			 * newer version of generating mac address in kernel.
450 			 */
451 			random_ether_addr(net_dev->dev_addr);
452 	}
453 
454 	if (dev_info.mtu)
455 		net_dev->mtu = dev_info.mtu;
456 
457 	ret = register_netdev(net_dev);
458 	if (ret) {
459 		pr_err("error %i registering device \"%s\"\n",
460 					ret, dev_info.name);
461 		kni->net_dev = NULL;
462 		kni_dev_remove(kni);
463 		free_netdev(net_dev);
464 		return -ENODEV;
465 	}
466 
467 	ret = kni_run_thread(knet, kni, dev_info.force_bind);
468 	if (ret != 0)
469 		return ret;
470 
471 	down_write(&knet->kni_list_lock);
472 	list_add(&kni->list, &knet->kni_list_head);
473 	up_write(&knet->kni_list_lock);
474 
475 	return 0;
476 }
477 
478 static int
479 kni_ioctl_release(struct net *net, uint32_t ioctl_num,
480 		unsigned long ioctl_param)
481 {
482 	struct kni_net *knet = net_generic(net, kni_net_id);
483 	int ret = -EINVAL;
484 	struct kni_dev *dev, *n;
485 	struct rte_kni_device_info dev_info;
486 
487 	if (_IOC_SIZE(ioctl_num) > sizeof(dev_info))
488 		return -EINVAL;
489 
490 	ret = copy_from_user(&dev_info, (void *)ioctl_param, sizeof(dev_info));
491 	if (ret) {
492 		pr_err("copy_from_user in kni_ioctl_release");
493 		return -EIO;
494 	}
495 
496 	/* Release the network device according to its name */
497 	if (strlen(dev_info.name) == 0)
498 		return ret;
499 
500 	down_write(&knet->kni_list_lock);
501 	list_for_each_entry_safe(dev, n, &knet->kni_list_head, list) {
502 		if (strncmp(dev->name, dev_info.name, RTE_KNI_NAMESIZE) != 0)
503 			continue;
504 
505 		if (multiple_kthread_on && dev->pthread != NULL) {
506 			kthread_stop(dev->pthread);
507 			dev->pthread = NULL;
508 		}
509 
510 		kni_dev_remove(dev);
511 		list_del(&dev->list);
512 		ret = 0;
513 		break;
514 	}
515 	up_write(&knet->kni_list_lock);
516 	pr_info("%s release kni named %s\n",
517 		(ret == 0 ? "Successfully" : "Unsuccessfully"), dev_info.name);
518 
519 	return ret;
520 }
521 
522 static int
523 kni_ioctl(struct inode *inode, uint32_t ioctl_num, unsigned long ioctl_param)
524 {
525 	int ret = -EINVAL;
526 	struct net *net = current->nsproxy->net_ns;
527 
528 	pr_debug("IOCTL num=0x%0x param=0x%0lx\n", ioctl_num, ioctl_param);
529 
530 	/*
531 	 * Switch according to the ioctl called
532 	 */
533 	switch (_IOC_NR(ioctl_num)) {
534 	case _IOC_NR(RTE_KNI_IOCTL_TEST):
535 		/* For test only, not used */
536 		break;
537 	case _IOC_NR(RTE_KNI_IOCTL_CREATE):
538 		ret = kni_ioctl_create(net, ioctl_num, ioctl_param);
539 		break;
540 	case _IOC_NR(RTE_KNI_IOCTL_RELEASE):
541 		ret = kni_ioctl_release(net, ioctl_num, ioctl_param);
542 		break;
543 	default:
544 		pr_debug("IOCTL default\n");
545 		break;
546 	}
547 
548 	return ret;
549 }
550 
551 static int
552 kni_compat_ioctl(struct inode *inode, uint32_t ioctl_num,
553 		unsigned long ioctl_param)
554 {
555 	/* 32 bits app on 64 bits OS to be supported later */
556 	pr_debug("Not implemented.\n");
557 
558 	return -EINVAL;
559 }
560 
561 static const struct file_operations kni_fops = {
562 	.owner = THIS_MODULE,
563 	.open = kni_open,
564 	.release = kni_release,
565 	.unlocked_ioctl = (void *)kni_ioctl,
566 	.compat_ioctl = (void *)kni_compat_ioctl,
567 };
568 
569 static struct miscdevice kni_misc = {
570 	.minor = MISC_DYNAMIC_MINOR,
571 	.name = KNI_DEVICE,
572 	.fops = &kni_fops,
573 };
574 
575 static int __init
576 kni_parse_kthread_mode(void)
577 {
578 	if (!kthread_mode)
579 		return 0;
580 
581 	if (strcmp(kthread_mode, "single") == 0)
582 		return 0;
583 	else if (strcmp(kthread_mode, "multiple") == 0)
584 		multiple_kthread_on = 1;
585 	else
586 		return -1;
587 
588 	return 0;
589 }
590 
591 static int __init
592 kni_init(void)
593 {
594 	int rc;
595 
596 	if (kni_parse_kthread_mode() < 0) {
597 		pr_err("Invalid parameter for kthread_mode\n");
598 		return -EINVAL;
599 	}
600 
601 	if (multiple_kthread_on == 0)
602 		pr_debug("Single kernel thread for all KNI devices\n");
603 	else
604 		pr_debug("Multiple kernel thread mode enabled\n");
605 
606 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
607 	rc = register_pernet_subsys(&kni_net_ops);
608 #else
609 	rc = register_pernet_gen_subsys(&kni_net_id, &kni_net_ops);
610 #endif
611 	if (rc)
612 		return -EPERM;
613 
614 	rc = misc_register(&kni_misc);
615 	if (rc != 0) {
616 		pr_err("Misc registration failed\n");
617 		goto out;
618 	}
619 
620 	/* Configure the lo mode according to the input parameter */
621 	kni_net_config_lo_mode(lo_mode);
622 
623 	return 0;
624 
625 out:
626 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
627 	unregister_pernet_subsys(&kni_net_ops);
628 #else
629 	unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
630 #endif
631 	return rc;
632 }
633 
634 static void __exit
635 kni_exit(void)
636 {
637 	misc_deregister(&kni_misc);
638 #ifdef HAVE_SIMPLIFIED_PERNET_OPERATIONS
639 	unregister_pernet_subsys(&kni_net_ops);
640 #else
641 	unregister_pernet_gen_subsys(kni_net_id, &kni_net_ops);
642 #endif
643 }
644 
645 module_init(kni_init);
646 module_exit(kni_exit);
647 
648 module_param(lo_mode, charp, S_IRUGO | S_IWUSR);
649 MODULE_PARM_DESC(lo_mode,
650 "KNI loopback mode (default=lo_mode_none):\n"
651 "    lo_mode_none        Kernel loopback disabled\n"
652 "    lo_mode_fifo        Enable kernel loopback with fifo\n"
653 "    lo_mode_fifo_skb    Enable kernel loopback with fifo and skb buffer\n"
654 "\n"
655 );
656 
657 module_param(kthread_mode, charp, S_IRUGO);
658 MODULE_PARM_DESC(kthread_mode,
659 "Kernel thread mode (default=single):\n"
660 "    single    Single kernel thread mode enabled.\n"
661 "    multiple  Multiple kernel thread mode enabled.\n"
662 "\n"
663 );
664