Commit Graph

248 Commits (9eefb48a0ad90831c09c7bd941d035b34e1f1771)

Author SHA1 Message Date
Michal Rostecki 43aff57793 maps: Disable miri warnings about integer-to-pointer conversions
`override_syscall` performs integer-to-pointer conversion. This is
considered harmful on the newest Rust nightly which provides
`ptr::from_exposed_addr`, but there is no other way on Rust stable than
doing `as *const T`, which is what miri is unhappy about.

Signed-off-by: Michal Rostecki <vadorovsky@gmail.com>
3 years ago
Dave Tucker 5693fb9941 aya: Rename from_pinned and from_path to from_pin
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
3 years ago
Dave Tucker de6fa98963 aya: Fix review comments from #387
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
3 years ago
Andrew Stoycos 8a9cbf179f Add `from_pinned` and `from_fd` methods
Add `from_pinned` to allow loading BPF maps
from pinned points in the bpffs and
`from_fd` to allow loading BPF maps from
RawFds aquired via some other means eg
a unix socket.

These functions return an
aya::Map which has not been used previously
but will be the future abstraction once
all bpf maps are represented as an enum.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
3 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>
3 years ago
Dave Tucker 03a15b9864 aya: Remove MapError::InvalidPinPath
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
3 years ago
Dave Tucker 34ba2bc048 aya: Use PinError for all pinning errors
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
3 years ago
Dave Tucker c9e70a8758 aya: Fix rlimit warning on for 32bit systems
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
3 years ago
Dave Tucker f976229477 Support BTF Maps
This commit allows for BTF maps in the .maps ELF section to be parsed.
It reads the necessary information from the BTF section of the ELF file.
While the btf_ids of Keys and Values types are stored, they are not (yet)
used.

When creating a BTF map, we pass the btf_key_type_id and
btf_value_type_id.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
3 years ago
Michal Rostecki 3d592d0f29 aya: Raise the RLIMIT_MEMLOCK warning only if failed to create a map
Also, mention that setting the RLIMIT_MEMLOCK to a higher value is an
option.

Signed-off-by: Michal Rostecki <vadorovsky@gmail.com>
3 years ago
Dave Tucker 336faf553e clippy: Fix latest nightly lints
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
3 years ago
Alessandro Decina a301a56316
Merge pull request #328 from drewkett/map-update-no-key
Have bpf_map_update_elem take Option<&K> for key
3 years ago
Dave Tucker b4413322e3 aya: Replace ProgramFd trait with struct
This removes the ProgramFd trait with a struct that wraps a RawFd.
Program::fd() has been implemented as well as fd() for each Program
Type. This allows for a better API than requiring the use of the
ProgramFd trait.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
3 years ago
Andrew Burkett 36edf09254 Have bpf_map_update_elem take Option<&K> for key
bpf_map_update_elem is used in lieu of bpf_map_push_elem to maintain support for kernel version < 4.20. The kernel expects a null pointer for the key for this use case. With this change, if you pass None as key to `bpf_map_update_elem`, it will pass null as key.
3 years ago
Dave Tucker 623579a47f aya: Add Map::fd() function to return a MapFd
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
3 years ago
Kenjiro Nakayama c192817a59 Fix typo, take & to query the value 3 years ago
Kenjiro Nakayama c4262f793d Add support for BPF_MAP_TYPE_BLOOM_FILTER
This patch adds support for `BPF_MAP_TYPE_BLOOM_FILTER`.
3 years ago
Dave Tucker 4a32e7d985 clippy: fix new lints on nightly
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
3 years ago
Dave Tucker d1f2215193 aya: Relocate maps using symbol_index
Since we support multiple maps in the same section, the section_index is
no longer a unique way to identify maps. This commit uses the symbol
index as the identifier, but falls back to section_index for rodata
and bss maps since we don't retrieve the symbol_index during parsing.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
4 years ago
Alessandro Decina ad1636d2e7 aya: perf_buffer: call BytesMut::reserve() internally
This changes PerfBuffer::read_events() to call BytesMut::reserve()
internally, and deprecates PerfBufferError::MoreSpaceNeeded.

