debugfs.c 14 KB

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