使用 cilium ebpf-go 跟踪网络接口

问题描述 投票:0回答:1

我正在尝试编写一个 go 程序,每次创建新的网络接口时都会在终端中写入一些内容。为此,我使用 Go 库 Cilium ebpf-go (https://github.com/cilium/ebpf)。我是 ebpf 新手,所以我需要帮助。目前我的理解是我必须使用 kprobe 或 fentry 程序,因为我必须将 ebpf 程序附加到没有定义的跟踪点可供使用的函数。我查看了 linux 内核,我很确定我应该将程序附加到

dev_alloc_name
rtnl_create_link
,但如果我错了,请告诉我。另外,由于这是我第一次使用这个库编写一些东西,所以我尝试从存储库中获取示例并修改它们。我尝试使用 kprobe 一个(https://github.com/cilium/ebpf/tree/main/examples/kprobe)和 fentry 一个(https://github.com/cilium/ebpf/tree/main /examples/fentry)但我在理解它们如何工作方面遇到了一些困难。

go linux-kernel ebpf cilium
1个回答
0
投票

您是否考虑过仅使用

Golang netlink 库
中的 LinkSubscribe,如下所示?

ctx, cancel := context.WithCancel(context.Background())
updates := make(chan netlink.LinkUpdate)

if err := netlink.LinkSubscribe(updates, ctx.Done()); err != nil {
    log.WithError(err).Fatal("Failed to subscribe for link changes")
}

查看 Cilium 如何使用它以获得更完整的示例:

pkg/datapath/loader/netlink.go#L480

© www.soinside.com 2019 - 2024. All rights reserved.