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