| 1 |
|
| 2 |
|
| 3 |
package link |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"sync/atomic" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/go-quicktest/qt" |
| 11 |
"github.com/jsimonetti/rtnetlink/v2" |
| 12 |
"github.com/jsimonetti/rtnetlink/v2/driver" |
| 13 |
|
| 14 |
"github.com/cilium/ebpf" |
| 15 |
"github.com/cilium/ebpf/internal/testutils" |
| 16 |
"github.com/cilium/ebpf/internal/unix" |
| 17 |
) |
| 18 |
|
| 19 |
func TestAttachNetkit(t *testing.T) { |
| 20 |
testutils.SkipOnOldKernel(t, "6.7", "Netkit Device") |
| 21 |
|
| 22 |
ns := testutils.NewNetNS(t) |
| 23 |
|
| 24 |
prog := mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachNetkitPrimary, "") |
| 25 |
link, _ := mustAttachNetkit(t, prog, ebpf.AttachNetkitPrimary, ns) |
| 26 |
|
| 27 |
testLink(t, link, prog) |
| 28 |
} |
| 29 |
|
| 30 |
func TestNetkitAnchor(t *testing.T) { |
| 31 |
testutils.SkipOnOldKernel(t, "6.7", "Netkit Device") |
| 32 |
|
| 33 |
a := mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachNetkitPrimary, "") |
| 34 |
b := mustLoadProgram(t, ebpf.SchedCLS, ebpf.AttachNetkitPrimary, "") |
| 35 |
|
| 36 |
ns := testutils.NewNetNS(t) |
| 37 |
|
| 38 |
linkA, ifIndex := mustAttachNetkit(t, a, ebpf.AttachNetkitPrimary, ns) |
| 39 |
|
| 40 |
programInfo, err := a.Info() |
| 41 |
qt.Assert(t, qt.IsNil(err)) |
| 42 |
programID, _ := programInfo.ID() |
| 43 |
|
| 44 |
linkInfo, err := linkA.Info() |
| 45 |
qt.Assert(t, qt.IsNil(err)) |
| 46 |
linkID := linkInfo.ID |
| 47 |
|
| 48 |
for _, anchor := range []Anchor{ |
| 49 |
Head(), |
| 50 |
Tail(), |
| 51 |
BeforeProgram(a), |
| 52 |
BeforeProgramByID(programID), |
| 53 |
AfterLink(linkA), |
| 54 |
AfterLinkByID(linkID), |
| 55 |
} { |
| 56 |
t.Run(fmt.Sprintf("%T", anchor), func(t *testing.T) { |
| 57 |
var linkB Link |
| 58 |
qt.Assert(t, qt.IsNil(ns.Do(func() (err error) { |
| 59 |
linkB, err = AttachNetkit(NetkitOptions{ |
| 60 |
Program: b, |
| 61 |
Attach: ebpf.AttachNetkitPrimary, |
| 62 |
Interface: ifIndex, |
| 63 |
Anchor: anchor, |
| 64 |
}) |
| 65 |
return err |
| 66 |
}))) |
| 67 |
qt.Assert(t, qt.IsNil(linkB.Close())) |
| 68 |
}) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
var prevIfindex atomic.Uint32 |
| 74 |
|
| 75 |
func init() { prevIfindex.Store(1000 - 1) } |
| 76 |
|
| 77 |
func mustAttachNetkit(tb testing.TB, prog *ebpf.Program, attachType ebpf.AttachType, ns *testutils.NetNS) (Link, int) { |
| 78 |
var conn *rtnetlink.Conn |
| 79 |
qt.Assert(tb, qt.IsNil(ns.Do(func() (err error) { |
| 80 |
conn, err = rtnetlink.Dial(nil) |
| 81 |
return err |
| 82 |
}))) |
| 83 |
tb.Cleanup(func() { |
| 84 |
qt.Assert(tb, qt.IsNil(conn.Close())) |
| 85 |
}) |
| 86 |
|
| 87 |
ifIndex := prevIfindex.Add(1) |
| 88 |
|
| 89 |
layer2 := driver.NetkitModeL2 |
| 90 |
blackhole := driver.NetkitPolicyDrop |
| 91 |
qt.Assert(tb, qt.IsNil(conn.Link.New(&rtnetlink.LinkMessage{ |
| 92 |
Family: unix.AF_UNSPEC, |
| 93 |
Index: ifIndex, |
| 94 |
Flags: unix.IFF_UP, |
| 95 |
Change: unix.IFF_UP, |
| 96 |
Attributes: &rtnetlink.LinkAttributes{ |
| 97 |
Info: &rtnetlink.LinkInfo{ |
| 98 |
Kind: "netkit", |
| 99 |
Data: &driver.Netkit{ |
| 100 |
Mode: &layer2, |
| 101 |
PeerPolicy: &blackhole, |
| 102 |
}, |
| 103 |
}, |
| 104 |
}, |
| 105 |
}))) |
| 106 |
tb.Cleanup(func() { |
| 107 |
qt.Assert(tb, qt.IsNil(conn.Link.Delete(uint32(ifIndex)))) |
| 108 |
}) |
| 109 |
|
| 110 |
var link Link |
| 111 |
qt.Assert(tb, qt.IsNil(ns.Do(func() (err error) { |
| 112 |
link, err = AttachNetkit(NetkitOptions{ |
| 113 |
Program: prog, |
| 114 |
Attach: attachType, |
| 115 |
Interface: int(ifIndex), |
| 116 |
}) |
| 117 |
return err |
| 118 |
}))) |
| 119 |
tb.Cleanup(func() { |
| 120 |
qt.Assert(tb, qt.IsNil(link.Close())) |
| 121 |
}) |
| 122 |
|
| 123 |
return link, int(ifIndex) |
| 124 |
} |