debugfs.c 12 KB

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