This makes for a more ergonomic API, and allows for a more idiomatic
usage of BytesMut. For example consider:

    let mut buffers = vec![BytesMut::with_capacity(N), ...];
    loop {
        let events = oob_cpu_buf.read_events(&mut buffers).unwrap();
        for buf in &mut buffers[..events.read] {
            let sub: Bytes = buf.split_off(n).into();
            process_sub_buf(sub);
        }
        ...
    }

This is a common way to process perf bufs, where a sub buffer is split
off from the original buffer and then processed. In the next iteration
of the loop when it's time to read again, two things can happen:

- if processing of the sub buffer is complete and `sub` has been
dropped, read_events() will call buf.reserve(sample_size) and hit a fast
path in BytesMut that will just restore the original capacity of the
buffer (assuming sample_size <= N).

- if processing of the sub buffer hasn't ended (eg the buffer has been
stored or is being processed in another thread),
buf.reserve(sample_size) will actually allocate the new memory required
to read the sample.

In other words, calling buf.reserve(sample_size) inside read_events()
simplifies doing zero-copy processing of buffers in many cases.
4 years ago
Alessandro Decina 9a642d373f aya: fix lint errors 4 years ago
Nimrod Shneor c6e66d8080 Fix #128: Add support for BPF_MAP_TYPE_LPM_TRIE map 4 years ago
Alessandro Decina 6a91fdf5a7
Merge pull request #157 from dave-tucker/doc-aya
aya: document the public api
4 years ago
Alessandro Decina 4e9bc32a3d aya: maps: rename from_pinned() to open_pinned() 4 years ago
Dave Tucker bca01580e7 aya: document the public api
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
4 years ago
Dave Tucker 65a0b83205 Mark .rodata maps as readonly and freeze on load
This commit marks .rodata maps as BPF_F_RDONLY_PROG when loaded to
prevent a BPF program mutating them.

Initial map data is populated by the loader using the new
`BpfLoader::set_global()` API. The loader will mark
is marked as frozen using bpf_map_freeze to prevent map data
being changed from userspace.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
4 years ago
Thia Wyrod 18970369e2
aya: Remove unnecessary unsafe markers on map iteration.
Map iteration can yield stale keys and values by virtue of sharing a
data structure with BPF programs which can modify it. However, all
accesses remain perfectly safe and will not cause memory corruption or
data races.
4 years ago
Alessandro Decina 07a6016ebb
Merge pull request #120 from eero-thia/thia/dedup
aya: eliminate name duplication in maps and programs.
4 years ago
Thia Wyrod f56dd0a70b
aya: eliminate name duplication in maps and programs.
Map and ProgramData objects had unnecessarily cloned strings for their
names, despite them being just as easily available to external users via
bpf.maps() and bpf.programs().
4 years ago
Thia Wyrod daa7ea6d0d
aya: remove unnecessary usage of &dyn trait in favor of impl trait.
This should improve performance in most situations by eliminating
unnecessary fat pointer indirection.
4 years ago
Thia Wyrod 1584bc47bd
aya: close file descriptors on Map drop. 4 years ago
William Findlay 27d803b634
aya/maps: improve map errors to be more descriptive 4 years ago
Dave Tucker dc4b928ec5 miri: Disable Stacked Borrows and skip some tests
The perf_buffer code fails due to stacked borrows, skip this for now.
munmap isn't supported by miri.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
4 years ago
Dave Tucker 9426f36f79 Implement Pinning For Programs and Maps
This commit adds 2 new methods to aya::sys
- bpf_pin_object
- bpf_get_object

Which allow the pinning and retrieval of programs/maps to bpffs.

It adds a `Program.pin` API, such that a loaded program can be pinned.
For map pinning, the user must ensure the `pinning u32` in the
`bpf_map_def` is set to 1, maps will be pinned using a new builder API.

