codegen: tidy up

Move some code out of a loop, where it appears to be nonsense.
reviewable/pr1168/r2
Tamir Duberstein 3 months ago
parent 547a75bb4d
commit 38ec9288c1
No known key found for this signature in database

@ -1,4 +1,3 @@
#include <asm-generic/socket.h>
#include <linux/bpf.h> #include <linux/bpf.h>
#include <linux/btf.h> #include <linux/btf.h>
#include <linux/if_link.h> #include <linux/if_link.h>
@ -7,3 +6,4 @@
#include <linux/pkt_cls.h> #include <linux/pkt_cls.h>
#include <linux/pkt_sched.h> #include <linux/pkt_sched.h>
#include <linux/rtnetlink.h> #include <linux/rtnetlink.h>
#include <sys/socket.h>

@ -1,9 +1,10 @@
#include <linux/types.h> #include <linux/types.h>
// __wsum is missing from types.h, see // __wsum is missing from types.h, compare:
// https://elixir.bootlin.com/linux/v5.13/source/include/uapi/linux/types.h // https://github.com/torvalds/linux/blob/v5.13/include/uapi/linux/types.h
// https://github.com/libbpf/libbpf/blob/v1.5.0/include/linux/types.h
typedef __u32 __bitwise __wsum; typedef __u32 __bitwise __wsum;
#include "bpf_helpers.h" #include <bpf/bpf_helpers.h>
#include <linux/bpf.h> #include <linux/bpf.h>
// needed for TC_ACT_* // needed for TC_ACT_*
#include <linux/pkt_cls.h> #include <linux/pkt_cls.h>

