debugfs.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. static struct dentry *batadv_debugfs;
  32. #ifdef CONFIG_BATMAN_ADV_DEBUG
  33. #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
  34. static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
  35. static char *batadv_log_char_addr(struct batadv_debug_log *debug_log,
  36. size_t idx)
  37. {
  38. return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK];
  39. }
  40. static void batadv_emit_log_char(struct batadv_debug_log *debug_log, 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_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. nonseekable_open(inode, file);
  82. file->private_data = inode->i_private;
  83. batadv_inc_module_count();
  84. return 0;
  85. }
  86. static int batadv_log_release(struct inode *inode, struct file *file)
  87. {
  88. batadv_dec_module_count();
  89. return 0;
  90. }
  91. static ssize_t batadv_log_read(struct file *file, char __user *buf,
  92. size_t count, loff_t *ppos)
  93. {
  94. struct batadv_priv *bat_priv = file->private_data;
  95. struct batadv_debug_log *debug_log = bat_priv->debug_log;
  96. int error, i = 0;
  97. char *char_addr;
  98. char c;
  99. if ((file->f_flags & O_NONBLOCK) &&
  100. !(debug_log->log_end - debug_log->log_start))
  101. return -EAGAIN;
  102. if (!buf)
  103. return -EINVAL;
  104. if (count == 0)
  105. return 0;
  106. if (!access_ok(VERIFY_WRITE, buf, count))
  107. return -EFAULT;
  108. error = wait_event_interruptible(debug_log->queue_wait,
  109. (debug_log->log_start - debug_log->log_end));
  110. if (error)
  111. return error;
  112. spin_lock_bh(&debug_log->lock);
  113. while ((!error) && (i < count) &&
  114. (debug_log->log_start != debug_log->log_end)) {
  115. char_addr = batadv_log_char_addr(debug_log,
  116. debug_log->log_start);
  117. c = *char_addr;
  118. debug_log->log_start++;
  119. spin_unlock_bh(&debug_log->lock);
  120. error = __put_user(c, buf);
  121. spin_lock_bh(&debug_log->lock);
  122. buf++;
  123. i++;
  124. }
  125. spin_unlock_bh(&debug_log->lock);
  126. if (!error)
  127. return i;
  128. return error;
  129. }
  130. static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
  131. {
  132. struct batadv_priv *bat_priv = file->private_data;
  133. struct batadv_debug_log *debug_log = bat_priv->debug_log;
  134. poll_wait(file, &debug_log->queue_wait, wait);
  135. if (debug_log->log_end - debug_log->log_start)
  136. return POLLIN | POLLRDNORM;
  137. return 0;
  138. }
  139. static const struct file_operations batadv_log_fops = {
  140. .open = batadv_log_open,
  141. .release = batadv_log_release,
  142. .read = batadv_log_read,
  143. .poll = batadv_log_poll,
  144. .llseek = no_llseek,
  145. };
  146. static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
  147. {
  148. struct dentry *d;
  149. if (!bat_priv->debug_dir)
  150. goto err;
  151. bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
  152. if (!bat_priv->debug_log)
  153. goto err;
  154. spin_lock_init(&bat_priv->debug_log->lock);
  155. init_waitqueue_head(&bat_priv->debug_log->queue_wait);
  156. d = debugfs_create_file("log", S_IFREG | S_IRUSR,
  157. bat_priv->debug_dir, bat_priv,
  158. &batadv_log_fops);
  159. if (!d)
  160. goto err;
  161. return 0;
  162. err:
  163. return -ENOMEM;
  164. }
  165. static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
  166. {
  167. kfree(bat_priv->debug_log);
  168. bat_priv->debug_log = NULL;
  169. }
  170. #else /* CONFIG_BATMAN_ADV_DEBUG */
  171. static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
  172. {
  173. bat_priv->debug_log = NULL;
  174. return 0;
  175. }
  176. static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
  177. {
  178. return;
  179. }
  180. #endif
  181. static int batadv_algorithms_open(struct inode *inode, struct file *file)
  182. {
  183. return single_open(file, batadv_algo_seq_print_text, NULL);
  184. }
  185. static int batadv_originators_open(struct inode *inode, struct file *file)
  186. {
  187. struct net_device *net_dev = (struct net_device *)inode->i_private;
  188. return single_open(file, batadv_orig_seq_print_text, net_dev);
  189. }
  190. static int batadv_gateways_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_gw_client_seq_print_text, net_dev);
  194. }
  195. static int batadv_transtable_global_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_tt_global_seq_print_text, net_dev);
  199. }
  200. #ifdef CONFIG_BATMAN_ADV_BLA
  201. static int batadv_bla_claim_table_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_bla_claim_table_seq_print_text,
  205. net_dev);
  206. }
  207. #endif
  208. static int batadv_transtable_local_open(struct inode *inode, struct file *file)
  209. {
  210. struct net_device *net_dev = (struct net_device *)inode->i_private;
  211. return single_open(file, batadv_tt_local_seq_print_text, net_dev);
  212. }
  213. static int batadv_vis_data_open(struct inode *inode, struct file *file)
  214. {
  215. struct net_device *net_dev = (struct net_device *)inode->i_private;
  216. return single_open(file, batadv_vis_seq_print_text, net_dev);
  217. }
  218. struct batadv_debuginfo {
  219. struct attribute attr;
  220. const struct file_operations fops;
  221. };
  222. #define BATADV_DEBUGINFO(_name, _mode, _open) \
  223. struct batadv_debuginfo batadv_debuginfo_##_name = { \
  224. .attr = { .name = __stringify(_name), \
  225. .mode = _mode, }, \
  226. .fops = { .owner = THIS_MODULE, \
  227. .open = _open, \
  228. .read = seq_read, \
  229. .llseek = seq_lseek, \
  230. .release = single_release, \
  231. } \
  232. };
  233. static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
  234. static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
  235. static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
  236. static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
  237. batadv_transtable_global_open);
  238. #ifdef CONFIG_BATMAN_ADV_BLA
  239. static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
  240. #endif
  241. static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
  242. batadv_transtable_local_open);
  243. static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
  244. static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
  245. &batadv_debuginfo_originators,
  246. &batadv_debuginfo_gateways,
  247. &batadv_debuginfo_transtable_global,
  248. #ifdef CONFIG_BATMAN_ADV_BLA
  249. &batadv_debuginfo_bla_claim_table,
  250. #endif
  251. &batadv_debuginfo_transtable_local,
  252. &batadv_debuginfo_vis_data,
  253. NULL,
  254. };
  255. void batadv_debugfs_init(void)
  256. {
  257. struct batadv_debuginfo *bat_debug;
  258. struct dentry *file;
  259. batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
  260. if (batadv_debugfs == ERR_PTR(-ENODEV))
  261. batadv_debugfs = NULL;
  262. if (!batadv_debugfs)
  263. goto out;
  264. bat_debug = &batadv_debuginfo_routing_algos;
  265. file = debugfs_create_file(bat_debug->attr.name,
  266. S_IFREG | bat_debug->attr.mode,
  267. batadv_debugfs, NULL, &bat_debug->fops);
  268. if (!file)
  269. pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name);
  270. out:
  271. return;
  272. }
  273. void batadv_debugfs_destroy(void)
  274. {
  275. if (batadv_debugfs) {
  276. debugfs_remove_recursive(batadv_debugfs);
  277. batadv_debugfs = NULL;
  278. }
  279. }
  280. int batadv_debugfs_add_meshif(struct net_device *dev)
  281. {
  282. struct batadv_priv *bat_priv = netdev_priv(dev);
  283. struct batadv_debuginfo **bat_debug;
  284. struct dentry *file;
  285. if (!batadv_debugfs)
  286. goto out;
  287. bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
  288. if (!bat_priv->debug_dir)
  289. goto out;
  290. if (batadv_socket_setup(bat_priv) < 0)
  291. goto rem_attr;
  292. if (batadv_debug_log_setup(bat_priv) < 0)
  293. goto rem_attr;
  294. for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
  295. file = debugfs_create_file(((*bat_debug)->attr).name,
  296. S_IFREG | ((*bat_debug)->attr).mode,
  297. bat_priv->debug_dir,
  298. dev, &(*bat_debug)->fops);
  299. if (!file) {
  300. batadv_err(dev, "Can't add debugfs file: %s/%s\n",
  301. dev->name, ((*bat_debug)->attr).name);
  302. goto rem_attr;
  303. }
  304. }
  305. return 0;
  306. rem_attr:
  307. debugfs_remove_recursive(bat_priv->debug_dir);
  308. bat_priv->debug_dir = NULL;
  309. out:
  310. #ifdef CONFIG_DEBUG_FS
  311. return -ENOMEM;
  312. #else
  313. return 0;
  314. #endif /* CONFIG_DEBUG_FS */
  315. }
  316. void batadv_debugfs_del_meshif(struct net_device *dev)
  317. {
  318. struct batadv_priv *bat_priv = netdev_priv(dev);
  319. batadv_debug_log_cleanup(bat_priv);
  320. if (batadv_debugfs) {
  321. debugfs_remove_recursive(bat_priv->debug_dir);
  322. bat_priv->debug_dir = NULL;
  323. }
  324. }