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" |
c6c8fea2 | 34 | |
9e466250 | 35 | static struct dentry *batadv_debugfs; |
c6c8fea2 SE |
36 | |
37 | #ifdef CONFIG_BATMAN_ADV_DEBUG | |
347c80f0 | 38 | #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1) |
c6c8fea2 | 39 | |
a8a0a62d SE |
40 | static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN; |
41 | ||
42 | static char *batadv_log_char_addr(struct batadv_debug_log *debug_log, | |
43 | size_t idx) | |
44 | { | |
45 | return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK]; | |
46 | } | |
c6c8fea2 | 47 | |
56303d34 | 48 | static void batadv_emit_log_char(struct batadv_debug_log *debug_log, char c) |
c6c8fea2 | 49 | { |
a8a0a62d SE |
50 | char *char_addr; |
51 | ||
52 | char_addr = batadv_log_char_addr(debug_log, debug_log->log_end); | |
53 | *char_addr = c; | |
c6c8fea2 SE |
54 | debug_log->log_end++; |
55 | ||
9e466250 SE |
56 | if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len) |
57 | debug_log->log_start = debug_log->log_end - batadv_log_buff_len; | |
c6c8fea2 SE |
58 | } |
59 | ||
d3a547be | 60 | __printf(2, 3) |
56303d34 SE |
61 | static int batadv_fdebug_log(struct batadv_debug_log *debug_log, |
62 | const char *fmt, ...) | |
c6c8fea2 | 63 | { |
c6c8fea2 SE |
64 | va_list args; |
65 | static char debug_log_buf[256]; | |
66 | char *p; | |
67 | ||
68 | if (!debug_log) | |
69 | return 0; | |
70 | ||
71 | spin_lock_bh(&debug_log->lock); | |
72 | va_start(args, fmt); | |
1299bdaa | 73 | vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args); |
c6c8fea2 SE |
74 | va_end(args); |
75 | ||
76 | for (p = debug_log_buf; *p != 0; p++) | |
9e466250 | 77 | batadv_emit_log_char(debug_log, *p); |
c6c8fea2 SE |
78 | |
79 | spin_unlock_bh(&debug_log->lock); | |
80 | ||
81 | wake_up(&debug_log->queue_wait); | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
56303d34 | 86 | int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...) |
c6c8fea2 SE |
87 | { |
88 | va_list args; | |
89 | char tmp_log_buf[256]; | |
90 | ||
91 | va_start(args, fmt); | |
92 | vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args); | |
9e466250 SE |
93 | batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s", |
94 | jiffies_to_msecs(jiffies), tmp_log_buf); | |
c6c8fea2 SE |
95 | va_end(args); |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
9e466250 | 100 | static int batadv_log_open(struct inode *inode, struct file *file) |
c6c8fea2 | 101 | { |
bd5b80d5 SE |
102 | if (!try_module_get(THIS_MODULE)) |
103 | return -EBUSY; | |
104 | ||
c6c8fea2 SE |
105 | nonseekable_open(inode, file); |
106 | file->private_data = inode->i_private; | |
c6c8fea2 SE |
107 | return 0; |
108 | } | |
109 | ||
9e466250 | 110 | static int batadv_log_release(struct inode *inode, struct file *file) |
c6c8fea2 | 111 | { |
bd5b80d5 | 112 | module_put(THIS_MODULE); |
c6c8fea2 SE |
113 | return 0; |
114 | } | |
115 | ||
0aca2369 SE |
116 | static int batadv_log_empty(struct batadv_debug_log *debug_log) |
117 | { | |
118 | return !(debug_log->log_start - debug_log->log_end); | |
119 | } | |
120 | ||
9e466250 SE |
121 | static ssize_t batadv_log_read(struct file *file, char __user *buf, |
122 | size_t count, loff_t *ppos) | |
c6c8fea2 | 123 | { |
56303d34 SE |
124 | struct batadv_priv *bat_priv = file->private_data; |
125 | struct batadv_debug_log *debug_log = bat_priv->debug_log; | |
c6c8fea2 | 126 | int error, i = 0; |
a8a0a62d | 127 | char *char_addr; |
c6c8fea2 SE |
128 | char c; |
129 | ||
0aca2369 | 130 | if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log)) |
c6c8fea2 SE |
131 | return -EAGAIN; |
132 | ||
16f14b45 | 133 | if (!buf) |
c6c8fea2 SE |
134 | return -EINVAL; |
135 | ||
136 | if (count == 0) | |
137 | return 0; | |
138 | ||
139 | if (!access_ok(VERIFY_WRITE, buf, count)) | |
140 | return -EFAULT; | |
141 | ||
142 | error = wait_event_interruptible(debug_log->queue_wait, | |
0aca2369 | 143 | (!batadv_log_empty(debug_log))); |
c6c8fea2 SE |
144 | |
145 | if (error) | |
146 | return error; | |
147 | ||
148 | spin_lock_bh(&debug_log->lock); | |
149 | ||
150 | while ((!error) && (i < count) && | |
151 | (debug_log->log_start != debug_log->log_end)) { | |
a8a0a62d SE |
152 | char_addr = batadv_log_char_addr(debug_log, |
153 | debug_log->log_start); | |
154 | c = *char_addr; | |
c6c8fea2 SE |
155 | |
156 | debug_log->log_start++; | |
157 | ||
158 | spin_unlock_bh(&debug_log->lock); | |
159 | ||
160 | error = __put_user(c, buf); | |
161 | ||
162 | spin_lock_bh(&debug_log->lock); | |
163 | ||
164 | buf++; | |
165 | i++; | |
166 | ||
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 SE |
231 | { |
232 | bat_priv->debug_log = NULL; | |
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 | |
9e466250 | 283 | static int batadv_transtable_local_open(struct inode *inode, struct file *file) |
c6c8fea2 SE |
284 | { |
285 | struct net_device *net_dev = (struct net_device *)inode->i_private; | |
08c36d3e | 286 | return single_open(file, batadv_tt_local_seq_print_text, net_dev); |
c6c8fea2 SE |
287 | } |
288 | ||
9e466250 | 289 | static int batadv_vis_data_open(struct inode *inode, struct file *file) |
c6c8fea2 SE |
290 | { |
291 | struct net_device *net_dev = (struct net_device *)inode->i_private; | |
d0f714f4 | 292 | return single_open(file, batadv_vis_seq_print_text, net_dev); |
c6c8fea2 SE |
293 | } |
294 | ||
7f223c0c | 295 | struct batadv_debuginfo { |
c6c8fea2 SE |
296 | struct attribute attr; |
297 | const struct file_operations fops; | |
298 | }; | |
299 | ||
347c80f0 | 300 | #define BATADV_DEBUGINFO(_name, _mode, _open) \ |
7f223c0c | 301 | struct batadv_debuginfo batadv_debuginfo_##_name = { \ |
9e466250 SE |
302 | .attr = { .name = __stringify(_name), \ |
303 | .mode = _mode, }, \ | |
304 | .fops = { .owner = THIS_MODULE, \ | |
305 | .open = _open, \ | |
306 | .read = seq_read, \ | |
307 | .llseek = seq_lseek, \ | |
308 | .release = single_release, \ | |
309 | } \ | |
c6c8fea2 SE |
310 | }; |
311 | ||
347c80f0 SE |
312 | static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open); |
313 | static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open); | |
314 | static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open); | |
315 | static BATADV_DEBUGINFO(transtable_global, S_IRUGO, | |
316 | batadv_transtable_global_open); | |
7a5cc242 | 317 | #ifdef CONFIG_BATMAN_ADV_BLA |
347c80f0 | 318 | static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open); |
536a23f1 SW |
319 | static BATADV_DEBUGINFO(bla_backbone_table, S_IRUGO, |
320 | batadv_bla_backbone_table_open); | |
7a5cc242 | 321 | #endif |
347c80f0 SE |
322 | static BATADV_DEBUGINFO(transtable_local, S_IRUGO, |
323 | batadv_transtable_local_open); | |
324 | static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open); | |
c6c8fea2 | 325 | |
7f223c0c | 326 | static struct batadv_debuginfo *batadv_mesh_debuginfos[] = { |
9e466250 SE |
327 | &batadv_debuginfo_originators, |
328 | &batadv_debuginfo_gateways, | |
329 | &batadv_debuginfo_transtable_global, | |
7a5cc242 | 330 | #ifdef CONFIG_BATMAN_ADV_BLA |
9e466250 | 331 | &batadv_debuginfo_bla_claim_table, |
536a23f1 | 332 | &batadv_debuginfo_bla_backbone_table, |
7a5cc242 | 333 | #endif |
9e466250 SE |
334 | &batadv_debuginfo_transtable_local, |
335 | &batadv_debuginfo_vis_data, | |
c6c8fea2 SE |
336 | NULL, |
337 | }; | |
338 | ||
40a072d7 | 339 | void batadv_debugfs_init(void) |
c6c8fea2 | 340 | { |
7f223c0c | 341 | struct batadv_debuginfo *bat_debug; |
1c280471 ML |
342 | struct dentry *file; |
343 | ||
54590e4d | 344 | batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL); |
9e466250 SE |
345 | if (batadv_debugfs == ERR_PTR(-ENODEV)) |
346 | batadv_debugfs = NULL; | |
1c280471 | 347 | |
9e466250 | 348 | if (!batadv_debugfs) |
1c280471 ML |
349 | goto out; |
350 | ||
9e466250 | 351 | bat_debug = &batadv_debuginfo_routing_algos; |
1c280471 ML |
352 | file = debugfs_create_file(bat_debug->attr.name, |
353 | S_IFREG | bat_debug->attr.mode, | |
9e466250 | 354 | batadv_debugfs, NULL, &bat_debug->fops); |
1c280471 ML |
355 | if (!file) |
356 | pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name); | |
357 | ||
358 | out: | |
359 | return; | |
c6c8fea2 SE |
360 | } |
361 | ||
40a072d7 | 362 | void batadv_debugfs_destroy(void) |
c6c8fea2 | 363 | { |
9e466250 SE |
364 | if (batadv_debugfs) { |
365 | debugfs_remove_recursive(batadv_debugfs); | |
366 | batadv_debugfs = NULL; | |
c6c8fea2 SE |
367 | } |
368 | } | |
369 | ||
40a072d7 | 370 | int batadv_debugfs_add_meshif(struct net_device *dev) |
c6c8fea2 | 371 | { |
56303d34 | 372 | struct batadv_priv *bat_priv = netdev_priv(dev); |
7f223c0c | 373 | struct batadv_debuginfo **bat_debug; |
c6c8fea2 SE |
374 | struct dentry *file; |
375 | ||
9e466250 | 376 | if (!batadv_debugfs) |
c6c8fea2 SE |
377 | goto out; |
378 | ||
9e466250 | 379 | bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs); |
c6c8fea2 SE |
380 | if (!bat_priv->debug_dir) |
381 | goto out; | |
382 | ||
9039dc7e | 383 | if (batadv_socket_setup(bat_priv) < 0) |
5346c35e SE |
384 | goto rem_attr; |
385 | ||
9e466250 | 386 | if (batadv_debug_log_setup(bat_priv) < 0) |
5346c35e | 387 | goto rem_attr; |
c6c8fea2 | 388 | |
9e466250 | 389 | for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) { |
c6c8fea2 | 390 | file = debugfs_create_file(((*bat_debug)->attr).name, |
0aca2369 SE |
391 | S_IFREG | ((*bat_debug)->attr).mode, |
392 | bat_priv->debug_dir, | |
393 | dev, &(*bat_debug)->fops); | |
c6c8fea2 | 394 | if (!file) { |
3e34819e SE |
395 | batadv_err(dev, "Can't add debugfs file: %s/%s\n", |
396 | dev->name, ((*bat_debug)->attr).name); | |
c6c8fea2 SE |
397 | goto rem_attr; |
398 | } | |
399 | } | |
400 | ||
401 | return 0; | |
402 | rem_attr: | |
403 | debugfs_remove_recursive(bat_priv->debug_dir); | |
404 | bat_priv->debug_dir = NULL; | |
405 | out: | |
406 | #ifdef CONFIG_DEBUG_FS | |
407 | return -ENOMEM; | |
408 | #else | |
409 | return 0; | |
410 | #endif /* CONFIG_DEBUG_FS */ | |
411 | } | |
412 | ||
40a072d7 | 413 | void batadv_debugfs_del_meshif(struct net_device *dev) |
c6c8fea2 | 414 | { |
56303d34 | 415 | struct batadv_priv *bat_priv = netdev_priv(dev); |
c6c8fea2 | 416 | |
9e466250 | 417 | batadv_debug_log_cleanup(bat_priv); |
c6c8fea2 | 418 | |
9e466250 | 419 | if (batadv_debugfs) { |
c6c8fea2 SE |
420 | debugfs_remove_recursive(bat_priv->debug_dir); |
421 | bat_priv->debug_dir = NULL; | |
422 | } | |
423 | } |