Skip to content

Commit

Permalink
examples/rust: Update libbpf-rs & libbpf-cargo
Browse files Browse the repository at this point in the history
Update the libbpf-rs & libbpf-cargo versions to the most recent
releases and adjust the code accordingly. Set minimum supported Rust
versions to prevent accidental upgrade to lock file version 4.

Closes: #295

Signed-off-by: Daniel Müller <[email protected]>
  • Loading branch information
d-e-s-o authored and anakryiko committed Nov 11, 2024
1 parent db67d46 commit cd287ee
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 303 deletions.
282 changes: 18 additions & 264 deletions examples/rust/Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions examples/rust/profile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ version = "0.1.0"
authors = ["Kuifeng Lee <[email protected]>"]
license = "GPL-2.0 OR BSD-3-Clause"
edition = "2021"
rust-version = "1.71"

[dependencies]
blazesym = { path = "../../../blazesym", features = ["tracing"] }
clap = { version = "4.0", features = ["derive"] }
libbpf-rs = "0.22"
libbpf-rs = "0.24"
libc = "*"
nix = "0.24.1"
tracing = "0.1"
tracing-subscriber = {version = "0.3", features = ["ansi", "env-filter", "fmt"]}

[build-dependencies]
libbpf-cargo = "0.22"
libbpf-cargo = "0.24"
9 changes: 5 additions & 4 deletions examples/rust/profile/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::ffi::OsStr;
use std::path::Path;
use std::path::PathBuf;