BpfLoader::new().map_pin_path("/sys/fs/bpf/myapp").load_file("myapp.o")

This will pin all maps whose definition requests pinning to path + name.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
4 years ago
Markus Stange c39dff6025 Add support for PerfEvent programs. 4 years ago
Alessandro Decina 0878c4505a aya: fix clippy warnings 4 years ago
Simone Margaritelli 8311abfdcb
added support for armv7-unknown-linux-gnueabi and armv7-unknown-linux-gnueabihf
* new: added support for armv7-unknown-linux-gnueabi (closes alessandrod/aya/issues/22)

* fix: replaced custom type defintions with proper libc types

* fix: fixed pointless parameter binding

* new: added linker for armv7-unknown-linux-gnueabihf to cargo config
4 years ago
Arnabjyoti Kalita 35f15f70e0
aya: add minimum kernel version for each map and program type (#18) 4 years ago
Rafael Ortiz d8d311738c
aya: support both bpf_map_def layout variants
Libbpf and iproute2 use two slightly different `bpf_map_def` layouts. This change implements support for loading both.

Refs: #10, #14
4 years ago
Sean Young 1196ba1dcc Fix doctest and run them during CI
Signed-off-by: Sean Young <sean@mess.org>
4 years ago
Alessandro Decina be0b7bbd83 Doc fixes 4 years ago
Alessandro Decina 768640dd46 aya: add doc aliases for maps and programs 4 years ago
Alessandro Decina 293e66af65 More docs 4 years ago
Alessandro Decina 11e21e83be More docs 4 years ago
Alessandro Decina 6c7df27bd0 More doc fixes 4 years ago
Alessandro Decina 28158e6028 aya: improve async perf map docs 4 years ago
Alessandro Decina 6ecf7dabf3 aya: tweak PerfEventArray docs 4 years ago
Alessandro Decina 6772595f3e aya: ProgramArray: more doc fixes 4 years ago
Alessandro Decina 4bde0c54bd aya: ProgramArray: tweak docs 4 years ago
Alessandro Decina 31f8d71604 aya: add support for Stack and Queue maps 4 years ago
Alessandro Decina 40b7da6655 aya: add id and pinning fields to bpf_map_def 4 years ago
Alessandro Decina 9e12c9324c aya: fix warnings 5 years ago
Alessandro Decina dad300c88b aya: maps: add SockHash 5 years ago
Alessandro Decina b57cace941 aya: add support BPF_PROG_TYPE_SK_SKB programs and SockMaps 5 years ago
Alessandro Decina b6cd813af5 aya: fix program array key size 5 years ago
Alessandro Decina 0b3e532d7a aya: small doc fixes 5 years ago
Alessandro Decina d9634ae945 aya: add maps::StackTraceMap
Map type for BPF_MAP_TYPE_STACK_TRACE.
5 years ago
Alessandro Decina ad6d0596ab aya: tweak docs 5 years ago
Alessandro Decina f464279740 aya: rename ProgramArray::unset to ProgramArray::clear_index 5 years ago
Alessandro Decina 9ad2a5e72d aya: rename ProgramArray::keys to ProgramArray::indices 5 years ago
Alessandro Decina b0364f76ab aya: maps: add PerCpuArray 5 years ago
Alessandro Decina 74d5f17559 aya: rework IterableMap and ProgramArray
Make MapKeys not use IterableMap. Leave only ProgramArray::get,
ProgramArray::set and ProgramArray::unset exposed as the other syscalls
don't work consistently for program arrays.
5 years ago
Alessandro Decina aa3a30d196 aya: PerCpuKernelMem doesn't need to be public 5 years ago
Alessandro Decina 1746bbf5b8 aya: add aya::maps::Array 5 years ago
Alessandro Decina c3b902137b aya: add aya::maps::array and move ProgramArray under it 5 years ago
Alessandro Decina 6cec8be564 aya: hash_map: add doc aliases for HASH and LRU_HASH 5 years ago
Alessandro Decina 7a989b43b9 aya: per_cpu_hash_map: add support for BPF_MAP_TYPE_LRU_PERCPU_HASH 5 years ago
Alessandro Decina 635dcd44b9 aya: maps: introduce MapError::KeyNotFound
Change get() from -> Result<Option<V>, MapError> to -> Result<V,
MapError> where MapError::KeyNotFound is returned instead of Ok(None) to
signify that the key is not present.
5 years ago
Alessandro Decina fd142e467c aya: rename MapError::NotFound to MapError::MapNotFound 5 years ago
Alessandro Decina 3a5b289163 aya: add PerCpuHashMap 5 years ago
Alessandro Decina d5098c9e57 aya: move hash_map.rs to hash_map/hash_map.rs 5 years ago
Alessandro Decina 6a12a48f03 aya: hash_map: factor out common hash code
This is in preparation of adding new hash map types
5 years ago
Alessandro Decina 7c6ae76975 aya: HashMap: add support for LRU maps 5 years ago
Alessandro Decina 04fde46855 aya: more docs 5 years ago
Alessandro Decina eea27f52f3 aya: tweak docs 5 years ago
Alessandro Decina 5aa9cb12ad aya: rename perf map and add docs
Rename the perf_map module to just perf, and rename PerfMap to
PerfEventArray.
5 years ago
Alessandro Decina d94bfde295 aya: maps: add docs and make the hash_map and program_array modules public 5 years ago
Alessandro Decina ce3f83acb1 aya: add HashMap docs 5 years ago
Alessandro Decina e28da8812e aya: make HashMap::new private 5 years ago
Alessandro Decina 24f7c37158 aya: add ProgramArray docs 5 years ago
Alessandro Decina 3fddc8165c aya: make ProgramArray::new private 5 years ago
Alessandro Decina 6682a5ff39 aya: remove pop()
lookup_and_delete_elem is only supported for QUEUE and STACK maps at the
moment.
5 years ago
Alessandro Decina 563ce46118 aya: maps: group syscall errors into MapError::SyscallError 5 years ago
Alessandro Decina 42e0a659b2 aya: remove TryInto cleverness from map() and map_mut()
Require callers to call try_into() explicitly. It's more characters, but
it's easier to understand/document.

Also introduce MapError::NotFound instead of returning Result<Option<_>>.
5 years ago
Alessandro Decina ed53f7470b aya: maps: add Map::name() and Map::map_type() 5 years ago
Alessandro Decina 29f2d9b2d9 aya: switch to rustified enums 5 years ago
Alessandro Decina a3ab2eff57 Format fixes 5 years ago
Alessandro Decina 3abe9bb859 Fix RawFd import paths 5 years ago
Alessandro Decina ba992a2414 maps: fail new() for high level wrappers if the underlying map hasn't been created 5 years ago
Alessandro Decina fdc4dad5ff maps: add AsyncPerfMap
When the async_tokio or async_std features are enabled, AsyncPerfMap
provides an async version of PerfMap which returns a future from
read_events()
5 years ago
Alessandro Decina 4be0c45305 perf_map: split in sub modules 5 years ago
Alessandro Decina 95a24c6f8b perf_map: implement AsRawFd 5 years ago
Alessandro Decina 5d6fe8bdf4 Add IOError variants to PerfMapError and PerfBufferError 5 years ago
Alessandro Decina b9be2f1a9b Make aya::maps::perf_map public 5 years ago
Alessandro Decina 160e0be6d6 Change the suffix of errors from *Failed to *Error 5 years ago
Alessandro Decina d4e282535b bpf, perf_map: make maps usable from multiple threads
Change PerfMap API so that individual buffers can be read from multiple
threads.

Change the way maps are stored in the `Bpf` struct from RefCell to a
custom RwLock.
5 years ago
Alessandro Decina d7c91efb2d Make online_cpus() util public 5 years ago
Alessandro Decina af8f769b50 Turn the project into a workspace, move code under aya/ 5 years ago