Commit Graph

145 Commits (c010505f193086e5a5abc4a99494a5f74cf994b4)

Author SHA1 Message Date
Tamir Duberstein fd2fb98792 Revert "bpf: appease nightly lint"
This reverts commit 9861c1446e.

This no longer warns. See
https://github.com/rust-lang/rust/commit/d95d6ceecb372c66ed18a4e7a0bbb7.
8 months ago
Tamir Duberstein 9861c1446e
bpf: appease nightly lint
```
error: field `0` is never read
   --> bpf/aya-bpf/src/helpers.rs:737:22
    |
737 | pub struct PrintkArg(u64);
    |            --------- ^^^
    |            |
    |            field in this struct
    |
    = note: `PrintkArg` has a derived impl for the trait `Clone`, but this is intentionally ignored during dead code analysis
    = note: `-D dead-code` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(dead_code)]`
help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
    |
737 | pub struct PrintkArg(());
    |                      ~~
```

See https://github.com/rust-lang/rust/issues/119659.
9 months ago
Dave Tucker 19af2497d7 aya-bpf: Fix XDP Map documentation
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
9 months ago
Andrew Werner e2cf734490 aya: Implement RingBuf
This implements the userspace binding for RingBuf.

Instead of streaming the samples as heap buffers, the process_ring
function takes a callback to which we pass the event's byte region,
roughly following [libbpf]'s API design. This avoids a copy and allows
marking the consumer pointer in a timely manner.

[libbpf]: https://github.com/libbpf/libbpf/blob/master/src/ringbuf.c

Additionally, integration tests are added to demonstrate the usage
of the new APIs and to ensure that they work end-to-end.

Co-authored-by: William Findlay <william@williamfindlay.com>
Co-authored-by: Tatsuyuki Ishi <ishitatsuyuki@gmail.com>
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 0edc13b4d4 bpf: add a shared try_redirect_map function for XDP maps 1 year ago
Tuetuopay 579e3cee22 aya, bpf: misc fixes following review comments 1 year ago
Tuetuopay 63ce2f013a bpf/devmap: don't expose `bpf_devmap_value`
Use our own type that:
- is stable as not from bindgen
- does not have an union inside
1 year ago
Tuetuopay 9ed1d3d281 bpf: add documentation for XDP maps 1 year ago
Tuetuopay db49633073 bpf: make xdp maps functions safe
Values in those map are small enough to return copied values instead of
reference to values.
1 year ago
Tuetuopay 0647927e32 xdp: add support for chained xdp programs in {cpu,dev}map
set/insert functions can now take an optional bpf program fd to run once
the packet has been redirected from the main probe
1 year ago
Tuetuopay ad3087d7eb bpf: Update XDP maps implementation
The implementation changed since the original commit was written, and
some mistakes went in:
- missing bpf_redirect_map wrapper
- extra bpf_map_lookup_elem on maps for which it is forbidden
1 year ago
Dave Tucker e90d521a21 bpf: Implement XDP maps
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
1 year ago
Dave Tucker 764eb309b0 Clippy fixes for latest nightly
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
1 year ago
Tamir Duberstein 6d06e2bf3a
Include ~all crates in default members
Several tests were not running due to being omitted from this list.
1 year ago
Tamir Duberstein fa91fb4f59
Remove "async" feature
This feature is equivalent to async_tokio || async_std; removing it
avoids warnings emitted during `cargo hack check --feature-powerset`
where async is selected without either of the other features.

Use cargo hack to ensure clippy runs on the powerset of features.
1 year ago
Tamir Duberstein 4fef255823
disallow (some) as conversions
See https://rust-lang.github.io/rust-clippy/master/#/as_conversions.
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
Alessandro Decina c89c485bca
Merge pull request #627 from nak3/add-riscv
bpf: Add `pt_regs` handling in aya-bpf/args.rs for riscv64
1 year ago
Alessandro Decina 11c227743d bpf: improve bpf_probe_read_kernel_str_bytes and bpf_probe_read_user_str_bytes
This change does a few things:

- it fixes a bug in the wrappers, where we were expecting the kernel to
  return len=1 for b"\0" where it instead returns 0 and doesn't write
  out the NULL terminator

- it makes the helpers more robust by hardcoding bound checks in
  assembly so that LLVM optimizations can't transform the checks in a
  way that the verifier can't understand.

- it adds integration tests
1 year ago
Kenjiro Nakayama 7dfabd07a7 Add pt_regs handling in aya-bpf/args.rs for riscv64
This patch introduces `pt_regs` handling in aya-bpf/args.rs
for the riscv64 architecture. The current CI is disabled
for riscv64 because this implementation is missing.
1 year ago
Kabir Kwatra 92f9c43230
feat(bpf+sk_skb): wrap `change_proto` helper 1 year ago
Quentin JEROME d031ce78bf fix issue #552 2 years ago
Alessandro Decina 556463a85f ebpf: SkbBuff: add some accessors 2 years ago
Michal Rostecki 33baf7ef22 aya-bpf/maps: Add `get_ptr` and `get_mut_ptr` methods to Array
Signed-off-by: Michal Rostecki <vadorovsky@gmail.com>
2 years ago
Tuetuopay 890e8c9340 bpf: fix set_mark by not copying __sk_buff
Such an assignment in two parts (first deref in `unsafe`, then field
access outside of `unsafe`) is bogus: the deref "returned" by the
`unsafe` block actually creates a copy of the `__sk_buff` struct because
it implements `Copy`. The mark value is written to the `mark` field of
the copy, and not the real `__sk_buff`.

Change it to do it all in the `unsafe` block.

The same is done for the `.len()` getter to avoid copying the whole
`__sk_buff` struct for a 32 bit field. Although such a copy should be
optimized out by the compiler, it's better to help it do so.
2 years ago
Milan 8f1163a400 Add support for BPF_PROG_TYPE_CGROUP_DEVICE
Kernel 4.15 added a new eBPF program that can
be used with cgroup v2 to control & observe device
access (e.g. read, write, mknod) - `BPF_PROG_TYPE_CGROUP_DEVICE`.

We add the ability to create these programs with the `cgroup_device`
proc macro which creates the `cgroup/dev` link section. Device
details are available to the eBPF program in `DeviceContext`.

The userspace representation is provided with the `CgroupDevice`
structure.

Fixes: #212
Signed-off-by: Milan <milan@mdaverde.com>
2 years ago
Alessandro Decina 88d7777553
Merge pull request #431 from 0b01/refs
aya: use impl Borrow<T> instead of T for maps
2 years ago
tiann 49404367d8 bpf: Add bindings for uid and gid 2 years ago
Ricky Han fbfbedb6a8 cargo fmt 2 years ago
Ricky Han e9ec257328 Add test case 2 years ago
abhijeetbhagat 6c813b8c38 fix all clippy warnings 2 years ago
Michal Rostecki 895f96e971 ebpf: Add TcContext for classifier programs
This change separates the previous `SkBuffContext` into three structs:

* `SkBuff` which is a wrapper around `__sk_buff` which contains all
  possible methods operating on it.
* `SkBuffContext` which is a program context for programs which
  **cannot** access `__sk_buff` directly and instead can only use
  `load_bytes`.
* `TcContext` which is a classifier context which can access `__sk_buff`
  directly, hence exposes `data` and `data_end`.

Signed-off-by: Michal Rostecki <vadorovsky@gmail.com>
2 years ago
Hengqi Chen c713dde061 aya-bpf/programs: Add `pull_data` method to SKB context
The pull_data method is used to ensure that all the required bytes
are available in the linear portion of the skb.

Signed-off-by: Hengqi Chen <chenhengqi@outlook.com>
2 years ago
Hengqi Chen e12e8a9ded aya-bpf/maps: Create LPMTrie with BPF_F_NO_PREALLOC
The Linux kernel requires BPF_F_NO_PREALLOC on creating LPMTrie ([0]).
Add BPF_F_NO_PREALLOC flag in LPMTrie constructor.

Closes #332.

  [0]: https://github.com/torvalds/linux/blob/9e6b19a66d9b/kernel/bpf/lpm_trie.c#L551

Signed-off-by: Hengqi Chen <chenhengqi@outlook.com>
2 years ago
Gabi 14bad9ba92
aya-bpf: minor fix lpm_trie::key docs 2 years ago
Dave Tucker 6ab7148731 bpf: Only use never type with rust nightly
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Joel Höner bfb5536f12 bpf: avoid stack-alloc for `fmt` in `bpf_printk!` 2 years ago
Joel Höner da13143c05 bpf: add `bpf_printk!` helper
The `bpf_printk!` macro is a helper providing a convenient way to invoke the
`bpf_trace_printk` and `bpf_trace_vprintk` BPF helpers. It is implemented as
a macro because it requires variadic arguments.
2 years ago
Michal Rostecki 6c9d814860
Merge pull request #345 from vadorovsky/bpf-get-current-comm-u8
bpf: Change the result type of bpf_get_current_comm helper
2 years ago
Michal Rostecki c6e1d56684 bpf: Add `load_bytes` method to SKB context
This new method allows to load bytes into the given bytes slice, not
requiring to alloate the memory on stack. It can be used with
PerCpuArrays.

Example:

https://github.com/vadorovsky/aya-examples/blob/main/tc-bytes/tc-bytes-ebpf/src/main.rs

Signed-off-by: Michal Rostecki <vadorovsky@gmail.com>
2 years ago
Michal Rostecki ee15fbbbc5 bpf: Use `then_some` instead of `then(|| [...])`
This change fixes the `unnecessary_lazy_evaluations` clippy warning.

Signed-off-by: Michal Rostecki <vadorovsky@gmail.com>
2 years ago
Michal Rostecki a3a96b8415 bpf: Change the result type of bpf_get_current_comm helper
Change it from `[i8; 16]` to `[u8; 18]`. `i8` arrays cannot be easily used in
Rust for converting to string (i.e. with `core::str::from_utf8_unchecked`)
and developers have to convert them themselves with unsafe code.

Using u8 arrays lets developers to just convert it with
`core::str::from_utf8_unchecked` without any limitations.

Example:

https://github.com/vadorovsky/aya-examples/blob/main/clone/clone-ebpf/src/main.rs

Signed-off-by: Michal Rostecki <vadorovsky@gmail.com>
2 years ago
Alessandro Decina 6f301ac9ca
Merge pull request #284 from vadorovsky/str
bpf(helpers): Add *_str_bytes helpers
2 years ago
Johannes Edmeier 2d79f22b40 aya-bpf: use bpf_read_probe for reading pt_regs
Linux Kernels < 5.5 don't support bpf_probe_read_kernel. Therefore
bpf_probe_read must be used for compatibility reasons.
2 years ago
Michal Rostecki 78e58b8960 bpf(helpers): Add *_str_bytes helpers
This change adds two new helpers:

* bpf_probe_read_user_str_bytes
* bpf_probe_read_kernel_str_bytes

Those new helpers are returning a bytes slice (`&[u8]`) with a length
equal to the length of probed, null-terminated string. When using those
helpers, users don't have to manually check for length and create such
slices themselves. They also make converting to `str` way more
convenient, for example:

```rust
let my_str = unsafe {
    core::str::from_utf8_unchecked(
        bpf_probe_read_user_str_bytes(user_ptr, &mut buf)?
    )
};
```

This change also deprecates the old helpers, since their names are
confusing (they have nothing to do with Rust `str`) and using them
requires writing boilerplate code (for checking the length and making
eBPF verifier happy):

* bpf_probe_read_user_str
* bpf_probe_read_kernel_str

Tested on:

516b29af68

Signed-off-by: Michal Rostecki <vadorovsky@gmail.com>
2 years ago
Alessandro Decina 150dc1b610
Merge pull request #326 from NoneTirex/fix-map-delete-return-value
Change condititon for some bpf helpers
2 years ago
tirex caa66cabac Add missing inline macro to xdp methods 2 years ago
tirex 741c35f555 Restore previous check for bpf_get_stackid 2 years ago
tirex 42c4d5c3af Temporary change return value condition to avoid problems with possibly (not aware) cast from int to long 2 years ago