feat(net/udp): UDP 数据面并入 netMsgQueue 串行执行(#73)
前置来自 #57 SNTP 调研:旧 net_sendto / net_recvfrom 由调用方直调 SPI 且无限自旋收包,既违约「netTask 是芯片唯一访问者」,又把 IWDG 预算吃在自旋里。 改造面 - 新增 Drivers/BSP/NET/net_udp.c/.h(真机专属,模拟器不编译): bind/set_peer/sendto/recvfrom 四条执行体 + 挂起收包表 + tick; net_msg_type_t 加四型,公开 API 一律「入队 → 睡在通知上」。 - 源地址出参不搭 net_msg_t(按值拷贝,AGENTS 禁加回传字段):改住 net_udp.c 的 per-fd 槽,netTask 就地写回调用方内存,通知值只带 结果码;每条出口都清槽,泄漏 = 该 fd 永久 BUSY。 - 发送复用 net_send_locked,不写第二份分块逻辑;「会话是否已死」 收成一条判据 net_send_state_down()(TCP 看 ESTABLISHED、UDP 看 UDP_OPEN),原先 4 处各写一遍。 - 顺带修掉真缺陷(新条 TRAP-NET-22):旧 sendto 把 DesIP 写成具体 IP,芯片静默切 UDP Client,而旧 recvfrom 无条件剥 8 字节 → SNTP 这类 48 B 应答丢载荷前 8 字节并交出垃圾源地址。加 udp_mode(塞进 原有 padding,sizeof 不变)、bind 恒 SERVER、显式对端 sendto 切 CLIENT、set_peer(fd, NULL) 轻量回 SERVER,仅 SERVER 且 avail >= 8 才剥头。 - 截断定案:一次调用交付一个数据报,buf 装不下的尾巴用 64 B 栈上 scratch 排空(不用 clear_recv_buf——它连后面已到达的包一起丢)。 - 取消点接齐:net_init 重建 cancel_all、net_socket 分配 fd clear、 net_close settle_close + clear、重 bind 前先结算等包者, net_process_messages 第 5 个 tick 位挂 net_udp_tick。 - 收紧两处契约(原实现无生产调用者):UDP 未 bind 一律 -5; net_recv / _nb / _to 拒 DGRAM(绕过剥头就是静默吃 8 字节); NET_ERR_NOT_IN_NETTASK(-12) 退役为历史常量并写明原因。 验证 - 主机单测 test/host/net_socket 全套 616/616 绿(改造前 470):新 分片 ns_s7_udp.c 20 组,改写 440 / 555 / 560 组;覆盖率 net_udp.c 234/236(99.2%)、net_socket.c 956/969(98.7%), scripts/test_coverage_baseline.json 随本提交登记新文件。 - 测试抓到 4 个被测码真 bug:自等守卫误用调用侧谓词、芯片长度被读 两次、槽指针取在 fd 合法性检查之前、重 bind 静默作废等待者。 - Keil ARMCC 0 Error(s) 0 Warning(s);net_udp.o Code 1,358 / ZI 224 (8 fd × 28 B,其余模块零增长),全仓 RW_IRAM1 72.2%;内存台账 受管区按门禁提示用 --write / --update 机器回填,未手改数字。 - pre-commit 全量钩子(含 doc-links、coding-standard、mem-budget、 generated-fresh、bsp-err、registration、nested-comment)全绿。 限度 - 本轮无板子:真机回归 5 条(两端互通、set_peer(NULL) 能否恢复收 广播、截断排空后下一包完整性、挂起 recv 期间 IWDG 余量、bind 写 DesIP 与 open 先后的真芯片效果)已写进覆盖率口径「台架必验」。 - scripts/stack_report.py 退出码 1 是 10-26 起挂在账上的 iec104Task 栈债(margin 1,016 < 1,024,见 docs/代码规范/pre-commit使用说明.md: 122);本轮只让 net_bind 的 UDP 分支多一层静态帧(1,544 → 1,600), netTask margin 1,888 与 timeSyncTask 1,152 不受影响。抬栈要过 mem-budget 台账,不在本分支自行处理。 - 登记簿销项按仓库约定留给主工作树,本分支不动 docs/任务规划/**。
This commit is contained in:
@@ -179,37 +179,51 @@ static void tc_505_inner_wait_and_last_chunk(void)
|
||||
reset_all();
|
||||
}
|
||||
|
||||
/* TC-NETSOCK-506 UDP sendto:等待腾出与短写留痕(与 TCP 同一套语义的另一份实现) */
|
||||
/* TC-NETSOCK-506 UDP 发送卡住:两条出口都要留痕(2026-11-03 队列化改造后重写)
|
||||
* 改造前这里钉的是"netTask 内自旋到 1s 停滞超时后短写"——那 1 秒全部花在 netTask
|
||||
* 里,正是本次要消掉的形态。改造后 UDP 与 TCP 共用 net_send_locked 的预算机:
|
||||
* ① 执行任务上下文:无人能完成挂起 → 拒绝自等 → -1(BUSY),且不写忙缓冲;
|
||||
* ② 调用方上下文:超预算转挂起(登记进 net_send_pending,与 TCP 同一张表),
|
||||
* 登记里带的是**已写进芯片的字节数**——续传从断点起,已交付的字节绝不重发
|
||||
* (重发半截数据报 = 对端收到两个包,比丢包更难查)。
|
||||
*/
|
||||
static void tc_506_sendto_stall_and_short(void)
|
||||
{
|
||||
static char big[2100];
|
||||
int fd;
|
||||
int rc;
|
||||
|
||||
/* ① 执行任务上下文、发送缓冲一开始就不空闲 */
|
||||
bring_up_netctx();
|
||||
fd = net_socket(NET_AF_INET, NET_SOCK_DGRAM);
|
||||
fd = udp_bind_ctx(8888);
|
||||
s_net_socks[fd].send_ready = 0U;
|
||||
g_ns_int_pin_set = 0;
|
||||
ch395f_fake_push_glob((uint16_t)(1U << (fd + 4)));
|
||||
/* 每轮只走 1ms:先命中 NET_SEND_TIMEOUT_MS(1s) 之前不会有别出口 → 短写 */
|
||||
/* 每轮 500ms:停在自旋预算(2ms)与停滞超时(1000ms)之间 → 走"转挂起被拒"这条口 */
|
||||
g_ns_poll_tick_inc = 500U;
|
||||
rc = net_sendto(fd, big, 2100, 0, NULL, 0);
|
||||
CHECK(rc < 2100 && rc >= 0,
|
||||
"TC-NETSOCK-506a sendto stalled returns only what went out (short-write semantics match TCP)");
|
||||
CHECK(g_ns_error_count >= 2,
|
||||
"TC-NETSOCK-506b both the stall [ERR] and the short-write [ERR] must be there (missing one degrades into a silent truncation)");
|
||||
CHECK(rc == -1 && net_get_errno() == NET_ERR_BUSY,
|
||||
"TC-NETSOCK-506a a stall inside netTask now gives up with BUSY instead of spinning a full second and short-writing");
|
||||
CHECK(g_chip.write_calls == 0,
|
||||
"TC-NETSOCK-506b it never wrote into a busy buffer (no half datagram left in the chip FIFO)");
|
||||
CHECK(g_peers.send_reg_calls == 1,
|
||||
"TC-NETSOCK-506c the hand-over was really attempted (the refusal comes from the register side, not from us not trying to hand over)");
|
||||
reset_all();
|
||||
|
||||
/* 写完一块后等发完的超时出口 */
|
||||
bring_up_netctx();
|
||||
fd = net_socket(NET_AF_INET, NET_SOCK_DGRAM);
|
||||
/* ② 调用方上下文:第一块写得进、之后芯片不再报空闲 → 第二块转挂起 */
|
||||
bring_up();
|
||||
fd = udp_bind_ctx(8888);
|
||||
g_ns_int_pin_set = 0;
|
||||
ch395f_fake_push_glob((uint16_t)(1U << (fd + 4)));
|
||||
g_fake.int_status[fd] = CH395F_SINT_STAT_SENBUF_FREE; /* 只给一次 */
|
||||
g_fake.int_status[fd] = CH395F_SINT_STAT_SENBUF_FREE; /* 只给一次空闲 */
|
||||
g_ns_poll_tick_inc = 500U;
|
||||
rc = net_sendto(fd, big, 2100, 0, NULL, 0);
|
||||
CHECK(rc >= 1024 && rc < 2100,
|
||||
"TC-NETSOCK-506c first chunk delivered and the second chunk wait timed out -> a value between 1024 and 2099");
|
||||
rc = net_send_locked(&s_net_socks[fd], big, 2100, (TaskHandle_t)&g_ns_task_caller);
|
||||
CHECK(rc == NET_PENDING_REGISTERED,
|
||||
"TC-NETSOCK-506d a UDP send past the spin budget converts to pending exactly like TCP (no in-place busy wait)");
|
||||
CHECK(g_peers.send_reg_calls == 1 && g_peers.last_snd_sent == 1024,
|
||||
"TC-NETSOCK-506e the pending entry carries the bytes already written (1024), so the resume starts at the break point");
|
||||
CHECK(g_chip.write_calls == 1,
|
||||
"TC-NETSOCK-506f exactly one chunk reached the chip before handing over");
|
||||
reset_all();
|
||||
}
|
||||
|
||||
@@ -447,7 +461,10 @@ static void tc_555_udp_src_addr(void)
|
||||
int fd;
|
||||
|
||||
bring_up_netctx();
|
||||
fd = net_socket(NET_AF_INET, NET_SOCK_DGRAM);
|
||||
/* 2026-11-03 队列化改造:剥头只发生在**服务器模式**,而服务器模式由 bind 建立
|
||||
* (DesIP=0xFFFFFFFF)。改造前这段直接 net_socket 就能收,是因为它无条件剥头——
|
||||
* 那正是本组要修的缺陷(客户端模式没有头,照剥就吃掉载荷前 8 字节)。 */
|
||||
fd = udp_bind_ctx(8888);
|
||||
ch395f_fake_push_recv_len((uint8_t)fd, (uint16_t)(8U + 3U));
|
||||
ch395f_fake_set_recv_bytes(hdr, (uint16_t)sizeof(hdr));
|
||||
|
||||
@@ -467,91 +484,84 @@ static void tc_555_udp_src_addr(void)
|
||||
}
|
||||
|
||||
|
||||
/* TC-NETSOCK-560 UDP 上下文守卫:非执行任务不得直调芯片
|
||||
* 三条一起才成立,缺一条都不算把危险面关掉:
|
||||
* ① 执行任务内照常成功(守卫不是"把 UDP 判死")
|
||||
* ② 非执行任务被拒且**一次芯片事务都没有**(拒了却已经抢过 SPI = 白拒)
|
||||
* ③ 正常错误路径不被守卫吞掉,且**走消息队列的接口完全不受影响**
|
||||
* (那些接口生来就是给非 netTask 用的,误伤它们才是大事故)
|
||||
/* TC-NETSOCK-560 UDP 上下文契约(2026-11-03 队列化改造**反转**了本组的原命题)
|
||||
* 改造前:非执行任务调用 net_sendto / net_recvfrom 一律 NET_ERR_NOT_IN_NETTASK(-12)
|
||||
* ——那是 2026-10-24"先把危险面关掉"的临时守卫(SPI 竞争 TRAP-NET-14)。
|
||||
* 改造后:任意任务都能调,芯片事务全部串行到 netTask 执行;守卫随之退役。
|
||||
* 三条一起才成立,缺一条都不算把改造钉住:
|
||||
* ① 非执行任务调用**成功**,且芯片事务发生在泵(=netTask 上下文)里,不在调用方
|
||||
* ② 执行任务内直接执行,不投自己(板级用例照旧可在自己任务里裸调)
|
||||
* ③ 参数/状态错误优先于上下文;-12 这个码在 UDP 路径上不再出现
|
||||
*/
|
||||
static void tc_560_udp_context_guard(void)
|
||||
{
|
||||
char buf[8];
|
||||
int fd;
|
||||
|
||||
/* ① 执行任务上下文:与修复前逐字相同 */
|
||||
bring_up_netctx();
|
||||
fd = net_socket(NET_AF_INET, NET_SOCK_DGRAM);
|
||||
CHECK(net_sendto(fd, "abc", 3, 0, NULL, 0) == 3,
|
||||
"TC-NETSOCK-560a net_sendto unchanged inside the executing task");
|
||||
reset_all();
|
||||
|
||||
/* ② 非执行任务:拒绝 + 零芯片事务(bring_up 里 net_init 是以 netTask 身份跑的,
|
||||
* 此刻 g_ns_cur_task 已还原成 caller,正是"别的任务") */
|
||||
/* ① 非执行任务(bring_up 已把身份还原成 caller):改造前被拒,改造后必须成 */
|
||||
bring_up();
|
||||
fd = net_socket(NET_AF_INET, NET_SOCK_DGRAM);
|
||||
fd = udp_bind_ctx(8888);
|
||||
CHECK(fd >= 0, "UDP-560a0 bind from a caller task succeeds (queue path, no direct SPI)");
|
||||
g_chip.write_calls = 0;
|
||||
g_chip.read_calls = 0;
|
||||
g_fxchip.set_des_ip_calls = 0;
|
||||
g_fxchip.glob_calls = 0;
|
||||
g_ns_poll_calls = 0;
|
||||
g_ns_error_count = 0;
|
||||
g_ns_put_calls = 0;
|
||||
g_ns_notify_calls = 0;
|
||||
CHECK(net_sendto(fd, "abc", 3, 0, NULL, 0) == 3,
|
||||
"UDP-560a non-executing task's sendto now succeeds (it used to be refused with -12)");
|
||||
CHECK(g_chip.write_calls == 1 && g_ns_put_calls >= 1 && g_ns_notify_calls >= 1,
|
||||
"UDP-560b the write happened while the pump ran as netTask: the caller enqueued, blocked, and was notified (serialised, not forbidden)");
|
||||
CHECK(net_get_errno() != NET_ERR_NOT_IN_NETTASK,
|
||||
"UDP-560c the retired guard does not come back: -12 is never returned on a success path");
|
||||
|
||||
CHECK(net_sendto(fd, "abc", 3, 0, NULL, 0) == -1 &&
|
||||
net_get_errno() == NET_ERR_NOT_IN_NETTASK,
|
||||
"TC-NETSOCK-560b non-executing task gets NET_ERR_NOT_IN_NETTASK (exact code, not -1)");
|
||||
CHECK(g_chip.write_calls == 0 && g_fxchip.set_des_ip_calls == 0 &&
|
||||
g_ns_poll_calls == 0,
|
||||
"TC-NETSOCK-560c clean refusal: zero writes, zero set_des_ip, zero net_poll rounds");
|
||||
CHECK(net_recvfrom(fd, buf, (int)sizeof(buf), NET_MSG_DONTWAIT, NULL, NULL) == -1 &&
|
||||
net_get_errno() == NET_ERR_NOT_IN_NETTASK,
|
||||
"TC-NETSOCK-560d net_recvfrom guarded the same way");
|
||||
CHECK(g_chip.read_calls == 0 && g_fxchip.glob_calls == 0 && g_ns_poll_calls == 0,
|
||||
"TC-NETSOCK-560e recvfrom touched no chip and no global interrupt register either");
|
||||
CHECK(g_ns_error_count == 2,
|
||||
"TC-NETSOCK-560f each refusal logs one ERR line (silence hides the wrong task)");
|
||||
ch395f_fake_push_recv_len((uint8_t)fd, 8U + 3U);
|
||||
s_net_socks[fd].readable = 1U;
|
||||
CHECK(net_recvfrom(fd, buf, (int)sizeof(buf), NET_MSG_DONTWAIT, NULL, NULL) == 3,
|
||||
"UDP-560d non-executing task's recvfrom goes the same route and returns the payload");
|
||||
CHECK(g_chip.read_calls >= 2 && g_ns_block_waits >= 1,
|
||||
"UDP-560e the caller slept on its own notification while netTask did the SPI reads (the old spin-in-caller shape is gone)");
|
||||
|
||||
/* ③ 正常错误路径优先:守卫排在参数校验之后 */
|
||||
/* ③ 参数检查排在上下文与状态之前(守卫退役不许顺手把错误优先级改掉) */
|
||||
CHECK(net_sendto(-1, "abc", 3, 0, NULL, 0) == -1 &&
|
||||
net_get_errno() == NET_ERR_BADF, "TC-NETSOCK-560g out-of-range fd still reports BADF first");
|
||||
net_get_errno() == NET_ERR_BADF, "UDP-560f out-of-range fd still reports BADF first");
|
||||
CHECK(net_sendto(fd, NULL, 3, 0, NULL, 0) == -1 &&
|
||||
net_get_errno() == NET_ERR_INVAL,
|
||||
"TC-NETSOCK-560h NULL buf still reports INVAL, the guard does not swallow it");
|
||||
net_get_errno() == NET_ERR_INVAL, "UDP-560g NULL buf still reports INVAL");
|
||||
CHECK(net_recvfrom(fd, NULL, 3, NET_MSG_DONTWAIT, NULL, NULL) == -1 &&
|
||||
net_get_errno() == NET_ERR_INVAL, "TC-NETSOCK-560i recvfrom keeps the same precedence");
|
||||
|
||||
/* ④ 走消息队列的接口不受守卫影响:同一个非 netTask 上下文照常能把活干完 */
|
||||
CHECK(net_close(fd) == 0,
|
||||
"TC-NETSOCK-560j net_close from a non-executing task still works (queue path untouched)");
|
||||
/* 执行完的证据用"收到回执 + 槽位释放",不看 close_calls:芯片此刻已是
|
||||
* CLOSED,走的是快路径(不再补发 CLOSE,见 TC-NETSOCK-320a/b),拿它当证据
|
||||
* 会把一条正确的快路径判成失败 */
|
||||
CHECK(s_net_socks[fd].in_use == 0U && g_ns_notify_calls >= 1,
|
||||
"TC-NETSOCK-560k queue path really completed (receipt seen and slot released)");
|
||||
net_get_errno() == NET_ERR_INVAL, "UDP-560h recvfrom keeps the same precedence");
|
||||
reset_all();
|
||||
|
||||
/* ⑤ 未捕获执行任务(net_init 没跑):守卫不抢先,仍由参数/状态检查给确定错误 */
|
||||
/* ② 执行任务上下文:直接执行,不投自己(板级 testTask 形态) */
|
||||
bring_up_netctx();
|
||||
fd = udp_bind_ctx(8888);
|
||||
g_ns_put_calls = 0;
|
||||
CHECK(net_sendto(fd, "abc", 3, 0, NULL, 0) == 3,
|
||||
"UDP-560i netTask context sends directly");
|
||||
CHECK(g_ns_put_calls == 0,
|
||||
"UDP-560j the executing task never enqueues to itself (a self-post would have to wait for its own pump)");
|
||||
reset_all();
|
||||
g_ns_pump = net_process_messages;
|
||||
CHECK(net_sendto(0, "abc", 3, 0, NULL, 0) == -1 &&
|
||||
net_get_errno() == NET_ERR_BADF,
|
||||
"TC-NETSOCK-560l before net_init sendto reports BADF, no invented context error");
|
||||
|
||||
/* 未捕获执行任务、但 socket 确实在用时,守卫**选择放行**:宁可不做判定,也不给
|
||||
* 一个无从判定的场景编造错误码。放行后走的是完全正常的执行路径(这里 DONTWAIT
|
||||
* 无数据 → WOULDBLOCK),证明守卫不是"没初始化就一律拒绝"的粗暴实现。
|
||||
* 刻意用 recvfrom 而不是 sendto:未初始化时 net_poll 直接短路返回、时间基准
|
||||
* 不前进,sendto 的等待环永远等不到超时(真机上不可达:net_socket 之后还有
|
||||
* NET_READY 屏障,但这条差异值得记一笔,见交付报告)。 */
|
||||
/* ④ 队列未绑定(net_init 之前的早期上下文):与其他队列化接口同一口径 NET_ERR,
|
||||
* 不是 BADF 也不是 -12——无从投递就是无从投递 */
|
||||
reset_all();
|
||||
g_ns_cur_task = (TaskHandle_t)&g_ns_task_caller; /* net_init 没跑:判据恒 NULL */
|
||||
g_ns_cur_task = (TaskHandle_t)&g_ns_task_caller;
|
||||
fd = net_socket(NET_AF_INET, NET_SOCK_DGRAM);
|
||||
g_fxchip.glob_calls = 0;
|
||||
CHECK(net_sendto(fd, "abc", 3, 0, NULL, 0) == NET_ERR &&
|
||||
s_net_udp[fd].armed == 0U,
|
||||
"UDP-560k before net_init (no queue bound) sendto returns NET_ERR and leaves no slot armed");
|
||||
CHECK(net_recvfrom(fd, buf, (int)sizeof(buf), NET_MSG_DONTWAIT, NULL, NULL) == NET_ERR,
|
||||
"UDP-560l the same for recvfrom (the armed out-param slot is handed back on that exit)");
|
||||
reset_all();
|
||||
|
||||
/* ⑤ 执行任务内的**阻塞** UDP 收包仍被拒(改造没有打开自等死锁的门):
|
||||
* 完成它的 tick 就跑在同一个任务里 */
|
||||
bring_up_netctx();
|
||||
fd = udp_bind_ctx(8888);
|
||||
CHECK(net_recvfrom(fd, buf, (int)sizeof(buf), 0, NULL, NULL) == -1 &&
|
||||
net_get_errno() == NET_ERR_BUSY,
|
||||
"UDP-560m a blocking UDP receive inside netTask is refused with BUSY (self-wait = deadlock)");
|
||||
CHECK(net_recvfrom(fd, buf, (int)sizeof(buf), NET_MSG_DONTWAIT, NULL, NULL) == -1 &&
|
||||
net_get_errno() == NET_ERR_WOULDBLOCK,
|
||||
"TC-NETSOCK-560m guard stays open when the executing task was never captured");
|
||||
CHECK(net_get_errno() != NET_ERR_NOT_IN_NETTASK && g_fxchip.glob_calls == 0,
|
||||
"TC-NETSOCK-560n no context error invented and no global register read");
|
||||
"UDP-560n the same call with NET_MSG_DONTWAIT is the legal netTask form (WOULDBLOCK, no wait)");
|
||||
reset_all();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user