aya_bpf_bindings.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. use std::path::{Path, PathBuf};
  2. use anyhow::anyhow;
  3. use aya_tool::{bindgen, write_to_file_fmt};
  4. use proc_macro2::TokenStream;
  5. use quote::ToTokens;
  6. use syn::{parse_str, Item};
  7. use crate::codegen::{
  8. helpers::{expand_helpers, extract_helpers},
  9. Architecture, SysrootOptions,
  10. };
  11. pub fn codegen(opts: &SysrootOptions, libbpf_dir: &Path) -> Result<(), anyhow::Error> {
  12. let SysrootOptions {
  13. x86_64_sysroot,
  14. aarch64_sysroot,
  15. armv7_sysroot,
  16. riscv64_sysroot,
  17. } = opts;
  18. let dir = PathBuf::from("bpf/aya-bpf-bindings");
  19. let builder = || {
  20. let mut bindgen = bindgen::bpf_builder()
  21. .header(&*dir.join("include/bindings.h").to_string_lossy())
  22. // aya-tool uses aya_bpf::cty. We can't use that here since aya-bpf
  23. // depends on aya-bpf-bindings so it would create a circular dep.
  24. .ctypes_prefix("::aya_bpf_cty")
  25. .clang_args(&["-I", &*libbpf_dir.join("include/uapi").to_string_lossy()])
  26. .clang_args(&["-I", &*libbpf_dir.join("include").to_string_lossy()])
  27. .clang_args(&["-I", &*libbpf_dir.join("src").to_string_lossy()])
  28. // open aya-bpf-bindings/.../bindings.rs and look for mod
  29. // _bindgen, those are anonymous enums
  30. .constified_enum("BPF_F_.*")
  31. .constified_enum("BPF_REG_.*")
  32. .constified_enum("BPF_CSUM_.*")
  33. .constified_enum("BPF_ADJ_.*")
  34. .constified_enum("BPF_SK_.*")
  35. .constified_enum("BPF_RB_.*")
  36. .constified_enum("BPF_RINGBUF_.*")
  37. .constified_enum("BPF_SOCK_.*")
  38. .constified_enum("BPF_TCP_.*")
  39. .constified_enum("BPF_DEVCG_.*")
  40. .constified_enum("BPF_FIB_.*")
  41. .constified_enum("BPF_FLOW_.*");
  42. let types = [
  43. "bpf_.*",
  44. "sk_action",
  45. "pt_regs",
  46. "user_pt_regs",
  47. "user_regs_struct",
  48. "xdp_action",
  49. ];
  50. let vars = ["BPF_.*", "bpf_.*", "TC_ACT_.*", "SOL_SOCKET", "SO_.*"];
  51. for x in &types {
  52. bindgen = bindgen.allowlist_type(x);
  53. }
  54. // we define our own version which is compatible with both libbpf and
  55. // iproute2
  56. bindgen = bindgen.blocklist_type("bpf_map_def");
  57. for x in &vars {
  58. bindgen = bindgen.allowlist_var(x);
  59. }
  60. bindgen
  61. };
  62. for arch in Architecture::supported() {
  63. let mut bindgen = builder();
  64. // Set target triple. This will set the right flags (which you can see
  65. // running clang -target=X -E - -dM </dev/null)
  66. let target = match arch {
  67. Architecture::X86_64 => "x86_64-unknown-linux-gnu",
  68. Architecture::ARMv7 => "armv7-unknown-linux-gnu",
  69. Architecture::AArch64 => "aarch64-unknown-linux-gnu",
  70. Architecture::RISCV64 => "riscv64-unknown-linux-gnu",
  71. };
  72. bindgen = bindgen.clang_args(&["-target", target]);
  73. // Set the sysroot. This is needed to ensure that the correct arch
  74. // specific headers are imported.
  75. let sysroot = match arch {
  76. Architecture::X86_64 => x86_64_sysroot,
  77. Architecture::ARMv7 => armv7_sysroot,
  78. Architecture::AArch64 => aarch64_sysroot,
  79. Architecture::RISCV64 => riscv64_sysroot,
  80. };
  81. bindgen = bindgen.clang_args(&["-I", &*sysroot.to_string_lossy()]);
  82. let bindings = bindgen
  83. .generate()
  84. .map_err(|_| anyhow!("bindgen failed"))?
  85. .to_string();
  86. let mut tree = parse_str::<syn::File>(&bindings).unwrap();
  87. let (indexes, helpers) = extract_helpers(&tree.items);
  88. let helpers = expand_helpers(&helpers);
  89. for index in indexes {
  90. tree.items[index] = Item::Verbatim(TokenStream::new())
  91. }
  92. let generated = dir.join("src").join(arch.to_string());
  93. // write the bindings, with the original helpers removed
  94. write_to_file_fmt(
  95. generated.join("bindings.rs"),
  96. &tree.to_token_stream().to_string(),
  97. )?;
  98. // write the new helpers as expanded by expand_helpers()
  99. write_to_file_fmt(
  100. generated.join("helpers.rs"),
  101. &format!("use super::bindings::*; {helpers}"),
  102. )?;
  103. }
  104. Ok(())
  105. }