]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/batman-adv/bat_debugfs.c
batman-adv: Prefix types structs with batadv_
[mirror_ubuntu-zesty-kernel.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
347c80f0
SE
38#define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
39#define BATADV_LOG_BUFF(idx) (debug_log->log_buff[(idx) & BATADV_LOG_BUFF_MASK])
c6c8fea2 40
42d0b044 41static int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
c6c8fea2 42
56303d34 43static void batadv_emit_log_char(struct batadv_debug_log *debug_log, char c)
c6c8fea2 44{
347c80f0 45 BATADV_LOG_BUFF(debug_log->log_end) = c;
c6c8fea2
SE
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)
56303d34
SE
53static int batadv_fdebug_log(struct batadv_debug_log *debug_log,
54 const char *fmt, ...)
c6c8fea2 55{
c6c8fea2
SE
56 va_list args;
57 static char debug_log_buf[256];
58 char *p;
59
60 if (!debug_log)
61 return 0;
62
63 spin_lock_bh(&debug_log->lock);
64 va_start(args, fmt);
1299bdaa 65 vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
c6c8fea2
SE
66 va_end(args);
67
68 for (p = debug_log_buf; *p != 0; p++)
9e466250 69 batadv_emit_log_char(debug_log, *p);
c6c8fea2
SE
70
71 spin_unlock_bh(&debug_log->lock);
72
73 wake_up(&debug_log->queue_wait);
74
75 return 0;
76}
77
56303d34 78int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
c6c8fea2
SE
79{
80 va_list args;
81 char tmp_log_buf[256];
82
83 va_start(args, fmt);
84 vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
9e466250
SE
85 batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
86 jiffies_to_msecs(jiffies), tmp_log_buf);
c6c8fea2
SE
87 va_end(args);
88
89 return 0;
90}
91
9e466250 92static int batadv_log_open(struct inode *inode, struct file *file)
c6c8fea2
SE
93{
94 nonseekable_open(inode, file);
95 file->private_data = inode->i_private;
3193e8fd 96 batadv_inc_module_count();
c6c8fea2
SE
97 return 0;
98}
99
9e466250 100static int batadv_log_release(struct inode *inode, struct file *file)
c6c8fea2 101{
3193e8fd 102 batadv_dec_module_count();
c6c8fea2
SE
103 return 0;
104}
105
9e466250
SE
106static ssize_t batadv_log_read(struct file *file, char __user *buf,
107 size_t count, loff_t *ppos)
c6c8fea2 108{
56303d34
SE
109 struct batadv_priv *bat_priv = file->private_data;
110 struct batadv_debug_log *debug_log = bat_priv->debug_log;
c6c8fea2
SE
111 int error, i = 0;
112 char c;
113
114 if ((file->f_flags & O_NONBLOCK) &&
115 !(debug_log->log_end - debug_log->log_start))
116 return -EAGAIN;
117
16f14b45 118 if (!buf)
c6c8fea2
SE
119 return -EINVAL;
120
121 if (count == 0)
122 return 0;
123
124 if (!access_ok(VERIFY_WRITE, buf, count))
125 return -EFAULT;
126
127 error = wait_event_interruptible(debug_log->queue_wait,
128 (debug_log->log_start - debug_log->log_end));
129
130 if (error)
131 return error;
132
133 spin_lock_bh(&debug_log->lock);
134
135 while ((!error) && (i < count) &&
136 (debug_log->log_start != debug_log->log_end)) {
347c80f0 137 c = BATADV_LOG_BUFF(debug_log->log_start);
c6c8fea2
SE
138
139 debug_log->log_start++;
140
141 spin_unlock_bh(&debug_log->lock);
142
143 error = __put_user(c, buf);
144
145 spin_lock_bh(&debug_log->lock);
146
147 buf++;
148 i++;
149
150 }
151
152 spin_unlock_bh(&debug_log->lock);
153
154 if (!error)
155 return i;
156
157 return error;
158}
159
9e466250 160static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
c6c8fea2 161{
56303d34
SE
162 struct batadv_priv *bat_priv = file->private_data;
163 struct batadv_debug_log *debug_log = bat_priv->debug_log;
c6c8fea2
SE
164
165 poll_wait(file, &debug_log->queue_wait, wait);
166
167 if (debug_log->log_end - debug_log->log_start)
168 return POLLIN | POLLRDNORM;
169
170 return 0;
171}
172
9e466250
SE
173static const struct file_operations batadv_log_fops = {
174 .open = batadv_log_open,
175 .release = batadv_log_release,
176 .read = batadv_log_read,
177 .poll = batadv_log_poll,
c6c8fea2
SE
178 .llseek = no_llseek,
179};
180
56303d34 181static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
c6c8fea2
SE
182{
183 struct dentry *d;
184
185 if (!bat_priv->debug_dir)
186 goto err;
187
704509b8 188 bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
c6c8fea2
SE
189 if (!bat_priv->debug_log)
190 goto err;
191
192 spin_lock_init(&bat_priv->debug_log->lock);
193 init_waitqueue_head(&bat_priv->debug_log->queue_wait);
194
195 d = debugfs_create_file("log", S_IFREG | S_IRUSR,
9e466250
SE
196 bat_priv->debug_dir, bat_priv,
197 &batadv_log_fops);
5346c35e 198 if (!d)
c6c8fea2
SE
199 goto err;
200
201 return 0;
202
203err:
5346c35e 204 return -ENOMEM;
c6c8fea2
SE
205}
206
56303d34 207static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
c6c8fea2
SE
208{
209 kfree(bat_priv->debug_log);
210 bat_priv->debug_log = NULL;
211}
212#else /* CONFIG_BATMAN_ADV_DEBUG */
56303d34 213static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
c6c8fea2
SE
214{
215 bat_priv->debug_log = NULL;
216 return 0;
217}
218
56303d34 219static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
c6c8fea2
SE
220{
221 return;
222}
223#endif
224
9e466250 225static int batadv_algorithms_open(struct inode *inode, struct file *file)
1c280471 226{
3193e8fd 227 return single_open(file, batadv_algo_seq_print_text, NULL);
1c280471
ML
228}
229
9e466250 230static int batadv_originators_open(struct inode *inode, struct file *file)
c6c8fea2
SE
231{
232 struct net_device *net_dev = (struct net_device *)inode->i_private;
7d211efc 233 return single_open(file, batadv_orig_seq_print_text, net_dev);
c6c8fea2
SE
234}
235
9e466250 236static int batadv_gateways_open(struct inode *inode, struct file *file)
c6c8fea2
SE
237{
238 struct net_device *net_dev = (struct net_device *)inode->i_private;
7cf06bc6 239 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
c6c8fea2
SE
240}
241
9e466250 242static int batadv_transtable_global_open(struct inode *inode, struct file *file)
c6c8fea2
SE
243{
244 struct net_device *net_dev = (struct net_device *)inode->i_private;
08c36d3e 245 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
c6c8fea2
SE
246}
247
7a5cc242 248#ifdef CONFIG_BATMAN_ADV_BLA
9e466250 249static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
9bf8e4d4
SW
250{
251 struct net_device *net_dev = (struct net_device *)inode->i_private;
08adf151
SE
252 return single_open(file, batadv_bla_claim_table_seq_print_text,
253 net_dev);
9bf8e4d4 254}
7a5cc242 255#endif
9bf8e4d4 256
9e466250 257static int batadv_transtable_local_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_local_seq_print_text, net_dev);
c6c8fea2
SE
261}
262
9e466250 263static int batadv_vis_data_open(struct inode *inode, struct file *file)
c6c8fea2
SE
264{
265 struct net_device *net_dev = (struct net_device *)inode->i_private;
d0f714f4 266 return single_open(file, batadv_vis_seq_print_text, net_dev);
c6c8fea2
SE
267}
268
7f223c0c 269struct batadv_debuginfo {
c6c8fea2
SE
270 struct attribute attr;
271 const struct file_operations fops;
272};
273
347c80f0 274#define BATADV_DEBUGINFO(_name, _mode, _open) \
7f223c0c 275struct batadv_debuginfo batadv_debuginfo_##_name = { \
9e466250
SE
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 } \
c6c8fea2
SE
284};
285
347c80f0
SE
286static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
287static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
288static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
289static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
290 batadv_transtable_global_open);
7a5cc242 291#ifdef CONFIG_BATMAN_ADV_BLA
347c80f0 292static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
7a5cc242 293#endif
347c80f0
SE
294static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
295 batadv_transtable_local_open);
296static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
c6c8fea2 297
7f223c0c 298static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
9e466250
SE
299 &batadv_debuginfo_originators,
300 &batadv_debuginfo_gateways,
301 &batadv_debuginfo_transtable_global,
7a5cc242 302#ifdef CONFIG_BATMAN_ADV_BLA
9e466250 303 &batadv_debuginfo_bla_claim_table,
7a5cc242 304#endif
9e466250
SE
305 &batadv_debuginfo_transtable_local,
306 &batadv_debuginfo_vis_data,
c6c8fea2
SE
307 NULL,
308};
309
40a072d7 310void batadv_debugfs_init(void)
c6c8fea2 311{
7f223c0c 312 struct batadv_debuginfo *bat_debug;
1c280471
ML
313 struct dentry *file;
314
54590e4d 315 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
9e466250
SE
316 if (batadv_debugfs == ERR_PTR(-ENODEV))
317 batadv_debugfs = NULL;
1c280471 318
9e466250 319 if (!batadv_debugfs)
1c280471
ML
320 goto out;
321
9e466250 322 bat_debug = &batadv_debuginfo_routing_algos;
1c280471
ML
323 file = debugfs_create_file(bat_debug->attr.name,
324 S_IFREG | bat_debug->attr.mode,
9e466250 325 batadv_debugfs, NULL, &bat_debug->fops);
1c280471
ML
326 if (!file)
327 pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name);
328
329out:
330 return;
c6c8fea2
SE
331}
332
40a072d7 333void batadv_debugfs_destroy(void)
c6c8fea2 334{
9e466250
SE
335 if (batadv_debugfs) {
336 debugfs_remove_recursive(batadv_debugfs);
337 batadv_debugfs = NULL;
c6c8fea2
SE
338 }
339}
340
40a072d7 341int batadv_debugfs_add_meshif(struct net_device *dev)
c6c8fea2 342{
56303d34 343 struct batadv_priv *bat_priv = netdev_priv(dev);
7f223c0c 344 struct batadv_debuginfo **bat_debug;
c6c8fea2
SE
345 struct dentry *file;
346
9e466250 347 if (!batadv_debugfs)
c6c8fea2
SE
348 goto out;
349
9e466250 350 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
c6c8fea2
SE
351 if (!bat_priv->debug_dir)
352 goto out;
353
9039dc7e 354 if (batadv_socket_setup(bat_priv) < 0)
5346c35e
SE
355 goto rem_attr;
356
9e466250 357 if (batadv_debug_log_setup(bat_priv) < 0)
5346c35e 358 goto rem_attr;
c6c8fea2 359
9e466250 360 for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
c6c8fea2
SE
361 file = debugfs_create_file(((*bat_debug)->attr).name,
362 S_IFREG | ((*bat_debug)->attr).mode,
363 bat_priv->debug_dir,
364 dev, &(*bat_debug)->fops);
365 if (!file) {
3e34819e
SE
366 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
367 dev->name, ((*bat_debug)->attr).name);
c6c8fea2
SE
368 goto rem_attr;
369 }
370 }
371
372 return 0;
373rem_attr:
374 debugfs_remove_recursive(bat_priv->debug_dir);
375 bat_priv->debug_dir = NULL;
376out:
377#ifdef CONFIG_DEBUG_FS
378 return -ENOMEM;
379#else
380 return 0;
381#endif /* CONFIG_DEBUG_FS */
382}
383
40a072d7 384void batadv_debugfs_del_meshif(struct net_device *dev)
c6c8fea2 385{
56303d34 386 struct batadv_priv *bat_priv = netdev_priv(dev);
c6c8fea2 387
9e466250 388 batadv_debug_log_cleanup(bat_priv);
c6c8fea2 389
9e466250 390 if (batadv_debugfs) {
c6c8fea2
SE
391 debugfs_remove_recursive(bat_priv->debug_dir);
392 bat_priv->debug_dir = NULL;
393 }
394}