188cd039cSMatthew Dillon /*
288cd039cSMatthew Dillon * Copyright (c) 2013 The DragonFly Project. All rights reserved.
388cd039cSMatthew Dillon *
488cd039cSMatthew Dillon * This code is derived from software contributed to The DragonFly Project
588cd039cSMatthew Dillon * by Matthew Dillon <dillon@backplane.com>
688cd039cSMatthew Dillon *
788cd039cSMatthew Dillon * Redistribution and use in source and binary forms, with or without
888cd039cSMatthew Dillon * modification, are permitted provided that the following conditions
988cd039cSMatthew Dillon * are met:
1088cd039cSMatthew Dillon *
1188cd039cSMatthew Dillon * 1. Redistributions of source code must retain the above copyright
1288cd039cSMatthew Dillon * notice, this list of conditions and the following disclaimer.
1388cd039cSMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
1488cd039cSMatthew Dillon * notice, this list of conditions and the following disclaimer in
1588cd039cSMatthew Dillon * the documentation and/or other materials provided with the
1688cd039cSMatthew Dillon * distribution.
1788cd039cSMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its
1888cd039cSMatthew Dillon * contributors may be used to endorse or promote products derived
1988cd039cSMatthew Dillon * from this software without specific, prior written permission.
2088cd039cSMatthew Dillon *
2188cd039cSMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2288cd039cSMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2388cd039cSMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2488cd039cSMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2588cd039cSMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2688cd039cSMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2788cd039cSMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2888cd039cSMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2988cd039cSMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3088cd039cSMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3188cd039cSMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3288cd039cSMatthew Dillon * SUCH DAMAGE.
3388cd039cSMatthew Dillon */
3488cd039cSMatthew Dillon #include <sys/param.h>
3588cd039cSMatthew Dillon #include <sys/queue.h>
3688cd039cSMatthew Dillon #include <sys/tree.h>
3788cd039cSMatthew Dillon #include <sys/socket.h>
3888cd039cSMatthew Dillon #include <sys/socketvar.h>
3988cd039cSMatthew Dillon #include <sys/protosw.h>
4088cd039cSMatthew Dillon #include <sys/sysctl.h>
4188cd039cSMatthew Dillon
4288cd039cSMatthew Dillon #include <netinet/in.h>
4388cd039cSMatthew Dillon #include <arpa/inet.h>
4488cd039cSMatthew Dillon #include <net/route.h>
4588cd039cSMatthew Dillon #include <netinet/in_systm.h>
4688cd039cSMatthew Dillon #include <netinet/ip.h>
4788cd039cSMatthew Dillon #ifdef INET6
4888cd039cSMatthew Dillon #include <netinet/ip6.h>
4988cd039cSMatthew Dillon #endif
5088cd039cSMatthew Dillon #include <netinet/in_pcb.h>
5188cd039cSMatthew Dillon #include <netinet/ip_icmp.h>
5288cd039cSMatthew Dillon #include <netinet/icmp_var.h>
5388cd039cSMatthew Dillon #include <netinet/ip_var.h>
5488cd039cSMatthew Dillon #include <netinet/tcp.h>
5588cd039cSMatthew Dillon #include <netinet/tcpip.h>
5688cd039cSMatthew Dillon #include <netinet/tcp_seq.h>
5788cd039cSMatthew Dillon #include <netinet/tcp_fsm.h>
5888cd039cSMatthew Dillon #include <netinet/tcp_timer.h>
5988cd039cSMatthew Dillon #include <netinet/tcp_var.h>
6088cd039cSMatthew Dillon #include <netinet/udp.h>
6188cd039cSMatthew Dillon #include <netinet/udp_var.h>
6288cd039cSMatthew Dillon
6388cd039cSMatthew Dillon #include <err.h>
6488cd039cSMatthew Dillon #include <errno.h>
6588cd039cSMatthew Dillon #include <netdb.h>
6688cd039cSMatthew Dillon #include <stdlib.h>
6788cd039cSMatthew Dillon #include <string.h>
6888cd039cSMatthew Dillon #include <nlist.h>
6988cd039cSMatthew Dillon #include <paths.h>
7088cd039cSMatthew Dillon #include "systat.h"
7188cd039cSMatthew Dillon #include "extern.h"
7288cd039cSMatthew Dillon
7388cd039cSMatthew Dillon struct mytcpcb {
7488cd039cSMatthew Dillon RB_ENTRY(mytcpcb) rb_node;
7588cd039cSMatthew Dillon int seq;
7688cd039cSMatthew Dillon struct xtcpcb xtcp;
7788cd039cSMatthew Dillon struct xtcpcb last_xtcp;
7888cd039cSMatthew Dillon };
7988cd039cSMatthew Dillon
8088cd039cSMatthew Dillon static int
mytcpcb_cmp(struct mytcpcb * tcp1,struct mytcpcb * tcp2)8188cd039cSMatthew Dillon mytcpcb_cmp(struct mytcpcb *tcp1, struct mytcpcb *tcp2)
8288cd039cSMatthew Dillon {
8333a04409SMatthew Dillon int r;
8433a04409SMatthew Dillon
8533a04409SMatthew Dillon /*
8633a04409SMatthew Dillon * Low local or foreign port comes first (local has priority).
8733a04409SMatthew Dillon */
8833a04409SMatthew Dillon if (ntohs(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie_lport) >= 1024 &&
8933a04409SMatthew Dillon ntohs(tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie_lport) >= 1024) {
9033a04409SMatthew Dillon if (ntohs(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie_fport) <
9133a04409SMatthew Dillon ntohs(tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie_fport))
9288cd039cSMatthew Dillon return(-1);
9333a04409SMatthew Dillon if (ntohs(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie_fport) >
9433a04409SMatthew Dillon ntohs(tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie_fport))
9588cd039cSMatthew Dillon return(1);
9633a04409SMatthew Dillon }
9733a04409SMatthew Dillon
9833a04409SMatthew Dillon if (ntohs(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie_lport) <
9933a04409SMatthew Dillon ntohs(tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie_lport))
10088cd039cSMatthew Dillon return(-1);
10133a04409SMatthew Dillon if (ntohs(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie_lport) >
10233a04409SMatthew Dillon ntohs(tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie_lport))
10388cd039cSMatthew Dillon return(1);
10433a04409SMatthew Dillon if (ntohs(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie_fport) <
10533a04409SMatthew Dillon ntohs(tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie_fport))
10688cd039cSMatthew Dillon return(-1);
10733a04409SMatthew Dillon if (ntohs(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie_fport) >
10833a04409SMatthew Dillon ntohs(tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie_fport))
10988cd039cSMatthew Dillon return(1);
11033a04409SMatthew Dillon
11133a04409SMatthew Dillon /*
11233a04409SMatthew Dillon * Sort IPV4 vs IPV6 addresses
11333a04409SMatthew Dillon */
114*727ccde8SSepherosa Ziehau if (tcp1->xtcp.xt_inp.inp_af < tcp2->xtcp.xt_inp.inp_af)
11588cd039cSMatthew Dillon return(-1);
116*727ccde8SSepherosa Ziehau if (tcp1->xtcp.xt_inp.inp_af > tcp2->xtcp.xt_inp.inp_af)
11788cd039cSMatthew Dillon return(1);
11833a04409SMatthew Dillon
11933a04409SMatthew Dillon /*
12033a04409SMatthew Dillon * Local and foreign addresses
12133a04409SMatthew Dillon */
122*727ccde8SSepherosa Ziehau if (INP_ISIPV4(&tcp1->xtcp.xt_inp)) {
12333a04409SMatthew Dillon if (ntohl(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie_laddr.s_addr) <
12433a04409SMatthew Dillon ntohl(tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie_laddr.s_addr))
12533a04409SMatthew Dillon return(-1);
12633a04409SMatthew Dillon if (ntohl(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie_laddr.s_addr) >
12733a04409SMatthew Dillon ntohl(tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie_laddr.s_addr))
12833a04409SMatthew Dillon return(1);
12933a04409SMatthew Dillon if (ntohl(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie_faddr.s_addr) <
13033a04409SMatthew Dillon ntohl(tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie_faddr.s_addr))
13133a04409SMatthew Dillon return(-1);
13233a04409SMatthew Dillon if (ntohl(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie_faddr.s_addr) >
13333a04409SMatthew Dillon ntohl(tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie_faddr.s_addr))
13433a04409SMatthew Dillon return(1);
135*727ccde8SSepherosa Ziehau } else if (INP_ISIPV6(&tcp1->xtcp.xt_inp)) {
13633a04409SMatthew Dillon r = bcmp(&tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie6_faddr,
13733a04409SMatthew Dillon &tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie6_faddr,
13833a04409SMatthew Dillon sizeof(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie6_faddr));
13933a04409SMatthew Dillon if (r)
14088cd039cSMatthew Dillon return(r);
14133a04409SMatthew Dillon } else {
14233a04409SMatthew Dillon r = bcmp(&tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie6_faddr,
14333a04409SMatthew Dillon &tcp2->xtcp.xt_inp.inp_inc.inc_ie.ie6_faddr,
14433a04409SMatthew Dillon sizeof(tcp1->xtcp.xt_inp.inp_inc.inc_ie.ie6_faddr));
14533a04409SMatthew Dillon if (r)
14633a04409SMatthew Dillon return(r);
14733a04409SMatthew Dillon }
14833a04409SMatthew Dillon return(0);
14988cd039cSMatthew Dillon }
15088cd039cSMatthew Dillon
15188cd039cSMatthew Dillon struct mytcpcb_tree;
15288cd039cSMatthew Dillon RB_HEAD(mytcpcb_tree, mytcpcb);
15388cd039cSMatthew Dillon RB_PROTOTYPE(mytcpcb_tree, mytcpcb, rb_node, mytcpcb_cmp);
15488cd039cSMatthew Dillon RB_GENERATE(mytcpcb_tree, mytcpcb, rb_node, mytcpcb_cmp);
15588cd039cSMatthew Dillon
15688cd039cSMatthew Dillon static struct mytcpcb_tree mytcp_tree;
15788cd039cSMatthew Dillon static struct timeval tv_curr;
15888cd039cSMatthew Dillon static struct timeval tv_last;
15988cd039cSMatthew Dillon static struct tcp_stats tcp_curr;
16088cd039cSMatthew Dillon static struct tcp_stats tcp_last;
16188cd039cSMatthew Dillon static int tcp_pcb_seq;
16288cd039cSMatthew Dillon
16388cd039cSMatthew Dillon static const char *numtok(double value);
16464a49666SMatthew Dillon static void netbwline(int row, struct mytcpcb *elm, double delta_time);
16588cd039cSMatthew Dillon const char * netaddrstr(u_char vflags, union in_dependaddr *depaddr,
16688cd039cSMatthew Dillon u_int16_t port);
16788cd039cSMatthew Dillon static void updatepcb(struct xtcpcb *xtcp);
16888cd039cSMatthew Dillon
16988cd039cSMatthew Dillon #define DELTARATE(field) \
17088cd039cSMatthew Dillon ((double)(tcp_curr.field - tcp_last.field) / delta_time)
17188cd039cSMatthew Dillon
17288cd039cSMatthew Dillon #define DELTAELM(field) \
17388cd039cSMatthew Dillon ((double)(tcp_seq_diff_t)(elm->xtcp.field - \
17488cd039cSMatthew Dillon elm->last_xtcp.field) / \
17588cd039cSMatthew Dillon delta_time)
17688cd039cSMatthew Dillon
17788cd039cSMatthew Dillon #define DELTAELMSCALE(field, scale) \
17888cd039cSMatthew Dillon ((double)((tcp_seq_diff_t)(elm->xtcp.field - \
17988cd039cSMatthew Dillon elm->last_xtcp.field) << scale) / \
18088cd039cSMatthew Dillon delta_time)
18188cd039cSMatthew Dillon
18288cd039cSMatthew Dillon WINDOW *
opennetbw(void)18388cd039cSMatthew Dillon opennetbw(void)
18488cd039cSMatthew Dillon {
18588cd039cSMatthew Dillon RB_INIT(&mytcp_tree);
18688cd039cSMatthew Dillon return (subwin(stdscr, LINES-0-1, 0, 0, 0));
18788cd039cSMatthew Dillon }
18888cd039cSMatthew Dillon
18988cd039cSMatthew Dillon void
closenetbw(WINDOW * w)19088cd039cSMatthew Dillon closenetbw(WINDOW *w)
19188cd039cSMatthew Dillon {
19288cd039cSMatthew Dillon struct mytcpcb *mytcp;
19388cd039cSMatthew Dillon
19488cd039cSMatthew Dillon while ((mytcp = RB_ROOT(&mytcp_tree)) != NULL) {
19588cd039cSMatthew Dillon RB_REMOVE(mytcpcb_tree, &mytcp_tree, mytcp);
19688cd039cSMatthew Dillon free(mytcp);
19788cd039cSMatthew Dillon }
19888cd039cSMatthew Dillon
19988cd039cSMatthew Dillon if (w != NULL) {
20088cd039cSMatthew Dillon wclear(w);
20188cd039cSMatthew Dillon wrefresh(w);
20288cd039cSMatthew Dillon delwin(w);
20388cd039cSMatthew Dillon }
20488cd039cSMatthew Dillon }
20588cd039cSMatthew Dillon
20688cd039cSMatthew Dillon int
initnetbw(void)20788cd039cSMatthew Dillon initnetbw(void)
20888cd039cSMatthew Dillon {
20988cd039cSMatthew Dillon return(1);
21088cd039cSMatthew Dillon }
21188cd039cSMatthew Dillon
21288cd039cSMatthew Dillon void
fetchnetbw(void)21388cd039cSMatthew Dillon fetchnetbw(void)
21488cd039cSMatthew Dillon {
21588cd039cSMatthew Dillon struct tcp_stats tcp_array[SMP_MAXCPU];
21688cd039cSMatthew Dillon struct xtcpcb *tcp_pcbs;
21788cd039cSMatthew Dillon size_t npcbs;
21888cd039cSMatthew Dillon size_t len;
21988cd039cSMatthew Dillon size_t i;
22088cd039cSMatthew Dillon size_t j;
22188cd039cSMatthew Dillon size_t ncpus;
22288cd039cSMatthew Dillon
22388cd039cSMatthew Dillon /*
22488cd039cSMatthew Dillon * Extract PCB list
22588cd039cSMatthew Dillon */
22688cd039cSMatthew Dillon len = 0;
22788cd039cSMatthew Dillon if (sysctlbyname("net.inet.tcp.pcblist", NULL, &len, NULL, 0) < 0)
22888cd039cSMatthew Dillon return;
22988cd039cSMatthew Dillon len += 128 * sizeof(tcp_pcbs[0]);
23088cd039cSMatthew Dillon tcp_pcbs = malloc(len);
23188cd039cSMatthew Dillon if (sysctlbyname("net.inet.tcp.pcblist", tcp_pcbs, &len, NULL, 0) < 0) {
23288cd039cSMatthew Dillon free(tcp_pcbs);
23388cd039cSMatthew Dillon return;
23488cd039cSMatthew Dillon }
23588cd039cSMatthew Dillon npcbs = len / sizeof(tcp_pcbs[0]);
23688cd039cSMatthew Dillon ++tcp_pcb_seq;
23788cd039cSMatthew Dillon
23888cd039cSMatthew Dillon for (i = 0; i < npcbs; ++i) {
23988cd039cSMatthew Dillon if (tcp_pcbs[i].xt_len != sizeof(tcp_pcbs[0]))
24088cd039cSMatthew Dillon break;
24188cd039cSMatthew Dillon updatepcb(&tcp_pcbs[i]);
24288cd039cSMatthew Dillon }
24388cd039cSMatthew Dillon free(tcp_pcbs);
24488cd039cSMatthew Dillon
24588cd039cSMatthew Dillon /*
24688cd039cSMatthew Dillon * General stats
24788cd039cSMatthew Dillon */
24888cd039cSMatthew Dillon len = sizeof(tcp_array);
24964a49666SMatthew Dillon if (sysctlbyname("net.inet.tcp.stats", tcp_array, &len, NULL, 0) < 0)
25088cd039cSMatthew Dillon return;
25188cd039cSMatthew Dillon ncpus = len / sizeof(tcp_array[0]);
25288cd039cSMatthew Dillon tcp_last = tcp_curr;
25388cd039cSMatthew Dillon tv_last = tv_curr;
25488cd039cSMatthew Dillon bzero(&tcp_curr, sizeof(tcp_curr));
25588cd039cSMatthew Dillon gettimeofday(&tv_curr, NULL);
25688cd039cSMatthew Dillon
25788cd039cSMatthew Dillon for (i = 0; i < ncpus; ++i) {
25888cd039cSMatthew Dillon for (j = 0; j < sizeof(tcp_curr) / sizeof(u_long); ++j) {
25988cd039cSMatthew Dillon ((u_long *)&tcp_curr)[j] +=
26088cd039cSMatthew Dillon ((u_long *)&tcp_array[i])[j];
26188cd039cSMatthew Dillon }
26288cd039cSMatthew Dillon }
26388cd039cSMatthew Dillon }
26488cd039cSMatthew Dillon
26588cd039cSMatthew Dillon void
labelnetbw(void)26688cd039cSMatthew Dillon labelnetbw(void)
26788cd039cSMatthew Dillon {
26888cd039cSMatthew Dillon wmove(wnd, 0, 0);
26988cd039cSMatthew Dillon wclrtobot(wnd);
27088cd039cSMatthew Dillon #if 0
27188cd039cSMatthew Dillon mvwaddstr(wnd, 0, LADDR, "Local Address");
27288cd039cSMatthew Dillon mvwaddstr(wnd, 0, FADDR, "Foreign Address");
27388cd039cSMatthew Dillon mvwaddstr(wnd, 0, PROTO, "Proto");
27488cd039cSMatthew Dillon mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
27588cd039cSMatthew Dillon mvwaddstr(wnd, 0, SNDCC, "Send-Q");
27688cd039cSMatthew Dillon mvwaddstr(wnd, 0, STATE, "(state)");
27788cd039cSMatthew Dillon #endif
27888cd039cSMatthew Dillon }
27988cd039cSMatthew Dillon
28088cd039cSMatthew Dillon void
shownetbw(void)28188cd039cSMatthew Dillon shownetbw(void)
28288cd039cSMatthew Dillon {
28388cd039cSMatthew Dillon double delta_time;
28488cd039cSMatthew Dillon struct mytcpcb *elm;
28588cd039cSMatthew Dillon struct mytcpcb *delm;
28688cd039cSMatthew Dillon int row;
28788cd039cSMatthew Dillon
28888cd039cSMatthew Dillon delta_time = (double)(tv_curr.tv_sec - tv_last.tv_sec) - 1.0 +
28988cd039cSMatthew Dillon (tv_curr.tv_usec + 1000000 - tv_last.tv_usec) / 1e6;
29088cd039cSMatthew Dillon if (delta_time < 0.1)
29188cd039cSMatthew Dillon return;
29288cd039cSMatthew Dillon
29333a04409SMatthew Dillon mvwprintw(wnd, 0, 0,
29433a04409SMatthew Dillon "tcp accepts %s connects %s "
2952828f71bSMatthew Dillon " rcv %s snd %s rexmit %s",
29688cd039cSMatthew Dillon numtok(DELTARATE(tcps_accepts)),
29733a04409SMatthew Dillon numtok(DELTARATE(tcps_connects) - DELTARATE(tcps_accepts)),
29888cd039cSMatthew Dillon numtok(DELTARATE(tcps_rcvbyte)),
2992828f71bSMatthew Dillon numtok(DELTARATE(tcps_sndbyte)),
3002828f71bSMatthew Dillon numtok(DELTARATE(tcps_sndrexmitbyte)));
30188cd039cSMatthew Dillon
30233a04409SMatthew Dillon row = 2;
30388cd039cSMatthew Dillon delm = NULL;
30488cd039cSMatthew Dillon RB_FOREACH(elm, mytcpcb_tree, &mytcp_tree) {
30588cd039cSMatthew Dillon if (delm) {
30688cd039cSMatthew Dillon RB_REMOVE(mytcpcb_tree, &mytcp_tree, delm);
30788cd039cSMatthew Dillon free(delm);
30888cd039cSMatthew Dillon delm = NULL;
30988cd039cSMatthew Dillon }
31088cd039cSMatthew Dillon if (elm->seq == tcp_pcb_seq &&
31188cd039cSMatthew Dillon (elm->xtcp.xt_socket.so_rcv.sb_cc ||
31288cd039cSMatthew Dillon elm->xtcp.xt_socket.so_snd.sb_cc ||
31388cd039cSMatthew Dillon DELTAELM(xt_tp.snd_max) ||
31488cd039cSMatthew Dillon DELTAELM(xt_tp.rcv_nxt)
31588cd039cSMatthew Dillon )) {
31664a49666SMatthew Dillon if (row < LINES - 3)
31764a49666SMatthew Dillon netbwline(row, elm, delta_time);
31864a49666SMatthew Dillon ++row;
31964a49666SMatthew Dillon } else if (elm->seq != tcp_pcb_seq) {
32064a49666SMatthew Dillon delm = elm;
32164a49666SMatthew Dillon }
32264a49666SMatthew Dillon }
32364a49666SMatthew Dillon if (delm) {
32464a49666SMatthew Dillon RB_REMOVE(mytcpcb_tree, &mytcp_tree, delm);
32564a49666SMatthew Dillon free(delm);
32664a49666SMatthew Dillon delm = NULL;
32764a49666SMatthew Dillon }
32864a49666SMatthew Dillon wmove(wnd, row, 0);
32964a49666SMatthew Dillon wclrtobot(wnd);
33064a49666SMatthew Dillon mvwprintw(wnd, LINES-2, 0,
33164a49666SMatthew Dillon "Rate/sec, "
33264a49666SMatthew Dillon "R=rxpend T=txpend N=nodelay T=tstmp "
33364a49666SMatthew Dillon "S=sack X=winscale F=fastrec");
33464a49666SMatthew Dillon }
33564a49666SMatthew Dillon
33664a49666SMatthew Dillon static
33764a49666SMatthew Dillon void
netbwline(int row,struct mytcpcb * elm,double delta_time)33864a49666SMatthew Dillon netbwline(int row, struct mytcpcb *elm, double delta_time)
33964a49666SMatthew Dillon {
34088cd039cSMatthew Dillon mvwprintw(wnd, row, 0,
34188cd039cSMatthew Dillon "%s %s "
34288cd039cSMatthew Dillon /*"rxb %s txb %s "*/
3432828f71bSMatthew Dillon "rcv %s snd %s "
34433a04409SMatthew Dillon "[%c%c%c%c%c%c%c]",
34588cd039cSMatthew Dillon netaddrstr(
346*727ccde8SSepherosa Ziehau elm->xtcp.xt_inp.inp_af,
34788cd039cSMatthew Dillon &elm->xtcp.xt_inp.inp_inc.inc_ie.
34888cd039cSMatthew Dillon ie_dependladdr,
34988cd039cSMatthew Dillon ntohs(elm->xtcp.xt_inp.inp_inc.inc_ie.ie_lport)),
35088cd039cSMatthew Dillon netaddrstr(
351*727ccde8SSepherosa Ziehau elm->xtcp.xt_inp.inp_af,
35288cd039cSMatthew Dillon &elm->xtcp.xt_inp.inp_inc.inc_ie.
35388cd039cSMatthew Dillon ie_dependfaddr,
35488cd039cSMatthew Dillon ntohs(elm->xtcp.xt_inp.inp_inc.inc_ie.ie_fport)),
35588cd039cSMatthew Dillon /*
35688cd039cSMatthew Dillon numtok(elm->xtcp.xt_socket.so_rcv.sb_cc),
35788cd039cSMatthew Dillon numtok(elm->xtcp.xt_socket.so_snd.sb_cc),
35888cd039cSMatthew Dillon */
35988cd039cSMatthew Dillon numtok(DELTAELM(xt_tp.rcv_nxt)),
36033a04409SMatthew Dillon numtok(DELTAELM(xt_tp.snd_max)),
36133a04409SMatthew Dillon (elm->xtcp.xt_socket.so_rcv.sb_cc > 15000 ?
36233a04409SMatthew Dillon 'R' : ' '),
36333a04409SMatthew Dillon (elm->xtcp.xt_socket.so_snd.sb_cc > 15000 ?
36433a04409SMatthew Dillon 'T' : ' '),
36533a04409SMatthew Dillon ((elm->xtcp.xt_tp.t_flags & TF_NODELAY) ?
36633a04409SMatthew Dillon 'N' : ' '),
36733a04409SMatthew Dillon ((elm->xtcp.xt_tp.t_flags & TF_RCVD_TSTMP) ?
36833a04409SMatthew Dillon 'T' : ' '),
36933a04409SMatthew Dillon ((elm->xtcp.xt_tp.t_flags &
37033a04409SMatthew Dillon TF_SACK_PERMITTED) ?
37133a04409SMatthew Dillon 'S' : ' '),
37233a04409SMatthew Dillon ((elm->xtcp.xt_tp.t_flags & TF_RCVD_SCALE) ?
37333a04409SMatthew Dillon 'X' : ' '),
37433a04409SMatthew Dillon ((elm->xtcp.xt_tp.t_flags & TF_FASTRECOVERY) ?
37533a04409SMatthew Dillon 'F' : ' ')
37633a04409SMatthew Dillon );
3774e42da30SMatthew Dillon wclrtoeol(wnd);
37888cd039cSMatthew Dillon }
37988cd039cSMatthew Dillon
38088cd039cSMatthew Dillon #if 0
38188cd039cSMatthew Dillon int
38288cd039cSMatthew Dillon cmdnetbw(const char *cmd __unused, char *args __unused)
38388cd039cSMatthew Dillon {
38488cd039cSMatthew Dillon fetchnetbw();
38588cd039cSMatthew Dillon shownetbw();
38688cd039cSMatthew Dillon refresh();
38788cd039cSMatthew Dillon
38888cd039cSMatthew Dillon return (0);
38988cd039cSMatthew Dillon }
39088cd039cSMatthew Dillon #endif
39188cd039cSMatthew Dillon
39288cd039cSMatthew Dillon #define MAXINDEXES 8
39388cd039cSMatthew Dillon
39488cd039cSMatthew Dillon static
39588cd039cSMatthew Dillon const char *
numtok(double value)39688cd039cSMatthew Dillon numtok(double value)
39788cd039cSMatthew Dillon {
39888cd039cSMatthew Dillon static char buf[MAXINDEXES][32];
39988cd039cSMatthew Dillon static int nexti;
40088cd039cSMatthew Dillon static const char *suffixes[] = { " ", "K", "M", "G", "T", NULL };
40188cd039cSMatthew Dillon int suffix = 0;
40288cd039cSMatthew Dillon const char *fmt;
40388cd039cSMatthew Dillon
40488cd039cSMatthew Dillon while (value >= 1000.0 && suffixes[suffix+1]) {
40588cd039cSMatthew Dillon value /= 1000.0;
40688cd039cSMatthew Dillon ++suffix;
40788cd039cSMatthew Dillon }
40888cd039cSMatthew Dillon nexti = (nexti + 1) % MAXINDEXES;
40988cd039cSMatthew Dillon if (value < 0.001) {
41088cd039cSMatthew Dillon fmt = " ";
41188cd039cSMatthew Dillon } else if (value < 1.0) {
41233a04409SMatthew Dillon fmt = "%5.3f%s";
41388cd039cSMatthew Dillon } else if (value < 10.0) {
41433a04409SMatthew Dillon fmt = "%5.3f%s";
41588cd039cSMatthew Dillon } else if (value < 100.0) {
41633a04409SMatthew Dillon fmt = "%5.2f%s";
41788cd039cSMatthew Dillon } else {
41833a04409SMatthew Dillon fmt = "%5.1f%s";
41988cd039cSMatthew Dillon }
42088cd039cSMatthew Dillon snprintf(buf[nexti], sizeof(buf[nexti]),
42188cd039cSMatthew Dillon fmt, value, suffixes[suffix]);
42288cd039cSMatthew Dillon return (buf[nexti]);
42388cd039cSMatthew Dillon }
42488cd039cSMatthew Dillon
42588cd039cSMatthew Dillon const char *
netaddrstr(u_char af,union in_dependaddr * depaddr,u_int16_t port)426*727ccde8SSepherosa Ziehau netaddrstr(u_char af, union in_dependaddr *depaddr, u_int16_t port)
42788cd039cSMatthew Dillon {
42888cd039cSMatthew Dillon static char buf[MAXINDEXES][64];
42988cd039cSMatthew Dillon static int nexta;
43033a04409SMatthew Dillon char bufip[64];
43188cd039cSMatthew Dillon
43288cd039cSMatthew Dillon nexta = (nexta + 1) % MAXINDEXES;
43388cd039cSMatthew Dillon
434*727ccde8SSepherosa Ziehau if (af == AF_INET) {
43533a04409SMatthew Dillon snprintf(bufip, sizeof(bufip),
43633a04409SMatthew Dillon "%d.%d.%d.%d",
43788cd039cSMatthew Dillon (ntohl(depaddr->id46_addr.ia46_addr4.s_addr) >> 24) &
43888cd039cSMatthew Dillon 255,
43988cd039cSMatthew Dillon (ntohl(depaddr->id46_addr.ia46_addr4.s_addr) >> 16) &
44088cd039cSMatthew Dillon 255,
44188cd039cSMatthew Dillon (ntohl(depaddr->id46_addr.ia46_addr4.s_addr) >> 8) &
44288cd039cSMatthew Dillon 255,
44388cd039cSMatthew Dillon (ntohl(depaddr->id46_addr.ia46_addr4.s_addr) >> 0) &
44433a04409SMatthew Dillon 255);
44533a04409SMatthew Dillon snprintf(buf[nexta], sizeof(buf[nexta]),
44633a04409SMatthew Dillon "%15s:%-5d", bufip, port);
447*727ccde8SSepherosa Ziehau } else if (af == AF_INET6) {
448948c6a2eSMatthew Dillon snprintf(bufip, sizeof(bufip),
44933a04409SMatthew Dillon "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
45088cd039cSMatthew Dillon ntohs(depaddr->id6_addr.s6_addr16[0]),
45188cd039cSMatthew Dillon ntohs(depaddr->id6_addr.s6_addr16[1]),
45288cd039cSMatthew Dillon ntohs(depaddr->id6_addr.s6_addr16[2]),
45388cd039cSMatthew Dillon ntohs(depaddr->id6_addr.s6_addr16[3]),
45488cd039cSMatthew Dillon ntohs(depaddr->id6_addr.s6_addr16[4]),
45588cd039cSMatthew Dillon ntohs(depaddr->id6_addr.s6_addr16[5]),
45688cd039cSMatthew Dillon ntohs(depaddr->id6_addr.s6_addr16[6]),
45733a04409SMatthew Dillon ntohs(depaddr->id6_addr.s6_addr16[7]));
45888cd039cSMatthew Dillon snprintf(buf[nexta], sizeof(buf[nexta]),
45933a04409SMatthew Dillon "%39s:%-5d", bufip, port);
46033a04409SMatthew Dillon } else {
461948c6a2eSMatthew Dillon snprintf(bufip, sizeof(bufip), "<unknown>");
462948c6a2eSMatthew Dillon snprintf(buf[nexta], sizeof(buf[nexta]),
463948c6a2eSMatthew Dillon "%15s:%-5d", bufip, port);
46488cd039cSMatthew Dillon }
46588cd039cSMatthew Dillon return (buf[nexta]);
46688cd039cSMatthew Dillon }
46788cd039cSMatthew Dillon
46888cd039cSMatthew Dillon static
46988cd039cSMatthew Dillon void
updatepcb(struct xtcpcb * xtcp)47088cd039cSMatthew Dillon updatepcb(struct xtcpcb *xtcp)
47188cd039cSMatthew Dillon {
47288cd039cSMatthew Dillon struct mytcpcb dummy;
47388cd039cSMatthew Dillon struct mytcpcb *elm;
47488cd039cSMatthew Dillon
47588cd039cSMatthew Dillon dummy.xtcp = *xtcp;
47688cd039cSMatthew Dillon if ((elm = RB_FIND(mytcpcb_tree, &mytcp_tree, &dummy)) == NULL) {
47788cd039cSMatthew Dillon elm = malloc(sizeof(*elm));
47888cd039cSMatthew Dillon bzero(elm, sizeof(*elm));
47988cd039cSMatthew Dillon elm->xtcp = *xtcp;
48088cd039cSMatthew Dillon elm->last_xtcp = *xtcp;
48188cd039cSMatthew Dillon RB_INSERT(mytcpcb_tree, &mytcp_tree, elm);
48288cd039cSMatthew Dillon } else {
48388cd039cSMatthew Dillon elm->last_xtcp = elm->xtcp;
48488cd039cSMatthew Dillon elm->xtcp = *xtcp;
48588cd039cSMatthew Dillon }
48688cd039cSMatthew Dillon elm->seq = tcp_pcb_seq;
48788cd039cSMatthew Dillon }
488