netdebug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * netdebug.c
  4. *
  5. * debug functionality for o2net
  6. *
  7. * Copyright (C) 2005, 2008 Oracle. All rights reserved.
  8. */
  9. #ifdef CONFIG_DEBUG_FS
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/slab.h>
  13. #include <linux/idr.h>
  14. #include <linux/kref.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/uaccess.h>
  18. #include "tcp.h"
  19. #include "nodemanager.h"
  20. #define MLOG_MASK_PREFIX ML_TCP
  21. #include "masklog.h"
  22. #include "tcp_internal.h"
  23. #define O2NET_DEBUG_DIR "o2net"
  24. #define SC_DEBUG_NAME "sock_containers"
  25. #define NST_DEBUG_NAME "send_tracking"
  26. #define STATS_DEBUG_NAME "stats"
  27. #define NODES_DEBUG_NAME "connected_nodes"
  28. #define SHOW_SOCK_CONTAINERS 0
  29. #define SHOW_SOCK_STATS 1
  30. static struct dentry *o2net_dentry;
  31. static DEFINE_SPINLOCK(o2net_debug_lock);
  32. static LIST_HEAD(sock_containers);
  33. static LIST_HEAD(send_tracking);
  34. void o2net_debug_add_nst(struct o2net_send_tracking *nst)
  35. {
  36. spin_lock_bh(&o2net_debug_lock);
  37. list_add(&nst->st_net_debug_item, &send_tracking);
  38. spin_unlock_bh(&o2net_debug_lock);
  39. }
  40. void o2net_debug_del_nst(struct o2net_send_tracking *nst)
  41. {
  42. spin_lock_bh(&o2net_debug_lock);
  43. if (!list_empty(&nst->st_net_debug_item))
  44. list_del_init(&nst->st_net_debug_item);
  45. spin_unlock_bh(&o2net_debug_lock);
  46. }
  47. static struct o2net_send_tracking
  48. *next_nst(struct o2net_send_tracking *nst_start)
  49. {
  50. struct o2net_send_tracking *nst, *ret = NULL;
  51. assert_spin_locked(&o2net_debug_lock);
  52. list_for_each_entry(nst, &nst_start->st_net_debug_item,
  53. st_net_debug_item) {
  54. /* discover the head of the list */
  55. if (&nst->st_net_debug_item == &send_tracking)
  56. break;
  57. /* use st_task to detect real nsts in the list */
  58. if (nst->st_task != NULL) {
  59. ret = nst;
  60. break;
  61. }
  62. }
  63. return ret;
  64. }
  65. static void *nst_seq_start(struct seq_file *seq, loff_t *pos)
  66. {
  67. struct o2net_send_tracking *nst, *dummy_nst = seq->private;
  68. spin_lock_bh(&o2net_debug_lock);
  69. nst = next_nst(dummy_nst);
  70. spin_unlock_bh(&o2net_debug_lock);
  71. return nst;
  72. }
  73. static void *nst_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  74. {
  75. struct o2net_send_tracking *nst, *dummy_nst = seq->private;
  76. spin_lock_bh(&o2net_debug_lock);
  77. nst = next_nst(dummy_nst);
  78. list_del_init(&dummy_nst->st_net_debug_item);
  79. if (nst)
  80. list_add(&dummy_nst->st_net_debug_item,
  81. &nst->st_net_debug_item);
  82. spin_unlock_bh(&o2net_debug_lock);
  83. return nst; /* unused, just needs to be null when done */
  84. }
  85. static int nst_seq_show(struct seq_file *seq, void *v)
  86. {
  87. struct o2net_send_tracking *nst, *dummy_nst = seq->private;
  88. ktime_t now;
  89. s64 sock, send, status;
  90. spin_lock_bh(&o2net_debug_lock);
  91. nst = next_nst(dummy_nst);
  92. if (!nst)
  93. goto out;
  94. now = ktime_get();
  95. sock = ktime_to_us(ktime_sub(now, nst->st_sock_time));
  96. send = ktime_to_us(ktime_sub(now, nst->st_send_time));
  97. status = ktime_to_us(ktime_sub(now, nst->st_status_time));
  98. /* get_task_comm isn't exported. oh well. */
  99. seq_printf(seq, "%p:\n"
  100. " pid: %lu\n"
  101. " tgid: %lu\n"
  102. " process name: %s\n"
  103. " node: %u\n"
  104. " sc: %p\n"
  105. " message id: %d\n"
  106. " message type: %u\n"
  107. " message key: 0x%08x\n"
  108. " sock acquiry: %lld usecs ago\n"
  109. " send start: %lld usecs ago\n"
  110. " wait start: %lld usecs ago\n",
  111. nst, (unsigned long)task_pid_nr(nst->st_task),
  112. (unsigned long)nst->st_task->tgid,
  113. nst->st_task->comm, nst->st_node,
  114. nst->st_sc, nst->st_id, nst->st_msg_type,
  115. nst->st_msg_key,
  116. (long long)sock,
  117. (long long)send,
  118. (long long)status);
  119. out:
  120. spin_unlock_bh(&o2net_debug_lock);
  121. return 0;
  122. }
  123. static void nst_seq_stop(struct seq_file *seq, void *v)
  124. {
  125. }
  126. static const struct seq_operations nst_seq_ops = {
  127. .start = nst_seq_start,
  128. .next = nst_seq_next,
  129. .stop = nst_seq_stop,
  130. .show = nst_seq_show,
  131. };
  132. static int nst_fop_open(struct inode *inode, struct file *file)
  133. {
  134. struct o2net_send_tracking *dummy_nst;
  135. dummy_nst = __seq_open_private(file, &nst_seq_ops, sizeof(*dummy_nst));
  136. if (!dummy_nst)
  137. return -ENOMEM;
  138. o2net_debug_add_nst(dummy_nst);
  139. return 0;
  140. }
  141. static int nst_fop_release(struct inode *inode, struct file *file)
  142. {
  143. struct seq_file *seq = file->private_data;
  144. struct o2net_send_tracking *dummy_nst = seq->private;
  145. o2net_debug_del_nst(dummy_nst);
  146. return seq_release_private(inode, file);
  147. }
  148. static const struct file_operations nst_seq_fops = {
  149. .open = nst_fop_open,
  150. .read = seq_read,
  151. .llseek = seq_lseek,
  152. .release = nst_fop_release,
  153. };
  154. void o2net_debug_add_sc(struct o2net_sock_container *sc)
  155. {
  156. spin_lock_bh(&o2net_debug_lock);
  157. list_add(&sc->sc_net_debug_item, &sock_containers);
  158. spin_unlock_bh(&o2net_debug_lock);
  159. }
  160. void o2net_debug_del_sc(struct o2net_sock_container *sc)
  161. {
  162. spin_lock_bh(&o2net_debug_lock);
  163. list_del_init(&sc->sc_net_debug_item);
  164. spin_unlock_bh(&o2net_debug_lock);
  165. }
  166. struct o2net_sock_debug {
  167. int dbg_ctxt;
  168. struct o2net_sock_container *dbg_sock;
  169. };
  170. static struct o2net_sock_container
  171. *next_sc(struct o2net_sock_container *sc_start)
  172. {
  173. struct o2net_sock_container *sc, *ret = NULL;
  174. assert_spin_locked(&o2net_debug_lock);
  175. list_for_each_entry(sc, &sc_start->sc_net_debug_item,
  176. sc_net_debug_item) {
  177. /* discover the head of the list miscast as a sc */
  178. if (&sc->sc_net_debug_item == &sock_containers)
  179. break;
  180. /* use sc_page to detect real scs in the list */
  181. if (sc->sc_page != NULL) {
  182. ret = sc;
  183. break;
  184. }
  185. }
  186. return ret;
  187. }
  188. static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
  189. {
  190. struct o2net_sock_debug *sd = seq->private;
  191. struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
  192. spin_lock_bh(&o2net_debug_lock);
  193. sc = next_sc(dummy_sc);
  194. spin_unlock_bh(&o2net_debug_lock);
  195. return sc;
  196. }
  197. static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  198. {
  199. struct o2net_sock_debug *sd = seq->private;
  200. struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
  201. spin_lock_bh(&o2net_debug_lock);
  202. sc = next_sc(dummy_sc);
  203. list_del_init(&dummy_sc->sc_net_debug_item);
  204. if (sc)
  205. list_add(&dummy_sc->sc_net_debug_item, &sc->sc_net_debug_item);
  206. spin_unlock_bh(&o2net_debug_lock);
  207. return sc; /* unused, just needs to be null when done */
  208. }
  209. #ifdef CONFIG_OCFS2_FS_STATS
  210. # define sc_send_count(_s) ((_s)->sc_send_count)
  211. # define sc_recv_count(_s) ((_s)->sc_recv_count)
  212. # define sc_tv_acquiry_total_ns(_s) (ktime_to_ns((_s)->sc_tv_acquiry_total))
  213. # define sc_tv_send_total_ns(_s) (ktime_to_ns((_s)->sc_tv_send_total))
  214. # define sc_tv_status_total_ns(_s) (ktime_to_ns((_s)->sc_tv_status_total))
  215. # define sc_tv_process_total_ns(_s) (ktime_to_ns((_s)->sc_tv_process_total))
  216. #else
  217. # define sc_send_count(_s) (0U)
  218. # define sc_recv_count(_s) (0U)
  219. # define sc_tv_acquiry_total_ns(_s) (0LL)
  220. # define sc_tv_send_total_ns(_s) (0LL)
  221. # define sc_tv_status_total_ns(_s) (0LL)
  222. # define sc_tv_process_total_ns(_s) (0LL)
  223. #endif
  224. /* So that debugfs.ocfs2 can determine which format is being used */
  225. #define O2NET_STATS_STR_VERSION 1
  226. static void sc_show_sock_stats(struct seq_file *seq,
  227. struct o2net_sock_container *sc)
  228. {
  229. if (!sc)
  230. return;
  231. seq_printf(seq, "%d,%u,%lu,%lld,%lld,%lld,%lu,%lld\n", O2NET_STATS_STR_VERSION,
  232. sc->sc_node->nd_num, (unsigned long)sc_send_count(sc),
  233. (long long)sc_tv_acquiry_total_ns(sc),
  234. (long long)sc_tv_send_total_ns(sc),
  235. (long long)sc_tv_status_total_ns(sc),
  236. (unsigned long)sc_recv_count(sc),
  237. (long long)sc_tv_process_total_ns(sc));
  238. }
  239. static void sc_show_sock_container(struct seq_file *seq,
  240. struct o2net_sock_container *sc)
  241. {
  242. struct inet_sock *inet = NULL;
  243. __be32 saddr = 0, daddr = 0;
  244. __be16 sport = 0, dport = 0;
  245. if (!sc)
  246. return;
  247. if (sc->sc_sock) {
  248. inet = inet_sk(sc->sc_sock->sk);
  249. /* the stack's structs aren't sparse endian clean */
  250. saddr = (__force __be32)inet->inet_saddr;
  251. daddr = (__force __be32)inet->inet_daddr;
  252. sport = (__force __be16)inet->inet_sport;
  253. dport = (__force __be16)inet->inet_dport;
  254. }
  255. /* XXX sigh, inet-> doesn't have sparse annotation so any
  256. * use of it here generates a warning with -Wbitwise */
  257. seq_printf(seq, "%p:\n"
  258. " krefs: %d\n"
  259. " sock: %pI4:%u -> "
  260. "%pI4:%u\n"
  261. " remote node: %s\n"
  262. " page off: %zu\n"
  263. " handshake ok: %u\n"
  264. " timer: %lld usecs\n"
  265. " data ready: %lld usecs\n"
  266. " advance start: %lld usecs\n"
  267. " advance stop: %lld usecs\n"
  268. " func start: %lld usecs\n"
  269. " func stop: %lld usecs\n"
  270. " func key: 0x%08x\n"
  271. " func type: %u\n",
  272. sc,
  273. kref_read(&sc->sc_kref),
  274. &saddr, inet ? ntohs(sport) : 0,
  275. &daddr, inet ? ntohs(dport) : 0,
  276. sc->sc_node->nd_name,
  277. sc->sc_page_off,
  278. sc->sc_handshake_ok,
  279. (long long)ktime_to_us(sc->sc_tv_timer),
  280. (long long)ktime_to_us(sc->sc_tv_data_ready),
  281. (long long)ktime_to_us(sc->sc_tv_advance_start),
  282. (long long)ktime_to_us(sc->sc_tv_advance_stop),
  283. (long long)ktime_to_us(sc->sc_tv_func_start),
  284. (long long)ktime_to_us(sc->sc_tv_func_stop),
  285. sc->sc_msg_key,
  286. sc->sc_msg_type);
  287. }
  288. static int sc_seq_show(struct seq_file *seq, void *v)
  289. {
  290. struct o2net_sock_debug *sd = seq->private;
  291. struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
  292. spin_lock_bh(&o2net_debug_lock);
  293. sc = next_sc(dummy_sc);
  294. if (sc) {
  295. if (sd->dbg_ctxt == SHOW_SOCK_CONTAINERS)
  296. sc_show_sock_container(seq, sc);
  297. else
  298. sc_show_sock_stats(seq, sc);
  299. }
  300. spin_unlock_bh(&o2net_debug_lock);
  301. return 0;
  302. }
  303. static void sc_seq_stop(struct seq_file *seq, void *v)
  304. {
  305. }
  306. static const struct seq_operations sc_seq_ops = {
  307. .start = sc_seq_start,
  308. .next = sc_seq_next,
  309. .stop = sc_seq_stop,
  310. .show = sc_seq_show,
  311. };
  312. static int sc_common_open(struct file *file, int ctxt)
  313. {
  314. struct o2net_sock_debug *sd;
  315. struct o2net_sock_container *dummy_sc;
  316. dummy_sc = kzalloc(sizeof(*dummy_sc), GFP_KERNEL);
  317. if (!dummy_sc)
  318. return -ENOMEM;
  319. sd = __seq_open_private(file, &sc_seq_ops, sizeof(*sd));
  320. if (!sd) {
  321. kfree(dummy_sc);
  322. return -ENOMEM;
  323. }
  324. sd->dbg_ctxt = ctxt;
  325. sd->dbg_sock = dummy_sc;
  326. o2net_debug_add_sc(dummy_sc);
  327. return 0;
  328. }
  329. static int sc_fop_release(struct inode *inode, struct file *file)
  330. {
  331. struct seq_file *seq = file->private_data;
  332. struct o2net_sock_debug *sd = seq->private;
  333. struct o2net_sock_container *dummy_sc = sd->dbg_sock;
  334. o2net_debug_del_sc(dummy_sc);
  335. kfree(dummy_sc);
  336. return seq_release_private(inode, file);
  337. }
  338. static int stats_fop_open(struct inode *inode, struct file *file)
  339. {
  340. return sc_common_open(file, SHOW_SOCK_STATS);
  341. }
  342. static const struct file_operations stats_seq_fops = {
  343. .open = stats_fop_open,
  344. .read = seq_read,
  345. .llseek = seq_lseek,
  346. .release = sc_fop_release,
  347. };
  348. static int sc_fop_open(struct inode *inode, struct file *file)
  349. {
  350. return sc_common_open(file, SHOW_SOCK_CONTAINERS);
  351. }
  352. static const struct file_operations sc_seq_fops = {
  353. .open = sc_fop_open,
  354. .read = seq_read,
  355. .llseek = seq_lseek,
  356. .release = sc_fop_release,
  357. };
  358. static int o2net_fill_bitmap(char *buf, int len)
  359. {
  360. unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  361. int i = -1, out = 0;
  362. o2net_fill_node_map(map, O2NM_MAX_NODES);
  363. while ((i = find_next_bit(map, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES)
  364. out += scnprintf(buf + out, PAGE_SIZE - out, "%d ", i);
  365. out += scnprintf(buf + out, PAGE_SIZE - out, "\n");
  366. return out;
  367. }
  368. static int nodes_fop_open(struct inode *inode, struct file *file)
  369. {
  370. char *buf;
  371. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  372. if (!buf)
  373. return -ENOMEM;
  374. i_size_write(inode, o2net_fill_bitmap(buf, PAGE_SIZE));
  375. file->private_data = buf;
  376. return 0;
  377. }
  378. static int o2net_debug_release(struct inode *inode, struct file *file)
  379. {
  380. kfree(file->private_data);
  381. return 0;
  382. }
  383. static ssize_t o2net_debug_read(struct file *file, char __user *buf,
  384. size_t nbytes, loff_t *ppos)
  385. {
  386. return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
  387. i_size_read(file->f_mapping->host));
  388. }
  389. static const struct file_operations nodes_fops = {
  390. .open = nodes_fop_open,
  391. .release = o2net_debug_release,
  392. .read = o2net_debug_read,
  393. .llseek = generic_file_llseek,
  394. };
  395. void o2net_debugfs_exit(void)
  396. {
  397. debugfs_remove_recursive(o2net_dentry);
  398. }
  399. void o2net_debugfs_init(void)
  400. {
  401. umode_t mode = S_IFREG|S_IRUSR;
  402. o2net_dentry = debugfs_create_dir(O2NET_DEBUG_DIR, NULL);
  403. debugfs_create_file(NST_DEBUG_NAME, mode, o2net_dentry, NULL,
  404. &nst_seq_fops);
  405. debugfs_create_file(SC_DEBUG_NAME, mode, o2net_dentry, NULL,
  406. &sc_seq_fops);
  407. debugfs_create_file(STATS_DEBUG_NAME, mode, o2net_dentry, NULL,
  408. &stats_seq_fops);
  409. debugfs_create_file(NODES_DEBUG_NAME, mode, o2net_dentry, NULL,
  410. &nodes_fops);
  411. }
  412. #endif /* CONFIG_DEBUG_FS */