2 * Copyright (c) 2007-2012 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/version.h>
24 * Sysfs attributes of bridge for Open vSwitch
26 * This has been shamelessly copied from the kernel sources.
29 #include <linux/capability.h>
30 #include <linux/device.h>
31 #include <linux/kernel.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_bridge.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/version.h>
39 #include "vport-internal_dev.h"
43 /* Hack to attempt to build on more platforms. */
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)
45 #define INTERNAL_DEVICE_ATTR CLASS_DEVICE_ATTR
46 #define DEVICE_PARAMS struct class_device *d
48 #define DEV_ATTR(NAME) class_device_attr_##NAME
50 #define INTERNAL_DEVICE_ATTR DEVICE_ATTR
51 #define DEVICE_PARAMS struct device *d, struct device_attribute *attr
52 #define DEVICE_ARGS d, attr
53 #define DEV_ATTR(NAME) dev_attr_##NAME
56 static struct datapath *sysfs_get_dp(struct net_device *netdev)
58 struct vport *vport = ovs_internal_dev_get_vport(netdev);
59 return vport ? vport->dp : NULL;
62 * Common code for storing bridge parameters.
64 static ssize_t store_bridge_parm(DEVICE_PARAMS,
65 const char *buf, size_t len,
66 void (*set)(struct datapath *, unsigned long))
72 if (!capable(CAP_NET_ADMIN))
75 val = simple_strtoul(buf, &endp, 0);
79 /* xxx We use a default value of 0 for all fields. If the caller is
80 * xxx attempting to set the value to our default, just silently
81 * xxx ignore the request.
88 dp = sysfs_get_dp(to_net_dev(d));
90 pr_warning("%s: xxx writing dp parms not supported yet!\n",
102 static ssize_t show_forward_delay(DEVICE_PARAMS, char *buf)
104 return sprintf(buf, "%d\n", 0);
107 static void set_forward_delay(struct datapath *dp, unsigned long val)
109 pr_info("%s: xxx attempt to set_forward_delay()\n", ovs_dp_name(dp));
112 static ssize_t store_forward_delay(DEVICE_PARAMS,
113 const char *buf, size_t len)
115 return store_bridge_parm(DEVICE_ARGS, buf, len, set_forward_delay);
117 static INTERNAL_DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
118 show_forward_delay, store_forward_delay);
120 static ssize_t show_hello_time(DEVICE_PARAMS, char *buf)
122 return sprintf(buf, "%d\n", 0);
125 static void set_hello_time(struct datapath *dp, unsigned long val)
127 pr_info("%s: xxx attempt to set_hello_time()\n", ovs_dp_name(dp));
130 static ssize_t store_hello_time(DEVICE_PARAMS,
134 return store_bridge_parm(DEVICE_ARGS, buf, len, set_hello_time);
136 static INTERNAL_DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
139 static ssize_t show_max_age(DEVICE_PARAMS, char *buf)
141 return sprintf(buf, "%d\n", 0);
144 static void set_max_age(struct datapath *dp, unsigned long val)
146 pr_info("%s: xxx attempt to set_max_age()\n", ovs_dp_name(dp));
149 static ssize_t store_max_age(DEVICE_PARAMS,
150 const char *buf, size_t len)
152 return store_bridge_parm(DEVICE_ARGS, buf, len, set_max_age);
154 static INTERNAL_DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age, store_max_age);
156 static ssize_t show_ageing_time(DEVICE_PARAMS, char *buf)
158 return sprintf(buf, "%d\n", 0);
161 static void set_ageing_time(struct datapath *dp, unsigned long val)
163 pr_info("%s: xxx attempt to set_ageing_time()\n", ovs_dp_name(dp));
166 static ssize_t store_ageing_time(DEVICE_PARAMS,
167 const char *buf, size_t len)
169 return store_bridge_parm(DEVICE_ARGS, buf, len, set_ageing_time);
171 static INTERNAL_DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
174 static ssize_t show_stp_state(DEVICE_PARAMS, char *buf)
176 return sprintf(buf, "%d\n", 0);
180 static ssize_t store_stp_state(DEVICE_PARAMS,
185 ssize_t result = len;
189 dp = sysfs_get_dp(to_net_dev(d));
191 pr_info("%s: xxx attempt to set_stp_state()\n", ovs_dp_name(dp));
199 static INTERNAL_DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
202 static ssize_t show_priority(DEVICE_PARAMS, char *buf)
204 return sprintf(buf, "%d\n", 0);
207 static void set_priority(struct datapath *dp, unsigned long val)
209 pr_info("%s: xxx attempt to set_priority()\n", ovs_dp_name(dp));
212 static ssize_t store_priority(DEVICE_PARAMS,
213 const char *buf, size_t len)
215 return store_bridge_parm(DEVICE_ARGS, buf, len, set_priority);
217 static INTERNAL_DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority);
219 static ssize_t show_root_id(DEVICE_PARAMS, char *buf)
221 return sprintf(buf, "0000.010203040506\n");
223 static INTERNAL_DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
225 static ssize_t show_bridge_id(DEVICE_PARAMS, char *buf)
232 vport = ovs_internal_dev_get_vport(to_net_dev(d));
234 const unsigned char *addr;
236 addr = vport->ops->get_addr(vport);
237 result = sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
238 0, 0, addr[0], addr[1], addr[2], addr[3],
247 static INTERNAL_DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
249 static ssize_t show_root_port(DEVICE_PARAMS, char *buf)
251 return sprintf(buf, "%d\n", 0);
253 static INTERNAL_DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
255 static ssize_t show_root_path_cost(DEVICE_PARAMS, char *buf)
257 return sprintf(buf, "%d\n", 0);
259 static INTERNAL_DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
261 static ssize_t show_topology_change(DEVICE_PARAMS, char *buf)
263 return sprintf(buf, "%d\n", 0);
265 static INTERNAL_DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
267 static ssize_t show_topology_change_detected(DEVICE_PARAMS, char *buf)
269 return sprintf(buf, "%d\n", 0);
271 static INTERNAL_DEVICE_ATTR(topology_change_detected, S_IRUGO,
272 show_topology_change_detected, NULL);
274 static ssize_t show_hello_timer(DEVICE_PARAMS, char *buf)
276 return sprintf(buf, "%d\n", 0);
278 static INTERNAL_DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
280 static ssize_t show_tcn_timer(DEVICE_PARAMS, char *buf)
282 return sprintf(buf, "%d\n", 0);
284 static INTERNAL_DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
286 static ssize_t show_topology_change_timer(DEVICE_PARAMS, char *buf)
288 return sprintf(buf, "%d\n", 0);
290 static INTERNAL_DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer,
293 static ssize_t show_gc_timer(DEVICE_PARAMS, char *buf)
295 return sprintf(buf, "%d\n", 0);
297 static INTERNAL_DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
299 static ssize_t show_group_addr(DEVICE_PARAMS, char *buf)
301 return sprintf(buf, "00:01:02:03:04:05\n");
304 static ssize_t store_group_addr(DEVICE_PARAMS,
305 const char *buf, size_t len)
308 ssize_t result = len;
312 dp = sysfs_get_dp(to_net_dev(d));
314 pr_info("%s: xxx attempt to store_group_addr()\n",
324 static INTERNAL_DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
325 show_group_addr, store_group_addr);
327 static struct attribute *bridge_attrs[] = {
328 &DEV_ATTR(forward_delay).attr,
329 &DEV_ATTR(hello_time).attr,
330 &DEV_ATTR(max_age).attr,
331 &DEV_ATTR(ageing_time).attr,
332 &DEV_ATTR(stp_state).attr,
333 &DEV_ATTR(priority).attr,
334 &DEV_ATTR(bridge_id).attr,
335 &DEV_ATTR(root_id).attr,
336 &DEV_ATTR(root_path_cost).attr,
337 &DEV_ATTR(root_port).attr,
338 &DEV_ATTR(topology_change).attr,
339 &DEV_ATTR(topology_change_detected).attr,
340 &DEV_ATTR(hello_timer).attr,
341 &DEV_ATTR(tcn_timer).attr,
342 &DEV_ATTR(topology_change_timer).attr,
343 &DEV_ATTR(gc_timer).attr,
344 &DEV_ATTR(group_addr).attr,
348 static struct attribute_group bridge_group = {
349 .name = SYSFS_BRIDGE_ATTR, /* "bridge" */
350 .attrs = bridge_attrs,
354 * Add entries in sysfs onto the existing network class device
356 * Adds a attribute group "bridge" containing tuning parameters.
357 * Sub directory to hold links to interfaces.
359 * Note: the ifobj exists only to be a subdirectory
360 * to hold links. The ifobj exists in the same data structure
361 * as its parent the bridge so reference counting works.
363 int ovs_dp_sysfs_add_dp(struct datapath *dp)
365 struct vport *vport = ovs_vport_rtnl(dp, OVSP_LOCAL);
366 struct kobject *kobj = vport->ops->get_kobj(vport);
370 /* Due to bug in 2.6.32 kernel, sysfs_create_group() could panic
371 * in other namespace than init_net. Following check is to avoid it. */
375 /* Create /sys/class/net/<devname>/bridge directory. */
376 err = sysfs_create_group(kobj, &bridge_group);
378 pr_info("%s: can't create group %s/%s\n",
379 __func__, ovs_dp_name(dp), bridge_group.name);
383 /* Create /sys/class/net/<devname>/brif directory. */
384 err = kobject_add(&dp->ifobj, kobj, SYSFS_BRIDGE_PORT_SUBDIR);
386 pr_info("%s: can't add kobject (directory) %s/%s\n",
387 __func__, ovs_dp_name(dp), kobject_name(&dp->ifobj));
390 kobject_uevent(&dp->ifobj, KOBJ_ADD);
394 sysfs_remove_group(kobj, &bridge_group);
399 int ovs_dp_sysfs_del_dp(struct datapath *dp)
401 struct vport *vport = ovs_vport_rtnl(dp, OVSP_LOCAL);
402 struct kobject *kobj = vport->ops->get_kobj(vport);
409 kobject_del(&dp->ifobj);
410 sysfs_remove_group(kobj, &bridge_group);
414 #else /* !CONFIG_SYSFS */
415 int ovs_dp_sysfs_add_dp(struct datapath *dp) { return 0; }
416 int ovs_dp_sysfs_del_dp(struct datapath *dp) { return 0; }
417 int dp_sysfs_add_if(struct vport *p) { return 0; }
418 int dp_sysfs_del_if(struct vport *p) { return 0; }
419 #endif /* !CONFIG_SYSFS */