Commit Graph

334 Commits

Author SHA1 Message Date
Dave Tucker 9eecbe9d0e Merge pull request #1224 from dave-tucker/unused_trait_names
chore(*): set clippy unused_trait_names = warn
2025-03-20 19:31:36 +00:00
Tamir Duberstein 77b1c6194c Add support for Flow Dissector programs
Closes #216.

Co-authored-by: Zenna Allwein <zrallwein@gmail.com>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
2025-03-20 13:54:10 -04:00
Dave Tucker f6c5cb2ad2 chore(*): set clippy unused_trait_names = warn
We have previously tried to import traits anonymously where possible but
enforcing this manually was hard.

Since Rust 1.83 clippy can now enforce this for us.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2025-03-20 15:16:11 +00:00
Tamir Duberstein 22c8f783e7 programs: allow program names to be static strings 2025-03-06 12:56:17 -05:00
Dave Tucker ea76e0f62d aya: parse complete entries from /proc/$pid/maps
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
2025-03-05 12:57:31 -05:00
Tamir Duberstein de0b7cee8d Destructure 2025-03-05 12:57:31 -05:00
Tamir Duberstein 7084df68aa Add punctuation 2025-03-05 12:57:31 -05:00
Tamir Duberstein 50ae0ee099 Avoid allocations when parsing /proc/{pid}/maps 2025-03-05 06:03:08 -05:00
Tamir Duberstein f0a9f19ddc Bump edition to 2024
Change FromRawTracepointArgs::arg to return T rather than *const T which
seems to have been returning a dangling pointer.

Arguably this is not strictly necessary; edition 2024 seems to be
focused on increased strictness around unsafe code which doesn't unlock
new functionality for our users. That said, this work revealed an
apparent bug (see above) that we wouldn't otherwise catch due to
allow-by-default lints.
2025-03-04 10:43:56 -05:00
Tamir Duberstein ea5f7e3015 Reduce the scope of expected warnings
This fixes a few safety comments to avoid the warnings.
2025-03-04 10:43:56 -05:00
Tamir Duberstein 4101a5a55d Use #[expect(...)] rather than #[allow(...)]
This is stricter, and revealed a few unused allowances.
2025-03-04 10:43:56 -05:00
Tamir Duberstein 27d69c35f0 aya: remove panics on indeterminate kernel version
Cache the current kernel version in a thread-local while I'm here.

Closes https://github.com/aya-rs/aya/issues/1024.
Closes https://github.com/aya-rs/aya/pull/1042.
2025-03-04 07:23:47 -05:00
Tamir Duberstein 9eefb48a0a aya: remove some copies
Replace truncation with proper NULL termination at the site of use.
2025-03-04 07:23:47 -05:00
Tamir Duberstein de1e80c1d1 aya: encode perf_event ioctl contract
Enumerate the possible ioctls in an enum and bake in the knowledge that
they all return 0 on success and -1 on error.
2025-03-03 11:28:34 -05:00
Tamir Duberstein 055e36e8d9 aya: encode perf_event_open(2) contract
Per man 2 perf_event_open:

> RETURN VALUE
>   On success, perf_event_open() returns the new file descriptor.  On
>   error, -1 is returned and errno is set to indicate the error.

Bake this into our syscalls so we stop using `_` so much which can hide
information loss. Remove the type parameter to SysResult.
2025-03-03 11:28:34 -05:00
Tamir Duberstein bdd8ae2d0b *: avoid _
This can silently discard information, so we shouldn't do it.
2025-03-03 11:28:34 -05:00
Tamir Duberstein f6df60fa70 aya: encode bpf(2) contract
Per man 2 bpf:

> RETURN VALUE
>   For a successful call, the return value depends on the operation:
>
>   BPF_MAP_CREATE
>     The new file descriptor associated with the eBPF map.
>
>   BPF_PROG_LOAD
>     The new file descriptor associated with the eBPF program.
>
>   All other commands
>     Zero.
>
>   On error, -1 is returned, and errno is set to indicate the error.

