kvm tools, setup: Create private directory
[linux-2.6/next.git] / net / batman-adv / bat_sysfs.c
blob497a0700cc3cb03881edd881c7850973fc45f2b8
1 /*
2 * Copyright (C) 2010-2011 B.A.T.M.A.N. contributors:
4 * Marek Lindner
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.
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.
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
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"
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))
35 /* Use this, if you have customized show and store functions */
36 #define BAT_ATTR(_name, _mode, _show, _store) \
37 struct bat_attribute bat_attr_##_name = { \
38 .attr = {.name = __stringify(_name), \
39 .mode = _mode }, \
40 .show = _show, \
41 .store = _store, \
44 #define BAT_ATTR_STORE_BOOL(_name, _post_func) \
45 ssize_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); \
54 #define BAT_ATTR_SHOW_BOOL(_name) \
55 ssize_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 } \
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)
71 #define BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func) \
72 ssize_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); \
81 #define BAT_ATTR_SHOW_UINT(_name) \
82 ssize_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 } \
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)
97 static int store_bool_attr(char *buff, size_t count,
98 struct net_device *net_dev,
99 char *attr_name, atomic_t *attr)
101 int enabled = -1;
103 if (buff[count - 1] == '\n')
104 buff[count - 1] = '\0';
106 if ((strncmp(buff, "1", 2) == 0) ||
107 (strncmp(buff, "enable", 7) == 0) ||
108 (strncmp(buff, "enabled", 8) == 0))
109 enabled = 1;
111 if ((strncmp(buff, "0", 2) == 0) ||
112 (strncmp(buff, "disable", 8) == 0) ||
113 (strncmp(buff, "disabled", 9) == 0))
114 enabled = 0;
116 if (enabled < 0) {
117 bat_info(net_dev,
118 "%s: Invalid parameter received: %s\n",
119 attr_name, buff);
120 return -EINVAL;
123 if (atomic_read(attr) == enabled)
124 return count;
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");
130 atomic_set(attr, (unsigned)enabled);
131 return count;
134 static 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)
139 int ret;
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);
146 return ret;
149 static 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)
153 unsigned long uint_val;
154 int ret;
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;
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;
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;
176 if (atomic_read(attr) == uint_val)
177 return count;
179 bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
180 attr_name, atomic_read(attr), uint_val);
182 atomic_set(attr, uint_val);
183 return count;
186 static 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)
192 int ret;
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);
199 return ret;
202 static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr,
203 char *buff)
205 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
206 int vis_mode = atomic_read(&bat_priv->vis_mode);
208 return sprintf(buff, "%s\n",
209 vis_mode == VIS_TYPE_CLIENT_UPDATE ?
210 "client" : "server");
213 static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr,
214 char *buff, size_t count)
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;
221 ret = strict_strtoul(buff, 10, &val);
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;
228 if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
229 (strncmp(buff, "server", 6) == 0))
230 vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
232 if (vis_mode_tmp < 0) {
233 if (buff[count - 1] == '\n')
234 buff[count - 1] = '\0';
236 bat_info(net_dev,
237 "Invalid parameter for 'vis mode' setting received: "
238 "%s\n", buff);
239 return -EINVAL;
242 if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
243 return count;
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");
250 atomic_set(&bat_priv->vis_mode, (unsigned)vis_mode_tmp);
251 return count;
254 static void post_gw_deselect(struct net_device *net_dev)
256 struct bat_priv *bat_priv = netdev_priv(net_dev);
257 gw_deselect(bat_priv);
260 static ssize_t show_gw_mode(struct kobject *kobj, struct attribute *attr,
261 char *buff)
263 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
264 int bytes_written;
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;
278 return bytes_written;
281 static ssize_t store_gw_mode(struct kobject *kobj, struct attribute *attr,
282 char *buff, size_t count)
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;
289 if (buff[count - 1] == '\n')
290 buff[count - 1] = '\0';
292 if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
293 gw_mode_tmp = GW_MODE_OFF;
295 if (strncmp(buff, GW_MODE_CLIENT_NAME,
296 strlen(GW_MODE_CLIENT_NAME)) == 0)
297 gw_mode_tmp = GW_MODE_CLIENT;
299 if (strncmp(buff, GW_MODE_SERVER_NAME,
300 strlen(GW_MODE_SERVER_NAME)) == 0)
301 gw_mode_tmp = GW_MODE_SERVER;
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;
310 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
311 return count;
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;
325 bat_info(net_dev, "Changing gw mode from: %s to: %s\n",
326 curr_gw_mode_str, buff);
328 gw_deselect(bat_priv);
329 atomic_set(&bat_priv->gw_mode, (unsigned)gw_mode_tmp);
330 return count;
333 static ssize_t show_gw_bwidth(struct kobject *kobj, struct attribute *attr,
334 char *buff)
336 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
337 int down, up;
338 int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
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"));
348 static ssize_t store_gw_bwidth(struct kobject *kobj, struct attribute *attr,
349 char *buff, size_t count)
351 struct net_device *net_dev = kobj_to_netdev(kobj);
353 if (buff[count - 1] == '\n')
354 buff[count - 1] = '\0';
356 return gw_bandwidth_set(net_dev, buff, count);
359 BAT_ATTR_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
360 BAT_ATTR_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
361 BAT_ATTR_BOOL(fragmentation, S_IRUGO | S_IWUSR, update_min_mtu);
362 static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, show_vis_mode, store_vis_mode);
363 static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, show_gw_mode, store_gw_mode);
364 BAT_ATTR_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
365 BAT_ATTR_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
366 BAT_ATTR_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
367 post_gw_deselect);
368 static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, show_gw_bwidth,
369 store_gw_bwidth);
370 #ifdef CONFIG_BATMAN_ADV_DEBUG
371 BAT_ATTR_UINT(log_level, S_IRUGO | S_IWUSR, 0, 3, NULL);
372 #endif
374 static 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,
390 int sysfs_add_meshif(struct net_device *dev)
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;
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;
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;
416 return 0;
418 rem_attr:
419 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
420 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
422 kobject_put(bat_priv->mesh_obj);
423 bat_priv->mesh_obj = NULL;
424 out:
425 return -ENOMEM;
428 void sysfs_del_meshif(struct net_device *dev)
430 struct bat_priv *bat_priv = netdev_priv(dev);
431 struct bat_attribute **bat_attr;
433 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
434 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
436 kobject_put(bat_priv->mesh_obj);
437 bat_priv->mesh_obj = NULL;
440 static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
441 char *buff)
443 struct net_device *net_dev = kobj_to_netdev(kobj);
444 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
445 ssize_t length;
447 if (!hard_iface)
448 return 0;
450 length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
451 "none" : hard_iface->soft_iface->name);
453 hardif_free_ref(hard_iface);
455 return length;
458 static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
459 char *buff, size_t count)
461 struct net_device *net_dev = kobj_to_netdev(kobj);
462 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
463 int status_tmp = -1;
464 int ret = count;
466 if (!hard_iface)
467 return count;
469 if (buff[count - 1] == '\n')
470 buff[count - 1] = '\0';
472 if (strlen(buff) >= IFNAMSIZ) {
473 pr_err("Invalid parameter for 'mesh_iface' setting received: "
474 "interface name too long '%s'\n", buff);
475 hardif_free_ref(hard_iface);
476 return -EINVAL;
479 if (strncmp(buff, "none", 4) == 0)
480 status_tmp = IF_NOT_IN_USE;
481 else
482 status_tmp = IF_I_WANT_YOU;
484 if (hard_iface->if_status == status_tmp)
485 goto out;
487 if ((hard_iface->soft_iface) &&
488 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
489 goto out;
491 if (!rtnl_trylock()) {
492 ret = -ERESTARTSYS;
493 goto out;
496 if (status_tmp == IF_NOT_IN_USE) {
497 hardif_disable_interface(hard_iface);
498 goto unlock;
501 /* if the interface already is in use */
502 if (hard_iface->if_status != IF_NOT_IN_USE)
503 hardif_disable_interface(hard_iface);
505 ret = hardif_enable_interface(hard_iface, buff);
507 unlock:
508 rtnl_unlock();
509 out:
510 hardif_free_ref(hard_iface);
511 return ret;
514 static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
515 char *buff)
517 struct net_device *net_dev = kobj_to_netdev(kobj);
518 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
519 ssize_t length;
521 if (!hard_iface)
522 return 0;
524 switch (hard_iface->if_status) {
525 case IF_TO_BE_REMOVED:
526 length = sprintf(buff, "disabling\n");
527 break;
528 case IF_INACTIVE:
529 length = sprintf(buff, "inactive\n");
530 break;
531 case IF_ACTIVE:
532 length = sprintf(buff, "active\n");
533 break;
534 case IF_TO_BE_ACTIVATED:
535 length = sprintf(buff, "enabling\n");
536 break;
537 case IF_NOT_IN_USE:
538 default:
539 length = sprintf(buff, "not in use\n");
540 break;
543 hardif_free_ref(hard_iface);
545 return length;
548 static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
549 show_mesh_iface, store_mesh_iface);
550 static BAT_ATTR(iface_status, S_IRUGO, show_iface_status, NULL);
552 static struct bat_attribute *batman_attrs[] = {
553 &bat_attr_mesh_iface,
554 &bat_attr_iface_status,
555 NULL,
558 int sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
560 struct kobject *hardif_kobject = &dev->dev.kobj;
561 struct bat_attribute **bat_attr;
562 int err;
564 *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
565 hardif_kobject);
567 if (!*hardif_obj) {
568 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
569 SYSFS_IF_BAT_SUBDIR);
570 goto out;
573 for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) {
574 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
575 if (err) {
576 bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
577 dev->name, SYSFS_IF_BAT_SUBDIR,
578 ((*bat_attr)->attr).name);
579 goto rem_attr;
583 return 0;
585 rem_attr:
586 for (bat_attr = batman_attrs; *bat_attr; ++bat_attr)
587 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
588 out:
589 return -ENOMEM;
592 void sysfs_del_hardif(struct kobject **hardif_obj)
594 kobject_put(*hardif_obj);
595 *hardif_obj = NULL;