debugfs.c 11 KB

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