美文网首页
2022-05-04 学习下eBPF

2022-05-04 学习下eBPF

作者: sxs7 | 来源:发表于2022-05-04 23:19 被阅读0次

参考主线:https://zhuanlan.zhihu.com/p/464285672

1、在虚拟机中安装golang和libbpfgo,我使用虚拟机是fedora35版本,没有用最新的36beta的原因是它安装过程中运行一会儿就死机。

libbpfgo的源码路径(好像很容易被墙):

https://github.com/aquasecurity/libbpfgo

采用

make libbpfgo-static

安装。

安装遇到错误:

o: golang.org/x/sys@v0.0.0-20210514084401-e8d321eab015: unrecognized import path "golang.org/x/sys": https fetch: Get "https://golang.org/x/sys?go-get=1": dial tcp 172.217.163.49:443: connect: connection refused

找到解决办法:

mkdir -p $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/sync.git
git clone https://github.com/golang/crypto.git
git clone https://github.com/golang/sys.git

git clone又遇到如下错误:

fatal: unable to access 'https://github.com/golang/sys.git/': OpenSSL SSL_read: Connection reset by peer, errno 104

解决办法如下:

[root@fedora x]# git config  --global --unset http.proxy
[root@fedora x]# git config  --global --unset https.proxy
[root@fedora x]# git clone https://github.com/golang/sys.git

之后返回运行make libbpfgo-static,仍报错:

unrecognized import path "golang.org/x/sys"

解决办法:

export GOPROXY=https://goproxy.io
source  /etc/profile

按照例子编写内核部分代码:

hello.bpf.c
#include <linux/bpf.h>
#include <linux/types.h>
#include <bpf/bpf_helpers.h>

SEC("kprobe/sys_execve")
int hello(void *ctx)
{
    bpf_printk("I'm alive!");
    return 0;
}

char _license[] SEC("license") = "GPL";

编译:

clang -O2 -c -target bpf hello.bpf.c

按照例子编写用户态部门代码:hellobpf.go

package main

import (
        "C"

        bpf "github.com/aquasecurity/tracee/libbpfgo"
)

import (
    "fmt"
)

const sys_execve="__arm64_sys_execve"

func main() {

        bpfModule, err := bpf.NewModuleFromFile("hello.bpf.o")
        must(err)
        defer bpfModule.Close()

        err = bpfModule.BPFLoadObject()
        must(err)

        prog, err := bpfModule.GetProgram("hello")
        must(err)

        _, err = prog.AttachKprobe(sys_execve)
        must(err)

    bpf.TracePrint()

    fmt.Println("Cleaning up")
}

func must(err error) {
        if err != nil {
                panic(err)
        }
}

编译:

CC=clang CGO_CFLAGS="-I /usr/include/bpf" CGO_LDFLAGS="/usr/src/kernels/5.15.17-200.fc35.aarch64/tools/bpf/resolve_btfids/libbpf/libbpf.a" go build

编译失败:

[root@fedora study]# CC=clang CGO_CFLAGS="-I /usr/include/bpf" CGO_LDFLAGS="/home/s30/libbpfgo/output/libbpf.a" go build
hellobpf.go:6:9: no required module provides package github.com/aquasecurity/tracee/libbpfgo; to add it:
    go get github.com/aquasecurity/tracee/libbpfgo

按照提示执行

go get github.com/aquasecurity/tracee/libbpfgo

不能成功。
无意中在上述链接中搜索“libbpfgo”,在源码中发现了libbpfgo的路径应该是

go get github.com/aquasecurity/libbpfgo

修改后进行尝试,

[root@fedora study]# CC=clang CGO_CFLAGS="-I /usr/include/bpf" CGO_LDFLAGS="/home/s30/libbpfgo/output/libbpf.a" go build
hellobpf.go:7:2: no required module provides package github.com/aquasecurity/libbpfgo; to add it:
    go get github.com/aquasecurity/libbpfgo
[root@fedora study]# go get github.com/aquasecurity/libbpfgo
go: downloading github.com/aquasecurity/libbpfgo v0.1.1
go: downloading golang.org/x/sys v0.0.0-20210514084401-e8d321eab015
github.com/aquasecurity/libbpfgo imports
    github.com/aquasecurity/libbpfgo/helpers imports
    golang.org/x/sys/unix: unrecognized import path "golang.org/x/sys": https fetch: Get "https://golang.org/x/sys?go-get=1": dial tcp 142.251.42.241:443: connect: connection refused

查了下,据说是众所周知的原因造成的。
想了下,libbpfgo前面不是已经下载到本地了么?为什么还要在线导入呢?

跟着学习到这里,卡住了,需要补很多golang的课程。后来觉得这样跑偏了。找了本<Linux内核观测技术BPF>来看,感觉豁然开朗了不少。原来学习eBPF并不需要搞得那么复杂,有很多线程的工具可以使用。后来将按照书的内容,逐步来学习。

相关文章

  • 2022-05-04 学习下eBPF

    参考主线:https://zhuanlan.zhihu.com/p/464285672[https://zhuan...

  • ebpf

    接上篇udp压测场景性能较差,想用ebpf优化下性能,毕竟ebpf已经有LB方案可以替代ipvs,基于更低的cpu...

  • 2022-05-04

    刘ber——2022-05-04笔记汇总!!!推荐一下幕布做思维导图!

  • [漏洞复现]Ubuntu16.04本地提权(CVE-2017-1

    漏洞概述: 该漏洞存在于Linux内核带有的eBPF bpf(2)系统调用中,当用户提供恶意BPF程序使eBPF验...

  • CVE-2017-16995 ubuntu16.04

    该漏洞存在于Linux内核带有的eBPF bpf(2)系统调用中,当用户提供恶意BPF程序使eBPF验证器模块产生...

  • eBPF深度探索: 高效DNS监控实现

    eBPF可以灵活扩展Linux内核机制,本文通过实现一个DNS监控工具为例,介绍了怎样开发实际的eBPF应用。原文...

  • 为什么k8s管理员要懂eBPF

    本文主要目的是让你了解eBPF的来龙去脉,以及为什么它在观察容器和Kubernetes集群时特别有用。 eBPF有...

  • eBPF verifier

    源:https://www.kernel.org/doc/Documentation/networking/fil...

  • linux调优工具

    eBPF https://www.cnblogs.com/charlieroro/p/13403672.html[...

  • 从零开始的内核ebpf开发之旅

    引言 内核研究与开发是计算机底层处于与硬件打交道的部位,ebpf可以理解为是内核开发的一个模块。在研究ebpf开发...

网友评论

      本文标题:2022-05-04 学习下eBPF

      本文链接:https://www.haomeiwen.com/subject/bwagyrtx.html