From d0374541c5c660a0dd6aff1dbe5c8e2a8399fdea Mon Sep 17 00:00:00 2001 From: bloqaudio Date: Mon, 21 Sep 2026 11:05:07 -0500 Subject: [PATCH] uip: rate-limit the dropped-fragment log uip has no reassembly, so every fragment it receives is discarded and logged. A neighbour that broadcasts fragmented traffic therefore produces a continuous stream of "ip: fragment dropped." rather than an occasional one, at roughly thirteen lines a second on a quiet segment, which buries every other console message. The message carries real information: a sudden burst of it says something about the network. What carries none is repeating it per fragment. Report the count once per five second window instead, which keeps the signal and ends the flood. The count is printed through the no_syslog primitives, as the original message was, so the flood cannot move from the console into the log ring. itoa() writes through write_char(), which also feeds the ring, so it gets a no_syslog twin beside it rather than a private copy of its digits. --- rtl837x_common.h | 1 + rtlplayground.c | 13 +++++++++++++ test/test_httpd_tx.c | 2 ++ uip/uip.c | 20 +++++++++++++++++++- 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/rtl837x_common.h b/rtl837x_common.h index 5e1373f8..75325c28 100644 --- a/rtl837x_common.h +++ b/rtl837x_common.h @@ -143,6 +143,7 @@ void print_long(uint32_t a); void print_short(uint16_t a); void print_byte(uint8_t a); void itoa(uint8_t v); +void itoa_no_syslog(uint8_t v); void itoa_short(uint16_t v); void print_sfr_data(void); void print_phy_data(void); diff --git a/rtlplayground.c b/rtlplayground.c index 6b0bf2df..9de5f4d4 100644 --- a/rtlplayground.c +++ b/rtlplayground.c @@ -277,6 +277,19 @@ void itoa(uint8_t v) } +void itoa_no_syslog(uint8_t v) +{ + uint8_t t = (v / 100); + uint8_t print_zeros = t; + if (print_zeros) + write_char_no_syslog('0' + t); + t = (v / 10) % 10; + print_zeros |= t; + if (print_zeros) + write_char_no_syslog('0' + t); + write_char_no_syslog('0' + (v % 10)); +} + /* Same as itoa(), one decade wider: enough for a port number. Kept separate * rather than widening itoa() itself, because every existing caller passes a * byte and would start paying for 16-bit divisions it does not need. */ diff --git a/test/test_httpd_tx.c b/test/test_httpd_tx.c index 6f464d2b..7fae3afd 100644 --- a/test/test_httpd_tx.c +++ b/test/test_httpd_tx.c @@ -98,7 +98,9 @@ void reset_chip(void) { } void delay(uint16_t t) { (void)t; } void write_char(char c) { (void)c; } void write_char_no_syslog(char c) { (void)c; } +void itoa_no_syslog(uint8_t v) { (void)v; } void print_string(const char *p) { (void)p; } +void print_string_no_syslog(const char *p) { (void)p; } void print_string_newline_no_syslog(const char *p) { (void)p; } void set_sys_led_state(uint8_t state) { (void)state; } void cmd_parser(void) { } diff --git a/uip/uip.c b/uip/uip.c index 774067da..86fb4be8 100644 --- a/uip/uip.c +++ b/uip/uip.c @@ -98,6 +98,7 @@ #include "../rtl837x_regs.h" extern __xdata uint8_t rx_headers[16]; +extern volatile __xdata uint32_t ticks; /*---------------------------------------------------------------------------*/ /* Variable definitions. */ @@ -243,6 +244,9 @@ __xdata struct uip_stats uip_stat; #if UIP_LOGGING == 1 #define UIP_LOG(m) print_string_newline_no_syslog(m); +#define FRAGDROP_WINDOW (5 * SYS_TICK_HZ) +static __xdata u16_t fragdrop_mark; +static __xdata u8_t fragdrop_count; #else #define UIP_LOG(m) #endif /* UIP_LOGGING == 1 */ @@ -710,6 +714,17 @@ uip_process(u8_t flag) __banked /* Check if we were invoked because of the perodic timer fireing. */ } else if(flag == UIP_TIMER) { // print_string("T1\n"); +#if UIP_LOGGING == 1 + if(fragdrop_count && (u16_t)ticks - fragdrop_mark >= FRAGDROP_WINDOW) { + print_string_no_syslog("ip: fragments dropped: "); + itoa_no_syslog(fragdrop_count); + if(fragdrop_count == 0xff) + write_char_no_syslog('+'); + write_char_no_syslog('\n'); + fragdrop_mark = (u16_t)ticks; + fragdrop_count = 0; + } +#endif /* UIP_LOGGING == 1 */ #if UIP_IDLE_TIMEOUT if(uip_connr == uip_conns) { uip_idle_age = 0; @@ -915,7 +930,10 @@ uip_process(u8_t flag) __banked #else /* UIP_REASSEMBLY */ UIP_STAT(++uip_stat.ip.drop); UIP_STAT(++uip_stat.ip.fragerr); - UIP_LOG("ip: fragment dropped."); +#if UIP_LOGGING == 1 + if(fragdrop_count != 0xff) + ++fragdrop_count; +#endif /* UIP_LOGGING == 1 */ goto drop; #endif /* UIP_REASSEMBLY */ }