From 1fb0f6665d05b9ecec23fba8fc5d260a3fa7e722 Mon Sep 17 00:00:00 2001 From: Huang Huang Date: Sat, 20 Jul 2024 10:02:12 +0800 Subject: [PATCH] improve compatibility with OpenCloudOS 7/8/9 and TencentOS Server 2.4/3.1 (#91) closes #89 --- bpf/bpf.go | 8 +++++--- bpf/bpf_legacy.go | 10 +++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/bpf/bpf.go b/bpf/bpf.go index 70669eff..38658321 100644 --- a/bpf/bpf.go +++ b/bpf/bpf.go @@ -149,7 +149,8 @@ func (b *BPF) Load(opts Options) error { } } - if b.isLegacyKernel || !supportGetSocketCookieWithCgroup() { + if b.isLegacyKernel || !supportCgroupSock() { + log.Debug("will load the objs for legacy kernel") b.skipAttachCgroup = true objs := BpfObjectsForLegacyKernel{} if err = b.spec.LoadAndAssign(&objs, &ebpf.CollectionOptions{ @@ -261,6 +262,7 @@ func (b *BPF) AttachKprobes() error { lk, err = link.Kprobe("udp_send_skb", b.objs.KprobeUdpSendSkb, &link.KprobeOptions{}) if err != nil { + log.Debugf("%+v", err) if strings.Contains(err.Error(), "no such file or directory") { lk, err = link.Kprobe("udp_sendmsg", b.objs.KprobeUdpSendmsg, &link.KprobeOptions{}) if err != nil { @@ -276,7 +278,7 @@ func (b *BPF) AttachKprobes() error { b.objs.KprobeNfNatPacket, &link.KprobeOptions{}) if err != nil { if strings.Contains(err.Error(), "nf_nat_packet: not found: no such file or directory") { - log.Warn("current system doest not enable netfilter based NAT feature, skip attach kprobe/nf_nat_packet") + log.Warn("the kernel does not support netfilter based NAT feature, skip attach kprobe/nf_nat_packet") } else { return fmt.Errorf("attach kprobe/nf_nat_packet: %w", err) } @@ -289,7 +291,7 @@ func (b *BPF) AttachKprobes() error { b.objs.KprobeNfNatManipPkt, &link.KprobeOptions{}) if err != nil { if strings.Contains(err.Error(), "nf_nat_manip_pkt: not found: no such file or directory") { - log.Warn("current system doest not enable netfilter based NAT feature, skip attach kprobe/nf_nat_manip_pkt") + log.Warn("the kernel does not support netfilter based NAT feature, skip attach kprobe/nf_nat_manip_pkt") } else { return fmt.Errorf("attach kprobe/nf_nat_manip_pkt: %w", err) } diff --git a/bpf/bpf_legacy.go b/bpf/bpf_legacy.go index ece708f8..8fca688f 100644 --- a/bpf/bpf_legacy.go +++ b/bpf/bpf_legacy.go @@ -45,12 +45,16 @@ func (b *BpfObjects) FromLegacy(o *BpfObjectsForLegacyKernel) { b.BpfMaps = o.BpfMaps } -func supportGetSocketCookieWithCgroup() bool { - err := features.HaveProgramHelper(ebpf.CGroupSock, asm.FnGetSocketCookie) - if err != nil { +func supportCgroupSock() bool { + if err := features.HaveProgramHelper(ebpf.CGroupSock, asm.FnGetSocketCookie); err != nil { log.Debugf("%+v", err) return false } + if err := features.HaveProgramHelper(ebpf.CGroupSock, asm.FnGetCurrentTask); err != nil { + log.Debugf("%+v", err) + return false + } + return true }