|
|
@ -4,13 +4,13 @@ use std::{
|
|
|
|
ffi::{OsStr, OsString},
|
|
|
|
ffi::{OsStr, OsString},
|
|
|
|
fs,
|
|
|
|
fs,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
process::{Command, Output},
|
|
|
|
process::Command,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
use anyhow::{bail, Context};
|
|
|
|
use anyhow::Result;
|
|
|
|
use clap::Parser;
|
|
|
|
use clap::Parser;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::utils::workspace_root;
|
|
|
|
use crate::utils::{exec, workspace_root};
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum Architecture {
|
|
|
|
pub enum Architecture {
|
|
|
@ -49,61 +49,56 @@ pub struct BuildEbpfOptions {
|
|
|
|
pub libbpf_dir: PathBuf,
|
|
|
|
pub libbpf_dir: PathBuf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn build_ebpf(opts: BuildEbpfOptions) -> anyhow::Result<()> {
|
|
|
|
pub fn build_ebpf(opts: BuildEbpfOptions) -> Result<()> {
|
|
|
|
build_rust_ebpf(&opts)?;
|
|
|
|
build_rust_ebpf(&opts)?;
|
|
|
|
build_c_ebpf(&opts)
|
|
|
|
build_c_ebpf(&opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn build_rust_ebpf(opts: &BuildEbpfOptions) -> anyhow::Result<()> {
|
|
|
|
fn build_rust_ebpf(opts: &BuildEbpfOptions) -> Result<()> {
|
|
|
|
|
|
|
|
let BuildEbpfOptions {
|
|
|
|
|
|
|
|
target,
|
|
|
|
|
|
|
|
libbpf_dir: _,
|
|
|
|
|
|
|
|
} = opts;
|
|
|
|
|
|
|
|
|
|
|
|
let mut dir = PathBuf::from(workspace_root());
|
|
|
|
let mut dir = PathBuf::from(workspace_root());
|
|
|
|
dir.push("test/integration-ebpf");
|
|
|
|
dir.push("test/integration-ebpf");
|
|
|
|
|
|
|
|
|
|
|
|
let target = format!("--target={}", opts.target);
|
|
|
|
exec(
|
|
|
|
let args = vec![
|
|
|
|
Command::new("cargo")
|
|
|
|
"+nightly",
|
|
|
|
|
|
|
|
"build",
|
|
|
|
|
|
|
|
"--release",
|
|
|
|
|
|
|
|
"--verbose",
|
|
|
|
|
|
|
|
target.as_str(),
|
|
|
|
|
|
|
|
"-Z",
|
|
|
|
|
|
|
|
"build-std=core",
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
let status = Command::new("cargo")
|
|
|
|
|
|
|
|
.current_dir(&dir)
|
|
|
|
.current_dir(&dir)
|
|
|
|
.args(&args)
|
|
|
|
.args(["+nightly", "build", "--release", "--target"])
|
|
|
|
.status()
|
|
|
|
.arg(target.to_string())
|
|
|
|
.expect("failed to build bpf program");
|
|
|
|
.args(["-Z", "build-std=core"])
|
|
|
|
assert!(status.success());
|
|
|
|
.current_dir(&dir),
|
|
|
|
Ok(())
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn get_libbpf_headers<P: AsRef<Path>>(libbpf_dir: P, include_path: P) -> anyhow::Result<()> {
|
|
|
|
fn get_libbpf_headers(libbpf_dir: &Path, include_path: &Path) -> Result<()> {
|
|
|
|
let dir = include_path.as_ref();
|
|
|
|
fs::create_dir_all(include_path)?;
|
|
|
|
fs::create_dir_all(dir)?;
|
|
|
|
|
|
|
|
let mut includedir = OsString::new();
|
|
|
|
let mut includedir = OsString::new();
|
|
|
|
includedir.push("INCLUDEDIR=");
|
|
|
|
includedir.push("INCLUDEDIR=");
|
|
|
|
includedir.push(dir.as_os_str());
|
|
|
|
includedir.push(include_path);
|
|
|
|
let status = Command::new("make")
|
|
|
|
exec(
|
|
|
|
.current_dir(libbpf_dir.as_ref().join("src"))
|
|
|
|
Command::new("make")
|
|
|
|
|
|
|
|
.current_dir(libbpf_dir.join("src"))
|
|
|
|
.arg(includedir)
|
|
|
|
.arg(includedir)
|
|
|
|
.arg("install_headers")
|
|
|
|
.arg("install_headers"),
|
|
|
|
.status()
|
|
|
|
)
|
|
|
|
.expect("failed to build get libbpf headers");
|
|
|
|
|
|
|
|
assert!(status.success());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn build_c_ebpf(opts: &BuildEbpfOptions) -> anyhow::Result<()> {
|
|
|
|
fn build_c_ebpf(opts: &BuildEbpfOptions) -> Result<()> {
|
|
|
|
|
|
|
|
let BuildEbpfOptions { target, libbpf_dir } = opts;
|
|
|
|
|
|
|
|
|
|
|
|
let mut src = PathBuf::from(workspace_root());
|
|
|
|
let mut src = PathBuf::from(workspace_root());
|
|
|
|
src.push("test/integration-ebpf/src/bpf");
|
|
|
|
src.push("test/integration-ebpf/src/bpf");
|
|
|
|
|
|
|
|
|
|
|
|
let mut out_path = PathBuf::from(workspace_root());
|
|
|
|
let mut out_path = PathBuf::from(workspace_root());
|
|
|
|
out_path.push("target");
|
|
|
|
out_path.push("target");
|
|
|
|
out_path.push(opts.target.to_string());
|
|
|
|
out_path.push(target.to_string());
|
|
|
|
out_path.push("release");
|
|
|
|
out_path.push("release");
|
|
|
|
|
|
|
|
|
|
|
|
let include_path = out_path.join("include");
|
|
|
|
let include_path = out_path.join("include");
|
|
|
|
get_libbpf_headers(&opts.libbpf_dir, &include_path)?;
|
|
|
|
get_libbpf_headers(libbpf_dir, &include_path)?;
|
|
|
|
let files = fs::read_dir(&src).unwrap();
|
|
|
|
let files = fs::read_dir(&src).unwrap();
|
|
|
|
for file in files {
|
|
|
|
for file in files {
|
|
|
|
let p = file.unwrap().path();
|
|
|
|
let p = file.unwrap().path();
|
|
|
@ -120,11 +115,7 @@ fn build_c_ebpf(opts: &BuildEbpfOptions) -> anyhow::Result<()> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Build eBPF programs with clang and libbpf headers.
|
|
|
|
/// Build eBPF programs with clang and libbpf headers.
|
|
|
|
fn compile_with_clang<P: Clone + AsRef<Path>>(
|
|
|
|
fn compile_with_clang(src: &Path, out: &Path, include_path: &Path) -> Result<()> {
|
|
|
|
src: P,
|
|
|
|
|
|
|
|
out: P,
|
|
|
|
|
|
|
|
include_path: P,
|
|
|
|
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
|
|
|
|
let clang: Cow<'_, _> = match env::var_os("CLANG") {
|
|
|
|
let clang: Cow<'_, _> = match env::var_os("CLANG") {
|
|
|
|
Some(val) => val.into(),
|
|
|
|
Some(val) => val.into(),
|
|
|
|
None => OsStr::new("/usr/bin/clang").into(),
|
|
|
|
None => OsStr::new("/usr/bin/clang").into(),
|
|
|
@ -134,36 +125,14 @@ fn compile_with_clang<P: Clone + AsRef<Path>>(
|
|
|
|
"aarch64" => "arm64",
|
|
|
|
"aarch64" => "arm64",
|
|
|
|
arch => arch,
|
|
|
|
arch => arch,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let mut cmd = Command::new(clang);
|
|
|
|
exec(
|
|
|
|
cmd.arg("-v")
|
|
|
|
Command::new(clang)
|
|
|
|
.arg("-I")
|
|
|
|
.arg("-I")
|
|
|
|
.arg(include_path.as_ref())
|
|
|
|
.arg(include_path)
|
|
|
|
.arg("-g")
|
|
|
|
.args(["-g", "-O2", "-target", "bpf", "-c"])
|
|
|
|
.arg("-O2")
|
|
|
|
|
|
|
|
.arg("-target")
|
|
|
|
|
|
|
|
.arg("bpf")
|
|
|
|
|
|
|
|
.arg("-c")
|
|
|
|
|
|
|
|
.arg(format!("-D__TARGET_ARCH_{arch}"))
|
|
|
|
.arg(format!("-D__TARGET_ARCH_{arch}"))
|
|
|
|
.arg(src.as_ref().as_os_str())
|
|
|
|
.arg(src)
|
|
|
|
.arg("-o")
|
|
|
|
.arg("-o")
|
|
|
|
.arg(out.as_ref().as_os_str());
|
|
|
|
.arg(out),
|
|
|
|
|
|
|
|
)
|
|
|
|
let Output {
|
|
|
|
|
|
|
|
status,
|
|
|
|
|
|
|
|
stdout,
|
|
|
|
|
|
|
|
stderr,
|
|
|
|
|
|
|
|
} = cmd.output().context("Failed to execute clang")?;
|
|
|
|
|
|
|
|
if !status.success() {
|
|
|
|
|
|
|
|
bail!(
|
|
|
|
|
|
|
|
"Failed to compile eBPF programs\n \
|
|
|
|
|
|
|
|
stdout=\n \
|
|
|
|
|
|
|
|
{}\n \
|
|
|
|
|
|
|
|
stderr=\n \
|
|
|
|
|
|
|
|
{}\n",
|
|
|
|
|
|
|
|
String::from_utf8(stdout).unwrap(),
|
|
|
|
|
|
|
|
String::from_utf8(stderr).unwrap()
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|