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.23 1998/07/07 04:23:04 paulus Exp $";
47 #include <sys/param.h>
48 #include <sys/types.h>
49 #include <sys/ioctl.h>
52 #include <sys/socket.h> /* *BSD, Linux, NeXT, Ultrix etc. */
55 #include <net/ppp_defs.h>
56 #include <net/if_ppp.h>
60 #include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
63 #include <linux/types.h>
66 #include <linux/ppp_defs.h>
67 #include <linux/if_ppp.h>
71 #include <sys/stropts.h> /* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
72 #include <net/ppp_defs.h>
73 #include <net/pppio.h>
77 int vflag, rflag, zflag; /* select type of display */
78 int aflag; /* print absolute values, not deltas */
79 int dflag; /* print data rates, not bytes */
83 int s; /* socket or /dev/ppp file descriptor */
84 int signalled; /* set if alarm goes off "early" */
88 #if defined(SUNOS4) || defined(ULTRIX) || defined(NeXT)
93 static void usage __P((void));
94 static void catchalarm __P((int));
95 static void get_ppp_stats __P((struct ppp_stats *));
96 static void get_ppp_cstats __P((struct ppp_comp_stats *));
97 static void intpr __P((void));
99 int main __P((int, char *argv[]));
104 fprintf(stderr, "Usage: %s [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]\n",
110 * Called if an interval expires before intpr has completed a loop.
111 * Sets a flag to not wait for the alarm.
124 struct ppp_stats *curp;
126 struct ifpppstatsreq req;
128 memset (&req, 0, sizeof (req));
131 req.stats_ptr = (caddr_t) &req.stats;
133 #define ifr_name ifr__name
136 strncpy(req.ifr_name, interface, sizeof(req.ifr_name));
137 if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
138 fprintf(stderr, "%s: ", progname);
140 fprintf(stderr, "kernel support missing\n");
142 perror("couldn't get PPP statistics");
150 struct ppp_comp_stats *csp;
152 struct ifpppcstatsreq creq;
154 memset (&creq, 0, sizeof (creq));
157 creq.stats_ptr = (caddr_t) &creq.stats;
159 #define ifr_name ifr__name
162 strncpy(creq.ifr_name, interface, sizeof(creq.ifr_name));
163 if (ioctl(s, SIOCGPPPCSTATS, &creq) < 0) {
164 fprintf(stderr, "%s: ", progname);
165 if (errno == ENOTTY) {
166 fprintf(stderr, "no kernel compression support\n");
171 perror("couldn't get PPP compression stats");
177 if (creq.stats.c.bytes_out == 0) {
178 creq.stats.c.bytes_out = creq.stats.c.comp_bytes + creq.stats.c.inc_bytes;
179 creq.stats.c.in_count = creq.stats.c.unc_bytes;
181 if (creq.stats.c.bytes_out == 0)
182 creq.stats.c.ratio = 0.0;
184 creq.stats.c.ratio = 256.0 * creq.stats.c.in_count /
185 creq.stats.c.bytes_out;
187 if (creq.stats.d.bytes_out == 0) {
188 creq.stats.d.bytes_out = creq.stats.d.comp_bytes + creq.stats.d.inc_bytes;
189 creq.stats.d.in_count = creq.stats.d.unc_bytes;
191 if (creq.stats.d.bytes_out == 0)
192 creq.stats.d.ratio = 0.0;
194 creq.stats.d.ratio = 256.0 * creq.stats.d.in_count /
195 creq.stats.d.bytes_out;
204 strioctl(fd, cmd, ptr, ilen, olen)
205 int fd, cmd, ilen, olen;
214 if (ioctl(fd, I_STR, &str) == -1)
216 if (str.ic_len != olen)
217 fprintf(stderr, "strioctl: expected %d bytes, got %d for cmd %x\n",
218 olen, str.ic_len, cmd);
224 struct ppp_stats *curp;
226 if (strioctl(s, PPPIO_GETSTAT, curp, 0, sizeof(*curp)) < 0) {
227 fprintf(stderr, "%s: ", progname);
229 fprintf(stderr, "kernel support missing\n");
231 perror("couldn't get PPP statistics");
238 struct ppp_comp_stats *csp;
240 if (strioctl(s, PPPIO_GETCSTAT, csp, 0, sizeof(*csp)) < 0) {
241 fprintf(stderr, "%s: ", progname);
242 if (errno == ENOTTY) {
243 fprintf(stderr, "no kernel compression support\n");
248 perror("couldn't get PPP compression statistics");
256 #define MAX0(a) ((int)(a) > 0? (a): 0)
257 #define V(offset) MAX0(cur.offset - old.offset)
258 #define W(offset) MAX0(ccs.offset - ocs.offset)
260 #define RATIO(c, i, u) ((c) == 0? 1.0: (u) / ((double)(c) + (i)))
261 #define CRATE(x) RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
263 #define KBPS(n) ((n) / (interval * 1000.0))
266 * Print a running summary of interface statistics.
267 * Repeat display every interval seconds, showing statistics
268 * collected over that interval. Assumes that interval is non-zero.
269 * First line printed is cumulative.
274 register int line = 0;
275 sigset_t oldmask, mask;
278 struct ppp_stats cur, old;
279 struct ppp_comp_stats ccs, ocs;
281 memset(&old, 0, sizeof(old));
282 memset(&ocs, 0, sizeof(ocs));
287 get_ppp_cstats(&ccs);
289 (void)signal(SIGALRM, catchalarm);
291 (void)alarm(interval);
293 if ((line % 20) == 0) {
295 printf("IN: COMPRESSED INCOMPRESSIBLE COMP | ");
296 printf("OUT: COMPRESSED INCOMPRESSIBLE COMP\n");
297 bunit = dflag? "KB/S": "BYTE";
298 printf(" %s PACK %s PACK RATIO | ", bunit, bunit);
299 printf(" %s PACK %s PACK RATIO", bunit, bunit);
301 printf("%8.8s %6.6s %6.6s",
302 "IN", "PACK", "VJCOMP");
305 printf(" %6.6s %6.6s", "VJUNC", "VJERR");
307 printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
309 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
310 printf(" | %8.8s %6.6s %6.6s",
311 "OUT", "PACK", "VJCOMP");
314 printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
316 printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
318 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
325 printf("%8.3f %6u %8.3f %6u %6.2f",
326 KBPS(W(d.comp_bytes)),
328 KBPS(W(d.inc_bytes)),
330 ccs.d.ratio / 256.0);
331 printf(" | %8.3f %6u %8.3f %6u %6.2f",
332 KBPS(W(c.comp_bytes)),
334 KBPS(W(c.inc_bytes)),
336 ccs.c.ratio / 256.0);
338 printf("%8u %6u %8u %6u %6.2f",
343 ccs.d.ratio / 256.0);
344 printf(" | %8u %6u %8u %6u %6.2f",
349 ccs.c.ratio / 256.0);
354 printf("%8.3f", KBPS(V(p.ppp_ibytes)));
356 printf("%8u", V(p.ppp_ibytes));
359 V(vj.vjs_compressedin));
362 V(vj.vjs_uncompressedin),
367 V(p.ppp_ipackets) - V(vj.vjs_compressedin)
368 - V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
370 printf(" %6.2f ", CRATE(d));
372 printf("%6.2f", KBPS(W(d.unc_bytes)));
374 printf("%6u", W(d.unc_bytes));
377 printf(" | %8.3f", KBPS(V(p.ppp_obytes)));
379 printf(" | %8u", V(p.ppp_obytes));
382 V(vj.vjs_compressed));
385 V(vj.vjs_packets) - V(vj.vjs_compressed),
386 V(p.ppp_opackets) - V(vj.vjs_packets));
392 printf(" %6.2f ", CRATE(c));
394 printf("%6.2f", KBPS(W(c.unc_bytes)));
396 printf("%6u", W(c.unc_bytes));
406 if (!infinite && !count)
410 sigaddset(&mask, SIGALRM);
411 sigprocmask(SIG_BLOCK, &mask, &oldmask);
416 sigprocmask(SIG_SETMASK, &oldmask, NULL);
418 (void)alarm(interval);
439 if ((progname = strrchr(argv[0], '/')) == NULL)
444 while ((c = getopt(argc, argv, "advrzc:w:")) != -1) {
462 count = atoi(optarg);
467 interval = atoi(optarg);
478 if (!interval && count)
480 if (interval && !count)
482 if (!interval && !count)
492 if (sscanf(interface, "ppp%d", &unit) != 1) {
493 fprintf(stderr, "%s: invalid interface '%s' specified\n",
494 progname, interface);
501 s = socket(AF_INET, SOCK_DGRAM, 0);
503 fprintf(stderr, "%s: ", progname);
504 perror("couldn't create IP socket");
510 #define ifr_name ifr_ifrn.ifrn_name
512 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
513 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
514 fprintf(stderr, "%s: nonexistent interface '%s' specified\n",
515 progname, interface);
522 dev = "/dev/streams/ppp";
526 if ((s = open(dev, O_RDONLY)) < 0) {
527 fprintf(stderr, "%s: couldn't open ", progname);
531 if (strioctl(s, PPPIO_ATTACH, &unit, sizeof(int), 0) < 0) {
532 fprintf(stderr, "%s: ppp%d is not available\n", progname, unit);