aya-bpf,xtask: generate kernel types by BTF

and bump version for bindgen
pull/20/head
lwintermelon 4 years ago
parent 08c71dfeb1
commit 0c68d38c15

@ -5,7 +5,7 @@ authors = ["Alessandro Decina <alessandro.d@gmail.com>"]
edition = "2018"
[dependencies]
bindgen = "0.57"
bindgen = "0.58"
structopt = {version = "0.3", default-features = false }
anyhow = "1"
thiserror = "1"

@ -34,7 +34,7 @@ pub fn generate<T: AsRef<str>>(
bindgen = bindgen.header_contents("kernel_types.h", &c_header);
for ty in types {
bindgen = bindgen.whitelist_type(ty);
bindgen = bindgen.allowlist_type(ty);
}
let bindings = bindgen.generate().or(Err(Error::Bindgen))?.to_string();
@ -54,7 +54,7 @@ pub fn generate<T: AsRef<str>>(
Ok(bindings)
}
fn c_header_from_btf(path: &Path) -> Result<String, Error> {
pub fn c_header_from_btf(path: &Path) -> Result<String, Error> {
let output = Command::new("bpftool")
.args(&["btf", "dump", "file"])
.arg(path)

@ -57,6 +57,10 @@ impl<'a> GetterList<'a> {
}) => &segments.first().unwrap().ident,
_ => panic!(),
};
// TODO: deal with `__BindgenUnionField` fields generated by bindgen
if field_ty_ident.to_string() == "__BindgenUnionField" {
continue;
}
let sub_fields = self
.item_fields
.get(field_ty_ident)

@ -1,7 +1,2 @@
#include <linux/types.h>
#include <linux/bpf.h>
#include <linux/ptrace.h>
#include <sys/socket.h>
// needed for TC_ACT_*
#include <linux/pkt_cls.h>
#include "vmlinux.h"
#include "bpf_helpers.h"

@ -0,0 +1,2 @@
// needed for TC_ACT_*
#include <linux/pkt_cls.h>

@ -14,6 +14,14 @@ fn codegen_internal_btf_bindings(opts: &Options) -> Result<(), anyhow::Error> {
let dir = PathBuf::from("aya");
let generated = dir.join("src/generated");
let mut bindgen = bindgen::user_builder()
.clang_arg(format!(
"-I{}",
opts.libbpf_dir
.join("include")
.canonicalize()
.unwrap()
.to_string_lossy(),
))
.clang_arg(format!(
"-I{}",
opts.libbpf_dir
@ -32,7 +40,7 @@ fn codegen_internal_btf_bindings(opts: &Options) -> Result<(), anyhow::Error> {
let types = ["bpf_core_relo", "btf_ext_header"];
for x in &types {
bindgen = bindgen.whitelist_type(x);
bindgen = bindgen.allowlist_type(x);
}
let bindings = bindgen
@ -149,10 +157,10 @@ fn codegen_bindings(opts: &Options) -> Result<(), anyhow::Error> {
let mut bindgen = builder();
for x in &types {
bindgen = bindgen.whitelist_type(x);
bindgen = bindgen.allowlist_type(x);
}
for x in &vars {
bindgen = bindgen.whitelist_var(x);
bindgen = bindgen.allowlist_var(x);
}
// FIXME: this stuff is probably debian/ubuntu specific
@ -166,11 +174,11 @@ fn codegen_bindings(opts: &Options) -> Result<(), anyhow::Error> {
};
for x in &types {
bindgen = bindgen.whitelist_type(x);
bindgen = bindgen.allowlist_type(x);
}
for x in &vars {
bindgen = bindgen.whitelist_var(x);
bindgen = bindgen.allowlist_var(x);
}
let bindings = bindgen

@ -5,8 +5,9 @@ use std::path::PathBuf;
use aya_gen::{
bindgen,
btf_types::c_header_from_btf,
getters::{generate_getters_for_items, read_getter},
write_to_file_fmt,
write_to_file, write_to_file_fmt,
};
use syn::{parse_str, Item};
@ -17,6 +18,8 @@ use crate::codegen::{
pub fn codegen(opts: &Options) -> Result<(), anyhow::Error> {
let dir = PathBuf::from("bpf/aya-bpf-bindings");
let vmlinux = c_header_from_btf(&*opts.btf)?;
write_to_file(&dir.join("include").join("vmlinux.h"), &vmlinux)?;
let builder = || {
let mut bindgen = bindgen::bpf_builder()
@ -41,11 +44,24 @@ pub fn codegen(opts: &Options) -> Result<(), anyhow::Error> {
let vars = ["BPF_.*", "bpf_.*", "TC_ACT_.*", "SOL_SOCKET", "SO_.*"];
for x in &types {
bindgen = bindgen.whitelist_type(x);
bindgen = bindgen.allowlist_type(x);
}
for x in &vars {
bindgen = bindgen.whitelist_var(x);
bindgen = bindgen.allowlist_var(x);
}
bindgen
};
// from BTF we can't generate bindings for macros like TC_ACT_OK, macro_bindings.h is for bindgen to do the work.
let marcro_bindings_builder = || {
let mut bindgen =
bindgen::bpf_builder().header(&*dir.join("include/macro_bindings.h").to_string_lossy());
let vars = ["TC_ACT_.*"];
for x in &vars {
bindgen = bindgen.allowlist_var(x);
}
bindgen
@ -54,11 +70,15 @@ pub fn codegen(opts: &Options) -> Result<(), anyhow::Error> {
for arch in Architecture::supported() {
let generated = dir.join("src").join(arch.to_string());
let bindings = builder()
let mut bindings = builder()
.generate()
.map_err(|_| anyhow!("bindgen failed"))?
.to_string();
let marcro_bindings = marcro_bindings_builder()
.generate()
.map_err(|_| anyhow!("bindgen failed"))?
.to_string();
bindings.push_str(&marcro_bindings);
let mut tree = parse_str::<syn::File>(&bindings).unwrap();
let (indexes, helpers) = extract_helpers(&tree.items);
let helpers = expand_helpers(&helpers);

@ -45,7 +45,8 @@ impl std::fmt::Display for Architecture {
pub struct Options {
#[structopt(long)]
libbpf_dir: PathBuf,
#[structopt(long, default_value = "/sys/kernel/btf/vmlinux")]
btf: PathBuf,
#[structopt(subcommand)]
command: Option<Command>,
}

Loading…
Cancel
Save