Merge commit 'origin/citrix'
[sliver-openvswitch.git] / 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 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 DP_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 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
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 DP_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 DP_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 DP_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 DP_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 DP_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 DP_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 DP_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 DP_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 DP_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 DP_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 DP_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 DP_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 DP_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 DP_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 DP_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 DP_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 DP_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, /* "bridge" */
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 dp_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         /* Create /sys/class/net/<devname>/bridge directory. */
471         err = sysfs_create_group(kobj, &bridge_group);
472         if (err) {
473                 pr_info("%s: can't create group %s/%s\n",
474                         __func__, dp_name(dp), bridge_group.name);
475                 goto out1;
476         }
477
478         /* Create /sys/class/net/<devname>/brif directory. */
479         err = kobject_add(&dp->ifobj, kobj, SYSFS_BRIDGE_PORT_SUBDIR);
480         if (err) {
481                 pr_info("%s: can't add kobject (directory) %s/%s\n",
482                         __FUNCTION__, dp_name(dp), kobject_name(&dp->ifobj));
483                 goto out2;
484         }
485         kobject_uevent(&dp->ifobj, KOBJ_ADD);
486         return 0;
487
488  out2:
489         sysfs_remove_group(kobj, &bridge_group);
490  out1:
491         return err;
492 }
493
494 int dp_sysfs_del_dp(struct datapath *dp)
495 {
496         struct kobject *kobj = &dp->ports[ODPP_LOCAL]->dev->NETDEV_DEV_MEMBER.kobj;
497
498         kobject_del(&dp->ifobj);
499         sysfs_remove_group(kobj, &bridge_group);
500
501         return 0;
502 }
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 */