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. return;
  182. }
  183. #endif
  184. static int batadv_algorithms_open(struct inode *inode, struct file *file)
  185. {
  186. return single_open(file, batadv_algo_seq_print_text, NULL);
  187. }
  188. static int batadv_originators_open(struct inode *inode, struct file *file)
  189. {
  190. struct net_device *net_dev = (struct net_device *)inode->i_private;
  191. return single_open(file, batadv_orig_seq_print_text, net_dev);
  192. }
  193. /**
  194. * batadv_originators_hardif_open - handles debugfs output for the
  195. * originator table of an hard interface
  196. * @inode: inode pointer to debugfs file
  197. * @file: pointer to the seq_file
  198. */
  199. static int batadv_originators_hardif_open(struct inode *inode,
  200. struct file *file)
  201. {
  202. struct net_device *net_dev = (struct net_device *)inode->i_private;
  203. return single_open(file, batadv_orig_hardif_seq_print_text, net_dev);
  204. }
  205. static int batadv_gateways_open(struct inode *inode, struct file *file)
  206. {
  207. struct net_device *net_dev = (struct net_device *)inode->i_private;
  208. return single_open(file, batadv_gw_client_seq_print_text, net_dev);
  209. }
  210. static int batadv_transtable_global_open(struct inode *inode, struct file *file)
  211. {
  212. struct net_device *net_dev = (struct net_device *)inode->i_private;
  213. return single_open(file, batadv_tt_global_seq_print_text, net_dev);
  214. }
  215. #ifdef CONFIG_BATMAN_ADV_BLA
  216. static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
  217. {
  218. struct net_device *net_dev = (struct net_device *)inode->i_private;
  219. return single_open(file, batadv_bla_claim_table_seq_print_text,
  220. net_dev);
  221. }
  222. static int batadv_bla_backbone_table_open(struct inode *inode,
  223. struct file *file)
  224. {
  225. struct net_device *net_dev = (struct net_device *)inode->i_private;
  226. return single_open(file, batadv_bla_backbone_table_seq_print_text,
  227. net_dev);
  228. }
  229. #endif
  230. #ifdef CONFIG_BATMAN_ADV_DAT
  231. /**
  232. * batadv_dat_cache_open - Prepare file handler for reads from dat_chache
  233. * @inode: inode which was opened
  234. * @file: file handle to be initialized
  235. */
  236. static int batadv_dat_cache_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_dat_cache_seq_print_text, net_dev);
  240. }
  241. #endif
  242. static int batadv_transtable_local_open(struct inode *inode, struct file *file)
  243. {
  244. struct net_device *net_dev = (struct net_device *)inode->i_private;
  245. return single_open(file, batadv_tt_local_seq_print_text, net_dev);
  246. }
  247. struct batadv_debuginfo {
  248. struct attribute attr;
  249. const struct file_operations fops;
  250. };
  251. #ifdef CONFIG_BATMAN_ADV_NC
  252. static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
  253. {
  254. struct net_device *net_dev = (struct net_device *)inode->i_private;
  255. return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
  256. }
  257. #endif
  258. #define BATADV_DEBUGINFO(_name, _mode, _open) \
  259. struct batadv_debuginfo batadv_debuginfo_##_name = { \
  260. .attr = { .name = __stringify(_name), \
  261. .mode = _mode, }, \
  262. .fops = { .owner = THIS_MODULE, \
  263. .open = _open, \
  264. .read = seq_read, \
  265. .llseek = seq_lseek, \
  266. .release = single_release, \
  267. } \
  268. }
  269. /* the following attributes are general and therefore they will be directly
  270. * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
  271. */
  272. static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
  273. static struct batadv_debuginfo *batadv_general_debuginfos[] = {
  274. &batadv_debuginfo_routing_algos,
  275. NULL,
  276. };
  277. /* The following attributes are per soft interface */
  278. static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
  279. static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
  280. static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
  281. batadv_transtable_global_open);
  282. #ifdef CONFIG_BATMAN_ADV_BLA
  283. static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
  284. static BATADV_DEBUGINFO(bla_backbone_table, S_IRUGO,
  285. batadv_bla_backbone_table_open);
  286. #endif
  287. #ifdef CONFIG_BATMAN_ADV_DAT
  288. static BATADV_DEBUGINFO(dat_cache, S_IRUGO, batadv_dat_cache_open);
  289. #endif
  290. static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
  291. batadv_transtable_local_open);
  292. #ifdef CONFIG_BATMAN_ADV_NC
  293. static BATADV_DEBUGINFO(nc_nodes, S_IRUGO, batadv_nc_nodes_open);
  294. #endif
  295. static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
  296. &batadv_debuginfo_originators,
  297. &batadv_debuginfo_gateways,
  298. &batadv_debuginfo_transtable_global,
  299. #ifdef CONFIG_BATMAN_ADV_BLA
  300. &batadv_debuginfo_bla_claim_table,
  301. &batadv_debuginfo_bla_backbone_table,
  302. #endif
  303. #ifdef CONFIG_BATMAN_ADV_DAT
  304. &batadv_debuginfo_dat_cache,
  305. #endif
  306. &batadv_debuginfo_transtable_local,
  307. #ifdef CONFIG_BATMAN_ADV_NC
  308. &batadv_debuginfo_nc_nodes,
  309. #endif
  310. NULL,
  311. };
  312. #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
  313. struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
  314. .attr = { \
  315. .name = __stringify(_name), \
  316. .mode = _mode, \
  317. }, \
  318. .fops = { \
  319. .owner = THIS_MODULE, \
  320. .open = _open, \
  321. .read = seq_read, \
  322. .llseek = seq_lseek, \
  323. .release = single_release, \
  324. }, \
  325. }
  326. static BATADV_HARDIF_DEBUGINFO(originators, S_IRUGO,
  327. batadv_originators_hardif_open);
  328. static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
  329. &batadv_hardif_debuginfo_originators,
  330. NULL,
  331. };
  332. void batadv_debugfs_init(void)
  333. {
  334. struct batadv_debuginfo **bat_debug;
  335. struct dentry *file;
  336. batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
  337. if (batadv_debugfs == ERR_PTR(-ENODEV))
  338. batadv_debugfs = NULL;
  339. if (!batadv_debugfs)
  340. goto err;
  341. for (bat_debug = batadv_general_debuginfos; *bat_debug; ++bat_debug) {
  342. file = debugfs_create_file(((*bat_debug)->attr).name,
  343. S_IFREG | ((*bat_debug)->attr).mode,
  344. batadv_debugfs, NULL,
  345. &(*bat_debug)->fops);
  346. if (!file) {
  347. pr_err("Can't add general debugfs file: %s\n",
  348. ((*bat_debug)->attr).name);
  349. goto err;
  350. }
  351. }
  352. return;
  353. err:
  354. debugfs_remove_recursive(batadv_debugfs);
  355. batadv_debugfs = NULL;
  356. }
  357. void batadv_debugfs_destroy(void)
  358. {
  359. debugfs_remove_recursive(batadv_debugfs);
  360. batadv_debugfs = NULL;
  361. }
  362. /**
  363. * batadv_debugfs_add_hardif - creates the base directory for a hard interface
  364. * in debugfs.
  365. * @hard_iface: hard interface which should be added.
  366. */
  367. int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
  368. {
  369. struct batadv_debuginfo **bat_debug;
  370. struct dentry *file;
  371. if (!batadv_debugfs)
  372. goto out;
  373. hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
  374. batadv_debugfs);
  375. if (!hard_iface->debug_dir)
  376. goto out;
  377. for (bat_debug = batadv_hardif_debuginfos; *bat_debug; ++bat_debug) {
  378. file = debugfs_create_file(((*bat_debug)->attr).name,
  379. S_IFREG | ((*bat_debug)->attr).mode,
  380. hard_iface->debug_dir,
  381. hard_iface->net_dev,
  382. &(*bat_debug)->fops);
  383. if (!file)
  384. goto rem_attr;
  385. }
  386. return 0;
  387. rem_attr:
  388. debugfs_remove_recursive(hard_iface->debug_dir);
  389. hard_iface->debug_dir = NULL;
  390. out:
  391. #ifdef CONFIG_DEBUG_FS
  392. return -ENOMEM;
  393. #else
  394. return 0;
  395. #endif /* CONFIG_DEBUG_FS */
  396. }
  397. /**
  398. * batadv_debugfs_del_hardif - delete the base directory for a hard interface
  399. * in debugfs.
  400. * @hard_iface: hard interface which is deleted.
  401. */
  402. void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
  403. {
  404. if (batadv_debugfs) {
  405. debugfs_remove_recursive(hard_iface->debug_dir);
  406. hard_iface->debug_dir = NULL;
  407. }
  408. }
  409. int batadv_debugfs_add_meshif(struct net_device *dev)
  410. {
  411. struct batadv_priv *bat_priv = netdev_priv(dev);
  412. struct batadv_debuginfo **bat_debug;
  413. struct dentry *file;
  414. if (!batadv_debugfs)
  415. goto out;
  416. bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
  417. if (!bat_priv->debug_dir)
  418. goto out;
  419. if (batadv_socket_setup(bat_priv) < 0)
  420. goto rem_attr;
  421. if (batadv_debug_log_setup(bat_priv) < 0)
  422. goto rem_attr;
  423. for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
  424. file = debugfs_create_file(((*bat_debug)->attr).name,
  425. S_IFREG | ((*bat_debug)->attr).mode,
  426. bat_priv->debug_dir,
  427. dev, &(*bat_debug)->fops);
  428. if (!file) {
  429. batadv_err(dev, "Can't add debugfs file: %s/%s\n",
  430. dev->name, ((*bat_debug)->attr).name);
  431. goto rem_attr;
  432. }
  433. }
  434. if (batadv_nc_init_debugfs(bat_priv) < 0)
  435. goto rem_attr;
  436. return 0;
  437. rem_attr:
  438. debugfs_remove_recursive(bat_priv->debug_dir);
  439. bat_priv->debug_dir = NULL;
  440. out:
  441. #ifdef CONFIG_DEBUG_FS
  442. return -ENOMEM;
  443. #else
  444. return 0;
  445. #endif /* CONFIG_DEBUG_FS */
  446. }
  447. void batadv_debugfs_del_meshif(struct net_device *dev)
  448. {
  449. struct batadv_priv *bat_priv = netdev_priv(dev);
  450. batadv_debug_log_cleanup(bat_priv);
  451. if (batadv_debugfs) {
  452. debugfs_remove_recursive(bat_priv->debug_dir);
  453. bat_priv->debug_dir = NULL;
  454. }
  455. }