#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <stdlib.h>
#include <string.h>
#include <paths.h>
#include "systat.h"
#include "extern.h"
#include "mode.h"
struct stat {
struct ip_stats i;
struct udpstat u;
};
static struct stat curstat, initstat, oldstat;
WINDOW *
openip(void)
{
return (subwin(stdscr, LINES-4-1, 0, 4, 0));
}
void
closeip(WINDOW *w)
{
if (w == NULL)
return;
wclear(w);
wrefresh(w);
delwin(w);
}
void
labelip(void)
{
wmove(wnd, 0, 0); wclrtoeol(wnd);
#define L(row, str) mvwprintw(wnd, row, 10, str)
#define R(row, str) mvwprintw(wnd, row, 45, str);
L(1, "IP Input"); R(1, "IP Output");
L(2, "total packets received"); R(2, "total packets sent");
L(3, "- with bad checksums"); R(3, "- generated locally");
L(4, "- too short for header"); R(4, "- output drops");
L(5, "- too short for data"); R(5, "output fragments generated");
L(6, "- with invalid hlen"); R(6, "- fragmentation failed");
L(7, "- with invalid length"); R(7, "destinations unreachable");
L(8, "- with invalid version"); R(8, "packets output via raw IP");
L(9, "- jumbograms");
L(10, "total fragments received"); R(10, "UDP Statistics");
L(11, "- fragments dropped"); R(11, "total input packets");
L(12, "- fragments timed out"); R(12, "- too short for header");
L(13, "- packets reassembled ok"); R(13, "- invalid checksum");
L(14, "packets forwarded"); R(14, "- no checksum");
L(15, "- unreachable dests"); R(15, "- invalid length");
L(16, "- redirects generated"); R(16, "- no socket for dest port");
L(17, "option errors"); R(17, "- no socket for broadcast");
L(18, "unwanted multicasts"); R(18, "- socket buffer full");
L(19, "delivered to upper layer"); R(19, "total output packets");
#undef L
#undef R
}
static void
domode(struct stat *ret)
{
const struct stat *sub;
double divisor = 1.0;
switch(currentmode) {
case display_RATE:
sub = &oldstat;
divisor = naptime;
break;
case display_DELTA:
sub = &oldstat;
break;
case display_SINCE:
sub = &initstat;
break;
default:
*ret = curstat;
return;
}
#define DO(stat) ret->stat = (double)(curstat.stat - sub->stat) / divisor
DO(i.ips_total);
DO(i.ips_badsum);
DO(i.ips_tooshort);
DO(i.ips_toosmall);
DO(i.ips_badhlen);
DO(i.ips_badlen);
DO(i.ips_fragments);
DO(i.ips_fragdropped);
DO(i.ips_fragtimeout);
DO(i.ips_forward);
DO(i.ips_cantforward);
DO(i.ips_redirectsent);
DO(i.ips_noproto);
DO(i.ips_delivered);
DO(i.ips_localout);
DO(i.ips_odropped);
DO(i.ips_reassembled);
DO(i.ips_fragmented);
DO(i.ips_ofragments);
DO(i.ips_cantfrag);
DO(i.ips_badoptions);
DO(i.ips_noroute);
DO(i.ips_badvers);
DO(i.ips_rawout);
DO(i.ips_toolong);
DO(i.ips_notmember);
DO(u.udps_ipackets);
DO(u.udps_hdrops);
DO(u.udps_badsum);
DO(u.udps_nosum);
DO(u.udps_badlen);
DO(u.udps_noport);
DO(u.udps_noportbcast);
DO(u.udps_fullsock);
DO(u.udps_opackets);
#undef DO
}
void
showip(void)
{
struct stat stats;
u_long totalout;
domode(&stats);
totalout = stats.i.ips_forward + stats.i.ips_localout;
#define DO(stat, row, col) \
mvwprintw(wnd, row, col, "%9lu", stats.stat)
DO(i.ips_total, 2, 0);
mvwprintw(wnd, 2, 35, "%9lu", totalout);
DO(i.ips_badsum, 3, 0);
DO(i.ips_localout, 3, 35);
DO(i.ips_tooshort, 4, 0);
DO(i.ips_odropped, 4, 35);
DO(i.ips_toosmall, 5, 0);
DO(i.ips_ofragments, 5, 35);
DO(i.ips_badhlen, 6, 0);
DO(i.ips_cantfrag, 6, 35);
DO(i.ips_badlen, 7, 0);
DO(i.ips_noroute, 7, 35);
DO(i.ips_badvers, 8, 0);
DO(i.ips_rawout, 8, 35);
DO(i.ips_toolong, 9, 0);
DO(i.ips_fragments, 10, 0);
DO(i.ips_fragdropped, 11, 0);
DO(u.udps_ipackets, 11, 35);
DO(i.ips_fragtimeout, 12, 0);
DO(u.udps_hdrops, 12, 35);
DO(i.ips_reassembled, 13, 0);
DO(u.udps_badsum, 13, 35);
DO(i.ips_forward, 14, 0);
DO(u.udps_nosum, 14, 35);
DO(i.ips_cantforward, 15, 0);
DO(u.udps_badlen, 15, 35);
DO(i.ips_redirectsent, 16, 0);
DO(u.udps_noport, 16, 35);
DO(i.ips_badoptions, 17, 0);
DO(u.udps_noportbcast, 17, 35);
DO(i.ips_notmember, 18, 0);
DO(u.udps_fullsock, 18, 35);
DO(i.ips_delivered, 19, 0);
DO(u.udps_opackets, 19, 35);
#undef DO
}
#define CPU_STATS_FUNC(proto,type) \
static void \
proto ##_stats_agg(type *ary, type *ttl, int cpucnt) \
{ \
int i, off, siz; \
siz = sizeof(type); \
\
if (!ary && !ttl) \
return; \
\
bzero(ttl, siz); \
if (cpucnt == 1) { \
*ttl = ary[0]; \
} else { \
for (i = 0; i < cpucnt; ++i) { \
for (off = 0; off < siz; off += sizeof(u_long)) { \
*(u_long *)((char *)(*(&ttl)) + off) += \
*(u_long *)((char *)&ary[i] + off); \
} \
} \
} \
}
CPU_STATS_FUNC(ip, struct ip_stats);
CPU_STATS_FUNC(udp, struct udpstat);
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;
}
static int
fetch_udpstat(struct udpstat *st)
{
struct udpstat stattmp[SMP_MAXCPU];
size_t len;
int name[4], cpucnt;
name[0] = CTL_NET;
name[1] = PF_INET;
name[2] = IPPROTO_UDP;
name[3] = UDPCTL_STATS;
len = sizeof(struct udpstat) * SMP_MAXCPU;
if (sysctl(name, 4, stattmp, &len, NULL, 0) < 0) {
error("sysctl getting udpstat failed");
return -1;
}
cpucnt = len / sizeof(struct udpstat);
udp_stats_agg(stattmp, st, cpucnt);
return 0;
}
int
initip(void)
{
if (fetch_ipstats(&initstat.i) < 0)
return 0;
if (fetch_udpstat(&initstat.u) < 0)
return 0;
oldstat = initstat;
curstat = initstat;
return 1;
}
void
resetip(void)
{
fetch_ipstats(&initstat.i);
fetch_udpstat(&initstat.u);
oldstat = initstat;
}
void
fetchip(void)
{
oldstat = curstat;
if (fetch_ipstats(&curstat.i) < 0)
return;
if (fetch_udpstat(&curstat.u) < 0)
return;
}