2 * print PPP statistics:
3 * pppstats [-a] [-v] [-r] [-z] [-c count] [-w wait] [interface]
5 * -a Show absolute values rather than deltas
6 * -v Verbose mode for default display
7 * -r Show compression ratio in default display
8 * -z Show compression statistics instead of default display
11 * perkins@cps.msu.edu: Added compression statistics and alternate
13 * Brad Parker (brad@cayman.com) 6/92
15 * from the original "slstats" by Van Jacobson
17 * Copyright (c) 1989 Regents of the University of California.
18 * All rights reserved.
20 * Redistribution and use in source and binary forms are permitted
21 * provided that the above copyright notice and this paragraph are
22 * duplicated in all such forms and that any documentation,
23 * advertising materials, and other materials related to such
24 * distribution and use acknowledge that the software was developed
25 * by the University of California, Berkeley. The name of the
26 * University may not be used to endorse or promote products derived
27 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
30 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
34 static char rcsid[] = "$Id: pppstats.c,v 1.13 1996/07/01 01:22:36 paulus Exp $";
46 #include <sys/param.h>
47 #include <sys/types.h>
48 #include <sys/ioctl.h>
49 #include <net/ppp_defs.h>
52 #include <sys/socket.h> /* *BSD, Linux, NeXT, Ultrix etc. */
54 #include <net/if_ppp.h>
57 #include <sys/stropts.h> /* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
58 #include <net/pppio.h>
62 int vflag, rflag, zflag; /* select type of display */
63 int aflag; /* print absolute values, not deltas */
67 int s; /* socket or /dev/ppp file descriptor */
68 int signalled; /* set if alarm goes off "early" */
75 fprintf(stderr, "Usage: %s [-v|-r|-z] [-a] [-c count] [-w wait] [interface]\n",
81 * Called if an interval expires before intpr has completed a loop.
82 * Sets a flag to not wait for the alarm.
95 struct ppp_stats *curp;
97 struct ifpppstatsreq req;
99 memset (&req, 0, sizeof (req));
102 req.stats_ptr = (caddr_t) &req.stats;
104 #define ifr_name ifr__name
107 strncpy(req.ifr_name, interface, sizeof(req.ifr_name));
108 if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
109 fprintf(stderr, "%s: ", progname);
111 fprintf(stderr, "kernel support missing\n");
113 perror("couldn't get PPP statistics");
121 struct ppp_comp_stats *csp;
123 struct ifpppcstatsreq creq;
125 memset (&creq, 0, sizeof (creq));
128 creq.stats_ptr = (caddr_t) &creq.stats;
130 #define ifr_name ifr__name
133 strncpy(creq.ifr_name, interface, sizeof(creq.ifr_name));
134 if (ioctl(s, SIOCGPPPCSTATS, &creq) < 0) {
135 fprintf(stderr, "%s: ", progname);
136 if (errno == ENOTTY) {
137 fprintf(stderr, "no kernel compression support\n");
142 perror("couldn't get PPP compression stats");
148 if (creq.stats.c.bytes_out == 0)
149 creq.stats.c.ratio = 0.0;
151 creq.stats.c.ratio = (double) creq.stats.c.in_count /
152 (double) creq.stats.c.bytes_out;
154 if (creq.stats.d.bytes_out == 0)
155 creq.stats.d.ratio = 0.0;
157 creq.stats.d.ratio = (double) creq.stats.d.in_count /
158 (double) creq.stats.d.bytes_out;
167 strioctl(fd, cmd, ptr, ilen, olen)
168 int fd, cmd, ilen, olen;
177 if (ioctl(fd, I_STR, &str) == -1)
179 if (str.ic_len != olen)
180 fprintf(stderr, "strioctl: expected %d bytes, got %d for cmd %x\n",
181 olen, str.ic_len, cmd);
187 struct ppp_stats *curp;
189 if (strioctl(s, PPPIO_GETSTAT, curp, 0, sizeof(*curp)) < 0) {
190 fprintf(stderr, "%s: ", progname);
192 fprintf(stderr, "kernel support missing\n");
194 perror("couldn't get PPP statistics");
201 struct ppp_comp_stats *csp;
203 if (strioctl(s, PPPIO_GETCSTAT, csp, 0, sizeof(*csp)) < 0) {
204 fprintf(stderr, "%s: ", progname);
205 if (errno == ENOTTY) {
206 fprintf(stderr, "no kernel compression support\n");
211 perror("couldn't get PPP compression statistics");
219 #define MAX0(a) ((int)(a) > 0? (a): 0)
220 #define V(offset) MAX0(cur.offset - old.offset)
221 #define W(offset) MAX0(ccs.offset - ocs.offset)
223 #define RATIO(c, i, u) ((c) == 0? 1.0: (u) / ((double)(c) + (i)))
224 #define CRATE(x) RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
227 * Print a running summary of interface statistics.
228 * Repeat display every interval seconds, showing statistics
229 * collected over that interval. Assumes that interval is non-zero.
230 * First line printed is cumulative.
235 register int line = 0;
236 sigset_t oldmask, mask;
237 struct ppp_stats cur, old;
238 struct ppp_comp_stats ccs, ocs;
240 memset(&old, 0, sizeof(old));
241 memset(&ocs, 0, sizeof(ocs));
246 get_ppp_cstats(&ccs);
248 (void)signal(SIGALRM, catchalarm);
250 (void)alarm(interval);
252 if ((line % 20) == 0) {
254 printf("IN: COMPRESSED INCOMPRESSIBLE COMP | ");
255 printf("OUT: COMPRESSED INCOMPRESSIBLE COMP\n");
256 printf(" BYTE PACK BYTE PACK RATIO | ");
257 printf(" BYTE PACK BYTE PACK RATIO");
259 printf("%8.8s %6.6s %6.6s",
260 "IN", "PACK", "VJCOMP");
263 printf(" %6.6s %6.6s", "VJUNC", "VJERR");
265 printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
267 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
268 printf(" | %8.8s %6.6s %6.6s",
269 "OUT", "PACK", "VJCOMP");
272 printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
274 printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
276 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
282 printf("%8u %6u %8u %6u %6.2f",
287 ccs.d.ratio * 256.0);
289 printf(" | %8u %6u %8u %6u %6.2f",
294 ccs.c.ratio * 256.0);
298 printf("%8u %6u %6u",
301 V(vj.vjs_compressedin));
304 V(vj.vjs_uncompressedin),
309 V(p.ppp_ipackets) - V(vj.vjs_compressedin)
310 - V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
315 printf(" | %8u %6u %6u",
318 V(vj.vjs_compressed));
321 V(vj.vjs_packets) - V(vj.vjs_compressed),
322 V(p.ppp_opackets) - V(vj.vjs_packets));
340 if (!infinite && !count)
344 sigaddset(&mask, SIGALRM);
345 sigprocmask(SIG_BLOCK, &mask, &oldmask);
350 sigprocmask(SIG_SETMASK, &oldmask, NULL);
352 (void)alarm(interval);
369 if ((progname = strrchr(argv[0], '/')) == NULL)
374 while ((c = getopt(argc, argv, "avrzc:w:")) != -1) {
389 count = atoi(optarg);
394 interval = atoi(optarg);
405 if (!interval && count)
407 if (interval && !count)
409 if (!interval && !count)
417 if (sscanf(interface, "ppp%d", &unit) != 1) {
418 fprintf(stderr, "%s: invalid interface '%s' specified\n",
419 progname, interface);
426 s = socket(AF_INET, SOCK_DGRAM, 0);
428 fprintf(stderr, "%s: ", progname);
429 perror("couldn't create IP socket");
432 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
433 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
434 fprintf(stderr, "%s: nonexistent interface '%s' specified\n",
435 progname, interface);
441 if ((s = open("/dev/ppp", O_RDONLY)) < 0) {
442 fprintf(stderr, "%s: ", progname);
443 perror("couldn't open /dev/ppp");
446 if (strioctl(s, PPPIO_ATTACH, &unit, sizeof(int), 0) < 0) {
447 fprintf(stderr, "%s: ppp%d is not available\n", progname, unit);