Commit Graph

90 Commits (ad460879ef84b25c93f77b7bfb8bf04b6d33ec08)

Author SHA1 Message Date
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
Tamir Duberstein b611038d5b
Use procfs crate for kernel version parsing
This allows the logic to be shared between aya and the integration tests
without exposing additional public API surface.
1 year ago
Tamir Duberstein 27120b328a
aya: don't allocate static strings 1 year ago
Mary 5c86b7ee95
Merge pull request #635 from marysaka/misc/aya-obj-enum-public
misc: aya-obj: Make it possible to externally assemble BtfEnum
1 year ago
Mary d9dfd94f29 misc: aya-obj: Make it possible to externally assemble BtfEnum 1 year ago
Dave Tucker 47f764c191 aya: Make Features part of the public API
This commit adds a new probe for bpf_attach_cookie, which would be used
to implement USDT probes. Since USDT probes aren't currently supported,
we this triggers a dead_code warning in clippy.

There are cases where exposing FEATURES - our lazy static - is actually
helpful to users of the library. For example, they may wish to choose to
load a different version of their bytecode based on current features.
Or, in the case of an orchestrator like bpfd, we might want to allow
users to describe which features their program needs and return nice
error message is one or more nodes in their cluster doesn't support the
necessary feature set.

To do this without breaking the API, we make all the internal members of
the `Features` and `BtfFeatures` structs private, and add accessors for
them. We then add a `features()` API to avoid leaking the
lazy_static.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
1 year ago
Mary 93435fc854 aya: allow global value to be optional
This allow to not error out when a global symbol is missing from the object.
1 year ago
dependabot[bot] f5f8083441
build(deps): update hashbrown requirement from 0.13 to 0.14
Updates the requirements on [hashbrown](https://github.com/rust-lang/hashbrown) to permit the latest version.
- [Changelog](https://github.com/rust-lang/hashbrown/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rust-lang/hashbrown/compare/v0.13.1...v0.14.0)

---
updated-dependencies:
- dependency-name: hashbrown
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
1 year ago
dependabot[bot] fa3dd4bef2
build(deps): update rbpf requirement from 0.1.0 to 0.2.0
Updates the requirements on [rbpf](https://github.com/qmonnet/rbpf) to permit the latest version.
- [Commits](https://github.com/qmonnet/rbpf/compare/v0.1.0...v0.2.0)

---
updated-dependencies:
- dependency-name: rbpf
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
1 year ago
Mary 85ad0197e0
Merge pull request #563 from marysaka/fix/reloc-less-strict
aya-obj: Make relocations less strict
1 year ago
Mary 35eaa50736 aya-obj: Make relocations less strict
Missing relocations at load time shouldn't cause an error in aya-obj
but instead poison related instructions.

This makes struct flavors work.
1 year ago
Alessandro Decina 3a9a54fd9b
Merge pull request #602 from marysaka/fix/btf-reloc-all-functions
aya: Apply BTF relocations to all functions
1 year ago
Mary c4e721f3d3 aya: Apply BTF relocations to all functions
This fix aya wrong logic causing non entrypoint functions to not have
any BTF relocations working.

Also fix missing section_offset computation for instruction offset in
multiple spots.
1 year ago
alessandrod 0bc886f163 [codegen] Update libbpf to f7eb43b90f4c8882edf6354f8585094f8f3aade0Update libbpf to f7eb43b90f4c8882edf6354f8585094f8f3aade0
Files changed:
M	aya-obj/src/generated/btf_internal_bindings.rs
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_riscv64.rs
M	aya-obj/src/generated/linux_bindings_x86_64.rs
M	bpf/aya-bpf-bindings/src/aarch64/bindings.rs
M	bpf/aya-bpf-bindings/src/armv7/bindings.rs
M	bpf/aya-bpf-bindings/src/riscv64/bindings.rs
M	bpf/aya-bpf-bindings/src/x86_64/bindings.rs
1 year ago
Andrés 5894c4ce82
Fix load errors for empty (but existent) BTF/BTF.ext sections (#608)
* use the hdr_len of BTF.ext sections rather than size of struct

Otherwise this will erroneously fail on older btf_ext_header that have
less fields than the bindgen'd struct

* do not attempt to load a BTF object that has no types

* add tests

* fix: hdr_len i32 -> u32

* guard against a bigger header in the future

* use separate unsafe blocks

* simplify writing to zero'd out header

* merge safe block and address typo
1 year ago
Alessandro Decina 5165bf2f99
Merge pull request #585 from probulate/tag-len-value
aya-log-common: support logging byte slices
1 year ago
Alessandro Decina 9c437aafd9
Merge pull request #605 from marysaka/fix/global-data-reloc-ancient-kernels
aya: Do not create data maps on kernel without global data support
1 year ago
Mary 591e21267a aya: Do not create data maps on kernel without global data support
Fix map creation failure when a BPF have a data section on older
kernel. (< 5.2)

If the BPF uses that section, relocation will fail accordingly and
report an error.
1 year ago
Mary 18b3d75d09 aya-obj: Fix ProgramSection::from_str for bss and rodata sections 1 year ago
Tamir Duberstein 4e2f8322cc
Build tests with all features
Remove mem::forget::<HashMap>() calls in tests which fail to compile when
HashMap is provided by hashbrown:

  info: running `cargo check --all-targets --no-default-features` on aya-obj (11/23)
      Checking aya-obj v0.1.0 (/home/tamird/src/aya/aya-obj)
  error[E0505]: cannot move out of `map` because it is borrowed
     --> aya-obj/src/relocation.rs:594:21
      |
  578 |         let map = fake_legacy_map(1);
      |             --- binding `map` declared here
  579 |         let maps_by_symbol = HashMap::from([(1, ("test_map", Some(1), &map))]);
      |                                                                       ---- borrow of `map` occurs here
  ...
  594 |         mem::forget(map);
      |                     ^^^ move out of `map` occurs here
  595 |     }
      |     - borrow might be used here, when `maps_by_symbol` is dropped and runs the destructor for type `hashbrown::HashMap<usize, (&str, Option<i32>, &maps::Map)>`

  error[E0505]: cannot move out of `map_1` because it is borrowed
     --> aya-obj/src/relocation.rs:655:21
      |
  632 |         let map_1 = fake_legacy_map(1);
      |             ----- binding `map_1` declared here
  ...
  635 |             (1, ("test_map_1", Some(1), &map_1)),
      |                                         ------ borrow of `map_1` occurs here
  ...
  655 |         mem::forget(map_1);
      |                     ^^^^^ move out of `map_1` occurs here
  656 |         mem::forget(map_2);
  657 |     }
      |     - borrow might be used here, when `maps_by_symbol` is dropped and runs the destructor for type `hashbrown::HashMap<usize, (&str, Option<i32>, &maps::Map)>`

  error[E0505]: cannot move out of `map_2` because it is borrowed
     --> aya-obj/src/relocation.rs:656:21
      |
  633 |         let map_2 = fake_legacy_map(2);
      |             ----- binding `map_2` declared here
  ...
  636 |             (2, ("test_map_2", Some(2), &map_2)),
      |                                         ------ borrow of `map_2` occurs here
  ...
  656 |         mem::forget(map_2);
      |                     ^^^^^ move out of `map_2` occurs here
  657 |     }
      |     - borrow might be used here, when `maps_by_symbol` is dropped and runs the destructor for type `hashbrown::HashMap<usize, (&str, Option<i32>, &maps::Map)>`

  error[E0505]: cannot move out of `map` because it is borrowed
     --> aya-obj/src/relocation.rs:694:21
      |
  678 |         let map = fake_btf_map(1);
      |             --- binding `map` declared here
  679 |         let maps_by_symbol = HashMap::from([(1, ("test_map", Some(1), &map))]);
      |                                                                       ---- borrow of `map` occurs here
  ...
  694 |         mem::forget(map);
      |                     ^^^ move out of `map` occurs here
  695 |     }
      |     - borrow might be used here, when `maps_by_symbol` is dropped and runs the destructor for type `hashbrown::HashMap<usize, (&str, Option<i32>, &maps::Map)>`

  error[E0505]: cannot move out of `map_1` because it is borrowed
     --> aya-obj/src/relocation.rs:755:21
      |
  732 |         let map_1 = fake_btf_map(1);
      |             ----- binding `map_1` declared here
  ...
  735 |             (1, ("test_map_1", Some(1), &map_1)),
      |                                         ------ borrow of `map_1` occurs here
  ...
  755 |         mem::forget(map_1);
      |                     ^^^^^ move out of `map_1` occurs here
  756 |         mem::forget(map_2);
  757 |     }
      |     - borrow might be used here, when `maps_by_symbol` is dropped and runs the destructor for type `hashbrown::HashMap<usize, (&str, Option<i32>, &maps::Map)>`

  error[E0505]: cannot move out of `map_2` because it is borrowed
     --> aya-obj/src/relocation.rs:756:21
      |
  733 |         let map_2 = fake_btf_map(2);
      |             ----- binding `map_2` declared here
  ...
  736 |             (2, ("test_map_2", Some(2), &map_2)),
      |                                         ------ borrow of `map_2` occurs here
  ...
  756 |         mem::forget(map_2);
      |                     ^^^^^ move out of `map_2` occurs here
  757 |     }
      |     - borrow might be used here, when `maps_by_symbol` is dropped and runs the destructor for type `hashbrown::HashMap<usize, (&str, Option<i32>, &maps::Map)>`

  For more information about this error, try `rustc --explain E0505`.
  error: could not compile `aya-obj` due to 6 previous errors
  warning: build failed, waiting for other jobs to finish...
  error: process didn't exit successfully: `/home/tamird/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo check --all-targets --manifest-path aya-obj/Cargo.toml --no-default-features` (exit status: 101)
1 year ago
Mary 9e1109b3ce aya: Move program's functions to the same map 1 year ago
Kenjiro Nakayama e67025b66f Drop unnecessary mut 1 year ago
vadorovsky deb054afa4
Merge pull request #577 from aya-rs/dependabot/cargo/object-0.31
build(deps): update object requirement from 0.30 to 0.31
1 year ago