debugfs.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 "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. static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
  35. static char *batadv_log_char_addr(struct batadv_debug_log *debug_log,
  36. size_t idx)
  37. {
  38. return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK];
  39. }
  40. static void batadv_emit_log_char(struct batadv_debug_log *debug_log, char c)
  41. {
  42. char *char_addr;
  43. char_addr = batadv_log_char_addr(debug_log, debug_log->log_end);
  44. *char_addr = c;
  45. debug_log->log_end++;
  46. if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
  47. debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
  48. }
  49. __printf(2, 3)
  50. static int batadv_fdebug_log(struct batadv_debug_log *debug_log,
  51. const char *fmt, ...)
  52. {
  53. va_list args;
  54. static char debug_log_buf[256];
  55. char *p;
  56. if (!debug_log)
  57. return 0;
  58. spin_lock_bh(&debug_log->lock);
  59. va_start(args, fmt);
  60. vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
  61. va_end(args);
  62. for (p = debug_log_buf; *p != 0; p++)
  63. batadv_emit_log_char(debug_log, *p);
  64. spin_unlock_bh(&debug_log->lock);
  65. wake_up(&debug_log->queue_wait);
  66. return 0;
  67. }
  68. int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
  69. {
  70. va_list args;
  71. char tmp_log_buf[256];
  72. va_start(args, fmt);
  73. vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
  74. batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
  75. jiffies_to_msecs(jiffies), tmp_log_buf);
  76. va_end(args);
  77. return 0;
  78. }
  79. static int batadv_log_open(struct inode *inode, struct file *file)
  80. {
  81. nonseekable_open(inode, file);
  82. file->private_data = inode->i_private;
  83. batadv_inc_module_count();
  84. return 0;
  85. }
  86. static int batadv_log_release(struct inode *inode, struct file *file)
  87. {
  88. batadv_dec_module_count();
  89. return 0;
  90. }
  91. static int batadv_log_empty(struct batadv_debug_log *debug_log)
  92. {
  93. return !(debug_log->log_start - debug_log->log_end);
  94. }
  95. static ssize_t batadv_log_read(struct file *file, char __user *buf,
  96. size_t count, loff_t *ppos)
  97. {
  98. struct batadv_priv *bat_priv = file->private_data;
  99. struct batadv_debug_log *debug_log = bat_priv->debug_log;
  100. int error, i = 0;
  101. char *char_addr;
  102. char c;
  103. if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log))
  104. return -EAGAIN;
  105. if (!buf)
  106. return -EINVAL;
  107. if (count == 0)
  108. return 0;
  109. if (!access_ok(VERIFY_WRITE, buf, count))
  110. return -EFAULT;
  111. error = wait_event_interruptible(debug_log->queue_wait,
  112. (!batadv_log_empty(debug_log)));
  113. if (error)
  114. return error;
  115. spin_lock_bh(&debug_log->lock);
  116. while ((!error) && (i < count) &&
  117. (debug_log->log_start != debug_log->log_end)) {
  118. char_addr = batadv_log_char_addr(debug_log,
  119. debug_log->log_start);
  120. c = *char_addr;
  121. debug_log->log_start++;
  122. spin_unlock_bh(&debug_log->lock);
  123. error = __put_user(c, buf);
  124. spin_lock_bh(&debug_log->lock);
  125. buf++;
  126. i++;
  127. }
  128. spin_unlock_bh(&debug_log->lock);
  129. if (!error)
  130. return i;
  131. return error;
  132. }
  133. static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
  134. {
  135. struct batadv_priv *bat_priv = file->private_data;
  136. struct batadv_debug_log *debug_log = bat_priv->debug_log;
  137. poll_wait(file, &debug_log->queue_wait, wait);
  138. if (!batadv_log_empty(debug_log))
  139. return POLLIN | POLLRDNORM;
  140. return 0;
  141. }
  142. static const struct file_operations batadv_log_fops = {
  143. .open = batadv_log_open,
  144. .release = batadv_log_release,
  145. .read = batadv_log_read,
  146. .poll = batadv_log_poll,
  147. .llseek = no_llseek,
  148. };
  149. static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
  150. {
  151. struct dentry *d;
  152. if (!bat_priv->debug_dir)
  153. goto err;
  154. bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
  155. if (!bat_priv->debug_log)
  156. goto err;
  157. spin_lock_init(&bat_priv->debug_log->lock);
  158. init_waitqueue_head(&bat_priv->debug_log->queue_wait);
  159. d = debugfs_create_file("log", S_IFREG | S_IRUSR,
  160. bat_priv->debug_dir, bat_priv,
  161. &batadv_log_fops);
  162. if (!d)
  163. goto err;
  164. return 0;
  165. err:
  166. return -ENOMEM;
  167. }
  168. static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
  169. {
  170. kfree(bat_priv->debug_log);
  171. bat_priv->debug_log = NULL;
  172. }
  173. #else /* CONFIG_BATMAN_ADV_DEBUG */
  174. static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
  175. {
  176. bat_priv->debug_log = NULL;
  177. return 0;
  178. }
  179. static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
  180. {
  181. return;
  182. }
  183. #endif
  184. static int batadv_algorithms_open(struct inode *inode, struct file *file)
  185. {
  186. return single_open(file, batadv_algo_seq_print_text, NULL);
  187. }
  188. static int batadv_originators_open(struct inode *inode, struct file *file)
  189. {
  190. struct net_device *net_dev = (struct net_device *)inode->i_private;
  191. return single_open(file, batadv_orig_seq_print_text, net_dev);
  192. }
  193. static int batadv_gateways_open(struct inode *inode, struct file *file)
  194. {
  195. struct net_device *net_dev = (struct net_device *)inode->i_private;
  196. return single_open(file, batadv_gw_client_seq_print_text, net_dev);
  197. }
  198. static int batadv_transtable_global_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_global_seq_print_text, net_dev);
  202. }
  203. #ifdef CONFIG_BATMAN_ADV_BLA
  204. static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
  205. {
  206. struct net_device *net_dev = (struct net_device *)inode->i_private;
  207. return single_open(file, batadv_bla_claim_table_seq_print_text,
  208. net_dev);
  209. }
  210. #endif
  211. static int batadv_transtable_local_open(struct inode *inode, struct file *file)
  212. {
  213. struct net_device *net_dev = (struct net_device *)inode->i_private;
  214. return single_open(file, batadv_tt_local_seq_print_text, net_dev);
  215. }
  216. static int batadv_vis_data_open(struct inode *inode, struct file *file)
  217. {
  218. struct net_device *net_dev = (struct net_device *)inode->i_private;
  219. return single_open(file, batadv_vis_seq_print_text, net_dev);
  220. }
  221. struct batadv_debuginfo {
  222. struct attribute attr;
  223. const struct file_operations fops;
  224. };
  225. #define BATADV_DEBUGINFO(_name, _mode, _open) \
  226. struct batadv_debuginfo batadv_debuginfo_##_name = { \
  227. .attr = { .name = __stringify(_name), \
  228. .mode = _mode, }, \
  229. .fops = { .owner = THIS_MODULE, \
  230. .open = _open, \
  231. .read = seq_read, \
  232. .llseek = seq_lseek, \
  233. .release = single_release, \
  234. } \
  235. };
  236. static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
  237. static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
  238. static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
  239. static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
  240. batadv_transtable_global_open);
  241. #ifdef CONFIG_BATMAN_ADV_BLA
  242. static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
  243. #endif
  244. static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
  245. batadv_transtable_local_open);
  246. static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
  247. static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
  248. &batadv_debuginfo_originators,
  249. &batadv_debuginfo_gateways,
  250. &batadv_debuginfo_transtable_global,
  251. #ifdef CONFIG_BATMAN_ADV_BLA
  252. &batadv_debuginfo_bla_claim_table,
  253. #endif
  254. &batadv_debuginfo_transtable_local,
  255. &batadv_debuginfo_vis_data,
  256. NULL,
  257. };
  258. void batadv_debugfs_init(void)
  259. {
  260. struct batadv_debuginfo *bat_debug;
  261. struct dentry *file;
  262. batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
  263. if (batadv_debugfs == ERR_PTR(-ENODEV))
  264. batadv_debugfs = NULL;
  265. if (!batadv_debugfs)
  266. goto out;
  267. bat_debug = &batadv_debuginfo_routing_algos;
  268. file = debugfs_create_file(bat_debug->attr.name,
  269. S_IFREG | bat_debug->attr.mode,
  270. batadv_debugfs, NULL, &bat_debug->fops);
  271. if (!file)
  272. pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name);
  273. out:
  274. return;
  275. }
  276. void batadv_debugfs_destroy(void)
  277. {
  278. if (batadv_debugfs) {
  279. debugfs_remove_recursive(batadv_debugfs);
  280. batadv_debugfs = NULL;
  281. }
  282. }
  283. int batadv_debugfs_add_meshif(struct net_device *dev)
  284. {
  285. struct batadv_priv *bat_priv = netdev_priv(dev);
  286. struct batadv_debuginfo **bat_debug;
  287. struct dentry *file;
  288. if (!batadv_debugfs)
  289. goto out;
  290. bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
  291. if (!bat_priv->debug_dir)
  292. goto out;
  293. if (batadv_socket_setup(bat_priv) < 0)
  294. goto rem_attr;
  295. if (batadv_debug_log_setup(bat_priv) < 0)
  296. goto rem_attr;
  297. for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
  298. file = debugfs_create_file(((*bat_debug)->attr).name,
  299. S_IFREG | ((*bat_debug)->attr).mode,
  300. bat_priv->debug_dir,
  301. dev, &(*bat_debug)->fops);
  302. if (!file) {
  303. batadv_err(dev, "Can't add debugfs file: %s/%s\n",
  304. dev->name, ((*bat_debug)->attr).name);
  305. goto rem_attr;
  306. }
  307. }
  308. return 0;
  309. rem_attr:
  310. debugfs_remove_recursive(bat_priv->debug_dir);
  311. bat_priv->debug_dir = NULL;
  312. out:
  313. #ifdef CONFIG_DEBUG_FS
  314. return -ENOMEM;
  315. #else
  316. return 0;
  317. #endif /* CONFIG_DEBUG_FS */
  318. }
  319. void batadv_debugfs_del_meshif(struct net_device *dev)
  320. {
  321. struct batadv_priv *bat_priv = netdev_priv(dev);
  322. batadv_debug_log_cleanup(bat_priv);
  323. if (batadv_debugfs) {
  324. debugfs_remove_recursive(bat_priv->debug_dir);
  325. bat_priv->debug_dir = NULL;
  326. }
  327. }