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 * $FreeBSD$
30 */
31
32/*
33 * Simple Marvell-specific tool to inspect and monitor network traffic
34 * statistics.
35 *
36 *	mwlstats [-i interface] [-l] [-o fmtstring] [interval]
37 *
38 * (default interface is mv0).  If interval is specified a rolling output
39 * a la netstat -i is displayed every interval seconds.  The format of
40 * the rolling display can be controlled a la ps.  The -l option will
41 * print a list of all possible statistics for use with the -o option.
42 */
43
44#include <stdlib.h>
45#include <stdio.h>
46#include <signal.h>
47#include <unistd.h>
48#include <err.h>
49
50#include "mwlstats.h"
51
52#define	S_DEFAULT \
53	"input,output,txtry,txretry,txmretry,txdoneput,rxfcs,rxcrypt,rxicv,rssi,rate"
54
55static int signalled;
56
57static void
58catchalarm(int signo __unused)
59{
60	signalled = 1;
61}
62
63int
64main(int argc, char *argv[])
65{
66	struct mwlstatfoo *wf;
67	int c;
68
69	wf = mwlstats_new("mwl0", S_DEFAULT);
70	while ((c = getopt(argc, argv, "i:lo:")) != -1) {
71		switch (c) {
72		case 'i':
73			wf->setifname(wf, optarg);
74			break;
75		case 'l':
76			wf->print_fields(wf, stdout);
77			return 0;
78		case 'o':
79			wf->setfmt(wf, optarg);
80			break;
81		default:
82			errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [interval]\n", argv[0]);
83			/*NOTREACHED*/
84		}
85	}
86	argc -= optind;
87	argv += optind;
88
89	if (argc > 0) {
90		u_long interval = strtoul(argv[0], NULL, 0);
91		int line, omask;
92
93		if (interval < 1)
94			interval = 1;
95		signal(SIGALRM, catchalarm);
96		signalled = 0;
97		alarm(interval);
98	banner:
99		wf->print_header(wf, stdout);
100		line = 0;
101	loop:
102		if (line != 0) {
103			wf->collect_cur(wf);
104			wf->print_current(wf, stdout);
105			wf->update_tot(wf);
106		} else {
107			wf->collect_tot(wf);
108			wf->print_total(wf, stdout);
109		}
110		fflush(stdout);
111		omask = sigblock(sigmask(SIGALRM));
112		if (!signalled)
113			sigpause(0);
114		sigsetmask(omask);
115		signalled = 0;
116		alarm(interval);
117		line++;
118		if (line == 21)		/* XXX tty line count */
119			goto banner;
120		else
121			goto loop;
122		/*NOTREACHED*/
123	} else {
124		wf->collect_tot(wf);
125		wf->print_verbose(wf, stdout);
126	}
127	return 0;
128}
129