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)
31 #define BAT_ATTR(_name, _mode, _show, _store) \
32 struct bat_attribute bat_attr_##_name = { \
33 .attr = {.name = __stringify(_name), \
39 static ssize_t
show_aggr_ogms(struct kobject
*kobj
, struct attribute
*attr
,
42 struct device
*dev
= to_dev(kobj
->parent
);
43 struct bat_priv
*bat_priv
= netdev_priv(to_net_dev(dev
));
44 int aggr_status
= atomic_read(&bat_priv
->aggregation_enabled
);
46 return sprintf(buff
, "%s\n",
47 aggr_status
== 0 ? "disabled" : "enabled");
50 static ssize_t
store_aggr_ogms(struct kobject
*kobj
, struct attribute
*attr
,
51 char *buff
, size_t count
)
53 struct device
*dev
= to_dev(kobj
->parent
);
54 struct net_device
*net_dev
= to_net_dev(dev
);
55 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
58 if (((count
== 2) && (buff
[0] == '1')) ||
59 (strncmp(buff
, "enable", 6) == 0))
62 if (((count
== 2) && (buff
[0] == '0')) ||
63 (strncmp(buff
, "disable", 7) == 0))
67 if (buff
[count
- 1] == '\n')
68 buff
[count
- 1] = '\0';
71 "Invalid parameter for 'aggregate OGM' setting"
72 "received: %s\n", buff
);
76 if (atomic_read(&bat_priv
->aggregation_enabled
) == aggr_tmp
)
79 bat_info(net_dev
, "Changing aggregation from: %s to: %s\n",
80 atomic_read(&bat_priv
->aggregation_enabled
) == 1 ?
81 "enabled" : "disabled", aggr_tmp
== 1 ? "enabled" :
84 atomic_set(&bat_priv
->aggregation_enabled
, (unsigned)aggr_tmp
);
88 static ssize_t
show_bond(struct kobject
*kobj
, struct attribute
*attr
,
91 struct device
*dev
= to_dev(kobj
->parent
);
92 struct bat_priv
*bat_priv
= netdev_priv(to_net_dev(dev
));
93 int bond_status
= atomic_read(&bat_priv
->bonding_enabled
);
95 return sprintf(buff
, "%s\n",
96 bond_status
== 0 ? "disabled" : "enabled");
99 static ssize_t
store_bond(struct kobject
*kobj
, struct attribute
*attr
,
100 char *buff
, size_t count
)
102 struct device
*dev
= to_dev(kobj
->parent
);
103 struct net_device
*net_dev
= to_net_dev(dev
);
104 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
105 int bonding_enabled_tmp
= -1;
107 if (((count
== 2) && (buff
[0] == '1')) ||
108 (strncmp(buff
, "enable", 6) == 0))
109 bonding_enabled_tmp
= 1;
111 if (((count
== 2) && (buff
[0] == '0')) ||
112 (strncmp(buff
, "disable", 7) == 0))
113 bonding_enabled_tmp
= 0;
115 if (bonding_enabled_tmp
< 0) {
116 if (buff
[count
- 1] == '\n')
117 buff
[count
- 1] = '\0';
120 "Invalid parameter for 'bonding' setting received: "
125 if (atomic_read(&bat_priv
->bonding_enabled
) == bonding_enabled_tmp
)
128 bat_info(net_dev
, "Changing bonding from: %s to: %s\n",
129 atomic_read(&bat_priv
->bonding_enabled
) == 1 ?
130 "enabled" : "disabled",
131 bonding_enabled_tmp
== 1 ? "enabled" : "disabled");
133 atomic_set(&bat_priv
->bonding_enabled
, (unsigned)bonding_enabled_tmp
);
137 static ssize_t
show_vis_mode(struct kobject
*kobj
, struct attribute
*attr
,
140 struct device
*dev
= to_dev(kobj
->parent
);
141 struct bat_priv
*bat_priv
= netdev_priv(to_net_dev(dev
));
142 int vis_mode
= atomic_read(&bat_priv
->vis_mode
);
144 return sprintf(buff
, "%s\n",
145 vis_mode
== VIS_TYPE_CLIENT_UPDATE
?
146 "client" : "server");
149 static ssize_t
store_vis_mode(struct kobject
*kobj
, struct attribute
*attr
,
150 char *buff
, size_t count
)
152 struct device
*dev
= to_dev(kobj
->parent
);
153 struct net_device
*net_dev
= to_net_dev(dev
);
154 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
156 int ret
, vis_mode_tmp
= -1;
158 ret
= strict_strtoul(buff
, 10, &val
);
160 if (((count
== 2) && (!ret
) && (val
== VIS_TYPE_CLIENT_UPDATE
)) ||
161 (strncmp(buff
, "client", 6) == 0) ||
162 (strncmp(buff
, "off", 3) == 0))
163 vis_mode_tmp
= VIS_TYPE_CLIENT_UPDATE
;
165 if (((count
== 2) && (!ret
) && (val
== VIS_TYPE_SERVER_SYNC
)) ||
166 (strncmp(buff
, "server", 6) == 0))
167 vis_mode_tmp
= VIS_TYPE_SERVER_SYNC
;
169 if (vis_mode_tmp
< 0) {
170 if (buff
[count
- 1] == '\n')
171 buff
[count
- 1] = '\0';
174 "Invalid parameter for 'vis mode' setting received: "
179 if (atomic_read(&bat_priv
->vis_mode
) == vis_mode_tmp
)
182 bat_info(net_dev
, "Changing vis mode from: %s to: %s\n",
183 atomic_read(&bat_priv
->vis_mode
) == VIS_TYPE_CLIENT_UPDATE
?
184 "client" : "server", vis_mode_tmp
== VIS_TYPE_CLIENT_UPDATE
?
185 "client" : "server");
187 atomic_set(&bat_priv
->vis_mode
, (unsigned)vis_mode_tmp
);
191 static ssize_t
show_orig_interval(struct kobject
*kobj
, struct attribute
*attr
,
194 struct device
*dev
= to_dev(kobj
->parent
);
195 struct bat_priv
*bat_priv
= netdev_priv(to_net_dev(dev
));
197 return sprintf(buff
, "%i\n",
198 atomic_read(&bat_priv
->orig_interval
));
201 static ssize_t
store_orig_interval(struct kobject
*kobj
, struct attribute
*attr
,
202 char *buff
, size_t count
)
204 struct device
*dev
= to_dev(kobj
->parent
);
205 struct net_device
*net_dev
= to_net_dev(dev
);
206 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
207 unsigned long orig_interval_tmp
;
210 ret
= strict_strtoul(buff
, 10, &orig_interval_tmp
);
212 bat_info(net_dev
, "Invalid parameter for 'orig_interval' "
213 "setting received: %s\n", buff
);
217 if (orig_interval_tmp
< JITTER
* 2) {
218 bat_info(net_dev
, "New originator interval too small: %li "
219 "(min: %i)\n", orig_interval_tmp
, JITTER
* 2);
223 if (atomic_read(&bat_priv
->orig_interval
) == orig_interval_tmp
)
226 bat_info(net_dev
, "Changing originator interval from: %i to: %li\n",
227 atomic_read(&bat_priv
->orig_interval
),
230 atomic_set(&bat_priv
->orig_interval
, orig_interval_tmp
);
234 #ifdef CONFIG_BATMAN_ADV_DEBUG
235 static ssize_t
show_log_level(struct kobject
*kobj
, struct attribute
*attr
,
238 struct device
*dev
= to_dev(kobj
->parent
);
239 struct bat_priv
*bat_priv
= netdev_priv(to_net_dev(dev
));
240 int log_level
= atomic_read(&bat_priv
->log_level
);
242 return sprintf(buff
, "%d\n", log_level
);
245 static ssize_t
store_log_level(struct kobject
*kobj
, struct attribute
*attr
,
246 char *buff
, size_t count
)
248 struct device
*dev
= to_dev(kobj
->parent
);
249 struct net_device
*net_dev
= to_net_dev(dev
);
250 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
251 unsigned long log_level_tmp
;
254 ret
= strict_strtoul(buff
, 10, &log_level_tmp
);
256 bat_info(net_dev
, "Invalid parameter for 'log_level' "
257 "setting received: %s\n", buff
);
261 if (log_level_tmp
> 3) {
262 bat_info(net_dev
, "New log level too big: %li "
263 "(max: %i)\n", log_level_tmp
, 3);
267 if (atomic_read(&bat_priv
->log_level
) == log_level_tmp
)
270 bat_info(net_dev
, "Changing log level from: %i to: %li\n",
271 atomic_read(&bat_priv
->log_level
),
274 atomic_set(&bat_priv
->log_level
, (unsigned)log_level_tmp
);
279 static BAT_ATTR(aggregated_ogms
, S_IRUGO
| S_IWUSR
,
280 show_aggr_ogms
, store_aggr_ogms
);
281 static BAT_ATTR(bonding
, S_IRUGO
| S_IWUSR
, show_bond
, store_bond
);
282 static BAT_ATTR(vis_mode
, S_IRUGO
| S_IWUSR
, show_vis_mode
, store_vis_mode
);
283 static BAT_ATTR(orig_interval
, S_IRUGO
| S_IWUSR
,
284 show_orig_interval
, store_orig_interval
);
285 #ifdef CONFIG_BATMAN_ADV_DEBUG
286 static BAT_ATTR(log_level
, S_IRUGO
| S_IWUSR
, show_log_level
, store_log_level
);
289 static struct bat_attribute
*mesh_attrs
[] = {
290 &bat_attr_aggregated_ogms
,
293 &bat_attr_orig_interval
,
294 #ifdef CONFIG_BATMAN_ADV_DEBUG
300 int sysfs_add_meshif(struct net_device
*dev
)
302 struct kobject
*batif_kobject
= &dev
->dev
.kobj
;
303 struct bat_priv
*bat_priv
= netdev_priv(dev
);
304 struct bat_attribute
**bat_attr
;
307 atomic_set(&bat_priv
->aggregation_enabled
, 1);
308 atomic_set(&bat_priv
->bonding_enabled
, 0);
309 atomic_set(&bat_priv
->vis_mode
, VIS_TYPE_CLIENT_UPDATE
);
310 atomic_set(&bat_priv
->orig_interval
, 1000);
311 atomic_set(&bat_priv
->log_level
, 0);
313 bat_priv
->primary_if
= NULL
;
314 bat_priv
->num_ifaces
= 0;
316 bat_priv
->mesh_obj
= kobject_create_and_add(SYSFS_IF_MESH_SUBDIR
,
318 if (!bat_priv
->mesh_obj
) {
319 bat_err(dev
, "Can't add sysfs directory: %s/%s\n", dev
->name
,
320 SYSFS_IF_MESH_SUBDIR
);
324 for (bat_attr
= mesh_attrs
; *bat_attr
; ++bat_attr
) {
325 err
= sysfs_create_file(bat_priv
->mesh_obj
,
326 &((*bat_attr
)->attr
));
328 bat_err(dev
, "Can't add sysfs file: %s/%s/%s\n",
329 dev
->name
, SYSFS_IF_MESH_SUBDIR
,
330 ((*bat_attr
)->attr
).name
);
338 for (bat_attr
= mesh_attrs
; *bat_attr
; ++bat_attr
)
339 sysfs_remove_file(bat_priv
->mesh_obj
, &((*bat_attr
)->attr
));
341 kobject_put(bat_priv
->mesh_obj
);
342 bat_priv
->mesh_obj
= NULL
;
347 void sysfs_del_meshif(struct net_device
*dev
)
349 struct bat_priv
*bat_priv
= netdev_priv(dev
);
350 struct bat_attribute
**bat_attr
;
352 for (bat_attr
= mesh_attrs
; *bat_attr
; ++bat_attr
)
353 sysfs_remove_file(bat_priv
->mesh_obj
, &((*bat_attr
)->attr
));
355 kobject_put(bat_priv
->mesh_obj
);
356 bat_priv
->mesh_obj
= NULL
;
359 static ssize_t
show_mesh_iface(struct kobject
*kobj
, struct attribute
*attr
,
362 struct device
*dev
= to_dev(kobj
->parent
);
363 struct net_device
*net_dev
= to_net_dev(dev
);
364 struct batman_if
*batman_if
= get_batman_if_by_netdev(net_dev
);
369 return sprintf(buff
, "%s\n",
370 batman_if
->if_status
== IF_NOT_IN_USE
?
374 static ssize_t
store_mesh_iface(struct kobject
*kobj
, struct attribute
*attr
,
375 char *buff
, size_t count
)
377 struct device
*dev
= to_dev(kobj
->parent
);
378 struct net_device
*net_dev
= to_net_dev(dev
);
379 struct batman_if
*batman_if
= get_batman_if_by_netdev(net_dev
);
385 if (strncmp(buff
, "none", 4) == 0)
386 status_tmp
= IF_NOT_IN_USE
;
388 if (strncmp(buff
, "bat0", 4) == 0)
389 status_tmp
= IF_I_WANT_YOU
;
391 if (status_tmp
< 0) {
392 if (buff
[count
- 1] == '\n')
393 buff
[count
- 1] = '\0';
395 pr_err("Invalid parameter for 'mesh_iface' setting received: "
400 if ((batman_if
->if_status
== status_tmp
) ||
401 ((status_tmp
== IF_I_WANT_YOU
) &&
402 (batman_if
->if_status
!= IF_NOT_IN_USE
)))
405 if (status_tmp
== IF_I_WANT_YOU
)
406 status_tmp
= hardif_enable_interface(batman_if
);
408 hardif_disable_interface(batman_if
);
410 return (status_tmp
< 0 ? status_tmp
: count
);
413 static ssize_t
show_iface_status(struct kobject
*kobj
, struct attribute
*attr
,
416 struct device
*dev
= to_dev(kobj
->parent
);
417 struct net_device
*net_dev
= to_net_dev(dev
);
418 struct batman_if
*batman_if
= get_batman_if_by_netdev(net_dev
);
423 switch (batman_if
->if_status
) {
424 case IF_TO_BE_REMOVED
:
425 return sprintf(buff
, "disabling\n");
427 return sprintf(buff
, "inactive\n");
429 return sprintf(buff
, "active\n");
430 case IF_TO_BE_ACTIVATED
:
431 return sprintf(buff
, "enabling\n");
434 return sprintf(buff
, "not in use\n");
438 static BAT_ATTR(mesh_iface
, S_IRUGO
| S_IWUSR
,
439 show_mesh_iface
, store_mesh_iface
);
440 static BAT_ATTR(iface_status
, S_IRUGO
, show_iface_status
, NULL
);
442 static struct bat_attribute
*batman_attrs
[] = {
443 &bat_attr_mesh_iface
,
444 &bat_attr_iface_status
,
448 int sysfs_add_hardif(struct kobject
**hardif_obj
, struct net_device
*dev
)
450 struct kobject
*hardif_kobject
= &dev
->dev
.kobj
;
451 struct bat_attribute
**bat_attr
;
454 *hardif_obj
= kobject_create_and_add(SYSFS_IF_BAT_SUBDIR
,
458 bat_err(dev
, "Can't add sysfs directory: %s/%s\n", dev
->name
,
459 SYSFS_IF_BAT_SUBDIR
);
463 for (bat_attr
= batman_attrs
; *bat_attr
; ++bat_attr
) {
464 err
= sysfs_create_file(*hardif_obj
, &((*bat_attr
)->attr
));
466 bat_err(dev
, "Can't add sysfs file: %s/%s/%s\n",
467 dev
->name
, SYSFS_IF_BAT_SUBDIR
,
468 ((*bat_attr
)->attr
).name
);
476 for (bat_attr
= batman_attrs
; *bat_attr
; ++bat_attr
)
477 sysfs_remove_file(*hardif_obj
, &((*bat_attr
)->attr
));
482 void sysfs_del_hardif(struct kobject
**hardif_obj
)
484 kobject_put(*hardif_obj
);