Commit Graph

112 Commits (main)

Author SHA1 Message Date
tyrone-wu fbb09304a2
aya,int-test: revamp MapInfo be more friendly with older kernels
Adds detection for whether a field is available in `MapInfo`:
- For `map_type()`, we treturn new enum `MapType` instead of the integer
  representation.
- For fields that can't be zero, we return `Option<NonZero*>` type.
- For `name_as_str()`, it now uses the feature probe `bpf_name()` to
  detect if field is available.
  Although the feature probe checks for program name, it can also be
  used for map name since they were both introduced in the same commit.
2 weeks ago
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`.
2 weeks ago
tyrone-wu 1634fa7188
aya-obj: add conversion u32 to enum type for prog, link, & attach type
Add conversion from u32 to program type, link type, and attach type.
Additionally, remove duplicate match statement for u32 conversion to
`BPF_MAP_TYPE_BLOOM_FILTER` & `BPF_MAP_TYPE_CGRP_STORAGE`.

New error `InvalidTypeBinding<T>` is created to represent when a
parsed/received value binding to a type is invalid.
This is used in the new conversions added here, and also replaces
`InvalidMapTypeError` in `TryFrom` for `bpf_map_type`.
2 weeks ago
Billy McFall 4dc4b5ccd4 test: adjust test to not use byte arrays
Where possible, replace the hardcoded byte arrays in the tests with the
structs they represent, then convert the structs to byte arrays.

Signed-off-by: Billy McFall <22157057+Billy99@users.noreply.github.com>
3 weeks ago
Billy McFall b513af12e8 aya: add archs powerpc64 and s390x to aya
bpfman, a project using aya, has a requirement to support powerpc64 and
s390x architectures. Adding these two architectures to aya.

Signed-off-by: Billy McFall <22157057+Billy99@users.noreply.github.com>
3 weeks ago
Billy McFall eef7346fb2 test: adjust test byte arrays for big endian
Adding support for s390x (big endian architecture) and found that some
of the unit tests have structures and files implemented as byte arrays.
They are all coded as little endian and need a bug endian version to
work properly.

Signed-off-by: Billy McFall <22157057+Billy99@users.noreply.github.com>
3 weeks ago
dave-tucker 8d7446e011 [codegen] Update libbpf to 686f600bca59e107af4040d0838ca2b02c14ff50
Update libbpf to 686f600bca59e107af4040d0838ca2b02c14ff50

Files changed:
M	aya-obj/src/generated/linux_bindings_aarch64.rs
M	aya-obj/src/generated/linux_bindings_armv7.rs
M	aya-obj/src/generated/linux_bindings_powerpc64.rs
M	aya-obj/src/generated/linux_bindings_riscv64.rs
M	aya-obj/src/generated/linux_bindings_s390x.rs
M	aya-obj/src/generated/linux_bindings_x86_64.rs

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 months ago
dave-tucker 8b50a6a573 [codegen] Update libbpf to c1a6c770c46c6e78ad6755bf596c23a4e6f6b216
Update libbpf to c1a6c770c46c6e78ad6755bf596c23a4e6f6b216

Files changed:
M	aya-obj/src/generated/linux_bindings_aarch64.rs
M	aya-obj/src/generated/linux_bindings_armv7.rs
A	aya-obj/src/generated/linux_bindings_powerpc64.rs
M	aya-obj/src/generated/linux_bindings_riscv64.rs
A	aya-obj/src/generated/linux_bindings_s390x.rs
M	aya-obj/src/generated/linux_bindings_x86_64.rs
M	ebpf/aya-ebpf-bindings/src/aarch64/bindings.rs
M	ebpf/aya-ebpf-bindings/src/armv7/bindings.rs
A	ebpf/aya-ebpf-bindings/src/powerpc64/bindings.rs
A	ebpf/aya-ebpf-bindings/src/powerpc64/helpers.rs
M	ebpf/aya-ebpf-bindings/src/riscv64/bindings.rs
A	ebpf/aya-ebpf-bindings/src/s390x/bindings.rs
A	ebpf/aya-ebpf-bindings/src/s390x/helpers.rs
M	ebpf/aya-ebpf-bindings/src/x86_64/bindings.rs

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
3 months ago
Tamir Duberstein 35962a4794 Document miri skip reasons 5 months ago
Kevin Ji b06ff40278 xtask: Generate new bindings 6 months ago
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>
7 months ago
Dave Tucker fd48c55466 feat(aya-obj)!: Rename BpfRelocationError -> EbpfRelocationError
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
7 months ago
Dave Tucker cf3e2ca677 feat(aya-obj)!: Rename BpfSectionKind to EbpfSectionKind
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
7 months ago
Dave Tucker 0e99fa0f34 Release aya-obj v0.1.0, aya v0.12.0, safety bump aya-log v0.2.0 7 months ago
Dave Tucker 72e8aab6c8 docs(aya-obj): Add CHANGELOG
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
7 months ago
Tamir Duberstein 3369169aac aya: appease new nightly clippy lints
```
  error: unnecessary use of `get("foo").is_some()`
      --> aya-obj/src/obj.rs:1690:26
       |
  1690 |         assert!(obj.maps.get("foo").is_some());
       |                          ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key("foo")`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_get_then_check
  note: the lint level is defined here
      --> aya-obj/src/lib.rs:68:9
       |
  68   | #![deny(clippy::all, missing_docs)]
       |         ^^^^^^^^^^^
       = note: `#[deny(clippy::unnecessary_get_then_check)]` implied by `#[deny(clippy::all)]`

  error: unnecessary use of `get("foo").is_some()`
      --> aya-obj/src/obj.rs:1777:26
       |
  1777 |         assert!(obj.maps.get("foo").is_some());
       |                          ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key("foo")`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_get_then_check

  error: unnecessary use of `get("bar").is_some()`
      --> aya-obj/src/obj.rs:1778:26
       |
  1778 |         assert!(obj.maps.get("bar").is_some());
       |                          ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key("bar")`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_get_then_check

  error: unnecessary use of `get("baz").is_some()`
      --> aya-obj/src/obj.rs:1779:26
       |
  1779 |         assert!(obj.maps.get("baz").is_some());
       |                          ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key("baz")`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_get_then_check

  error: unnecessary use of `get(".bss").is_some()`
      --> aya-obj/src/obj.rs:1799:26
       |
  1799 |         assert!(obj.maps.get(".bss").is_some());
       |                          ^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key(".bss")`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_get_then_check

  error: unnecessary use of `get(".rodata").is_some()`
      --> aya-obj/src/obj.rs:1810:26
       |
  1810 |         assert!(obj.maps.get(".rodata").is_some());
       |                          ^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key(".rodata")`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_get_then_check

  error: unnecessary use of `get(".rodata.boo").is_some()`
      --> aya-obj/src/obj.rs:1821:26
       |
  1821 |         assert!(obj.maps.get(".rodata.boo").is_some());
       |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key(".rodata.boo")`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_get_then_check

  error: unnecessary use of `get(".data").is_some()`
      --> aya-obj/src/obj.rs:1832:26
       |
  1832 |         assert!(obj.maps.get(".data").is_some());
       |                          ^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key(".data")`
       |
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_get_then_check

  error: unnecessary use of `get(".data.boo").is_some()`
      --> aya-obj/src/obj.rs:1843:26
       |
  1843 |         assert!(obj.maps.get(".data.boo").is_some());
       |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key(".data.boo")`
