2 * Copyright (c) 2009 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.
9 #include <linux/version.h>
12 * Sysfs attributes of bridge for Open vSwitch
14 * This has been shamelessly copied from the kernel sources.
17 #include <linux/capability.h>
18 #include <linux/device.h>
19 #include <linux/kernel.h>
20 #include <linux/netdevice.h>
21 #include <linux/if_bridge.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/spinlock.h>
24 #include <linux/times.h>
25 #include <linux/version.h>
32 #define to_dev(obj) container_of(obj, struct device, kobj)
34 /* Hack to attempt to build on more platforms. */
35 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)
36 #define DP_DEVICE_ATTR CLASS_DEVICE_ATTR
37 #define DEVICE_PARAMS struct class_device *d
39 #define DEV_ATTR(NAME) class_device_attr_##NAME
41 #define DP_DEVICE_ATTR DEVICE_ATTR
42 #define DEVICE_PARAMS struct device *d, struct device_attribute *attr
43 #define DEVICE_ARGS d, attr
44 #define DEV_ATTR(NAME) dev_attr_##NAME
48 * Common code for storing bridge parameters.
50 static ssize_t
store_bridge_parm(DEVICE_PARAMS
,
51 const char *buf
, size_t len
,
52 void (*set
)(struct datapath
*, unsigned long))
54 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
58 if (!capable(CAP_NET_ADMIN
))
61 val
= simple_strtoul(buf
, &endp
, 0);
66 spin_lock_bh(&br
->lock
);
68 spin_unlock_bh(&br
->lock
);
70 /* xxx We use a default value of 0 for all fields. If the caller is
71 * xxx attempting to set the value to our default, just silently
72 * xxx ignore the request.
75 printk("%s: xxx writing dp parms not supported yet!\n",
83 static ssize_t
show_forward_delay(DEVICE_PARAMS
, char *buf
)
86 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
87 return sprintf(buf
, "%lu\n", jiffies_to_clock_t(br
->forward_delay
));
89 return sprintf(buf
, "%d\n", 0);
93 static void set_forward_delay(struct datapath
*dp
, unsigned long val
)
96 unsigned long delay
= clock_t_to_jiffies(val
);
97 br
->forward_delay
= delay
;
98 if (br_is_root_bridge(br
))
99 br
->bridge_forward_delay
= delay
;
101 printk("%s: xxx attempt to set_forward_delay()\n", dp_name(dp
));
105 static ssize_t
store_forward_delay(DEVICE_PARAMS
,
106 const char *buf
, size_t len
)
108 return store_bridge_parm(DEVICE_ARGS
, buf
, len
, set_forward_delay
);
110 static DP_DEVICE_ATTR(forward_delay
, S_IRUGO
| S_IWUSR
,
111 show_forward_delay
, store_forward_delay
);
113 static ssize_t
show_hello_time(DEVICE_PARAMS
, char *buf
)
116 return sprintf(buf
, "%lu\n",
117 jiffies_to_clock_t(to_bridge(d
)->hello_time
));
119 return sprintf(buf
, "%d\n", 0);
123 static void set_hello_time(struct datapath
*dp
, unsigned long val
)
126 unsigned long t
= clock_t_to_jiffies(val
);
128 if (br_is_root_bridge(br
))
129 br
->bridge_hello_time
= t
;
131 printk("%s: xxx attempt to set_hello_time()\n", dp_name(dp
));
135 static ssize_t
store_hello_time(DEVICE_PARAMS
,
139 return store_bridge_parm(DEVICE_ARGS
, buf
, len
, set_hello_time
);
141 static DP_DEVICE_ATTR(hello_time
, S_IRUGO
| S_IWUSR
, show_hello_time
,
144 static ssize_t
show_max_age(DEVICE_PARAMS
, char *buf
)
147 return sprintf(buf
, "%lu\n",
148 jiffies_to_clock_t(to_bridge(d
)->max_age
));
150 return sprintf(buf
, "%d\n", 0);
154 static void set_max_age(struct datapath
*dp
, unsigned long val
)
157 unsigned long t
= clock_t_to_jiffies(val
);
159 if (br_is_root_bridge(br
))
160 br
->bridge_max_age
= t
;
162 printk("%s: xxx attempt to set_max_age()\n", dp_name(dp
));
166 static ssize_t
store_max_age(DEVICE_PARAMS
,
167 const char *buf
, size_t len
)
169 return store_bridge_parm(DEVICE_ARGS
, buf
, len
, set_max_age
);
171 static DP_DEVICE_ATTR(max_age
, S_IRUGO
| S_IWUSR
, show_max_age
, store_max_age
);
173 static ssize_t
show_ageing_time(DEVICE_PARAMS
, char *buf
)
176 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
177 return sprintf(buf
, "%lu\n", jiffies_to_clock_t(br
->ageing_time
));
179 return sprintf(buf
, "%d\n", 0);
183 static void set_ageing_time(struct datapath
*dp
, unsigned long val
)
186 br
->ageing_time
= clock_t_to_jiffies(val
);
188 printk("%s: xxx attempt to set_ageing_time()\n", dp_name(dp
));
192 static ssize_t
store_ageing_time(DEVICE_PARAMS
,
193 const char *buf
, size_t len
)
195 return store_bridge_parm(DEVICE_ARGS
, buf
, len
, set_ageing_time
);
197 static DP_DEVICE_ATTR(ageing_time
, S_IRUGO
| S_IWUSR
, show_ageing_time
,
200 static ssize_t
show_stp_state(DEVICE_PARAMS
, char *buf
)
203 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
204 return sprintf(buf
, "%d\n", br
->stp_enabled
);
206 return sprintf(buf
, "%d\n", 0);
211 static ssize_t
store_stp_state(DEVICE_PARAMS
,
215 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
220 if (!capable(CAP_NET_ADMIN
))
223 val
= simple_strtoul(buf
, &endp
, 0);
228 br_stp_set_enabled(br
, val
);
231 printk("%s: xxx attempt to set_stp_state()\n", dp_name(dp
));
236 static DP_DEVICE_ATTR(stp_state
, S_IRUGO
| S_IWUSR
, show_stp_state
,
239 static ssize_t
show_priority(DEVICE_PARAMS
, char *buf
)
242 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
243 return sprintf(buf
, "%d\n",
244 (br
->bridge_id
.prio
[0] << 8) | br
->bridge_id
.prio
[1]);
246 return sprintf(buf
, "%d\n", 0);
250 static void set_priority(struct datapath
*dp
, unsigned long val
)
253 br_stp_set_bridge_priority(br
, (u16
) val
);
255 printk("%s: xxx attempt to set_priority()\n", dp_name(dp
));
259 static ssize_t
store_priority(DEVICE_PARAMS
,
260 const char *buf
, size_t len
)
262 return store_bridge_parm(DEVICE_ARGS
, buf
, len
, set_priority
);
264 static DP_DEVICE_ATTR(priority
, S_IRUGO
| S_IWUSR
, show_priority
, store_priority
);
266 static ssize_t
show_root_id(DEVICE_PARAMS
, char *buf
)
269 return br_show_bridge_id(buf
, &to_bridge(d
)->designated_root
);
271 return sprintf(buf
, "0000.010203040506\n");
274 static DP_DEVICE_ATTR(root_id
, S_IRUGO
, show_root_id
, NULL
);
276 static ssize_t
show_bridge_id(DEVICE_PARAMS
, char *buf
)
278 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
279 const unsigned char *addr
= dp
->ports
[ODPP_LOCAL
]->dev
->dev_addr
;
281 /* xxx Do we need a lock of some sort? */
282 return sprintf(buf
, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
283 0, 0, addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
285 static DP_DEVICE_ATTR(bridge_id
, S_IRUGO
, show_bridge_id
, NULL
);
287 static ssize_t
show_root_port(DEVICE_PARAMS
, char *buf
)
290 return sprintf(buf
, "%d\n", to_bridge(d
)->root_port
);
292 return sprintf(buf
, "%d\n", 0);
295 static DP_DEVICE_ATTR(root_port
, S_IRUGO
, show_root_port
, NULL
);
297 static ssize_t
show_root_path_cost(DEVICE_PARAMS
, char *buf
)
300 return sprintf(buf
, "%d\n", to_bridge(d
)->root_path_cost
);
302 return sprintf(buf
, "%d\n", 0);
305 static DP_DEVICE_ATTR(root_path_cost
, S_IRUGO
, show_root_path_cost
, NULL
);
307 static ssize_t
show_topology_change(DEVICE_PARAMS
, char *buf
)
310 return sprintf(buf
, "%d\n", to_bridge(d
)->topology_change
);
312 return sprintf(buf
, "%d\n", 0);
315 static DP_DEVICE_ATTR(topology_change
, S_IRUGO
, show_topology_change
, NULL
);
317 static ssize_t
show_topology_change_detected(DEVICE_PARAMS
, char *buf
)
320 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
321 return sprintf(buf
, "%d\n", br
->topology_change_detected
);
323 return sprintf(buf
, "%d\n", 0);
326 static DP_DEVICE_ATTR(topology_change_detected
, S_IRUGO
,
327 show_topology_change_detected
, NULL
);
329 static ssize_t
show_hello_timer(DEVICE_PARAMS
, char *buf
)
332 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
333 return sprintf(buf
, "%ld\n", br_timer_value(&br
->hello_timer
));
335 return sprintf(buf
, "%d\n", 0);
338 static DP_DEVICE_ATTR(hello_timer
, S_IRUGO
, show_hello_timer
, NULL
);
340 static ssize_t
show_tcn_timer(DEVICE_PARAMS
, char *buf
)
343 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
344 return sprintf(buf
, "%ld\n", br_timer_value(&br
->tcn_timer
));
346 return sprintf(buf
, "%d\n", 0);
349 static DP_DEVICE_ATTR(tcn_timer
, S_IRUGO
, show_tcn_timer
, NULL
);
351 static ssize_t
show_topology_change_timer(DEVICE_PARAMS
, char *buf
)
354 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
355 return sprintf(buf
, "%ld\n", br_timer_value(&br
->topology_change_timer
));
357 return sprintf(buf
, "%d\n", 0);
360 static DP_DEVICE_ATTR(topology_change_timer
, S_IRUGO
, show_topology_change_timer
,
363 static ssize_t
show_gc_timer(DEVICE_PARAMS
, char *buf
)
366 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
367 return sprintf(buf
, "%ld\n", br_timer_value(&br
->gc_timer
));
369 return sprintf(buf
, "%d\n", 0);
372 static DP_DEVICE_ATTR(gc_timer
, S_IRUGO
, show_gc_timer
, NULL
);
374 static ssize_t
show_group_addr(DEVICE_PARAMS
, char *buf
)
377 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
378 return sprintf(buf
, "%x:%x:%x:%x:%x:%x\n",
379 br
->group_addr
[0], br
->group_addr
[1],
380 br
->group_addr
[2], br
->group_addr
[3],
381 br
->group_addr
[4], br
->group_addr
[5]);
383 return sprintf(buf
, "00:01:02:03:04:05\n");
387 static ssize_t
store_group_addr(DEVICE_PARAMS
,
388 const char *buf
, size_t len
)
390 struct datapath
*dp
= dp_dev_get_dp(to_net_dev(d
));
392 unsigned new_addr
[6];
395 if (!capable(CAP_NET_ADMIN
))
398 if (sscanf(buf
, "%x:%x:%x:%x:%x:%x",
399 &new_addr
[0], &new_addr
[1], &new_addr
[2],
400 &new_addr
[3], &new_addr
[4], &new_addr
[5]) != 6)
403 /* Must be 01:80:c2:00:00:0X */
404 for (i
= 0; i
< 5; i
++)
405 if (new_addr
[i
] != br_group_address
[i
])
408 if (new_addr
[5] & ~0xf)
411 if (new_addr
[5] == 1 /* 802.3x Pause address */
412 || new_addr
[5] == 2 /* 802.3ad Slow protocols */
413 || new_addr
[5] == 3) /* 802.1X PAE address */
416 spin_lock_bh(&br
->lock
);
417 for (i
= 0; i
< 6; i
++)
418 br
->group_addr
[i
] = new_addr
[i
];
419 spin_unlock_bh(&br
->lock
);
421 printk("%s: xxx attempt to store_group_addr()\n", dp_name(dp
));
426 static DP_DEVICE_ATTR(group_addr
, S_IRUGO
| S_IWUSR
,
427 show_group_addr
, store_group_addr
);
429 static struct attribute
*bridge_attrs
[] = {
430 &DEV_ATTR(forward_delay
).attr
,
431 &DEV_ATTR(hello_time
).attr
,
432 &DEV_ATTR(max_age
).attr
,
433 &DEV_ATTR(ageing_time
).attr
,
434 &DEV_ATTR(stp_state
).attr
,
435 &DEV_ATTR(priority
).attr
,
436 &DEV_ATTR(bridge_id
).attr
,
437 &DEV_ATTR(root_id
).attr
,
438 &DEV_ATTR(root_path_cost
).attr
,
439 &DEV_ATTR(root_port
).attr
,
440 &DEV_ATTR(topology_change
).attr
,
441 &DEV_ATTR(topology_change_detected
).attr
,
442 &DEV_ATTR(hello_timer
).attr
,
443 &DEV_ATTR(tcn_timer
).attr
,
444 &DEV_ATTR(topology_change_timer
).attr
,
445 &DEV_ATTR(gc_timer
).attr
,
446 &DEV_ATTR(group_addr
).attr
,
450 static struct attribute_group bridge_group
= {
451 .name
= SYSFS_BRIDGE_ATTR
, /* "bridge" */
452 .attrs
= bridge_attrs
,
456 * Add entries in sysfs onto the existing network class device
458 * Adds a attribute group "bridge" containing tuning parameters.
459 * Sub directory to hold links to interfaces.
461 * Note: the ifobj exists only to be a subdirectory
462 * to hold links. The ifobj exists in the same data structure
463 * as its parent the bridge so reference counting works.
465 int dp_sysfs_add_dp(struct datapath
*dp
)
467 struct kobject
*kobj
= &dp
->ports
[ODPP_LOCAL
]->dev
->NETDEV_DEV_MEMBER
.kobj
;
470 /* Create /sys/class/net/<devname>/bridge directory. */
471 err
= sysfs_create_group(kobj
, &bridge_group
);
473 pr_info("%s: can't create group %s/%s\n",
474 __func__
, dp_name(dp
), bridge_group
.name
);
478 /* Create /sys/class/net/<devname>/brif directory. */
479 err
= kobject_add(&dp
->ifobj
, kobj
, SYSFS_BRIDGE_PORT_SUBDIR
);
481 pr_info("%s: can't add kobject (directory) %s/%s\n",
482 __FUNCTION__
, dp_name(dp
), kobject_name(&dp
->ifobj
));
485 kobject_uevent(&dp
->ifobj
, KOBJ_ADD
);
489 sysfs_remove_group(kobj
, &bridge_group
);
494 int dp_sysfs_del_dp(struct datapath
*dp
)
496 struct kobject
*kobj
= &dp
->ports
[ODPP_LOCAL
]->dev
->NETDEV_DEV_MEMBER
.kobj
;
498 kobject_del(&dp
->ifobj
);
499 sysfs_remove_group(kobj
, &bridge_group
);
503 #else /* !CONFIG_SYSFS */
504 int dp_sysfs_add_dp(struct datapath
*dp
) { return 0; }
505 int dp_sysfs_del_dp(struct datapath
*dp
) { return 0; }
506 int dp_sysfs_add_if(struct net_bridge_port
*p
) { return 0; }
507 int dp_sysfs_del_if(struct net_bridge_port
*p
) { return 0; }
508 #endif /* !CONFIG_SYSFS */