2 * Copyright (C) 2010 B.A.T.M.A.N. contributors:
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
23 #include "bat_sysfs.h"
24 #include "translation-table.h"
25 #include "originator.h"
26 #include "hard-interface.h"
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))
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), \
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) \
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); \
52 #define BAT_ATTR_SHOW_BOOL(_name) \
53 ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \
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"); \
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)
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) \
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); \
79 #define BAT_ATTR_SHOW_UINT(_name) \
80 ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \
83 struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
84 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
87 /* Use this, if you are going to set [name] in bat_priv to unsigned integer
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)
95 static int store_bool_attr(char *buff, size_t count,
96 struct net_device *net_dev,
97 char *attr_name, atomic_t *attr)
101 if (buff[count - 1] == '\n')
102 buff[count - 1] = '\0';
104 if ((strncmp(buff, "1", 2) == 0) ||
105 (strncmp(buff, "enable", 7) == 0) ||
106 (strncmp(buff, "enabled", 8) == 0))
109 if ((strncmp(buff, "0", 2) == 0) ||
110 (strncmp(buff, "disable", 8) == 0) ||
111 (strncmp(buff, "disabled", 9) == 0))
116 "%s: Invalid parameter received: %s\n",
121 if (atomic_read(attr) == enabled)
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");
128 atomic_set(attr, (unsigned)enabled);
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)
139 ret = store_bool_attr(buff, count, net_dev, (char *)attr->name,
141 if (post_func && ret)
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)
151 unsigned long uint_val;
154 ret = strict_strtoul(buff, 10, &uint_val);
157 "%s: Invalid parameter received: %s\n",
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);
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);
174 if (atomic_read(attr) == uint_val)
177 bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
178 attr_name, atomic_read(attr), uint_val);
180 atomic_set(attr, uint_val);
184 static inline ssize_t __store_uint_attr(char *buff, size_t count,
186 void (*post_func)(struct net_device *),
187 struct attribute *attr,
188 atomic_t *attr_store, struct net_device *net_dev)
192 ret = store_uint_attr(buff, count, net_dev, (char *)attr->name,
193 min, max, attr_store);
194 if (post_func && ret)
200 static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr,
203 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
204 int vis_mode = atomic_read(&bat_priv->vis_mode);
206 return sprintf(buff, "%s\n",
207 vis_mode == VIS_TYPE_CLIENT_UPDATE ?
208 "client" : "server");
211 static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr,
212 char *buff, size_t count)
214 struct net_device *net_dev = kobj_to_netdev(kobj);
215 struct bat_priv *bat_priv = netdev_priv(net_dev);
217 int ret, vis_mode_tmp = -1;
219 ret = strict_strtoul(buff, 10, &val);
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;
226 if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
227 (strncmp(buff, "server", 6) == 0))
228 vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
230 if (vis_mode_tmp < 0) {
231 if (buff[count - 1] == '\n')
232 buff[count - 1] = '\0';
235 "Invalid parameter for 'vis mode' setting received: "
240 if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
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");
248 atomic_set(&bat_priv->vis_mode, (unsigned)vis_mode_tmp);
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);
262 static struct bat_attribute *mesh_attrs[] = {
263 &bat_attr_aggregated_ogms,
265 &bat_attr_fragmentation,
267 &bat_attr_orig_interval,
268 &bat_attr_hop_penalty,
269 #ifdef CONFIG_BATMAN_ADV_DEBUG
275 int sysfs_add_meshif(struct net_device *dev)
277 struct kobject *batif_kobject = &dev->dev.kobj;
278 struct bat_priv *bat_priv = netdev_priv(dev);
279 struct bat_attribute **bat_attr;
282 bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
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);
290 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) {
291 err = sysfs_create_file(bat_priv->mesh_obj,
292 &((*bat_attr)->attr));
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);
304 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
305 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
307 kobject_put(bat_priv->mesh_obj);
308 bat_priv->mesh_obj = NULL;
313 void sysfs_del_meshif(struct net_device *dev)
315 struct bat_priv *bat_priv = netdev_priv(dev);
316 struct bat_attribute **bat_attr;
318 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
319 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
321 kobject_put(bat_priv->mesh_obj);
322 bat_priv->mesh_obj = NULL;
325 static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
328 struct net_device *net_dev = kobj_to_netdev(kobj);
329 struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
335 length = sprintf(buff, "%s\n", batman_if->if_status == IF_NOT_IN_USE ?
336 "none" : batman_if->soft_iface->name);
338 kref_put(&batman_if->refcount, hardif_free_ref);
343 static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
344 char *buff, size_t count)
346 struct net_device *net_dev = kobj_to_netdev(kobj);
347 struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
354 if (buff[count - 1] == '\n')
355 buff[count - 1] = '\0';
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);
364 if (strncmp(buff, "none", 4) == 0)
365 status_tmp = IF_NOT_IN_USE;
367 status_tmp = IF_I_WANT_YOU;
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);
375 if (status_tmp == IF_NOT_IN_USE) {
377 hardif_disable_interface(batman_if);
379 kref_put(&batman_if->refcount, hardif_free_ref);
383 /* if the interface already is in use */
384 if (batman_if->if_status != IF_NOT_IN_USE) {
386 hardif_disable_interface(batman_if);
390 ret = hardif_enable_interface(batman_if, buff);
391 kref_put(&batman_if->refcount, hardif_free_ref);
396 static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
399 struct net_device *net_dev = kobj_to_netdev(kobj);
400 struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
406 switch (batman_if->if_status) {
407 case IF_TO_BE_REMOVED:
408 length = sprintf(buff, "disabling\n");
411 length = sprintf(buff, "inactive\n");
414 length = sprintf(buff, "active\n");
416 case IF_TO_BE_ACTIVATED:
417 length = sprintf(buff, "enabling\n");
421 length = sprintf(buff, "not in use\n");
425 kref_put(&batman_if->refcount, hardif_free_ref);
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);
434 static struct bat_attribute *batman_attrs[] = {
435 &bat_attr_mesh_iface,
436 &bat_attr_iface_status,
440 int sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
442 struct kobject *hardif_kobject = &dev->dev.kobj;
443 struct bat_attribute **bat_attr;
446 *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
450 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
451 SYSFS_IF_BAT_SUBDIR);
455 for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) {
456 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
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);
468 for (bat_attr = batman_attrs; *bat_attr; ++bat_attr)
469 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
474 void sysfs_del_hardif(struct kobject **hardif_obj)
476 kobject_put(*hardif_obj);