Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Support global variable #276

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions examples/example-probes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@ required-features = ["probes"]
name = "bounded_loop"
path = "src/bounded_loop/main.rs"
required-features = ["probes"]

[[bin]]
name = "global_var"
path = "src/global_var/main.rs"
required-features = ["probes"]
28 changes: 28 additions & 0 deletions examples/example-probes/src/global_var/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![no_std]
#![no_main]
use core::sync::atomic::Ordering;

use redbpf_probes::kprobe::prelude::*;

use example_probes::global_var::{GLOBAL_VAR, GLOBAL_VAR_INCORRECT};

program!(0xFFFFFFFE, "GPL");

#[map]
static mut PERCPU_MAP: PerCpuArray<u64> = PerCpuArray::with_max_entries(1);

#[kprobe]
fn incr_write_count(_regs: Registers) {
unsafe {
GLOBAL_VAR.fetch_add(1, Ordering::Relaxed);
}

unsafe {
GLOBAL_VAR_INCORRECT += 1;
}

unsafe {
let val = PERCPU_MAP.get_mut(0).unwrap();
*val += 1;
}
}
13 changes: 13 additions & 0 deletions examples/example-probes/src/global_var/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use core::sync::atomic::AtomicU64;

use redbpf_macros::global;

/// global variable is shared between multiple cores so proper synchronization
/// should be involved carefully.
#[global]
pub static GLOBAL_VAR: AtomicU64 = AtomicU64::new(0);

/// global variable without any synchronization mechanism. This results in wrong
/// statistics.
#[global]
pub static mut GLOBAL_VAR_INCORRECT: u64 = 0;
1 change: 1 addition & 0 deletions examples/example-probes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
pub mod bindings;

pub mod echo;
pub mod global_var;
pub mod hashmaps;
pub mod mallocstacks;
pub mod p0f;
Expand Down
73 changes: 73 additions & 0 deletions examples/example-userspace/examples/global-var.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/// This example shows that global variable without proper synchronization has
/// incorrect value. On the other hand, the values of per-cpu map and global
/// variable updated by synchronized method are the same. It may take some
/// time to observe the incorrect value if the write-load is light.
///
/// Note that the values of per-cpu map and global variable differ from time to
/// time because of the timing of map-read.
use libc;
use std::process;
use std::time::Duration;
use tokio;
use tokio::select;
use tracing::{error, Level};
use tracing_subscriber::FmtSubscriber;

use redbpf::{load::Loader, Array, PerCpuArray};

use probes::global_var::{GLOBAL_VAR, GLOBAL_VAR_INCORRECT};
#[repr(C)]
#[derive(Debug, Clone)]
struct Data {
var: u64,
var_wo_sync: u64,
}

#[tokio::main(flavor = "current_thread")]
async fn main() {
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::TRACE)
.finish();
tracing::subscriber::set_global_default(subscriber).unwrap();
if unsafe { libc::geteuid() != 0 } {
error!("You must be root to use eBPF!");
process::exit(1);
}

let mut loaded = Loader::load(probe_code()).unwrap();
loaded
.kprobe_mut("incr_write_count")
.expect("kprobe_mut error")
.attach_kprobe("ksys_write", 0)
.expect("error attach_kprobe");

let gvar = loaded
.global::<u64>("GLOBAL_VAR")
.expect("error on accessing gvar");
let gvar_wo_sync = loaded
.global::<u64>("GLOBAL_VAR_INCORRECT")
.expect("error on accessing gvar-wo-sync ");
let percpu_map =
PerCpuArray::<u64>::new(loaded.map("PERCPU_MAP").expect("PERCPU_MAP not found"))
.expect("can not initialize PerCpuArray");
loop {
let pcpu_val = percpu_map.get(0).expect("percpu value");
println!(
"w/ sync, w/o sync, pcpu = {}, {}, {}",
gvar.load().unwrap(),
gvar_wo_sync.load().unwrap(),
pcpu_val.iter().sum::<u64>()
);
select! {
_ = tokio::time::sleep(Duration::from_secs(1)) => {}
_ = tokio::signal::ctrl_c() => { break }
}
}
}