Bake this into our syscalls so we stop using `_` so much which can hide
information loss.
2025-03-03 11:28:34 -05:00
Tamir Duberstein 2d782606fe *: avoid Result::is_{ok,err}
These methods discard information. Discarding information is bad.
2025-03-03 11:28:34 -05:00
Tamir Duberstein 122c49fca4 *: appease clippy
While I'm here convert a String to a PathBuf in an error to avoid lossy
conversions.

See https://rust-lang.github.io/rust-clippy/master/index.html#io_other_error.
2025-03-03 08:28:38 -05:00
Tamir Duberstein 9198335100 codegen: remove outdated workaround
bindgen can handle these macros now.
2025-02-05 10:39:17 -05:00
Tyrone Wu 665d4f20bb chore: remove aya_obj -> obj alias
When `aya::obj` was migrated to be its own crate `aya-obj`, the `obj`
alias was created to preserve existing imports that relied on
`crate::obj`.

This resulted in 3 ways to import `aya-obj` objects:
- `use aya_obj::*`
- `use obj::*`
- `use crate::obj::*`

The `obj` alias is now removed to avoid confusion, and all `obj` imports
are funneled through `aya_obj`.
2025-02-03 16:30:23 +01:00
Dave Tucker 39cf6c12f2 feat(aya): Return error messages from netlink
This returns error strings from netlink since they are more informative
than the raw os error. For example:

"Device or Resource Busy" vs. "XDP program already attached".

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2025-02-01 21:42:52 +00:00
Tamir Duberstein 356cf45914 Use Set (instead of Map) to hold links
This uses hashbrown instead of std because the latter relies on Borrow
which requires a reference; hashbrown's Equivalent is more flexible.
2025-01-14 09:40:00 -05:00
Tamir Duberstein 69144a977e Appease clippy
```
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator
   --> aya/src/programs/uprobe.rs:282:64
    |
282 |             let path = line.split(|b| b.is_ascii_whitespace()).last()?;
    |                                                                ^^^^^^ help: try: `next_back()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#double_ended_iterator_last
