xref: /openbmc/linux/fs/ocfs2/cluster/netdebug.c (revision 565d76cb)
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 
27 #ifdef CONFIG_DEBUG_FS
28 
29 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
32 #include <linux/idr.h>
33 #include <linux/kref.h>
34 #include <linux/seq_file.h>
35 #include <linux/debugfs.h>
36 
37 #include <linux/uaccess.h>
38 
39 #include "tcp.h"
40 #include "nodemanager.h"
41 #define MLOG_MASK_PREFIX ML_TCP
42 #include "masklog.h"
43 
44 #include "tcp_internal.h"
45 
46 #define O2NET_DEBUG_DIR		"o2net"
47 #define SC_DEBUG_NAME		"sock_containers"
48 #define NST_DEBUG_NAME		"send_tracking"
49 #define STATS_DEBUG_NAME	"stats"
50 
51 #define SHOW_SOCK_CONTAINERS	0
52 #define SHOW_SOCK_STATS		1
53 
54 static struct dentry *o2net_dentry;
55 static struct dentry *sc_dentry;
56 static struct dentry *nst_dentry;
57 static struct dentry *stats_dentry;
58 
59 static DEFINE_SPINLOCK(o2net_debug_lock);
60 
61 static LIST_HEAD(sock_containers);
62 static LIST_HEAD(send_tracking);
63 
64 void o2net_debug_add_nst(struct o2net_send_tracking *nst)
65 {
66 	spin_lock(&o2net_debug_lock);
67 	list_add(&nst->st_net_debug_item, &send_tracking);
68 	spin_unlock(&o2net_debug_lock);
69 }
70 
71 void o2net_debug_del_nst(struct o2net_send_tracking *nst)
72 {
73 	spin_lock(&o2net_debug_lock);
74 	if (!list_empty(&nst->st_net_debug_item))
75 		list_del_init(&nst->st_net_debug_item);
76 	spin_unlock(&o2net_debug_lock);
77 }
78 
79 static struct o2net_send_tracking
80 			*next_nst(struct o2net_send_tracking *nst_start)
81 {
82 	struct o2net_send_tracking *nst, *ret = NULL;
83 
84 	assert_spin_locked(&o2net_debug_lock);
85 
86 	list_for_each_entry(nst, &nst_start->st_net_debug_item,
87 			    st_net_debug_item) {
88 		/* discover the head of the list */
89 		if (&nst->st_net_debug_item == &send_tracking)
90 			break;
91 
92 		/* use st_task to detect real nsts in the list */
93 		if (nst->st_task != NULL) {
94 			ret = nst;
95 			break;
96 		}
97 	}
98 
99 	return ret;
100 }
101 
102 static void *nst_seq_start(struct seq_file *seq, loff_t *pos)
103 {
104 	struct o2net_send_tracking *nst, *dummy_nst = seq->private;
105 
106 	spin_lock(&o2net_debug_lock);
107 	nst = next_nst(dummy_nst);
108 	spin_unlock(&o2net_debug_lock);
109 
110 	return nst;
111 }
112 
113 static void *nst_seq_next(struct seq_file *seq, void *v, loff_t *pos)
114 {
115 	struct o2net_send_tracking *nst, *dummy_nst = seq->private;
116 
117 	spin_lock(&o2net_debug_lock);
118 	nst = next_nst(dummy_nst);
119 	list_del_init(&dummy_nst->st_net_debug_item);
120 	if (nst)
121 		list_add(&dummy_nst->st_net_debug_item,
122 			 &nst->st_net_debug_item);
123 	spin_unlock(&o2net_debug_lock);
124 
125 	return nst; /* unused, just needs to be null when done */
126 }
127 
128 static int nst_seq_show(struct seq_file *seq, void *v)
129 {
130 	struct o2net_send_tracking *nst, *dummy_nst = seq->private;
131 	ktime_t now;
132 	s64 sock, send, status;
133 
134 	spin_lock(&o2net_debug_lock);
135 	nst = next_nst(dummy_nst);
136 	if (!nst)
137 		goto out;
138 
139 	now = ktime_get();
140 	sock = ktime_to_us(ktime_sub(now, nst->st_sock_time));
141 	send = ktime_to_us(ktime_sub(now, nst->st_send_time));
142 	status = ktime_to_us(ktime_sub(now, nst->st_status_time));
143 
144 	/* get_task_comm isn't exported.  oh well. */
145 	seq_printf(seq, "%p:\n"
146 		   "  pid:          %lu\n"
147 		   "  tgid:         %lu\n"
148 		   "  process name: %s\n"
149 		   "  node:         %u\n"
150 		   "  sc:           %p\n"
151 		   "  message id:   %d\n"
152 		   "  message type: %u\n"
153 		   "  message key:  0x%08x\n"
154 		   "  sock acquiry: %lld usecs ago\n"
155 		   "  send start:   %lld usecs ago\n"
156 		   "  wait start:   %lld usecs ago\n",
157 		   nst, (unsigned long)task_pid_nr(nst->st_task),
158 		   (unsigned long)nst->st_task->tgid,
159 		   nst->st_task->comm, nst->st_node,
160 		   nst->st_sc, nst->st_id, nst->st_msg_type,
161 		   nst->st_msg_key,
162 		   (long long)sock,
163 		   (long long)send,
164 		   (long long)status);
165 
166 out:
167 	spin_unlock(&o2net_debug_lock);
168 
169 	return 0;
170 }
171 
172 static void nst_seq_stop(struct seq_file *seq, void *v)
173 {
174 }
175 
176 static const struct seq_operations nst_seq_ops = {
177 	.start = nst_seq_start,
178 	.next = nst_seq_next,
179 	.stop = nst_seq_stop,
180 	.show = nst_seq_show,
181 };
182 
183 static int nst_fop_open(struct inode *inode, struct file *file)
184 {
185 	struct o2net_send_tracking *dummy_nst;
186 	struct seq_file *seq;
187 	int ret;
188 
189 	dummy_nst = kmalloc(sizeof(struct o2net_send_tracking), GFP_KERNEL);
190 	if (dummy_nst == NULL) {
191 		ret = -ENOMEM;
192 		goto out;
193 	}
194 	dummy_nst->st_task = NULL;
195 
196 	ret = seq_open(file, &nst_seq_ops);
197 	if (ret)
198 		goto out;
199 
200 	seq = file->private_data;
201 	seq->private = dummy_nst;
202 	o2net_debug_add_nst(dummy_nst);
203 
204 	dummy_nst = NULL;
205 
206 out:
207 	kfree(dummy_nst);
208 	return ret;
209 }
210 
211 static int nst_fop_release(struct inode *inode, struct file *file)
212 {
213 	struct seq_file *seq = file->private_data;
214 	struct o2net_send_tracking *dummy_nst = seq->private;
215 
216 	o2net_debug_del_nst(dummy_nst);
217 	return seq_release_private(inode, file);
218 }
219 
220 static const struct file_operations nst_seq_fops = {
221 	.open = nst_fop_open,
222 	.read = seq_read,
223 	.llseek = seq_lseek,
224 	.release = nst_fop_release,
225 };
226 
227 void o2net_debug_add_sc(struct o2net_sock_container *sc)
228 {
229 	spin_lock(&o2net_debug_lock);
230 	list_add(&sc->sc_net_debug_item, &sock_containers);
231 	spin_unlock(&o2net_debug_lock);
232 }
233 
234 void o2net_debug_del_sc(struct o2net_sock_container *sc)
235 {
236 	spin_lock(&o2net_debug_lock);
237 	list_del_init(&sc->sc_net_debug_item);
238 	spin_unlock(&o2net_debug_lock);
239 }
240 
241 struct o2net_sock_debug {
242 	int dbg_ctxt;
243 	struct o2net_sock_container *dbg_sock;
244 };
245 
246 static struct o2net_sock_container
247 			*next_sc(struct o2net_sock_container *sc_start)
248 {
249 	struct o2net_sock_container *sc, *ret = NULL;
250 
251 	assert_spin_locked(&o2net_debug_lock);
252 
253 	list_for_each_entry(sc, &sc_start->sc_net_debug_item,
254 			    sc_net_debug_item) {
255 		/* discover the head of the list miscast as a sc */
256 		if (&sc->sc_net_debug_item == &sock_containers)
257 			break;
258 
259 		/* use sc_page to detect real scs in the list */
260 		if (sc->sc_page != NULL) {
261 			ret = sc;
262 			break;
263 		}
264 	}
265 
266 	return ret;
267 }
268 
269 static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
270 {
271 	struct o2net_sock_debug *sd = seq->private;
272 	struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
273 
274 	spin_lock(&o2net_debug_lock);
275 	sc = next_sc(dummy_sc);
276 	spin_unlock(&o2net_debug_lock);
277 
278 	return sc;
279 }
280 
281 static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
282 {
283 	struct o2net_sock_debug *sd = seq->private;
284 	struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
285 
286 	spin_lock(&o2net_debug_lock);
287 	sc = next_sc(dummy_sc);
288 	list_del_init(&dummy_sc->sc_net_debug_item);
289 	if (sc)
290 		list_add(&dummy_sc->sc_net_debug_item, &sc->sc_net_debug_item);
291 	spin_unlock(&o2net_debug_lock);
292 
293 	return sc; /* unused, just needs to be null when done */
294 }
295 
296 #ifdef CONFIG_OCFS2_FS_STATS
297 # define sc_send_count(_s)		((_s)->sc_send_count)
298 # define sc_recv_count(_s)		((_s)->sc_recv_count)
299 # define sc_tv_acquiry_total_ns(_s)	(ktime_to_ns((_s)->sc_tv_acquiry_total))
300 # define sc_tv_send_total_ns(_s)	(ktime_to_ns((_s)->sc_tv_send_total))
301 # define sc_tv_status_total_ns(_s)	(ktime_to_ns((_s)->sc_tv_status_total))
302 # define sc_tv_process_total_ns(_s)	(ktime_to_ns((_s)->sc_tv_process_total))
303 #else
304 # define sc_send_count(_s)		(0U)
305 # define sc_recv_count(_s)		(0U)
306 # define sc_tv_acquiry_total_ns(_s)	(0LL)
307 # define sc_tv_send_total_ns(_s)	(0LL)
308 # define sc_tv_status_total_ns(_s)	(0LL)
309 # define sc_tv_process_total_ns(_s)	(0LL)
310 #endif
311 
312 /* So that debugfs.ocfs2 can determine which format is being used */
313 #define O2NET_STATS_STR_VERSION		1
314 static void sc_show_sock_stats(struct seq_file *seq,
315 			       struct o2net_sock_container *sc)
316 {
317 	if (!sc)
318 		return;
319 
320 	seq_printf(seq, "%d,%u,%lu,%lld,%lld,%lld,%lu,%lld\n", O2NET_STATS_STR_VERSION,
321 		   sc->sc_node->nd_num, (unsigned long)sc_send_count(sc),
322 		   (long long)sc_tv_acquiry_total_ns(sc),
323 		   (long long)sc_tv_send_total_ns(sc),
324 		   (long long)sc_tv_status_total_ns(sc),
325 		   (unsigned long)sc_recv_count(sc),
326 		   (long long)sc_tv_process_total_ns(sc));
327 }
328 
329 static void sc_show_sock_container(struct seq_file *seq,
330 				   struct o2net_sock_container *sc)
331 {
332 	struct inet_sock *inet = NULL;
333 	__be32 saddr = 0, daddr = 0;
334 	__be16 sport = 0, dport = 0;
335 
336 	if (!sc)
337 		return;
338 
339 	if (sc->sc_sock) {
340 		inet = inet_sk(sc->sc_sock->sk);
341 		/* the stack's structs aren't sparse endian clean */
342 		saddr = (__force __be32)inet->inet_saddr;
343 		daddr = (__force __be32)inet->inet_daddr;
344 		sport = (__force __be16)inet->inet_sport;
345 		dport = (__force __be16)inet->inet_dport;
346 	}
347 
348 	/* XXX sigh, inet-> doesn't have sparse annotation so any
349 	 * use of it here generates a warning with -Wbitwise */
350 	seq_printf(seq, "%p:\n"
351 		   "  krefs:           %d\n"
352 		   "  sock:            %pI4:%u -> "
353 				      "%pI4:%u\n"
354 		   "  remote node:     %s\n"
355 		   "  page off:        %zu\n"
356 		   "  handshake ok:    %u\n"
357 		   "  timer:           %lld usecs\n"
358 		   "  data ready:      %lld usecs\n"
359 		   "  advance start:   %lld usecs\n"
360 		   "  advance stop:    %lld usecs\n"
361 		   "  func start:      %lld usecs\n"
362 		   "  func stop:       %lld usecs\n"
363 		   "  func key:        0x%08x\n"
364 		   "  func type:       %u\n",
365 		   sc,
366 		   atomic_read(&sc->sc_kref.refcount),
367 		   &saddr, inet ? ntohs(sport) : 0,
368 		   &daddr, inet ? ntohs(dport) : 0,
369 		   sc->sc_node->nd_name,
370 		   sc->sc_page_off,
371 		   sc->sc_handshake_ok,
372 		   (long long)ktime_to_us(sc->sc_tv_timer),
373 		   (long long)ktime_to_us(sc->sc_tv_data_ready),
374 		   (long long)ktime_to_us(sc->sc_tv_advance_start),
375 		   (long long)ktime_to_us(sc->sc_tv_advance_stop),
376 		   (long long)ktime_to_us(sc->sc_tv_func_start),
377 		   (long long)ktime_to_us(sc->sc_tv_func_stop),
378 		   sc->sc_msg_key,
379 		   sc->sc_msg_type);
380 }
381 
382 static int sc_seq_show(struct seq_file *seq, void *v)
383 {
384 	struct o2net_sock_debug *sd = seq->private;
385 	struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
386 
387 	spin_lock(&o2net_debug_lock);
388 	sc = next_sc(dummy_sc);
389 
390 	if (sc) {
391 		if (sd->dbg_ctxt == SHOW_SOCK_CONTAINERS)
392 			sc_show_sock_container(seq, sc);
393 		else
394 			sc_show_sock_stats(seq, sc);
395 	}
396 
397 	spin_unlock(&o2net_debug_lock);
398 
399 	return 0;
400 }
401 
402 static void sc_seq_stop(struct seq_file *seq, void *v)
403 {
404 }
405 
406 static const struct seq_operations sc_seq_ops = {
407 	.start = sc_seq_start,
408 	.next = sc_seq_next,
409 	.stop = sc_seq_stop,
410 	.show = sc_seq_show,
411 };
412 
413 static int sc_common_open(struct file *file, struct o2net_sock_debug *sd)
414 {
415 	struct o2net_sock_container *dummy_sc;
416 	struct seq_file *seq;
417 	int ret;
418 
419 	dummy_sc = kmalloc(sizeof(struct o2net_sock_container), GFP_KERNEL);
420 	if (dummy_sc == NULL) {
421 		ret = -ENOMEM;
422 		goto out;
423 	}
424 	dummy_sc->sc_page = NULL;
425 
426 	ret = seq_open(file, &sc_seq_ops);
427 	if (ret)
428 		goto out;
429 
430 	seq = file->private_data;
431 	seq->private = sd;
432 	sd->dbg_sock = dummy_sc;
433 	o2net_debug_add_sc(dummy_sc);
434 
435 	dummy_sc = NULL;
436 
437 out:
438 	kfree(dummy_sc);
439 	return ret;
440 }
441 
442 static int sc_fop_release(struct inode *inode, struct file *file)
443 {
444 	struct seq_file *seq = file->private_data;
445 	struct o2net_sock_debug *sd = seq->private;
446 	struct o2net_sock_container *dummy_sc = sd->dbg_sock;
447 
448 	o2net_debug_del_sc(dummy_sc);
449 	return seq_release_private(inode, file);
450 }
451 
452 static int stats_fop_open(struct inode *inode, struct file *file)
453 {
454 	struct o2net_sock_debug *sd;
455 
456 	sd = kmalloc(sizeof(struct o2net_sock_debug), GFP_KERNEL);
457 	if (sd == NULL)
458 		return -ENOMEM;
459 
460 	sd->dbg_ctxt = SHOW_SOCK_STATS;
461 	sd->dbg_sock = NULL;
462 
463 	return sc_common_open(file, sd);
464 }
465 
466 static const struct file_operations stats_seq_fops = {
467 	.open = stats_fop_open,
468 	.read = seq_read,
469 	.llseek = seq_lseek,
470 	.release = sc_fop_release,
471 };
472 
473 static int sc_fop_open(struct inode *inode, struct file *file)
474 {
475 	struct o2net_sock_debug *sd;
476 
477 	sd = kmalloc(sizeof(struct o2net_sock_debug), GFP_KERNEL);
478 	if (sd == NULL)
479 		return -ENOMEM;
480 
481 	sd->dbg_ctxt = SHOW_SOCK_CONTAINERS;
482 	sd->dbg_sock = NULL;
483 
484 	return sc_common_open(file, sd);
485 }
486 
487 static const struct file_operations sc_seq_fops = {
488 	.open = sc_fop_open,
489 	.read = seq_read,
490 	.llseek = seq_lseek,
491 	.release = sc_fop_release,
492 };
493 
494 int o2net_debugfs_init(void)
495 {
496 	o2net_dentry = debugfs_create_dir(O2NET_DEBUG_DIR, NULL);
497 	if (!o2net_dentry) {
498 		mlog_errno(-ENOMEM);
499 		goto bail;
500 	}
501 
502 	nst_dentry = debugfs_create_file(NST_DEBUG_NAME, S_IFREG|S_IRUSR,
503 					 o2net_dentry, NULL,
504 					 &nst_seq_fops);
505 	if (!nst_dentry) {
506 		mlog_errno(-ENOMEM);
507 		goto bail;
508 	}
509 
510 	sc_dentry = debugfs_create_file(SC_DEBUG_NAME, S_IFREG|S_IRUSR,
511 					o2net_dentry, NULL,
512 					&sc_seq_fops);
513 	if (!sc_dentry) {
514 		mlog_errno(-ENOMEM);
515 		goto bail;
516 	}
517 
518 	stats_dentry = debugfs_create_file(STATS_DEBUG_NAME, S_IFREG|S_IRUSR,
519 					   o2net_dentry, NULL,
520 					   &stats_seq_fops);
521 	if (!stats_dentry) {
522 		mlog_errno(-ENOMEM);
523 		goto bail;
524 	}
525 
526 	return 0;
527 bail:
528 	debugfs_remove(stats_dentry);
529 	debugfs_remove(sc_dentry);
530 	debugfs_remove(nst_dentry);
531 	debugfs_remove(o2net_dentry);
532 	return -ENOMEM;
533 }
534 
535 void o2net_debugfs_exit(void)
536 {
537 	debugfs_remove(stats_dentry);
538 	debugfs_remove(sc_dentry);
539 	debugfs_remove(nst_dentry);
540 	debugfs_remove(o2net_dentry);
541 }
542 
543 #endif	/* CONFIG_DEBUG_FS */
544