|  |  |  | @ -1,16 +1,16 @@ | 
		
	
		
			
				|  |  |  |  | use std::path::{Path, PathBuf}; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | use anyhow::anyhow; | 
		
	
		
			
				|  |  |  |  | use anyhow::{Context as _, Result}; | 
		
	
		
			
				|  |  |  |  | use aya_tool::{bindgen, write_to_file}; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 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_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 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); | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     let bindings = bindgen | 
		
	
		
			
				|  |  |  |  |         .generate() | 
		
	
		
			
				|  |  |  |  |         .map_err(|op| anyhow!("bindgen failed - {op}"))? | 
		
	
		
			
				|  |  |  |  |         .to_string(); | 
		
	
		
			
				|  |  |  |  |     let bindings = bindgen.generate().context("bindgen failed")?.to_string(); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     // write the bindings, with the original helpers removed
 | 
		
	
		
			
				|  |  |  |  |     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(()) | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::Error> { | 
		
	
		
			
				|  |  |  |  | fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<()> { | 
		
	
		
			
				|  |  |  |  |     let SysrootOptions { | 
		
	
		
			
				|  |  |  |  |         x86_64_sysroot, | 
		
	
		
			
				|  |  |  |  |         aarch64_sysroot, | 
		
	
	
		
			
				
					|  |  |  | @ -47,9 +44,29 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh | 
		
	
		
			
				|  |  |  |  |         s390x_sysroot, | 
		
	
		
			
				|  |  |  |  |         mips_sysroot, | 
		
	
		
			
				|  |  |  |  |     } = 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 = [ | 
		
	
		
			
				|  |  |  |  |             // BPF
 | 
		
	
		
			
				|  |  |  |  |         "BPF_TYPES", | 
		
	
		
			
				|  |  |  |  |             "bpf_cmd", | 
		
	
		
			
				|  |  |  |  |             "bpf_insn", | 
		
	
		
			
				|  |  |  |  |             "bpf_attr", | 
		
	
	
		
			
				
					|  |  |  | @ -163,14 +180,15 @@ fn codegen_bindings(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyh | 
		
	
		
			
				|  |  |  |  |             "NFPROTO_.*", | 
		
	
		
			
				|  |  |  |  |         ]; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     let dir = PathBuf::from("aya-obj"); | 
		
	
		
			
				|  |  |  |  |     let generated = dir.join("src/generated"); | 
		
	
		
			
				|  |  |  |  |         for x in &types { | 
		
	
		
			
				|  |  |  |  |             bindgen = bindgen.allowlist_type(x); | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     let builder = || { | 
		
	
		
			
				|  |  |  |  |         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()]) | 
		
	
		
			
				|  |  |  |  |         for x in &vars { | 
		
	
		
			
				|  |  |  |  |             bindgen = bindgen.allowlist_var(x); | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         bindgen | 
		
	
		
			
				|  |  |  |  |     }; | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     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()]); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         for x in &types { | 
		
	
		
			
				|  |  |  |  |             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(); | 
		
	
		
			
				|  |  |  |  |         let bindings = bindgen.generate().context("bindgen failed")?.to_string(); | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |         // write the bindings, with the original helpers removed
 | 
		
	
		
			
				|  |  |  |  |         write_to_file( | 
		
	
	
		
			
				
					|  |  |  | 
 |