From 84e0a76a3ce61ba508f8638a559913b653d3cc66 Mon Sep 17 00:00:00 2001 From: arloor Date: Tue, 15 Oct 2024 00:30:56 +0800 Subject: [PATCH] Refactor Cargo.toml and linux_monitor.rs to use the 'bpf' feature for the rust_http_proxy task --- rust_http_proxy/Cargo.toml | 4 ++-- rust_http_proxy/src/linux_monitor.rs | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/rust_http_proxy/Cargo.toml b/rust_http_proxy/Cargo.toml index a68efb4..e699d51 100644 --- a/rust_http_proxy/Cargo.toml +++ b/rust_http_proxy/Cargo.toml @@ -50,8 +50,8 @@ serde_yaml = "0.9" serde = { version = "1.0", features = ["derive"] } [target.'cfg(target_os = "linux")'.dependencies] -socket_filter = { version = "0.1", optional = true } -cgroup_traffic = { version = "0.1", optional = true } +socket_filter = { git = "https://github.com/arloor/bpf_rs_hub", optional = true } +cgroup_traffic = { git = "https://github.com/arloor/bpf_rs_hub", optional = true } libbpf-sys = { version = "1", optional = true } pnet = { version = "0.35", optional = true } diff --git a/rust_http_proxy/src/linux_monitor.rs b/rust_http_proxy/src/linux_monitor.rs index 61b83d0..aa2f36c 100644 --- a/rust_http_proxy/src/linux_monitor.rs +++ b/rust_http_proxy/src/linux_monitor.rs @@ -46,14 +46,24 @@ const INTERVAL_SECONDS: u64 = 5; const SIZE: usize = TOTAL_SECONDS as usize / INTERVAL_SECONDS as usize; impl NetMonitor { pub fn new() -> Result { + #[cfg(all(target_os = "linux", feature = "bpf"))] + use std::mem::MaybeUninit; + #[cfg(all(target_os = "linux", feature = "bpf"))] + let (cgroup_traffic_counter, links) = cgroup_traffic::init_cgroup_skb_monitor( + Box::leak(Box::new(MaybeUninit::uninit())), + cgroup_traffic::SELF, + )?; + #[cfg(all(target_os = "linux", feature = "bpf"))] + Box::leak(Box::new(links)); Ok(NetMonitor { buffer: Arc::new(RwLock::new(VecDeque::::new())), #[cfg(all(target_os = "linux", feature = "bpf"))] - cgroup_transmit_counter: Arc::new(cgroup_traffic::init_cgroup_skb_monitor( - cgroup_traffic::SELF, - )?), + cgroup_transmit_counter: Arc::new(cgroup_traffic_counter), #[cfg(all(target_os = "linux", feature = "bpf"))] - transmit_counter: Arc::new(socket_filter::TransmitCounter::new(IGNORED_INTERFACES)?), + transmit_counter: Arc::new(socket_filter::TransmitCounter::new( + Box::leak(Box::new(MaybeUninit::uninit())), + IGNORED_INTERFACES, + )?), }) }