debugfs.c 14 KB

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