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