static int fetch_ipstats(struct ip_stats *st) { struct ip_stats stattmp[SMP_MAXCPU]; size_t len; int name[4], cpucnt; name[0] = CTL_NET; name[1] = PF_INET; name[2] = IPPROTO_IP; name[3] = IPCTL_STATS; len = sizeof(struct ip_stats) * SMP_MAXCPU; if (sysctl(name, 4, stattmp, &len, NULL, 0) < 0) { error("sysctl getting ip_stats failed"); return -1; } cpucnt = len / sizeof(struct ip_stats); ip_stats_agg(stattmp, st, cpucnt); return 0; }
/* * Dump IP statistics structure. */ void ip_stats(u_long off __unused, const char *name, int af1 __unused) { struct ip_stats ipstat, *stattmp; struct ip_stats zerostat[SMP_MAXCPU]; size_t len = sizeof(struct ip_stats) * SMP_MAXCPU; int cpucnt; if (zflag) memset(zerostat, 0, len); if ((stattmp = malloc(len)) == NULL) { return; } else { if (sysctlbyname("net.inet.ip.stats", stattmp, &len, zflag ? zerostat : NULL, zflag ? len : 0) < 0) { warn("sysctl: net.inet.ip.stats"); free(stattmp); return; } else { if ((stattmp = realloc(stattmp, len)) == NULL) { warn("ip_stats"); return; } } } cpucnt = len / sizeof(struct ip_stats); ip_stats_agg(stattmp, &ipstat, cpucnt); printf("%s:\n", name); #define p(f, m) if (ipstat.f || sflag <= 1) \ printf(m, ipstat.f, plural(ipstat.f)) #define p1a(f, m) if (ipstat.f || sflag <= 1) \ printf(m, ipstat.f) p(ips_total, "\t%lu total packet%s received\n"); p(ips_badsum, "\t%lu bad header checksum%s\n"); p1a(ips_toosmall, "\t%lu with size smaller than minimum\n"); p1a(ips_tooshort, "\t%lu with data size < data length\n"); p1a(ips_toolong, "\t%lu with ip length > max ip packet size\n"); p1a(ips_badhlen, "\t%lu with header length < data size\n"); p1a(ips_badlen, "\t%lu with data length < header length\n"); p1a(ips_badoptions, "\t%lu with bad options\n"); p1a(ips_badvers, "\t%lu with incorrect version number\n"); p(ips_fragments, "\t%lu fragment%s received\n"); p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n"); p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n"); p(ips_reassembled, "\t%lu packet%s reassembled ok\n"); p(ips_delivered, "\t%lu packet%s for this host\n"); p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n"); p(ips_forward, "\t%lu packet%s forwarded"); p(ips_fastforward, " (%lu packet%s fast forwarded)"); if (ipstat.ips_forward || sflag <= 1) putchar('\n'); p(ips_cantforward, "\t%lu packet%s not forwardable\n"); p(ips_notmember, "\t%lu packet%s received for unknown multicast group\n"); p(ips_redirectsent, "\t%lu redirect%s sent\n"); p(ips_localout, "\t%lu packet%s sent from this host\n"); p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n"); p(ips_odropped, "\t%lu output packet%s dropped due to no bufs, etc.\n"); p(ips_noroute, "\t%lu output packet%s discarded due to no route\n"); p(ips_fragmented, "\t%lu output datagram%s fragmented\n"); p(ips_ofragments, "\t%lu fragment%s created\n"); p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n"); p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n"); p(ips_badaddr, "\t%lu datagram%s with bad address in header\n"); free(stattmp); #undef p #undef p1a }