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