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,
270 * Add sysfs entries to ethernet device added to a bridge.
271 * Creates a brport subdirectory with bridge attributes.
272 * Puts symlink in bridge's brport subdirectory
274 int dp_sysfs_add_if(struct net_bridge_port *p)
276 struct datapath *dp = p->dp;
277 struct brport_attribute **a;
280 /* Create /sys/class/net/<devname>/brport directory. */
281 err = kobject_add(&p->kobj);
285 /* Create symlink from /sys/class/net/<devname>/brport/bridge to
286 * /sys/class/net/<bridgename>. */
287 err = sysfs_create_link(&p->kobj,
288 &dp->ports[ODPP_LOCAL]->dev->NETDEV_DEV_MEMBER.kobj,
289 SYSFS_BRIDGE_PORT_LINK); /* "bridge" */
293 /* Populate /sys/class/net/<devname>/brport directory with files. */
294 for (a = brport_attrs; *a; ++a) {
295 err = sysfs_create_file(&p->kobj, &((*a)->attr));
300 /* Create symlink from /sys/class/net/<bridgename>/brif/<devname> to
301 * /sys/class/net/<devname>/brport. */
302 err = sysfs_create_link(&dp->ifobj, &p->kobj, p->dev->name);
305 strcpy(p->linkname, p->dev->name);
307 kobject_uevent(&p->kobj, KOBJ_ADD);
312 kobject_del(&p->kobj);
314 kobject_put(&p->kobj);
316 /* Ensure that dp_sysfs_del_if becomes a no-op. */
317 p->kobj.dentry = NULL;
321 int dp_sysfs_del_if(struct net_bridge_port *p)
323 if (p->linkname[0]) {
324 sysfs_remove_link(&p->dp->ifobj, p->linkname);
325 p->linkname[0] = '\0';
327 if (p->kobj.dentry) {
328 kobject_uevent(&p->kobj, KOBJ_REMOVE);
329 kobject_del(&p->kobj);
333 #endif /* SUPPORT_SYSFS */