From 806e39cfdf17ae61221eefb6e292f99c8abc2efc Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 12 Jun 2009 16:45:01 -0700 Subject: [PATCH] datapath: Add sysfs support for all (otherwise supported) Linux versions. This turned out to be less trouble than I expected. This builds successfully against 2.6.18 through 2.6.28. Justin has lightly tested it on a 2.6.27 kernel provided by Citrix. --- datapath/brc_sysfs.h | 9 -- datapath/brc_sysfs_dp.c | 134 ++++++++---------- datapath/brc_sysfs_if.c | 24 ++-- datapath/brcompat.c | 6 - datapath/datapath.c | 7 +- datapath/datapath.h | 9 +- datapath/linux-2.6/Modules.mk | 1 + .../compat-2.6/include/linux/kobject.h | 23 +++ .../compat-2.6/include/linux/netdevice.h | 13 +- 9 files changed, 121 insertions(+), 105 deletions(-) create mode 100644 datapath/linux-2.6/compat-2.6/include/linux/kobject.h diff --git a/datapath/brc_sysfs.h b/datapath/brc_sysfs.h index 0c72fb22..245492f4 100644 --- a/datapath/brc_sysfs.h +++ b/datapath/brc_sysfs.h @@ -12,14 +12,5 @@ int brc_sysfs_del_dp(struct datapath *dp); int brc_sysfs_add_if(struct net_bridge_port *p); int brc_sysfs_del_if(struct net_bridge_port *p); -#include -#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,18) -#define SUPPORT_SYSFS 1 -#else -/* We only support sysfs on Linux 2.6.18 because that's the only place we - * really need it (on Xen, for brcompat) and it's a big pain to try to support - * multiple versions. */ -#endif - #endif /* brc_sysfs.h */ diff --git a/datapath/brc_sysfs_dp.c b/datapath/brc_sysfs_dp.c index fc02f279..6b5dd5cc 100644 --- a/datapath/brc_sysfs_dp.c +++ b/datapath/brc_sysfs_dp.c @@ -20,22 +20,26 @@ #include "datapath.h" #include "dp_dev.h" -#ifdef SUPPORT_SYSFS +#ifdef CONFIG_SYSFS #define to_dev(obj) container_of(obj, struct device, kobj) /* Hack to attempt to build on more platforms. */ #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21) -#define to_kobj(d) &(d)->class_dev.kobj #define BRC_DEVICE_ATTR CLASS_DEVICE_ATTR +#define DEVICE_PARAMS struct class_device *d +#define DEVICE_ARGS d +#define DEV_ATTR(NAME) class_device_attr_##NAME #else -#define to_kobj(d) &(d)->dev.kobj #define BRC_DEVICE_ATTR DEVICE_ATTR +#define DEVICE_PARAMS struct device *d, struct device_attribute *attr +#define DEVICE_ARGS d, attr +#define DEV_ATTR(NAME) dev_attr_##NAME #endif /* * Common code for storing bridge parameters. */ -static ssize_t store_bridge_parm(struct class_device *d, +static ssize_t store_bridge_parm(DEVICE_PARAMS, const char *buf, size_t len, void (*set)(struct datapath *, unsigned long)) { @@ -68,8 +72,7 @@ static ssize_t store_bridge_parm(struct class_device *d, } -static ssize_t show_forward_delay(struct class_device *d, - char *buf) +static ssize_t show_forward_delay(DEVICE_PARAMS, char *buf) { #if 0 struct datapath *dp = dp_dev_get_dp(to_net_dev(d)); @@ -91,15 +94,15 @@ static void set_forward_delay(struct datapath *dp, unsigned long val) #endif } -static ssize_t store_forward_delay(struct class_device *d, +static ssize_t store_forward_delay(DEVICE_PARAMS, const char *buf, size_t len) { - return store_bridge_parm(d, buf, len, set_forward_delay); + return store_bridge_parm(DEVICE_ARGS, buf, len, set_forward_delay); } static BRC_DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR, show_forward_delay, store_forward_delay); -static ssize_t show_hello_time(struct class_device *d, char *buf) +static ssize_t show_hello_time(DEVICE_PARAMS, char *buf) { #if 0 return sprintf(buf, "%lu\n", @@ -121,17 +124,16 @@ static void set_hello_time(struct datapath *dp, unsigned long val) #endif } -static ssize_t store_hello_time(struct class_device *d, +static ssize_t store_hello_time(DEVICE_PARAMS, const char *buf, size_t len) { - return store_bridge_parm(d, buf, len, set_hello_time); + return store_bridge_parm(DEVICE_ARGS, buf, len, set_hello_time); } static BRC_DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time, store_hello_time); -static ssize_t show_max_age(struct class_device *d, - char *buf) +static ssize_t show_max_age(DEVICE_PARAMS, char *buf) { #if 0 return sprintf(buf, "%lu\n", @@ -153,15 +155,14 @@ static void set_max_age(struct datapath *dp, unsigned long val) #endif } -static ssize_t store_max_age(struct class_device *d, +static ssize_t store_max_age(DEVICE_PARAMS, const char *buf, size_t len) { - return store_bridge_parm(d, buf, len, set_max_age); + return store_bridge_parm(DEVICE_ARGS, buf, len, set_max_age); } static BRC_DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age, store_max_age); -static ssize_t show_ageing_time(struct class_device *d, - char *buf) +static ssize_t show_ageing_time(DEVICE_PARAMS, char *buf) { #if 0 struct datapath *dp = dp_dev_get_dp(to_net_dev(d)); @@ -180,16 +181,15 @@ static void set_ageing_time(struct datapath *dp, unsigned long val) #endif } -static ssize_t store_ageing_time(struct class_device *d, +static ssize_t store_ageing_time(DEVICE_PARAMS, const char *buf, size_t len) { - return store_bridge_parm(d, buf, len, set_ageing_time); + return store_bridge_parm(DEVICE_ARGS, buf, len, set_ageing_time); } static BRC_DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time, store_ageing_time); -static ssize_t show_stp_state(struct class_device *d, - char *buf) +static ssize_t show_stp_state(DEVICE_PARAMS, char *buf) { #if 0 struct datapath *dp = dp_dev_get_dp(to_net_dev(d)); @@ -200,7 +200,7 @@ static ssize_t show_stp_state(struct class_device *d, } -static ssize_t store_stp_state(struct class_device *d, +static ssize_t store_stp_state(DEVICE_PARAMS, const char *buf, size_t len) { @@ -228,8 +228,7 @@ static ssize_t store_stp_state(struct class_device *d, static BRC_DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state, store_stp_state); -static ssize_t show_priority(struct class_device *d, - char *buf) +static ssize_t show_priority(DEVICE_PARAMS, char *buf) { #if 0 struct datapath *dp = dp_dev_get_dp(to_net_dev(d)); @@ -249,15 +248,14 @@ static void set_priority(struct datapath *dp, unsigned long val) #endif } -static ssize_t store_priority(struct class_device *d, +static ssize_t store_priority(DEVICE_PARAMS, const char *buf, size_t len) { - return store_bridge_parm(d, buf, len, set_priority); + return store_bridge_parm(DEVICE_ARGS, buf, len, set_priority); } static BRC_DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority); -static ssize_t show_root_id(struct class_device *d, - char *buf) +static ssize_t show_root_id(DEVICE_PARAMS, char *buf) { #if 0 return br_show_bridge_id(buf, &to_bridge(d)->designated_root); @@ -267,8 +265,7 @@ static ssize_t show_root_id(struct class_device *d, } static BRC_DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL); -static ssize_t show_bridge_id(struct class_device *d, - char *buf) +static ssize_t show_bridge_id(DEVICE_PARAMS, char *buf) { struct datapath *dp = dp_dev_get_dp(to_net_dev(d)); const unsigned char *addr = dp->ports[ODPP_LOCAL]->dev->dev_addr; @@ -279,8 +276,7 @@ static ssize_t show_bridge_id(struct class_device *d, } static BRC_DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL); -static ssize_t show_root_port(struct class_device *d, - char *buf) +static ssize_t show_root_port(DEVICE_PARAMS, char *buf) { #if 0 return sprintf(buf, "%d\n", to_bridge(d)->root_port); @@ -290,8 +286,7 @@ static ssize_t show_root_port(struct class_device *d, } static BRC_DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL); -static ssize_t show_root_path_cost(struct class_device *d, - char *buf) +static ssize_t show_root_path_cost(DEVICE_PARAMS, char *buf) { #if 0 return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost); @@ -301,8 +296,7 @@ static ssize_t show_root_path_cost(struct class_device *d, } static BRC_DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL); -static ssize_t show_topology_change(struct class_device *d, - char *buf) +static ssize_t show_topology_change(DEVICE_PARAMS, char *buf) { #if 0 return sprintf(buf, "%d\n", to_bridge(d)->topology_change); @@ -312,8 +306,7 @@ static ssize_t show_topology_change(struct class_device *d, } static BRC_DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL); -static ssize_t show_topology_change_detected(struct class_device *d, - char *buf) +static ssize_t show_topology_change_detected(DEVICE_PARAMS, char *buf) { #if 0 struct datapath *dp = dp_dev_get_dp(to_net_dev(d)); @@ -325,8 +318,7 @@ static ssize_t show_topology_change_detected(struct class_device *d, static BRC_DEVICE_ATTR(topology_change_detected, S_IRUGO, show_topology_change_detected, NULL); -static ssize_t show_hello_timer(struct class_device *d, - char *buf) +static ssize_t show_hello_timer(DEVICE_PARAMS, char *buf) { #if 0 struct datapath *dp = dp_dev_get_dp(to_net_dev(d)); @@ -337,8 +329,7 @@ static ssize_t show_hello_timer(struct class_device *d, } static BRC_DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL); -static ssize_t show_tcn_timer(struct class_device *d, - char *buf) +static ssize_t show_tcn_timer(DEVICE_PARAMS, char *buf) { #if 0 struct datapath *dp = dp_dev_get_dp(to_net_dev(d)); @@ -349,8 +340,7 @@ static ssize_t show_tcn_timer(struct class_device *d, } static BRC_DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL); -static ssize_t show_topology_change_timer(struct class_device *d, - char *buf) +static ssize_t show_topology_change_timer(DEVICE_PARAMS, char *buf) { #if 0 struct datapath *dp = dp_dev_get_dp(to_net_dev(d)); @@ -362,8 +352,7 @@ static ssize_t show_topology_change_timer(struct class_device *d, static BRC_DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer, NULL); -static ssize_t show_gc_timer(struct class_device *d, - char *buf) +static ssize_t show_gc_timer(DEVICE_PARAMS, char *buf) { #if 0 struct datapath *dp = dp_dev_get_dp(to_net_dev(d)); @@ -374,8 +363,7 @@ static ssize_t show_gc_timer(struct class_device *d, } static BRC_DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL); -static ssize_t show_group_addr(struct class_device *d, - char *buf) +static ssize_t show_group_addr(DEVICE_PARAMS, char *buf) { #if 0 struct datapath *dp = dp_dev_get_dp(to_net_dev(d)); @@ -388,7 +376,7 @@ static ssize_t show_group_addr(struct class_device *d, #endif } -static ssize_t store_group_addr(struct class_device *d, +static ssize_t store_group_addr(DEVICE_PARAMS, const char *buf, size_t len) { struct datapath *dp = dp_dev_get_dp(to_net_dev(d)); @@ -431,23 +419,23 @@ static BRC_DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR, show_group_addr, store_group_addr); static struct attribute *bridge_attrs[] = { - &class_device_attr_forward_delay.attr, - &class_device_attr_hello_time.attr, - &class_device_attr_max_age.attr, - &class_device_attr_ageing_time.attr, - &class_device_attr_stp_state.attr, - &class_device_attr_priority.attr, - &class_device_attr_bridge_id.attr, - &class_device_attr_root_id.attr, - &class_device_attr_root_path_cost.attr, - &class_device_attr_root_port.attr, - &class_device_attr_topology_change.attr, - &class_device_attr_topology_change_detected.attr, - &class_device_attr_hello_timer.attr, - &class_device_attr_tcn_timer.attr, - &class_device_attr_topology_change_timer.attr, - &class_device_attr_gc_timer.attr, - &class_device_attr_group_addr.attr, + &DEV_ATTR(forward_delay).attr, + &DEV_ATTR(hello_time).attr, + &DEV_ATTR(max_age).attr, + &DEV_ATTR(ageing_time).attr, + &DEV_ATTR(stp_state).attr, + &DEV_ATTR(priority).attr, + &DEV_ATTR(bridge_id).attr, + &DEV_ATTR(root_id).attr, + &DEV_ATTR(root_path_cost).attr, + &DEV_ATTR(root_port).attr, + &DEV_ATTR(topology_change).attr, + &DEV_ATTR(topology_change_detected).attr, + &DEV_ATTR(hello_timer).attr, + &DEV_ATTR(tcn_timer).attr, + &DEV_ATTR(topology_change_timer).attr, + &DEV_ATTR(gc_timer).attr, + &DEV_ATTR(group_addr).attr, NULL }; @@ -468,7 +456,7 @@ static struct attribute_group bridge_group = { */ int brc_sysfs_add_dp(struct datapath *dp) { - struct kobject *kobj = to_kobj(dp->ports[ODPP_LOCAL]->dev); + struct kobject *kobj = &dp->ports[ODPP_LOCAL]->dev->NETDEV_DEV_MEMBER.kobj; int err; err = sysfs_create_group(kobj, &bridge_group); @@ -487,12 +475,12 @@ int brc_sysfs_add_dp(struct datapath *dp) err = kobject_register(&dp->ifobj); if (err) { pr_info("%s: can't add kobject (directory) %s/%s\n", - __FUNCTION__, dp_name(dp), dp->ifobj.name); + __FUNCTION__, dp_name(dp), kobject_name(&dp->ifobj)); goto out2; } #else - br->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, kobj); - if (!br->ifobj) { + dp->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, kobj); + if (!dp->ifobj) { pr_info("%s: can't add kobject (directory) %s/%s\n", __func__, dp_name(dp), SYSFS_BRIDGE_PORT_SUBDIR); goto out2; @@ -508,7 +496,7 @@ int brc_sysfs_add_dp(struct datapath *dp) int brc_sysfs_del_dp(struct datapath *dp) { - struct kobject *kobj = to_kobj(dp->ports[ODPP_LOCAL]->dev); + struct kobject *kobj = &dp->ports[ODPP_LOCAL]->dev->NETDEV_DEV_MEMBER.kobj; #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) kobject_unregister(&dp->ifobj); @@ -519,7 +507,7 @@ int brc_sysfs_del_dp(struct datapath *dp) return 0; } -#else /* !SUPPORT_SYSFS */ +#else /* !CONFIG_SYSFS */ int brc_sysfs_add_dp(struct datapath *dp) { return 0; } int brc_sysfs_del_dp(struct datapath *dp) { return 0; } int brc_sysfs_add_if(struct net_bridge_port *p) { return 0; } @@ -529,4 +517,4 @@ int brc_sysfs_del_if(struct net_bridge_port *p) kfree(p); return 0; } -#endif /* !SUPPORT_SYSFS */ +#endif /* !CONFIG_SYSFS */ diff --git a/datapath/brc_sysfs_if.c b/datapath/brc_sysfs_if.c index 20bb109b..cd24c936 100644 --- a/datapath/brc_sysfs_if.c +++ b/datapath/brc_sysfs_if.c @@ -13,7 +13,7 @@ #include "brc_sysfs.h" #include "datapath.h" -#ifdef SUPPORT_SYSFS +#ifdef CONFIG_SYSFS struct brport_attribute { struct attribute attr; @@ -281,18 +281,14 @@ int brc_sysfs_add_if(struct net_bridge_port *p) struct brport_attribute **a; int err; - kobject_init(&p->kobj); - kobject_set_name(&p->kobj, SYSFS_BRIDGE_PORT_ATTR); - p->kobj.ktype = &brport_ktype; - p->kobj.kset = NULL; - p->kobj.parent = &(p->dev->class_dev.kobj); - - err = kobject_add(&p->kobj); + err = kobject_init_and_add(&p->kobj, &brport_ktype, + &(p->dev->NETDEV_DEV_MEMBER.kobj), + SYSFS_BRIDGE_PORT_ATTR); if (err) - goto err_put; + goto err; err = sysfs_create_link(&p->kobj, - &dp->ports[ODPP_LOCAL]->dev->class_dev.kobj, + &dp->ports[ODPP_LOCAL]->dev->NETDEV_DEV_MEMBER.kobj, SYSFS_BRIDGE_PORT_LINK); if (err) goto err_del; @@ -303,7 +299,11 @@ int brc_sysfs_add_if(struct net_bridge_port *p) goto err_del; } +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) err = sysfs_create_link(&dp->ifobj, &p->kobj, p->dev->name); +#else + err = sysfs_create_link(dp->ifobj, &p->kobj, p->dev->name); +#endif if (err) goto err_del; @@ -313,8 +313,8 @@ int brc_sysfs_add_if(struct net_bridge_port *p) err_del: kobject_del(&p->kobj); -err_put: kobject_put(&p->kobj); +err: return err; } @@ -331,4 +331,4 @@ int brc_sysfs_del_if(struct net_bridge_port *p) return 0; } -#endif /* SUPPORT_SYSFS */ +#endif /* CONFIG_SYSFS */ diff --git a/datapath/brcompat.c b/datapath/brcompat.c index 2e437ccd..c4baac38 100644 --- a/datapath/brcompat.c +++ b/datapath/brcompat.c @@ -404,18 +404,14 @@ int brc_add_dp(struct datapath *dp) { if (!try_module_get(THIS_MODULE)) return -ENODEV; -#ifdef SUPPORT_SYSFS brc_sysfs_add_dp(dp); -#endif return 0; } int brc_del_dp(struct datapath *dp) { -#ifdef SUPPORT_SYSFS brc_sysfs_del_dp(dp); -#endif module_put(THIS_MODULE); return 0; @@ -450,10 +446,8 @@ __init brc_init(void) dp_del_dp_hook = brc_del_dp; /* Register hooks for interface adds and deletes */ -#ifdef SUPPORT_SYSFS dp_add_if_hook = brc_sysfs_add_if; dp_del_if_hook = brc_sysfs_del_if; -#endif /* Randomize the initial sequence number. This is not a security * feature; it only helps avoid crossed wires between userspace and diff --git a/datapath/datapath.c b/datapath/datapath.c index f519809d..3233361c 100644 --- a/datapath/datapath.c +++ b/datapath/datapath.c @@ -421,10 +421,13 @@ int dp_del_port(struct net_bridge_port *p, struct list_head *dp_devs) { ASSERT_RTNL(); -#ifdef SUPPORT_SYSFS - if (p->port_no != ODPP_LOCAL && dp_del_if_hook) + if (p->port_no != ODPP_LOCAL && dp_del_if_hook) { +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) sysfs_remove_link(&p->dp->ifobj, p->dev->name); +#else + sysfs_remove_link(p->dp->ifobj, p->dev->name); #endif + } dp_ifinfo_notify(RTM_DELLINK, p); p->dp->n_ports--; diff --git a/datapath/datapath.h b/datapath/datapath.h index 102b27f3..9332782d 100644 --- a/datapath/datapath.h +++ b/datapath/datapath.h @@ -10,6 +10,7 @@ #include #include #include +#include #include "flow.h" #include "brc_sysfs.h" @@ -58,8 +59,12 @@ struct datapath { struct mutex mutex; int dp_idx; -#ifdef SUPPORT_SYSFS +#ifdef CONFIG_SYSFS +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) struct kobject ifobj; +#else + struct kobject *ifobj; +#endif #endif int drop_frags; @@ -88,7 +93,7 @@ struct net_bridge_port { u16 port_no; struct datapath *dp; struct net_device *dev; -#ifdef SUPPORT_SYSFS +#ifdef CONFIG_SYSFS struct kobject kobj; #endif struct list_head node; /* Element in datapath.ports. */ diff --git a/datapath/linux-2.6/Modules.mk b/datapath/linux-2.6/Modules.mk index bbc4c72f..e7359a46 100644 --- a/datapath/linux-2.6/Modules.mk +++ b/datapath/linux-2.6/Modules.mk @@ -12,6 +12,7 @@ openvswitch_headers += \ linux-2.6/compat-2.6/include/linux/ipv6.h \ linux-2.6/compat-2.6/include/linux/jiffies.h \ linux-2.6/compat-2.6/include/linux/kernel.h \ + linux-2.6/compat-2.6/include/linux/kobject.h \ linux-2.6/compat-2.6/include/linux/log2.h \ linux-2.6/compat-2.6/include/linux/lockdep.h \ linux-2.6/compat-2.6/include/linux/mutex.h \ diff --git a/datapath/linux-2.6/compat-2.6/include/linux/kobject.h b/datapath/linux-2.6/compat-2.6/include/linux/kobject.h new file mode 100644 index 00000000..7dbb010c --- /dev/null +++ b/datapath/linux-2.6/compat-2.6/include/linux/kobject.h @@ -0,0 +1,23 @@ +#ifndef __LINUX_KOBJECT_WRAPPER_H +#define __LINUX_KOBJECT_WRAPPER_H 1 + +#include_next + +#include +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) +static inline int kobject_init_and_add(struct kobject *kobj, + struct kobj_type *ktype, + struct kobject *parent, + const char *name) +{ + kobject_init(kobj); + kobject_set_name(kobj, "%s", name); + kobj->ktype = ktype; + kobj->kset = NULL; + kobj->parent = parent; + + return kobject_add(kobj); +} +#endif + +#endif /* linux/kobject.h wrapper */ diff --git a/datapath/linux-2.6/compat-2.6/include/linux/netdevice.h b/datapath/linux-2.6/compat-2.6/include/linux/netdevice.h index 32e1735d..d2915133 100644 --- a/datapath/linux-2.6/compat-2.6/include/linux/netdevice.h +++ b/datapath/linux-2.6/compat-2.6/include/linux/netdevice.h @@ -5,8 +5,19 @@ struct net; +/* Before 2.6.21, struct net_device has a "struct class_device" member named + * class_dev. Beginning with 2.6.21, struct net_device instead has a "struct + * device" member named dev. Otherwise the usage of these members is pretty + * much the same. */ +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21) +#define NETDEV_DEV_MEMBER class_dev +#else +#define NETDEV_DEV_MEMBER dev +#endif + #ifndef to_net_dev -#define to_net_dev(class) container_of(class, struct net_device, class_dev) +#define to_net_dev(class) \ + container_of(class, struct net_device, NETDEV_DEV_MEMBER) #endif #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26) -- 2.30.2