Skip to content

Commit

Permalink
link: add Replace, Before, After WIP
Browse files Browse the repository at this point in the history
- Is this a good idea?

Signed-off-by: Lorenz Bauer <[email protected]>
  • Loading branch information
lmb committed Oct 10, 2023
1 parent 7dd5e97 commit 660b2b3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
29 changes: 28 additions & 1 deletion link/anchor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
8 changes: 6 additions & 2 deletions link/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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()),
Expand Down
10 changes: 3 additions & 7 deletions link/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -95,17 +94,15 @@ 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)

err = RawDetachProgram(RawDetachProgramOptions{
Target: iface.Index,
Program: b,
Attach: ebpf.AttachTCXIngress,
Anchor: anchor,
Flags: sys.BPF_F_AFTER,
Anchor: After(anchor),
})
qt.Assert(t, err, qt.IsNil)
})
Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions link/tcx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
7 changes: 3 additions & 4 deletions link/tcx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 660b2b3

Please sign in to comment.