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 vport *, char *);
30 ssize_t (*store)(struct vport *, unsigned long);
33 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
34 #define BRPORT_ATTR(_name,_mode,_show,_store) \
35 struct brport_attribute brport_attr_##_name = { \
36 .attr = {.name = __stringify(_name), \
42 #define BRPORT_ATTR(_name,_mode,_show,_store) \
43 struct brport_attribute brport_attr_##_name = { \
44 .attr = {.name = __stringify(_name), \
46 .owner = THIS_MODULE, }, \
52 static ssize_t show_path_cost(struct vport *p, char *buf)
54 return sprintf(buf, "%d\n", 0);
56 static ssize_t store_path_cost(struct vport *p, unsigned long v)
60 static BRPORT_ATTR(path_cost, S_IRUGO | S_IWUSR,
61 show_path_cost, store_path_cost);
63 static ssize_t show_priority(struct vport *p, char *buf)
65 return sprintf(buf, "%d\n", 0);
67 static ssize_t store_priority(struct vport *p, unsigned long v)
71 static BRPORT_ATTR(priority, S_IRUGO | S_IWUSR,
72 show_priority, store_priority);
74 static ssize_t show_designated_root(struct vport *p, char *buf)
76 return sprintf(buf, "0000.010203040506\n");
78 static BRPORT_ATTR(designated_root, S_IRUGO, show_designated_root, NULL);
80 static ssize_t show_designated_bridge(struct vport *p, char *buf)
82 return sprintf(buf, "0000.060504030201\n");
84 static BRPORT_ATTR(designated_bridge, S_IRUGO, show_designated_bridge, NULL);
86 static ssize_t show_designated_port(struct vport *p, char *buf)
88 return sprintf(buf, "%d\n", 0);
90 static BRPORT_ATTR(designated_port, S_IRUGO, show_designated_port, NULL);
92 static ssize_t show_designated_cost(struct vport *p, char *buf)
94 return sprintf(buf, "%d\n", 0);
96 static BRPORT_ATTR(designated_cost, S_IRUGO, show_designated_cost, NULL);
98 static ssize_t show_port_id(struct vport *p, char *buf)
100 return sprintf(buf, "0x%x\n", 0);
102 static BRPORT_ATTR(port_id, S_IRUGO, show_port_id, NULL);
104 static ssize_t show_port_no(struct vport *p, char *buf)
106 return sprintf(buf, "0x%x\n", p->port_no);
109 static BRPORT_ATTR(port_no, S_IRUGO, show_port_no, NULL);
111 static ssize_t show_change_ack(struct vport *p, char *buf)
113 return sprintf(buf, "%d\n", 0);
115 static BRPORT_ATTR(change_ack, S_IRUGO, show_change_ack, NULL);
117 static ssize_t show_config_pending(struct vport *p, char *buf)
119 return sprintf(buf, "%d\n", 0);
121 static BRPORT_ATTR(config_pending, S_IRUGO, show_config_pending, NULL);
123 static ssize_t show_port_state(struct vport *p, char *buf)
125 return sprintf(buf, "%d\n", 0);
127 static BRPORT_ATTR(state, S_IRUGO, show_port_state, NULL);
129 static ssize_t show_message_age_timer(struct vport *p,
132 return sprintf(buf, "%d\n", 0);
134 static BRPORT_ATTR(message_age_timer, S_IRUGO, show_message_age_timer, NULL);
136 static ssize_t show_forward_delay_timer(struct vport *p,
139 return sprintf(buf, "%d\n", 0);
141 static BRPORT_ATTR(forward_delay_timer, S_IRUGO, show_forward_delay_timer, NULL);
143 static ssize_t show_hold_timer(struct vport *p,
146 return sprintf(buf, "%d\n", 0);
148 static BRPORT_ATTR(hold_timer, S_IRUGO, show_hold_timer, NULL);
150 static struct brport_attribute *brport_attrs[] = {
151 &brport_attr_path_cost,
152 &brport_attr_priority,
153 &brport_attr_port_id,
154 &brport_attr_port_no,
155 &brport_attr_designated_root,
156 &brport_attr_designated_bridge,
157 &brport_attr_designated_port,
158 &brport_attr_designated_cost,
160 &brport_attr_change_ack,
161 &brport_attr_config_pending,
162 &brport_attr_message_age_timer,
163 &brport_attr_forward_delay_timer,
164 &brport_attr_hold_timer,
168 #define to_vport_attr(_at) container_of(_at, struct brport_attribute, attr)
169 #define to_vport(obj) container_of(obj, struct vport, kobj)
171 static ssize_t brport_show(struct kobject * kobj,
172 struct attribute * attr, char * buf)
174 struct brport_attribute * brport_attr = to_vport_attr(attr);
175 struct vport * p = to_vport(kobj);
177 return brport_attr->show(p, buf);
180 static ssize_t brport_store(struct kobject * kobj,
181 struct attribute * attr,
182 const char * buf, size_t count)
184 struct vport * p = to_vport(kobj);
185 ssize_t ret = -EINVAL;
187 if (!capable(CAP_NET_ADMIN))
190 printk("%s: xxx writing port parms not supported yet!\n",
196 struct sysfs_ops brport_sysfs_ops = {
198 .store = brport_store,
202 * Add sysfs entries to ethernet device added to a bridge.
203 * Creates a brport subdirectory with bridge attributes.
204 * Puts symlink in bridge's brport subdirectory
206 int dp_sysfs_add_if(struct vport *p)
208 struct kobject *kobj = vport_get_kobj(p);
209 struct datapath *dp = p->dp;
210 struct brport_attribute **a;
213 /* Create /sys/class/net/<devname>/brport directory. */
217 err = kobject_add(&p->kobj, kobj, SYSFS_BRIDGE_PORT_ATTR);
221 /* Create symlink from /sys/class/net/<devname>/brport/bridge to
222 * /sys/class/net/<bridgename>. */
223 err = sysfs_create_link(&p->kobj,
224 vport_get_kobj(rtnl_dereference(dp->ports[ODPP_LOCAL])),
225 SYSFS_BRIDGE_PORT_LINK); /* "bridge" */
229 /* Populate /sys/class/net/<devname>/brport directory with files. */
230 for (a = brport_attrs; *a; ++a) {
231 err = sysfs_create_file(&p->kobj, &((*a)->attr));
236 /* Create symlink from /sys/class/net/<bridgename>/brif/<devname> to
237 * /sys/class/net/<devname>/brport. */
238 err = sysfs_create_link(&dp->ifobj, &p->kobj, vport_get_name(p));
241 strcpy(p->linkname, vport_get_name(p));
243 kobject_uevent(&p->kobj, KOBJ_ADD);
248 kobject_del(&p->kobj);
254 int dp_sysfs_del_if(struct vport *p)
256 if (p->linkname[0]) {
257 sysfs_remove_link(&p->dp->ifobj, p->linkname);
258 kobject_uevent(&p->kobj, KOBJ_REMOVE);
259 kobject_del(&p->kobj);
260 p->linkname[0] = '\0';
264 #endif /* CONFIG_SYSFS */