debugfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. 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_priv_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_priv_debug_log *debug_log,
  42. char c)
  43. {
  44. char *char_addr;
  45. char_addr = batadv_log_char_addr(debug_log, debug_log->log_end);
  46. *char_addr = c;
  47. debug_log->log_end++;
  48. if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
  49. debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
  50. }
  51. __printf(2, 3)
  52. static int batadv_fdebug_log(struct batadv_priv_debug_log *debug_log,
  53. const char *fmt, ...)
  54. {
  55. va_list args;
  56. static char debug_log_buf[256];
  57. char *p;
  58. if (!debug_log)
  59. return 0;
  60. spin_lock_bh(&debug_log->lock);
  61. va_start(args, fmt);
  62. vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
  63. va_end(args);
  64. for (p = debug_log_buf; *p != 0; p++)
  65. batadv_emit_log_char(debug_log, *p);
  66. spin_unlock_bh(&debug_log->lock);
  67. wake_up(&debug_log->queue_wait);
  68. return 0;
  69. }
  70. int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
  71. {
  72. va_list args;
  73. char tmp_log_buf[256];
  74. va_start(args, fmt);
  75. vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
  76. batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
  77. jiffies_to_msecs(jiffies), tmp_log_buf);
  78. va_end(args);
  79. return 0;
  80. }
  81. static int batadv_log_open(struct inode *inode, struct file *file)
  82. {
  83. if (!try_module_get(THIS_MODULE))
  84. return -EBUSY;
  85. nonseekable_open(inode, file);
  86. file->private_data = inode->i_private;
  87. return 0;
  88. }
  89. static int batadv_log_release(struct inode *inode, struct file *file)
  90. {
  91. module_put(THIS_MODULE);
  92. return 0;
  93. }
  94. static int batadv_log_empty(struct batadv_priv_debug_log *debug_log)
  95. {
  96. return !(debug_log->log_start - debug_log->log_end);
  97. }
  98. static ssize_t batadv_log_read(struct file *file, char __user *buf,
  99. size_t count, loff_t *ppos)
  100. {
  101. struct batadv_priv *bat_priv = file->private_data;
  102. struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
  103. int error, i = 0;
  104. char *char_addr;
  105. char c;
  106. if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log))
  107. return -EAGAIN;
  108. if (!buf)
  109. return -EINVAL;
  110. if (count == 0)
  111. return 0;
  112. if (!access_ok(VERIFY_WRITE, buf, count))
  113. return -EFAULT;
  114. error = wait_event_interruptible(debug_log->queue_wait,
  115. (!batadv_log_empty(debug_log)));
  116. if (error)
  117. return error;
  118. spin_lock_bh(&debug_log->lock);
  119. while ((!error) && (i < count) &&
  120. (debug_log->log_start != debug_log->log_end)) {
  121. char_addr = batadv_log_char_addr(debug_log,
  122. debug_log->log_start);
  123. c = *char_addr;
  124. debug_log->log_start++;
  125. spin_unlock_bh(&debug_log->lock);
  126. error = __put_user(c, buf);
  127. spin_lock_bh(&debug_log->lock);
  128. buf++;
  129. i++;
  130. }
  131. spin_unlock_bh(&debug_log->lock);
  132. if (!error)
  133. return i;
  134. return error;
  135. }
  136. static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
  137. {
  138. struct batadv_priv *bat_priv = file->private_data;
  139. struct batadv_priv_debug_log *debug_log = bat_priv->debug_log;
  140. poll_wait(file, &debug_log->queue_wait, wait);
  141. if (!batadv_log_empty(debug_log))
  142. return POLLIN | POLLRDNORM;
  143. return 0;
  144. }
  145. static const struct file_operations batadv_log_fops = {
  146. .open = batadv_log_open,
  147. .release = batadv_log_release,
  148. .read = batadv_log_read,
  149. .poll = batadv_log_poll,
  150. .llseek = no_llseek,
  151. };
  152. static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
  153. {
  154. struct dentry *d;
  155. if (!bat_priv->debug_dir)
  156. goto err;
  157. bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
  158. if (!bat_priv->debug_log)
  159. goto err;
  160. spin_lock_init(&bat_priv->debug_log->lock);
  161. init_waitqueue_head(&bat_priv->debug_log->queue_wait);
  162. d = debugfs_create_file("log", S_IFREG | S_IRUSR,
  163. bat_priv->debug_dir, bat_priv,
  164. &batadv_log_fops);
  165. if (!d)
  166. goto err;
  167. return 0;
  168. err:
  169. return -ENOMEM;
  170. }
  171. static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
  172. {
  173. kfree(bat_priv->debug_log);
  174. bat_priv->debug_log = NULL;
  175. }
  176. #else /* CONFIG_BATMAN_ADV_DEBUG */
  177. static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
  178. {
  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. /* the following attributes are general and therefore they will be directly
  258. * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
  259. */
  260. static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
  261. static struct batadv_debuginfo *batadv_general_debuginfos[] = {
  262. &batadv_debuginfo_routing_algos,
  263. NULL,
  264. };
  265. /* The following attributes are per soft interface */
  266. static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
  267. static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
  268. static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
  269. batadv_transtable_global_open);
  270. #ifdef CONFIG_BATMAN_ADV_BLA
  271. static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
  272. static BATADV_DEBUGINFO(bla_backbone_table, S_IRUGO,
  273. batadv_bla_backbone_table_open);
  274. #endif
  275. #ifdef CONFIG_BATMAN_ADV_DAT
  276. static BATADV_DEBUGINFO(dat_cache, S_IRUGO, batadv_dat_cache_open);
  277. #endif
  278. static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
  279. batadv_transtable_local_open);
  280. static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
  281. static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
  282. &batadv_debuginfo_originators,
  283. &batadv_debuginfo_gateways,
  284. &batadv_debuginfo_transtable_global,
  285. #ifdef CONFIG_BATMAN_ADV_BLA
  286. &batadv_debuginfo_bla_claim_table,
  287. &batadv_debuginfo_bla_backbone_table,
  288. #endif
  289. #ifdef CONFIG_BATMAN_ADV_DAT
  290. &batadv_debuginfo_dat_cache,
  291. #endif
  292. &batadv_debuginfo_transtable_local,
  293. &batadv_debuginfo_vis_data,
  294. NULL,
  295. };
  296. void batadv_debugfs_init(void)
  297. {
  298. struct batadv_debuginfo **bat_debug;
  299. struct dentry *file;
  300. batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
  301. if (batadv_debugfs == ERR_PTR(-ENODEV))
  302. batadv_debugfs = NULL;
  303. if (!batadv_debugfs)
  304. goto err;
  305. for (bat_debug = batadv_general_debuginfos; *bat_debug; ++bat_debug) {
  306. file = debugfs_create_file(((*bat_debug)->attr).name,
  307. S_IFREG | ((*bat_debug)->attr).mode,
  308. batadv_debugfs, NULL,
  309. &(*bat_debug)->fops);
  310. if (!file) {
  311. pr_err("Can't add general debugfs file: %s\n",
  312. ((*bat_debug)->attr).name);
  313. goto err;
  314. }
  315. }
  316. return;
  317. err:
  318. debugfs_remove_recursive(batadv_debugfs);
  319. }
  320. void batadv_debugfs_destroy(void)
  321. {
  322. debugfs_remove_recursive(batadv_debugfs);
  323. batadv_debugfs = NULL;
  324. }
  325. int batadv_debugfs_add_meshif(struct net_device *dev)
  326. {
  327. struct batadv_priv *bat_priv = netdev_priv(dev);
  328. struct batadv_debuginfo **bat_debug;
  329. struct dentry *file;
  330. if (!batadv_debugfs)
  331. goto out;
  332. bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
  333. if (!bat_priv->debug_dir)
  334. goto out;
  335. if (batadv_socket_setup(bat_priv) < 0)
  336. goto rem_attr;
  337. if (batadv_debug_log_setup(bat_priv) < 0)
  338. goto rem_attr;
  339. for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
  340. file = debugfs_create_file(((*bat_debug)->attr).name,
  341. S_IFREG | ((*bat_debug)->attr).mode,
  342. bat_priv->debug_dir,
  343. dev, &(*bat_debug)->fops);
  344. if (!file) {
  345. batadv_err(dev, "Can't add debugfs file: %s/%s\n",
  346. dev->name, ((*bat_debug)->attr).name);
  347. goto rem_attr;
  348. }
  349. }
  350. return 0;
  351. rem_attr:
  352. debugfs_remove_recursive(bat_priv->debug_dir);
  353. bat_priv->debug_dir = NULL;
  354. out:
  355. #ifdef CONFIG_DEBUG_FS
  356. return -ENOMEM;
  357. #else
  358. return 0;
  359. #endif /* CONFIG_DEBUG_FS */
  360. }
  361. void batadv_debugfs_del_meshif(struct net_device *dev)
  362. {
  363. struct batadv_priv *bat_priv = netdev_priv(dev);
  364. batadv_debug_log_cleanup(bat_priv);
  365. if (batadv_debugfs) {
  366. debugfs_remove_recursive(bat_priv->debug_dir);
  367. bat_priv->debug_dir = NULL;
  368. }
  369. }