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>
27 struct brport_attribute {
28 struct attribute attr;
29 ssize_t (*show)(struct dp_port *, char *);
30 ssize_t (*store)(struct dp_port *, unsigned long);
33 #define BRPORT_ATTR(_name,_mode,_show,_store) \
34 struct brport_attribute brport_attr_##_name = { \
35 .attr = {.name = __stringify(_name), \
37 .owner = THIS_MODULE, }, \
42 static ssize_t show_path_cost(struct dp_port *p, char *buf)
44 return sprintf(buf, "%d\n", 0);
46 static ssize_t store_path_cost(struct dp_port *p, unsigned long v)
50 static BRPORT_ATTR(path_cost, S_IRUGO | S_IWUSR,
51 show_path_cost, store_path_cost);
53 static ssize_t show_priority(struct dp_port *p, char *buf)
55 return sprintf(buf, "%d\n", 0);
57 static ssize_t store_priority(struct dp_port *p, unsigned long v)
61 static BRPORT_ATTR(priority, S_IRUGO | S_IWUSR,
62 show_priority, store_priority);
64 static ssize_t show_designated_root(struct dp_port *p, char *buf)
66 return sprintf(buf, "0000.010203040506\n");
68 static BRPORT_ATTR(designated_root, S_IRUGO, show_designated_root, NULL);
70 static ssize_t show_designated_bridge(struct dp_port *p, char *buf)
72 return sprintf(buf, "0000.060504030201\n");
74 static BRPORT_ATTR(designated_bridge, S_IRUGO, show_designated_bridge, NULL);
76 static ssize_t show_designated_port(struct dp_port *p, char *buf)
78 return sprintf(buf, "%d\n", 0);
80 static BRPORT_ATTR(designated_port, S_IRUGO, show_designated_port, NULL);
82 static ssize_t show_designated_cost(struct dp_port *p, char *buf)
84 return sprintf(buf, "%d\n", 0);
86 static BRPORT_ATTR(designated_cost, S_IRUGO, show_designated_cost, NULL);
88 static ssize_t show_port_id(struct dp_port *p, char *buf)
90 return sprintf(buf, "0x%x\n", 0);
92 static BRPORT_ATTR(port_id, S_IRUGO, show_port_id, NULL);
94 static ssize_t show_port_no(struct dp_port *p, char *buf)
96 return sprintf(buf, "0x%x\n", p->port_no);
99 static BRPORT_ATTR(port_no, S_IRUGO, show_port_no, NULL);
101 static ssize_t show_change_ack(struct dp_port *p, char *buf)
103 return sprintf(buf, "%d\n", 0);
105 static BRPORT_ATTR(change_ack, S_IRUGO, show_change_ack, NULL);
107 static ssize_t show_config_pending(struct dp_port *p, char *buf)
109 return sprintf(buf, "%d\n", 0);
111 static BRPORT_ATTR(config_pending, S_IRUGO, show_config_pending, NULL);
113 static ssize_t show_port_state(struct dp_port *p, char *buf)
115 return sprintf(buf, "%d\n", 0);
117 static BRPORT_ATTR(state, S_IRUGO, show_port_state, NULL);
119 static ssize_t show_message_age_timer(struct dp_port *p,
122 return sprintf(buf, "%d\n", 0);
124 static BRPORT_ATTR(message_age_timer, S_IRUGO, show_message_age_timer, NULL);
126 static ssize_t show_forward_delay_timer(struct dp_port *p,
129 return sprintf(buf, "%d\n", 0);
131 static BRPORT_ATTR(forward_delay_timer, S_IRUGO, show_forward_delay_timer, NULL);
133 static ssize_t show_hold_timer(struct dp_port *p,
136 return sprintf(buf, "%d\n", 0);
138 static BRPORT_ATTR(hold_timer, S_IRUGO, show_hold_timer, NULL);
140 static struct brport_attribute *brport_attrs[] = {
141 &brport_attr_path_cost,
142 &brport_attr_priority,
143 &brport_attr_port_id,
144 &brport_attr_port_no,
145 &brport_attr_designated_root,
146 &brport_attr_designated_bridge,
147 &brport_attr_designated_port,
148 &brport_attr_designated_cost,
150 &brport_attr_change_ack,
151 &brport_attr_config_pending,
152 &brport_attr_message_age_timer,
153 &brport_attr_forward_delay_timer,
154 &brport_attr_hold_timer,
158 #define to_brport_attr(_at) container_of(_at, struct brport_attribute, attr)
159 #define to_brport(obj) container_of(obj, struct dp_port, kobj)
161 static ssize_t brport_show(struct kobject * kobj,
162 struct attribute * attr, char * buf)
164 struct brport_attribute * brport_attr = to_brport_attr(attr);
165 struct dp_port * p = to_brport(kobj);
167 return brport_attr->show(p, buf);
170 static ssize_t brport_store(struct kobject * kobj,
171 struct attribute * attr,
172 const char * buf, size_t count)
174 struct dp_port * p = to_brport(kobj);
175 ssize_t ret = -EINVAL;
177 if (!capable(CAP_NET_ADMIN))
180 printk("%s: xxx writing port parms not supported yet!\n",
186 struct sysfs_ops brport_sysfs_ops = {
188 .store = brport_store,
192 * Add sysfs entries to ethernet device added to a bridge.
193 * Creates a brport subdirectory with bridge attributes.
194 * Puts symlink in bridge's brport subdirectory
196 int dp_sysfs_add_if(struct dp_port *p)
198 struct kobject *kobj = vport_get_kobj(p->vport);
199 struct datapath *dp = p->dp;
200 struct brport_attribute **a;
203 /* Create /sys/class/net/<devname>/brport directory. */
207 err = kobject_add(&p->kobj, kobj, SYSFS_BRIDGE_PORT_ATTR);
211 /* Create symlink from /sys/class/net/<devname>/brport/bridge to
212 * /sys/class/net/<bridgename>. */
213 err = sysfs_create_link(&p->kobj, vport_get_kobj(dp->ports[ODPP_LOCAL]->vport),
214 SYSFS_BRIDGE_PORT_LINK); /* "bridge" */
218 /* Populate /sys/class/net/<devname>/brport directory with files. */
219 for (a = brport_attrs; *a; ++a) {
220 err = sysfs_create_file(&p->kobj, &((*a)->attr));
225 /* Create symlink from /sys/class/net/<bridgename>/brif/<devname> to
226 * /sys/class/net/<devname>/brport. */
227 err = sysfs_create_link(&dp->ifobj, &p->kobj, vport_get_name(p->vport));
230 strcpy(p->linkname, vport_get_name(p->vport));
232 kobject_uevent(&p->kobj, KOBJ_ADD);
237 kobject_del(&p->kobj);
243 int dp_sysfs_del_if(struct dp_port *p)
245 if (p->linkname[0]) {
246 sysfs_remove_link(&p->dp->ifobj, p->linkname);
247 kobject_uevent(&p->kobj, KOBJ_REMOVE);
248 kobject_del(&p->kobj);
249 p->linkname[0] = '\0';
253 #endif /* CONFIG_SYSFS */