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[] = "$FreeBSD$";
47 #include <sys/param.h>
48 #include <sys/types.h>
49 #include <sys/ioctl.h>
51 #include <net/ppp_defs.h>
54 #include <sys/socket.h> /* *BSD, Linux, NeXT, Ultrix etc. */
56 #include <net/if_ppp.h>
59 #include <sys/stropts.h> /* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
60 #include <net/pppio.h>
64 int vflag, rflag, zflag; /* select type of display */
65 int aflag; /* print absolute values, not deltas */
66 int dflag; /* print data rates, not bytes */
70 int s; /* socket or /dev/ppp file descriptor */
71 int signalled; /* set if alarm goes off "early" */
75 #if defined(SUNOS4) || defined(ULTRIX) || defined(NeXT)
80 static void usage __P((void));
81 static void catchalarm __P((int));
82 static void get_ppp_stats __P((struct ppp_stats *));
83 static void get_ppp_cstats __P((struct ppp_comp_stats *));
84 static void intpr __P((void));
86 int main __P((int, char *argv[]));
91 fprintf(stderr, "usage: %s [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]\n",
97 * Called if an interval expires before intpr has completed a loop.
98 * Sets a flag to not wait for the alarm.
111 struct ppp_stats *curp;
113 struct ifpppstatsreq req;
115 memset (&req, 0, sizeof (req));
118 req.stats_ptr = (caddr_t) &req.stats;
120 #define ifr_name ifr__name
123 strncpy(req.ifr_name, interface, sizeof(req.ifr_name));
124 if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
125 fprintf(stderr, "%s: ", progname);
127 fprintf(stderr, "kernel support missing\n");
129 perror("couldn't get PPP statistics");
137 struct ppp_comp_stats *csp;
139 struct ifpppcstatsreq creq;
141 memset (&creq, 0, sizeof (creq));
144 creq.stats_ptr = (caddr_t) &creq.stats;
146 #define ifr_name ifr__name
149 strncpy(creq.ifr_name, interface, sizeof(creq.ifr_name));
150 if (ioctl(s, SIOCGPPPCSTATS, &creq) < 0) {
151 fprintf(stderr, "%s: ", progname);
152 if (errno == ENOTTY) {
153 fprintf(stderr, "no kernel compression support\n");
158 perror("couldn't get PPP compression stats");
164 if (creq.stats.c.bytes_out == 0)
165 creq.stats.c.ratio = 0.0;
167 creq.stats.c.ratio = (double) creq.stats.c.in_count /
168 (double) creq.stats.c.bytes_out;
170 if (creq.stats.d.bytes_out == 0)
171 creq.stats.d.ratio = 0.0;
173 creq.stats.d.ratio = (double) creq.stats.d.in_count /
174 (double) creq.stats.d.bytes_out;
183 strioctl(fd, cmd, ptr, ilen, olen)
184 int fd, cmd, ilen, olen;
193 if (ioctl(fd, I_STR, &str) == -1)
195 if (str.ic_len != olen)
196 fprintf(stderr, "strioctl: expected %d bytes, got %d for cmd %x\n",
197 olen, str.ic_len, cmd);
203 struct ppp_stats *curp;
205 if (strioctl(s, PPPIO_GETSTAT, curp, 0, sizeof(*curp)) < 0) {
206 fprintf(stderr, "%s: ", progname);
208 fprintf(stderr, "kernel support missing\n");
210 perror("couldn't get PPP statistics");
217 struct ppp_comp_stats *csp;
219 if (strioctl(s, PPPIO_GETCSTAT, csp, 0, sizeof(*csp)) < 0) {
220 fprintf(stderr, "%s: ", progname);
221 if (errno == ENOTTY) {
222 fprintf(stderr, "no kernel compression support\n");
227 perror("couldn't get PPP compression statistics");
235 #define MAX0(a) ((int)(a) > 0? (a): 0)
236 #define V(offset) MAX0(cur.offset - old.offset)
237 #define W(offset) MAX0(ccs.offset - ocs.offset)
239 #define RATIO(c, i, u) ((c) == 0? 1.0: (u) / ((double)(c) + (i)))
240 #define CRATE(x) RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
242 #define KBPS(n) ((n) / (interval * 1000.0))
245 * Print a running summary of interface statistics.
246 * Repeat display every interval seconds, showing statistics
247 * collected over that interval. Assumes that interval is non-zero.
248 * First line printed is cumulative.
253 register int line = 0;
254 sigset_t oldmask, mask;
257 struct ppp_stats cur, old;
258 struct ppp_comp_stats ccs, ocs;
260 memset(&old, 0, sizeof(old));
261 memset(&ocs, 0, sizeof(ocs));
266 get_ppp_cstats(&ccs);
268 (void)signal(SIGALRM, catchalarm);
270 (void)alarm(interval);
272 if ((line % 20) == 0) {
274 printf("IN: COMPRESSED INCOMPRESSIBLE COMP | ");
275 printf("OUT: COMPRESSED INCOMPRESSIBLE COMP\n");
276 bunit = dflag? "KB/S": "BYTE";
277 printf(" %s PACK %s PACK RATIO | ", bunit, bunit);
278 printf(" %s PACK %s PACK RATIO", bunit, bunit);
280 printf("%8.8s %6.6s %6.6s",
281 "IN", "PACK", "VJCOMP");
284 printf(" %6.6s %6.6s", "VJUNC", "VJERR");
286 printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
288 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
289 printf(" | %8.8s %6.6s %6.6s",
290 "OUT", "PACK", "VJCOMP");
293 printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
295 printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
297 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
304 printf("%8.3f %6u %8.3f %6u %6.2f",
305 KBPS(W(d.comp_bytes)),
307 KBPS(W(d.inc_bytes)),
309 ccs.d.ratio / 256.0);
310 printf(" | %8.3f %6u %8.3f %6u %6.2f",
311 KBPS(W(c.comp_bytes)),
313 KBPS(W(c.inc_bytes)),
315 ccs.c.ratio / 256.0);
317 printf("%8u %6u %8u %6u %6.2f",
322 ccs.d.ratio / 256.0);
323 printf(" | %8u %6u %8u %6u %6.2f",
328 ccs.c.ratio / 256.0);
333 printf("%8.3f", KBPS(V(p.ppp_ibytes)));
335 printf("%8u", V(p.ppp_ibytes));
338 V(vj.vjs_compressedin));
341 V(vj.vjs_uncompressedin),
346 V(p.ppp_ipackets) - V(vj.vjs_compressedin)
347 - V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
349 printf(" %6.2f ", CRATE(d));
351 printf("%6.2f", KBPS(W(d.unc_bytes)));
353 printf("%6u", W(d.unc_bytes));
356 printf(" | %8.3f", KBPS(V(p.ppp_obytes)));
358 printf(" | %8u", V(p.ppp_obytes));
361 V(vj.vjs_compressed));
364 V(vj.vjs_packets) - V(vj.vjs_compressed),
365 V(p.ppp_opackets) - V(vj.vjs_packets));
371 printf(" %6.2f ", CRATE(c));
373 printf("%6.2f", KBPS(W(c.unc_bytes)));
375 printf("%6u", W(c.unc_bytes));
385 if (!infinite && !count)
389 sigaddset(&mask, SIGALRM);
390 sigprocmask(SIG_BLOCK, &mask, &oldmask);
395 sigprocmask(SIG_SETMASK, &oldmask, NULL);
397 (void)alarm(interval);
418 if ((progname = strrchr(argv[0], '/')) == NULL)
423 while ((c = getopt(argc, argv, "advrzc:w:")) != -1) {
441 count = atoi(optarg);
446 interval = atoi(optarg);
457 if (!interval && count)
459 if (interval && !count)
461 if (!interval && !count)
471 if (sscanf(interface, "ppp%d", &unit) != 1) {
472 fprintf(stderr, "%s: invalid interface '%s' specified\n",
473 progname, interface);
480 s = socket(AF_INET, SOCK_DGRAM, 0);
482 fprintf(stderr, "%s: ", progname);
483 perror("couldn't create IP socket");
489 #define ifr_name ifr_ifrn.ifrn_name
491 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
492 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
493 fprintf(stderr, "%s: nonexistent interface '%s' specified\n",
494 progname, interface);
501 dev = "/dev/streams/ppp";
505 if ((s = open(dev, O_RDONLY)) < 0) {
506 fprintf(stderr, "%s: couldn't open ", progname);
510 if (strioctl(s, PPPIO_ATTACH, &unit, sizeof(int), 0) < 0) {
511 fprintf(stderr, "%s: ppp%d is not available\n", progname, unit);