add optional TCP retransmission

This commit is contained in:
Sergio R. Caprile committed 2026-09-07 15:34:08 -03:00
1 parent c857ddae57
commit 5c34018899
9 files changed
+464 -41

No files matched your search

+27
View File
@@ -19,7 +19,33 @@ static size_t sock_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) {
return (size_t) received;
}
// ignore IPv6, testing segment loss under IPv4 is enough
static bool is_tcp_segment(const void *buf, size_t len) {
const struct eth *eth = (const struct eth *) buf;
const struct ip *ip;
const struct tcp *tcp;
size_t hlen;
if (len < sizeof(*eth)) return false;
if (eth->type != mg_htons(0x800)) return false;
ip = (const struct ip *) (eth + 1);
if (len < sizeof(*eth) + sizeof(*ip)) return false;
hlen = (size_t) (ip->ver & 15) * 4;
if ((ip->ver >> 4) != 4 || ip->proto != 6 || hlen < sizeof(*ip) ||
len < sizeof(*eth) + hlen + sizeof(*tcp))
return false;
tcp = (const struct tcp *) ((const char *) ip + hlen);
return (tcp->flags & (TH_SYN | TH_FIN)) == 0;
}
static size_t sock_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
uint32_t random;
if (ifp->enable_tcp_retransmit && is_tcp_segment(buf, len)) { // introduce a 30% segment loss
mg_random(&random, sizeof(random));
if (random % 10 < 3) {
MG_DEBUG(("dropping TCP segment"));
return len;
}
}
ssize_t res = send(((struct port_driver_data *)ifp->driver_data)->sockfd, buf, len, 0);
if (res < 0) {
MG_ERROR(("sock_tx failed: %d", errno));
@@ -113,6 +139,7 @@ int main(void) {
}
// RUN TESTS
mif.enable_tcp_retransmit = getenv("RTX") != NULL;
result = mip_x_test(&mgr);
close(pdd.sockfd);
if (!result) return EXIT_FAILURE;
+27
View File
@@ -22,7 +22,33 @@ static size_t tap_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) {
return (size_t) received;
}
// ignore IPv6, testing segment loss under IPv4 is enough
static bool is_tcp_segment(const void *buf, size_t len) {
const struct eth *eth = (const struct eth *) buf;
const struct ip *ip;
const struct tcp *tcp;
size_t hlen;
if (len < sizeof(*eth)) return false;
if (eth->type != mg_htons(0x800)) return false;
ip = (const struct ip *) (eth + 1);
if (len < sizeof(*eth) + sizeof(*ip)) return false;
hlen = (size_t) (ip->ver & 15) * 4;
if ((ip->ver >> 4) != 4 || ip->proto != 6 || hlen < sizeof(*ip) ||
len < sizeof(*eth) + hlen + sizeof(*tcp))
return false;
tcp = (const struct tcp *) ((const char *) ip + hlen);
return (tcp->flags & (TH_SYN | TH_FIN)) == 0;
}
static size_t tap_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
uint32_t random;
if (ifp->enable_tcp_retransmit && is_tcp_segment(buf, len)) { // introduce a 30% segment loss
mg_random(&random, sizeof(random));
if (random % 10 < 3) {
MG_DEBUG(("dropping TCP segment"));
return len;
}
}
ssize_t res = write(*(int *) ifp->driver_data, buf, len);
if (res < 0) {
MG_ERROR(("tap_tx failed: %d", errno));
@@ -125,6 +151,7 @@ int main(void) {
}
// RUN TESTS
mif.enable_tcp_retransmit = getenv("RTX") != NULL;
result = mip_x_test(&mgr);
close(fd);
if (!result) return EXIT_FAILURE;
+136 -1
View File
@@ -157,6 +157,15 @@ static void txwindow_fn(struct mg_connection *c, int ev, void *ev_data) {
(void) c, (void) ev_data;
}
static char s_rtx_data[MG_TCPIP_WIN];
static size_t s_rtx_len;
static uint16_t s_tcp_window;
static void rtx_fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_ACCEPT) mg_send(c, s_rtx_data, s_rtx_len);
(void) ev_data;
}
static void client_fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_ERROR || ev == MG_EV_CONNECT) (*(int *) c->fn_data) = ev;
(void) c, (void) ev_data;
@@ -210,6 +219,7 @@ static void test_poll(void) {
struct driver_data {
char buf[DRIVER_BUF_SIZE];
size_t len;
size_t tx_count;
bool tx_ready; // data can be read from tx
};
@@ -219,6 +229,7 @@ static size_t if_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
struct driver_data *driver_data = (struct driver_data *) ifp->driver_data;
if (len > DRIVER_BUF_SIZE) len = DRIVER_BUF_SIZE;
driver_data->len = len;
driver_data->tx_count++;
memcpy(driver_data->buf, buf, len);
driver_data->tx_ready = true;
return len;
@@ -255,7 +266,7 @@ static void create_tcp_seg(struct eth *e, struct ipp *ipp, uint32_t seq,
t.ack = mg_htonl(ack);
t.sport = mg_htons(sport);
t.dport = mg_htons(dport);
t.win = mg_htons(TCP_TEST_WIN);
t.win = mg_htons(s_tcp_window);
t.off = (uint8_t) ((sizeof(t) / 4) << 4) + (uint8_t) ((opts_len / 4) << 4);
memcpy(s_driver_data.buf, e, sizeof(*e));
#if MG_ENABLE_IPV6
@@ -307,6 +318,7 @@ static void init_tests(struct mg_mgr *mgr, struct eth *e, struct ipp *ipp,
mg_mgr_init(mgr);
memset(mif, 0, sizeof(*mif));
memset(&s_driver_data, 0, sizeof(struct driver_data));
s_tcp_window = TCP_TEST_WIN;
driver->init = NULL, driver->tx = if_tx, driver->poll = if_poll,
driver->rx = if_rx;
mif->driver = driver;
@@ -962,6 +974,127 @@ static void test_tcp_retransmit(void) {
mg_mgr_free(&mgr);
}
static void test_tcp_retransmit_queue(void) {
struct mg_mgr mgr;
struct eth e;
struct ip ip;
struct ipp ipp;
struct tcp *t = (struct tcp *) (s_driver_data.buf + sizeof(e) + sizeof(ip));
struct connstate *s;
size_t i, first, off, len = 0, retired;
uint8_t *data;
struct mg_tcpip_driver driver;
struct mg_tcpip_if mif;
ipp.ip4 = &ip;
ipp.ip6 = NULL;
for (i = 0; i < sizeof(s_rtx_data); i++) s_rtx_data[i] = (char) i;
s_rtx_len = sizeof(s_rtx_data);
init_tcp_tests(&mgr, &e, &ipp, &driver, &mif, rtx_fn);
mif.enable_tcp_retransmit = true;
s_tcp_window = MG_TCPIP_WIN;
init_tcp_handshake(&e, &ipp, &mgr);
s = (struct connstate *) (mgr.conns + 1);
// Fill the retransmission queue and verify its hard cap
while (s->seq - s->txq_una < s_rtx_len) {
while (!received_response(&s_driver_data)) mg_mgr_poll(&mgr, 0);
}
ASSERT(s->txq_seq == 2);
ASSERT(s->txq_una == 2);
ASSERT(s->seq - s->txq_una == s_rtx_len);
first = txq_next(s, &data);
ASSERT(first == s->dmss);
ASSERT(memcmp(data, s_rtx_data, first) == 0);
ASSERT(mg_io_send(mgr.conns, s_rtx_data, 1) == MG_IO_WAIT);
// Timeout resends the first retained segment only
s_driver_data.len = 0;
s_driver_data.tx_ready = false;
s_driver_data.tx_count = 0;
s->txq_timer = 0;
mg_mgr_poll(&mgr, 0);
ASSERT(s_driver_data.tx_count == 1);
ASSERT(t->flags == (TH_PUSH | TH_ACK));
ASSERT(t->seq == mg_htonl(2));
ASSERT(s->txq_retries == 1);
ASSERT(txq_next(s, &data) == first);
ASSERT(memcmp(data, s_rtx_data, first) == 0);
// A piggybacked partial ACK advances SND.UNA but retains the segment
retired = first / 2;
create_tcp_simpleseg(&e, &ipp, 1001, 2 + (uint32_t) retired,
TH_PUSH | TH_ACK, 2);
mg_mgr_poll(&mgr, 0);
ASSERT(s->txq_seq == 2);
ASSERT(s->txq_una == 2 + retired);
ASSERT(s->seq - s->txq_una == s_rtx_len - retired);
ASSERT(s->txq_retries == 0);
ASSERT(txq_next(s, &data) == first);
ASSERT(memcmp(data, s_rtx_data, first) == 0);
ASSERT(mgr.conns->recv.len == 2);
// Timeout after partial ACK retains the segment's original SEQ and data
s_driver_data.tx_count = 0;
s->txq_timer = 0;
mg_mgr_poll(&mgr, 0);
ASSERT(s_driver_data.tx_count == 1);
ASSERT(t->seq == mg_htonl(2));
ASSERT(s->txq_retries == 1);
ASSERT(txq_next(s, &data) == first);
ASSERT(memcmp(data, s_rtx_data, first) == 0);
// ACKing record one and part of two retires only record one
create_tcp_simpleseg(&e, &ipp, 1003, 2 + (uint32_t) (first + retired),
TH_ACK, 0);
mg_mgr_poll(&mgr, 0);
ASSERT(s->txq_seq == 2 + first);
ASSERT(s->txq_una == 2 + first + retired);
ASSERT(s->txq_retries == 0);
ASSERT(txq_next(s, &data) == first);
ASSERT(memcmp(data, s_rtx_data + first, first) == 0);
// Timeout retransmits record two with its original SEQ and data
s_driver_data.tx_count = 0;
s->txq_timer = 0;
mg_mgr_poll(&mgr, 0);
ASSERT(s_driver_data.tx_count == 1);
ASSERT(t->seq == mg_htonl(2 + (uint32_t) first));
ASSERT(s->txq_retries == 1);
ASSERT(txq_next(s, &data) == first);
ASSERT(memcmp(data, s_rtx_data + first, first) == 0);
// A complete ACK retires record two; appending restores the full flight
create_tcp_simpleseg(&e, &ipp, 1003, 2 + (uint32_t) (2 * first), TH_ACK, 0);
mg_mgr_poll(&mgr, 0);
ASSERT(s->txq_seq == 2 + 2 * first);
ASSERT(s->txq_una == 2 + 2 * first);
ASSERT(txq_next(s, &data) == first);
ASSERT(memcmp(data, s_rtx_data + 2 * first, first) == 0);
mg_send(mgr.conns, s_rtx_data, 2 * first);
while (s->seq - s->txq_una < s_rtx_len) {
while (!received_response(&s_driver_data)) mg_mgr_poll(&mgr, 0);
}
for (off = 0; off < s->txq.len; off += sizeof(uint32_t) + len) {
memcpy(&len, s->txq.buf + off, sizeof(uint32_t));
}
ASSERT(off == s->txq.len);
ASSERT(len == first);
ASSERT(memcmp(s->txq.buf + off - len, s_rtx_data + first, len) == 0);
// Draining waits for the rebuilt flight to be acknowledged before FIN
mgr.conns->is_draining = 1;
s_driver_data.len = 0;
mg_mgr_poll(&mgr, 0);
ASSERT(!received_response(&s_driver_data));
create_tcp_simpleseg(&e, &ipp, 1003, s->seq, TH_ACK, 0);
while (!received_response(&s_driver_data)) mg_mgr_poll(&mgr, 0);
ASSERT(t->flags == (TH_FIN | TH_ACK));
ASSERT(s->txq.len == 0);
s_driver_data.len = 0;
mg_mgr_free(&mgr);
}
static void test_tcp_txwindow(void) {
struct mg_mgr mgr;
@@ -987,6 +1120,7 @@ static void test_tcp_txwindow(void) {
seq = (uint32_t)(mg_htonl(t->seq) + s_driver_data.len - (size_t)((char *)((uint32_t *)t + (t->off >> 4)) - s_driver_data.buf));
} while (seq < (TCP_TEST_WIN + 2));
stallcount = count;
ASSERT(((struct connstate *) (mgr.conns + 1))->txq.buf == NULL);
mg_mgr_poll(&mgr, 0), s_driver_data.len = 0;
mg_mgr_poll(&mgr, 0), s_driver_data.len = 0;
ASSERT((stallcount == count));
@@ -1248,6 +1382,7 @@ static void test_tcp(bool ipv6) {
if (!ipv6) {
test_tcp_backlog();
test_tcp_retransmit();
test_tcp_retransmit_queue();
test_tcp_txwindow();
test_tcp_ackseq();
test_tcp_drain(false);
+18 -6
View File
@@ -75,6 +75,8 @@ static char *host_ip, *host_ip6;
static int s_num_tests = 0;
static bool s_error = false;
#define POLL_LIMIT(n) (mgr->ifp->enable_tcp_retransmit ? (n) * 10 : (n))
#ifdef NO_ABORT
static int s_abort = 0;
#define ABORT() ++s_abort, s_error = true
@@ -205,7 +207,7 @@ static int fetch(struct mg_mgr *mgr, char *buf, const char *url,
buf[0] = '\0';
// - TLS: multiple (small) records: allow enough loops so mg_mgr_poll can
// process buffered records when no more frames are coming in
for (i = 0; i < 500 && buf[0] == '\0' && !fd.closed; i++) {
for (i = 0; i < POLL_LIMIT(500) && buf[0] == '\0' && !fd.closed; i++) {
mg_mgr_poll(mgr, 0);
usleep(5000); // 5 ms. Slow down poll loop to ensure packet transit, but
// allow enough loops to get the ARP response, otherwise,
@@ -360,7 +362,9 @@ static void test_mqtt_connsubpub(struct mg_mgr *mgr) {
mg_mgr_poll(mgr, 0);
s_conn = mg_mqtt_connect(mgr, data.url, &opts, mqtt_fn, &data);
ASSERT(s_conn != NULL);
for (int i = 0; i < 1000 && s_conn != NULL && !s_conn->is_closing; i++) {
for (int i = 0;
i < POLL_LIMIT(1000) && s_conn != NULL && !s_conn->is_closing;
i++) {
mg_mgr_poll(mgr, 0);
usleep(5000); // 5 ms (*) See fetch() above for reasons
}
@@ -406,7 +410,9 @@ static void test_mqtt_connsubpub(struct mg_mgr *mgr) {
mg_mgr_poll(mgr, 0);
s_conn = mg_mqtt_connect(mgr, data.url, &opts, mqtt_fn, &data);
ASSERT(s_conn != NULL);
for (int i = 0; i < 1000 && s_conn != NULL && !s_conn->is_closing; i++) {
for (int i = 0;
i < POLL_LIMIT(1000) && s_conn != NULL && !s_conn->is_closing;
i++) {
mg_mgr_poll(mgr, 0);
usleep(5000); // 5 ms (*) See fetch() above for reasons
}
@@ -418,10 +424,12 @@ static void test_mqtt_connsubpub(struct mg_mgr *mgr) {
#ifndef NO_HTTPSERVER_TEST
#include <pthread.h>
static volatile bool s_poll_done;
static void *poll_thread(void *p) {
struct mg_mgr *mgr = (struct mg_mgr *) p;
int i;
for (i = 0; i < 300; i++) {
for (i = 0; i < POLL_LIMIT(300) && !s_poll_done; i++) {
mg_mgr_poll(mgr, 0);
usleep(10000); // 10 ms. Slow down poll loop to ensure packet transit
}
@@ -436,6 +444,7 @@ static void test_http_server(struct mg_mgr *mgr) {
struct mg_connection *c;
char *cmd;
pthread_t thread_id = (pthread_t) 0;
int rc;
#if MG_TLS
struct mg_tls_opts opts;
memset(&opts, 0, sizeof(opts));
@@ -455,12 +464,15 @@ static void test_http_server(struct mg_mgr *mgr) {
#endif
ASSERT(c != NULL);
ASSERT (mg_send(c, "NADA", 0)); // check mg_send allows len=0
s_poll_done = false;
pthread_create(&thread_id, NULL, poll_thread,
mgr); // simpler this way, no concurrency anyway
MG_DEBUG(("CURL"));
ASSERT(system(cmd) == 0); // wait for curl
rc = system(cmd); // wait for curl
MG_DEBUG(("MONGOOSE"));
s_poll_done = true;
pthread_join(thread_id, NULL); // wait for Mongoose
ASSERT(rc == 0);
MG_DEBUG(("DONE"));
free(cmd);
#endif
@@ -529,7 +541,7 @@ static bool sntpms(struct mg_mgr *mgr, const char *url) {
int64_t ms = 0;
int i;
mg_sntp_connect(mgr, url, sntpcb, &ms);
for (i = 0; i < 50 && ms == 0; i++) {
for (i = 0; i < POLL_LIMIT(50) && ms == 0; i++) {
mg_mgr_poll(mgr, 0);
usleep(10000); // 10 ms. Slow down poll loop to ensure packet transit
}