Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / slstat / slstat.c
1 /*
2  * print serial line IP statistics:
3  *      slstat [-i interval] [-v] [interface]
4  *
5  * Copyright (c) 1989, 1990, 1991, 1992 Regents of the University of
6  * California. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  *      Van Jacobson (van@ee.lbl.gov), Dec 31, 1989:
21  *      - Initial distribution.
22  */
23
24 #ifndef lint
25 static const char rcsid[] =
26   "$FreeBSD: src/usr.sbin/slstat/slstat.c,v 1.14 1999/08/28 01:20:00 peter Exp $";
27 #endif
28
29 #include <sys/param.h>
30 #include <sys/mbuf.h>
31 #include <sys/socket.h>
32 #include <sys/sysctl.h>
33 #include <sys/time.h>
34
35 #include <ctype.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_mib.h>
47 #include <net/if_types.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/ip.h>
51 #include <net/slcompress.h>
52 #include <net/if_slvar.h>
53
54 static  void usage __P((void));
55 static  void intpr __P((void));
56 static  void catchalarm __P((int));
57
58 #define INTERFACE_PREFIX        "sl%d"
59 char    interface[IFNAMSIZ];
60
61 int     rflag;
62 int     vflag;
63 unsigned interval = 5;
64 int     unit;
65 int     name[6];
66
67 int
68 main(argc, argv)
69         int argc;
70         char *argv[];
71 {
72         int c, i;
73         size_t len;
74         int maxifno;
75         int indx;
76         struct ifmibdata ifmd;
77
78         while ((c = getopt(argc, argv, "vri:")) != -1) {
79                 switch(c) {
80                 case 'v':
81                         ++vflag;
82                         break;
83                 case 'r':
84                         ++rflag;
85                         break;
86                 case 'i':
87                         interval = atoi(optarg);
88                         if (interval <= 0)
89                                 usage();
90                         break;
91                 default:
92                         usage();
93                 }
94         }
95         if (optind >= argc)
96                 sprintf(interface, INTERFACE_PREFIX, unit);
97         else if (isdigit(argv[optind][0])) {
98                 unit = atoi(argv[optind]);
99                 if (unit < 0)
100                         usage();
101                 sprintf(interface, INTERFACE_PREFIX, unit);
102         } else if (strncmp(argv[optind], "sl", 2) == 0
103                   && isdigit(argv[optind][2])
104                   && sscanf(argv[optind], "sl%d", &unit) == 1) {
105                 strncpy(interface, argv[optind], IFNAMSIZ);
106         } else
107                 usage();
108
109         name[0] = CTL_NET;
110         name[1] = PF_LINK;
111         name[2] = NETLINK_GENERIC;
112         name[3] = IFMIB_SYSTEM;
113         name[4] = IFMIB_IFCOUNT;
114         len = sizeof maxifno;
115         if (sysctl(name, 5, &maxifno, &len, 0, 0) < 0)
116                 err(1, "sysctl net.link.generic.system.ifcount");
117
118         name[3] = IFMIB_IFDATA;
119         name[5] = IFDATA_GENERAL;
120         len = sizeof ifmd;
121         for (i = 1; ; i++) {
122                 name[4] = i;
123
124                 if (sysctl(name, 6, &ifmd, &len, 0, 0) < 0)
125                         err(1, "sysctl");
126                 if (strncmp(interface, ifmd.ifmd_name, IFNAMSIZ) == 0
127                     && ifmd.ifmd_data.ifi_type == IFT_SLIP) {
128                         indx = i;
129                         break;
130                 }
131                 if (i >= maxifno)
132                         errx(1, "interface %s does not exist", interface);
133         }
134
135         name[4] = indx;
136         name[5] = IFDATA_LINKSPECIFIC;
137         intpr();
138         exit(0);
139 }
140
141 #define V(offset) ((line % 20)? ((sc->offset - osc->offset) / \
142                   (rflag ? interval : 1)) : sc->offset)
143 #define AMT (sizeof(*sc) - 2 * sizeof(sc->sc_comp.tstate))
144
145 static void
146 usage()
147 {
148         fprintf(stderr, "usage: slstat [-i interval] [-vr] [unit]\n");
149         exit(1);
150 }
151
152 u_char  signalled;                      /* set if alarm goes off "early" */
153
154 /*
155  * Print a running summary of interface statistics.
156  * Repeat display every interval seconds, showing statistics
157  * collected over that interval.  Assumes that interval is non-zero.
158  * First line printed at top of screen is always cumulative.
159  */
160 static void
161 intpr()
162 {
163         register int line = 0;
164         int oldmask;
165         struct sl_softc *sc, *osc;
166         size_t len;
167
168         sc = (struct sl_softc *)malloc(AMT);
169         osc = (struct sl_softc *)malloc(AMT);
170         bzero((char *)osc, AMT);
171         len = AMT;
172
173         while (1) {
174                 if (sysctl(name, 6, sc, &len, 0, 0) < 0 &&
175                     (errno != ENOMEM || len != AMT))
176                         err(1, "sysctl linkspecific");
177
178                 (void)signal(SIGALRM, catchalarm);
179                 signalled = 0;
180                 (void)alarm(interval);
181
182                 if ((line % 20) == 0) {
183                         printf("%8.8s %6.6s %6.6s %6.6s %6.6s",
184                                 "in", "pack", "comp", "uncomp", "unknwn");
185                         if (vflag)
186                                 printf(" %6.6s %6.6s %6.6s",
187                                        "toss", "other", "err");
188                         printf(" | %8.8s %6.6s %6.6s %6.6s %6.6s",
189                                 "out", "pack", "comp", "uncomp", "other");
190                         if (vflag)
191                                 printf(" %6.6s %6.6s %6.6s %6.6s",
192                                        "search", "miss", "err", "coll");
193                         putchar('\n');
194                 }
195                 printf("%8lu %6ld %6u %6u %6u",
196                         V(sc_if.if_ibytes),
197                         V(sc_if.if_ipackets),
198                         V(sc_comp.sls_compressedin),
199                         V(sc_comp.sls_uncompressedin),
200                         V(sc_comp.sls_errorin));
201                 if (vflag)
202                         printf(" %6u %6lu %6lu",
203                                 V(sc_comp.sls_tossed),
204                                 V(sc_if.if_ipackets) -
205                                   V(sc_comp.sls_compressedin) -
206                                   V(sc_comp.sls_uncompressedin) -
207                                   V(sc_comp.sls_errorin),
208                                V(sc_if.if_ierrors));
209                 printf(" | %8lu %6ld %6u %6u %6lu",
210                         V(sc_if.if_obytes) / (rflag ? interval : 1),
211                         V(sc_if.if_opackets),
212                         V(sc_comp.sls_compressed),
213                         V(sc_comp.sls_packets) - V(sc_comp.sls_compressed),
214                         V(sc_if.if_opackets) - V(sc_comp.sls_packets));
215                 if (vflag)
216                         printf(" %6u %6u %6lu %6lu",
217                                 V(sc_comp.sls_searches),
218                                 V(sc_comp.sls_misses),
219                                 V(sc_if.if_oerrors),
220                                 V(sc_if.if_collisions));
221                 putchar('\n');
222                 fflush(stdout);
223                 line++;
224                 oldmask = sigblock(sigmask(SIGALRM));
225                 if (! signalled) {
226                         sigpause(0);
227                 }
228                 sigsetmask(oldmask);
229                 signalled = 0;
230                 (void)alarm(interval);
231                 bcopy((char *)sc, (char *)osc, AMT);
232         }
233 }
234
235 /*
236  * Called if an interval expires before sidewaysintpr has completed a loop.
237  * Sets a flag to not wait for the alarm.
238  */
239 static void
240 catchalarm(sig)
241         int sig;
242 {
243         signalled = 1;
244 }