From fd3d66e42413520b817287d62d04811ef494253d Mon Sep 17 00:00:00 2001 From: kuwoyuki Date: Sun, 9 Nov 2025 20:24:50 +0600 Subject: [PATCH] tweak lwipopts.h --- Makefile | 4 ++-- README.md | 14 -------------- port/arch/cc.h | 2 ++ port/{ => arch}/sys_arch.c | 15 ++++++++------- port/arch/sys_arch.h | 8 ++++++++ port/ethernetif.c | 19 ++++++------------- port/lwipopts.h | 16 ++++++++-------- 7 files changed, 34 insertions(+), 44 deletions(-) rename port/{ => arch}/sys_arch.c (61%) create mode 100644 port/arch/sys_arch.h diff --git a/Makefile b/Makefile index d828b97..31a12ff 100644 --- a/Makefile +++ b/Makefile @@ -23,9 +23,9 @@ LWIP_C_FILES += $(NETIFFILES) LWIP_C_FILES += $(HTTPFILES) LWIP_C_FILES_WITH_PATH := $(LWIP_C_FILES) -LWIP_PORT_FILES := $(wildcard $(PORT_DIR)/*.c) +LWIP_PORT_FILES := $(wildcard $(PORT_DIR)/*.c $(PORT_DIR)/arch/*.c) -INCLUDE_DIRS ?= \ +INCLUDE_DIRS += \ -I./inc \ -I$(LWIP_DIR)/src/include \ -I$(PORT_DIR) diff --git a/README.md b/README.md index 8025023..b96fdcd 100644 --- a/README.md +++ b/README.md @@ -28,20 +28,6 @@ while (1) { } ``` -seems okayish - -```sh -$ wrk -t12 -c500 -d10s http://192.168.102.119 -Running 10s test @ http://192.168.102.119 - 12 threads and 500 connections - Thread Stats Avg Stdev Max +/- Stdev - Latency 1.87ms 6.98ms 613.62ms 99.63% - Req/Sec 334.20 201.29 0.88k 74.58% - 8197 requests in 10.10s, 5.30MB read -Requests/sec: 811.63 -Transfer/sec: 537.39KB -``` - ## Impl note This driver is kinda functional but not optimized diff --git a/port/arch/cc.h b/port/arch/cc.h index 5b99363..b87f8c3 100644 --- a/port/arch/cc.h +++ b/port/arch/cc.h @@ -32,4 +32,6 @@ #define LWIP_RAND() ((u32_t)rand()) +#include "arch/sys_arch.h" + #endif /* LWIP_ARCH_CC_H */ \ No newline at end of file diff --git a/port/sys_arch.c b/port/arch/sys_arch.c similarity index 61% rename from port/sys_arch.c rename to port/arch/sys_arch.c index 090b440..feda3ba 100644 --- a/port/sys_arch.c +++ b/port/arch/sys_arch.c @@ -1,8 +1,9 @@ +#include "sys_arch.h" + #include "ch32fun.h" -#include "lwip/def.h" +#include "lwip/sys.h" #include "systick.h" -typedef uint32_t sys_prot_t; static unsigned long next = 1; int rand(void) { @@ -15,11 +16,11 @@ void srand(unsigned int seed) { next = seed; } uint32_t sys_now(void) { return millis(); } sys_prot_t sys_arch_protect(void) { - unsigned int old_mstatus; - __asm__ volatile("csrrci %0, mstatus, 8" : "=r"(old_mstatus)); - return old_mstatus; + __disable_irq(); + return 1; } void sys_arch_unprotect(sys_prot_t pval) { - __asm__ volatile("csrw mstatus, %0" : : "r"(pval)); -} + (void)pval; + __enable_irq(); +} \ No newline at end of file diff --git a/port/arch/sys_arch.h b/port/arch/sys_arch.h new file mode 100644 index 0000000..765c7ed --- /dev/null +++ b/port/arch/sys_arch.h @@ -0,0 +1,8 @@ +#ifndef LWIP_ARCH_SYS_ARCH_H +#define LWIP_ARCH_SYS_ARCH_H + +#include + +typedef uint32_t sys_prot_t; + +#endif /* LWIP_ARCH_SYS_ARCH_H */ \ No newline at end of file diff --git a/port/ethernetif.c b/port/ethernetif.c index 3b9a310..7e7fe6c 100644 --- a/port/ethernetif.c +++ b/port/ethernetif.c @@ -13,7 +13,7 @@ #define IFNAME0 'e' #define IFNAME1 'n' -#define ETH_RX_BUF_COUNT 2 +#define ETH_RX_BUF_COUNT 4 #define ETH_TX_BUF_COUNT 2 /* buf size should be at least ETH_MAX_PACKET_SIZE */ #define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE @@ -58,8 +58,8 @@ static inline void tx_queue_produce(tx_queue_t* q) { } } static inline void tx_queue_consume(tx_queue_t* q) { - q->is_full = false; q->tail = (q->tail + 1) % ETH_TX_BUF_COUNT; + q->is_full = false; } static void low_level_init(struct netif* netif); @@ -200,11 +200,11 @@ static err_t low_level_output(struct netif* netif, struct pbuf* p) { struct ethernetif* ethernetif = netif->state; err_t errval = ERR_OK; - NVIC_DisableIRQ(ETH_IRQn); - if (tx_queue_is_full(ðernetif->tx_q)) { + // should this be ERR_BUF or ERR_MEM? does ERR_MEM re-queue the packet? // queue full, drop pkt errval = ERR_BUF; + // errval = ERR_MEM; } else { uint32_t current_idx = ethernetif->tx_q.head; uint8_t* tx_buf_ptr = (uint8_t*)g_dma_tx_descs[current_idx].Buffer1Addr; @@ -218,19 +218,10 @@ static err_t low_level_output(struct netif* netif, struct pbuf* p) { g_dma_tx_descs[current_idx].Status = len; tx_queue_produce(ðernetif->tx_q); - - LINK_STATS_INC(link.xmit); - MIB2_STATS_NETIF_ADD(netif, ifoutoctets, len); } tx_start_if_possible(); - NVIC_EnableIRQ(ETH_IRQn); - - if (errval == ERR_BUF) { - LINK_STATS_INC(link.drop); - } - return errval; } @@ -332,8 +323,10 @@ void ETH_IRQHandler(void) { ETH10M->EIR = RB_ETH_EIR_TXIF; if (!tx_queue_is_empty(ðernetif->tx_q)) { + LINK_STATS_INC(link.xmit); tx_queue_consume(ðernetif->tx_q); } + tx_start_if_possible(); } diff --git a/port/lwipopts.h b/port/lwipopts.h index b1ff645..e07648b 100644 --- a/port/lwipopts.h +++ b/port/lwipopts.h @@ -11,7 +11,7 @@ // #define ETHARP_DEBUG LWIP_DBG_ON #define NO_SYS 1 -#define SYS_LIGHTWEIGHT_PROT 0 +#define SYS_LIGHTWEIGHT_PROT 1 #define LWIP_NETCONN 0 #define LWIP_SOCKET 0 @@ -20,20 +20,20 @@ #define MEM_SIZE (4 * 1024) // Pbuf options -#define PBUF_POOL_SIZE 4 -#define PBUF_POOL_BUFSIZE 1524 +#define PBUF_POOL_SIZE 6 +#define PBUF_POOL_BUFSIZE 590 -#define MEMP_NUM_PBUF 8 // default 16 +#define MEMP_NUM_PBUF 6 // default 16 // TCP options #define LWIP_TCP 1 #define TCP_MSS 536 #define TCP_SND_BUF (2 * TCP_MSS) -#define TCP_WND (2 * TCP_MSS) +#define TCP_WND (4 * TCP_MSS) #define TCP_QUEUE_OOSEQ 0 #define LWIP_UDP 1 -#define MEMP_NUM_UDP_PCB 2 // # of concurrent UDP "connections" +#define MEMP_NUM_UDP_PCB 3 // # of concurrent UDP "connections" #define LWIP_ICMP 1 #define LWIP_DHCP 1 @@ -54,7 +54,7 @@ #define CHECKSUM_CHECK_TCP 1 #define LWIP_CHECKSUM_ON_COPY 0 -#define LWIP_STATS 1 -#define LINK_STATS 1 +#define LWIP_STATS 0 +#define LINK_STATS 0 #endif /* __LWIPOPTS_H__ */