bat_debugfs.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (C) 2010-2011 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 originators_open(struct inode *inode, struct file *file)
  172. {
  173. struct net_device *net_dev = (struct net_device *)inode->i_private;
  174. return single_open(file, orig_seq_print_text, net_dev);
  175. }
  176. static int gateways_open(struct inode *inode, struct file *file)
  177. {
  178. struct net_device *net_dev = (struct net_device *)inode->i_private;
  179. return single_open(file, gw_client_seq_print_text, net_dev);
  180. }
  181. static int softif_neigh_open(struct inode *inode, struct file *file)
  182. {
  183. struct net_device *net_dev = (struct net_device *)inode->i_private;
  184. return single_open(file, softif_neigh_seq_print_text, net_dev);
  185. }
  186. static int transtable_global_open(struct inode *inode, struct file *file)
  187. {
  188. struct net_device *net_dev = (struct net_device *)inode->i_private;
  189. return single_open(file, tt_global_seq_print_text, net_dev);
  190. }
  191. static int transtable_local_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, tt_local_seq_print_text, net_dev);
  195. }
  196. static int vis_data_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, vis_seq_print_text, net_dev);
  200. }
  201. struct bat_debuginfo {
  202. struct attribute attr;
  203. const struct file_operations fops;
  204. };
  205. #define BAT_DEBUGINFO(_name, _mode, _open) \
  206. struct bat_debuginfo bat_debuginfo_##_name = { \
  207. .attr = { .name = __stringify(_name), \
  208. .mode = _mode, }, \
  209. .fops = { .owner = THIS_MODULE, \
  210. .open = _open, \
  211. .read = seq_read, \
  212. .llseek = seq_lseek, \
  213. .release = single_release, \
  214. } \
  215. };
  216. static BAT_DEBUGINFO(originators, S_IRUGO, originators_open);
  217. static BAT_DEBUGINFO(gateways, S_IRUGO, gateways_open);
  218. static BAT_DEBUGINFO(softif_neigh, S_IRUGO, softif_neigh_open);
  219. static BAT_DEBUGINFO(transtable_global, S_IRUGO, transtable_global_open);
  220. static BAT_DEBUGINFO(transtable_local, S_IRUGO, transtable_local_open);
  221. static BAT_DEBUGINFO(vis_data, S_IRUGO, vis_data_open);
  222. static struct bat_debuginfo *mesh_debuginfos[] = {
  223. &bat_debuginfo_originators,
  224. &bat_debuginfo_gateways,
  225. &bat_debuginfo_softif_neigh,
  226. &bat_debuginfo_transtable_global,
  227. &bat_debuginfo_transtable_local,
  228. &bat_debuginfo_vis_data,
  229. NULL,
  230. };
  231. void debugfs_init(void)
  232. {
  233. bat_debugfs = debugfs_create_dir(DEBUGFS_BAT_SUBDIR, NULL);
  234. if (bat_debugfs == ERR_PTR(-ENODEV))
  235. bat_debugfs = NULL;
  236. }
  237. void debugfs_destroy(void)
  238. {
  239. if (bat_debugfs) {
  240. debugfs_remove_recursive(bat_debugfs);
  241. bat_debugfs = NULL;
  242. }
  243. }
  244. int debugfs_add_meshif(struct net_device *dev)
  245. {
  246. struct bat_priv *bat_priv = netdev_priv(dev);
  247. struct bat_debuginfo **bat_debug;
  248. struct dentry *file;
  249. if (!bat_debugfs)
  250. goto out;
  251. bat_priv->debug_dir = debugfs_create_dir(dev->name, bat_debugfs);
  252. if (!bat_priv->debug_dir)
  253. goto out;
  254. bat_socket_setup(bat_priv);
  255. debug_log_setup(bat_priv);
  256. for (bat_debug = mesh_debuginfos; *bat_debug; ++bat_debug) {
  257. file = debugfs_create_file(((*bat_debug)->attr).name,
  258. S_IFREG | ((*bat_debug)->attr).mode,
  259. bat_priv->debug_dir,
  260. dev, &(*bat_debug)->fops);
  261. if (!file) {
  262. bat_err(dev, "Can't add debugfs file: %s/%s\n",
  263. dev->name, ((*bat_debug)->attr).name);
  264. goto rem_attr;
  265. }
  266. }
  267. return 0;
  268. rem_attr:
  269. debugfs_remove_recursive(bat_priv->debug_dir);
  270. bat_priv->debug_dir = NULL;
  271. out:
  272. #ifdef CONFIG_DEBUG_FS
  273. return -ENOMEM;
  274. #else
  275. return 0;
  276. #endif /* CONFIG_DEBUG_FS */
  277. }
  278. void debugfs_del_meshif(struct net_device *dev)
  279. {
  280. struct bat_priv *bat_priv = netdev_priv(dev);
  281. debug_log_cleanup(bat_priv);
  282. if (bat_debugfs) {
  283. debugfs_remove_recursive(bat_priv->debug_dir);
  284. bat_priv->debug_dir = NULL;
  285. }
  286. }