From 5964d3d255bcfaa3e4cd21009ec2f2dd4cd4f2f1 Mon Sep 17 00:00:00 2001 From: Mahendra Paipuri Date: Sun, 29 Sep 2024 16:06:19 +0200 Subject: [PATCH] feat: Add ebpf support for mips,ppc and risc archs * Test if the bpf assets can be compiled for different archs in CI. * We are only ensuring the bpf assets are able to compile. No guarantee that these work in prod as we are not doing any functional testing. Signed-off-by: Mahendra Paipuri --- .github/workflows/step_tests-unit.yml | 4 +- .promu-go.yml | 12 +- Makefile | 13 ++- pkg/collector/bpf/Makefile.common | 32 +++-- pkg/collector/bpf/include/vmlinux.h | 161 ++++++++++++++++++++++++++ scripts/checkbpf.sh | 19 +++ 6 files changed, 223 insertions(+), 18 deletions(-) create mode 100755 scripts/checkbpf.sh diff --git a/.github/workflows/step_tests-unit.yml b/.github/workflows/step_tests-unit.yml index 5e98fd10..04d29a21 100644 --- a/.github/workflows/step_tests-unit.yml +++ b/.github/workflows/step_tests-unit.yml @@ -20,8 +20,8 @@ jobs: - name: Setup clang 18 run: ./scripts/install_clang.sh - - name: Run checkmetrics and checkrules - run: make checkmetrics checkrules + - name: Run checkmetrics, checkrules and checkbpf + run: make checkmetrics checkrules checkbpf - name: Run unit tests for Go packages run: make test diff --git a/.promu-go.yml b/.promu-go.yml index 6548f29f..dc85ea24 100644 --- a/.promu-go.yml +++ b/.promu-go.yml @@ -27,11 +27,11 @@ crossbuild: - linux/386 - linux/amd64 - linux/arm64 - # - linux/mips - # - linux/mips64 - # - linux/mips64le - # - linux/mipsle + - linux/mips + - linux/mips64 + - linux/mips64le + - linux/mipsle # - linux/ppc64 - # - linux/ppc64le - # - linux/riscv64 + - linux/ppc64le + - linux/riscv64 # - linux/s390x diff --git a/Makefile b/Makefile index 927fed04..c6a4b6ed 100644 --- a/Makefile +++ b/Makefile @@ -57,6 +57,7 @@ ifeq ($(CGO_BUILD), 1) ./cmd/ceems_lb checkmetrics := skip-checkmetrics checkrules := skip-checkrules + checkbpf := skip-checkbpf # go test flags coverage-file := coverage-cgo.out @@ -67,6 +68,7 @@ else ./internal/security ./cmd/ceems_exporter checkmetrics := checkmetrics checkrules := checkrules + checkbpf := checkbpf # go test flags coverage-file := coverage-go.out @@ -107,7 +109,7 @@ $(eval $(call goarch_pair,amd64,386)) $(eval $(call goarch_pair,mips64,mips)) $(eval $(call goarch_pair,mips64el,mipsel)) -all:: vet common-all $(cross-test) $(test-docker) $(checkmetrics) $(checkrules) $(test-e2e) +all:: vet common-all $(cross-test) $(test-docker) $(checkmetrics) $(checkrules) $(checkbpf) $(test-e2e) .PHONY: coverage coverage: @@ -262,6 +264,15 @@ checkrules: $(PROMTOOL) skip-checkrules: $(PROMTOOL) @echo ">> SKIP checking rules for correctness" +.PHONY: checkbpf +checkbpf: $(PROMTOOL) + @echo ">> checking bpf assets compilation" + ./scripts/checkbpf.sh + +.PHONY: skip-checkbpf +skip-checkbpf: $(PROMTOOL) + @echo ">> SKIP checking bpf assets compilation" + .PHONY: test-docker test-docker: @echo ">> testing docker image" diff --git a/pkg/collector/bpf/Makefile.common b/pkg/collector/bpf/Makefile.common index e617a882..4fc51f3d 100644 --- a/pkg/collector/bpf/Makefile.common +++ b/pkg/collector/bpf/Makefile.common @@ -30,6 +30,15 @@ else ifeq ($(GOARCH),arm64) else ifeq ($(GOARCH),mips) BPF_TARGET_ARCH = mips BPF_TARGET_MARCH = bpfeb +else ifeq ($(GOARCH),mipsle) + BPF_TARGET_ARCH = mips + BPF_TARGET_MARCH = bpfel +else ifeq ($(GOARCH),mips64) + BPF_TARGET_ARCH = mips + BPF_TARGET_MARCH = bpfeb +else ifeq ($(GOARCH),mips64le) + BPF_TARGET_ARCH = mips + BPF_TARGET_MARCH = bpfel else ifeq ($(GOARCH),ppc64) BPF_TARGET_ARCH = powerpc BPF_TARGET_MARCH = bpfeb @@ -45,15 +54,20 @@ else ifeq ($(GOARCH),s390x) endif # Do not compile BPF assets for mipsle, mips64 and mips64le architectures -ifeq ($(GOARCH),mipsle) - BPF_TARGET_COMPILE = 0 -endif -ifeq ($(GOARCH),mips64) - BPF_TARGET_COMPILE = 0 -endif -ifeq ($(GOARCH),mips64le) - BPF_TARGET_COMPILE = 0 -endif +# Update 20240929: We are compiling all mips now as we added type defs in +# vmlinux.h. +# Atleast clang compiles them but not sure if it works in prod as we dont +# test them in CI. +# +# ifeq ($(GOARCH),mipsle) +# BPF_TARGET_COMPILE = 0 +# endif +# ifeq ($(GOARCH),mips64) +# BPF_TARGET_COMPILE = 0 +# endif +# ifeq ($(GOARCH),mips64le) +# BPF_TARGET_COMPILE = 0 +# endif ROOT_DIR := $(dir $(lastword $(MAKEFILE_LIST))) diff --git a/pkg/collector/bpf/include/vmlinux.h b/pkg/collector/bpf/include/vmlinux.h index 2e692b64..073f8179 100644 --- a/pkg/collector/bpf/include/vmlinux.h +++ b/pkg/collector/bpf/include/vmlinux.h @@ -1472,6 +1472,126 @@ struct pt_regs { u64 exit_rcu; }; +#elif defined(__TARGET_ARCH_mips) +/* + * Definitions for the following architectures are nicked from + * https://github.com/eunomia-bpf/vmlinux/tree/main + */ + +struct user_pt_regs { + __u64 regs[32]; + __u64 lo; + __u64 hi; + __u64 cp0_epc; + __u64 cp0_badvaddr; + __u64 cp0_status; + __u64 cp0_cause; +}; + +struct pt_regs { + long unsigned int pad0[8]; + long unsigned int regs[32]; + long unsigned int cp0_status; + long unsigned int hi; + long unsigned int lo; + long unsigned int cp0_badvaddr; + long unsigned int cp0_cause; + long unsigned int cp0_epc; + long unsigned int __last[0]; +}; + +#elif defined(__TARGET_ARCH_powerpc) + +struct user_pt_regs { + long unsigned int gpr[32]; + long unsigned int nip; + long unsigned int msr; + long unsigned int orig_gpr3; + long unsigned int ctr; + long unsigned int link; + long unsigned int xer; + long unsigned int ccr; + long unsigned int softe; + long unsigned int trap; + long unsigned int dar; + long unsigned int dsisr; + long unsigned int result; +}; + +struct pt_regs { + union { + struct user_pt_regs user_regs; + struct { + long unsigned int gpr[32]; + long unsigned int nip; + long unsigned int msr; + long unsigned int orig_gpr3; + long unsigned int ctr; + long unsigned int link; + long unsigned int xer; + long unsigned int ccr; + long unsigned int softe; + long unsigned int trap; + long unsigned int dar; + long unsigned int dsisr; + long unsigned int result; + }; + }; + union { + struct { + long unsigned int ppr; + long unsigned int exit_result; + union { + long unsigned int kuap; + long unsigned int amr; + }; + long unsigned int iamr; + }; + long unsigned int __pad[4]; + }; +}; + +#elif defined(__TARGET_ARCH_riscv) + +struct pt_regs { + long unsigned int epc; + long unsigned int ra; + long unsigned int sp; + long unsigned int gp; + long unsigned int tp; + long unsigned int t0; + long unsigned int t1; + long unsigned int t2; + long unsigned int s0; + long unsigned int s1; + long unsigned int a0; + long unsigned int a1; + long unsigned int a2; + long unsigned int a3; + long unsigned int a4; + long unsigned int a5; + long unsigned int a6; + long unsigned int a7; + long unsigned int s2; + long unsigned int s3; + long unsigned int s4; + long unsigned int s5; + long unsigned int s6; + long unsigned int s7; + long unsigned int s8; + long unsigned int s9; + long unsigned int s10; + long unsigned int s11; + long unsigned int t3; + long unsigned int t4; + long unsigned int t5; + long unsigned int t6; + long unsigned int status; + long unsigned int badaddr; + long unsigned int cause; + long unsigned int orig_a0; +}; + #endif struct desc_ptr { @@ -21518,6 +21638,45 @@ struct user_i387_struct { __u32 padding[24]; }; +#if defined(__TARGET_ARCH_riscv) + +struct user_regs_struct { + long unsigned int pc; + long unsigned int ra; + long unsigned int sp; + long unsigned int gp; + long unsigned int tp; + long unsigned int t0; + long unsigned int t1; + long unsigned int t2; + long unsigned int s0; + long unsigned int s1; + long unsigned int a0; + long unsigned int a1; + long unsigned int a2; + long unsigned int a3; + long unsigned int a4; + long unsigned int a5; + long unsigned int a6; + long unsigned int a7; + long unsigned int s2; + long unsigned int s3; + long unsigned int s4; + long unsigned int s5; + long unsigned int s6; + long unsigned int s7; + long unsigned int s8; + long unsigned int s9; + long unsigned int s10; + long unsigned int s11; + long unsigned int t3; + long unsigned int t4; + long unsigned int t5; + long unsigned int t6; +}; + +#else + struct user_regs_struct { long unsigned int r15; long unsigned int r14; @@ -21548,6 +21707,8 @@ struct user_regs_struct { long unsigned int gs; }; +#endif + struct user { struct user_regs_struct regs; int u_fpvalid; diff --git a/scripts/checkbpf.sh b/scripts/checkbpf.sh new file mode 100755 index 00000000..3225fc8b --- /dev/null +++ b/scripts/checkbpf.sh @@ -0,0 +1,19 @@ +#!/bin/bash +set -exo pipefail + +# Change directory +cd pkg/collector/bpf + +# List of archs to test +declare -a archs=("386" "amd64" "arm64" "mips" "mipsle" "mips64" "mips64le" "ppc64le" "riscv64") + +# Test if we can compile bpf assets for all these archs +for arch in "${archs[@]}" +do + echo "Compiling bpf assets for $arch" + make clean + GOARCH="$arch" make +done + +# Clean up all the assets +make clean