Debian Codegraph @

link/program_test.go

raw symbols SCIP
1 //go:build !windows
2
3 package link
4
5 import (
6 "fmt"
7 "net"
8 "os"
9 "testing"
10
11 "github.com/cilium/ebpf"
12 "github.com/cilium/ebpf/internal/testutils"
13
14 "github.com/go-quicktest/qt"
15 )
16
17 func TestProgramAlter(t *testing.T) {
18 testutils.SkipOnOldKernel(t, "4.13", "SkSKB type")
19
20 prog := mustLoadProgram(t, ebpf.SkSKB, 0, "")
21
22 var sockMap *ebpf.Map
23 sockMap, err := ebpf.NewMap(&ebpf.MapSpec{
24 Type: ebpf.MapType(15), // BPF_MAP_TYPE_SOCKMAP
25 KeySize: 4,
26 ValueSize: 4,
27 MaxEntries: 2,
28 })
29 if err != nil {
30 t.Fatal(err)
31 }
32 defer sockMap.Close()
33
34 err = RawAttachProgram(RawAttachProgramOptions{
35 Target: sockMap.FD(),
36 Program: prog,
37 Attach: ebpf.AttachSkSKBStreamParser,
38 })
39 if err != nil {
40 t.Fatal(err)
41 }
42
43 err = RawDetachProgram(RawDetachProgramOptions{
44 Target: sockMap.FD(),
45 Program: prog,
46 Attach: ebpf.AttachSkSKBStreamParser,
47 })
48 if err != nil {
49 t.Fatal(err)
50 }
51 }
52
53 func TestRawAttachProgramAnchor(t *testing.T) {
54 testutils.SkipOnOldKernel(t, "6.6", "attach anchor")
55
56 iface, err := net.InterfaceByName("lo")
57 qt.Assert(t, qt.IsNil(err))
58
59 a := mustLoadProgram(t, ebpf.SchedCLS, 0, "")
60 info, err := a.Info()
61 qt.Assert(t, qt.IsNil(err))
62 aID, _ := info.ID()
63
64 err = RawAttachProgram(RawAttachProgramOptions{
65 Target: iface.Index,
66 Program: a,
67 Attach: ebpf.AttachTCXIngress,
68 })
69 qt.Assert(t, qt.IsNil(err))
70 defer RawDetachProgram(RawDetachProgramOptions{
71 Target: iface.Index,
72 Program: a,
73 Attach: ebpf.AttachTCXIngress,
74 })
75
76 link, err := AttachTCX(TCXOptions{
77 Interface: iface.Index,
78 Program: mustLoadProgram(t, ebpf.SchedCLS, 0, ""),
79 Attach: ebpf.AttachTCXIngress,
80 })
81 qt.Assert(t, qt.IsNil(err))
82 defer link.Close()
83
84 linkInfo, err := link.Info()
85 qt.Assert(t, qt.IsNil(err))
86
87 b := mustLoadProgram(t, ebpf.SchedCLS, 0, "")
88
89 for _, anchor := range []Anchor{
90 Head(),
91 Tail(),
92 AfterProgram(a),
93 AfterProgramByID(aID),
94 AfterLink(link),
95 AfterLinkByID(linkInfo.ID),
96 } {
97 t.Run(fmt.Sprintf("%T", anchor), func(t *testing.T) {
98 err := RawAttachProgram(RawAttachProgramOptions{
99 Target: iface.Index,
100 Program: b,
101 Attach: ebpf.AttachTCXIngress,
102 Anchor: anchor,
103 })
104 qt.Assert(t, qt.IsNil(err))
105
106 // Detach doesn't allow first or last anchor.
107 if _, ok := anchor.(firstAnchor); ok {
108 anchor = nil
109 } else if _, ok := anchor.(lastAnchor); ok {
110 anchor = nil
111 }
112
113 err = RawDetachProgram(RawDetachProgramOptions{
114 Target: iface.Index,
115 Program: b,
116 Attach: ebpf.AttachTCXIngress,
117 Anchor: anchor,
118 })
119 qt.Assert(t, qt.IsNil(err))
120 })
121 }
122
123 // Check that legacy replacement with a program works.
124 err = RawAttachProgram(RawAttachProgramOptions{
125 Target: iface.Index,
126 Program: b,
127 Attach: ebpf.AttachTCXIngress,
128 Anchor: ReplaceProgram(a),
129 })
130 qt.Assert(t, qt.IsNil(err))
131
132 err = RawDetachProgram(RawDetachProgramOptions{
133 Target: iface.Index,
134 Program: b,
135 Attach: ebpf.AttachTCXIngress,
136 })
137 qt.Assert(t, qt.IsNil(err))
138 }
139
140 func TestRawAttachProgramReplace(t *testing.T) {
141 testutils.SkipOnOldKernel(t, "5.9", "BPF_F_ALLOW_MULTI with BPF_F_REPLACE")
142
143 cgroup, progA := mustCgroupFixtures(t)
144
145 // Attach progA.
146 qt.Assert(t, qt.IsNil(RawAttachProgram(RawAttachProgramOptions{
147 Target: int(cgroup.Fd()),
148 Program: progA,
149 Attach: ebpf.AttachCGroupInetEgress,
150 Flags: uint32(flagAllowMulti),
151 })))
152
153 // Replace progA with progB. BPF_F_ALLOW_MULTI must be passed again when
154 // replacing a program that was originally attached with that flag.
155 progB := mustLoadProgram(t, ebpf.CGroupSKB, ebpf.AttachCGroupInetEgress, "")
156 qt.Assert(t, qt.IsNil(RawAttachProgram(RawAttachProgramOptions{
157 Target: int(cgroup.Fd()),
158 Program: progB,
159 Attach: ebpf.AttachCGroupInetEgress,
160 Flags: uint32(flagAllowMulti),
161 Anchor: ReplaceProgram(progA),
162 })))
163 t.Cleanup(func() {
164 qt.Assert(t, qt.IsNil(RawDetachProgram(RawDetachProgramOptions{
165 Target: int(cgroup.Fd()),
166 Program: progB,
167 Attach: ebpf.AttachCGroupInetEgress,
168 Flags: uint32(flagAllowMulti),
169 })))
170 })
171
172 progBInfo, err := progB.Info()
173 qt.Assert(t, qt.IsNil(err))
174 progBID, _ := progBInfo.ID()
175
176 // Query attached programs and verify only progB is present.
177 result, err := QueryPrograms(QueryOptions{
178 Target: int(cgroup.Fd()),
179 Attach: ebpf.AttachCGroupInetEgress,
180 })
181 qt.Assert(t, qt.IsNil(err))
182 qt.Assert(t, qt.HasLen(result.Programs, 1))
183 qt.Assert(t, qt.Equals(result.Programs[0].ID, progBID))
184
185 // progA should fail to detach.
186 qt.Assert(t, qt.ErrorIs(RawDetachProgram(RawDetachProgramOptions{
187 Target: int(cgroup.Fd()),
188 Program: progA,
189 Attach: ebpf.AttachCGroupInetEgress,
190 Flags: uint32(flagAllowMulti),
191 }), os.ErrNotExist))
192 }