2 * print PPP statistics:
3 * pppstats [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]
5 * -a Show absolute values rather than deltas
6 * -d Show data rate (kB/s) rather than bytes
7 * -v Show more stats for VJ TCP header compression
8 * -r Show compression ratio
9 * -z Show compression statistics instead of default display
12 * perkins@cps.msu.edu: Added compression statistics and alternate
14 * Brad Parker (brad@cayman.com) 6/92
16 * from the original "slstats" by Van Jacobson
18 * Copyright (c) 1989 Regents of the University of California.
19 * All rights reserved.
21 * Redistribution and use in source and binary forms are permitted
22 * provided that the above copyright notice and this paragraph are
23 * duplicated in all such forms and that any documentation,
24 * advertising materials, and other materials related to such
25 * distribution and use acknowledge that the software was developed
26 * by the University of California, Berkeley. The name of the
27 * University may not be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
31 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
39 static const char rcsid
[] = "$Id: pppstats.c,v 1.29 2002/10/27 12:56:26 fcusack Exp $";
51 #include <sys/param.h>
52 #include <sys/types.h>
53 #include <sys/ioctl.h>
56 #if defined(__linux__) && defined(__powerpc__) \
57 && (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
61 #include <sys/socket.h> /* *BSD, Linux, NeXT, Ultrix etc. */
64 #include <net/ppp_defs.h>
65 #include <net/if_ppp.h>
69 #include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
72 #include <linux/types.h>
75 #include <linux/ppp_defs.h>
76 #include <linux/if_ppp.h>
77 #endif /* __linux__ */
80 #include <sys/stropts.h> /* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
81 #include <net/ppp_defs.h>
82 #include <net/pppio.h>
86 int vflag
, rflag
, zflag
; /* select type of display */
87 int aflag
; /* print absolute values, not deltas */
88 int dflag
; /* print data rates, not bytes */
92 int s
; /* socket or /dev/ppp file descriptor */
93 int signalled
; /* set if alarm goes off "early" */
97 #if defined(SUNOS4) || defined(ULTRIX) || defined(NeXT)
103 * If PPP_DRV_NAME is not defined, use the legacy "ppp" as the
106 #if !defined(PPP_DRV_NAME)
107 #define PPP_DRV_NAME "ppp"
108 #endif /* !defined(PPP_DRV_NAME) */
110 static void usage
__P((void));
111 static void catchalarm
__P((int));
112 static void get_ppp_stats
__P((struct ppp_stats
*));
113 static void get_ppp_cstats
__P((struct ppp_comp_stats
*));
114 static void intpr
__P((void));
116 int main
__P((int, char *argv
[]));
121 fprintf(stderr
, "Usage: %s [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]\n",
127 * Called if an interval expires before intpr has completed a loop.
128 * Sets a flag to not wait for the alarm.
141 struct ppp_stats
*curp
;
143 struct ifpppstatsreq req
;
145 memset (&req
, 0, sizeof (req
));
148 req
.stats_ptr
= (caddr_t
) &req
.stats
;
150 #define ifr_name ifr__name
153 strncpy(req
.ifr_name
, interface
, sizeof(req
.ifr_name
));
154 if (ioctl(s
, SIOCGPPPSTATS
, &req
) < 0) {
155 fprintf(stderr
, "%s: ", progname
);
157 fprintf(stderr
, "kernel support missing\n");
159 perror("couldn't get PPP statistics");
167 struct ppp_comp_stats
*csp
;
169 struct ifpppcstatsreq creq
;
171 memset (&creq
, 0, sizeof (creq
));
174 creq
.stats_ptr
= (caddr_t
) &creq
.stats
;
176 #define ifr_name ifr__name
179 strncpy(creq
.ifr_name
, interface
, sizeof(creq
.ifr_name
));
180 if (ioctl(s
, SIOCGPPPCSTATS
, &creq
) < 0) {
181 fprintf(stderr
, "%s: ", progname
);
182 if (errno
== ENOTTY
) {
183 fprintf(stderr
, "no kernel compression support\n");
188 perror("couldn't get PPP compression stats");
194 if (creq
.stats
.c
.bytes_out
== 0) {
195 creq
.stats
.c
.bytes_out
= creq
.stats
.c
.comp_bytes
+ creq
.stats
.c
.inc_bytes
;
196 creq
.stats
.c
.in_count
= creq
.stats
.c
.unc_bytes
;
198 if (creq
.stats
.c
.bytes_out
== 0)
199 creq
.stats
.c
.ratio
= 0.0;
201 creq
.stats
.c
.ratio
= 256.0 * creq
.stats
.c
.in_count
/
202 creq
.stats
.c
.bytes_out
;
204 if (creq
.stats
.d
.bytes_out
== 0) {
205 creq
.stats
.d
.bytes_out
= creq
.stats
.d
.comp_bytes
+ creq
.stats
.d
.inc_bytes
;
206 creq
.stats
.d
.in_count
= creq
.stats
.d
.unc_bytes
;
208 if (creq
.stats
.d
.bytes_out
== 0)
209 creq
.stats
.d
.ratio
= 0.0;
211 creq
.stats
.d
.ratio
= 256.0 * creq
.stats
.d
.in_count
/
212 creq
.stats
.d
.bytes_out
;
221 strioctl(fd
, cmd
, ptr
, ilen
, olen
)
222 int fd
, cmd
, ilen
, olen
;
231 if (ioctl(fd
, I_STR
, &str
) == -1)
233 if (str
.ic_len
!= olen
)
234 fprintf(stderr
, "strioctl: expected %d bytes, got %d for cmd %x\n",
235 olen
, str
.ic_len
, cmd
);
241 struct ppp_stats
*curp
;
243 if (strioctl(s
, PPPIO_GETSTAT
, curp
, 0, sizeof(*curp
)) < 0) {
244 fprintf(stderr
, "%s: ", progname
);
246 fprintf(stderr
, "kernel support missing\n");
248 perror("couldn't get PPP statistics");
255 struct ppp_comp_stats
*csp
;
257 if (strioctl(s
, PPPIO_GETCSTAT
, csp
, 0, sizeof(*csp
)) < 0) {
258 fprintf(stderr
, "%s: ", progname
);
259 if (errno
== ENOTTY
) {
260 fprintf(stderr
, "no kernel compression support\n");
265 perror("couldn't get PPP compression statistics");
273 #define MAX0(a) ((int)(a) > 0? (a): 0)
274 #define V(offset) MAX0(cur.offset - old.offset)
275 #define W(offset) MAX0(ccs.offset - ocs.offset)
277 #define RATIO(c, i, u) ((c) == 0? 1.0: (u) / ((double)(c) + (i)))
278 #define CRATE(x) RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
280 #define KBPS(n) ((n) / (interval * 1000.0))
283 * Print a running summary of interface statistics.
284 * Repeat display every interval seconds, showing statistics
285 * collected over that interval. Assumes that interval is non-zero.
286 * First line printed is cumulative.
291 register int line
= 0;
292 sigset_t oldmask
, mask
;
295 struct ppp_stats cur
, old
;
296 struct ppp_comp_stats ccs
, ocs
;
298 memset(&old
, 0, sizeof(old
));
299 memset(&ocs
, 0, sizeof(ocs
));
304 get_ppp_cstats(&ccs
);
306 (void)signal(SIGALRM
, catchalarm
);
308 (void)alarm(interval
);
310 if ((line
% 20) == 0) {
312 printf("IN: COMPRESSED INCOMPRESSIBLE COMP | ");
313 printf("OUT: COMPRESSED INCOMPRESSIBLE COMP\n");
314 bunit
= dflag
? "KB/S": "BYTE";
315 printf(" %s PACK %s PACK RATIO | ", bunit
, bunit
);
316 printf(" %s PACK %s PACK RATIO", bunit
, bunit
);
318 printf("%8.8s %6.6s %6.6s",
319 "IN", "PACK", "VJCOMP");
322 printf(" %6.6s %6.6s", "VJUNC", "VJERR");
324 printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
326 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
327 printf(" | %8.8s %6.6s %6.6s",
328 "OUT", "PACK", "VJCOMP");
331 printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
333 printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
335 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
342 printf("%8.3f %6u %8.3f %6u %6.2f",
343 KBPS(W(d
.comp_bytes
)),
345 KBPS(W(d
.inc_bytes
)),
347 ccs
.d
.ratio
/ 256.0);
348 printf(" | %8.3f %6u %8.3f %6u %6.2f",
349 KBPS(W(c
.comp_bytes
)),
351 KBPS(W(c
.inc_bytes
)),
353 ccs
.c
.ratio
/ 256.0);
355 printf("%8u %6u %8u %6u %6.2f",
360 ccs
.d
.ratio
/ 256.0);
361 printf(" | %8u %6u %8u %6u %6.2f",
366 ccs
.c
.ratio
/ 256.0);
371 printf("%8.3f", KBPS(V(p
.ppp_ibytes
)));
373 printf("%8u", V(p
.ppp_ibytes
));
376 V(vj
.vjs_compressedin
));
379 V(vj
.vjs_uncompressedin
),
384 V(p
.ppp_ipackets
) - V(vj
.vjs_compressedin
)
385 - V(vj
.vjs_uncompressedin
) - V(vj
.vjs_errorin
));
387 printf(" %6.2f ", CRATE(d
));
389 printf("%6.2f", KBPS(W(d
.unc_bytes
)));
391 printf("%6u", W(d
.unc_bytes
));
394 printf(" | %8.3f", KBPS(V(p
.ppp_obytes
)));
396 printf(" | %8u", V(p
.ppp_obytes
));
399 V(vj
.vjs_compressed
));
402 V(vj
.vjs_packets
) - V(vj
.vjs_compressed
),
403 V(p
.ppp_opackets
) - V(vj
.vjs_packets
));
409 printf(" %6.2f ", CRATE(c
));
411 printf("%6.2f", KBPS(W(c
.unc_bytes
)));
413 printf("%6u", W(c
.unc_bytes
));
423 if (!infinite
&& !count
)
427 sigaddset(&mask
, SIGALRM
);
428 sigprocmask(SIG_BLOCK
, &mask
, &oldmask
);
433 sigprocmask(SIG_SETMASK
, &oldmask
, NULL
);
435 (void)alarm(interval
);
455 interface
= PPP_DRV_NAME
"0";
456 if ((progname
= strrchr(argv
[0], '/')) == NULL
)
461 while ((c
= getopt(argc
, argv
, "advrzc:w:")) != -1) {
479 count
= atoi(optarg
);
484 interval
= atoi(optarg
);
495 if (!interval
&& count
)
497 if (interval
&& !count
)
499 if (!interval
&& !count
)
509 if (sscanf(interface
, PPP_DRV_NAME
"%d", &unit
) != 1) {
510 fprintf(stderr
, "%s: invalid interface '%s' specified\n",
511 progname
, interface
);
518 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
520 fprintf(stderr
, "%s: ", progname
);
521 perror("couldn't create IP socket");
527 #define ifr_name ifr_ifrn.ifrn_name
529 strncpy(ifr
.ifr_name
, interface
, sizeof(ifr
.ifr_name
));
530 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
) < 0) {
531 fprintf(stderr
, "%s: nonexistent interface '%s' specified\n",
532 progname
, interface
);
539 dev
= "/dev/streams/ppp";
541 dev
= "/dev/" PPP_DRV_NAME
;
543 if ((s
= open(dev
, O_RDONLY
)) < 0) {
544 fprintf(stderr
, "%s: couldn't open ", progname
);
548 if (strioctl(s
, PPPIO_ATTACH
, &unit
, sizeof(int), 0) < 0) {
549 fprintf(stderr
, "%s: ppp%d is not available\n", progname
, unit
);