Spell verb form of "set up" correctly throughout the tree.
[openvswitch] / datapath / dp_sysfs_dp.c
1 /*
2  * Copyright (c) 2009 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/spinlock.h>
24 #include <linux/times.h>
25 #include <linux/version.h>
26
27 #include "dp_sysfs.h"
28 #include "datapath.h"
29 #include "dp_dev.h"
30
31 #ifdef SUPPORT_SYSFS
32 #define to_dev(obj)     container_of(obj, struct device, kobj)
33
34 /* Hack to attempt to build on more platforms. */
35 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)
36 #define to_kobj(d) &(d)->class_dev.kobj
37 #define DP_DEVICE_ATTR CLASS_DEVICE_ATTR
38 #else
39 #define to_kobj(d) &(d)->dev.kobj
40 #define DP_DEVICE_ATTR DEVICE_ATTR
41 #endif
42
43 /*
44  * Common code for storing bridge parameters.
45  */
46 static ssize_t store_bridge_parm(struct class_device *d,
47                                  const char *buf, size_t len,
48                                  void (*set)(struct datapath *, unsigned long))
49 {
50         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
51         char *endp;
52         unsigned long val;
53
54         if (!capable(CAP_NET_ADMIN))
55                 return -EPERM;
56
57         val = simple_strtoul(buf, &endp, 0);
58         if (endp == buf)
59                 return -EINVAL;
60
61 #if 0
62         spin_lock_bh(&br->lock);
63         (*set)(br, val);
64         spin_unlock_bh(&br->lock);
65 #else
66         /* xxx We use a default value of 0 for all fields.  If the caller is
67          * xxx attempting to set the value to our default, just silently
68          * xxx ignore the request. 
69          */
70         if (val != 0) {
71                 printk("%s: xxx writing dp parms not supported yet!\n", 
72                        dp_name(dp));
73         }
74 #endif
75         return len;
76 }
77
78
79 static ssize_t show_forward_delay(struct class_device *d,
80                                   char *buf)
81 {
82 #if 0
83         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
84         return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
85 #else
86         return sprintf(buf, "%d\n", 0);
87 #endif
88 }
89
90 static void set_forward_delay(struct datapath *dp, unsigned long val)
91 {
92 #if 0
93         unsigned long delay = clock_t_to_jiffies(val);
94         br->forward_delay = delay;
95         if (br_is_root_bridge(br))
96                 br->bridge_forward_delay = delay;
97 #else
98         printk("%s: xxx attempt to set_forward_delay()\n", dp_name(dp));
99 #endif
100 }
101
102 static ssize_t store_forward_delay(struct class_device *d,
103                                    const char *buf, size_t len)
104 {
105         return store_bridge_parm(d, buf, len, set_forward_delay);
106 }
107 static DP_DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
108                    show_forward_delay, store_forward_delay);
109
110 static ssize_t show_hello_time(struct class_device *d, char *buf)
111 {
112 #if 0
113         return sprintf(buf, "%lu\n",
114                        jiffies_to_clock_t(to_bridge(d)->hello_time));
115 #else
116         return sprintf(buf, "%d\n", 0);
117 #endif
118 }
119
120 static void set_hello_time(struct datapath *dp, unsigned long val)
121 {
122 #if 0
123         unsigned long t = clock_t_to_jiffies(val);
124         br->hello_time = t;
125         if (br_is_root_bridge(br))
126                 br->bridge_hello_time = t;
127 #else
128         printk("%s: xxx attempt to set_hello_time()\n", dp_name(dp));
129 #endif
130 }
131
132 static ssize_t store_hello_time(struct class_device *d,
133                                 const char *buf,
134                                 size_t len)
135 {
136         return store_bridge_parm(d, buf, len, set_hello_time);
137 }
138 static DP_DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
139                    store_hello_time);
140
141 static ssize_t show_max_age(struct class_device *d, 
142                             char *buf)
143 {
144 #if 0
145         return sprintf(buf, "%lu\n",
146                        jiffies_to_clock_t(to_bridge(d)->max_age));
147 #else
148         return sprintf(buf, "%d\n", 0);
149 #endif
150 }
151
152 static void set_max_age(struct datapath *dp, unsigned long val)
153 {
154 #if 0
155         unsigned long t = clock_t_to_jiffies(val);
156         br->max_age = t;
157         if (br_is_root_bridge(br))
158                 br->bridge_max_age = t;
159 #else
160         printk("%s: xxx attempt to set_max_age()\n", dp_name(dp));
161 #endif
162 }
163
164 static ssize_t store_max_age(struct class_device *d, 
165                              const char *buf, size_t len)
166 {
167         return store_bridge_parm(d, buf, len, set_max_age);
168 }
169 static DP_DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age, store_max_age);
170
171 static ssize_t show_ageing_time(struct class_device *d,
172                                 char *buf)
173 {
174 #if 0
175         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
176         return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
177 #else
178         return sprintf(buf, "%d\n", 0);
179 #endif
180 }
181
182 static void set_ageing_time(struct datapath *dp, unsigned long val)
183 {
184 #if 0
185         br->ageing_time = clock_t_to_jiffies(val);
186 #else
187         printk("%s: xxx attempt to set_ageing_time()\n", dp_name(dp));
188 #endif
189 }
190
191 static ssize_t store_ageing_time(struct class_device *d,
192                                  const char *buf, size_t len)
193 {
194         return store_bridge_parm(d, buf, len, set_ageing_time);
195 }
196 static DP_DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
197                    store_ageing_time);
198
199 static ssize_t show_stp_state(struct class_device *d,
200                               char *buf)
201 {
202 #if 0
203         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
204         return sprintf(buf, "%d\n", br->stp_enabled);
205 #else
206         return sprintf(buf, "%d\n", 0);
207 #endif
208 }
209
210
211 static ssize_t store_stp_state(struct class_device *d,
212                                const char *buf,
213                                size_t len)
214 {
215         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
216 #if 0
217         char *endp;
218         unsigned long val;
219
220         if (!capable(CAP_NET_ADMIN))
221                 return -EPERM;
222
223         val = simple_strtoul(buf, &endp, 0);
224         if (endp == buf)
225                 return -EINVAL;
226
227         rtnl_lock();
228         br_stp_set_enabled(br, val);
229         rtnl_unlock();
230 #else
231         printk("%s: xxx attempt to set_stp_state()\n", dp_name(dp));
232 #endif
233
234         return len;
235 }
236 static DP_DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
237                    store_stp_state);
238
239 static ssize_t show_priority(struct class_device *d, 
240                              char *buf)
241 {
242 #if 0
243         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
244         return sprintf(buf, "%d\n",
245                        (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
246 #else
247         return sprintf(buf, "%d\n", 0);
248 #endif
249 }
250
251 static void set_priority(struct datapath *dp, unsigned long val)
252 {
253 #if 0
254         br_stp_set_bridge_priority(br, (u16) val);
255 #else
256         printk("%s: xxx attempt to set_priority()\n", dp_name(dp));
257 #endif
258 }
259
260 static ssize_t store_priority(struct class_device *d, 
261                                const char *buf, size_t len)
262 {
263         return store_bridge_parm(d, buf, len, set_priority);
264 }
265 static DP_DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority);
266
267 static ssize_t show_root_id(struct class_device *d, 
268                             char *buf)
269 {
270 #if 0
271         return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
272 #else
273         return sprintf(buf, "0000.010203040506\n");
274 #endif
275 }
276 static DP_DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
277
278 static ssize_t show_bridge_id(struct class_device *d, 
279                               char *buf)
280 {
281         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
282         const unsigned char *addr = dp->ports[ODPP_LOCAL]->dev->dev_addr;
283
284         /* xxx Do we need a lock of some sort? */
285         return sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
286                         0, 0, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
287 }
288 static DP_DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
289
290 static ssize_t show_root_port(struct class_device *d, 
291                               char *buf)
292 {
293 #if 0
294         return sprintf(buf, "%d\n", to_bridge(d)->root_port);
295 #else
296         return sprintf(buf, "%d\n", 0);
297 #endif
298 }
299 static DP_DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
300
301 static ssize_t show_root_path_cost(struct class_device *d,
302                                    char *buf)
303 {
304 #if 0
305         return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
306 #else
307         return sprintf(buf, "%d\n", 0);
308 #endif
309 }
310 static DP_DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
311
312 static ssize_t show_topology_change(struct class_device *d,
313                                     char *buf)
314 {
315 #if 0
316         return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
317 #else
318         return sprintf(buf, "%d\n", 0);
319 #endif
320 }
321 static DP_DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
322
323 static ssize_t show_topology_change_detected(struct class_device *d,
324                                              char *buf)
325 {
326 #if 0
327         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
328         return sprintf(buf, "%d\n", br->topology_change_detected);
329 #else
330         return sprintf(buf, "%d\n", 0);
331 #endif
332 }
333 static DP_DEVICE_ATTR(topology_change_detected, S_IRUGO,
334                    show_topology_change_detected, NULL);
335
336 static ssize_t show_hello_timer(struct class_device *d,
337                                 char *buf)
338 {
339 #if 0
340         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
341         return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
342 #else
343         return sprintf(buf, "%d\n", 0);
344 #endif
345 }
346 static DP_DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
347
348 static ssize_t show_tcn_timer(struct class_device *d, 
349                               char *buf)
350 {
351 #if 0
352         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
353         return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
354 #else
355         return sprintf(buf, "%d\n", 0);
356 #endif
357 }
358 static DP_DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
359
360 static ssize_t show_topology_change_timer(struct class_device *d,
361                                           char *buf)
362 {
363 #if 0
364         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
365         return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
366 #else
367         return sprintf(buf, "%d\n", 0);
368 #endif
369 }
370 static DP_DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer,
371                    NULL);
372
373 static ssize_t show_gc_timer(struct class_device *d, 
374                              char *buf)
375 {
376 #if 0
377         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
378         return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
379 #else
380         return sprintf(buf, "%d\n", 0);
381 #endif
382 }
383 static DP_DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
384
385 static ssize_t show_group_addr(struct class_device *d,
386                                char *buf)
387 {
388 #if 0
389         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
390         return sprintf(buf, "%x:%x:%x:%x:%x:%x\n",
391                        br->group_addr[0], br->group_addr[1],
392                        br->group_addr[2], br->group_addr[3],
393                        br->group_addr[4], br->group_addr[5]);
394 #else
395         return sprintf(buf, "00:01:02:03:04:05\n");
396 #endif
397 }
398
399 static ssize_t store_group_addr(struct class_device *d,
400                                 const char *buf, size_t len)
401 {
402         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
403 #if 0
404         unsigned new_addr[6];
405         int i;
406
407         if (!capable(CAP_NET_ADMIN))
408                 return -EPERM;
409
410         if (sscanf(buf, "%x:%x:%x:%x:%x:%x",
411                    &new_addr[0], &new_addr[1], &new_addr[2],
412                    &new_addr[3], &new_addr[4], &new_addr[5]) != 6)
413                 return -EINVAL;
414
415         /* Must be 01:80:c2:00:00:0X */
416         for (i = 0; i < 5; i++)
417                 if (new_addr[i] != br_group_address[i])
418                         return -EINVAL;
419
420         if (new_addr[5] & ~0xf)
421                 return -EINVAL;
422
423         if (new_addr[5] == 1    /* 802.3x Pause address */
424             || new_addr[5] == 2 /* 802.3ad Slow protocols */
425             || new_addr[5] == 3) /* 802.1X PAE address */
426                 return -EINVAL;
427
428         spin_lock_bh(&br->lock);
429         for (i = 0; i < 6; i++)
430                 br->group_addr[i] = new_addr[i];
431         spin_unlock_bh(&br->lock);
432 #else
433         printk("%s: xxx attempt to store_group_addr()\n", dp_name(dp));
434 #endif
435         return len;
436 }
437
438 static DP_DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
439                    show_group_addr, store_group_addr);
440
441 static struct attribute *bridge_attrs[] = {
442         &class_device_attr_forward_delay.attr,
443         &class_device_attr_hello_time.attr,
444         &class_device_attr_max_age.attr,
445         &class_device_attr_ageing_time.attr,
446         &class_device_attr_stp_state.attr,
447         &class_device_attr_priority.attr,
448         &class_device_attr_bridge_id.attr,
449         &class_device_attr_root_id.attr,
450         &class_device_attr_root_path_cost.attr,
451         &class_device_attr_root_port.attr,
452         &class_device_attr_topology_change.attr,
453         &class_device_attr_topology_change_detected.attr,
454         &class_device_attr_hello_timer.attr,
455         &class_device_attr_tcn_timer.attr,
456         &class_device_attr_topology_change_timer.attr,
457         &class_device_attr_gc_timer.attr,
458         &class_device_attr_group_addr.attr,
459         NULL
460 };
461
462 static struct attribute_group bridge_group = {
463         .name = SYSFS_BRIDGE_ATTR, /* "bridge" */
464         .attrs = bridge_attrs,
465 };
466
467 /*
468  * Add entries in sysfs onto the existing network class device
469  * for the bridge.
470  *   Adds a attribute group "bridge" containing tuning parameters.
471  *   Sub directory to hold links to interfaces.
472  *
473  * Note: the ifobj exists only to be a subdirectory
474  *   to hold links.  The ifobj exists in the same data structure
475  *   as its parent the bridge so reference counting works.
476  */
477 int dp_sysfs_add_dp(struct datapath *dp)
478 {
479         struct kobject *kobj = to_kobj(dp->ports[ODPP_LOCAL]->dev);
480         int err;
481
482         /* Create /sys/class/net/<devname>/bridge directory. */
483         err = sysfs_create_group(kobj, &bridge_group);
484         if (err) {
485                 pr_info("%s: can't create group %s/%s\n",
486                         __func__, dp_name(dp), bridge_group.name);
487                 goto out1;
488         }
489
490         /* Create /sys/class/net/<devname>/brif directory. */
491         dp->ifobj.parent = kobj;
492         err = kobject_add(&dp->ifobj);
493         if (err) {
494                 pr_info("%s: can't add kobject (directory) %s/%s\n",
495                                 __FUNCTION__, dp_name(dp), dp->ifobj.name);
496                 goto out2;
497         }
498         kobject_uevent(&dp->ifobj, KOBJ_ADD);
499         return 0;
500
501  out2:
502         sysfs_remove_group(kobj, &bridge_group);
503  out1:
504         return err;
505 }
506
507 int dp_sysfs_del_dp(struct datapath *dp)
508 {
509         struct kobject *kobj = to_kobj(dp->ports[ODPP_LOCAL]->dev);
510
511         kobject_del(&dp->ifobj);
512         sysfs_remove_group(kobj, &bridge_group);
513
514         return 0;
515 }
516 #else /* !SUPPORT_SYSFS */
517 int dp_sysfs_add_dp(struct datapath *dp) { return 0; }
518 int dp_sysfs_del_dp(struct datapath *dp) { return 0; }
519 int dp_sysfs_add_if(struct net_bridge_port *p) { return 0; }
520 int dp_sysfs_del_if(struct net_bridge_port *p)
521 {
522         dev_put(p->dev);
523         kfree(p);
524         return 0;
525 }
526 #endif /* !SUPPORT_SYSFS */