2 * Copyright (c) 2009 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
9 #include <linux/version.h>
12 * Sysfs attributes of bridge for Open vSwitch
14 * This has been shamelessly copied from the kernel sources.
17 #include <linux/capability.h>
18 #include <linux/device.h>
19 #include <linux/kernel.h>
20 #include <linux/netdevice.h>
21 #include <linux/if_bridge.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/spinlock.h>
24 #include <linux/times.h>
25 #include <linux/version.h>
32 #define to_dev(obj) container_of(obj, struct device, kobj)
34 /* Hack to attempt to build on more platforms. */
35 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)
36 #define to_kobj(d) &(d)->class_dev.kobj
37 #define DP_DEVICE_ATTR CLASS_DEVICE_ATTR
39 #define to_kobj(d) &(d)->dev.kobj
40 #define DP_DEVICE_ATTR DEVICE_ATTR
44 * Common code for storing bridge parameters.
46 static ssize_t store_bridge_parm(struct class_device *d,
47 const char *buf, size_t len,
48 void (*set)(struct datapath *, unsigned long))
50 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
54 if (!capable(CAP_NET_ADMIN))
57 val = simple_strtoul(buf, &endp, 0);
62 spin_lock_bh(&br->lock);
64 spin_unlock_bh(&br->lock);
66 /* xxx We use a default value of 0 for all fields. If the caller is
67 * xxx attempting to set the value to our default, just silently
68 * xxx ignore the request.
71 printk("%s: xxx writing dp parms not supported yet!\n",
79 static ssize_t show_forward_delay(struct class_device *d,
83 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
84 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
86 return sprintf(buf, "%d\n", 0);
90 static void set_forward_delay(struct datapath *dp, unsigned long val)
93 unsigned long delay = clock_t_to_jiffies(val);
94 br->forward_delay = delay;
95 if (br_is_root_bridge(br))
96 br->bridge_forward_delay = delay;
98 printk("%s: xxx attempt to set_forward_delay()\n", dp_name(dp));
102 static ssize_t store_forward_delay(struct class_device *d,
103 const char *buf, size_t len)
105 return store_bridge_parm(d, buf, len, set_forward_delay);
107 static DP_DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
108 show_forward_delay, store_forward_delay);
110 static ssize_t show_hello_time(struct class_device *d, char *buf)
113 return sprintf(buf, "%lu\n",
114 jiffies_to_clock_t(to_bridge(d)->hello_time));
116 return sprintf(buf, "%d\n", 0);
120 static void set_hello_time(struct datapath *dp, unsigned long val)
123 unsigned long t = clock_t_to_jiffies(val);
125 if (br_is_root_bridge(br))
126 br->bridge_hello_time = t;
128 printk("%s: xxx attempt to set_hello_time()\n", dp_name(dp));
132 static ssize_t store_hello_time(struct class_device *d,
136 return store_bridge_parm(d, buf, len, set_hello_time);
138 static DP_DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
141 static ssize_t show_max_age(struct class_device *d,
145 return sprintf(buf, "%lu\n",
146 jiffies_to_clock_t(to_bridge(d)->max_age));
148 return sprintf(buf, "%d\n", 0);
152 static void set_max_age(struct datapath *dp, unsigned long val)
155 unsigned long t = clock_t_to_jiffies(val);
157 if (br_is_root_bridge(br))
158 br->bridge_max_age = t;
160 printk("%s: xxx attempt to set_max_age()\n", dp_name(dp));
164 static ssize_t store_max_age(struct class_device *d,
165 const char *buf, size_t len)
167 return store_bridge_parm(d, buf, len, set_max_age);
169 static DP_DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age, store_max_age);
171 static ssize_t show_ageing_time(struct class_device *d,
175 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
176 return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
178 return sprintf(buf, "%d\n", 0);
182 static void set_ageing_time(struct datapath *dp, unsigned long val)
185 br->ageing_time = clock_t_to_jiffies(val);
187 printk("%s: xxx attempt to set_ageing_time()\n", dp_name(dp));
191 static ssize_t store_ageing_time(struct class_device *d,
192 const char *buf, size_t len)
194 return store_bridge_parm(d, buf, len, set_ageing_time);
196 static DP_DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
199 static ssize_t show_stp_state(struct class_device *d,
203 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
204 return sprintf(buf, "%d\n", br->stp_enabled);
206 return sprintf(buf, "%d\n", 0);
211 static ssize_t store_stp_state(struct class_device *d,
215 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
220 if (!capable(CAP_NET_ADMIN))
223 val = simple_strtoul(buf, &endp, 0);
228 br_stp_set_enabled(br, val);
231 printk("%s: xxx attempt to set_stp_state()\n", dp_name(dp));
236 static DP_DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
239 static ssize_t show_priority(struct class_device *d,
243 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
244 return sprintf(buf, "%d\n",
245 (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
247 return sprintf(buf, "%d\n", 0);
251 static void set_priority(struct datapath *dp, unsigned long val)
254 br_stp_set_bridge_priority(br, (u16) val);
256 printk("%s: xxx attempt to set_priority()\n", dp_name(dp));
260 static ssize_t store_priority(struct class_device *d,
261 const char *buf, size_t len)
263 return store_bridge_parm(d, buf, len, set_priority);
265 static DP_DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority);
267 static ssize_t show_root_id(struct class_device *d,
271 return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
273 return sprintf(buf, "0000.010203040506\n");
276 static DP_DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
278 static ssize_t show_bridge_id(struct class_device *d,
281 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
282 const unsigned char *addr = dp->ports[ODPP_LOCAL]->dev->dev_addr;
284 /* xxx Do we need a lock of some sort? */
285 return sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
286 0, 0, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
288 static DP_DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
290 static ssize_t show_root_port(struct class_device *d,
294 return sprintf(buf, "%d\n", to_bridge(d)->root_port);
296 return sprintf(buf, "%d\n", 0);
299 static DP_DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
301 static ssize_t show_root_path_cost(struct class_device *d,
305 return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
307 return sprintf(buf, "%d\n", 0);
310 static DP_DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
312 static ssize_t show_topology_change(struct class_device *d,
316 return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
318 return sprintf(buf, "%d\n", 0);
321 static DP_DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
323 static ssize_t show_topology_change_detected(struct class_device *d,
327 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
328 return sprintf(buf, "%d\n", br->topology_change_detected);
330 return sprintf(buf, "%d\n", 0);
333 static DP_DEVICE_ATTR(topology_change_detected, S_IRUGO,
334 show_topology_change_detected, NULL);
336 static ssize_t show_hello_timer(struct class_device *d,
340 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
341 return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
343 return sprintf(buf, "%d\n", 0);
346 static DP_DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
348 static ssize_t show_tcn_timer(struct class_device *d,
352 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
353 return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
355 return sprintf(buf, "%d\n", 0);
358 static DP_DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
360 static ssize_t show_topology_change_timer(struct class_device *d,
364 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
365 return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
367 return sprintf(buf, "%d\n", 0);
370 static DP_DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer,
373 static ssize_t show_gc_timer(struct class_device *d,
377 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
378 return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
380 return sprintf(buf, "%d\n", 0);
383 static DP_DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
385 static ssize_t show_group_addr(struct class_device *d,
389 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
390 return sprintf(buf, "%x:%x:%x:%x:%x:%x\n",
391 br->group_addr[0], br->group_addr[1],
392 br->group_addr[2], br->group_addr[3],
393 br->group_addr[4], br->group_addr[5]);
395 return sprintf(buf, "00:01:02:03:04:05\n");
399 static ssize_t store_group_addr(struct class_device *d,
400 const char *buf, size_t len)
402 struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
404 unsigned new_addr[6];
407 if (!capable(CAP_NET_ADMIN))
410 if (sscanf(buf, "%x:%x:%x:%x:%x:%x",
411 &new_addr[0], &new_addr[1], &new_addr[2],
412 &new_addr[3], &new_addr[4], &new_addr[5]) != 6)
415 /* Must be 01:80:c2:00:00:0X */
416 for (i = 0; i < 5; i++)
417 if (new_addr[i] != br_group_address[i])
420 if (new_addr[5] & ~0xf)
423 if (new_addr[5] == 1 /* 802.3x Pause address */
424 || new_addr[5] == 2 /* 802.3ad Slow protocols */
425 || new_addr[5] == 3) /* 802.1X PAE address */
428 spin_lock_bh(&br->lock);
429 for (i = 0; i < 6; i++)
430 br->group_addr[i] = new_addr[i];
431 spin_unlock_bh(&br->lock);
433 printk("%s: xxx attempt to store_group_addr()\n", dp_name(dp));
438 static DP_DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
439 show_group_addr, store_group_addr);
441 static struct attribute *bridge_attrs[] = {
442 &class_device_attr_forward_delay.attr,
443 &class_device_attr_hello_time.attr,
444 &class_device_attr_max_age.attr,
445 &class_device_attr_ageing_time.attr,
446 &class_device_attr_stp_state.attr,
447 &class_device_attr_priority.attr,
448 &class_device_attr_bridge_id.attr,
449 &class_device_attr_root_id.attr,
450 &class_device_attr_root_path_cost.attr,
451 &class_device_attr_root_port.attr,
452 &class_device_attr_topology_change.attr,
453 &class_device_attr_topology_change_detected.attr,
454 &class_device_attr_hello_timer.attr,
455 &class_device_attr_tcn_timer.attr,
456 &class_device_attr_topology_change_timer.attr,
457 &class_device_attr_gc_timer.attr,
458 &class_device_attr_group_addr.attr,
462 static struct attribute_group bridge_group = {
463 .name = SYSFS_BRIDGE_ATTR, /* "bridge" */
464 .attrs = bridge_attrs,
468 * Add entries in sysfs onto the existing network class device
470 * Adds a attribute group "bridge" containing tuning parameters.
471 * Sub directory to hold links to interfaces.
473 * Note: the ifobj exists only to be a subdirectory
474 * to hold links. The ifobj exists in the same data structure
475 * as its parent the bridge so reference counting works.
477 int dp_sysfs_add_dp(struct datapath *dp)
479 struct kobject *kobj = to_kobj(dp->ports[ODPP_LOCAL]->dev);
482 /* Create /sys/class/net/<devname>/bridge directory. */
483 err = sysfs_create_group(kobj, &bridge_group);
485 pr_info("%s: can't create group %s/%s\n",
486 __func__, dp_name(dp), bridge_group.name);
490 /* Create /sys/class/net/<devname>/brif directory. */
491 dp->ifobj.parent = kobj;
492 err = kobject_add(&dp->ifobj);
494 pr_info("%s: can't add kobject (directory) %s/%s\n",
495 __FUNCTION__, dp_name(dp), dp->ifobj.name);
498 kobject_uevent(&dp->ifobj, KOBJ_ADD);
502 sysfs_remove_group(kobj, &bridge_group);
507 int dp_sysfs_del_dp(struct datapath *dp)
509 struct kobject *kobj = to_kobj(dp->ports[ODPP_LOCAL]->dev);
511 kobject_del(&dp->ifobj);
512 sysfs_remove_group(kobj, &bridge_group);
516 #else /* !SUPPORT_SYSFS */
517 int dp_sysfs_add_dp(struct datapath *dp) { return 0; }
518 int dp_sysfs_del_dp(struct datapath *dp) { return 0; }
519 int dp_sysfs_add_if(struct net_bridge_port *p) { return 0; }
520 int dp_sysfs_del_if(struct net_bridge_port *p)
526 #endif /* !SUPPORT_SYSFS */