Skip to content

Commit

Permalink
Add helper functions for checking if map and program types are supported
Browse files Browse the repository at this point in the history
Signed-off-by: grantseltzer <grantseltzer@gmail.com>
  • Loading branch information
grantseltzer committed May 4, 2022
1 parent 64458ba commit d29f9c2
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 1 deletion.
18 changes: 17 additions & 1 deletion libbpfgo.go
Expand Up @@ -787,7 +787,7 @@ func (b *BPFMap) DeleteKeyBatch(keys unsafe.Pointer, count uint32) error {
ElemFlags: C.BPF_ANY,
Flags: C.BPF_ANY,
}

errC := C.bpf_map_delete_batch(b.fd, keys, &countC, bpfMapBatchOptsToC(opts))
if errC != 0 {
sc := syscall.Errno(-errC)
Expand Down Expand Up @@ -1641,3 +1641,19 @@ func (hook *TcHook) Query(tcOpts *TcOpts) error {

return nil
}

func BPFMapTypeIsSupported(mapType MapType) (bool, error) {
cSupported := C.libbpf_probe_bpf_map_type(C.enum_bpf_map_type(int(mapType)), nil)
if cSupported < 1 {
return false, syscall.Errno(-cSupported)
}
return cSupported == 1, nil
}

func BPFProgramTypeIsSupported(progType BPFProgType) (bool, error) {
cSupported := C.libbpf_probe_bpf_prog_type(C.enum_bpf_prog_type(int(progType)), nil)
if cSupported < 1 {
return false, syscall.Errno(-cSupported)
}
return cSupported == 1, nil
}
1 change: 1 addition & 0 deletions selftest/probe-features/Makefile
7 changes: 7 additions & 0 deletions selftest/probe-features/go.mod
@@ -0,0 +1,7 @@
module github.com/aquasecurity/libbpfgo/selftest/map-pin-info

go 1.16

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

replace github.com/aquasecurity/libbpfgo => ../../
11 changes: 11 additions & 0 deletions selftest/probe-features/go.sum
@@ -0,0 +1,11 @@
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/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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=
13 changes: 13 additions & 0 deletions selftest/probe-features/main.bpf.c
@@ -0,0 +1,13 @@
//+build ignore
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>


SEC("kprobe/sys_mmap")
int kprobe__sys_mmap(struct pt_regs *ctx)
{
bpf_printk("Yankees will win the 2022 world series");
return 0;
}

char LICENSE[] SEC("license") = "Dual BSD/GPL";
66 changes: 66 additions & 0 deletions selftest/probe-features/main.go
@@ -0,0 +1,66 @@
package main

import "C"

import (
"fmt"
"log"
"os"

"github.com/aquasecurity/libbpfgo"
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()

kprobeProg, err := bpfModule.GetProgram("kprobe__sys_mmap")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

err = kprobeProg.SetAutoload(false)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

isSupported, err := bpf.BPFProgramTypeIsSupported(libbpfgo.BPFProgTypeKprobe)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

if isSupported {
err = kprobeProg.SetAutoload(true)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
}

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

isSupported, err = bpf.BPFMapTypeIsSupported(libbpfgo.MapTypeHash)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

if isSupported {
_, err = libbpfgo.CreateMap(libbpfgo.MapTypeHash, "foobar", 4, 4, 420, nil)
if err != nil {
log.Fatal(err)
}
}
}
1 change: 1 addition & 0 deletions selftest/probe-features/run.sh

0 comments on commit d29f9c2

Please sign in to comment.