bat_debugfs.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 "bat_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. #define BATADV_LOG_BUFF(idx) (debug_log->log_buff[(idx) & BATADV_LOG_BUFF_MASK])
  35. static int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
  36. static void batadv_emit_log_char(struct debug_log *debug_log, char c)
  37. {
  38. BATADV_LOG_BUFF(debug_log->log_end) = c;
  39. debug_log->log_end++;
  40. if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
  41. debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
  42. }
  43. __printf(2, 3)
  44. static int batadv_fdebug_log(struct debug_log *debug_log, const 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. batadv_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 batadv_debug_log(struct bat_priv *bat_priv, const 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. batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
  68. jiffies_to_msecs(jiffies), tmp_log_buf);
  69. va_end(args);
  70. return 0;
  71. }
  72. static int batadv_log_open(struct inode *inode, struct file *file)
  73. {
  74. nonseekable_open(inode, file);
  75. file->private_data = inode->i_private;
  76. batadv_inc_module_count();
  77. return 0;
  78. }
  79. static int batadv_log_release(struct inode *inode, struct file *file)
  80. {
  81. batadv_dec_module_count();
  82. return 0;
  83. }
  84. static ssize_t batadv_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)
  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 = BATADV_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 batadv_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 batadv_log_fops = {
  130. .open = batadv_log_open,
  131. .release = batadv_log_release,
  132. .read = batadv_log_read,
  133. .poll = batadv_log_poll,
  134. .llseek = no_llseek,
  135. };
  136. static int batadv_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(*bat_priv->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,
  148. &batadv_log_fops);
  149. if (!d)
  150. goto err;
  151. return 0;
  152. err:
  153. return -ENOMEM;
  154. }
  155. static void batadv_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 batadv_debug_log_setup(struct bat_priv *bat_priv)
  162. {
  163. bat_priv->debug_log = NULL;
  164. return 0;
  165. }
  166. static void batadv_debug_log_cleanup(struct bat_priv *bat_priv)
  167. {
  168. return;
  169. }
  170. #endif
  171. static int batadv_algorithms_open(struct inode *inode, struct file *file)
  172. {
  173. return single_open(file, batadv_algo_seq_print_text, NULL);
  174. }
  175. static int batadv_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, batadv_orig_seq_print_text, net_dev);
  179. }
  180. static int batadv_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, batadv_gw_client_seq_print_text, net_dev);
  184. }
  185. static int batadv_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, batadv_tt_global_seq_print_text, net_dev);
  189. }
  190. #ifdef CONFIG_BATMAN_ADV_BLA
  191. static int batadv_bla_claim_table_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, batadv_bla_claim_table_seq_print_text,
  195. net_dev);
  196. }
  197. #endif
  198. static int batadv_transtable_local_open(struct inode *inode, struct file *file)
  199. {
  200. struct net_device *net_dev = (struct net_device *)inode->i_private;
  201. return single_open(file, batadv_tt_local_seq_print_text, net_dev);
  202. }
  203. static int batadv_vis_data_open(struct inode *inode, struct file *file)
  204. {
  205. struct net_device *net_dev = (struct net_device *)inode->i_private;
  206. return single_open(file, batadv_vis_seq_print_text, net_dev);
  207. }
  208. struct bat_debuginfo {
  209. struct attribute attr;
  210. const struct file_operations fops;
  211. };
  212. #define BATADV_DEBUGINFO(_name, _mode, _open) \
  213. struct bat_debuginfo batadv_debuginfo_##_name = { \
  214. .attr = { .name = __stringify(_name), \
  215. .mode = _mode, }, \
  216. .fops = { .owner = THIS_MODULE, \
  217. .open = _open, \
  218. .read = seq_read, \
  219. .llseek = seq_lseek, \
  220. .release = single_release, \
  221. } \
  222. };
  223. static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
  224. static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
  225. static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
  226. static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
  227. batadv_transtable_global_open);
  228. #ifdef CONFIG_BATMAN_ADV_BLA
  229. static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
  230. #endif
  231. static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
  232. batadv_transtable_local_open);
  233. static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
  234. static struct bat_debuginfo *batadv_mesh_debuginfos[] = {
  235. &batadv_debuginfo_originators,
  236. &batadv_debuginfo_gateways,
  237. &batadv_debuginfo_transtable_global,
  238. #ifdef CONFIG_BATMAN_ADV_BLA
  239. &batadv_debuginfo_bla_claim_table,
  240. #endif
  241. &batadv_debuginfo_transtable_local,
  242. &batadv_debuginfo_vis_data,
  243. NULL,
  244. };
  245. void batadv_debugfs_init(void)
  246. {
  247. struct bat_debuginfo *bat_debug;
  248. struct dentry *file;
  249. batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
  250. if (batadv_debugfs == ERR_PTR(-ENODEV))
  251. batadv_debugfs = NULL;
  252. if (!batadv_debugfs)
  253. goto out;
  254. bat_debug = &batadv_debuginfo_routing_algos;
  255. file = debugfs_create_file(bat_debug->attr.name,
  256. S_IFREG | bat_debug->attr.mode,
  257. batadv_debugfs, NULL, &bat_debug->fops);
  258. if (!file)
  259. pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name);
  260. out:
  261. return;
  262. }
  263. void batadv_debugfs_destroy(void)
  264. {
  265. if (batadv_debugfs) {
  266. debugfs_remove_recursive(batadv_debugfs);
  267. batadv_debugfs = NULL;
  268. }
  269. }
  270. int batadv_debugfs_add_meshif(struct net_device *dev)
  271. {
  272. struct bat_priv *bat_priv = netdev_priv(dev);
  273. struct bat_debuginfo **bat_debug;
  274. struct dentry *file;
  275. if (!batadv_debugfs)
  276. goto out;
  277. bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
  278. if (!bat_priv->debug_dir)
  279. goto out;
  280. if (batadv_socket_setup(bat_priv) < 0)
  281. goto rem_attr;
  282. if (batadv_debug_log_setup(bat_priv) < 0)
  283. goto rem_attr;
  284. for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
  285. file = debugfs_create_file(((*bat_debug)->attr).name,
  286. S_IFREG | ((*bat_debug)->attr).mode,
  287. bat_priv->debug_dir,
  288. dev, &(*bat_debug)->fops);
  289. if (!file) {
  290. batadv_err(dev, "Can't add debugfs file: %s/%s\n",
  291. dev->name, ((*bat_debug)->attr).name);
  292. goto rem_attr;
  293. }
  294. }
  295. return 0;
  296. rem_attr:
  297. debugfs_remove_recursive(bat_priv->debug_dir);
  298. bat_priv->debug_dir = NULL;
  299. out:
  300. #ifdef CONFIG_DEBUG_FS
  301. return -ENOMEM;
  302. #else
  303. return 0;
  304. #endif /* CONFIG_DEBUG_FS */
  305. }
  306. void batadv_debugfs_del_meshif(struct net_device *dev)
  307. {
  308. struct bat_priv *bat_priv = netdev_priv(dev);
  309. batadv_debug_log_cleanup(bat_priv);
  310. if (batadv_debugfs) {
  311. debugfs_remove_recursive(bat_priv->debug_dir);
  312. bat_priv->debug_dir = NULL;
  313. }
  314. }