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

Add ringbuf support tests #478

Merged
merged 4 commits into from
Feb 12, 2025
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
1 change: 1 addition & 0 deletions .github/actions/build-dependencies/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ runs:
sudo apt-get install --yes pkgconf
sudo apt-get install --yes llvm-14 clang-14 clang-format-14
sudo apt-get install --yes libelf-dev libzstd-dev zlib1g-dev
sudo apt-get install --yes virtme-ng
for tool in "clang" "clang-format" "llc" "llvm-strip"
do
sudo rm -f /usr/bin/$tool
Expand Down
6 changes: 6 additions & 0 deletions map-common.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const (
MapTypeInodeStorage MapType = C.BPF_MAP_TYPE_INODE_STORAGE
MapTypeTaskStorage MapType = C.BPF_MAP_TYPE_TASK_STORAGE
MapTypeBloomFilter MapType = C.BPF_MAP_TYPE_BLOOM_FILTER
MapTypeUserRingbuf MapType = C.BPF_MAP_TYPE_USER_RINGBUF
MapTypeCgrpStorage MapType = C.BPF_MAP_TYPE_CGRP_STORAGE
MapTypeArena MapType = C.BPF_MAP_TYPE_ARENA
)

var mapTypeToString = map[MapType]string{
Expand Down Expand Up @@ -83,6 +86,9 @@ var mapTypeToString = map[MapType]string{
MapTypeInodeStorage: "BPF_MAP_TYPE_INODE_STORAGE",
MapTypeTaskStorage: "BPF_MAP_TYPE_TASK_STORAGE",
MapTypeBloomFilter: "BPF_MAP_TYPE_BLOOM_FILTER",
MapTypeUserRingbuf: "BPF_MAP_TYPE_USER_RINGBUF",
MapTypeCgrpStorage: "BPF_MAP_TYPE_CGRP_STORAGE",
MapTypeArena: "BPF_MAP_TYPE_ARENA",
}

func (t MapType) String() string {
Expand Down
34 changes: 34 additions & 0 deletions prog-common.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ const (
BPFProgTypeLsm BPFProgType = C.BPF_PROG_TYPE_LSM
BPFProgTypeSkLookup BPFProgType = C.BPF_PROG_TYPE_SK_LOOKUP
BPFProgTypeSyscall BPFProgType = C.BPF_PROG_TYPE_SYSCALL
BPFProgTypeNetfilter BPFProgType = C.BPF_PROG_TYPE_NETFILTER
)

// Deprecated: Convert type directly instead.
Expand Down Expand Up @@ -743,6 +744,7 @@ var bpfProgTypeToString = map[BPFProgType]string{
BPFProgTypeLsm: "BPF_PROG_TYPE_LSM",
BPFProgTypeSkLookup: "BPF_PROG_TYPE_SK_LOOKUP",
BPFProgTypeSyscall: "BPF_PROG_TYPE_SYSCALL",
BPFProgTypeNetfilter: "BPF_PROG_TYPE_NETFILTER",
}

func (t BPFProgType) String() string {
Expand Down Expand Up @@ -809,6 +811,20 @@ const (
BPFAttachTypeSKReusePortSelectorMigrate BPFAttachType = C.BPF_SK_REUSEPORT_SELECT_OR_MIGRATE
BPFAttachTypePerfEvent BPFAttachType = C.BPF_PERF_EVENT
BPFAttachTypeTraceKprobeMulti BPFAttachType = C.BPF_TRACE_KPROBE_MULTI
BPFAttachTypeLSMCgroup BPFAttachType = C.BPF_LSM_CGROUP
BPFAttachTypeStructOps BPFAttachType = C.BPF_STRUCT_OPS
BPFAttachTypeNetfilter BPFAttachType = C.BPF_NETFILTER
BPFAttachTypeTCXIngress BPFAttachType = C.BPF_TCX_INGRESS
BPFAttachTypeTCXEgress BPFAttachType = C.BPF_TCX_EGRESS
BPFAttachTypeTraceUprobeMulti BPFAttachType = C.BPF_TRACE_UPROBE_MULTI
BPFAttachTypeCgroupUnixConnect BPFAttachType = C.BPF_CGROUP_UNIX_CONNECT
BPFAttachTypeCgroupUnixSendMsg BPFAttachType = C.BPF_CGROUP_UNIX_SENDMSG
BPFAttachTypeCgroupUnixRecvMsg BPFAttachType = C.BPF_CGROUP_UNIX_RECVMSG
BPFAttachTypeCgroupUnixGetPeerName BPFAttachType = C.BPF_CGROUP_UNIX_GETPEERNAME
BPFAttachTypeCgroupUnixGetSockName BPFAttachType = C.BPF_CGROUP_UNIX_GETSOCKNAME
BPFAttachTypeNetkitPrimary BPFAttachType = C.BPF_NETKIT_PRIMARY
BPFAttachTypeNetkitPeer BPFAttachType = C.BPF_NETKIT_PEER
BPFAttachTypeTraceKprobeSession BPFAttachType = C.BPF_TRACE_KPROBE_SESSION
)

var bpfAttachTypeToString = map[BPFAttachType]string{
Expand Down Expand Up @@ -855,6 +871,20 @@ var bpfAttachTypeToString = map[BPFAttachType]string{
BPFAttachTypeSKReusePortSelectorMigrate: "BPF_SK_REUSEPORT_SELECT_OR_MIGRATE",
BPFAttachTypePerfEvent: "BPF_PERF_EVENT",
BPFAttachTypeTraceKprobeMulti: "BPF_TRACE_KPROBE_MULTI",
BPFAttachTypeLSMCgroup: "BPF_LSM_CGROUP",
BPFAttachTypeStructOps: "BPF_STRUCT_OPS",
BPFAttachTypeNetfilter: "BPF_NETFILTER",
BPFAttachTypeTCXIngress: "BPF_TCX_INGRESS",
BPFAttachTypeTCXEgress: "BPF_TCX_EGRESS",
BPFAttachTypeTraceUprobeMulti: "BPF_TRACE_UPROBE_MULTI",
BPFAttachTypeCgroupUnixConnect: "BPF_CGROUP_UNIX_CONNECT",
BPFAttachTypeCgroupUnixSendMsg: "BPF_CGROUP_UNIX_SENDMSG",
BPFAttachTypeCgroupUnixRecvMsg: "BPF_CGROUP_UNIX_RECVMSG",
BPFAttachTypeCgroupUnixGetPeerName: "BPF_CGROUP_UNIX_GETPEERNAME",
BPFAttachTypeCgroupUnixGetSockName: "BPF_CGROUP_UNIX_GETSOCKNAME",
BPFAttachTypeNetkitPrimary: "BPF_NETKIT_PRIMARY",
BPFAttachTypeNetkitPeer: "BPF_NETKIT_PEER",
BPFAttachTypeTraceKprobeSession: "BPF_TRACE_KPROBE_SESSION",
}

func (t BPFAttachType) String() string {
Expand Down Expand Up @@ -895,6 +925,10 @@ const (
BPFFAllowOverride AttachFlag = C.BPF_F_ALLOW_OVERRIDE
BPFFAllowMulti AttachFlag = C.BPF_F_ALLOW_MULTI
BPFFReplace AttachFlag = C.BPF_F_REPLACE
BPFFBefore AttachFlag = C.BPF_F_BEFORE
BPFFAfter AttachFlag = C.BPF_F_AFTER
BPFFID AttachFlag = C.BPF_F_ID
BPFFLink AttachFlag = C.BPF_F_LINK
)

//
Expand Down
14 changes: 14 additions & 0 deletions selftest/common/run-vm-stage2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# COMMON

[[ -f $COMMON ]] && { . $COMMON; } || { error "no common"; exit 1; }

# MAIN

check_build
# check_ppid # not needed for VM
test_exec
test_finish

exit 0
1 change: 1 addition & 0 deletions selftest/probe-ringbuf-non-supported/Makefile
7 changes: 7 additions & 0 deletions selftest/probe-ringbuf-non-supported/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/aquasecurity/libbpfgo/selftest/map-pin-info

go 1.21

require github.com/aquasecurity/libbpfgo v0.0.0

replace github.com/aquasecurity/libbpfgo => ../../
12 changes: 12 additions & 0 deletions selftest/probe-ringbuf-non-supported/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/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/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
kernel.org/pub/linux/libs/security/libcap/cap v1.2.73 h1:Th2b8jljYqkyZKS3aD3N9VpYsQpHuXLgea+SZUIfODA=
kernel.org/pub/linux/libs/security/libcap/cap v1.2.73/go.mod h1:hbeKwKcboEsxARYmcy/AdPVN11wmT/Wnpgv4k4ftyqY=
kernel.org/pub/linux/libs/security/libcap/psx v1.2.73 h1:SEAEUiPVylTD4vqqi+vtGkSnXeP2FcRO3FoZB1MklMw=
kernel.org/pub/linux/libs/security/libcap/psx v1.2.73/go.mod h1:+l6Ee2F59XiJ2I6WR5ObpC1utCQJZ/VLsEbQCD8RG24=
8 changes: 8 additions & 0 deletions selftest/probe-ringbuf-non-supported/main.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//+build ignore

#include <vmlinux.h>

#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

char LICENSE[] SEC("license") = "Dual BSD/GPL";
19 changes: 19 additions & 0 deletions selftest/probe-ringbuf-non-supported/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"os"

bpf "github.com/aquasecurity/libbpfgo"
)

func main() {
// Should not be supported before 5.8
isSupported, err := bpf.BPFMapTypeIsSupported(bpf.MapTypeRingbuf)
if err == nil || isSupported {
fmt.Fprintln(os.Stderr, "Ringbuf is supported unexpectedly or no error")
os.Exit(-1)
}

fmt.Fprintln(os.Stdout, "Ringbuf is not supported as expected")
}
1 change: 1 addition & 0 deletions selftest/probe-ringbuf-non-supported/run-vm.sh
12 changes: 12 additions & 0 deletions selftest/probe-ringbuf-non-supported/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

TEST=$(dirname $0)/$1 # execute
TIMEOUT=10 # seconds
KERNEL_VERSION=v5.7 # kernel version

# SETTINGS
COMMON="$(dirname $0)/../common/common.sh"

vng -v -r $KERNEL_VERSION --rodir="$(realpath ..)" -- "export TEST=$TEST COMMON=$COMMON TIMEOUT=$TIMEOUT; ./run-vm.sh"

exit 0
1 change: 1 addition & 0 deletions selftest/probe-ringbuf/Makefile
7 changes: 7 additions & 0 deletions selftest/probe-ringbuf/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/aquasecurity/libbpfgo/selftest/map-pin-info

go 1.21

require github.com/aquasecurity/libbpfgo v0.0.0

replace github.com/aquasecurity/libbpfgo => ../../
12 changes: 12 additions & 0 deletions selftest/probe-ringbuf/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/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/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
kernel.org/pub/linux/libs/security/libcap/cap v1.2.73 h1:Th2b8jljYqkyZKS3aD3N9VpYsQpHuXLgea+SZUIfODA=
kernel.org/pub/linux/libs/security/libcap/cap v1.2.73/go.mod h1:hbeKwKcboEsxARYmcy/AdPVN11wmT/Wnpgv4k4ftyqY=
kernel.org/pub/linux/libs/security/libcap/psx v1.2.73 h1:SEAEUiPVylTD4vqqi+vtGkSnXeP2FcRO3FoZB1MklMw=
kernel.org/pub/linux/libs/security/libcap/psx v1.2.73/go.mod h1:+l6Ee2F59XiJ2I6WR5ObpC1utCQJZ/VLsEbQCD8RG24=
13 changes: 13 additions & 0 deletions selftest/probe-ringbuf/main.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//+build ignore

#include <vmlinux.h>

#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 1 << 8);
} events1 SEC(".maps");

char LICENSE[] SEC("license") = "Dual BSD/GPL";
39 changes: 39 additions & 0 deletions selftest/probe-ringbuf/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import "C"

import (
"fmt"
"os"

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()

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

// Should be supported from 5.8 onwards
isSupported, err := bpf.BPFMapTypeIsSupported(bpf.MapTypeRingbuf)
if err != nil || !isSupported {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

eventsChannel1 := make(chan []byte)
_, err = bpfModule.InitRingBuf("events1", eventsChannel1)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
}
1 change: 1 addition & 0 deletions selftest/probe-ringbuf/run-vm.sh
12 changes: 12 additions & 0 deletions selftest/probe-ringbuf/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

TEST=$(dirname $0)/$1 # execute
TIMEOUT=10 # seconds
KERNEL_VERSION=v5.8 # kernel version

# SETTINGS
COMMON="$(dirname $0)/../common/common.sh"

vng -v -r $KERNEL_VERSION --rodir="$(realpath ..)" -- "export TEST=$TEST COMMON=$COMMON TIMEOUT=$TIMEOUT; ./run-vm.sh"

exit 0
Loading