基于 eBPF 的网络可观测:协议栈路径与 sk_buff

基于 eBPF 的网络可观测:协议栈路径与 sk_buff

网络可观测要回答的是:包从网卡到套接字(或反向)经过哪些阶段、在哪变慢、在哪被丢。eBPF 可以挂在 XDP、TC、tracepoint、kprobe 等位置,对同一条流做分段计时与丢包归因。本文从 内核收发包路径sk_buff 数据布局 入手,说明观测该怎么选挂点------不依赖其他篇章即可用于设计网络探针。


目录

  1. 网络可观测要看见什么
  2. [发包路径(TCP 为例)](#发包路径(TCP 为例))
  3. 收包路径对照
  4. sk_buff:线性区与非线性区
  5. [eBPF 常见挂点与能回答的问题](#eBPF 常见挂点与能回答的问题)
  6. 可观测指标怎么设计
  7. 丢包与延迟的归因思路
  8. 实践注意
  9. 系列结语:从使用工具到设计方案

网络可观测要看见什么

相对「机器 CPU/内存」,网络向可观测更关心:

信号 例子
延迟分段 应用到协议栈、协议栈到驱动、驱动到线缆
丢包点 队列满、conntrack、netfilter、套接字接收队列
流量结构 五元组、重传、窗口、异常复位
策略命中 iptables/nft、cgroup、带宽限制

eBPF 的优势是:在 不改业务、少采样偏差 的前提下,把这些点连成时间线。代价是内核版本、程序类型与开销控制。


发包路径(TCP 为例)

用户 send/write 之后,数据大致经历(简化,忽略零拷贝与特殊加速):

text 复制代码
用户缓冲区
  → 套接字发送队列 / TCP 分段与拥塞控制
  → IP 层封装
  → netfilter / 路由决策
  → 邻居与设备队列(qdisc)
  → 网卡驱动 TX
  → 线缆

#mermaid-svg-aSKyeJpdGyR0IRA2{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-aSKyeJpdGyR0IRA2 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-aSKyeJpdGyR0IRA2 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-aSKyeJpdGyR0IRA2 .error-icon{fill:#552222;}#mermaid-svg-aSKyeJpdGyR0IRA2 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-aSKyeJpdGyR0IRA2 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-aSKyeJpdGyR0IRA2 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-aSKyeJpdGyR0IRA2 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-aSKyeJpdGyR0IRA2 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-aSKyeJpdGyR0IRA2 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-aSKyeJpdGyR0IRA2 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-aSKyeJpdGyR0IRA2 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-aSKyeJpdGyR0IRA2 .marker.cross{stroke:#333333;}#mermaid-svg-aSKyeJpdGyR0IRA2 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-aSKyeJpdGyR0IRA2 p{margin:0;}#mermaid-svg-aSKyeJpdGyR0IRA2 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-aSKyeJpdGyR0IRA2 .cluster-label text{fill:#333;}#mermaid-svg-aSKyeJpdGyR0IRA2 .cluster-label span{color:#333;}#mermaid-svg-aSKyeJpdGyR0IRA2 .cluster-label span p{background-color:transparent;}#mermaid-svg-aSKyeJpdGyR0IRA2 .label text,#mermaid-svg-aSKyeJpdGyR0IRA2 span{fill:#333;color:#333;}#mermaid-svg-aSKyeJpdGyR0IRA2 .node rect,#mermaid-svg-aSKyeJpdGyR0IRA2 .node circle,#mermaid-svg-aSKyeJpdGyR0IRA2 .node ellipse,#mermaid-svg-aSKyeJpdGyR0IRA2 .node polygon,#mermaid-svg-aSKyeJpdGyR0IRA2 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-aSKyeJpdGyR0IRA2 .rough-node .label text,#mermaid-svg-aSKyeJpdGyR0IRA2 .node .label text,#mermaid-svg-aSKyeJpdGyR0IRA2 .image-shape .label,#mermaid-svg-aSKyeJpdGyR0IRA2 .icon-shape .label{text-anchor:middle;}#mermaid-svg-aSKyeJpdGyR0IRA2 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-aSKyeJpdGyR0IRA2 .rough-node .label,#mermaid-svg-aSKyeJpdGyR0IRA2 .node .label,#mermaid-svg-aSKyeJpdGyR0IRA2 .image-shape .label,#mermaid-svg-aSKyeJpdGyR0IRA2 .icon-shape .label{text-align:center;}#mermaid-svg-aSKyeJpdGyR0IRA2 .node.clickable{cursor:pointer;}#mermaid-svg-aSKyeJpdGyR0IRA2 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-aSKyeJpdGyR0IRA2 .arrowheadPath{fill:#333333;}#mermaid-svg-aSKyeJpdGyR0IRA2 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-aSKyeJpdGyR0IRA2 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-aSKyeJpdGyR0IRA2 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-aSKyeJpdGyR0IRA2 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-aSKyeJpdGyR0IRA2 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-aSKyeJpdGyR0IRA2 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-aSKyeJpdGyR0IRA2 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-aSKyeJpdGyR0IRA2 .cluster text{fill:#333;}#mermaid-svg-aSKyeJpdGyR0IRA2 .cluster span{color:#333;}#mermaid-svg-aSKyeJpdGyR0IRA2 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-aSKyeJpdGyR0IRA2 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-aSKyeJpdGyR0IRA2 rect.text{fill:none;stroke-width:0;}#mermaid-svg-aSKyeJpdGyR0IRA2 .icon-shape,#mermaid-svg-aSKyeJpdGyR0IRA2 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-aSKyeJpdGyR0IRA2 .icon-shape p,#mermaid-svg-aSKyeJpdGyR0IRA2 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-aSKyeJpdGyR0IRA2 .icon-shape .label rect,#mermaid-svg-aSKyeJpdGyR0IRA2 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-aSKyeJpdGyR0IRA2 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-aSKyeJpdGyR0IRA2 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-aSKyeJpdGyR0IRA2 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 用户态 send
Socket / TCP
IP
Netfilter 等
qdisc
Driver TX
NIC

原书「TCP 发包流程图」强调的是这条 自上而下 的路径。可观测上可对关键函数或 tracepoint 打点,例如:

  • 进入协议栈的时刻
  • 进入 qdisc 的时刻
  • 驱动硬排队 / NAPI 相关事件
  • 重传定时器触发

不同发行版符号名可能变化,优先稳定 tracepoint,动态 kprobe 作补充。


收包路径对照

收包大致反向,但多了硬中断与软中断调度:

text 复制代码
NIC RX
  → 驱动 / NAPI
  →(可选)XDP
  → 协议栈(IP/TCP)
  → netfilter
  → 套接字接收队列
  → 用户态 recv

XDP 出现在协议栈之前:可做丢弃、重定向、统计。若你的探针只挂在 TCP 层,会 看不见 在 XDP 已被丢掉的包------挂点选择直接决定「视野」。
#mermaid-svg-PlGjlSy840nXjsPu{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-PlGjlSy840nXjsPu .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-PlGjlSy840nXjsPu .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-PlGjlSy840nXjsPu .error-icon{fill:#552222;}#mermaid-svg-PlGjlSy840nXjsPu .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-PlGjlSy840nXjsPu .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-PlGjlSy840nXjsPu .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-PlGjlSy840nXjsPu .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-PlGjlSy840nXjsPu .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-PlGjlSy840nXjsPu .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-PlGjlSy840nXjsPu .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-PlGjlSy840nXjsPu .marker{fill:#333333;stroke:#333333;}#mermaid-svg-PlGjlSy840nXjsPu .marker.cross{stroke:#333333;}#mermaid-svg-PlGjlSy840nXjsPu svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-PlGjlSy840nXjsPu p{margin:0;}#mermaid-svg-PlGjlSy840nXjsPu .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-PlGjlSy840nXjsPu .cluster-label text{fill:#333;}#mermaid-svg-PlGjlSy840nXjsPu .cluster-label span{color:#333;}#mermaid-svg-PlGjlSy840nXjsPu .cluster-label span p{background-color:transparent;}#mermaid-svg-PlGjlSy840nXjsPu .label text,#mermaid-svg-PlGjlSy840nXjsPu span{fill:#333;color:#333;}#mermaid-svg-PlGjlSy840nXjsPu .node rect,#mermaid-svg-PlGjlSy840nXjsPu .node circle,#mermaid-svg-PlGjlSy840nXjsPu .node ellipse,#mermaid-svg-PlGjlSy840nXjsPu .node polygon,#mermaid-svg-PlGjlSy840nXjsPu .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-PlGjlSy840nXjsPu .rough-node .label text,#mermaid-svg-PlGjlSy840nXjsPu .node .label text,#mermaid-svg-PlGjlSy840nXjsPu .image-shape .label,#mermaid-svg-PlGjlSy840nXjsPu .icon-shape .label{text-anchor:middle;}#mermaid-svg-PlGjlSy840nXjsPu .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-PlGjlSy840nXjsPu .rough-node .label,#mermaid-svg-PlGjlSy840nXjsPu .node .label,#mermaid-svg-PlGjlSy840nXjsPu .image-shape .label,#mermaid-svg-PlGjlSy840nXjsPu .icon-shape .label{text-align:center;}#mermaid-svg-PlGjlSy840nXjsPu .node.clickable{cursor:pointer;}#mermaid-svg-PlGjlSy840nXjsPu .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-PlGjlSy840nXjsPu .arrowheadPath{fill:#333333;}#mermaid-svg-PlGjlSy840nXjsPu .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-PlGjlSy840nXjsPu .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-PlGjlSy840nXjsPu .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-PlGjlSy840nXjsPu .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-PlGjlSy840nXjsPu .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-PlGjlSy840nXjsPu .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-PlGjlSy840nXjsPu .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-PlGjlSy840nXjsPu .cluster text{fill:#333;}#mermaid-svg-PlGjlSy840nXjsPu .cluster span{color:#333;}#mermaid-svg-PlGjlSy840nXjsPu div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-PlGjlSy840nXjsPu .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-PlGjlSy840nXjsPu rect.text{fill:none;stroke-width:0;}#mermaid-svg-PlGjlSy840nXjsPu .icon-shape,#mermaid-svg-PlGjlSy840nXjsPu .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-PlGjlSy840nXjsPu .icon-shape p,#mermaid-svg-PlGjlSy840nXjsPu .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-PlGjlSy840nXjsPu .icon-shape .label rect,#mermaid-svg-PlGjlSy840nXjsPu .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-PlGjlSy840nXjsPu .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-PlGjlSy840nXjsPu .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-PlGjlSy840nXjsPu :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} PASS
DROP/REDIRECT
NIC RX
Driver / NAPI
XDP?
协议栈
提前结束
Socket
用户态 recv

容器场景:veth 会让路径多走一遍边界

容器网络里,报文常不止经过一块物理网卡。以宿主机路由到容器的典型路径为例(具体顺序会受 CNI、桥接、隧道和策略配置影响):
#mermaid-svg-VgVighwB5co3uqMP{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-VgVighwB5co3uqMP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-VgVighwB5co3uqMP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-VgVighwB5co3uqMP .error-icon{fill:#552222;}#mermaid-svg-VgVighwB5co3uqMP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-VgVighwB5co3uqMP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-VgVighwB5co3uqMP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-VgVighwB5co3uqMP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-VgVighwB5co3uqMP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-VgVighwB5co3uqMP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-VgVighwB5co3uqMP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-VgVighwB5co3uqMP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-VgVighwB5co3uqMP .marker.cross{stroke:#333333;}#mermaid-svg-VgVighwB5co3uqMP svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-VgVighwB5co3uqMP p{margin:0;}#mermaid-svg-VgVighwB5co3uqMP .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-VgVighwB5co3uqMP .cluster-label text{fill:#333;}#mermaid-svg-VgVighwB5co3uqMP .cluster-label span{color:#333;}#mermaid-svg-VgVighwB5co3uqMP .cluster-label span p{background-color:transparent;}#mermaid-svg-VgVighwB5co3uqMP .label text,#mermaid-svg-VgVighwB5co3uqMP span{fill:#333;color:#333;}#mermaid-svg-VgVighwB5co3uqMP .node rect,#mermaid-svg-VgVighwB5co3uqMP .node circle,#mermaid-svg-VgVighwB5co3uqMP .node ellipse,#mermaid-svg-VgVighwB5co3uqMP .node polygon,#mermaid-svg-VgVighwB5co3uqMP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-VgVighwB5co3uqMP .rough-node .label text,#mermaid-svg-VgVighwB5co3uqMP .node .label text,#mermaid-svg-VgVighwB5co3uqMP .image-shape .label,#mermaid-svg-VgVighwB5co3uqMP .icon-shape .label{text-anchor:middle;}#mermaid-svg-VgVighwB5co3uqMP .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-VgVighwB5co3uqMP .rough-node .label,#mermaid-svg-VgVighwB5co3uqMP .node .label,#mermaid-svg-VgVighwB5co3uqMP .image-shape .label,#mermaid-svg-VgVighwB5co3uqMP .icon-shape .label{text-align:center;}#mermaid-svg-VgVighwB5co3uqMP .node.clickable{cursor:pointer;}#mermaid-svg-VgVighwB5co3uqMP .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-VgVighwB5co3uqMP .arrowheadPath{fill:#333333;}#mermaid-svg-VgVighwB5co3uqMP .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-VgVighwB5co3uqMP .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-VgVighwB5co3uqMP .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-VgVighwB5co3uqMP .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-VgVighwB5co3uqMP .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-VgVighwB5co3uqMP .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-VgVighwB5co3uqMP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-VgVighwB5co3uqMP .cluster text{fill:#333;}#mermaid-svg-VgVighwB5co3uqMP .cluster span{color:#333;}#mermaid-svg-VgVighwB5co3uqMP div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-VgVighwB5co3uqMP .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-VgVighwB5co3uqMP rect.text{fill:none;stroke-width:0;}#mermaid-svg-VgVighwB5co3uqMP .icon-shape,#mermaid-svg-VgVighwB5co3uqMP .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-VgVighwB5co3uqMP .icon-shape p,#mermaid-svg-VgVighwB5co3uqMP .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-VgVighwB5co3uqMP .icon-shape .label rect,#mermaid-svg-VgVighwB5co3uqMP .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-VgVighwB5co3uqMP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-VgVighwB5co3uqMP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-VgVighwB5co3uqMP :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 物理网卡 RX
宿主机协议栈 / 路由
veth host 端
veth 容器端 eth0
容器 netns 协议栈
容器 Socket / 应用

veth 像一根虚拟网线:一端发出的包会成为另一端的接收包。因此,宿主机物理网卡、veth host 端和容器 eth0 上的 TC ingress/egress 观察结果并不等价 ;方向还可能与直觉相反。设计探针时至少携带 ifindex + netns,先画清 CNI 实际路径,再决定在哪一端计数,避免把同一包重复统计。


sk_buff:线性区与非线性区

内核网络包的核心描述符是 sk_buff(skb)。对 eBPF/内核代码读写载荷时,必须区分:

区域 含义 访问方式(概念)
线性区 skb 头部连续缓冲区 可通过 skb->data 起的一段连续内存直接读
非线性区 额外页片段(frags) 不能假设都在 data 里;需按 frags/frag_list 描述遍历物理页

直观理解:小包或已线性化的包,头部与部分载荷在线性区;大包、 gro/gso、零拷贝等场景下,载荷常散在多个 page fragment 上。

text 复制代码
sk_buff
├── 线性区:skb->data ... skb->data + len_linear
└── 非线性区:frags[i] → 某 page + offset + size

对可观测与包解析的含义:

  • probe_read 线性区,可能 截断 HTTP 头/载荷
  • 需要完整载荷时,要处理非线性区,或接受「只解析头、统计长度」的折中
  • 部分 helper(如包数据加载类)会帮你按偏移取字节,但仍受程序类型与校验器约束

内核某些路径会调用 skb_linearize(),把非线性分片合并为连续线性区,例如后续模块确实需要连续数据时。但 eBPF 观测不能假设「前面总有人替我拉平」:是否线性化取决于具体路径、特性和内核版本,而且线性化本身有复制成本。探针必须按最通用的非线性场景设计,或明确只读取已证明连续的头部。

XDP 的 xdp_md 为什么不是 sk_buff

写 XDP 时上下文是 xdp_md,模型与 skb 不同;写 TC/socket 类程序才大量面对 skb:

维度 XDP(xdp_md TC / socket(skb 上下文)
所处阶段 驱动早期,Native 模式通常尚未分配 skb 已进入更完整的网络栈
数据视图 data / data_end 指向原始帧,另有入接口、RX queue 等有限元数据 可访问 skb 长度、协议标记等更丰富元数据
套接字信息 通常没有 struct sock,无法直接按 socket 归因 某些挂点可关联 socket
代价 少了 skb 分配与管理,路径极短 信息更丰富,但位置更晚、开销更高

这意味着 XDP 里不能照搬 skb->lenskb->devskb->sk 等思路;需要自己按 data/data_end 解析以太网、IP、TCP/UDP 头。XDP 的高性能,恰恰来自它绕开了大量 sk_buff 分配与协议栈工作。


eBPF 常见挂点与能回答的问题

挂点层级 典型能力 擅长回答
XDP 极早统计/丢弃/重定向 攻击流量、超高 PPS 过滤是否生效
TC ingress/egress 分类、标记、观测 策略前后流量差、容器 veth 边界
tracepoint:netif_/napi_ 驱动与软中断 是否卡在 NAPI、是否丢在设备
TCP tracepoints 建连、重传、探测 重传率、握手失败、RTO
kprobe 路由/netfilter 细函数级 特定模块是否成为瓶颈(脆弱、难移植)
sockops / sk_msg 等 套接字级 服务网格、加速路径(视内核)

没有「一个挂点看清一切」。完整故事通常是 多点串联 + 同一流标识关联

  • 五元组直观,但经过 NAT、代理或不同 netns 后会变化;
  • socket cookie 是内核为 socket 提供的稳定标识,在该 socket 生命周期内可用于关联多处事件,容器环境尤其有价值;
  • 不是所有包级挂点都能取得 socket cookie,跨 NAT、veth 与无 socket 的早期路径仍需结合五元组、netns、cgroup 等字段拼接。

可观测指标怎么设计

建议分层,避免只做一个「总带宽」:

示例指标
设备 RX/TX pps、drop、环缓冲不足
qdisc 排队时延、backlog、丢包
协议 重传、乱序、零窗口、监听队列溢出
套接字 发送缓冲占用、读缓冲积压
应用可见 RTT 分段(应用时间戳 vs 栈内时间戳)

聚合上优先 直方图 + top-K 流,而不是对每个包打全量日志。eBPF Map 用 per-CPU 计数降低竞争,用户态再汇总。


丢包与延迟的归因思路

#mermaid-svg-sBrQiMtpCUcWBtbj{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-sBrQiMtpCUcWBtbj .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-sBrQiMtpCUcWBtbj .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-sBrQiMtpCUcWBtbj .error-icon{fill:#552222;}#mermaid-svg-sBrQiMtpCUcWBtbj .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-sBrQiMtpCUcWBtbj .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-sBrQiMtpCUcWBtbj .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-sBrQiMtpCUcWBtbj .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-sBrQiMtpCUcWBtbj .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-sBrQiMtpCUcWBtbj .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-sBrQiMtpCUcWBtbj .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-sBrQiMtpCUcWBtbj .marker{fill:#333333;stroke:#333333;}#mermaid-svg-sBrQiMtpCUcWBtbj .marker.cross{stroke:#333333;}#mermaid-svg-sBrQiMtpCUcWBtbj svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-sBrQiMtpCUcWBtbj p{margin:0;}#mermaid-svg-sBrQiMtpCUcWBtbj .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-sBrQiMtpCUcWBtbj .cluster-label text{fill:#333;}#mermaid-svg-sBrQiMtpCUcWBtbj .cluster-label span{color:#333;}#mermaid-svg-sBrQiMtpCUcWBtbj .cluster-label span p{background-color:transparent;}#mermaid-svg-sBrQiMtpCUcWBtbj .label text,#mermaid-svg-sBrQiMtpCUcWBtbj span{fill:#333;color:#333;}#mermaid-svg-sBrQiMtpCUcWBtbj .node rect,#mermaid-svg-sBrQiMtpCUcWBtbj .node circle,#mermaid-svg-sBrQiMtpCUcWBtbj .node ellipse,#mermaid-svg-sBrQiMtpCUcWBtbj .node polygon,#mermaid-svg-sBrQiMtpCUcWBtbj .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-sBrQiMtpCUcWBtbj .rough-node .label text,#mermaid-svg-sBrQiMtpCUcWBtbj .node .label text,#mermaid-svg-sBrQiMtpCUcWBtbj .image-shape .label,#mermaid-svg-sBrQiMtpCUcWBtbj .icon-shape .label{text-anchor:middle;}#mermaid-svg-sBrQiMtpCUcWBtbj .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-sBrQiMtpCUcWBtbj .rough-node .label,#mermaid-svg-sBrQiMtpCUcWBtbj .node .label,#mermaid-svg-sBrQiMtpCUcWBtbj .image-shape .label,#mermaid-svg-sBrQiMtpCUcWBtbj .icon-shape .label{text-align:center;}#mermaid-svg-sBrQiMtpCUcWBtbj .node.clickable{cursor:pointer;}#mermaid-svg-sBrQiMtpCUcWBtbj .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-sBrQiMtpCUcWBtbj .arrowheadPath{fill:#333333;}#mermaid-svg-sBrQiMtpCUcWBtbj .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-sBrQiMtpCUcWBtbj .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-sBrQiMtpCUcWBtbj .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-sBrQiMtpCUcWBtbj .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-sBrQiMtpCUcWBtbj .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-sBrQiMtpCUcWBtbj .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-sBrQiMtpCUcWBtbj .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-sBrQiMtpCUcWBtbj .cluster text{fill:#333;}#mermaid-svg-sBrQiMtpCUcWBtbj .cluster span{color:#333;}#mermaid-svg-sBrQiMtpCUcWBtbj div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-sBrQiMtpCUcWBtbj .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-sBrQiMtpCUcWBtbj rect.text{fill:none;stroke-width:0;}#mermaid-svg-sBrQiMtpCUcWBtbj .icon-shape,#mermaid-svg-sBrQiMtpCUcWBtbj .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-sBrQiMtpCUcWBtbj .icon-shape p,#mermaid-svg-sBrQiMtpCUcWBtbj .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-sBrQiMtpCUcWBtbj .icon-shape .label rect,#mermaid-svg-sBrQiMtpCUcWBtbj .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-sBrQiMtpCUcWBtbj .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-sBrQiMtpCUcWBtbj .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-sBrQiMtpCUcWBtbj :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是



用户感知:慢或超时
重传升高?
查 TCP 重传点 / RTT / 对端
接收队列满?
应用读太慢或 CPU 饥饿
更早层 drop?
XDP/TC/网卡环/qdisc

实操顺序(可按环境裁剪):

  1. 确认是 客户端慢、服务端慢,还是路径丢包
  2. 看 TCP 重传与 RTT,区分拥塞与应用迟钝
  3. 看 socket 队列与应用 CPU
  4. 再向下看 qdisc、驱动、XDP 计数器

eBPF 负责把各层计数变成 同一时间窗可比的数据;最终结论仍要结合拓扑与业务。


实践注意

说明
开销 每包逻辑要极短;采样或按流采样
可移植 少绑私人 kprobe 符号;多用 tracepoint / CO-RE
容器 ifindex、netns、veth 成对出现;标识要带 netns
校验和与 GRO/GSO RX 侧 GRO 会合并多个包,TX 侧 GSO 会在稍后才分段;探针看到的可能是大 skb,PPS 口径会随挂点变化
安全 解析载荷涉及隐私时需最小化与授权

收束 :网络 eBPF 可观测的核心,是在 正确的路径高度 挂点,并用 skb(或 XDP 上下文)的真实布局 读数据;线性区不够时要承认非线性区的存在。先画清收发包路径,再写程序,比先选框架更重要。

尤其要先定义统计口径:你要数的是 线上实际包、GRO 聚合后的 RX skb,还是 GSO 分段前的 TX skb?同一流量在不同挂点得到的 PPS 不同,并不一定是谁统计错了,而可能是观察阶段不同。


系列结语:从使用工具到设计方案

至此,这套内容完成了一条从原理到工程的路径:

  1. 用挂载点回答 代码在哪里运行
  2. 用 Verifier、JIT 与 Map 回答 代码如何安全存活、数据如何出来
  3. 用框架选型回答 怎样开发、部署与维护
  4. 用 SSL/TLS 观测回答 如何处理敏感的用户态边界
  5. 用网络协议栈与 sk_buff 回答 怎样在基础设施路径上定位延迟和丢包

eBPF 的强大不只在字节码本身,而在于它提供了一组受控观察点,让工程师可以重新审视操作系统内部路径。真正的能力分水岭也不是「会不会运行某个现成工具」,而是能否从问题出发,选对 hook、数据结构、关联标识、聚合口径与权限边界,设计出可验证、可回滚、开销可控的观测方案。

整理自《深入理解 eBPF 与可观测性》网络可观测相关章节,并扩展路径、挂点与归因方法。

相关推荐
Tizzgod9 小时前
W55MH32 开箱分享 — 一块板子同时连俩TCP服务器
服务器·网络协议·tcp/ip
看-是灰机9 小时前
使用go语言实现对接
linux·开发语言·后端·docker·语言模型·golang·飞书
草莓熊Lotso9 小时前
【Linux网络】深入理解Linux IO多路复用:从本质到select服务器实战
linux·运维·服务器·c语言·网络·数据库·c++
fen_fen9 小时前
麒麟 Linux aarch64安装.NET 6,并适配.net应用(达梦数据库)
linux·服务器·.net
fen_fen9 小时前
Filebeat 采集 应用日志部署 & Kibana 查询操作文档
linux·运维·服务器
苏宸啊9 小时前
linux线程池
linux
草莓熊Lotso9 小时前
【LangChain】输出解析器全解:让大模型输出从 “聊天” 变 “机器可读”
服务器·数据库·python·langchain·pip
huohuopro9 小时前
手写 Tomcat 步骤教程
java·网络