From b872e2961cbd05136c50f91c81dd040335b2a9f4 Mon Sep 17 00:00:00 2001 From: Tao Chen Date: Mon, 8 Apr 2024 11:31:21 +0800 Subject: [PATCH 1/2] chore(selftest): Add tc Query/Detach test case The bpf_tc_query/bpf_tc_detach interface should init the parameter first, sometimes it may be confused for us, so add the case to reduce the confusion. https://github.com/aquasecurity/libbpfgo/issues/421 https://github.com/aquasecurity/libbpfgo/issues/420 Signed-off-by: Tao Chen --- selftest/tc/main.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/selftest/tc/main.go b/selftest/tc/main.go index 0174eb16..584c608d 100644 --- a/selftest/tc/main.go +++ b/selftest/tc/main.go @@ -56,12 +56,38 @@ func main() { var tcOpts bpf.TcOpts tcOpts.ProgFd = int(tcProg.GetFd()) + tcOpts.Handle = 1 + tcOpts.Priority = 1 err = hook.Attach(&tcOpts) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(-1) } + // test for query + tcOpts.ProgFd = 0 + tcOpts.ProgId = 0 + err = hook.Query(&tcOpts) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + if tcOpts.Handle != 1 { + fmt.Fprintln(os.Stderr, "query info error, handle:%d", tcOpts.Handle) + os.Exit(-1) + } + + // test for detach + defer func() { + tcOpts.ProgFd = 0 + tcOpts.ProgId = 0 + err = hook.Detach(&tcOpts) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(-1) + } + }() + eventsChannel := make(chan []byte) rb, err := bpfModule.InitRingBuf("events", eventsChannel) if err != nil { From e670b536180187ca62ba86f476c008952ed66775 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geyslan=20Greg=C3=B3rio?= Date: Tue, 9 Apr 2024 14:44:59 -0300 Subject: [PATCH 2/2] add comment with tc opts values origin --- selftest/tc/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/selftest/tc/main.go b/selftest/tc/main.go index 584c608d..8304afbf 100644 --- a/selftest/tc/main.go +++ b/selftest/tc/main.go @@ -54,7 +54,7 @@ func main() { os.Exit(-1) } - var tcOpts bpf.TcOpts + var tcOpts bpf.TcOpts // https://elixir.bootlin.com/linux/v6.8.4/source/tools/testing/selftests/bpf/prog_tests/tc_bpf.c#L26 tcOpts.ProgFd = int(tcProg.GetFd()) tcOpts.Handle = 1 tcOpts.Priority = 1