]> git.saurik.com Git - apple/network_cmds.git/blob - netstat.tproj/if.c
network_cmds-329.2.2.tar.gz
[apple/network_cmds.git] / netstat.tproj / if.c
1 /*
2 * Copyright (c) 2008-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1983, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 #ifndef lint
62 /*
63 static char sccsid[] = "@(#)if.c 8.3 (Berkeley) 4/28/95";
64 */
65 static const char rcsid[] =
66 "$Id: if.c,v 1.7 2006/01/16 04:53:59 lindak Exp $";
67 #endif /* not lint */
68
69 #include <sys/types.h>
70 #include <sys/socket.h>
71 #include <sys/sysctl.h>
72 #include <sys/time.h>
73
74 #include <net/if.h>
75 #include <net/if_var.h>
76 #include <net/if_dl.h>
77 #include <net/if_types.h>
78 #include <net/if_mib.h>
79 #include <net/ethernet.h>
80 #include <net/route.h>
81
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84
85 #include <arpa/inet.h>
86
87 #include <signal.h>
88 #include <stdio.h>
89 #include <string.h>
90 #include <unistd.h>
91 #include <stdlib.h>
92 #include <err.h>
93
94 #include "netstat.h"
95
96 #define YES 1
97 #define NO 0
98
99 #define ROUNDUP(a, size) (((a) & ((size) - 1)) ? (1 + ((a)|(size - 1))) : (a))
100
101 #define NEXT_SA(p) (struct sockaddr *) \
102 ((caddr_t)p + (p->sa_len ? ROUNDUP(p->sa_len, sizeof(uint32_t)) : \
103 sizeof(uint32_t)))
104
105 static void sidewaysintpr ();
106 static void catchalarm (int);
107
108 #ifdef INET6
109 char *netname6 (struct sockaddr_in6 *, struct sockaddr *);
110 static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
111 #endif
112
113 /*
114 * Display a formatted value, or a '-' in the same space.
115 */
116 static void
117 show_stat(const char *fmt, int width, u_int64_t value, short showvalue)
118 {
119 char newfmt[32];
120
121 /* Construct the format string */
122 if (showvalue) {
123 sprintf(newfmt, "%%%d%s", width, fmt);
124 printf(newfmt, value);
125 } else {
126 sprintf(newfmt, "%%%ds", width);
127 printf(newfmt, "-");
128 }
129 }
130
131 size_t
132 get_rti_info(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
133 {
134 int i;
135 size_t len = 0;
136
137 for (i = 0; i < RTAX_MAX; i++) {
138 if (addrs & (1 << i)) {
139 rti_info[i] = sa;
140 if (sa->sa_len < sizeof(struct sockaddr))
141 len += sizeof(struct sockaddr);
142 else
143 len += sa->sa_len;
144 sa = NEXT_SA(sa);
145 } else {
146 rti_info[i] = NULL;
147 }
148 }
149 return len;
150 }
151
152 static void
153 multipr(int family, char *buf, char *lim)
154 {
155 char *next;
156
157 for (next = buf; next < lim; ) {
158 struct ifma_msghdr2 *ifmam = (struct ifma_msghdr2 *)next;
159 struct sockaddr *rti_info[RTAX_MAX];
160 struct sockaddr *sa;
161 const char *fmt = 0;
162
163 next += ifmam->ifmam_msglen;
164 if (ifmam->ifmam_type == RTM_IFINFO2)
165 break;
166 else if (ifmam->ifmam_type != RTM_NEWMADDR2)
167 continue;
168 get_rti_info(ifmam->ifmam_addrs, (struct sockaddr*)(ifmam + 1), rti_info);
169 sa = rti_info[RTAX_IFA];
170
171 if (sa->sa_family != family)
172 continue;
173 switch (sa->sa_family) {
174 case AF_INET: {
175 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
176
177 fmt = routename(sin->sin_addr.s_addr);
178 break;
179 }
180 #ifdef INET6
181 case AF_INET6: {
182 struct sockaddr_in6 sin6;
183
184 memcpy(&sin6, sa, sizeof(struct sockaddr_in6));
185
186 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
187 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) {
188 sin6.sin6_scope_id = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
189 sin6.sin6_addr.s6_addr[2] = 0;
190 sin6.sin6_addr.s6_addr[3] = 0;
191 }
192
193 printf("%23s %-19.19s(refs: %d)\n", "",
194 inet_ntop(AF_INET6, &sin6.sin6_addr,
195 ntop_buf, sizeof(ntop_buf)),
196 ifmam->ifmam_refcount);
197 break;
198 }
199 #endif /* INET6 */
200 case AF_LINK: {
201 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
202
203 switch (sdl->sdl_type) {
204 case IFT_ETHER:
205 case IFT_FDDI:
206 fmt = ether_ntoa((struct ether_addr *)
207 LLADDR(sdl));
208 break;
209 }
210 break;
211 }
212 }
213 if (fmt)
214 printf("%23s %s\n", "", fmt);
215 }
216 }
217
218 /*
219 * Print a description of the network interfaces.
220 */
221 void
222 intpr(void (*pfunc)(char *))
223 {
224 u_int64_t opackets = 0;
225 u_int64_t ipackets = 0;
226 u_int64_t obytes = 0;
227 u_int64_t ibytes = 0;
228 u_int64_t oerrors = 0;
229 u_int64_t ierrors = 0;
230 u_int64_t collisions = 0;
231 uint32_t mtu = 0;
232 short timer = 0;
233 int drops = 0;
234 struct sockaddr *sa = NULL;
235 char name[32];
236 short network_layer;
237 short link_layer;
238 int mib[6];
239 char *buf = NULL, *lim, *next;
240 size_t len;
241 struct if_msghdr *ifm;
242 struct sockaddr *rti_info[RTAX_MAX];
243 unsigned int ifindex = 0;
244
245 if (interval) {
246 sidewaysintpr();
247 return;
248 }
249
250 if (interface != 0)
251 ifindex = if_nametoindex(interface);
252
253 mib[0] = CTL_NET; // networking subsystem
254 mib[1] = PF_ROUTE; // type of information
255 mib[2] = 0; // protocol (IPPROTO_xxx)
256 mib[3] = 0; // address family
257 mib[4] = NET_RT_IFLIST2; // operation
258 mib[5] = 0;
259 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
260 return;
261 if ((buf = malloc(len)) == NULL) {
262 printf("malloc failed\n");
263 exit(1);
264 }
265 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
266 if (buf)
267 free(buf);
268 return;
269 }
270 if (!pfunc) {
271 printf("%-5.5s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
272 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
273 if (bflag)
274 printf(" %10.10s","Ibytes");
275 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
276 if (bflag)
277 printf(" %10.10s","Obytes");
278 printf(" %5s", "Coll");
279 if (tflag)
280 printf(" %s", "Time");
281 if (dflag)
282 printf(" %s", "Drop");
283 putchar('\n');
284 }
285 lim = buf + len;
286 for (next = buf; next < lim; ) {
287 char *cp;
288 int n, m;
289
290 network_layer = 0;
291 link_layer = 0;
292 ifm = (struct if_msghdr *)next;
293 next += ifm->ifm_msglen;
294
295 if (ifm->ifm_type == RTM_IFINFO2) {
296 struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm;
297 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(if2m + 1);
298
299 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
300 name[sdl->sdl_nlen] = 0;
301 if (interface != 0 && if2m->ifm_index != ifindex)
302 continue;
303 cp = index(name, '\0');
304
305 if (pfunc) {
306 (*pfunc)(name);
307 continue;
308 }
309
310 if ((if2m->ifm_flags & IFF_UP) == 0)
311 *cp++ = '*';
312 *cp = '\0';
313
314 /*
315 * Get the interface stats. These may get
316 * overriden below on a per-interface basis.
317 */
318 opackets = if2m->ifm_data.ifi_opackets;
319 ipackets = if2m->ifm_data.ifi_ipackets;
320 obytes = if2m->ifm_data.ifi_obytes;
321 ibytes = if2m->ifm_data.ifi_ibytes;
322 oerrors =if2m->ifm_data.ifi_oerrors;
323 ierrors = if2m->ifm_data.ifi_ierrors;
324 collisions = if2m->ifm_data.ifi_collisions;
325 timer = if2m->ifm_timer;
326 drops = if2m->ifm_snd_drops;
327 mtu = if2m->ifm_data.ifi_mtu;
328
329 get_rti_info(if2m->ifm_addrs, (struct sockaddr*)(if2m + 1), rti_info);
330 sa = rti_info[RTAX_IFP];
331 } else if (ifm->ifm_type == RTM_NEWADDR) {
332 struct ifa_msghdr *ifam = (struct ifa_msghdr *)ifm;
333
334 if (interface != 0 && ifam->ifam_index != ifindex)
335 continue;
336 get_rti_info(ifam->ifam_addrs, (struct sockaddr*)(ifam + 1), rti_info);
337 sa = rti_info[RTAX_IFA];
338 } else {
339 continue;
340 }
341 printf("%-5.5s %-5u ", name, mtu);
342
343 if (sa == 0) {
344 printf("%-13.13s ", "none");
345 printf("%-15.15s ", "none");
346 } else {
347 switch (sa->sa_family) {
348 case AF_UNSPEC:
349 printf("%-13.13s ", "none");
350 printf("%-15.15s ", "none");
351 break;
352
353 case AF_INET: {
354 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
355 struct sockaddr_in mask;
356
357 mask.sin_addr.s_addr = 0;
358 memcpy(&mask,
359 rti_info[RTAX_NETMASK],
360 ((struct sockaddr_in *)rti_info[RTAX_NETMASK])->sin_len);
361
362 printf("%-13.13s ",
363 netname(sin->sin_addr.s_addr & mask.sin_addr.s_addr,
364 ntohl(mask.sin_addr.s_addr)));
365
366 printf("%-15.15s ",
367 routename(sin->sin_addr.s_addr));
368
369 network_layer = 1;
370 break;
371 }
372 #ifdef INET6
373 case AF_INET6: {
374 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
375 struct sockaddr *mask = (struct sockaddr *)rti_info[RTAX_NETMASK];
376
377 printf("%-11.11s ",
378 netname6(sin6,
379 mask));
380 printf("%-17.17s ",
381 (char *)inet_ntop(AF_INET6,
382 &sin6->sin6_addr,
383 ntop_buf, sizeof(ntop_buf)));
384
385 network_layer = 1;
386 break;
387 }
388 #endif /*INET6*/
389 case AF_LINK: {
390 struct sockaddr_dl *sdl =
391 (struct sockaddr_dl *)sa;
392 char linknum[10];
393 cp = (char *)LLADDR(sdl);
394 n = sdl->sdl_alen;
395 sprintf(linknum, "<Link#%d>", sdl->sdl_index);
396 m = printf("%-11.11s ", linknum);
397 goto hexprint;
398 }
399
400 default:
401 m = printf("(%d)", sa->sa_family);
402 for (cp = sa->sa_len + (char *)sa;
403 --cp > sa->sa_data && (*cp == 0);) {}
404 n = cp - sa->sa_data + 1;
405 cp = sa->sa_data;
406 hexprint:
407 while (--n >= 0)
408 m += printf("%02x%c", *cp++ & 0xff,
409 n > 0 ? ':' : ' ');
410 m = 30 - m;
411 while (m-- > 0)
412 putchar(' ');
413
414 link_layer = 1;
415 break;
416 }
417 }
418
419 show_stat("llu", 8, ipackets, link_layer|network_layer);
420 printf(" ");
421 show_stat("llu", 5, ierrors, link_layer);
422 printf(" ");
423 if (bflag) {
424 show_stat("llu", 10, ibytes, link_layer|network_layer);
425 printf(" ");
426 }
427 show_stat("llu", 8, opackets, link_layer|network_layer);
428 printf(" ");
429 show_stat("llu", 5, oerrors, link_layer);
430 printf(" ");
431 if (bflag) {
432 show_stat("llu", 10, obytes, link_layer|network_layer);
433 printf(" ");
434 }
435 show_stat("llu", 5, collisions, link_layer);
436 if (tflag) {
437 printf(" ");
438 show_stat("ll", 3, timer, link_layer);
439 }
440 if (dflag) {
441 printf(" ");
442 show_stat("ll", 3, drops, link_layer);
443 }
444 putchar('\n');
445
446 if (aflag)
447 multipr(sa->sa_family, next, lim);
448 }
449 }
450
451 struct iftot {
452 SLIST_ENTRY(iftot) chain;
453 char ift_name[16]; /* interface name */
454 u_int64_t ift_ip; /* input packets */
455 u_int64_t ift_ie; /* input errors */
456 u_int64_t ift_op; /* output packets */
457 u_int64_t ift_oe; /* output errors */
458 u_int64_t ift_co; /* collisions */
459 u_int64_t ift_dr; /* drops */
460 u_int64_t ift_ib; /* input bytes */
461 u_int64_t ift_ob; /* output bytes */
462 u_int64_t ift_itcp; /* input tc packets */
463 u_int64_t ift_itcb; /* input tc bytes */
464 u_int64_t ift_otcp; /* output tc packets */
465 u_int64_t ift_otcb; /* output tc bytes */
466 };
467
468 u_char signalled; /* set if alarm goes off "early" */
469
470 /*
471 * Print a running summary of interface statistics.
472 * Repeat display every interval seconds, showing statistics
473 * collected over that interval. Assumes that interval is non-zero.
474 * First line printed at top of screen is always cumulative.
475 * XXX - should be rewritten to use ifmib(4).
476 */
477 static void
478 sidewaysintpr()
479 {
480 struct iftot *total, *sum, *interesting;
481 register int line;
482 int first;
483 int name[6];
484 size_t len;
485 unsigned int ifcount, i;
486 struct ifmibdata *ifmdall = 0;
487 int interesting_row;
488 sigset_t sigset, oldsigset;
489 struct itimerval timer_interval;
490
491 /* Common OID prefix */
492 name[0] = CTL_NET;
493 name[1] = PF_LINK;
494 name[2] = NETLINK_GENERIC;
495
496 len = sizeof(int);
497 name[3] = IFMIB_SYSTEM;
498 name[4] = IFMIB_IFCOUNT;
499 if (sysctl(name, 5, &ifcount, &len, 0, 0) == 1)
500 err(1, "sysctl IFMIB_IFCOUNT");
501
502 len = ifcount * sizeof(struct ifmibdata);
503 ifmdall = malloc(len);
504 if (ifmdall == 0)
505 err(1, "malloc failed");
506 name[3] = IFMIB_IFALLDATA;
507 name[4] = 0;
508 name[5] = IFDATA_GENERAL;
509 if (sysctl(name, 6, ifmdall, &len, (void *)0, 0) == -1)
510 err(1, "sysctl IFMIB_IFALLDATA");
511
512 interesting = NULL;
513 interesting_row = 0;
514 for (i = 0; i < ifcount; i++) {
515 struct ifmibdata *ifmd = ifmdall + i;
516
517 if (interface && strcmp(ifmd->ifmd_name, interface) == 0) {
518 if ((interesting = calloc(ifcount, sizeof(struct iftot))) == NULL)
519 err(1, "malloc failed");
520 interesting_row = i + 1;
521 snprintf(interesting->ift_name, 16, "(%s)", ifmd->ifmd_name);;
522 }
523 }
524 if ((total = calloc(1, sizeof(struct iftot))) == NULL)
525 err(1, "malloc failed");
526
527 if ((sum = calloc(1, sizeof(struct iftot))) == NULL)
528 err(1, "malloc failed");
529
530 /* create a timer that fires repeatedly every interval seconds */
531 timer_interval.it_value.tv_sec = interval;
532 timer_interval.it_value.tv_usec = 0;
533 timer_interval.it_interval.tv_sec = interval;
534 timer_interval.it_interval.tv_usec = 0;
535 (void)signal(SIGALRM, catchalarm);
536 signalled = NO;
537 (void)setitimer(ITIMER_REAL, &timer_interval, NULL);
538 first = 1;
539 banner:
540 if (prioflag)
541 printf("%37s %14s %16s", "input",
542 interesting ? interesting->ift_name : "(Total)", "output");
543 else
544 printf("%17s %14s %16s", "input",
545 interesting ? interesting->ift_name : "(Total)", "output");
546 putchar('\n');
547 printf("%10s %5s %10s ",
548 "packets", "errs", "bytes");
549 if (prioflag)
550 printf(" %10s %10s", "tcpkts", "tcbytes");
551 printf("%10s %5s %10s %5s",
552 "packets", "errs", "bytes", "colls");
553 if (dflag)
554 printf(" %5.5s", "drops");
555 if (prioflag)
556 printf(" %10s %10s", "tcpkts", "tcbytes");
557 putchar('\n');
558 fflush(stdout);
559 line = 0;
560 loop:
561 if (interesting != NULL) {
562 struct ifmibdata ifmd;
563 struct ifmibdata_supplemental ifmsupp;
564
565 len = sizeof(struct ifmibdata);
566 name[3] = IFMIB_IFDATA;
567 name[4] = interesting_row;
568 name[5] = IFDATA_GENERAL;
569 if (sysctl(name, 6, &ifmd, &len, (void *)0, 0) == -1)
570 err(1, "sysctl IFDATA_GENERAL %d", interesting_row);
571
572 if (prioflag) {
573 len = sizeof(struct ifmibdata_supplemental);
574 name[3] = IFMIB_IFDATA;
575 name[4] = interesting_row;
576 name[5] = IFDATA_SUPPLEMENTAL;
577 if (sysctl(name, 6, &ifmsupp, &len, (void *)0, 0) == -1)
578 err(1, "sysctl IFDATA_SUPPLEMENTAL %d", interesting_row);
579 }
580 if (!first) {
581 printf("%10llu %5llu %10llu ",
582 ifmd.ifmd_data.ifi_ipackets - interesting->ift_ip,
583 ifmd.ifmd_data.ifi_ierrors - interesting->ift_ie,
584 ifmd.ifmd_data.ifi_ibytes - interesting->ift_ib);
585 switch (prioflag) {
586 case SO_TC_BK:
587 printf("%10llu %10llu ",
588 ifmsupp.ifmd_traffic_class.ifi_ibkpackets - interesting->ift_itcp,
589 ifmsupp.ifmd_traffic_class.ifi_ibkbytes - interesting->ift_itcb);
590 break;
591 case SO_TC_VI:
592 printf("%10llu %10llu ",
593 ifmsupp.ifmd_traffic_class.ifi_ivipackets - interesting->ift_itcp,
594 ifmsupp.ifmd_traffic_class.ifi_ivibytes - interesting->ift_itcb);
595 break;
596 case SO_TC_VO:
597 printf("%10llu %10llu ",
598 ifmsupp.ifmd_traffic_class.ifi_ivopackets - interesting->ift_itcp,
599 ifmsupp.ifmd_traffic_class.ifi_ivobytes - interesting->ift_itcb);
600 break;
601 default:
602 break;
603 }
604 printf("%10llu %5llu %10llu %5llu",
605 ifmd.ifmd_data.ifi_opackets - interesting->ift_op,
606 ifmd.ifmd_data.ifi_oerrors - interesting->ift_oe,
607 ifmd.ifmd_data.ifi_obytes - interesting->ift_ob,
608 ifmd.ifmd_data.ifi_collisions - interesting->ift_co);
609 if (dflag)
610 printf(" %5llu", ifmd.ifmd_snd_drops - interesting->ift_dr);
611 switch (prioflag) {
612 case SO_TC_BK:
613 printf(" %10llu %10llu",
614 ifmsupp.ifmd_traffic_class.ifi_obkpackets - interesting->ift_otcp,
615 ifmsupp.ifmd_traffic_class.ifi_obkbytes - interesting->ift_otcb);
616 break;
617 case SO_TC_VI:
618 printf(" %10llu %10llu",
619 ifmsupp.ifmd_traffic_class.ifi_ovipackets - interesting->ift_otcp,
620 ifmsupp.ifmd_traffic_class.ifi_ovibytes - interesting->ift_otcb);
621 break;
622 case SO_TC_VO:
623 printf(" %10llu %10llu",
624 ifmsupp.ifmd_traffic_class.ifi_ovopackets - interesting->ift_otcp,
625 ifmsupp.ifmd_traffic_class.ifi_ovobytes - interesting->ift_otcb);
626 break;
627 default:
628 break;
629 }
630 }
631 interesting->ift_ip = ifmd.ifmd_data.ifi_ipackets;
632 interesting->ift_ie = ifmd.ifmd_data.ifi_ierrors;
633 interesting->ift_ib = ifmd.ifmd_data.ifi_ibytes;
634 interesting->ift_op = ifmd.ifmd_data.ifi_opackets;
635 interesting->ift_oe = ifmd.ifmd_data.ifi_oerrors;
636 interesting->ift_ob = ifmd.ifmd_data.ifi_obytes;
637 interesting->ift_co = ifmd.ifmd_data.ifi_collisions;
638 interesting->ift_dr = ifmd.ifmd_snd_drops;
639 /* private counters */
640 switch (prioflag) {
641 case SO_TC_BK:
642 interesting->ift_itcp = ifmsupp.ifmd_traffic_class.ifi_ibkpackets;
643 interesting->ift_itcb = ifmsupp.ifmd_traffic_class.ifi_ibkbytes;
644 interesting->ift_otcp = ifmsupp.ifmd_traffic_class.ifi_obkpackets;
645 interesting->ift_otcb = ifmsupp.ifmd_traffic_class.ifi_obkbytes;
646 break;
647 case SO_TC_VI:
648 interesting->ift_itcp = ifmsupp.ifmd_traffic_class.ifi_ivipackets;
649 interesting->ift_itcb = ifmsupp.ifmd_traffic_class.ifi_ivibytes;
650 interesting->ift_otcp = ifmsupp.ifmd_traffic_class.ifi_ovipackets;
651 interesting->ift_otcb = ifmsupp.ifmd_traffic_class.ifi_ovibytes;
652 break;
653 case SO_TC_VO:
654 interesting->ift_itcp = ifmsupp.ifmd_traffic_class.ifi_ivopackets;
655 interesting->ift_itcb = ifmsupp.ifmd_traffic_class.ifi_ivobytes;
656 interesting->ift_otcp = ifmsupp.ifmd_traffic_class.ifi_ovopackets;
657 interesting->ift_otcb = ifmsupp.ifmd_traffic_class.ifi_ovobytes;
658 break;
659 default:
660 break;
661 }
662 } else {
663 unsigned int latest_ifcount;
664 struct ifmibdata_supplemental *ifmsuppall = NULL;
665
666 len = sizeof(int);
667 name[3] = IFMIB_SYSTEM;
668 name[4] = IFMIB_IFCOUNT;
669 if (sysctl(name, 5, &latest_ifcount, &len, 0, 0) == 1)
670 err(1, "sysctl IFMIB_IFCOUNT");
671 if (latest_ifcount > ifcount) {
672 ifcount = latest_ifcount;
673 len = ifcount * sizeof(struct ifmibdata);
674 free(ifmdall);
675 ifmdall = malloc(len);
676 if (ifmdall == 0)
677 err(1, "malloc ifmdall failed");
678 } else if (latest_ifcount > ifcount) {
679 ifcount = latest_ifcount;
680 len = ifcount * sizeof(struct ifmibdata);
681 }
682 len = ifcount * sizeof(struct ifmibdata);
683 name[3] = IFMIB_IFALLDATA;
684 name[4] = 0;
685 name[5] = IFDATA_GENERAL;
686 if (sysctl(name, 6, ifmdall, &len, (void *)0, 0) == -1)
687 err(1, "sysctl IFMIB_IFALLDATA");
688 if (prioflag) {
689 len = ifcount * sizeof(struct ifmibdata_supplemental);
690 ifmsuppall = malloc(len);
691 if (ifmsuppall == NULL)
692 err(1, "malloc ifmsuppall failed");
693 name[3] = IFMIB_IFALLDATA;
694 name[4] = 0;
695 name[5] = IFDATA_SUPPLEMENTAL;
696 if (sysctl(name, 6, ifmsuppall, &len, (void *)0, 0) == -1)
697 err(1, "sysctl IFMIB_IFALLDATA SUPPLEMENTAL");
698 }
699 sum->ift_ip = 0;
700 sum->ift_ie = 0;
701 sum->ift_ib = 0;
702 sum->ift_op = 0;
703 sum->ift_oe = 0;
704 sum->ift_ob = 0;
705 sum->ift_co = 0;
706 sum->ift_dr = 0;
707 sum->ift_itcp = 0;
708 sum->ift_itcb = 0;
709 sum->ift_otcp = 0;
710 sum->ift_otcb = 0;
711 for (i = 0; i < ifcount; i++) {
712 struct ifmibdata *ifmd = ifmdall + i;
713
714 sum->ift_ip += ifmd->ifmd_data.ifi_ipackets;
715 sum->ift_ie += ifmd->ifmd_data.ifi_ierrors;
716 sum->ift_ib += ifmd->ifmd_data.ifi_ibytes;
717 sum->ift_op += ifmd->ifmd_data.ifi_opackets;
718 sum->ift_oe += ifmd->ifmd_data.ifi_oerrors;
719 sum->ift_ob += ifmd->ifmd_data.ifi_obytes;
720 sum->ift_co += ifmd->ifmd_data.ifi_collisions;
721 sum->ift_dr += ifmd->ifmd_snd_drops;
722 /* private counters */
723 if (prioflag) {
724 struct ifmibdata_supplemental *ifmsupp = ifmsuppall + i;
725 switch (prioflag) {
726 case SO_TC_BK:
727 sum->ift_itcp += ifmsupp->ifmd_traffic_class.ifi_ibkpackets;
728 sum->ift_itcb += ifmsupp->ifmd_traffic_class.ifi_ibkbytes;
729 sum->ift_otcp += ifmsupp->ifmd_traffic_class.ifi_obkpackets;
730 sum->ift_otcb += ifmsupp->ifmd_traffic_class.ifi_obkbytes;
731 break;
732 case SO_TC_VI:
733 sum->ift_itcp += ifmsupp->ifmd_traffic_class.ifi_ivipackets;
734 sum->ift_itcb += ifmsupp->ifmd_traffic_class.ifi_ivibytes;
735 sum->ift_otcp += ifmsupp->ifmd_traffic_class.ifi_ovipackets;
736 sum->ift_otcb += ifmsupp->ifmd_traffic_class.ifi_ovibytes;
737 break;
738 case SO_TC_VO:
739 sum->ift_itcp += ifmsupp->ifmd_traffic_class.ifi_ivopackets;
740 sum->ift_itcb += ifmsupp->ifmd_traffic_class.ifi_ivobytes;
741 sum->ift_otcp += ifmsupp->ifmd_traffic_class.ifi_ovopackets;
742 sum->ift_otcb += ifmsupp->ifmd_traffic_class.ifi_ovobytes;
743 break;
744 default:
745 break;
746 }
747 }
748 }
749 if (!first) {
750 printf("%10llu %5llu %10llu ",
751 sum->ift_ip - total->ift_ip,
752 sum->ift_ie - total->ift_ie,
753 sum->ift_ib - total->ift_ib);
754 if (prioflag)
755 printf(" %10llu %10llu",
756 sum->ift_itcp - total->ift_itcp,
757 sum->ift_itcb - total->ift_itcb);
758 printf("%10llu %5llu %10llu %5llu",
759 sum->ift_op - total->ift_op,
760 sum->ift_oe - total->ift_oe,
761 sum->ift_ob - total->ift_ob,
762 sum->ift_co - total->ift_co);
763 if (dflag)
764 printf(" %5llu", sum->ift_dr - total->ift_dr);
765 if (prioflag)
766 printf(" %10llu %10llu",
767 sum->ift_otcp - total->ift_otcp,
768 sum->ift_otcb - total->ift_otcb);
769 }
770 *total = *sum;
771 }
772 if (!first)
773 putchar('\n');
774 fflush(stdout);
775 sigemptyset(&sigset);
776 sigaddset(&sigset, SIGALRM);
777 (void)sigprocmask(SIG_BLOCK, &sigset, &oldsigset);
778 if (!signalled) {
779 sigemptyset(&sigset);
780 sigsuspend(&sigset);
781 }
782 (void)sigprocmask(SIG_SETMASK, &oldsigset, NULL);
783
784 signalled = NO;
785 line++;
786 first = 0;
787 if (line == 21)
788 goto banner;
789 else
790 goto loop;
791 /*NOTREACHED*/
792 }
793
794 void
795 intervalpr(void (*pr)(uint32_t, char *, int), uint32_t off, char *name , int af)
796 {
797 struct itimerval timer_interval;
798 sigset_t sigset, oldsigset;
799
800 /* create a timer that fires repeatedly every interval seconds */
801 timer_interval.it_value.tv_sec = interval;
802 timer_interval.it_value.tv_usec = 0;
803 timer_interval.it_interval.tv_sec = interval;
804 timer_interval.it_interval.tv_usec = 0;
805 (void) signal(SIGALRM, catchalarm);
806 signalled = NO;
807 (void) setitimer(ITIMER_REAL, &timer_interval, NULL);
808
809 for (;;) {
810 pr(off, name, af);
811
812 fflush(stdout);
813 sigemptyset(&sigset);
814 sigaddset(&sigset, SIGALRM);
815 (void) sigprocmask(SIG_BLOCK, &sigset, &oldsigset);
816 if (!signalled) {
817 sigemptyset(&sigset);
818 sigsuspend(&sigset);
819 }
820 (void) sigprocmask(SIG_SETMASK, &oldsigset, NULL);
821 signalled = NO;
822 }
823 }
824
825 /*
826 * Called if an interval expires before sidewaysintpr has completed a loop.
827 * Sets a flag to not wait for the alarm.
828 */
829 static void
830 catchalarm(int signo )
831 {
832 signalled = YES;
833 }