diff --git a/link/anchor.go b/link/anchor.go index d4f5b7f0e..7f2bfde14 100644 --- a/link/anchor.go +++ b/link/anchor.go @@ -7,7 +7,11 @@ import ( "github.com/cilium/ebpf/internal/sys" ) -const anchorTypeFlags = sys.BPF_F_LINK_MPROG | sys.BPF_F_ID +const anchorFlags = sys.BPF_F_REPLACE | + sys.BPF_F_BEFORE | + sys.BPF_F_AFTER | + sys.BPF_F_ID | + sys.BPF_F_LINK_MPROG type AttachAnchor interface { target() (fdOrID uint32, flags uint32, _ error) @@ -72,3 +76,26 @@ type anchorProgramID struct { func (apid *anchorProgramID) target() (uint32, uint32, error) { return uint32(apid.Program), sys.BPF_F_ID, nil } + +func Before(anchor AttachAnchor) AttachAnchor { + return anchorPosition{anchor, sys.BPF_F_BEFORE} +} + +func After(anchor AttachAnchor) AttachAnchor { + return anchorPosition{anchor, sys.BPF_F_AFTER} +} + +func Replace(anchor AttachAnchor) AttachAnchor { + return anchorPosition{anchor, sys.BPF_F_REPLACE} +} + +type anchorPosition struct { + anchor AttachAnchor + flag uint32 +} + +func (ap anchorPosition) target() (uint32, uint32, error) { + fdOrId, flags, err := ap.anchor.target() + flags |= ap.flag + return fdOrId, flags, err +} diff --git a/link/program.go b/link/program.go index ae67dcd60..95af8c17c 100644 --- a/link/program.go +++ b/link/program.go @@ -28,8 +28,8 @@ type RawAttachProgramOptions struct { // You should use one of the higher level abstractions available in this // package if possible. func RawAttachProgram(opts RawAttachProgramOptions) error { - if opts.Flags&anchorTypeFlags != 0 { - return fmt.Errorf("don't specify attach target type in flags") + if opts.Flags&anchorFlags != 0 { + return fmt.Errorf("disallowed flags: use Anchor to specify attach target") } attr := sys.ProgAttachAttr{ @@ -80,6 +80,10 @@ type RawDetachProgramOptions struct { // You should use one of the higher level abstractions available in this // package if possible. func RawDetachProgram(opts RawDetachProgramOptions) error { + if opts.Flags&anchorFlags != 0 { + return fmt.Errorf("disallowed flags: use Anchor to specify attach target") + } + attr := sys.ProgDetachAttr{ TargetFdOrIfindex: uint32(opts.Target), AttachBpfFd: uint32(opts.Program.FD()), diff --git a/link/program_test.go b/link/program_test.go index 3ba793e24..baa6b90a1 100644 --- a/link/program_test.go +++ b/link/program_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/cilium/ebpf" - "github.com/cilium/ebpf/internal/sys" "github.com/cilium/ebpf/internal/testutils" qt "github.com/frankban/quicktest" @@ -95,8 +94,7 @@ func TestRawAttachProgramAnchor(t *testing.T) { Target: iface.Index, Program: b, Attach: ebpf.AttachTCXIngress, - Anchor: anchor, - Flags: sys.BPF_F_AFTER, + Anchor: After(anchor), }) qt.Assert(t, err, qt.IsNil) @@ -104,8 +102,7 @@ func TestRawAttachProgramAnchor(t *testing.T) { Target: iface.Index, Program: b, Attach: ebpf.AttachTCXIngress, - Anchor: anchor, - Flags: sys.BPF_F_AFTER, + Anchor: After(anchor), }) qt.Assert(t, err, qt.IsNil) }) @@ -116,8 +113,7 @@ func TestRawAttachProgramAnchor(t *testing.T) { Target: iface.Index, Program: b, Attach: ebpf.AttachTCXIngress, - Anchor: AnchorProgram(a), - Flags: sys.BPF_F_REPLACE, + Anchor: Replace(AnchorProgram(a)), }) qt.Assert(t, err, qt.IsNil) diff --git a/link/tcx.go b/link/tcx.go index aa82a05f1..a72deef2f 100644 --- a/link/tcx.go +++ b/link/tcx.go @@ -27,8 +27,8 @@ func AttachTCX(opts TCXOptions) (Link, error) { return nil, fmt.Errorf("interface %d is out of bounds", opts.Interface) } - if opts.Flags&anchorTypeFlags != 0 { - return nil, fmt.Errorf("don't specify attach target type in flags") + if opts.Flags&anchorFlags != 0 { + return nil, fmt.Errorf("disallowed flags: use Anchor to specify attach target") } attr := sys.LinkCreateTcxAttr{ diff --git a/link/tcx_test.go b/link/tcx_test.go index 184c2ba6b..57d2ad33d 100644 --- a/link/tcx_test.go +++ b/link/tcx_test.go @@ -6,11 +6,11 @@ import ( "net" "testing" + qt "github.com/frankban/quicktest" + "github.com/cilium/ebpf" - "github.com/cilium/ebpf/internal/sys" "github.com/cilium/ebpf/internal/testutils" "github.com/cilium/ebpf/internal/unix" - qt "github.com/frankban/quicktest" ) func TestTCX(t *testing.T) { @@ -49,8 +49,7 @@ func TestTCXAnchor(t *testing.T) { Program: b, Attach: ebpf.AttachTCXEgress, Interface: iface, - Anchor: anchor, - Flags: sys.BPF_F_AFTER, + Anchor: After(anchor), }) qt.Assert(t, err, qt.IsNil) qt.Assert(t, linkB.Close(), qt.IsNil)