Staging: batman-adv: Make hop_penalty configurable via sysfs
[cascardo/linux.git] / drivers / staging / batman-adv / bat_sysfs.c
1 /*
2  * Copyright (C) 2010 B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA
19  *
20  */
21
22 #include "main.h"
23 #include "bat_sysfs.h"
24 #include "translation-table.h"
25 #include "originator.h"
26 #include "hard-interface.h"
27 #include "vis.h"
28
29 #define to_dev(obj)             container_of(obj, struct device, kobj)
30 #define kobj_to_netdev(obj)     to_net_dev(to_dev(obj->parent))
31 #define kobj_to_batpriv(obj)    netdev_priv(kobj_to_netdev(obj))
32
33 /* Use this, if you have customized show and store functions */
34 #define BAT_ATTR(_name, _mode, _show, _store)   \
35 struct bat_attribute bat_attr_##_name = {       \
36         .attr = {.name = __stringify(_name),    \
37                  .mode = _mode },               \
38         .show   = _show,                        \
39         .store  = _store,                       \
40 };
41
42 #define BAT_ATTR_STORE_BOOL(_name, _post_func)                          \
43 ssize_t store_##_name(struct kobject *kobj, struct attribute *attr,     \
44                       char *buff, size_t count)                         \
45 {                                                                       \
46         struct net_device *net_dev = kobj_to_netdev(kobj);              \
47         struct bat_priv *bat_priv = netdev_priv(net_dev);               \
48         return __store_bool_attr(buff, count, _post_func, attr,         \
49                                  &bat_priv->_name, net_dev);            \
50 }
51
52 #define BAT_ATTR_SHOW_BOOL(_name)                                       \
53 ssize_t show_##_name(struct kobject *kobj, struct attribute *attr,      \
54                             char *buff)                                 \
55 {                                                                       \
56         struct bat_priv *bat_priv = kobj_to_batpriv(kobj);              \
57         return sprintf(buff, "%s\n",                                    \
58                        atomic_read(&bat_priv->_name) == 0 ?             \
59                        "disabled" : "enabled");                         \
60 }                                                                       \
61
62 /* Use this, if you are going to turn a [name] in bat_priv on or off */
63 #define BAT_ATTR_BOOL(_name, _mode, _post_func)                         \
64         static BAT_ATTR_STORE_BOOL(_name, _post_func)                   \
65         static BAT_ATTR_SHOW_BOOL(_name)                                \
66         static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
67
68
69 #define BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func)              \
70 ssize_t store_##_name(struct kobject *kobj, struct attribute *attr,     \
71                              char *buff, size_t count)                  \
72 {                                                                       \
73         struct net_device *net_dev = kobj_to_netdev(kobj);              \
74         struct bat_priv *bat_priv = netdev_priv(net_dev);               \
75         return __store_uint_attr(buff, count, _min, _max, _post_func,   \
76                                  attr, &bat_priv->_name, net_dev);      \
77 }
78
79 #define BAT_ATTR_SHOW_UINT(_name)                                       \
80 ssize_t show_##_name(struct kobject *kobj, struct attribute *attr,      \
81                             char *buff)                                 \
82 {                                                                       \
83         struct bat_priv *bat_priv = kobj_to_batpriv(kobj);              \
84         return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name));    \
85 }                                                                       \
86
87 /* Use this, if you are going to set [name] in bat_priv to unsigned integer
88  * values only */
89 #define BAT_ATTR_UINT(_name, _mode, _min, _max, _post_func)             \
90         static BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func)       \
91         static BAT_ATTR_SHOW_UINT(_name)                                \
92         static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
93
94
95 static int store_bool_attr(char *buff, size_t count,
96                            struct net_device *net_dev,
97                            char *attr_name, atomic_t *attr)
98 {
99         int enabled = -1;
100
101         if (buff[count - 1] == '\n')
102                 buff[count - 1] = '\0';
103
104         if ((strncmp(buff, "1", 2) == 0) ||
105             (strncmp(buff, "enable", 7) == 0) ||
106             (strncmp(buff, "enabled", 8) == 0))
107                 enabled = 1;
108
109         if ((strncmp(buff, "0", 2) == 0) ||
110             (strncmp(buff, "disable", 8) == 0) ||
111             (strncmp(buff, "disabled", 9) == 0))
112                 enabled = 0;
113
114         if (enabled < 0) {
115                 bat_info(net_dev,
116                          "%s: Invalid parameter received: %s\n",
117                          attr_name, buff);
118                 return -EINVAL;
119         }
120
121         if (atomic_read(attr) == enabled)
122                 return count;
123
124         bat_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
125                  atomic_read(attr) == 1 ? "enabled" : "disabled",
126                  enabled == 1 ? "enabled" : "disabled");
127
128         atomic_set(attr, (unsigned)enabled);
129         return count;
130 }
131
132 static inline ssize_t __store_bool_attr(char *buff, size_t count,
133                         void (*post_func)(struct net_device *),
134                         struct attribute *attr,
135                         atomic_t *attr_store, struct net_device *net_dev)
136 {
137         int ret;
138
139         ret = store_bool_attr(buff, count, net_dev, (char *)attr->name,
140                               attr_store);
141         if (post_func && ret)
142                 post_func(net_dev);
143
144         return ret;
145 }
146
147 static int store_uint_attr(char *buff, size_t count,
148                            struct net_device *net_dev, char *attr_name,
149                            unsigned int min, unsigned int max, atomic_t *attr)
150 {
151         unsigned long uint_val;
152         int ret;
153
154         ret = strict_strtoul(buff, 10, &uint_val);
155         if (ret) {
156                 bat_info(net_dev,
157                          "%s: Invalid parameter received: %s\n",
158                          attr_name, buff);
159                 return -EINVAL;
160         }
161
162         if (uint_val < min) {
163                 bat_info(net_dev, "%s: Value is too small: %lu min: %u\n",
164                          attr_name, uint_val, min);
165                 return -EINVAL;
166         }
167
168         if (uint_val > max) {
169                 bat_info(net_dev, "%s: Value is too big: %lu max: %u\n",
170                          attr_name, uint_val, max);
171                 return -EINVAL;
172         }
173
174         if (atomic_read(attr) == uint_val)
175                 return count;
176
177         bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
178                  attr_name, atomic_read(attr), uint_val);
179
180         atomic_set(attr, uint_val);
181         return count;
182 }
183
184 static inline ssize_t __store_uint_attr(char *buff, size_t count,
185                         int min, int max,
186                         void (*post_func)(struct net_device *),
187                         struct attribute *attr,
188                         atomic_t *attr_store, struct net_device *net_dev)
189 {
190         int ret;
191
192         ret = store_uint_attr(buff, count, net_dev, (char *)attr->name,
193                               min, max, attr_store);
194         if (post_func && ret)
195                 post_func(net_dev);
196
197         return ret;
198 }
199
200 static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr,
201                              char *buff)
202 {
203         struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
204         int vis_mode = atomic_read(&bat_priv->vis_mode);
205
206         return sprintf(buff, "%s\n",
207                        vis_mode == VIS_TYPE_CLIENT_UPDATE ?
208                                                         "client" : "server");
209 }
210
211 static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr,
212                               char *buff, size_t count)
213 {
214         struct net_device *net_dev = kobj_to_netdev(kobj);
215         struct bat_priv *bat_priv = netdev_priv(net_dev);
216         unsigned long val;
217         int ret, vis_mode_tmp = -1;
218
219         ret = strict_strtoul(buff, 10, &val);
220
221         if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
222             (strncmp(buff, "client", 6) == 0) ||
223             (strncmp(buff, "off", 3) == 0))
224                 vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
225
226         if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
227             (strncmp(buff, "server", 6) == 0))
228                 vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
229
230         if (vis_mode_tmp < 0) {
231                 if (buff[count - 1] == '\n')
232                         buff[count - 1] = '\0';
233
234                 bat_info(net_dev,
235                          "Invalid parameter for 'vis mode' setting received: "
236                          "%s\n", buff);
237                 return -EINVAL;
238         }
239
240         if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
241                 return count;
242
243         bat_info(net_dev, "Changing vis mode from: %s to: %s\n",
244                  atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE ?
245                  "client" : "server", vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE ?
246                  "client" : "server");
247
248         atomic_set(&bat_priv->vis_mode, (unsigned)vis_mode_tmp);
249         return count;
250 }
251
252 BAT_ATTR_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
253 BAT_ATTR_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
254 BAT_ATTR_BOOL(fragmentation, S_IRUGO | S_IWUSR, update_min_mtu);
255 static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, show_vis_mode, store_vis_mode);
256 BAT_ATTR_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
257 BAT_ATTR_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
258 #ifdef CONFIG_BATMAN_ADV_DEBUG
259 BAT_ATTR_UINT(log_level, S_IRUGO | S_IWUSR, 0, 3, NULL);
260 #endif
261
262 static struct bat_attribute *mesh_attrs[] = {
263         &bat_attr_aggregated_ogms,
264         &bat_attr_bonding,
265         &bat_attr_fragmentation,
266         &bat_attr_vis_mode,
267         &bat_attr_orig_interval,
268         &bat_attr_hop_penalty,
269 #ifdef CONFIG_BATMAN_ADV_DEBUG
270         &bat_attr_log_level,
271 #endif
272         NULL,
273 };
274
275 int sysfs_add_meshif(struct net_device *dev)
276 {
277         struct kobject *batif_kobject = &dev->dev.kobj;
278         struct bat_priv *bat_priv = netdev_priv(dev);
279         struct bat_attribute **bat_attr;
280         int err;
281
282         bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
283                                                     batif_kobject);
284         if (!bat_priv->mesh_obj) {
285                 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
286                         SYSFS_IF_MESH_SUBDIR);
287                 goto out;
288         }
289
290         for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) {
291                 err = sysfs_create_file(bat_priv->mesh_obj,
292                                         &((*bat_attr)->attr));
293                 if (err) {
294                         bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
295                                 dev->name, SYSFS_IF_MESH_SUBDIR,
296                                 ((*bat_attr)->attr).name);
297                         goto rem_attr;
298                 }
299         }
300
301         return 0;
302
303 rem_attr:
304         for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
305                 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
306
307         kobject_put(bat_priv->mesh_obj);
308         bat_priv->mesh_obj = NULL;
309 out:
310         return -ENOMEM;
311 }
312
313 void sysfs_del_meshif(struct net_device *dev)
314 {
315         struct bat_priv *bat_priv = netdev_priv(dev);
316         struct bat_attribute **bat_attr;
317
318         for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
319                 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
320
321         kobject_put(bat_priv->mesh_obj);
322         bat_priv->mesh_obj = NULL;
323 }
324
325 static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
326                                char *buff)
327 {
328         struct net_device *net_dev = kobj_to_netdev(kobj);
329         struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
330         ssize_t length;
331
332         if (!batman_if)
333                 return 0;
334
335         length = sprintf(buff, "%s\n", batman_if->if_status == IF_NOT_IN_USE ?
336                          "none" : batman_if->soft_iface->name);
337
338         kref_put(&batman_if->refcount, hardif_free_ref);
339
340         return length;
341 }
342
343 static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
344                                 char *buff, size_t count)
345 {
346         struct net_device *net_dev = kobj_to_netdev(kobj);
347         struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
348         int status_tmp = -1;
349         int ret;
350
351         if (!batman_if)
352                 return count;
353
354         if (buff[count - 1] == '\n')
355                 buff[count - 1] = '\0';
356
357         if (strlen(buff) >= IFNAMSIZ) {
358                 pr_err("Invalid parameter for 'mesh_iface' setting received: "
359                        "interface name too long '%s'\n", buff);
360                 kref_put(&batman_if->refcount, hardif_free_ref);
361                 return -EINVAL;
362         }
363
364         if (strncmp(buff, "none", 4) == 0)
365                 status_tmp = IF_NOT_IN_USE;
366         else
367                 status_tmp = IF_I_WANT_YOU;
368
369         if ((batman_if->if_status == status_tmp) || ((batman_if->soft_iface) &&
370             (strncmp(batman_if->soft_iface->name, buff, IFNAMSIZ) == 0))) {
371                 kref_put(&batman_if->refcount, hardif_free_ref);
372                 return count;
373         }
374
375         if (status_tmp == IF_NOT_IN_USE) {
376                 rtnl_lock();
377                 hardif_disable_interface(batman_if);
378                 rtnl_unlock();
379                 kref_put(&batman_if->refcount, hardif_free_ref);
380                 return count;
381         }
382
383         /* if the interface already is in use */
384         if (batman_if->if_status != IF_NOT_IN_USE) {
385                 rtnl_lock();
386                 hardif_disable_interface(batman_if);
387                 rtnl_unlock();
388         }
389
390         ret = hardif_enable_interface(batman_if, buff);
391         kref_put(&batman_if->refcount, hardif_free_ref);
392
393         return ret;
394 }
395
396 static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
397                                  char *buff)
398 {
399         struct net_device *net_dev = kobj_to_netdev(kobj);
400         struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
401         ssize_t length;
402
403         if (!batman_if)
404                 return 0;
405
406         switch (batman_if->if_status) {
407         case IF_TO_BE_REMOVED:
408                 length = sprintf(buff, "disabling\n");
409                 break;
410         case IF_INACTIVE:
411                 length = sprintf(buff, "inactive\n");
412                 break;
413         case IF_ACTIVE:
414                 length = sprintf(buff, "active\n");
415                 break;
416         case IF_TO_BE_ACTIVATED:
417                 length = sprintf(buff, "enabling\n");
418                 break;
419         case IF_NOT_IN_USE:
420         default:
421                 length = sprintf(buff, "not in use\n");
422                 break;
423         }
424
425         kref_put(&batman_if->refcount, hardif_free_ref);
426
427         return length;
428 }
429
430 static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
431                 show_mesh_iface, store_mesh_iface);
432 static BAT_ATTR(iface_status, S_IRUGO, show_iface_status, NULL);
433
434 static struct bat_attribute *batman_attrs[] = {
435         &bat_attr_mesh_iface,
436         &bat_attr_iface_status,
437         NULL,
438 };
439
440 int sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
441 {
442         struct kobject *hardif_kobject = &dev->dev.kobj;
443         struct bat_attribute **bat_attr;
444         int err;
445
446         *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
447                                                     hardif_kobject);
448
449         if (!*hardif_obj) {
450                 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
451                         SYSFS_IF_BAT_SUBDIR);
452                 goto out;
453         }
454
455         for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) {
456                 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
457                 if (err) {
458                         bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
459                                 dev->name, SYSFS_IF_BAT_SUBDIR,
460                                 ((*bat_attr)->attr).name);
461                         goto rem_attr;
462                 }
463         }
464
465         return 0;
466
467 rem_attr:
468         for (bat_attr = batman_attrs; *bat_attr; ++bat_attr)
469                 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
470 out:
471         return -ENOMEM;
472 }
473
474 void sysfs_del_hardif(struct kobject **hardif_obj)
475 {
476         kobject_put(*hardif_obj);
477         *hardif_obj = NULL;
478 }