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