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. static int fdebug_log(struct debug_log *debug_log, char *fmt, ...)
  45. {
  46. va_list args;
  47. static char debug_log_buf[256];
  48. char *p;
  49. if (!debug_log)
  50. return 0;
  51. spin_lock_bh(&debug_log->lock);
  52. va_start(args, fmt);
  53. vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
  54. va_end(args);
  55. for (p = debug_log_buf; *p != 0; p++)
  56. emit_log_char(debug_log, *p);
  57. spin_unlock_bh(&debug_log->lock);
  58. wake_up(&debug_log->queue_wait);
  59. return 0;
  60. }
  61. int debug_log(struct bat_priv *bat_priv, char *fmt, ...)
  62. {
  63. va_list args;
  64. char tmp_log_buf[256];
  65. va_start(args, fmt);
  66. vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
  67. fdebug_log(bat_priv->debug_log, "[%10u] %s",
  68. (jiffies / HZ), tmp_log_buf);
  69. va_end(args);
  70. return 0;
  71. }
  72. static int log_open(struct inode *inode, struct file *file)
  73. {
  74. nonseekable_open(inode, file);
  75. file->private_data = inode->i_private;
  76. inc_module_count();
  77. return 0;
  78. }
  79. static int log_release(struct inode *inode, struct file *file)
  80. {
  81. dec_module_count();
  82. return 0;
  83. }
  84. static ssize_t log_read(struct file *file, char __user *buf,
  85. size_t count, loff_t *ppos)
  86. {
  87. struct bat_priv *bat_priv = file->private_data;
  88. struct debug_log *debug_log = bat_priv->debug_log;
  89. int error, i = 0;
  90. char c;
  91. if ((file->f_flags & O_NONBLOCK) &&
  92. !(debug_log->log_end - debug_log->log_start))
  93. return -EAGAIN;
  94. if ((!buf) || (count < 0))
  95. return -EINVAL;
  96. if (count == 0)
  97. return 0;
  98. if (!access_ok(VERIFY_WRITE, buf, count))
  99. return -EFAULT;
  100. error = wait_event_interruptible(debug_log->queue_wait,
  101. (debug_log->log_start - debug_log->log_end));
  102. if (error)
  103. return error;
  104. spin_lock_bh(&debug_log->lock);
  105. while ((!error) && (i < count) &&
  106. (debug_log->log_start != debug_log->log_end)) {
  107. c = LOG_BUFF(debug_log->log_start);
  108. debug_log->log_start++;
  109. spin_unlock_bh(&debug_log->lock);
  110. error = __put_user(c, buf);
  111. spin_lock_bh(&debug_log->lock);
  112. buf++;
  113. i++;
  114. }
  115. spin_unlock_bh(&debug_log->lock);
  116. if (!error)
  117. return i;
  118. return error;
  119. }
  120. static unsigned int log_poll(struct file *file, poll_table *wait)
  121. {
  122. struct bat_priv *bat_priv = file->private_data;
  123. struct debug_log *debug_log = bat_priv->debug_log;
  124. poll_wait(file, &debug_log->queue_wait, wait);
  125. if (debug_log->log_end - debug_log->log_start)
  126. return POLLIN | POLLRDNORM;
  127. return 0;
  128. }
  129. static const struct file_operations log_fops = {
  130. .open = log_open,
  131. .release = log_release,
  132. .read = log_read,
  133. .poll = log_poll,
  134. .llseek = no_llseek,
  135. };
  136. static int debug_log_setup(struct bat_priv *bat_priv)
  137. {
  138. struct dentry *d;
  139. if (!bat_priv->debug_dir)
  140. goto err;
  141. bat_priv->debug_log = kzalloc(sizeof(struct debug_log), GFP_ATOMIC);
  142. if (!bat_priv->debug_log)
  143. goto err;
  144. spin_lock_init(&bat_priv->debug_log->lock);
  145. init_waitqueue_head(&bat_priv->debug_log->queue_wait);
  146. d = debugfs_create_file("log", S_IFREG | S_IRUSR,
  147. bat_priv->debug_dir, bat_priv, &log_fops);
  148. if (d)
  149. goto err;
  150. return 0;
  151. err:
  152. return 1;
  153. }
  154. static void debug_log_cleanup(struct bat_priv *bat_priv)
  155. {
  156. kfree(bat_priv->debug_log);
  157. bat_priv->debug_log = NULL;
  158. }
  159. #else /* CONFIG_BATMAN_ADV_DEBUG */
  160. static int debug_log_setup(struct bat_priv *bat_priv)
  161. {
  162. bat_priv->debug_log = NULL;
  163. return 0;
  164. }
  165. static void debug_log_cleanup(struct bat_priv *bat_priv)
  166. {
  167. return;
  168. }
  169. #endif
  170. static int originators_open(struct inode *inode, struct file *file)
  171. {
  172. struct net_device *net_dev = (struct net_device *)inode->i_private;
  173. return single_open(file, orig_seq_print_text, net_dev);
  174. }
  175. static int gateways_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, gw_client_seq_print_text, net_dev);
  179. }
  180. static int softif_neigh_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, softif_neigh_seq_print_text, net_dev);
  184. }
  185. static int transtable_global_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, tt_global_seq_print_text, net_dev);
  189. }
  190. static int transtable_local_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_local_seq_print_text, net_dev);
  194. }
  195. static int vis_data_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, vis_seq_print_text, net_dev);
  199. }
  200. struct bat_debuginfo {
  201. struct attribute attr;
  202. const struct file_operations fops;
  203. };
  204. #define BAT_DEBUGINFO(_name, _mode, _open) \
  205. struct bat_debuginfo bat_debuginfo_##_name = { \
  206. .attr = { .name = __stringify(_name), \
  207. .mode = _mode, }, \
  208. .fops = { .owner = THIS_MODULE, \
  209. .open = _open, \
  210. .read = seq_read, \
  211. .llseek = seq_lseek, \
  212. .release = single_release, \
  213. } \
  214. };
  215. static BAT_DEBUGINFO(originators, S_IRUGO, originators_open);
  216. static BAT_DEBUGINFO(gateways, S_IRUGO, gateways_open);
  217. static BAT_DEBUGINFO(softif_neigh, S_IRUGO, softif_neigh_open);
  218. static BAT_DEBUGINFO(transtable_global, S_IRUGO, transtable_global_open);
  219. static BAT_DEBUGINFO(transtable_local, S_IRUGO, transtable_local_open);
  220. static BAT_DEBUGINFO(vis_data, S_IRUGO, vis_data_open);
  221. static struct bat_debuginfo *mesh_debuginfos[] = {
  222. &bat_debuginfo_originators,
  223. &bat_debuginfo_gateways,
  224. &bat_debuginfo_softif_neigh,
  225. &bat_debuginfo_transtable_global,
  226. &bat_debuginfo_transtable_local,
  227. &bat_debuginfo_vis_data,
  228. NULL,
  229. };
  230. void debugfs_init(void)
  231. {
  232. bat_debugfs = debugfs_create_dir(DEBUGFS_BAT_SUBDIR, NULL);
  233. if (bat_debugfs == ERR_PTR(-ENODEV))
  234. bat_debugfs = NULL;
  235. }
  236. void debugfs_destroy(void)
  237. {
  238. if (bat_debugfs) {
  239. debugfs_remove_recursive(bat_debugfs);
  240. bat_debugfs = NULL;
  241. }
  242. }
  243. int debugfs_add_meshif(struct net_device *dev)
  244. {
  245. struct bat_priv *bat_priv = netdev_priv(dev);
  246. struct bat_debuginfo **bat_debug;
  247. struct dentry *file;
  248. if (!bat_debugfs)
  249. goto out;
  250. bat_priv->debug_dir = debugfs_create_dir(dev->name, bat_debugfs);
  251. if (!bat_priv->debug_dir)
  252. goto out;
  253. bat_socket_setup(bat_priv);
  254. debug_log_setup(bat_priv);
  255. for (bat_debug = mesh_debuginfos; *bat_debug; ++bat_debug) {
  256. file = debugfs_create_file(((*bat_debug)->attr).name,
  257. S_IFREG | ((*bat_debug)->attr).mode,
  258. bat_priv->debug_dir,
  259. dev, &(*bat_debug)->fops);
  260. if (!file) {
  261. bat_err(dev, "Can't add debugfs file: %s/%s\n",
  262. dev->name, ((*bat_debug)->attr).name);
  263. goto rem_attr;
  264. }
  265. }
  266. return 0;
  267. rem_attr:
  268. debugfs_remove_recursive(bat_priv->debug_dir);
  269. bat_priv->debug_dir = NULL;
  270. out:
  271. #ifdef CONFIG_DEBUG_FS
  272. return -ENOMEM;
  273. #else
  274. return 0;
  275. #endif /* CONFIG_DEBUG_FS */
  276. }
  277. void debugfs_del_meshif(struct net_device *dev)
  278. {
  279. struct bat_priv *bat_priv = netdev_priv(dev);
  280. debug_log_cleanup(bat_priv);
  281. if (bat_debugfs) {
  282. debugfs_remove_recursive(bat_priv->debug_dir);
  283. bat_priv->debug_dir = NULL;
  284. }
  285. }