xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/pppstats/pppstats.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * print PPP statistics:
3*0Sstevel@tonic-gate  * 	pppstats [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  *   -a Show absolute values rather than deltas
6*0Sstevel@tonic-gate  *   -d Show data rate (kB/s) rather than bytes
7*0Sstevel@tonic-gate  *   -v Show more stats for VJ TCP header compression
8*0Sstevel@tonic-gate  *   -r Show compression ratio
9*0Sstevel@tonic-gate  *   -z Show compression statistics instead of default display
10*0Sstevel@tonic-gate  *
11*0Sstevel@tonic-gate  * History:
12*0Sstevel@tonic-gate  *      perkins@cps.msu.edu: Added compression statistics and alternate
13*0Sstevel@tonic-gate  *                display. 11/94
14*0Sstevel@tonic-gate  *	Brad Parker (brad@cayman.com) 6/92
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * from the original "slstats" by Van Jacobson
17*0Sstevel@tonic-gate  *
18*0Sstevel@tonic-gate  * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
19*0Sstevel@tonic-gate  * All rights reserved.
20*0Sstevel@tonic-gate  *
21*0Sstevel@tonic-gate  * Copyright (c) 1989 Regents of the University of California.
22*0Sstevel@tonic-gate  * All rights reserved.
23*0Sstevel@tonic-gate  *
24*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
25*0Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
26*0Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
27*0Sstevel@tonic-gate  * advertising materials, and other materials related to such
28*0Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
29*0Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
30*0Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
31*0Sstevel@tonic-gate  * from this software without specific prior written permission.
32*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
33*0Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
34*0Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #ifndef __STDC__
38*0Sstevel@tonic-gate #define const
39*0Sstevel@tonic-gate #endif
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #ifndef lint
44*0Sstevel@tonic-gate static const char rcsid[] = "$Id: pppstats.c,v 1.27 1999/08/13 06:46:23 paulus Exp $";
45*0Sstevel@tonic-gate #endif
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #include <stdio.h>
48*0Sstevel@tonic-gate #include <stddef.h>
49*0Sstevel@tonic-gate #include <stdlib.h>
50*0Sstevel@tonic-gate #include <string.h>
51*0Sstevel@tonic-gate #include <ctype.h>
52*0Sstevel@tonic-gate #include <errno.h>
53*0Sstevel@tonic-gate #include <signal.h>
54*0Sstevel@tonic-gate #include <fcntl.h>
55*0Sstevel@tonic-gate #include <unistd.h>
56*0Sstevel@tonic-gate #include <sys/param.h>
57*0Sstevel@tonic-gate #include <sys/types.h>
58*0Sstevel@tonic-gate #include <sys/ioctl.h>
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #ifndef STREAMS
61*0Sstevel@tonic-gate #if defined(_linux_) && defined(__powerpc__) \
62*0Sstevel@tonic-gate     && (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
63*0Sstevel@tonic-gate /* kludge alert! */
64*0Sstevel@tonic-gate #undef __GLIBC__
65*0Sstevel@tonic-gate #endif
66*0Sstevel@tonic-gate #include <sys/socket.h>		/* *BSD, Linux, NeXT, Ultrix etc. */
67*0Sstevel@tonic-gate #ifndef _linux_
68*0Sstevel@tonic-gate #include <net/if.h>
69*0Sstevel@tonic-gate #include <net/ppp_defs.h>
70*0Sstevel@tonic-gate #include <net/if_ppp.h>
71*0Sstevel@tonic-gate #else
72*0Sstevel@tonic-gate /* Linux */
73*0Sstevel@tonic-gate #if __GLIBC__ >= 2
74*0Sstevel@tonic-gate #include <asm/types.h>		/* glibc 2 conflicts with linux/types.h */
75*0Sstevel@tonic-gate #include <net/if.h>
76*0Sstevel@tonic-gate #else
77*0Sstevel@tonic-gate #include <linux/types.h>
78*0Sstevel@tonic-gate #include <linux/if.h>
79*0Sstevel@tonic-gate #endif
80*0Sstevel@tonic-gate #include <linux/ppp_defs.h>
81*0Sstevel@tonic-gate #include <linux/if_ppp.h>
82*0Sstevel@tonic-gate #endif /* _linux_ */
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate #else	/* STREAMS */
85*0Sstevel@tonic-gate #include <sys/stropts.h>	/* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
86*0Sstevel@tonic-gate #include <net/ppp_defs.h>
87*0Sstevel@tonic-gate #include <net/pppio.h>
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate #ifdef PPPIO_GETSTAT64
90*0Sstevel@tonic-gate #define	ppp_stats64	ppp_stats64
91*0Sstevel@tonic-gate #endif
92*0Sstevel@tonic-gate #endif	/* STREAMS */
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate #ifndef ppp_stats64
95*0Sstevel@tonic-gate #define	ppp_stats64	ppp_stats
96*0Sstevel@tonic-gate #endif
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate static int	vflag, rflag, zflag;	/* select type of display */
99*0Sstevel@tonic-gate static int	aflag;			/* print absolute values, not deltas */
100*0Sstevel@tonic-gate static int	dflag;			/* print data rates, not bytes */
101*0Sstevel@tonic-gate static int	interval, count;
102*0Sstevel@tonic-gate static int	infinite;
103*0Sstevel@tonic-gate static int	unit;
104*0Sstevel@tonic-gate static int	s;			/* socket or /dev/ppp file descriptor */
105*0Sstevel@tonic-gate static int	signalled;		/* set if alarm goes off "early" */
106*0Sstevel@tonic-gate static char	*progname;
107*0Sstevel@tonic-gate static char	*interface;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate #if defined(SUNOS4) || defined(ULTRIX) || defined(NeXT)
110*0Sstevel@tonic-gate extern int optind;
111*0Sstevel@tonic-gate extern char *optarg;
112*0Sstevel@tonic-gate #endif
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate  * If PPP_DRV_NAME is not defined, use the legacy "ppp" as the
116*0Sstevel@tonic-gate  * device name.
117*0Sstevel@tonic-gate  */
118*0Sstevel@tonic-gate #if !defined(PPP_DRV_NAME)
119*0Sstevel@tonic-gate #define PPP_DRV_NAME    "ppp"
120*0Sstevel@tonic-gate #endif /* !defined(PPP_DRV_NAME) */
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate static void usage __P((void));
123*0Sstevel@tonic-gate static void catchalarm __P((int));
124*0Sstevel@tonic-gate static void get_ppp_stats __P((struct ppp_stats64 *));
125*0Sstevel@tonic-gate static void get_ppp_cstats __P((struct ppp_comp_stats *));
126*0Sstevel@tonic-gate static void intpr __P((void));
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate int main __P((int, char *argv[]));
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate static void
usage()131*0Sstevel@tonic-gate usage()
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate     (void) fprintf(stderr,
134*0Sstevel@tonic-gate 	"Usage: %s [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]\n",
135*0Sstevel@tonic-gate 	progname);
136*0Sstevel@tonic-gate     exit(1);
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate  * Called if an interval expires before intpr has completed a loop.
141*0Sstevel@tonic-gate  * Sets a flag to not wait for the alarm.
142*0Sstevel@tonic-gate  */
143*0Sstevel@tonic-gate /* ARGSUSED */
144*0Sstevel@tonic-gate static void
catchalarm(arg)145*0Sstevel@tonic-gate catchalarm(arg)
146*0Sstevel@tonic-gate     int arg;
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate     signalled = 1;
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate #ifndef STREAMS
153*0Sstevel@tonic-gate static void
get_ppp_stats(curp)154*0Sstevel@tonic-gate get_ppp_stats(curp)
155*0Sstevel@tonic-gate     struct ppp_stats64 *curp;
156*0Sstevel@tonic-gate {
157*0Sstevel@tonic-gate     struct ifpppstatsreq req;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate     (void) memset (&req, 0, sizeof (req));
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate #ifdef _linux_
162*0Sstevel@tonic-gate     req.stats_ptr = (caddr_t) &req.stats;
163*0Sstevel@tonic-gate #undef ifr_name
164*0Sstevel@tonic-gate #define ifr_name ifr__name
165*0Sstevel@tonic-gate #endif
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate     strncpy(req.ifr_name, interface, sizeof(req.ifr_name));
168*0Sstevel@tonic-gate     if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
169*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", progname);
170*0Sstevel@tonic-gate 	if (errno == ENOTTY)
171*0Sstevel@tonic-gate 	    (void) fprintf(stderr, "kernel support missing\n");
172*0Sstevel@tonic-gate 	else
173*0Sstevel@tonic-gate 	    perror("couldn't get PPP statistics");
174*0Sstevel@tonic-gate 	exit(1);
175*0Sstevel@tonic-gate     }
176*0Sstevel@tonic-gate     *curp = req.stats;
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate static void
get_ppp_cstats(csp)180*0Sstevel@tonic-gate get_ppp_cstats(csp)
181*0Sstevel@tonic-gate     struct ppp_comp_stats *csp;
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate     struct ifpppcstatsreq creq;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate     (void) memset (&creq, 0, sizeof (creq));
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate #ifdef _linux_
188*0Sstevel@tonic-gate     creq.stats_ptr = (caddr_t) &creq.stats;
189*0Sstevel@tonic-gate #undef  ifr_name
190*0Sstevel@tonic-gate #define ifr_name ifr__name
191*0Sstevel@tonic-gate #endif
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate     strncpy(creq.ifr_name, interface, sizeof(creq.ifr_name));
194*0Sstevel@tonic-gate     if (ioctl(s, SIOCGPPPCSTATS, &creq) < 0) {
195*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", progname);
196*0Sstevel@tonic-gate 	if (errno == ENOTTY) {
197*0Sstevel@tonic-gate 	    (void) fprintf(stderr, "no kernel compression support\n");
198*0Sstevel@tonic-gate 	    if (zflag)
199*0Sstevel@tonic-gate 		exit(1);
200*0Sstevel@tonic-gate 	    rflag = 0;
201*0Sstevel@tonic-gate 	} else {
202*0Sstevel@tonic-gate 	    perror("couldn't get PPP compression stats");
203*0Sstevel@tonic-gate 	    exit(1);
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate     }
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate #ifdef _linux_
208*0Sstevel@tonic-gate     if (creq.stats.c.bytes_out == 0) {
209*0Sstevel@tonic-gate 	creq.stats.c.bytes_out = creq.stats.c.comp_bytes + creq.stats.c.inc_bytes;
210*0Sstevel@tonic-gate 	creq.stats.c.in_count = creq.stats.c.unc_bytes;
211*0Sstevel@tonic-gate     }
212*0Sstevel@tonic-gate     if (creq.stats.c.bytes_out == 0)
213*0Sstevel@tonic-gate 	creq.stats.c.ratio = 0.0;
214*0Sstevel@tonic-gate     else
215*0Sstevel@tonic-gate 	creq.stats.c.ratio = 256.0 * creq.stats.c.in_count /
216*0Sstevel@tonic-gate 			     creq.stats.c.bytes_out;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate     if (creq.stats.d.bytes_out == 0) {
219*0Sstevel@tonic-gate 	creq.stats.d.bytes_out = creq.stats.d.comp_bytes + creq.stats.d.inc_bytes;
220*0Sstevel@tonic-gate 	creq.stats.d.in_count = creq.stats.d.unc_bytes;
221*0Sstevel@tonic-gate     }
222*0Sstevel@tonic-gate     if (creq.stats.d.bytes_out == 0)
223*0Sstevel@tonic-gate 	creq.stats.d.ratio = 0.0;
224*0Sstevel@tonic-gate     else
225*0Sstevel@tonic-gate 	creq.stats.d.ratio = 256.0 * creq.stats.d.in_count /
226*0Sstevel@tonic-gate 			     creq.stats.d.bytes_out;
227*0Sstevel@tonic-gate #endif
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate     *csp = creq.stats;
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate #else	/* STREAMS */
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate static int
strioctl(fd,cmd,ptr,ilen,olen)235*0Sstevel@tonic-gate strioctl(fd, cmd, ptr, ilen, olen)
236*0Sstevel@tonic-gate     int fd, cmd, ilen, olen;
237*0Sstevel@tonic-gate     char *ptr;
238*0Sstevel@tonic-gate {
239*0Sstevel@tonic-gate     struct strioctl str;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate     str.ic_cmd = cmd;
242*0Sstevel@tonic-gate     str.ic_timout = 0;
243*0Sstevel@tonic-gate     str.ic_len = ilen;
244*0Sstevel@tonic-gate     str.ic_dp = ptr;
245*0Sstevel@tonic-gate     if (ioctl(fd, I_STR, &str) == -1)
246*0Sstevel@tonic-gate 	return -1;
247*0Sstevel@tonic-gate     if (str.ic_len != olen)
248*0Sstevel@tonic-gate 	(void) fprintf(stderr,
249*0Sstevel@tonic-gate 	    "strioctl: expected %d bytes, got %d for cmd %x\n",
250*0Sstevel@tonic-gate 	    olen, str.ic_len, cmd);
251*0Sstevel@tonic-gate     return 0;
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate static void
get_ppp_stats(curp)255*0Sstevel@tonic-gate get_ppp_stats(curp)
256*0Sstevel@tonic-gate     struct ppp_stats64 *curp;
257*0Sstevel@tonic-gate {
258*0Sstevel@tonic-gate #ifdef PPPIO_GETSTAT64
259*0Sstevel@tonic-gate     struct ppp_stats oldstat;
260*0Sstevel@tonic-gate     if (strioctl(s, PPPIO_GETSTAT64, (char *)curp, 0, sizeof(*curp)) >= 0)
261*0Sstevel@tonic-gate 	return;
262*0Sstevel@tonic-gate     if (strioctl(s, PPPIO_GETSTAT, (char *)&oldstat, 0, sizeof(oldstat)) >= 0) {
263*0Sstevel@tonic-gate 	curp->p.ppp_ibytes = oldstat.p.ppp_ibytes;
264*0Sstevel@tonic-gate 	curp->p.ppp_ipackets = oldstat.p.ppp_ipackets;
265*0Sstevel@tonic-gate 	curp->p.ppp_ierrors = oldstat.p.ppp_ierrors;
266*0Sstevel@tonic-gate 	curp->p.ppp_obytes = oldstat.p.ppp_obytes;
267*0Sstevel@tonic-gate 	curp->p.ppp_opackets = oldstat.p.ppp_opackets;
268*0Sstevel@tonic-gate 	curp->p.ppp_oerrors = oldstat.p.ppp_oerrors;
269*0Sstevel@tonic-gate 	curp->vj = oldstat.vj;
270*0Sstevel@tonic-gate 	return;
271*0Sstevel@tonic-gate     }
272*0Sstevel@tonic-gate #else
273*0Sstevel@tonic-gate     if (strioctl(s, PPPIO_GETSTAT, (char *)curp, 0, sizeof(*curp)) >= 0)
274*0Sstevel@tonic-gate 	return;
275*0Sstevel@tonic-gate #endif
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate     (void) fprintf(stderr, "%s: ", progname);
278*0Sstevel@tonic-gate     if (errno == EINVAL)
279*0Sstevel@tonic-gate 	(void) fprintf(stderr, "kernel support missing\n");
280*0Sstevel@tonic-gate     else
281*0Sstevel@tonic-gate 	perror("couldn't get PPP statistics");
282*0Sstevel@tonic-gate     exit(1);
283*0Sstevel@tonic-gate }
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate static void
get_ppp_cstats(csp)286*0Sstevel@tonic-gate get_ppp_cstats(csp)
287*0Sstevel@tonic-gate     struct ppp_comp_stats *csp;
288*0Sstevel@tonic-gate {
289*0Sstevel@tonic-gate     if (strioctl(s, PPPIO_GETCSTAT, (char *)csp, 0, sizeof(*csp)) < 0) {
290*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", progname);
291*0Sstevel@tonic-gate 	if (errno == ENOTTY) {
292*0Sstevel@tonic-gate 	    (void) fprintf(stderr, "no kernel compression support\n");
293*0Sstevel@tonic-gate 	    if (zflag)
294*0Sstevel@tonic-gate 		exit(1);
295*0Sstevel@tonic-gate 	    rflag = 0;
296*0Sstevel@tonic-gate 	} else {
297*0Sstevel@tonic-gate 	    perror("couldn't get PPP compression statistics");
298*0Sstevel@tonic-gate 	    exit(1);
299*0Sstevel@tonic-gate 	}
300*0Sstevel@tonic-gate     }
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate #endif /* STREAMS */
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate #define MAX0(a)		((int)(a) > 0? (a): 0)
306*0Sstevel@tonic-gate #define V(offset)	MAX0(cur.offset - old.offset)
307*0Sstevel@tonic-gate #define W(offset)	MAX0(ccs.offset - ocs.offset)
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate #define RATIO(c, i, u)	((c) == 0? 1.0: (u) / ((double)(c) + (i)))
310*0Sstevel@tonic-gate #define CRATE(x)	RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate #define KBPS(n)		((n) / (interval * 1000.0))
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate /*
315*0Sstevel@tonic-gate  * Print a running summary of interface statistics.
316*0Sstevel@tonic-gate  * Repeat display every interval seconds, showing statistics
317*0Sstevel@tonic-gate  * collected over that interval.  Assumes that interval is non-zero.
318*0Sstevel@tonic-gate  * First line printed is cumulative.
319*0Sstevel@tonic-gate  */
320*0Sstevel@tonic-gate static void
intpr()321*0Sstevel@tonic-gate intpr()
322*0Sstevel@tonic-gate {
323*0Sstevel@tonic-gate     register int line = 0;
324*0Sstevel@tonic-gate     sigset_t oldmask, mask;
325*0Sstevel@tonic-gate     char *bunit;
326*0Sstevel@tonic-gate     int ratef = 0;
327*0Sstevel@tonic-gate     struct ppp_stats64 cur, old;
328*0Sstevel@tonic-gate     struct ppp_comp_stats ccs, ocs;
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate     (void) memset(&old, 0, sizeof(old));
331*0Sstevel@tonic-gate     (void) memset(&ocs, 0, sizeof(ocs));
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate     for (;;) {
334*0Sstevel@tonic-gate 	get_ppp_stats(&cur);
335*0Sstevel@tonic-gate 	if (zflag || rflag)
336*0Sstevel@tonic-gate 	    get_ppp_cstats(&ccs);
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	(void)signal(SIGALRM, catchalarm);
339*0Sstevel@tonic-gate 	signalled = 0;
340*0Sstevel@tonic-gate 	(void)alarm(interval);
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	if ((line % 20) == 0) {
343*0Sstevel@tonic-gate 	    if (zflag) {
344*0Sstevel@tonic-gate 		(void) printf("IN:  COMPRESSED  INCOMPRESSIBLE   COMP | ");
345*0Sstevel@tonic-gate 		(void) printf("OUT: COMPRESSED  INCOMPRESSIBLE   COMP\n");
346*0Sstevel@tonic-gate 		bunit = dflag? "KB/S": "BYTE";
347*0Sstevel@tonic-gate 		(void) printf("    %s   PACK     %s   PACK  RATIO | ", bunit,
348*0Sstevel@tonic-gate 		    bunit);
349*0Sstevel@tonic-gate 		(void) printf("    %s   PACK     %s   PACK  RATIO", bunit,
350*0Sstevel@tonic-gate 		    bunit);
351*0Sstevel@tonic-gate 	    } else {
352*0Sstevel@tonic-gate 		(void) printf("%8.8s %6.6s %6.6s",
353*0Sstevel@tonic-gate 		       "IN", "PACK", "VJCOMP");
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 		if (!rflag)
356*0Sstevel@tonic-gate 		    (void) printf(" %6.6s %6.6s", "VJUNC", "VJERR");
357*0Sstevel@tonic-gate 		if (vflag)
358*0Sstevel@tonic-gate 		    (void) printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
359*0Sstevel@tonic-gate 		if (rflag)
360*0Sstevel@tonic-gate 		    (void) printf(" %6.6s %6.6s", "RATIO", "UBYTE");
361*0Sstevel@tonic-gate 		(void) printf("  | %8.8s %6.6s %6.6s",
362*0Sstevel@tonic-gate 		       "OUT", "PACK", "VJCOMP");
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 		if (!rflag)
365*0Sstevel@tonic-gate 		    (void) printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
366*0Sstevel@tonic-gate 		if (vflag)
367*0Sstevel@tonic-gate 		    (void) printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
368*0Sstevel@tonic-gate 		if (rflag)
369*0Sstevel@tonic-gate 		    (void) printf(" %6.6s %6.6s", "RATIO", "UBYTE");
370*0Sstevel@tonic-gate 	    }
371*0Sstevel@tonic-gate 	    (void) putchar('\n');
372*0Sstevel@tonic-gate 	}
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	if (zflag) {
375*0Sstevel@tonic-gate 	    if (ratef) {
376*0Sstevel@tonic-gate 		(void) printf("%8.3f %6u %8.3f %6u %6.2f",
377*0Sstevel@tonic-gate 		       KBPS(W(d.comp_bytes)),
378*0Sstevel@tonic-gate 		       W(d.comp_packets),
379*0Sstevel@tonic-gate 		       KBPS(W(d.inc_bytes)),
380*0Sstevel@tonic-gate 		       W(d.inc_packets),
381*0Sstevel@tonic-gate 		       ccs.d.ratio / 256.0);
382*0Sstevel@tonic-gate 		(void) printf(" | %8.3f %6u %8.3f %6u %6.2f",
383*0Sstevel@tonic-gate 		       KBPS(W(c.comp_bytes)),
384*0Sstevel@tonic-gate 		       W(c.comp_packets),
385*0Sstevel@tonic-gate 		       KBPS(W(c.inc_bytes)),
386*0Sstevel@tonic-gate 		       W(c.inc_packets),
387*0Sstevel@tonic-gate 		       ccs.c.ratio / 256.0);
388*0Sstevel@tonic-gate 	    } else {
389*0Sstevel@tonic-gate 		(void) printf("%8u %6u %8u %6u %6.2f",
390*0Sstevel@tonic-gate 		       W(d.comp_bytes),
391*0Sstevel@tonic-gate 		       W(d.comp_packets),
392*0Sstevel@tonic-gate 		       W(d.inc_bytes),
393*0Sstevel@tonic-gate 		       W(d.inc_packets),
394*0Sstevel@tonic-gate 		       ccs.d.ratio / 256.0);
395*0Sstevel@tonic-gate 		(void) printf(" | %8u %6u %8u %6u %6.2f",
396*0Sstevel@tonic-gate 		       W(c.comp_bytes),
397*0Sstevel@tonic-gate 		       W(c.comp_packets),
398*0Sstevel@tonic-gate 		       W(c.inc_bytes),
399*0Sstevel@tonic-gate 		       W(c.inc_packets),
400*0Sstevel@tonic-gate 		       ccs.c.ratio / 256.0);
401*0Sstevel@tonic-gate 	    }
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	} else {
404*0Sstevel@tonic-gate 	    if (ratef)
405*0Sstevel@tonic-gate 		(void) printf("%8.3f", KBPS(V(p.ppp_ibytes)));
406*0Sstevel@tonic-gate 	    else
407*0Sstevel@tonic-gate 		(void) printf("%8" PPP_COUNTER_F, V(p.ppp_ibytes));
408*0Sstevel@tonic-gate 	    (void) printf(" %6" PPP_COUNTER_F " %6u",
409*0Sstevel@tonic-gate 		   V(p.ppp_ipackets),
410*0Sstevel@tonic-gate 		   V(vj.vjs_compressedin));
411*0Sstevel@tonic-gate 	    if (!rflag)
412*0Sstevel@tonic-gate 		(void) printf(" %6u %6u",
413*0Sstevel@tonic-gate 		       V(vj.vjs_uncompressedin),
414*0Sstevel@tonic-gate 		       V(vj.vjs_errorin));
415*0Sstevel@tonic-gate 	    if (vflag)
416*0Sstevel@tonic-gate 		(void) printf(" %6u %6" PPP_COUNTER_F,
417*0Sstevel@tonic-gate 		       V(vj.vjs_tossed),
418*0Sstevel@tonic-gate 		       V(p.ppp_ipackets) - V(vj.vjs_compressedin)
419*0Sstevel@tonic-gate 		       - V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
420*0Sstevel@tonic-gate 	    if (rflag) {
421*0Sstevel@tonic-gate 		(void) printf(" %6.2f ", CRATE(d));
422*0Sstevel@tonic-gate 		if (ratef)
423*0Sstevel@tonic-gate 		    (void) printf("%6.2f", KBPS(W(d.unc_bytes)));
424*0Sstevel@tonic-gate 		else
425*0Sstevel@tonic-gate 		    (void) printf("%6u", W(d.unc_bytes));
426*0Sstevel@tonic-gate 	    }
427*0Sstevel@tonic-gate 	    if (ratef)
428*0Sstevel@tonic-gate 		(void) printf("  | %8.3f", KBPS(V(p.ppp_obytes)));
429*0Sstevel@tonic-gate 	    else
430*0Sstevel@tonic-gate 		(void) printf("  | %8" PPP_COUNTER_F, V(p.ppp_obytes));
431*0Sstevel@tonic-gate 	    (void) printf(" %6" PPP_COUNTER_F " %6u",
432*0Sstevel@tonic-gate 		   V(p.ppp_opackets),
433*0Sstevel@tonic-gate 		   V(vj.vjs_compressed));
434*0Sstevel@tonic-gate 	    if (!rflag)
435*0Sstevel@tonic-gate 		(void) printf(" %6u %6" PPP_COUNTER_F,
436*0Sstevel@tonic-gate 		       V(vj.vjs_packets) - V(vj.vjs_compressed),
437*0Sstevel@tonic-gate 		       V(p.ppp_opackets) - V(vj.vjs_packets));
438*0Sstevel@tonic-gate 	    if (vflag)
439*0Sstevel@tonic-gate 		(void) printf(" %6u %6u",
440*0Sstevel@tonic-gate 		       V(vj.vjs_searches),
441*0Sstevel@tonic-gate 		       V(vj.vjs_misses));
442*0Sstevel@tonic-gate 	    if (rflag) {
443*0Sstevel@tonic-gate 		(void) printf(" %6.2f ", CRATE(c));
444*0Sstevel@tonic-gate 		if (ratef)
445*0Sstevel@tonic-gate 		    (void) printf("%6.2f", KBPS(W(c.unc_bytes)));
446*0Sstevel@tonic-gate 		else
447*0Sstevel@tonic-gate 		    (void) printf("%6u", W(c.unc_bytes));
448*0Sstevel@tonic-gate 	    }
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 	}
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	(void) putchar('\n');
453*0Sstevel@tonic-gate 	(void) fflush(stdout);
454*0Sstevel@tonic-gate 	line++;
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	count--;
457*0Sstevel@tonic-gate 	if (!infinite && !count)
458*0Sstevel@tonic-gate 	    break;
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 	(void) sigemptyset(&mask);
461*0Sstevel@tonic-gate 	(void) sigaddset(&mask, SIGALRM);
462*0Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &mask, &oldmask);
463*0Sstevel@tonic-gate 	if (!signalled) {
464*0Sstevel@tonic-gate 	    (void) sigemptyset(&mask);
465*0Sstevel@tonic-gate 	    (void) sigsuspend(&mask);
466*0Sstevel@tonic-gate 	}
467*0Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oldmask, NULL);
468*0Sstevel@tonic-gate 	signalled = 0;
469*0Sstevel@tonic-gate 	(void)alarm(interval);
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	if (!aflag) {
472*0Sstevel@tonic-gate 	    old = cur;
473*0Sstevel@tonic-gate 	    ocs = ccs;
474*0Sstevel@tonic-gate 	    ratef = dflag;
475*0Sstevel@tonic-gate 	}
476*0Sstevel@tonic-gate     }
477*0Sstevel@tonic-gate }
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate int
main(argc,argv)480*0Sstevel@tonic-gate main(argc, argv)
481*0Sstevel@tonic-gate     int argc;
482*0Sstevel@tonic-gate     char *argv[];
483*0Sstevel@tonic-gate {
484*0Sstevel@tonic-gate     int c;
485*0Sstevel@tonic-gate #ifdef STREAMS
486*0Sstevel@tonic-gate     char *dev;
487*0Sstevel@tonic-gate #endif
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate     interface = PPP_DRV_NAME "0";
490*0Sstevel@tonic-gate     if ((progname = strrchr(argv[0], '/')) == NULL)
491*0Sstevel@tonic-gate 	progname = argv[0];
492*0Sstevel@tonic-gate     else
493*0Sstevel@tonic-gate 	++progname;
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate     while ((c = getopt(argc, argv, "advrzc:w:")) != -1) {
496*0Sstevel@tonic-gate 	switch (c) {
497*0Sstevel@tonic-gate 	case 'a':
498*0Sstevel@tonic-gate 	    ++aflag;
499*0Sstevel@tonic-gate 	    break;
500*0Sstevel@tonic-gate 	case 'd':
501*0Sstevel@tonic-gate 	    ++dflag;
502*0Sstevel@tonic-gate 	    break;
503*0Sstevel@tonic-gate 	case 'v':
504*0Sstevel@tonic-gate 	    ++vflag;
505*0Sstevel@tonic-gate 	    break;
506*0Sstevel@tonic-gate 	case 'r':
507*0Sstevel@tonic-gate 	    ++rflag;
508*0Sstevel@tonic-gate 	    break;
509*0Sstevel@tonic-gate 	case 'z':
510*0Sstevel@tonic-gate 	    ++zflag;
511*0Sstevel@tonic-gate 	    break;
512*0Sstevel@tonic-gate 	case 'c':
513*0Sstevel@tonic-gate 	    count = atoi(optarg);
514*0Sstevel@tonic-gate 	    if (count <= 0)
515*0Sstevel@tonic-gate 		usage();
516*0Sstevel@tonic-gate 	    break;
517*0Sstevel@tonic-gate 	case 'w':
518*0Sstevel@tonic-gate 	    interval = atoi(optarg);
519*0Sstevel@tonic-gate 	    if (interval <= 0)
520*0Sstevel@tonic-gate 		usage();
521*0Sstevel@tonic-gate 	    break;
522*0Sstevel@tonic-gate 	default:
523*0Sstevel@tonic-gate 	    usage();
524*0Sstevel@tonic-gate 	}
525*0Sstevel@tonic-gate     }
526*0Sstevel@tonic-gate     argc -= optind;
527*0Sstevel@tonic-gate     argv += optind;
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate     if (!interval && count)
530*0Sstevel@tonic-gate 	interval = 5;
531*0Sstevel@tonic-gate     if (interval && !count)
532*0Sstevel@tonic-gate 	infinite = 1;
533*0Sstevel@tonic-gate     if (!interval && !count)
534*0Sstevel@tonic-gate 	count = 1;
535*0Sstevel@tonic-gate     if (aflag)
536*0Sstevel@tonic-gate 	dflag = 0;
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate     if (argc > 1)
539*0Sstevel@tonic-gate 	usage();
540*0Sstevel@tonic-gate     if (argc > 0)
541*0Sstevel@tonic-gate 	interface = argv[0];
542*0Sstevel@tonic-gate 
543*0Sstevel@tonic-gate     if (sscanf(interface, PPP_DRV_NAME "%d", &unit) != 1) {
544*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: invalid interface '%s' specified\n",
545*0Sstevel@tonic-gate 		progname, interface);
546*0Sstevel@tonic-gate     }
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate #ifndef STREAMS
549*0Sstevel@tonic-gate     {
550*0Sstevel@tonic-gate 	struct ifreq ifr;
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 	s = socket(AF_INET, SOCK_DGRAM, 0);
553*0Sstevel@tonic-gate 	if (s < 0) {
554*0Sstevel@tonic-gate 	    (void) fprintf(stderr, "%s: ", progname);
555*0Sstevel@tonic-gate 	    perror("couldn't create IP socket");
556*0Sstevel@tonic-gate 	    exit(1);
557*0Sstevel@tonic-gate 	}
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate #ifdef _linux_
560*0Sstevel@tonic-gate #undef  ifr_name
561*0Sstevel@tonic-gate #define ifr_name ifr_ifrn.ifrn_name
562*0Sstevel@tonic-gate #endif
563*0Sstevel@tonic-gate 	strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
564*0Sstevel@tonic-gate 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
565*0Sstevel@tonic-gate 	    (void) fprintf(stderr, "%s: nonexistent interface '%s' specified\n",
566*0Sstevel@tonic-gate 		    progname, interface);
567*0Sstevel@tonic-gate 	    exit(1);
568*0Sstevel@tonic-gate 	}
569*0Sstevel@tonic-gate     }
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate #else	/* STREAMS */
572*0Sstevel@tonic-gate #ifdef __osf__
573*0Sstevel@tonic-gate     dev = "/dev/streams/ppp";
574*0Sstevel@tonic-gate #else
575*0Sstevel@tonic-gate     dev = "/dev/" PPP_DRV_NAME;
576*0Sstevel@tonic-gate #endif
577*0Sstevel@tonic-gate     if ((s = open(dev, O_RDONLY)) < 0) {
578*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: couldn't open ", progname);
579*0Sstevel@tonic-gate 	perror(dev);
580*0Sstevel@tonic-gate 	exit(1);
581*0Sstevel@tonic-gate     }
582*0Sstevel@tonic-gate     if (strioctl(s, PPPIO_ATTACH, (char *)&unit, sizeof(int), 0) < 0) {
583*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: " PPP_DRV_NAME "%d is not available\n",
584*0Sstevel@tonic-gate 	    progname, unit);
585*0Sstevel@tonic-gate 	exit(1);
586*0Sstevel@tonic-gate     }
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate #endif	/* STREAMS */
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate     intpr();
591*0Sstevel@tonic-gate     return (0);
592*0Sstevel@tonic-gate }
593