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