Skip to content

Commit

Permalink
fix: ci failed & update API based on reviewer's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ianchen0119 committed Feb 13, 2025
1 parent 8eac639 commit cdb7e95
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
4 changes: 4 additions & 0 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
CgroupLegacy
Netns
Iter
StructOps
)

//
Expand All @@ -47,6 +48,7 @@ type bpfLinkLegacy struct {
type BPFLink struct {
link *C.struct_bpf_link
prog *BPFProg
m *BPFMap
linkType LinkType
eventName string
legacy *bpfLinkLegacy // if set, this is a fake BPFLink
Expand All @@ -59,6 +61,8 @@ func (l *BPFLink) DestroyLegacy(linkType LinkType) error {
l.legacy.cgroupDir,
l.legacy.attachType,
)
// case StructOps:
// return l.m.DetachStructOps(l.link)
}

return fmt.Errorf("unable to destroy legacy link")
Expand Down
24 changes: 17 additions & 7 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package libbpfgo
import "C"

import (
"errors"
"fmt"
"syscall"
"unsafe"
Expand Down Expand Up @@ -74,17 +73,28 @@ func (m *BPFMap) ReuseFD(fd int) error {
return nil
}

func (m *BPFMap) AttachStructOps() error {
if m.Type().String() != MapTypeStructOps.String() {
return errors.New("Map type should be BPF_MAP_TYPE_STRUCT_OPS")
}
func (m *BPFMap) AttachStructOps() (*BPFLink, error) {
linkC, errno := C.bpf_map__attach_struct_ops(m.bpfMap)
if linkC == nil {
return fmt.Errorf("Map attach failed: %v", &errno)
return nil, fmt.Errorf("failed to attach struct_ops: %w", errno)
}
return nil
return &BPFLink{
link: linkC,
m: m,
linkType: StructOps,
eventName: fmt.Sprintf("structOps-%s", m.Name()),
}, nil
}

// func (m *BPFMap) DetachStructOps(l *C.struct_bpf_link) error {
// retC := C.bpf_link__detach_struct_ops(l)
// if retC < 0 {
// return fmt.Errorf("failed to detach struct_ops: %w", syscall.Errno(-retC))
// }

// return nil
// }

func (m *BPFMap) Name() string {
return C.GoString(C.bpf_map__name(m.bpfMap))
}
Expand Down
2 changes: 1 addition & 1 deletion selftest/struct-ops/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/aquasecurity/libbpfgo/selftest/perfbuffers
module github.com/aquasecurity/libbpfgo/selftest/struct-ops

go 1.21

Expand Down
16 changes: 12 additions & 4 deletions selftest/struct-ops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,25 @@ func main() {

m := bpfModule

var afterFunc func()

iters := m.Iterator()
for {
m := iters.NextMap()
if m == nil {
break
}
if m.Type().String() == "BPF_MAP_TYPE_STRUCT_OPS" {
if err := m.AttachStructOps(); err != nil {
if link, err := m.AttachStructOps(); err != nil {
log.Printf("error: %v", err)
os.Exit(-1)
} else {
afterFunc = func() {
if err := link.Destroy(); err != nil {
log.Printf("error: %v", err)
os.Exit(-1)
}
}
}
}
}
Expand Down Expand Up @@ -82,6 +91,7 @@ func main() {
time.Sleep(3 * time.Second)
cancel()
wg.Wait()
afterFunc()
log.Println("scheduler exit")
os.Exit(0)
}
Expand All @@ -91,17 +101,15 @@ func getStat(m *bpf.BPFMap) []uint64 {
cpuNum, err := bpf.NumPossibleCPUs()
if err != nil {
log.Fatal(err)
os.Exit(-1)
}
var cnts [][]uint64 = make([][]uint64, 2)
cnts := make([][]uint64, 2)
cnts[0] = make([]uint64, cpuNum)
cnts[1] = make([]uint64, cpuNum)
stats := []uint64{0, 0}
for i := 0; i < 2; i++ {
v, err := m.GetValue(unsafe.Pointer(&i))
if err != nil {
log.Fatal(err)
os.Exit(-1)
}
for cpu := 0; cpu < cpuNum; cpu++ {
n := v[cpu*8 : cpu*8+8]
Expand Down

0 comments on commit cdb7e95

Please sign in to comment.