xref: /freebsd/tools/tools/mwl/mwlstats/main.c (revision e0c4386e)
1 /*-
2  * Copyright (c) 2006 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 /*
31  * Simple Marvell-specific tool to inspect and monitor network traffic
32  * statistics.
33  *
34  *	mwlstats [-i interface] [-l] [-o fmtstring] [interval]
35  *
36  * (default interface is mv0).  If interval is specified a rolling output
37  * a la netstat -i is displayed every interval seconds.  The format of
38  * the rolling display can be controlled a la ps.  The -l option will
39  * print a list of all possible statistics for use with the -o option.
40  */
41 
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <signal.h>
45 #include <unistd.h>
46 #include <err.h>
47 
48 #include "mwlstats.h"
49 
50 #define	S_DEFAULT \
51 	"input,output,txtry,txretry,txmretry,txdoneput,rxfcs,rxcrypt,rxicv,rssi,rate"
52 
53 static int signalled;
54 
55 static void
56 catchalarm(int signo __unused)
57 {
58 	signalled = 1;
59 }
60 
61 int
62 main(int argc, char *argv[])
63 {
64 	struct mwlstatfoo *wf;
65 	int c;
66 
67 	wf = mwlstats_new("mwl0", S_DEFAULT);
68 	while ((c = getopt(argc, argv, "i:lo:")) != -1) {
69 		switch (c) {
70 		case 'i':
71 			wf->setifname(wf, optarg);
72 			break;
73 		case 'l':
74 			wf->print_fields(wf, stdout);
75 			return 0;
76 		case 'o':
77 			wf->setfmt(wf, optarg);
78 			break;
79 		default:
80 			errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [interval]\n", argv[0]);
81 			/*NOTREACHED*/
82 		}
83 	}
84 	argc -= optind;
85 	argv += optind;
86 
87 	if (argc > 0) {
88 		u_long interval = strtoul(argv[0], NULL, 0);
89 		int line, omask;
90 
91 		if (interval < 1)
92 			interval = 1;
93 		signal(SIGALRM, catchalarm);
94 		signalled = 0;
95 		alarm(interval);
96 	banner:
97 		wf->print_header(wf, stdout);
98 		line = 0;
99 	loop:
100 		if (line != 0) {
101 			wf->collect_cur(wf);
102 			wf->print_current(wf, stdout);
103 			wf->update_tot(wf);
104 		} else {
105 			wf->collect_tot(wf);
106 			wf->print_total(wf, stdout);
107 		}
108 		fflush(stdout);
109 		omask = sigblock(sigmask(SIGALRM));
110 		if (!signalled)
111 			sigpause(0);
112 		sigsetmask(omask);
113 		signalled = 0;
114 		alarm(interval);
115 		line++;
116 		if (line == 21)		/* XXX tty line count */
117 			goto banner;
118 		else
119 			goto loop;
120 		/*NOTREACHED*/
121 	} else {
122 		wf->collect_tot(wf);
123 		wf->print_verbose(wf, stdout);
124 	}
125 	return 0;
126 }
127