datapath: Remove dead code.
[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         return vport_get_dp_port(internal_dev_get_vport(netdev))->dp;
47 }
48
49 /*
50  * Common code for storing bridge parameters.
51  */
52 static ssize_t store_bridge_parm(DEVICE_PARAMS,
53                                  const char *buf, size_t len,
54                                  void (*set)(struct datapath *, unsigned long))
55 {
56         struct datapath *dp = sysfs_get_dp(to_net_dev(d));
57         char *endp;
58         unsigned long val;
59
60         if (!capable(CAP_NET_ADMIN))
61                 return -EPERM;
62
63         val = simple_strtoul(buf, &endp, 0);
64         if (endp == buf)
65                 return -EINVAL;
66
67         /* xxx We use a default value of 0 for all fields.  If the caller is
68          * xxx attempting to set the value to our default, just silently
69          * xxx ignore the request. 
70          */
71         if (val != 0) {
72                 printk("%s: xxx writing dp parms not supported yet!\n", 
73                        dp_name(dp));
74         }
75
76         return len;
77 }
78
79
80 static ssize_t show_forward_delay(DEVICE_PARAMS, char *buf)
81 {
82         return sprintf(buf, "%d\n", 0);
83 }
84
85 static void set_forward_delay(struct datapath *dp, unsigned long val)
86 {
87         printk("%s: xxx attempt to set_forward_delay()\n", dp_name(dp));
88 }
89
90 static ssize_t store_forward_delay(DEVICE_PARAMS,
91                                    const char *buf, size_t len)
92 {
93         return store_bridge_parm(DEVICE_ARGS, buf, len, set_forward_delay);
94 }
95 static INTERNAL_DEVICE_ATTR(forward_delay, S_IRUGO | S_IWUSR,
96                    show_forward_delay, store_forward_delay);
97
98 static ssize_t show_hello_time(DEVICE_PARAMS, char *buf)
99 {
100         return sprintf(buf, "%d\n", 0);
101 }
102
103 static void set_hello_time(struct datapath *dp, unsigned long val)
104 {
105         printk("%s: xxx attempt to set_hello_time()\n", dp_name(dp));
106 }
107
108 static ssize_t store_hello_time(DEVICE_PARAMS,
109                                 const char *buf,
110                                 size_t len)
111 {
112         return store_bridge_parm(DEVICE_ARGS, buf, len, set_hello_time);
113 }
114 static INTERNAL_DEVICE_ATTR(hello_time, S_IRUGO | S_IWUSR, show_hello_time,
115                    store_hello_time);
116
117 static ssize_t show_max_age(DEVICE_PARAMS, char *buf)
118 {
119         return sprintf(buf, "%d\n", 0);
120 }
121
122 static void set_max_age(struct datapath *dp, unsigned long val)
123 {
124         printk("%s: xxx attempt to set_max_age()\n", dp_name(dp));
125 }
126
127 static ssize_t store_max_age(DEVICE_PARAMS,
128                              const char *buf, size_t len)
129 {
130         return store_bridge_parm(DEVICE_ARGS, buf, len, set_max_age);
131 }
132 static INTERNAL_DEVICE_ATTR(max_age, S_IRUGO | S_IWUSR, show_max_age, store_max_age);
133
134 static ssize_t show_ageing_time(DEVICE_PARAMS, char *buf)
135 {
136         return sprintf(buf, "%d\n", 0);
137 }
138
139 static void set_ageing_time(struct datapath *dp, unsigned long val)
140 {
141         printk("%s: xxx attempt to set_ageing_time()\n", dp_name(dp));
142 }
143
144 static ssize_t store_ageing_time(DEVICE_PARAMS,
145                                  const char *buf, size_t len)
146 {
147         return store_bridge_parm(DEVICE_ARGS, buf, len, set_ageing_time);
148 }
149 static INTERNAL_DEVICE_ATTR(ageing_time, S_IRUGO | S_IWUSR, show_ageing_time,
150                    store_ageing_time);
151
152 static ssize_t show_stp_state(DEVICE_PARAMS, char *buf)
153 {
154         return sprintf(buf, "%d\n", 0);
155 }
156
157
158 static ssize_t store_stp_state(DEVICE_PARAMS,
159                                const char *buf,
160                                size_t len)
161 {
162         struct datapath *dp = sysfs_get_dp(to_net_dev(d));
163
164         printk("%s: xxx attempt to set_stp_state()\n", dp_name(dp));
165
166         return len;
167 }
168 static INTERNAL_DEVICE_ATTR(stp_state, S_IRUGO | S_IWUSR, show_stp_state,
169                    store_stp_state);
170
171 static ssize_t show_priority(DEVICE_PARAMS, char *buf)
172 {
173         return sprintf(buf, "%d\n", 0);
174 }
175
176 static void set_priority(struct datapath *dp, unsigned long val)
177 {
178         printk("%s: xxx attempt to set_priority()\n", dp_name(dp));
179 }
180
181 static ssize_t store_priority(DEVICE_PARAMS,
182                                const char *buf, size_t len)
183 {
184         return store_bridge_parm(DEVICE_ARGS, buf, len, set_priority);
185 }
186 static INTERNAL_DEVICE_ATTR(priority, S_IRUGO | S_IWUSR, show_priority, store_priority);
187
188 static ssize_t show_root_id(DEVICE_PARAMS, char *buf)
189 {
190         return sprintf(buf, "0000.010203040506\n");
191 }
192 static INTERNAL_DEVICE_ATTR(root_id, S_IRUGO, show_root_id, NULL);
193
194 static ssize_t show_bridge_id(DEVICE_PARAMS, char *buf)
195 {
196         struct datapath *dp = sysfs_get_dp(to_net_dev(d));
197         const unsigned char *addr = vport_get_addr(dp->ports[ODPP_LOCAL]->vport);
198
199         /* xxx Do we need a lock of some sort? */
200         return sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
201                         0, 0, addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
202 }
203 static INTERNAL_DEVICE_ATTR(bridge_id, S_IRUGO, show_bridge_id, NULL);
204
205 static ssize_t show_root_port(DEVICE_PARAMS, char *buf)
206 {
207         return sprintf(buf, "%d\n", 0);
208 }
209 static INTERNAL_DEVICE_ATTR(root_port, S_IRUGO, show_root_port, NULL);
210
211 static ssize_t show_root_path_cost(DEVICE_PARAMS, char *buf)
212 {
213         return sprintf(buf, "%d\n", 0);
214 }
215 static INTERNAL_DEVICE_ATTR(root_path_cost, S_IRUGO, show_root_path_cost, NULL);
216
217 static ssize_t show_topology_change(DEVICE_PARAMS, char *buf)
218 {
219         return sprintf(buf, "%d\n", 0);
220 }
221 static INTERNAL_DEVICE_ATTR(topology_change, S_IRUGO, show_topology_change, NULL);
222
223 static ssize_t show_topology_change_detected(DEVICE_PARAMS, char *buf)
224 {
225         return sprintf(buf, "%d\n", 0);
226 }
227 static INTERNAL_DEVICE_ATTR(topology_change_detected, S_IRUGO,
228                    show_topology_change_detected, NULL);
229
230 static ssize_t show_hello_timer(DEVICE_PARAMS, char *buf)
231 {
232         return sprintf(buf, "%d\n", 0);
233 }
234 static INTERNAL_DEVICE_ATTR(hello_timer, S_IRUGO, show_hello_timer, NULL);
235
236 static ssize_t show_tcn_timer(DEVICE_PARAMS, char *buf)
237 {
238         return sprintf(buf, "%d\n", 0);
239 }
240 static INTERNAL_DEVICE_ATTR(tcn_timer, S_IRUGO, show_tcn_timer, NULL);
241
242 static ssize_t show_topology_change_timer(DEVICE_PARAMS, char *buf)
243 {
244         return sprintf(buf, "%d\n", 0);
245 }
246 static INTERNAL_DEVICE_ATTR(topology_change_timer, S_IRUGO, show_topology_change_timer,
247                    NULL);
248
249 static ssize_t show_gc_timer(DEVICE_PARAMS, char *buf)
250 {
251         return sprintf(buf, "%d\n", 0);
252 }
253 static INTERNAL_DEVICE_ATTR(gc_timer, S_IRUGO, show_gc_timer, NULL);
254
255 static ssize_t show_group_addr(DEVICE_PARAMS, char *buf)
256 {
257         return sprintf(buf, "00:01:02:03:04:05\n");
258 }
259
260 static ssize_t store_group_addr(DEVICE_PARAMS,
261                                 const char *buf, size_t len)
262 {
263         struct datapath *dp = sysfs_get_dp(to_net_dev(d));
264
265         printk("%s: xxx attempt to store_group_addr()\n", dp_name(dp));
266         return len;
267 }
268
269 static INTERNAL_DEVICE_ATTR(group_addr, S_IRUGO | S_IWUSR,
270                    show_group_addr, store_group_addr);
271
272 static struct attribute *bridge_attrs[] = {
273         &DEV_ATTR(forward_delay).attr,
274         &DEV_ATTR(hello_time).attr,
275         &DEV_ATTR(max_age).attr,
276         &DEV_ATTR(ageing_time).attr,
277         &DEV_ATTR(stp_state).attr,
278         &DEV_ATTR(priority).attr,
279         &DEV_ATTR(bridge_id).attr,
280         &DEV_ATTR(root_id).attr,
281         &DEV_ATTR(root_path_cost).attr,
282         &DEV_ATTR(root_port).attr,
283         &DEV_ATTR(topology_change).attr,
284         &DEV_ATTR(topology_change_detected).attr,
285         &DEV_ATTR(hello_timer).attr,
286         &DEV_ATTR(tcn_timer).attr,
287         &DEV_ATTR(topology_change_timer).attr,
288         &DEV_ATTR(gc_timer).attr,
289         &DEV_ATTR(group_addr).attr,
290         NULL
291 };
292
293 static struct attribute_group bridge_group = {
294         .name = SYSFS_BRIDGE_ATTR, /* "bridge" */
295         .attrs = bridge_attrs,
296 };
297
298 /*
299  * Add entries in sysfs onto the existing network class device
300  * for the bridge.
301  *   Adds a attribute group "bridge" containing tuning parameters.
302  *   Sub directory to hold links to interfaces.
303  *
304  * Note: the ifobj exists only to be a subdirectory
305  *   to hold links.  The ifobj exists in the same data structure
306  *   as its parent the bridge so reference counting works.
307  */
308 int dp_sysfs_add_dp(struct datapath *dp)
309 {
310         struct kobject *kobj = vport_get_kobj(dp->ports[ODPP_LOCAL]->vport);
311         int err;
312
313         /* Create /sys/class/net/<devname>/bridge directory. */
314         err = sysfs_create_group(kobj, &bridge_group);
315         if (err) {
316                 pr_info("%s: can't create group %s/%s\n",
317                         __func__, dp_name(dp), bridge_group.name);
318                 goto out1;
319         }
320
321         /* Create /sys/class/net/<devname>/brif directory. */
322         err = kobject_add(&dp->ifobj, kobj, SYSFS_BRIDGE_PORT_SUBDIR);
323         if (err) {
324                 pr_info("%s: can't add kobject (directory) %s/%s\n",
325                         __FUNCTION__, dp_name(dp), kobject_name(&dp->ifobj));
326                 goto out2;
327         }
328         kobject_uevent(&dp->ifobj, KOBJ_ADD);
329         return 0;
330
331  out2:
332         sysfs_remove_group(kobj, &bridge_group);
333  out1:
334         return err;
335 }
336
337 int dp_sysfs_del_dp(struct datapath *dp)
338 {
339         struct kobject *kobj = vport_get_kobj(dp->ports[ODPP_LOCAL]->vport);
340
341         kobject_del(&dp->ifobj);
342         sysfs_remove_group(kobj, &bridge_group);
343
344         return 0;
345 }
346 #else /* !CONFIG_SYSFS */
347 int dp_sysfs_add_dp(struct datapath *dp) { return 0; }
348 int dp_sysfs_del_dp(struct datapath *dp) { return 0; }
349 int dp_sysfs_add_if(struct dp_port *p) { return 0; }
350 int dp_sysfs_del_if(struct dp_port *p) { return 0; }
351 #endif /* !CONFIG_SYSFS */