From: Guus Sliepen Date: Thu, 19 Mar 2020 21:17:34 +0000 (+0100) Subject: Allocate a persistent packet buffer. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=d41728102f197003c7db2db6f679e1ca5e26535d;p=utcp Allocate a persistent packet buffer. Instead of allocating a new packet buffer every time we generate a packet, we pre-allocate it and keep it around. We only reallocate when the MTU increases. --- diff --git a/utcp.c b/utcp.c index 50c7e36..f7d18b9 100644 --- a/utcp.c +++ b/utcp.c @@ -549,13 +549,7 @@ static void ack(struct utcp_connection *c, bool sendatleastone) { struct { struct hdr hdr; uint8_t data[]; - } *pkt; - - pkt = malloc(c->utcp->mtu); - - if(!pkt) { - return; - } + } *pkt = c->utcp->pkt; pkt->hdr.src = c->src; pkt->hdr.dst = c->dst; @@ -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) { @@ -762,13 +754,7 @@ static void retransmit(struct utcp_connection *c) { struct { struct hdr hdr; uint8_t data[]; - } *pkt; - - pkt = malloc(c->utcp->mtu); - - if(!pkt) { - return; - } + } *pkt = c->utcp->pkt; pkt->hdr.src = c->src; pkt->hdr.dst = c->dst; @@ -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. @@ -1991,8 +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->mss = DEFAULT_MTU - sizeof(struct hdr); + utcp_set_mtu(utcp, DEFAULT_MTU); utcp->timeout = DEFAULT_USER_TIMEOUT; // sec utcp->rto = START_RTO; // usec @@ -2031,11 +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; - utcp->mss = mtu - sizeof(struct hdr); + 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) { diff --git a/utcp_priv.h b/utcp_priv.h index de942a8..49ff4bf 100644 --- a/utcp_priv.h +++ b/utcp_priv.h @@ -169,6 +169,10 @@ struct utcp { utcp_pre_accept_t pre_accept; utcp_send_t send; + // Packet buffer + + void *pkt; + // Global socket options uint16_t mtu; // The maximum size of a UTCP packet, including headers.