netdebug.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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.%lu\n"
  117. " send start: %lu.%lu\n"
  118. " wait start: %lu.%lu\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, nst->st_sock_time.tv_usec,
  125. nst->st_send_time.tv_sec, nst->st_send_time.tv_usec,
  126. nst->st_status_time.tv_sec,
  127. nst->st_status_time.tv_usec);
  128. }
  129. spin_unlock(&o2net_debug_lock);
  130. return 0;
  131. }
  132. static void nst_seq_stop(struct seq_file *seq, void *v)
  133. {
  134. }
  135. static struct seq_operations nst_seq_ops = {
  136. .start = nst_seq_start,
  137. .next = nst_seq_next,
  138. .stop = nst_seq_stop,
  139. .show = nst_seq_show,
  140. };
  141. static int nst_fop_open(struct inode *inode, struct file *file)
  142. {
  143. struct o2net_send_tracking *dummy_nst;
  144. struct seq_file *seq;
  145. int ret;
  146. dummy_nst = kmalloc(sizeof(struct o2net_send_tracking), GFP_KERNEL);
  147. if (dummy_nst == NULL) {
  148. ret = -ENOMEM;
  149. goto out;
  150. }
  151. dummy_nst->st_task = NULL;
  152. ret = seq_open(file, &nst_seq_ops);
  153. if (ret)
  154. goto out;
  155. seq = file->private_data;
  156. seq->private = dummy_nst;
  157. o2net_debug_add_nst(dummy_nst);
  158. dummy_nst = NULL;
  159. out:
  160. kfree(dummy_nst);
  161. return ret;
  162. }
  163. static int nst_fop_release(struct inode *inode, struct file *file)
  164. {
  165. struct seq_file *seq = file->private_data;
  166. struct o2net_send_tracking *dummy_nst = seq->private;
  167. o2net_debug_del_nst(dummy_nst);
  168. return seq_release_private(inode, file);
  169. }
  170. static struct file_operations nst_seq_fops = {
  171. .open = nst_fop_open,
  172. .read = seq_read,
  173. .llseek = seq_lseek,
  174. .release = nst_fop_release,
  175. };
  176. void o2net_debug_add_sc(struct o2net_sock_container *sc)
  177. {
  178. spin_lock(&o2net_debug_lock);
  179. list_add(&sc->sc_net_debug_item, &sock_containers);
  180. spin_unlock(&o2net_debug_lock);
  181. }
  182. void o2net_debug_del_sc(struct o2net_sock_container *sc)
  183. {
  184. spin_lock(&o2net_debug_lock);
  185. list_del_init(&sc->sc_net_debug_item);
  186. spin_unlock(&o2net_debug_lock);
  187. }
  188. static struct o2net_sock_container
  189. *next_sc(struct o2net_sock_container *sc_start)
  190. {
  191. struct o2net_sock_container *sc, *ret = NULL;
  192. assert_spin_locked(&o2net_debug_lock);
  193. list_for_each_entry(sc, &sc_start->sc_net_debug_item,
  194. sc_net_debug_item) {
  195. /* discover the head of the list miscast as a sc */
  196. if (&sc->sc_net_debug_item == &sock_containers)
  197. break;
  198. /* use sc_page to detect real scs in the list */
  199. if (sc->sc_page != NULL) {
  200. ret = sc;
  201. break;
  202. }
  203. }
  204. return ret;
  205. }
  206. static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
  207. {
  208. struct o2net_sock_container *sc, *dummy_sc = seq->private;
  209. spin_lock(&o2net_debug_lock);
  210. sc = next_sc(dummy_sc);
  211. spin_unlock(&o2net_debug_lock);
  212. return sc;
  213. }
  214. static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  215. {
  216. struct o2net_sock_container *sc, *dummy_sc = seq->private;
  217. spin_lock(&o2net_debug_lock);
  218. sc = next_sc(dummy_sc);
  219. list_del_init(&dummy_sc->sc_net_debug_item);
  220. if (sc)
  221. list_add(&dummy_sc->sc_net_debug_item, &sc->sc_net_debug_item);
  222. spin_unlock(&o2net_debug_lock);
  223. return sc; /* unused, just needs to be null when done */
  224. }
  225. #define TV_SEC_USEC(TV) TV.tv_sec, TV.tv_usec
  226. static int sc_seq_show(struct seq_file *seq, void *v)
  227. {
  228. struct o2net_sock_container *sc, *dummy_sc = seq->private;
  229. spin_lock(&o2net_debug_lock);
  230. sc = next_sc(dummy_sc);
  231. if (sc != NULL) {
  232. struct inet_sock *inet = NULL;
  233. __be32 saddr = 0, daddr = 0;
  234. __be16 sport = 0, dport = 0;
  235. if (sc->sc_sock) {
  236. inet = inet_sk(sc->sc_sock->sk);
  237. /* the stack's structs aren't sparse endian clean */
  238. saddr = (__force __be32)inet->saddr;
  239. daddr = (__force __be32)inet->daddr;
  240. sport = (__force __be16)inet->sport;
  241. dport = (__force __be16)inet->dport;
  242. }
  243. /* XXX sigh, inet-> doesn't have sparse annotation so any
  244. * use of it here generates a warning with -Wbitwise */
  245. seq_printf(seq, "%p:\n"
  246. " krefs: %d\n"
  247. " sock: %u.%u.%u.%u:%u -> "
  248. "%u.%u.%u.%u:%u\n"
  249. " remote node: %s\n"
  250. " page off: %zu\n"
  251. " handshake ok: %u\n"
  252. " timer: %lu.%lu\n"
  253. " data ready: %lu.%lu\n"
  254. " advance start: %lu.%lu\n"
  255. " advance stop: %lu.%lu\n"
  256. " func start: %lu.%lu\n"
  257. " func stop: %lu.%lu\n"
  258. " func key: %u\n"
  259. " func type: %u\n",
  260. sc,
  261. atomic_read(&sc->sc_kref.refcount),
  262. NIPQUAD(saddr), inet ? ntohs(sport) : 0,
  263. NIPQUAD(daddr), inet ? ntohs(dport) : 0,
  264. sc->sc_node->nd_name,
  265. sc->sc_page_off,
  266. sc->sc_handshake_ok,
  267. TV_SEC_USEC(sc->sc_tv_timer),
  268. TV_SEC_USEC(sc->sc_tv_data_ready),
  269. TV_SEC_USEC(sc->sc_tv_advance_start),
  270. TV_SEC_USEC(sc->sc_tv_advance_stop),
  271. TV_SEC_USEC(sc->sc_tv_func_start),
  272. TV_SEC_USEC(sc->sc_tv_func_stop),
  273. sc->sc_msg_key,
  274. sc->sc_msg_type);
  275. }
  276. spin_unlock(&o2net_debug_lock);
  277. return 0;
  278. }
  279. static void sc_seq_stop(struct seq_file *seq, void *v)
  280. {
  281. }
  282. static struct seq_operations sc_seq_ops = {
  283. .start = sc_seq_start,
  284. .next = sc_seq_next,
  285. .stop = sc_seq_stop,
  286. .show = sc_seq_show,
  287. };
  288. static int sc_fop_open(struct inode *inode, struct file *file)
  289. {
  290. struct o2net_sock_container *dummy_sc;
  291. struct seq_file *seq;
  292. int ret;
  293. dummy_sc = kmalloc(sizeof(struct o2net_sock_container), GFP_KERNEL);
  294. if (dummy_sc == NULL) {
  295. ret = -ENOMEM;
  296. goto out;
  297. }
  298. dummy_sc->sc_page = NULL;
  299. ret = seq_open(file, &sc_seq_ops);
  300. if (ret)
  301. goto out;
  302. seq = file->private_data;
  303. seq->private = dummy_sc;
  304. o2net_debug_add_sc(dummy_sc);
  305. dummy_sc = NULL;
  306. out:
  307. kfree(dummy_sc);
  308. return ret;
  309. }
  310. static int sc_fop_release(struct inode *inode, struct file *file)
  311. {
  312. struct seq_file *seq = file->private_data;
  313. struct o2net_sock_container *dummy_sc = seq->private;
  314. o2net_debug_del_sc(dummy_sc);
  315. return seq_release_private(inode, file);
  316. }
  317. static struct file_operations sc_seq_fops = {
  318. .open = sc_fop_open,
  319. .read = seq_read,
  320. .llseek = seq_lseek,
  321. .release = sc_fop_release,
  322. };
  323. int o2net_debugfs_init(void)
  324. {
  325. o2net_dentry = debugfs_create_dir(O2NET_DEBUG_DIR, NULL);
  326. if (!o2net_dentry) {
  327. mlog_errno(-ENOMEM);
  328. goto bail;
  329. }
  330. nst_dentry = debugfs_create_file(NST_DEBUG_NAME, S_IFREG|S_IRUSR,
  331. o2net_dentry, NULL,
  332. &nst_seq_fops);
  333. if (!nst_dentry) {
  334. mlog_errno(-ENOMEM);
  335. goto bail;
  336. }
  337. sc_dentry = debugfs_create_file(SC_DEBUG_NAME, S_IFREG|S_IRUSR,
  338. o2net_dentry, NULL,
  339. &sc_seq_fops);
  340. if (!sc_dentry) {
  341. mlog_errno(-ENOMEM);
  342. goto bail;
  343. }
  344. return 0;
  345. bail:
  346. if (sc_dentry)
  347. debugfs_remove(sc_dentry);
  348. if (nst_dentry)
  349. debugfs_remove(nst_dentry);
  350. if (o2net_dentry)
  351. debugfs_remove(o2net_dentry);
  352. return -ENOMEM;
  353. }
  354. void o2net_debugfs_exit(void)
  355. {
  356. if (sc_dentry)
  357. debugfs_remove(sc_dentry);
  358. if (nst_dentry)
  359. debugfs_remove(nst_dentry);
  360. if (o2net_dentry)
  361. debugfs_remove(o2net_dentry);
  362. }
  363. #endif /* CONFIG_DEBUG_FS */