Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add AttachProgs/DetachProgs api for bpf object #409

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,65 @@ func (m *Module) Iterator() *BPFObjectIterator {
prevMap: nil,
}
}

func (m *Module) linkExist(prog *BPFProg) bool {
for _, link := range m.links {
if link.prog.Name() == prog.Name() {
return true
}
}

return false
}

// AttachPrograms attach all loaded and no attached progs once like bpf_object__attach_skeleton
func (m *Module) AttachPrograms() error {
iters := m.Iterator()
for {
prog := iters.NextProgram()
if prog == nil {
break
}

if !prog.Autoload() || !prog.Autoattach() {
continue
}
// if link already exist (is attached), skip it
if m.linkExist(prog) {
continue
}

link, err := prog.AttachGeneric()
geyslan marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

m.links = append(m.links, link)
}

return nil
}

// DetachPrograms detach all attached progs once like bpf_object__detach_skeleton
func (m *Module) DetachPrograms() error {
errInfo := make(map[string]error)

for _, link := range m.links {
err := link.Destroy()
if err != nil {
errInfo[link.prog.Name()] = err
}
}
m.links = nil

if len(errInfo) > 0 {
var str string
for name, err := range errInfo {
str += fmt.Sprintf(" [prog:%s,err:%s]", name, err)
}

return fmt.Errorf("link destroy failed:%s", str)
}

return nil
}
4 changes: 4 additions & 0 deletions prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (p *BPFProg) SetAutoload(autoload bool) error {
return nil
}

func (p *BPFProg) Autoload() bool {
return bool(C.bpf_program__autoload(p.prog))
}

func (p *BPFProg) SetAutoattach(autoload bool) {
C.bpf_program__set_autoattach(p.prog, C.bool(autoload))
}
Expand Down
1 change: 1 addition & 0 deletions selftest/module-attach-detach/Makefile
14 changes: 14 additions & 0 deletions selftest/module-attach-detach/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/aquasecurity/libbpfgo/selftest/module-attach-detach

go 1.18

require (
github.com/aquasecurity/libbpfgo v0.4.7-libbpf-1.2.0-b2e29a1
github.com/aquasecurity/libbpfgo/helpers v0.4.5
)

require golang.org/x/sys v0.7.0 // indirect

replace github.com/aquasecurity/libbpfgo => ../../

replace github.com/aquasecurity/libbpfgo/helpers => ../../helpers
6 changes: 6 additions & 0 deletions selftest/module-attach-detach/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
24 changes: 24 additions & 0 deletions selftest/module-attach-detach/main.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//+build ignore

#include <vmlinux.h>

#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#ifdef __TARGET_ARCH_amd64
SEC("fentry/__x64_sys_mmap")
#elif defined(__TARGET_ARCH_arm64)
SEC("fentry/__arm64_sys_mmap")
#endif
int sys_mmap(struct pt_regs *ctx)
{
return 0;
}

SEC("kprobe/do_sys_open")
int kprobe__sys_open(struct pt_regs *ctx)
{
return 0;
}

char LICENSE[] SEC("license") = "GPL";
40 changes: 40 additions & 0 deletions selftest/module-attach-detach/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import "C"

import (
"os"

"fmt"

bpf "github.com/aquasecurity/libbpfgo"
)

func main() {
bpfModule, err := bpf.NewModuleFromFile("main.bpf.o")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
defer bpfModule.Close()

err = bpfModule.BPFLoadObject()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

// attach all programs
err = bpfModule.AttachPrograms()
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("attach programs failed: %s", err))
os.Exit(-1)
}

// detach all programs
err = bpfModule.DetachPrograms()
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("detach programs failed: %s", err))
os.Exit(-1)
}
}
1 change: 1 addition & 0 deletions selftest/module-attach-detach/run.sh
9 changes: 9 additions & 0 deletions selftest/probe-features/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ func main() {
}
}

// Test auto load result
autoLoadOrig := kprobeProg.Autoload()
kprobeProg.SetAutoload((!autoLoadOrig))
if kprobeProg.Autoload() == autoLoadOrig {
fmt.Println(os.Stderr, "auto load result wrong")
os.Exit(-1)
}
kprobeProg.SetAutoload((autoLoadOrig))

err = bpfModule.BPFLoadObject()
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down