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

feat: support use extra btf as kernel spec when it is not enabled #7

Merged
merged 1 commit into from
Aug 14, 2024
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
Binary file added pkg/btfs/archives/5.4.28-200.el7.x86_64.btf
Binary file not shown.
68 changes: 68 additions & 0 deletions pkg/btfs/btfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package btfs

import (
"bytes"
"embed"
"sync"
"syscall"

"github.com/cilium/ebpf/btf"
)

func init() {
setKernelSpec()
}

var BtfSpec *btf.Spec

var (
once sync.Once
// TODO: add more kernel release to btf file mapping
releaseToBtf = map[string]string{
"5.10.134-16.1.al8.x86_64": "5.4.28-200.el7.x86_64.btf",
"5.4.278-1.el7.elrepo.x86_64": "5.4.28-200.el7.x86_64.btf",
"5.5.5-1.el7.elrepo.x86_64": "5.4.28-200.el7.x86_64.btf",
}
)

//go:embed archives/*
var btfFiles embed.FS

func int8ToStr(arr []int8) string {
b := make([]byte, 0, len(arr))
for _, v := range arr {
if v == 0x00 {
break
}
b = append(b, byte(v))
}
return string(b)
}

func setKernelSpec() {
once.Do(func() {
btfSpec, err := btf.LoadKernelSpec()
if err != nil {
var uname syscall.Utsname
if err := syscall.Uname(&uname); err != nil {
panic(err)
}
release := int8ToStr(uname.Release[:])
releaseTarget, ok := releaseToBtf[release]
if !ok {
panic("no btf file found for kernel release: " + release)
}
btfFileReader, err := btfFiles.ReadFile("archives/" + releaseTarget)
if err != nil {
panic(err)
}
btfSpec, err = btf.LoadSpecFromReader(bytes.NewReader(btfFileReader))
if err != nil {
panic(err)
}
BtfSpec = btfSpec
return
}
BtfSpec = btfSpec
})
}
8 changes: 7 additions & 1 deletion pkg/plugins/kprobe/kprobesysctl/ebpf_kprobe_sysctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"strconv"
"time"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"
"github.com/erda-project/ebpf-agent/metric"
"github.com/erda-project/ebpf-agent/pkg/btfs"
"github.com/erda-project/ebpf-agent/pkg/envconf"
"github.com/erda-project/ebpf-agent/pkg/exporter/collector"
"github.com/patrickmn/go-cache"
Expand Down Expand Up @@ -44,7 +46,11 @@ func New(clientSet *kubernetes.Clientset) *KprobeSysctlController {
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatal(err)
}
if err := loadBpfObjects(&objs, nil); err != nil {
if err := loadBpfObjects(&objs, &ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
KernelTypes: btfs.BtfSpec,
},
}); err != nil {
log.Fatalf("loading objects: %v", err)
}
reportConfig := &collector.CollectorConfig{}
Expand Down
7 changes: 6 additions & 1 deletion pkg/plugins/memory/oomprocesser/ebpf_oom_kill_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/erda-project/ebpf-agent/pkg/btfs"
"k8s.io/klog"
)

Expand All @@ -28,7 +29,11 @@ func WatchOOM(ch chan<- *OOMEvent) {
log.Fatal(err)
}

coll, err := ebpf.NewCollectionWithOptions(spec, ebpf.CollectionOptions{})
coll, err := ebpf.NewCollectionWithOptions(spec, ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
KernelTypes: btfs.BtfSpec,
},
})
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/plugins/netfilter/ebpf/bpf_netfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"

"github.com/cilium/ebpf"
"github.com/erda-project/ebpf-agent/pkg/btfs"
)

func RunEbpf() *NetfilterObjects {
Expand All @@ -16,7 +17,8 @@ func RunEbpf() *NetfilterObjects {
var bpfObj NetfilterObjects
if err := spec.LoadAndAssign(&bpfObj, &ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
LogSize: ebpf.DefaultVerifierLogSize * 10,
LogSize: ebpf.DefaultVerifierLogSize * 10,
KernelTypes: btfs.BtfSpec,
},
}); err != nil {
panic(err)
Expand Down
7 changes: 6 additions & 1 deletion pkg/plugins/protocols/http/ebpf/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/cilium/ebpf"
"github.com/erda-project/ebpf-agent/pkg/btfs"
"k8s.io/klog/v2"

"github.com/erda-project/ebpf-agent/pkg/utils"
Expand Down Expand Up @@ -61,7 +62,11 @@ func (e *provider) Load() error {
if err != nil {
return err
}
e.collection, err = ebpf.NewCollectionWithOptions(spec, ebpf.CollectionOptions{})
e.collection, err = ebpf.NewCollectionWithOptions(spec, ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
KernelTypes: btfs.BtfSpec,
},
})
if err != nil {
return err
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/plugins/protocols/kafka/ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/cilium/ebpf"
"io/ioutil"
"k8s.io/klog"
"log"
"syscall"
"time"
"unsafe"

"github.com/cilium/ebpf"
"github.com/erda-project/ebpf-agent/pkg/btfs"
"k8s.io/klog"
)

const (
Expand Down Expand Up @@ -43,7 +45,11 @@ func NewEbpf(ifindex int, ip string, ch chan Event) *Ebpf {
func (e *Ebpf) Load(spec *ebpf.CollectionSpec) error {
klog.Infof("ip: %s, index: %d start kafka", e.IPaddress, e.IfIndex)
var err error
e.collection, err = ebpf.NewCollectionWithOptions(spec, ebpf.CollectionOptions{})
e.collection, err = ebpf.NewCollectionWithOptions(spec, ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
KernelTypes: btfs.BtfSpec,
},
})
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/plugins/protocols/rpc/ebpf/ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"errors"
"fmt"
"io/ioutil"
"k8s.io/klog"
"log"
"sync"
"syscall"
"time"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/erda-project/ebpf-agent/pkg/btfs"
"k8s.io/klog"
)

const (
Expand Down Expand Up @@ -70,7 +71,11 @@ func NewEbpf(ifindex int, ip string, ch chan Metric) *Ebpf {
func (e *Ebpf) Load(spec *ebpf.CollectionSpec) error {
klog.Infof("ip: %s, index: %d start rpc", e.IPaddress, e.IfIndex)
var err error
e.collection, err = ebpf.NewCollectionWithOptions(spec, ebpf.CollectionOptions{})
e.collection, err = ebpf.NewCollectionWithOptions(spec, ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
KernelTypes: btfs.BtfSpec,
},
})
if err != nil {
return err
}
Expand Down
Loading