netdebug.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * netdebug.c
  5. *
  6. * debug functionality for o2net
  7. *
  8. * Copyright (C) 2005, 2008 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. *
  25. */
  26. #ifdef CONFIG_DEBUG_FS
  27. #include <linux/module.h>
  28. #include <linux/types.h>
  29. #include <linux/slab.h>
  30. #include <linux/idr.h>
  31. #include <linux/kref.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/debugfs.h>
  34. #include <linux/uaccess.h>
  35. #include "tcp.h"
  36. #include "nodemanager.h"
  37. #define MLOG_MASK_PREFIX ML_TCP
  38. #include "masklog.h"
  39. #include "tcp_internal.h"
  40. #define O2NET_DEBUG_DIR "o2net"
  41. #define SC_DEBUG_NAME "sock_containers"
  42. #define NST_DEBUG_NAME "send_tracking"
  43. static struct dentry *o2net_dentry;
  44. static struct dentry *sc_dentry;
  45. static struct dentry *nst_dentry;
  46. static DEFINE_SPINLOCK(o2net_debug_lock);
  47. static LIST_HEAD(sock_containers);
  48. static LIST_HEAD(send_tracking);
  49. void o2net_debug_add_nst(struct o2net_send_tracking *nst)
  50. {
  51. spin_lock(&o2net_debug_lock);
  52. list_add(&nst->st_net_debug_item, &send_tracking);
  53. spin_unlock(&o2net_debug_lock);
  54. }
  55. void o2net_debug_del_nst(struct o2net_send_tracking *nst)
  56. {
  57. spin_lock(&o2net_debug_lock);
  58. if (!list_empty(&nst->st_net_debug_item))
  59. list_del_init(&nst->st_net_debug_item);
  60. spin_unlock(&o2net_debug_lock);
  61. }
  62. static struct o2net_send_tracking
  63. *next_nst(struct o2net_send_tracking *nst_start)
  64. {
  65. struct o2net_send_tracking *nst, *ret = NULL;
  66. assert_spin_locked(&o2net_debug_lock);
  67. list_for_each_entry(nst, &nst_start->st_net_debug_item,
  68. st_net_debug_item) {
  69. /* discover the head of the list */
  70. if (&nst->st_net_debug_item == &send_tracking)
  71. break;
  72. /* use st_task to detect real nsts in the list */
  73. if (nst->st_task != NULL) {
  74. ret = nst;
  75. break;
  76. }
  77. }
  78. return ret;
  79. }
  80. static void *nst_seq_start(struct seq_file *seq, loff_t *pos)
  81. {
  82. struct o2net_send_tracking *nst, *dummy_nst = seq->private;
  83. spin_lock(&o2net_debug_lock);
  84. nst = next_nst(dummy_nst);
  85. spin_unlock(&o2net_debug_lock);
  86. return nst;
  87. }
  88. static void *nst_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  89. {
  90. struct o2net_send_tracking *nst, *dummy_nst = seq->private;
  91. spin_lock(&o2net_debug_lock);
  92. nst = next_nst(dummy_nst);
  93. list_del_init(&dummy_nst->st_net_debug_item);
  94. if (nst)
  95. list_add(&dummy_nst->st_net_debug_item,
  96. &nst->st_net_debug_item);
  97. spin_unlock(&o2net_debug_lock);
  98. return nst; /* unused, just needs to be null when done */
  99. }
  100. static int nst_seq_show(struct seq_file *seq, void *v)
  101. {
  102. struct o2net_send_tracking *nst, *dummy_nst = seq->private;
  103. spin_lock(&o2net_debug_lock);
  104. nst = next_nst(dummy_nst);
  105. if (nst != NULL) {
  106. /* get_task_comm isn't exported. oh well. */
  107. seq_printf(seq, "%p:\n"
  108. " pid: %lu\n"
  109. " tgid: %lu\n"
  110. " process name: %s\n"
  111. " node: %u\n"
  112. " sc: %p\n"
  113. " message id: %d\n"
  114. " message type: %u\n"
  115. " message key: 0x%08x\n"
  116. " sock acquiry: %lu.%ld\n"
  117. " send start: %lu.%ld\n"
  118. " wait start: %lu.%ld\n",
  119. nst, (unsigned long)nst->st_task->pid,
  120. (unsigned long)nst->st_task->tgid,
  121. nst->st_task->comm, nst->st_node,
  122. nst->st_sc, nst->st_id, nst->st_msg_type,
  123. nst->st_msg_key,
  124. nst->st_sock_time.tv_sec,
  125. (long)nst->st_sock_time.tv_usec,
  126. nst->st_send_time.tv_sec,
  127. (long)nst->st_send_time.tv_usec,
  128. nst->st_status_time.tv_sec,
  129. (long)nst->st_status_time.tv_usec);
  130. }
  131. spin_unlock(&o2net_debug_lock);
  132. return 0;
  133. }
  134. static void nst_seq_stop(struct seq_file *seq, void *v)
  135. {
  136. }
  137. static struct seq_operations nst_seq_ops = {
  138. .start = nst_seq_start,
  139. .next = nst_seq_next,
  140. .stop = nst_seq_stop,
  141. .show = nst_seq_show,
  142. };
  143. static int nst_fop_open(struct inode *inode, struct file *file)
  144. {
  145. struct o2net_send_tracking *dummy_nst;
  146. struct seq_file *seq;
  147. int ret;
  148. dummy_nst = kmalloc(sizeof(struct o2net_send_tracking), GFP_KERNEL);
  149. if (dummy_nst == NULL) {
  150. ret = -ENOMEM;
  151. goto out;
  152. }
  153. dummy_nst->st_task = NULL;
  154. ret = seq_open(file, &nst_seq_ops);
  155. if (ret)
  156. goto out;
  157. seq = file->private_data;
  158. seq->private = dummy_nst;
  159. o2net_debug_add_nst(dummy_nst);
  160. dummy_nst = NULL;
  161. out:
  162. kfree(dummy_nst);
  163. return ret;
  164. }
  165. static int nst_fop_release(struct inode *inode, struct file *file)
  166. {
  167. struct seq_file *seq = file->private_data;
  168. struct o2net_send_tracking *dummy_nst = seq->private;
  169. o2net_debug_del_nst(dummy_nst);
  170. return seq_release_private(inode, file);
  171. }
  172. static struct file_operations nst_seq_fops = {
  173. .open = nst_fop_open,
  174. .read = seq_read,
  175. .llseek = seq_lseek,
  176. .release = nst_fop_release,
  177. };
  178. void o2net_debug_add_sc(struct o2net_sock_container *sc)
  179. {
  180. spin_lock(&o2net_debug_lock);
  181. list_add(&sc->sc_net_debug_item, &sock_containers);
  182. spin_unlock(&o2net_debug_lock);
  183. }
  184. void o2net_debug_del_sc(struct o2net_sock_container *sc)
  185. {
  186. spin_lock(&o2net_debug_lock);
  187. list_del_init(&sc->sc_net_debug_item);
  188. spin_unlock(&o2net_debug_lock);
  189. }
  190. static struct o2net_sock_container
  191. *next_sc(struct o2net_sock_container *sc_start)
  192. {
  193. struct o2net_sock_container *sc, *ret = NULL;
  194. assert_spin_locked(&o2net_debug_lock);
  195. list_for_each_entry(sc, &sc_start->sc_net_debug_item,
  196. sc_net_debug_item) {
  197. /* discover the head of the list miscast as a sc */
  198. if (&sc->sc_net_debug_item == &sock_containers)
  199. break;
  200. /* use sc_page to detect real scs in the list */
  201. if (sc->sc_page != NULL) {
  202. ret = sc;
  203. break;
  204. }
  205. }
  206. return ret;
  207. }
  208. static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
  209. {
  210. struct o2net_sock_container *sc, *dummy_sc = seq->private;
  211. spin_lock(&o2net_debug_lock);
  212. sc = next_sc(dummy_sc);
  213. spin_unlock(&o2net_debug_lock);
  214. return sc;
  215. }
  216. static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  217. {
  218. struct o2net_sock_container *sc, *dummy_sc = seq->private;
  219. spin_lock(&o2net_debug_lock);
  220. sc = next_sc(dummy_sc);
  221. list_del_init(&dummy_sc->sc_net_debug_item);
  222. if (sc)
  223. list_add(&dummy_sc->sc_net_debug_item, &sc->sc_net_debug_item);
  224. spin_unlock(&o2net_debug_lock);
  225. return sc; /* unused, just needs to be null when done */
  226. }
  227. #define TV_SEC_USEC(TV) TV.tv_sec, (long)TV.tv_usec
  228. static int sc_seq_show(struct seq_file *seq, void *v)
  229. {
  230. struct o2net_sock_container *sc, *dummy_sc = seq->private;
  231. spin_lock(&o2net_debug_lock);
  232. sc = next_sc(dummy_sc);
  233. if (sc != NULL) {
  234. struct inet_sock *inet = NULL;
  235. __be32 saddr = 0, daddr = 0;
  236. __be16 sport = 0, dport = 0;
  237. if (sc->sc_sock) {
  238. inet = inet_sk(sc->sc_sock->sk);
  239. /* the stack's structs aren't sparse endian clean */
  240. saddr = (__force __be32)inet->saddr;
  241. daddr = (__force __be32)inet->daddr;
  242. sport = (__force __be16)inet->sport;
  243. dport = (__force __be16)inet->dport;
  244. }
  245. /* XXX sigh, inet-> doesn't have sparse annotation so any
  246. * use of it here generates a warning with -Wbitwise */
  247. seq_printf(seq, "%p:\n"
  248. " krefs: %d\n"
  249. " sock: %pI4:%u -> "
  250. "%pI4:%u\n"
  251. " remote node: %s\n"
  252. " page off: %zu\n"
  253. " handshake ok: %u\n"
  254. " timer: %lu.%ld\n"
  255. " data ready: %lu.%ld\n"
  256. " advance start: %lu.%ld\n"
  257. " advance stop: %lu.%ld\n"
  258. " func start: %lu.%ld\n"
  259. " func stop: %lu.%ld\n"
  260. " func key: %u\n"
  261. " func type: %u\n",
  262. sc,
  263. atomic_read(&sc->sc_kref.refcount),
  264. &saddr, inet ? ntohs(sport) : 0,
  265. &daddr, inet ? ntohs(dport) : 0,
  266. sc->sc_node->nd_name,
  267. sc->sc_page_off,
  268. sc->sc_handshake_ok,
  269. TV_SEC_USEC(sc->sc_tv_timer),
  270. TV_SEC_USEC(sc->sc_tv_data_ready),
  271. TV_SEC_USEC(sc->sc_tv_advance_start),
  272. TV_SEC_USEC(sc->sc_tv_advance_stop),
  273. TV_SEC_USEC(sc->sc_tv_func_start),
  274. TV_SEC_USEC(sc->sc_tv_func_stop),
  275. sc->sc_msg_key,
  276. sc->sc_msg_type);
  277. }
  278. spin_unlock(&o2net_debug_lock);
  279. return 0;
  280. }
  281. static void sc_seq_stop(struct seq_file *seq, void *v)
  282. {
  283. }
  284. static struct seq_operations sc_seq_ops = {
  285. .start = sc_seq_start,
  286. .next = sc_seq_next,
  287. .stop = sc_seq_stop,
  288. .show = sc_seq_show,
  289. };
  290. static int sc_fop_open(struct inode *inode, struct file *file)
  291. {
  292. struct o2net_sock_container *dummy_sc;
  293. struct seq_file *seq;
  294. int ret;
  295. dummy_sc = kmalloc(sizeof(struct o2net_sock_container), GFP_KERNEL);
  296. if (dummy_sc == NULL) {
  297. ret = -ENOMEM;
  298. goto out;
  299. }
  300. dummy_sc->sc_page = NULL;
  301. ret = seq_open(file, &sc_seq_ops);
  302. if (ret)
  303. goto out;
  304. seq = file->private_data;
  305. seq->private = dummy_sc;
  306. o2net_debug_add_sc(dummy_sc);
  307. dummy_sc = NULL;
  308. out:
  309. kfree(dummy_sc);
  310. return ret;
  311. }
  312. static int sc_fop_release(struct inode *inode, struct file *file)
  313. {
  314. struct seq_file *seq = file->private_data;
  315. struct o2net_sock_container *dummy_sc = seq->private;
  316. o2net_debug_del_sc(dummy_sc);
  317. return seq_release_private(inode, file);
  318. }
  319. static struct file_operations sc_seq_fops = {
  320. .open = sc_fop_open,
  321. .read = seq_read,
  322. .llseek = seq_lseek,
  323. .release = sc_fop_release,
  324. };
  325. int o2net_debugfs_init(void)
  326. {
  327. o2net_dentry = debugfs_create_dir(O2NET_DEBUG_DIR, NULL);
  328. if (!o2net_dentry) {
  329. mlog_errno(-ENOMEM);
  330. goto bail;
  331. }
  332. nst_dentry = debugfs_create_file(NST_DEBUG_NAME, S_IFREG|S_IRUSR,
  333. o2net_dentry, NULL,
  334. &nst_seq_fops);
  335. if (!nst_dentry) {
  336. mlog_errno(-ENOMEM);
  337. goto bail;
  338. }
  339. sc_dentry = debugfs_create_file(SC_DEBUG_NAME, S_IFREG|S_IRUSR,
  340. o2net_dentry, NULL,
  341. &sc_seq_fops);
  342. if (!sc_dentry) {
  343. mlog_errno(-ENOMEM);
  344. goto bail;
  345. }
  346. return 0;
  347. bail:
  348. if (sc_dentry)
  349. debugfs_remove(sc_dentry);
  350. if (nst_dentry)
  351. debugfs_remove(nst_dentry);
  352. if (o2net_dentry)
  353. debugfs_remove(o2net_dentry);
  354. return -ENOMEM;
  355. }
  356. void o2net_debugfs_exit(void)
  357. {
  358. if (sc_dentry)
  359. debugfs_remove(sc_dentry);
  360. if (nst_dentry)
  361. debugfs_remove(nst_dentry);
  362. if (o2net_dentry)
  363. debugfs_remove(o2net_dentry);
  364. }
  365. #endif /* CONFIG_DEBUG_FS */