Skip to content

Commit

Permalink
Make helpers package it's own module (#246)
Browse files Browse the repository at this point in the history
* Move rwarray functionality into libbpfgo
* New helpers module
* Update usage of all modules

Signed-off-by: grantseltzer <[email protected]>
  • Loading branch information
grantseltzer authored Oct 4, 2022
1 parent a8b06c3 commit 7139cb4
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 61 deletions.
13 changes: 1 addition & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
module github.com/aquasecurity/libbpfgo

go 1.18

require (
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
go 1.19
13 changes: 0 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 3 additions & 0 deletions helpers/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/aquasecurity/libbpfgo/helpers

go 1.19
6 changes: 3 additions & 3 deletions libbpf_cb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import (

//export perfCallback
func perfCallback(ctx unsafe.Pointer, cpu C.int, data unsafe.Pointer, size C.int) {
pb := eventChannels.Get(uint(uintptr(ctx))).(*PerfBuffer)
pb := eventChannels.get(uint(uintptr(ctx))).(*PerfBuffer)
pb.eventsChan <- C.GoBytes(data, size)
}

//export perfLostCallback
func perfLostCallback(ctx unsafe.Pointer, cpu C.int, cnt C.ulonglong) {
pb := eventChannels.Get(uint(uintptr(ctx))).(*PerfBuffer)
pb := eventChannels.get(uint(uintptr(ctx))).(*PerfBuffer)
if pb.lostChan != nil {
pb.lostChan <- uint64(cnt)
}
}

//export ringbufferCallback
func ringbufferCallback(ctx unsafe.Pointer, data unsafe.Pointer, size C.int) C.int {
ch := eventChannels.Get(uint(uintptr(ctx))).(chan []byte)
ch := eventChannels.get(uint(uintptr(ctx))).(chan []byte)
ch <- C.GoBytes(data, size)
return C.int(0)
}
16 changes: 7 additions & 9 deletions libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ import (
"sync"
"syscall"
"unsafe"

"github.com/aquasecurity/libbpfgo/helpers/rwarray"
)

const (
Expand Down Expand Up @@ -1682,7 +1680,7 @@ func doAttachUprobe(prog *BPFProg, isUretprobe bool, pid int, path string, offse
return bpfLink, nil
}

var eventChannels = rwarray.NewRWArray(maxEventChannels)
var eventChannels = newRWArray(maxEventChannels)

func (m *Module) InitRingBuf(mapName string, eventsChan chan []byte) (*RingBuffer, error) {
bpfMap, err := m.GetMap(mapName)
Expand All @@ -1694,7 +1692,7 @@ func (m *Module) InitRingBuf(mapName string, eventsChan chan []byte) (*RingBuffe
return nil, fmt.Errorf("events channel can not be nil")
}

slot := eventChannels.Put(eventsChan)
slot := eventChannels.put(eventsChan)
if slot == -1 {
return nil, fmt.Errorf("max ring buffers reached")
}
Expand Down Expand Up @@ -1728,7 +1726,7 @@ func (rb *RingBuffer) Stop() {
// may have stopped at this point. Failure to drain it will
// result in a deadlock: the channel will fill up and the poll
// goroutine will block in the callback.
eventChan := eventChannels.Get(rb.slot).(chan []byte)
eventChan := eventChannels.get(rb.slot).(chan []byte)
go func() {
for range eventChan {
}
Expand All @@ -1752,7 +1750,7 @@ func (rb *RingBuffer) Close() {
}
rb.Stop()
C.ring_buffer__free(rb.rb)
eventChannels.Remove(rb.slot)
eventChannels.remove(rb.slot)
rb.closed = true
}

Expand Down Expand Up @@ -1799,14 +1797,14 @@ func (m *Module) InitPerfBuf(mapName string, eventsChan chan []byte, lostChan ch
lostChan: lostChan,
}

slot := eventChannels.Put(perfBuf)
slot := eventChannels.put(perfBuf)
if slot == -1 {
return nil, fmt.Errorf("max number of ring/perf buffers reached")
}

pb := C.init_perf_buf(bpfMap.fd, C.int(pageCnt), C.uintptr_t(slot))
if pb == nil {
eventChannels.Remove(uint(slot))
eventChannels.remove(uint(slot))
return nil, fmt.Errorf("failed to initialize perf buffer")
}

Expand Down Expand Up @@ -1863,7 +1861,7 @@ func (pb *PerfBuffer) Close() {
}
pb.Stop()
C.perf_buffer__free(pb.pb)
eventChannels.Remove(pb.slot)
eventChannels.remove(pb.slot)
pb.closed = true
}

Expand Down
20 changes: 10 additions & 10 deletions helpers/rwarray/rwArray.go → rwArray.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rwarray
package libbpfgo

import (
"sync"
Expand All @@ -9,7 +9,7 @@ type slot struct {
used bool
}

// RWArray allows for multiple concurrent readers but
// rwArray allows for multiple concurrent readers but
// only a single writer. The writers lock a mutex while the readers
// are lock free.
// It is implemented as an array of slots where each slot holds a
Expand All @@ -18,18 +18,18 @@ type slot struct {
// looking for an available slot as indicated by the in-use marker.
// While probing, it is not touching the value itself, as it's
// being read without a lock by the readers.
type RWArray struct {
type rwArray struct {
slots []slot
mux sync.Mutex
}

func NewRWArray(capacity uint) RWArray {
return RWArray{
func newRWArray(capacity uint) rwArray {
return rwArray{
slots: make([]slot, capacity),
}
}

func (a *RWArray) Put(v interface{}) int {
func (a *rwArray) put(v interface{}) int {
a.mux.Lock()
defer a.mux.Unlock()

Expand All @@ -47,7 +47,7 @@ func (a *RWArray) Put(v interface{}) int {
return -1
}

func (a *RWArray) Remove(index uint) {
func (a *rwArray) remove(index uint) {
a.mux.Lock()
defer a.mux.Unlock()

Expand All @@ -59,17 +59,17 @@ func (a *RWArray) Remove(index uint) {
a.slots[index].used = false
}

func (a *RWArray) Get(index uint) interface{} {
func (a *rwArray) get(index uint) interface{} {
if int(index) >= len(a.slots) {
return nil
}

// N.B. If slot[index].used == false, this is technically
// a race since Put() might be putting the value in there
// a race since put() might be putting the value in there
// at the same time.
return a.slots[index].value
}

func (a *RWArray) Capacity() uint {
func (a *rwArray) capacity() uint {
return uint(len(a.slots))
}
26 changes: 13 additions & 13 deletions helpers/rwarray/rwArray_test.go → rwArray_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package rwarray
package libbpfgo

import (
"sync"
"testing"
)

func TestRWArrayWrite(t *testing.T) {
a := NewRWArray(1024)
a := newRWArray(1024)

last := 0

for i := 0; i < 1000; i++ {
slot1 := a.Put(&i)
slot1 := a.put(&i)
if slot1 < 0 {
t.Errorf("failed to put")
}
Expand All @@ -20,7 +20,7 @@ func TestRWArrayWrite(t *testing.T) {
t.Fatalf("Put didn't occupy first available; expected=%v, got=%v", last, slot1)
}

slot2 := a.Put(&i)
slot2 := a.put(&i)
if slot2 < 0 {
t.Fatalf("failed to put")
}
Expand All @@ -36,16 +36,16 @@ func TestRWArrayWrite(t *testing.T) {
}

func TestRWArrayExhaust(t *testing.T) {
a := NewRWArray(1024)
a := newRWArray(1024)

last := -1

for {
v := 123
slot := a.Put(&v)
slot := a.put(&v)

if slot < 0 {
if uint(last) != a.Capacity()-1 {
if uint(last) != a.capacity()-1 {
t.Fatalf("failed to put, last=%v", last)
}
return
Expand All @@ -60,11 +60,11 @@ func TestRWArrayExhaust(t *testing.T) {
}

func TestRWArrayRead(t *testing.T) {
a := NewRWArray(1024)
a := newRWArray(1024)

for i := 0; i < 1000; i++ {
v := i
slot := a.Put(&v)
slot := a.put(&v)
if slot != i {
t.Errorf("Put returned non-sequential slot; expected=%v, got=%v", i, slot)
}
Expand All @@ -80,16 +80,16 @@ func TestRWArrayRead(t *testing.T) {

// Designed to be run under race detector
func TestRWArrayConcurrent(t *testing.T) {
a := NewRWArray(16 * 1024)
capacity := a.Capacity()
a := newRWArray(16 * 1024)
capacity := a.capacity()

stop := make(chan struct{})
wg := sync.WaitGroup{}

// Populate every other slot
v := 123
for i := uint(0); i < capacity; i++ {
a.Put(&v)
a.put(&v)
}
for i := uint(1); i < capacity; i += 2 {
a.Remove(i)
Expand All @@ -99,7 +99,7 @@ func TestRWArrayConcurrent(t *testing.T) {
for {
// fill the holes
for i := uint(0); i < capacity/2; i++ {
a.Put(&v)
a.put(&v)
}

// make some holes
Expand Down
5 changes: 4 additions & 1 deletion selftest/tracing/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ go 1.18

require github.com/aquasecurity/libbpfgo v0.2.1-libbpf-0.4.0

require golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
require (
github.com/aquasecurity/libbpfgo/helpers v0.0.0-20220919184217-8ef1425cccdf // indirect
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
)

replace github.com/aquasecurity/libbpfgo => ../../
2 changes: 2 additions & 0 deletions selftest/tracing/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/aquasecurity/libbpfgo/helpers v0.0.0-20220919184217-8ef1425cccdf h1:HNzXWDHFjYZ8jP6vCHW1R8NLblm7vKIM3Y0y+r4OX9E=
github.com/aquasecurity/libbpfgo/helpers v0.0.0-20220919184217-8ef1425cccdf/go.mod h1:oCaNN2RsEVEErc1PyvJn1ttVoxAKAGRKPrHY9n3QqS0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down

0 comments on commit 7139cb4

Please sign in to comment.