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.
10 * Sysfs attributes of bridge ports for Open vSwitch
12 * This has been shamelessly copied from the kernel sources.
15 #include <linux/capability.h>
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/if_bridge.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/spinlock.h>
26 struct brport_attribute {
27 struct attribute attr;
28 ssize_t (*show)(struct net_bridge_port *, char *);
29 ssize_t (*store)(struct net_bridge_port *, unsigned long);
32 #define BRPORT_ATTR(_name,_mode,_show,_store) \
33 struct brport_attribute brport_attr_##_name = { \
34 .attr = {.name = __stringify(_name), \
36 .owner = THIS_MODULE, }, \
41 static ssize_t show_path_cost(struct net_bridge_port *p, char *buf)
44 return sprintf(buf, "%d\n", p->path_cost);
46 return sprintf(buf, "%d\n", 0);
49 static ssize_t store_path_cost(struct net_bridge_port *p, unsigned long v)
52 br_stp_set_path_cost(p, v);
56 static BRPORT_ATTR(path_cost, S_IRUGO | S_IWUSR,
57 show_path_cost, store_path_cost);
59 static ssize_t show_priority(struct net_bridge_port *p, char *buf)
62 return sprintf(buf, "%d\n", p->priority);
64 return sprintf(buf, "%d\n", 0);
67 static ssize_t store_priority(struct net_bridge_port *p, unsigned long v)
70 if (v >= (1<<(16-BR_PORT_BITS)))
72 br_stp_set_port_priority(p, v);
76 static BRPORT_ATTR(priority, S_IRUGO | S_IWUSR,
77 show_priority, store_priority);
79 static ssize_t show_designated_root(struct net_bridge_port *p, char *buf)
82 return br_show_bridge_id(buf, &p->designated_root);
84 return sprintf(buf, "0000.010203040506\n");
87 static BRPORT_ATTR(designated_root, S_IRUGO, show_designated_root, NULL);
89 static ssize_t show_designated_bridge(struct net_bridge_port *p, char *buf)
92 return br_show_bridge_id(buf, &p->designated_bridge);
94 return sprintf(buf, "0000.060504030201\n");
97 static BRPORT_ATTR(designated_bridge, S_IRUGO, show_designated_bridge, NULL);
99 static ssize_t show_designated_port(struct net_bridge_port *p, char *buf)
102 return sprintf(buf, "%d\n", p->designated_port);
104 return sprintf(buf, "%d\n", 0);
107 static BRPORT_ATTR(designated_port, S_IRUGO, show_designated_port, NULL);
109 static ssize_t show_designated_cost(struct net_bridge_port *p, char *buf)
112 return sprintf(buf, "%d\n", p->designated_cost);
114 return sprintf(buf, "%d\n", 0);
117 static BRPORT_ATTR(designated_cost, S_IRUGO, show_designated_cost, NULL);
119 static ssize_t show_port_id(struct net_bridge_port *p, char *buf)
122 return sprintf(buf, "0x%x\n", p->port_id);
124 return sprintf(buf, "0x%x\n", 0);
127 static BRPORT_ATTR(port_id, S_IRUGO, show_port_id, NULL);
129 static ssize_t show_port_no(struct net_bridge_port *p, char *buf)
131 return sprintf(buf, "0x%x\n", p->port_no);
134 static BRPORT_ATTR(port_no, S_IRUGO, show_port_no, NULL);
136 static ssize_t show_change_ack(struct net_bridge_port *p, char *buf)
139 return sprintf(buf, "%d\n", p->topology_change_ack);
141 return sprintf(buf, "%d\n", 0);
144 static BRPORT_ATTR(change_ack, S_IRUGO, show_change_ack, NULL);
146 static ssize_t show_config_pending(struct net_bridge_port *p, char *buf)
149 return sprintf(buf, "%d\n", p->config_pending);
151 return sprintf(buf, "%d\n", 0);
154 static BRPORT_ATTR(config_pending, S_IRUGO, show_config_pending, NULL);
156 static ssize_t show_port_state(struct net_bridge_port *p, char *buf)
159 return sprintf(buf, "%d\n", p->state);
161 return sprintf(buf, "%d\n", 0);
164 static BRPORT_ATTR(state, S_IRUGO, show_port_state, NULL);
166 static ssize_t show_message_age_timer(struct net_bridge_port *p,
170 return sprintf(buf, "%ld\n", br_timer_value(&p->message_age_timer));
172 return sprintf(buf, "%d\n", 0);
175 static BRPORT_ATTR(message_age_timer, S_IRUGO, show_message_age_timer, NULL);
177 static ssize_t show_forward_delay_timer(struct net_bridge_port *p,
181 return sprintf(buf, "%ld\n", br_timer_value(&p->forward_delay_timer));
183 return sprintf(buf, "%d\n", 0);
186 static BRPORT_ATTR(forward_delay_timer, S_IRUGO, show_forward_delay_timer, NULL);
188 static ssize_t show_hold_timer(struct net_bridge_port *p,
192 return sprintf(buf, "%ld\n", br_timer_value(&p->hold_timer));
194 return sprintf(buf, "%d\n", 0);
197 static BRPORT_ATTR(hold_timer, S_IRUGO, show_hold_timer, NULL);
199 static struct brport_attribute *brport_attrs[] = {
200 &brport_attr_path_cost,
201 &brport_attr_priority,
202 &brport_attr_port_id,
203 &brport_attr_port_no,
204 &brport_attr_designated_root,
205 &brport_attr_designated_bridge,
206 &brport_attr_designated_port,
207 &brport_attr_designated_cost,
209 &brport_attr_change_ack,
210 &brport_attr_config_pending,
211 &brport_attr_message_age_timer,
212 &brport_attr_forward_delay_timer,
213 &brport_attr_hold_timer,
217 #define to_brport_attr(_at) container_of(_at, struct brport_attribute, attr)
218 #define to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
220 static ssize_t brport_show(struct kobject * kobj,
221 struct attribute * attr, char * buf)
223 struct brport_attribute * brport_attr = to_brport_attr(attr);
224 struct net_bridge_port * p = to_brport(kobj);
226 return brport_attr->show(p, buf);
229 static ssize_t brport_store(struct kobject * kobj,
230 struct attribute * attr,
231 const char * buf, size_t count)
233 struct net_bridge_port * p = to_brport(kobj);
235 struct brport_attribute * brport_attr = to_brport_attr(attr);
239 ssize_t ret = -EINVAL;
241 if (!capable(CAP_NET_ADMIN))
245 val = simple_strtoul(buf, &endp, 0);
248 if (p->dev && p->br && brport_attr->store) {
249 spin_lock_bh(&p->br->lock);
250 ret = brport_attr->store(p, val);
251 spin_unlock_bh(&p->br->lock);
258 printk("%s: xxx writing port parms not supported yet!\n",
264 struct sysfs_ops brport_sysfs_ops = {
266 .store = brport_store,
269 static void release_nbp(struct kobject *kobj)
271 struct net_bridge_port *p
272 = container_of(kobj, struct net_bridge_port, kobj);
276 struct kobj_type brport_ktype = {
277 .sysfs_ops = &brport_sysfs_ops,
278 .release = release_nbp
282 * Add sysfs entries to ethernet device added to a bridge.
283 * Creates a brport subdirectory with bridge attributes.
284 * Puts symlink in bridge's brport subdirectory
286 int dp_sysfs_add_if(struct net_bridge_port *p)
288 struct datapath *dp = p->dp;
289 struct brport_attribute **a;
292 /* Create /sys/class/net/<devname>/brport directory. */
293 kobject_init(&p->kobj);
294 kobject_set_name(&p->kobj, SYSFS_BRIDGE_PORT_ATTR); /* "brport" */
295 p->kobj.ktype = &brport_ktype;
297 p->kobj.parent = &(p->dev->class_dev.kobj);
299 err = kobject_add(&p->kobj);
303 /* Create symlink from /sys/class/net/<devname>/brport/bridge to
304 * /sys/class/net/<bridgename>. */
305 err = sysfs_create_link(&p->kobj,
306 &dp->ports[ODPP_LOCAL]->dev->class_dev.kobj,
307 SYSFS_BRIDGE_PORT_LINK); /* "bridge" */
311 /* Populate /sys/class/net/<devname>/brport directory with files. */
312 for (a = brport_attrs; *a; ++a) {
313 err = sysfs_create_file(&p->kobj, &((*a)->attr));
318 /* Create symlink from /sys/class/net/<bridgename>/brif/<devname> to
319 * /sys/class/net/<devname>/brport. */
320 err = sysfs_create_link(&dp->ifobj, &p->kobj, p->dev->name);
324 kobject_uevent(&p->kobj, KOBJ_ADD);
329 kobject_del(&p->kobj);
331 kobject_put(&p->kobj);
335 int dp_sysfs_del_if(struct net_bridge_port *p)
337 struct net_device *dev = p->dev;
339 kobject_uevent(&p->kobj, KOBJ_REMOVE);
340 kobject_del(&p->kobj);
344 kobject_put(&p->kobj);
348 #endif /* SUPPORT_SYSFS */