Skip to content

Commit

Permalink
link: add Cookie to Tracing and LSM opts
Browse files Browse the repository at this point in the history
Signed-off-by: Mattia Meleleo <[email protected]>
  • Loading branch information
mmat11 committed Feb 13, 2023
1 parent 724318b commit 9120a11
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
17 changes: 14 additions & 3 deletions link/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,22 @@ type TracingOptions struct {
// If empty (AttachNone), the legacy attach method via raw tracepoint
// open will be used.
AttachType ebpf.AttachType
// Arbitrary value that can be fetched from an eBPF program
// via `bpf_get_attach_cookie()`.
Cookie uint64
}

type LSMOptions struct {
// Program must be of type LSM with attach type
// AttachLSMMac.
Program *ebpf.Program
// Arbitrary value that can be fetched from an eBPF program
// via `bpf_get_attach_cookie()`.
Cookie uint64
}

// attachBTFID links all BPF program types (Tracing/LSM) that they attach to a btf_id.
func attachBTFID(program *ebpf.Program, at ebpf.AttachType) (Link, error) {
func attachBTFID(program *ebpf.Program, at ebpf.AttachType, cookie uint64) (Link, error) {
if program.FD() < 0 {
return nil, fmt.Errorf("invalid program %w", sys.ErrClosedFd)
}
Expand All @@ -121,6 +127,7 @@ func attachBTFID(program *ebpf.Program, at ebpf.AttachType) (Link, error) {
fd, err = sys.LinkCreateTracing(&sys.LinkCreateTracingAttr{
ProgFd: uint32(program.FD()),
AttachType: sys.AttachType(at),
Cookie: cookie,
})
if err == nil {
break
Expand All @@ -131,6 +138,10 @@ func attachBTFID(program *ebpf.Program, at ebpf.AttachType) (Link, error) {
fallthrough
case ebpf.AttachNone:
// Attach via RawTracepointOpen
if cookie > 0 {
return nil, fmt.Errorf("create raw tracepoint with cookie: %w", ErrNotSupported)
}

fd, err = sys.RawTracepointOpen(&sys.RawTracepointOpenAttr{
ProgFd: uint32(program.FD()),
})
Expand Down Expand Up @@ -168,7 +179,7 @@ func AttachTracing(opts TracingOptions) (Link, error) {
return nil, fmt.Errorf("invalid program type %s, expected Tracing", t)
}

return attachBTFID(opts.Program, opts.AttachType)
return attachBTFID(opts.Program, opts.AttachType, opts.Cookie)
}

// AttachLSM links a Linux security module (LSM) BPF Program to a BPF
Expand All @@ -178,5 +189,5 @@ func AttachLSM(opts LSMOptions) (Link, error) {
return nil, fmt.Errorf("invalid program type %s, expected LSM", t)
}

return attachBTFID(opts.Program, ebpf.AttachLSMMac)
return attachBTFID(opts.Program, ebpf.AttachLSMMac, opts.Cookie)
}
28 changes: 12 additions & 16 deletions link/tracing_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package link

import (
"fmt"
"testing"

"github.com/cilium/ebpf"
Expand Down Expand Up @@ -85,30 +84,27 @@ func TestTracing(t *testing.T) {

test := func(
t *testing.T,
method string,
pt ebpf.ProgramType,
at ebpf.AttachType,
ato string,
atOpt ebpf.AttachType) {
prog := mustLoadProgram(t, pt, at, ato)
link, err := AttachTracing(TracingOptions{Program: prog, AttachType: atOpt})
err2 := fmt.Errorf("%s: %w", method, err)
testutils.SkipIfNotSupported(t, err2)
programType ebpf.ProgramType,
attachType ebpf.AttachType,
attachTo string,
attachTypeOpt ebpf.AttachType) {
prog := mustLoadProgram(t, programType, attachType, attachTo)

link, err := AttachTracing(TracingOptions{Program: prog, AttachType: attachTypeOpt})
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err2)
}
testLink(t, link, prog)
if err = link.Close(); err != nil {
t.Fatal(err)
}
testLink(t, link, prog)
link.Close()
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// exercise attach via BPF link
test(t, "bpf_link", tt.programType, tt.attachType, tt.attachTo, tt.attachType)
test(t, tt.programType, tt.attachType, tt.attachTo, tt.attachType)
// exercise legacy attach via RawTracepointOpen
test(t, "raw_tracepoint_open", tt.programType, tt.attachType, tt.attachTo, ebpf.AttachNone)
test(t, tt.programType, tt.attachType, tt.attachTo, ebpf.AttachNone)
})
}
}
Expand Down

0 comments on commit 9120a11

Please sign in to comment.