integration-test: avoid reliance on kernel headers

This should allow us to build on any system.
reviewable/pr727/r1
Tamir Duberstein 2 years ago
parent f56effedd3
commit 041aac4f58

@ -5,7 +5,7 @@ edition = "2021"
publish = false publish = false
[dependencies] [dependencies]
anyhow = { workspace = true, default-features = true } anyhow = { workspace = true, features = ["std"] }
assert_matches = { workspace = true } assert_matches = { workspace = true }
aya = { workspace = true } aya = { workspace = true }
aya-log = { workspace = true } aya-log = { workspace = true }
@ -16,7 +16,7 @@ netns-rs = { workspace = true }
object = { workspace = true } object = { workspace = true }
rbpf = { workspace = true } rbpf = { workspace = true }
test-case = { workspace = true } test-case = { workspace = true }
tokio = { workspace = true, default-features = false, features = [ tokio = { workspace = true, features = [
"macros", "macros",
"time", "time",
] } ] }

@ -1,10 +1,9 @@
#include <linux/bpf.h> // clang-format off
#include <vmlinux.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
// clang-format on
SEC("xdp") SEC("xdp")
int xdp_drop(struct xdp_md *ctx) int xdp_drop(struct xdp_md *ctx) { return XDP_DROP; }
{
return XDP_DROP;
}
char _license[] SEC("license") = "GPL"; char _license[] SEC("license") = "GPL";

@ -1,10 +1,9 @@
#include <linux/bpf.h> // clang-format off
#include <vmlinux.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
// clang-format on
SEC("xdp") SEC("xdp")
int xdp_pass(struct xdp_md *ctx) int xdp_pass(struct xdp_md *ctx) { return XDP_PASS; }
{
return XDP_PASS;
}
char _license[] SEC("license") = "GPL"; char _license[] SEC("license") = "GPL";

@ -1,30 +1,30 @@
#include <linux/bpf.h> // clang-format off
#include <vmlinux.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
// clang-format on
struct { struct {
__uint(type, BPF_MAP_TYPE_ARRAY); __uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32); __type(key, __u32);
__type(value, __u64); __type(value, __u64);
__uint(max_entries, 1); __uint(max_entries, 1);
} map_1 SEC(".maps"); } map_1 SEC(".maps");
struct { struct {
__uint(type, BPF_MAP_TYPE_ARRAY); __uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32); __type(key, __u32);
__type(value, __u64); __type(value, __u64);
__uint(max_entries, 1); __uint(max_entries, 1);
} map_2 SEC(".maps"); } map_2 SEC(".maps");
SEC("tracepoint") SEC("tracepoint")
int bpf_prog(void *ctx) int bpf_prog(void *ctx) {
{ __u32 key = 0;
__u32 key = 0; __u64 twenty_four = 24;
__u64 twenty_four = 24; __u64 forty_two = 42;
__u64 forty_two = 42; bpf_map_update_elem(&map_1, &key, &twenty_four, BPF_ANY);
bpf_map_update_elem(&map_1, &key, &twenty_four, BPF_ANY); bpf_map_update_elem(&map_2, &key, &forty_two, BPF_ANY);
bpf_map_update_elem(&map_2, &key, &forty_two, BPF_ANY); return 0;
return 0;
} }
char _license[] SEC("license") = "GPL"; char _license[] SEC("license") = "GPL";

@ -1,5 +1,5 @@
// clang-format off // clang-format off
#include <linux/bpf.h> #include <vmlinux.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h> #include <bpf/bpf_core_read.h>
// clang-format on // clang-format on
@ -57,38 +57,40 @@ __noinline int struct_flavors_global() {
SEC("uprobe") int struct_flavors(void *ctx) { return struct_flavors_global(); } SEC("uprobe") int struct_flavors(void *ctx) { return struct_flavors_global(); }
enum relocated_enum_unsigned_32 { U32 = 0xAAAAAAAA }; enum relocated_enum_unsigned_32 { MyU32 = 0xAAAAAAAA };
__noinline int enum_unsigned_32_global() { __noinline int enum_unsigned_32_global() {
return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_32, U32)); return set_output(
bpf_core_enum_value(enum relocated_enum_unsigned_32, MyU32));
} }
SEC("uprobe") int enum_unsigned_32(void *ctx) { SEC("uprobe") int enum_unsigned_32(void *ctx) {
return enum_unsigned_32_global(); return enum_unsigned_32_global();
} }
enum relocated_enum_signed_32 { S32 = -0x7AAAAAAA }; enum relocated_enum_signed_32 { MyS32 = -0x7AAAAAAA };
__noinline int enum_signed_32_global() { __noinline int enum_signed_32_global() {
return set_output(bpf_core_enum_value(enum relocated_enum_signed_32, S32)); return set_output(bpf_core_enum_value(enum relocated_enum_signed_32, MyS32));
} }
SEC("uprobe") int enum_signed_32(void *ctx) { return enum_signed_32_global(); } SEC("uprobe") int enum_signed_32(void *ctx) { return enum_signed_32_global(); }
enum relocated_enum_unsigned_64 { U64 = 0xAAAAAAAABBBBBBBB }; enum relocated_enum_unsigned_64 { MyU64 = 0xAAAAAAAABBBBBBBB };
__noinline int enum_unsigned_64_global() { __noinline int enum_unsigned_64_global() {
return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_64, U64)); return set_output(
bpf_core_enum_value(enum relocated_enum_unsigned_64, MyU64));
} }
SEC("uprobe") int enum_unsigned_64(void *ctx) { SEC("uprobe") int enum_unsigned_64(void *ctx) {
return enum_unsigned_64_global(); return enum_unsigned_64_global();
} }
enum relocated_enum_signed_64 { u64 = -0xAAAAAAABBBBBBBB }; enum relocated_enum_signed_64 { MyS64 = -0xAAAAAAABBBBBBBB };
__noinline int enum_signed_64_global() { __noinline int enum_signed_64_global() {
return set_output(bpf_core_enum_value(enum relocated_enum_signed_64, u64)); return set_output(bpf_core_enum_value(enum relocated_enum_signed_64, MyS64));
} }
SEC("uprobe") int enum_signed_64(void *ctx) { return enum_signed_64_global(); } SEC("uprobe") int enum_signed_64(void *ctx) { return enum_signed_64_global(); }

