bridge: Turn flag change macro into a function.
[deliverable/linux.git] / net / bridge / br_sysfs_if.c
1 /*
2 * Sysfs attributes of bridge ports
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Stephen Hemminger <shemminger@osdl.org>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14 #include <linux/capability.h>
15 #include <linux/kernel.h>
16 #include <linux/netdevice.h>
17 #include <linux/if_bridge.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/spinlock.h>
20
21 #include "br_private.h"
22
23 struct brport_attribute {
24 struct attribute attr;
25 ssize_t (*show)(struct net_bridge_port *, char *);
26 int (*store)(struct net_bridge_port *, unsigned long);
27 };
28
29 #define BRPORT_ATTR(_name, _mode, _show, _store) \
30 const struct brport_attribute brport_attr_##_name = { \
31 .attr = {.name = __stringify(_name), \
32 .mode = _mode }, \
33 .show = _show, \
34 .store = _store, \
35 };
36
37 #define BRPORT_ATTR_FLAG(_name, _mask) \
38 static ssize_t show_##_name(struct net_bridge_port *p, char *buf) \
39 { \
40 return sprintf(buf, "%d\n", !!(p->flags & _mask)); \
41 } \
42 static int store_##_name(struct net_bridge_port *p, unsigned long v) \
43 { \
44 return store_flag(p, v, _mask); \
45 } \
46 static BRPORT_ATTR(_name, S_IRUGO | S_IWUSR, \
47 show_##_name, store_##_name)
48
49 static int store_flag(struct net_bridge_port *p, unsigned long v,
50 unsigned long mask)
51 {
52 unsigned long flags = p->flags;
53
54 if (v)
55 flags |= mask;
56 else
57 flags &= ~mask;
58
59 if (flags != p->flags) {
60 p->flags = flags;
61 br_ifinfo_notify(RTM_NEWLINK, p);
62 }
63 return 0;
64 }
65
66 static ssize_t show_path_cost(struct net_bridge_port *p, char *buf)
67 {
68 return sprintf(buf, "%d\n", p->path_cost);
69 }
70
71 static BRPORT_ATTR(path_cost, S_IRUGO | S_IWUSR,
72 show_path_cost, br_stp_set_path_cost);
73
74 static ssize_t show_priority(struct net_bridge_port *p, char *buf)
75 {
76 return sprintf(buf, "%d\n", p->priority);
77 }
78
79 static BRPORT_ATTR(priority, S_IRUGO | S_IWUSR,
80 show_priority, br_stp_set_port_priority);
81
82 static ssize_t show_designated_root(struct net_bridge_port *p, char *buf)
83 {
84 return br_show_bridge_id(buf, &p->designated_root);
85 }
86 static BRPORT_ATTR(designated_root, S_IRUGO, show_designated_root, NULL);
87
88 static ssize_t show_designated_bridge(struct net_bridge_port *p, char *buf)
89 {
90 return br_show_bridge_id(buf, &p->designated_bridge);
91 }
92 static BRPORT_ATTR(designated_bridge, S_IRUGO, show_designated_bridge, NULL);
93
94 static ssize_t show_designated_port(struct net_bridge_port *p, char *buf)
95 {
96 return sprintf(buf, "%d\n", p->designated_port);
97 }
98 static BRPORT_ATTR(designated_port, S_IRUGO, show_designated_port, NULL);
99
100 static ssize_t show_designated_cost(struct net_bridge_port *p, char *buf)
101 {
102 return sprintf(buf, "%d\n", p->designated_cost);
103 }
104 static BRPORT_ATTR(designated_cost, S_IRUGO, show_designated_cost, NULL);
105
106 static ssize_t show_port_id(struct net_bridge_port *p, char *buf)
107 {
108 return sprintf(buf, "0x%x\n", p->port_id);
109 }
110 static BRPORT_ATTR(port_id, S_IRUGO, show_port_id, NULL);
111
112 static ssize_t show_port_no(struct net_bridge_port *p, char *buf)
113 {
114 return sprintf(buf, "0x%x\n", p->port_no);
115 }
116
117 static BRPORT_ATTR(port_no, S_IRUGO, show_port_no, NULL);
118
119 static ssize_t show_change_ack(struct net_bridge_port *p, char *buf)
120 {
121 return sprintf(buf, "%d\n", p->topology_change_ack);
122 }
123 static BRPORT_ATTR(change_ack, S_IRUGO, show_change_ack, NULL);
124
125 static ssize_t show_config_pending(struct net_bridge_port *p, char *buf)
126 {
127 return sprintf(buf, "%d\n", p->config_pending);
128 }
129 static BRPORT_ATTR(config_pending, S_IRUGO, show_config_pending, NULL);
130
131 static ssize_t show_port_state(struct net_bridge_port *p, char *buf)
132 {
133 return sprintf(buf, "%d\n", p->state);
134 }
135 static BRPORT_ATTR(state, S_IRUGO, show_port_state, NULL);
136
137 static ssize_t show_message_age_timer(struct net_bridge_port *p,
138 char *buf)
139 {
140 return sprintf(buf, "%ld\n", br_timer_value(&p->message_age_timer));
141 }
142 static BRPORT_ATTR(message_age_timer, S_IRUGO, show_message_age_timer, NULL);
143
144 static ssize_t show_forward_delay_timer(struct net_bridge_port *p,
145 char *buf)
146 {
147 return sprintf(buf, "%ld\n", br_timer_value(&p->forward_delay_timer));
148 }
149 static BRPORT_ATTR(forward_delay_timer, S_IRUGO, show_forward_delay_timer, NULL);
150
151 static ssize_t show_hold_timer(struct net_bridge_port *p,
152 char *buf)
153 {
154 return sprintf(buf, "%ld\n", br_timer_value(&p->hold_timer));
155 }
156 static BRPORT_ATTR(hold_timer, S_IRUGO, show_hold_timer, NULL);
157
158 static int store_flush(struct net_bridge_port *p, unsigned long v)
159 {
160 br_fdb_delete_by_port(p->br, p, 0); // Don't delete local entry
161 return 0;
162 }
163 static BRPORT_ATTR(flush, S_IWUSR, NULL, store_flush);
164
165 BRPORT_ATTR_FLAG(hairpin_mode, BR_HAIRPIN_MODE);
166 BRPORT_ATTR_FLAG(bpdu_guard, BR_BPDU_GUARD);
167 BRPORT_ATTR_FLAG(root_block, BR_ROOT_BLOCK);
168 BRPORT_ATTR_FLAG(learning, BR_LEARNING);
169 BRPORT_ATTR_FLAG(unicast_flood, BR_FLOOD);
170
171 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
172 static ssize_t show_multicast_router(struct net_bridge_port *p, char *buf)
173 {
174 return sprintf(buf, "%d\n", p->multicast_router);
175 }
176
177 static int store_multicast_router(struct net_bridge_port *p,
178 unsigned long v)
179 {
180 return br_multicast_set_port_router(p, v);
181 }
182 static BRPORT_ATTR(multicast_router, S_IRUGO | S_IWUSR, show_multicast_router,
183 store_multicast_router);
184
185 BRPORT_ATTR_FLAG(multicast_fast_leave, BR_MULTICAST_FAST_LEAVE);
186 #endif
187
188 static const struct brport_attribute *brport_attrs[] = {
189 &brport_attr_path_cost,
190 &brport_attr_priority,
191 &brport_attr_port_id,
192 &brport_attr_port_no,
193 &brport_attr_designated_root,
194 &brport_attr_designated_bridge,
195 &brport_attr_designated_port,
196 &brport_attr_designated_cost,
197 &brport_attr_state,
198 &brport_attr_change_ack,
199 &brport_attr_config_pending,
200 &brport_attr_message_age_timer,
201 &brport_attr_forward_delay_timer,
202 &brport_attr_hold_timer,
203 &brport_attr_flush,
204 &brport_attr_hairpin_mode,
205 &brport_attr_bpdu_guard,
206 &brport_attr_root_block,
207 &brport_attr_learning,
208 &brport_attr_unicast_flood,
209 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
210 &brport_attr_multicast_router,
211 &brport_attr_multicast_fast_leave,
212 #endif
213 NULL
214 };
215
216 #define to_brport_attr(_at) container_of(_at, struct brport_attribute, attr)
217 #define to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
218
219 static ssize_t brport_show(struct kobject *kobj,
220 struct attribute *attr, char *buf)
221 {
222 struct brport_attribute *brport_attr = to_brport_attr(attr);
223 struct net_bridge_port *p = to_brport(kobj);
224
225 return brport_attr->show(p, buf);
226 }
227
228 static ssize_t brport_store(struct kobject *kobj,
229 struct attribute *attr,
230 const char *buf, size_t count)
231 {
232 struct brport_attribute *brport_attr = to_brport_attr(attr);
233 struct net_bridge_port *p = to_brport(kobj);
234 ssize_t ret = -EINVAL;
235 char *endp;
236 unsigned long val;
237
238 if (!ns_capable(dev_net(p->dev)->user_ns, CAP_NET_ADMIN))
239 return -EPERM;
240
241 val = simple_strtoul(buf, &endp, 0);
242 if (endp != buf) {
243 if (!rtnl_trylock())
244 return restart_syscall();
245 if (p->dev && p->br && brport_attr->store) {
246 spin_lock_bh(&p->br->lock);
247 ret = brport_attr->store(p, val);
248 spin_unlock_bh(&p->br->lock);
249 if (ret == 0)
250 ret = count;
251 }
252 rtnl_unlock();
253 }
254 return ret;
255 }
256
257 const struct sysfs_ops brport_sysfs_ops = {
258 .show = brport_show,
259 .store = brport_store,
260 };
261
262 /*
263 * Add sysfs entries to ethernet device added to a bridge.
264 * Creates a brport subdirectory with bridge attributes.
265 * Puts symlink in bridge's brif subdirectory
266 */
267 int br_sysfs_addif(struct net_bridge_port *p)
268 {
269 struct net_bridge *br = p->br;
270 const struct brport_attribute **a;
271 int err;
272
273 err = sysfs_create_link(&p->kobj, &br->dev->dev.kobj,
274 SYSFS_BRIDGE_PORT_LINK);
275 if (err)
276 return err;
277
278 for (a = brport_attrs; *a; ++a) {
279 err = sysfs_create_file(&p->kobj, &((*a)->attr));
280 if (err)
281 return err;
282 }
283
284 strlcpy(p->sysfs_name, p->dev->name, IFNAMSIZ);
285 return sysfs_create_link(br->ifobj, &p->kobj, p->sysfs_name);
286 }
287
288 /* Rename bridge's brif symlink */
289 int br_sysfs_renameif(struct net_bridge_port *p)
290 {
291 struct net_bridge *br = p->br;
292 int err;
293
294 /* If a rename fails, the rollback will cause another
295 * rename call with the existing name.
296 */
297 if (!strncmp(p->sysfs_name, p->dev->name, IFNAMSIZ))
298 return 0;
299
300 err = sysfs_rename_link(br->ifobj, &p->kobj,
301 p->sysfs_name, p->dev->name);
302 if (err)
303 netdev_notice(br->dev, "unable to rename link %s to %s",
304 p->sysfs_name, p->dev->name);
305 else
306 strlcpy(p->sysfs_name, p->dev->name, IFNAMSIZ);
307
308 return err;
309 }
This page took 0.037377 seconds and 5 git commands to generate.