Commit Graph

406 Commits

Author SHA1 Message Date
Tamir Duberstein 2c927581fe Remove duplicate attach type state
Pass the kernel attach type into the shared loader from each typed
wrapper instead of storing a second mutable copy in `ProgramData`.

This removes the from_pin repair pattern, avoids stale shadow state, and
keeps attach paths driven by wrapper-owned typed attach metadata or
fixed constants.

Also simplify `{sk,cgroup}_skb` section parsing by representing all
variants as `ProgramSection::SkSkb { kind }` and
`ProgramSection::CgroupSkb { attach_type }` respectively, with the
attach type enums shared from aya-obj.
2026-04-03 11:21:32 -04:00
Aurélien DEROIDE 8aeec41eec aya-ebpf: Add bpf_f_adj_room_encap_l2
This function is needed to properly add a L2 header when using `bpf_skb_adjust_room` [1].
As it is originally a C macro, it isn't automatically generated in the `bindings` cratea

[1]: https://docs.ebpf.io/linux/helper-function/bpf_skb_adjust_room/
2026-03-20 10:07:09 +00:00
Afonso Quintas a826cc92c9 aya-ebpf: use &self for store, set_mark and set_reply
Currently, the API for calling kernel helpers on `TcContext`, `SkBuff`,
and `SockOpsContext` has inconsistent semantics. Methods like `store`,
`set_mark`, and `set_reply` require a mutable reference (`&mut self`),
while others that also modify the underlying state take a read-only
reference (`&self`).

This commit resolves the inconsistency reported in #1442 by changing
`store`, `set_mark`, and `set_reply` to take `&self` across `SkBuff`,
`SkBuffContext`, `TcContext`, and `SockOpsContext`.

The `cb_mut` method is intentionally left unchanged because returning
a mutable slice (`&mut [u32]`) from an immutable reference would
violate Rust's aliasing rules.

Fixes: #1442
2026-03-18 11:23:34 -04:00
Tamir Duberstein b9cb76b302 Rename EbpfGlobal to Global
Rename the eBPF global wrapper type to `Global` and update its
downstream uses and API snapshot so the public surface matches the new
name.

