1 /* $NetBSD: pppstats.c,v 1.2 2006/03/02 17:32:28 christos Exp $ */
4 * print PPP statistics:
5 * pppstats [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]
7 * -a Show absolute values rather than deltas
8 * -d Show data rate (kB/s) rather than bytes
9 * -v Show more stats for VJ TCP header compression
10 * -r Show compression ratio
11 * -z Show compression statistics instead of default display
14 * perkins@cps.msu.edu: Added compression statistics and alternate
16 * Brad Parker (brad@cayman.com) 6/92
18 * from the original "slstats" by Van Jacobson
20 * Copyright (c) 1989 Regents of the University of California.
21 * All rights reserved.
23 * Redistribution and use in source and binary forms are permitted
24 * provided that the above copyright notice and this paragraph are
25 * duplicated in all such forms and that any documentation,
26 * advertising materials, and other materials related to such
27 * distribution and use acknowledge that the software was developed
28 * by the University of California, Berkeley. The name of the
29 * University may not be used to endorse or promote products derived
30 * from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
33 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
40 #include <sys/cdefs.h>
43 static const char rcsid
[] = "Id: pppstats.c,v 1.29 2002/10/27 12:56:26 fcusack Exp";
45 __RCSID("$NetBSD: pppstats.c,v 1.2 2006/03/02 17:32:28 christos Exp $");
58 #include <sys/param.h>
59 #include <sys/types.h>
60 #include <sys/ioctl.h>
63 #if defined(__linux__) && defined(__powerpc__) \
64 && (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
68 #include <sys/socket.h> /* *BSD, Linux, NeXT, Ultrix etc. */
71 #include <net/ppp_defs.h>
72 #include <net/if_ppp.h>
76 #include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
79 #include <linux/types.h>
82 #include <linux/ppp_defs.h>
83 #include <linux/if_ppp.h>
84 #endif /* __linux__ */
87 #include <sys/stropts.h> /* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
88 #include <net/ppp_defs.h>
89 #include <net/pppio.h>
93 int vflag
, rflag
, zflag
; /* select type of display */
94 int aflag
; /* print absolute values, not deltas */
95 int dflag
; /* print data rates, not bytes */
99 int s
; /* socket or /dev/ppp file descriptor */
100 int signalled
; /* set if alarm goes off "early" */
104 #if defined(SUNOS4) || defined(ULTRIX) || defined(NeXT)
110 * If PPP_DRV_NAME is not defined, use the legacy "ppp" as the
113 #if !defined(PPP_DRV_NAME)
114 #define PPP_DRV_NAME "ppp"
115 #endif /* !defined(PPP_DRV_NAME) */
116 #if !defined(SL_DRV_NAME)
117 #define SL_DRV_NAME "sl"
118 #endif /* !defined(SL_DRV_NAME) */
120 static void usage
__P((void));
121 static void catchalarm
__P((int));
122 static void get_ppp_stats
__P((struct ppp_stats
*));
123 static void get_ppp_cstats
__P((struct ppp_comp_stats
*));
124 static void intpr
__P((void));
126 int main
__P((int, char *argv
[]));
131 fprintf(stderr
, "Usage: %s [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]\n",
137 * Called if an interval expires before intpr has completed a loop.
138 * Sets a flag to not wait for the alarm.
151 struct ppp_stats
*curp
;
153 struct ifpppstatsreq req
;
155 memset (&req
, 0, sizeof (req
));
158 req
.stats_ptr
= (caddr_t
) &req
.stats
;
160 #define ifr_name ifr__name
163 strncpy(req
.ifr_name
, interface
, sizeof(req
.ifr_name
));
164 if (ioctl(s
, SIOCGPPPSTATS
, &req
) < 0) {
165 fprintf(stderr
, "%s: ", progname
);
167 fprintf(stderr
, "kernel support missing\n");
169 perror("couldn't get PPP statistics");
177 struct ppp_comp_stats
*csp
;
179 struct ifpppcstatsreq creq
;
181 memset (&creq
, 0, sizeof (creq
));
184 creq
.stats_ptr
= (caddr_t
) &creq
.stats
;
186 #define ifr_name ifr__name
189 strncpy(creq
.ifr_name
, interface
, sizeof(creq
.ifr_name
));
190 if (ioctl(s
, SIOCGPPPCSTATS
, &creq
) < 0) {
191 fprintf(stderr
, "%s: ", progname
);
192 if (errno
== ENOTTY
) {
193 fprintf(stderr
, "no kernel compression support\n");
198 perror("couldn't get PPP compression stats");
204 if (creq
.stats
.c
.bytes_out
== 0) {
205 creq
.stats
.c
.bytes_out
= creq
.stats
.c
.comp_bytes
+ creq
.stats
.c
.inc_bytes
;
206 creq
.stats
.c
.in_count
= creq
.stats
.c
.unc_bytes
;
208 if (creq
.stats
.c
.bytes_out
== 0)
209 creq
.stats
.c
.ratio
= 0.0;
211 creq
.stats
.c
.ratio
= 256.0 * creq
.stats
.c
.in_count
/
212 creq
.stats
.c
.bytes_out
;
214 if (creq
.stats
.d
.bytes_out
== 0) {
215 creq
.stats
.d
.bytes_out
= creq
.stats
.d
.comp_bytes
+ creq
.stats
.d
.inc_bytes
;
216 creq
.stats
.d
.in_count
= creq
.stats
.d
.unc_bytes
;
218 if (creq
.stats
.d
.bytes_out
== 0)
219 creq
.stats
.d
.ratio
= 0.0;
221 creq
.stats
.d
.ratio
= 256.0 * creq
.stats
.d
.in_count
/
222 creq
.stats
.d
.bytes_out
;
231 strioctl(fd
, cmd
, ptr
, ilen
, olen
)
232 int fd
, cmd
, ilen
, olen
;
241 if (ioctl(fd
, I_STR
, &str
) == -1)
243 if (str
.ic_len
!= olen
)
244 fprintf(stderr
, "strioctl: expected %d bytes, got %d for cmd %x\n",
245 olen
, str
.ic_len
, cmd
);
251 struct ppp_stats
*curp
;
253 if (strioctl(s
, PPPIO_GETSTAT
, curp
, 0, sizeof(*curp
)) < 0) {
254 fprintf(stderr
, "%s: ", progname
);
256 fprintf(stderr
, "kernel support missing\n");
258 perror("couldn't get PPP statistics");
265 struct ppp_comp_stats
*csp
;
267 if (strioctl(s
, PPPIO_GETCSTAT
, csp
, 0, sizeof(*csp
)) < 0) {
268 fprintf(stderr
, "%s: ", progname
);
269 if (errno
== ENOTTY
) {
270 fprintf(stderr
, "no kernel compression support\n");
275 perror("couldn't get PPP compression statistics");
283 #define MAX0(a) ((int)(a) > 0? (a): 0)
284 #define V(offset) MAX0(cur.offset - old.offset)
285 #define W(offset) MAX0(ccs.offset - ocs.offset)
287 #define RATIO(c, i, u) ((c) == 0? 1.0: (u) / ((double)(c) + (i)))
288 #define CRATE(x) RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
290 #define KBPS(n) ((n) / (interval * 1000.0))
293 * Print a running summary of interface statistics.
294 * Repeat display every interval seconds, showing statistics
295 * collected over that interval. Assumes that interval is non-zero.
296 * First line printed is cumulative.
301 register int line
= 0;
302 sigset_t oldmask
, mask
;
305 struct ppp_stats cur
, old
;
306 struct ppp_comp_stats ccs
, ocs
;
308 memset(&ccs
, 0, sizeof(ccs
)); /* XXX gcc */
309 memset(&old
, 0, sizeof(old
));
310 memset(&ocs
, 0, sizeof(ocs
));
315 get_ppp_cstats(&ccs
);
317 (void)signal(SIGALRM
, catchalarm
);
319 (void)alarm(interval
);
321 if ((line
% 20) == 0) {
323 printf("IN: COMPRESSED INCOMPRESSIBLE COMP | ");
324 printf("OUT: COMPRESSED INCOMPRESSIBLE COMP\n");
325 bunit
= dflag
? "KB/S": "BYTE";
326 printf(" %s PACK %s PACK RATIO | ", bunit
, bunit
);
327 printf(" %s PACK %s PACK RATIO", bunit
, bunit
);
329 printf("%8.8s %6.6s %6.6s",
330 "IN", "PACK", "VJCOMP");
333 printf(" %6.6s %6.6s", "VJUNC", "VJERR");
335 printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
337 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
338 printf(" | %8.8s %6.6s %6.6s",
339 "OUT", "PACK", "VJCOMP");
342 printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
344 printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
346 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
353 printf("%8.3f %6u %8.3f %6u %6.2f",
354 KBPS(W(d
.comp_bytes
)),
356 KBPS(W(d
.inc_bytes
)),
358 ccs
.d
.ratio
/ 256.0);
359 printf(" | %8.3f %6u %8.3f %6u %6.2f",
360 KBPS(W(c
.comp_bytes
)),
362 KBPS(W(c
.inc_bytes
)),
364 ccs
.c
.ratio
/ 256.0);
366 printf("%8u %6u %8u %6u %6.2f",
371 ccs
.d
.ratio
/ 256.0);
372 printf(" | %8u %6u %8u %6u %6.2f",
377 ccs
.c
.ratio
/ 256.0);
382 printf("%8.3f", KBPS(V(p
.ppp_ibytes
)));
384 printf("%8u", V(p
.ppp_ibytes
));
387 V(vj
.vjs_compressedin
));
390 V(vj
.vjs_uncompressedin
),
395 V(p
.ppp_ipackets
) - V(vj
.vjs_compressedin
)
396 - V(vj
.vjs_uncompressedin
) - V(vj
.vjs_errorin
));
398 printf(" %6.2f ", CRATE(d
));
400 printf("%6.2f", KBPS(W(d
.unc_bytes
)));
402 printf("%6u", W(d
.unc_bytes
));
405 printf(" | %8.3f", KBPS(V(p
.ppp_obytes
)));
407 printf(" | %8u", V(p
.ppp_obytes
));
410 V(vj
.vjs_compressed
));
413 V(vj
.vjs_packets
) - V(vj
.vjs_compressed
),
414 V(p
.ppp_opackets
) - V(vj
.vjs_packets
));
420 printf(" %6.2f ", CRATE(c
));
422 printf("%6.2f", KBPS(W(c
.unc_bytes
)));
424 printf("%6u", W(c
.unc_bytes
));
434 if (!infinite
&& !count
)
438 sigaddset(&mask
, SIGALRM
);
439 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
444 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
446 (void)alarm(interval
);
467 if ((progname
= strrchr(argv
[0], '/')) == NULL
)
472 if (strncmp(progname
, SL_DRV_NAME
, sizeof(SL_DRV_NAME
) - 1) == 0) {
473 interface
= SL_DRV_NAME
"0";
474 fmt
= SL_DRV_NAME
"%d";
476 interface
= PPP_DRV_NAME
"0";
477 fmt
= PPP_DRV_NAME
"%d";
480 while ((c
= getopt(argc
, argv
, "advrzc:w:")) != -1) {
498 count
= atoi(optarg
);
503 interval
= atoi(optarg
);
514 if (!interval
&& count
)
516 if (interval
&& !count
)
518 if (!interval
&& !count
)
528 if (sscanf(interface
, fmt
, &unit
) != 1) {
529 fprintf(stderr
, "%s: invalid interface '%s' specified\n",
530 progname
, interface
);
537 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
539 fprintf(stderr
, "%s: ", progname
);
540 perror("couldn't create IP socket");
546 #define ifr_name ifr_ifrn.ifrn_name
548 strncpy(ifr
.ifr_name
, interface
, sizeof(ifr
.ifr_name
));
549 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
) < 0) {
550 fprintf(stderr
, "%s: nonexistent interface '%s' specified\n",
551 progname
, interface
);
558 dev
= "/dev/streams/ppp";
560 dev
= "/dev/" PPP_DRV_NAME
;
562 if ((s
= open(dev
, O_RDONLY
)) < 0) {
563 fprintf(stderr
, "%s: couldn't open ", progname
);
567 if (strioctl(s
, PPPIO_ATTACH
, &unit
, sizeof(int), 0) < 0) {
568 fprintf(stderr
, "%s: ppp%d is not available\n", progname
, unit
);