]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/slstat/slstat.c
Consider PROTO_IPV6 as compressible by CCP.
[FreeBSD/FreeBSD.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$";
27 #endif
28
29 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <sys/sysctl.h>
32 #include <sys/time.h>
33
34 #include <ctype.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <net/if.h>
44 #include <net/if_var.h>
45 #include <net/if_mib.h>
46 #include <net/if_types.h>
47 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/ip.h>
50 #include <net/slcompress.h>
51 #include <net/if_slvar.h>
52
53 static  void usage __P((void));
54 static  void intpr __P((void));
55 static  void catchalarm __P((int));
56
57 #define INTERFACE_PREFIX        "sl%d"
58 char    interface[IFNAMSIZ];
59
60 int     rflag;
61 int     vflag;
62 unsigned interval = 5;
63 int     unit;
64 int     name[6];
65
66 int
67 main(argc, argv)
68         int argc;
69         char *argv[];
70 {
71         int c, i;
72         size_t len;
73         int maxifno;
74         int indx;
75         struct ifmibdata ifmd;
76
77         while ((c = getopt(argc, argv, "vri:")) != -1) {
78                 switch(c) {
79                 case 'v':
80                         ++vflag;
81                         break;
82                 case 'r':
83                         ++rflag;
84                         break;
85                 case 'i':
86                         interval = atoi(optarg);
87                         if (interval <= 0)
88                                 usage();
89                         break;
90                 default:
91                         usage();
92                 }
93         }
94         if (optind >= argc)
95                 sprintf(interface, INTERFACE_PREFIX, unit);
96         else if (isdigit(argv[optind][0])) {
97                 unit = atoi(argv[optind]);
98                 if (unit < 0)
99                         usage();
100                 sprintf(interface, INTERFACE_PREFIX, unit);
101         } else if (strncmp(argv[optind], "sl", 2) == 0
102                   && isdigit(argv[optind][2])
103                   && sscanf(argv[optind], "sl%d", &unit) == 1) {
104                 strncpy(interface, argv[optind], IFNAMSIZ);
105         } else
106                 usage();
107
108         name[0] = CTL_NET;
109         name[1] = PF_LINK;
110         name[2] = NETLINK_GENERIC;
111         name[3] = IFMIB_SYSTEM;
112         name[4] = IFMIB_IFCOUNT;
113         len = sizeof maxifno;
114         if (sysctl(name, 5, &maxifno, &len, 0, 0) < 0)
115                 err(1, "sysctl net.link.generic.system.ifcount");
116
117         name[3] = IFMIB_IFDATA;
118         name[5] = IFDATA_GENERAL;
119         len = sizeof ifmd;
120         for (i = 1; ; i++) {
121                 name[4] = i;
122
123                 if (sysctl(name, 6, &ifmd, &len, 0, 0) < 0) {
124                         if (errno == ENOENT)
125                                 continue;
126
127                         err(1, "sysctl");
128                 }
129                 if (strncmp(interface, ifmd.ifmd_name, IFNAMSIZ) == 0
130                     && ifmd.ifmd_data.ifi_type == IFT_SLIP) {
131                         indx = i;
132                         break;
133                 }
134                 if (i >= maxifno)
135                         errx(1, "interface %s does not exist", interface);
136         }
137
138         name[4] = indx;
139         name[5] = IFDATA_LINKSPECIFIC;
140         intpr();
141         exit(0);
142 }
143
144 #define V(offset) ((line % 20)? ((sc->offset - osc->offset) / \
145                   (rflag ? interval : 1)) : sc->offset)
146 #define AMT (sizeof(*sc) - 2 * sizeof(sc->sc_comp.tstate))
147
148 static void
149 usage()
150 {
151         fprintf(stderr, "usage: slstat [-i interval] [-vr] [unit]\n");
152         exit(1);
153 }
154
155 u_char  signalled;                      /* set if alarm goes off "early" */
156
157 /*
158  * Print a running summary of interface statistics.
159  * Repeat display every interval seconds, showing statistics
160  * collected over that interval.  Assumes that interval is non-zero.
161  * First line printed at top of screen is always cumulative.
162  */
163 static void
164 intpr()
165 {
166         register int line = 0;
167         int oldmask;
168         struct sl_softc *sc, *osc;
169         size_t len;
170
171         sc = (struct sl_softc *)malloc(AMT);
172         osc = (struct sl_softc *)malloc(AMT);
173         bzero((char *)osc, AMT);
174         len = AMT;
175
176         while (1) {
177                 if (sysctl(name, 6, sc, &len, 0, 0) < 0 &&
178                     (errno != ENOMEM || len != AMT))
179                         err(1, "sysctl linkspecific");
180
181                 (void)signal(SIGALRM, catchalarm);
182                 signalled = 0;
183                 (void)alarm(interval);
184
185                 if ((line % 20) == 0) {
186                         printf("%8.8s %6.6s %6.6s %6.6s %6.6s",
187                                 "in", "pack", "comp", "uncomp", "unknwn");
188                         if (vflag)
189                                 printf(" %6.6s %6.6s %6.6s",
190                                        "toss", "other", "err");
191                         printf(" | %8.8s %6.6s %6.6s %6.6s %6.6s",
192                                 "out", "pack", "comp", "uncomp", "other");
193                         if (vflag)
194                                 printf(" %6.6s %6.6s %6.6s %6.6s",
195                                        "search", "miss", "err", "coll");
196                         putchar('\n');
197                 }
198                 printf("%8lu %6ld %6u %6u %6u",
199                         V(sc_if.if_ibytes),
200                         V(sc_if.if_ipackets),
201                         V(sc_comp.sls_compressedin),
202                         V(sc_comp.sls_uncompressedin),
203                         V(sc_comp.sls_errorin));
204                 if (vflag)
205                         printf(" %6u %6lu %6lu",
206                                 V(sc_comp.sls_tossed),
207                                 V(sc_if.if_ipackets) -
208                                   V(sc_comp.sls_compressedin) -
209                                   V(sc_comp.sls_uncompressedin) -
210                                   V(sc_comp.sls_errorin),
211                                V(sc_if.if_ierrors));
212                 printf(" | %8lu %6ld %6u %6u %6lu",
213                         V(sc_if.if_obytes) / (rflag ? interval : 1),
214                         V(sc_if.if_opackets),
215                         V(sc_comp.sls_compressed),
216                         V(sc_comp.sls_packets) - V(sc_comp.sls_compressed),
217                         V(sc_if.if_opackets) - V(sc_comp.sls_packets));
218                 if (vflag)
219                         printf(" %6u %6u %6lu %6lu",
220                                 V(sc_comp.sls_searches),
221                                 V(sc_comp.sls_misses),
222                                 V(sc_if.if_oerrors),
223                                 V(sc_if.if_collisions));
224                 putchar('\n');
225                 fflush(stdout);
226                 line++;
227                 oldmask = sigblock(sigmask(SIGALRM));
228                 if (! signalled) {
229                         sigpause(0);
230                 }
231                 sigsetmask(oldmask);
232                 signalled = 0;
233                 (void)alarm(interval);
234                 bcopy((char *)sc, (char *)osc, AMT);
235         }
236 }
237
238 /*
239  * Called if an interval expires before sidewaysintpr has completed a loop.
240  * Sets a flag to not wait for the alarm.
241  */
242 static void
243 catchalarm(sig)
244         int sig;
245 {
246         signalled = 1;
247 }