#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/tcp.h>
#include <netinet/tcp_seq.h>
#include <netinet/tcp_fsm.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#include <stdlib.h>
#include <string.h>
#include <paths.h>
#include "systat.h"
#include "extern.h"
#include "mode.h"
static struct tcp_stats curstat, initstat, oldstat;
WINDOW *
opentcp(void)
{
return (subwin(stdscr, LINES-5-1, 0, 5, 0));
}
void
closetcp(WINDOW *w)
{
if (w == NULL)
return;
wclear(w);
wrefresh(w);
delwin(w);
}
void
labeltcp(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, "TCP Connections"); R(1, "TCP Packets");
L(2, "connections initiated"); R(2, "total packets sent");
L(3, "connections accepted"); R(3, "- data");
L(4, "connections established"); R(4, "- data (retransmit)");
L(5, "connections dropped"); R(5, "- ack-only");
L(6, "- in embryonic state"); R(6, "- window probes");
L(7, "- on retransmit timeout"); R(7, "- window updates");
L(8, "- by keepalive"); R(8, "- urgent data only");
L(9, "- from listen queue"); R(9, "- control");
R(10, "- resends by PMTU discovery");
L(11, "TCP Timers"); R(11, "total packets received");
L(12, "potential rtt updates"); R(12, "- in sequence");
L(13, "- successful"); R(13, "- completely duplicate");
L(14, "delayed acks sent"); R(14, "- with some duplicate data");
L(15, "retransmit timeouts"); R(15, "- out-of-order");
L(16, "persist timeouts"); R(16, "- duplicate acks");
L(17, "keepalive probes"); R(17, "- acks");
L(18, "- timeouts"); R(18, "- window probes");
R(19, "- window updates");
#undef L
#undef R
}
static void
domode(struct tcp_stats *ret)
{
const struct tcp_stats *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(tcps_connattempt);
DO(tcps_accepts);
DO(tcps_connects);
DO(tcps_drops);
DO(tcps_conndrops);
DO(tcps_closed);
DO(tcps_segstimed);
DO(tcps_rttupdated);
DO(tcps_delack);
DO(tcps_timeoutdrop);
DO(tcps_rexmttimeo);
DO(tcps_persisttimeo);
DO(tcps_keeptimeo);
DO(tcps_keepprobe);
DO(tcps_keepdrops);
DO(tcps_sndtotal);
DO(tcps_sndpack);
DO(tcps_sndbyte);
DO(tcps_sndrexmitpack);
DO(tcps_sndrexmitbyte);
DO(tcps_sndacks);
DO(tcps_sndprobe);
DO(tcps_sndurg);
DO(tcps_sndwinup);
DO(tcps_sndctrl);
DO(tcps_rcvtotal);
DO(tcps_rcvpack);
DO(tcps_rcvbyte);
DO(tcps_rcvbadsum);
DO(tcps_rcvbadoff);
DO(tcps_rcvshort);
DO(tcps_rcvduppack);
DO(tcps_rcvdupbyte);
DO(tcps_rcvpartduppack);
DO(tcps_rcvpartdupbyte);
DO(tcps_rcvoopack);
DO(tcps_rcvoobyte);
DO(tcps_rcvpackafterwin);
DO(tcps_rcvbyteafterwin);
DO(tcps_rcvafterclose);
DO(tcps_rcvwinprobe);
DO(tcps_rcvdupack);
DO(tcps_rcvacktoomuch);
DO(tcps_rcvackpack);
DO(tcps_rcvackbyte);
DO(tcps_rcvwinupd);
DO(tcps_pawsdrop);
DO(tcps_predack);
DO(tcps_preddat);
DO(tcps_pcbcachemiss);
DO(tcps_cachedrtt);
DO(tcps_cachedrttvar);
DO(tcps_cachedssthresh);
DO(tcps_usedrtt);
DO(tcps_usedrttvar);
DO(tcps_usedssthresh);
DO(tcps_persistdrop);
DO(tcps_badsyn);
DO(tcps_mturesent);
DO(tcps_listendrop);
#undef DO
}
void
showtcp(void)
{
struct tcp_stats stats;
memset(&stats, 0, sizeof stats);
domode(&stats);
#define DO(stat, row, col) \
mvwprintw(wnd, row, col, "%9lu", stats.stat)
#define L(row, stat) DO(stat, row, 0)
#define R(row, stat) DO(stat, row, 35)
L(2, tcps_connattempt); R(2, tcps_sndtotal);
L(3, tcps_accepts); R(3, tcps_sndpack);
L(4, tcps_connects); R(4, tcps_sndrexmitpack);
L(5, tcps_drops); R(5, tcps_sndacks);
L(6, tcps_conndrops); R(6, tcps_sndprobe);
L(7, tcps_timeoutdrop); R(7, tcps_sndwinup);
L(8, tcps_keepdrops); R(8, tcps_sndurg);
L(9, tcps_listendrop); R(9, tcps_sndctrl);
R(10, tcps_mturesent);
R(11, tcps_rcvtotal);
L(12, tcps_segstimed); R(12, tcps_rcvpack);
L(13, tcps_rttupdated); R(13, tcps_rcvduppack);
L(14, tcps_delack); R(14, tcps_rcvpartduppack);
L(15, tcps_rexmttimeo); R(15, tcps_rcvoopack);
L(16, tcps_persisttimeo); R(16, tcps_rcvdupack);
L(17, tcps_keepprobe); R(17, tcps_rcvackpack);
L(18, tcps_keeptimeo); R(18, tcps_rcvwinprobe);
R(19, tcps_rcvwinupd);
#undef DO
#undef L
#undef R
}
#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(tcp, struct tcp_stats);
static int
fetch_tcpstats(struct tcp_stats *st)
{
struct tcp_stats stattmp[SMP_MAXCPU];
size_t len;
int name[4], cpucnt;
name[0] = CTL_NET;
name[1] = PF_INET;
name[2] = IPPROTO_TCP;
name[3] = TCPCTL_STATS;
len = sizeof(struct tcp_stats) * SMP_MAXCPU;
if (sysctl(name, 4, stattmp, &len, NULL, 0) < 0) {
error("sysctl getting tcp_stats failed");
return -1;
}
cpucnt = len / sizeof(struct tcp_stats);
tcp_stats_agg(stattmp, st, cpucnt);
return 0;
}
int
inittcp(void)
{
fetch_tcpstats(&initstat);
oldstat = initstat;
curstat = initstat;
return 1;
}
void
resettcp(void)
{
fetch_tcpstats(&initstat);
oldstat = initstat;
}
void
fetchtcp(void)
{
oldstat = curstat;
fetch_tcpstats(&curstat);
}