Co-authored-by: Codex <noreply@openai.com>
2026-03-06 11:20:10 -05:00
Alessandro Decina 197f8a5394 ci: arm64: use cortex-a72 cpu
ERROR:target/arm/internals.h:767:regime_is_user: code should not be
reached Bail out! ERROR:target/arm/internals.h:767:regime_is_user: code
should not be reached Error: "qemu-system-aarch64" "-machine" "virt"
"-cpu" "max" "-accel" "kvm" "-accel" "hvf" "-accel" "tcg" "-no-reboot"
"-nographic" "-m" "1024M" "-smp" "2" "-append" "console=ttyAMA0
init.arg=--test-threads=1 noapic" "-kernel"
"/tmp/.tmpROemqg/kernel-archive-0-image/./boot/vmlinuz-6.17.13+deb13-cloud-arm64"
"-initrd" "/tmp/.tmpNJFz3n/qemu-initramfs.img" failed
2026-03-07 03:18:03 +11:00
Tamir Duberstein ab7f96dafb aya-ebpf: document nul termination
See https://docs.ebpf.io/linux/helper-function/bpf_get_current_comm/.
2026-03-03 12:59:09 -05:00
dependabot[bot] db64fcf73b build(deps): update public-api requirement in the cargo-crates group (#1488)
Updates the requirements on [public-api](https://github.com/cargo-public-api/cargo-public-api) to permit the latest version.

Updates `public-api` to 0.51.0
- [Release notes](https://github.com/cargo-public-api/cargo-public-api/releases)
- [Changelog](https://github.com/cargo-public-api/cargo-public-api/blob/main/CHANGELOG.md)
- [Commits](https://github.com/cargo-public-api/cargo-public-api/compare/public-api-v0.50.0...public-api-v0.51.0)

---
updated-dependencies:
- dependency-name: public-api
  dependency-version: 0.51.0
  dependency-type: direct:production
  dependency-group: cargo-crates
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-03-02 07:31:25 -08:00
vad ae8c76d037 maps: Fix BloomFilter::contains method
Require the caller to pass a reference, not the owned value. That makes
sure that the pointer is valid from the beginning.
2026-02-16 18:19:28 +00:00
vad 0bf462d221 maps: Take &self in all methods of BloomFilter 2026-02-16 18:19:28 +00:00
vad 8962fc79c1 aya-ebpf: Use i32 as a type for eBPF helper return codes
The helpers always return a signed 64-bit r0 value, but the JIT that
translates eBPF into native instructions differs by architecture. On
x86_64 the generated code writes the helper result into a 64-bit
register, so the CPU sign-extends negative errnos automatically. On
aarch64 the JIT frequently uses 32-bit operations (w0) when copying the
helper return and only zero-extends into the upper half of x0.

That results in broken errno codes on aarch64. For example, when a map
operation returns `-ENOENT`, which is supposed to be -2, the i64
representation yields something like `0x0000_0000_FFFF_FFFE`
(4294967294) instead of -2. In short: the ARM64 JIT doesn’t preserve the
sign in the upper half of the 64-bit register, and the error code has to
be cast to a 32-bit integer to make the sign visible.

This makes it awkward for users, because that behavior prevents simply
comparing helper error codes with constants like `libc::ENOENT` without
manual casts.

Given that the maximum error code limit `MAX_ERRNO` in the kernel is
4095, which fits in a 32-bit integer, coerce the error codes to be `i32`
and make all helpers return `Result<T, i32>`.
2026-02-16 18:19:28 +00:00
tamird a30c1496f7 public-api: regenerate 2026-02-16 08:04:48 +00:00
Jamie Hill-Daniel 294e0c1941 ebpf: Add helper for safe loading of globals 2026-02-13 11:13:57 -08:00
Tamir Duberstein 2f8759cc62 Dial the lints to 100 2026-01-29 10:02:48 -05:00
Tamir Duberstein d10ed519c3 xtask: move libbpf-sys hacks to Rust
This restores my ability to run integration tests locally.

Replace `clippy.sh` with `cargo xtask clippy`.
2026-01-28 21:08:01 -05:00
root 930fa7b8af aya-ebpf: generalize btf_map_def macro type parameters
Modify the btf_map_def! macro to generate flat #[repr(C)] structs
instead of UnsafeCell wrappers. This produces BTF that both aya
and libbpf can parse.

Support type parameters with optional defaults and const generics with
configurable types. Allow trailing commas and improve formatting.

Also remove UnsafeCell traversal code from aya-obj loader since
it is no longer needed with flat struct layout.
2026-01-27 14:49:22 +00:00
Tamir Duberstein e746618143 enable unused_qualifications lint 2026-01-23 11:03:26 -05:00
Tamir Duberstein f35f7a3610 aya, aya-ebpf: reduce duplication 2026-01-21 09:05:51 -05:00
Tamir Duberstein 3eb9cacef4 aya-ebpf: add BTF ring buffer
Co-developed-by: Lukas Hoehl <lukas.hoehl@stackit.cloud>
2026-01-13 05:43:45 -05:00
Donghyun Shin 1d10f8751d aya-ebpf: fix redirect_sk_lookup receiver type
The `redirect_sk_lookup` method for SockMap and SockHash
previously required exclusive references.

The documentation for `bpf_map_lookup_elem` makes no
mention of a requirement for exclusive references.

Therefore, `redirect_sk_lookup` has been changed to
receive shared references to SockMap and SockHash.
2026-01-12 10:43:04 -05:00
tamird b8ed06a4cd aya-obj, aya-ebpf-bindings: regenerate
libbpf commit: 20ea95b4505c477af3b6ff6ce9d19cee868ddc5d
2026-01-09 06:14:47 +01:00
Tamir Duberstein d1f72a580c aya: improve ParseLine as Debug
Print human-readable strings rather than sequences of bytes.
2026-01-05 12:23:54 -05:00
Tamir Duberstein a9945b0957 xtask: implement unit tests in VM
This is quite useful when developing on macOS.
2025-12-31 05:01:43 -05:00
swananan eb99da3cbb uprobe: bundle attach location+cookie via UProbeAttachPoint
This follows the #1417 review discussion: by bundling location
+ cookie into a UProbeAttachPoint we get a more idiomatic Into<_>
entry point, keep the one-to-one relationship enforced by the type
system, and make it easier to extend attach with multi-location
support without introducing parallel arrays or a brand new API.
2025-12-23 18:22:42 +01:00
Tamir Duberstein 72810f095f aya: ProbeKind is Entry/Return
We already have separate types for KProbe and UProbe.
2025-11-12 11:28:03 -05:00
Tamir Duberstein 1944c4aa00 perf_event: refactor perf_event_open_trace_point
Rewrite it in terms of perf_event_open.
2025-11-12 11:28:03 -05:00
Tamir Duberstein 7e07f85edc perf_attach: privatize PerfLink
There's no way to obtain this type externally.
2025-11-12 11:28:03 -05:00
Friday Ortiz ab38afe95d perf_event: support hardware breakpoints
Implement `PerfEventConfig::Breakpoint`, allowing users to attach
hardware breakpoints. Generate `HW_BREAKPOINT_*` and `struct
bpf_perf_event_data` in support of this feature and update the type of
`PerfEventContext` accordingly.

Add a test exercising R, W, RW, and X breakpoints. Note that R
breakpoints are unsupported on x86, and this is asserted in the test.

Extend the VM integration test harness and supporting infrastructure
(e.g. `download_kernel_images.sh`) to download kernel debug packages and
mount `System.map` in initramfs. This is needed (at least) on the aarch
6.1 Debian kernel which was not compiled with `CONFIG_KALLSYMS_ALL=y`
for some reason, and the locations of globals are not available in
kallsyms. To attach breakpoints to these symbols in the test pipeline,
we need to read them from System.map and apply the KASLR offset to get
their real address. The `System.map` file is not provided in the kernel
package by default, so we need to extract it from the corresponding
debug package. The KASLR offset is computed using `gunzip` which appears
in kallsyms on all Debian kernels tested.

Co-authored-by: Tamir Duberstein <tamird@gmail.com>
2025-11-10 16:33:54 -05:00
Tamir Duberstein 0484ab5c57 xtask: use -cpu host iff host == guest 2025-11-10 16:33:53 -05:00
JPaja d8f5497884 feat: add Ebpf::maps_disjoint_mut
Implemented using `HashMap::get_disjoint_mut` introduced in Rust 1.86.0.
2025-11-10 14:12:04 -05:00
Tamir Duberstein 989a465f0c xtask: avoid extracting complete archives
Now that we're no longer shelling out to do this, we can avoid most of
the work. Operate directly on the archive, unpacking only what we need.
2025-11-10 08:53:46 -05:00
Tamir Duberstein 2b8cd557aa xtask: drop build script logging prefix
This code doesn't run under cargo.
2025-11-09 18:49:27 -05:00
Tamir Duberstein 18c7f7ccd6 perf_event: push down type safety
This makes it more difficult to mishandle callers of `perf_event_open`.

Change `wakeup_events = 0` to 1; per `man 2 perf_event_open`:

  Prior to Linux 3.0, setting wakeup_events to 0 resulted in
  no overflow notifications; more recent kernels treat 0 the
  same as 1.
2025-11-09 17:50:53 -05:00
Tamir Duberstein 7e405c216e perf_event: simplify PerfEventScope 2025-11-09 13:20:41 -05:00
Tamir Duberstein 468b9b73db xtask: use -cpu host on x86
We have started to see errors in CI:

  qemu-system-x86_64: warning: host doesn't support requested feature: CPUID[eax=80000001h].ECX.svm [bit 2]

The internet says this is the remedy.
2025-11-09 08:19:30 -05:00
Tim W 17573e0e47 aya-build: plumb features of ebpf crates
This allows callers to select features of the ebpf crate.
2025-11-03 18:43:45 -05:00
Alessandro Decina 3a3c451009 aya: restore must_exist argument to set_global
In
https://github.com/aya-rs/aya/commit/03e84871773e09badf08bdef8e83b4f1256850a4
we deprecated set_global but accidentally broke its API by deleting the
must_exist argument.
2025-10-29 13:01:34 +01:00
Tamir Duberstein 03fea9e304 aya-ebpf: reduce duplication 2025-10-26 15:51:12 -04:00
Tamir Duberstein d9704be77d aya-build: remove cargo_metadata from public API 2025-10-26 09:50:52 -04:00
Tamir Duberstein f610453ec2 ebpf: extract CARGO_CFG_BPF_TARGET_ARCH logic 2025-10-26 07:24:46 -04:00
Tamir Duberstein 05250da20b aya-ebpf: reduce repetition and excessive traits
The traits `FromBtfArgument`, `FromRawTracepointArgs`, `FromPtRegs` are
all fancy ways of saying `Argument` - so replace these traits with it.

This also removes the use of `bpf_probe_read` which was introduced in
05c1586202 because I can't reproduce the
need for it.
2025-10-25 15:01:52 -04:00
Michal R a7cfc694bd xtask: Allow to run VM integration tests without dpkg
Debian packages are just nested archives, where the outer one is ar
and the inner one is lzma2 tarball. Use Rust crates to unpack them.

Co-authored-by: Tamir Duberstein <tamird@gmail.com>
2025-10-23 21:17:31 +02:00
Andrew Werner 03e8487177 aya: rename set_ methods on EbpfLoader
This loader is more of a builder, so these `set_` methods didn't
quite fit. See [this discussion][1] for the motivation.

[1]: https://reviewable.io/reviews/aya-rs/aya/1318#gh-2384180366
2025-10-22 00:05:20 -04:00
Andrew Werner 5802dc7a23 aya: allow specifying a pin path for a named map
This commit extends the EbpfLoader with set_map_pin_path that allows the
caller to associate a named map with a pin path.

One note is that this path is an absolute path, not relative to
`map_pin_path`, and it forces the map to be loaded from that path.
2025-10-21 08:30:20 -04:00
Andrew Werner 1c924bb421 aya: rename map_pin_path to default_map_pin_path
This is the path in which pinned maps are created or resolved. It
isn't actually the path for any specific map itself. This rename
makes way for a method `set_map_pin_path` that actually specifies
the pin path for a specific map.
2025-10-21 08:30:20 -04:00
Tamir Duberstein 0144c0eb22 integration-test: defer cleanup to ensure unpin 2025-10-17 22:16:28 -04:00
Tamir Duberstein 2e5f5efbf1 aya: make ProgramInfo a proper enum
This allows us to distinguish between LSM and LSM_CGROUP programs and do
the proper capability check in is_program_supported.
2025-10-17 22:16:28 -04:00
Tamir Duberstein 8e9404ecd4 aya-obj: apply enum64-to-union fixup in reloc
This code is just awful.
2025-10-17 22:16:25 -04:00
Tamir Duberstein 7224efcad8 aya-obj: patch up 0-size datasec
Use OnceCell for ENUM64 while I'm here as well.
2025-10-17 15:28:22 -04:00
Tamir Duberstein 3ade19b869 Promote BTF loading failure to error on BTF relocs 2025-10-17 15:28:22 -04:00
Tyrone Wu 3d7fbaad28 aya: enforce valid perf_event type & config combos
Add guardrails for when setting event type and config for perf_event
programs. The `PerfEventConfig` enum now defines the event `type` and
`config` of interest.

Remove public re-exports, and add idiomatic Rust types for:
- perf_hw_id => HardwareEvent
- perf_sw_ids => SoftwareEvent
- perf_hw_cache_id => HwCacheEvent
- perf_hw_cache_op_id => HwCacheOp
- perf_hw_cache_op_result_id => HwCacheResult

The motivation behind this is mainly for the `type` and `config` fields
of `bpf_link_info.perf_event.event`. The newly added enums are planned
to also be used in the `bpf_link_info` metadata.

Although `Breakpoint`/`PERF_TYPE_BREAKPOINT` variant exists, it is not
fully implemented. It's only usage at the moment is in link info.
2025-10-16 09:51:10 -04:00