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 to_kobj(d) &(d)->class_dev.kobj
29 #define BRC_DEVICE_ATTR CLASS_DEVICE_ATTR
31 #define to_kobj(d) &(d)->dev.kobj
32 #define BRC_DEVICE_ATTR DEVICE_ATTR
36 * Common code for storing bridge parameters.
38 static ssize_t store_bridge_parm(struct class_device *d,
39 const char *buf, size_t len,
40 void (*set)(struct datapath *, unsigned long))
42 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
46 if (!capable(CAP_NET_ADMIN))
49 val = simple_strtoul(buf, &endp, 0);
54 spin_lock_bh(&br->lock);
56 spin_unlock_bh(&br->lock);
58 /* xxx We use a default value of 0 for all fields. If the caller is
59 * xxx attempting to set the value to our default, just silently
60 * xxx ignore the request.
63 printk("%s: xxx writing dp parms not supported yet!\n",
71 static ssize_t show_forward_delay(struct class_device *d,
75 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
76 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
78 return sprintf(buf, "%d\n", 0);
82 static void set_forward_delay(struct datapath *dp, unsigned long val)
85 unsigned long delay = clock_t_to_jiffies(val);
86 br->forward_delay = delay;
87 if (br_is_root_bridge(br))
88 br->bridge_forward_delay = delay;
90 printk("%s: xxx attempt to set_forward_delay()\n", dp_name(dp));
94 static ssize_t store_forward_delay(struct class_device *d,
95 const char *buf, size_t len)
97 return store_bridge_parm(d, buf, len, set_forward_delay);
99 static BRC_DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
100 show_forward_delay, store_forward_delay);
102 static ssize_t show_hello_time(struct class_device *d, char *buf)
105 return sprintf(buf, "%lu\n",
106 jiffies_to_clock_t(to_bridge(d)->hello_time));
108 return sprintf(buf, "%d\n", 0);
112 static void set_hello_time(struct datapath *dp, unsigned long val)
115 unsigned long t = clock_t_to_jiffies(val);
117 if (br_is_root_bridge(br))
118 br->bridge_hello_time = t;
120 printk("%s: xxx attempt to set_hello_time()\n", dp_name(dp));
124 static ssize_t store_hello_time(struct class_device *d,
128 return store_bridge_parm(d, buf, len, set_hello_time);
130 static BRC_DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
133 static ssize_t show_max_age(struct class_device *d,
137 return sprintf(buf, "%lu\n",
138 jiffies_to_clock_t(to_bridge(d)->max_age));
140 return sprintf(buf, "%d\n", 0);
144 static void set_max_age(struct datapath *dp, unsigned long val)
147 unsigned long t = clock_t_to_jiffies(val);
149 if (br_is_root_bridge(br))
150 br->bridge_max_age = t;
152 printk("%s: xxx attempt to set_max_age()\n", dp_name(dp));
156 static ssize_t store_max_age(struct class_device *d,
157 const char *buf, size_t len)
159 return store_bridge_parm(d, buf, len, set_max_age);
161 static BRC_DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age, store_max_age);
163 static ssize_t show_ageing_time(struct class_device *d,
167 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
168 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
170 return sprintf(buf, "%d\n", 0);
174 static void set_ageing_time(struct datapath *dp, unsigned long val)
177 br->ageing_time = clock_t_to_jiffies(val);
179 printk("%s: xxx attempt to set_ageing_time()\n", dp_name(dp));
183 static ssize_t store_ageing_time(struct class_device *d,
184 const char *buf, size_t len)
186 return store_bridge_parm(d, buf, len, set_ageing_time);
188 static BRC_DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
191 static ssize_t show_stp_state(struct class_device *d,
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(struct class_device *d,
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(struct class_device *d,
235 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
236 return sprintf(buf, "%d\n",
237 (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
239 return sprintf(buf, "%d\n", 0);
243 static void set_priority(struct datapath *dp, unsigned long val)
246 br_stp_set_bridge_priority(br, (u16) val);
248 printk("%s: xxx attempt to set_priority()\n", dp_name(dp));
252 static ssize_t store_priority(struct class_device *d,
253 const char *buf, size_t len)
255 return store_bridge_parm(d, buf, len, set_priority);
257 static BRC_DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority);
259 static ssize_t show_root_id(struct class_device *d,
263 return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
265 return sprintf(buf, "0000.010203040506\n");
268 static BRC_DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
270 static ssize_t show_bridge_id(struct class_device *d,
273 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
274 const unsigned char *addr = dp->ports[ODPP_LOCAL]->dev->dev_addr;
276 /* xxx Do we need a lock of some sort? */
277 return sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
278 0, 0, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
280 static BRC_DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
282 static ssize_t show_root_port(struct class_device *d,
286 return sprintf(buf, "%d\n", to_bridge(d)->root_port);
288 return sprintf(buf, "%d\n", 0);
291 static BRC_DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
293 static ssize_t show_root_path_cost(struct class_device *d,
297 return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
299 return sprintf(buf, "%d\n", 0);
302 static BRC_DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
304 static ssize_t show_topology_change(struct class_device *d,
308 return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
310 return sprintf(buf, "%d\n", 0);
313 static BRC_DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
315 static ssize_t show_topology_change_detected(struct class_device *d,
319 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
320 return sprintf(buf, "%d\n", br->topology_change_detected);
322 return sprintf(buf, "%d\n", 0);
325 static BRC_DEVICE_ATTR(topology_change_detected, S_IRUGO,
326 show_topology_change_detected, NULL);
328 static ssize_t show_hello_timer(struct class_device *d,
332 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
333 return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
335 return sprintf(buf, "%d\n", 0);
338 static BRC_DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
340 static ssize_t show_tcn_timer(struct class_device *d,
344 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
345 return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
347 return sprintf(buf, "%d\n", 0);
350 static BRC_DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
352 static ssize_t show_topology_change_timer(struct class_device *d,
356 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
357 return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
359 return sprintf(buf, "%d\n", 0);
362 static BRC_DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer,
365 static ssize_t show_gc_timer(struct class_device *d,
369 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
370 return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
372 return sprintf(buf, "%d\n", 0);
375 static BRC_DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
377 static ssize_t show_group_addr(struct class_device *d,
381 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
382 return sprintf(buf, "%x:%x:%x:%x:%x:%x\n",
383 br->group_addr[0], br->group_addr[1],
384 br->group_addr[2], br->group_addr[3],
385 br->group_addr[4], br->group_addr[5]);
387 return sprintf(buf, "00:01:02:03:04:05\n");
391 static ssize_t store_group_addr(struct class_device *d,
392 const char *buf, size_t len)
394 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
396 unsigned new_addr[6];
399 if (!capable(CAP_NET_ADMIN))
402 if (sscanf(buf, "%x:%x:%x:%x:%x:%x",
403 &new_addr[0], &new_addr[1], &new_addr[2],
404 &new_addr[3], &new_addr[4], &new_addr[5]) != 6)
407 /* Must be 01:80:c2:00:00:0X */
408 for (i = 0; i < 5; i++)
409 if (new_addr[i] != br_group_address[i])
412 if (new_addr[5] & ~0xf)
415 if (new_addr[5] == 1 /* 802.3x Pause address */
416 || new_addr[5] == 2 /* 802.3ad Slow protocols */
417 || new_addr[5] == 3) /* 802.1X PAE address */
420 spin_lock_bh(&br->lock);
421 for (i = 0; i < 6; i++)
422 br->group_addr[i] = new_addr[i];
423 spin_unlock_bh(&br->lock);
425 printk("%s: xxx attempt to store_group_addr()\n", dp_name(dp));
430 static BRC_DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
431 show_group_addr, store_group_addr);
433 static struct attribute *bridge_attrs[] = {
434 &class_device_attr_forward_delay.attr,
435 &class_device_attr_hello_time.attr,
436 &class_device_attr_max_age.attr,
437 &class_device_attr_ageing_time.attr,
438 &class_device_attr_stp_state.attr,
439 &class_device_attr_priority.attr,
440 &class_device_attr_bridge_id.attr,
441 &class_device_attr_root_id.attr,
442 &class_device_attr_root_path_cost.attr,
443 &class_device_attr_root_port.attr,
444 &class_device_attr_topology_change.attr,
445 &class_device_attr_topology_change_detected.attr,
446 &class_device_attr_hello_timer.attr,
447 &class_device_attr_tcn_timer.attr,
448 &class_device_attr_topology_change_timer.attr,
449 &class_device_attr_gc_timer.attr,
450 &class_device_attr_group_addr.attr,
454 static struct attribute_group bridge_group = {
455 .name = SYSFS_BRIDGE_ATTR,
456 .attrs = bridge_attrs,
460 * Add entries in sysfs onto the existing network class device
462 * Adds a attribute group "bridge" containing tuning parameters.
463 * Sub directory to hold links to interfaces.
465 * Note: the ifobj exists only to be a subdirectory
466 * to hold links. The ifobj exists in the same data structure
467 * as its parent the bridge so reference counting works.
469 int brc_sysfs_add_dp(struct datapath *dp)
471 struct kobject *kobj = to_kobj(dp->ports[ODPP_LOCAL]->dev);
474 err = sysfs_create_group(kobj, &bridge_group);
476 pr_info("%s: can't create group %s/%s\n",
477 __func__, dp_name(dp), bridge_group.name);
481 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
482 kobject_set_name(&dp->ifobj, SYSFS_BRIDGE_PORT_SUBDIR);
483 dp->ifobj.ktype = NULL;
484 dp->ifobj.kset = NULL;
485 dp->ifobj.parent = kobj;
487 err = kobject_register(&dp->ifobj);
489 pr_info("%s: can't add kobject (directory) %s/%s\n",
490 __FUNCTION__, dp_name(dp), dp->ifobj.name);
494 br->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, kobj);
496 pr_info("%s: can't add kobject (directory) %s/%s\n",
497 __func__, dp_name(dp), SYSFS_BRIDGE_PORT_SUBDIR);
504 sysfs_remove_group(kobj, &bridge_group);
509 int brc_sysfs_del_dp(struct datapath *dp)
511 struct kobject *kobj = to_kobj(dp->ports[ODPP_LOCAL]->dev);
513 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
514 kobject_unregister(&dp->ifobj);
516 kobject_put(dp->ifobj);
518 sysfs_remove_group(kobj, &bridge_group);
522 #else /* !SUPPORT_SYSFS */
523 int brc_sysfs_add_dp(struct datapath *dp) { return 0; }
524 int brc_sysfs_del_dp(struct datapath *dp) { return 0; }
525 int brc_sysfs_add_if(struct net_bridge_port *p) { return 0; }
526 int brc_sysfs_del_if(struct net_bridge_port *p)
532 #endif /* !SUPPORT_SYSFS */