From 881b282604806e889857a62204a0518fe10dfcec Mon Sep 17 00:00:00 2001 From: cpq Date: Wed, 7 Sep 2022 20:48:23 +0100 Subject: [PATCH] Implement ack timer --- examples/mip-pcap/Makefile | 5 ++-- examples/mip-pcap/main.c | 48 +++++++++++--------------------------- mip/mip.c | 44 +++++++++++++++++++++++++++------- mongoose.c | 47 +++++++++++++++++++++++++++++-------- src/net.c | 3 +-- 5 files changed, 90 insertions(+), 57 deletions(-) diff --git a/examples/mip-pcap/Makefile b/examples/mip-pcap/Makefile index 15bea4bf..8a1934ac 100644 --- a/examples/mip-pcap/Makefile +++ b/examples/mip-pcap/Makefile @@ -1,13 +1,14 @@ PROG ?= example -DEFS ?= -DMG_ENABLE_LINES=1 -DMG_ENABLE_MIP=1 -DMG_ENABLE_SOCKET=0 -DMG_ENABLE_WINSOCK=0 -I../.. +DEFS ?= -DMG_ENABLE_LINES=1 -DMG_ENABLE_MIP=1 -DMG_ENABLE_SOCKET=0 -DMG_ENABLE_WINSOCK=0 -DMG_ENABLE_PACKED_FS=1 -I../.. CFLAGS ?= -W -Wall LIBS ?= -lpcap +SOURCES = main.c ../../mongoose.c ../device-dashboard/net.c ../device-dashboard/packed_fs.c all: $(PROG) $(RUN) ./$(PROG) $(ARGS) $(PROG): main.c - $(CC) main.c ../../mongoose.c $(CFLAGS) $(DEFS) -o $(PROG) $(LIBS) + $(CC) $(SOURCES) $(CFLAGS) $(DEFS) -o $(PROG) $(LIBS) clean: rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb diff --git a/examples/mip-pcap/main.c b/examples/mip-pcap/main.c index 0b1aace2..79903e69 100644 --- a/examples/mip-pcap/main.c +++ b/examples/mip-pcap/main.c @@ -19,8 +19,6 @@ static size_t pcap_tx(const void *buf, size_t len, void *userdata) { if (res == PCAP_ERROR) { MG_ERROR(("pcap_inject: %d", res)); } - MG_INFO(("TX %lu", len)); - // mg_hexdump(buf, len); return res == PCAP_ERROR ? 0 : len; } @@ -32,40 +30,22 @@ static size_t pcap_rx(void *buf, size_t len, void *userdata) { size_t received = 0; struct pcap_pkthdr *hdr = NULL; const unsigned char *pkt = NULL; - if (pcap_next_ex((pcap_t *) userdata, &hdr, &pkt) == 1) { + + // To avoid busy-loop and 100% CPU time, wait on pcap for some time + int fd = pcap_get_selectable_fd((pcap_t *) userdata); // Pcap file descriptor + struct timeval tv = {0, 50000}; // 50 ms + fd_set rset; + FD_ZERO(&rset); + FD_SET(fd, &rset); + if (select(fd + 1, &rset, 0, 0, &tv) == 1 && // Have data ? + pcap_next_ex((pcap_t *) userdata, &hdr, &pkt) == 1) { // Yes, read received = hdr->len < len ? hdr->len : len; memcpy(buf, pkt, received); - MG_INFO(("RX %lu", received)); - // mg_hexdump(buf, received); } + return received; } -static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - if (ev == MG_EV_HTTP_MSG) mg_http_reply(c, 200, NULL, "hi\n"); - (void) fn_data, (void) ev_data; -} - -// MQTT event handler function -static void mfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - if (ev == MG_EV_CONNECT && mg_url_is_ssl(MQTT_URL)) { - struct mg_tls_opts opts = {.ca = "ca.pem", - .srvname = mg_url_host(MQTT_URL)}; - mg_tls_init(c, &opts); - } else if (ev == MG_EV_MQTT_OPEN) { - c->is_hexdumping = 1; - mg_mqtt_sub(c, mg_str(MQTT_TOPIC), 2); - } else if (ev == MG_EV_MQTT_MSG) { - struct mg_mqtt_message *mm = ev_data; - MG_INFO(("%.*s", (int) mm->data.len, mm->data.ptr)); - } else if (ev == MG_EV_MQTT_CMD) { - struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data; - MG_DEBUG(("cmd %d qos %d", mm->cmd, mm->qos)); - } else if (ev == MG_EV_CLOSE) { - } - (void) fn_data; -} - int main(int argc, char *argv[]) { const char *iface = "lo0"; // Network iface const char *mac = "00:00:01:02:03:77"; // MAC address @@ -126,12 +106,10 @@ int main(int argc, char *argv[]) { mip_init(&mgr, &c, &driver, ph); MG_INFO(("Init done, starting main loop")); - mg_http_listen(&mgr, "http://0.0.0.0:8000", fn, NULL); + extern void device_dashboard_fn(struct mg_connection *, int, void *, void *); + mg_http_listen(&mgr, "http://0.0.0.0:8000", device_dashboard_fn, &mgr); - struct mg_mqtt_opts opts = {0}; - // mg_mqtt_connect(&mgr, MQTT_URL, &opts, mfn, NULL); - - while (s_signo == 0) mg_mgr_poll(&mgr, 1); // Infinite event loop + while (s_signo == 0) mg_mgr_poll(&mgr, 100); // Infinite event loop mg_mgr_free(&mgr); pcap_close(ph); diff --git a/mip/mip.c b/mip/mip.c index 9b054c4b..16e2356a 100644 --- a/mip/mip.c +++ b/mip/mip.c @@ -16,11 +16,14 @@ #define MIP_ARP_ENTRIES 5 // Number of ARP cache entries. Maximum 21 #endif #define MIP_ARP_CS (2 + 12 * MIP_ARP_ENTRIES) // ARP cache size +#define MIP_TCP_KEEPALIVE_MS 5000 // TCP keep-alive period, ms +#define MIP_TCP_ACK_MS 150 // Timeout for ACKing struct connstate { uint32_t seq, ack; // TCP seq/ack counters - uint64_t timer; // Used to send ACKs + uint64_t timer; // TCP keep-alive / ACK timer uint8_t mac[6]; // Peer MAC address + uint8_t ttype; // Timer type. 0: ack, 1: keep-alive }; struct str { @@ -49,6 +52,7 @@ struct mip_if { struct mg_mgr *mgr; // Mongoose event manager // Internal state, user can use it but should not change it + uint64_t now; // Current time uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state uint8_t arp_cache[MIP_ARP_CS]; // Each entry is 12 bytes uint16_t eport; // Next ephemeral port @@ -244,6 +248,7 @@ static void arp_cache_init(uint8_t *p, int n, int size) { p[1] = p[3 + (n - 1) * size] = 2; } +#if 0 static inline void arp_cache_dump(const uint8_t *p) { MG_INFO(("ARP cache:")); for (uint8_t i = 0, j = p[1]; i < MIP_ARP_ENTRIES; i++, j = p[j + 1]) { @@ -252,6 +257,7 @@ static inline void arp_cache_dump(const uint8_t *p) { p[j + 11])); } } +#endif static uint8_t *arp_cache_find(struct mip_if *ifp, uint32_t ip) { uint8_t *p = ifp->arp_cache; @@ -375,7 +381,6 @@ static void tx_dhcp_request(struct mip_if *ifp, uint32_t src, uint32_t dst) { memcpy(opts + 14, &dst, sizeof(dst)); memcpy(opts + 20, &src, sizeof(src)); tx_dhcp(ifp, src, dst, opts, sizeof(opts)); - MG_DEBUG(("DHCP request sent")); } static void tx_dhcp_discover(struct mip_if *ifp) { @@ -430,7 +435,7 @@ static void rx_dhcp(struct mip_if *ifp, struct pkt *pkt) { uint32_t ip = 0, gw = 0, mask = 0; uint8_t *p = pkt->dhcp->options, *end = &pkt->raw.buf[pkt->raw.len]; if (end < (uint8_t *) (pkt->dhcp + 1)) return; - MG_DEBUG(("DHCP %u", (unsigned) pkt->raw.len)); + // MG_DEBUG(("DHCP %u", (unsigned) pkt->raw.len)); while (p < end && p[0] != 255) { if (p[0] == 1 && p[1] == sizeof(ifp->mask)) { memcpy(&mask, p + 2, sizeof(mask)); @@ -526,9 +531,8 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn, s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq); c->rem.ip = pkt->ip->src; c->rem.port = pkt->tcp->sport; - MG_DEBUG(("%lu accepted %lx:%hx", c->id, c->rem.ip, c->rem.port)); + MG_DEBUG(("%lu accepted %lx:%hx", c->id, mg_ntohl(c->rem.ip), c->rem.port)); LIST_ADD_HEAD(struct mg_connection, &lsn->mgr->conns, c); - c->fd = (void *) (size_t) mg_ntohl(pkt->tcp->ack); c->is_accepted = 1; c->is_hexdumping = lsn->is_hexdumping; c->pfn = lsn->pfn; @@ -563,11 +567,16 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) { static void rx_tcp(struct mip_if *ifp, struct pkt *pkt) { struct mg_connection *c = getpeer(ifp->mgr, pkt, false); + struct connstate *s = (struct connstate *) (c + 1); + + if (c != NULL) { + s->timer = ifp->now + MIP_TCP_KEEPALIVE_MS; // Shift next keep-alive + s->ttype = 1; // ping to the future + } #if 0 MG_INFO(("%lu %hhu %d", c ? c->id : 0, pkt->tcp->flags, (int) pkt->pay.len)); #endif if (c != NULL && c->is_connecting && pkt->tcp->flags & (TH_SYN | TH_ACK)) { - struct connstate *s = (struct connstate *) (c + 1); s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq) + 1; tx_tcp_pkt(ifp, pkt, TH_ACK, pkt->tcp->ack, NULL, 0); c->is_connecting = 0; // Client connected @@ -581,6 +590,8 @@ static void rx_tcp(struct mip_if *ifp, struct pkt *pkt) { mg_hexdump(pkt->pay.buf, pkt->pay.len); #endif read_conn(c, pkt); + s->timer = ifp->now + MIP_TCP_ACK_MS; // Set ACK timeout + s->ttype = 0; } else if ((c = getpeer(ifp->mgr, pkt, true)) == NULL) { tx_tcp_pkt(ifp, pkt, TH_RST | TH_ACK, pkt->tcp->ack, NULL, 0); } else if (pkt->tcp->flags & TH_SYN) { @@ -675,6 +686,7 @@ static void mip_rx(struct mip_if *ifp, void *buf, size_t len) { static void mip_poll(struct mip_if *ifp, uint64_t uptime_ms) { if (ifp == NULL || ifp->driver == NULL) return; bool expired_1000ms = mg_timer_expired(&ifp->timer_1000ms, 1000, uptime_ms); + ifp->now = uptime_ms; // Handle physical interface up/down status if (expired_1000ms && ifp->driver->up) { @@ -706,6 +718,23 @@ static void mip_poll(struct mip_if *ifp, uint64_t uptime_ms) { if (len == 0) break; mip_rx(ifp, ifp->rx.buf, len); } + + // Process timeouts + for (struct mg_connection *c = ifp->mgr->conns; c != NULL; c = c->next) { + if (c->is_udp || c->is_listening) continue; + struct connstate *s = (struct connstate *) (c + 1); + if (uptime_ms > s->timer) { + if (s->ttype == 0) { + MG_INFO(("%lu sending ack", c->id)); + tx_tcp(ifp, c->rem.ip, TH_ACK, c->loc.port, c->rem.port, + mg_htonl(s->seq), mg_htonl(s->ack), "", 0); + } else { + MG_INFO(("%lu sending keepalive", c->id)); + } + s->timer = uptime_ms + MIP_TCP_KEEPALIVE_MS; + s->ttype = 1; + } + } } // This function executes in interrupt context, thus it should copy data @@ -797,7 +826,6 @@ static void write_conn(struct mg_connection *c) { static void fin_conn(struct mg_connection *c) { struct mip_if *ifp = (struct mip_if *) c->mgr->priv; struct connstate *s = (struct connstate *) (c + 1); - MG_INFO(("AAA")); tx_tcp(ifp, c->rem.ip, TH_FIN | TH_ACK, c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0); } @@ -826,7 +854,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) { bool mg_send(struct mg_connection *c, const void *buf, size_t len) { struct mip_if *ifp = (struct mip_if *) c->mgr->priv; bool res = false; - if (ifp->ip == 0) { + if (ifp->ip == 0 || ifp->state != MIP_STATE_READY) { mg_error(c, "net down"); } else if (c->is_udp) { tx_udp(ifp, ifp->ip, c->loc.port, c->rem.ip, c->rem.port, buf, len); diff --git a/mongoose.c b/mongoose.c index 65545fab..9a307cfe 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3434,9 +3434,8 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, c->is_udp = (strncmp(url, "udp:", 4) == 0); c->fn = fn; c->is_client = true; - c->fd = (void *) (size_t) -1; // Set to invalid socket c->fn_data = fn_data; - MG_DEBUG(("%lu -1 %s", c->id, url)); + MG_DEBUG(("%lu %p %s", c->id, c->fd, url)); mg_call(c, MG_EV_OPEN, NULL); mg_resolve(c, url); } @@ -6305,11 +6304,14 @@ struct mip_driver mip_driver_w5500 = { #define MIP_ARP_ENTRIES 5 // Number of ARP cache entries. Maximum 21 #endif #define MIP_ARP_CS (2 + 12 * MIP_ARP_ENTRIES) // ARP cache size +#define MIP_TCP_KEEPALIVE_MS 5000 // TCP keep-alive period, ms +#define MIP_TCP_ACK_MS 150 // Timeout for ACKing struct connstate { uint32_t seq, ack; // TCP seq/ack counters - uint64_t timer; // Used to send ACKs + uint64_t timer; // TCP keep-alive / ACK timer uint8_t mac[6]; // Peer MAC address + uint8_t ttype; // Timer type. 0: ack, 1: keep-alive }; struct str { @@ -6338,6 +6340,7 @@ struct mip_if { struct mg_mgr *mgr; // Mongoose event manager // Internal state, user can use it but should not change it + uint64_t now; // Current time uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state uint8_t arp_cache[MIP_ARP_CS]; // Each entry is 12 bytes uint16_t eport; // Next ephemeral port @@ -6533,6 +6536,7 @@ static void arp_cache_init(uint8_t *p, int n, int size) { p[1] = p[3 + (n - 1) * size] = 2; } +#if 0 static inline void arp_cache_dump(const uint8_t *p) { MG_INFO(("ARP cache:")); for (uint8_t i = 0, j = p[1]; i < MIP_ARP_ENTRIES; i++, j = p[j + 1]) { @@ -6541,6 +6545,7 @@ static inline void arp_cache_dump(const uint8_t *p) { p[j + 11])); } } +#endif static uint8_t *arp_cache_find(struct mip_if *ifp, uint32_t ip) { uint8_t *p = ifp->arp_cache; @@ -6664,7 +6669,6 @@ static void tx_dhcp_request(struct mip_if *ifp, uint32_t src, uint32_t dst) { memcpy(opts + 14, &dst, sizeof(dst)); memcpy(opts + 20, &src, sizeof(src)); tx_dhcp(ifp, src, dst, opts, sizeof(opts)); - MG_DEBUG(("DHCP request sent")); } static void tx_dhcp_discover(struct mip_if *ifp) { @@ -6719,7 +6723,7 @@ static void rx_dhcp(struct mip_if *ifp, struct pkt *pkt) { uint32_t ip = 0, gw = 0, mask = 0; uint8_t *p = pkt->dhcp->options, *end = &pkt->raw.buf[pkt->raw.len]; if (end < (uint8_t *) (pkt->dhcp + 1)) return; - MG_DEBUG(("DHCP %u", (unsigned) pkt->raw.len)); + // MG_DEBUG(("DHCP %u", (unsigned) pkt->raw.len)); while (p < end && p[0] != 255) { if (p[0] == 1 && p[1] == sizeof(ifp->mask)) { memcpy(&mask, p + 2, sizeof(mask)); @@ -6815,9 +6819,8 @@ static struct mg_connection *accept_conn(struct mg_connection *lsn, s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq); c->rem.ip = pkt->ip->src; c->rem.port = pkt->tcp->sport; - MG_DEBUG(("%lu accepted %lx:%hx", c->id, c->rem.ip, c->rem.port)); + MG_DEBUG(("%lu accepted %lx:%hx", c->id, mg_ntohl(c->rem.ip), c->rem.port)); LIST_ADD_HEAD(struct mg_connection, &lsn->mgr->conns, c); - c->fd = (void *) (size_t) mg_ntohl(pkt->tcp->ack); c->is_accepted = 1; c->is_hexdumping = lsn->is_hexdumping; c->pfn = lsn->pfn; @@ -6852,11 +6855,16 @@ static void read_conn(struct mg_connection *c, struct pkt *pkt) { static void rx_tcp(struct mip_if *ifp, struct pkt *pkt) { struct mg_connection *c = getpeer(ifp->mgr, pkt, false); + struct connstate *s = (struct connstate *) (c + 1); + + if (c != NULL) { + s->timer = ifp->now + MIP_TCP_KEEPALIVE_MS; // Shift next keep-alive + s->ttype = 1; // ping to the future + } #if 0 MG_INFO(("%lu %hhu %d", c ? c->id : 0, pkt->tcp->flags, (int) pkt->pay.len)); #endif if (c != NULL && c->is_connecting && pkt->tcp->flags & (TH_SYN | TH_ACK)) { - struct connstate *s = (struct connstate *) (c + 1); s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq) + 1; tx_tcp_pkt(ifp, pkt, TH_ACK, pkt->tcp->ack, NULL, 0); c->is_connecting = 0; // Client connected @@ -6870,6 +6878,8 @@ static void rx_tcp(struct mip_if *ifp, struct pkt *pkt) { mg_hexdump(pkt->pay.buf, pkt->pay.len); #endif read_conn(c, pkt); + s->timer = ifp->now + MIP_TCP_ACK_MS; // Set ACK timeout + s->ttype = 0; } else if ((c = getpeer(ifp->mgr, pkt, true)) == NULL) { tx_tcp_pkt(ifp, pkt, TH_RST | TH_ACK, pkt->tcp->ack, NULL, 0); } else if (pkt->tcp->flags & TH_SYN) { @@ -6964,6 +6974,7 @@ static void mip_rx(struct mip_if *ifp, void *buf, size_t len) { static void mip_poll(struct mip_if *ifp, uint64_t uptime_ms) { if (ifp == NULL || ifp->driver == NULL) return; bool expired_1000ms = mg_timer_expired(&ifp->timer_1000ms, 1000, uptime_ms); + ifp->now = uptime_ms; // Handle physical interface up/down status if (expired_1000ms && ifp->driver->up) { @@ -6995,6 +7006,23 @@ static void mip_poll(struct mip_if *ifp, uint64_t uptime_ms) { if (len == 0) break; mip_rx(ifp, ifp->rx.buf, len); } + + // Process timeouts + for (struct mg_connection *c = ifp->mgr->conns; c != NULL; c = c->next) { + if (c->is_udp || c->is_listening) continue; + struct connstate *s = (struct connstate *) (c + 1); + if (uptime_ms > s->timer) { + if (s->ttype == 0) { + MG_INFO(("%lu sending ack", c->id)); + tx_tcp(ifp, c->rem.ip, TH_ACK, c->loc.port, c->rem.port, + mg_htonl(s->seq), mg_htonl(s->ack), "", 0); + } else { + MG_INFO(("%lu sending keepalive", c->id)); + } + s->timer = uptime_ms + MIP_TCP_KEEPALIVE_MS; + s->ttype = 1; + } + } } // This function executes in interrupt context, thus it should copy data @@ -7086,7 +7114,6 @@ static void write_conn(struct mg_connection *c) { static void fin_conn(struct mg_connection *c) { struct mip_if *ifp = (struct mip_if *) c->mgr->priv; struct connstate *s = (struct connstate *) (c + 1); - MG_INFO(("AAA")); tx_tcp(ifp, c->rem.ip, TH_FIN | TH_ACK, c->loc.port, c->rem.port, mg_htonl(s->seq), mg_htonl(s->ack), NULL, 0); } @@ -7115,7 +7142,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) { bool mg_send(struct mg_connection *c, const void *buf, size_t len) { struct mip_if *ifp = (struct mip_if *) c->mgr->priv; bool res = false; - if (ifp->ip == 0) { + if (ifp->ip == 0 || ifp->state != MIP_STATE_READY) { mg_error(c, "net down"); } else if (c->is_udp) { tx_udp(ifp, ifp->ip, c->loc.port, c->rem.ip, c->rem.port, buf, len); diff --git a/src/net.c b/src/net.c index 5be5b9e9..146cca71 100644 --- a/src/net.c +++ b/src/net.c @@ -178,9 +178,8 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, c->is_udp = (strncmp(url, "udp:", 4) == 0); c->fn = fn; c->is_client = true; - c->fd = (void *) (size_t) -1; // Set to invalid socket c->fn_data = fn_data; - MG_DEBUG(("%lu -1 %s", c->id, url)); + MG_DEBUG(("%lu %p %s", c->id, c->fd, url)); mg_call(c, MG_EV_OPEN, NULL); mg_resolve(c, url); }