aboutsummaryrefslogtreecommitdiffstats
path: root/net/core/net-sysfs.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/core/net-sysfs.c')
-rw-r--r--net/core/net-sysfs.c377
1 files changed, 362 insertions, 15 deletions
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index 59cfc7d8fc45..99e7052d7323 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -14,9 +14,12 @@
14#include <linux/netdevice.h> 14#include <linux/netdevice.h>
15#include <linux/if_arp.h> 15#include <linux/if_arp.h>
16#include <linux/slab.h> 16#include <linux/slab.h>
17#include <linux/nsproxy.h>
17#include <net/sock.h> 18#include <net/sock.h>
19#include <net/net_namespace.h>
18#include <linux/rtnetlink.h> 20#include <linux/rtnetlink.h>
19#include <linux/wireless.h> 21#include <linux/wireless.h>
22#include <linux/vmalloc.h>
20#include <net/wext.h> 23#include <net/wext.h>
21 24
22#include "net-sysfs.h" 25#include "net-sysfs.h"
@@ -466,18 +469,345 @@ static struct attribute_group wireless_group = {
466 .attrs = wireless_attrs, 469 .attrs = wireless_attrs,
467}; 470};
468#endif 471#endif
469
470#endif /* CONFIG_SYSFS */ 472#endif /* CONFIG_SYSFS */
471 473
474#ifdef CONFIG_RPS
475/*
476 * RX queue sysfs structures and functions.
477 */
478struct rx_queue_attribute {
479 struct attribute attr;
480 ssize_t (*show)(struct netdev_rx_queue *queue,
481 struct rx_queue_attribute *attr, char *buf);
482 ssize_t (*store)(struct netdev_rx_queue *queue,
483 struct rx_queue_attribute *attr, const char *buf, size_t len);
484};
485#define to_rx_queue_attr(_attr) container_of(_attr, \
486 struct rx_queue_attribute, attr)
487
488#define to_rx_queue(obj) container_of(obj, struct netdev_rx_queue, kobj)
489
490static ssize_t rx_queue_attr_show(struct kobject *kobj, struct attribute *attr,
491 char *buf)
492{
493 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
494 struct netdev_rx_queue *queue = to_rx_queue(kobj);
495
496 if (!attribute->show)
497 return -EIO;
498
499 return attribute->show(queue, attribute, buf);
500}
501
502static ssize_t rx_queue_attr_store(struct kobject *kobj, struct attribute *attr,
503 const char *buf, size_t count)
504{
505 struct rx_queue_attribute *attribute = to_rx_queue_attr(attr);
506 struct netdev_rx_queue *queue = to_rx_queue(kobj);
507
508 if (!attribute->store)
509 return -EIO;
510
511 return attribute->store(queue, attribute, buf, count);
512}
513
514static struct sysfs_ops rx_queue_sysfs_ops = {
515 .show = rx_queue_attr_show,
516 .store = rx_queue_attr_store,
517};
518
519static ssize_t show_rps_map(struct netdev_rx_queue *queue,
520 struct rx_queue_attribute *attribute, char *buf)
521{
522 struct rps_map *map;
523 cpumask_var_t mask;
524 size_t len = 0;
525 int i;
526
527 if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
528 return -ENOMEM;
529
530 rcu_read_lock();
531 map = rcu_dereference(queue->rps_map);
532 if (map)
533 for (i = 0; i < map->len; i++)
534 cpumask_set_cpu(map->cpus[i], mask);
535
536 len += cpumask_scnprintf(buf + len, PAGE_SIZE, mask);
537 if (PAGE_SIZE - len < 3) {
538 rcu_read_unlock();
539 free_cpumask_var(mask);
540 return -EINVAL;
541 }
542 rcu_read_unlock();
543
544 free_cpumask_var(mask);
545 len += sprintf(buf + len, "\n");
546 return len;
547}
548
549static void rps_map_release(struct rcu_head *rcu)
550{
551 struct rps_map *map = container_of(rcu, struct rps_map, rcu);
552
553 kfree(map);
554}
555
556static ssize_t store_rps_map(struct netdev_rx_queue *queue,
557 struct rx_queue_attribute *attribute,
558 const char *buf, size_t len)
559{
560 struct rps_map *old_map, *map;
561 cpumask_var_t mask;
562 int err, cpu, i;
563 static DEFINE_SPINLOCK(rps_map_lock);
564
565 if (!capable(CAP_NET_ADMIN))
566 return -EPERM;
567
568 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
569 return -ENOMEM;
570
571 err = bitmap_parse(buf, len, cpumask_bits(mask), nr_cpumask_bits);
572 if (err) {
573 free_cpumask_var(mask);
574 return err;
575 }
576
577 map = kzalloc(max_t(unsigned,
578 RPS_MAP_SIZE(cpumask_weight(mask)), L1_CACHE_BYTES),
579 GFP_KERNEL);
580 if (!map) {
581 free_cpumask_var(mask);
582 return -ENOMEM;
583 }
584
585 i = 0;
586 for_each_cpu_and(cpu, mask, cpu_online_mask)
587 map->cpus[i++] = cpu;
588
589 if (i)
590 map->len = i;
591 else {
592 kfree(map);
593 map = NULL;
594 }
595
596 spin_lock(&rps_map_lock);
597 old_map = queue->rps_map;
598 rcu_assign_pointer(queue->rps_map, map);
599 spin_unlock(&rps_map_lock);
600
601 if (old_map)
602 call_rcu(&old_map->rcu, rps_map_release);
603
604 free_cpumask_var(mask);
605 return len;
606}
607
608static ssize_t show_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
609 struct rx_queue_attribute *attr,
610 char *buf)
611{
612 struct rps_dev_flow_table *flow_table;
613 unsigned int val = 0;
614
615 rcu_read_lock();
616 flow_table = rcu_dereference(queue->rps_flow_table);
617 if (flow_table)
618 val = flow_table->mask + 1;
619 rcu_read_unlock();
620
621 return sprintf(buf, "%u\n", val);
622}
623
624static void rps_dev_flow_table_release_work(struct work_struct *work)
625{
626 struct rps_dev_flow_table *table = container_of(work,
627 struct rps_dev_flow_table, free_work);
628
629 vfree(table);
630}
631
632static void rps_dev_flow_table_release(struct rcu_head *rcu)
633{
634 struct rps_dev_flow_table *table = container_of(rcu,
635 struct rps_dev_flow_table, rcu);
636
637 INIT_WORK(&table->free_work, rps_dev_flow_table_release_work);
638 schedule_work(&table->free_work);
639}
640
641static ssize_t store_rps_dev_flow_table_cnt(struct netdev_rx_queue *queue,
642 struct rx_queue_attribute *attr,
643 const char *buf, size_t len)
644{
645 unsigned int count;
646 char *endp;
647 struct rps_dev_flow_table *table, *old_table;
648 static DEFINE_SPINLOCK(rps_dev_flow_lock);
649
650 if (!capable(CAP_NET_ADMIN))
651 return -EPERM;
652
653 count = simple_strtoul(buf, &endp, 0);
654 if (endp == buf)
655 return -EINVAL;
656
657 if (count) {
658 int i;
659
660 if (count > 1<<30) {
661 /* Enforce a limit to prevent overflow */
662 return -EINVAL;
663 }
664 count = roundup_pow_of_two(count);
665 table = vmalloc(RPS_DEV_FLOW_TABLE_SIZE(count));
666 if (!table)
667 return -ENOMEM;
668
669 table->mask = count - 1;
670 for (i = 0; i < count; i++)
671 table->flows[i].cpu = RPS_NO_CPU;
672 } else
673 table = NULL;
674
675 spin_lock(&rps_dev_flow_lock);
676 old_table = queue->rps_flow_table;
677 rcu_assign_pointer(queue->rps_flow_table, table);
678 spin_unlock(&rps_dev_flow_lock);
679
680 if (old_table)
681 call_rcu(&old_table->rcu, rps_dev_flow_table_release);
682
683 return len;
684}
685
686static struct rx_queue_attribute rps_cpus_attribute =
687 __ATTR(rps_cpus, S_IRUGO | S_IWUSR, show_rps_map, store_rps_map);
688
689
690static struct rx_queue_attribute rps_dev_flow_table_cnt_attribute =
691 __ATTR(rps_flow_cnt, S_IRUGO | S_IWUSR,
692 show_rps_dev_flow_table_cnt, store_rps_dev_flow_table_cnt);
693
694static struct attribute *rx_queue_default_attrs[] = {
695 &rps_cpus_attribute.attr,
696 &rps_dev_flow_table_cnt_attribute.attr,
697 NULL
698};
699
700static void rx_queue_release(struct kobject *kobj)
701{
702 struct netdev_rx_queue *queue = to_rx_queue(kobj);
703 struct netdev_rx_queue *first = queue->first;
704
705 if (queue->rps_map)
706 call_rcu(&queue->rps_map->rcu, rps_map_release);
707
708 if (queue->rps_flow_table)
709 call_rcu(&queue->rps_flow_table->rcu,
710 rps_dev_flow_table_release);
711
712 if (atomic_dec_and_test(&first->count))
713 kfree(first);
714}
715
716static struct kobj_type rx_queue_ktype = {
717 .sysfs_ops = &rx_queue_sysfs_ops,
718 .release = rx_queue_release,
719 .default_attrs = rx_queue_default_attrs,
720};
721
722static int rx_queue_add_kobject(struct net_device *net, int index)
723{
724 struct netdev_rx_queue *queue = net->_rx + index;
725 struct kobject *kobj = &queue->kobj;
726 int error = 0;
727
728 kobj->kset = net->queues_kset;
729 error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
730 "rx-%u", index);
731 if (error) {
732 kobject_put(kobj);
733 return error;
734 }
735
736 kobject_uevent(kobj, KOBJ_ADD);
737
738 return error;
739}
740
741static int rx_queue_register_kobjects(struct net_device *net)
742{
743 int i;
744 int error = 0;
745
746 net->queues_kset = kset_create_and_add("queues",
747 NULL, &net->dev.kobj);
748 if (!net->queues_kset)
749 return -ENOMEM;
750 for (i = 0; i < net->num_rx_queues; i++) {
751 error = rx_queue_add_kobject(net, i);
752 if (error)
753 break;
754 }
755
756 if (error)
757 while (--i >= 0)
758 kobject_put(&net->_rx[i].kobj);
759
760 return error;
761}
762
763static void rx_queue_remove_kobjects(struct net_device *net)
764{
765 int i;
766
767 for (i = 0; i < net->num_rx_queues; i++)
768 kobject_put(&net->_rx[i].kobj);
769 kset_unregister(net->queues_kset);
770}
771#endif /* CONFIG_RPS */
772
773static const void *net_current_ns(void)
774{
775 return current->nsproxy->net_ns;
776}
777
778static const void *net_initial_ns(void)
779{
780 return &init_net;
781}
782
783static const void *net_netlink_ns(struct sock *sk)
784{
785 return sock_net(sk);
786}
787
788static struct kobj_ns_type_operations net_ns_type_operations = {
789 .type = KOBJ_NS_TYPE_NET,
790 .current_ns = net_current_ns,
791 .netlink_ns = net_netlink_ns,
792 .initial_ns = net_initial_ns,
793};
794
795static void net_kobj_ns_exit(struct net *net)
796{
797 kobj_ns_exit(KOBJ_NS_TYPE_NET, net);
798}
799
800static struct pernet_operations kobj_net_ops = {
801 .exit = net_kobj_ns_exit,
802};
803
804
472#ifdef CONFIG_HOTPLUG 805#ifdef CONFIG_HOTPLUG
473static int netdev_uevent(struct device *d, struct kobj_uevent_env *env) 806static int netdev_uevent(struct device *d, struct kobj_uevent_env *env)
474{ 807{
475 struct net_device *dev = to_net_dev(d); 808 struct net_device *dev = to_net_dev(d);
476 int retval; 809 int retval;
477 810
478 if (!net_eq(dev_net(dev), &init_net))
479 return 0;
480
481 /* pass interface to uevent. */ 811 /* pass interface to uevent. */
482 retval = add_uevent_var(env, "INTERFACE=%s", dev->name); 812 retval = add_uevent_var(env, "INTERFACE=%s", dev->name);
483 if (retval) 813 if (retval)
@@ -507,6 +837,13 @@ static void netdev_release(struct device *d)
507 kfree((char *)dev - dev->padded); 837 kfree((char *)dev - dev->padded);
508} 838}
509 839
840static const void *net_namespace(struct device *d)
841{
842 struct net_device *dev;
843 dev = container_of(d, struct net_device, dev);
844 return dev_net(dev);
845}
846
510static struct class net_class = { 847static struct class net_class = {
511 .name = "net", 848 .name = "net",
512 .dev_release = netdev_release, 849 .dev_release = netdev_release,
@@ -516,6 +853,8 @@ static struct class net_class = {
516#ifdef CONFIG_HOTPLUG 853#ifdef CONFIG_HOTPLUG
517 .dev_uevent = netdev_uevent, 854 .dev_uevent = netdev_uevent,
518#endif 855#endif
856 .ns_type = &net_ns_type_operations,
857 .namespace = net_namespace,
519}; 858};
520 859
521/* Delete sysfs entries but hold kobject reference until after all 860/* Delete sysfs entries but hold kobject reference until after all
@@ -527,8 +866,9 @@ void netdev_unregister_kobject(struct net_device * net)
527 866
528 kobject_get(&dev->kobj); 867 kobject_get(&dev->kobj);
529 868
530 if (!net_eq(dev_net(net), &init_net)) 869#ifdef CONFIG_RPS
531 return; 870 rx_queue_remove_kobjects(net);
871#endif
532 872
533 device_del(dev); 873 device_del(dev);
534} 874}
@@ -538,7 +878,9 @@ int netdev_register_kobject(struct net_device *net)
538{ 878{
539 struct device *dev = &(net->dev); 879 struct device *dev = &(net->dev);
540 const struct attribute_group **groups = net->sysfs_groups; 880 const struct attribute_group **groups = net->sysfs_groups;
881 int error = 0;
541 882
883 device_initialize(dev);
542 dev->class = &net_class; 884 dev->class = &net_class;
543 dev->platform_data = net; 885 dev->platform_data = net;
544 dev->groups = groups; 886 dev->groups = groups;
@@ -561,10 +903,19 @@ int netdev_register_kobject(struct net_device *net)
561#endif 903#endif
562#endif /* CONFIG_SYSFS */ 904#endif /* CONFIG_SYSFS */
563 905
564 if (!net_eq(dev_net(net), &init_net)) 906 error = device_add(dev);
565 return 0; 907 if (error)
908 return error;
909
910#ifdef CONFIG_RPS
911 error = rx_queue_register_kobjects(net);
912 if (error) {
913 device_del(dev);
914 return error;
915 }
916#endif
566 917
567 return device_add(dev); 918 return error;
568} 919}
569 920
570int netdev_class_create_file(struct class_attribute *class_attr) 921int netdev_class_create_file(struct class_attribute *class_attr)
@@ -580,13 +931,9 @@ void netdev_class_remove_file(struct class_attribute *class_attr)
580EXPORT_SYMBOL(netdev_class_create_file); 931EXPORT_SYMBOL(netdev_class_create_file);
581EXPORT_SYMBOL(netdev_class_remove_file); 932EXPORT_SYMBOL(netdev_class_remove_file);
582 933
583void netdev_initialize_kobject(struct net_device *net)
584{
585 struct device *device = &(net->dev);
586 device_initialize(device);
587}
588
589int netdev_kobject_init(void) 934int netdev_kobject_init(void)
590{ 935{
936 kobj_ns_type_register(&net_ns_type_operations);
937 register_pernet_subsys(&kobj_net_ops);
591 return class_register(&net_class); 938 return class_register(&net_class);
592} 939}