diff --git a/go.mod b/go.mod index ddf5365f..09fe5a0b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e432b16b..e69de29b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/helpers/go.mod b/helpers/go.mod new file mode 100644 index 00000000..7d82139b --- /dev/null +++ b/helpers/go.mod @@ -0,0 +1,3 @@ +module github.com/aquasecurity/libbpfgo/helpers + +go 1.19 diff --git a/libbpf_cb.go b/libbpf_cb.go index fc75c9d5..b118ca7f 100644 --- a/libbpf_cb.go +++ b/libbpf_cb.go @@ -10,13 +10,13 @@ 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) } @@ -24,7 +24,7 @@ func perfLostCallback(ctx unsafe.Pointer, cpu C.int, cnt C.ulonglong) { //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) } diff --git a/libbpfgo.go b/libbpfgo.go index 757ccc61..398a077d 100644 --- a/libbpfgo.go +++ b/libbpfgo.go @@ -155,8 +155,6 @@ import ( "sync" "syscall" "unsafe" - - "github.com/aquasecurity/libbpfgo/helpers/rwarray" ) const ( @@ -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) @@ -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") } @@ -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 { } @@ -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 } @@ -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") } @@ -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 } diff --git a/helpers/rwarray/rwArray.go b/rwArray.go similarity index 75% rename from helpers/rwarray/rwArray.go rename to rwArray.go index 678aa8dc..211a8ca2 100644 --- a/helpers/rwarray/rwArray.go +++ b/rwArray.go @@ -1,4 +1,4 @@ -package rwarray +package libbpfgo import ( "sync" @@ -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 @@ -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() @@ -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() @@ -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)) } diff --git a/helpers/rwarray/rwArray_test.go b/rwArray_test.go similarity index 86% rename from helpers/rwarray/rwArray_test.go rename to rwArray_test.go index a7b6ab6a..bef3b82a 100644 --- a/helpers/rwarray/rwArray_test.go +++ b/rwArray_test.go @@ -1,4 +1,4 @@ -package rwarray +package libbpfgo import ( "sync" @@ -6,12 +6,12 @@ import ( ) 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") } @@ -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") } @@ -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 @@ -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) } @@ -80,8 +80,8 @@ 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{} @@ -89,7 +89,7 @@ func TestRWArrayConcurrent(t *testing.T) { // 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) @@ -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 diff --git a/selftest/tracing/go.mod b/selftest/tracing/go.mod index 29fc9756..80be14b6 100644 --- a/selftest/tracing/go.mod +++ b/selftest/tracing/go.mod @@ -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 => ../../ diff --git a/selftest/tracing/go.sum b/selftest/tracing/go.sum index b0f21d61..1ec00734 100644 --- a/selftest/tracing/go.sum +++ b/selftest/tracing/go.sum @@ -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=