1*17315Skarels /* tcp_timer.c 6.4 84/11/01 */ 25069Swnj 317064Sbloom #include "param.h" 417064Sbloom #include "systm.h" 517064Sbloom #include "mbuf.h" 617064Sbloom #include "socket.h" 717064Sbloom #include "socketvar.h" 817064Sbloom #include "protosw.h" 917064Sbloom #include "errno.h" 108697Sroot 118697Sroot #include "../net/if.h" 128697Sroot #include "../net/route.h" 1310896Ssam 1417064Sbloom #include "in.h" 1517064Sbloom #include "in_pcb.h" 1617064Sbloom #include "in_systm.h" 1717064Sbloom #include "ip.h" 1817064Sbloom #include "ip_var.h" 1917064Sbloom #include "tcp.h" 2017064Sbloom #include "tcp_fsm.h" 2117064Sbloom #include "tcp_seq.h" 2217064Sbloom #include "tcp_timer.h" 2317064Sbloom #include "tcp_var.h" 2417064Sbloom #include "tcpip.h" 255069Swnj 265442Swnj int tcpnodelack = 0; 275069Swnj /* 285069Swnj * Fast timeout routine for processing delayed acks 295069Swnj */ 305069Swnj tcp_fasttimo() 315069Swnj { 325284Sroot register struct inpcb *inp; 335284Sroot register struct tcpcb *tp; 345284Sroot int s = splnet(); 355069Swnj 365691Swnj inp = tcb.inp_next; 375691Swnj if (inp) 385691Swnj for (; inp != &tcb; inp = inp->inp_next) 395284Sroot if ((tp = (struct tcpcb *)inp->inp_ppcb) && 405284Sroot (tp->t_flags & TF_DELACK)) { 415284Sroot tp->t_flags &= ~TF_DELACK; 425284Sroot tp->t_flags |= TF_ACKNOW; 435284Sroot (void) tcp_output(tp); 445284Sroot } 455284Sroot splx(s); 465069Swnj } 475069Swnj 485069Swnj /* 495069Swnj * Tcp protocol timeout routine called every 500 ms. 505069Swnj * Updates the timers in all active tcb's and 515069Swnj * causes finite state machine actions if timers expire. 525069Swnj */ 535069Swnj tcp_slowtimo() 545069Swnj { 556212Swnj register struct inpcb *ip, *ipnxt; 565069Swnj register struct tcpcb *tp; 575069Swnj int s = splnet(); 585069Swnj register int i; 595069Swnj 605069Swnj /* 615069Swnj * Search through tcb's and update active timers. 625069Swnj */ 635245Sroot ip = tcb.inp_next; 645245Sroot if (ip == 0) { 655245Sroot splx(s); 665245Sroot return; 675245Sroot } 68*17315Skarels for (; ip != &tcb; ip = ipnxt) { 69*17315Skarels ipnxt = ip->inp_next; 705069Swnj tp = intotcpcb(ip); 715260Swnj if (tp == 0) 725260Swnj continue; 735075Swnj for (i = 0; i < TCPT_NTIMERS; i++) { 746212Swnj if (tp->t_timer[i] && --tp->t_timer[i] == 0) { 755069Swnj (void) tcp_usrreq(tp->t_inpcb->inp_socket, 765069Swnj PRU_SLOWTIMO, (struct mbuf *)0, 7712764Ssam (struct mbuf *)i, (struct mbuf *)0); 786212Swnj if (ipnxt->inp_prev != ip) 796212Swnj goto tpgone; 806212Swnj } 815069Swnj } 825165Swnj tp->t_idle++; 835165Swnj if (tp->t_rtt) 845165Swnj tp->t_rtt++; 856212Swnj tpgone: 86*17315Skarels ; 875069Swnj } 885075Swnj tcp_iss += TCP_ISSINCR/PR_SLOWHZ; /* increment iss */ 895069Swnj splx(s); 905069Swnj } 915069Swnj 925069Swnj /* 935075Swnj * Cancel all timers for TCP tp. 945069Swnj */ 955089Swnj tcp_canceltimers(tp) 965069Swnj struct tcpcb *tp; 975069Swnj { 985069Swnj register int i; 995069Swnj 1005075Swnj for (i = 0; i < TCPT_NTIMERS; i++) 1015075Swnj tp->t_timer[i] = 0; 1025069Swnj } 1035069Swnj 1046474Sroot float tcp_backoff[TCP_MAXRXTSHIFT] = 1056474Sroot { 1.0, 1.2, 1.4, 1.7, 2.0, 3.0, 5.0, 8.0, 16.0, 32.0 }; 1066474Sroot int tcpexprexmtbackoff = 0; 1075069Swnj /* 1085165Swnj * TCP timer processing. 1095069Swnj */ 11010396Ssam struct tcpcb * 1115075Swnj tcp_timers(tp, timer) 1125069Swnj register struct tcpcb *tp; 1135075Swnj int timer; 1145069Swnj { 1155069Swnj 1165089Swnj switch (timer) { 1175069Swnj 1185165Swnj /* 1195165Swnj * 2 MSL timeout in shutdown went off. Delete connection 1205165Swnj * control block. 1215165Swnj */ 1225075Swnj case TCPT_2MSL: 12310396Ssam tp = tcp_close(tp); 12410396Ssam break; 1255069Swnj 1265165Swnj /* 1275165Swnj * Retransmission timer went off. Message has not 1285165Swnj * been acked within retransmit interval. Back off 1295165Swnj * to a longer retransmit interval and retransmit all 1305165Swnj * unacknowledged messages in the window. 1315165Swnj */ 1325075Swnj case TCPT_REXMT: 1335165Swnj tp->t_rxtshift++; 1345691Swnj if (tp->t_rxtshift > TCP_MAXRXTSHIFT) { 13510396Ssam tp = tcp_drop(tp, ETIMEDOUT); 13610396Ssam break; 1375691Swnj } 1385165Swnj TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 1395952Swnj (int)tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 1406474Sroot if (tcpexprexmtbackoff) { 1416474Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 1426474Sroot tp->t_timer[TCPT_REXMT] << tp->t_rxtshift, 1436474Sroot TCPTV_MIN, TCPTV_MAX); 1446474Sroot } else { 1456474Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 1466474Sroot tp->t_timer[TCPT_REXMT] * 1476474Sroot tcp_backoff[tp->t_rxtshift - 1], 1486474Sroot TCPTV_MIN, TCPTV_MAX); 1496474Sroot } 1505165Swnj tp->snd_nxt = tp->snd_una; 15117314Skarels /* 15217314Skarels * If timing a segment in this window, stop the timer. 15317314Skarels */ 15417314Skarels if (tp->t_rtt && SEQ_GT(tp->t_rtseq, tp->snd_una)) 15517314Skarels tp->t_rtt = 0; 1565165Swnj (void) tcp_output(tp); 15710396Ssam break; 1585069Swnj 1595165Swnj /* 1605165Swnj * Persistance timer into zero window. 1615165Swnj * Force a byte to be output, if possible. 1625165Swnj */ 1635075Swnj case TCPT_PERSIST: 1647156Swnj tcp_setpersist(tp); 1655165Swnj tp->t_force = 1; 1665165Swnj (void) tcp_output(tp); 1675165Swnj tp->t_force = 0; 16810396Ssam break; 1695069Swnj 1705165Swnj /* 1715165Swnj * Keep-alive timer went off; send something 1725165Swnj * or drop connection if idle for too long. 1735165Swnj */ 1745075Swnj case TCPT_KEEP: 1756304Sroot if (tp->t_state < TCPS_ESTABLISHED) 1766304Sroot goto dropit; 1776267Sroot if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) { 1786304Sroot if (tp->t_idle >= TCPTV_MAXIDLE) 1796304Sroot goto dropit; 1806267Sroot /* 1816267Sroot * Saying tp->rcv_nxt-1 lies about what 1826267Sroot * we have received, and by the protocol spec 1836267Sroot * requires the correspondent TCP to respond. 1846267Sroot * Saying tp->snd_una-1 causes the transmitted 1856267Sroot * byte to lie outside the receive window; this 1866267Sroot * is important because we don't necessarily 1876267Sroot * have a byte in the window to send (consider 1886267Sroot * a one-way stream!) 1896267Sroot */ 1905392Swnj tcp_respond(tp, 1916267Sroot tp->t_template, tp->rcv_nxt-1, tp->snd_una-1, 0); 1926267Sroot } else 1936212Swnj tp->t_idle = 0; 1945165Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 19510396Ssam break; 1966304Sroot dropit: 19710396Ssam tp = tcp_drop(tp, ETIMEDOUT); 19810396Ssam break; 1995069Swnj } 20010396Ssam return (tp); 2015069Swnj } 202