}
static void debug_cwnd(struct utcp_connection *c) {
- debug(c, "snd.cwnd %u snd.ssthresh %u\n", c->snd.cwnd, c->snd.ssthresh);
+ debug(c, "snd.cwnd %u snd.ssthresh %u\n", c->snd.cwnd, ~c->snd.ssthresh ? c->snd.ssthresh : 0);
}
#else
#define debug(...) do {} while(0)
c->snd.nxt = c->snd.iss + 1;
c->snd.last = c->snd.nxt;
c->snd.cwnd = (utcp->mtu > 2190 ? 2 : utcp->mtu > 1095 ? 3 : 4) * utcp->mtu;
-#if UTCP_DEBUG
- c->snd.ssthresh = c->sndbuf.maxsize;
-#else
c->snd.ssthresh = ~0;
-#endif
debug_cwnd(c);
c->utcp = utcp;
pkt->hdr.seq = c->snd.una;
pkt->hdr.ack = c->rcv.nxt;
pkt->hdr.ctl = ACK;
- uint32_t len = seqdiff(c->snd.last, c->snd.una);
-
- if(len > utcp->mtu) {
- len = utcp->mtu;
- }
+ uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mtu);
if(fin_wanted(c, c->snd.una + len)) {
len--;
pkt->hdr.ctl |= FIN;
}
- c->snd.nxt = c->snd.una + len;
-
// RFC 5681 slow start after timeout
- c->snd.ssthresh = max(c->snd.cwnd / 2, utcp->mtu * 2); // eq. 4
+ uint32_t flightsize = seqdiff(c->snd.nxt, c->snd.una);
+ c->snd.ssthresh = max(flightsize / 2, utcp->mtu * 2); // eq. 4
c->snd.cwnd = utcp->mtu;
debug_cwnd(c);
buffer_copy(&c->sndbuf, pkt->data, 0, len);
print_packet(c, "rtrx", pkt, sizeof(pkt->hdr) + len);
utcp->send(utcp, pkt, sizeof(pkt->hdr) + len);
+
+ c->snd.nxt = c->snd.una + len;
break;
case CLOSED:
}
c->rtt_start.tv_sec = 0; // invalidate RTT timer
+ c->dupack = 0; // cancel any ongoing fast recovery
cleanup:
free(pkt);
break;
}
} else {
- if(!len && is_reliable(c)) {
+ if(!len && is_reliable(c) && c->snd.una != c->snd.last) {
c->dupack++;
debug(c, "duplicate ACK %d\n", c->dupack);
if(c->dupack == 3) {
// RFC 5681 fast recovery
debug(c, "fast recovery started\n", c->dupack);
- c->snd.ssthresh = max(c->snd.cwnd / 2, utcp->mtu * 2); // eq. 4
+ uint32_t flightsize = seqdiff(c->snd.nxt, c->snd.una);
+ c->snd.ssthresh = max(flightsize / 2, utcp->mtu * 2); // eq. 4
c->snd.cwnd = min(c->snd.ssthresh + 3 * utcp->mtu, c->sndbuf.maxsize);
if(c->snd.cwnd > c->sndbuf.maxsize) {
debug_cwnd(c);
}
+
+ // We got an ACK which indicates the other side did get one of our packets.
+ // Reset the retransmission timer to avoid going to slow start,
+ // but don't touch the connection timeout.
+ start_retransmit_timer(c);
}
}