@ -1,12 +1,10 @@
// clang-format off // clang-format off
#include <linux/bpf.h> #include <vmlinux.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h> #include <bpf/bpf_core_read.h>
// clang-format on // clang-format on
#include <stdlib.h> long set_output(__u64 value) { return 0; }
long set_output(__u64 value) { exit((int)value); }
struct relocated_struct_with_scalars { struct relocated_struct_with_scalars {
__u8 b; __u8 b;
@ -41,28 +39,30 @@ __attribute__((noinline)) int struct_flavors_global() {
} }
} }
enum relocated_enum_unsigned_32 { U32 = 0xBBBBBBBB }; enum relocated_enum_unsigned_32 { MyU32 = 0xBBBBBBBB };
__attribute__((noinline)) int enum_unsigned_32_global() { __attribute__((noinline)) int enum_unsigned_32_global() {
return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_32, U32)); return set_output(
bpf_core_enum_value(enum relocated_enum_unsigned_32, MyU32));
} }
enum relocated_enum_signed_32 { S32 = -0x7BBBBBBB }; enum relocated_enum_signed_32 { MyS32 = -0x7BBBBBBB };
__attribute__((noinline)) int enum_signed_32_global() { __attribute__((noinline)) int enum_signed_32_global() {
return set_output(bpf_core_enum_value(enum relocated_enum_signed_32, S32)); return set_output(bpf_core_enum_value(enum relocated_enum_signed_32, MyS32));
} }
enum relocated_enum_unsigned_64 { U64 = 0xCCCCCCCCDDDDDDDD }; enum relocated_enum_unsigned_64 { MyU64 = 0xCCCCCCCCDDDDDDDD };
__attribute__((noinline)) int enum_unsigned_64_global() { __attribute__((noinline)) int enum_unsigned_64_global() {
return set_output(bpf_core_enum_value(enum relocated_enum_unsigned_64, U64)); return set_output(
bpf_core_enum_value(enum relocated_enum_unsigned_64, MyU64));
} }
enum relocated_enum_signed_64 { u64 = -0xCCCCCCCDDDDDDDD }; enum relocated_enum_signed_64 { MyS64 = -0xCCCCCCCDDDDDDDD };
__attribute__((noinline)) int enum_signed_64_global() { __attribute__((noinline)) int enum_signed_64_global() {
return set_output(bpf_core_enum_value(enum relocated_enum_signed_64, u64)); return set_output(bpf_core_enum_value(enum relocated_enum_signed_64, MyS64));
} }
// Avoids dead code elimination by the compiler. // Avoids dead code elimination by the compiler.

@ -1,28 +1,25 @@
#include <linux/bpf.h> // clang-format off
#include <vmlinux.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
// clang-format on
char _license[] SEC("license") = "GPL"; char _license[] SEC("license") = "GPL";
struct { struct {
__uint(type, BPF_MAP_TYPE_ARRAY); __uint(type, BPF_MAP_TYPE_ARRAY);
__type(key, __u32); __type(key, __u32);
__type(value, __u64); __type(value, __u64);
__uint(max_entries, 2); __uint(max_entries, 2);
} RESULTS SEC(".maps"); } RESULTS SEC(".maps");
static __u64 static __u64 inc_cb(void *map, __u32 *key, void *val, void *data) {
inc_cb(void *map, __u32 *key, void *val, __u64 *value = val;
void *data) *value += 1;
{ return 0;
__u64 *value = val;
*value += 1;
return 0;
} }
SEC("uprobe/test_text_64_64_reloc") SEC("uprobe/test_text_64_64_reloc")
int test_text_64_64_reloc(struct pt_regs *ctx) int test_text_64_64_reloc(struct pt_regs *ctx) {
{ bpf_for_each_map_elem(&RESULTS, inc_cb, NULL, 0);
bpf_for_each_map_elem(&RESULTS, inc_cb, NULL, 0); return 0;
return 0;
} }

