Linux tcp_keepalive 保活定时器 keepalive_timer 实现 Linux tcp_keepalive 保活定时器 keepalive_timer 实现tcp_keepalive_timer 是 TCP 保活探测的核心定时器回调函数注册在 inet_connection_sock 的 icsk_delack_timer 定时器基座上但使用 ICSK_TIME_KEEPALIVE 作为定时器类型区分。该定时器在连接建立后启动tcp_init_transfer 中调用 tcp_keepalive_timer 仅为首次 reset但真正的保活探测序列在无数据交换的静默期后触发。cstatic void tcp_keepalive_timer(struct timer_list *t){struct sock *sk from_timer(sk, t, sk_timer);struct tcp_sock *tp tcp_sk(sk);u32 elapsed;bh_lock_sock(sk);if (sock_owned_by_user(sk)) {if (__timer_pending(sk-sk_timer))goto out;sk_reset_timer(sk, sk-sk_timer, jiffies HZ / 20);goto out;}if (sk-sk_state TCP_CLOSE || sk-sk_state TCP_LISTEN)goto out;elapsed keepalive_time_elapsed(tp);if (elapsed keepalive_time keepalive_intvl * tp-keepalive_probes) {tcp_send_active_reset(sk, GFP_ATOMIC);tcp_write_err(sk);goto out;}if (elapsed keepalive_time)goto resched;if (!tp-packets_out !tcp_write_queue_empty(sk))tcp_push_one(sk, mss_now, TCP_NAGLE_OFF);tcp_send_probe0(sk);resched:sk_reset_timer(sk, sk-sk_timer, keepalive_time_when(tp));out:bh_unlock_sock(sk);sock_put(sk);}函数入口使用 bh_lock_sock 而非 lock_sock因为定时器运行在 TIMER_SOFTIRQ 上下文。若用户进程已持有 lock_socksock_owned_by_user 为 true定时器不阻塞等待而是短延时HZ/20 50ms后重新调度。这种 defer 策略避免了优先级反转——用户进程可能在用户态长时间阻塞如 fgets 等待输入而保活探测仍需进行。保活探测的阈值链keepalive_time默认 7200 秒定义了空闲时长超过该值后发送第一个保活探测报文空 ACK 段序列号为 snd_una-1 以触发对端 ACK。每个探测的间隔由 keepalive_intvl默认 75 秒控制。在连续发送 keepalive_probes默认 9 次失败后定时器调用 tcp_send_active_reset 发送 RST 断开连接并调用 tcp_write_err 通知上层 socket 错误sk-sk_err ETIMEDOUT。cvoid tcp_send_probe0(struct sock *sk){struct tcp_sock *tp tcp_sk(sk);struct sk_buff *skb tcp_write_queue_tail(sk);int err;if (skb !tcp_skb_is_write_queue(sk, skb))skb NULL;if (skb) {if (TCP_SKB_CB(skb)-end_seq ! tp-snd_nxt)tp-snd_nxt TCP_SKB_CB(skb)-end_seq;err tcp_write_xmit(sk, tcp_current_mss(sk), 1, 0, GFP_ATOMIC);} else {tcp_v4_send_ack(sk, tp-snd_una - 1, tp-rcv_nxt,tp-rcv_wnd tp-rcv_wscale,tcp_time_stamp_raw(), tp-rx_opt.ts_recent,0, 0, FLAG_ACK_NO_DELAY);}}tcp_send_probe0 负责将保活探测报文实际发送到 IP 层。有两种情况如果 write_queue 非空即有未确认数据则调用 tcp_write_xmit 发送尾部 skb 的剩余数据如果 write_queue 为空直接调用 tcp_v4_send_ack 构造一个 seq snd_una-1 的 ACK 包。对端收到 seq snd_una-1 的 ACK 后若连接尚存则回复 ACK序列号为 rcv_nxtsender 收到该 ACK 后重置保活定时器。tcp_keepalive_timer 由 sk_reset_timer 在多个路径被重置- tcp_rcv_established 收到数据包时重置- tcp_ack 累积确认前进时重置- 用户进程调用 send/recv 导致数据交互时在 __tcp_push_pending_frames 中重置。核心的竞争条件出现在连接关闭路径。当用户进程调用 close() 触发 tcp_close该函数调用 tcp_set_state(sk, TCP_CLOSE) 后tcp_keepalive_timer 可能已经在 TIMER_SOFTIRQ 队列中 pending。tcp_close 中调用 sk_stop_timer 移除定时器但如果定时器已经触发并在 bh_lock_sock 上自旋则退出时 sk_state 已被改为 TCP_CLOSE定时器检查到 TCP_CLOSE 后直接 goto out不会发送探测报文。另一种情况是 tcp_write_err在保活探测超限后调用 tcp_done 关闭 sockettcp_done 内部将 sk_state 置为 TCP_CLOSE 并调用 sk_stop_timer 停止所有定时器。保活探测对 TCP_NODELAY 的影响若设置了 TCP_NODELAY保活探测的 tcp_push_one 不受 Nagle 约束立刻发送。默认情况下TCP_CORK 或 Nagle 算法tcp_push_one 可能延迟探测报文发送但 keepalive 定时器的逻辑直接调用 tcp_v4_send_ack 会绕过 Nagle 检查。cstatic u32 keepalive_time_elapsed(struct tcp_sock *tp){const struct tcp_sock *tp tcp_sk(sk);s32 delta;delta tp-rcv_tstamp - tp-lsndtime;if (delta 0)delta 0;return delta;}keepalive_time_elapsed 使用 tp-rcv_tstamp最后收到数据的时间戳和 tp-lsndtime最后发送数据的时间戳中的较新者减去当前时间计算空闲时长。rcv_tstamp 在 tcp_replace_ts_recent 中被更新而 lsndtime 在 tcp_transmit_skb 结束时被 tp-lsndtime tcp_jiffies32 更新。这里的一个角落问题若 tcp_v4_rcv 中调用 tcp_rcv_established 时没有数据如 pure ACKrcv_tstamp 不会被更新可能导致保活定时器未按预期重置。但 tcp_rcv_established 在收到 ACK 不带数据时会通过 tcp_data_queue 的 __kfree_skb 路径而在 tcp_replace_ts_recent 中只要时间戳选项非空rcv_tstamp 依然会更新。keepalive_intvl 的 sysctl 修改不影响已经在运行的保活探测序列定时器一旦启动其间隔由 tp-keepalive_intvl 快照决定该值在 tcp_init_transfer 时从 net-ipv4.sysctl_tcp_keepalive_intvl 复制。若需要立即生效必须重启连接或通过 setsockopt TCP_KEEPINTVL 覆盖 socket 级别的设置。

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/9/30 14:41:37
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/9/29 2:52:51
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/9/30 19:41:56
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/9/30 18:23:43
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/29 22:57:57
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/9/29 2:52:53

日新闻

周新闻