2 * print serial line IP statistics:
3 * slstat [-i interval] [-v] [interface]
5 * Copyright (c) 1989, 1990, 1991, 1992 Regents of the University of
6 * California. All rights reserved.
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.
20 * Van Jacobson (van@ee.lbl.gov), Dec 31, 1989:
21 * - Initial distribution.
23 * $FreeBSD: src/usr.sbin/slstat/slstat.c,v 1.14 1999/08/28 01:20:00 peter Exp $
24 * $DragonFly: src/usr.sbin/slstat/slstat.c,v 1.5 2004/03/20 17:46:48 cpressey Exp $
27 #define _KERNEL_STRUCTURES
28 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <sys/sysctl.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/sl/if_slvar.h>
53 static void usage(void);
54 static void intpr(void);
55 static void catchalarm(int);
57 #define INTERFACE_PREFIX "sl%d"
59 char interface
[IFNAMSIZ
];
63 unsigned interval
= 5;
68 main(int argc
, char **argv
)
74 struct ifmibdata ifmd
;
76 while ((c
= getopt(argc
, argv
, "vri:")) != -1) {
85 interval
= atoi(optarg
);
94 sprintf(interface
, INTERFACE_PREFIX
, unit
);
95 else if (isdigit(argv
[optind
][0])) {
96 unit
= atoi(argv
[optind
]);
99 sprintf(interface
, INTERFACE_PREFIX
, unit
);
100 } else if (strncmp(argv
[optind
], "sl", 2) == 0
101 && isdigit(argv
[optind
][2])
102 && sscanf(argv
[optind
], "sl%d", &unit
) == 1) {
103 strncpy(interface
, argv
[optind
], IFNAMSIZ
);
109 name
[2] = NETLINK_GENERIC
;
110 name
[3] = IFMIB_SYSTEM
;
111 name
[4] = IFMIB_IFCOUNT
;
112 len
= sizeof maxifno
;
113 if (sysctl(name
, 5, &maxifno
, &len
, 0, 0) < 0)
114 err(1, "sysctl net.link.generic.system.ifcount");
116 name
[3] = IFMIB_IFDATA
;
117 name
[5] = IFDATA_GENERAL
;
122 if (sysctl(name
, 6, &ifmd
, &len
, 0, 0) < 0)
124 if (strncmp(interface
, ifmd
.ifmd_name
, IFNAMSIZ
) == 0
125 && ifmd
.ifmd_data
.ifi_type
== IFT_SLIP
) {
130 errx(1, "interface %s does not exist", interface
);
134 name
[5] = IFDATA_LINKSPECIFIC
;
139 #define V(offset) ((line % 20)? ((sc->offset - osc->offset) / \
140 (rflag ? interval : 1)) : sc->offset)
141 #define AMT (sizeof(*sc) - 2 * sizeof(sc->sc_comp.tstate))
146 fprintf(stderr
, "usage: slstat [-i interval] [-vr] [unit]\n");
150 u_char signalled
; /* set if alarm goes off "early" */
153 * Print a running summary of interface statistics.
154 * Repeat display every interval seconds, showing statistics
155 * collected over that interval. Assumes that interval is non-zero.
156 * First line printed at top of screen is always cumulative.
163 struct sl_softc
*sc
, *osc
;
166 sc
= (struct sl_softc
*)malloc(AMT
);
167 osc
= (struct sl_softc
*)malloc(AMT
);
168 bzero((char *)osc
, AMT
);
172 if (sysctl(name
, 6, sc
, &len
, 0, 0) < 0 &&
173 (errno
!= ENOMEM
|| len
!= AMT
))
174 err(1, "sysctl linkspecific");
176 signal(SIGALRM
, catchalarm
);
180 if ((line
% 20) == 0) {
181 printf("%8.8s %6.6s %6.6s %6.6s %6.6s",
182 "in", "pack", "comp", "uncomp", "unknwn");
184 printf(" %6.6s %6.6s %6.6s",
185 "toss", "other", "err");
186 printf(" | %8.8s %6.6s %6.6s %6.6s %6.6s",
187 "out", "pack", "comp", "uncomp", "other");
189 printf(" %6.6s %6.6s %6.6s %6.6s",
190 "search", "miss", "err", "coll");
193 printf("%8lu %6ld %6u %6u %6u",
195 V(sc_if
.if_ipackets
),
196 V(sc_comp
.sls_compressedin
),
197 V(sc_comp
.sls_uncompressedin
),
198 V(sc_comp
.sls_errorin
));
200 printf(" %6u %6lu %6lu",
201 V(sc_comp
.sls_tossed
),
202 V(sc_if
.if_ipackets
) -
203 V(sc_comp
.sls_compressedin
) -
204 V(sc_comp
.sls_uncompressedin
) -
205 V(sc_comp
.sls_errorin
),
206 V(sc_if
.if_ierrors
));
207 printf(" | %8lu %6ld %6u %6u %6lu",
208 V(sc_if
.if_obytes
) / (rflag
? interval
: 1),
209 V(sc_if
.if_opackets
),
210 V(sc_comp
.sls_compressed
),
211 V(sc_comp
.sls_packets
) - V(sc_comp
.sls_compressed
),
212 V(sc_if
.if_opackets
) - V(sc_comp
.sls_packets
));
214 printf(" %6u %6u %6lu %6lu",
215 V(sc_comp
.sls_searches
),
216 V(sc_comp
.sls_misses
),
218 V(sc_if
.if_collisions
));
222 oldmask
= sigblock(sigmask(SIGALRM
));
229 bcopy((char *)sc
, (char *)osc
, AMT
);
234 * Called if an interval expires before sidewaysintpr has completed a loop.
235 * Sets a flag to not wait for the alarm.