batman-adv: Prefix main local static functions with batadv_
[linux-block.git] / net / batman-adv / bat_debugfs.c
CommitLineData
9cfc7bd6 1/* Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
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
c6c8fea2
SE
18 */
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"
9bf8e4d4 33#include "bridge_loop_avoidance.h"
c6c8fea2 34
9e466250 35static struct dentry *batadv_debugfs;
c6c8fea2
SE
36
37#ifdef CONFIG_BATMAN_ADV_DEBUG
9e466250 38#define LOG_BUFF_MASK (batadv_log_buff_len - 1)
c6c8fea2
SE
39#define LOG_BUFF(idx) (debug_log->log_buff[(idx) & LOG_BUFF_MASK])
40
9e466250 41static int batadv_log_buff_len = LOG_BUF_LEN;
c6c8fea2 42
9e466250 43static void batadv_emit_log_char(struct debug_log *debug_log, char c)
c6c8fea2
SE
44{
45 LOG_BUFF(debug_log->log_end) = c;
46 debug_log->log_end++;
47
9e466250
SE
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;
c6c8fea2
SE
50}
51
d3a547be 52__printf(2, 3)
9e466250 53static int batadv_fdebug_log(struct debug_log *debug_log, const char *fmt, ...)
c6c8fea2 54{
c6c8fea2
SE
55 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);
1299bdaa 64 vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
c6c8fea2
SE
65 va_end(args);
66
67 for (p = debug_log_buf; *p != 0; p++)
9e466250 68 batadv_emit_log_char(debug_log, *p);
c6c8fea2
SE
69
70 spin_unlock_bh(&debug_log->lock);
71
72 wake_up(&debug_log->queue_wait);
73
74 return 0;
75}
76
40a072d7 77int batadv_debug_log(struct bat_priv *bat_priv, const char *fmt, ...)
c6c8fea2
SE
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);
9e466250
SE
84 batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
85 jiffies_to_msecs(jiffies), tmp_log_buf);
c6c8fea2
SE
86 va_end(args);
87
88 return 0;
89}
90
9e466250 91static int batadv_log_open(struct inode *inode, struct file *file)
c6c8fea2
SE
92{
93 nonseekable_open(inode, file);
94 file->private_data = inode->i_private;
3193e8fd 95 batadv_inc_module_count();
c6c8fea2
SE
96 return 0;
97}
98
9e466250 99static int batadv_log_release(struct inode *inode, struct file *file)
c6c8fea2 100{
3193e8fd 101 batadv_dec_module_count();
c6c8fea2
SE
102 return 0;
103}
104
9e466250
SE
105static ssize_t batadv_log_read(struct file *file, char __user *buf,
106 size_t count, loff_t *ppos)
c6c8fea2
SE
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
16f14b45 117 if (!buf)
c6c8fea2
SE
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
9e466250 159static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
c6c8fea2
SE
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
9e466250
SE
172static 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,
c6c8fea2
SE
177 .llseek = no_llseek,
178};
179
9e466250 180static int batadv_debug_log_setup(struct bat_priv *bat_priv)
c6c8fea2
SE
181{
182 struct dentry *d;
183
184 if (!bat_priv->debug_dir)
185 goto err;
186
704509b8 187 bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
c6c8fea2
SE
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,
9e466250
SE
195 bat_priv->debug_dir, bat_priv,
196 &batadv_log_fops);
5346c35e 197 if (!d)
c6c8fea2
SE
198 goto err;
199
200 return 0;
201
202err:
5346c35e 203 return -ENOMEM;
c6c8fea2
SE
204}
205
9e466250 206static void batadv_debug_log_cleanup(struct bat_priv *bat_priv)
c6c8fea2
SE
207{
208 kfree(bat_priv->debug_log);
209 bat_priv->debug_log = NULL;
210}
211#else /* CONFIG_BATMAN_ADV_DEBUG */
9e466250 212static int batadv_debug_log_setup(struct bat_priv *bat_priv)
c6c8fea2
SE
213{
214 bat_priv->debug_log = NULL;
215 return 0;
216}
217
9e466250 218static void batadv_debug_log_cleanup(struct bat_priv *bat_priv)
c6c8fea2
SE
219{
220 return;
221}
222#endif
223
9e466250 224static int batadv_algorithms_open(struct inode *inode, struct file *file)
1c280471 225{
3193e8fd 226 return single_open(file, batadv_algo_seq_print_text, NULL);
1c280471
ML
227}
228
9e466250 229static int batadv_originators_open(struct inode *inode, struct file *file)
c6c8fea2
SE
230{
231 struct net_device *net_dev = (struct net_device *)inode->i_private;
7d211efc 232 return single_open(file, batadv_orig_seq_print_text, net_dev);
c6c8fea2
SE
233}
234
9e466250 235static int batadv_gateways_open(struct inode *inode, struct file *file)
c6c8fea2
SE
236{
237 struct net_device *net_dev = (struct net_device *)inode->i_private;
7cf06bc6 238 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
c6c8fea2
SE
239}
240
9e466250 241static int batadv_transtable_global_open(struct inode *inode, struct file *file)
c6c8fea2
SE
242{
243 struct net_device *net_dev = (struct net_device *)inode->i_private;
08c36d3e 244 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
c6c8fea2
SE
245}
246
7a5cc242 247#ifdef CONFIG_BATMAN_ADV_BLA
9e466250 248static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
9bf8e4d4
SW
249{
250 struct net_device *net_dev = (struct net_device *)inode->i_private;
08adf151
SE
251 return single_open(file, batadv_bla_claim_table_seq_print_text,
252 net_dev);
9bf8e4d4 253}
7a5cc242 254#endif
9bf8e4d4 255
9e466250 256static int batadv_transtable_local_open(struct inode *inode, struct file *file)
c6c8fea2
SE
257{
258 struct net_device *net_dev = (struct net_device *)inode->i_private;
08c36d3e 259 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
c6c8fea2
SE
260}
261
9e466250 262static int batadv_vis_data_open(struct inode *inode, struct file *file)
c6c8fea2
SE
263{
264 struct net_device *net_dev = (struct net_device *)inode->i_private;
d0f714f4 265 return single_open(file, batadv_vis_seq_print_text, net_dev);
c6c8fea2
SE
266}
267
268struct bat_debuginfo {
269 struct attribute attr;
270 const struct file_operations fops;
271};
272
9e466250
SE
273#define BAT_DEBUGINFO(_name, _mode, _open) \
274struct bat_debuginfo batadv_debuginfo_##_name = { \
275 .attr = { .name = __stringify(_name), \
276 .mode = _mode, }, \
277 .fops = { .owner = THIS_MODULE, \
278 .open = _open, \
279 .read = seq_read, \
280 .llseek = seq_lseek, \
281 .release = single_release, \
282 } \
c6c8fea2
SE
283};
284
9e466250
SE
285static BAT_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
286static BAT_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
287static BAT_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
288static BAT_DEBUGINFO(transtable_global, S_IRUGO, batadv_transtable_global_open);
7a5cc242 289#ifdef CONFIG_BATMAN_ADV_BLA
9e466250 290static BAT_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
7a5cc242 291#endif
9e466250
SE
292static BAT_DEBUGINFO(transtable_local, S_IRUGO, batadv_transtable_local_open);
293static BAT_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
c6c8fea2 294
9e466250
SE
295static struct bat_debuginfo *batadv_mesh_debuginfos[] = {
296 &batadv_debuginfo_originators,
297 &batadv_debuginfo_gateways,
298 &batadv_debuginfo_transtable_global,
7a5cc242 299#ifdef CONFIG_BATMAN_ADV_BLA
9e466250 300 &batadv_debuginfo_bla_claim_table,
7a5cc242 301#endif
9e466250
SE
302 &batadv_debuginfo_transtable_local,
303 &batadv_debuginfo_vis_data,
c6c8fea2
SE
304 NULL,
305};
306
40a072d7 307void batadv_debugfs_init(void)
c6c8fea2 308{
1c280471
ML
309 struct bat_debuginfo *bat_debug;
310 struct dentry *file;
311
9e466250
SE
312 batadv_debugfs = debugfs_create_dir(DEBUGFS_BAT_SUBDIR, NULL);
313 if (batadv_debugfs == ERR_PTR(-ENODEV))
314 batadv_debugfs = NULL;
1c280471 315
9e466250 316 if (!batadv_debugfs)
1c280471
ML
317 goto out;
318
9e466250 319 bat_debug = &batadv_debuginfo_routing_algos;
1c280471
ML
320 file = debugfs_create_file(bat_debug->attr.name,
321 S_IFREG | bat_debug->attr.mode,
9e466250 322 batadv_debugfs, NULL, &bat_debug->fops);
1c280471
ML
323 if (!file)
324 pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name);
325
326out:
327 return;
c6c8fea2
SE
328}
329
40a072d7 330void batadv_debugfs_destroy(void)
c6c8fea2 331{
9e466250
SE
332 if (batadv_debugfs) {
333 debugfs_remove_recursive(batadv_debugfs);
334 batadv_debugfs = NULL;
c6c8fea2
SE
335 }
336}
337
40a072d7 338int batadv_debugfs_add_meshif(struct net_device *dev)
c6c8fea2
SE
339{
340 struct bat_priv *bat_priv = netdev_priv(dev);
341 struct bat_debuginfo **bat_debug;
342 struct dentry *file;
343
9e466250 344 if (!batadv_debugfs)
c6c8fea2
SE
345 goto out;
346
9e466250 347 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
c6c8fea2
SE
348 if (!bat_priv->debug_dir)
349 goto out;
350
9039dc7e 351 if (batadv_socket_setup(bat_priv) < 0)
5346c35e
SE
352 goto rem_attr;
353
9e466250 354 if (batadv_debug_log_setup(bat_priv) < 0)
5346c35e 355 goto rem_attr;
c6c8fea2 356
9e466250 357 for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
c6c8fea2
SE
358 file = debugfs_create_file(((*bat_debug)->attr).name,
359 S_IFREG | ((*bat_debug)->attr).mode,
360 bat_priv->debug_dir,
361 dev, &(*bat_debug)->fops);
362 if (!file) {
363 bat_err(dev, "Can't add debugfs file: %s/%s\n",
364 dev->name, ((*bat_debug)->attr).name);
365 goto rem_attr;
366 }
367 }
368
369 return 0;
370rem_attr:
371 debugfs_remove_recursive(bat_priv->debug_dir);
372 bat_priv->debug_dir = NULL;
373out:
374#ifdef CONFIG_DEBUG_FS
375 return -ENOMEM;
376#else
377 return 0;
378#endif /* CONFIG_DEBUG_FS */
379}
380
40a072d7 381void batadv_debugfs_del_meshif(struct net_device *dev)
c6c8fea2
SE
382{
383 struct bat_priv *bat_priv = netdev_priv(dev);
384
9e466250 385 batadv_debug_log_cleanup(bat_priv);
c6c8fea2 386
9e466250 387 if (batadv_debugfs) {
c6c8fea2
SE
388 debugfs_remove_recursive(bat_priv->debug_dir);
389 bat_priv->debug_dir = NULL;
390 }
391}