Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3e99962
rex: modify task_struct's get_comm
MinhPhan8803 Mar 14, 2025
cc95b6a
rex: add perf_array_map helper symbols
MinhPhan8803 Mar 19, 2025
7815e04
rex-macros: add macro for uprobe
MinhPhan8803 Mar 19, 2025
e3d3321
rex: add PerfEventArray and StreamableProgram trait
MinhPhan8803 Mar 19, 2025
08922c6
samples: initial code for harpoon port
MinhPhan8803 Mar 19, 2025
db4b053
Merge branch 'main' into harpoon_port
MinhPhan8803 Mar 19, 2025
9fa66c7
rex: fix event output symbols in stub
MinhPhan8803 Mar 19, 2025
e91f541
rex: implement StreamableProgram for tracepoint
MinhPhan8803 Mar 22, 2025
a659c1e
Merge branch 'main' into harpoon_port
MinhPhan8803 Mar 22, 2025
3fe9cb1
rex/src/utils.rs: update PerfEventMaskedCPU method names
MinhPhan8803 Mar 22, 2025
abdc31d
rex: fix type issues in StreamableProgram implementation
MinhPhan8803 Mar 22, 2025
046d284
Merge branch 'main' into harpoon_port
MinhPhan8803 Mar 22, 2025
182bf51
rex: cast c_char to u8 in TaskStruct::get_comm()
MinhPhan8803 Mar 23, 2025
70e86aa
Merge branch 'main' into harpoon_port
MinhPhan8803 Mar 30, 2025
30f8795
rex: cleanup: hide BPF_F_CURRENT_CPU, remove ProgramContextPair, add …
MinhPhan8803 Mar 30, 2025
6d23109
rex: add RawSyscallsEnter tracepoint variant, complete harpoon kernel…
MinhPhan8803 Apr 4, 2025
d9321a4
Merge branch 'main' into harpoon_port
MinhPhan8803 Apr 13, 2025
b6bcc6c
rex: change stub to ffi
MinhPhan8803 Apr 14, 2025
bf10a1a
Merge branch 'main' into harpoon_port
MinhPhan8803 Apr 24, 2025
6590fae
rex: clean up tracepoint and map impls
MinhPhan8803 Apr 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions rex/src/map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::bindings::uapi::linux::bpf::BPF_F_INDEX_MASK;
use crate::utils::{to_result, NoRef, Result, StreamableProgram};
use crate::utils::{
to_result, NoRef, PerfEventMaskedCPU, Result, StreamableProgram,
};
use crate::CURRENT_CPU;
use crate::{
base_helper::{
Expand Down Expand Up @@ -133,21 +134,14 @@ impl<'a, V> RexPerfEventArray<V>
where
V: Copy + NoRef,
{
pub fn output_on_cur_cpu<P: StreamableProgram>(
&'static self,
program: &P,
data: &V,
) {
context.output_event(self, data, CURRENT_CPU);
}

pub unsafe fn output_on_any_cpu<P: StreamableProgram>(
pub fn output<P: StreamableProgram>(
&'static self,
program: &P,
ctx: &P::Context,
data: &V,
cpu: u64,
) {
context.output_event(self, data, cpu & BPF_F_INDEX_MASK);
cpu: PerfEventMaskedCPU,
) -> Result {
program.output_event(ctx, self, data, cpu)
}
}

Expand Down
4 changes: 2 additions & 2 deletions rex/src/stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ unsafe extern "C" {
size: u32,
) -> i64;

/// `long bpf_perf_event_output(void *ctx, struct bpf_map *map, u64
/// `long bpf_perf_event_output_tp(void *tp_buff, struct bpf_map *map, u64
/// flags, void *data, u64 size)`
pub(crate) fn bpf_perf_event_output_tp(
tp_buff: *const (),
map: *mut (),
flags: u64,
data: *mut (),
data: *const (),
size: u64,
) -> i64;

Expand Down
52 changes: 51 additions & 1 deletion rex/src/tracepoint/tp_impl.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use core::ffi::c_void;

use crate::bindings::uapi::linux::bpf::{
bpf_map_type, BPF_PROG_TYPE_TRACEPOINT,
};
use crate::map::*;
use crate::prog_type::rex_prog;
use crate::stub;
use crate::task_struct::TaskStruct;
use crate::utils::{PerfEventMaskedCPU, StreamableProgram};
use crate::Result;
use crate::{map::*, CURRENT_CPU};

use super::binding::*;

Expand Down Expand Up @@ -75,3 +79,49 @@ impl rex_prog for tracepoint {
((self.prog)(self, newctx)).unwrap_or_else(|e| e) as u32
}
}

impl StreamableProgram for tracepoint {
type Context = tp_ctx;
fn output_event<T>(
&self,
ctx: &Self::Context,
map: &'static RexPerfEventArray<T>,
data: &T,
cpu: PerfEventMaskedCPU,
) -> Result {
let map_kptr = unsafe { core::ptr::read_volatile(&map.kptr) };
let masked_cpu = match PerfEventMaskedCPU {
PerfEventMaskedCPU::CurrentCPU => CURRENT_CPU,
PerfEventMaskedCPU::AnyCPU(cpu) => cpu,
};
termination_check!(unsafe {
to_result!(match ctx {
tp_ctx::Void => stub::bpf_perf_event_output_tp(
c_void,
map_kptr,
masked_cpu,
data as *const T as *const (),
mem::size_of::<V>() as u64,
),
tp_ctx::SyscallsEnterOpen(args) => {
stub::bpf_perf_event_output_tp(
args as *const SyscallsEnterOpenArgs as *const (),
map_kptr,
masked_cpu,
data as *const T as *const (),
mem::size_of::<V>() as u64,
)
}
tp_ctx::SyscallsExitOpen(args) => {
stub::bpf_perf_event_output_tp(
args as *const SyscallsExitOpenArgs as *const (),
map_kptr,
masked_cpu,
data as *const T as *const (),
mem::size_of::<V>() as u64,
)
}
})
})
}
}
31 changes: 28 additions & 3 deletions rex/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use crate::bindings::uapi::linux::bpf::BPF_F_INDEX_MASK;
use crate::tracepoint::{tp_ctx, tracepoint};
use crate::Result;
use core::ffi::{c_int, c_uchar};
use core::mem;
use core::ops::{Deref, DerefMut, Drop};
Expand Down Expand Up @@ -259,13 +262,35 @@ where
}
}

/// Design decision: programs that can stream data through a
pub enum ProgramContextPair {
Tracepoint(),
}

/// programs that can stream data through a
/// RexPerfEventArray will implement this trait
pub trait StreamableProgram {
type Context: ?Sized;
pub fn output_event<T>(
&self,
ctx: &Self::Context,
map: &'static RexPerfEventArray<T>,
data: &T,
idx: u64,
);
cpu: PerfEventMaskedCPU,
) -> Result;
}

/// newtype for a cpu for perf event output
/// since the cpu must be masked with BPF_F_INDEX_MASK
pub enum PerfEventMaskedCPU {
CurrentCPU,
AnyCPU(u64),
}

impl PerfEventMaskedCPU {
pub fn new_current_cpu() -> Self {
PerfEventCPU::CurrentCPU
}
pub fn new_any_cpu(cpu: u64) -> Self {
PerfEventCPU::AnyCPU(cpu & BPF_F_INDEX_MASK)
}
}