@ -1,16 +1,16 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use anyhow::anyhow; use anyhow::{Context as _, Result};
use aya_tool::{bindgen, write_to_file}; use aya_tool::{bindgen, write_to_file};
use crate::codegen::{Architecture, SysrootOptions}; use crate::codegen::{Architecture, SysrootOptions};
pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::Error> { pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<()> {
codegen_internal_btf_bindings(libbpf_dir)?; codegen_internal_btf_bindings(libbpf_dir)?;
codegen_bindings(opts, libbpf_dir) codegen_bindings(opts, libbpf_dir)
} }
fn codegen_internal_btf_bindings(libbpf_dir: &Path) -> Result<(), anyhow::Error> { fn codegen_internal_btf_bindings(libbpf_dir: &Path) -> Result<()> {
let dir = PathBuf::from("aya-obj"); let dir = PathBuf::from("aya-obj");
let generated = dir.join("src/generated"); let generated = dir.join("src/generated");
@ -26,10 +26,7 @@ fn codegen_internal_btf_bindings(libbpf_dir: &Path) -> Result<(), anyhow::Error>
bindgen = bindgen.allowlist_type(x); bindgen = bindgen.allowlist_type(x);
} }
let bindings = bindgen let bindings = bindgen.generate().context("bindgen failed")?.to_string();
.generate()
.map_err(|op| anyhow!("bindgen failed - {op}"))?
.to_string();
// write the bindings, with the original helpers removed // write the bindings, with the original helpers removed
write_to_file(generated.join("btf_internal_bindings.rs"), &bindings)?; write_to_file(generated.join("btf_internal_bindings.rs"), &bindings)?;
@ -37,7 +34,7 @@ fn codegen_internal_btf_bindings(libbpf_dir: &Path) -> Result<(), anyhow::Error>
Ok(()) Ok(())
} }
fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::Error> { fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<()> {
let SysrootOptions { let SysrootOptions {
x86_64_sysroot, x86_64_sysroot,
aarch64_sysroot, aarch64_sysroot,
@ -47,9 +44,29 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh
s390x_sysroot, s390x_sysroot,
mips_sysroot, mips_sysroot,
} = opts; } = opts;
let dir = PathBuf::from("aya-obj");
let generated = dir.join("src/generated");
let builder = || {
let mut bindgen = bindgen::user_builder()
.header(dir.join("include/linux_wrapper.h").to_str().unwrap())
.clang_args(["-I", libbpf_dir.join("include/uapi").to_str().unwrap()])
.clang_args(["-I", libbpf_dir.join("include").to_str().unwrap()])
// BPF_F_LINK is defined twice. Once in an anonymous enum
// which bindgen will constify, and once via #define macro
// which generates a duplicate const.
.blocklist_var("BPF_F_LINK")
.constified_enum("BPF_F_.*")
.constified_enum("BTF_KIND_.*")
.constified_enum("BTF_VAR_.*")
.constified_enum("IFLA_.*")
.constified_enum("TCA_.*")
.constified_enum("BPF_RINGBUF_.*")
// NETFILTER
.constified_enum("NFPROTO_.*");
let types = [ let types = [
// BPF // BPF
"BPF_TYPES",
"bpf_cmd", "bpf_cmd",
"bpf_insn", "bpf_insn",
"bpf_attr", "bpf_attr",
@ -163,14 +180,15 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh
"NFPROTO_.*", "NFPROTO_.*",
]; ];
let dir = PathBuf::from("aya-obj"); for x in &types {
let generated = dir.join("src/generated"); bindgen = bindgen.allowlist_type(x);
}
let builder = || { for x in &vars {
bindgen::user_builder() bindgen = bindgen.allowlist_var(x);
.header(dir.join("include/linux_wrapper.h").to_str().unwrap()) }
.clang_args(["-I", libbpf_dir.join("include/uapi").to_str().unwrap()])
.clang_args(["-I", libbpf_dir.join("include").to_str().unwrap()]) bindgen
}; };
for arch in Architecture::supported() { for arch in Architecture::supported() {
@ -202,38 +220,7 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh
}; };
bindgen = bindgen.clang_args(["-I", sysroot.to_str().unwrap()]); bindgen = bindgen.clang_args(["-I", sysroot.to_str().unwrap()]);
for x in &types { let bindings = bindgen.generate().context("bindgen failed")?.to_string();
bindgen = bindgen.allowlist_type(x);
}
for x in &vars {
bindgen = bindgen
.allowlist_var(x)
// BPF_F_LINK is defined twice. Once in an anonymous enum
// which bindgen will constify, and once via #define macro
// which generates a duplicate const.
.blocklist_var("BPF_F_LINK")
.constified_enum("BPF_F_.*")
.constified_enum("BTF_KIND_.*")
.constified_enum("BTF_VAR_.*")
.constified_enum("IFLA_.*")
.constified_enum("TCA_.*")
.constified_enum("BPF_RINGBUF_.*")
// NETFILTER
.constified_enum("NFPROTO_.*");
}
for x in &types {
bindgen = bindgen.allowlist_type(x);
}
for x in &vars {
bindgen = bindgen.allowlist_var(x);
}
let bindings = bindgen
.generate()
.map_err(|op| anyhow!("bindgen failed - {op}"))?
.to_string();
// write the bindings, with the original helpers removed // write the bindings, with the original helpers removed
write_to_file( write_to_file(

@ -1,20 +1,25 @@
use std::{ use std::{
ffi::OsString,
fs::create_dir_all, fs::create_dir_all,
path::{Path, PathBuf}, path::{Path, PathBuf},
process::Command,
}; };
use anyhow::anyhow; use anyhow::{Context as _, Result};
use aya_tool::{bindgen, write_to_file_fmt}; use aya_tool::{bindgen, write_to_file_fmt};
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::ToTokens; use quote::ToTokens;
use syn::{parse_str, Item}; use syn::{parse_str, Item};
use crate::codegen::{ use crate::{
codegen::{
helpers::{expand_helpers, extract_helpers}, helpers::{expand_helpers, extract_helpers},
Architecture, SysrootOptions, Architecture, SysrootOptions,
},
exec,
}; };
pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::Error> { pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<()> {
let SysrootOptions { let SysrootOptions {
x86_64_sysroot, x86_64_sysroot,
aarch64_sysroot, aarch64_sysroot,
@ -25,17 +30,35 @@ pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::E
mips_sysroot, mips_sysroot,
} = opts; } = opts;
let tmp_dir = tempfile::tempdir().context("tempdir failed")?;
let libbpf_headers_dir = tmp_dir.path().join("libbpf_headers");
let mut includedir = OsString::new();
includedir.push("INCLUDEDIR=");
includedir.push(&libbpf_headers_dir);
exec(
Command::new("make")
.arg("-C")
.arg(libbpf_dir.join("src"))
.arg(includedir)
.arg("install_headers"),
)?;
let dir = PathBuf::from("ebpf/aya-ebpf-bindings"); let dir = PathBuf::from("ebpf/aya-ebpf-bindings");
let builder = || { let builder = || {
let mut bindgen = bindgen::bpf_builder() let mut bindgen = bindgen::bpf_builder()
.header(dir.join("include/bindings.h").to_str().unwrap()) .header(dir.join("include/bindings.h").to_str().unwrap())
.clang_args(["-I", libbpf_dir.join("include/uapi").to_str().unwrap()])
.clang_args(["-I", libbpf_dir.join("include").to_str().unwrap()])
.clang_args(["-I", libbpf_headers_dir.to_str().unwrap()])
// aya-tool uses aya_ebpf::cty. We can't use that here since aya-bpf // aya-tool uses aya_ebpf::cty. We can't use that here since aya-bpf
// depends on aya-ebpf-bindings so it would create a circular dep. // depends on aya-ebpf-bindings so it would create a circular dep.
.ctypes_prefix("::aya_ebpf_cty") .ctypes_prefix("::aya_ebpf_cty")
.clang_args(["-I", libbpf_dir.join("include/uapi").to_str().unwrap()]) // we define our own version which is compatible with both libbpf
.clang_args(["-I", libbpf_dir.join("include").to_str().unwrap()]) // and iproute2.
.clang_args(["-I", libbpf_dir.join("src").to_str().unwrap()]) .blocklist_type("bpf_map_def")
// BPF_F_LINK is defined twice. Once in an anonymous enum // BPF_F_LINK is defined twice. Once in an anonymous enum
// which bindgen will constify, and once via #define macro // which bindgen will constify, and once via #define macro
// which generates a duplicate const. // which generates a duplicate const.
@ -70,10 +93,6 @@ pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::E
bindgen = bindgen.allowlist_type(x); bindgen = bindgen.allowlist_type(x);
} }
// we define our own version which is compatible with both libbpf and
// iproute2
bindgen = bindgen.blocklist_type("bpf_map_def");
for x in &vars { for x in &vars {
bindgen = bindgen.allowlist_var(x); bindgen = bindgen.allowlist_var(x);
} }
@ -110,10 +129,7 @@ pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::E
}; };
bindgen = bindgen.clang_args(["-I", sysroot.to_str().unwrap()]); bindgen = bindgen.clang_args(["-I", sysroot.to_str().unwrap()]);
let bindings = bindgen let bindings = bindgen.generate().context("bindgen failed")?.to_string();
.generate()
.map_err(|op| anyhow!("bindgen failed - {op}"))?
.to_string();
let mut tree = parse_str::<syn::File>(&bindings).unwrap(); let mut tree = parse_str::<syn::File>(&bindings).unwrap();

@ -4,6 +4,7 @@ mod helpers;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use anyhow::{Context as _, Result};
use clap::Parser; use clap::Parser;
const SUPPORTED_ARCHS: &[Architecture] = &[ const SUPPORTED_ARCHS: &[Architecture] = &[
@ -92,34 +93,38 @@ pub struct SysrootOptions {
#[derive(Parser)] #[derive(Parser)]
pub struct Options { pub struct Options {
#[command(flatten)] #[clap(flatten)]
sysroot_options: SysrootOptions, sysroot_options: SysrootOptions,
#[command(subcommand)] #[clap(subcommand)]
command: Option<Command>, command: Option<Target>,
} }
#[derive(clap::Subcommand)] #[derive(clap::Subcommand)]
enum Command { enum Target {
#[command(name = "aya")] #[command(name = "aya")]
Aya, Aya,
#[command(name = "aya-ebpf-bindings")] #[command(name = "aya-ebpf-bindings")]
AyaEbpfBindings, AyaEbpfBindings,
} }
pub fn codegen(opts: Options, libbpf_dir: &Path) -> Result<(), anyhow::Error> { pub fn codegen(opts: Options, libbpf_dir: &Path) -> Result<()> {
let Options { let Options {
sysroot_options, sysroot_options,
command, command,
} = opts; } = opts;
match command { match command {
Some(command) => match command { Some(command) => match command {
Command::Aya => aya::codegen(&sysroot_options, libbpf_dir), Target::Aya => aya::codegen(&sysroot_options, libbpf_dir).context("aya"),
Command::AyaEbpfBindings => aya_ebpf_bindings::codegen(&sysroot_options, libbpf_dir), Target::AyaEbpfBindings => aya_ebpf_bindings::codegen(&sysroot_options, libbpf_dir)
.context("aya_ebpf_bindings"),
}, },
None => { None => {
aya::codegen(&sysroot_options, libbpf_dir)?; aya::codegen(&sysroot_options, libbpf_dir).context("aya")?;
aya_ebpf_bindings::codegen(&sysroot_options, libbpf_dir) aya_ebpf_bindings::codegen(&sysroot_options, libbpf_dir)
.context("aya_ebpf_bindings")?;
Ok(())
} }
} }
} }

Loading…
Cancel
Save