]> git.ozlabs.org Git - ppp.git/blob - pppstats/pppstats.c
For Linux, use the Linux / Glibc based defines instead of included headers
[ppp.git] / pppstats / pppstats.c
1 /*
2  * print PPP statistics:
3  *      pppstats [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]
4  *
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
10  *
11  * History:
12  *      perkins@cps.msu.edu: Added compression statistics and alternate 
13  *                display. 11/94
14  *      Brad Parker (brad@cayman.com) 6/92
15  *
16  * from the original "slstats" by Van Jacobson
17  *
18  * Copyright (c) 1989 Regents of the University of California.
19  * All rights reserved.
20  *
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.
32  */
33
34 #ifndef __STDC__
35 #define const
36 #endif
37
38 #ifndef lint
39 static const char rcsid[] = "$Id: pppstats.c,v 1.29 2002/10/27 12:56:26 fcusack Exp $";
40 #endif
41
42 #include <stdio.h>
43 #include <stddef.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include <errno.h>
48 #include <signal.h>
49 #include <fcntl.h>
50 #include <unistd.h>
51 #include <sys/param.h>
52 #include <sys/types.h>
53 #include <sys/ioctl.h>
54
55 #ifndef STREAMS
56 #if defined(__linux__) && defined(__powerpc__) \
57     && (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
58 /* kludge alert! */
59 #undef __GLIBC__
60 #endif
61 #include <sys/socket.h>         /* *BSD, Linux, NeXT, Ultrix etc. */
62 #ifndef __linux__
63 #include <net/if.h>
64 #include <net/ppp_defs.h>
65 #else
66 /* Linux */
67 #if __GLIBC__ >= 2
68 #include <asm/types.h>          /* glibc 2 conflicts with linux/types.h */
69 #include <net/if.h>
70 #else
71 #include <linux/types.h>
72 #include <linux/if.h>
73 #endif
74 #include <linux/ppp_defs.h>
75 #include <linux/if_ppp.h>
76
77 #endif /* __linux__ */
78
79 #else   /* STREAMS */
80 #include <sys/stropts.h>        /* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
81 #include <net/ppp_defs.h>
82 #include <net/pppio.h>
83
84 #endif  /* STREAMS */
85
86 int     vflag, rflag, zflag;    /* select type of display */
87 int     aflag;                  /* print absolute values, not deltas */
88 int     dflag;                  /* print data rates, not bytes */
89 int     interval, count;
90 int     infinite;
91 int     s;                      /* socket or /dev/ppp file descriptor */
92 int     signalled;              /* set if alarm goes off "early" */
93 char    *progname;
94 char    *interface;
95
96 #if defined(SUNOS4) || defined(ULTRIX) || defined(NeXT)
97 extern int optind;
98 extern char *optarg;
99 #endif
100
101 /*
102  * If PPP_DRV_NAME is not defined, use the legacy "ppp" as the
103  * device name.
104  */
105 #if !defined(PPP_DRV_NAME)
106 #define PPP_DRV_NAME    "ppp"
107 #endif /* !defined(PPP_DRV_NAME) */
108
109 static void usage(void);
110 static void catchalarm(int);
111 static void get_ppp_stats(struct ppp_stats *);
112 static void get_ppp_cstats(struct ppp_comp_stats *);
113 static void intpr(void);
114
115 int main(int, char *argv[]);
116
117 static void
118 usage(void)
119 {
120     fprintf(stderr, "Usage: %s [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]\n",
121             progname);
122     exit(1);
123 }
124
125 /*
126  * Called if an interval expires before intpr has completed a loop.
127  * Sets a flag to not wait for the alarm.
128  */
129 static void
130 catchalarm(int arg)
131 {
132     signalled = 1;
133 }
134
135
136 #ifndef STREAMS
137 static void
138 get_ppp_stats(struct ppp_stats *curp)
139 {
140     struct ifreq req;
141
142     memset (&req, 0, sizeof (req));
143
144     req.ifr_data = (caddr_t) curp;
145
146     strncpy(req.ifr_name, interface, IFNAMSIZ);
147     req.ifr_name[IFNAMSIZ - 1] = 0;
148     if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
149         fprintf(stderr, "%s: ", progname);
150         if (errno == ENOTTY)
151             fprintf(stderr, "kernel support missing\n");
152         else
153             perror("couldn't get PPP statistics");
154         exit(1);
155     }
156 }
157
158 static void
159 get_ppp_cstats(struct ppp_comp_stats *csp)
160 {
161     struct ifreq req;
162     struct ppp_comp_stats stats;
163
164     memset (&req, 0, sizeof (req));
165
166     req.ifr_data = (caddr_t) &stats;
167
168     strncpy(req.ifr_name, interface, IFNAMSIZ);
169     req.ifr_name[IFNAMSIZ - 1] = 0;
170     if (ioctl(s, SIOCGPPPCSTATS, &req) < 0) {
171         fprintf(stderr, "%s: ", progname);
172         if (errno == ENOTTY) {
173             fprintf(stderr, "no kernel compression support\n");
174             if (zflag)
175                 exit(1);
176             rflag = 0;
177         } else {
178             perror("couldn't get PPP compression stats");
179             exit(1);
180         }
181     }
182
183 #ifdef __linux__
184     if (stats.c.bytes_out == 0) {
185         stats.c.bytes_out = stats.c.comp_bytes + stats.c.inc_bytes;
186         stats.c.in_count = stats.c.unc_bytes;
187     }
188     if (stats.c.bytes_out == 0)
189         stats.c.ratio = 0.0;
190     else
191         stats.c.ratio = 256.0 * stats.c.in_count / stats.c.bytes_out;
192
193     if (stats.d.bytes_out == 0) {
194         stats.d.bytes_out = stats.d.comp_bytes + stats.d.inc_bytes;
195         stats.d.in_count = stats.d.unc_bytes;
196     }
197     if (stats.d.bytes_out == 0)
198         stats.d.ratio = 0.0;
199     else
200         stats.d.ratio = 256.0 * stats.d.in_count / stats.d.bytes_out;
201 #endif
202
203     *csp = stats;
204 }
205
206 #else   /* STREAMS */
207
208 int
209 strioctl(int fd, int cmd, char *ptr, int ilen, int olen)
210 {
211     struct strioctl str;
212
213     str.ic_cmd = cmd;
214     str.ic_timout = 0;
215     str.ic_len = ilen;
216     str.ic_dp = ptr;
217     if (ioctl(fd, I_STR, &str) == -1)
218         return -1;
219     if (str.ic_len != olen)
220         fprintf(stderr, "strioctl: expected %d bytes, got %d for cmd %x\n",
221                olen, str.ic_len, cmd);
222     return 0;
223 }
224
225 static void
226 get_ppp_stats(struct ppp_stats *curp)
227 {
228     if (strioctl(s, PPPIO_GETSTAT, (char*) curp, 0, sizeof(*curp)) < 0) {
229         fprintf(stderr, "%s: ", progname);
230         if (errno == EINVAL)
231             fprintf(stderr, "kernel support missing\n");
232         else
233             perror("couldn't get PPP statistics");
234         exit(1);
235     }
236 }
237
238 static void
239 get_ppp_cstats(struct ppp_comp_stats *csp)
240 {
241     if (strioctl(s, PPPIO_GETCSTAT, (char*) csp, 0, sizeof(*csp)) < 0) {
242         fprintf(stderr, "%s: ", progname);
243         if (errno == ENOTTY) {
244             fprintf(stderr, "no kernel compression support\n");
245             if (zflag)
246                 exit(1);
247             rflag = 0;
248         } else {
249             perror("couldn't get PPP compression statistics");
250             exit(1);
251         }
252     }
253 }
254
255 #endif /* STREAMS */
256
257 #define MAX0(a)         ((int)(a) > 0? (a): 0)
258 #define V(offset)       MAX0(cur.offset - old.offset)
259 #define W(offset)       MAX0(ccs.offset - ocs.offset)
260
261 #define RATIO(c, i, u)  ((c) == 0? 1.0: (u) / ((double)(c) + (i)))
262 #define CRATE(x)        RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
263
264 #define KBPS(n)         ((n) / (interval * 1000.0))
265
266 /*
267  * Print a running summary of interface statistics.
268  * Repeat display every interval seconds, showing statistics
269  * collected over that interval.  Assumes that interval is non-zero.
270  * First line printed is cumulative.
271  */
272 static void
273 intpr(void)
274 {
275     register int line = 0;
276     sigset_t oldmask, mask;
277     char *bunit;
278     int ratef = 0;
279     struct ppp_stats cur, old;
280     struct ppp_comp_stats ccs, ocs;
281
282     memset(&old, 0, sizeof(old));
283     memset(&ocs, 0, sizeof(ocs));
284
285     while (1) {
286         get_ppp_stats(&cur);
287         if (zflag || rflag)
288             get_ppp_cstats(&ccs);
289
290         (void)signal(SIGALRM, catchalarm);
291         signalled = 0;
292         (void)alarm(interval);
293
294         if ((line % 20) == 0) {
295             if (zflag) {
296                 printf("IN:  COMPRESSED  INCOMPRESSIBLE   COMP | ");
297                 printf("OUT: COMPRESSED  INCOMPRESSIBLE   COMP\n");
298                 bunit = dflag? "KB/S": "BYTE";
299                 printf("    %s   PACK     %s   PACK  RATIO | ", bunit, bunit);
300                 printf("    %s   PACK     %s   PACK  RATIO", bunit, bunit);
301             } else {
302                 printf("%8.8s %6.6s %6.6s",
303                        "IN", "PACK", "VJCOMP");
304
305                 if (!rflag)
306                     printf(" %6.6s %6.6s", "VJUNC", "VJERR");
307                 if (vflag)
308                     printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
309                 if (rflag)
310                     printf(" %6.6s %6.6s", "RATIO", "UBYTE");
311                 printf("  | %8.8s %6.6s %6.6s",
312                        "OUT", "PACK", "VJCOMP");
313
314                 if (!rflag)
315                     printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
316                 if (vflag)
317                     printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
318                 if (rflag)
319                     printf(" %6.6s %6.6s", "RATIO", "UBYTE");
320             }
321             putchar('\n');
322         }
323
324         if (zflag) {
325             if (ratef) {
326                 printf("%8.3f %6u %8.3f %6u %6.2f",
327                        KBPS(W(d.comp_bytes)),
328                        W(d.comp_packets),
329                        KBPS(W(d.inc_bytes)),
330                        W(d.inc_packets),
331                        ccs.d.ratio / 256.0);
332                 printf(" | %8.3f %6u %8.3f %6u %6.2f",
333                        KBPS(W(c.comp_bytes)),
334                        W(c.comp_packets),
335                        KBPS(W(c.inc_bytes)),
336                        W(c.inc_packets),
337                        ccs.c.ratio / 256.0);
338             } else {
339                 printf("%8u %6u %8u %6u %6.2f",
340                        W(d.comp_bytes),
341                        W(d.comp_packets),
342                        W(d.inc_bytes),
343                        W(d.inc_packets),
344                        ccs.d.ratio / 256.0);
345                 printf(" | %8u %6u %8u %6u %6.2f",
346                        W(c.comp_bytes),
347                        W(c.comp_packets),
348                        W(c.inc_bytes),
349                        W(c.inc_packets),
350                        ccs.c.ratio / 256.0);
351             }
352         
353         } else {
354             if (ratef)
355                 printf("%8.3f", KBPS(V(p.ppp_ibytes)));
356             else
357                 printf("%8u", V(p.ppp_ibytes));
358             printf(" %6u %6u",
359                    V(p.ppp_ipackets),
360                    V(vj.vjs_compressedin));
361             if (!rflag)
362                 printf(" %6u %6u",
363                        V(vj.vjs_uncompressedin),
364                        V(vj.vjs_errorin));
365             if (vflag)
366                 printf(" %6u %6u",
367                        V(vj.vjs_tossed),
368                        V(p.ppp_ipackets) - V(vj.vjs_compressedin)
369                        - V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
370             if (rflag) {
371                 printf(" %6.2f ", CRATE(d));
372                 if (ratef)
373                     printf("%6.2f", KBPS(W(d.unc_bytes)));
374                 else
375                     printf("%6u", W(d.unc_bytes));
376             }
377             if (ratef)
378                 printf("  | %8.3f", KBPS(V(p.ppp_obytes)));
379             else
380                 printf("  | %8u", V(p.ppp_obytes));
381             printf(" %6u %6u",
382                    V(p.ppp_opackets),
383                    V(vj.vjs_compressed));
384             if (!rflag)
385                 printf(" %6u %6u",
386                        V(vj.vjs_packets) - V(vj.vjs_compressed),
387                        V(p.ppp_opackets) - V(vj.vjs_packets));
388             if (vflag)
389                 printf(" %6u %6u",
390                        V(vj.vjs_searches),
391                        V(vj.vjs_misses));
392             if (rflag) {
393                 printf(" %6.2f ", CRATE(c));
394                 if (ratef)
395                     printf("%6.2f", KBPS(W(c.unc_bytes)));
396                 else
397                     printf("%6u", W(c.unc_bytes));
398             }
399
400         }
401
402         putchar('\n');
403         fflush(stdout);
404         line++;
405
406         count--;
407         if (!infinite && !count)
408             break;
409
410         sigemptyset(&mask);
411         sigaddset(&mask, SIGALRM);
412         sigprocmask(SIG_BLOCK, &mask, &oldmask);
413         if (!signalled) {
414             sigemptyset(&mask);
415             sigsuspend(&mask);
416         }
417         sigprocmask(SIG_SETMASK, &oldmask, NULL);
418         signalled = 0;
419         (void)alarm(interval);
420
421         if (!aflag) {
422             old = cur;
423             ocs = ccs;
424             ratef = dflag;
425         }
426     }
427 }
428
429 int
430 main(int argc, char *argv[])
431 {
432     int c;
433 #ifdef STREAMS
434     int unit;
435     char *dev;
436 #endif
437
438     interface = PPP_DRV_NAME "0";
439     if ((progname = strrchr(argv[0], '/')) == NULL)
440         progname = argv[0];
441     else
442         ++progname;
443
444     while ((c = getopt(argc, argv, "advrzc:w:")) != -1) {
445         switch (c) {
446         case 'a':
447             ++aflag;
448             break;
449         case 'd':
450             ++dflag;
451             break;
452         case 'v':
453             ++vflag;
454             break;
455         case 'r':
456             ++rflag;
457             break;
458         case 'z':
459             ++zflag;
460             break;
461         case 'c':
462             count = atoi(optarg);
463             if (count <= 0)
464                 usage();
465             break;
466         case 'w':
467             interval = atoi(optarg);
468             if (interval <= 0)
469                 usage();
470             break;
471         default:
472             usage();
473         }
474     }
475     argc -= optind;
476     argv += optind;
477
478     if (!interval && count)
479         interval = 5;
480     if (interval && !count)
481         infinite = 1;
482     if (!interval && !count)
483         count = 1;
484     if (aflag)
485         dflag = 0;
486
487     if (argc > 1)
488         usage();
489     if (argc > 0)
490         interface = argv[0];
491
492 #ifndef STREAMS
493     {
494         struct ifreq ifr;
495
496         s = socket(AF_INET, SOCK_DGRAM, 0);
497         if (s < 0) {
498             fprintf(stderr, "%s: ", progname);
499             perror("couldn't create IP socket");
500             exit(1);
501         }
502
503 #ifdef __linux__
504 #undef  ifr_name
505 #define ifr_name ifr_ifrn.ifrn_name
506 #endif
507         strncpy(ifr.ifr_name, interface, IFNAMSIZ);
508         ifr.ifr_name[IFNAMSIZ - 1] = 0;
509         if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
510             fprintf(stderr, "%s: nonexistent interface '%s' specified\n",
511                     progname, interface);
512             exit(1);
513         }
514     }
515
516 #else   /* STREAMS */
517     if (sscanf(interface, PPP_DRV_NAME "%d", &unit) != 1) {
518         fprintf(stderr, "%s: invalid interface '%s' specified\n",
519                 progname, interface);
520     }
521
522 #ifdef __osf__
523     dev = "/dev/streams/ppp";
524 #else
525     dev = "/dev/" PPP_DRV_NAME;
526 #endif
527     if ((s = open(dev, O_RDONLY)) < 0) {
528         fprintf(stderr, "%s: couldn't open ", progname);
529         perror(dev);
530         exit(1);
531     }
532     if (strioctl(s, PPPIO_ATTACH, (char*) &unit, sizeof(int), 0) < 0) {
533         fprintf(stderr, "%s: ppp%d is not available\n", progname, unit);
534         exit(1);
535     }
536
537 #endif  /* STREAMS */
538
539     intpr();
540     exit(0);
541 }