```
2025-01-11 10:47:21 -05:00
Andrew Werner 628b7fb022 aya::programs::uprobe: add support for cookies
Fixes #1132.

Note that this change does not add support in the public API for kprobes
or tracepoints, but it's a trivial matter of plumbing.

Along the way, the Uprobe::attach API is cleaned up to make the
attachment location more coherent. The logic being: if we're going to be
breaking the API anyway, may as well clean it up a bit.

Furthermore, the aya::sys::bpf_link_attach function is cleaned up by
properly modeling the the union in the final field with a rust enum.
2025-01-10 11:58:57 -05:00
Tamir Duberstein 78ee9a4634 Avoid useless conversions
Use native C types to avoid platform-dependent conversions.
2024-12-27 10:27:41 -05:00
Tamir Duberstein 119049f2a2 Define {detach,take_link} in macro when possible 2024-11-24 12:25:55 -05:00
Michal Rostecki 85c73af1fe doc(aya): Improve the description of take_link methods
Don't mix the tenses.
2024-11-24 16:27:44 +01:00
Michal Rostecki bf2164c92f feat(aya): Add iterator program type
BPF iterators[0] are a way to dump kernel data into user-space and an
alternative to `/proc` filesystem.

This change adds support for BPF iterators on the user-space side. It
provides a possibility to retrieve the outputs of BPF iterator programs
both from sync and async Rust code.

[0] https://docs.kernel.org/bpf/bpf_iterators.html
2024-11-24 16:27:44 +01:00
astoycos 5478cac008 feat(aya): Implement TCX
This commit adds the initial support for TCX
bpf links. This is a new, multi-program, attachment
type allows for the caller to specify where
they would like to be attached relative to other
programs at the attachment point using the LinkOrder
type.

Signed-off-by: astoycos <astoycos@redhat.com>
Co-authored-by: Andre Fredette <afredette@redhat.com>
Co-authored-by: Dave Tucker <dave@dtucker.co.uk>
Co-authored-by: Tamir Duberstein <tamird@gmail.com>
2024-10-09 16:31:40 +07:00
Tamir Duberstein e992c280cb Replace Arc with &'static 2024-10-02 14:00:13 -04:00
Tamir Duberstein f3b2744072 Reduce duplication in {nr,possible}_cpus 2024-10-02 14:00:13 -04:00
Tamir Duberstein 2b299d4fba Replace lazy_static with std::sync::LazyLock 2024-10-02 14:00:13 -04:00
Zero King c44f8b0f5b aya: use FdLink in SockOps programs
See: https://github.com/aya-rs/aya/issues/987
2024-09-09 22:36:13 +08:00
tyrone-wu 02d1db5fc0 aya: remove unwrap and NonZero* in info
Addresses the feedback from #1007:
- remove panic from `unwrap` and `expect`
- Option<NonZero*> => Option<int> with `0` mapping to `None`

Refs: #1007
2024-09-08 12:46:02 +00:00
Harvo Jones f790685d75 Add the option to support multiple and overrideable programs per cgroup
This change allows multiple BPF programs to attach to a cgroup (via the option
`CgroupAttachMode::AllowMultiple`), and allows a program to specify that it can be
overridden by one in a sub-cgroup (via the option `CgroupAttachMode::AllowOverride`).
2024-09-04 09:45:10 +00:00
tyrone-wu 88f5ac3114 aya,obj,int-test: revamp ProgramInfo be more friendly with older kernels
Purpose of this commit is to add detections for whether a field is
available in `ProgramInfo`.
- For `program_type()`, we return the new enum `ProgramType` instead of
  the integer representation.
- For fields that we know cannot be zero, we return `Option<NonZero*>`
  type.
- For `name_as_str()`, it now also uses the feature probe `bpf_name()`
  to detect if field is available or not.
- Two additional feature probes are added for the fields:
  - `prog_info_map_ids()` probe -> `map_ids()` field
  - `prog_info_gpl_compatible()` probe -> `gpl_compatible()` field

With the `prog_info_map_ids()` probe, the previous implementation that
I had for `bpf_prog_get_info_by_fd()` is shortened to use the probe
instead of having to make 2 potential syscalls.

The `test_loaded_at()` test is also moved into info tests since it is
better related to the info tests.

`aya::programs::Programs::prog_type(&self)` now returns `ProgramType`
instead of the generated FFI from aya-obj.

Also previously, `loaded_programs()` could be accessed either through
`aya` or `aya::programs`. To avoid confusion and duplicate export of
the item, the function should now only be exposed through
`aya::programs`.
2024-09-02 15:48:58 +00:00
tyrone-wu cb8e478800 aya,integration-test: improve integration tests for info API
Improves the existing integraiton tests for `loaded_programs()` and
`loaded_maps()` in consideration for older kernels:
  - Opt for `SocketFilter` program in tests since XDP requires v4.8 and
    fragments requires v5.18.
  - For assertion tests, first perform the assertion, if the assertion
    fails, then it checks the host kernel version to see if it is above
    the minimum version requirement. If not, then continue with test,
    otherwise fail.
    For assertions that are skipped, they're logged in stderr which can
    be observed with `-- --nocapture`.

This also fixes the `bpf_prog_get_info_by_fd()` call for kernels below
v4.15. If calling syscall  on kernels below v4.15, it can produce an
`E2BIG` error  because `check_uarg_tail_zero()` expects the entire
struct to all-zero bytes (which is caused from the map info).

Instead, we first attempt the syscall with the map info filled, if it
returns `E2BIG`, then perform syscall again with empty closure.

Also adds doc for which version a kernel feature was introduced for
better  awareness.

The tests have been verified kernel versions:
  - 4.13.0
  - 4.15.0
  - 6.1.0
2024-09-01 17:36:25 +00:00
Alessandro Decina ab000ad7c3 Merge pull request #959 from tyrone-wu/aya/program_info_stats
aya,aya-obj: expose run_time_ns & run_cnt fields, and add bpf_enable_stats util function
2024-09-01 20:07:19 +07:00
Tamir Duberstein 4362020691 Simplify doctest 2024-08-26 10:13:41 -04:00
Tamir Duberstein bce3c4fb1d Appease nightly clippy
```
error: first doc comment paragraph is too long
  --> aya/src/programs/raw_trace_point.rs:12:1
   |
