1*8697Sroot /* tcp_timer.c 4.28 82/10/20 */ 25069Swnj 35069Swnj #include "../h/param.h" 45069Swnj #include "../h/systm.h" 55069Swnj #include "../h/mbuf.h" 65069Swnj #include "../h/socket.h" 75069Swnj #include "../h/socketvar.h" 85069Swnj #include "../h/protosw.h" 9*8697Sroot #include <errno.h> 10*8697Sroot 11*8697Sroot #include "../net/if.h" 12*8697Sroot #include "../net/route.h" 138404Swnj #include "../netinet/in.h" 148404Swnj #include "../netinet/in_pcb.h" 158404Swnj #include "../netinet/in_systm.h" 168404Swnj #include "../netinet/ip.h" 178404Swnj #include "../netinet/ip_var.h" 188404Swnj #include "../netinet/tcp.h" 198404Swnj #include "../netinet/tcp_fsm.h" 208404Swnj #include "../netinet/tcp_seq.h" 218404Swnj #include "../netinet/tcp_timer.h" 228404Swnj #include "../netinet/tcp_var.h" 238404Swnj #include "../netinet/tcpip.h" 245069Swnj 255442Swnj int tcpnodelack = 0; 265069Swnj /* 275069Swnj * Fast timeout routine for processing delayed acks 285069Swnj */ 295069Swnj tcp_fasttimo() 305069Swnj { 315284Sroot register struct inpcb *inp; 325284Sroot register struct tcpcb *tp; 335284Sroot int s = splnet(); 345069Swnj 355691Swnj inp = tcb.inp_next; 365691Swnj if (inp) 375691Swnj for (; inp != &tcb; inp = inp->inp_next) 385284Sroot if ((tp = (struct tcpcb *)inp->inp_ppcb) && 395284Sroot (tp->t_flags & TF_DELACK)) { 405284Sroot tp->t_flags &= ~TF_DELACK; 415284Sroot tp->t_flags |= TF_ACKNOW; 425284Sroot (void) tcp_output(tp); 435284Sroot } 445284Sroot splx(s); 455069Swnj } 465069Swnj 475069Swnj /* 485069Swnj * Tcp protocol timeout routine called every 500 ms. 495069Swnj * Updates the timers in all active tcb's and 505069Swnj * causes finite state machine actions if timers expire. 515069Swnj */ 525069Swnj tcp_slowtimo() 535069Swnj { 546212Swnj register struct inpcb *ip, *ipnxt; 555069Swnj register struct tcpcb *tp; 565069Swnj int s = splnet(); 575069Swnj register int i; 585069Swnj 595069Swnj /* 605069Swnj * Search through tcb's and update active timers. 615069Swnj */ 625245Sroot ip = tcb.inp_next; 635245Sroot if (ip == 0) { 645245Sroot splx(s); 655245Sroot return; 665245Sroot } 676212Swnj while (ip != &tcb) { 685069Swnj tp = intotcpcb(ip); 695260Swnj if (tp == 0) 705260Swnj continue; 716212Swnj ipnxt = ip->inp_next; 725075Swnj for (i = 0; i < TCPT_NTIMERS; i++) { 736212Swnj if (tp->t_timer[i] && --tp->t_timer[i] == 0) { 745069Swnj (void) tcp_usrreq(tp->t_inpcb->inp_socket, 755069Swnj PRU_SLOWTIMO, (struct mbuf *)0, 768640Sroot (struct mbuf *)i, 77*8697Sroot (struct socketopt *)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: 866212Swnj ip = ipnxt; 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 tcprexmtprint = 0; 1076474Sroot int tcpexprexmtbackoff = 0; 1085069Swnj /* 1095165Swnj * TCP timer processing. 1105069Swnj */ 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: 1235075Swnj tcp_close(tp); 1245075Swnj return; 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) { 1355691Swnj tcp_drop(tp, ETIMEDOUT); 1365691Swnj return; 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 } 1505952Swnj if (tcprexmtprint) 1515952Swnj printf("rexmt set to %d\n", tp->t_timer[TCPT_REXMT]); 1525165Swnj tp->snd_nxt = tp->snd_una; 1535165Swnj /* this only transmits one segment! */ 1545165Swnj (void) tcp_output(tp); 1555075Swnj return; 1565069Swnj 1575165Swnj /* 1585165Swnj * Persistance timer into zero window. 1595165Swnj * Force a byte to be output, if possible. 1605165Swnj */ 1615075Swnj case TCPT_PERSIST: 1627156Swnj tcp_setpersist(tp); 1635165Swnj tp->t_force = 1; 1645165Swnj (void) tcp_output(tp); 1655165Swnj tp->t_force = 0; 1665089Swnj return; 1675069Swnj 1685165Swnj /* 1695165Swnj * Keep-alive timer went off; send something 1705165Swnj * or drop connection if idle for too long. 1715165Swnj */ 1725075Swnj case TCPT_KEEP: 1736304Sroot if (tp->t_state < TCPS_ESTABLISHED) 1746304Sroot goto dropit; 1756267Sroot if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE) { 1766304Sroot if (tp->t_idle >= TCPTV_MAXIDLE) 1776304Sroot goto dropit; 1786267Sroot /* 1796267Sroot * Saying tp->rcv_nxt-1 lies about what 1806267Sroot * we have received, and by the protocol spec 1816267Sroot * requires the correspondent TCP to respond. 1826267Sroot * Saying tp->snd_una-1 causes the transmitted 1836267Sroot * byte to lie outside the receive window; this 1846267Sroot * is important because we don't necessarily 1856267Sroot * have a byte in the window to send (consider 1866267Sroot * a one-way stream!) 1876267Sroot */ 1885392Swnj tcp_respond(tp, 1896267Sroot tp->t_template, tp->rcv_nxt-1, tp->snd_una-1, 0); 1906267Sroot } else 1916212Swnj tp->t_idle = 0; 1925165Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 1935075Swnj return; 1946304Sroot dropit: 1956304Sroot tcp_drop(tp, ETIMEDOUT); 1966304Sroot return; 1975069Swnj } 1985069Swnj } 199