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