Giter Club home page Giter Club logo

linux-bpf-learning's Introduction

学习Linux BPF/eBPF 编程

打造学习BPF知识的中文社区。学习计划如下: bpf-learning-path

相关博文参考

实验环境准备

  • Linux操作系统(喜欢尝鲜新版内核的同学,推荐使用bumblebeevagrantfile)

    本人自己的实验环境是Ubuntu 18.04标准版vagrant虚拟机,内核版本为4.15.0。可以从这里下载该vagrant虚拟机环境,已安装bcc工具集合:

    下载链接: https://pan.baidu.com/s/11dsEU6Yk6KGDGNor-fbsgQ 提取码: qvhc。 使用方式可以参考这篇文章

    以下命令如无特殊说明,均在Ubuntu环境下测试执行。

  • 预装clang、LLVM、iproute2、libelf-dev

    # for ubuntu
    apt install clang llvm libelf-dev iproute2
    # test clang
    clang -v
    # test llvm
    llc --version
    # test iproute2
    ip link
  • bpftool命令行安装说明

    下载Linux内核源码,进行本地编译。

    # 确认内核版本
    uname -r
    # 找到对应内核版本的源代码
    apt-cache search linux-source
    apt install linux-source-5.3.0
    apt install libelf-dev
    
    cd /usr/src/linux-source-5.3.0
    tar xjf linux-source-5.3.0.tar.bz2
    cd linux-source-5.3.0/tools
    make -C  bpf/bpftool/
    cd bpf/bpftool/
    ./bpftool prog/net

目录说明

常见问题Q&A

1. 'asm/type.h' file not found

  • 错误现象

    在执行下面命令进行代码编译时,可能会遇到某些头文件找不到的错误:

    clang -I ./headers/ -O2 -target bpf -c tc-xdp-drop-tcp.c -o tc-xdp-drop-tcp.o
    
    In file included from tc-xdp-drop-tcp.c:2:
    In file included from /usr/include/linux/bpf.h:11:
    /usr/include/linux/types.h:5:10: fatal error: 'asm/types.h' file not found
    #include <asm/types.h>
            ^~~~~~~~~~~~~
    1 error generated.
  • 原因分析

    在源代码文件中引用了某些系统目录(一般为/usr/include/)下的头文件,而这些头文件没有出现在目标路径下,导致编译失败。

    如上述问题中的asm相关文件,asm全称Architecture Specific Macros,直译过来“与机器架构相关的宏文件”,顾名思义它是跟机器架构密切相关的,不同的架构x86、x64、arm实现是不一样的,而操作系统并没有提供/usr/include/asm/这样通用的目录,只提供了具体架构相关的目录,如/usr/include/x86_64-linux-gnu/asm/,因此无法找到引用。

  • 解决方案

    添加软链/usr/include/asm/,指向操作系统自带的asm目录:

    cd /usr/include
    ln -s ./x86_64-linux-gnu/asm asm

2. 'bpf/bpf_helpers.h' file not found

  • 解决方案

    apt-get install libbpf-dev
    # run `apt-file update` if needed
    apt-file list libbpf-dev | grep bpf_helpers.h
    # you will get the result like: libbpf-dev: /usr/include/bpf/bpf_helpers.h

slack社区

感兴趣可加入

参考材料

linux-bpf-learning's People

Contributors

nevermosby avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

linux-bpf-learning's Issues

XDP示例疑问

我想请问一下 在主机侧veth网卡上attach xdp程序丢弃TCP数据包的话,从容器内部访问外部服务的TCP请求数据包不是也会被丢弃吗,为什么还能够访问外部服务呀

XDP BPF 程序示例的README写错了

clang -O2 -target bpf -c xdp_drop_all.c -o xdp_drop_all.o
这一句应该是
clang -O2 -target bpf -c xdp_drop_world.c -o xdp_drop_all.o
因为上面的那个文件名是xdp_drop_world.c

加载网卡的时候出错

运行的linux-bpf-learning/bpf/bpf-maps/examples-in-kernel/xdp_ip_tracker_kern.c,正常clang编译完成之后,加载在网卡上出现的错误,系统环境是linux,版本是Linux ubuntu 4.15.0-29-generic #31-Ubuntu SMP UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

开发环境

您好,开发环境请教几个问题:

  1. IDE用的是什么编辑器呢?
  2. 在mac pro下能编译么?
  3. 在macpro下能debug么?
    求大神解答。

eBPF TC 程序获取 payload

大佬你好,

不知道你有没有尝试过在 tc 程序中获取网络数据包的 payload。

一般根据 skb 的长度和各个协议包头的长度可以计算出有效的 payload 长度,现在我想利用 bpf_skb_load_bytes() 这个函数将 payload 拷贝到自己定义的 buffer 中,想着后续匹配下关键词什么的。

结果一直绕不过 eBPF verifier 的校验,不知道大佬是否有相关经验可以分享?

ebpf的用途

大佬你好,我想在内核网络IO路径上对指定的数据结构(msghdr/iov_iter/iovec)做可靠性加固(假设是极端环境、软硬件不可靠的场景)。比如说在内核访问这些数据结构前需要验证下这些数据结构的正确性,是否可以用ebpf程序对这些数据结构做监测,一旦内核访问这些数据结构,就跳转到我的ebpf程序执行?望百忙中抽空指导下实现思路,不胜感激~
PS:不知道有没有群/微信可以加一下呢?想交流学习下

社区群

读您的例子感觉收获很多,关于bpf的介绍应该是见过最详细的了,请问有没有微信群之类的交流地方?应该也 from China吧?

ip link set dev 加载在内核出现问题

你好 clang完.c没有问题,但是加载内核的时候出现12 bytes struct bpf_elf_map fixup performed due to size mismatch!
ELF contains relo data for non ld64 instruction at offset 19! Compiler bug?!

  • Try to annotate functions with always_inline attribute!
    Error fetching program/map!
    是什么情况呢

基于Docker的第二个XDP程序,有疑问?

从上面的例子可以看到XDP BPF程序只会对传给目标网卡的数据包进行丢弃,不会影响从目标网卡出去的数据包,也就是只影响ingress流量。那么能不能控制egress流量呢?

以上是你得出的结论:我有个疑问,我自己做实验没有像你视频中的一样,可以在容器所有网络名称空间下还能curl通https://www.baidu.com/

我的理解:在宿主机上面对vethxxx进行了 drop所有tcp协议,那么容器内的eth0就不应该收到任何的tcp包的,127.0.0.1除外。
veth peer 是成对出现的,我既然在宿主上面dorp掉一端,为什么在容器所有网络名称空间下还能curl通呢。

你视频中显示在容器网络名称空间内还能curl通,不理解原理是什么。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.