blob: 4001c57a25e495de1ef9358aebfce97daf68ca27 [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Marek Lindner
4 *
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.
8 *
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.
13 *
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
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21
22#include <linux/debugfs.h>
23
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"
31#include "vis.h"
32#include "icmp_socket.h"
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +010033#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034
35static struct dentry *bat_debugfs;
36
37#ifdef CONFIG_BATMAN_ADV_DEBUG
38#define LOG_BUFF_MASK (log_buff_len-1)
39#define LOG_BUFF(idx) (debug_log->log_buff[(idx) & LOG_BUFF_MASK])
40
41static int log_buff_len = LOG_BUF_LEN;
42
43static void emit_log_char(struct debug_log *debug_log, char c)
44{
45 LOG_BUFF(debug_log->log_end) = c;
46 debug_log->log_end++;
47
48 if (debug_log->log_end - debug_log->log_start > log_buff_len)
49 debug_log->log_start = debug_log->log_end - log_buff_len;
50}
51
Sven Eckelmannd3a547b2011-05-14 23:14:46 +020052__printf(2, 3)
Sven Eckelmann747e4222011-05-14 23:14:50 +020053static int fdebug_log(struct debug_log *debug_log, const char *fmt, ...)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000055 va_list args;
56 static char debug_log_buf[256];
57 char *p;
58
59 if (!debug_log)
60 return 0;
61
62 spin_lock_bh(&debug_log->lock);
63 va_start(args, fmt);
Sven Eckelmann1299bda2011-01-27 13:48:54 +010064 vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000065 va_end(args);
66
67 for (p = debug_log_buf; *p != 0; p++)
68 emit_log_char(debug_log, *p);
69
70 spin_unlock_bh(&debug_log->lock);
71
72 wake_up(&debug_log->queue_wait);
73
74 return 0;
75}
76
Sven Eckelmann40a072d2012-05-12 02:09:23 +020077int batadv_debug_log(struct bat_priv *bat_priv, const char *fmt, ...)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000078{
79 va_list args;
80 char tmp_log_buf[256];
81
82 va_start(args, fmt);
83 vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
Marek Lindner0b0094e2012-03-01 15:35:20 +080084 fdebug_log(bat_priv->debug_log, "[%10u] %s",
85 jiffies_to_msecs(jiffies), tmp_log_buf);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000086 va_end(args);
87
88 return 0;
89}
90
91static int log_open(struct inode *inode, struct file *file)
92{
93 nonseekable_open(inode, file);
94 file->private_data = inode->i_private;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020095 batadv_inc_module_count();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000096 return 0;
97}
98
99static int log_release(struct inode *inode, struct file *file)
100{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200101 batadv_dec_module_count();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000102 return 0;
103}
104
105static ssize_t log_read(struct file *file, char __user *buf,
106 size_t count, loff_t *ppos)
107{
108 struct bat_priv *bat_priv = file->private_data;
109 struct debug_log *debug_log = bat_priv->debug_log;
110 int error, i = 0;
111 char c;
112
113 if ((file->f_flags & O_NONBLOCK) &&
114 !(debug_log->log_end - debug_log->log_start))
115 return -EAGAIN;
116
Sven Eckelmann16f14b42011-05-14 23:14:48 +0200117 if (!buf)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000118 return -EINVAL;
119
120 if (count == 0)
121 return 0;
122
123 if (!access_ok(VERIFY_WRITE, buf, count))
124 return -EFAULT;
125
126 error = wait_event_interruptible(debug_log->queue_wait,
127 (debug_log->log_start - debug_log->log_end));
128
129 if (error)
130 return error;
131
132 spin_lock_bh(&debug_log->lock);
133
134 while ((!error) && (i < count) &&
135 (debug_log->log_start != debug_log->log_end)) {
136 c = LOG_BUFF(debug_log->log_start);
137
138 debug_log->log_start++;
139
140 spin_unlock_bh(&debug_log->lock);
141
142 error = __put_user(c, buf);
143
144 spin_lock_bh(&debug_log->lock);
145
146 buf++;
147 i++;
148
149 }
150
151 spin_unlock_bh(&debug_log->lock);
152
153 if (!error)
154 return i;
155
156 return error;
157}
158
159static unsigned int log_poll(struct file *file, poll_table *wait)
160{
161 struct bat_priv *bat_priv = file->private_data;
162 struct debug_log *debug_log = bat_priv->debug_log;
163
164 poll_wait(file, &debug_log->queue_wait, wait);
165
166 if (debug_log->log_end - debug_log->log_start)
167 return POLLIN | POLLRDNORM;
168
169 return 0;
170}
171
172static const struct file_operations log_fops = {
173 .open = log_open,
174 .release = log_release,
175 .read = log_read,
176 .poll = log_poll,
177 .llseek = no_llseek,
178};
179
180static int debug_log_setup(struct bat_priv *bat_priv)
181{
182 struct dentry *d;
183
184 if (!bat_priv->debug_dir)
185 goto err;
186
Sven Eckelmann704509b2011-05-14 23:14:54 +0200187 bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188 if (!bat_priv->debug_log)
189 goto err;
190
191 spin_lock_init(&bat_priv->debug_log->lock);
192 init_waitqueue_head(&bat_priv->debug_log->queue_wait);
193
194 d = debugfs_create_file("log", S_IFREG | S_IRUSR,
195 bat_priv->debug_dir, bat_priv, &log_fops);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200196 if (!d)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197 goto err;
198
199 return 0;
200
201err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200202 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000203}
204
205static void debug_log_cleanup(struct bat_priv *bat_priv)
206{
207 kfree(bat_priv->debug_log);
208 bat_priv->debug_log = NULL;
209}
210#else /* CONFIG_BATMAN_ADV_DEBUG */
211static int debug_log_setup(struct bat_priv *bat_priv)
212{
213 bat_priv->debug_log = NULL;
214 return 0;
215}
216
217static void debug_log_cleanup(struct bat_priv *bat_priv)
218{
219 return;
220}
221#endif
222
Marek Lindner1c280472011-11-28 17:40:17 +0800223static int bat_algorithms_open(struct inode *inode, struct file *file)
224{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200225 return single_open(file, batadv_algo_seq_print_text, NULL);
Marek Lindner1c280472011-11-28 17:40:17 +0800226}
227
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228static int originators_open(struct inode *inode, struct file *file)
229{
230 struct net_device *net_dev = (struct net_device *)inode->i_private;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200231 return single_open(file, batadv_orig_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232}
233
234static int gateways_open(struct inode *inode, struct file *file)
235{
236 struct net_device *net_dev = (struct net_device *)inode->i_private;
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200237 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000238}
239
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240static int transtable_global_open(struct inode *inode, struct file *file)
241{
242 struct net_device *net_dev = (struct net_device *)inode->i_private;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200243 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244}
245
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100246#ifdef CONFIG_BATMAN_ADV_BLA
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +0100247static int bla_claim_table_open(struct inode *inode, struct file *file)
248{
249 struct net_device *net_dev = (struct net_device *)inode->i_private;
Sven Eckelmann08adf152012-05-12 13:38:47 +0200250 return single_open(file, batadv_bla_claim_table_seq_print_text,
251 net_dev);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +0100252}
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100253#endif
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +0100254
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255static int transtable_local_open(struct inode *inode, struct file *file)
256{
257 struct net_device *net_dev = (struct net_device *)inode->i_private;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200258 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259}
260
261static int vis_data_open(struct inode *inode, struct file *file)
262{
263 struct net_device *net_dev = (struct net_device *)inode->i_private;
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200264 return single_open(file, batadv_vis_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265}
266
267struct bat_debuginfo {
268 struct attribute attr;
269 const struct file_operations fops;
270};
271
272#define BAT_DEBUGINFO(_name, _mode, _open) \
273struct bat_debuginfo bat_debuginfo_##_name = { \
274 .attr = { .name = __stringify(_name), \
275 .mode = _mode, }, \
276 .fops = { .owner = THIS_MODULE, \
277 .open = _open, \
278 .read = seq_read, \
279 .llseek = seq_lseek, \
280 .release = single_release, \
281 } \
282};
283
Marek Lindner1c280472011-11-28 17:40:17 +0800284static BAT_DEBUGINFO(routing_algos, S_IRUGO, bat_algorithms_open);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285static BAT_DEBUGINFO(originators, S_IRUGO, originators_open);
286static BAT_DEBUGINFO(gateways, S_IRUGO, gateways_open);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287static BAT_DEBUGINFO(transtable_global, S_IRUGO, transtable_global_open);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100288#ifdef CONFIG_BATMAN_ADV_BLA
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +0100289static BAT_DEBUGINFO(bla_claim_table, S_IRUGO, bla_claim_table_open);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100290#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000291static BAT_DEBUGINFO(transtable_local, S_IRUGO, transtable_local_open);
292static BAT_DEBUGINFO(vis_data, S_IRUGO, vis_data_open);
293
294static struct bat_debuginfo *mesh_debuginfos[] = {
295 &bat_debuginfo_originators,
296 &bat_debuginfo_gateways,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297 &bat_debuginfo_transtable_global,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100298#ifdef CONFIG_BATMAN_ADV_BLA
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +0100299 &bat_debuginfo_bla_claim_table,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100300#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301 &bat_debuginfo_transtable_local,
302 &bat_debuginfo_vis_data,
303 NULL,
304};
305
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200306void batadv_debugfs_init(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307{
Marek Lindner1c280472011-11-28 17:40:17 +0800308 struct bat_debuginfo *bat_debug;
309 struct dentry *file;
310
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311 bat_debugfs = debugfs_create_dir(DEBUGFS_BAT_SUBDIR, NULL);
312 if (bat_debugfs == ERR_PTR(-ENODEV))
313 bat_debugfs = NULL;
Marek Lindner1c280472011-11-28 17:40:17 +0800314
315 if (!bat_debugfs)
316 goto out;
317
318 bat_debug = &bat_debuginfo_routing_algos;
319 file = debugfs_create_file(bat_debug->attr.name,
320 S_IFREG | bat_debug->attr.mode,
321 bat_debugfs, NULL, &bat_debug->fops);
322 if (!file)
323 pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name);
324
325out:
326 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327}
328
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200329void batadv_debugfs_destroy(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330{
331 if (bat_debugfs) {
332 debugfs_remove_recursive(bat_debugfs);
333 bat_debugfs = NULL;
334 }
335}
336
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200337int batadv_debugfs_add_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338{
339 struct bat_priv *bat_priv = netdev_priv(dev);
340 struct bat_debuginfo **bat_debug;
341 struct dentry *file;
342
343 if (!bat_debugfs)
344 goto out;
345
346 bat_priv->debug_dir = debugfs_create_dir(dev->name, bat_debugfs);
347 if (!bat_priv->debug_dir)
348 goto out;
349
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200350 if (batadv_socket_setup(bat_priv) < 0)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200351 goto rem_attr;
352
353 if (debug_log_setup(bat_priv) < 0)
354 goto rem_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355
356 for (bat_debug = mesh_debuginfos; *bat_debug; ++bat_debug) {
357 file = debugfs_create_file(((*bat_debug)->attr).name,
358 S_IFREG | ((*bat_debug)->attr).mode,
359 bat_priv->debug_dir,
360 dev, &(*bat_debug)->fops);
361 if (!file) {
362 bat_err(dev, "Can't add debugfs file: %s/%s\n",
363 dev->name, ((*bat_debug)->attr).name);
364 goto rem_attr;
365 }
366 }
367
368 return 0;
369rem_attr:
370 debugfs_remove_recursive(bat_priv->debug_dir);
371 bat_priv->debug_dir = NULL;
372out:
373#ifdef CONFIG_DEBUG_FS
374 return -ENOMEM;
375#else
376 return 0;
377#endif /* CONFIG_DEBUG_FS */
378}
379
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200380void batadv_debugfs_del_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381{
382 struct bat_priv *bat_priv = netdev_priv(dev);
383
384 debug_log_cleanup(bat_priv);
385
386 if (bat_debugfs) {
387 debugfs_remove_recursive(bat_priv->debug_dir);
388 bat_priv->debug_dir = NULL;
389 }
390}