blob: 71b225c1b5cac39bb7d7bbd6109ee118bac7093b [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann567db7b2012-01-01 00:41:38 +01002 * Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner
5 *
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.
9 *
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.
14 *
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
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23
24#include <linux/debugfs.h>
25
26#include "bat_debugfs.h"
27#include "translation-table.h"
28#include "originator.h"
29#include "hard-interface.h"
30#include "gateway_common.h"
31#include "gateway_client.h"
32#include "soft-interface.h"
33#include "vis.h"
34#include "icmp_socket.h"
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +010035#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036
37static struct dentry *bat_debugfs;
38
39#ifdef CONFIG_BATMAN_ADV_DEBUG
40#define LOG_BUFF_MASK (log_buff_len-1)
41#define LOG_BUFF(idx) (debug_log->log_buff[(idx) & LOG_BUFF_MASK])
42
43static int log_buff_len = LOG_BUF_LEN;
44
45static void emit_log_char(struct debug_log *debug_log, char c)
46{
47 LOG_BUFF(debug_log->log_end) = c;
48 debug_log->log_end++;
49
50 if (debug_log->log_end - debug_log->log_start > log_buff_len)
51 debug_log->log_start = debug_log->log_end - log_buff_len;
52}
53
Sven Eckelmannd3a547b2011-05-14 23:14:46 +020054__printf(2, 3)
Sven Eckelmann747e4222011-05-14 23:14:50 +020055static int fdebug_log(struct debug_log *debug_log, const char *fmt, ...)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057 va_list args;
58 static char debug_log_buf[256];
59 char *p;
60
61 if (!debug_log)
62 return 0;
63
64 spin_lock_bh(&debug_log->lock);
65 va_start(args, fmt);
Sven Eckelmann1299bda2011-01-27 13:48:54 +010066 vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000067 va_end(args);
68
69 for (p = debug_log_buf; *p != 0; p++)
70 emit_log_char(debug_log, *p);
71
72 spin_unlock_bh(&debug_log->lock);
73
74 wake_up(&debug_log->queue_wait);
75
76 return 0;
77}
78
Sven Eckelmann40a072d2012-05-12 02:09:23 +020079int batadv_debug_log(struct bat_priv *bat_priv, const char *fmt, ...)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000080{
81 va_list args;
82 char tmp_log_buf[256];
83
84 va_start(args, fmt);
85 vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
Marek Lindner0b0094e2012-03-01 15:35:20 +080086 fdebug_log(bat_priv->debug_log, "[%10u] %s",
87 jiffies_to_msecs(jiffies), tmp_log_buf);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000088 va_end(args);
89
90 return 0;
91}
92
93static int log_open(struct inode *inode, struct file *file)
94{
95 nonseekable_open(inode, file);
96 file->private_data = inode->i_private;
97 inc_module_count();
98 return 0;
99}
100
101static int log_release(struct inode *inode, struct file *file)
102{
103 dec_module_count();
104 return 0;
105}
106
107static ssize_t log_read(struct file *file, char __user *buf,
108 size_t count, loff_t *ppos)
109{
110 struct bat_priv *bat_priv = file->private_data;
111 struct debug_log *debug_log = bat_priv->debug_log;
112 int error, i = 0;
113 char c;
114
115 if ((file->f_flags & O_NONBLOCK) &&
116 !(debug_log->log_end - debug_log->log_start))
117 return -EAGAIN;
118
Sven Eckelmann16f14b42011-05-14 23:14:48 +0200119 if (!buf)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000120 return -EINVAL;
121
122 if (count == 0)
123 return 0;
124
125 if (!access_ok(VERIFY_WRITE, buf, count))
126 return -EFAULT;
127
128 error = wait_event_interruptible(debug_log->queue_wait,
129 (debug_log->log_start - debug_log->log_end));
130
131 if (error)
132 return error;
133
134 spin_lock_bh(&debug_log->lock);
135
136 while ((!error) && (i < count) &&
137 (debug_log->log_start != debug_log->log_end)) {
138 c = LOG_BUFF(debug_log->log_start);
139
140 debug_log->log_start++;
141
142 spin_unlock_bh(&debug_log->lock);
143
144 error = __put_user(c, buf);
145
146 spin_lock_bh(&debug_log->lock);
147
148 buf++;
149 i++;
150
151 }
152
153 spin_unlock_bh(&debug_log->lock);
154
155 if (!error)
156 return i;
157
158 return error;
159}
160
161static unsigned int log_poll(struct file *file, poll_table *wait)
162{
163 struct bat_priv *bat_priv = file->private_data;
164 struct debug_log *debug_log = bat_priv->debug_log;
165
166 poll_wait(file, &debug_log->queue_wait, wait);
167
168 if (debug_log->log_end - debug_log->log_start)
169 return POLLIN | POLLRDNORM;
170
171 return 0;
172}
173
174static const struct file_operations log_fops = {
175 .open = log_open,
176 .release = log_release,
177 .read = log_read,
178 .poll = log_poll,
179 .llseek = no_llseek,
180};
181
182static int debug_log_setup(struct bat_priv *bat_priv)
183{
184 struct dentry *d;
185
186 if (!bat_priv->debug_dir)
187 goto err;
188
Sven Eckelmann704509b2011-05-14 23:14:54 +0200189 bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190 if (!bat_priv->debug_log)
191 goto err;
192
193 spin_lock_init(&bat_priv->debug_log->lock);
194 init_waitqueue_head(&bat_priv->debug_log->queue_wait);
195
196 d = debugfs_create_file("log", S_IFREG | S_IRUSR,
197 bat_priv->debug_dir, bat_priv, &log_fops);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200198 if (!d)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000199 goto err;
200
201 return 0;
202
203err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200204 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000205}
206
207static void debug_log_cleanup(struct bat_priv *bat_priv)
208{
209 kfree(bat_priv->debug_log);
210 bat_priv->debug_log = NULL;
211}
212#else /* CONFIG_BATMAN_ADV_DEBUG */
213static int debug_log_setup(struct bat_priv *bat_priv)
214{
215 bat_priv->debug_log = NULL;
216 return 0;
217}
218
219static void debug_log_cleanup(struct bat_priv *bat_priv)
220{
221 return;
222}
223#endif
224
Marek Lindner1c280472011-11-28 17:40:17 +0800225static int bat_algorithms_open(struct inode *inode, struct file *file)
226{
227 return single_open(file, bat_algo_seq_print_text, NULL);
228}
229
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230static int originators_open(struct inode *inode, struct file *file)
231{
232 struct net_device *net_dev = (struct net_device *)inode->i_private;
233 return single_open(file, orig_seq_print_text, net_dev);
234}
235
236static int gateways_open(struct inode *inode, struct file *file)
237{
238 struct net_device *net_dev = (struct net_device *)inode->i_private;
239 return single_open(file, gw_client_seq_print_text, net_dev);
240}
241
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000242static int transtable_global_open(struct inode *inode, struct file *file)
243{
244 struct net_device *net_dev = (struct net_device *)inode->i_private;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200245 return single_open(file, tt_global_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246}
247
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100248#ifdef CONFIG_BATMAN_ADV_BLA
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +0100249static int bla_claim_table_open(struct inode *inode, struct file *file)
250{
251 struct net_device *net_dev = (struct net_device *)inode->i_private;
Sven Eckelmann08adf152012-05-12 13:38:47 +0200252 return single_open(file, batadv_bla_claim_table_seq_print_text,
253 net_dev);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +0100254}
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100255#endif
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +0100256
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257static int transtable_local_open(struct inode *inode, struct file *file)
258{
259 struct net_device *net_dev = (struct net_device *)inode->i_private;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200260 return single_open(file, tt_local_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000261}
262
263static int vis_data_open(struct inode *inode, struct file *file)
264{
265 struct net_device *net_dev = (struct net_device *)inode->i_private;
266 return single_open(file, vis_seq_print_text, net_dev);
267}
268
269struct bat_debuginfo {
270 struct attribute attr;
271 const struct file_operations fops;
272};
273
274#define BAT_DEBUGINFO(_name, _mode, _open) \
275struct bat_debuginfo bat_debuginfo_##_name = { \
276 .attr = { .name = __stringify(_name), \
277 .mode = _mode, }, \
278 .fops = { .owner = THIS_MODULE, \
279 .open = _open, \
280 .read = seq_read, \
281 .llseek = seq_lseek, \
282 .release = single_release, \
283 } \
284};
285
Marek Lindner1c280472011-11-28 17:40:17 +0800286static BAT_DEBUGINFO(routing_algos, S_IRUGO, bat_algorithms_open);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287static BAT_DEBUGINFO(originators, S_IRUGO, originators_open);
288static BAT_DEBUGINFO(gateways, S_IRUGO, gateways_open);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289static BAT_DEBUGINFO(transtable_global, S_IRUGO, transtable_global_open);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100290#ifdef CONFIG_BATMAN_ADV_BLA
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +0100291static BAT_DEBUGINFO(bla_claim_table, S_IRUGO, bla_claim_table_open);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100292#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293static BAT_DEBUGINFO(transtable_local, S_IRUGO, transtable_local_open);
294static BAT_DEBUGINFO(vis_data, S_IRUGO, vis_data_open);
295
296static struct bat_debuginfo *mesh_debuginfos[] = {
297 &bat_debuginfo_originators,
298 &bat_debuginfo_gateways,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299 &bat_debuginfo_transtable_global,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100300#ifdef CONFIG_BATMAN_ADV_BLA
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +0100301 &bat_debuginfo_bla_claim_table,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100302#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000303 &bat_debuginfo_transtable_local,
304 &bat_debuginfo_vis_data,
305 NULL,
306};
307
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200308void batadv_debugfs_init(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309{
Marek Lindner1c280472011-11-28 17:40:17 +0800310 struct bat_debuginfo *bat_debug;
311 struct dentry *file;
312
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313 bat_debugfs = debugfs_create_dir(DEBUGFS_BAT_SUBDIR, NULL);
314 if (bat_debugfs == ERR_PTR(-ENODEV))
315 bat_debugfs = NULL;
Marek Lindner1c280472011-11-28 17:40:17 +0800316
317 if (!bat_debugfs)
318 goto out;
319
320 bat_debug = &bat_debuginfo_routing_algos;
321 file = debugfs_create_file(bat_debug->attr.name,
322 S_IFREG | bat_debug->attr.mode,
323 bat_debugfs, NULL, &bat_debug->fops);
324 if (!file)
325 pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name);
326
327out:
328 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329}
330
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200331void batadv_debugfs_destroy(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332{
333 if (bat_debugfs) {
334 debugfs_remove_recursive(bat_debugfs);
335 bat_debugfs = NULL;
336 }
337}
338
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200339int batadv_debugfs_add_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340{
341 struct bat_priv *bat_priv = netdev_priv(dev);
342 struct bat_debuginfo **bat_debug;
343 struct dentry *file;
344
345 if (!bat_debugfs)
346 goto out;
347
348 bat_priv->debug_dir = debugfs_create_dir(dev->name, bat_debugfs);
349 if (!bat_priv->debug_dir)
350 goto out;
351
Sven Eckelmann5346c352012-05-05 13:27:28 +0200352 if (bat_socket_setup(bat_priv) < 0)
353 goto rem_attr;
354
355 if (debug_log_setup(bat_priv) < 0)
356 goto rem_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357
358 for (bat_debug = mesh_debuginfos; *bat_debug; ++bat_debug) {
359 file = debugfs_create_file(((*bat_debug)->attr).name,
360 S_IFREG | ((*bat_debug)->attr).mode,
361 bat_priv->debug_dir,
362 dev, &(*bat_debug)->fops);
363 if (!file) {
364 bat_err(dev, "Can't add debugfs file: %s/%s\n",
365 dev->name, ((*bat_debug)->attr).name);
366 goto rem_attr;
367 }
368 }
369
370 return 0;
371rem_attr:
372 debugfs_remove_recursive(bat_priv->debug_dir);
373 bat_priv->debug_dir = NULL;
374out:
375#ifdef CONFIG_DEBUG_FS
376 return -ENOMEM;
377#else
378 return 0;
379#endif /* CONFIG_DEBUG_FS */
380}
381
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200382void batadv_debugfs_del_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383{
384 struct bat_priv *bat_priv = netdev_priv(dev);
385
386 debug_log_cleanup(bat_priv);
387
388 if (bat_debugfs) {
389 debugfs_remove_recursive(bat_priv->debug_dir);
390 bat_priv->debug_dir = NULL;
391 }
392}