Debian Codegraph @

link/query_test.go

raw symbols SCIP
1 //go:build !windows
2
3 package link
4
5 import (
6 "os"
7 "slices"
8 "testing"
9
10 "github.com/cilium/ebpf"
11 "github.com/cilium/ebpf/internal/testutils"
12
13 "github.com/go-quicktest/qt"
14 )
15
16 func TestQueryPrograms(t *testing.T) {
17 for name, fn := range map[string]func(*testing.T) (*ebpf.Program, Link, QueryOptions){
18 "cgroup": queryCgroupProgAttachFixtures,
19 "cgroup link": queryCgroupLinkFixtures,
20 "netns": queryNetNSFixtures,
21 "tcx": queryTCXFixtures,
22 } {
23 t.Run(name, func(t *testing.T) {
24 prog, link, opts := fn(t)
25 result, err := QueryPrograms(opts)
26 testutils.SkipIfNotSupported(t, err)
27 qt.Assert(t, qt.IsNil(err))
28
29 progInfo, err := prog.Info()
30 qt.Assert(t, qt.IsNil(err))
31 progID, _ := progInfo.ID()
32
33 i := slices.IndexFunc(result.Programs, func(ap AttachedProgram) bool {
34 return ap.ID == progID
35 })
36 qt.Assert(t, qt.Not(qt.Equals(i, -1)))
37
38 if name == "tcx" {
39 qt.Assert(t, qt.Not(qt.Equals(result.Revision, 0)))
40 }
41
42 if result.HaveLinkInfo() {
43 ap := result.Programs[i]
44 linkInfo, err := link.Info()
45 qt.Assert(t, qt.IsNil(err))
46
47 linkID, ok := ap.LinkID()
48 qt.Assert(t, qt.IsTrue(ok))
49 qt.Assert(t, qt.Equals(linkID, linkInfo.ID))
50 }
51 })
52 }
53 }
54
55 func queryCgroupProgAttachFixtures(t *testing.T) (*ebpf.Program, Link, QueryOptions) {
56 cgroup, prog := mustCgroupFixtures(t)
57
58 link, err := newProgAttachCgroup(cgroup, ebpf.AttachCGroupInetEgress, prog, flagAllowOverride)
59 if err != nil {
60 t.Fatal("Can't create link:", err)
61 }
62 t.Cleanup(func() {
63 qt.Assert(t, qt.IsNil(link.Close()))
64 })
65
66 return prog, nil, QueryOptions{
67 Target: int(cgroup.Fd()),
68 Attach: ebpf.AttachCGroupInetEgress,
69 }
70 }
71
72 func queryCgroupLinkFixtures(t *testing.T) (*ebpf.Program, Link, QueryOptions) {
73 cgroup, prog := mustCgroupFixtures(t)
74
75 link, err := newLinkCgroup(cgroup, ebpf.AttachCGroupInetEgress, prog)
76 testutils.SkipIfNotSupported(t, err)
77 if err != nil {
78 t.Fatal("Can't create link:", err)
79 }
80 t.Cleanup(func() {
81 qt.Assert(t, qt.IsNil(link.Close()))
82 })
83
84 return prog, nil, QueryOptions{
85 Target: int(cgroup.Fd()),
86 Attach: ebpf.AttachCGroupInetEgress,
87 }
88 }
89
90 func queryNetNSFixtures(t *testing.T) (*ebpf.Program, Link, QueryOptions) {
91 testutils.SkipOnOldKernel(t, "4.20", "flow_dissector program")
92
93 prog := mustLoadProgram(t, ebpf.FlowDissector, ebpf.AttachFlowDissector, "")
94
95 // RawAttachProgramOptions.Target needs to be 0, as PROG_ATTACH with namespaces
96 // only works with the threads current netns. Any other fd will be rejected.
97 if err := RawAttachProgram(RawAttachProgramOptions{
98 Target: 0,
99 Program: prog,
100 Attach: ebpf.AttachFlowDissector,
101 }); err != nil {
102 t.Fatal(err)
103 }
104
105 t.Cleanup(func() {
106 err := RawDetachProgram(RawDetachProgramOptions{
107 Target: 0,
108 Program: prog,
109 Attach: ebpf.AttachFlowDissector,
110 })
111 if err != nil {
112 t.Fatal(err)
113 }
114 })
115
116 netns, err := os.Open("/proc/self/ns/net")
117 qt.Assert(t, qt.IsNil(err))
118 t.Cleanup(func() { netns.Close() })
119
120 return prog, nil, QueryOptions{
121 Target: int(netns.Fd()),
122 Attach: ebpf.AttachFlowDissector,
123 }
124 }
125
126 func queryTCXFixtures(t *testing.T) (*ebpf.Program, Link, QueryOptions) {
127 testutils.SkipOnOldKernel(t, "6.6", "TCX link")
128
129 prog := mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachTCXIngress, "")
130
131 link, iface := mustAttachTCX(t, prog, ebpf.AttachTCXIngress)
132
133 return prog, link, QueryOptions{
134 Target: iface,
135 Attach: ebpf.AttachTCXIngress,
136 }
137 }