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

Implement API for checking if map and program types are supported #164

Merged
merged 3 commits into from May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 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 @@ -954,6 +954,12 @@ func (p *BPFProg) GetName() string {
return p.name
}

func (p *BPFProg) GetSectionName() string {
cs := C.bpf_program__section_name(p.prog)
gs := C.GoString(cs)
return gs
}

func (p *BPFProg) GetPinPath() string {
return p.pinnedPath
}
Expand Down Expand Up @@ -1641,3 +1647,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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: unaligned.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

important: needs a baseball fan review, let's block this until then.

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