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.15 1996/08/28 06:43:30 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. */
55 #include <net/if_ppp.h>
58 #include <sys/stropts.h> /* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
59 #include <net/pppio.h>
63 int vflag, rflag, zflag; /* select type of display */
64 int aflag; /* print absolute values, not deltas */
65 int dflag; /* print data rates, not bytes */
69 int s; /* socket or /dev/ppp file descriptor */
70 int signalled; /* set if alarm goes off "early" */
77 fprintf(stderr, "Usage: %s [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]\n",
83 * Called if an interval expires before intpr has completed a loop.
84 * Sets a flag to not wait for the alarm.
97 struct ppp_stats *curp;
99 struct ifpppstatsreq req;
101 memset (&req, 0, sizeof (req));
104 req.stats_ptr = (caddr_t) &req.stats;
106 #define ifr_name ifr__name
109 strncpy(req.ifr_name, interface, sizeof(req.ifr_name));
110 if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
111 fprintf(stderr, "%s: ", progname);
113 fprintf(stderr, "kernel support missing\n");
115 perror("couldn't get PPP statistics");
123 struct ppp_comp_stats *csp;
125 struct ifpppcstatsreq creq;
127 memset (&creq, 0, sizeof (creq));
130 creq.stats_ptr = (caddr_t) &creq.stats;
132 #define ifr_name ifr__name
135 strncpy(creq.ifr_name, interface, sizeof(creq.ifr_name));
136 if (ioctl(s, SIOCGPPPCSTATS, &creq) < 0) {
137 fprintf(stderr, "%s: ", progname);
138 if (errno == ENOTTY) {
139 fprintf(stderr, "no kernel compression support\n");
144 perror("couldn't get PPP compression stats");
150 if (creq.stats.c.bytes_out == 0)
151 creq.stats.c.ratio = 0.0;
153 creq.stats.c.ratio = (double) creq.stats.c.in_count /
154 (double) creq.stats.c.bytes_out;
156 if (creq.stats.d.bytes_out == 0)
157 creq.stats.d.ratio = 0.0;
159 creq.stats.d.ratio = (double) creq.stats.d.in_count /
160 (double) creq.stats.d.bytes_out;
169 strioctl(fd, cmd, ptr, ilen, olen)
170 int fd, cmd, ilen, olen;
179 if (ioctl(fd, I_STR, &str) == -1)
181 if (str.ic_len != olen)
182 fprintf(stderr, "strioctl: expected %d bytes, got %d for cmd %x\n",
183 olen, str.ic_len, cmd);
189 struct ppp_stats *curp;
191 if (strioctl(s, PPPIO_GETSTAT, curp, 0, sizeof(*curp)) < 0) {
192 fprintf(stderr, "%s: ", progname);
194 fprintf(stderr, "kernel support missing\n");
196 perror("couldn't get PPP statistics");
203 struct ppp_comp_stats *csp;
205 if (strioctl(s, PPPIO_GETCSTAT, csp, 0, sizeof(*csp)) < 0) {
206 fprintf(stderr, "%s: ", progname);
207 if (errno == ENOTTY) {
208 fprintf(stderr, "no kernel compression support\n");
213 perror("couldn't get PPP compression statistics");
221 #define MAX0(a) ((int)(a) > 0? (a): 0)
222 #define V(offset) MAX0(cur.offset - old.offset)
223 #define W(offset) MAX0(ccs.offset - ocs.offset)
225 #define RATIO(c, i, u) ((c) == 0? 1.0: (u) / ((double)(c) + (i)))
226 #define CRATE(x) RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
228 #define KBPS(n) ((n) / (interval * 1000.0))
231 * Print a running summary of interface statistics.
232 * Repeat display every interval seconds, showing statistics
233 * collected over that interval. Assumes that interval is non-zero.
234 * First line printed is cumulative.
239 register int line = 0;
240 sigset_t oldmask, mask;
243 struct ppp_stats cur, old;
244 struct ppp_comp_stats ccs, ocs;
246 memset(&old, 0, sizeof(old));
247 memset(&ocs, 0, sizeof(ocs));
252 get_ppp_cstats(&ccs);
254 (void)signal(SIGALRM, catchalarm);
256 (void)alarm(interval);
258 if ((line % 20) == 0) {
260 printf("IN: COMPRESSED INCOMPRESSIBLE COMP | ");
261 printf("OUT: COMPRESSED INCOMPRESSIBLE COMP\n");
262 bunit = dflag? "KB/S": "BYTE";
263 printf(" %s PACK %s PACK RATIO | ", bunit, bunit);
264 printf(" %s PACK %s PACK RATIO", bunit, bunit);
266 printf("%8.8s %6.6s %6.6s",
267 "IN", "PACK", "VJCOMP");
270 printf(" %6.6s %6.6s", "VJUNC", "VJERR");
272 printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
274 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
275 printf(" | %8.8s %6.6s %6.6s",
276 "OUT", "PACK", "VJCOMP");
279 printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
281 printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
283 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
290 printf("%8.3f %6u %8.3f %6u %6.2f",
291 KBPS(W(d.comp_bytes)),
293 KBPS(W(d.inc_bytes)),
295 ccs.d.ratio * 256.0);
296 printf(" | %8.3f %6u %8.3f %6u %6.2f",
297 KBPS(W(c.comp_bytes)),
299 KBPS(W(c.inc_bytes)),
301 ccs.c.ratio * 256.0);
303 printf("%8u %6u %8u %6u %6.2f",
308 ccs.d.ratio * 256.0);
309 printf(" | %8u %6u %8u %6u %6.2f",
314 ccs.c.ratio * 256.0);
319 printf("%8.3f", KBPS(V(p.ppp_ibytes)));
321 printf("%8u", V(p.ppp_ibytes));
324 V(vj.vjs_compressedin));
327 V(vj.vjs_uncompressedin),
332 V(p.ppp_ipackets) - V(vj.vjs_compressedin)
333 - V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
335 printf(" %6.2f ", CRATE(d));
337 printf("%6.2f", KBPS(W(d.unc_bytes)));
339 printf("%6u", W(d.unc_bytes));
342 printf(" | %8.3f", KBPS(V(p.ppp_obytes)));
344 printf(" | %8u", V(p.ppp_obytes));
348 V(vj.vjs_compressed));
351 V(vj.vjs_packets) - V(vj.vjs_compressed),
352 V(p.ppp_opackets) - V(vj.vjs_packets));
358 printf(" %6.2f ", CRATE(c));
360 printf("%6.2f", KBPS(W(c.unc_bytes)));
362 printf("%6u", W(c.unc_bytes));
372 if (!infinite && !count)
376 sigaddset(&mask, SIGALRM);
377 sigprocmask(SIG_BLOCK, &mask, &oldmask);
382 sigprocmask(SIG_SETMASK, &oldmask, NULL);
384 (void)alarm(interval);
405 if ((progname = strrchr(argv[0], '/')) == NULL)
410 while ((c = getopt(argc, argv, "advrzc:w:")) != -1) {
428 count = atoi(optarg);
433 interval = atoi(optarg);
444 if (!interval && count)
446 if (interval && !count)
448 if (!interval && !count)
458 if (sscanf(interface, "ppp%d", &unit) != 1) {
459 fprintf(stderr, "%s: invalid interface '%s' specified\n",
460 progname, interface);
467 s = socket(AF_INET, SOCK_DGRAM, 0);
469 fprintf(stderr, "%s: ", progname);
470 perror("couldn't create IP socket");
476 #define ifr_name ifr_ifrn.ifrn_name
478 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
479 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
480 fprintf(stderr, "%s: nonexistent interface '%s' specified\n",
481 progname, interface);
488 dev = "/dev/streams/ppp";
492 if ((s = open(dev, O_RDONLY)) < 0) {
493 fprintf(stderr, "%s: couldn't open ", progname);
497 if (strioctl(s, PPPIO_ATTACH, &unit, sizeof(int), 0) < 0) {
498 fprintf(stderr, "%s: ppp%d is not available\n", progname, unit);