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