-
Notifications
You must be signed in to change notification settings - Fork 325
feat(aya): Add task storage map type (in the user-space) #1161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vadorovsky
wants to merge
1
commit into
aya-rs:main
Choose a base branch
from
vadorovsky:task-storage-user-space
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
//! Task storage. | ||
use std::{ | ||
borrow::Borrow, | ||
marker::PhantomData, | ||
os::fd::{AsFd as _, AsRawFd as _}, | ||
}; | ||
|
||
use crate::{ | ||
Pod, | ||
maps::{MapData, MapError, check_kv_size}, | ||
sys::{PidFd, SyscallError, bpf_map_lookup_elem}, | ||
}; | ||
|
||
/// Task storage is a type of map which uses `task_struct` kernel type as a | ||
/// key. When the task (process) stops, the corresponding entry is | ||
/// automatically removed. | ||
/// | ||
/// # Minimum kernel version | ||
/// | ||
/// The minimum kernel version required to use this feature is 5.12. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// # let mut ebpf = aya::Ebpf::load(&[])?; | ||
/// use aya::maps::TaskStorage; | ||
/// | ||
/// let mut task_storage: TaskStorage<_, u32> = TaskStorage::try_from(ebpf.map_mut("TASK_STORAGE").unwrap())?; | ||
/// | ||
/// let pid = 0; | ||
/// let value = task_storage.get(&pid, 0)?; | ||
/// # Ok::<(), aya::EbpfError>(()) | ||
/// ``` | ||
#[doc(alias = "BPF_MAP_TYPE_TASK_STORAGE")] | ||
#[derive(Debug)] | ||
pub struct TaskStorage<T, V> { | ||
pub(crate) inner: T, | ||
_v: PhantomData<V>, | ||
} | ||
|
||
impl<T: Borrow<MapData>, V: Pod> TaskStorage<T, V> { | ||
pub(crate) fn new(map: T) -> Result<Self, MapError> { | ||
let data = map.borrow(); | ||
check_kv_size::<u32, V>(data)?; | ||
Ok(Self { | ||
inner: map, | ||
_v: PhantomData, | ||
}) | ||
} | ||
|
||
/// Returns the value stored for the given `pid`. | ||
pub fn get(&self, pid: &u32, flags: u64) -> Result<V, MapError> { | ||
let pidfd = PidFd::open(*pid, 0).map_err(|(_, io_error)| SyscallError { | ||
call: "pidfd_open", | ||
io_error, | ||
})?; | ||
let map_fd = self.inner.borrow().fd().as_fd(); | ||
let value = bpf_map_lookup_elem(map_fd, &pidfd.as_raw_fd(), flags).map_err(|io_error| { | ||
SyscallError { | ||
call: "bpf_map_lookup_elem", | ||
io_error, | ||
} | ||
})?; | ||
value.ok_or(MapError::KeyNotFound) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::io; | ||
|
||
use assert_matches::assert_matches; | ||
use aya_obj::generated::bpf_map_type::BPF_MAP_TYPE_TASK_STORAGE; | ||
use libc::EFAULT; | ||
|
||
use super::*; | ||
use crate::{ | ||
maps::{ | ||
Map, | ||
test_utils::{self, new_map}, | ||
}, | ||
sys::{SysResult, Syscall, override_syscall}, | ||
}; | ||
|
||
fn new_obj_map() -> aya_obj::Map { | ||
test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_TASK_STORAGE) | ||
} | ||
|
||
fn sys_error(value: i32) -> SysResult { | ||
Err((-1, io::Error::from_raw_os_error(value))) | ||
} | ||
|
||
#[test] | ||
fn test_wrong_value_size() { | ||
let map = new_map(new_obj_map()); | ||
let map = Map::TaskStorage(map); | ||
assert_matches!( | ||
TaskStorage::<_, u16>::try_from(&map), | ||
Err(MapError::InvalidValueSize { | ||
size: 2, | ||
expected: 4 | ||
}) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_try_from_wrong_map() { | ||
let map = new_map(new_obj_map()); | ||
let map = Map::Array(map); | ||
assert_matches!( | ||
TaskStorage::<_, u32>::try_from(&map), | ||
Err(MapError::InvalidMapType { .. }) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_new_ok() { | ||
let map = new_map(new_obj_map()); | ||
assert!(TaskStorage::<_, u32>::new(&map).is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_try_from_ok() { | ||
let map = new_map(new_obj_map()); | ||
let map = Map::TaskStorage(map); | ||
assert!(TaskStorage::<_, u32>::try_from(&map).is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_get_pidfd_syscall_error() { | ||
let mut map = new_map(new_obj_map()); | ||
let map = TaskStorage::<_, u32>::new(&mut map).unwrap(); | ||
|
||
override_syscall(|call| match call { | ||
Syscall::Ebpf { .. } => Ok(1), | ||
Syscall::PidfdOpen { .. } => sys_error(EFAULT), | ||
_ => sys_error(EFAULT), | ||
}); | ||
|
||
assert_matches!( | ||
map.get(&1, 0), Err(MapError::SyscallError( | ||
SyscallError { | ||
call: "pidfd_open", | ||
io_error | ||
} | ||
)) | ||
if io_error.raw_os_error() == Some(EFAULT) | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// clang-format off | ||
#include <vmlinux.h> | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_core_read.h> | ||
#include <bpf/bpf_tracing.h> | ||
// clang-format on | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_TASK_STORAGE); | ||
__uint(map_flags, BPF_F_NO_PREALLOC); | ||
__type(key, int); | ||
__type(value, __u32); | ||
} task_storage SEC(".maps"); | ||
|
||
void bpf_rcu_read_lock(void) __ksym; | ||
void bpf_rcu_read_unlock(void) __ksym; | ||
|
||
SEC("tp_btf/sys_enter") | ||
int BPF_PROG(sys_enter, struct pt_regs *regs, long id) { | ||
__u32 value = 1; | ||
struct task_struct *task = bpf_get_current_task_btf(); | ||
// This test is triggered by a Rust test, running in a thread. A current task | ||
// (the one returned by `bpf_get_current_task()`) represents that thread. If | ||
// we create a task storage entry for that task, our user-space test will not | ||
// be able to retrieve it as pidfd. | ||
// To make retrieval of the map element by pidfd possible, we need to use the | ||
// `group_leader` (a `struct task_struct*` instance representing the process) | ||
// as the key. | ||
bpf_rcu_read_lock(); | ||
struct task_struct *group_leader = BPF_CORE_READ(task, group_leader); | ||
bpf_task_storage_get(&task_storage, group_leader, &value, | ||
BPF_LOCAL_STORAGE_GET_F_CREATE); | ||
bpf_rcu_read_unlock(); | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ mod relocations; | |
mod ring_buf; | ||
mod smoke; | ||
mod strncmp; | ||
mod task_storage; | ||
mod tcx; | ||
mod uprobe_cookie; | ||
mod xdp; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coming back to earlier comment - if you're expecting c_int to always be a 4 byte value, you may as well use
u32
.