]> git.meshlink.io Git - utcp/blobdiff - utcp.c
Allocate a persistent packet buffer.
[utcp] / utcp.c
diff --git a/utcp.c b/utcp.c
index ac598ddae28667fd9a40c28ace29255b7dd22f66..f7d18b9f89c736050f3fcb29104aec22c316c49a 100644 (file)
--- a/utcp.c
+++ b/utcp.c
@@ -401,7 +401,7 @@ static struct utcp_connection *allocate_connection(struct utcp *utcp, uint16_t s
        c->snd.una = c->snd.iss;
        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;
+       c->snd.cwnd = (utcp->mss > 2190 ? 2 : utcp->mss > 1095 ? 3 : 4) * utcp->mss;
        c->snd.ssthresh = ~0;
        debug_cwnd(c);
        c->utcp = utcp;
@@ -535,8 +535,8 @@ static void ack(struct utcp_connection *c, bool sendatleastone) {
        } else if(cwndleft < left) {
                left = cwndleft;
 
-               if(!sendatleastone || cwndleft > c->utcp->mtu) {
-                       left -= left % c->utcp->mtu;
+               if(!sendatleastone || cwndleft > c->utcp->mss) {
+                       left -= left % c->utcp->mss;
                }
        }
 
@@ -549,13 +549,7 @@ static void ack(struct utcp_connection *c, bool sendatleastone) {
        struct {
                struct hdr hdr;
                uint8_t data[];
-       } *pkt;
-
-       pkt = malloc(sizeof(pkt->hdr) + c->utcp->mtu);
-
-       if(!pkt) {
-               return;
-       }
+       } *pkt = c->utcp->pkt;
 
        pkt->hdr.src = c->src;
        pkt->hdr.dst = c->dst;
@@ -565,7 +559,7 @@ static void ack(struct utcp_connection *c, bool sendatleastone) {
        pkt->hdr.aux = 0;
 
        do {
-               uint32_t seglen = left > c->utcp->mtu ? c->utcp->mtu : left;
+               uint32_t seglen = left > c->utcp->mss ? c->utcp->mss : left;
                pkt->hdr.seq = c->snd.nxt;
 
                buffer_copy(&c->sndbuf, pkt->data, seqdiff(c->snd.nxt, c->snd.una), seglen);
@@ -588,8 +582,6 @@ static void ack(struct utcp_connection *c, bool sendatleastone) {
                print_packet(c, "send", pkt, sizeof(pkt->hdr) + seglen);
                c->utcp->send(c->utcp, pkt, sizeof(pkt->hdr) + seglen);
        } while(left);
-
-       free(pkt);
 }
 
 ssize_t utcp_send(struct utcp_connection *c, const void *data, size_t len) {
@@ -710,7 +702,7 @@ static void fast_retransmit(struct utcp_connection *c) {
                uint8_t data[];
        } *pkt;
 
-       pkt = malloc(sizeof(pkt->hdr) + c->utcp->mtu);
+       pkt = malloc(c->utcp->mtu);
 
        if(!pkt) {
                return;
@@ -731,7 +723,7 @@ static void fast_retransmit(struct utcp_connection *c) {
                pkt->hdr.seq = c->snd.una;
                pkt->hdr.ack = c->rcv.nxt;
                pkt->hdr.ctl = ACK;
-               uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mtu);
+               uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mss);
 
                if(fin_wanted(c, c->snd.una + len)) {
                        len--;
@@ -762,13 +754,7 @@ static void retransmit(struct utcp_connection *c) {
        struct {
                struct hdr hdr;
                uint8_t data[];
-       } *pkt;
-
-       pkt = malloc(sizeof(pkt->hdr) + c->utcp->mtu);
-
-       if(!pkt) {
-               return;
-       }
+       } *pkt = c->utcp->pkt;
 
        pkt->hdr.src = c->src;
        pkt->hdr.dst = c->dst;
@@ -808,7 +794,7 @@ static void retransmit(struct utcp_connection *c) {
                pkt->hdr.seq = c->snd.una;
                pkt->hdr.ack = c->rcv.nxt;
                pkt->hdr.ctl = ACK;
-               uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mtu);
+               uint32_t len = min(seqdiff(c->snd.last, c->snd.una), utcp->mss);
 
                if(fin_wanted(c, c->snd.una + len)) {
                        len--;
@@ -817,8 +803,8 @@ static void retransmit(struct utcp_connection *c) {
 
                // RFC 5681 slow start after timeout
                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;
+               c->snd.ssthresh = max(flightsize / 2, utcp->mss * 2); // eq. 4
+               c->snd.cwnd = utcp->mss;
                debug_cwnd(c);
 
                buffer_copy(&c->sndbuf, pkt->data, 0, len);
@@ -851,7 +837,7 @@ static void retransmit(struct utcp_connection *c) {
        c->dupack = 0; // cancel any ongoing fast recovery
 
 cleanup:
-       free(pkt);
+       return;
 }
 
 /* Update receive buffer and SACK entries after consuming data.
@@ -1252,6 +1238,19 @@ synack:
                        // Otherwise, continue processing.
                        len = 0;
                }
+       } else {
+#if UTCP_DEBUG
+               int32_t rcv_offset = seqdiff(hdr.seq, c->rcv.nxt);
+
+               if(rcv_offset) {
+                       debug(c, "packet out of order, offset %u bytes", rcv_offset);
+               }
+
+               if(rcv_offset >= 0) {
+                       c->rcv.nxt = hdr.seq + len;
+               }
+
+#endif
        }
 
        c->snd.wnd = hdr.wnd; // TODO: move below
@@ -1425,9 +1424,9 @@ synack:
 
                // Increase the congestion window according to RFC 5681
                if(c->snd.cwnd < c->snd.ssthresh) {
-                       c->snd.cwnd += min(advanced, utcp->mtu); // eq. 2
+                       c->snd.cwnd += min(advanced, utcp->mss); // eq. 2
                } else {
-                       c->snd.cwnd += max(1, (utcp->mtu * utcp->mtu) / c->snd.cwnd); // eq. 3
+                       c->snd.cwnd += max(1, (utcp->mss * utcp->mss) / c->snd.cwnd); // eq. 3
                }
 
                if(c->snd.cwnd > c->sndbuf.maxsize) {
@@ -1466,8 +1465,8 @@ synack:
                                // RFC 5681 fast recovery
                                debug(c, "fast recovery started\n", c->dupack);
                                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);
+                               c->snd.ssthresh = max(flightsize / 2, utcp->mss * 2); // eq. 4
+                               c->snd.cwnd = min(c->snd.ssthresh + 3 * utcp->mss, c->sndbuf.maxsize);
 
                                if(c->snd.cwnd > c->sndbuf.maxsize) {
                                        c->snd.cwnd = c->sndbuf.maxsize;
@@ -1477,7 +1476,7 @@ synack:
 
                                fast_retransmit(c);
                        } else if(c->dupack > 3) {
-                               c->snd.cwnd += utcp->mtu;
+                               c->snd.cwnd += utcp->mss;
 
                                if(c->snd.cwnd > c->sndbuf.maxsize) {
                                        c->snd.cwnd = c->sndbuf.maxsize;
@@ -1978,7 +1977,7 @@ struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_
        utcp->pre_accept = pre_accept;
        utcp->send = send;
        utcp->priv = priv;
-       utcp->mtu = DEFAULT_MTU;
+       utcp_set_mtu(utcp, DEFAULT_MTU);
        utcp->timeout = DEFAULT_USER_TIMEOUT; // sec
        utcp->rto = START_RTO; // usec
 
@@ -2017,10 +2016,23 @@ uint16_t utcp_get_mtu(struct utcp *utcp) {
 }
 
 void utcp_set_mtu(struct utcp *utcp, uint16_t mtu) {
-       // TODO: handle overhead of the header
-       if(utcp) {
-               utcp->mtu = mtu;
+       if (!utcp) {
+               return;
+       }
+
+       if (mtu <= sizeof(struct hdr)) {
+               return;
        }
+
+       if (mtu > utcp->mtu) {
+               char *new = realloc(utcp->pkt, mtu);
+               if (!new)
+                       return;
+               utcp->pkt = new;
+       }
+
+       utcp->mtu = mtu;
+       utcp->mss = mtu - sizeof(struct hdr);
 }
 
 void utcp_reset_timers(struct utcp *utcp) {