1 /* Copyright (C) 2010-2014 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, see <http://www.gnu.org/licenses/>.
20 #include <linux/debugfs.h>
23 #include "translation-table.h"
24 #include "originator.h"
25 #include "hard-interface.h"
26 #include "gateway_common.h"
27 #include "gateway_client.h"
28 #include "soft-interface.h"
29 #include "icmp_socket.h"
30 #include "bridge_loop_avoidance.h"
31 #include "distributed-arp-table.h"
32 #include "network-coding.h"
34 static struct dentry *batadv_debugfs;
36 #ifdef CONFIG_BATMAN_ADV_DEBUG
37 #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
39 static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
41 static char *batadv_log_char_addr(struct batadv_priv_debug_log *debug_log,
44 return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK];
47 static void batadv_emit_log_char(struct batadv_priv_debug_log *debug_log,
52 char_addr = batadv_log_char_addr(debug_log, debug_log->log_end);
56 if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
57 debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
61 static int batadv_fdebug_log(struct batadv_priv_debug_log *debug_log,
65 static char debug_log_buf[256];
71 spin_lock_bh(&debug_log->lock);
73 vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
76 for (p = debug_log_buf; *p != 0; p++)
77 batadv_emit_log_char(debug_log, *p);
79 spin_unlock_bh(&debug_log->lock);
81 wake_up(&debug_log->queue_wait);
86 int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
89 char tmp_log_buf[256];
92 vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
93 batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
94 jiffies_to_msecs(jiffies), tmp_log_buf);
100 static int batadv_log_open(struct inode *inode, struct file *file)
102 if (!try_module_get(THIS_MODULE))
105 nonseekable_open(inode, file);
106 file->private_data = inode->i_private;
110 static int batadv_log_release(struct inode *inode, struct file *file)
112 module_put(THIS_MODULE);
116 static int batadv_log_empty(struct batadv_priv_debug_log *debug_log)
118 return !(debug_log->log_start - debug_log->log_end);
121 static ssize_t batadv_log_read(struct file *file, char __user *buf,
122 size_t count, loff_t *ppos)
124 struct batadv_priv *bat_priv = file->private_data;
125 struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
130 if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log))
139 if (!access_ok(VERIFY_WRITE, buf, count))
142 error = wait_event_interruptible(debug_log->queue_wait,
143 (!batadv_log_empty(debug_log)));
148 spin_lock_bh(&debug_log->lock);
150 while ((!error) && (i < count) &&
151 (debug_log->log_start != debug_log->log_end)) {
152 char_addr = batadv_log_char_addr(debug_log,
153 debug_log->log_start);
156 debug_log->log_start++;
158 spin_unlock_bh(&debug_log->lock);
160 error = __put_user(c, buf);
162 spin_lock_bh(&debug_log->lock);
168 spin_unlock_bh(&debug_log->lock);
176 static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
178 struct batadv_priv *bat_priv = file->private_data;
179 struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
181 poll_wait(file, &debug_log->queue_wait, wait);
183 if (!batadv_log_empty(debug_log))
184 return POLLIN | POLLRDNORM;
189 static const struct file_operations batadv_log_fops = {
190 .open = batadv_log_open,
191 .release = batadv_log_release,
192 .read = batadv_log_read,
193 .poll = batadv_log_poll,
197 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
201 if (!bat_priv->debug_dir)
204 bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
205 if (!bat_priv->debug_log)
208 spin_lock_init(&bat_priv->debug_log->lock);
209 init_waitqueue_head(&bat_priv->debug_log->queue_wait);
211 d = debugfs_create_file("log", S_IFREG | S_IRUSR,
212 bat_priv->debug_dir, bat_priv,
223 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
225 kfree(bat_priv->debug_log);
226 bat_priv->debug_log = NULL;
228 #else /* CONFIG_BATMAN_ADV_DEBUG */
229 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
234 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
240 static int batadv_algorithms_open(struct inode *inode, struct file *file)
242 return single_open(file, batadv_algo_seq_print_text, NULL);
245 static int batadv_originators_open(struct inode *inode, struct file *file)
247 struct net_device *net_dev = (struct net_device *)inode->i_private;
248 return single_open(file, batadv_orig_seq_print_text, net_dev);
252 * batadv_originators_hardif_open - handles debugfs output for the
253 * originator table of an hard interface
254 * @inode: inode pointer to debugfs file
255 * @file: pointer to the seq_file
257 static int batadv_originators_hardif_open(struct inode *inode,
260 struct net_device *net_dev = (struct net_device *)inode->i_private;
261 return single_open(file, batadv_orig_hardif_seq_print_text, net_dev);
264 static int batadv_gateways_open(struct inode *inode, struct file *file)
266 struct net_device *net_dev = (struct net_device *)inode->i_private;
267 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
270 static int batadv_transtable_global_open(struct inode *inode, struct file *file)
272 struct net_device *net_dev = (struct net_device *)inode->i_private;
273 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
276 #ifdef CONFIG_BATMAN_ADV_BLA
277 static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
279 struct net_device *net_dev = (struct net_device *)inode->i_private;
280 return single_open(file, batadv_bla_claim_table_seq_print_text,
284 static int batadv_bla_backbone_table_open(struct inode *inode,
287 struct net_device *net_dev = (struct net_device *)inode->i_private;
288 return single_open(file, batadv_bla_backbone_table_seq_print_text,
294 #ifdef CONFIG_BATMAN_ADV_DAT
296 * batadv_dat_cache_open - Prepare file handler for reads from dat_chache
297 * @inode: inode which was opened
298 * @file: file handle to be initialized
300 static int batadv_dat_cache_open(struct inode *inode, struct file *file)
302 struct net_device *net_dev = (struct net_device *)inode->i_private;
303 return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
307 static int batadv_transtable_local_open(struct inode *inode, struct file *file)
309 struct net_device *net_dev = (struct net_device *)inode->i_private;
310 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
313 struct batadv_debuginfo {
314 struct attribute attr;
315 const struct file_operations fops;
318 #ifdef CONFIG_BATMAN_ADV_NC
319 static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
321 struct net_device *net_dev = (struct net_device *)inode->i_private;
322 return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
326 #define BATADV_DEBUGINFO(_name, _mode, _open) \
327 struct batadv_debuginfo batadv_debuginfo_##_name = { \
328 .attr = { .name = __stringify(_name), \
330 .fops = { .owner = THIS_MODULE, \
333 .llseek = seq_lseek, \
334 .release = single_release, \
338 /* the following attributes are general and therefore they will be directly
339 * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
341 static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
343 static struct batadv_debuginfo *batadv_general_debuginfos[] = {
344 &batadv_debuginfo_routing_algos,
348 /* The following attributes are per soft interface */
349 static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
350 static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
351 static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
352 batadv_transtable_global_open);
353 #ifdef CONFIG_BATMAN_ADV_BLA
354 static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
355 static BATADV_DEBUGINFO(bla_backbone_table, S_IRUGO,
356 batadv_bla_backbone_table_open);
358 #ifdef CONFIG_BATMAN_ADV_DAT
359 static BATADV_DEBUGINFO(dat_cache, S_IRUGO, batadv_dat_cache_open);
361 static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
362 batadv_transtable_local_open);
363 #ifdef CONFIG_BATMAN_ADV_NC
364 static BATADV_DEBUGINFO(nc_nodes, S_IRUGO, batadv_nc_nodes_open);
367 static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
368 &batadv_debuginfo_originators,
369 &batadv_debuginfo_gateways,
370 &batadv_debuginfo_transtable_global,
371 #ifdef CONFIG_BATMAN_ADV_BLA
372 &batadv_debuginfo_bla_claim_table,
373 &batadv_debuginfo_bla_backbone_table,
375 #ifdef CONFIG_BATMAN_ADV_DAT
376 &batadv_debuginfo_dat_cache,
378 &batadv_debuginfo_transtable_local,
379 #ifdef CONFIG_BATMAN_ADV_NC
380 &batadv_debuginfo_nc_nodes,
385 #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
386 struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
388 .name = __stringify(_name), \
392 .owner = THIS_MODULE, \
395 .llseek = seq_lseek, \
396 .release = single_release, \
399 static BATADV_HARDIF_DEBUGINFO(originators, S_IRUGO,
400 batadv_originators_hardif_open);
402 static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
403 &batadv_hardif_debuginfo_originators,
407 void batadv_debugfs_init(void)
409 struct batadv_debuginfo **bat_debug;
412 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
413 if (batadv_debugfs == ERR_PTR(-ENODEV))
414 batadv_debugfs = NULL;
419 for (bat_debug = batadv_general_debuginfos; *bat_debug; ++bat_debug) {
420 file = debugfs_create_file(((*bat_debug)->attr).name,
421 S_IFREG | ((*bat_debug)->attr).mode,
422 batadv_debugfs, NULL,
423 &(*bat_debug)->fops);
425 pr_err("Can't add general debugfs file: %s\n",
426 ((*bat_debug)->attr).name);
433 debugfs_remove_recursive(batadv_debugfs);
434 batadv_debugfs = NULL;
437 void batadv_debugfs_destroy(void)
439 debugfs_remove_recursive(batadv_debugfs);
440 batadv_debugfs = NULL;
444 * batadv_debugfs_add_hardif - creates the base directory for a hard interface
446 * @hard_iface: hard interface which should be added.
448 int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
450 struct batadv_debuginfo **bat_debug;
456 hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
458 if (!hard_iface->debug_dir)
461 for (bat_debug = batadv_hardif_debuginfos; *bat_debug; ++bat_debug) {
462 file = debugfs_create_file(((*bat_debug)->attr).name,
463 S_IFREG | ((*bat_debug)->attr).mode,
464 hard_iface->debug_dir,
466 &(*bat_debug)->fops);
473 debugfs_remove_recursive(hard_iface->debug_dir);
474 hard_iface->debug_dir = NULL;
476 #ifdef CONFIG_DEBUG_FS
480 #endif /* CONFIG_DEBUG_FS */
484 * batadv_debugfs_del_hardif - delete the base directory for a hard interface
486 * @hard_iface: hard interface which is deleted.
488 void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
490 if (batadv_debugfs) {
491 debugfs_remove_recursive(hard_iface->debug_dir);
492 hard_iface->debug_dir = NULL;
496 int batadv_debugfs_add_meshif(struct net_device *dev)
498 struct batadv_priv *bat_priv = netdev_priv(dev);
499 struct batadv_debuginfo **bat_debug;
505 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
506 if (!bat_priv->debug_dir)
509 if (batadv_socket_setup(bat_priv) < 0)
512 if (batadv_debug_log_setup(bat_priv) < 0)
515 for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
516 file = debugfs_create_file(((*bat_debug)->attr).name,
517 S_IFREG | ((*bat_debug)->attr).mode,
519 dev, &(*bat_debug)->fops);
521 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
522 dev->name, ((*bat_debug)->attr).name);
527 if (batadv_nc_init_debugfs(bat_priv) < 0)
532 debugfs_remove_recursive(bat_priv->debug_dir);
533 bat_priv->debug_dir = NULL;
535 #ifdef CONFIG_DEBUG_FS
539 #endif /* CONFIG_DEBUG_FS */
542 void batadv_debugfs_del_meshif(struct net_device *dev)
544 struct batadv_priv *bat_priv = netdev_priv(dev);
546 batadv_debug_log_cleanup(bat_priv);
548 if (batadv_debugfs) {
549 debugfs_remove_recursive(bat_priv->debug_dir);
550 bat_priv->debug_dir = NULL;