Aya Cross-Dev-Env for aarch64

- ebpf aya

Anything

最近想试试 Ebpf ,于是想着 Rust 的工具链会相对好用也比较齐全,实际上并没有想象的那么轻松。尤其是在 Mac 上编译,困难重重。

主要的问题一个是 Macos 本身并不支持 Ebpf 所以他没有那些 Ebpf 的 libc 头文件,在 crate::libc 中,就会缺少定义,代码提示就会出问题,链接也会出问题。安装官方文档配置了相应的环境,还是编译不过。索性放弃了,选择装了个虚拟机来编译,就没什么问题了。

For Ubuntu x86_64

  1. Install Rust
    rustup install stable
    rustup toolchain install nightly --component rust-src
    cargo install bpf-linker
    cargo install cargo-generate
    
    IF DOWNLOAD VERY SLOW TRY:
    export RUSTUP_DIST_SERVER="https://rsproxy.cn"
    export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
    
  2. Install musl-tools
    curl -s musl.cc | grep aarch64
    wget https://musl.cc/aarch64-linux-musl-cross.tgz
    tar -zxvf aarch64-linux-musl-cross.tgz
    # ADD IT TO YOUR PATH
    # eg.
    export PATH=${PATH}:/repalce/with/your/unzip_path/aarch64-linux-musl-cross/bin
    
  3. Build Test
    cargo generate --name myapp -d program_type=xdp https://github.com/aya-rs/aya-template
    cd myapp
    export ARCH=aarch64
    cargo build --package myapp --release --target=${ARCH}-unknown-linux-musl
    --config=target.${ARCH}-unknown-linux-musl.linker=\"${ARCH}-linux-musl-gcc\"
    
  4. If you want to make “–config” global
     printf "[target.aarch64-unknown-linux-musl]\n" >> ~/.cargo/config.toml
     printf 'linker = "aarch64-linux-musl-gcc"\n' >> ~/.cargo/config.toml 
    

For Macos M chip

  1. Try Official Steps refer: https://aya-rs.dev/book/start/development/

If things went wrong and YOU CAN NOT FIGURE IT OUT:

Update Your LLVM Version

Compiling an Aya program requires a special linker called “bpf-link”, which must be compatible with the LLVM version used by rustc. Additionally, Aya uses the Rust nightly compiler, which frequently updates its LLVM backend. As a result, when this happens, there is often a time gap between the LLVM version provided by Homebrew and the one used by Rust nightly. This can lead to build errors in Aya programs.

You can either build the latest LLVM from source or find a compatible version of “bpf-link” that works with your Rust nightly compiler.

Use VM

  1. Install a ubuntu vm \ ubuntu docker image

    refer: https://canonical.com/multipass/install

    refer: https://hub.docker.com/_/ubuntu

  2. Repeat Steps Mentioned Before

    YOU WILL BE FINE