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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/capability.h>
18 #include <linux/kernel.h>
19 #include <linux/netdevice.h>
20 #include <linux/if_bridge.h>
21 #include <linux/rtnetlink.h>
29 struct brport_attribute {
30 struct attribute attr;
31 ssize_t (*show)(struct vport *, char *);
32 ssize_t (*store)(struct vport *, unsigned long);
35 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
36 #define BRPORT_ATTR(_name, _mode, _show, _store) \
37 struct brport_attribute brport_attr_##_name = { \
38 .attr = {.name = __stringify(_name), \
44 #define BRPORT_ATTR(_name, _mode, _show, _store) \
45 struct brport_attribute brport_attr_##_name = { \
46 .attr = {.name = __stringify(_name), \
48 .owner = THIS_MODULE, }, \
54 static ssize_t show_path_cost(struct vport *p, char *buf)
56 return sprintf(buf, "%d\n", 0);
58 static ssize_t store_path_cost(struct vport *p, unsigned long v)
62 static BRPORT_ATTR(path_cost, S_IRUGO | S_IWUSR,
63 show_path_cost, store_path_cost);
65 static ssize_t show_priority(struct vport *p, char *buf)
67 return sprintf(buf, "%d\n", 0);
69 static ssize_t store_priority(struct vport *p, unsigned long v)
73 static BRPORT_ATTR(priority, S_IRUGO | S_IWUSR,
74 show_priority, store_priority);
76 static ssize_t show_designated_root(struct vport *p, char *buf)
78 return sprintf(buf, "0000.010203040506\n");
80 static BRPORT_ATTR(designated_root, S_IRUGO, show_designated_root, NULL);
82 static ssize_t show_designated_bridge(struct vport *p, char *buf)
84 return sprintf(buf, "0000.060504030201\n");
86 static BRPORT_ATTR(designated_bridge, S_IRUGO, show_designated_bridge, NULL);
88 static ssize_t show_designated_port(struct vport *p, char *buf)
90 return sprintf(buf, "%d\n", 0);
92 static BRPORT_ATTR(designated_port, S_IRUGO, show_designated_port, NULL);
94 static ssize_t show_designated_cost(struct vport *p, char *buf)
96 return sprintf(buf, "%d\n", 0);
98 static BRPORT_ATTR(designated_cost, S_IRUGO, show_designated_cost, NULL);
100 static ssize_t show_port_id(struct vport *p, char *buf)
102 return sprintf(buf, "0x%x\n", 0);
104 static BRPORT_ATTR(port_id, S_IRUGO, show_port_id, NULL);
106 static ssize_t show_port_no(struct vport *p, char *buf)
108 return sprintf(buf, "0x%x\n", p->port_no);
111 static BRPORT_ATTR(port_no, S_IRUGO, show_port_no, NULL);
113 static ssize_t show_change_ack(struct vport *p, char *buf)
115 return sprintf(buf, "%d\n", 0);
117 static BRPORT_ATTR(change_ack, S_IRUGO, show_change_ack, NULL);
119 static ssize_t show_config_pending(struct vport *p, char *buf)
121 return sprintf(buf, "%d\n", 0);
123 static BRPORT_ATTR(config_pending, S_IRUGO, show_config_pending, NULL);
125 static ssize_t show_port_state(struct vport *p, char *buf)
127 return sprintf(buf, "%d\n", 0);
129 static BRPORT_ATTR(state, S_IRUGO, show_port_state, NULL);
131 static ssize_t show_message_age_timer(struct vport *p, char *buf)
133 return sprintf(buf, "%d\n", 0);
135 static BRPORT_ATTR(message_age_timer, S_IRUGO, show_message_age_timer, NULL);
137 static ssize_t show_forward_delay_timer(struct vport *p, char *buf)
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, char *buf)
145 return sprintf(buf, "%d\n", 0);
147 static BRPORT_ATTR(hold_timer, S_IRUGO, show_hold_timer, NULL);
149 static struct brport_attribute *brport_attrs[] = {
150 &brport_attr_path_cost,
151 &brport_attr_priority,
152 &brport_attr_port_id,
153 &brport_attr_port_no,
154 &brport_attr_designated_root,
155 &brport_attr_designated_bridge,
156 &brport_attr_designated_port,
157 &brport_attr_designated_cost,
159 &brport_attr_change_ack,
160 &brport_attr_config_pending,
161 &brport_attr_message_age_timer,
162 &brport_attr_forward_delay_timer,
163 &brport_attr_hold_timer,
167 #define to_vport_attr(_at) container_of(_at, struct brport_attribute, attr)
168 #define to_vport(obj) container_of(obj, struct vport, kobj)
170 static ssize_t brport_show(struct kobject *kobj,
171 struct attribute *attr, char *buf)
173 struct brport_attribute *brport_attr = to_vport_attr(attr);
174 struct vport *p = to_vport(kobj);
176 return brport_attr->show(p, buf);
179 static ssize_t brport_store(struct kobject *kobj,
180 struct attribute *attr,
181 const char *buf, size_t count)
183 struct vport *p = to_vport(kobj);
184 ssize_t ret = -EINVAL;
186 if (!capable(CAP_NET_ADMIN))
189 pr_warning("%s: xxx writing port parms not supported yet!\n",
195 const struct sysfs_ops brport_sysfs_ops = {
197 .store = brport_store,
201 * Add sysfs entries to ethernet device added to a bridge.
202 * Creates a brport subdirectory with bridge attributes.
203 * Puts symlink in bridge's brport subdirectory
205 int dp_sysfs_add_if(struct vport *p)
207 struct datapath *dp = p->dp;
208 struct vport *local_port = rtnl_dereference(dp->ports[OVSP_LOCAL]);
209 struct brport_attribute **a;
212 /* Create /sys/class/net/<devname>/brport directory. */
213 if (!p->ops->get_kobj)
216 err = kobject_add(&p->kobj, p->ops->get_kobj(p),
217 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, local_port->ops->get_kobj(local_port),
224 SYSFS_BRIDGE_PORT_LINK); /* "bridge" */
228 /* Populate /sys/class/net/<devname>/brport directory with files. */
229 for (a = brport_attrs; *a; ++a) {
230 err = sysfs_create_file(&p->kobj, &((*a)->attr));
235 /* Create symlink from /sys/class/net/<bridgename>/brif/<devname> to
236 * /sys/class/net/<devname>/brport. */
237 err = sysfs_create_link(&dp->ifobj, &p->kobj, p->ops->get_name(p));
240 strcpy(p->linkname, p->ops->get_name(p));
242 kobject_uevent(&p->kobj, KOBJ_ADD);
247 kobject_del(&p->kobj);
253 int dp_sysfs_del_if(struct vport *p)
255 if (p->linkname[0]) {
256 sysfs_remove_link(&p->dp->ifobj, p->linkname);
257 kobject_uevent(&p->kobj, KOBJ_REMOVE);
258 kobject_del(&p->kobj);
259 p->linkname[0] = '\0';
263 #endif /* CONFIG_SYSFS */