bat_debugfs.c 8.3 KB

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