12 | / /// A program that can be attached at a pre-defined kernel trace point, but also
13 | | /// has an access to kernel internal arguments of trace points, which
14 | | /// differentiates them from traditional tracepoint eBPF programs.
15 | | ///
16 | | /// The kernel provides a set of pre-defined trace points that eBPF programs can
   | |_
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_long_first_doc_paragraph

error: first doc comment paragraph is too long
  --> ebpf/aya-ebpf/src/helpers.rs:1:1
   |
1  | / //! This module contains kernel helper functions that may be exposed to specific BPF
2  | | //! program types. These helpers can be used to perform common tasks, query and operate on
3  | | //! data exposed by the kernel, and perform some operations that would normally be denied
4  | | //! by the BPF verifier.
5  | | //!
6  | | //! Here, we provide some higher-level wrappers around the underlying kernel helpers, but
   | |_
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_long_first_doc_paragraph
```
2024-08-26 10:13:41 -04:00
tyrone-wu a25f501ece aya: expose run_time_ns and run_cnt fields in ProgramInfo
Added functions to expose `run_time_ns` & `run_cnt` statistics from
ProgramInfo/bpf_prog_info.
2024-08-19 18:23:27 +00:00
Tamir Duberstein e12fcf46cb Use MockableFd everywhere
Rust 1.80 contains https://github.com/rust-lang/rust/pull/124210,
causing tests which we skip under miri to segfault.
2024-07-29 10:42:22 +02:00
Andrew Werner d413e2f285 aya::programs::uprobe: fix bad variable name
The variable fn_name was very much *not* the fn_name, but rather the
object file path.
2024-07-10 15:36:21 -04:00
Alessandro Decina c7898c596f Fix clippy 2024-05-09 12:25:12 +10:00
swananan 462514ed4c aya: adjust symbol lookup tests for object crate alignment requirements
The object::File::parse API requires parameter to be aligned with 8 bytes.
Adjusted the Vec in the tests with miri to meet this requirement.
2024-05-09 00:23:08 +08:00
swananan e6e1bfeb58 aya: add symbol lookup in associated debug files
This change enhances the logic for symbol lookup in uprobe or uretprobe.
If the symbol is not found in the original binary, the search continues
in the debug file associated through the debuglink section. Before
searching the symbol table, it compares the build IDs of the two files.
The symbol lookup will only be terminated if both build IDs exist and do
not match. This modification does not affect the existing symbol lookup
logic.

Refs: #936
2024-05-08 18:13:05 +08:00
Tamir Duberstein 35962a4794 Document miri skip reasons 2024-05-03 08:17:42 -04:00
Dave Tucker 8c79b71bd5 feat(aya): Rename Bpf to Ebpf
And BpfLoader to EbpfLoader.
This also adds type aliases to preserve the use of the old names, making
updating to a new Aya release less of a burden. These aliases are marked
as deprecated since we'll likely remove them in a later release.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2024-03-05 11:53:12 +00:00
Gary Guo 542306d295 Add CgroupDevice::query
This follows closely the existing `LircMode2::query`.
2024-03-05 20:15:35 +11:00