xenserver: Store XAPI dbcache as XML in interface-reconfigure.
[sliver-openvswitch.git] / datapath / brc_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 "brc_sysfs.h"
28 #include "datapath.h"
29 #include "dp_dev.h"
30
31 #ifdef CONFIG_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 BRC_DEVICE_ATTR CLASS_DEVICE_ATTR
37 #define DEVICE_PARAMS struct class_device *d
38 #define DEVICE_ARGS d
39 #define DEV_ATTR(NAME) class_device_attr_##NAME
40 #else
41 #define BRC_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
45 #endif
46
47 /*
48  * Common code for storing bridge parameters.
49  */
50 static ssize_t store_bridge_parm(DEVICE_PARAMS,
51                                  const char *buf, size_t len,
52                                  void (*set)(struct datapath *, unsigned long))
53 {
54         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
55         char *endp;
56         unsigned long val;
57
58         if (!capable(CAP_NET_ADMIN))
59                 return -EPERM;
60
61         val = simple_strtoul(buf, &endp, 0);
62         if (endp == buf)
63                 return -EINVAL;
64
65 #if 0
66         spin_lock_bh(&br->lock);
67         (*set)(br, val);
68         spin_unlock_bh(&br->lock);
69 #else
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. 
73          */
74         if (val != 0) {
75                 printk("%s: xxx writing dp parms not supported yet!\n", 
76                        dp_name(dp));
77         }
78 #endif
79         return len;
80 }
81
82
83 static ssize_t show_forward_delay(DEVICE_PARAMS, char *buf)
84 {
85 #if 0
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));
88 #else
89         return sprintf(buf, "%d\n", 0);
90 #endif
91 }
92
93 static void set_forward_delay(struct datapath *dp, unsigned long val)
94 {
95 #if 0
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;
100 #else
101         printk("%s: xxx attempt to set_forward_delay()\n", dp_name(dp));
102 #endif
103 }
104
105 static ssize_t store_forward_delay(DEVICE_PARAMS,
106                                    const char *buf, size_t len)
107 {
108         return store_bridge_parm(DEVICE_ARGS, buf, len, set_forward_delay);
109 }
110 static BRC_DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
111                    show_forward_delay, store_forward_delay);
112
113 static ssize_t show_hello_time(DEVICE_PARAMS, char *buf)
114 {
115 #if 0
116         return sprintf(buf, "%lu\n",
117                        jiffies_to_clock_t(to_bridge(d)->hello_time));
118 #else
119         return sprintf(buf, "%d\n", 0);
120 #endif
121 }
122
123 static void set_hello_time(struct datapath *dp, unsigned long val)
124 {
125 #if 0
126         unsigned long t = clock_t_to_jiffies(val);
127         br->hello_time = t;
128         if (br_is_root_bridge(br))
129                 br->bridge_hello_time = t;
130 #else
131         printk("%s: xxx attempt to set_hello_time()\n", dp_name(dp));
132 #endif
133 }
134
135 static ssize_t store_hello_time(DEVICE_PARAMS,
136                                 const char *buf,
137                                 size_t len)
138 {
139         return store_bridge_parm(DEVICE_ARGS, buf, len, set_hello_time);
140 }
141 static BRC_DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
142                    store_hello_time);
143
144 static ssize_t show_max_age(DEVICE_PARAMS, char *buf)
145 {
146 #if 0
147         return sprintf(buf, "%lu\n",
148                        jiffies_to_clock_t(to_bridge(d)->max_age));
149 #else
150         return sprintf(buf, "%d\n", 0);
151 #endif
152 }
153
154 static void set_max_age(struct datapath *dp, unsigned long val)
155 {
156 #if 0
157         unsigned long t = clock_t_to_jiffies(val);
158         br->max_age = t;
159         if (br_is_root_bridge(br))
160                 br->bridge_max_age = t;
161 #else
162         printk("%s: xxx attempt to set_max_age()\n", dp_name(dp));
163 #endif
164 }
165
166 static ssize_t store_max_age(DEVICE_PARAMS,
167                              const char *buf, size_t len)
168 {
169         return store_bridge_parm(DEVICE_ARGS, buf, len, set_max_age);
170 }
171 static BRC_DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age, store_max_age);
172
173 static ssize_t show_ageing_time(DEVICE_PARAMS, char *buf)
174 {
175 #if 0
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));
178 #else
179         return sprintf(buf, "%d\n", 0);
180 #endif
181 }
182
183 static void set_ageing_time(struct datapath *dp, unsigned long val)
184 {
185 #if 0
186         br->ageing_time = clock_t_to_jiffies(val);
187 #else
188         printk("%s: xxx attempt to set_ageing_time()\n", dp_name(dp));
189 #endif
190 }
191
192 static ssize_t store_ageing_time(DEVICE_PARAMS,
193                                  const char *buf, size_t len)
194 {
195         return store_bridge_parm(DEVICE_ARGS, buf, len, set_ageing_time);
196 }
197 static BRC_DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
198                    store_ageing_time);
199
200 static ssize_t show_stp_state(DEVICE_PARAMS, 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(DEVICE_PARAMS,
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 BRC_DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
237                    store_stp_state);
238
239 static ssize_t show_priority(DEVICE_PARAMS, char *buf)
240 {
241 #if 0
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]);
245 #else
246         return sprintf(buf, "%d\n", 0);
247 #endif
248 }
249
250 static void set_priority(struct datapath *dp, unsigned long val)
251 {
252 #if 0
253         br_stp_set_bridge_priority(br, (u16) val);
254 #else
255         printk("%s: xxx attempt to set_priority()\n", dp_name(dp));
256 #endif
257 }
258
259 static ssize_t store_priority(DEVICE_PARAMS,
260                                const char *buf, size_t len)
261 {
262         return store_bridge_parm(DEVICE_ARGS, buf, len, set_priority);
263 }
264 static BRC_DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority);
265
266 static ssize_t show_root_id(DEVICE_PARAMS, char *buf)
267 {
268 #if 0
269         return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
270 #else
271         return sprintf(buf, "0000.010203040506\n");
272 #endif
273 }
274 static BRC_DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
275
276 static ssize_t show_bridge_id(DEVICE_PARAMS, char *buf)
277 {
278         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
279         const unsigned char *addr = dp->ports[ODPP_LOCAL]->dev->dev_addr;
280
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]);
284 }
285 static BRC_DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
286
287 static ssize_t show_root_port(DEVICE_PARAMS, char *buf)
288 {
289 #if 0
290         return sprintf(buf, "%d\n", to_bridge(d)->root_port);
291 #else
292         return sprintf(buf, "%d\n", 0);
293 #endif
294 }
295 static BRC_DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
296
297 static ssize_t show_root_path_cost(DEVICE_PARAMS, char *buf)
298 {
299 #if 0
300         return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
301 #else
302         return sprintf(buf, "%d\n", 0);
303 #endif
304 }
305 static BRC_DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
306
307 static ssize_t show_topology_change(DEVICE_PARAMS, char *buf)
308 {
309 #if 0
310         return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
311 #else
312         return sprintf(buf, "%d\n", 0);
313 #endif
314 }
315 static BRC_DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
316
317 static ssize_t show_topology_change_detected(DEVICE_PARAMS, char *buf)
318 {
319 #if 0
320         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
321         return sprintf(buf, "%d\n", br->topology_change_detected);
322 #else
323         return sprintf(buf, "%d\n", 0);
324 #endif
325 }
326 static BRC_DEVICE_ATTR(topology_change_detected, S_IRUGO,
327                    show_topology_change_detected, NULL);
328
329 static ssize_t show_hello_timer(DEVICE_PARAMS, char *buf)
330 {
331 #if 0
332         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
333         return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
334 #else
335         return sprintf(buf, "%d\n", 0);
336 #endif
337 }
338 static BRC_DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
339
340 static ssize_t show_tcn_timer(DEVICE_PARAMS, char *buf)
341 {
342 #if 0
343         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
344         return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
345 #else
346         return sprintf(buf, "%d\n", 0);
347 #endif
348 }
349 static BRC_DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
350
351 static ssize_t show_topology_change_timer(DEVICE_PARAMS, char *buf)
352 {
353 #if 0
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));
356 #else
357         return sprintf(buf, "%d\n", 0);
358 #endif
359 }
360 static BRC_DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer,
361                    NULL);
362
363 static ssize_t show_gc_timer(DEVICE_PARAMS, char *buf)
364 {
365 #if 0
366         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
367         return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
368 #else
369         return sprintf(buf, "%d\n", 0);
370 #endif
371 }
372 static BRC_DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
373
374 static ssize_t show_group_addr(DEVICE_PARAMS, char *buf)
375 {
376 #if 0
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]);
382 #else
383         return sprintf(buf, "00:01:02:03:04:05\n");
384 #endif
385 }
386
387 static ssize_t store_group_addr(DEVICE_PARAMS,
388                                 const char *buf, size_t len)
389 {
390         struct datapath *dp = dp_dev_get_dp(to_net_dev(d));
391 #if 0
392         unsigned new_addr[6];
393         int i;
394
395         if (!capable(CAP_NET_ADMIN))
396                 return -EPERM;
397
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)
401                 return -EINVAL;
402
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])
406                         return -EINVAL;
407
408         if (new_addr[5] & ~0xf)
409                 return -EINVAL;
410
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 */
414                 return -EINVAL;
415
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);
420 #else
421         printk("%s: xxx attempt to store_group_addr()\n", dp_name(dp));
422 #endif
423         return len;
424 }
425
426 static BRC_DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
427                    show_group_addr, store_group_addr);
428
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,
447         NULL
448 };
449
450 static struct attribute_group bridge_group = {
451         .name = SYSFS_BRIDGE_ATTR,
452         .attrs = bridge_attrs,
453 };
454
455 /*
456  * Add entries in sysfs onto the existing network class device
457  * for the bridge.
458  *   Adds a attribute group "bridge" containing tuning parameters.
459  *   Sub directory to hold links to interfaces.
460  *
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.
464  */
465 int brc_sysfs_add_dp(struct datapath *dp)
466 {
467         struct kobject *kobj = &dp->ports[ODPP_LOCAL]->dev->NETDEV_DEV_MEMBER.kobj;
468         int err;
469
470         err = sysfs_create_group(kobj, &bridge_group);
471         if (err) {
472                 pr_info("%s: can't create group %s/%s\n",
473                         __func__, dp_name(dp), bridge_group.name);
474                 goto out1;
475         }
476
477 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
478         kobject_set_name(&dp->ifobj, SYSFS_BRIDGE_PORT_SUBDIR);
479         dp->ifobj.ktype = NULL;
480         dp->ifobj.kset = NULL;
481         dp->ifobj.parent = kobj;
482
483         err = kobject_register(&dp->ifobj);
484         if (err) {
485                 pr_info("%s: can't add kobject (directory) %s/%s\n",
486                         __FUNCTION__, dp_name(dp), kobject_name(&dp->ifobj));
487                 goto out2;
488         }
489 #else
490         dp->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, kobj);
491         if (!dp->ifobj) {
492                 pr_info("%s: can't add kobject (directory) %s/%s\n",
493                         __func__, dp_name(dp), SYSFS_BRIDGE_PORT_SUBDIR);
494                 goto out2;
495         }
496 #endif
497         return 0;
498
499  out2:
500         sysfs_remove_group(kobj, &bridge_group);
501  out1:
502         return err;
503 }
504
505 int brc_sysfs_del_dp(struct datapath *dp)
506 {
507         struct kobject *kobj = &dp->ports[ODPP_LOCAL]->dev->NETDEV_DEV_MEMBER.kobj;
508
509 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
510         kobject_unregister(&dp->ifobj);
511 #else 
512         kobject_put(dp->ifobj);
513 #endif
514         sysfs_remove_group(kobj, &bridge_group);
515
516         return 0;
517 }
518 #else /* !CONFIG_SYSFS */
519 int brc_sysfs_add_dp(struct datapath *dp) { return 0; }
520 int brc_sysfs_del_dp(struct datapath *dp) { return 0; }
521 int brc_sysfs_add_if(struct net_bridge_port *p) { return 0; }
522 int brc_sysfs_del_if(struct net_bridge_port *p)
523 {
524         dev_put(p->dev);
525         kfree(p);
526         return 0;
527 }
528 #endif /* !CONFIG_SYSFS */