treewide: Remove trailing whitespace
[sliver-openvswitch.git] / datapath / dp_sysfs_dp.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #include <linux/version.h>
10
11 /*
12  *      Sysfs attributes of bridge for Open vSwitch
13  *
14  *  This has been shamelessly copied from the kernel sources.
15  */
16
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/version.h>
24
25 #include "dp_sysfs.h"
26 #include "datapath.h"
27 #include "vport-internal_dev.h"
28
29 #ifdef CONFIG_SYSFS
30
31 /* Hack to attempt to build on more platforms. */
32 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)
33 #define INTERNAL_DEVICE_ATTR CLASS_DEVICE_ATTR
34 #define DEVICE_PARAMS struct class_device *d
35 #define DEVICE_ARGS d
36 #define DEV_ATTR(NAME) class_device_attr_##NAME
37 #else
38 #define INTERNAL_DEVICE_ATTR DEVICE_ATTR
39 #define DEVICE_PARAMS struct device *d, struct device_attribute *attr
40 #define DEVICE_ARGS d, attr
41 #define DEV_ATTR(NAME) dev_attr_##NAME
42 #endif
43
44 struct datapath *sysfs_get_dp(struct net_device *netdev)
45 {
46         struct vport *vport = internal_dev_get_vport(netdev);
47         struct dp_port *dp_port;
48
49         if (!vport)
50                 return NULL;
51
52         dp_port = vport_get_dp_port(vport);
53         if (!dp_port)
54                 return NULL;
55
56         return dp_port->dp;
57 }
58 /*
59  * Common code for storing bridge parameters.
60  */
61 static ssize_t store_bridge_parm(DEVICE_PARAMS,
62                                  const char *buf, size_t len,
63                                  void (*set)(struct datapath *, unsigned long))
64 {
65         char *endp;
66         unsigned long val;
67         ssize_t result = len;
68
69         if (!capable(CAP_NET_ADMIN))
70                 return -EPERM;
71
72         val = simple_strtoul(buf, &endp, 0);
73         if (endp == buf)
74                 return -EINVAL;
75
76         /* xxx We use a default value of 0 for all fields.  If the caller is
77          * xxx attempting to set the value to our default, just silently
78          * xxx ignore the request.
79          */
80         if (val != 0) {
81                 struct datapath *dp;
82
83                 rcu_read_lock();
84
85                 dp = sysfs_get_dp(to_net_dev(d));
86                 if (dp)
87                         printk("%s: xxx writing dp parms not supported yet!\n",
88                                dp_name(dp));
89                 else
90                         result = -ENODEV;
91
92                 rcu_read_unlock();
93         }
94
95         return result;
96 }
97
98
99 static ssize_t show_forward_delay(DEVICE_PARAMS, char *buf)
100 {
101         return sprintf(buf, "%d\n", 0);
102 }
103
104 static void set_forward_delay(struct datapath *dp, unsigned long val)
105 {
106         printk("%s: xxx attempt to set_forward_delay()\n", dp_name(dp));
107 }
108
109 static ssize_t store_forward_delay(DEVICE_PARAMS,
110                                    const char *buf, size_t len)
111 {
112         return store_bridge_parm(DEVICE_ARGS, buf, len, set_forward_delay);
113 }
114 static INTERNAL_DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
115                    show_forward_delay, store_forward_delay);
116
117 static ssize_t show_hello_time(DEVICE_PARAMS, char *buf)
118 {
119         return sprintf(buf, "%d\n", 0);
120 }
121
122 static void set_hello_time(struct datapath *dp, unsigned long val)
123 {
124         printk("%s: xxx attempt to set_hello_time()\n", dp_name(dp));
125 }
126
127 static ssize_t store_hello_time(DEVICE_PARAMS,
128                                 const char *buf,
129                                 size_t len)
130 {
131         return store_bridge_parm(DEVICE_ARGS, buf, len, set_hello_time);
132 }
133 static INTERNAL_DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
134                    store_hello_time);
135
136 static ssize_t show_max_age(DEVICE_PARAMS, char *buf)
137 {
138         return sprintf(buf, "%d\n", 0);
139 }
140
141 static void set_max_age(struct datapath *dp, unsigned long val)
142 {
143         printk("%s: xxx attempt to set_max_age()\n", dp_name(dp));
144 }
145
146 static ssize_t store_max_age(DEVICE_PARAMS,
147                              const char *buf, size_t len)
148 {
149         return store_bridge_parm(DEVICE_ARGS, buf, len, set_max_age);
150 }
151 static INTERNAL_DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age, store_max_age);
152
153 static ssize_t show_ageing_time(DEVICE_PARAMS, char *buf)
154 {
155         return sprintf(buf, "%d\n", 0);
156 }
157
158 static void set_ageing_time(struct datapath *dp, unsigned long val)
159 {
160         printk("%s: xxx attempt to set_ageing_time()\n", dp_name(dp));
161 }
162
163 static ssize_t store_ageing_time(DEVICE_PARAMS,
164                                  const char *buf, size_t len)
165 {
166         return store_bridge_parm(DEVICE_ARGS, buf, len, set_ageing_time);
167 }
168 static INTERNAL_DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
169                    store_ageing_time);
170
171 static ssize_t show_stp_state(DEVICE_PARAMS, char *buf)
172 {
173         return sprintf(buf, "%d\n", 0);
174 }
175
176
177 static ssize_t store_stp_state(DEVICE_PARAMS,
178                                const char *buf,
179                                size_t len)
180 {
181         struct datapath *dp;
182         ssize_t result = len;
183
184         rcu_read_lock();
185
186         dp = sysfs_get_dp(to_net_dev(d));
187         if (dp)
188                 printk("%s: xxx attempt to set_stp_state()\n", dp_name(dp));
189         else
190                 result = -ENODEV;
191
192         rcu_read_unlock();
193
194         return result;
195 }
196 static INTERNAL_DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
197                    store_stp_state);
198
199 static ssize_t show_priority(DEVICE_PARAMS, char *buf)
200 {
201         return sprintf(buf, "%d\n", 0);
202 }
203
204 static void set_priority(struct datapath *dp, unsigned long val)
205 {
206         printk("%s: xxx attempt to set_priority()\n", dp_name(dp));
207 }
208
209 static ssize_t store_priority(DEVICE_PARAMS,
210                                const char *buf, size_t len)
211 {
212         return store_bridge_parm(DEVICE_ARGS, buf, len, set_priority);
213 }
214 static INTERNAL_DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority);
215
216 static ssize_t show_root_id(DEVICE_PARAMS, char *buf)
217 {
218         return sprintf(buf, "0000.010203040506\n");
219 }
220 static INTERNAL_DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
221
222 static ssize_t show_bridge_id(DEVICE_PARAMS, char *buf)
223 {
224         struct vport *vport;
225         ssize_t result;
226
227         rcu_read_lock();
228
229         vport = internal_dev_get_vport(to_net_dev(d));
230         if (vport) {
231                 const unsigned char *addr;
232
233                 addr = vport_get_addr(vport);
234                 result = sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
235                                  0, 0, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
236         } else
237                 result = -ENODEV;
238
239         rcu_read_unlock();
240
241         return result;
242 }
243 static INTERNAL_DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
244
245 static ssize_t show_root_port(DEVICE_PARAMS, char *buf)
246 {
247         return sprintf(buf, "%d\n", 0);
248 }
249 static INTERNAL_DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
250
251 static ssize_t show_root_path_cost(DEVICE_PARAMS, char *buf)
252 {
253         return sprintf(buf, "%d\n", 0);
254 }
255 static INTERNAL_DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
256
257 static ssize_t show_topology_change(DEVICE_PARAMS, char *buf)
258 {
259         return sprintf(buf, "%d\n", 0);
260 }
261 static INTERNAL_DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
262
263 static ssize_t show_topology_change_detected(DEVICE_PARAMS, char *buf)
264 {
265         return sprintf(buf, "%d\n", 0);
266 }
267 static INTERNAL_DEVICE_ATTR(topology_change_detected, S_IRUGO,
268                    show_topology_change_detected, NULL);
269
270 static ssize_t show_hello_timer(DEVICE_PARAMS, char *buf)
271 {
272         return sprintf(buf, "%d\n", 0);
273 }
274 static INTERNAL_DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
275
276 static ssize_t show_tcn_timer(DEVICE_PARAMS, char *buf)
277 {
278         return sprintf(buf, "%d\n", 0);
279 }
280 static INTERNAL_DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
281
282 static ssize_t show_topology_change_timer(DEVICE_PARAMS, char *buf)
283 {
284         return sprintf(buf, "%d\n", 0);
285 }
286 static INTERNAL_DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer,
287                    NULL);
288
289 static ssize_t show_gc_timer(DEVICE_PARAMS, char *buf)
290 {
291         return sprintf(buf, "%d\n", 0);
292 }
293 static INTERNAL_DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
294
295 static ssize_t show_group_addr(DEVICE_PARAMS, char *buf)
296 {
297         return sprintf(buf, "00:01:02:03:04:05\n");
298 }
299
300 static ssize_t store_group_addr(DEVICE_PARAMS,
301                                 const char *buf, size_t len)
302 {
303         struct datapath *dp;
304         ssize_t result = len;
305
306         rcu_read_lock();
307
308         dp = sysfs_get_dp(to_net_dev(d));
309         if (dp)
310                 printk("%s: xxx attempt to store_group_addr()\n", dp_name(dp));
311         else
312                 result = -ENODEV;
313
314         rcu_read_unlock();
315
316         return result;
317 }
318
319 static INTERNAL_DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
320                    show_group_addr, store_group_addr);
321
322 static struct attribute *bridge_attrs[] = {
323         &DEV_ATTR(forward_delay).attr,
324         &DEV_ATTR(hello_time).attr,
325         &DEV_ATTR(max_age).attr,
326         &DEV_ATTR(ageing_time).attr,
327         &DEV_ATTR(stp_state).attr,
328         &DEV_ATTR(priority).attr,
329         &DEV_ATTR(bridge_id).attr,
330         &DEV_ATTR(root_id).attr,
331         &DEV_ATTR(root_path_cost).attr,
332         &DEV_ATTR(root_port).attr,
333         &DEV_ATTR(topology_change).attr,
334         &DEV_ATTR(topology_change_detected).attr,
335         &DEV_ATTR(hello_timer).attr,
336         &DEV_ATTR(tcn_timer).attr,
337         &DEV_ATTR(topology_change_timer).attr,
338         &DEV_ATTR(gc_timer).attr,
339         &DEV_ATTR(group_addr).attr,
340         NULL
341 };
342
343 static struct attribute_group bridge_group = {
344         .name = SYSFS_BRIDGE_ATTR, /* "bridge" */
345         .attrs = bridge_attrs,
346 };
347
348 /*
349  * Add entries in sysfs onto the existing network class device
350  * for the bridge.
351  *   Adds a attribute group "bridge" containing tuning parameters.
352  *   Sub directory to hold links to interfaces.
353  *
354  * Note: the ifobj exists only to be a subdirectory
355  *   to hold links.  The ifobj exists in the same data structure
356  *   as its parent the bridge so reference counting works.
357  */
358 int dp_sysfs_add_dp(struct datapath *dp)
359 {
360         struct kobject *kobj = vport_get_kobj(dp->ports[ODPP_LOCAL]->vport);
361         int err;
362
363         /* Create /sys/class/net/<devname>/bridge directory. */
364         err = sysfs_create_group(kobj, &bridge_group);
365         if (err) {
366                 pr_info("%s: can't create group %s/%s\n",
367                         __func__, dp_name(dp), bridge_group.name);
368                 goto out1;
369         }
370
371         /* Create /sys/class/net/<devname>/brif directory. */
372         err = kobject_add(&dp->ifobj, kobj, SYSFS_BRIDGE_PORT_SUBDIR);
373         if (err) {
374                 pr_info("%s: can't add kobject (directory) %s/%s\n",
375                         __FUNCTION__, dp_name(dp), kobject_name(&dp->ifobj));
376                 goto out2;
377         }
378         kobject_uevent(&dp->ifobj, KOBJ_ADD);
379         return 0;
380
381  out2:
382         sysfs_remove_group(kobj, &bridge_group);
383  out1:
384         return err;
385 }
386
387 int dp_sysfs_del_dp(struct datapath *dp)
388 {
389         struct kobject *kobj = vport_get_kobj(dp->ports[ODPP_LOCAL]->vport);
390
391         kobject_del(&dp->ifobj);
392         sysfs_remove_group(kobj, &bridge_group);
393
394         return 0;
395 }
396 #else /* !CONFIG_SYSFS */
397 int dp_sysfs_add_dp(struct datapath *dp) { return 0; }
398 int dp_sysfs_del_dp(struct datapath *dp) { return 0; }
399 int dp_sysfs_add_if(struct dp_port *p) { return 0; }
400 int dp_sysfs_del_if(struct dp_port *p) { return 0; }
401 #endif /* !CONFIG_SYSFS */