解决 Linux TC 报错 clsact direct-action filter invalid packet drop 过滤丢包修复
排查 使用 Linux Traffic Control (TC) 挂载 eBPF 流量调度程序后,正常流量被静默丢弃(Dropped),tc -s qdisc show 报告 drop 计数飙升,分析 direct-action 返回码与 SKB 协议头解析。
问题现象
向网卡 egress/ingress 挂载 TC eBPF 过滤器后,所有经过的 TCP/UDP 数据包无法到达应用层,tc -s filter show 显示:
filter parent ffff: protocol all pref 49152 bpf
filter parent ffff: protocol all pref 49152 bpf flowid 1:1 handle 0x1 filter.o:[egress] direct-action id 402 tag a1b2c3d4 jited
Sent 0 bytes 0 pkt (dropped 894212, overlimits 0 requeues 0)
触发根因
- eBPF 代码中对非线性 SKB 内存切片直接执行裸指针偏移解包,超出直接访问边界时触发内核安全校验截断并返回
TC_ACT_SHOT; - 程序未调用
bpf_skb_pull_data()保证数据包头部的线性连续性; - 挂载时未指定
direct-action(da)标志位,导致 TC 将 BPF 返回的动作码误当成分类器 Class ID 解析失败。
解决步骤
挂载 TC 过滤器时显式声明 direct-action 参数
执行标准挂载命令:
tc qdisc add dev eth0 clsact
tc filter add dev eth0 egress bpf da obj filter.o sec tc_egress
在 eBPF C 代码中解包前先调用 bpf_skb_pull_data 线性化报文头
保护内存安全:
if (bpf_skb_pull_data(skb, sizeof(struct ethhdr) + sizeof(struct iphdr)) < 0)
return TC_ACT_OK; // 无法线性化时安全放行
确保有明确的默认放行动作
未命中阻断规则的数据包统一显式返回 TC_ACT_OK。
验证确认
重新注入测试报文:
dropped 计数停止增长,合规业务流量全速转发,TC eBPF 策略精准命中并生效。


