Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/netstat Update for per-cpu interface statistics.



details:   https://anonhg.NetBSD.org/src/rev/2e629a219fbe
branches:  trunk
changeset: 744460:2e629a219fbe
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Sun Feb 02 03:41:12 2020 +0000

description:
Update for per-cpu interface statistics.

diffstat:

 usr.bin/netstat/if.c |  66 +++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 47 insertions(+), 19 deletions(-)

diffs (143 lines):

diff -r 5ca32ff415ff -r 2e629a219fbe usr.bin/netstat/if.c
--- a/usr.bin/netstat/if.c      Sat Feb 01 22:38:31 2020 +0000
+++ b/usr.bin/netstat/if.c      Sun Feb 02 03:41:12 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if.c,v 1.95 2018/07/11 07:46:20 msaitoh Exp $  */
+/*     $NetBSD: if.c,v 1.96 2020/02/02 03:41:12 thorpej Exp $  */
 
 /*
  * Copyright (c) 1983, 1988, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "from: @(#)if.c 8.2 (Berkeley) 2/21/94";
 #else
-__RCSID("$NetBSD: if.c,v 1.95 2018/07/11 07:46:20 msaitoh Exp $");
+__RCSID("$NetBSD: if.c,v 1.96 2020/02/02 03:41:12 thorpej Exp $");
 #endif
 #endif /* not lint */
 
@@ -289,9 +289,34 @@
 };
 
 static void
+ifnet_to_ifdata_kvm(const struct ifnet * const ifp, struct if_data * const ifd)
+{
+
+       /*
+        * Interface statistics are no longer kept in struct ifnet,
+        * and thus an if_data is no longer embedded in struct ifnet.
+        * We cannot read stats via kvm without chasing per-cpu data,
+        * and maybe someday we could do that.  But for now, this is
+        * what we have.
+        *
+        * Just copy the fields that do exist.
+        */
+       memset(ifd, 0, sizeof(*ifd));
+       ifd->ifi_type = ifp->if_type;
+       ifd->ifi_addrlen = ifp->if_addrlen;
+       ifd->ifi_hdrlen = ifp->if_hdrlen;
+       ifd->ifi_link_state = ifp->if_link_state;
+       ifd->ifi_mtu = ifp->if_mtu;
+       ifd->ifi_metric = ifp->if_metric;
+       ifd->ifi_baudrate = ifp->if_baudrate;
+       ifd->ifi_lastchange = ifp->if_lastchange;
+}
+
+static void
 intpr_kvm(u_long ifnetaddr, void (*pfunc)(const char *))
 {
        struct ifnet ifnet;
+       struct if_data ifd;
        union ifaddr_u ifaddr;
        u_long ifaddraddr;
        struct ifnet_head ifhead;       /* TAILQ_HEAD */
@@ -358,8 +383,9 @@
                        cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
                            CP(&ifaddr);
                        sa = (struct sockaddr *)cp;
+                       ifnet_to_ifdata_kvm(&ifnet, &ifd);
                        print_addr(ifnet.if_index, sa, (void *)&ifaddr,
-                           &ifnet.if_data, &ifnet);
+                           &ifd, &ifnet);
                }
                ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
        }
@@ -787,6 +813,7 @@
        sigset_t emptyset;
        sigset_t noalrm;
        struct ifnet ifnet;
+       struct if_data ifd;
        u_long firstifnet;
        struct iftot *ip, *total;
        unsigned line;
@@ -906,53 +933,54 @@
                        off = 0;
                        continue;
                }
+               ifnet_to_ifdata_kvm(&ifnet, &ifd);
                if (ip == interesting) {
                        if (bflag) {
                                char humbuf[HUMBUF_SIZE];
 
                                if (hflag && humanize_number(humbuf,
                                    sizeof(humbuf),
-                                   ifnet.if_ibytes - ip->ift_ib, "",
+                                   ifd.ifi_ibytes - ip->ift_ib, "",
                                    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
                                        printf("%10s %8.8s ", humbuf, " ");
                                else
                                        printf("%10llu %8.8s ", 
                                            (unsigned long long)
-                                           (ifnet.if_ibytes-ip->ift_ib), " ");
+                                           (ifd.ifi_ibytes-ip->ift_ib), " ");
 
                                if (hflag && humanize_number(humbuf,
                                    sizeof(humbuf),
-                                   ifnet.if_obytes - ip->ift_ob, "",
+                                   ifd.ifi_obytes - ip->ift_ob, "",
                                    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
                                        printf("%10s %5.5s", humbuf, " ");
                                else
                                        printf("%10llu %5.5s", 
                                            (unsigned long long)
-                                           (ifnet.if_obytes-ip->ift_ob), " ");
+                                           (ifd.ifi_obytes-ip->ift_ob), " ");
                        } else {
                                printf("%8llu %5llu %8llu %5llu %5llu",
                                    (unsigned long long)
-                                       (ifnet.if_ipackets - ip->ift_ip),
+                                       (ifd.ifi_ipackets - ip->ift_ip),
                                    (unsigned long long)
-                                       (ifnet.if_ierrors - ip->ift_ie),
+                                       (ifd.ifi_ierrors - ip->ift_ie),
                                    (unsigned long long)
-                                       (ifnet.if_opackets - ip->ift_op),
+                                       (ifd.ifi_opackets - ip->ift_op),
                                    (unsigned long long)
-                                       (ifnet.if_oerrors - ip->ift_oe),
+                                       (ifd.ifi_oerrors - ip->ift_oe),
                                    (unsigned long long)
-                                       (ifnet.if_collisions - ip->ift_co));
+                                       (ifd.ifi_collisions - ip->ift_co));
                        }
                        if (dflag)
                                printf(" %5" PRIu64,
                                        ifnet.if_snd.ifq_drops - ip->ift_dr);
                }
-               ip->ift_ip = ifnet.if_ipackets;
-               ip->ift_ib = ifnet.if_ibytes;
-               ip->ift_ie = ifnet.if_ierrors;
-               ip->ift_op = ifnet.if_opackets;
-               ip->ift_ob = ifnet.if_obytes;
-               ip->ift_oe = ifnet.if_oerrors;
-               ip->ift_co = ifnet.if_collisions;
+               ip->ift_ip = ifd.ifi_ipackets;
+               ip->ift_ib = ifd.ifi_ibytes;
+               ip->ift_ie = ifd.ifi_ierrors;
+               ip->ift_op = ifd.ifi_opackets;
+               ip->ift_ob = ifd.ifi_obytes;
+               ip->ift_oe = ifd.ifi_oerrors;
+               ip->ift_co = ifd.ifi_collisions;
                ip->ift_dr = ifnet.if_snd.ifq_drops;
                sum->ift_ip += ip->ift_ip;
                sum->ift_ib += ip->ift_ib;



Home | Main Index | Thread Index | Old Index