2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if.c 8.3 (Berkeley) 4/28/95
30 * $FreeBSD: src/usr.bin/netstat/if.c,v 1.32.2.9 2001/09/17 14:35:46 ru Exp $
33 #define _KERNEL_STRUCTURES
34 #include <sys/param.h>
35 #include <sys/protosw.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
41 #include <net/if_var.h>
42 #include <net/if_dl.h>
43 #include <net/if_types.h>
44 #include <net/ethernet.h>
45 #include <netinet/in.h>
46 #include <netinet/in_var.h>
47 #include <arpa/inet.h>
61 static void sidewaysintpr (u_int, u_long, int);
62 static void catchalarm (int);
65 static char ntop_buf[INET6_ADDRSTRLEN]; /* for inet_ntop() */
70 * Display a formatted value, or a '-' in the same space.
73 show_stat(const char *fmt, int width, u_long value, short showvalue)
78 /* no value, just the dash */
79 sprintf(newfmt, "%%%ds", width);
84 if (hflag) { /* human-readable */
86 humanize_number(buf, sizeof buf, (int64_t) value, "",
88 HN_NOSPACE | HN_DECIMAL);
89 sprintf(newfmt, "%%%ds", width);
92 sprintf(newfmt, "%%%d%s", width, fmt);
93 printf(newfmt, value);
100 * Print a description of the network interfaces.
103 intpr(int interval1, u_long ifnetaddr, void (*pfunc)(char *), u_long ncpusaddr)
106 struct ifdata_pcpu ifdata;
107 struct ifaddr_container ifac;
108 struct ifnethead ifnethead;
113 struct in6_ifaddr in6;
117 u_long ifaddrcont_addr;
129 struct sockaddr *sa = NULL;
135 if (kread(ncpusaddr, (char *)&ncpus, sizeof(ncpus)))
138 if (ifnetaddr == 0) {
139 printf("ifnet: symbol not defined\n");
143 sidewaysintpr((unsigned)interval1, ifnetaddr, ncpus);
146 if (kread(ifnetaddr, (char *)&ifnethead, sizeof ifnethead))
148 ifnetaddr = (u_long)TAILQ_FIRST(&ifnethead);
149 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
153 printf("%-7.7s %-5.5s %-13.13s %-15.15s %8.8s %5.5s",
154 "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
156 printf(" %10.10s","Ibytes");
157 printf(" %8.8s %5.5s", "Opkts", "Oerrs");
159 printf(" %10.10s","Obytes");
160 printf(" %5s", "Coll");
162 printf(" %s", "Time");
164 printf(" %s", "Drop");
169 while (ifnetaddr || ifaddraddr) {
170 struct sockaddr_in *sin;
172 struct sockaddr_in6 *sin6;
180 if (ifaddraddr == 0) {
181 struct ifaddrhead head;
183 if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
185 strlcpy(name, ifnet.if_xname, sizeof(name));
186 ifnetaddr = (u_long)TAILQ_NEXT(&ifnet, if_link);
187 if (interface != 0 && (strcmp(name, interface) != 0))
189 cp = strchr(name, '\0');
196 if ((ifnet.if_flags&IFF_UP) == 0)
200 if (kread((u_long)ifnet.if_addrheads,
201 (char *)&head, sizeof(head)))
205 (u_long)TAILQ_FIRST(&head);
206 if (ifaddrcont_addr == 0) {
209 if (kread(ifaddrcont_addr, (char *)&ifac,
212 ifaddraddr = (u_long)ifac.ifa;
215 ifaddrfound = ifaddraddr;
218 * Get the interface stats. These may get
219 * overriden below on a per-interface basis.
221 ifdataaddr = (u_long)ifnet.if_data_pcpu;
222 if (kread(ifdataaddr, (char *)&ifdata, sizeof(ifdata)))
224 opackets = ifdata.ifd_opackets;
225 ipackets = ifdata.ifd_ipackets;
226 obytes = ifdata.ifd_obytes;
227 ibytes = ifdata.ifd_ibytes;
228 oerrors = ifdata.ifd_oerrors;
229 ierrors = ifdata.ifd_ierrors;
230 collisions = ifdata.ifd_collisions;
231 oqdrops = ifdata.ifd_oqdrops;
233 for (cpu = 1; cpu < ncpus; ++cpu) {
234 if (kread(ifdataaddr + (cpu * sizeof(ifdata)),
235 (char *)&ifdata, sizeof(ifdata)))
237 opackets += ifdata.ifd_opackets;
238 ipackets += ifdata.ifd_ipackets;
239 obytes += ifdata.ifd_obytes;
240 ibytes += ifdata.ifd_ibytes;
241 oerrors += ifdata.ifd_oerrors;
242 ierrors += ifdata.ifd_ierrors;
243 collisions += ifdata.ifd_collisions;
244 oqdrops += ifdata.ifd_oqdrops;
247 timer = ifnet.if_timer;
249 if (ifaddraddr == 0) {
250 printf("%-7.7s %-5lu ", name, ifnet.if_mtu);
251 printf("%-13.13s ", "none");
252 printf("%-15.15s ", "none");
254 if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
259 ifaddr.ifa.if_ipackets = ifac.ifa_ipackets;
260 ifaddr.ifa.if_ibytes = ifac.ifa_ibytes;
261 ifaddr.ifa.if_opackets = ifac.ifa_opackets;
262 ifaddr.ifa.if_obytes = ifac.ifa_obytes;
263 for (cpu = 1; cpu < ncpus; ++cpu) {
264 struct ifaddr_container nifac;
266 if (kread(ifaddrcont_addr +
267 (cpu * sizeof(nifac)),
268 (char *)&nifac, sizeof(nifac))) {
272 ifaddr.ifa.if_ipackets += nifac.ifa_ipackets;
273 ifaddr.ifa.if_ibytes += nifac.ifa_ibytes;
274 ifaddr.ifa.if_opackets += nifac.ifa_opackets;
275 ifaddr.ifa.if_obytes += nifac.ifa_obytes;
278 #define CP(x) ((char *)(x))
279 cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
281 sa = (struct sockaddr *)cp;
282 if (af != AF_UNSPEC && sa->sa_family != af) {
284 (u_long)TAILQ_NEXT(&ifac, ifa_link);
285 if (ifaddrcont_addr == 0) {
288 if (kread(ifaddrcont_addr,
289 (char *)&ifac, sizeof(ifac))) {
293 ifaddraddr = (u_long)ifac.ifa;
297 printf("%-7.7s %-5lu ", name, ifnet.if_mtu);
298 switch (sa->sa_family) {
300 printf("%-13.13s ", "none");
301 printf("%-15.15s ", "none");
304 sin = (struct sockaddr_in *)sa;
306 /* can't use inet_makeaddr because kernel
307 * keeps nets unshifted.
309 in = inet_makeaddr(ifaddr.in.ia_subnet,
311 printf("%-13.13s ", netname(in.s_addr,
312 ifaddr.in.ia_subnetmask));
315 netname(htonl(ifaddr.in.ia_subnet),
316 ifaddr.in.ia_subnetmask));
319 routename(sin->sin_addr.s_addr));
325 sin6 = (struct sockaddr_in6 *)sa;
327 netname6(&ifaddr.in6.ia_addr,
328 &ifaddr.in6.ia_prefixmask.sin6_addr));
332 ntop_buf, sizeof(ntop_buf)));
339 struct sockaddr_dl *sdl =
340 (struct sockaddr_dl *)sa;
342 cp = (char *)LLADDR(sdl);
344 sprintf(linknum, "<Link#%hu>", sdl->sdl_index);
345 m = printf("%-11.11s ", linknum);
349 m = printf("(%d)", sa->sa_family);
350 for (cp = sa->sa_len + (char *)sa;
351 --cp > sa->sa_data && (*cp == 0);) {}
352 n = cp - sa->sa_data + 1;
356 m += printf("%02x%c", *cp++ & 0xff,
367 * Fixup the statistics for interfaces that
368 * update stats for their network addresses
371 opackets = ifaddr.ifa.if_opackets;
372 ipackets = ifaddr.ifa.if_ipackets;
373 obytes = ifaddr.ifa.if_obytes;
374 ibytes = ifaddr.ifa.if_ibytes;
378 (u_long)TAILQ_NEXT(&ifac, ifa_link);
379 if (ifaddrcont_addr == 0) {
382 if (kread(ifaddrcont_addr,
383 (char *)&ifac, sizeof(ifac))) {
386 ifaddraddr = (u_long)ifac.ifa;
391 show_stat("lu", 8, ipackets, link_layer|network_layer);
393 show_stat("lu", 5, ierrors, link_layer);
396 show_stat("lu", 10, ibytes, link_layer|network_layer);
399 show_stat("lu", 8, opackets, link_layer|network_layer);
401 show_stat("lu", 5, oerrors, link_layer);
404 show_stat("lu", 10, obytes, link_layer|network_layer);
407 show_stat("lu", 5, collisions, link_layer);
410 show_stat("d", 3, timer, link_layer);
414 show_stat("lu", 5, oqdrops, link_layer);
417 if (aflag && ifaddrfound) {
419 * Print family's multicast addresses
421 struct ifmultiaddr *multiaddr;
422 struct ifmultiaddr ifma;
425 struct sockaddr_in in;
427 struct sockaddr_in6 in6;
429 struct sockaddr_dl dl;
433 TAILQ_FOREACH(multiaddr, &ifnet.if_multiaddrs, ifma_link) {
434 if (kread((u_long)multiaddr, (char *)&ifma,
438 if (kread((u_long)ifma.ifma_addr, (char *)&msa,
441 if (msa.sa.sa_family != sa->sa_family)
445 switch (msa.sa.sa_family) {
447 fmt = routename(msa.in.sin_addr.s_addr);
451 printf("%23s %-19.19s(refs: %d)\n", "",
460 switch (msa.dl.sdl_type) {
464 (struct ether_addr *)
471 printf("%23s %s\n", "", fmt);
478 SLIST_ENTRY(iftot) chain;
479 char ift_name[IFNAMSIZ]; /* interface name */
480 u_long ift_ip; /* input packets */
481 u_long ift_ie; /* input errors */
482 u_long ift_op; /* output packets */
483 u_long ift_oe; /* output errors */
484 u_long ift_co; /* collisions */
485 u_long ift_dr; /* drops */
486 u_long ift_ib; /* input bytes */
487 u_long ift_ob; /* output bytes */
490 u_char signalled; /* set if alarm goes off "early" */
493 * Print a running summary of interface statistics.
494 * Repeat display every interval1 seconds, showing statistics
495 * collected over that interval. Assumes that interval1 is non-zero.
496 * First line printed at top of screen is always cumulative.
497 * XXX - should be rewritten to use ifmib(4).
500 sidewaysintpr(unsigned interval1, u_long off, int ncpus)
504 struct ifnethead ifnethead;
505 struct ifdata_pcpu ifdata;
506 struct iftot *iftot, *ip, *ipn, *total, *sum, *interesting;
509 u_long interesting_off;
512 if (kread(off, (char *)&ifnethead, sizeof ifnethead))
514 firstifnet = (u_long)TAILQ_FIRST(&ifnethead);
516 if ((iftot = malloc(sizeof(struct iftot))) == NULL) {
517 printf("malloc failed\n");
520 memset(iftot, 0, sizeof(struct iftot));
524 for (off = firstifnet, ip = iftot; off;) {
527 if (kread(off, (char *)&ifnet, sizeof ifnet))
529 strlcpy(name, ifnet.if_xname, sizeof(name));
530 if (interface && strcmp(name, interface) == 0) {
532 interesting_off = off;
534 snprintf(ip->ift_name, 16, "(%.13s)", name);
535 if ((ipn = malloc(sizeof(struct iftot))) == NULL) {
536 printf("malloc failed\n");
539 memset(ipn, 0, sizeof(struct iftot));
540 SLIST_NEXT(ip, chain) = ipn;
542 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
544 if ((total = malloc(sizeof(struct iftot))) == NULL) {
545 printf("malloc failed\n");
548 memset(total, 0, sizeof(struct iftot));
549 if ((sum = malloc(sizeof(struct iftot))) == NULL) {
550 printf("malloc failed\n");
553 memset(sum, 0, sizeof(struct iftot));
556 (void)signal(SIGALRM, catchalarm);
558 (void)alarm(interval1);
561 printf("%17s %14s %16s", "input",
562 interesting ? interesting->ift_name : "(Total)", "output");
564 printf("%10s %5s %10s %10s %5s %10s %5s",
565 "packets", "errs", "bytes", "packets", "errs", "bytes", "colls");
567 printf(" %5.5s", "drops");
572 if (interesting != NULL) {
574 if (kread(interesting_off, (char *)&ifnet, sizeof ifnet)) {
579 ifdata_addr = (u_long)ifnet.if_data_pcpu;
580 if (kread(ifdata_addr, (char *)&ifdata, sizeof(ifdata))) {
581 printf("ifdata 1\n");
584 ifnet.if_ipackets = ifdata.ifd_ipackets;
585 ifnet.if_ierrors = ifdata.ifd_ierrors;
586 ifnet.if_ibytes = ifdata.ifd_ibytes;
587 ifnet.if_opackets = ifdata.ifd_opackets;
588 ifnet.if_oerrors = ifdata.ifd_oerrors;
589 ifnet.if_obytes = ifdata.ifd_obytes;
590 ifnet.if_collisions = ifdata.ifd_collisions;
591 ifnet.if_oqdrops = ifdata.ifd_oqdrops;
593 for (cpu = 1; cpu < ncpus; ++cpu) {
594 if (kread(ifdata_addr + (cpu * sizeof(ifdata)),
595 (char *)&ifdata, sizeof(ifdata))) {
596 printf("ifdata 2\n");
599 ifnet.if_ipackets += ifdata.ifd_ipackets;
600 ifnet.if_ierrors += ifdata.ifd_ierrors;
601 ifnet.if_ibytes += ifdata.ifd_ibytes;
602 ifnet.if_opackets += ifdata.ifd_opackets;
603 ifnet.if_oerrors += ifdata.ifd_oerrors;
604 ifnet.if_obytes += ifdata.ifd_obytes;
605 ifnet.if_collisions += ifdata.ifd_collisions;
606 ifnet.if_oqdrops += ifdata.ifd_oqdrops;
610 printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu",
611 ifnet.if_ipackets - ip->ift_ip,
612 ifnet.if_ierrors - ip->ift_ie,
613 ifnet.if_ibytes - ip->ift_ib,
614 ifnet.if_opackets - ip->ift_op,
615 ifnet.if_oerrors - ip->ift_oe,
616 ifnet.if_obytes - ip->ift_ob,
617 ifnet.if_collisions - ip->ift_co);
619 printf(" %5lu", ifnet.if_oqdrops - ip->ift_dr);
621 ip->ift_ip = ifnet.if_ipackets;
622 ip->ift_ie = ifnet.if_ierrors;
623 ip->ift_ib = ifnet.if_ibytes;
624 ip->ift_op = ifnet.if_opackets;
625 ip->ift_oe = ifnet.if_oerrors;
626 ip->ift_ob = ifnet.if_obytes;
627 ip->ift_co = ifnet.if_collisions;
628 ip->ift_dr = ifnet.if_oqdrops;
638 for (off = firstifnet, ip = iftot;
639 off && SLIST_NEXT(ip, chain) != NULL;
640 ip = SLIST_NEXT(ip, chain)) {
641 if (kread(off, (char *)&ifnet, sizeof ifnet)) {
646 ifdata_addr = (u_long)ifnet.if_data_pcpu;
647 if (kread(ifdata_addr, (char *)&ifdata,
649 printf("ifdata 3\n");
652 ifnet.if_ipackets = ifdata.ifd_ipackets;
653 ifnet.if_ierrors = ifdata.ifd_ierrors;
654 ifnet.if_ibytes = ifdata.ifd_ibytes;
655 ifnet.if_opackets = ifdata.ifd_opackets;
656 ifnet.if_oerrors = ifdata.ifd_oerrors;
657 ifnet.if_obytes = ifdata.ifd_obytes;
658 ifnet.if_collisions = ifdata.ifd_collisions;
659 ifnet.if_oqdrops = ifdata.ifd_oqdrops;
661 for (cpu = 1; cpu < ncpus; ++cpu) {
662 if (kread(ifdata_addr + (cpu * sizeof(ifdata)),
663 (char *)&ifdata, sizeof(ifdata))) {
664 printf("ifdata 2\n");
667 ifnet.if_ipackets += ifdata.ifd_ipackets;
668 ifnet.if_ierrors += ifdata.ifd_ierrors;
669 ifnet.if_ibytes += ifdata.ifd_ibytes;
670 ifnet.if_opackets += ifdata.ifd_opackets;
671 ifnet.if_oerrors += ifdata.ifd_oerrors;
672 ifnet.if_obytes += ifdata.ifd_obytes;
673 ifnet.if_collisions += ifdata.ifd_collisions;
674 ifnet.if_oqdrops += ifdata.ifd_oqdrops;
678 * Don't double-count interfaces that are associated
679 * with bridges, they will be rolled up by the
680 * bridge. Errors and collisions are not rolled up.
682 if (ifnet.if_bridge) {
683 sum->ift_ie += ifnet.if_ierrors;
684 sum->ift_oe += ifnet.if_oerrors;
685 sum->ift_co += ifnet.if_collisions;
687 sum->ift_ip += ifnet.if_ipackets;
688 sum->ift_ie += ifnet.if_ierrors;
689 sum->ift_ib += ifnet.if_ibytes;
690 sum->ift_op += ifnet.if_opackets;
691 sum->ift_oe += ifnet.if_oerrors;
692 sum->ift_ob += ifnet.if_obytes;
693 sum->ift_co += ifnet.if_collisions;
694 sum->ift_dr += ifnet.if_oqdrops;
696 off = (u_long)TAILQ_NEXT(&ifnet, if_link);
699 /* %10lu %5lu %10lu %10lu %5lu %10lu %5lu */
700 show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1);
701 show_stat("lu", 5+1, sum->ift_ie - total->ift_ie, 1);
702 show_stat("lu", 10+1, sum->ift_ib - total->ift_ib, 1);
703 show_stat("lu", 10+1, sum->ift_op - total->ift_op, 1);
704 show_stat("lu", 5+1, sum->ift_oe - total->ift_oe, 1);
705 show_stat("lu", 10+1, sum->ift_ob - total->ift_ob, 1);
706 show_stat("lu", 5+1, sum->ift_co - total->ift_co, 1);
708 show_stat("u", 5+1, sum->ift_dr - total->ift_dr, 1);
715 oldmask = sigblock(sigmask(SIGALRM));
721 (void)alarm(interval1);
732 * Called if an interval expires before sidewaysintpr has completed a loop.
733 * Sets a flag to not wait for the alarm.
736 catchalarm(int signo __unused)