1N/A/*
1N/A * print PPP statistics:
1N/A * pppstats [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]
1N/A *
1N/A * -a Show absolute values rather than deltas
1N/A * -d Show data rate (kB/s) rather than bytes
1N/A * -v Show more stats for VJ TCP header compression
1N/A * -r Show compression ratio
1N/A * -z Show compression statistics instead of default display
1N/A *
1N/A * History:
1N/A * perkins@cps.msu.edu: Added compression statistics and alternate
1N/A * display. 11/94
1N/A * Brad Parker (brad@cayman.com) 6/92
1N/A *
1N/A * from the original "slstats" by Van Jacobson
1N/A *
1N/A * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
1N/A * All rights reserved.
1N/A *
1N/A * Copyright (c) 1989 Regents of the University of California.
1N/A * All rights reserved.
1N/A *
1N/A * Redistribution and use in source and binary forms are permitted
1N/A * provided that the above copyright notice and this paragraph are
1N/A * duplicated in all such forms and that any documentation,
1N/A * advertising materials, and other materials related to such
1N/A * distribution and use acknowledge that the software was developed
1N/A * by the University of California, Berkeley. The name of the
1N/A * University may not be used to endorse or promote products derived
1N/A * from this software without specific prior written permission.
1N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1N/A * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1N/A * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1N/A */
1N/A
1N/A#ifndef __STDC__
1N/A#define const
1N/A#endif
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#ifndef lint
1N/Astatic const char rcsid[] = "$Id: pppstats.c,v 1.27 1999/08/13 06:46:23 paulus Exp $";
1N/A#endif
1N/A
1N/A#include <stdio.h>
1N/A#include <stddef.h>
1N/A#include <stdlib.h>
1N/A#include <string.h>
1N/A#include <ctype.h>
1N/A#include <errno.h>
1N/A#include <signal.h>
1N/A#include <fcntl.h>
1N/A#include <unistd.h>
1N/A#include <sys/param.h>
1N/A#include <sys/types.h>
1N/A#include <sys/ioctl.h>
1N/A
1N/A#ifndef STREAMS
1N/A#if defined(_linux_) && defined(__powerpc__) \
1N/A && (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
1N/A/* kludge alert! */
1N/A#undef __GLIBC__
1N/A#endif
1N/A#include <sys/socket.h> /* *BSD, Linux, NeXT, Ultrix etc. */
1N/A#ifndef _linux_
1N/A#include <net/if.h>
1N/A#include <net/ppp_defs.h>
1N/A#include <net/if_ppp.h>
1N/A#else
1N/A/* Linux */
1N/A#if __GLIBC__ >= 2
1N/A#include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
1N/A#include <net/if.h>
1N/A#else
1N/A#include <linux/types.h>
1N/A#include <linux/if.h>
1N/A#endif
1N/A#include <linux/ppp_defs.h>
1N/A#include <linux/if_ppp.h>
1N/A#endif /* _linux_ */
1N/A
1N/A#else /* STREAMS */
1N/A#include <sys/stropts.h> /* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
1N/A#include <net/ppp_defs.h>
1N/A#include <net/pppio.h>
1N/A
1N/A#ifdef PPPIO_GETSTAT64
1N/A#define ppp_stats64 ppp_stats64
1N/A#endif
1N/A#endif /* STREAMS */
1N/A
1N/A#ifndef ppp_stats64
1N/A#define ppp_stats64 ppp_stats
1N/A#endif
1N/A
1N/Astatic int vflag, rflag, zflag; /* select type of display */
1N/Astatic int aflag; /* print absolute values, not deltas */
1N/Astatic int dflag; /* print data rates, not bytes */
1N/Astatic int interval, count;
1N/Astatic int infinite;
1N/Astatic int unit;
1N/Astatic int s; /* socket or /dev/ppp file descriptor */
1N/Astatic int signalled; /* set if alarm goes off "early" */
1N/Astatic char *progname;
1N/Astatic char *interface;
1N/A
1N/A#if defined(SUNOS4) || defined(ULTRIX) || defined(NeXT)
1N/Aextern int optind;
1N/Aextern char *optarg;
1N/A#endif
1N/A
1N/A/*
1N/A * If PPP_DRV_NAME is not defined, use the legacy "ppp" as the
1N/A * device name.
1N/A */
1N/A#if !defined(PPP_DRV_NAME)
1N/A#define PPP_DRV_NAME "ppp"
1N/A#endif /* !defined(PPP_DRV_NAME) */
1N/A
1N/Astatic void usage __P((void));
1N/Astatic void catchalarm __P((int));
1N/Astatic void get_ppp_stats __P((struct ppp_stats64 *));
1N/Astatic void get_ppp_cstats __P((struct ppp_comp_stats *));
1N/Astatic void intpr __P((void));
1N/A
1N/Aint main __P((int, char *argv[]));
1N/A
1N/Astatic void
1N/Ausage()
1N/A{
1N/A (void) fprintf(stderr,
1N/A "Usage: %s [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]\n",
1N/A progname);
1N/A exit(1);
1N/A}
1N/A
1N/A/*
1N/A * Called if an interval expires before intpr has completed a loop.
1N/A * Sets a flag to not wait for the alarm.
1N/A */
1N/A/* ARGSUSED */
1N/Astatic void
1N/Acatchalarm(arg)
1N/A int arg;
1N/A{
1N/A signalled = 1;
1N/A}
1N/A
1N/A
1N/A#ifndef STREAMS
1N/Astatic void
1N/Aget_ppp_stats(curp)
1N/A struct ppp_stats64 *curp;
1N/A{
1N/A struct ifpppstatsreq req;
1N/A
1N/A (void) memset (&req, 0, sizeof (req));
1N/A
1N/A#ifdef _linux_
1N/A req.stats_ptr = (caddr_t) &req.stats;
1N/A#undef ifr_name
1N/A#define ifr_name ifr__name
1N/A#endif
1N/A
1N/A strncpy(req.ifr_name, interface, sizeof(req.ifr_name));
1N/A if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
1N/A (void) fprintf(stderr, "%s: ", progname);
1N/A if (errno == ENOTTY)
1N/A (void) fprintf(stderr, "kernel support missing\n");
1N/A else
1N/A perror("couldn't get PPP statistics");
1N/A exit(1);
1N/A }
1N/A *curp = req.stats;
1N/A}
1N/A
1N/Astatic void
1N/Aget_ppp_cstats(csp)
1N/A struct ppp_comp_stats *csp;
1N/A{
1N/A struct ifpppcstatsreq creq;
1N/A
1N/A (void) memset (&creq, 0, sizeof (creq));
1N/A
1N/A#ifdef _linux_
1N/A creq.stats_ptr = (caddr_t) &creq.stats;
1N/A#undef ifr_name
1N/A#define ifr_name ifr__name
1N/A#endif
1N/A
1N/A strncpy(creq.ifr_name, interface, sizeof(creq.ifr_name));
1N/A if (ioctl(s, SIOCGPPPCSTATS, &creq) < 0) {
1N/A (void) fprintf(stderr, "%s: ", progname);
1N/A if (errno == ENOTTY) {
1N/A (void) fprintf(stderr, "no kernel compression support\n");
1N/A if (zflag)
1N/A exit(1);
1N/A rflag = 0;
1N/A } else {
1N/A perror("couldn't get PPP compression stats");
1N/A exit(1);
1N/A }
1N/A }
1N/A
1N/A#ifdef _linux_
1N/A if (creq.stats.c.bytes_out == 0) {
1N/A creq.stats.c.bytes_out = creq.stats.c.comp_bytes + creq.stats.c.inc_bytes;
1N/A creq.stats.c.in_count = creq.stats.c.unc_bytes;
1N/A }
1N/A if (creq.stats.c.bytes_out == 0)
1N/A creq.stats.c.ratio = 0.0;
1N/A else
1N/A creq.stats.c.ratio = 256.0 * creq.stats.c.in_count /
1N/A creq.stats.c.bytes_out;
1N/A
1N/A if (creq.stats.d.bytes_out == 0) {
1N/A creq.stats.d.bytes_out = creq.stats.d.comp_bytes + creq.stats.d.inc_bytes;
1N/A creq.stats.d.in_count = creq.stats.d.unc_bytes;
1N/A }
1N/A if (creq.stats.d.bytes_out == 0)
1N/A creq.stats.d.ratio = 0.0;
1N/A else
1N/A creq.stats.d.ratio = 256.0 * creq.stats.d.in_count /
1N/A creq.stats.d.bytes_out;
1N/A#endif
1N/A
1N/A *csp = creq.stats;
1N/A}
1N/A
1N/A#else /* STREAMS */
1N/A
1N/Astatic int
1N/Astrioctl(fd, cmd, ptr, ilen, olen)
1N/A int fd, cmd, ilen, olen;
1N/A char *ptr;
1N/A{
1N/A struct strioctl str;
1N/A
1N/A str.ic_cmd = cmd;
1N/A str.ic_timout = 0;
1N/A str.ic_len = ilen;
1N/A str.ic_dp = ptr;
1N/A if (ioctl(fd, I_STR, &str) == -1)
1N/A return -1;
1N/A if (str.ic_len != olen)
1N/A (void) fprintf(stderr,
1N/A "strioctl: expected %d bytes, got %d for cmd %x\n",
1N/A olen, str.ic_len, cmd);
1N/A return 0;
1N/A}
1N/A
1N/Astatic void
1N/Aget_ppp_stats(curp)
1N/A struct ppp_stats64 *curp;
1N/A{
1N/A#ifdef PPPIO_GETSTAT64
1N/A struct ppp_stats oldstat;
1N/A if (strioctl(s, PPPIO_GETSTAT64, (char *)curp, 0, sizeof(*curp)) >= 0)
1N/A return;
1N/A if (strioctl(s, PPPIO_GETSTAT, (char *)&oldstat, 0, sizeof(oldstat)) >= 0) {
1N/A curp->p.ppp_ibytes = oldstat.p.ppp_ibytes;
1N/A curp->p.ppp_ipackets = oldstat.p.ppp_ipackets;
1N/A curp->p.ppp_ierrors = oldstat.p.ppp_ierrors;
1N/A curp->p.ppp_obytes = oldstat.p.ppp_obytes;
1N/A curp->p.ppp_opackets = oldstat.p.ppp_opackets;
1N/A curp->p.ppp_oerrors = oldstat.p.ppp_oerrors;
1N/A curp->vj = oldstat.vj;
1N/A return;
1N/A }
1N/A#else
1N/A if (strioctl(s, PPPIO_GETSTAT, (char *)curp, 0, sizeof(*curp)) >= 0)
1N/A return;
1N/A#endif
1N/A
1N/A (void) fprintf(stderr, "%s: ", progname);
1N/A if (errno == EINVAL)
1N/A (void) fprintf(stderr, "kernel support missing\n");
1N/A else
1N/A perror("couldn't get PPP statistics");
1N/A exit(1);
1N/A}
1N/A
1N/Astatic void
1N/Aget_ppp_cstats(csp)
1N/A struct ppp_comp_stats *csp;
1N/A{
1N/A if (strioctl(s, PPPIO_GETCSTAT, (char *)csp, 0, sizeof(*csp)) < 0) {
1N/A (void) fprintf(stderr, "%s: ", progname);
1N/A if (errno == ENOTTY) {
1N/A (void) fprintf(stderr, "no kernel compression support\n");
1N/A if (zflag)
1N/A exit(1);
1N/A rflag = 0;
1N/A } else {
1N/A perror("couldn't get PPP compression statistics");
1N/A exit(1);
1N/A }
1N/A }
1N/A}
1N/A
1N/A#endif /* STREAMS */
1N/A
1N/A#define MAX0(a) ((int)(a) > 0? (a): 0)
1N/A#define V(offset) MAX0(cur.offset - old.offset)
1N/A#define W(offset) MAX0(ccs.offset - ocs.offset)
1N/A
1N/A#define RATIO(c, i, u) ((c) == 0? 1.0: (u) / ((double)(c) + (i)))
1N/A#define CRATE(x) RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
1N/A
1N/A#define KBPS(n) ((n) / (interval * 1000.0))
1N/A
1N/A/*
1N/A * Print a running summary of interface statistics.
1N/A * Repeat display every interval seconds, showing statistics
1N/A * collected over that interval. Assumes that interval is non-zero.
1N/A * First line printed is cumulative.
1N/A */
1N/Astatic void
1N/Aintpr()
1N/A{
1N/A register int line = 0;
1N/A sigset_t oldmask, mask;
1N/A char *bunit;
1N/A int ratef = 0;
1N/A struct ppp_stats64 cur, old;
1N/A struct ppp_comp_stats ccs, ocs;
1N/A
1N/A (void) memset(&old, 0, sizeof(old));
1N/A (void) memset(&ocs, 0, sizeof(ocs));
1N/A
1N/A for (;;) {
1N/A get_ppp_stats(&cur);
1N/A if (zflag || rflag)
1N/A get_ppp_cstats(&ccs);
1N/A
1N/A (void)signal(SIGALRM, catchalarm);
1N/A signalled = 0;
1N/A (void)alarm(interval);
1N/A
1N/A if ((line % 20) == 0) {
1N/A if (zflag) {
1N/A (void) printf("IN: COMPRESSED INCOMPRESSIBLE COMP | ");
1N/A (void) printf("OUT: COMPRESSED INCOMPRESSIBLE COMP\n");
1N/A bunit = dflag? "KB/S": "BYTE";
1N/A (void) printf(" %s PACK %s PACK RATIO | ", bunit,
1N/A bunit);
1N/A (void) printf(" %s PACK %s PACK RATIO", bunit,
1N/A bunit);
1N/A } else {
1N/A (void) printf("%8.8s %6.6s %6.6s",
1N/A "IN", "PACK", "VJCOMP");
1N/A
1N/A if (!rflag)
1N/A (void) printf(" %6.6s %6.6s", "VJUNC", "VJERR");
1N/A if (vflag)
1N/A (void) printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
1N/A if (rflag)
1N/A (void) printf(" %6.6s %6.6s", "RATIO", "UBYTE");
1N/A (void) printf(" | %8.8s %6.6s %6.6s",
1N/A "OUT", "PACK", "VJCOMP");
1N/A
1N/A if (!rflag)
1N/A (void) printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
1N/A if (vflag)
1N/A (void) printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
1N/A if (rflag)
1N/A (void) printf(" %6.6s %6.6s", "RATIO", "UBYTE");
1N/A }
1N/A (void) putchar('\n');
1N/A }
1N/A
1N/A if (zflag) {
1N/A if (ratef) {
1N/A (void) printf("%8.3f %6u %8.3f %6u %6.2f",
1N/A KBPS(W(d.comp_bytes)),
1N/A W(d.comp_packets),
1N/A KBPS(W(d.inc_bytes)),
1N/A W(d.inc_packets),
1N/A ccs.d.ratio / 256.0);
1N/A (void) printf(" | %8.3f %6u %8.3f %6u %6.2f",
1N/A KBPS(W(c.comp_bytes)),
1N/A W(c.comp_packets),
1N/A KBPS(W(c.inc_bytes)),
1N/A W(c.inc_packets),
1N/A ccs.c.ratio / 256.0);
1N/A } else {
1N/A (void) printf("%8u %6u %8u %6u %6.2f",
1N/A W(d.comp_bytes),
1N/A W(d.comp_packets),
1N/A W(d.inc_bytes),
1N/A W(d.inc_packets),
1N/A ccs.d.ratio / 256.0);
1N/A (void) printf(" | %8u %6u %8u %6u %6.2f",
1N/A W(c.comp_bytes),
1N/A W(c.comp_packets),
1N/A W(c.inc_bytes),
1N/A W(c.inc_packets),
1N/A ccs.c.ratio / 256.0);
1N/A }
1N/A
1N/A } else {
1N/A if (ratef)
1N/A (void) printf("%8.3f", KBPS(V(p.ppp_ibytes)));
1N/A else
1N/A (void) printf("%8" PPP_COUNTER_F, V(p.ppp_ibytes));
1N/A (void) printf(" %6" PPP_COUNTER_F " %6u",
1N/A V(p.ppp_ipackets),
1N/A V(vj.vjs_compressedin));
1N/A if (!rflag)
1N/A (void) printf(" %6u %6u",
1N/A V(vj.vjs_uncompressedin),
1N/A V(vj.vjs_errorin));
1N/A if (vflag)
1N/A (void) printf(" %6u %6" PPP_COUNTER_F,
1N/A V(vj.vjs_tossed),
1N/A V(p.ppp_ipackets) - V(vj.vjs_compressedin)
1N/A - V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
1N/A if (rflag) {
1N/A (void) printf(" %6.2f ", CRATE(d));
1N/A if (ratef)
1N/A (void) printf("%6.2f", KBPS(W(d.unc_bytes)));
1N/A else
1N/A (void) printf("%6u", W(d.unc_bytes));
1N/A }
1N/A if (ratef)
1N/A (void) printf(" | %8.3f", KBPS(V(p.ppp_obytes)));
1N/A else
1N/A (void) printf(" | %8" PPP_COUNTER_F, V(p.ppp_obytes));
1N/A (void) printf(" %6" PPP_COUNTER_F " %6u",
1N/A V(p.ppp_opackets),
1N/A V(vj.vjs_compressed));
1N/A if (!rflag)
1N/A (void) printf(" %6u %6" PPP_COUNTER_F,
1N/A V(vj.vjs_packets) - V(vj.vjs_compressed),
1N/A V(p.ppp_opackets) - V(vj.vjs_packets));
1N/A if (vflag)
1N/A (void) printf(" %6u %6u",
1N/A V(vj.vjs_searches),
1N/A V(vj.vjs_misses));
1N/A if (rflag) {
1N/A (void) printf(" %6.2f ", CRATE(c));
1N/A if (ratef)
1N/A (void) printf("%6.2f", KBPS(W(c.unc_bytes)));
1N/A else
1N/A (void) printf("%6u", W(c.unc_bytes));
1N/A }
1N/A
1N/A }
1N/A
1N/A (void) putchar('\n');
1N/A (void) fflush(stdout);
1N/A line++;
1N/A
1N/A count--;
1N/A if (!infinite && !count)
1N/A break;
1N/A
1N/A (void) sigemptyset(&mask);
1N/A (void) sigaddset(&mask, SIGALRM);
1N/A (void) sigprocmask(SIG_BLOCK, &mask, &oldmask);
1N/A if (!signalled) {
1N/A (void) sigemptyset(&mask);
1N/A (void) sigsuspend(&mask);
1N/A }
1N/A (void) sigprocmask(SIG_SETMASK, &oldmask, NULL);
1N/A signalled = 0;
1N/A (void)alarm(interval);
1N/A
1N/A if (!aflag) {
1N/A old = cur;
1N/A ocs = ccs;
1N/A ratef = dflag;
1N/A }
1N/A }
1N/A}
1N/A
1N/Aint
1N/Amain(argc, argv)
1N/A int argc;
1N/A char *argv[];
1N/A{
1N/A int c;
1N/A#ifdef STREAMS
1N/A char *dev;
1N/A#endif
1N/A
1N/A interface = PPP_DRV_NAME "0";
1N/A if ((progname = strrchr(argv[0], '/')) == NULL)
1N/A progname = argv[0];
1N/A else
1N/A ++progname;
1N/A
1N/A while ((c = getopt(argc, argv, "advrzc:w:")) != -1) {
1N/A switch (c) {
1N/A case 'a':
1N/A ++aflag;
1N/A break;
1N/A case 'd':
1N/A ++dflag;
1N/A break;
1N/A case 'v':
1N/A ++vflag;
1N/A break;
1N/A case 'r':
1N/A ++rflag;
1N/A break;
1N/A case 'z':
1N/A ++zflag;
1N/A break;
1N/A case 'c':
1N/A count = atoi(optarg);
1N/A if (count <= 0)
1N/A usage();
1N/A break;
1N/A case 'w':
1N/A interval = atoi(optarg);
1N/A if (interval <= 0)
1N/A usage();
1N/A break;
1N/A default:
1N/A usage();
1N/A }
1N/A }
1N/A argc -= optind;
1N/A argv += optind;
1N/A
1N/A if (!interval && count)
1N/A interval = 5;
1N/A if (interval && !count)
1N/A infinite = 1;
1N/A if (!interval && !count)
1N/A count = 1;
1N/A if (aflag)
1N/A dflag = 0;
1N/A
1N/A if (argc > 1)
1N/A usage();
1N/A if (argc > 0)
1N/A interface = argv[0];
1N/A
1N/A if (sscanf(interface, PPP_DRV_NAME "%d", &unit) != 1) {
1N/A (void) fprintf(stderr, "%s: invalid interface '%s' specified\n",
1N/A progname, interface);
1N/A }
1N/A
1N/A#ifndef STREAMS
1N/A {
1N/A struct ifreq ifr;
1N/A
1N/A s = socket(AF_INET, SOCK_DGRAM, 0);
1N/A if (s < 0) {
1N/A (void) fprintf(stderr, "%s: ", progname);
1N/A perror("couldn't create IP socket");
1N/A exit(1);
1N/A }
1N/A
1N/A#ifdef _linux_
1N/A#undef ifr_name
1N/A#define ifr_name ifr_ifrn.ifrn_name
1N/A#endif
1N/A strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
1N/A if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
1N/A (void) fprintf(stderr, "%s: nonexistent interface '%s' specified\n",
1N/A progname, interface);
1N/A exit(1);
1N/A }
1N/A }
1N/A
1N/A#else /* STREAMS */
1N/A#ifdef __osf__
1N/A dev = "/dev/streams/ppp";
1N/A#else
1N/A dev = "/dev/" PPP_DRV_NAME;
1N/A#endif
1N/A if ((s = open(dev, O_RDONLY)) < 0) {
1N/A (void) fprintf(stderr, "%s: couldn't open ", progname);
1N/A perror(dev);
1N/A exit(1);
1N/A }
1N/A if (strioctl(s, PPPIO_ATTACH, (char *)&unit, sizeof(int), 0) < 0) {
1N/A (void) fprintf(stderr, "%s: " PPP_DRV_NAME "%d is not available\n",
1N/A progname, unit);
1N/A exit(1);
1N/A }
1N/A
1N/A#endif /* STREAMS */
1N/A
1N/A intpr();
1N/A return (0);
1N/A}