]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/batman-adv/debugfs.c
batman-adv: add debugfs structure for information per interface
[mirror_ubuntu-zesty-kernel.git] / net / batman-adv / debugfs.c
CommitLineData
0b873931 1/* Copyright (C) 2010-2013 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
ebf38fb7 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
c6c8fea2
SE
16 */
17
18#include "main.h"
19
20#include <linux/debugfs.h>
21
b706b13b 22#include "debugfs.h"
c6c8fea2
SE
23#include "translation-table.h"
24#include "originator.h"
25#include "hard-interface.h"
26#include "gateway_common.h"
27#include "gateway_client.h"
28#include "soft-interface.h"
c6c8fea2 29#include "icmp_socket.h"
9bf8e4d4 30#include "bridge_loop_avoidance.h"
2f1dfbe1 31#include "distributed-arp-table.h"
d56b1705 32#include "network-coding.h"
c6c8fea2 33
9e466250 34static struct dentry *batadv_debugfs;
c6c8fea2
SE
35
36#ifdef CONFIG_BATMAN_ADV_DEBUG
347c80f0 37#define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
c6c8fea2 38
a8a0a62d
SE
39static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
40
0abf5d81 41static char *batadv_log_char_addr(struct batadv_priv_debug_log *debug_log,
a8a0a62d
SE
42 size_t idx)
43{
44 return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK];
45}
c6c8fea2 46
0abf5d81
ML
47static void batadv_emit_log_char(struct batadv_priv_debug_log *debug_log,
48 char c)
c6c8fea2 49{
a8a0a62d
SE
50 char *char_addr;
51
52 char_addr = batadv_log_char_addr(debug_log, debug_log->log_end);
53 *char_addr = c;
c6c8fea2
SE
54 debug_log->log_end++;
55
9e466250
SE
56 if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
57 debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
c6c8fea2
SE
58}
59
d3a547be 60__printf(2, 3)
0abf5d81 61static int batadv_fdebug_log(struct batadv_priv_debug_log *debug_log,
56303d34 62 const char *fmt, ...)
c6c8fea2 63{
c6c8fea2
SE
64 va_list args;
65 static char debug_log_buf[256];
66 char *p;
67
68 if (!debug_log)
69 return 0;
70
71 spin_lock_bh(&debug_log->lock);
72 va_start(args, fmt);
1299bdaa 73 vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
c6c8fea2
SE
74 va_end(args);
75
76 for (p = debug_log_buf; *p != 0; p++)
9e466250 77 batadv_emit_log_char(debug_log, *p);
c6c8fea2
SE
78
79 spin_unlock_bh(&debug_log->lock);
80
81 wake_up(&debug_log->queue_wait);
82
83 return 0;
84}
85
56303d34 86int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
c6c8fea2
SE
87{
88 va_list args;
89 char tmp_log_buf[256];
90
91 va_start(args, fmt);
92 vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
9e466250
SE
93 batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
94 jiffies_to_msecs(jiffies), tmp_log_buf);
c6c8fea2
SE
95 va_end(args);
96
97 return 0;
98}
99
9e466250 100static int batadv_log_open(struct inode *inode, struct file *file)
c6c8fea2 101{
bd5b80d5
SE
102 if (!try_module_get(THIS_MODULE))
103 return -EBUSY;
104
c6c8fea2
SE
105 nonseekable_open(inode, file);
106 file->private_data = inode->i_private;
c6c8fea2
SE
107 return 0;
108}
109
9e466250 110static int batadv_log_release(struct inode *inode, struct file *file)
c6c8fea2 111{
bd5b80d5 112 module_put(THIS_MODULE);
c6c8fea2
SE
113 return 0;
114}
115
0abf5d81 116static int batadv_log_empty(struct batadv_priv_debug_log *debug_log)
0aca2369
SE
117{
118 return !(debug_log->log_start - debug_log->log_end);
119}
120
9e466250
SE
121static ssize_t batadv_log_read(struct file *file, char __user *buf,
122 size_t count, loff_t *ppos)
c6c8fea2 123{
56303d34 124 struct batadv_priv *bat_priv = file->private_data;
0abf5d81 125 struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
c6c8fea2 126 int error, i = 0;
a8a0a62d 127 char *char_addr;
c6c8fea2
SE
128 char c;
129
0aca2369 130 if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log))
c6c8fea2
SE
131 return -EAGAIN;
132
16f14b45 133 if (!buf)
c6c8fea2
SE
134 return -EINVAL;
135
136 if (count == 0)
137 return 0;
138
139 if (!access_ok(VERIFY_WRITE, buf, count))
140 return -EFAULT;
141
142 error = wait_event_interruptible(debug_log->queue_wait,
0aca2369 143 (!batadv_log_empty(debug_log)));
c6c8fea2
SE
144
145 if (error)
146 return error;
147
148 spin_lock_bh(&debug_log->lock);
149
150 while ((!error) && (i < count) &&
151 (debug_log->log_start != debug_log->log_end)) {
a8a0a62d
SE
152 char_addr = batadv_log_char_addr(debug_log,
153 debug_log->log_start);
154 c = *char_addr;
c6c8fea2
SE
155
156 debug_log->log_start++;
157
158 spin_unlock_bh(&debug_log->lock);
159
160 error = __put_user(c, buf);
161
162 spin_lock_bh(&debug_log->lock);
163
164 buf++;
165 i++;
c6c8fea2
SE
166 }
167
168 spin_unlock_bh(&debug_log->lock);
169
170 if (!error)
171 return i;
172
173 return error;
174}
175
9e466250 176static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
c6c8fea2 177{
56303d34 178 struct batadv_priv *bat_priv = file->private_data;
0abf5d81 179 struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
c6c8fea2
SE
180
181 poll_wait(file, &debug_log->queue_wait, wait);
182
0aca2369 183 if (!batadv_log_empty(debug_log))
c6c8fea2
SE
184 return POLLIN | POLLRDNORM;
185
186 return 0;
187}
188
9e466250
SE
189static const struct file_operations batadv_log_fops = {
190 .open = batadv_log_open,
191 .release = batadv_log_release,
192 .read = batadv_log_read,
193 .poll = batadv_log_poll,
c6c8fea2
SE
194 .llseek = no_llseek,
195};
196
56303d34 197static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
c6c8fea2
SE
198{
199 struct dentry *d;
200
201 if (!bat_priv->debug_dir)
202 goto err;
203
704509b8 204 bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
c6c8fea2
SE
205 if (!bat_priv->debug_log)
206 goto err;
207
208 spin_lock_init(&bat_priv->debug_log->lock);
209 init_waitqueue_head(&bat_priv->debug_log->queue_wait);
210
211 d = debugfs_create_file("log", S_IFREG | S_IRUSR,
9e466250
SE
212 bat_priv->debug_dir, bat_priv,
213 &batadv_log_fops);
5346c35e 214 if (!d)
c6c8fea2
SE
215 goto err;
216
217 return 0;
218
219err:
5346c35e 220 return -ENOMEM;
c6c8fea2
SE
221}
222
56303d34 223static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
c6c8fea2
SE
224{
225 kfree(bat_priv->debug_log);
226 bat_priv->debug_log = NULL;
227}
228#else /* CONFIG_BATMAN_ADV_DEBUG */
56303d34 229static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
c6c8fea2 230{
c6c8fea2
SE
231 return 0;
232}
233
56303d34 234static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
c6c8fea2
SE
235{
236 return;
237}
238#endif
239
9e466250 240static int batadv_algorithms_open(struct inode *inode, struct file *file)
1c280471 241{
3193e8fd 242 return single_open(file, batadv_algo_seq_print_text, NULL);
1c280471
ML
243}
244
9e466250 245static int batadv_originators_open(struct inode *inode, struct file *file)
c6c8fea2
SE
246{
247 struct net_device *net_dev = (struct net_device *)inode->i_private;
7d211efc 248 return single_open(file, batadv_orig_seq_print_text, net_dev);
c6c8fea2
SE
249}
250
9e466250 251static int batadv_gateways_open(struct inode *inode, struct file *file)
c6c8fea2
SE
252{
253 struct net_device *net_dev = (struct net_device *)inode->i_private;
7cf06bc6 254 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
c6c8fea2
SE
255}
256
9e466250 257static int batadv_transtable_global_open(struct inode *inode, struct file *file)
c6c8fea2
SE
258{
259 struct net_device *net_dev = (struct net_device *)inode->i_private;
08c36d3e 260 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
c6c8fea2
SE
261}
262
7a5cc242 263#ifdef CONFIG_BATMAN_ADV_BLA
9e466250 264static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
9bf8e4d4
SW
265{
266 struct net_device *net_dev = (struct net_device *)inode->i_private;
08adf151
SE
267 return single_open(file, batadv_bla_claim_table_seq_print_text,
268 net_dev);
9bf8e4d4 269}
536a23f1
SW
270
271static int batadv_bla_backbone_table_open(struct inode *inode,
272 struct file *file)
273{
274 struct net_device *net_dev = (struct net_device *)inode->i_private;
275 return single_open(file, batadv_bla_backbone_table_seq_print_text,
276 net_dev);
277}
278
7a5cc242 279#endif
9bf8e4d4 280
17224474 281#ifdef CONFIG_BATMAN_ADV_DAT
2f1dfbe1
AQ
282/**
283 * batadv_dat_cache_open - Prepare file handler for reads from dat_chache
284 * @inode: inode which was opened
285 * @file: file handle to be initialized
286 */
287static int batadv_dat_cache_open(struct inode *inode, struct file *file)
288{
289 struct net_device *net_dev = (struct net_device *)inode->i_private;
290 return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
291}
17224474 292#endif
2f1dfbe1 293
9e466250 294static int batadv_transtable_local_open(struct inode *inode, struct file *file)
c6c8fea2
SE
295{
296 struct net_device *net_dev = (struct net_device *)inode->i_private;
08c36d3e 297 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
c6c8fea2
SE
298}
299
7f223c0c 300struct batadv_debuginfo {
c6c8fea2
SE
301 struct attribute attr;
302 const struct file_operations fops;
303};
304
d56b1705
MH
305#ifdef CONFIG_BATMAN_ADV_NC
306static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
307{
308 struct net_device *net_dev = (struct net_device *)inode->i_private;
309 return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
310}
311#endif
312
347c80f0 313#define BATADV_DEBUGINFO(_name, _mode, _open) \
7f223c0c 314struct batadv_debuginfo batadv_debuginfo_##_name = { \
9e466250
SE
315 .attr = { .name = __stringify(_name), \
316 .mode = _mode, }, \
317 .fops = { .owner = THIS_MODULE, \
318 .open = _open, \
319 .read = seq_read, \
320 .llseek = seq_lseek, \
321 .release = single_release, \
322 } \
c6c8fea2
SE
323};
324
637fbd12
AQ
325/* the following attributes are general and therefore they will be directly
326 * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
327 */
347c80f0 328static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
637fbd12
AQ
329
330static struct batadv_debuginfo *batadv_general_debuginfos[] = {
331 &batadv_debuginfo_routing_algos,
332 NULL,
333};
334
335/* The following attributes are per soft interface */
347c80f0
SE
336static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
337static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
338static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
339 batadv_transtable_global_open);
7a5cc242 340#ifdef CONFIG_BATMAN_ADV_BLA
347c80f0 341static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
536a23f1
SW
342static BATADV_DEBUGINFO(bla_backbone_table, S_IRUGO,
343 batadv_bla_backbone_table_open);
7a5cc242 344#endif
17224474 345#ifdef CONFIG_BATMAN_ADV_DAT
2f1dfbe1 346static BATADV_DEBUGINFO(dat_cache, S_IRUGO, batadv_dat_cache_open);
17224474 347#endif
347c80f0
SE
348static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
349 batadv_transtable_local_open);
d56b1705
MH
350#ifdef CONFIG_BATMAN_ADV_NC
351static BATADV_DEBUGINFO(nc_nodes, S_IRUGO, batadv_nc_nodes_open);
352#endif
c6c8fea2 353
7f223c0c 354static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
9e466250
SE
355 &batadv_debuginfo_originators,
356 &batadv_debuginfo_gateways,
357 &batadv_debuginfo_transtable_global,
7a5cc242 358#ifdef CONFIG_BATMAN_ADV_BLA
9e466250 359 &batadv_debuginfo_bla_claim_table,
536a23f1 360 &batadv_debuginfo_bla_backbone_table,
7a5cc242 361#endif
17224474 362#ifdef CONFIG_BATMAN_ADV_DAT
2f1dfbe1 363 &batadv_debuginfo_dat_cache,
17224474 364#endif
9e466250 365 &batadv_debuginfo_transtable_local,
d56b1705
MH
366#ifdef CONFIG_BATMAN_ADV_NC
367 &batadv_debuginfo_nc_nodes,
368#endif
c6c8fea2
SE
369 NULL,
370};
371
5bc7c1eb
SW
372#define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
373struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
374 .attr = { \
375 .name = __stringify(_name), \
376 .mode = _mode, \
377 }, \
378 .fops = { \
379 .owner = THIS_MODULE, \
380 .open = _open, \
381 .read = seq_read, \
382 .llseek = seq_lseek, \
383 .release = single_release, \
384 }, \
385};
386
387static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
388 NULL,
389};
390
40a072d7 391void batadv_debugfs_init(void)
c6c8fea2 392{
637fbd12 393 struct batadv_debuginfo **bat_debug;
1c280471
ML
394 struct dentry *file;
395
54590e4d 396 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
9e466250
SE
397 if (batadv_debugfs == ERR_PTR(-ENODEV))
398 batadv_debugfs = NULL;
1c280471 399
9e466250 400 if (!batadv_debugfs)
637fbd12 401 goto err;
1c280471 402
637fbd12
AQ
403 for (bat_debug = batadv_general_debuginfos; *bat_debug; ++bat_debug) {
404 file = debugfs_create_file(((*bat_debug)->attr).name,
405 S_IFREG | ((*bat_debug)->attr).mode,
406 batadv_debugfs, NULL,
407 &(*bat_debug)->fops);
408 if (!file) {
409 pr_err("Can't add general debugfs file: %s\n",
410 ((*bat_debug)->attr).name);
411 goto err;
412 }
413 }
1c280471 414
1c280471 415 return;
637fbd12
AQ
416err:
417 debugfs_remove_recursive(batadv_debugfs);
5bc7c1eb 418 batadv_debugfs = NULL;
c6c8fea2
SE
419}
420
40a072d7 421void batadv_debugfs_destroy(void)
c6c8fea2 422{
3f87c423
AQ
423 debugfs_remove_recursive(batadv_debugfs);
424 batadv_debugfs = NULL;
c6c8fea2
SE
425}
426
5bc7c1eb
SW
427/**
428 * batadv_debugfs_add_hardif - creates the base directory for a hard interface
429 * in debugfs.
430 * @hard_iface: hard interface which should be added.
431 */
432int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
433{
434 struct batadv_debuginfo **bat_debug;
435 struct dentry *file;
436
437 if (!batadv_debugfs)
438 goto out;
439
440 hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
441 batadv_debugfs);
442 if (!hard_iface->debug_dir)
443 goto out;
444
445 for (bat_debug = batadv_hardif_debuginfos; *bat_debug; ++bat_debug) {
446 file = debugfs_create_file(((*bat_debug)->attr).name,
447 S_IFREG | ((*bat_debug)->attr).mode,
448 hard_iface->debug_dir,
449 hard_iface->net_dev,
450 &(*bat_debug)->fops);
451 if (!file)
452 goto rem_attr;
453 }
454
455 return 0;
456rem_attr:
457 debugfs_remove_recursive(hard_iface->debug_dir);
458 hard_iface->debug_dir = NULL;
459out:
460#ifdef CONFIG_DEBUG_FS
461 return -ENOMEM;
462#else
463 return 0;
464#endif /* CONFIG_DEBUG_FS */
465}
466
467/**
468 * batadv_debugfs_del_hardif - delete the base directory for a hard interface
469 * in debugfs.
470 * @hard_iface: hard interface which is deleted.
471 */
472void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
473{
474 if (batadv_debugfs) {
475 debugfs_remove_recursive(hard_iface->debug_dir);
476 hard_iface->debug_dir = NULL;
477 }
478}
479
40a072d7 480int batadv_debugfs_add_meshif(struct net_device *dev)
c6c8fea2 481{
56303d34 482 struct batadv_priv *bat_priv = netdev_priv(dev);
7f223c0c 483 struct batadv_debuginfo **bat_debug;
c6c8fea2
SE
484 struct dentry *file;
485
9e466250 486 if (!batadv_debugfs)
c6c8fea2
SE
487 goto out;
488
9e466250 489 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
c6c8fea2
SE
490 if (!bat_priv->debug_dir)
491 goto out;
492
9039dc7e 493 if (batadv_socket_setup(bat_priv) < 0)
5346c35e
SE
494 goto rem_attr;
495
9e466250 496 if (batadv_debug_log_setup(bat_priv) < 0)
5346c35e 497 goto rem_attr;
c6c8fea2 498
9e466250 499 for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
c6c8fea2 500 file = debugfs_create_file(((*bat_debug)->attr).name,
0aca2369
SE
501 S_IFREG | ((*bat_debug)->attr).mode,
502 bat_priv->debug_dir,
503 dev, &(*bat_debug)->fops);
c6c8fea2 504 if (!file) {
3e34819e
SE
505 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
506 dev->name, ((*bat_debug)->attr).name);
c6c8fea2
SE
507 goto rem_attr;
508 }
509 }
510
d56b1705
MH
511 if (batadv_nc_init_debugfs(bat_priv) < 0)
512 goto rem_attr;
513
c6c8fea2
SE
514 return 0;
515rem_attr:
516 debugfs_remove_recursive(bat_priv->debug_dir);
517 bat_priv->debug_dir = NULL;
518out:
519#ifdef CONFIG_DEBUG_FS
520 return -ENOMEM;
521#else
522 return 0;
523#endif /* CONFIG_DEBUG_FS */
524}
525
40a072d7 526void batadv_debugfs_del_meshif(struct net_device *dev)
c6c8fea2 527{
56303d34 528 struct batadv_priv *bat_priv = netdev_priv(dev);
c6c8fea2 529
9e466250 530 batadv_debug_log_cleanup(bat_priv);
c6c8fea2 531
9e466250 532 if (batadv_debugfs) {
c6c8fea2
SE
533 debugfs_remove_recursive(bat_priv->debug_dir);
534 bat_priv->debug_dir = NULL;
535 }
536}