Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/tcp_timer.c
2 views
/* SPDX-License-Identifier: BSD-3-Clause */1/*2* Copyright (c) 1982, 1986, 1988, 1990, 19933* The Regents of the University of California. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13* 3. Neither the name of the University nor the names of its contributors14* may be used to endorse or promote products derived from this software15* without specific prior written permission.16*17* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*29* @(#)tcp_timer.c 8.1 (Berkeley) 6/10/9330* tcp_timer.c,v 1.2 1994/08/02 07:49:10 davidg Exp31*/3233#include "slirp.h"3435static struct tcpcb *tcp_timers(register struct tcpcb *tp, int timer);3637/*38* Fast timeout routine for processing delayed acks39*/40void tcp_fasttimo(Slirp *slirp)41{42register struct socket *so;43register struct tcpcb *tp;4445DEBUG_CALL("tcp_fasttimo");4647so = slirp->tcb.so_next;48if (so)49for (; so != &slirp->tcb; so = so->so_next)50if ((tp = (struct tcpcb *)so->so_tcpcb) &&51(tp->t_flags & TF_DELACK)) {52tp->t_flags &= ~TF_DELACK;53tp->t_flags |= TF_ACKNOW;54tcp_output(tp);55}56}5758/*59* Tcp protocol timeout routine called every 500 ms.60* Updates the timers in all active tcb's and61* causes finite state machine actions if timers expire.62*/63void tcp_slowtimo(Slirp *slirp)64{65register struct socket *ip, *ipnxt;66register struct tcpcb *tp;67register int i;6869DEBUG_CALL("tcp_slowtimo");7071/*72* Search through tcb's and update active timers.73*/74ip = slirp->tcb.so_next;75if (ip == NULL) {76return;77}78for (; ip != &slirp->tcb; ip = ipnxt) {79ipnxt = ip->so_next;80tp = sototcpcb(ip);81if (tp == NULL) {82continue;83}84for (i = 0; i < TCPT_NTIMERS; i++) {85if (tp->t_timer[i] && --tp->t_timer[i] == 0) {86tcp_timers(tp, i);87if (ipnxt->so_prev != ip)88goto tpgone;89}90}91tp->t_idle++;92if (tp->t_rtt)93tp->t_rtt++;94tpgone:;95}96slirp->tcp_iss += TCP_ISSINCR / PR_SLOWHZ; /* increment iss */97slirp->tcp_now++; /* for timestamps */98}99100void tcp_canceltimers(struct tcpcb *tp)101{102register int i;103104for (i = 0; i < TCPT_NTIMERS; i++)105tp->t_timer[i] = 0;106}107108const int tcp_backoff[TCP_MAXRXTSHIFT + 1] = { 1, 2, 4, 8, 16, 32, 64,10964, 64, 64, 64, 64, 64 };110111/*112* TCP timer processing.113*/114static struct tcpcb *tcp_timers(register struct tcpcb *tp, int timer)115{116register int rexmt;117118DEBUG_CALL("tcp_timers");119120switch (timer) {121/*122* 2 MSL timeout in shutdown went off. If we're closed but123* still waiting for peer to close and connection has been idle124* too long, or if 2MSL time is up from TIME_WAIT, delete connection125* control block. Otherwise, check again in a bit.126*/127case TCPT_2MSL:128if (tp->t_state != TCPS_TIME_WAIT && tp->t_idle <= TCP_MAXIDLE)129tp->t_timer[TCPT_2MSL] = TCPTV_KEEPINTVL;130else131tp = tcp_close(tp);132break;133134/*135* Retransmission timer went off. Message has not136* been acked within retransmit interval. Back off137* to a longer retransmit interval and retransmit one segment.138*/139case TCPT_REXMT:140141/*142* XXXXX If a packet has timed out, then remove all the queued143* packets for that session.144*/145146if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {147/*148* This is a hack to suit our terminal server here at the uni of149* canberra since they have trouble with zeroes... It usually lets150* them through unharmed, but under some conditions, it'll eat the151* zeros. If we keep retransmitting it, it'll keep eating the152* zeroes, so we keep retransmitting, and eventually the connection153* dies... (this only happens on incoming data)154*155* So, if we were gonna drop the connection from too many156* retransmits, don't... instead halve the t_maxseg, which might157* break up the NULLs and let them through158*159* *sigh*160*/161162tp->t_maxseg >>= 1;163if (tp->t_maxseg < 32) {164/*165* We tried our best, now the connection must die!166*/167tp->t_rxtshift = TCP_MAXRXTSHIFT;168tp = tcp_drop(tp, tp->t_softerror);169/* tp->t_softerror : ETIMEDOUT); */ /* XXX */170return (tp); /* XXX */171}172173/*174* Set rxtshift to 6, which is still at the maximum175* backoff time176*/177tp->t_rxtshift = 6;178}179rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];180TCPT_RANGESET(tp->t_rxtcur, rexmt, (short)tp->t_rttmin,181TCPTV_REXMTMAX); /* XXX */182tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;183/*184* If losing, let the lower level know and try for185* a better route. Also, if we backed off this far,186* our srtt estimate is probably bogus. Clobber it187* so we'll take the next rtt measurement as our srtt;188* move the current srtt into rttvar to keep the current189* retransmit times until then.190*/191if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {192tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);193tp->t_srtt = 0;194}195tp->snd_nxt = tp->snd_una;196/*197* If timing a segment in this window, stop the timer.198*/199tp->t_rtt = 0;200/*201* Close the congestion window down to one segment202* (we'll open it by one segment for each ack we get).203* Since we probably have a window's worth of unacked204* data accumulated, this "slow start" keeps us from205* dumping all that data as back-to-back packets (which206* might overwhelm an intermediate gateway).207*208* There are two phases to the opening: Initially we209* open by one mss on each ack. This makes the window210* size increase exponentially with time. If the211* window is larger than the path can handle, this212* exponential growth results in dropped packet(s)213* almost immediately. To get more time between214* drops but still "push" the network to take advantage215* of improving conditions, we switch from exponential216* to linear window opening at some threshold size.217* For a threshold, we use half the current window218* size, truncated to a multiple of the mss.219*220* (the minimum cwnd that will give us exponential221* growth is 2 mss. We don't allow the threshold222* to go below this.)223*/224{225unsigned win = MIN(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;226if (win < 2)227win = 2;228tp->snd_cwnd = tp->t_maxseg;229tp->snd_ssthresh = win * tp->t_maxseg;230tp->t_dupacks = 0;231}232tcp_output(tp);233break;234235/*236* Persistence timer into zero window.237* Force a byte to be output, if possible.238*/239case TCPT_PERSIST:240tcp_setpersist(tp);241tp->t_force = 1;242tcp_output(tp);243tp->t_force = 0;244break;245246/*247* Keep-alive timer went off; send something248* or drop connection if idle for too long.249*/250case TCPT_KEEP:251if (tp->t_state < TCPS_ESTABLISHED)252goto dropit;253254if (slirp_do_keepalive && tp->t_state <= TCPS_CLOSE_WAIT) {255if (tp->t_idle >= TCPTV_KEEP_IDLE + TCP_MAXIDLE)256goto dropit;257/*258* Send a packet designed to force a response259* if the peer is up and reachable:260* either an ACK if the connection is still alive,261* or an RST if the peer has closed the connection262* due to timeout or reboot.263* Using sequence number tp->snd_una-1264* causes the transmitted zero-length segment265* to lie outside the receive window;266* by the protocol spec, this requires the267* correspondent TCP to respond.268*/269tcp_respond(tp, &tp->t_template, (struct mbuf *)NULL, tp->rcv_nxt,270tp->snd_una - 1, 0, tp->t_socket->so_ffamily);271tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL;272} else273tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_IDLE;274break;275276dropit:277tp = tcp_drop(tp, 0);278break;279}280281return (tp);282}283284285