3 Star 1 Fork 1

Gitee 极速下载/xdpcap

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/cloudflare/xdpcap
克隆/下载
hook_test.go 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
Arthur Fabre 提交于 2021-11-30 21:57 . hook: Simplify Hook.Patch()
package xdpcap
import (
"testing"
"github.com/cilium/ebpf"
)
const (
elf = "testdata/xdp_hook.c.elf"
hookSymbol = "xdpcap_hook"
)
// Test loading an elf with a hook map using ebpf.NewCollection().
func TestHookNewCollection(t *testing.T) {
hook, err := NewHook("foo")
if err != nil {
t.Fatal(err)
}
defer hook.Close()
spec := mustPatchSpec(t, hook)
coll, err := ebpf.NewCollection(spec)
if err != nil {
t.Fatal(err)
}
defer coll.Close()
}
// Test loading an elf with a hook map using ebpf.CollectionSpec.LoadAndReplace(),
// which didn't always work: https://github.com/cilium/ebpf/commit/04b5c2a901f3bcfa7d7a13c59f7c1c556f2f3d5f
func TestHookLoadAndReplace(t *testing.T) {
test := func(t *testing.T, hook *Hook) {
spec := mustPatchSpec(t, hook)
var objs struct {
// Works for both programs that do and don't use the hook.
Hook *ebpf.Program `ebpf:"xdp_hook"`
NoHook *ebpf.Program `ebpf:"xdp_nohook"`
}
if err := spec.LoadAndAssign(&objs, nil); err != nil {
t.Fatal(err)
}
defer objs.Hook.Close()
defer objs.NoHook.Close()
}
t.Run("nil", func(t *testing.T) {
test(t, nil)
})
t.Run("not-nil", func(t *testing.T) {
hook, err := NewHook("foo")
if err != nil {
t.Fatal(err)
}
defer hook.Close()
test(t, hook)
})
}
// Test loading an elf that uses a hook without an explicit hook map
func TestNoHook(t *testing.T) {
spec, err := ebpf.LoadCollectionSpec(elf)
if err != nil {
t.Fatal(err)
}
coll, err := ebpf.NewCollection(spec)
if err != nil {
t.Fatal(err)
}
defer coll.Close()
}
func mustPatchSpec(tb testing.TB, hook *Hook) *ebpf.CollectionSpec {
spec, err := ebpf.LoadCollectionSpec(elf)
if err != nil {
tb.Fatal(err)
}
err = hook.Patch(spec, hookSymbol)
if err != nil {
tb.Fatal(err)
}
return spec
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mirrors/xdpcap.git
[email protected]:mirrors/xdpcap.git
mirrors
xdpcap
xdpcap
master

搜索帮助