Complete the final step in moving XNSrouted, mrouted and routed to their
[unix-history] / usr.sbin / pppstats / pppstats.c
CommitLineData
3397e9c9
RG
1/*
2 * print PPP statistics:
3 * pppstats [-i interval] [-v] [interface] [system] [core]
4 *
5 * Brad Parker (brad@cayman.com) 6/92
6 *
7 * from the original "slstats" by Van Jaconson
8 *
9 * Copyright (c) 1989 Regents of the University of California.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms are permitted
13 * provided that the above copyright notice and this paragraph are
14 * duplicated in all such forms and that any documentation,
15 * advertising materials, and other materials related to such
16 * distribution and use acknowledge that the software was developed
17 * by the University of California, Berkeley. The name of the
18 * University may not be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
22 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 *
24 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
25 * - Initial distribution.
26 */
27
28#include <sys/param.h>
29#include <sys/mbuf.h>
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <sys/file.h>
33#ifndef KVMLIB
34#include <machine/pte.h>
35#endif
36#ifdef sun
37#include <kvm.h>
38#endif
39#include <ctype.h>
40#include <errno.h>
41#include <nlist.h>
42#include <stdio.h>
43#include <signal.h>
44#include <net/if.h>
45#include <netinet/in.h>
46#include <netinet/in_systm.h>
47#include <netinet/ip.h>
48#include <netinet/ip_var.h>
49
50#define VJC 1
51#include <net/slcompress.h>
52#include <net/if_ppp.h>
53
54#ifdef STREAMS
55#include <sys/stream.h>
56#include "ppp_str.h"
57#endif
58
59#ifdef STREAMS
60struct nlist nl[] = {
61#define N_SOFTC 0
62 { "_pii" },
63 "",
64};
65#else
66struct nlist nl[] = {
67#define N_SOFTC 0
68 { "_ppp_softc" },
69 "",
70};
71#endif
72
73#ifndef KVMLIB
74struct pte *Sysmap;
75int kmem;
76extern off_t lseek();
77#endif
78
79#ifdef sun
80kvm_t *kd;
81#endif
82
83#ifdef sun
84char *system = "/vmunix";
85#else
86char *system = "/386bsd";
87#endif
88
89#ifndef KVMLIB
90char *kmemf = "/dev/kmem";
91#else
92char *kmemf;
93#endif
94int kflag;
95int vflag;
96unsigned interval = 5;
97int unit;
98
99extern char *malloc();
100
101
102main(argc, argv)
103 int argc;
104 char *argv[];
105{
106 --argc; ++argv;
107 while (argc > 0) {
108 if (strcmp(argv[0], "-v") == 0) {
109 ++vflag;
110 ++argv, --argc;
111 continue;
112 }
113 if (strcmp(argv[0], "-i") == 0 && argv[1] &&
114 isdigit(argv[1][0])) {
115 interval = atoi(argv[1]);
116 if (interval <= 0)
117 usage();
118 ++argv, --argc;
119 ++argv, --argc;
120 continue;
121 }
122 if (isdigit(argv[0][0])) {
123 unit = atoi(argv[0]);
124 if (unit < 0)
125 usage();
126 ++argv, --argc;
127 continue;
128 }
129 if (kflag)
130 usage();
131
132 system = *argv;
133 ++argv, --argc;
134 if (argc > 0) {
135 kmemf = *argv++;
136 --argc;
137 kflag++;
138 }
139 }
140#ifndef KVMLIB
141 if (nlist(system, nl) < 0 || nl[0].n_type == 0) {
142 fprintf(stderr, "%s: no namelist\n", system);
143 exit(1);
144 }
145 kmem = open(kmemf, O_RDONLY);
146 if (kmem < 0) {
147 perror(kmemf);
148 exit(1);
149 }
150 if (kflag) {
151 off_t off;
152
153 Sysmap = (struct pte *)
154 malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
155 if (!Sysmap) {
156 fputs("netstat: can't get memory for Sysmap.\n", stderr);
157 exit(1);
158 }
159 off = nl[N_SYSMAP].n_value & ~KERNBASE;
160 (void)lseek(kmem, off, L_SET);
161 (void)read(kmem, (char *)Sysmap,
162 (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
163 }
164#else
165#ifdef sun
166 /* SunOS */
167 if ((kd = kvm_open(system, kmemf, (char *)0, O_RDONLY, NULL)) == NULL) {
168 perror("kvm_open");
169 exit(1);
170 }
171#else
172 /* BSD4.3+ */
173 if (kvm_openfiles(system, kmemf, (char *)0) == -1) {
174 fprintf(stderr, "kvm_openfiles: %s", kvm_geterr());
175 exit(1);
176 }
177#endif
178
179#ifdef sun
180 if (kvm_nlist(kd, nl)) {
181#else
182 if (kvm_nlist(nl)) {
183#endif
184 fprintf(stderr, "pppstats: can't find symbols in nlist\n");
185 exit(1);
186 }
187#endif
188 intpr();
189 exit(0);
190}
191
192#ifndef KVMLIB
193/*
194 * Seek into the kernel for a value.
195 */
196off_t
197klseek(fd, base, off)
198 int fd, off;
199 off_t base;
200{
201 if (kflag) {
202 /* get kernel pte */
203 base &= ~KERNBASE;
204 base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
205 }
206 return (lseek(fd, base, off));
207}
208#endif
209
210usage()
211{
212 fprintf(stderr,"usage: pppstats [-i interval] [-v] [unit] [system] [core]\n");
213 exit(1);
214}
215
216u_char signalled; /* set if alarm goes off "early" */
217
218#define V(offset) ((line % 20)? sc->offset - osc->offset : sc->offset)
219
220/*
221 * Print a running summary of interface statistics.
222 * Repeat display every interval seconds, showing statistics
223 * collected over that interval. Assumes that interval is non-zero.
224 * First line printed at top of screen is always cumulative.
225 */
226intpr()
227{
228 register int line = 0;
229 int oldmask;
230#ifdef __STDC__
231 void catchalarm(int);
232#else
233 void catchalarm();
234#endif
235
236#ifdef STREAMS
237#define STRUCT struct ppp_if_info
238#else
239#define STRUCT struct ppp_softc
240#endif
241
242 STRUCT *sc, *osc;
243
244 nl[N_SOFTC].n_value += unit * sizeof(struct ppp_softc);
245 sc = (STRUCT *)malloc(sizeof(STRUCT));
246 osc = (STRUCT *)malloc(sizeof(STRUCT));
247
248 bzero((char *)osc, sizeof(STRUCT));
249
250 while (1) {
251#ifndef KVMLIB
252 if (klseek(kmem, (off_t)nl[N_SOFTC].n_value, 0) < 0)
253 if(errno != EINTR)
254 perror("kmem seek");
255 if (read(kmem, (char *)sc, sizeof(STRUCT)) <= 0)
256 perror("kmem read");
257#else
258#ifdef sun
259 if (kvm_read(kd, nl[N_SOFTC].n_value,
260#else
261 if (kvm_read(nl[N_SOFTC].n_value,
262#endif
263 sc, sizeof(STRUCT)) !=
264 sizeof(STRUCT))
265 perror("kvm_read");
266#endif
267
268 (void)signal(SIGALRM, catchalarm);
269 signalled = 0;
270 (void)alarm(interval);
271
272 if ((line % 20) == 0) {
273 printf("%6.6s %6.6s %6.6s %6.6s %6.6s",
274 "in", "pack", "comp", "uncomp", "err");
275 if (vflag)
276 printf(" %6.6s %6.6s", "toss", "ip");
277 printf(" | %6.6s %6.6s %6.6s %6.6s %6.6s",
278 "out", "pack", "comp", "uncomp", "ip");
279 if (vflag)
280 printf(" %6.6s %6.6s", "search", "miss");
281 putchar('\n');
282 }
283
284#ifdef STREAMS
285#define COMP pii_sc_comp
286#define STATS pii_ifnet
287#else
288#define COMP sc_comp
289#define STATS sc_if
290#endif
291
292 printf("%6d %6d %6d %6d %6d",
293#if BSD > 43
294 V(STATS.if_ibytes),
295#else
296 0,
297#endif
298 V(STATS.if_ipackets),
299 V(COMP.sls_compressedin),
300 V(COMP.sls_uncompressedin),
301 V(COMP.sls_errorin));
302 if (vflag)
303 printf(" %6d %6d",
304 V(COMP.sls_tossed),
305 V(STATS.if_ipackets) -
306 V(COMP.sls_compressedin) -
307 V(COMP.sls_uncompressedin) -
308 V(COMP.sls_errorin));
309 printf(" | %6d %6d %6d %6d %6d",
310#if BSD > 43
311 V(STATS.if_obytes),
312#else
313 0,
314#endif
315 V(STATS.if_opackets),
316 V(COMP.sls_compressed),
317 V(COMP.sls_packets) - V(COMP.sls_compressed),
318 V(STATS.if_opackets) - V(COMP.sls_packets));
319 if (vflag)
320 printf(" %6d %6d",
321 V(COMP.sls_searches),
322 V(COMP.sls_misses));
323
324 putchar('\n');
325 fflush(stdout);
326 line++;
327 oldmask = sigblock(sigmask(SIGALRM));
328 if (! signalled) {
329 sigpause(0);
330 }
331 sigsetmask(oldmask);
332 signalled = 0;
333 (void)alarm(interval);
334 bcopy((char *)sc, (char *)osc, sizeof(STRUCT));
335 }
336}
337
338/*
339 * Called if an interval expires before sidewaysintpr has completed a loop.
340 * Sets a flag to not wait for the alarm.
341 */
342void catchalarm(arg)
343int arg;
344{
345 signalled = 1;
346}