Expand All @@ -16,10 +17,10 @@ fn main() {

SkeletonBuilder::new()
.source(SRC)
.clang_args(format!(
"-I{}",
Path::new("../../../vmlinux.h/include").join(arch).display()
))
.clang_args([
OsStr::new("-I"),
Path::new("../../../vmlinux.h/include").join(arch).as_os_str()
])
.build_and_generate(out)
.expect("bpf compilation failed");
println!("cargo:rerun-if-changed={}", SRC);
Expand Down
13 changes: 7 additions & 6 deletions examples/rust/profile/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io;
use std::mem;
use std::mem::MaybeUninit;
use std::time::Duration;

use blazesym::symbolize;
Expand Down Expand Up @@ -71,7 +72,7 @@ fn init_perf_monitor(freq: u64) -> Result<Vec<i32>, libbpf_rs::Error> {

fn attach_perf_event(
pefds: &[i32],
prog: &mut libbpf_rs::Program,
prog: &libbpf_rs::ProgramMut,
) -> Vec<Result<libbpf_rs::Link, libbpf_rs::Error>> {
pefds
.iter()
Expand Down Expand Up @@ -246,16 +247,16 @@ fn main() -> Result<(), libbpf_rs::Error> {
let symbolizer = symbolize::Symbolizer::new();

let skel_builder = ProfileSkelBuilder::default();
let open_skel = skel_builder.open().unwrap();
let mut skel = open_skel.load().unwrap();
let mut open_object = MaybeUninit::uninit();
let open_skel = skel_builder.open(&mut open_object).unwrap();
let skel = open_skel.load().unwrap();

let pefds = init_perf_monitor(freq)?;
let _links = attach_perf_event(&pefds, skel.progs_mut().profile());
let _links = attach_perf_event(&pefds, &skel.progs.profile);

let maps = skel.maps();
let mut builder = libbpf_rs::RingBufferBuilder::new();
builder
.add(maps.events(), move |data| event_handler(&symbolizer, data))
.add(&skel.maps.events, move |data| event_handler(&symbolizer, data))
.unwrap();
let ringbuf = builder.build().unwrap();
while ringbuf.poll(Duration::MAX).is_ok() {}
Expand Down
5 changes: 3 additions & 2 deletions examples/rust/tracecon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ version = "0.1.0"
authors = ["Magnus Kulke <[email protected]>"]
edition = "2021"
license = "GPL-2.0 OR BSD-3-Clause"
rust-version = "1.71"

[dependencies]
anyhow = "1.0"
libbpf-rs = "0.19"
libbpf-rs = "0.24"
libc = "0.2"
structopt = "0.3"
ctrlc = "3.1"
object = "0.25"
plain = "0.2"

[build-dependencies]
libbpf-cargo = "0.13"
libbpf-cargo = "0.24"
11 changes: 7 additions & 4 deletions examples/rust/tracecon/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::ffi::OsStr;
use std::path::Path;
use std::path::PathBuf;

Expand All @@ -16,10 +17,12 @@ fn main() {

SkeletonBuilder::new()
.source(SRC)
.clang_args(format!(
"-I{}",
Path::new("../../../vmlinux.h/include").join(arch).display()
))
.clang_args([
OsStr::new("-I"),
Path::new("../../../vmlinux.h/include")
.join(arch)
.as_os_str(),
])
.build_and_generate(&out)
.expect("bpf compilation failed");
println!("cargo:rerun-if-changed={}", SRC);
Expand Down
30 changes: 17 additions & 13 deletions examples/rust/tracecon/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use anyhow::{anyhow, bail, Context, Result};
use core::time::Duration;
use std::mem::MaybeUninit;
use libbpf_rs::PerfBufferBuilder;
use libbpf_rs::skel::SkelBuilder as _;
use libbpf_rs::skel::OpenSkel as _;
use object::Object;
use object::ObjectSymbol;
use plain::Plain;
Expand All @@ -16,7 +19,7 @@ mod tracecon {
}
use tracecon::*;

type Event = tracecon_bss_types::event;
type Event = tracecon::types::event;
unsafe impl Plain for Event {}

#[derive(Debug, StructOpt)]
Expand Down Expand Up @@ -84,34 +87,35 @@ fn main() -> Result<()> {
}

bump_memlock_rlimit()?;
let mut open_skel = skel_builder.open()?;
let mut open_object = MaybeUninit::uninit();
let open_skel = skel_builder.open(&mut open_object)?;
if let Some(pid) = opts.pid {
open_skel.rodata().target_pid = pid;
open_skel.maps.rodata_data.target_pid = pid;
}
let mut skel = open_skel.load()?;
let skel = open_skel.load()?;
let address = get_symbol_address(&opts.glibc, "getaddrinfo")?;

let _uprobe =
skel.progs_mut()
.getaddrinfo_enter()
skel.progs
.getaddrinfo_enter
.attach_uprobe(false, -1, &opts.glibc, address)?;

let _uretprobe =
skel.progs_mut()
.getaddrinfo_exit()
skel.progs
.getaddrinfo_exit
.attach_uprobe(true, -1, &opts.glibc, address)?;

let _kprobe = skel
.progs_mut()
.tcp_v4_connect_enter()
.progs
.tcp_v4_connect_enter
.attach_kprobe(false, "tcp_v4_connect")?;

let _kretprobe = skel
.progs_mut()
.tcp_v4_connect_exit()
.progs
.tcp_v4_connect_exit
.attach_kprobe(true, "tcp_v4_connect")?;

let perf = PerfBufferBuilder::new(skel.maps_mut().events())
let perf = PerfBufferBuilder::new(&skel.maps.events)
.sample_cb(handle_event)
.build()?;

Expand Down
5 changes: 3 additions & 2 deletions examples/rust/xdp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ name = "xdp"
version = "0.1.0"
authors = ["Hengqi Chen <[email protected]>"]
edition = "2021"
rust-version = "1.71"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0"
ctrlc = { version = "3.0", features = ["termination"] }
libc = "0.2"
libbpf-rs = "0.19"
libbpf-rs = "0.24"
structopt = "0.3"

[build-dependencies]
libbpf-cargo = "0.13"
libbpf-cargo = "0.24"
11 changes: 7 additions & 4 deletions examples/rust/xdp/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::ffi::OsStr;
use std::path::Path;
use std::path::PathBuf;

Expand All @@ -16,10 +17,12 @@ fn main() {

SkeletonBuilder::new()
.source(SRC)
.clang_args(format!(
"-I{}",
Path::new("../../../vmlinux.h/include").join(arch).display()
))
.clang_args([
OsStr::new("-I"),
Path::new("../../../vmlinux.h/include")
.join(arch)
.as_os_str(),
])
.build_and_generate(out)
.unwrap();
println!("cargo:rerun-if-changed={}", SRC);
Expand Down
8 changes: 6 additions & 2 deletions examples/rust/xdp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::mem::MaybeUninit;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::{thread, time};

use anyhow::{bail, Result};
use libbpf_rs::skel::OpenSkel as _;
use libbpf_rs::skel::SkelBuilder as _;
use structopt::StructOpt;

mod xdppass {
Expand Down Expand Up @@ -36,9 +39,10 @@ fn main() -> Result<()> {
bump_memlock_rlimit()?;

let skel_builder = XdppassSkelBuilder::default();
let open_skel = skel_builder.open()?;
let mut open_object = MaybeUninit::uninit();
let open_skel = skel_builder.open(&mut open_object)?;
let mut skel = open_skel.load()?;
let link = skel.progs_mut().xdp_pass().attach_xdp(opts.ifindex)?;
let link = skel.progs.xdp_pass.attach_xdp(opts.ifindex)?;
skel.links = XdppassLinks {
xdp_pass: Some(link),
};
Expand Down

0 comments on commit cd287ee

Please sign in to comment.