Skip to content

Commit

Permalink
chore(prog): align BPFProg API (#467)
Browse files Browse the repository at this point in the history
Align BPFProg API to return error accordingly:

- SetType() -> bpf_program__set_type() 
- SetExpectedAttachType() -> bpf_program__set_expected_attach_type()

Both SetProgramType() and SetAttachType() are now deprecated.
  • Loading branch information
CodePrometheus authored Dec 11, 2024
1 parent d3ea3ba commit 79f5509
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
26 changes: 22 additions & 4 deletions prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,32 @@ func (p *BPFProg) SetAttachTarget(attachProgFD int, attachFuncName string) error
return nil
}

// TODO: fix API to return error
// Deprecated: use BPFProg.SetType() instead.
func (p *BPFProg) SetProgramType(progType BPFProgType) {
C.bpf_program__set_type(p.prog, C.enum_bpf_prog_type(int(progType)))
_ = p.SetType(progType)
}

// TODO: fix API to return error
func (p *BPFProg) SetType(progType BPFProgType) error {
retC := C.bpf_program__set_type(p.prog, C.enum_bpf_prog_type(progType))
if retC < 0 {
return fmt.Errorf("failed to set prog_type %s for program %s: %w", progType.String(), p.Name(), syscall.Errno(-retC))
}

return nil
}

// Deprecated: use BPFProg.SetExpectedAttachType() instead.
func (p *BPFProg) SetAttachType(attachType BPFAttachType) {
C.bpf_program__set_expected_attach_type(p.prog, C.enum_bpf_attach_type(int(attachType)))
_ = p.SetExpectedAttachType(attachType)
}

func (p *BPFProg) SetExpectedAttachType(attachType BPFAttachType) error {
retC := C.bpf_program__set_expected_attach_type(p.prog, C.enum_bpf_attach_type(attachType))
if retC < 0 {
return fmt.Errorf("failed to set attach_type %s for program %s: %w", attachType.String(), p.Name(), syscall.Errno(-retC))
}

return nil
}

// getCgroupDirFD returns a file descriptor for a given cgroup2 directory path
Expand Down
12 changes: 10 additions & 2 deletions selftest/set-attach/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ func main() {

// eBPF program type should only be set if it differs from the desired one
// commit d6e6286a12e7 ("libbpf: disassociate section handler on explicit bpf_program__set_type() call")
// prog.SetProgramType(bpf.BPFProgTypeTracing)
prog.SetAttachType(bpf.BPFAttachTypeTraceFentry)
// err = prog.SetType(bpf.BPFProgTypeTracing)
// if err != nil {
// fmt.Fprintln(os.Stderr, err)
// os.Exit(-1)
// }
err = prog.SetExpectedAttachType(bpf.BPFAttachTypeTraceFentry)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

funcName := fmt.Sprintf("__%s_sys_mmap", ksymArch())
err = prog.SetAttachTarget(0, funcName)
Expand Down

0 comments on commit 79f5509

Please sign in to comment.