bat_debugfs.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include <linux/debugfs.h>
  23. #include "bat_debugfs.h"
  24. #include "translation-table.h"
  25. #include "originator.h"
  26. #include "hard-interface.h"
  27. #include "gateway_common.h"
  28. #include "gateway_client.h"
  29. #include "soft-interface.h"
  30. #include "vis.h"
  31. #include "icmp_socket.h"
  32. static struct dentry *bat_debugfs;
  33. #ifdef CONFIG_BATMAN_ADV_DEBUG
  34. #define LOG_BUFF_MASK (log_buff_len-1)
  35. #define LOG_BUFF(idx) (debug_log->log_buff[(idx) & LOG_BUFF_MASK])
  36. static int log_buff_len = LOG_BUF_LEN;
  37. static void emit_log_char(struct debug_log *debug_log, char c)
  38. {
  39. LOG_BUFF(debug_log->log_end) = c;
  40. debug_log->log_end++;
  41. if (debug_log->log_end - debug_log->log_start > log_buff_len)
  42. debug_log->log_start = debug_log->log_end - log_buff_len;
  43. }
  44. __printf(2, 3)
  45. static int fdebug_log(struct debug_log *debug_log, const char *fmt, ...)
  46. {
  47. va_list args;
  48. static char debug_log_buf[256];
  49. char *p;
  50. if (!debug_log)
  51. return 0;
  52. spin_lock_bh(&debug_log->lock);
  53. va_start(args, fmt);
  54. vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
  55. va_end(args);
  56. for (p = debug_log_buf; *p != 0; p++)
  57. emit_log_char(debug_log, *p);
  58. spin_unlock_bh(&debug_log->lock);
  59. wake_up(&debug_log->queue_wait);
  60. return 0;
  61. }
  62. int debug_log(struct bat_priv *bat_priv, const char *fmt, ...)
  63. {
  64. va_list args;
  65. char tmp_log_buf[256];
  66. va_start(args, fmt);
  67. vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
  68. fdebug_log(bat_priv->debug_log, "[%10lu] %s",
  69. (jiffies / HZ), tmp_log_buf);
  70. va_end(args);
  71. return 0;
  72. }
  73. static int log_open(struct inode *inode, struct file *file)
  74. {
  75. nonseekable_open(inode, file);
  76. file->private_data = inode->i_private;
  77. inc_module_count();
  78. return 0;
  79. }
  80. static int log_release(struct inode *inode, struct file *file)
  81. {
  82. dec_module_count();
  83. return 0;
  84. }
  85. static ssize_t log_read(struct file *file, char __user *buf,
  86. size_t count, loff_t *ppos)
  87. {
  88. struct bat_priv *bat_priv = file->private_data;
  89. struct debug_log *debug_log = bat_priv->debug_log;
  90. int error, i = 0;
  91. char c;
  92. if ((file->f_flags & O_NONBLOCK) &&
  93. !(debug_log->log_end - debug_log->log_start))
  94. return -EAGAIN;
  95. if (!buf)
  96. return -EINVAL;
  97. if (count == 0)
  98. return 0;
  99. if (!access_ok(VERIFY_WRITE, buf, count))
  100. return -EFAULT;
  101. error = wait_event_interruptible(debug_log->queue_wait,
  102. (debug_log->log_start - debug_log->log_end));
  103. if (error)
  104. return error;
  105. spin_lock_bh(&debug_log->lock);
  106. while ((!error) && (i < count) &&
  107. (debug_log->log_start != debug_log->log_end)) {
  108. c = LOG_BUFF(debug_log->log_start);
  109. debug_log->log_start++;
  110. spin_unlock_bh(&debug_log->lock);
  111. error = __put_user(c, buf);
  112. spin_lock_bh(&debug_log->lock);
  113. buf++;
  114. i++;
  115. }
  116. spin_unlock_bh(&debug_log->lock);
  117. if (!error)
  118. return i;
  119. return error;
  120. }
  121. static unsigned int log_poll(struct file *file, poll_table *wait)
  122. {
  123. struct bat_priv *bat_priv = file->private_data;
  124. struct debug_log *debug_log = bat_priv->debug_log;
  125. poll_wait(file, &debug_log->queue_wait, wait);
  126. if (debug_log->log_end - debug_log->log_start)
  127. return POLLIN | POLLRDNORM;
  128. return 0;
  129. }
  130. static const struct file_operations log_fops = {
  131. .open = log_open,
  132. .release = log_release,
  133. .read = log_read,
  134. .poll = log_poll,
  135. .llseek = no_llseek,
  136. };
  137. static int debug_log_setup(struct bat_priv *bat_priv)
  138. {
  139. struct dentry *d;
  140. if (!bat_priv->debug_dir)
  141. goto err;
  142. bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
  143. if (!bat_priv->debug_log)
  144. goto err;
  145. spin_lock_init(&bat_priv->debug_log->lock);
  146. init_waitqueue_head(&bat_priv->debug_log->queue_wait);
  147. d = debugfs_create_file("log", S_IFREG | S_IRUSR,
  148. bat_priv->debug_dir, bat_priv, &log_fops);
  149. if (d)
  150. goto err;
  151. return 0;
  152. err:
  153. return 1;
  154. }
  155. static void debug_log_cleanup(struct bat_priv *bat_priv)
  156. {
  157. kfree(bat_priv->debug_log);
  158. bat_priv->debug_log = NULL;
  159. }
  160. #else /* CONFIG_BATMAN_ADV_DEBUG */
  161. static int debug_log_setup(struct bat_priv *bat_priv)
  162. {
  163. bat_priv->debug_log = NULL;
  164. return 0;
  165. }
  166. static void debug_log_cleanup(struct bat_priv *bat_priv)
  167. {
  168. return;
  169. }
  170. #endif
  171. static int bat_algorithms_open(struct inode *inode, struct file *file)
  172. {
  173. return single_open(file, bat_algo_seq_print_text, NULL);
  174. }
  175. static int originators_open(struct inode *inode, struct file *file)
  176. {
  177. struct net_device *net_dev = (struct net_device *)inode->i_private;
  178. return single_open(file, orig_seq_print_text, net_dev);
  179. }
  180. static int gateways_open(struct inode *inode, struct file *file)
  181. {
  182. struct net_device *net_dev = (struct net_device *)inode->i_private;
  183. return single_open(file, gw_client_seq_print_text, net_dev);
  184. }
  185. static int softif_neigh_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, softif_neigh_seq_print_text, net_dev);
  189. }
  190. static int transtable_global_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, tt_global_seq_print_text, net_dev);
  194. }
  195. static int transtable_local_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, tt_local_seq_print_text, net_dev);
  199. }
  200. static int vis_data_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, vis_seq_print_text, net_dev);
  204. }
  205. struct bat_debuginfo {
  206. struct attribute attr;
  207. const struct file_operations fops;
  208. };
  209. #define BAT_DEBUGINFO(_name, _mode, _open) \
  210. struct bat_debuginfo bat_debuginfo_##_name = { \
  211. .attr = { .name = __stringify(_name), \
  212. .mode = _mode, }, \
  213. .fops = { .owner = THIS_MODULE, \
  214. .open = _open, \
  215. .read = seq_read, \
  216. .llseek = seq_lseek, \
  217. .release = single_release, \
  218. } \
  219. };
  220. static BAT_DEBUGINFO(routing_algos, S_IRUGO, bat_algorithms_open);
  221. static BAT_DEBUGINFO(originators, S_IRUGO, originators_open);
  222. static BAT_DEBUGINFO(gateways, S_IRUGO, gateways_open);
  223. static BAT_DEBUGINFO(softif_neigh, S_IRUGO, softif_neigh_open);
  224. static BAT_DEBUGINFO(transtable_global, S_IRUGO, transtable_global_open);
  225. static BAT_DEBUGINFO(transtable_local, S_IRUGO, transtable_local_open);
  226. static BAT_DEBUGINFO(vis_data, S_IRUGO, vis_data_open);
  227. static struct bat_debuginfo *mesh_debuginfos[] = {
  228. &bat_debuginfo_originators,
  229. &bat_debuginfo_gateways,
  230. &bat_debuginfo_softif_neigh,
  231. &bat_debuginfo_transtable_global,
  232. &bat_debuginfo_transtable_local,
  233. &bat_debuginfo_vis_data,
  234. NULL,
  235. };
  236. void debugfs_init(void)
  237. {
  238. struct bat_debuginfo *bat_debug;
  239. struct dentry *file;
  240. bat_debugfs = debugfs_create_dir(DEBUGFS_BAT_SUBDIR, NULL);
  241. if (bat_debugfs == ERR_PTR(-ENODEV))
  242. bat_debugfs = NULL;
  243. if (!bat_debugfs)
  244. goto out;
  245. bat_debug = &bat_debuginfo_routing_algos;
  246. file = debugfs_create_file(bat_debug->attr.name,
  247. S_IFREG | bat_debug->attr.mode,
  248. bat_debugfs, NULL, &bat_debug->fops);
  249. if (!file)
  250. pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name);
  251. out:
  252. return;
  253. }
  254. void debugfs_destroy(void)
  255. {
  256. if (bat_debugfs) {
  257. debugfs_remove_recursive(bat_debugfs);
  258. bat_debugfs = NULL;
  259. }
  260. }
  261. int debugfs_add_meshif(struct net_device *dev)
  262. {
  263. struct bat_priv *bat_priv = netdev_priv(dev);
  264. struct bat_debuginfo **bat_debug;
  265. struct dentry *file;
  266. if (!bat_debugfs)
  267. goto out;
  268. bat_priv->debug_dir = debugfs_create_dir(dev->name, bat_debugfs);
  269. if (!bat_priv->debug_dir)
  270. goto out;
  271. bat_socket_setup(bat_priv);
  272. debug_log_setup(bat_priv);
  273. for (bat_debug = mesh_debuginfos; *bat_debug; ++bat_debug) {
  274. file = debugfs_create_file(((*bat_debug)->attr).name,
  275. S_IFREG | ((*bat_debug)->attr).mode,
  276. bat_priv->debug_dir,
  277. dev, &(*bat_debug)->fops);
  278. if (!file) {
  279. bat_err(dev, "Can't add debugfs file: %s/%s\n",
  280. dev->name, ((*bat_debug)->attr).name);
  281. goto rem_attr;
  282. }
  283. }
  284. return 0;
  285. rem_attr:
  286. debugfs_remove_recursive(bat_priv->debug_dir);
  287. bat_priv->debug_dir = NULL;
  288. out:
  289. #ifdef CONFIG_DEBUG_FS
  290. return -ENOMEM;
  291. #else
  292. return 0;
  293. #endif /* CONFIG_DEBUG_FS */
  294. }
  295. void debugfs_del_meshif(struct net_device *dev)
  296. {
  297. struct bat_priv *bat_priv = netdev_priv(dev);
  298. debug_log_cleanup(bat_priv);
  299. if (bat_debugfs) {
  300. debugfs_remove_recursive(bat_priv->debug_dir);
  301. bat_priv->debug_dir = NULL;
  302. }
  303. }