blob: db7b9bf895aa2015a793f410482c34e843c0877f [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
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"
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +010033#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034
Sven Eckelmann9e466252012-05-12 18:33:50 +020035static struct dentry *batadv_debugfs;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036
37#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann347c80f2012-06-03 22:19:07 +020038#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])
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000040
Sven Eckelmann42d0b042012-06-03 22:19:17 +020041static int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000042
Sven Eckelmann56303d32012-06-05 22:31:31 +020043static void batadv_emit_log_char(struct batadv_debug_log *debug_log, char c)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044{
Sven Eckelmann347c80f2012-06-03 22:19:07 +020045 BATADV_LOG_BUFF(debug_log->log_end) = c;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046 debug_log->log_end++;
47
Sven Eckelmann9e466252012-05-12 18:33:50 +020048 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;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050}
51
Sven Eckelmannd3a547b2011-05-14 23:14:46 +020052__printf(2, 3)
Sven Eckelmann56303d32012-06-05 22:31:31 +020053static int batadv_fdebug_log(struct batadv_debug_log *debug_log,
54 const char *fmt, ...)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000055{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056 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);
Sven Eckelmann1299bda2011-01-27 13:48:54 +010065 vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000066 va_end(args);
67
68 for (p = debug_log_buf; *p != 0; p++)
Sven Eckelmann9e466252012-05-12 18:33:50 +020069 batadv_emit_log_char(debug_log, *p);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000070
71 spin_unlock_bh(&debug_log->lock);
72
73 wake_up(&debug_log->queue_wait);
74
75 return 0;
76}
77
Sven Eckelmann56303d32012-06-05 22:31:31 +020078int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000079{
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);
Sven Eckelmann9e466252012-05-12 18:33:50 +020085 batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
86 jiffies_to_msecs(jiffies), tmp_log_buf);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000087 va_end(args);
88
89 return 0;
90}
91
Sven Eckelmann9e466252012-05-12 18:33:50 +020092static int batadv_log_open(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000093{
94 nonseekable_open(inode, file);
95 file->private_data = inode->i_private;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020096 batadv_inc_module_count();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000097 return 0;
98}
99
Sven Eckelmann9e466252012-05-12 18:33:50 +0200100static int batadv_log_release(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000101{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200102 batadv_dec_module_count();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000103 return 0;
104}
105
Sven Eckelmann9e466252012-05-12 18:33:50 +0200106static ssize_t batadv_log_read(struct file *file, char __user *buf,
107 size_t count, loff_t *ppos)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000108{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200109 struct batadv_priv *bat_priv = file->private_data;
110 struct batadv_debug_log *debug_log = bat_priv->debug_log;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111 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
Sven Eckelmann16f14b42011-05-14 23:14:48 +0200118 if (!buf)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000119 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)) {
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200137 c = BATADV_LOG_BUFF(debug_log->log_start);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000138
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
Sven Eckelmann9e466252012-05-12 18:33:50 +0200160static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200162 struct batadv_priv *bat_priv = file->private_data;
163 struct batadv_debug_log *debug_log = bat_priv->debug_log;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164
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
Sven Eckelmann9e466252012-05-12 18:33:50 +0200173static 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,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000178 .llseek = no_llseek,
179};
180
Sven Eckelmann56303d32012-06-05 22:31:31 +0200181static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000182{
183 struct dentry *d;
184
185 if (!bat_priv->debug_dir)
186 goto err;
187
Sven Eckelmann704509b2011-05-14 23:14:54 +0200188 bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000189 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,
Sven Eckelmann9e466252012-05-12 18:33:50 +0200196 bat_priv->debug_dir, bat_priv,
197 &batadv_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
Sven Eckelmann56303d32012-06-05 22:31:31 +0200207static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208{
209 kfree(bat_priv->debug_log);
210 bat_priv->debug_log = NULL;
211}
212#else /* CONFIG_BATMAN_ADV_DEBUG */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200213static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214{
215 bat_priv->debug_log = NULL;
216 return 0;
217}
218
Sven Eckelmann56303d32012-06-05 22:31:31 +0200219static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000220{
221 return;
222}
223#endif
224
Sven Eckelmann9e466252012-05-12 18:33:50 +0200225static int batadv_algorithms_open(struct inode *inode, struct file *file)
Marek Lindner1c280472011-11-28 17:40:17 +0800226{
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200227 return single_open(file, batadv_algo_seq_print_text, NULL);
Marek Lindner1c280472011-11-28 17:40:17 +0800228}
229
Sven Eckelmann9e466252012-05-12 18:33:50 +0200230static int batadv_originators_open(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000231{
232 struct net_device *net_dev = (struct net_device *)inode->i_private;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200233 return single_open(file, batadv_orig_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234}
235
Sven Eckelmann9e466252012-05-12 18:33:50 +0200236static int batadv_gateways_open(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237{
238 struct net_device *net_dev = (struct net_device *)inode->i_private;
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200239 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240}
241
Sven Eckelmann9e466252012-05-12 18:33:50 +0200242static int batadv_transtable_global_open(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243{
244 struct net_device *net_dev = (struct net_device *)inode->i_private;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200245 return single_open(file, batadv_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
Sven Eckelmann9e466252012-05-12 18:33:50 +0200249static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +0100250{
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 Eckelmann9e466252012-05-12 18:33:50 +0200257static int batadv_transtable_local_open(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258{
259 struct net_device *net_dev = (struct net_device *)inode->i_private;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200260 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000261}
262
Sven Eckelmann9e466252012-05-12 18:33:50 +0200263static int batadv_vis_data_open(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264{
265 struct net_device *net_dev = (struct net_device *)inode->i_private;
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200266 return single_open(file, batadv_vis_seq_print_text, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267}
268
Sven Eckelmann7f223c02012-06-05 22:31:27 +0200269struct batadv_debuginfo {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270 struct attribute attr;
271 const struct file_operations fops;
272};
273
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200274#define BATADV_DEBUGINFO(_name, _mode, _open) \
Sven Eckelmann7f223c02012-06-05 22:31:27 +0200275struct batadv_debuginfo batadv_debuginfo_##_name = { \
Sven Eckelmann9e466252012-05-12 18:33:50 +0200276 .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 } \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000284};
285
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200286static 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);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100291#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200292static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100293#endif
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200294static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
295 batadv_transtable_local_open);
296static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297
Sven Eckelmann7f223c02012-06-05 22:31:27 +0200298static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
Sven Eckelmann9e466252012-05-12 18:33:50 +0200299 &batadv_debuginfo_originators,
300 &batadv_debuginfo_gateways,
301 &batadv_debuginfo_transtable_global,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100302#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann9e466252012-05-12 18:33:50 +0200303 &batadv_debuginfo_bla_claim_table,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100304#endif
Sven Eckelmann9e466252012-05-12 18:33:50 +0200305 &batadv_debuginfo_transtable_local,
306 &batadv_debuginfo_vis_data,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307 NULL,
308};
309
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200310void batadv_debugfs_init(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311{
Sven Eckelmann7f223c02012-06-05 22:31:27 +0200312 struct batadv_debuginfo *bat_debug;
Marek Lindner1c280472011-11-28 17:40:17 +0800313 struct dentry *file;
314
Sven Eckelmann54590e42012-06-03 22:19:08 +0200315 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
Sven Eckelmann9e466252012-05-12 18:33:50 +0200316 if (batadv_debugfs == ERR_PTR(-ENODEV))
317 batadv_debugfs = NULL;
Marek Lindner1c280472011-11-28 17:40:17 +0800318
Sven Eckelmann9e466252012-05-12 18:33:50 +0200319 if (!batadv_debugfs)
Marek Lindner1c280472011-11-28 17:40:17 +0800320 goto out;
321
Sven Eckelmann9e466252012-05-12 18:33:50 +0200322 bat_debug = &batadv_debuginfo_routing_algos;
Marek Lindner1c280472011-11-28 17:40:17 +0800323 file = debugfs_create_file(bat_debug->attr.name,
324 S_IFREG | bat_debug->attr.mode,
Sven Eckelmann9e466252012-05-12 18:33:50 +0200325 batadv_debugfs, NULL, &bat_debug->fops);
Marek Lindner1c280472011-11-28 17:40:17 +0800326 if (!file)
327 pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name);
328
329out:
330 return;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331}
332
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200333void batadv_debugfs_destroy(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334{
Sven Eckelmann9e466252012-05-12 18:33:50 +0200335 if (batadv_debugfs) {
336 debugfs_remove_recursive(batadv_debugfs);
337 batadv_debugfs = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338 }
339}
340
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200341int batadv_debugfs_add_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200343 struct batadv_priv *bat_priv = netdev_priv(dev);
Sven Eckelmann7f223c02012-06-05 22:31:27 +0200344 struct batadv_debuginfo **bat_debug;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345 struct dentry *file;
346
Sven Eckelmann9e466252012-05-12 18:33:50 +0200347 if (!batadv_debugfs)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348 goto out;
349
Sven Eckelmann9e466252012-05-12 18:33:50 +0200350 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351 if (!bat_priv->debug_dir)
352 goto out;
353
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200354 if (batadv_socket_setup(bat_priv) < 0)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200355 goto rem_attr;
356
Sven Eckelmann9e466252012-05-12 18:33:50 +0200357 if (batadv_debug_log_setup(bat_priv) < 0)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200358 goto rem_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359
Sven Eckelmann9e466252012-05-12 18:33:50 +0200360 for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361 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) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200366 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
367 dev->name, ((*bat_debug)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368 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
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200384void batadv_debugfs_del_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200386 struct batadv_priv *bat_priv = netdev_priv(dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387
Sven Eckelmann9e466252012-05-12 18:33:50 +0200388 batadv_debug_log_cleanup(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389
Sven Eckelmann9e466252012-05-12 18:33:50 +0200390 if (batadv_debugfs) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391 debugfs_remove_recursive(bat_priv->debug_dir);
392 bat_priv->debug_dir = NULL;
393 }
394}