1 /* Copyright (C) 2010-2012 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>
24 #include "bat_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"
35 static struct dentry *batadv_debugfs;
37 #ifdef CONFIG_BATMAN_ADV_DEBUG
38 #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
39 #define BATADV_LOG_BUFF(idx) (debug_log->log_buff[(idx) & BATADV_LOG_BUFF_MASK])
41 static int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
43 static void batadv_emit_log_char(struct debug_log *debug_log, char c)
45 BATADV_LOG_BUFF(debug_log->log_end) = c;
48 if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
49 debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
53 static int batadv_fdebug_log(struct debug_log *debug_log, const char *fmt, ...)
56 static char debug_log_buf[256];
62 spin_lock_bh(&debug_log->lock);
64 vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
67 for (p = debug_log_buf; *p != 0; p++)
68 batadv_emit_log_char(debug_log, *p);
70 spin_unlock_bh(&debug_log->lock);
72 wake_up(&debug_log->queue_wait);
77 int batadv_debug_log(struct bat_priv *bat_priv, const char *fmt, ...)
80 char tmp_log_buf[256];
83 vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
84 batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
85 jiffies_to_msecs(jiffies), tmp_log_buf);
91 static int batadv_log_open(struct inode *inode, struct file *file)
93 nonseekable_open(inode, file);
94 file->private_data = inode->i_private;
95 batadv_inc_module_count();
99 static int batadv_log_release(struct inode *inode, struct file *file)
101 batadv_dec_module_count();
105 static ssize_t batadv_log_read(struct file *file, char __user *buf,
106 size_t count, loff_t *ppos)
108 struct bat_priv *bat_priv = file->private_data;
109 struct debug_log *debug_log = bat_priv->debug_log;
113 if ((file->f_flags & O_NONBLOCK) &&
114 !(debug_log->log_end - debug_log->log_start))
123 if (!access_ok(VERIFY_WRITE, buf, count))
126 error = wait_event_interruptible(debug_log->queue_wait,
127 (debug_log->log_start - debug_log->log_end));
132 spin_lock_bh(&debug_log->lock);
134 while ((!error) && (i < count) &&
135 (debug_log->log_start != debug_log->log_end)) {
136 c = BATADV_LOG_BUFF(debug_log->log_start);
138 debug_log->log_start++;
140 spin_unlock_bh(&debug_log->lock);
142 error = __put_user(c, buf);
144 spin_lock_bh(&debug_log->lock);
151 spin_unlock_bh(&debug_log->lock);
159 static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
161 struct bat_priv *bat_priv = file->private_data;
162 struct debug_log *debug_log = bat_priv->debug_log;
164 poll_wait(file, &debug_log->queue_wait, wait);
166 if (debug_log->log_end - debug_log->log_start)
167 return POLLIN | POLLRDNORM;
172 static const struct file_operations batadv_log_fops = {
173 .open = batadv_log_open,
174 .release = batadv_log_release,
175 .read = batadv_log_read,
176 .poll = batadv_log_poll,
180 static int batadv_debug_log_setup(struct bat_priv *bat_priv)
184 if (!bat_priv->debug_dir)
187 bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
188 if (!bat_priv->debug_log)
191 spin_lock_init(&bat_priv->debug_log->lock);
192 init_waitqueue_head(&bat_priv->debug_log->queue_wait);
194 d = debugfs_create_file("log", S_IFREG | S_IRUSR,
195 bat_priv->debug_dir, bat_priv,
206 static void batadv_debug_log_cleanup(struct bat_priv *bat_priv)
208 kfree(bat_priv->debug_log);
209 bat_priv->debug_log = NULL;
211 #else /* CONFIG_BATMAN_ADV_DEBUG */
212 static int batadv_debug_log_setup(struct bat_priv *bat_priv)
214 bat_priv->debug_log = NULL;
218 static void batadv_debug_log_cleanup(struct bat_priv *bat_priv)
224 static int batadv_algorithms_open(struct inode *inode, struct file *file)
226 return single_open(file, batadv_algo_seq_print_text, NULL);
229 static int batadv_originators_open(struct inode *inode, struct file *file)
231 struct net_device *net_dev = (struct net_device *)inode->i_private;
232 return single_open(file, batadv_orig_seq_print_text, net_dev);
235 static int batadv_gateways_open(struct inode *inode, struct file *file)
237 struct net_device *net_dev = (struct net_device *)inode->i_private;
238 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
241 static int batadv_transtable_global_open(struct inode *inode, struct file *file)
243 struct net_device *net_dev = (struct net_device *)inode->i_private;
244 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
247 #ifdef CONFIG_BATMAN_ADV_BLA
248 static int batadv_bla_claim_table_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_bla_claim_table_seq_print_text,
256 static int batadv_transtable_local_open(struct inode *inode, struct file *file)
258 struct net_device *net_dev = (struct net_device *)inode->i_private;
259 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
262 static int batadv_vis_data_open(struct inode *inode, struct file *file)
264 struct net_device *net_dev = (struct net_device *)inode->i_private;
265 return single_open(file, batadv_vis_seq_print_text, net_dev);
268 struct bat_debuginfo {
269 struct attribute attr;
270 const struct file_operations fops;
273 #define BATADV_DEBUGINFO(_name, _mode, _open) \
274 struct bat_debuginfo batadv_debuginfo_##_name = { \
275 .attr = { .name = __stringify(_name), \
277 .fops = { .owner = THIS_MODULE, \
280 .llseek = seq_lseek, \
281 .release = single_release, \
285 static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
286 static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
287 static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
288 static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
289 batadv_transtable_global_open);
290 #ifdef CONFIG_BATMAN_ADV_BLA
291 static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
293 static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
294 batadv_transtable_local_open);
295 static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
297 static struct bat_debuginfo *batadv_mesh_debuginfos[] = {
298 &batadv_debuginfo_originators,
299 &batadv_debuginfo_gateways,
300 &batadv_debuginfo_transtable_global,
301 #ifdef CONFIG_BATMAN_ADV_BLA
302 &batadv_debuginfo_bla_claim_table,
304 &batadv_debuginfo_transtable_local,
305 &batadv_debuginfo_vis_data,
309 void batadv_debugfs_init(void)
311 struct bat_debuginfo *bat_debug;
314 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
315 if (batadv_debugfs == ERR_PTR(-ENODEV))
316 batadv_debugfs = NULL;
321 bat_debug = &batadv_debuginfo_routing_algos;
322 file = debugfs_create_file(bat_debug->attr.name,
323 S_IFREG | bat_debug->attr.mode,
324 batadv_debugfs, NULL, &bat_debug->fops);
326 pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name);
332 void batadv_debugfs_destroy(void)
334 if (batadv_debugfs) {
335 debugfs_remove_recursive(batadv_debugfs);
336 batadv_debugfs = NULL;
340 int batadv_debugfs_add_meshif(struct net_device *dev)
342 struct bat_priv *bat_priv = netdev_priv(dev);
343 struct bat_debuginfo **bat_debug;
349 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
350 if (!bat_priv->debug_dir)
353 if (batadv_socket_setup(bat_priv) < 0)
356 if (batadv_debug_log_setup(bat_priv) < 0)
359 for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
360 file = debugfs_create_file(((*bat_debug)->attr).name,
361 S_IFREG | ((*bat_debug)->attr).mode,
363 dev, &(*bat_debug)->fops);
365 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
366 dev->name, ((*bat_debug)->attr).name);
373 debugfs_remove_recursive(bat_priv->debug_dir);
374 bat_priv->debug_dir = NULL;
376 #ifdef CONFIG_DEBUG_FS
380 #endif /* CONFIG_DEBUG_FS */
383 void batadv_debugfs_del_meshif(struct net_device *dev)
385 struct bat_priv *bat_priv = netdev_priv(dev);
387 batadv_debug_log_cleanup(bat_priv);
389 if (batadv_debugfs) {
390 debugfs_remove_recursive(bat_priv->debug_dir);
391 bat_priv->debug_dir = NULL;