blob: abaeec5f6247dc8e486bb9bb1b0511551984fff2 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2010-2011 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"
35
36static struct dentry *bat_debugfs;
37
38#ifdef CONFIG_BATMAN_ADV_DEBUG
39#define LOG_BUFF_MASK (log_buff_len-1)
40#define LOG_BUFF(idx) (debug_log->log_buff[(idx) & LOG_BUFF_MASK])
41
42static int log_buff_len = LOG_BUF_LEN;
43
44static void emit_log_char(struct debug_log *debug_log, char c)
45{
46 LOG_BUFF(debug_log->log_end) = c;
47 debug_log->log_end++;
48
49 if (debug_log->log_end - debug_log->log_start > log_buff_len)
50 debug_log->log_start = debug_log->log_end - log_buff_len;
51}
52
53static int fdebug_log(struct debug_log *debug_log, char *fmt, ...)
54{
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
77int debug_log(struct bat_priv *bat_priv, char *fmt, ...)
78{
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);
84 fdebug_log(bat_priv->debug_log, "[%10u] %s",
85 (jiffies / HZ), tmp_log_buf);
86 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;
95 inc_module_count();
96 return 0;
97}
98
99static int log_release(struct inode *inode, struct file *file)
100{
101 dec_module_count();
102 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
117 if ((!buf) || (count < 0))
118 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
187 bat_priv->debug_log = kzalloc(sizeof(struct debug_log), GFP_ATOMIC);
188 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);
196 if (d)
197 goto err;
198
199 return 0;
200
201err:
202 return 1;
203}
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
223static int originators_open(struct inode *inode, struct file *file)
224{
225 struct net_device *net_dev = (struct net_device *)inode->i_private;
226 return single_open(file, orig_seq_print_text, net_dev);
227}
228
229static int gateways_open(struct inode *inode, struct file *file)
230{
231 struct net_device *net_dev = (struct net_device *)inode->i_private;
232 return single_open(file, gw_client_seq_print_text, net_dev);
233}
234
235static int softif_neigh_open(struct inode *inode, struct file *file)
236{
237 struct net_device *net_dev = (struct net_device *)inode->i_private;
238 return single_open(file, softif_neigh_seq_print_text, net_dev);
239}
240
241static int transtable_global_open(struct inode *inode, struct file *file)
242{
243 struct net_device *net_dev = (struct net_device *)inode->i_private;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200244 return single_open(file, tt_global_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245}
246
247static int transtable_local_open(struct inode *inode, struct file *file)
248{
249 struct net_device *net_dev = (struct net_device *)inode->i_private;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200250 return single_open(file, tt_local_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251}
252
253static int vis_data_open(struct inode *inode, struct file *file)
254{
255 struct net_device *net_dev = (struct net_device *)inode->i_private;
256 return single_open(file, vis_seq_print_text, net_dev);
257}
258
259struct bat_debuginfo {
260 struct attribute attr;
261 const struct file_operations fops;
262};
263
264#define BAT_DEBUGINFO(_name, _mode, _open) \
265struct bat_debuginfo bat_debuginfo_##_name = { \
266 .attr = { .name = __stringify(_name), \
267 .mode = _mode, }, \
268 .fops = { .owner = THIS_MODULE, \
269 .open = _open, \
270 .read = seq_read, \
271 .llseek = seq_lseek, \
272 .release = single_release, \
273 } \
274};
275
276static BAT_DEBUGINFO(originators, S_IRUGO, originators_open);
277static BAT_DEBUGINFO(gateways, S_IRUGO, gateways_open);
278static BAT_DEBUGINFO(softif_neigh, S_IRUGO, softif_neigh_open);
279static BAT_DEBUGINFO(transtable_global, S_IRUGO, transtable_global_open);
280static BAT_DEBUGINFO(transtable_local, S_IRUGO, transtable_local_open);
281static BAT_DEBUGINFO(vis_data, S_IRUGO, vis_data_open);
282
283static struct bat_debuginfo *mesh_debuginfos[] = {
284 &bat_debuginfo_originators,
285 &bat_debuginfo_gateways,
286 &bat_debuginfo_softif_neigh,
287 &bat_debuginfo_transtable_global,
288 &bat_debuginfo_transtable_local,
289 &bat_debuginfo_vis_data,
290 NULL,
291};
292
293void debugfs_init(void)
294{
295 bat_debugfs = debugfs_create_dir(DEBUGFS_BAT_SUBDIR, NULL);
296 if (bat_debugfs == ERR_PTR(-ENODEV))
297 bat_debugfs = NULL;
298}
299
300void debugfs_destroy(void)
301{
302 if (bat_debugfs) {
303 debugfs_remove_recursive(bat_debugfs);
304 bat_debugfs = NULL;
305 }
306}
307
308int debugfs_add_meshif(struct net_device *dev)
309{
310 struct bat_priv *bat_priv = netdev_priv(dev);
311 struct bat_debuginfo **bat_debug;
312 struct dentry *file;
313
314 if (!bat_debugfs)
315 goto out;
316
317 bat_priv->debug_dir = debugfs_create_dir(dev->name, bat_debugfs);
318 if (!bat_priv->debug_dir)
319 goto out;
320
321 bat_socket_setup(bat_priv);
322 debug_log_setup(bat_priv);
323
324 for (bat_debug = mesh_debuginfos; *bat_debug; ++bat_debug) {
325 file = debugfs_create_file(((*bat_debug)->attr).name,
326 S_IFREG | ((*bat_debug)->attr).mode,
327 bat_priv->debug_dir,
328 dev, &(*bat_debug)->fops);
329 if (!file) {
330 bat_err(dev, "Can't add debugfs file: %s/%s\n",
331 dev->name, ((*bat_debug)->attr).name);
332 goto rem_attr;
333 }
334 }
335
336 return 0;
337rem_attr:
338 debugfs_remove_recursive(bat_priv->debug_dir);
339 bat_priv->debug_dir = NULL;
340out:
341#ifdef CONFIG_DEBUG_FS
342 return -ENOMEM;
343#else
344 return 0;
345#endif /* CONFIG_DEBUG_FS */
346}
347
348void debugfs_del_meshif(struct net_device *dev)
349{
350 struct bat_priv *bat_priv = netdev_priv(dev);
351
352 debug_log_cleanup(bat_priv);
353
354 if (bat_debugfs) {
355 debugfs_remove_recursive(bat_priv->debug_dir);
356 bat_priv->debug_dir = NULL;
357 }
358}