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

UTs for cache and kprobes #28

Merged
merged 2 commits into from
Aug 22, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@ EBPF_TEST_LIC_BINARY := test-data/test_license.bpf.elf
EBPF_TEST_INV_MAP_SOURCE := test-data/invalid_map.bpf.c
EBPF_TEST_INV_MAP_BINARY := test-data/invalid_map.bpf.elf
EBPF_TEST_RECOVERY_SOURCE := test-data/recoverydata.bpf.c
EBPF_TEST_RECOVERY_BINARY := test-data/recoverydata.bpf.elf
EBPF_TEST_RECOVERY_BINARY := test-data/recoverydata.bpf.elf
EBPF_TEST_KPROBE_SOURCE := test-data/test-kprobe.bpf.c
EBPF_TEST_KPROBE_BINARY := test-data/test-kprobe.bpf.elf
build-bpf: ## Build BPF
$(CLANG) $(CLANG_INCLUDE) -g -O2 -Wall -fpie -target bpf -DCORE -D__BPF_TRACING__ -march=bpf -D__TARGET_ARCH_$(ARCH) -c $(EBPF_SOURCE) -o $(EBPF_BINARY)
$(CLANG) $(CLANG_INCLUDE) -g -O2 -Wall -fpie -target bpf -DCORE -D__BPF_TRACING__ -march=bpf -D__TARGET_ARCH_$(ARCH) -c $(EBPF_TEST_SOURCE) -o $(EBPF_TEST_BINARY)
$(CLANG) $(CLANG_INCLUDE) -g -O2 -Wall -fpie -target bpf -DCORE -D__BPF_TRACING__ -march=bpf -D__TARGET_ARCH_$(ARCH) -c $(EBPF_TEST_MAP_SOURCE) -o $(EBPF_TEST_MAP_BINARY)
$(CLANG) $(CLANG_INCLUDE) -g -O2 -Wall -fpie -target bpf -DCORE -D__BPF_TRACING__ -march=bpf -D__TARGET_ARCH_$(ARCH) -c $(EBPF_TEST_LIC_SOURCE) -o $(EBPF_TEST_LIC_BINARY)
$(CLANG) $(CLANG_INCLUDE) -g -O2 -Wall -fpie -target bpf -DCORE -D__BPF_TRACING__ -march=bpf -D__TARGET_ARCH_$(ARCH) -c $(EBPF_TEST_INV_MAP_SOURCE) -o $(EBPF_TEST_INV_MAP_BINARY)
$(CLANG) $(CLANG_INCLUDE) -g -O2 -Wall -fpie -target bpf -DCORE -D__BPF_TRACING__ -march=bpf -D__TARGET_ARCH_$(ARCH) -c $(EBPF_TEST_RECOVERY_SOURCE) -o $(EBPF_TEST_RECOVERY_BINARY)
$(CLANG) $(CLANG_INCLUDE) -g -O2 -Wall -fpie -target bpf -DCORE -D__BPF_TRACING__ -march=bpf -D__TARGET_ARCH_$(ARCH) -c $(EBPF_TEST_KPROBE_SOURCE) -o $(EBPF_TEST_KPROBE_BINARY)

vmlinuxh:
bpftool btf dump file /sys/kernel/btf/vmlinux format c > $(abspath ./test-data/vmlinux.h)
Expand Down
14 changes: 10 additions & 4 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ import (
"github.com/aws/aws-ebpf-sdk-go/pkg/logger"
)

var sdkCache *GlobalCacheMap
var sdkCache GlobalCache
var log = logger.Get()

// Adding a struct if in future we need a cleanup routine
type CacheValue struct {
mapFD int
}

type GlobalCache interface {
Set(key string, value int)
Get(key string) (int, bool)
Delete(key string)
}

type GlobalCacheMap struct {
globalMap *sync.Map
}
Expand All @@ -49,16 +55,16 @@ func (c *GlobalCacheMap) Delete(key string) {
c.globalMap.Delete(key)
}

