From 80104224350196d153047dbc8e75b11da138b15d Mon Sep 17 00:00:00 2001 From: Nikita Baksalyar Date: Sun, 10 Apr 2022 01:07:21 +0100 Subject: [PATCH 1/4] Support LLVM14 Signed-off-by: Nikita Baksalyar --- Cargo.lock | 16 +++++++++++++++- README.md | 32 ++++++++++++++++++++------------ TUTORIAL.md | 10 +++++----- cargo-bpf/Cargo.toml | 6 ++++-- cargo-bpf/build.rs | 2 +- cargo-bpf/src/llvm.rs | 2 ++ 6 files changed, 47 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7caa41a..437d852 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -121,7 +121,8 @@ dependencies = [ "lazy_static", "libbpf-sys", "libc", - "llvm-sys", + "llvm-sys 130.0.3", + "llvm-sys 140.0.0", "proc-macro2", "quote", "redbpf", @@ -480,6 +481,19 @@ dependencies = [ "semver 0.11.0", ] +[[package]] +name = "llvm-sys" +version = "140.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edbec78fa56ea7a1ff451683a51b8ecf79a65ca9e88a4be6c4b0a6fc300d2a6" +dependencies = [ + "cc", + "lazy_static", + "libc", + "regex", + "semver 1.0.7", +] + [[package]] name = "log" version = "0.4.16" diff --git a/README.md b/README.md index 54494d7..2f62936 100644 --- a/README.md +++ b/README.md @@ -63,8 +63,9 @@ programs using Rust. It includes: `LLVM` is required in your build system to compile BPF bytecode using RedBPF. -- **LLVM 13** - It is needed to compile BPF bytecode. +- **LLVM 14 or 13** + It is needed to compile BPF bytecode. If you're using Rust 1.60 or later, LLVM 14 is required. + If you need to use LLVM 13, use the feature `llvm13`. - One of the followings: 1. The Linux kernel headers @@ -74,7 +75,7 @@ programs using Rust. It includes: ### On Ubuntu 20.04 LTS -Install LLVM 13 and the Linux kernel headers +Install LLVM 14 and the Linux kernel headers ```console # apt-get update \ && apt-get -y install \ @@ -85,12 +86,15 @@ Install LLVM 13 and the Linux kernel headers libelf-dev \ linux-headers-generic \ pkg-config \ - && wget https://apt.llvm.org/llvm.sh && chmod +x llvm.sh && ./llvm.sh 13 && rm -f ./llvm.sh -# llvm-config-13 --version | grep 13 + && wget https://apt.llvm.org/llvm.sh && chmod +x llvm.sh && ./llvm.sh 14 && rm -f ./llvm.sh +# llvm-config-14 --version | grep 14 ``` ### On Fedora 35 +Currently, only LLVM 13 is available in the official Fedora repositories. +Consider building LLVM from source until Fedora 36 is released. + Install LLVM 13 and the Linux kernel headers ```console # dnf install -y \ @@ -126,16 +130,19 @@ Install LLVM 13 and the Linux kernel headers # llvm-config --version | grep -q '^13' ``` +At the time of writing, LLVM 14 is not available in the official package repository. +If you need to use Rust 1.60 or later, please build LLVM from source. + ### Building LLVM from source If your Linux distro does not support the latest LLVM as pre-built packages yet, you may build LLVM from the LLVM source code. ```console -$ tar -xaf llvm-13.0.0.src.tar.xz -$ mkdir -p llvm-13.0.0.src/build -$ cd llvm-13.0.0.src/build -$ cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/llvm-13-release -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_LLVM_DYLIB=1 +$ tar -xaf llvm-14.0.0.src.tar.xz +$ mkdir -p llvm-14.0.0.src/build +$ cd llvm-14.0.0.src/build +$ cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/llvm-14-release -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_LLVM_DYLIB=1 $ cmake --build . --target install ``` @@ -143,8 +150,8 @@ Then you can use your LLVM by specifying the custom installation path when installing `cargo-bpf` or building RedBPF like this: ```console -$ LLVM_SYS_130_PREFIX=$HOME/llvm-13-release/ cargo install cargo-bpf -$ LLVM_SYS_130_PREFIX=$HOME/llvm-13-release/ cargo build +$ LLVM_SYS_140_PREFIX=$HOME/llvm-14-release/ cargo install cargo-bpf +$ LLVM_SYS_140_PREFIX=$HOME/llvm-14-release/ cargo build ``` Make sure correct `-DCMAKE_BUILD_TYPE` is specified. Typically `Debug` type is @@ -235,7 +242,8 @@ There are two LLVM versions involved in compiling BPF programs: | Rust version | LLVM version of the rustc | Valid LLVM version of system | |:-------------|:-------------------------:|:-----------------------------| -| 1.56 ~ | LLVM 13 | LLVM 13 and newer | +| 1.56 ~ 1.60 | LLVM 13 | LLVM 13 and newer | +| 1.60 ~ | LLVM 14 | LLVM 14 and newer | ## Docker images for RedBPF build test diff --git a/TUTORIAL.md b/TUTORIAL.md index 0577812..53f1293 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -45,14 +45,14 @@ For some reasons, you may want to build LLVM from source code. When you build LLVM, consider building LLVM with `Release` build mode. -For example, when you build LLVM13 from source code, you can pass +For example, when you build LLVM14 from source code, you can pass `-DCMAKE_BUILD_TYPE=Release` to the `cmake` command as below: ```console -$ tar -xaf llvm-13.0.0.src.tar.xz -$ mkdir -p llvm-13.0.0.src/build -$ cd llvm-13.0.0.src/build -$ cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/llvm-13-release -DCMAKE_BUILD_TYPE=Release +$ tar -xaf llvm-14.0.0.src.tar.xz +$ mkdir -p llvm-14.0.0.src/build +$ cd llvm-14.0.0.src/build +$ cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/llvm-14-release -DCMAKE_BUILD_TYPE=Release $ cmake --build . --target install ``` diff --git a/cargo-bpf/Cargo.toml b/cargo-bpf/Cargo.toml index 937bae3..e62f095 100644 --- a/cargo-bpf/Cargo.toml +++ b/cargo-bpf/Cargo.toml @@ -39,6 +39,7 @@ anyhow = "1.0" # if `strict-versioning` feature is off, llvm-sys-130 can be linked to # libLLVM13 and newer libraries. llvm-sys-130 = { version = "130", optional = true, package = "llvm-sys" } +llvm-sys-140 = { version = "140", optional = true, package = "llvm-sys" } cfg-if = "1.0" rustversion = "1.0" rustc_version = "0.4.0" @@ -53,11 +54,12 @@ cfg-if = "1.0.0" default = ["command-line", "llvm-sys"] bindings = ["libbpf-sys", "bpf-sys", "bindgen", "syn", "quote", "proc-macro2", "tempfile"] build = ["bindings", "libc", "toml_edit", "redbpf"] -docsrs-llvm = ["llvm-sys-130/no-llvm-linking", "llvm-sys-130/disable-alltargets-init"] +docsrs-llvm = ["llvm-sys-140/no-llvm-linking", "llvm-sys-140/disable-alltargets-init"] build-c = [] command-line = ["build", "clap", "redbpf/load", "futures", "tokio", "hexdump"] -llvm-sys = ["llvm-sys-130"] # Use current default LLVM version or higher +llvm-sys = ["llvm-sys-140"] # Use current default LLVM version or higher llvm13 = ["llvm-sys-130"] +llvm14 = ["llvm-sys-140"] [package.metadata.docs.rs] all-features = false diff --git a/cargo-bpf/build.rs b/cargo-bpf/build.rs index 5918380..b8c891b 100644 --- a/cargo-bpf/build.rs +++ b/cargo-bpf/build.rs @@ -56,7 +56,7 @@ fn print_cargo_bpf_llvm_version() { fn main() { cfg_if::cfg_if! { - if #[cfg(all(feature = "llvm-sys-130", not(feature = "docsrs-llvm")))] { + if #[cfg(all(any(feature = "llvm-sys-130", feature = "llvm-sys-140"), not(feature = "docsrs-llvm")))] { print_cargo_bpf_llvm_version(); } else { println!("cargo:rustc-env=CARGO_BPF_LLVM_VERSION=0.0.0"); diff --git a/cargo-bpf/src/llvm.rs b/cargo-bpf/src/llvm.rs index 0e95ae2..f65f3a4 100644 --- a/cargo-bpf/src/llvm.rs +++ b/cargo-bpf/src/llvm.rs @@ -22,6 +22,8 @@ second. cfg_if::cfg_if! { if #[cfg(feature = "llvm-sys-130")] { use llvm_sys_130 as llvm_sys; + } else if #[cfg(feature = "llvm-sys-140")] { + use llvm_sys_140 as llvm_sys; } else { compile_error!("Specify --features llvm13"); } From b4fa5b332015efbe910935045582881cc33dc443 Mon Sep 17 00:00:00 2001 From: Nikita Baksalyar Date: Fri, 6 May 2022 23:12:54 +0100 Subject: [PATCH 2/4] Update CI configuration * Use latest Docker images for builds. * Re-enable Alpine build because it now supports llvm13. Signed-off-by: Nikita Baksalyar --- .github/workflows/build-test.yml | 78 ++++++++++++++++---------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 6ccbac0..b3d75bd 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -17,7 +17,7 @@ env: REDBPF_IMAGE_NAME: ghcr.io/${{ github.repository_owner }}/redbpf-build jobs: - build-on-x86_64-ubuntu-2104: + build-on-x86_64-ubuntu-2204: runs-on: ubuntu-latest steps: - @@ -31,12 +31,12 @@ jobs: uname -a cat /etc/os-release - - name: Build RedBPF with the kernel headers on x86_64 Ubuntu 21.04 + name: Build RedBPF with the kernel headers on x86_64 Ubuntu 22.04 run: | docker run --privileged \ -v $PWD:/build \ -w /build \ - $REDBPF_IMAGE_NAME:latest-x86_64-ubuntu21.04 \ + $REDBPF_IMAGE_NAME:latest-x86_64-ubuntu22.04 \ bash -c 'export KERNEL_SOURCE=$(echo /lib/modules/*) \ && echo KERNEL_SOURCE=$KERNEL_SOURCE \ && cargo clean \ @@ -44,12 +44,12 @@ jobs: && cargo build --bin cargo-bpf \ && cargo build --features=kernel5_8 --examples' - - name: Build RedBPF with vmlinux on x86_64 Ubuntu 21.04 + name: Build RedBPF with vmlinux on x86_64 Ubuntu 22.04 run: | docker run --privileged \ -v $PWD:/build \ -w /build \ - $REDBPF_IMAGE_NAME:latest-x86_64-ubuntu21.04 \ + $REDBPF_IMAGE_NAME:latest-x86_64-ubuntu22.04 \ /bin/bash -c 'export REDBPF_VMLINUX=/boot/vmlinux \ && cargo clean \ && cargo build \ @@ -95,7 +95,7 @@ jobs: && cargo build --bin cargo-bpf \ && cargo build --features=kernel5_8 --examples' - build-on-x86_64-fedora35: + build-on-x86_64-fedora36: runs-on: ubuntu-latest steps: - @@ -109,12 +109,12 @@ jobs: uname -a cat /etc/os-release - - name: Build RedBPF with the kernel headers on x86_64 Fedora 35 + name: Build RedBPF with the kernel headers on x86_64 Fedora 36 run: | docker run --privileged \ -v $PWD:/build \ -w /build \ - $REDBPF_IMAGE_NAME:latest-x86_64-fedora35 \ + $REDBPF_IMAGE_NAME:latest-x86_64-fedora36 \ /bin/bash -c 'export KERNEL_SOURCE=$(echo /lib/modules/*) \ && echo KERNEL_SOURCE=$KERNEL_SOURCE \ && cargo clean \ @@ -122,20 +122,20 @@ jobs: && cargo build --bin cargo-bpf \ && cargo build --features=kernel5_8 --examples' - - name: Build RedBPF with vmlinux on x86_64 Fedora 35 + name: Build RedBPF with vmlinux on x86_64 Fedora 36 run: | docker run --privileged \ -v $PWD:/build \ -w /build \ - $REDBPF_IMAGE_NAME:latest-x86_64-fedora35 \ + $REDBPF_IMAGE_NAME:latest-x86_64-fedora36 \ /bin/bash -c 'export REDBPF_VMLINUX=/boot/vmlinux \ && cargo clean \ && cargo build \ && cargo build --bin cargo-bpf \ && cargo build --features=kernel5_8 --examples' - build-on-x86_64-alpine315: - name: Alpine 3.15 is disabled temporarily (LLVM13 unsupported) + build-on-x86_64-alpine-edge: + name: Alpine Edge if: false runs-on: ubuntu-latest steps: @@ -150,19 +150,19 @@ jobs: uname -a cat /etc/os-release - - name: Build RedBPF with the kernel headers on x86_64 Alpine 3.15 + name: Build RedBPF with the kernel headers on x86_64 Alpine Edge run: | docker run --privileged \ -v $PWD:/build \ -w /build \ - $REDBPF_IMAGE_NAME:latest-x86_64-alpine3.15 \ + $REDBPF_IMAGE_NAME:latest-x86_64-alpine-edge \ /bin/bash -c 'export KERNEL_SOURCE=$(echo /lib/modules/*) \ && echo KERNEL_SOURCE=$KERNEL_SOURCE \ && export RUSTFLAGS=-Ctarget-feature=-crt-static \ && cargo clean \ && cargo build \ && cargo build --bin cargo-bpf \ - && cargo build --features=kernel5_8 --examples' + && cargo build --features="kernel5_8 llvm13" --examples' build-on-x86_64-archlinux: runs-on: ubuntu-latest @@ -257,7 +257,7 @@ jobs: && cargo build --bin cargo-bpf \ && cargo build --examples' - build-on-aarch64-ubuntu-2104: + build-on-aarch64-ubuntu-2204: runs-on: ubuntu-latest steps: - @@ -281,12 +281,12 @@ jobs: name: Available platforms run: echo ${{ steps.qemu.outputs.platforms }} - - name: Build RedBPF with the kernel headers on aarch64 Ubuntu 21.04 + name: Build RedBPF with the kernel headers on aarch64 Ubuntu 22.04 run: | docker run --privileged \ -v $PWD:/build \ -w /build \ - $REDBPF_IMAGE_NAME:latest-aarch64-ubuntu21.04 \ + $REDBPF_IMAGE_NAME:latest-aarch64-ubuntu22.04 \ /bin/bash -c 'export KERNEL_SOURCE=$(echo /lib/modules/*) \ && echo KERNEL_SOURCE=$KERNEL_SOURCE \ && cargo clean \ @@ -294,12 +294,12 @@ jobs: && cargo build --bin cargo-bpf \ && cargo build --features=kernel5_8 --examples' - - name: Build RedBPF with vmlinux on aarch64 Ubuntu 21.04 + name: Build RedBPF with vmlinux on aarch64 Ubuntu 22.04 run: | docker run --privileged \ -v $PWD:/build \ -w /build \ - $REDBPF_IMAGE_NAME:latest-aarch64-ubuntu21.04 \ + $REDBPF_IMAGE_NAME:latest-aarch64-ubuntu22.04 \ /bin/bash -c 'export REDBPF_VMLINUX=/boot/vmlinux-btf \ && cargo clean \ && cargo build \ @@ -355,9 +355,9 @@ jobs: && cargo build --bin cargo-bpf \ && cargo build --features=kernel5_8 --examples' - # Split aarch64-fedora35 into two parts because its running time exceeds 6 + # Split aarch64-fedora36 into two parts because its running time exceeds 6 # hours, the maximum time for build test. - build-on-aarch64-fedora35-header: + build-on-aarch64-fedora36-header: runs-on: ubuntu-latest steps: - @@ -381,12 +381,12 @@ jobs: name: Available platforms run: echo ${{ steps.qemu.outputs.platforms }} - - name: Build RedBPF with the kernel headers on aarch64 Fedora 35 + name: Build RedBPF with the kernel headers on aarch64 Fedora 36 run: | docker run --privileged \ -v $PWD:/build \ -w /build \ - $REDBPF_IMAGE_NAME:latest-aarch64-fedora35 \ + $REDBPF_IMAGE_NAME:latest-aarch64-fedora36 \ /bin/bash -c 'export KERNEL_SOURCE=$(echo /lib/modules/*) \ && echo KERNEL_SOURCE=$KERNEL_SOURCE \ && cargo clean \ @@ -394,7 +394,7 @@ jobs: && cargo build --bin cargo-bpf \ && cargo build --features=kernel5_8 --examples' - build-on-aarch64-fedora35-vmlinux: + build-on-aarch64-fedora36-vmlinux: runs-on: ubuntu-latest steps: - @@ -418,20 +418,20 @@ jobs: name: Available platforms run: echo ${{ steps.qemu.outputs.platforms }} - - name: Build RedBPF with vmlinux on aarch64 Fedora 35 + name: Build RedBPF with vmlinux on aarch64 Fedora 36 run: | docker run --privileged \ -v $PWD:/build \ -w /build \ - $REDBPF_IMAGE_NAME:latest-aarch64-fedora35 \ + $REDBPF_IMAGE_NAME:latest-aarch64-fedora36 \ /bin/bash -c 'export REDBPF_VMLINUX=/boot/vmlinux-btf \ && cargo clean \ && cargo build \ && cargo build --bin cargo-bpf \ && cargo build --features=kernel5_8 --examples' - build-on-aarch64-alpine315: - name: Alpine 3.15 is disabled temporarily (LLVM13 unsupported) + build-on-aarch64-alpine-edge: + name: Alpine Edge if: false runs-on: ubuntu-latest steps: @@ -456,19 +456,19 @@ jobs: name: Available platforms run: echo ${{ steps.qemu.outputs.platforms }} - - name: Build RedBPF with the kernel headers on aarch64 Alpine 3.15 + name: Build RedBPF with the kernel headers on aarch64 Alpine Edge run: | docker run --privileged \ -v $PWD:/build \ -w /build \ - $REDBPF_IMAGE_NAME:latest-aarch64-alpine3.15 \ + $REDBPF_IMAGE_NAME:latest-aarch64-alpine-edge \ /bin/bash -c 'export KERNEL_SOURCE=$(echo /lib/modules/*) \ && echo KERNEL_SOURCE=$KERNEL_SOURCE \ && export RUSTFLAGS=-Ctarget-feature=-crt-static \ && cargo clean \ && cargo build \ && cargo build --bin cargo-bpf \ - && cargo build --features=kernel5_8 --examples' + && cargo build --features="kernel5_8 llvm13" --examples' build-on-aarch64-ubuntu-2004: runs-on: ubuntu-latest @@ -535,20 +535,20 @@ jobs: runs-on: ubuntu-latest if: startsWith( github.ref, 'refs/tags/v') needs: - - build-on-x86_64-ubuntu-2104 + - build-on-x86_64-ubuntu-2204 - build-on-x86_64-debian11 - - build-on-x86_64-fedora35 - # - build-on-x86_64-alpine315 + - build-on-x86_64-fedora36 + - build-on-x86_64-alpine-edge - build-on-x86_64-archlinux - build-on-x86_64-ubuntu-2004 - build-on-x86_64-gentoo - build-on-x86_64-nix-stable - build-on-x86_64-nix-unstable - - build-on-aarch64-ubuntu-2104 + - build-on-aarch64-ubuntu-2204 - build-on-aarch64-debian11 - - build-on-aarch64-fedora35-header - - build-on-aarch64-fedora35-vmlinux - # - build-on-aarch64-alpine315 + - build-on-aarch64-fedora36-header + - build-on-aarch64-fedora36-vmlinux + - build-on-aarch64-alpine-edge - build-on-aarch64-ubuntu-2004 steps: From bca45c00c6e784ec3544b433aee901cd21a05547 Mon Sep 17 00:00:00 2001 From: Nikita Baksalyar Date: Fri, 6 May 2022 23:13:43 +0100 Subject: [PATCH 3/4] Update iotop probe to support latest kernels rq_disk was removed from the request struct. Use request.q.disk instead. Signed-off-by: Nikita Baksalyar --- redbpf-tools/probes/src/iotop/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redbpf-tools/probes/src/iotop/main.rs b/redbpf-tools/probes/src/iotop/main.rs index 3540444..d7d908a 100644 --- a/redbpf-tools/probes/src/iotop/main.rs +++ b/redbpf-tools/probes/src/iotop/main.rs @@ -49,7 +49,7 @@ fn do_complete(regs: Registers) -> Option<()> { let delta_us = (bpf_ktime_get_ns() - start_ts) / 1000u64; let request = unsafe { &*req }; - let rq_disk = unsafe { &*request.rq_disk()? }; + let rq_disk = unsafe { &*(&*request.q()?).disk }; let major = rq_disk.major()?; let minor = rq_disk.first_minor()?; let write = (request.cmd_flags()? & REQ_OP_WRITE != 0) as u64; From 339886875f1dbf3ad9fb9e059c92e2cb7ef6b677 Mon Sep 17 00:00:00 2001 From: Nikita Baksalyar Date: Fri, 6 May 2022 23:15:28 +0100 Subject: [PATCH 4/4] Fix LLVM versions reference in readme Signed-off-by: Nikita Baksalyar --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2f62936..caabdbb 100644 --- a/README.md +++ b/README.md @@ -242,7 +242,7 @@ There are two LLVM versions involved in compiling BPF programs: | Rust version | LLVM version of the rustc | Valid LLVM version of system | |:-------------|:-------------------------:|:-----------------------------| -| 1.56 ~ 1.60 | LLVM 13 | LLVM 13 and newer | +| 1.56 ~ 1.59 | LLVM 13 | LLVM 13 and newer | | 1.60 ~ | LLVM 14 | LLVM 14 and newer | ## Docker images for RedBPF build test