c - BPF : mark in structure __skbuff is not writeable?

标签 c linux-kernel network-programming iptables ebpf

我有一个 BPF 代码(“classifier”部分)。我使用它加载到使用 tc(流量 Controller )实用程序的界面。我的代码更改了 __skbuff 中的标记。后来当我尝试使用 iptables 捕获这个标记时,我发现我编辑的标记已经消失了。

代码:

__section("classifier")
int function(struct __sk_buff *skb)
{
    skb->mark = 0x123;

我使用 iptable mangle 表的以下规则来查看标记是否写入正确。

# iptables -t mangle -A PREROUTING -i <my_interface> \
    -m mark --mark 0x123 \
    -j LOG --log-prefix "MY_PRINTS" --log-level 7

以下是我用来加载 bpf 程序的 TC 命令;

# tc qdisc add dev <myInterface> root handle 1: prio
# tc filter add dev <myInterface> parent 1: bpf obj bpf.o flowid 1:1 direct-action

最佳答案

问题出在您的 tc 命令中。您正在将过滤器连接到导出侧。

root parent 指的是导出侧,用于流量整形。相反,如果您想在入口侧附加过滤器,则应使用类似这样的东西(不需要句柄):

# tc qdisc add dev <myInterface> ingress
# tc filter add dev <myInterface> ingress bpf obj bpf.o direct-action

或者,更好的做法是,使用 BPF 特定的 qdisc clsact,它可用于为入口和导出附加过滤器(关于它的文档不多,除了它的 commit logCilium's BPF documentation(搜索clsact)):

# tc qdisc add dev <myInterface> clsact
# tc filter add dev <myInterface> ingress bpf obj bpf.o direct-action

关于c - BPF : mark in structure __skbuff is not writeable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57055240/

相关文章:

C 程序之间通过自组织网络进行通信

c - 仅监视目录中的新文件

asp.net - 如何在 Web API 中使用两个参数构建 Get

c - Linux的disable_irq()和local_irq_save()

linux - 如何在Linux中设置和锁定CPU频率

c# - C# 中的 TCPClient 与套接字

c - 我正在尝试将 C 代码转换为 Mathematica 代码,我做错了什么?

c - 使用 %*[^|] 分隔符时 sscanf() 是否从标准输入中删除字符?

android - 在移动设备上修改和启动 linux 内核

python - 我如何告诉 boto 操作成功?