func Get() *GlobalCacheMap {
func Get() GlobalCache {
if sdkCache == nil {
sdkCache = New()
log.Info("Initialized new SDK cache as an existing instance was not found")
}
return sdkCache
}

func New() *GlobalCacheMap {
sdkCache := &GlobalCacheMap{
func New() GlobalCache {
sdkCache = &GlobalCacheMap{
globalMap: new(sync.Map),
}
return sdkCache
Expand Down
64 changes: 64 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
//limitations under the License.

package cache

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCache(t *testing.T) {
SdkCache := New()

value, _ := SdkCache.Get("invalidKey")
assert.Equal(t, -1, value)

SdkCache.Set("ingress-map", 10)

value, _ = SdkCache.Get("ingress-map")
assert.Equal(t, 10, value)

SdkCache.Set("ingress-map", 11)

value, _ = SdkCache.Get("ingress-map")
assert.Equal(t, 11, value)

SdkCache.Set("egress-map", 12)

SdkCache.Delete("egress-map")

value, _ = SdkCache.Get("egress-map")
assert.Equal(t, -1, value)

tempSdkCache := Get()
value, _ = tempSdkCache.Get("ingress-map")
assert.Equal(t, 11, value)

}

func TestCacheGetSameInstance(t *testing.T) {
cache1 := Get()
cache2 := Get()

assert.True(t, cache1 == cache2)
}

func TestCacheNewAndGetSameInstance(t *testing.T) {
cache1 := New()
cache2 := Get()

assert.True(t, cache1 == cache2)
}
112 changes: 85 additions & 27 deletions pkg/kprobe/kprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package kprobe

import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
Expand All @@ -29,22 +30,55 @@ import (

var log = logger.Get()

type BpfKprobe interface {
KprobeAttach() error
KretprobeAttach() error
KprobeDetach() error
KretprobeDetach() error
}

var _ BpfKprobe = &bpfKprobe{}

type bpfKprobe struct {
progFD int
eventName string
funcName string
perfFD int
}

func New(fd int, eName, fName string) BpfKprobe {
return &bpfKprobe{
progFD: fd,
eventName: eName,
funcName: fName,
}

}

func (k *bpfKprobe) SetPerfFD(perfFD int) {
k.perfFD = perfFD
}

func (k *bpfKprobe) GetPerfFD() int {
return k.perfFD
}

/*
p[:[GRP/]EVENT] [MOD:]SYM[+offs]|MEMADDR [FETCHARGS] : Set a probe
r[MAXACTIVE][:[GRP/]EVENT] [MOD:]SYM[+0] [FETCHARGS] : Set a return probe
-:[GRP/]EVENT
*/
func KprobeAttach(progFD int, eventName string, funcName string) error {
func (k *bpfKprobe) KprobeAttach() error {

if progFD <= 0 {
log.Errorf("invalid BPF prog FD %d", progFD)
return fmt.Errorf("Invalid BPF prog FD %d", progFD)
if k.progFD <= 0 {
log.Errorf("invalid BPF prog FD %d", k.progFD)
return fmt.Errorf("Invalid BPF prog FD %d", k.progFD)

}

// if event is nil, we pick funcName
if len(eventName) == 0 {
eventName = funcName
if len(k.eventName) == 0 {
k.eventName = k.funcName
}

// Register the Kprobe event
Expand All @@ -54,15 +88,15 @@ func KprobeAttach(progFD int, eventName string, funcName string) error {
return err
}

eventString := fmt.Sprintf("p:kprobes/%s %s", eventName, funcName)
eventString := fmt.Sprintf("p:kprobes/%s %s", k.eventName, k.funcName)
_, err = file.WriteString(eventString)
if err != nil {
log.Errorf("error writing to kprobe_events file: %v", err)
return err
}

//Get the Kprobe ID
kprobeIDpath := fmt.Sprintf("%s/%s/id", constdef.KPROBE_SYS_DEBUG, eventName)
kprobeIDpath := fmt.Sprintf("%s/%s/id", constdef.KPROBE_SYS_DEBUG, k.eventName)
data, err := os.ReadFile(kprobeIDpath)
if err != nil {
log.Errorf("unable to read the kprobeID: %v", err)
Expand Down Expand Up @@ -91,9 +125,9 @@ func KprobeAttach(progFD int, eventName string, funcName string) error {
return err
}

log.Infof("Attach bpf program to perf event Prog FD %d Event FD %d", progFD, fd)
log.Infof("Attach bpf program to perf event Prog FD %d Event FD %d", k.progFD, fd)

if _, _, err := unix.Syscall(unix.SYS_IOCTL, uintptr(int(fd)), uintptr(uint(unix.PERF_EVENT_IOC_SET_BPF)), uintptr(progFD)); err != 0 {
if _, _, err := unix.Syscall(unix.SYS_IOCTL, uintptr(int(fd)), uintptr(uint(unix.PERF_EVENT_IOC_SET_BPF)), uintptr(k.progFD)); err != 0 {
log.Errorf("error attaching bpf program to perf event: %v", err)
return err
}
Expand All @@ -103,7 +137,10 @@ func KprobeAttach(progFD int, eventName string, funcName string) error {
return err
}

k.SetPerfFD(fd)

log.Infof("KPROBE Attach done!!! %d", fd)

return nil

}
Expand All @@ -118,16 +155,16 @@ MAXACTIVE : Maximum number of instances of the specified function that
can be probed simultaneously, or 0 for the default value
as defined in Documentation/kprobes.txt section 1.3.1.
*/
func KretprobeAttach(progFD int, eventName string, funcName string) error {
func (k *bpfKprobe) KretprobeAttach() error {

if progFD <= 0 {
log.Infof("invalid BPF prog FD %d", progFD)
return fmt.Errorf("Invalid BPF prog FD %d", progFD)
if k.progFD <= 0 {
log.Infof("invalid BPF prog FD %d", k.progFD)
return fmt.Errorf("Invalid BPF prog FD %d", k.progFD)

}
// if event is nil, we pick funcName
if len(eventName) == 0 {
eventName = funcName
if len(k.eventName) == 0 {
k.eventName = k.funcName
}

// Register the Kprobe event
Expand All @@ -137,15 +174,15 @@ func KretprobeAttach(progFD int, eventName string, funcName string) error {
return err
}

eventString := fmt.Sprintf("r4096:kretprobes/%s %s", eventName, funcName)
eventString := fmt.Sprintf("r4096:kretprobes/%s %s", k.eventName, k.funcName)
_, err = file.WriteString(eventString)
if err != nil {
log.Errorf("error writing to kprobe_events file: %v", err)
return err
}

//Get the Kprobe ID
kprobeIDpath := fmt.Sprintf("%s/%s/id", constdef.KRETPROBE_SYS_DEBUG, eventName)
kprobeIDpath := fmt.Sprintf("%s/%s/id", constdef.KRETPROBE_SYS_DEBUG, k.eventName)
data, err := os.ReadFile(kprobeIDpath)
if err != nil {
log.Errorf("unable to read the kretprobeID: %v", err)
Expand Down Expand Up @@ -174,9 +211,9 @@ func KretprobeAttach(progFD int, eventName string, funcName string) error {
return err
}

log.Infof("Attach bpf program to perf event Prog FD %d Event FD %d", progFD, fd)
log.Infof("Attach bpf program to perf event Prog FD %d Event FD %d", k.progFD, fd)

if _, _, err := unix.Syscall(unix.SYS_IOCTL, uintptr(int(fd)), uintptr(uint(unix.PERF_EVENT_IOC_SET_BPF)), uintptr(progFD)); err != 0 {
if _, _, err := unix.Syscall(unix.SYS_IOCTL, uintptr(int(fd)), uintptr(uint(unix.PERF_EVENT_IOC_SET_BPF)), uintptr(k.progFD)); err != 0 {
log.Errorf("error attaching bpf program to perf event: %v", err)
return err
}
Expand All @@ -186,13 +223,34 @@ func KretprobeAttach(progFD int, eventName string, funcName string) error {
return err
}

k.SetPerfFD(fd)

log.Infof("KRETPROBE Attach done!!! %d", fd)
return nil

}

func probeDetach(eventName string) error {
func probeDetach(eventName string, perfFD int, kretProbe bool) error {
log.Infof("Calling Detach on %s", eventName)

if _, _, err := unix.Syscall(unix.SYS_IOCTL, uintptr(int(perfFD)), uintptr(uint(unix.PERF_EVENT_IOC_DISABLE)), 0); err != 0 {
log.Errorf("error enabling perf event: %v", err)
return err
}
unix.Close(perfFD)

eventEnable := constdef.KPROBE_SYS_DEBUG + "/" + eventName + "/enable"
if kretProbe {
eventEnable = constdef.KRETPROBE_SYS_DEBUG + "/" + eventName + "/enable"
}

setEnable := []byte("0")

err := ioutil.WriteFile(eventEnable, setEnable, os.ModePerm)
if err != nil {
return err
}

file, err := os.OpenFile(constdef.KPROBE_SYS_EVENTS, os.O_APPEND|os.O_WRONLY, 0)
if err != nil {
log.Errorf("cannot open probe events: %v", err)
Expand All @@ -214,12 +272,12 @@ func probeDetach(eventName string) error {
return nil
}

func KprobeDetach(eventName string) error {
log.Infof("Calling Kprobe Detach on %s", eventName)
return probeDetach(eventName)
func (k *bpfKprobe) KprobeDetach() error {
log.Infof("Calling Kprobe Detach on %s", k.eventName)
return probeDetach(k.eventName, k.perfFD, false)
}

func KretprobeDetach(eventName string) error {
log.Infof("Calling Kretprobe Detach on %s", eventName)
return probeDetach(eventName)
func (k *bpfKprobe) KretprobeDetach() error {
log.Infof("Calling Kretprobe Detach on %s", k.eventName)
return probeDetach(k.eventName, k.perfFD, true)
}
Loading
Loading