```
7 months ago
Dave Tucker b3e7ef741c chore: Use the cargo workspace package table
This allows for inheritance of common fields from the workspace root.
The following fields have been made common:

- authors
- license
- repository
- homepage
- edition

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
7 months ago
Dave Tucker 770a95e077 chore: Appease clippy unused imports
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
7 months ago
Michal Rostecki c05a3b69b7 aya-obj: Handle lack of match of enum variants correctly
When comparing `local_spec` with `target_spec` for enum relocations,
we can encounter a situation when a matchinng variant in a candidate
spec doesn't exist.

Before this change, such case wasn't handled explicitly, therefore
resulted in returning currently constructed `target_spec` at the
end. The problem is that such `target_spec` was, due to lack of
match, incomplete. It didn't contain any `accessors` nor `parts`.

Later usage of such incomplete `target_spec` was leading to panics,
since the code operating on enums' `target_spec` expects at least
one `accessor` to be available.

Fixes #868
8 months ago
Alessandro Decina 35e21ae007 aya: don't parse labels as programs
Fixes a bug introduced by https://github.com/aya-rs/aya/pull/413 where
we were generating a bunch of spurious LBB* programs.
11 months ago
Tamir Duberstein cc48523347
Cargo.toml: remove redundant keys
`default-features = false` is already in the root Cargo.toml.
11 months ago
Tamir Duberstein d16e607fd4
rustfmt: group_imports = "StdExternalCrate"
High time we stop debating this; let the robots do the work.
12 months ago
Tuetuopay 579e3cee22 aya, bpf: misc fixes following review comments 1 year ago
Tuetuopay 00dc7a5bd4 maps/xdp: make maps work on kernels not supporting ProgIds
On startup, the kernel is probed for support of chained program ids for
CpuMap, DevMap and DevMapHash, and will patch maps at load time to have
the proper size. Then, at runtime, the support is checked and will error
out if a program id is passed when the kernel does not support it.
1 year ago
Tuetuopay 139f382638 aya: add support for map-bound XDP programs
Such programs are to be bound to cpumap or devmap instead of the usual
network interfaces.
1 year ago
Tamir Duberstein f41592663c
maps: `MapFd` and `SockMapFd` are owned
`MapData::fd` is now a `MapFd`. This means that `MapData` now closes the
file descriptor on drop. In the future we might consider making `MapFd`
hold a `BorrowedFd` but this requires API design work due to overlapping
borrows.

Since `SockMapFd` is no longer `Copy`, attach methods to take it by
reference to allow callers to use it multiple times as they are
accustomed to doing.

`SockMapFd` implements `try_clone`. `MapFd` and `SockMapFd` are now
returned by reference to allow callers to avoid file descriptor cloning
when desired.

This is an API breaking change.

Updates #612.
1 year ago
Tamir Duberstein c139627f8f aya-obj: reduce indirection in section parsing
Remove repetition of permitted cgroup attach types. Make optionality of
name more explicit rather than pretending both kind and name are equal
to section.
1 year ago
Tamir Duberstein 572d047e37
test: avoid lossy string conversions
We can be strict in tests.
1 year ago
Tamir Duberstein 89bc255f1d
aya: MapData::fd is non-optional
The primary driver of change here is that `MapData::create` is now a
factory function that returns `Result<Self, _>` rather than mutating
`&mut self`. The remaining changes are consequences of that change, the
most notable of which is the removal of several errors which are no
longer possible.
1 year ago
Dave Tucker 02124002c8 .github: Add clang-format
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
1 year ago
Tamir Duberstein dfb6020a1d
aya-obj: s/types.types[i]/*t/ where possible
We already have a mutable reference in scope, use it where possible.
1 year ago
Dave Tucker e38e2566e3 aya, aya-obj: Implement ENUM64 fixups
This commit adds:

- A probe to see if the ENUM64 feature is supported
- Fixups for the use of signed enums, or enum64 types
  on systems where enum64 is not supported

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
1 year ago
Dave Tucker 098d4364bd aya-obj: Mutate BTF in-place without clone
The BTF we're working on is Cow anyway so modifying in-place is fine.
All we need to do is store some information before we start our
mutable iteration to avoid concurrently borrowing types both mutably and
immutably.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
1 year ago
Tamir Duberstein 826e0e5050
btf: use Self instead of restating the type 1 year ago
Tamir Duberstein 2a054d76ae
btf: avoid multiple vector allocations
Rather than creating an empty vector and iteratively appending - which
might induce intermediate allocations - create an ExactSizeIterator and
collect it into a vector, which should produce exactly one allocation.
1 year ago
Dave Tucker 79ea64ca7f aya: Fix (func|line)_info multiple progs in section
This commit fixes the (func|line)_info when we have multiple programs in
the same section. The integration test reloc.bpf.c serves as our test
case here. This required filtering down the (func|line)_info to only
that in scope of the current symbol + then adjusting the offets to
appease the kernel.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
1 year ago
Dave Tucker cca9b8f1a7 aya-obj: Remove name from ProgramSection
The name here is never used as we get the program name from the symbol
table instead.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
1 year ago
Dave Tucker 677e7bda4a aya-obj: Propagate sleepable into ProgramSection
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
1 year ago
Dave Tucker e833a71b02
Merge pull request #413 from dave-tucker/fix-names-once-and-for-all
aya: fix names once and for all
1 year ago
Dave Tucker bf7fdff1ce aya: Find programs using the symbol table
This makes a few changes to the way that Aya reads the ELF object
files.

1. To find programs in a section, we use the symbols table. This allows
   for cases where multiple programs could appear in the same section.
2. When parsing our ELF file we build symbols_by_section_index as an
   optimization as we use it for legacy maps, BTF maps and now programs.

As a result of theses changes the "NAME" used in `bpf.prog_mut("NAME")`
is now ALWAYS the same as the function name in the eBPF code, making the
user experience more consistent.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
1 year ago
Tamir Duberstein 17f25a6793
all: better panic messages
Always include operands in failing assertions. Use assert_matches over
manual match + panic.
1 year ago
Tamir Duberstein 8961be9526
Do not escape newlines on Err(LoadError).unwrap()
Wrap verifier logs in a newtype whose `Debug` impl emits unescaped
newlines. This improves ergonomics in tests where we `Result::unwrap()`
those load errors; when these fail today they emit the errors with
newlines escaped, making them incredibly difficult to read.
1 year ago
Mike Rostecki 96fa08bd82 cargo: Define dependencies on the workspace level
This way we will avoid version mismatches and make differences in
features across our crates clearer.
1 year ago
Tamir Duberstein 6f2a8c8a5c
aya: avoid an allocation
str::rsplitn(2, c) is a special case that can be expressed as
str::rsplit_once(c).
1 year ago
Tamir Duberstein d71d1e1993
aya: remove dead code
This logic moved in bb595c4e69. The
mutation here prevented the compiler from noticing.
1 year ago
Tamir Duberstein 961f45da37
Replace matches with assert_matches
The matches crate has been archived now that `matches!` is in std.
However `assert_matches!` is still unstable in std, and the
assert_matches crate provides a more expressive form:

```
assert_matches!(foo, Ok(bar) => {
  assert_eq!(bar, baz);
});
```
1 year ago
Tamir Duberstein 8e9712ac02
Run tests with powerset of features 1 year ago
Tamir Duberstein e621a09181
Clippy over tests and integration-ebpf
Replace all `assert!(matches!(..))` with `assert_matches!(..)`.

Remove the now-unused build-integration-test xtask command whose logic
doesn't match that of the build-and-run command.
1 year ago
Tamir Duberstein b5ebcb7cc5
Remove verifier log special case 1 year ago
Tamir Duberstein 6b94b2080d
Hide details of VerifierLog
This type is really only used by one function.
1 year ago