Home | History | Annotate | Line # | Download | only in systat
tcp.c revision 1.9
      1  1.9     wiz /*	$NetBSD: tcp.c,v 1.9 2001/06/12 15:17:29 wiz Exp $	*/
      2  1.1      ad 
      3  1.1      ad /*
      4  1.7      ad  * Copyright (c) 1999, 2000 Andrew Doran <ad (at) NetBSD.org>
      5  1.1      ad  * All rights reserved.
      6  1.1      ad  *
      7  1.1      ad  * Redistribution and use in source and binary forms, with or without
      8  1.1      ad  * modification, are permitted provided that the following conditions
      9  1.1      ad  * are met:
     10  1.1      ad  * 1. Redistributions of source code must retain the above copyright
     11  1.1      ad  *    notice, this list of conditions and the following disclaimer.
     12  1.1      ad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      ad  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      ad  *    documentation and/or other materials provided with the distribution.
     15  1.1      ad  *
     16  1.1      ad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1      ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1      ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1      ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1      ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1      ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1      ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1      ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1      ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1      ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1      ad  * SUCH DAMAGE.
     27  1.1      ad  *
     28  1.1      ad  */
     29  1.1      ad 
     30  1.1      ad #include <sys/cdefs.h>
     31  1.1      ad #ifndef lint
     32  1.9     wiz __RCSID("$NetBSD: tcp.c,v 1.9 2001/06/12 15:17:29 wiz Exp $");
     33  1.1      ad #endif /* not lint */
     34  1.1      ad 
     35  1.1      ad #include <sys/param.h>
     36  1.1      ad #include <sys/sysctl.h>
     37  1.1      ad 
     38  1.1      ad #include <netinet/in.h>
     39  1.1      ad #include <netinet/in_systm.h>
     40  1.1      ad #include <netinet/ip.h>
     41  1.1      ad #include <netinet/ip_var.h>
     42  1.1      ad #include <netinet/tcp.h>
     43  1.1      ad #include <netinet/tcp_timer.h>
     44  1.1      ad #include <netinet/tcp_var.h>
     45  1.1      ad 
     46  1.8  simonb #include <kvm.h>
     47  1.1      ad #include <string.h>
     48  1.3      ad 
     49  1.1      ad #include "systat.h"
     50  1.1      ad #include "extern.h"
     51  1.1      ad 
     52  1.1      ad #define LHD(row, str)		mvwprintw(wnd, row, 10, str)
     53  1.1      ad #define RHD(row, str)		mvwprintw(wnd, row, 45, str)
     54  1.7      ad #define SHOW(row, col, stat) \
     55  1.7      ad     mvwprintw(wnd, row, col, "%9llu", (unsigned long long)curstat.stat)
     56  1.1      ad 
     57  1.7      ad enum update {
     58  1.7      ad 	UPDATE_TIME,
     59  1.7      ad 	UPDATE_BOOT,
     60  1.7      ad 	UPDATE_RUN,
     61  1.7      ad };
     62  1.7      ad 
     63  1.7      ad static enum update update = UPDATE_TIME;
     64  1.7      ad static struct tcpstat curstat;
     65  1.7      ad static struct tcpstat newstat;
     66  1.7      ad static struct tcpstat oldstat;
     67  1.1      ad 
     68  1.1      ad static struct nlist namelist[] = {
     69  1.1      ad 	{ "_tcpstat" },
     70  1.1      ad 	{ "" }
     71  1.1      ad };
     72  1.1      ad 
     73  1.1      ad WINDOW *
     74  1.1      ad opentcp(void)
     75  1.1      ad {
     76  1.1      ad 
     77  1.1      ad 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
     78  1.1      ad }
     79  1.1      ad 
     80  1.1      ad void
     81  1.7      ad closetcp(WINDOW *w)
     82  1.1      ad {
     83  1.1      ad 
     84  1.1      ad 	if (w != NULL) {
     85  1.1      ad 		wclear(w);
     86  1.1      ad 		wrefresh(w);
     87  1.1      ad 		delwin(w);
     88  1.1      ad 	}
     89  1.1      ad }
     90  1.1      ad 
     91  1.1      ad void
     92  1.1      ad labeltcp(void)
     93  1.1      ad {
     94  1.2      ad 
     95  1.1      ad 	wmove(wnd, 0, 0); wclrtoeol(wnd);
     96  1.1      ad 
     97  1.1      ad 	LHD(0,  "connections initiated");
     98  1.1      ad 	LHD(1,  "connections accepted");
     99  1.1      ad 	LHD(2,  "connections established");
    100  1.1      ad 
    101  1.1      ad 	LHD(4,  "connections dropped");
    102  1.1      ad 	LHD(5,  "  in embryonic state");
    103  1.1      ad 	LHD(6,  "  on retransmit timeout");
    104  1.1      ad 	LHD(7,  "  by keepalive");
    105  1.1      ad 	LHD(8,  "  by persist");
    106  1.1      ad 
    107  1.1      ad 	LHD(10, "potential rtt updates");
    108  1.1      ad 	LHD(11, "successful rtt updates");
    109  1.1      ad 	LHD(12, "delayed acks sent");
    110  1.1      ad 	LHD(13, "retransmit timeouts");
    111  1.1      ad 	LHD(14, "persist timeouts");
    112  1.1      ad 	LHD(15, "keepalive probes");
    113  1.1      ad 	LHD(16, "keepalive timeouts");
    114  1.1      ad 
    115  1.1      ad 	RHD(9,  "total TCP packets received");
    116  1.1      ad 	RHD(10, "  in sequence");
    117  1.1      ad 	RHD(11, "  completely duplicate");
    118  1.1      ad 	RHD(12, "  with some duplicate data");
    119  1.1      ad 	RHD(13, "  out of order");
    120  1.1      ad 	RHD(14, "  duplicate acks");
    121  1.1      ad 	RHD(15, "  acks");
    122  1.1      ad 	RHD(16, "  window probes");
    123  1.1      ad 	RHD(17, "  window updates");
    124  1.1      ad 
    125  1.1      ad 	RHD(0, "total TCP packets sent");
    126  1.1      ad 	RHD(1, "  data");
    127  1.1      ad 	RHD(2, "  data (retransmit)");
    128  1.1      ad 	RHD(3, "  ack-only");
    129  1.1      ad 	RHD(4, "  window probes");
    130  1.1      ad 	RHD(5, "  window updates");
    131  1.1      ad 	RHD(6, "  urgent data only");
    132  1.1      ad 	RHD(7, "  control");
    133  1.1      ad }
    134  1.1      ad 
    135  1.1      ad void
    136  1.1      ad showtcpsyn(void)
    137  1.1      ad {
    138  1.1      ad 
    139  1.1      ad 	SHOW(0, 0, tcps_sc_added);
    140  1.1      ad 	SHOW(1, 0, tcps_sc_completed);
    141  1.1      ad 	SHOW(2, 0, tcps_sc_timed_out);
    142  1.1      ad 	SHOW(3, 0, tcps_sc_dupesyn);
    143  1.1      ad 	SHOW(4, 0, tcps_sc_collisions);
    144  1.1      ad 	SHOW(5, 0, tcps_sc_retransmitted);
    145  1.1      ad 	SHOW(6, 0, tcps_sc_aborted);
    146  1.1      ad 	SHOW(7, 0, tcps_sc_overflowed);
    147  1.1      ad 	SHOW(8, 0, tcps_sc_reset);
    148  1.1      ad 	SHOW(9, 0, tcps_sc_unreach);
    149  1.1      ad 	SHOW(10, 0, tcps_sc_bucketoverflow);
    150  1.1      ad 	SHOW(11, 0, tcps_sc_dropped);
    151  1.1      ad }
    152  1.1      ad 
    153  1.1      ad void
    154  1.1      ad labeltcpsyn(void)
    155  1.1      ad {
    156  1.1      ad 
    157  1.1      ad 	wmove(wnd, 0, 0); wclrtoeol(wnd);
    158  1.1      ad 	LHD(0,  "entries added");
    159  1.1      ad 	LHD(1,  "connections completed");
    160  1.1      ad 	LHD(2,  "entries timed out");
    161  1.9     wiz 	LHD(3,  "duplicate SYNs received");
    162  1.1      ad 	LHD(4,  "hash collisions");
    163  1.1      ad 	LHD(5,  "retransmissions");
    164  1.1      ad 	LHD(6,  "entries aborted (no memory)");
    165  1.1      ad 	LHD(7,  "dropped (overflow)");
    166  1.1      ad 	LHD(8,  "dropped (RST)");
    167  1.1      ad 	LHD(9,  "dropped (ICMP UNRCH)");
    168  1.1      ad 	LHD(10,  "dropped (bucket overflow)");
    169  1.1      ad 	LHD(11,  "dropped (unreachable/no memory)");
    170  1.1      ad }
    171  1.1      ad 
    172  1.1      ad void
    173  1.1      ad showtcp(void)
    174  1.1      ad {
    175  1.1      ad 
    176  1.1      ad 	SHOW(0, 0, tcps_connattempt);
    177  1.1      ad 	SHOW(1, 0, tcps_accepts);
    178  1.1      ad 	SHOW(2, 0, tcps_connects);
    179  1.1      ad 
    180  1.1      ad 	SHOW(4, 0, tcps_drops);
    181  1.1      ad 	SHOW(5, 0, tcps_conndrops);
    182  1.1      ad 	SHOW(6, 0, tcps_timeoutdrop);
    183  1.1      ad 	SHOW(7, 0, tcps_keepdrops);
    184  1.1      ad 	SHOW(8, 0, tcps_persistdrops);
    185  1.1      ad 
    186  1.1      ad 	SHOW(10, 0, tcps_segstimed);
    187  1.1      ad 	SHOW(11, 0, tcps_rttupdated);
    188  1.1      ad 	SHOW(12, 0, tcps_delack);
    189  1.1      ad 	SHOW(13, 0, tcps_rexmttimeo);
    190  1.1      ad 	SHOW(14, 0, tcps_persisttimeo);
    191  1.1      ad 	SHOW(15, 0, tcps_keepprobe);
    192  1.1      ad 	SHOW(16, 0, tcps_keeptimeo);
    193  1.1      ad 
    194  1.1      ad 	SHOW(0, 35, tcps_sndtotal);
    195  1.1      ad 	SHOW(1, 35, tcps_sndpack);
    196  1.1      ad 	SHOW(2, 35, tcps_sndrexmitpack);
    197  1.1      ad 
    198  1.1      ad 	SHOW(3, 35, tcps_sndacks);
    199  1.1      ad 	SHOW(4, 35, tcps_sndprobe);
    200  1.1      ad 	SHOW(5, 35, tcps_sndwinup);
    201  1.1      ad 	SHOW(6, 35, tcps_sndurg);
    202  1.1      ad 	SHOW(7, 35, tcps_sndctrl);
    203  1.1      ad 
    204  1.1      ad 	SHOW(9, 35, tcps_rcvtotal);
    205  1.1      ad 	SHOW(10, 35, tcps_rcvpack);
    206  1.1      ad 	SHOW(11, 35, tcps_rcvduppack);
    207  1.1      ad 	SHOW(12, 35, tcps_rcvpartduppack);
    208  1.1      ad 	SHOW(13, 35, tcps_rcvoopack);
    209  1.1      ad 	SHOW(14, 35, tcps_rcvdupack);
    210  1.1      ad 	SHOW(15, 35, tcps_rcvackpack);
    211  1.1      ad 	SHOW(16, 35, tcps_rcvwinprobe);
    212  1.1      ad 	SHOW(17, 35, tcps_rcvwinupd);
    213  1.1      ad }
    214  1.1      ad 
    215  1.1      ad int
    216  1.1      ad inittcp(void)
    217  1.1      ad {
    218  1.2      ad 
    219  1.1      ad 	if (namelist[0].n_type == 0) {
    220  1.1      ad 		if (kvm_nlist(kd, namelist)) {
    221  1.1      ad 			nlisterr(namelist);
    222  1.1      ad 			return(0);
    223  1.1      ad 		}
    224  1.1      ad 		if (namelist[0].n_type == 0) {
    225  1.1      ad 			error("No namelist");
    226  1.1      ad 			return(0);
    227  1.1      ad 		}
    228  1.1      ad 	}
    229  1.1      ad 	return 1;
    230  1.1      ad }
    231  1.1      ad 
    232  1.1      ad void
    233  1.1      ad fetchtcp(void)
    234  1.1      ad {
    235  1.2      ad 
    236  1.7      ad 	KREAD((void *)namelist[0].n_value, &newstat, sizeof(newstat));
    237  1.7      ad 
    238  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_connattempt);
    239  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_accepts);
    240  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_connects);
    241  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_drops);
    242  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_conndrops);
    243  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_timeoutdrop);
    244  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_keepdrops);
    245  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_persistdrops);
    246  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_segstimed);
    247  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_rttupdated);
    248  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_delack);
    249  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_rexmttimeo);
    250  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_persisttimeo);
    251  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_keepprobe);
    252  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_keeptimeo);
    253  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndtotal);
    254  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndpack);
    255  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndrexmitpack);
    256  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndacks);
    257  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndprobe);
    258  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndwinup);
    259  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndurg);
    260  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndctrl);
    261  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvtotal);
    262  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvpack);
    263  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvduppack);
    264  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvpartduppack);
    265  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvoopack);
    266  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvdupack);
    267  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvackpack);
    268  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvwinprobe);
    269  1.7      ad 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvwinupd);
    270  1.7      ad 
    271  1.7      ad 	if (update == UPDATE_TIME)
    272  1.7      ad 		memcpy(&oldstat, &newstat, sizeof(oldstat));
    273  1.7      ad }
    274  1.7      ad 
    275  1.7      ad void
    276  1.7      ad tcp_boot(char *args)
    277  1.7      ad {
    278  1.7      ad 
    279  1.7      ad 	memset(&oldstat, 0, sizeof(oldstat));
    280  1.7      ad 	update = UPDATE_BOOT;
    281  1.7      ad }
    282  1.7      ad 
    283  1.7      ad void
    284  1.7      ad tcp_run(char *args)
    285  1.7      ad {
    286  1.7      ad 
    287  1.7      ad 	if (update != UPDATE_RUN) {
    288  1.7      ad 		memcpy(&oldstat, &newstat, sizeof(oldstat));
    289  1.7      ad 		update = UPDATE_RUN;
    290  1.7      ad 	}
    291  1.7      ad }
    292  1.7      ad 
    293  1.7      ad void
    294  1.7      ad tcp_time(char *args)
    295  1.7      ad {
    296  1.7      ad 
    297  1.7      ad 	if (update != UPDATE_TIME) {
    298  1.7      ad 		memcpy(&oldstat, &newstat, sizeof(oldstat));
    299  1.7      ad 		update = UPDATE_TIME;
    300  1.7      ad 	}
    301  1.7      ad }
    302  1.7      ad 
    303  1.7      ad void
    304  1.7      ad tcp_zero(char *args)
    305  1.7      ad {
    306  1.7      ad 
    307  1.7      ad 	if (update == UPDATE_RUN)
    308  1.7      ad 		memcpy(&oldstat, &newstat, sizeof(oldstat));
    309  1.1      ad }
    310