diff --git a/examples/netload.c b/examples/netload.c index 641c3012..6fc3d11c 100644 --- a/examples/netload.c +++ b/examples/netload.c @@ -72,17 +72,36 @@ main (int argc, char *argv []) glibtop_get_netload (&netload, argv [1]); - printf ("Network Load (0x%08lx): " - "%lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu\n", + printf ("Network Load (0x%08lx):\n\n" + "\tInterface Flags:\t0x%08lx\n" + "\tSubnet:\t\t\t0x%08lx\n" + "\tAddress:\t\t0x%08lx\n\n" + "\tMTU:\t\t\t%ld\n" + "\tCollisions:\t\t%ld\n\n" + "\tPackets In:\t\t%ld\n" + "\tPackets Out:\t\t%ld\n" + "\tPackets Total:\t\t%ld\n\n" + "\tBytes In:\t\t%ld\n" + "\tBytes Out:\t\t%ld\n" + "\tBytes Total:\t\t%ld\n\n" + "\tErrors In:\t\t%ld\n" + "\tErrors Out:\t\t%ld\n" + "\tErrors Total:\t\t%ld\n\n", (unsigned long) netload.flags, + (unsigned long) netload.if_flags, + (unsigned long) netload.subnet, + (unsigned long) netload.address, (unsigned long) netload.mtu, + (unsigned long) netload.collisions, (unsigned long) netload.packets_in, (unsigned long) netload.packets_out, + (unsigned long) netload.packets_total, (unsigned long) netload.bytes_in, (unsigned long) netload.bytes_out, + (unsigned long) netload.bytes_total, (unsigned long) netload.errors_in, (unsigned long) netload.errors_out, - (unsigned long) netload.collisions); + (unsigned long) netload.errors_total); glibtop_close (); diff --git a/features.def b/features.def index 951b60bd..641a8384 100644 --- a/features.def +++ b/features.def @@ -18,5 +18,5 @@ const char *|proc_args|ulong(size)|pid_t(pid):unsigned(max_len) glibtop_map_entry *|proc_map|ulong(number,size,total)|pid_t(pid) glibtop_mountentry *|@mountlist|ulong(number,size,total)|int(all_fs) void|@fsusage|ulong(blocks,bfree,bavail,files,ffree)|string|mount_dir -void|netload|ulong(mtu,address,packets_in,packets_out,packets_total,bytes_in,bytes_out,bytes_total,errors_in,errors_out,errors_total,collisions)|string|interface +void|netload|ulong(if_flags,mtu,subnet,address,packets_in,packets_out,packets_total,bytes_in,bytes_out,bytes_total,errors_in,errors_out,errors_total,collisions)|string|interface void|ppp|ulong(state,bytes_in,bytes_out)|ushort(device) diff --git a/include/glibtop/netload.h b/include/glibtop/netload.h index 14e3a406..1412c56a 100644 --- a/include/glibtop/netload.h +++ b/include/glibtop/netload.h @@ -27,26 +27,50 @@ __BEGIN_DECLS -#define GLIBTOP_NETLOAD_MTU 0 -#define GLIBTOP_NETLOAD_PACKETS_IN 1 -#define GLIBTOP_NETLOAD_PACKETS_OUT 2 -#define GLIBTOP_NETLOAD_PACKETS_TOTAL 3 -#define GLIBTOP_NETLOAD_BYTES_IN 4 -#define GLIBTOP_NETLOAD_BYTES_OUT 5 -#define GLIBTOP_NETLOAD_BYTES_TOTAL 6 -#define GLIBTOP_NETLOAD_ERRORS_IN 7 -#define GLIBTOP_NETLOAD_ERRORS_OUT 8 -#define GLIBTOP_NETLOAD_ERRORS_TOTAL 9 -#define GLIBTOP_NETLOAD_COLLISIONS 10 +#define GLIBTOP_NETLOAD_IF_FLAGS 0 +#define GLIBTOP_NETLOAD_MTU 1 +#define GLIBTOP_NETLOAD_SUBNET 2 +#define GLIBTOP_NETLOAD_ADDRESS 3 +#define GLIBTOP_NETLOAD_PACKETS_IN 4 +#define GLIBTOP_NETLOAD_PACKETS_OUT 5 +#define GLIBTOP_NETLOAD_PACKETS_TOTAL 6 +#define GLIBTOP_NETLOAD_BYTES_IN 7 +#define GLIBTOP_NETLOAD_BYTES_OUT 8 +#define GLIBTOP_NETLOAD_BYTES_TOTAL 9 +#define GLIBTOP_NETLOAD_ERRORS_IN 10 +#define GLIBTOP_NETLOAD_ERRORS_OUT 11 +#define GLIBTOP_NETLOAD_ERRORS_TOTAL 12 +#define GLIBTOP_NETLOAD_COLLISIONS 13 -#define GLIBTOP_MAX_NETLOAD 11 +#define GLIBTOP_MAX_NETLOAD 14 typedef struct _glibtop_netload glibtop_netload; +enum { + GLIBTOP_IF_FLAGS_UP = 1, + GLIBTOP_IF_FLAGS_BROADCAST, + GLIBTOP_IF_FLAGS_DEBUG, + GLIBTOP_IF_FLAGS_LOOPBACK, + GLIBTOP_IF_FLAGS_POINTOPOINT, + GLIBTOP_IF_FLAGS_RUNNING, + GLIBTOP_IF_FLAGS_NOARP, + GLIBTOP_IF_FLAGS_PROMISC, + GLIBTOP_IF_FLAGS_ALLMULTI, + GLIBTOP_IF_FLAGS_OACTIVE, + GLIBTOP_IF_FLAGS_SIMPLEX, + GLIBTOP_IF_FLAGS_LINK0, + GLIBTOP_IF_FLAGS_LINK1, + GLIBTOP_IF_FLAGS_LINK2, + GLIBTOP_IF_FLAGS_ALTPHYS, + GLIBTOP_IF_FLAGS_MULTICAST +}; + struct _glibtop_netload { u_int64_t flags, + if_flags, /* GLIBTOP_NETLOAD_IF_FLAGS */ mtu, /* GLIBTOP_NETLOAD_MTU */ + subnet, /* GLIBTOP_NETLOAD_SUBNET */ address, /* GLIBTOP_NETLOAD_ADDRESS */ packets_in, /* GLIBTOP_NETLOAD_PACKETS_IN */ packets_out, /* GLIBTOP_NETLOAD_PACKETS_OUT */ diff --git a/sysdeps/freebsd/ChangeLog b/sysdeps/freebsd/ChangeLog index fe9fc503..cb85aee5 100644 --- a/sysdeps/freebsd/ChangeLog +++ b/sysdeps/freebsd/ChangeLog @@ -1,6 +1,6 @@ 1998-10-26 Martin Baulig - * netload.c: New file. Currently empty. + * netload.c: New file to get network load. 1998-10-26 Martin Baulig diff --git a/sysdeps/freebsd/netload.c b/sysdeps/freebsd/netload.c index e8ecaa3c..cc727b6e 100644 --- a/sysdeps/freebsd/netload.c +++ b/sysdeps/freebsd/netload.c @@ -23,14 +23,45 @@ #include #include -static const unsigned long _glibtop_sysdeps_netload = 0; +#include + +#include +#include +#include +#include +#include + +static const unsigned long _glibtop_sysdeps_netload = +(1 << GLIBTOP_NETLOAD_IF_FLAGS) + +(1 << GLIBTOP_NETLOAD_MTU) + +(1 << GLIBTOP_NETLOAD_SUBNET) + +(1 << GLIBTOP_NETLOAD_ADDRESS) + +(1 << GLIBTOP_NETLOAD_PACKETS_IN) + +(1 << GLIBTOP_NETLOAD_PACKETS_OUT) + +(1 << GLIBTOP_NETLOAD_PACKETS_TOTAL) + +(1 << GLIBTOP_NETLOAD_BYTES_IN) + +(1 << GLIBTOP_NETLOAD_BYTES_OUT) + +(1 << GLIBTOP_NETLOAD_BYTES_TOTAL) + +(1 << GLIBTOP_NETLOAD_ERRORS_IN) + +(1 << GLIBTOP_NETLOAD_ERRORS_OUT) + +(1 << GLIBTOP_NETLOAD_ERRORS_TOTAL) + +(1 << GLIBTOP_NETLOAD_COLLISIONS); + +/* nlist structure for kernel access */ +static struct nlist nlst [] = { + { "_ifnet" }, + { 0 } +}; /* Init function. */ void glibtop_init_netload_p (glibtop *server) { - server->sysdeps.netload = _glibtop_sysdeps_netload; + server->sysdeps.netload = _glibtop_sysdeps_netload; + + if (kvm_nlist (server->machine.kd, nlst) != 0) + glibtop_error_io_r (server, "kvm_nlist"); } /* Provides Network statistics. */ @@ -39,5 +70,111 @@ void glibtop_get_netload_p (glibtop *server, glibtop_netload *buf, const char *interface) { - memset (buf, 0, sizeof (glibtop_netload)); + struct ifnet ifnet; + u_long ifnetaddr, ifnetfound, ifaddraddr; + struct sockaddr *sa; + char tname [16]; + + union { + struct ifaddr ifa; + struct in_ifaddr in; + } ifaddr; + + glibtop_init_p (server, (1 << GLIBTOP_SYSDEPS_NETLOAD), 0); + + memset (buf, 0, sizeof (glibtop_netload)); + + if (kvm_read (server->machine.kd, nlst [0].n_value, + &ifnetaddr, sizeof (ifnetaddr)) != sizeof (ifnetaddr)) + glibtop_error_io_r (server, "kvm_read (ifnet)"); + + ifaddraddr = 0; + while (ifnetaddr || ifaddraddr) { + struct sockaddr_in *sin; + register char *cp; + + if (ifaddraddr == 0) { + ifnetfound = ifnetaddr; + + if ((kvm_read (server->machine.kd, ifnetaddr, &ifnet, + sizeof (ifnet)) != sizeof (ifnet)) || + (kvm_read (server->machine.kd, (u_long) ifnet.if_name, + tname, 16) != 16)) + glibtop_error_io_r (server, "kvm_read (ifnetaddr)"); + + ifaddraddr = (u_long) ifnet.if_addrlist; + } + + if (ifaddraddr) { + if ((kvm_read (server->machine.kd, ifaddraddr, &ifaddr, + sizeof (ifaddr)) != sizeof (ifaddr))) + glibtop_error_io_r (server, "kvm_read (ifaddraddr)"); + +#define CP(x) ((char *)(x)) + cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) + + CP(&ifaddr); sa = (struct sockaddr *)cp; + + if (!strcmp (interface, tname) && (sa->sa_family == AF_INET)) { + sin = (struct sockaddr_in *)sa; + + if (ifnet.if_flags & IFF_UP) + buf->if_flags |= GLIBTOP_IF_FLAGS_UP; + if (ifnet.if_flags & IFF_BROADCAST) + buf->if_flags |= GLIBTOP_IF_FLAGS_BROADCAST; + if (ifnet.if_flags & IFF_DEBUG) + buf->if_flags |= GLIBTOP_IF_FLAGS_DEBUG; + if (ifnet.if_flags & IFF_LOOPBACK) + buf->if_flags |= GLIBTOP_IF_FLAGS_LOOPBACK; + if (ifnet.if_flags & IFF_POINTOPOINT) + buf->if_flags |= GLIBTOP_IF_FLAGS_POINTOPOINT; + if (ifnet.if_flags & IFF_RUNNING) + buf->if_flags |= GLIBTOP_IF_FLAGS_RUNNING; + if (ifnet.if_flags & IFF_NOARP) + buf->if_flags |= GLIBTOP_IF_FLAGS_NOARP; + if (ifnet.if_flags & IFF_PROMISC) + buf->if_flags |= GLIBTOP_IF_FLAGS_PROMISC; + if (ifnet.if_flags & IFF_ALLMULTI) + buf->if_flags |= GLIBTOP_IF_FLAGS_ALLMULTI; + if (ifnet.if_flags & IFF_OACTIVE) + buf->if_flags |= GLIBTOP_IF_FLAGS_OACTIVE; + if (ifnet.if_flags & IFF_SIMPLEX) + buf->if_flags |= GLIBTOP_IF_FLAGS_SIMPLEX; + if (ifnet.if_flags & IFF_LINK0) + buf->if_flags |= GLIBTOP_IF_FLAGS_LINK0; + if (ifnet.if_flags & IFF_LINK1) + buf->if_flags |= GLIBTOP_IF_FLAGS_LINK1; + if (ifnet.if_flags & IFF_LINK2) + buf->if_flags |= GLIBTOP_IF_FLAGS_LINK2; + if (ifnet.if_flags & IFF_ALTPHYS) + buf->if_flags |= GLIBTOP_IF_FLAGS_ALTPHYS; + if (ifnet.if_flags & IFF_MULTICAST) + buf->if_flags |= GLIBTOP_IF_FLAGS_MULTICAST; + + buf->subnet = htonl (ifaddr.in.ia_subnet); + buf->address = sin->sin_addr.s_addr; + + buf->mtu = ifnet.if_mtu; + + buf->packets_in = ifnet.if_ipackets; + buf->packets_out = ifnet.if_opackets; + buf->packets_total = buf->packets_in + buf->packets_out; + + buf->bytes_in = ifnet.if_ibytes; + buf->bytes_out = ifnet.if_obytes; + buf->bytes_total = buf->bytes_in + buf->bytes_out; + + buf->errors_in = ifnet.if_ierrors; + buf->errors_out = ifnet.if_oerrors; + buf->errors_total = buf->errors_in + buf->errors_out; + + buf->collisions = ifnet.if_collisions; + buf->flags = _glibtop_sysdeps_netload; + return; + } + + ifaddraddr = (u_long)ifaddr.ifa.ifa_next; + } + + ifnetaddr = (u_long) ifnet.if_next; + } } diff --git a/sysdeps/names/netload.c b/sysdeps/names/netload.c index a85eafe9..30c1382c 100644 --- a/sysdeps/names/netload.c +++ b/sysdeps/names/netload.c @@ -23,9 +23,10 @@ const char *glibtop_names_netload [GLIBTOP_MAX_NETLOAD] = { - "mtu", "packets_in", "packets_out", "packets_total", - "bytes_in", "bytes_out", "bytes_total", "errors_in", - "errors_out", "errors_total", "collisions" + "if_flags", "mtu", "subnet", "address", "packets_in", + "packets_out", "packets_total", "bytes_in", "bytes_out", + "bytes_total", "errors_in", "errors_out", "errors_total", + "collisions" }; const unsigned glibtop_types_netload [GLIBTOP_MAX_NETLOAD] = @@ -33,12 +34,16 @@ const unsigned glibtop_types_netload [GLIBTOP_MAX_NETLOAD] = GLIBTOP_TYPE_ULONG, GLIBTOP_TYPE_ULONG, GLIBTOP_TYPE_ULONG, GLIBTOP_TYPE_ULONG, GLIBTOP_TYPE_ULONG, GLIBTOP_TYPE_ULONG, GLIBTOP_TYPE_ULONG, GLIBTOP_TYPE_ULONG, GLIBTOP_TYPE_ULONG, + GLIBTOP_TYPE_ULONG, GLIBTOP_TYPE_ULONG, GLIBTOP_TYPE_ULONG, GLIBTOP_TYPE_ULONG, GLIBTOP_TYPE_ULONG }; const char *glibtop_labels_netload [GLIBTOP_MAX_NETLOAD] = { + N_("Interface Flags"), N_("MTU"), + N_("Subnet"), + N_("Address"), N_("Packets In"), N_("Packets Out"), N_("Packets Total"), @@ -53,7 +58,10 @@ const char *glibtop_labels_netload [GLIBTOP_MAX_NETLOAD] = const char *glibtop_descriptions_netload [GLIBTOP_MAX_NETLOAD] = { + N_("Interface Flags"), N_("Maximum Transfer Unit"), + N_("Subnet"), + N_("Address"), N_("Packets In"), N_("Packets Out"), N_("Packets Total"),