2 * Copyright (c) 2009, 2010 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>
28 struct brport_attribute {
29 struct attribute attr;
30 ssize_t (*show)(struct dp_port *, char *);
31 ssize_t (*store)(struct dp_port *, unsigned long);
34 #define BRPORT_ATTR(_name,_mode,_show,_store) \
35 struct brport_attribute brport_attr_##_name = { \
36 .attr = {.name = __stringify(_name), \
38 .owner = THIS_MODULE, }, \
43 static ssize_t show_path_cost(struct dp_port *p, char *buf)
46 return sprintf(buf, "%d\n", p->path_cost);
48 return sprintf(buf, "%d\n", 0);
51 static ssize_t store_path_cost(struct dp_port *p, unsigned long v)
54 br_stp_set_path_cost(p, v);
58 static BRPORT_ATTR(path_cost, S_IRUGO | S_IWUSR,
59 show_path_cost, store_path_cost);
61 static ssize_t show_priority(struct dp_port *p, char *buf)
64 return sprintf(buf, "%d\n", p->priority);
66 return sprintf(buf, "%d\n", 0);
69 static ssize_t store_priority(struct dp_port *p, unsigned long v)
72 if (v >= (1<<(16-BR_PORT_BITS)))
74 br_stp_set_port_priority(p, v);
78 static BRPORT_ATTR(priority, S_IRUGO | S_IWUSR,
79 show_priority, store_priority);
81 static ssize_t show_designated_root(struct dp_port *p, char *buf)
84 return br_show_bridge_id(buf, &p->designated_root);
86 return sprintf(buf, "0000.010203040506\n");
89 static BRPORT_ATTR(designated_root, S_IRUGO, show_designated_root, NULL);
91 static ssize_t show_designated_bridge(struct dp_port *p, char *buf)
94 return br_show_bridge_id(buf, &p->designated_bridge);
96 return sprintf(buf, "0000.060504030201\n");
99 static BRPORT_ATTR(designated_bridge, S_IRUGO, show_designated_bridge, NULL);
101 static ssize_t show_designated_port(struct dp_port *p, char *buf)
104 return sprintf(buf, "%d\n", p->designated_port);
106 return sprintf(buf, "%d\n", 0);
109 static BRPORT_ATTR(designated_port, S_IRUGO, show_designated_port, NULL);
111 static ssize_t show_designated_cost(struct dp_port *p, char *buf)
114 return sprintf(buf, "%d\n", p->designated_cost);
116 return sprintf(buf, "%d\n", 0);
119 static BRPORT_ATTR(designated_cost, S_IRUGO, show_designated_cost, NULL);
121 static ssize_t show_port_id(struct dp_port *p, char *buf)
124 return sprintf(buf, "0x%x\n", p->port_id);
126 return sprintf(buf, "0x%x\n", 0);
129 static BRPORT_ATTR(port_id, S_IRUGO, show_port_id, NULL);
131 static ssize_t show_port_no(struct dp_port *p, char *buf)
133 return sprintf(buf, "0x%x\n", p->port_no);
136 static BRPORT_ATTR(port_no, S_IRUGO, show_port_no, NULL);
138 static ssize_t show_change_ack(struct dp_port *p, char *buf)
141 return sprintf(buf, "%d\n", p->topology_change_ack);
143 return sprintf(buf, "%d\n", 0);
146 static BRPORT_ATTR(change_ack, S_IRUGO, show_change_ack, NULL);
148 static ssize_t show_config_pending(struct dp_port *p, char *buf)
151 return sprintf(buf, "%d\n", p->config_pending);
153 return sprintf(buf, "%d\n", 0);
156 static BRPORT_ATTR(config_pending, S_IRUGO, show_config_pending, NULL);
158 static ssize_t show_port_state(struct dp_port *p, char *buf)
161 return sprintf(buf, "%d\n", p->state);
163 return sprintf(buf, "%d\n", 0);
166 static BRPORT_ATTR(state, S_IRUGO, show_port_state, NULL);
168 static ssize_t show_message_age_timer(struct dp_port *p,
172 return sprintf(buf, "%ld\n", br_timer_value(&p->message_age_timer));
174 return sprintf(buf, "%d\n", 0);
177 static BRPORT_ATTR(message_age_timer, S_IRUGO, show_message_age_timer, NULL);
179 static ssize_t show_forward_delay_timer(struct dp_port *p,
183 return sprintf(buf, "%ld\n", br_timer_value(&p->forward_delay_timer));
185 return sprintf(buf, "%d\n", 0);
188 static BRPORT_ATTR(forward_delay_timer, S_IRUGO, show_forward_delay_timer, NULL);
190 static ssize_t show_hold_timer(struct dp_port *p,
194 return sprintf(buf, "%ld\n", br_timer_value(&p->hold_timer));
196 return sprintf(buf, "%d\n", 0);
199 static BRPORT_ATTR(hold_timer, S_IRUGO, show_hold_timer, NULL);
201 static struct brport_attribute *brport_attrs[] = {
202 &brport_attr_path_cost,
203 &brport_attr_priority,
204 &brport_attr_port_id,
205 &brport_attr_port_no,
206 &brport_attr_designated_root,
207 &brport_attr_designated_bridge,
208 &brport_attr_designated_port,
209 &brport_attr_designated_cost,
211 &brport_attr_change_ack,
212 &brport_attr_config_pending,
213 &brport_attr_message_age_timer,
214 &brport_attr_forward_delay_timer,
215 &brport_attr_hold_timer,
219 #define to_brport_attr(_at) container_of(_at, struct brport_attribute, attr)
220 #define to_brport(obj) container_of(obj, struct dp_port, kobj)
222 static ssize_t brport_show(struct kobject * kobj,
223 struct attribute * attr, char * buf)
225 struct brport_attribute * brport_attr = to_brport_attr(attr);
226 struct dp_port * p = to_brport(kobj);
228 return brport_attr->show(p, buf);
231 static ssize_t brport_store(struct kobject * kobj,
232 struct attribute * attr,
233 const char * buf, size_t count)
235 struct dp_port * p = to_brport(kobj);
237 struct brport_attribute * brport_attr = to_brport_attr(attr);
241 ssize_t ret = -EINVAL;
243 if (!capable(CAP_NET_ADMIN))
247 val = simple_strtoul(buf, &endp, 0);
250 if (p->dev && p->br && brport_attr->store) {
251 spin_lock_bh(&p->br->lock);
252 ret = brport_attr->store(p, val);
253 spin_unlock_bh(&p->br->lock);
260 printk("%s: xxx writing port parms not supported yet!\n",
266 struct sysfs_ops brport_sysfs_ops = {
268 .store = brport_store,
272 * Add sysfs entries to ethernet device added to a bridge.
273 * Creates a brport subdirectory with bridge attributes.
274 * Puts symlink in bridge's brport subdirectory
276 int dp_sysfs_add_if(struct dp_port *p)
278 struct kobject *kobj = vport_get_kobj(p->vport);
279 struct datapath *dp = p->dp;
280 struct brport_attribute **a;
283 /* Create /sys/class/net/<devname>/brport directory. */
287 err = kobject_add(&p->kobj, kobj, SYSFS_BRIDGE_PORT_ATTR);
291 /* Create symlink from /sys/class/net/<devname>/brport/bridge to
292 * /sys/class/net/<bridgename>. */
293 err = sysfs_create_link(&p->kobj, vport_get_kobj(dp->ports[ODPP_LOCAL]->vport),
294 SYSFS_BRIDGE_PORT_LINK); /* "bridge" */
298 /* Populate /sys/class/net/<devname>/brport directory with files. */
299 for (a = brport_attrs; *a; ++a) {
300 err = sysfs_create_file(&p->kobj, &((*a)->attr));
305 /* Create symlink from /sys/class/net/<bridgename>/brif/<devname> to
306 * /sys/class/net/<devname>/brport. */
307 err = sysfs_create_link(&dp->ifobj, &p->kobj, vport_get_name(p->vport));
310 strcpy(p->linkname, vport_get_name(p->vport));
312 kobject_uevent(&p->kobj, KOBJ_ADD);
317 kobject_del(&p->kobj);
323 int dp_sysfs_del_if(struct dp_port *p)
325 if (p->linkname[0]) {
326 sysfs_remove_link(&p->dp->ifobj, p->linkname);
327 kobject_uevent(&p->kobj, KOBJ_REMOVE);
328 kobject_del(&p->kobj);
329 p->linkname[0] = '\0';
333 #endif /* CONFIG_SYSFS */