1 #include <linux/version.h>
4 * Sysfs attributes of bridge for Open vSwitch
6 * This has been shamelessly copied from the kernel sources.
9 #include <linux/capability.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/netdevice.h>
13 #include <linux/if_bridge.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/spinlock.h>
16 #include <linux/times.h>
17 #include <linux/version.h>
19 #include "brc_sysfs.h"
24 #define to_dev(obj) container_of(obj, struct device, kobj)
26 /* Hack to attempt to build on more platforms. */
27 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)
28 #define BRC_DEVICE_ATTR CLASS_DEVICE_ATTR
29 #define DEVICE_PARAMS struct class_device *d
31 #define DEV_ATTR(NAME) class_device_attr_##NAME
33 #define BRC_DEVICE_ATTR DEVICE_ATTR
34 #define DEVICE_PARAMS struct device *d, struct device_attribute *attr
35 #define DEVICE_ARGS d, attr
36 #define DEV_ATTR(NAME) dev_attr_##NAME
40 * Common code for storing bridge parameters.
42 static ssize_t store_bridge_parm(DEVICE_PARAMS,
43 const char *buf, size_t len,
44 void (*set)(struct datapath *, unsigned long))
46 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
50 if (!capable(CAP_NET_ADMIN))
53 val = simple_strtoul(buf, &endp, 0);
58 spin_lock_bh(&br->lock);
60 spin_unlock_bh(&br->lock);
62 /* xxx We use a default value of 0 for all fields. If the caller is
63 * xxx attempting to set the value to our default, just silently
64 * xxx ignore the request.
67 printk("%s: xxx writing dp parms not supported yet!\n",
75 static ssize_t show_forward_delay(DEVICE_PARAMS, char *buf)
78 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
79 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
81 return sprintf(buf, "%d\n", 0);
85 static void set_forward_delay(struct datapath *dp, unsigned long val)
88 unsigned long delay = clock_t_to_jiffies(val);
89 br->forward_delay = delay;
90 if (br_is_root_bridge(br))
91 br->bridge_forward_delay = delay;
93 printk("%s: xxx attempt to set_forward_delay()\n", dp_name(dp));
97 static ssize_t store_forward_delay(DEVICE_PARAMS,
98 const char *buf, size_t len)
100 return store_bridge_parm(DEVICE_ARGS, buf, len, set_forward_delay);
102 static BRC_DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
103 show_forward_delay, store_forward_delay);
105 static ssize_t show_hello_time(DEVICE_PARAMS, char *buf)
108 return sprintf(buf, "%lu\n",
109 jiffies_to_clock_t(to_bridge(d)->hello_time));
111 return sprintf(buf, "%d\n", 0);
115 static void set_hello_time(struct datapath *dp, unsigned long val)
118 unsigned long t = clock_t_to_jiffies(val);
120 if (br_is_root_bridge(br))
121 br->bridge_hello_time = t;
123 printk("%s: xxx attempt to set_hello_time()\n", dp_name(dp));
127 static ssize_t store_hello_time(DEVICE_PARAMS,
131 return store_bridge_parm(DEVICE_ARGS, buf, len, set_hello_time);
133 static BRC_DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
136 static ssize_t show_max_age(DEVICE_PARAMS, char *buf)
139 return sprintf(buf, "%lu\n",
140 jiffies_to_clock_t(to_bridge(d)->max_age));
142 return sprintf(buf, "%d\n", 0);
146 static void set_max_age(struct datapath *dp, unsigned long val)
149 unsigned long t = clock_t_to_jiffies(val);
151 if (br_is_root_bridge(br))
152 br->bridge_max_age = t;
154 printk("%s: xxx attempt to set_max_age()\n", dp_name(dp));
158 static ssize_t store_max_age(DEVICE_PARAMS,
159 const char *buf, size_t len)
161 return store_bridge_parm(DEVICE_ARGS, buf, len, set_max_age);
163 static BRC_DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age, store_max_age);
165 static ssize_t show_ageing_time(DEVICE_PARAMS, char *buf)
168 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
169 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
171 return sprintf(buf, "%d\n", 0);
175 static void set_ageing_time(struct datapath *dp, unsigned long val)
178 br->ageing_time = clock_t_to_jiffies(val);
180 printk("%s: xxx attempt to set_ageing_time()\n", dp_name(dp));
184 static ssize_t store_ageing_time(DEVICE_PARAMS,
185 const char *buf, size_t len)
187 return store_bridge_parm(DEVICE_ARGS, buf, len, set_ageing_time);
189 static BRC_DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
192 static ssize_t show_stp_state(DEVICE_PARAMS, char *buf)
195 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
196 return sprintf(buf, "%d\n", br->stp_enabled);
198 return sprintf(buf, "%d\n", 0);
203 static ssize_t store_stp_state(DEVICE_PARAMS,
207 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
212 if (!capable(CAP_NET_ADMIN))
215 val = simple_strtoul(buf, &endp, 0);
220 br_stp_set_enabled(br, val);
223 printk("%s: xxx attempt to set_stp_state()\n", dp_name(dp));
228 static BRC_DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
231 static ssize_t show_priority(DEVICE_PARAMS, char *buf)
234 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
235 return sprintf(buf, "%d\n",
236 (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
238 return sprintf(buf, "%d\n", 0);
242 static void set_priority(struct datapath *dp, unsigned long val)
245 br_stp_set_bridge_priority(br, (u16) val);
247 printk("%s: xxx attempt to set_priority()\n", dp_name(dp));
251 static ssize_t store_priority(DEVICE_PARAMS,
252 const char *buf, size_t len)
254 return store_bridge_parm(DEVICE_ARGS, buf, len, set_priority);
256 static BRC_DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority);
258 static ssize_t show_root_id(DEVICE_PARAMS, char *buf)
261 return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
263 return sprintf(buf, "0000.010203040506\n");
266 static BRC_DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
268 static ssize_t show_bridge_id(DEVICE_PARAMS, char *buf)
270 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
271 const unsigned char *addr = dp->ports[ODPP_LOCAL]->dev->dev_addr;
273 /* xxx Do we need a lock of some sort? */
274 return sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
275 0, 0, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
277 static BRC_DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
279 static ssize_t show_root_port(DEVICE_PARAMS, char *buf)
282 return sprintf(buf, "%d\n", to_bridge(d)->root_port);
284 return sprintf(buf, "%d\n", 0);
287 static BRC_DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
289 static ssize_t show_root_path_cost(DEVICE_PARAMS, char *buf)
292 return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
294 return sprintf(buf, "%d\n", 0);
297 static BRC_DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
299 static ssize_t show_topology_change(DEVICE_PARAMS, char *buf)
302 return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
304 return sprintf(buf, "%d\n", 0);
307 static BRC_DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
309 static ssize_t show_topology_change_detected(DEVICE_PARAMS, char *buf)
312 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
313 return sprintf(buf, "%d\n", br->topology_change_detected);
315 return sprintf(buf, "%d\n", 0);
318 static BRC_DEVICE_ATTR(topology_change_detected, S_IRUGO,
319 show_topology_change_detected, NULL);
321 static ssize_t show_hello_timer(DEVICE_PARAMS, char *buf)
324 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
325 return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
327 return sprintf(buf, "%d\n", 0);
330 static BRC_DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
332 static ssize_t show_tcn_timer(DEVICE_PARAMS, char *buf)
335 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
336 return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
338 return sprintf(buf, "%d\n", 0);
341 static BRC_DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
343 static ssize_t show_topology_change_timer(DEVICE_PARAMS, char *buf)
346 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
347 return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
349 return sprintf(buf, "%d\n", 0);
352 static BRC_DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer,
355 static ssize_t show_gc_timer(DEVICE_PARAMS, char *buf)
358 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
359 return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
361 return sprintf(buf, "%d\n", 0);
364 static BRC_DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
366 static ssize_t show_group_addr(DEVICE_PARAMS, char *buf)
369 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
370 return sprintf(buf, "%x:%x:%x:%x:%x:%x\n",
371 br->group_addr[0], br->group_addr[1],
372 br->group_addr[2], br->group_addr[3],
373 br->group_addr[4], br->group_addr[5]);
375 return sprintf(buf, "00:01:02:03:04:05\n");
379 static ssize_t store_group_addr(DEVICE_PARAMS,
380 const char *buf, size_t len)
382 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
384 unsigned new_addr[6];
387 if (!capable(CAP_NET_ADMIN))
390 if (sscanf(buf, "%x:%x:%x:%x:%x:%x",
391 &new_addr[0], &new_addr[1], &new_addr[2],
392 &new_addr[3], &new_addr[4], &new_addr[5]) != 6)
395 /* Must be 01:80:c2:00:00:0X */
396 for (i = 0; i < 5; i++)
397 if (new_addr[i] != br_group_address[i])
400 if (new_addr[5] & ~0xf)
403 if (new_addr[5] == 1 /* 802.3x Pause address */
404 || new_addr[5] == 2 /* 802.3ad Slow protocols */
405 || new_addr[5] == 3) /* 802.1X PAE address */
408 spin_lock_bh(&br->lock);
409 for (i = 0; i < 6; i++)
410 br->group_addr[i] = new_addr[i];
411 spin_unlock_bh(&br->lock);
413 printk("%s: xxx attempt to store_group_addr()\n", dp_name(dp));
418 static BRC_DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
419 show_group_addr, store_group_addr);
421 static struct attribute *bridge_attrs[] = {
422 &DEV_ATTR(forward_delay).attr,
423 &DEV_ATTR(hello_time).attr,
424 &DEV_ATTR(max_age).attr,
425 &DEV_ATTR(ageing_time).attr,
426 &DEV_ATTR(stp_state).attr,
427 &DEV_ATTR(priority).attr,
428 &DEV_ATTR(bridge_id).attr,
429 &DEV_ATTR(root_id).attr,
430 &DEV_ATTR(root_path_cost).attr,
431 &DEV_ATTR(root_port).attr,
432 &DEV_ATTR(topology_change).attr,
433 &DEV_ATTR(topology_change_detected).attr,
434 &DEV_ATTR(hello_timer).attr,
435 &DEV_ATTR(tcn_timer).attr,
436 &DEV_ATTR(topology_change_timer).attr,
437 &DEV_ATTR(gc_timer).attr,
438 &DEV_ATTR(group_addr).attr,
442 static struct attribute_group bridge_group = {
443 .name = SYSFS_BRIDGE_ATTR,
444 .attrs = bridge_attrs,
448 * Add entries in sysfs onto the existing network class device
450 * Adds a attribute group "bridge" containing tuning parameters.
451 * Sub directory to hold links to interfaces.
453 * Note: the ifobj exists only to be a subdirectory
454 * to hold links. The ifobj exists in the same data structure
455 * as its parent the bridge so reference counting works.
457 int brc_sysfs_add_dp(struct datapath *dp)
459 struct kobject *kobj = &dp->ports[ODPP_LOCAL]->dev->NETDEV_DEV_MEMBER.kobj;
462 err = sysfs_create_group(kobj, &bridge_group);
464 pr_info("%s: can't create group %s/%s\n",
465 __func__, dp_name(dp), bridge_group.name);
469 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
470 kobject_set_name(&dp->ifobj, SYSFS_BRIDGE_PORT_SUBDIR);
471 dp->ifobj.ktype = NULL;
472 dp->ifobj.kset = NULL;
473 dp->ifobj.parent = kobj;
475 err = kobject_register(&dp->ifobj);
477 pr_info("%s: can't add kobject (directory) %s/%s\n",
478 __FUNCTION__, dp_name(dp), kobject_name(&dp->ifobj));
482 dp->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, kobj);
484 pr_info("%s: can't add kobject (directory) %s/%s\n",
485 __func__, dp_name(dp), SYSFS_BRIDGE_PORT_SUBDIR);
492 sysfs_remove_group(kobj, &bridge_group);
497 int brc_sysfs_del_dp(struct datapath *dp)
499 struct kobject *kobj = &dp->ports[ODPP_LOCAL]->dev->NETDEV_DEV_MEMBER.kobj;
501 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
502 kobject_unregister(&dp->ifobj);
504 kobject_put(dp->ifobj);
506 sysfs_remove_group(kobj, &bridge_group);
510 #else /* !CONFIG_SYSFS */
511 int brc_sysfs_add_dp(struct datapath *dp) { return 0; }
512 int brc_sysfs_del_dp(struct datapath *dp) { return 0; }
513 int brc_sysfs_add_if(struct net_bridge_port *p) { return 0; }
514 int brc_sysfs_del_if(struct net_bridge_port *p)
520 #endif /* !CONFIG_SYSFS */