1 /* Copyright (C) 2010-2013 B.A.T.M.A.N. contributors:
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <linux/debugfs.h>
25 #include "translation-table.h"
26 #include "originator.h"
27 #include "hard-interface.h"
28 #include "gateway_common.h"
29 #include "gateway_client.h"
30 #include "soft-interface.h"
32 #include "icmp_socket.h"
33 #include "bridge_loop_avoidance.h"
34 #include "distributed-arp-table.h"
35 #include "network-coding.h"
37 static struct dentry
*batadv_debugfs
;
39 #ifdef CONFIG_BATMAN_ADV_DEBUG
40 #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
42 static const int batadv_log_buff_len
= BATADV_LOG_BUF_LEN
;
44 static char *batadv_log_char_addr(struct batadv_priv_debug_log
*debug_log
,
47 return &debug_log
->log_buff
[idx
& BATADV_LOG_BUFF_MASK
];
50 static void batadv_emit_log_char(struct batadv_priv_debug_log
*debug_log
,
55 char_addr
= batadv_log_char_addr(debug_log
, debug_log
->log_end
);
59 if (debug_log
->log_end
- debug_log
->log_start
> batadv_log_buff_len
)
60 debug_log
->log_start
= debug_log
->log_end
- batadv_log_buff_len
;
64 static int batadv_fdebug_log(struct batadv_priv_debug_log
*debug_log
,
68 static char debug_log_buf
[256];
74 spin_lock_bh(&debug_log
->lock
);
76 vscnprintf(debug_log_buf
, sizeof(debug_log_buf
), fmt
, args
);
79 for (p
= debug_log_buf
; *p
!= 0; p
++)
80 batadv_emit_log_char(debug_log
, *p
);
82 spin_unlock_bh(&debug_log
->lock
);
84 wake_up(&debug_log
->queue_wait
);
89 int batadv_debug_log(struct batadv_priv
*bat_priv
, const char *fmt
, ...)
92 char tmp_log_buf
[256];
95 vscnprintf(tmp_log_buf
, sizeof(tmp_log_buf
), fmt
, args
);
96 batadv_fdebug_log(bat_priv
->debug_log
, "[%10u] %s",
97 jiffies_to_msecs(jiffies
), tmp_log_buf
);
103 static int batadv_log_open(struct inode
*inode
, struct file
*file
)
105 if (!try_module_get(THIS_MODULE
))
108 nonseekable_open(inode
, file
);
109 file
->private_data
= inode
->i_private
;
113 static int batadv_log_release(struct inode
*inode
, struct file
*file
)
115 module_put(THIS_MODULE
);
119 static int batadv_log_empty(struct batadv_priv_debug_log
*debug_log
)
121 return !(debug_log
->log_start
- debug_log
->log_end
);
124 static ssize_t
batadv_log_read(struct file
*file
, char __user
*buf
,
125 size_t count
, loff_t
*ppos
)
127 struct batadv_priv
*bat_priv
= file
->private_data
;
128 struct batadv_priv_debug_log
*debug_log
= bat_priv
->debug_log
;
133 if ((file
->f_flags
& O_NONBLOCK
) && batadv_log_empty(debug_log
))
142 if (!access_ok(VERIFY_WRITE
, buf
, count
))
145 error
= wait_event_interruptible(debug_log
->queue_wait
,
146 (!batadv_log_empty(debug_log
)));
151 spin_lock_bh(&debug_log
->lock
);
153 while ((!error
) && (i
< count
) &&
154 (debug_log
->log_start
!= debug_log
->log_end
)) {
155 char_addr
= batadv_log_char_addr(debug_log
,
156 debug_log
->log_start
);
159 debug_log
->log_start
++;
161 spin_unlock_bh(&debug_log
->lock
);
163 error
= __put_user(c
, buf
);
165 spin_lock_bh(&debug_log
->lock
);
171 spin_unlock_bh(&debug_log
->lock
);
179 static unsigned int batadv_log_poll(struct file
*file
, poll_table
*wait
)
181 struct batadv_priv
*bat_priv
= file
->private_data
;
182 struct batadv_priv_debug_log
*debug_log
= bat_priv
->debug_log
;
184 poll_wait(file
, &debug_log
->queue_wait
, wait
);
186 if (!batadv_log_empty(debug_log
))
187 return POLLIN
| POLLRDNORM
;
192 static const struct file_operations batadv_log_fops
= {
193 .open
= batadv_log_open
,
194 .release
= batadv_log_release
,
195 .read
= batadv_log_read
,
196 .poll
= batadv_log_poll
,
200 static int batadv_debug_log_setup(struct batadv_priv
*bat_priv
)
204 if (!bat_priv
->debug_dir
)
207 bat_priv
->debug_log
= kzalloc(sizeof(*bat_priv
->debug_log
), GFP_ATOMIC
);
208 if (!bat_priv
->debug_log
)
211 spin_lock_init(&bat_priv
->debug_log
->lock
);
212 init_waitqueue_head(&bat_priv
->debug_log
->queue_wait
);
214 d
= debugfs_create_file("log", S_IFREG
| S_IRUSR
,
215 bat_priv
->debug_dir
, bat_priv
,
226 static void batadv_debug_log_cleanup(struct batadv_priv
*bat_priv
)
228 kfree(bat_priv
->debug_log
);
229 bat_priv
->debug_log
= NULL
;
231 #else /* CONFIG_BATMAN_ADV_DEBUG */
232 static int batadv_debug_log_setup(struct batadv_priv
*bat_priv
)
237 static void batadv_debug_log_cleanup(struct batadv_priv
*bat_priv
)
243 static int batadv_algorithms_open(struct inode
*inode
, struct file
*file
)
245 return single_open(file
, batadv_algo_seq_print_text
, NULL
);
248 static int batadv_originators_open(struct inode
*inode
, struct file
*file
)
250 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
251 return single_open(file
, batadv_orig_seq_print_text
, net_dev
);
254 static int batadv_gateways_open(struct inode
*inode
, struct file
*file
)
256 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
257 return single_open(file
, batadv_gw_client_seq_print_text
, net_dev
);
260 static int batadv_transtable_global_open(struct inode
*inode
, struct file
*file
)
262 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
263 return single_open(file
, batadv_tt_global_seq_print_text
, net_dev
);
266 #ifdef CONFIG_BATMAN_ADV_BLA
267 static int batadv_bla_claim_table_open(struct inode
*inode
, struct file
*file
)
269 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
270 return single_open(file
, batadv_bla_claim_table_seq_print_text
,
274 static int batadv_bla_backbone_table_open(struct inode
*inode
,
277 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
278 return single_open(file
, batadv_bla_backbone_table_seq_print_text
,
284 #ifdef CONFIG_BATMAN_ADV_DAT
286 * batadv_dat_cache_open - Prepare file handler for reads from dat_chache
287 * @inode: inode which was opened
288 * @file: file handle to be initialized
290 static int batadv_dat_cache_open(struct inode
*inode
, struct file
*file
)
292 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
293 return single_open(file
, batadv_dat_cache_seq_print_text
, net_dev
);
297 static int batadv_transtable_local_open(struct inode
*inode
, struct file
*file
)
299 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
300 return single_open(file
, batadv_tt_local_seq_print_text
, net_dev
);
303 static int batadv_vis_data_open(struct inode
*inode
, struct file
*file
)
305 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
306 return single_open(file
, batadv_vis_seq_print_text
, net_dev
);
309 struct batadv_debuginfo
{
310 struct attribute attr
;
311 const struct file_operations fops
;
314 #ifdef CONFIG_BATMAN_ADV_NC
315 static int batadv_nc_nodes_open(struct inode
*inode
, struct file
*file
)
317 struct net_device
*net_dev
= (struct net_device
*)inode
->i_private
;
318 return single_open(file
, batadv_nc_nodes_seq_print_text
, net_dev
);
322 #define BATADV_DEBUGINFO(_name, _mode, _open) \
323 struct batadv_debuginfo batadv_debuginfo_##_name = { \
324 .attr = { .name = __stringify(_name), \
326 .fops = { .owner = THIS_MODULE, \
329 .llseek = seq_lseek, \
330 .release = single_release, \
334 /* the following attributes are general and therefore they will be directly
335 * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
337 static BATADV_DEBUGINFO(routing_algos
, S_IRUGO
, batadv_algorithms_open
);
339 static struct batadv_debuginfo
*batadv_general_debuginfos
[] = {
340 &batadv_debuginfo_routing_algos
,
344 /* The following attributes are per soft interface */
345 static BATADV_DEBUGINFO(originators
, S_IRUGO
, batadv_originators_open
);
346 static BATADV_DEBUGINFO(gateways
, S_IRUGO
, batadv_gateways_open
);
347 static BATADV_DEBUGINFO(transtable_global
, S_IRUGO
,
348 batadv_transtable_global_open
);
349 #ifdef CONFIG_BATMAN_ADV_BLA
350 static BATADV_DEBUGINFO(bla_claim_table
, S_IRUGO
, batadv_bla_claim_table_open
);
351 static BATADV_DEBUGINFO(bla_backbone_table
, S_IRUGO
,
352 batadv_bla_backbone_table_open
);
354 #ifdef CONFIG_BATMAN_ADV_DAT
355 static BATADV_DEBUGINFO(dat_cache
, S_IRUGO
, batadv_dat_cache_open
);
357 static BATADV_DEBUGINFO(transtable_local
, S_IRUGO
,
358 batadv_transtable_local_open
);
359 static BATADV_DEBUGINFO(vis_data
, S_IRUGO
, batadv_vis_data_open
);
360 #ifdef CONFIG_BATMAN_ADV_NC
361 static BATADV_DEBUGINFO(nc_nodes
, S_IRUGO
, batadv_nc_nodes_open
);
364 static struct batadv_debuginfo
*batadv_mesh_debuginfos
[] = {
365 &batadv_debuginfo_originators
,
366 &batadv_debuginfo_gateways
,
367 &batadv_debuginfo_transtable_global
,
368 #ifdef CONFIG_BATMAN_ADV_BLA
369 &batadv_debuginfo_bla_claim_table
,
370 &batadv_debuginfo_bla_backbone_table
,
372 #ifdef CONFIG_BATMAN_ADV_DAT
373 &batadv_debuginfo_dat_cache
,
375 &batadv_debuginfo_transtable_local
,
376 &batadv_debuginfo_vis_data
,
377 #ifdef CONFIG_BATMAN_ADV_NC
378 &batadv_debuginfo_nc_nodes
,
383 void batadv_debugfs_init(void)
385 struct batadv_debuginfo
**bat_debug
;
388 batadv_debugfs
= debugfs_create_dir(BATADV_DEBUGFS_SUBDIR
, NULL
);
389 if (batadv_debugfs
== ERR_PTR(-ENODEV
))
390 batadv_debugfs
= NULL
;
395 for (bat_debug
= batadv_general_debuginfos
; *bat_debug
; ++bat_debug
) {
396 file
= debugfs_create_file(((*bat_debug
)->attr
).name
,
397 S_IFREG
| ((*bat_debug
)->attr
).mode
,
398 batadv_debugfs
, NULL
,
399 &(*bat_debug
)->fops
);
401 pr_err("Can't add general debugfs file: %s\n",
402 ((*bat_debug
)->attr
).name
);
409 debugfs_remove_recursive(batadv_debugfs
);
412 void batadv_debugfs_destroy(void)
414 debugfs_remove_recursive(batadv_debugfs
);
415 batadv_debugfs
= NULL
;
418 int batadv_debugfs_add_meshif(struct net_device
*dev
)
420 struct batadv_priv
*bat_priv
= netdev_priv(dev
);
421 struct batadv_debuginfo
**bat_debug
;
427 bat_priv
->debug_dir
= debugfs_create_dir(dev
->name
, batadv_debugfs
);
428 if (!bat_priv
->debug_dir
)
431 if (batadv_socket_setup(bat_priv
) < 0)
434 if (batadv_debug_log_setup(bat_priv
) < 0)
437 for (bat_debug
= batadv_mesh_debuginfos
; *bat_debug
; ++bat_debug
) {
438 file
= debugfs_create_file(((*bat_debug
)->attr
).name
,
439 S_IFREG
| ((*bat_debug
)->attr
).mode
,
441 dev
, &(*bat_debug
)->fops
);
443 batadv_err(dev
, "Can't add debugfs file: %s/%s\n",
444 dev
->name
, ((*bat_debug
)->attr
).name
);
449 if (batadv_nc_init_debugfs(bat_priv
) < 0)
454 debugfs_remove_recursive(bat_priv
->debug_dir
);
455 bat_priv
->debug_dir
= NULL
;
457 #ifdef CONFIG_DEBUG_FS
461 #endif /* CONFIG_DEBUG_FS */
464 void batadv_debugfs_del_meshif(struct net_device
*dev
)
466 struct batadv_priv
*bat_priv
= netdev_priv(dev
);
468 batadv_debug_log_cleanup(bat_priv
);
470 if (batadv_debugfs
) {
471 debugfs_remove_recursive(bat_priv
->debug_dir
);
472 bat_priv
->debug_dir
= NULL
;