63738ec1051112e34b2ccf7d772fdf65ad1c06e0
2 * Copyright (C) 2010-2011 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"
27 #include "gateway_common.h"
28 #include "gateway_client.h"
31 static struct net_device
*kobj_to_netdev(struct kobject
*obj
)
33 struct device
*dev
= container_of(obj
->parent
, struct device
, kobj
);
34 return to_net_dev(dev
);
37 static struct bat_priv
*kobj_to_batpriv(struct kobject
*obj
)
39 struct net_device
*net_dev
= kobj_to_netdev(obj
);
40 return netdev_priv(net_dev
);
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), \
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) \
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); \
62 #define BAT_ATTR_SHOW_BOOL(_name) \
63 ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \
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"); \
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)
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) \
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); \
89 #define BAT_ATTR_SHOW_UINT(_name) \
90 ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \
93 struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
94 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
97 /* Use this, if you are going to set [name] in bat_priv to unsigned integer
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)
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
)
111 if (buff
[count
- 1] == '\n')
112 buff
[count
- 1] = '\0';
114 if ((strncmp(buff
, "1", 2) == 0) ||
115 (strncmp(buff
, "enable", 7) == 0) ||
116 (strncmp(buff
, "enabled", 8) == 0))
119 if ((strncmp(buff
, "0", 2) == 0) ||
120 (strncmp(buff
, "disable", 8) == 0) ||
121 (strncmp(buff
, "disabled", 9) == 0))
126 "%s: Invalid parameter received: %s\n",
131 if (atomic_read(attr
) == enabled
)
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");
138 atomic_set(attr
, (unsigned)enabled
);
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
)
149 ret
= store_bool_attr(buff
, count
, net_dev
, attr
->name
, attr_store
);
150 if (post_func
&& ret
)
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
)
160 unsigned long uint_val
;
163 ret
= strict_strtoul(buff
, 10, &uint_val
);
166 "%s: Invalid parameter received: %s\n",
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
);
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
);
183 if (atomic_read(attr
) == uint_val
)
186 bat_info(net_dev
, "%s: Changing from: %i to: %lu\n",
187 attr_name
, atomic_read(attr
), uint_val
);
189 atomic_set(attr
, uint_val
);
193 static inline ssize_t
__store_uint_attr(const char *buff
, size_t count
,
195 void (*post_func
)(struct net_device
*),
196 const struct attribute
*attr
,
197 atomic_t
*attr_store
, struct net_device
*net_dev
)
201 ret
= store_uint_attr(buff
, count
, net_dev
, attr
->name
,
202 min
, max
, attr_store
);
203 if (post_func
&& ret
)
209 static ssize_t
show_vis_mode(struct kobject
*kobj
, struct attribute
*attr
,
212 struct bat_priv
*bat_priv
= kobj_to_batpriv(kobj
);
213 int vis_mode
= atomic_read(&bat_priv
->vis_mode
);
215 return sprintf(buff
, "%s\n",
216 vis_mode
== VIS_TYPE_CLIENT_UPDATE
?
217 "client" : "server");
220 static ssize_t
store_vis_mode(struct kobject
*kobj
, struct attribute
*attr
,
221 char *buff
, size_t count
)
223 struct net_device
*net_dev
= kobj_to_netdev(kobj
);
224 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
226 int ret
, vis_mode_tmp
= -1;
228 ret
= strict_strtoul(buff
, 10, &val
);
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
;
235 if (((count
== 2) && (!ret
) && (val
== VIS_TYPE_SERVER_SYNC
)) ||
236 (strncmp(buff
, "server", 6) == 0))
237 vis_mode_tmp
= VIS_TYPE_SERVER_SYNC
;
239 if (vis_mode_tmp
< 0) {
240 if (buff
[count
- 1] == '\n')
241 buff
[count
- 1] = '\0';
244 "Invalid parameter for 'vis mode' setting received: "
249 if (atomic_read(&bat_priv
->vis_mode
) == vis_mode_tmp
)
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");
257 atomic_set(&bat_priv
->vis_mode
, (unsigned)vis_mode_tmp
);
261 static void post_gw_deselect(struct net_device
*net_dev
)
263 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
264 gw_deselect(bat_priv
);
267 static ssize_t
show_gw_mode(struct kobject
*kobj
, struct attribute
*attr
,
270 struct bat_priv
*bat_priv
= kobj_to_batpriv(kobj
);
273 switch (atomic_read(&bat_priv
->gw_mode
)) {
275 bytes_written
= sprintf(buff
, "%s\n", GW_MODE_CLIENT_NAME
);
278 bytes_written
= sprintf(buff
, "%s\n", GW_MODE_SERVER_NAME
);
281 bytes_written
= sprintf(buff
, "%s\n", GW_MODE_OFF_NAME
);
285 return bytes_written
;
288 static ssize_t
store_gw_mode(struct kobject
*kobj
, struct attribute
*attr
,
289 char *buff
, size_t count
)
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;
296 if (buff
[count
- 1] == '\n')
297 buff
[count
- 1] = '\0';
299 if (strncmp(buff
, GW_MODE_OFF_NAME
, strlen(GW_MODE_OFF_NAME
)) == 0)
300 gw_mode_tmp
= GW_MODE_OFF
;
302 if (strncmp(buff
, GW_MODE_CLIENT_NAME
,
303 strlen(GW_MODE_CLIENT_NAME
)) == 0)
304 gw_mode_tmp
= GW_MODE_CLIENT
;
306 if (strncmp(buff
, GW_MODE_SERVER_NAME
,
307 strlen(GW_MODE_SERVER_NAME
)) == 0)
308 gw_mode_tmp
= GW_MODE_SERVER
;
310 if (gw_mode_tmp
< 0) {
312 "Invalid parameter for 'gw mode' setting received: "
317 if (atomic_read(&bat_priv
->gw_mode
) == gw_mode_tmp
)
320 switch (atomic_read(&bat_priv
->gw_mode
)) {
322 curr_gw_mode_str
= GW_MODE_CLIENT_NAME
;
325 curr_gw_mode_str
= GW_MODE_SERVER_NAME
;
328 curr_gw_mode_str
= GW_MODE_OFF_NAME
;
332 bat_info(net_dev
, "Changing gw mode from: %s to: %s\n",
333 curr_gw_mode_str
, buff
);
335 gw_deselect(bat_priv
);
336 atomic_set(&bat_priv
->gw_mode
, (unsigned)gw_mode_tmp
);
340 static ssize_t
show_gw_bwidth(struct kobject
*kobj
, struct attribute
*attr
,
343 struct bat_priv
*bat_priv
= kobj_to_batpriv(kobj
);
345 int gw_bandwidth
= atomic_read(&bat_priv
->gw_bandwidth
);
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"));
355 static ssize_t
store_gw_bwidth(struct kobject
*kobj
, struct attribute
*attr
,
356 char *buff
, size_t count
)
358 struct net_device
*net_dev
= kobj_to_netdev(kobj
);
360 if (buff
[count
- 1] == '\n')
361 buff
[count
- 1] = '\0';
363 return gw_bandwidth_set(net_dev
, buff
, count
);
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
,
375 static BAT_ATTR(gw_bandwidth
, S_IRUGO
| S_IWUSR
, show_gw_bwidth
,
377 #ifdef CONFIG_BATMAN_ADV_DEBUG
378 BAT_ATTR_UINT(log_level
, S_IRUGO
| S_IWUSR
, 0, 7, NULL
);
381 static struct bat_attribute
*mesh_attrs
[] = {
382 &bat_attr_aggregated_ogms
,
384 &bat_attr_fragmentation
,
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
397 int sysfs_add_meshif(struct net_device
*dev
)
399 struct kobject
*batif_kobject
= &dev
->dev
.kobj
;
400 struct bat_priv
*bat_priv
= netdev_priv(dev
);
401 struct bat_attribute
**bat_attr
;
404 bat_priv
->mesh_obj
= kobject_create_and_add(SYSFS_IF_MESH_SUBDIR
,
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
);
412 for (bat_attr
= mesh_attrs
; *bat_attr
; ++bat_attr
) {
413 err
= sysfs_create_file(bat_priv
->mesh_obj
,
414 &((*bat_attr
)->attr
));
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
);
426 for (bat_attr
= mesh_attrs
; *bat_attr
; ++bat_attr
)
427 sysfs_remove_file(bat_priv
->mesh_obj
, &((*bat_attr
)->attr
));
429 kobject_put(bat_priv
->mesh_obj
);
430 bat_priv
->mesh_obj
= NULL
;
435 void sysfs_del_meshif(struct net_device
*dev
)
437 struct bat_priv
*bat_priv
= netdev_priv(dev
);
438 struct bat_attribute
**bat_attr
;
440 for (bat_attr
= mesh_attrs
; *bat_attr
; ++bat_attr
)
441 sysfs_remove_file(bat_priv
->mesh_obj
, &((*bat_attr
)->attr
));
443 kobject_put(bat_priv
->mesh_obj
);
444 bat_priv
->mesh_obj
= NULL
;
447 static ssize_t
show_mesh_iface(struct kobject
*kobj
, struct attribute
*attr
,
450 struct net_device
*net_dev
= kobj_to_netdev(kobj
);
451 struct hard_iface
*hard_iface
= hardif_get_by_netdev(net_dev
);
457 length
= sprintf(buff
, "%s\n", hard_iface
->if_status
== IF_NOT_IN_USE
?
458 "none" : hard_iface
->soft_iface
->name
);
460 hardif_free_ref(hard_iface
);
465 static ssize_t
store_mesh_iface(struct kobject
*kobj
, struct attribute
*attr
,
466 char *buff
, size_t count
)
468 struct net_device
*net_dev
= kobj_to_netdev(kobj
);
469 struct hard_iface
*hard_iface
= hardif_get_by_netdev(net_dev
);
476 if (buff
[count
- 1] == '\n')
477 buff
[count
- 1] = '\0';
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
);
486 if (strncmp(buff
, "none", 4) == 0)
487 status_tmp
= IF_NOT_IN_USE
;
489 status_tmp
= IF_I_WANT_YOU
;
491 if (hard_iface
->if_status
== status_tmp
)
494 if ((hard_iface
->soft_iface
) &&
495 (strncmp(hard_iface
->soft_iface
->name
, buff
, IFNAMSIZ
) == 0))
498 if (!rtnl_trylock()) {
503 if (status_tmp
== IF_NOT_IN_USE
) {
504 hardif_disable_interface(hard_iface
);
508 /* if the interface already is in use */
509 if (hard_iface
->if_status
!= IF_NOT_IN_USE
)
510 hardif_disable_interface(hard_iface
);
512 ret
= hardif_enable_interface(hard_iface
, buff
);
517 hardif_free_ref(hard_iface
);
521 static ssize_t
show_iface_status(struct kobject
*kobj
, struct attribute
*attr
,
524 struct net_device
*net_dev
= kobj_to_netdev(kobj
);
525 struct hard_iface
*hard_iface
= hardif_get_by_netdev(net_dev
);
531 switch (hard_iface
->if_status
) {
532 case IF_TO_BE_REMOVED
:
533 length
= sprintf(buff
, "disabling\n");
536 length
= sprintf(buff
, "inactive\n");
539 length
= sprintf(buff
, "active\n");
541 case IF_TO_BE_ACTIVATED
:
542 length
= sprintf(buff
, "enabling\n");
546 length
= sprintf(buff
, "not in use\n");
550 hardif_free_ref(hard_iface
);
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
);
559 static struct bat_attribute
*batman_attrs
[] = {
560 &bat_attr_mesh_iface
,
561 &bat_attr_iface_status
,
565 int sysfs_add_hardif(struct kobject
**hardif_obj
, struct net_device
*dev
)
567 struct kobject
*hardif_kobject
= &dev
->dev
.kobj
;
568 struct bat_attribute
**bat_attr
;
571 *hardif_obj
= kobject_create_and_add(SYSFS_IF_BAT_SUBDIR
,
575 bat_err(dev
, "Can't add sysfs directory: %s/%s\n", dev
->name
,
576 SYSFS_IF_BAT_SUBDIR
);
580 for (bat_attr
= batman_attrs
; *bat_attr
; ++bat_attr
) {
581 err
= sysfs_create_file(*hardif_obj
, &((*bat_attr
)->attr
));
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
);
593 for (bat_attr
= batman_attrs
; *bat_attr
; ++bat_attr
)
594 sysfs_remove_file(*hardif_obj
, &((*bat_attr
)->attr
));
599 void sysfs_del_hardif(struct kobject
**hardif_obj
)
601 kobject_put(*hardif_obj
);
This page took 0.055369 seconds and 4 git commands to generate.