Commit Graph

119 Commits (176d61ae23c3977a8f2b6c9a41286604ebe9280e)

Author SHA1 Message Date
tiann 49404367d8 bpf: Add bindings for uid and gid 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
Michal Rostecki 944d6b8a16 Change from Rust edition 2018 to 2021
Rust 2021 adds more core prelude imports, including `TryFrom` and
`TryInto`.

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
Dave Tucker dc31e11691 Re-organize into a single workspace
This commit moves the aya-log projects from the subtree and adds them to
the main cargo workspace. It also brings the BPF crates into the
workspace and moves the macro crates up a level since they aren't BPF
code.

Miri was disabled for aya-bpf as the previous config wasn't actually
checking anything.

CI, clippy, fmt and release configurations have all been adjusted
appropriately.

CI was not properly running for other supported arches which was also
ixed here.

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
tirex 5b1a3ed866 Fix return value for map removing/deleting 2 years ago
Dave Tucker 713cd4e858 docs: Add crabby logo
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Alessandro Decina 7549eb979c
Merge pull request #315 from dave-tucker/sock
Add support for BPF_PROG_TYPE_CGROUP_SOCK
2 years ago
Dave Tucker 05c1586202 bpf: bpf_probe_read_kernel fields in pt_regs
As it turns out, the verifier gets upset if you try to read the values
directly without using bpf_probe_read.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Tatsuyuki Ishi 5e24cf7a29
bpf: Remove unnecessary Sync bound on PerCpuArray<T> 2 years ago
Alessandro Decina 82cd3e695a ebpf: PerCpuArray: remove get_mut() and add get_ptr() and get_ptr_mut()
get_mut() wasn't sound as it allowed creating multiple mutable
references.
2 years ago
Alessandro Decina 1eb9ef5488
Merge pull request #290 from ishitatsuyuki/immut-self
bpf: Replace map types to use &self, remove HashMap::get_mut
2 years ago
Dave Tucker 6f51b616e1 bpf: Implement BPF_PROG_TYPE_CGROUP_SOCK
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Dave Tucker 9d18a48c3c bpf: Add accessors to SockOpsContext
Allow getting family, remote/local v4/v6 addresses and the remote and
local ports.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Dave Tucker c725e72796 bpf: re-reun build.rs if env changed
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Dave Tucker e3c8c659a1 bpf: arm fix pt_regs handling
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Tatsuyuki Ishi 41c6b56142 bpf: Replace map types to use &self, remove HashMap::get_mut
The bpf_map_defs are now wrapped with UnsafeCell, which also happens to
provide a cozy way to get a *mut pointer. An UnsafeCell isn't strictly
required as the struct fields are practically opaque to us, but using an
UnsafeCell follows the general best practices against miscompilation and
also prevents some obvious errors.

HashMap::get_mut was removed because the idea is completely unsound.
Previous users should wrap their data with UnsafeCell instead or use
atomics.

Closes: https://github.com/aya-rs/aya/issues/233
2 years ago
Dave Tucker 218d4842d6 bpf: Be consistent with arm target_arch
This is `arm` in bindings but `armv7` in args.
Let's use `arm`

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Dave Tucker 7f6da53c34 bpf: Fix pt_regs for aarch64 (again)
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Dave Tucker 1d06b41e57 ci: Test all architectures
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Dave Tucker 7e828df1c9 bpf: Fix aarch64 access to regs
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Alessandro Decina 6b1f47323b
Merge pull request #293 from dave-tucker/getters-be-gone
Getters be gone
2 years ago
Alessandro Decina 16337001e4
Merge pull request #286 from nak3/add-BPF_MAP_TYPE_BLOOM_FILTER
Add support for BPF_MAP_TYPE_BLOOM_FILTER
2 years ago
Kenjiro Nakayama c5d5695a71 Add missing BPF_MAP_TYPE_STACK in BPF code
Currently BPF_MAP_TYPE_STACK is supported by user code (`aya/src/maps/stack.rs`),
but it does not support the BPF_MAP_TYPE_STACK BPF code.

This patch fixes it.
2 years ago
Dave Tucker 8672ff6c56 bpf: Remove generated getters
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Dave Tucker 74704c3b28 bpf: Support BPF_PROG_TYPE_SK_LOOKUP
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Kenjiro Nakayama c4262f793d Add support for BPF_MAP_TYPE_BLOOM_FILTER
This patch adds support for `BPF_MAP_TYPE_BLOOM_FILTER`.
2 years ago
Dave Tucker 4acd996cb8 bpf: Add PtRegs wrapper
This adds a portable wrapper around pt_regs and user_pt_regs.
It makes writing Raw Tracepoint or KProbe programs easier when the
arguments are one of these types while also ensuring code is portable
across architectures

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Dave Tucker 4a32e7d985 clippy: fix new lints on nightly
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Michal Rostecki 3262f85925 bpf(doc): Hide docs of bindings
Before this change, documentation of helper functions (defined by us,
not bindings) were not visible, because `use gen::*` was overriding them
with helpers coming from aya-bpf-bindings, which have the same names and
no docs.

Signed-off-by: Michal Rostecki <vadorovsky@gmail.com>
2 years ago
Kenjiro Nakayama e68d734c68
Add support for BPF_PROG_TYPE_CGROUP_SOCKOPT (#268) 2 years ago
Dave Tucker 2bac924464 bpf: Add support for BPF_PROG_TYPE_CGROUP_SOCK_ADDR
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
2 years ago
Kenjiro Nakayama f721021a0a
Add support for BPF_PROG_TYPE_CGROUP_SYSCTL (#256)
* Add support for BPF_PROG_TYPE_CGROUP_SYSCTL

This patch adds support for `BPF_PROG_TYPE_CGROUP_SYSCTL`.

* Parse unnamed macro

* Fix docs
2 years ago