63738ec1051112e34b2ccf7d772fdf65ad1c06e0
[deliverable/linux.git] / net / batman-adv / bat_sysfs.c
1 /*
2 * Copyright (C) 2010-2011 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 "gateway_common.h"
28 #include "gateway_client.h"
29 #include "vis.h"
30
31 static struct net_device *kobj_to_netdev(struct kobject *obj)
32 {
33 struct device *dev = container_of(obj->parent, struct device, kobj);
34 return to_net_dev(dev);
35 }
36
37 static struct bat_priv *kobj_to_batpriv(struct kobject *obj)
38 {
39 struct net_device *net_dev = kobj_to_netdev(obj);
40 return netdev_priv(net_dev);
41 }
42
43 /* Use this, if you have customized show and store functions */
44 #define BAT_ATTR(_name, _mode, _show, _store) \
45 struct bat_attribute bat_attr_##_name = { \
46 .attr = {.name = __stringify(_name), \
47 .mode = _mode }, \
48 .show = _show, \
49 .store = _store, \
50 };
51
52 #define BAT_ATTR_STORE_BOOL(_name, _post_func) \
53 ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
54 char *buff, size_t count) \
55 { \
56 struct net_device *net_dev = kobj_to_netdev(kobj); \
57 struct bat_priv *bat_priv = netdev_priv(net_dev); \
58 return __store_bool_attr(buff, count, _post_func, attr, \
59 &bat_priv->_name, net_dev); \
60 }
61
62 #define BAT_ATTR_SHOW_BOOL(_name) \
63 ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \
64 char *buff) \
65 { \
66 struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
67 return sprintf(buff, "%s\n", \
68 atomic_read(&bat_priv->_name) == 0 ? \
69 "disabled" : "enabled"); \
70 } \
71
72 /* Use this, if you are going to turn a [name] in bat_priv on or off */
73 #define BAT_ATTR_BOOL(_name, _mode, _post_func) \
74 static BAT_ATTR_STORE_BOOL(_name, _post_func) \
75 static BAT_ATTR_SHOW_BOOL(_name) \
76 static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
77
78
79 #define BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func) \
80 ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
81 char *buff, size_t count) \
82 { \
83 struct net_device *net_dev = kobj_to_netdev(kobj); \
84 struct bat_priv *bat_priv = netdev_priv(net_dev); \
85 return __store_uint_attr(buff, count, _min, _max, _post_func, \
86 attr, &bat_priv->_name, net_dev); \
87 }
88
89 #define BAT_ATTR_SHOW_UINT(_name) \
90 ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \
91 char *buff) \
92 { \
93 struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
94 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
95 } \
96
97 /* Use this, if you are going to set [name] in bat_priv to unsigned integer
98 * values only */
99 #define BAT_ATTR_UINT(_name, _mode, _min, _max, _post_func) \
100 static BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func) \
101 static BAT_ATTR_SHOW_UINT(_name) \
102 static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
103
104
105 static int store_bool_attr(char *buff, size_t count,
106 struct net_device *net_dev,
107 const char *attr_name, atomic_t *attr)
108 {
109 int enabled = -1;
110
111 if (buff[count - 1] == '\n')
112 buff[count - 1] = '\0';
113
114 if ((strncmp(buff, "1", 2) == 0) ||
115 (strncmp(buff, "enable", 7) == 0) ||
116 (strncmp(buff, "enabled", 8) == 0))
117 enabled = 1;
118
119 if ((strncmp(buff, "0", 2) == 0) ||
120 (strncmp(buff, "disable", 8) == 0) ||
121 (strncmp(buff, "disabled", 9) == 0))
122 enabled = 0;
123
124 if (enabled < 0) {
125 bat_info(net_dev,
126 "%s: Invalid parameter received: %s\n",
127 attr_name, buff);
128 return -EINVAL;
129 }
130
131 if (atomic_read(attr) == enabled)
132 return count;
133
134 bat_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
135 atomic_read(attr) == 1 ? "enabled" : "disabled",
136 enabled == 1 ? "enabled" : "disabled");
137
138 atomic_set(attr, (unsigned)enabled);
139 return count;
140 }
141
142 static inline ssize_t __store_bool_attr(char *buff, size_t count,
143 void (*post_func)(struct net_device *),
144 struct attribute *attr,
145 atomic_t *attr_store, struct net_device *net_dev)
146 {
147 int ret;
148
149 ret = store_bool_attr(buff, count, net_dev, attr->name, attr_store);
150 if (post_func && ret)
151 post_func(net_dev);
152
153 return ret;
154 }
155
156 static int store_uint_attr(const char *buff, size_t count,
157 struct net_device *net_dev, const char *attr_name,
158 unsigned int min, unsigned int max, atomic_t *attr)
159 {
160 unsigned long uint_val;
161 int ret;
162
163 ret = strict_strtoul(buff, 10, &uint_val);
164 if (ret) {
165 bat_info(net_dev,
166 "%s: Invalid parameter received: %s\n",
167 attr_name, buff);
168 return -EINVAL;
169 }
170
171 if (uint_val < min) {
172 bat_info(net_dev, "%s: Value is too small: %lu min: %u\n",
173 attr_name, uint_val, min);
174 return -EINVAL;
175 }
176
177 if (uint_val > max) {
178 bat_info(net_dev, "%s: Value is too big: %lu max: %u\n",
179 attr_name, uint_val, max);
180 return -EINVAL;
181 }
182
183 if (atomic_read(attr) == uint_val)
184 return count;
185
186 bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
187 attr_name, atomic_read(attr), uint_val);
188
189 atomic_set(attr, uint_val);
190 return count;
191 }
192
193 static inline ssize_t __store_uint_attr(const char *buff, size_t count,
194 int min, int max,
195 void (*post_func)(struct net_device *),
196 const struct attribute *attr,
197 atomic_t *attr_store, struct net_device *net_dev)
198 {
199 int ret;
200
201 ret = store_uint_attr(buff, count, net_dev, attr->name,
202 min, max, attr_store);
203 if (post_func && ret)
204 post_func(net_dev);
205
206 return ret;
207 }
208
209 static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr,
210 char *buff)
211 {
212 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
213 int vis_mode = atomic_read(&bat_priv->vis_mode);
214
215 return sprintf(buff, "%s\n",
216 vis_mode == VIS_TYPE_CLIENT_UPDATE ?
217 "client" : "server");
218 }
219
220 static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr,
221 char *buff, size_t count)
222 {
223 struct net_device *net_dev = kobj_to_netdev(kobj);
224 struct bat_priv *bat_priv = netdev_priv(net_dev);
225 unsigned long val;
226 int ret, vis_mode_tmp = -1;
227
228 ret = strict_strtoul(buff, 10, &val);
229
230 if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
231 (strncmp(buff, "client", 6) == 0) ||
232 (strncmp(buff, "off", 3) == 0))
233 vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
234
235 if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
236 (strncmp(buff, "server", 6) == 0))
237 vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
238
239 if (vis_mode_tmp < 0) {
240 if (buff[count - 1] == '\n')
241 buff[count - 1] = '\0';
242
243 bat_info(net_dev,
244 "Invalid parameter for 'vis mode' setting received: "
245 "%s\n", buff);
246 return -EINVAL;
247 }
248
249 if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
250 return count;
251
252 bat_info(net_dev, "Changing vis mode from: %s to: %s\n",
253 atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE ?
254 "client" : "server", vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE ?
255 "client" : "server");
256
257 atomic_set(&bat_priv->vis_mode, (unsigned)vis_mode_tmp);
258 return count;
259 }
260
261 static void post_gw_deselect(struct net_device *net_dev)
262 {
263 struct bat_priv *bat_priv = netdev_priv(net_dev);
264 gw_deselect(bat_priv);
265 }
266
267 static ssize_t show_gw_mode(struct kobject *kobj, struct attribute *attr,
268 char *buff)
269 {
270 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
271 int bytes_written;
272
273 switch (atomic_read(&bat_priv->gw_mode)) {
274 case GW_MODE_CLIENT:
275 bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME);
276 break;
277 case GW_MODE_SERVER:
278 bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME);
279 break;
280 default:
281 bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME);
282 break;
283 }
284
285 return bytes_written;
286 }
287
288 static ssize_t store_gw_mode(struct kobject *kobj, struct attribute *attr,
289 char *buff, size_t count)
290 {
291 struct net_device *net_dev = kobj_to_netdev(kobj);
292 struct bat_priv *bat_priv = netdev_priv(net_dev);
293 char *curr_gw_mode_str;
294 int gw_mode_tmp = -1;
295
296 if (buff[count - 1] == '\n')
297 buff[count - 1] = '\0';
298
299 if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
300 gw_mode_tmp = GW_MODE_OFF;
301
302 if (strncmp(buff, GW_MODE_CLIENT_NAME,
303 strlen(GW_MODE_CLIENT_NAME)) == 0)
304 gw_mode_tmp = GW_MODE_CLIENT;
305
306 if (strncmp(buff, GW_MODE_SERVER_NAME,
307 strlen(GW_MODE_SERVER_NAME)) == 0)
308 gw_mode_tmp = GW_MODE_SERVER;
309
310 if (gw_mode_tmp < 0) {
311 bat_info(net_dev,
312 "Invalid parameter for 'gw mode' setting received: "
313 "%s\n", buff);
314 return -EINVAL;
315 }
316
317 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
318 return count;
319
320 switch (atomic_read(&bat_priv->gw_mode)) {
321 case GW_MODE_CLIENT:
322 curr_gw_mode_str = GW_MODE_CLIENT_NAME;
323 break;
324 case GW_MODE_SERVER:
325 curr_gw_mode_str = GW_MODE_SERVER_NAME;
326 break;
327 default:
328 curr_gw_mode_str = GW_MODE_OFF_NAME;
329 break;
330 }
331
332 bat_info(net_dev, "Changing gw mode from: %s to: %s\n",
333 curr_gw_mode_str, buff);
334
335 gw_deselect(bat_priv);
336 atomic_set(&bat_priv->gw_mode, (unsigned)gw_mode_tmp);
337 return count;
338 }
339
340 static ssize_t show_gw_bwidth(struct kobject *kobj, struct attribute *attr,
341 char *buff)
342 {
343 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
344 int down, up;
345 int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
346
347 gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
348 return sprintf(buff, "%i%s/%i%s\n",
349 (down > 2048 ? down / 1024 : down),
350 (down > 2048 ? "MBit" : "KBit"),
351 (up > 2048 ? up / 1024 : up),
352 (up > 2048 ? "MBit" : "KBit"));
353 }
354
355 static ssize_t store_gw_bwidth(struct kobject *kobj, struct attribute *attr,
356 char *buff, size_t count)
357 {
358 struct net_device *net_dev = kobj_to_netdev(kobj);
359
360 if (buff[count - 1] == '\n')
361 buff[count - 1] = '\0';
362
363 return gw_bandwidth_set(net_dev, buff, count);
364 }
365
366 BAT_ATTR_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
367 BAT_ATTR_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
368 BAT_ATTR_BOOL(fragmentation, S_IRUGO | S_IWUSR, update_min_mtu);
369 static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, show_vis_mode, store_vis_mode);
370 static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, show_gw_mode, store_gw_mode);
371 BAT_ATTR_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
372 BAT_ATTR_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
373 BAT_ATTR_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
374 post_gw_deselect);
375 static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, show_gw_bwidth,
376 store_gw_bwidth);
377 #ifdef CONFIG_BATMAN_ADV_DEBUG
378 BAT_ATTR_UINT(log_level, S_IRUGO | S_IWUSR, 0, 7, NULL);
379 #endif
380
381 static struct bat_attribute *mesh_attrs[] = {
382 &bat_attr_aggregated_ogms,
383 &bat_attr_bonding,
384 &bat_attr_fragmentation,
385 &bat_attr_vis_mode,
386 &bat_attr_gw_mode,
387 &bat_attr_orig_interval,
388 &bat_attr_hop_penalty,
389 &bat_attr_gw_sel_class,
390 &bat_attr_gw_bandwidth,
391 #ifdef CONFIG_BATMAN_ADV_DEBUG
392 &bat_attr_log_level,
393 #endif
394 NULL,
395 };
396
397 int sysfs_add_meshif(struct net_device *dev)
398 {
399 struct kobject *batif_kobject = &dev->dev.kobj;
400 struct bat_priv *bat_priv = netdev_priv(dev);
401 struct bat_attribute **bat_attr;
402 int err;
403
404 bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
405 batif_kobject);
406 if (!bat_priv->mesh_obj) {
407 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
408 SYSFS_IF_MESH_SUBDIR);
409 goto out;
410 }
411
412 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) {
413 err = sysfs_create_file(bat_priv->mesh_obj,
414 &((*bat_attr)->attr));
415 if (err) {
416 bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
417 dev->name, SYSFS_IF_MESH_SUBDIR,
418 ((*bat_attr)->attr).name);
419 goto rem_attr;
420 }
421 }
422
423 return 0;
424
425 rem_attr:
426 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
427 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
428
429 kobject_put(bat_priv->mesh_obj);
430 bat_priv->mesh_obj = NULL;
431 out:
432 return -ENOMEM;
433 }
434
435 void sysfs_del_meshif(struct net_device *dev)
436 {
437 struct bat_priv *bat_priv = netdev_priv(dev);
438 struct bat_attribute **bat_attr;
439
440 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
441 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
442
443 kobject_put(bat_priv->mesh_obj);
444 bat_priv->mesh_obj = NULL;
445 }
446
447 static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
448 char *buff)
449 {
450 struct net_device *net_dev = kobj_to_netdev(kobj);
451 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
452 ssize_t length;
453
454 if (!hard_iface)
455 return 0;
456
457 length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
458 "none" : hard_iface->soft_iface->name);
459
460 hardif_free_ref(hard_iface);
461
462 return length;
463 }
464
465 static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
466 char *buff, size_t count)
467 {
468 struct net_device *net_dev = kobj_to_netdev(kobj);
469 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
470 int status_tmp = -1;
471 int ret = count;
472
473 if (!hard_iface)
474 return count;
475
476 if (buff[count - 1] == '\n')
477 buff[count - 1] = '\0';
478
479 if (strlen(buff) >= IFNAMSIZ) {
480 pr_err("Invalid parameter for 'mesh_iface' setting received: "
481 "interface name too long '%s'\n", buff);
482 hardif_free_ref(hard_iface);
483 return -EINVAL;
484 }
485
486 if (strncmp(buff, "none", 4) == 0)
487 status_tmp = IF_NOT_IN_USE;
488 else
489 status_tmp = IF_I_WANT_YOU;
490
491 if (hard_iface->if_status == status_tmp)
492 goto out;
493
494 if ((hard_iface->soft_iface) &&
495 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
496 goto out;
497
498 if (!rtnl_trylock()) {
499 ret = -ERESTARTSYS;
500 goto out;
501 }
502
503 if (status_tmp == IF_NOT_IN_USE) {
504 hardif_disable_interface(hard_iface);
505 goto unlock;
506 }
507
508 /* if the interface already is in use */
509 if (hard_iface->if_status != IF_NOT_IN_USE)
510 hardif_disable_interface(hard_iface);
511
512 ret = hardif_enable_interface(hard_iface, buff);
513
514 unlock:
515 rtnl_unlock();
516 out:
517 hardif_free_ref(hard_iface);
518 return ret;
519 }
520
521 static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
522 char *buff)
523 {
524 struct net_device *net_dev = kobj_to_netdev(kobj);
525 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
526 ssize_t length;
527
528 if (!hard_iface)
529 return 0;
530
531 switch (hard_iface->if_status) {
532 case IF_TO_BE_REMOVED:
533 length = sprintf(buff, "disabling\n");
534 break;
535 case IF_INACTIVE:
536 length = sprintf(buff, "inactive\n");
537 break;
538 case IF_ACTIVE:
539 length = sprintf(buff, "active\n");
540 break;
541 case IF_TO_BE_ACTIVATED:
542 length = sprintf(buff, "enabling\n");
543 break;
544 case IF_NOT_IN_USE:
545 default:
546 length = sprintf(buff, "not in use\n");
547 break;
548 }
549
550 hardif_free_ref(hard_iface);
551
552 return length;
553 }
554
555 static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
556 show_mesh_iface, store_mesh_iface);
557 static BAT_ATTR(iface_status, S_IRUGO, show_iface_status, NULL);
558
559 static struct bat_attribute *batman_attrs[] = {
560 &bat_attr_mesh_iface,
561 &bat_attr_iface_status,
562 NULL,
563 };
564
565 int sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
566 {
567 struct kobject *hardif_kobject = &dev->dev.kobj;
568 struct bat_attribute **bat_attr;
569 int err;
570
571 *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
572 hardif_kobject);
573
574 if (!*hardif_obj) {
575 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
576 SYSFS_IF_BAT_SUBDIR);
577 goto out;
578 }
579
580 for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) {
581 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
582 if (err) {
583 bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
584 dev->name, SYSFS_IF_BAT_SUBDIR,
585 ((*bat_attr)->attr).name);
586 goto rem_attr;
587 }
588 }
589
590 return 0;
591
592 rem_attr:
593 for (bat_attr = batman_attrs; *bat_attr; ++bat_attr)
594 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
595 out:
596 return -ENOMEM;
597 }
598
599 void sysfs_del_hardif(struct kobject **hardif_obj)
600 {
601 kobject_put(*hardif_obj);
602 *hardif_obj = NULL;
603 }
This page took 0.055369 seconds and 4 git commands to generate.