Skip to content

Commit 96dff81

Browse files
fix: remove vishvananda/netns pkg
Signed-off-by: Jack-R-lantern <tjdfkr2421@gmail.com>
1 parent 53fb8a1 commit 96dff81

File tree

4 files changed

+78
-46
lines changed

4 files changed

+78
-46
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/go-quicktest/qt v1.101.1-0.20240301121107-c6c8733fa1e6
77
github.com/google/go-cmp v0.7.0
88
github.com/jsimonetti/rtnetlink/v2 v2.0.1
9-
github.com/vishvananda/netns v0.0.5
109
golang.org/x/sys v0.37.0
1110
)
1211

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
6060
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
6161
github.com/vbatts/tar-split v0.12.1 h1:CqKoORW7BUWBe7UL/iqTVvkTBOF8UvOMKOIZykxnnbo=
6262
github.com/vbatts/tar-split v0.12.1/go.mod h1:eF6B6i6ftWQcDqEn3/iGFRFRo8cBIMSJVOpnNdfTMFA=
63-
github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zdEY=
64-
github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
6563
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
6664
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
6765
golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4=

internal/testutils/netns.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package testutils
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"runtime"
7+
"syscall"
8+
"testing"
9+
10+
"golang.org/x/sys/unix"
11+
)
12+
13+
// GetThreadNetNS gets the network namespace file descriptor of the thread the current goroutine
14+
// is running on. Make sure to call runtime.LockOSThread() before this so the goroutine does not
15+
// get scheduled on another thread in the meantime.
16+
func GetThreadNetNS() (int, error) {
17+
file, err := os.Open(fmt.Sprintf("/proc/%d/task/%d/ns/net", unix.Getpid(), unix.Gettid()))
18+
if err != nil {
19+
return -1, err
20+
}
21+
return int(file.Fd()), nil
22+
}
23+
24+
// CreateNamedNetNS unshares the current thread's network namespace,
25+
// creating a new isolated network namespace for testing or sandboxing purposes.
26+
// It returns an error if the unshare operation fails.
27+
func CreateNamedNetNS() error {
28+
if err := unix.Unshare(unix.CLONE_NEWNET); err != nil {
29+
return err
30+
}
31+
return nil
32+
}
33+
34+
// SetThreadNetNS sets the network namespace of the thread of the current goroutine to
35+
// the namespace described by the user-provided file descriptor.
36+
func SetThreadNetNS(fd int) error {
37+
return unix.Setns(fd, unix.CLONE_NEWNET)
38+
}
39+
40+
func WithNetNameSpace(ns int, fn func(t *testing.T)) func(t *testing.T) {
41+
return func(t *testing.T) {
42+
runtime.LockOSThread()
43+
defer runtime.UnlockOSThread()
44+
45+
origNS, err := GetThreadNetNS()
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
defer syscall.Close(origNS)
50+
defer SetThreadNetNS(origNS)
51+
52+
if err := SetThreadNetNS(ns); err != nil {
53+
t.Fatal(err)
54+
}
55+
56+
fn(t)
57+
}
58+
}

link/netkit_test.go

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"fmt"
77
"runtime"
88
"sync/atomic"
9+
"syscall"
910
"testing"
1011

1112
"github.com/go-quicktest/qt"
1213
"github.com/jsimonetti/rtnetlink/v2"
1314
"github.com/jsimonetti/rtnetlink/v2/driver"
14-
"github.com/vishvananda/netns"
1515

1616
"github.com/cilium/ebpf"
1717
"github.com/cilium/ebpf/internal/testutils"
@@ -33,21 +33,25 @@ func TestNetkitAnchor(t *testing.T) {
3333
a := mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachNetkitPrimary, "")
3434
b := mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachNetkitPrimary, "")
3535

36-
const testNamespace string = "test_netkit_anchor"
36+
runtime.LockOSThread()
37+
defer runtime.UnlockOSThread()
3738

38-
rootNS, err := netns.Get()
39+
rootNS, err := testutils.GetThreadNetNS()
3940
if err != nil {
4041
t.Fatal(err)
4142
}
42-
defer rootNS.Close()
43-
defer netns.Set(rootNS)
43+
defer syscall.Close(rootNS)
44+
defer testutils.SetThreadNetNS(rootNS)
4445

45-
testNS, err := netns.NewNamed(testNamespace)
46+
err = testutils.CreateNamedNetNS()
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
testNS, err := testutils.GetThreadNetNS()
4651
if err != nil {
4752
t.Fatal(err)
4853
}
49-
defer netns.DeleteNamed(testNamespace)
50-
defer testNS.Close()
54+
defer syscall.Close(testNS)
5155

5256
linkA, ifIndex := mustAttachNetkit(t, a, ebpf.AttachNetkitPrimary)
5357

@@ -67,19 +71,16 @@ func TestNetkitAnchor(t *testing.T) {
6771
AfterLink(linkA),
6872
AfterLinkByID(linkID),
6973
} {
70-
t.Run(fmt.Sprintf("%T", anchor), func(t *testing.T) {
71-
linkB, err := withNetNameSpace(
72-
testNamespace,
73-
NetkitOptions{
74-
Program: b,
75-
Attach: ebpf.AttachNetkitPrimary,
76-
Interface: ifIndex,
77-
Anchor: anchor,
78-
},
79-
AttachNetkit)
74+
t.Run(fmt.Sprintf("%T", anchor), testutils.WithNetNameSpace(testNS, func(t *testing.T) {
75+
linkB, err := AttachNetkit(NetkitOptions{
76+
Program: b,
77+
Attach: ebpf.AttachNetkitPrimary,
78+
Interface: ifIndex,
79+
Anchor: anchor,
80+
})
8081
qt.Assert(t, qt.IsNil(err))
8182
qt.Assert(t, qt.IsNil(linkB.Close()))
82-
})
83+
}))
8384
}
8485
}
8586

@@ -132,27 +133,3 @@ func mustAttachNetkit(tb testing.TB, prog *ebpf.Program, attachType ebpf.AttachT
132133

133134
return link, int(ifIndex)
134135
}
135-
136-
func withNetNameSpace(name string, opt NetkitOptions, fn func(opt NetkitOptions) (Link, error)) (Link, error) {
137-
runtime.LockOSThread()
138-
defer runtime.UnlockOSThread()
139-
140-
rootNS, err := netns.Get()
141-
if err != nil {
142-
return nil, err
143-
}
144-
defer rootNS.Close()
145-
defer netns.Set(rootNS)
146-
147-
netns.NewNamed("")
148-
149-
testNS, err := netns.GetFromName(name)
150-
if err != nil {
151-
return nil, err
152-
}
153-
defer testNS.Close()
154-
155-
netns.Set(testNS)
156-
157-
return fn(opt)
158-
}

0 commit comments

Comments
 (0)