@ -116,34 +116,33 @@ fn main() {
target_arch.push(arch); target_arch.push(arch);
}; };
let libbpf_vmlinux_dir = libbpf_dir.join(".github/actions/build-selftests");
let clang = || {
let mut cmd = Command::new("clang");
cmd.arg("-nostdlibinc")
.arg("-I")
.arg(&libbpf_headers_dir)
.arg("-I")
.arg(&libbpf_vmlinux_dir)
.args(["-g", "-O2", "-target", target, "-c"])
.arg(&target_arch);
cmd
};
for (src, dst) in c_bpf { for (src, dst) in c_bpf {
let src = bpf_dir.join(src); let src = bpf_dir.join(src);
println!("cargo:rerun-if-changed={}", src.to_str().unwrap()); println!("cargo:rerun-if-changed={}", src.to_str().unwrap());
exec( exec(clang().arg(src).arg("-o").arg(dst)).unwrap();
Command::new("clang")
.arg("-I")
.arg(&libbpf_headers_dir)
.args(["-g", "-O2", "-target", target, "-c"])
.arg(&target_arch)
.arg(src)
.arg("-o")
.arg(dst),
)
.unwrap();
} }
for (src, dst) in c_btf { for (src, dst) in c_btf {
let src = bpf_dir.join(src); let src = bpf_dir.join(src);
println!("cargo:rerun-if-changed={}", src.to_str().unwrap()); println!("cargo:rerun-if-changed={}", src.to_str().unwrap());
let mut cmd = Command::new("clang"); let mut cmd = clang();
cmd.arg("-I") cmd.arg(src).args(["-o", "-"]);
.arg(&libbpf_headers_dir)
.args(["-g", "-target", target, "-c"])
.arg(&target_arch)
.arg(src)
.args(["-o", "-"]);
let mut child = cmd let mut child = cmd
.stdout(Stdio::piped()) .stdout(Stdio::piped())

@ -2,20 +2,20 @@ use test_case::test_case;
use aya::{maps::Array, programs::UProbe, util::KernelVersion, BpfLoader, Btf, Endianness}; use aya::{maps::Array, programs::UProbe, util::KernelVersion, BpfLoader, Btf, Endianness};
#[test_case("field", false, None, 2)] #[test_case("enum_signed_32", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7AAAAAAAi32 as u64)]
#[test_case("field", true, None, 1)] #[test_case("enum_signed_32", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7BBBBBBBi32 as u64)]
#[test_case("enum_signed_64", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0xAAAAAAABBBBBBBBi64 as u64)]
#[test_case("enum_signed_64", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0xCCCCCCCDDDDDDDDi64 as u64)]
#[test_case("enum_unsigned_32", false, None, 0xAAAAAAAA)] #[test_case("enum_unsigned_32", false, None, 0xAAAAAAAA)]
#[test_case("enum_unsigned_32", true, None, 0xBBBBBBBB)] #[test_case("enum_unsigned_32", true, None, 0xBBBBBBBB)]
#[test_case("enum_unsigned_64", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), 0xAAAAAAAABBBBBBBB)]
#[test_case("enum_unsigned_64", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), 0xCCCCCCCCDDDDDDDD)]
#[test_case("field", false, None, 2)]
#[test_case("field", true, None, 1)]
#[test_case("pointer", false, None, 42)] #[test_case("pointer", false, None, 42)]
#[test_case("pointer", true, None, 21)] #[test_case("pointer", true, None, 21)]
#[test_case("struct_flavors", false, None, 1)] #[test_case("struct_flavors", false, None, 1)]
#[test_case("struct_flavors", true, None, 1)] #[test_case("struct_flavors", true, None, 1)]
#[test_case("enum_signed_32", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7AAAAAAAi32 as u64)]
#[test_case("enum_signed_32", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0x7BBBBBBBi32 as u64)]
#[test_case("enum_unsigned_64", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), 0xAAAAAAAABBBBBBBB)]
#[test_case("enum_unsigned_64", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), 0xCCCCCCCCDDDDDDDD)]
#[test_case("enum_signed_64", false, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0xAAAAAAABBBBBBBBi64 as u64)]
#[test_case("enum_signed_64", true, Some((KernelVersion::new(6, 0, 0), "https://github.com/torvalds/linux/commit/6089fb3")), -0xCCCCCCCDDDDDDDDi64 as u64)]
fn relocation_tests( fn relocation_tests(
program: &str, program: &str,
with_relocations: bool, with_relocations: bool,

@ -6,10 +6,10 @@ edition = "2021"
publish = false publish = false
[dependencies] [dependencies]
anyhow = { workspace = true, default-features = true } anyhow = { workspace = true, features = ["std"] }
aya-tool = { workspace = true } aya-tool = { workspace = true }
cargo_metadata = { workspace = true } cargo_metadata = { workspace = true }
clap = { workspace = true, default-features = true, features = ["derive"] } clap = { workspace = true, features = ["derive"] }
dialoguer = { workspace = true } dialoguer = { workspace = true }
diff = { workspace = true } diff = { workspace = true }
indoc = { workspace = true } indoc = { workspace = true }

Loading…
Cancel
Save