dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.bin / pppstats / pppstats.c
blobaf35827260422f1f247a0131592e0cea85343bf4
1 /*
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
11 * History:
12 * perkins@cps.msu.edu: Added compression statistics and alternate
13 * display. 11/94
14 * Brad Parker (brad@cayman.com) 6/92
16 * from the original "slstats" by Van Jacobson
18 * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
19 * All rights reserved.
21 * Copyright (c) 1989 Regents of the University of California.
22 * All rights reserved.
24 * Redistribution and use in source and binary forms are permitted
25 * provided that the above copyright notice and this paragraph are
26 * duplicated in all such forms and that any documentation,
27 * advertising materials, and other materials related to such
28 * distribution and use acknowledge that the software was developed
29 * by the University of California, Berkeley. The name of the
30 * University may not be used to endorse or promote products derived
31 * from this software without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
33 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
34 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
37 #ifndef __STDC__
38 #define const
39 #endif
41 #pragma ident "%Z%%M% %I% %E% SMI"
43 static const char rcsid[] = "$Id: pppstats.c,v 1.27 1999/08/13 06:46:23 paulus Exp $";
45 #include <stdio.h>
46 #include <stddef.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include <errno.h>
51 #include <signal.h>
52 #include <fcntl.h>
53 #include <unistd.h>
54 #include <sys/param.h>
55 #include <sys/types.h>
56 #include <sys/ioctl.h>
58 #ifndef STREAMS
59 #if defined(_linux_) && defined(__powerpc__) \
60 && (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
61 /* kludge alert! */
62 #undef __GLIBC__
63 #endif
64 #include <sys/socket.h> /* *BSD, Linux, NeXT, Ultrix etc. */
65 #ifndef _linux_
66 #include <net/if.h>
67 #include <net/ppp_defs.h>
68 #include <net/if_ppp.h>
69 #else
70 /* Linux */
71 #if __GLIBC__ >= 2
72 #include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
73 #include <net/if.h>
74 #else
75 #include <linux/types.h>
76 #include <linux/if.h>
77 #endif
78 #include <linux/ppp_defs.h>
79 #include <linux/if_ppp.h>
80 #endif /* _linux_ */
82 #else /* STREAMS */
83 #include <sys/stropts.h> /* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
84 #include <net/ppp_defs.h>
85 #include <net/pppio.h>
87 #ifdef PPPIO_GETSTAT64
88 #define ppp_stats64 ppp_stats64
89 #endif
90 #endif /* STREAMS */
92 #ifndef ppp_stats64
93 #define ppp_stats64 ppp_stats
94 #endif
96 static int vflag, rflag, zflag; /* select type of display */
97 static int aflag; /* print absolute values, not deltas */
98 static int dflag; /* print data rates, not bytes */
99 static int interval, count;
100 static int infinite;
101 static int unit;
102 static int s; /* socket or /dev/ppp file descriptor */
103 static int signalled; /* set if alarm goes off "early" */
104 static char *progname;
105 static char *interface;
107 #if defined(SUNOS4) || defined(ULTRIX) || defined(NeXT)
108 extern int optind;
109 extern char *optarg;
110 #endif
113 * If PPP_DRV_NAME is not defined, use the legacy "ppp" as the
114 * device name.
116 #if !defined(PPP_DRV_NAME)
117 #define PPP_DRV_NAME "ppp"
118 #endif /* !defined(PPP_DRV_NAME) */
120 static void usage __P((void));
121 static void catchalarm __P((int));
122 static void get_ppp_stats __P((struct ppp_stats64 *));
123 static void get_ppp_cstats __P((struct ppp_comp_stats *));
124 static void intpr __P((void));
126 int main __P((int, char *argv[]));
128 static void
129 usage()
131 (void) fprintf(stderr,
132 "Usage: %s [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]\n",
133 progname);
134 exit(1);
138 * Called if an interval expires before intpr has completed a loop.
139 * Sets a flag to not wait for the alarm.
141 /* ARGSUSED */
142 static void
143 catchalarm(arg)
144 int arg;
146 signalled = 1;
150 #ifndef STREAMS
151 static void
152 get_ppp_stats(curp)
153 struct ppp_stats64 *curp;
155 struct ifpppstatsreq req;
157 (void) memset (&req, 0, sizeof (req));
159 #ifdef _linux_
160 req.stats_ptr = (caddr_t) &req.stats;
161 #undef ifr_name
162 #define ifr_name ifr__name
163 #endif
165 strncpy(req.ifr_name, interface, sizeof(req.ifr_name));
166 if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
167 (void) fprintf(stderr, "%s: ", progname);
168 if (errno == ENOTTY)
169 (void) fprintf(stderr, "kernel support missing\n");
170 else
171 perror("couldn't get PPP statistics");
172 exit(1);
174 *curp = req.stats;
177 static void
178 get_ppp_cstats(csp)
179 struct ppp_comp_stats *csp;
181 struct ifpppcstatsreq creq;
183 (void) memset (&creq, 0, sizeof (creq));
185 #ifdef _linux_
186 creq.stats_ptr = (caddr_t) &creq.stats;
187 #undef ifr_name
188 #define ifr_name ifr__name
189 #endif
191 strncpy(creq.ifr_name, interface, sizeof(creq.ifr_name));
192 if (ioctl(s, SIOCGPPPCSTATS, &creq) < 0) {
193 (void) fprintf(stderr, "%s: ", progname);
194 if (errno == ENOTTY) {
195 (void) fprintf(stderr, "no kernel compression support\n");
196 if (zflag)
197 exit(1);
198 rflag = 0;
199 } else {
200 perror("couldn't get PPP compression stats");
201 exit(1);
205 #ifdef _linux_
206 if (creq.stats.c.bytes_out == 0) {
207 creq.stats.c.bytes_out = creq.stats.c.comp_bytes + creq.stats.c.inc_bytes;
208 creq.stats.c.in_count = creq.stats.c.unc_bytes;
210 if (creq.stats.c.bytes_out == 0)
211 creq.stats.c.ratio = 0.0;
212 else
213 creq.stats.c.ratio = 256.0 * creq.stats.c.in_count /
214 creq.stats.c.bytes_out;
216 if (creq.stats.d.bytes_out == 0) {
217 creq.stats.d.bytes_out = creq.stats.d.comp_bytes + creq.stats.d.inc_bytes;
218 creq.stats.d.in_count = creq.stats.d.unc_bytes;
220 if (creq.stats.d.bytes_out == 0)
221 creq.stats.d.ratio = 0.0;
222 else
223 creq.stats.d.ratio = 256.0 * creq.stats.d.in_count /
224 creq.stats.d.bytes_out;
225 #endif
227 *csp = creq.stats;
230 #else /* STREAMS */
232 static int
233 strioctl(fd, cmd, ptr, ilen, olen)
234 int fd, cmd, ilen, olen;
235 char *ptr;
237 struct strioctl str;
239 str.ic_cmd = cmd;
240 str.ic_timout = 0;
241 str.ic_len = ilen;
242 str.ic_dp = ptr;
243 if (ioctl(fd, I_STR, &str) == -1)
244 return -1;
245 if (str.ic_len != olen)
246 (void) fprintf(stderr,
247 "strioctl: expected %d bytes, got %d for cmd %x\n",
248 olen, str.ic_len, cmd);
249 return 0;
252 static void
253 get_ppp_stats(curp)
254 struct ppp_stats64 *curp;
256 #ifdef PPPIO_GETSTAT64
257 struct ppp_stats oldstat;
258 if (strioctl(s, PPPIO_GETSTAT64, (char *)curp, 0, sizeof(*curp)) >= 0)
259 return;
260 if (strioctl(s, PPPIO_GETSTAT, (char *)&oldstat, 0, sizeof(oldstat)) >= 0) {
261 curp->p.ppp_ibytes = oldstat.p.ppp_ibytes;
262 curp->p.ppp_ipackets = oldstat.p.ppp_ipackets;
263 curp->p.ppp_ierrors = oldstat.p.ppp_ierrors;
264 curp->p.ppp_obytes = oldstat.p.ppp_obytes;
265 curp->p.ppp_opackets = oldstat.p.ppp_opackets;
266 curp->p.ppp_oerrors = oldstat.p.ppp_oerrors;
267 curp->vj = oldstat.vj;
268 return;
270 #else
271 if (strioctl(s, PPPIO_GETSTAT, (char *)curp, 0, sizeof(*curp)) >= 0)
272 return;
273 #endif
275 (void) fprintf(stderr, "%s: ", progname);
276 if (errno == EINVAL)
277 (void) fprintf(stderr, "kernel support missing\n");
278 else
279 perror("couldn't get PPP statistics");
280 exit(1);
283 static void
284 get_ppp_cstats(csp)
285 struct ppp_comp_stats *csp;
287 if (strioctl(s, PPPIO_GETCSTAT, (char *)csp, 0, sizeof(*csp)) < 0) {
288 (void) fprintf(stderr, "%s: ", progname);
289 if (errno == ENOTTY) {
290 (void) fprintf(stderr, "no kernel compression support\n");
291 if (zflag)
292 exit(1);
293 rflag = 0;
294 } else {
295 perror("couldn't get PPP compression statistics");
296 exit(1);
301 #endif /* STREAMS */
303 #define MAX0(a) ((int)(a) > 0? (a): 0)
304 #define V(offset) MAX0(cur.offset - old.offset)
305 #define W(offset) MAX0(ccs.offset - ocs.offset)
307 #define RATIO(c, i, u) ((c) == 0? 1.0: (u) / ((double)(c) + (i)))
308 #define CRATE(x) RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
310 #define KBPS(n) ((n) / (interval * 1000.0))
313 * Print a running summary of interface statistics.
314 * Repeat display every interval seconds, showing statistics
315 * collected over that interval. Assumes that interval is non-zero.
316 * First line printed is cumulative.
318 static void
319 intpr()
321 register int line = 0;
322 sigset_t oldmask, mask;
323 char *bunit;
324 int ratef = 0;
325 struct ppp_stats64 cur, old;
326 struct ppp_comp_stats ccs, ocs;
328 (void) memset(&old, 0, sizeof(old));
329 (void) memset(&ocs, 0, sizeof(ocs));
331 for (;;) {
332 get_ppp_stats(&cur);
333 if (zflag || rflag)
334 get_ppp_cstats(&ccs);
336 (void)signal(SIGALRM, catchalarm);
337 signalled = 0;
338 (void)alarm(interval);
340 if ((line % 20) == 0) {
341 if (zflag) {
342 (void) printf("IN: COMPRESSED INCOMPRESSIBLE COMP | ");
343 (void) printf("OUT: COMPRESSED INCOMPRESSIBLE COMP\n");
344 bunit = dflag? "KB/S": "BYTE";
345 (void) printf(" %s PACK %s PACK RATIO | ", bunit,
346 bunit);
347 (void) printf(" %s PACK %s PACK RATIO", bunit,
348 bunit);
349 } else {
350 (void) printf("%8.8s %6.6s %6.6s",
351 "IN", "PACK", "VJCOMP");
353 if (!rflag)
354 (void) printf(" %6.6s %6.6s", "VJUNC", "VJERR");
355 if (vflag)
356 (void) printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
357 if (rflag)
358 (void) printf(" %6.6s %6.6s", "RATIO", "UBYTE");
359 (void) printf(" | %8.8s %6.6s %6.6s",
360 "OUT", "PACK", "VJCOMP");
362 if (!rflag)
363 (void) printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
364 if (vflag)
365 (void) printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
366 if (rflag)
367 (void) printf(" %6.6s %6.6s", "RATIO", "UBYTE");
369 (void) putchar('\n');
372 if (zflag) {
373 if (ratef) {
374 (void) printf("%8.3f %6u %8.3f %6u %6.2f",
375 KBPS(W(d.comp_bytes)),
376 W(d.comp_packets),
377 KBPS(W(d.inc_bytes)),
378 W(d.inc_packets),
379 ccs.d.ratio / 256.0);
380 (void) printf(" | %8.3f %6u %8.3f %6u %6.2f",
381 KBPS(W(c.comp_bytes)),
382 W(c.comp_packets),
383 KBPS(W(c.inc_bytes)),
384 W(c.inc_packets),
385 ccs.c.ratio / 256.0);
386 } else {
387 (void) printf("%8u %6u %8u %6u %6.2f",
388 W(d.comp_bytes),
389 W(d.comp_packets),
390 W(d.inc_bytes),
391 W(d.inc_packets),
392 ccs.d.ratio / 256.0);
393 (void) printf(" | %8u %6u %8u %6u %6.2f",
394 W(c.comp_bytes),
395 W(c.comp_packets),
396 W(c.inc_bytes),
397 W(c.inc_packets),
398 ccs.c.ratio / 256.0);
401 } else {
402 if (ratef)
403 (void) printf("%8.3f", KBPS(V(p.ppp_ibytes)));
404 else
405 (void) printf("%8" PPP_COUNTER_F, V(p.ppp_ibytes));
406 (void) printf(" %6" PPP_COUNTER_F " %6u",
407 V(p.ppp_ipackets),
408 V(vj.vjs_compressedin));
409 if (!rflag)
410 (void) printf(" %6u %6u",
411 V(vj.vjs_uncompressedin),
412 V(vj.vjs_errorin));
413 if (vflag)
414 (void) printf(" %6u %6" PPP_COUNTER_F,
415 V(vj.vjs_tossed),
416 V(p.ppp_ipackets) - V(vj.vjs_compressedin)
417 - V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
418 if (rflag) {
419 (void) printf(" %6.2f ", CRATE(d));
420 if (ratef)
421 (void) printf("%6.2f", KBPS(W(d.unc_bytes)));
422 else
423 (void) printf("%6u", W(d.unc_bytes));
425 if (ratef)
426 (void) printf(" | %8.3f", KBPS(V(p.ppp_obytes)));
427 else
428 (void) printf(" | %8" PPP_COUNTER_F, V(p.ppp_obytes));
429 (void) printf(" %6" PPP_COUNTER_F " %6u",
430 V(p.ppp_opackets),
431 V(vj.vjs_compressed));
432 if (!rflag)
433 (void) printf(" %6u %6" PPP_COUNTER_F,
434 V(vj.vjs_packets) - V(vj.vjs_compressed),
435 V(p.ppp_opackets) - V(vj.vjs_packets));
436 if (vflag)
437 (void) printf(" %6u %6u",
438 V(vj.vjs_searches),
439 V(vj.vjs_misses));
440 if (rflag) {
441 (void) printf(" %6.2f ", CRATE(c));
442 if (ratef)
443 (void) printf("%6.2f", KBPS(W(c.unc_bytes)));
444 else
445 (void) printf("%6u", W(c.unc_bytes));
450 (void) putchar('\n');
451 (void) fflush(stdout);
452 line++;
454 count--;
455 if (!infinite && !count)
456 break;
458 (void) sigemptyset(&mask);
459 (void) sigaddset(&mask, SIGALRM);
460 (void) sigprocmask(SIG_BLOCK, &mask, &oldmask);
461 if (!signalled) {
462 (void) sigemptyset(&mask);
463 (void) sigsuspend(&mask);
465 (void) sigprocmask(SIG_SETMASK, &oldmask, NULL);
466 signalled = 0;
467 (void)alarm(interval);
469 if (!aflag) {
470 old = cur;
471 ocs = ccs;
472 ratef = dflag;
478 main(argc, argv)
479 int argc;
480 char *argv[];
482 int c;
483 #ifdef STREAMS
484 char *dev;
485 #endif
487 interface = PPP_DRV_NAME "0";
488 if ((progname = strrchr(argv[0], '/')) == NULL)
489 progname = argv[0];
490 else
491 ++progname;
493 while ((c = getopt(argc, argv, "advrzc:w:")) != -1) {
494 switch (c) {
495 case 'a':
496 ++aflag;
497 break;
498 case 'd':
499 ++dflag;
500 break;
501 case 'v':
502 ++vflag;
503 break;
504 case 'r':
505 ++rflag;
506 break;
507 case 'z':
508 ++zflag;
509 break;
510 case 'c':
511 count = atoi(optarg);
512 if (count <= 0)
513 usage();
514 break;
515 case 'w':
516 interval = atoi(optarg);
517 if (interval <= 0)
518 usage();
519 break;
520 default:
521 usage();
524 argc -= optind;
525 argv += optind;
527 if (!interval && count)
528 interval = 5;
529 if (interval && !count)
530 infinite = 1;
531 if (!interval && !count)
532 count = 1;
533 if (aflag)
534 dflag = 0;
536 if (argc > 1)
537 usage();
538 if (argc > 0)
539 interface = argv[0];
541 if (sscanf(interface, PPP_DRV_NAME "%d", &unit) != 1) {
542 (void) fprintf(stderr, "%s: invalid interface '%s' specified\n",
543 progname, interface);
546 #ifndef STREAMS
548 struct ifreq ifr;
550 s = socket(AF_INET, SOCK_DGRAM, 0);
551 if (s < 0) {
552 (void) fprintf(stderr, "%s: ", progname);
553 perror("couldn't create IP socket");
554 exit(1);
557 #ifdef _linux_
558 #undef ifr_name
559 #define ifr_name ifr_ifrn.ifrn_name
560 #endif
561 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
562 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
563 (void) fprintf(stderr, "%s: nonexistent interface '%s' specified\n",
564 progname, interface);
565 exit(1);
569 #else /* STREAMS */
570 #ifdef __osf__
571 dev = "/dev/streams/ppp";
572 #else
573 dev = "/dev/" PPP_DRV_NAME;
574 #endif
575 if ((s = open(dev, O_RDONLY)) < 0) {
576 (void) fprintf(stderr, "%s: couldn't open ", progname);
577 perror(dev);
578 exit(1);
580 if (strioctl(s, PPPIO_ATTACH, (char *)&unit, sizeof(int), 0) < 0) {
581 (void) fprintf(stderr, "%s: " PPP_DRV_NAME "%d is not available\n",
582 progname, unit);
583 exit(1);
586 #endif /* STREAMS */
588 intpr();
589 return (0);