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.
47 #include <sys/param.h>
48 #include <sys/types.h>
49 #include <sys/ioctl.h>
52 #if defined(__linux__) && defined(__powerpc__) \
53 && (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
57 #include <sys/socket.h> /* *BSD, Linux, NeXT, Ultrix etc. */
60 #include <net/ppp_defs.h>
64 #include <asm/types.h> /* glibc 2 conflicts with linux/types.h */
67 #include <linux/types.h>
70 #include <linux/ppp_defs.h>
71 #include <linux/ppp-ioctl.h>
73 #endif /* __linux__ */
76 #include <sys/stropts.h> /* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
77 #include <net/ppp_defs.h>
78 #include <net/pppio.h>
82 int vflag, rflag, zflag; /* select type of display */
83 int aflag; /* print absolute values, not deltas */
84 int dflag; /* print data rates, not bytes */
87 int s; /* socket or /dev/ppp file descriptor */
88 int signalled; /* set if alarm goes off "early" */
92 #if defined(SUNOS4) || defined(ULTRIX) || defined(NeXT)
98 * If PPP_DRV_NAME is not defined, use the legacy "ppp" as the
101 #if !defined(PPP_DRV_NAME)
102 #define PPP_DRV_NAME "ppp"
103 #endif /* !defined(PPP_DRV_NAME) */
105 static void usage(void);
106 static void catchalarm(int);
107 static void get_ppp_stats(struct ppp_stats *);
108 static void get_ppp_cstats(struct ppp_comp_stats *);
109 static void intpr(void);
111 int main(int, char *argv[]);
116 fprintf(stderr, "Usage: %s [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]\n",
122 * Called if an interval expires before intpr has completed a loop.
123 * Sets a flag to not wait for the alarm.
134 get_ppp_stats(struct ppp_stats *curp)
138 memset (&req, 0, sizeof (req));
140 req.ifr_data = (caddr_t) curp;
142 strncpy(req.ifr_name, interface, IFNAMSIZ);
143 req.ifr_name[IFNAMSIZ - 1] = 0;
144 if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
145 fprintf(stderr, "%s: ", progname);
147 fprintf(stderr, "kernel support missing\n");
149 perror("couldn't get PPP statistics");
155 get_ppp_cstats(struct ppp_comp_stats *csp)
158 struct ppp_comp_stats stats;
160 memset (&req, 0, sizeof (req));
162 req.ifr_data = (caddr_t) &stats;
164 strncpy(req.ifr_name, interface, IFNAMSIZ);
165 req.ifr_name[IFNAMSIZ - 1] = 0;
166 if (ioctl(s, SIOCGPPPCSTATS, &req) < 0) {
167 fprintf(stderr, "%s: ", progname);
168 if (errno == ENOTTY) {
169 fprintf(stderr, "no kernel compression support\n");
174 perror("couldn't get PPP compression stats");
180 if (stats.c.bytes_out == 0) {
181 stats.c.bytes_out = stats.c.comp_bytes + stats.c.inc_bytes;
182 stats.c.in_count = stats.c.unc_bytes;
184 if (stats.c.bytes_out == 0)
187 stats.c.ratio = 256.0 * stats.c.in_count / stats.c.bytes_out;
189 if (stats.d.bytes_out == 0) {
190 stats.d.bytes_out = stats.d.comp_bytes + stats.d.inc_bytes;
191 stats.d.in_count = stats.d.unc_bytes;
193 if (stats.d.bytes_out == 0)
196 stats.d.ratio = 256.0 * stats.d.in_count / stats.d.bytes_out;
205 strioctl(int fd, int cmd, char *ptr, int ilen, int olen)
213 if (ioctl(fd, I_STR, &str) == -1)
215 if (str.ic_len != olen)
216 fprintf(stderr, "strioctl: expected %d bytes, got %d for cmd %x\n",
217 olen, str.ic_len, cmd);
222 get_ppp_stats(struct ppp_stats *curp)
224 if (strioctl(s, PPPIO_GETSTAT, (char*) curp, 0, sizeof(*curp)) < 0) {
225 fprintf(stderr, "%s: ", progname);
227 fprintf(stderr, "kernel support missing\n");
229 perror("couldn't get PPP statistics");
235 get_ppp_cstats(struct ppp_comp_stats *csp)
237 if (strioctl(s, PPPIO_GETCSTAT, (char*) csp, 0, sizeof(*csp)) < 0) {
238 fprintf(stderr, "%s: ", progname);
239 if (errno == ENOTTY) {
240 fprintf(stderr, "no kernel compression support\n");
245 perror("couldn't get PPP compression statistics");
253 #define MAX0(a) ((int)(a) > 0? (a): 0)
254 #define V(offset) MAX0(cur.offset - old.offset)
255 #define W(offset) MAX0(ccs.offset - ocs.offset)
257 #define RATIO(c, i, u) ((c) == 0? 1.0: (u) / ((double)(c) + (i)))
258 #define CRATE(x) RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
260 #define KBPS(n) ((n) / (interval * 1000.0))
263 * Print a running summary of interface statistics.
264 * Repeat display every interval seconds, showing statistics
265 * collected over that interval. Assumes that interval is non-zero.
266 * First line printed is cumulative.
271 register int line = 0;
272 sigset_t oldmask, mask;
275 struct ppp_stats cur, old;
276 struct ppp_comp_stats ccs, ocs;
278 memset(&old, 0, sizeof(old));
279 memset(&ocs, 0, sizeof(ocs));
284 get_ppp_cstats(&ccs);
286 (void)signal(SIGALRM, catchalarm);
288 (void)alarm(interval);
290 if ((line % 20) == 0) {
292 printf("IN: COMPRESSED INCOMPRESSIBLE COMP | ");
293 printf("OUT: COMPRESSED INCOMPRESSIBLE COMP\n");
294 bunit = dflag? "KB/S": "BYTE";
295 printf(" %s PACK %s PACK RATIO | ", bunit, bunit);
296 printf(" %s PACK %s PACK RATIO", bunit, bunit);
298 printf("%8.8s %6.6s %6.6s",
299 "IN", "PACK", "VJCOMP");
302 printf(" %6.6s %6.6s", "VJUNC", "VJERR");
304 printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
306 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
307 printf(" | %8.8s %6.6s %6.6s",
308 "OUT", "PACK", "VJCOMP");
311 printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
313 printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
315 printf(" %6.6s %6.6s", "RATIO", "UBYTE");
322 printf("%8.3f %6u %8.3f %6u %6.2f",
323 KBPS(W(d.comp_bytes)),
325 KBPS(W(d.inc_bytes)),
327 ccs.d.ratio / 256.0);
328 printf(" | %8.3f %6u %8.3f %6u %6.2f",
329 KBPS(W(c.comp_bytes)),
331 KBPS(W(c.inc_bytes)),
333 ccs.c.ratio / 256.0);
335 printf("%8u %6u %8u %6u %6.2f",
340 ccs.d.ratio / 256.0);
341 printf(" | %8u %6u %8u %6u %6.2f",
346 ccs.c.ratio / 256.0);
351 printf("%8.3f", KBPS(V(p.ppp_ibytes)));
353 printf("%8u", V(p.ppp_ibytes));
356 V(vj.vjs_compressedin));
359 V(vj.vjs_uncompressedin),
364 V(p.ppp_ipackets) - V(vj.vjs_compressedin)
365 - V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
367 printf(" %6.2f ", CRATE(d));
369 printf("%6.2f", KBPS(W(d.unc_bytes)));
371 printf("%6u", W(d.unc_bytes));
374 printf(" | %8.3f", KBPS(V(p.ppp_obytes)));
376 printf(" | %8u", V(p.ppp_obytes));
379 V(vj.vjs_compressed));
382 V(vj.vjs_packets) - V(vj.vjs_compressed),
383 V(p.ppp_opackets) - V(vj.vjs_packets));
389 printf(" %6.2f ", CRATE(c));
391 printf("%6.2f", KBPS(W(c.unc_bytes)));
393 printf("%6u", W(c.unc_bytes));
403 if (!infinite && !count)
407 sigaddset(&mask, SIGALRM);
408 sigprocmask(SIG_BLOCK, &mask, &oldmask);
413 sigprocmask(SIG_SETMASK, &oldmask, NULL);
415 (void)alarm(interval);
426 main(int argc, char *argv[])
434 interface = PPP_DRV_NAME "0";
435 if ((progname = strrchr(argv[0], '/')) == NULL)
440 while ((c = getopt(argc, argv, "advrzc:w:")) != -1) {
458 count = atoi(optarg);
463 interval = atoi(optarg);
474 if (!interval && count)
476 if (interval && !count)
478 if (!interval && !count)
492 s = socket(AF_INET, SOCK_DGRAM, 0);
494 fprintf(stderr, "%s: ", progname);
495 perror("couldn't create IP socket");
501 #define ifr_name ifr_ifrn.ifrn_name
503 strncpy(ifr.ifr_name, interface, IFNAMSIZ);
504 ifr.ifr_name[IFNAMSIZ - 1] = 0;
505 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
506 fprintf(stderr, "%s: nonexistent interface '%s' specified\n",
507 progname, interface);
513 if (sscanf(interface, PPP_DRV_NAME "%d", &unit) != 1) {
514 fprintf(stderr, "%s: invalid interface '%s' specified\n",
515 progname, interface);
519 dev = "/dev/streams/ppp";
521 dev = "/dev/" PPP_DRV_NAME;
523 if ((s = open(dev, O_RDONLY)) < 0) {
524 fprintf(stderr, "%s: couldn't open ", progname);
528 if (strioctl(s, PPPIO_ATTACH, (char*) &unit, sizeof(int), 0) < 0) {
529 fprintf(stderr, "%s: ppp%d is not available\n", progname, unit);