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.
35 static char rcsid[] = "$Id: pppstats.c,v 1.19 1997/04/30 06:00:27 paulus Exp $";
47 #include <sys/param.h>
48 #include <sys/types.h>
49 #include <sys/ioctl.h>
50 #include <net/ppp_defs.h>
53 #include <sys/socket.h> /* *BSD, Linux, NeXT, Ultrix etc. */
56 #include <net/if_ppp.h>
58 #include <net/if_ppp.h>
62 #include <sys/stropts.h> /* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
63 #include <net/pppio.h>
67 int vflag, rflag, zflag; /* select type of display */
68 int aflag; /* print absolute values, not deltas */
69 int dflag; /* print data rates, not bytes */
73 int s; /* socket or /dev/ppp file descriptor */
74 int signalled; /* set if alarm goes off "early" */
78 #if defined(SUNOS4) || defined(ULTRIX) || defined(NeXT)
83 static void usage __P((void));
84 static void catchalarm __P((int));
85 static void get_ppp_stats __P((struct ppp_stats *));
86 static void get_ppp_cstats __P((struct ppp_comp_stats *));
87 static void intpr __P((void));
89 int main __P((int, char *argv[]));
94 fprintf(stderr, "Usage: %s [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]\n",
100 * Called if an interval expires before intpr has completed a loop.
101 * Sets a flag to not wait for the alarm.
114 struct ppp_stats *curp;
116 struct ifpppstatsreq req;
118 memset (&req, 0, sizeof (req));
121 req.stats_ptr = (caddr_t) &req.stats;
123 #define ifr_name ifr__name
126 strncpy(req.ifr_name, interface, sizeof(req.ifr_name));
127 if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
128 fprintf(stderr, "%s: ", progname);
130 fprintf(stderr, "kernel support missing\n");
132 perror("couldn't get PPP statistics");
140 struct ppp_comp_stats *csp;
142 struct ifpppcstatsreq creq;
144 memset (&creq, 0, sizeof (creq));
147 creq.stats_ptr = (caddr_t) &creq.stats;
149 #define ifr_name ifr__name
152 strncpy(creq.ifr_name, interface, sizeof(creq.ifr_name));
153 if (ioctl(s, SIOCGPPPCSTATS, &creq) < 0) {
154 fprintf(stderr, "%s: ", progname);
155 if (errno == ENOTTY) {
156 fprintf(stderr, "no kernel compression support\n");
161 perror("couldn't get PPP compression stats");
167 if (creq.stats.c.bytes_out == 0)
168 creq.stats.c.ratio = 0.0;
170 creq.stats.c.ratio = (double) creq.stats.c.in_count /
171 (double) creq.stats.c.bytes_out;
173 if (creq.stats.d.bytes_out == 0)
174 creq.stats.d.ratio = 0.0;
176 creq.stats.d.ratio = (double) creq.stats.d.in_count /
177 (double) creq.stats.d.bytes_out;
186 strioctl(fd, cmd, ptr, ilen, olen)
187 int fd, cmd, ilen, olen;
196 if (ioctl(fd, I_STR, &str) == -1)
198 if (str.ic_len != olen)
199 fprintf(stderr, "strioctl: expected %d bytes, got %d for cmd %x\n",
200 olen, str.ic_len, cmd);
206 struct ppp_stats *curp;
208 if (strioctl(s, PPPIO_GETSTAT, curp, 0, sizeof(*curp)) < 0) {
209 fprintf(stderr, "%s: ", progname);
211 fprintf(stderr, "kernel support missing\n");
213 perror("couldn't get PPP statistics");
220 struct ppp_comp_stats *csp;
222 if (strioctl(s, PPPIO_GETCSTAT, csp, 0, sizeof(*csp)) < 0) {
223 fprintf(stderr, "%s: ", progname);
224 if (errno == ENOTTY) {
225 fprintf(stderr, "no kernel compression support\n");
230 perror("couldn't get PPP compression statistics");
238 #define MAX0(a) ((int)(a) > 0? (a): 0)
239 #define V(offset) MAX0(cur.offset - old.offset)
240 #define W(offset) MAX0(ccs.offset - ocs.offset)
242 #define RATIO(c, i, u) ((c) == 0? 1.0: (u) / ((double)(c) + (i)))
243 #define CRATE(x) RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
245 #define KBPS(n) ((n) / (interval * 1000.0))
248 * Print a running summary of interface statistics.
249 * Repeat display every interval seconds, showing statistics
250 * collected over that interval. Assumes that interval is non-zero.
251 * First line printed is cumulative.
256 register int line = 0;
257 sigset_t oldmask, mask;
260 struct ppp_stats cur, old;
261 struct ppp_comp_stats ccs, ocs;
263 memset(&old, 0, sizeof(old));
264 memset(&ocs, 0, sizeof(ocs));
269 get_ppp_cstats(&ccs);
271 (void)signal(SIGALRM, catchalarm);
273 (void)alarm(interval);
275 if ((line % 20) == 0) {
277 printf("IN: COMPRESSED INCOMPRESSIBLE COMP | ");
278 printf("OUT: COMPRESSED INCOMPRESSIBLE COMP\n");
279 bunit = dflag? "KB/S": "BYTE";
280 printf(" %s PACK %s PACK RATIO | ", bunit, bunit);
281 printf(" %s PACK %s PACK RATIO", bunit, bunit);
283 printf("%8.8s %6.6s %6.6s",
284 "IN", "PACK", "VJCOMP");
287 printf(" %6.6s %6.6s", "VJUNC", "VJERR");
289 printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
291 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
292 printf(" | %8.8s %6.6s %6.6s",
293 "OUT", "PACK", "VJCOMP");
296 printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
298 printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
300 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
307 printf("%8.3f %6u %8.3f %6u %6.2f",
308 KBPS(W(d.comp_bytes)),
310 KBPS(W(d.inc_bytes)),
312 ccs.d.ratio * 256.0);
313 printf(" | %8.3f %6u %8.3f %6u %6.2f",
314 KBPS(W(c.comp_bytes)),
316 KBPS(W(c.inc_bytes)),
318 ccs.c.ratio * 256.0);
320 printf("%8u %6u %8u %6u %6.2f",
325 ccs.d.ratio * 256.0);
326 printf(" | %8u %6u %8u %6u %6.2f",
331 ccs.c.ratio * 256.0);
336 printf("%8.3f", KBPS(V(p.ppp_ibytes)));
338 printf("%8u", V(p.ppp_ibytes));
341 V(vj.vjs_compressedin));
344 V(vj.vjs_uncompressedin),
349 V(p.ppp_ipackets) - V(vj.vjs_compressedin)
350 - V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
352 printf(" %6.2f ", CRATE(d));
354 printf("%6.2f", KBPS(W(d.unc_bytes)));
356 printf("%6u", W(d.unc_bytes));
359 printf(" | %8.3f", KBPS(V(p.ppp_obytes)));
361 printf(" | %8u", V(p.ppp_obytes));
364 V(vj.vjs_compressed));
367 V(vj.vjs_packets) - V(vj.vjs_compressed),
368 V(p.ppp_opackets) - V(vj.vjs_packets));
374 printf(" %6.2f ", CRATE(c));
376 printf("%6.2f", KBPS(W(c.unc_bytes)));
378 printf("%6u", W(c.unc_bytes));
388 if (!infinite && !count)
392 sigaddset(&mask, SIGALRM);
393 sigprocmask(SIG_BLOCK, &mask, &oldmask);
398 sigprocmask(SIG_SETMASK, &oldmask, NULL);
400 (void)alarm(interval);
421 if ((progname = strrchr(argv[0], '/')) == NULL)
426 while ((c = getopt(argc, argv, "advrzc:w:")) != -1) {
444 count = atoi(optarg);
449 interval = atoi(optarg);
460 if (!interval && count)
462 if (interval && !count)
464 if (!interval && !count)
474 if (sscanf(interface, "ppp%d", &unit) != 1) {
475 fprintf(stderr, "%s: invalid interface '%s' specified\n",
476 progname, interface);
483 s = socket(AF_INET, SOCK_DGRAM, 0);
485 fprintf(stderr, "%s: ", progname);
486 perror("couldn't create IP socket");
492 #define ifr_name ifr_ifrn.ifrn_name
494 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
495 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
496 fprintf(stderr, "%s: nonexistent interface '%s' specified\n",
497 progname, interface);
504 dev = "/dev/streams/ppp";
508 if ((s = open(dev, O_RDONLY)) < 0) {
509 fprintf(stderr, "%s: couldn't open ", progname);
513 if (strioctl(s, PPPIO_ATTACH, &unit, sizeof(int), 0) < 0) {
514 fprintf(stderr, "%s: ppp%d is not available\n", progname, unit);