fn probe_code() -> &'static [u8] {
include_bytes!(concat!(
env!("OUT_DIR"),
"/target/bpf/programs/global_var/global_var.elf"
))
}
19 changes: 19 additions & 0 deletions redbpf-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,3 +904,22 @@ fn parse_format_string(fmt: &str) -> Vec<FmtPlaceholder> {
}
res
}

/// Attribute macro for defining global variables that can be accessed by
/// userspace programs
///
/// It is not necessary to call this attribute for all global variables. This
/// attribute is needed to be called only if global variables are accessed by
/// userspace programs. You can omit this attribute if the global variable is
/// used among only BPF programs.
#[proc_macro_attribute]
pub fn global(_attrs: TokenStream, item: TokenStream) -> TokenStream {
let item = parse_macro_input!(item as ItemStatic);
let section_name = format!("globals/{}", item.ident.to_string());
quote! {
#[no_mangle]
#[link_section = #section_name]
#item
}
.into()
}
42 changes: 42 additions & 0 deletions redbpf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,19 @@ impl Module {
pub fn task_iter_mut(&mut self, name: &str) -> Option<&mut TaskIter> {
self.task_iters_mut().find(|p| p.common.name == name)
}

/// Get [`GlobalVariable`](struct.GlobalVariable.html)
///
/// Returned instance is corresponding to a global variable of which name
/// is `name` in BPF programs.
pub fn global<T: Clone>(&self, name: &str) -> Result<GlobalVariable<'_, T>> {
let map = self.map(name).ok_or_else(|| {
error!("map not found: {}", name);
Error::Map
})?;

GlobalVariable::new(map)
}
}

impl<'a> ModuleBuilder<'a> {
Expand Down Expand Up @@ -1483,6 +1496,10 @@ impl<'a> ModuleBuilder<'a> {
symval_to_map_builders.insert(sym.st_value, map_builder);
}
}
(hdr::SHT_PROGBITS, Some("globals"), Some(name)) => {
let map_builder = MapBuilder::with_section_data(name, &content)?;
map_builders.insert(shndx, map_builder);
}
(hdr::SHT_PROGBITS, Some(kind @ "kprobe"), Some(name))
| (hdr::SHT_PROGBITS, Some(kind @ "kretprobe"), Some(name))
| (hdr::SHT_PROGBITS, Some(kind @ "uprobe"), Some(name))
Expand Down Expand Up @@ -2729,6 +2746,31 @@ impl Drop for TaskIter {
}
}

/// A userspace proxy for global variables of BPF programs
///
/// Global variables in BPF programs are implemented by BPF array map.
/// `GlobalVariable` provides a simple wrapper around it.
pub struct GlobalVariable<'a, T: Clone> {
array: Array<'a, T>,
}

impl<'a, T: Clone> GlobalVariable<'a, T> {
fn new(map: &Map) -> Result<GlobalVariable<T>> {
let array = Array::<T>::new(map)?;
Ok(GlobalVariable { array })
}

/// Load value from the global variable of BPF programs
pub fn load(&self) -> Option<T> {
self.array.get(0)
}

/// Store value to the global variable of BPF programs
pub fn store(&self, val: T) -> Result<()> {
self.array.set(0, val)
}
}

#[inline]
fn add_relocation(
rels: &mut Vec<RelocationInfo>,
Expand Down
8 changes: 6 additions & 2 deletions redbpf/src/load/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use std::path::Path;
use crate::load::map_io::PerfMessageStream;
use crate::{cpus, Program};
use crate::{
Error, KProbe, Map, Module, PerfMap, SkLookup, SocketFilter, StreamParser, StreamVerdict,
TaskIter, UProbe, XDP,
Error, GlobalVariable, KProbe, Map, Module, PerfMap, SkLookup, SocketFilter, StreamParser,
StreamVerdict, TaskIter, UProbe, XDP,
};

#[derive(Debug)]
Expand Down Expand Up @@ -188,4 +188,8 @@ impl Loaded {
pub fn task_iter_mut(&mut self, name: &str) -> Option<&mut TaskIter> {
self.module.task_iter_mut(name)
}

pub fn global<T: Clone>(&self, name: &str) -> Result<GlobalVariable<'_, T>, Error> {
self.module.global(name)
}
}