From 301be351b074639f742981beed10ec0a91e8f33e Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Fri, 16 Jan 2026 10:54:24 +0100 Subject: [PATCH 01/24] [#158]: moved map_handlers module from identity crate to common crate --- core/Cargo.lock | 6 +- core/common/Cargo.toml | 6 ++ core/common/src/lib.rs | 3 +- .../identity => common}/src/map_handlers.rs | 96 +++++++++++-------- core/src/components/identity/src/lib.rs | 3 +- 5 files changed, 68 insertions(+), 46 deletions(-) rename core/{src/components/identity => common}/src/map_handlers.rs (56%) diff --git a/core/Cargo.lock b/core/Cargo.lock index 506d5dc4..e980659b 100644 --- a/core/Cargo.lock +++ b/core/Cargo.lock @@ -406,6 +406,9 @@ name = "cortexbrain-common" version = "0.1.0" dependencies = [ "anyhow", + "aya", + "k8s-openapi", + "kube", "tracing", "tracing-subscriber", ] @@ -453,10 +456,9 @@ dependencies = [ "bytemuck", "bytemuck_derive", "bytes", - "cortexbrain-common 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cortexbrain-common 0.1.0", "k8s-openapi", "kube", - "libc", "nix", "tokio", "tracing", diff --git a/core/common/Cargo.toml b/core/common/Cargo.toml index 70545781..ac87f689 100644 --- a/core/common/Cargo.toml +++ b/core/common/Cargo.toml @@ -13,3 +13,9 @@ repository = "https://github.com/CortexFlow/CortexBrain" tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } anyhow = "1.0" +kube = { version = "2.0.1", features = ["client"] } +k8s-openapi = { version = "0.26.0", features = ["v1_34"] } +aya = "0.13.1" + +[features] +map-handlers = [] diff --git a/core/common/src/lib.rs b/core/common/src/lib.rs index f8fadc66..2f8e5635 100644 --- a/core/common/src/lib.rs +++ b/core/common/src/lib.rs @@ -1,3 +1,4 @@ pub mod constants; +pub mod formatters; pub mod logger; -pub mod formatters; \ No newline at end of file +pub mod map_handlers; diff --git a/core/src/components/identity/src/map_handlers.rs b/core/common/src/map_handlers.rs similarity index 56% rename from core/src/components/identity/src/map_handlers.rs rename to core/common/src/map_handlers.rs index a225a470..43330fab 100644 --- a/core/src/components/identity/src/map_handlers.rs +++ b/core/common/src/map_handlers.rs @@ -13,39 +13,49 @@ use std::sync::Mutex; use tracing::warn; use tracing::{error, info}; -pub fn init_bpf_maps(bpf: Arc>) -> Result<(Map, Map, Map, Map), anyhow::Error> { - // this function init the bpfs maps used in the main program - /* - index 0: events_map - index 1: veth_map - index 2: blocklist map - */ - let mut bpf_new = bpf.lock().unwrap(); +// docs +// +// this function init the bpfs maps used in the main program +// +// index 0: events_map +// index 1: veth_map +// index 2: blocklist map +// index 3: tcp_registry map +// - let events_map = bpf_new - .take_map("EventsMap") - .ok_or_else(|| anyhow::anyhow!("EventsMap map not found"))?; - - let veth_map = bpf_new - .take_map("veth_identity_map") - .ok_or_else(|| anyhow::anyhow!("veth_identity_map map not found"))?; - - let blocklist_map = bpf_new - .take_map("Blocklist") - .ok_or_else(|| anyhow::anyhow!("Blocklist map not found"))?; +#[cfg(feature = "map-handlers")] +pub struct BpfMapsData { + pub bpf_obj_names: Vec, + pub bpf_obj_map: Vec, +} - let tcp_registry_map = bpf_new - .take_map("TcpPacketRegistry") - .ok_or_else(|| anyhow::anyhow!("TcpPacketRegistry map not found"))?; +#[cfg(feature = "map-handlers")] +pub fn init_bpf_maps( + bpf: Arc>, + map_names: Vec, +) -> Result { + let mut bpf_new = bpf.lock().expect("Cannot get value from lock"); + let mut maps = Vec::new(); // stores bpf_maps_objects - Ok((events_map, veth_map, blocklist_map, tcp_registry_map)) + for name in &map_names { + let bpf_map_init = bpf_new + .take_map(&name) + .ok_or_else(|| anyhow::anyhow!("{} map not found", &name))?; + maps.push(bpf_map_init); + } + Ok(BpfMapsData { + bpf_obj_names: map_names.clone(), + bpf_obj_map: maps, + }) } //TODO: save bpf maps path in the cli metadata + //takes an array of bpf maps and pin them to persiste session data -//TODO: change maps type with a Vec instead of (Map,Map). This method is only for fast development and it's not optimized -//TODO: add bpf mounts during cli installation -pub fn map_pinner(maps: &(Map, Map, Map, Map), path: &PathBuf) -> Result<(), Error> { +// FIXME: is this ok that we are returning a BpfMapsData? + +#[cfg(feature = "map-handlers")] +pub fn map_pinner(maps: BpfMapsData, path: &PathBuf) -> Result, Error> { if !path.exists() { info!("Pin path {:?} does not exist. Creating it...", path); std::fs::create_dir_all(&path)?; @@ -56,28 +66,32 @@ pub fn map_pinner(maps: &(Map, Map, Map, Map), path: &PathBuf) -> Result<(), Err } } - let configs = [ - (&maps.0, "events_map"), - (&maps.1, "veth_map"), - (&maps.2, "blocklist_map"), - (&maps.3, "tcp_packet_registry"), - ]; - - for (name, paths) in configs { - let map_path = path.join(paths); + let mut owned_maps = Vec::new(); // aya::Maps does not implement the clone trait i need to create a raw copy of the vec map + // an iterator that iterates two iterators simultaneously + for (map_obj, name) in maps + .bpf_obj_map + .into_iter() + .zip(maps.bpf_obj_names.into_iter()) + { + let map_path = path.join(&name); if map_path.exists() { - warn!("Path {} already exists", paths); - warn!("Removing path {}", paths); - let _ = std::fs::remove_file(&map_path); + warn!("Path {} already exists", name); + warn!("Removing path {}", name); + std::fs::remove_file(&map_path)?; } info!("Trying to pin map {:?} in map path: {:?}", name, &map_path); - name.pin(&map_path)?; + map_obj.pin(&map_path)?; + owned_maps.push(map_obj); } - Ok(()) + Ok(owned_maps) } + +#[cfg(feature = "map-handlers")] pub async fn populate_blocklist(map: &mut Map) -> Result<(), Error> { - let client = Client::try_default().await.unwrap(); + let client = Client::try_default() + .await + .expect("Cannot connect to Kubernetes Client"); let namespace = "cortexflow"; let configmap = "cortexbrain-client-config"; diff --git a/core/src/components/identity/src/lib.rs b/core/src/components/identity/src/lib.rs index e3bb59e0..54134144 100644 --- a/core/src/components/identity/src/lib.rs +++ b/core/src/components/identity/src/lib.rs @@ -1,4 +1,3 @@ pub mod helpers; pub mod structs; -pub mod enums; -pub mod map_handlers; \ No newline at end of file +pub mod enums; \ No newline at end of file From 1385bcfcb09dbf94a5994b804b03b064a27a425a Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Fri, 16 Jan 2026 10:54:52 +0100 Subject: [PATCH 02/24] [#158]: simplified identity logic. removed duplicated code and functions --- core/src/components/identity/Cargo.toml | 8 +- core/src/components/identity/src/helpers.rs | 12 +- core/src/components/identity/src/main.rs | 195 +++++++++----------- core/src/components/identity/src/mod.rs | 3 +- core/src/components/identity/src/structs.rs | 26 +-- 5 files changed, 112 insertions(+), 132 deletions(-) diff --git a/core/src/components/identity/Cargo.toml b/core/src/components/identity/Cargo.toml index 08d753eb..3146991c 100644 --- a/core/src/components/identity/Cargo.toml +++ b/core/src/components/identity/Cargo.toml @@ -10,11 +10,10 @@ homepage = "https://docs.cortexflow.org" repository = "https://github.com/CortexFlow/CortexBrain" [features] -default = ["map-handlers", "struct", "enums"] -map-handlers = [] +default = ["struct", "enums"] struct = [] enums = [] -experimental = ["map-handlers", "struct", "enums"] +experimental = ["struct", "enums"] [dependencies] @@ -31,10 +30,9 @@ tokio = { version = "1.48.0", features = [ anyhow = "1.0" tracing = "0.1.41" tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } -libc = "0.2.172" bytemuck = { version = "1.23.0", features = ["derive"] } bytemuck_derive = "1.10.1" -cortexbrain-common = "0.1.0" +cortexbrain-common = { path = "../../../common/", features = ["map-handlers"] } nix = { version = "0.30.1", features = ["net"] } kube = { version = "2.0.1", features = ["client"] } k8s-openapi = { version = "0.26.0", features = ["v1_34"] } diff --git a/core/src/components/identity/src/helpers.rs b/core/src/components/identity/src/helpers.rs index 7855edc4..05b96032 100644 --- a/core/src/components/identity/src/helpers.rs +++ b/core/src/components/identity/src/helpers.rs @@ -49,10 +49,10 @@ impl TryFrom for IpProtocols { /* helper functions to read and log net events in the container */ pub async fn display_events>( mut perf_buffers: Vec>, - running: Arc, + //running: Arc, mut buffers: Vec, ) { - while running.load(Ordering::SeqCst) { + while true { for buf in perf_buffers.iter_mut() { match buf.read_events(&mut buffers) { std::result::Result::Ok(events) => { @@ -105,11 +105,11 @@ pub fn reverse_be_addr(addr: u32) -> Ipv4Addr { pub async fn display_veth_events>( bpf: Arc>, mut perf_buffers: Vec>, - running: Arc, + //running: Arc, mut buffers: Vec, mut link_ids: Arc>>, ) { - while running.load(Ordering::SeqCst) { + while true { for buf in perf_buffers.iter_mut() { match buf.read_events(&mut buffers) { std::result::Result::Ok(events) => { @@ -265,10 +265,10 @@ async fn attach_detach_veth( /* helper functions to display events from the TcpPacketRegistry structure */ pub async fn display_tcp_registry_events>( mut perf_buffers: Vec>, - running: Arc, + //running: Arc, mut buffers: Vec, ) { - while running.load(Ordering::SeqCst) { + while true { for buf in perf_buffers.iter_mut() { match buf.read_events(&mut buffers) { std::result::Result::Ok(events) => { diff --git a/core/src/components/identity/src/main.rs b/core/src/components/identity/src/main.rs index 56887158..9dd6ce94 100644 --- a/core/src/components/identity/src/main.rs +++ b/core/src/components/identity/src/main.rs @@ -7,46 +7,36 @@ * 4. [Experimental]: cgroup scanner * */ -#![allow(warnings)] mod enums; mod helpers; -mod map_handlers; mod structs; +use crate::helpers::{ + display_events, display_tcp_registry_events, display_veth_events, get_veth_channels, +}; use aya::{ Ebpf, - maps::{ - Map, MapData, - perf::{PerfEventArray, PerfEventArrayBuffer}, - }, + maps::{Map, perf::PerfEventArray}, programs::{KProbe, SchedClassifier, TcAttachType, tc::SchedClassifierLinkId}, util::online_cpus, }; -use crate::helpers::{ - display_events, display_tcp_registry_events, display_veth_events, get_veth_channels, -}; - #[cfg(feature = "experimental")] use crate::helpers::scan_cgroup_cronjob; -use crate::map_handlers::{init_bpf_maps, map_pinner, populate_blocklist}; - use bytes::BytesMut; +use cortexbrain_common::map_handlers::{init_bpf_maps, map_pinner, populate_blocklist}; use std::{ convert::TryInto, path::Path, - sync::{ - Arc, Mutex, - atomic::{AtomicBool, Ordering}, - }, + sync::{Arc, Mutex}, }; use anyhow::{Context, Ok}; use cortexbrain_common::{constants, logger}; use tokio::{fs, signal}; -use tracing::{error, info}; +use tracing::{debug, error, info}; use std::collections::HashMap; @@ -72,14 +62,19 @@ async fn main() -> Result<(), anyhow::Error> { let bpf = Arc::new(Mutex::new(Ebpf::load(&data)?)); let bpf_map_save_path = std::env::var(constants::PIN_MAP_PATH) .context("PIN_MAP_PATH environment variable required")?; - - match init_bpf_maps(bpf.clone()) { - std::result::Result::Ok(mut bpf_maps) => { + let data = vec![ + "EventsMap".to_string(), + "veth_identity_map".to_string(), + //"Blocklist".to_string(), + "TcpPacketRegistry".to_string(), + ]; + match init_bpf_maps(bpf.clone(), data) { + std::result::Result::Ok(bpf_maps) => { info!("Successfully loaded bpf maps"); let pin_path = std::path::PathBuf::from(&bpf_map_save_path); info!("About to call map_pinner with path: {:?}", pin_path); - match map_pinner(&bpf_maps, &pin_path) { - std::result::Result::Ok(_) => { + match map_pinner(bpf_maps, &pin_path) { + std::result::Result::Ok(maps) => { info!("maps pinned successfully"); //load veth_trace program ref veth_trace.rs { @@ -90,9 +85,9 @@ async fn main() -> Result<(), anyhow::Error> { info!("Found interfaces: {:?}", interfaces); - { - populate_blocklist(&mut bpf_maps.2).await; - } + //{ FIXME: paused for testing the other features + // populate_blocklist(&mut maps.2).await?; + //} { init_tc_classifier(bpf.clone(), interfaces, link_ids.clone()).await.context( @@ -105,9 +100,11 @@ async fn main() -> Result<(), anyhow::Error> { )?; } - event_listener(bpf_maps, link_ids.clone(), bpf.clone()) + event_listener(maps, link_ids.clone(), bpf.clone()) .await - .context("Error initializing event_listener")?; + .map_err(|e| { + anyhow::anyhow!("Error inizializing event_listener. Reason: {}", e) + })?; } Err(e) => { error!("Error while pinning bpf_maps: {}", e); @@ -116,7 +113,7 @@ async fn main() -> Result<(), anyhow::Error> { } Err(e) => { error!("Error while loading bpf maps {}", e); - signal::ctrl_c(); + let _ = signal::ctrl_c().await; } } @@ -132,7 +129,9 @@ async fn init_tc_classifier( //this funtion initialize the tc classifier program info!("Loading programs"); - let mut bpf_new = bpf.lock().unwrap(); + let mut bpf_new = bpf + .lock() + .map_err(|e| anyhow::anyhow!("Cannot get value from lock. Reason: {}", e))?; let program: &mut SchedClassifier = bpf_new .program_mut("identity_classifier") @@ -151,7 +150,9 @@ async fn init_tc_classifier( "Program 'identity_classifier' attached to interface {}", interface ); - let mut map = link_ids.lock().unwrap(); + let mut map = link_ids + .lock() + .map_err(|e| anyhow::anyhow!("Cannot get value from lock. Reason: {}", e))?; map.insert(interface.clone(), link_id); } Err(e) => error!( @@ -167,7 +168,9 @@ async fn init_tc_classifier( async fn init_veth_tracer(bpf: Arc>) -> Result<(), anyhow::Error> { //this functions init the veth_tracer used to make the InterfacesRegistry - let mut bpf_new = bpf.lock().unwrap(); + let mut bpf_new = bpf + .lock() + .map_err(|e| anyhow::anyhow!("Cannot get value from lock. Reason: {}", e))?; //creation tracer let veth_creation_tracer: &mut KProbe = bpf_new @@ -199,7 +202,9 @@ async fn init_veth_tracer(bpf: Arc>) -> Result<(), anyhow::Error> { } async fn init_tcp_registry(bpf: Arc>) -> Result<(), anyhow::Error> { - let mut bpf_new = bpf.lock().unwrap(); + let mut bpf_new = bpf + .lock() + .map_err(|e| anyhow::anyhow!("Cannot get value from lock. Reason: {}", e))?; // init tcp registry let tcp_analyzer: &mut KProbe = bpf_new @@ -236,91 +241,81 @@ async fn init_tcp_registry(bpf: Arc>) -> Result<(), anyhow::Error> { Ok(()) } +// this function init the event listener. Listens for veth events (creation/deletion) and network events (pod to pod communications) +// Doc: +// +// perf_net_events_array: contains is associated with the network events stored in the events_map (EventsMap) +// perf_veth_array: contains is associated with the network events stored in the veth_map (veth_identity_map) +// +// async fn event_listener( - bpf_maps: (Map, Map, Map, Map), + bpf_maps: Vec, link_ids: Arc>>, bpf: Arc>, ) -> Result<(), anyhow::Error> { - // this function init the event listener. Listens for veth events (creation/deletion) and network events (pod to pod communications) - /* Doc: - - perf_net_events_array: contains is associated with the network events stored in the events_map (EventsMap) - perf_veth_array: contains is associated with the network events stored in the veth_map (veth_identity_map) - - */ - info!("Preparing perf_buffers and perf_arrays"); //TODO: try to change from PerfEventArray to a RingBuffer data structure - //let m0=bpf_maps[0]; - //let m1 = bpf_maps[1]; - //let mut ring1=RingBuf::try_from(m0)?; - //let mut ring2=RingBuf::try_from(m1)?; - - //TODO:create an helper function that initialize the data structures and the running - // init PerfEventArrays - let mut perf_veth_array: PerfEventArray = PerfEventArray::try_from(bpf_maps.1)?; - let mut perf_net_events_array: PerfEventArray = PerfEventArray::try_from(bpf_maps.0)?; - let mut tcp_registry_array: PerfEventArray = PerfEventArray::try_from(bpf_maps.3)?; - - // init PerfEventArrays buffers - let mut perf_veth_buffer: Vec> = Vec::new(); - let mut perf_net_events_buffer: Vec> = Vec::new(); - let mut tcp_registry_buffer: Vec> = Vec::new(); - - // fill the input buffers - - for cpu_id in online_cpus().map_err(|e| anyhow::anyhow!("Error {:?}", e))? { - let veth_buf: PerfEventArrayBuffer = perf_veth_array.open(cpu_id, None)?; - perf_veth_buffer.push(veth_buf); - } - for cpu_id in online_cpus().map_err(|e| anyhow::anyhow!("Error {:?}", e))? { - let events_buf: PerfEventArrayBuffer = perf_net_events_array.open(cpu_id, None)?; - perf_net_events_buffer.push(events_buf); + + let mut perf_event_arrays = Vec::new(); // contains a vector of PerfEventArrays + let mut event_buffers = Vec::new(); // contains a vector of buffers + + // create the PerfEventArrays and the buffers + for map in bpf_maps { + debug!("Debugging map type:{:?}", map); + let perf_event_array = PerfEventArray::try_from(map).map_err(|e| { + error!("Cannot create perf_event_array for map.Reason: {}", e); + anyhow::anyhow!("Cannot create perf_event_array for map.Reason: {}", e) + })?; + perf_event_arrays.push(perf_event_array); // this is step 1 + let perf_event_array_buffer = Vec::new(); + event_buffers.push(perf_event_array_buffer); //this is step 2 } - for cpu_id in online_cpus().map_err(|e| anyhow::anyhow!("Error {:?}", e))? { - let tcp_registry_buf: PerfEventArrayBuffer = - tcp_registry_array.open(cpu_id, None)?; - tcp_registry_buffer.push(tcp_registry_buf); + + // fill the input buffers with data from the PerfEventArrays + let cpus = online_cpus().map_err(|e| anyhow::anyhow!("Error {:?}", e))?; + + for (perf_evt_array, perf_evt_array_buffer) in + perf_event_arrays.iter_mut().zip(event_buffers.iter_mut()) + { + for cpu_id in &cpus { + let single_buffer = perf_evt_array.open(*cpu_id, None)?; + perf_evt_array_buffer.push(single_buffer); + } } info!("Listening for events..."); - // init runnings - let veth_running = Arc::new(AtomicBool::new(true)); - let net_events_running = Arc::new(AtomicBool::new(true)); - let tcp_registry_running = Arc::new(AtomicBool::new(true)); + let mut event_buffers = event_buffers.into_iter(); + let perf_veth_buffer = event_buffers + .next() + .expect("Cannot create perf_veth buffer"); + let perf_net_events_buffer = event_buffers + .next() + .expect("Cannot create perf_net_events buffer"); + let tcp_registry_buffer = event_buffers + .next() + .expect("Cannot create tcp_registry buffer"); // init output buffers - let mut veth_buffers = vec![BytesMut::with_capacity(1024); 10]; - let mut events_buffers = vec![BytesMut::with_capacity(1024); online_cpus().iter().len()]; - let mut tcp_buffers = vec![BytesMut::with_capacity(1024); online_cpus().iter().len()]; - - // init running signals - let veth_running_signal = veth_running.clone(); - let net_events_running_signal = net_events_running.clone(); - let tcp_registry_running_signal = tcp_registry_running.clone(); + let veth_buffers = vec![BytesMut::with_capacity(1024); 10]; + let events_buffers = vec![BytesMut::with_capacity(1024); online_cpus().iter().len()]; + let tcp_buffers = vec![BytesMut::with_capacity(1024); online_cpus().iter().len()]; - let veth_link_ids = link_ids.clone(); + // init veth link ids + let veth_link_ids = link_ids; + // spawn async tasks let veth_events_displayer = tokio::spawn(async move { - display_veth_events( - bpf.clone(), - perf_veth_buffer, - veth_running, - veth_buffers, - veth_link_ids, - ) - .await; + display_veth_events(bpf.clone(), perf_veth_buffer, veth_buffers, veth_link_ids).await; }); - // IDEA: Maybe we don't need to display all this events let net_events_displayer = tokio::spawn(async move { - display_events(perf_net_events_buffer, net_events_running, events_buffers).await; + display_events(perf_net_events_buffer, events_buffers).await; }); let tcp_registry_events_displayer: tokio::task::JoinHandle<()> = tokio::spawn(async move { - display_tcp_registry_events(tcp_registry_buffer, tcp_registry_running, tcp_buffers).await; + display_tcp_registry_events(tcp_registry_buffer, tcp_buffers).await; }); #[cfg(feature = "experimental")] @@ -330,12 +325,6 @@ async fn event_listener( #[cfg(not(feature = "experimental"))] tokio::select! { - /* result = scan_cgroup_cronjob=>{ - match result{ - Err(e)=>error!("scan_cgroup_cronjob panicked {:?}",e), - std::result::Result::Ok(_) => info!("cgroup scan cronjob exited"), - } - } */ result = veth_events_displayer=>{ match result{ Err(e)=>error!("veth_event_displayer panicked {:?}",e), @@ -359,9 +348,6 @@ async fn event_listener( _= signal::ctrl_c()=>{ info!("Triggered Exiting..."); - veth_running_signal.store(false, Ordering::SeqCst); - net_events_running_signal.store(false, Ordering::SeqCst); - tcp_registry_running_signal.store(false, Ordering::SeqCst); } } @@ -396,9 +382,6 @@ async fn event_listener( _= signal::ctrl_c()=>{ info!("Triggered Exiting..."); - veth_running_signal.store(false, Ordering::SeqCst); - net_events_running_signal.store(false, Ordering::SeqCst); - tcp_registry_running_signal.store(false, Ordering::SeqCst); } } diff --git a/core/src/components/identity/src/mod.rs b/core/src/components/identity/src/mod.rs index e3bb59e0..54134144 100644 --- a/core/src/components/identity/src/mod.rs +++ b/core/src/components/identity/src/mod.rs @@ -1,4 +1,3 @@ pub mod helpers; pub mod structs; -pub mod enums; -pub mod map_handlers; \ No newline at end of file +pub mod enums; \ No newline at end of file diff --git a/core/src/components/identity/src/structs.rs b/core/src/components/identity/src/structs.rs index d8cff939..7e2aa2b0 100644 --- a/core/src/components/identity/src/structs.rs +++ b/core/src/components/identity/src/structs.rs @@ -19,17 +19,17 @@ unsafe impl aya::Pod for PacketLog {} /* * Connection Array that contains the hash_id associated with an active connection */ -#[repr(C)] -#[derive(Clone, Copy, Zeroable)] -pub struct ConnArray { - pub src_ip: u32, - pub dst_ip: u32, - pub src_port: u16, - pub dst_port: u16, - pub proto: u8, -} +//#[repr(C)] +//#[derive(Clone, Copy, Zeroable)] +//pub struct ConnArray { +// pub src_ip: u32, +// pub dst_ip: u32, +// pub src_port: u16, +// pub dst_port: u16, +// pub proto: u8, +//} -unsafe impl aya::Pod for ConnArray {} +//unsafe impl aya::Pod for ConnArray {} #[repr(C)] #[derive(Clone, Copy)] @@ -44,13 +44,13 @@ pub struct VethLog { #[repr(C)] #[derive(Clone, Copy)] -pub struct TcpPacketRegistry{ +pub struct TcpPacketRegistry { pub proto: u8, pub src_ip: u32, pub dst_ip: u32, pub src_port: u16, pub dst_port: u16, pub pid: u32, - pub command: [u8;16], + pub command: [u8; 16], pub cgroup_id: u64, -} \ No newline at end of file +} From b9edd1dac7a98a8565ce056b110b3187e7d49d08 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Thu, 22 Jan 2026 21:07:47 +0100 Subject: [PATCH 03/24] [#158]: added program handlers function in the common crate. Remove duplicated code in metrics module --- core/common/Cargo.toml | 1 + core/common/src/lib.rs | 3 + core/common/src/map_handlers.rs | 4 - core/common/src/program_handlers.rs | 42 +++++++ core/src/components/metrics/Cargo.toml | 16 ++- core/src/components/metrics/src/helpers.rs | 107 +++++++++++------- core/src/components/metrics/src/main.rs | 72 ++++++------ .../components/metrics/src/maps_handlers.rs | 48 -------- core/src/components/metrics/src/mod.rs | 4 +- .../metrics/src/program_handlers.rs | 59 ---------- 10 files changed, 165 insertions(+), 191 deletions(-) create mode 100644 core/common/src/program_handlers.rs delete mode 100644 core/src/components/metrics/src/maps_handlers.rs delete mode 100644 core/src/components/metrics/src/program_handlers.rs diff --git a/core/common/Cargo.toml b/core/common/Cargo.toml index ac87f689..854c04e5 100644 --- a/core/common/Cargo.toml +++ b/core/common/Cargo.toml @@ -19,3 +19,4 @@ aya = "0.13.1" [features] map-handlers = [] +program-handlers = [] diff --git a/core/common/src/lib.rs b/core/common/src/lib.rs index 2f8e5635..1d015a27 100644 --- a/core/common/src/lib.rs +++ b/core/common/src/lib.rs @@ -1,4 +1,7 @@ pub mod constants; pub mod formatters; pub mod logger; +#[cfg(feature = "map-handlers")] pub mod map_handlers; +#[cfg(feature = "program-handlers")] +pub mod program_handlers; \ No newline at end of file diff --git a/core/common/src/map_handlers.rs b/core/common/src/map_handlers.rs index 43330fab..2882d66f 100644 --- a/core/common/src/map_handlers.rs +++ b/core/common/src/map_handlers.rs @@ -17,10 +17,6 @@ use tracing::{error, info}; // // this function init the bpfs maps used in the main program // -// index 0: events_map -// index 1: veth_map -// index 2: blocklist map -// index 3: tcp_registry map // #[cfg(feature = "map-handlers")] diff --git a/core/common/src/program_handlers.rs b/core/common/src/program_handlers.rs new file mode 100644 index 00000000..8832daff --- /dev/null +++ b/core/common/src/program_handlers.rs @@ -0,0 +1,42 @@ +use aya::{Ebpf, programs::KProbe}; +use std::convert::TryInto; +use std::sync::{Arc, Mutex}; +use tracing::{error, info}; + +#[cfg(feature = "program-handlers")] +pub fn load_program( + bpf: Arc>, + program_name: &str, + actual_program: &str, +) -> Result<(), anyhow::Error> { + let mut bpf_new = bpf.lock().expect("Cannot get value from lock"); + + // Load and attach the eBPF programs + let program: &mut KProbe = bpf_new + .program_mut(program_name) + .ok_or_else(|| anyhow::anyhow!("Program {} not found", program_name))? + .try_into() + .map_err(|e| anyhow::anyhow!("Failed to convert program: {:?}", e))?; + + program + .load() + .map_err(|e| anyhow::anyhow!("Cannot load program: {}. Error: {}", &program_name, e))?; + + match program.attach(actual_program, 0) { + Ok(_) => info!("{} program attached successfully", actual_program), + Err(e) => { + error!("Error attaching {} program {:?}", actual_program, e); + return Err(anyhow::anyhow!( + "Failed to attach {}: {:?}", + actual_program, + e + )); + } + }; + + info!( + "eBPF program {} loaded and attached successfully", + program_name + ); + Ok(()) +} diff --git a/core/src/components/metrics/Cargo.toml b/core/src/components/metrics/Cargo.toml index 112872e8..0e88d8c0 100644 --- a/core/src/components/metrics/Cargo.toml +++ b/core/src/components/metrics/Cargo.toml @@ -7,11 +7,21 @@ edition = "2024" aya = "0.13.1" aya-log = "0.2.1" bytes = "1.4" -tokio = { version = "1.48.0", features = ["rt","macros","time","fs","signal","rt-multi-thread"] } +tokio = { version = "1.48.0", features = [ + "rt", + "macros", + "time", + "fs", + "signal", + "rt-multi-thread", +] } anyhow = "1.0" tracing = "0.1.41" tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } libc = "0.2.172" bytemuck = "1.23.0" -cortexbrain-common = { path = "../../../common" } -nix ={version="0.30.1",features=["net"]} +cortexbrain-common = { path = "../../../common", features = [ + "map-handlers", + "program-handlers", +] } +nix = { version = "0.30.1", features = ["net"] } diff --git a/core/src/components/metrics/src/helpers.rs b/core/src/components/metrics/src/helpers.rs index 1b4628e4..f519c7ea 100644 --- a/core/src/components/metrics/src/helpers.rs +++ b/core/src/components/metrics/src/helpers.rs @@ -1,24 +1,23 @@ -use aya::{maps::{ - perf::PerfEventArrayBuffer, Map, MapData, PerfEventArray - }, util::online_cpus}; +use aya::{ + maps::{Map, MapData, PerfEventArray, perf::PerfEventArrayBuffer}, + util::online_cpus, +}; use bytes::BytesMut; -use tokio::signal; -use std::{ - sync::{ - Arc, - atomic::{AtomicBool, Ordering}, - }, +use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, }; +use tokio::signal; -use tracing::{error, info}; +use tracing::{debug, error, info}; use crate::structs::NetworkMetrics; use crate::structs::TimeStampMetrics; pub async fn display_metrics_map( mut perf_buffers: Vec>, - running: Arc, // Changed to Arc + running: Arc, // Changed to Arc mut buffers: Vec, ) { info!("Starting metrics event listener..."); @@ -46,10 +45,23 @@ pub async fn display_metrics_map( let sk_receive_buffer_size = net_metrics.sk_receive_buffer_size; info!( "tgid: {}, comm: {}, ts_us: {}, sk_drops: {}, sk_err: {}, sk_err_soft: {}, sk_backlog_len: {}, sk_write_memory_queued: {}, sk_ack_backlog: {}, sk_receive_buffer_size: {}", - tgid, comm, ts_us, sk_drop_count, sk_err, sk_err_soft, sk_backlog_len, sk_write_memory_queued, sk_ack_backlog, sk_receive_buffer_size + tgid, + comm, + ts_us, + sk_drop_count, + sk_err, + sk_err_soft, + sk_backlog_len, + sk_write_memory_queued, + sk_ack_backlog, + sk_receive_buffer_size ); } else { - info!("Received data too small: {} bytes, expected: {}", data.len(), std::mem::size_of::()); + info!( + "Received data too small: {} bytes, expected: {}", + data.len(), + std::mem::size_of::() + ); } } } @@ -65,7 +77,7 @@ pub async fn display_metrics_map( pub async fn display_time_stamp_events_map( mut perf_buffers: Vec>, - running: Arc, // Changed to Arc + running: Arc, // Changed to Arc mut buffers: Vec, ) { info!("Starting timestamp event listener..."); @@ -107,48 +119,67 @@ pub async fn display_time_stamp_events_map( info!("Timestamp event listener stopped"); } -pub async fn event_listener(bpf_maps: (Map, Map)) -> Result<(), anyhow::Error> { +pub async fn event_listener(bpf_maps: Vec) -> Result<(), anyhow::Error> { info!("Getting CPU count..."); - let cpu_count = online_cpus().map_err(|e| anyhow::anyhow!("Error {:?}", e))?.len(); - info!("CPU count: {}", cpu_count); - + + let mut perf_event_arrays = Vec::new(); // contains a vector of PerfEventArrays + let mut event_buffers = Vec::new(); // contains a vector of buffers + info!("Creating perf buffers..."); - let mut net_perf_buffer: Vec> = Vec::new(); - let mut net_perf_array: PerfEventArray = PerfEventArray::try_from(bpf_maps.0)?; - let mut time_stamp_events_perf_buffer: Vec> = Vec::new(); - let mut time_stamp_events_perf_array: PerfEventArray = - PerfEventArray::try_from(bpf_maps.1)?; - - info!("Opening perf buffers for {} CPUs...", cpu_count); - for cpu_id in online_cpus().map_err(|e| anyhow::anyhow!("Error {:?}", e))? { - let buf: PerfEventArrayBuffer = net_perf_array.open(cpu_id, None)?; - net_perf_buffer.push(buf); + for map in bpf_maps { + debug!("Debugging map type:{:?}", map); + let perf_event_array = PerfEventArray::try_from(map).map_err(|e| { + error!("Cannot create perf_event_array for map.Reason: {}", e); + anyhow::anyhow!("Cannot create perf_event_array for map.Reason: {}", e) + })?; + perf_event_arrays.push(perf_event_array); // this is step 1 + let perf_event_array_buffer = Vec::new(); + event_buffers.push(perf_event_array_buffer); //this is step 2 } - for cpu_id in online_cpus().map_err(|e| anyhow::anyhow!("Error {:?}", e))? { - let buf: PerfEventArrayBuffer = time_stamp_events_perf_array.open(cpu_id, None)?; - time_stamp_events_perf_buffer.push(buf); + + let cpu_count = online_cpus().map_err(|e| anyhow::anyhow!("Error {:?}", e))?; + + //info!("CPU count: {}", cpu_count); + for (perf_evt_array, perf_evt_array_buffer) in + perf_event_arrays.iter_mut().zip(event_buffers.iter_mut()) + { + for cpu_id in &cpu_count { + let single_buffer = perf_evt_array.open(*cpu_id, None)?; + perf_evt_array_buffer.push(single_buffer); + } } + + //info!("Opening perf buffers for {} CPUs...", cpu_count); info!("Perf buffers created successfully"); + let mut event_buffers = event_buffers.into_iter(); + + let time_stamp_events_perf_buffer = event_buffers.next().expect(""); + let net_perf_buffer = event_buffers.next().expect(""); // Create shared running flags let net_metrics_running = Arc::new(AtomicBool::new(true)); let time_stamp_events_running = Arc::new(AtomicBool::new(true)); - + // Create proper sized buffers - let net_metrics_buffers = vec![BytesMut::with_capacity(1024); cpu_count]; - let time_stamp_events_buffers = vec![BytesMut::with_capacity(1024); cpu_count]; - + let net_metrics_buffers = vec![BytesMut::with_capacity(1024); cpu_count.len()]; + let time_stamp_events_buffers = vec![BytesMut::with_capacity(1024); cpu_count.len()]; + // Clone for the signal handler let net_metrics_running_signal = net_metrics_running.clone(); let time_stamp_events_running_signal = time_stamp_events_running.clone(); - + info!("Starting event listener tasks..."); let metrics_map_displayer = tokio::spawn(async move { display_metrics_map(net_perf_buffer, net_metrics_running, net_metrics_buffers).await; }); let time_stamp_events_displayer = tokio::spawn(async move { - display_time_stamp_events_map(time_stamp_events_perf_buffer, time_stamp_events_running, time_stamp_events_buffers).await + display_time_stamp_events_map( + time_stamp_events_perf_buffer, + time_stamp_events_running, + time_stamp_events_buffers, + ) + .await }); info!("Event listeners started, entering main loop..."); @@ -176,4 +207,4 @@ pub async fn event_listener(bpf_maps: (Map, Map)) -> Result<(), anyhow::Error> { // return success Ok(()) -} \ No newline at end of file +} diff --git a/core/src/components/metrics/src/main.rs b/core/src/components/metrics/src/main.rs index 6b22a865..9648e8a2 100644 --- a/core/src/components/metrics/src/main.rs +++ b/core/src/components/metrics/src/main.rs @@ -1,27 +1,18 @@ -use aya::{ - Ebpf -}; - +use anyhow::{Context, Ok}; +use aya::Ebpf; +use cortexbrain_common::{constants, logger}; use std::{ env, fs, path::Path, - sync::{ - Arc, Mutex, - }, + sync::{Arc, Mutex}, }; - -use anyhow::{Context, Ok}; use tracing::{error, info}; -use cortexbrain_common::{constants, logger}; mod helpers; -use crate::{helpers::event_listener, maps_handlers::map_pinner, program_handlers::load_and_attach_tcp_programs}; - -mod maps_handlers; -use crate::maps_handlers::init_ebpf_maps; +use crate::helpers::event_listener; -mod program_handlers; -use crate::program_handlers::load_program; +use cortexbrain_common::map_handlers::{init_bpf_maps, map_pinner}; +use cortexbrain_common::program_handlers::load_program; mod structs; @@ -33,41 +24,50 @@ async fn main() -> Result<(), anyhow::Error> { info!("Starting metrics service..."); info!("fetching data"); - let bpf_path = env::var(constants::BPF_PATH).context("BPF_PATH environment variable required")?; + let bpf_path = + env::var(constants::BPF_PATH).context("BPF_PATH environment variable required")?; let data = fs::read(Path::new(&bpf_path)).context("Failed to load file from path")?; let bpf = Arc::new(Mutex::new(Ebpf::load(&data)?)); let tcp_bpf = bpf.clone(); let tcp_rev_bpf = bpf.clone(); + let tcp_v6_bpf = bpf.clone(); info!("Running Ebpf logger"); info!("loading programs"); - let bpf_map_save_path = - std::env::var(constants::PIN_MAP_PATH).context("PIN_MAP_PATH environment variable required")?; + let bpf_map_save_path = std::env::var(constants::PIN_MAP_PATH) + .context("PIN_MAP_PATH environment variable required")?; - match init_ebpf_maps(bpf.clone()) { - std::result::Result::Ok(maps) => { + let map_data = vec!["time_stamp_events".to_string(), "net_metrics".to_string()]; + + match init_bpf_maps(bpf.clone(), map_data) { + std::result::Result::Ok(bpf_maps) => { info!("BPF maps loaded successfully"); let pin_path = std::path::PathBuf::from(&bpf_map_save_path); info!("About to call map_pinner with path: {:?}", pin_path); - match map_pinner(&maps, &pin_path).await { - std::result::Result::Ok(_) => { + match map_pinner(bpf_maps, &pin_path) { + std::result::Result::Ok(maps) => { info!("BPF maps pinned successfully to {}", bpf_map_save_path); { load_program(bpf.clone(), "metrics_tracer", "tcp_identify_packet_loss") - .context("An error occured during the execution of load_program function")?; - } - - { - load_and_attach_tcp_programs(tcp_bpf.clone()) - .context("An error occured during the execution of load_and_attach_tcp_programs function")?; + .context( + "An error occured during the execution of load_program function", + )?; + + load_program(tcp_bpf,"tcp_connect","tcp_v4_connect") + .context("An error occured during the execution of load_and_attach_tcp_programs function")?; + load_program(tcp_v6_bpf,"tcp_connect","tcp_v6_connect") + .context("An error occured during the execution of load_and_attach_tcp_programs function")?; + + load_program( + tcp_rev_bpf, + "tcp_rcv_state_process", + "tcp_rcv_state_process", + ) + .context( + "An error occured during the execution of load_program function", + )?; } - - { - load_program(tcp_rev_bpf.clone(), "tcp_rcv_state_process", "tcp_rcv_state_process") - .context("An error occured during the execution of load_program function")?; - } - event_listener(maps).await?; } Err(e) => { @@ -83,4 +83,4 @@ async fn main() -> Result<(), anyhow::Error> { } Ok(()) -} \ No newline at end of file +} diff --git a/core/src/components/metrics/src/maps_handlers.rs b/core/src/components/metrics/src/maps_handlers.rs deleted file mode 100644 index 12c3d0a2..00000000 --- a/core/src/components/metrics/src/maps_handlers.rs +++ /dev/null @@ -1,48 +0,0 @@ -use std::{path::PathBuf, sync::{Arc, Mutex}}; -use tokio::fs; -use anyhow::Error; -use aya::{maps::Map, Ebpf}; -use tracing::info; - - - -pub fn init_ebpf_maps(bpf: Arc>) -> Result<(Map, Map), anyhow::Error> { - // this function init the bpfs maps used in the main program - /* - index 0: net_metrics - index 1: time_stamp_events - */ - let mut bpf_new = bpf.lock().unwrap(); - - let net_metrics_map = bpf_new - .take_map("net_metrics") - .ok_or_else(|| anyhow::anyhow!("net_metrics map not found"))?; - - let time_stamps_events_map = bpf_new - .take_map("time_stamp_events") - .ok_or_else(|| anyhow::anyhow!("time_stamp_events map not found"))?; - - Ok((net_metrics_map, time_stamps_events_map)) -} - -pub async fn map_pinner(maps: &(Map, Map), path: &PathBuf) -> Result<(), Error> { - // check if the map exists - if !path.exists() { - info!("Pin path {:?} does not exist. Creating it...", path); - fs::create_dir_all(&path).await?; - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).await?; - } - } - - let map1_path = path.join("net_metrics"); - let map2_path = path.join("time_stamp_events"); - - // maps pinning - maps.0.pin(&map1_path)?; - maps.1.pin(&map2_path)?; - - Ok(()) -} diff --git a/core/src/components/metrics/src/mod.rs b/core/src/components/metrics/src/mod.rs index 8c4a839a..8414b63d 100644 --- a/core/src/components/metrics/src/mod.rs +++ b/core/src/components/metrics/src/mod.rs @@ -1,5 +1,3 @@ mod structs; mod enums; -mod map_handlers; -mod helpers; -mod program_handlers; \ No newline at end of file +mod helpers; \ No newline at end of file diff --git a/core/src/components/metrics/src/program_handlers.rs b/core/src/components/metrics/src/program_handlers.rs deleted file mode 100644 index 24d18cbd..00000000 --- a/core/src/components/metrics/src/program_handlers.rs +++ /dev/null @@ -1,59 +0,0 @@ -use std::sync::{Arc, Mutex}; - -use aya::{programs::KProbe, Ebpf}; -use tracing::{info, error}; -use std::convert::TryInto; - -pub fn load_program(bpf: Arc>, program_name: &str, actual_program: &str) -> Result<(), anyhow::Error> { - let mut bpf_new = bpf.lock().unwrap(); - - // Load and attach the eBPF programs - let program: &mut KProbe = bpf_new - .program_mut(program_name) - .ok_or_else(|| anyhow::anyhow!("Program {} not found", program_name))? - .try_into() - .map_err(|e| anyhow::anyhow!("Failed to convert program: {:?}", e))?; - - program.load()?; - - match program.attach(actual_program, 0) { - Ok(_) => info!("{} program attached successfully", actual_program), - Err(e) => { - error!("Error attaching {} program {:?}", actual_program, e); - return Err(anyhow::anyhow!("Failed to attach {}: {:?}", actual_program, e)); - } - }; - - info!("eBPF program {} loaded and attached successfully", program_name); - Ok(()) -} - -pub fn load_and_attach_tcp_programs(bpf: Arc>) -> Result<(), anyhow::Error> { - let mut bpf_new = bpf.lock().unwrap(); - - // Load and attach the eBPF programs - let tcp_prog: &mut KProbe = bpf_new - .program_mut("tcp_connect") - .ok_or_else(|| anyhow::anyhow!("Program tcp_connect not found"))? - .try_into() - .map_err(|e| anyhow::anyhow!("Failed to convert program tcp_connect: {:?}", e))?; - tcp_prog.load()?; - - match tcp_prog.attach("tcp_v4_connect", 0) { - Ok(_) => info!("tcp_v4_connect program attached successfully"), - Err(e) => { - error!("Error attaching tcp_v4_connect: {:?}", e); - return Err(anyhow::anyhow!("Failed to attach tcp_v4_connect: {:?}", e)); - } - }; - - match tcp_prog.attach("tcp_v6_connect", 0) { - Ok(_) => info!("tcp_v6_connect program attached successfully"), - Err(e) => { - error!("Error attaching tcp_v6_connect: {:?}", e); - return Err(anyhow::anyhow!("Failed to attach tcp_v6_connect: {:?}", e)); - } - }; - - Ok(()) -} \ No newline at end of file From d3a5342c057233eeb369664f50657268fa5458a1 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Thu, 22 Jan 2026 23:05:29 +0100 Subject: [PATCH 04/24] [#168]: added load from program-handlers in identity user space implementation. Added a small doc in the conntracker/main.rs file --- core/common/src/program_handlers.rs | 4 +- core/src/components/conntracker/src/main.rs | 24 ++++++-- core/src/components/identity/Cargo.toml | 2 +- core/src/components/identity/src/helpers.rs | 3 + core/src/components/identity/src/main.rs | 67 +++------------------ 5 files changed, 34 insertions(+), 66 deletions(-) diff --git a/core/common/src/program_handlers.rs b/core/common/src/program_handlers.rs index 8832daff..5991befe 100644 --- a/core/common/src/program_handlers.rs +++ b/core/common/src/program_handlers.rs @@ -9,7 +9,9 @@ pub fn load_program( program_name: &str, actual_program: &str, ) -> Result<(), anyhow::Error> { - let mut bpf_new = bpf.lock().expect("Cannot get value from lock"); + let mut bpf_new = bpf + .lock() + .map_err(|e| anyhow::anyhow!("Cannot get value from lock. Reason: {}", e))?; // Load and attach the eBPF programs let program: &mut KProbe = bpf_new diff --git a/core/src/components/conntracker/src/main.rs b/core/src/components/conntracker/src/main.rs index 7a12642d..e723e4b4 100644 --- a/core/src/components/conntracker/src/main.rs +++ b/core/src/components/conntracker/src/main.rs @@ -29,14 +29,13 @@ use aya_ebpf::{ }; use crate::tc::try_identity_classifier; -use crate::veth_tracer::try_veth_tracer; use crate::tcp_analyzer::try_tcp_analyzer; - +use crate::veth_tracer::try_veth_tracer; // docs: // // virtual ethernet (veth) interface tracer: -// This function is triggered when a virtual ethernet interface is created +// This function is triggered when a virtual ethernet interface is created // #[kprobe] @@ -50,7 +49,7 @@ pub fn veth_creation_trace(ctx: ProbeContext) -> u32 { // docs: // // virtual ethernet (veth) interface tracer: -// This function is triggered when a virtual ethernet interface is deleted +// This function is triggered when a virtual ethernet interface is deleted // #[kprobe] @@ -94,14 +93,29 @@ pub fn identity_classifier(ctx: TcContext) -> i32 { // // this kprobe retrieves pid data and task id of an incoming packet +// this kprobe separation is needed because every kprobe program can be attached only one time. +// if you try to attach the same program the kernel returns this error: "Program is already attached" +// this is the reason why we have tcp_message_tracer_connect and tcp_message_tracer_rcv that are essentially the same functions +// but in the kernel space one is attached to the tcp_v4_connect kprobe and one to the tcp_v4_rcv kprobe +// TODO: a good addition to the library will be a function that check if the program is already attached: +// if the program is attached it creates a safe copy of the program to attach a second kernel symbol (kprobes) +// if the program is not attached we have the traditional behaviour (load the program + attach the program to the kernel symbol (kprobes)) + #[kprobe] -pub fn tcp_message_tracer(ctx: ProbeContext) -> u32 { +pub fn tcp_message_tracer_connect(ctx: ProbeContext) -> u32 { match try_tcp_analyzer(ctx) { Ok(ret_val) => ret_val, Err(ret_val) => ret_val.try_into().unwrap_or(1), } } +#[kprobe] +pub fn tcp_message_tracer_rcv(ctx: ProbeContext) -> u32 { + match try_tcp_analyzer(ctx) { + Ok(ret_val) => ret_val, + Err(ret_val) => ret_val.try_into().unwrap_or(1), + } +} //ref:https://elixir.bootlin.com/linux/v6.15.1/source/include/uapi/linux/ethtool.h#L536 //https://elixir.bootlin.com/linux/v6.15.1/source/drivers/net/veth.c#L268 diff --git a/core/src/components/identity/Cargo.toml b/core/src/components/identity/Cargo.toml index 3146991c..f5bdb378 100644 --- a/core/src/components/identity/Cargo.toml +++ b/core/src/components/identity/Cargo.toml @@ -32,7 +32,7 @@ tracing = "0.1.41" tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } bytemuck = { version = "1.23.0", features = ["derive"] } bytemuck_derive = "1.10.1" -cortexbrain-common = { path = "../../../common/", features = ["map-handlers"] } +cortexbrain-common = { path = "../../../common/", features = ["map-handlers","program-handlers"] } nix = { version = "0.30.1", features = ["net"] } kube = { version = "2.0.1", features = ["client"] } k8s-openapi = { version = "0.26.0", features = ["v1_34"] } diff --git a/core/src/components/identity/src/helpers.rs b/core/src/components/identity/src/helpers.rs index 05b96032..95127893 100644 --- a/core/src/components/identity/src/helpers.rs +++ b/core/src/components/identity/src/helpers.rs @@ -52,6 +52,7 @@ pub async fn display_events>( //running: Arc, mut buffers: Vec, ) { + // FIXME: here maybe we need to use a loop with tokio::select while true { for buf in perf_buffers.iter_mut() { match buf.read_events(&mut buffers) { @@ -109,6 +110,7 @@ pub async fn display_veth_events>( mut buffers: Vec, mut link_ids: Arc>>, ) { + // FIXME: here maybe we need to use a loop with tokio::select while true { for buf in perf_buffers.iter_mut() { match buf.read_events(&mut buffers) { @@ -268,6 +270,7 @@ pub async fn display_tcp_registry_events>( //running: Arc, mut buffers: Vec, ) { + // FIXME: here maybe we need to use a loop with tokio::select while true { for buf in perf_buffers.iter_mut() { match buf.read_events(&mut buffers) { diff --git a/core/src/components/identity/src/main.rs b/core/src/components/identity/src/main.rs index 9dd6ce94..ac4ed376 100644 --- a/core/src/components/identity/src/main.rs +++ b/core/src/components/identity/src/main.rs @@ -18,7 +18,7 @@ use crate::helpers::{ use aya::{ Ebpf, maps::{Map, perf::PerfEventArray}, - programs::{KProbe, SchedClassifier, TcAttachType, tc::SchedClassifierLinkId}, + programs::{SchedClassifier, TcAttachType, tc::SchedClassifierLinkId}, util::online_cpus, }; @@ -27,6 +27,7 @@ use crate::helpers::scan_cgroup_cronjob; use bytes::BytesMut; use cortexbrain_common::map_handlers::{init_bpf_maps, map_pinner, populate_blocklist}; +use cortexbrain_common::program_handlers::load_program; use std::{ convert::TryInto, path::Path, @@ -65,7 +66,6 @@ async fn main() -> Result<(), anyhow::Error> { let data = vec![ "EventsMap".to_string(), "veth_identity_map".to_string(), - //"Blocklist".to_string(), "TcpPacketRegistry".to_string(), ]; match init_bpf_maps(bpf.clone(), data) { @@ -167,76 +167,25 @@ async fn init_tc_classifier( async fn init_veth_tracer(bpf: Arc>) -> Result<(), anyhow::Error> { //this functions init the veth_tracer used to make the InterfacesRegistry - - let mut bpf_new = bpf - .lock() - .map_err(|e| anyhow::anyhow!("Cannot get value from lock. Reason: {}", e))?; - //creation tracer - let veth_creation_tracer: &mut KProbe = bpf_new - .program_mut("veth_creation_trace") - .ok_or_else(|| anyhow::anyhow!("program 'veth_creation_trace' not found"))? - .try_into()?; - veth_creation_tracer.load()?; - - match veth_creation_tracer.attach("register_netdevice", 0) { - std::result::Result::Ok(_) => info!("veth_creation_tracer program attached successfully"), - Err(e) => error!("Error attaching veth_creation_tracer program {:?}", e), - } - //deletion tracer - let veth_deletion_tracer: &mut KProbe = bpf_new - .program_mut("veth_deletion_trace") - .ok_or_else(|| anyhow::anyhow!("program 'veth_deletion_trace' not found"))? - .try_into()?; - veth_deletion_tracer - .load() - .context("Failed to load deletetion_tracer program")?; + load_program(bpf.clone(), "veth_creation_trace", "register_netdevice")?; - match veth_deletion_tracer.attach("unregister_netdevice_queue", 0) { - std::result::Result::Ok(_) => info!("veth_deletion_trace program attached successfully"), - Err(e) => error!("Error attaching veth_deletetion_trace program {:?}", e), - } + //deletion tracer + load_program(bpf, "veth_deletion_trace", "unregister_netdevice_queue")?; Ok(()) } async fn init_tcp_registry(bpf: Arc>) -> Result<(), anyhow::Error> { - let mut bpf_new = bpf - .lock() - .map_err(|e| anyhow::anyhow!("Cannot get value from lock. Reason: {}", e))?; - // init tcp registry - let tcp_analyzer: &mut KProbe = bpf_new - .program_mut("tcp_message_tracer") - .ok_or_else(|| anyhow::anyhow!("program 'tcp_message_tracer' not found"))? - .try_into()?; - tcp_analyzer - .load() - .context("Failed to load tcp_message_tracer")?; + // .clone() increments the reference count of the shared Ebpf instance. + load_program(bpf.clone(), "tcp_message_tracer_rcv", "tcp_v4_rcv")?; info!("initializing tcp tracing functions"); - match tcp_analyzer.attach("tcp_v4_rcv", 0) { - std::result::Result::Ok(_) => { - info!("tcp_message_tracer attached successfully to the tcp_v4_rcv function ") - } - Err(e) => error!( - "Error attaching tcp_message_tracer to the tcp_v4_rcv function. Error: {:?}", - e - ), - } - - match tcp_analyzer.attach("tcp_v4_connect", 0) { - std::result::Result::Ok(_) => { - info!("tcp_message_tracer attached successfully to the tcp_v4_connect function ") - } - Err(e) => error!( - "Error attaching tcp_message_tracer to the tcp_v4_connect function. Error: {:?}", - e - ), - } + load_program(bpf, "tcp_message_tracer_connect", "tcp_v4_connect")?; Ok(()) } From b8449a36f6e2a001ccfe4a2ffb2791d7c890ee4c Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Fri, 23 Jan 2026 20:24:15 +0100 Subject: [PATCH 05/24] [#158]: fixed typos in the map names --- core/src/components/conntracker/src/data_structures.rs | 2 +- core/src/components/identity/src/main.rs | 2 +- core/src/testing/identity.yaml | 2 +- core/src/testing/metrics.yaml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/components/conntracker/src/data_structures.rs b/core/src/components/conntracker/src/data_structures.rs index 35861a84..4de05cca 100644 --- a/core/src/components/conntracker/src/data_structures.rs +++ b/core/src/components/conntracker/src/data_structures.rs @@ -87,7 +87,7 @@ pub struct TcpPacketRegistry{ // -#[map(name = "EventsMap", pinning = "by_name")] +#[map(name = "events_map", pinning = "by_name")] pub static mut EVENTS: PerfEventArray = PerfEventArray::new(0); // FIXME: this might be useless diff --git a/core/src/components/identity/src/main.rs b/core/src/components/identity/src/main.rs index ac4ed376..b4f6d18a 100644 --- a/core/src/components/identity/src/main.rs +++ b/core/src/components/identity/src/main.rs @@ -64,7 +64,7 @@ async fn main() -> Result<(), anyhow::Error> { let bpf_map_save_path = std::env::var(constants::PIN_MAP_PATH) .context("PIN_MAP_PATH environment variable required")?; let data = vec![ - "EventsMap".to_string(), + "events_map".to_string(), "veth_identity_map".to_string(), "TcpPacketRegistry".to_string(), ]; diff --git a/core/src/testing/identity.yaml b/core/src/testing/identity.yaml index bb027d2a..1b77d607 100644 --- a/core/src/testing/identity.yaml +++ b/core/src/testing/identity.yaml @@ -53,7 +53,7 @@ spec: - SYS_PTRACE containers: - name: identity - image: ghcr.io/cortexflow/identity:latest + image: lorenzotettamanti/cortexflow-identity:0.1.5-refcount7 command: ["/bin/bash", "-c"] args: - | diff --git a/core/src/testing/metrics.yaml b/core/src/testing/metrics.yaml index 4c775cab..1c1ecf83 100644 --- a/core/src/testing/metrics.yaml +++ b/core/src/testing/metrics.yaml @@ -19,7 +19,7 @@ spec: hostNetwork: true containers: - name: metrics - image: ghcr.io/cortexflow/metrics:latest + image: lorenzotettamanti/cortexflow-metrics:0.1.2-test8 command: ["/bin/bash", "-c"] args: - | From 52cab4c0090930796656ec7da9a576d7ecf366e0 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Fri, 23 Jan 2026 22:35:12 +0100 Subject: [PATCH 06/24] [#158]: fixed bpf error: Error: the program is already loaded.Improved map handlers code --- core/common/src/map_handlers.rs | 5 ++++- core/src/components/identity/src/main.rs | 21 ++++++++++--------- core/src/components/metrics/src/helpers.rs | 16 +++++++------- core/src/components/metrics/src/main.rs | 4 ++-- .../src/components/metrics_tracer/src/main.rs | 8 ++++++- core/src/testing/identity.yaml | 2 +- core/src/testing/metrics.yaml | 2 +- 7 files changed, 34 insertions(+), 24 deletions(-) diff --git a/core/common/src/map_handlers.rs b/core/common/src/map_handlers.rs index 2882d66f..2e22736a 100644 --- a/core/common/src/map_handlers.rs +++ b/core/common/src/map_handlers.rs @@ -30,7 +30,10 @@ pub fn init_bpf_maps( bpf: Arc>, map_names: Vec, ) -> Result { - let mut bpf_new = bpf.lock().expect("Cannot get value from lock"); + let mut bpf_new = bpf + .lock() + .map_err(|e| anyhow::anyhow!("Cannot get value from lock. Reason: {}", e))?; + let mut maps = Vec::new(); // stores bpf_maps_objects for name in &map_names { diff --git a/core/src/components/identity/src/main.rs b/core/src/components/identity/src/main.rs index b4f6d18a..56f81d68 100644 --- a/core/src/components/identity/src/main.rs +++ b/core/src/components/identity/src/main.rs @@ -37,7 +37,7 @@ use std::{ use anyhow::{Context, Ok}; use cortexbrain_common::{constants, logger}; use tokio::{fs, signal}; -use tracing::{debug, error, info}; +use tracing::{debug, error, info, warn}; use std::collections::HashMap; @@ -63,12 +63,13 @@ async fn main() -> Result<(), anyhow::Error> { let bpf = Arc::new(Mutex::new(Ebpf::load(&data)?)); let bpf_map_save_path = std::env::var(constants::PIN_MAP_PATH) .context("PIN_MAP_PATH environment variable required")?; - let data = vec![ + let map_data = vec![ "events_map".to_string(), "veth_identity_map".to_string(), "TcpPacketRegistry".to_string(), + "Blocklist".to_string(), ]; - match init_bpf_maps(bpf.clone(), data) { + match init_bpf_maps(bpf.clone(), map_data) { std::result::Result::Ok(bpf_maps) => { info!("Successfully loaded bpf maps"); let pin_path = std::path::PathBuf::from(&bpf_map_save_path); @@ -212,13 +213,13 @@ async fn event_listener( // create the PerfEventArrays and the buffers for map in bpf_maps { debug!("Debugging map type:{:?}", map); - let perf_event_array = PerfEventArray::try_from(map).map_err(|e| { - error!("Cannot create perf_event_array for map.Reason: {}", e); - anyhow::anyhow!("Cannot create perf_event_array for map.Reason: {}", e) - })?; - perf_event_arrays.push(perf_event_array); // this is step 1 - let perf_event_array_buffer = Vec::new(); - event_buffers.push(perf_event_array_buffer); //this is step 2 + if let std::result::Result::Ok(perf_event_array) = PerfEventArray::try_from(map) { + perf_event_arrays.push(perf_event_array); // this is step 1 + let perf_event_array_buffer = Vec::new(); + event_buffers.push(perf_event_array_buffer); //this is step 2 + } else { + warn!("Map is not a PerfEventArray, skipping load"); + } } // fill the input buffers with data from the PerfEventArrays diff --git a/core/src/components/metrics/src/helpers.rs b/core/src/components/metrics/src/helpers.rs index f519c7ea..a67b6074 100644 --- a/core/src/components/metrics/src/helpers.rs +++ b/core/src/components/metrics/src/helpers.rs @@ -10,7 +10,7 @@ use std::sync::{ }; use tokio::signal; -use tracing::{debug, error, info}; +use tracing::{debug, error, info, warn}; use crate::structs::NetworkMetrics; use crate::structs::TimeStampMetrics; @@ -128,13 +128,13 @@ pub async fn event_listener(bpf_maps: Vec) -> Result<(), anyhow::Error> { info!("Creating perf buffers..."); for map in bpf_maps { debug!("Debugging map type:{:?}", map); - let perf_event_array = PerfEventArray::try_from(map).map_err(|e| { - error!("Cannot create perf_event_array for map.Reason: {}", e); - anyhow::anyhow!("Cannot create perf_event_array for map.Reason: {}", e) - })?; - perf_event_arrays.push(perf_event_array); // this is step 1 - let perf_event_array_buffer = Vec::new(); - event_buffers.push(perf_event_array_buffer); //this is step 2 + if let std::result::Result::Ok(perf_event_array) = PerfEventArray::try_from(map) { + perf_event_arrays.push(perf_event_array); // this is step 1 + let perf_event_array_buffer = Vec::new(); + event_buffers.push(perf_event_array_buffer); //this is step 2 + } else { + warn!("Map is not a PerfEventArray, skipping load"); + } } let cpu_count = online_cpus().map_err(|e| anyhow::anyhow!("Error {:?}", e))?; diff --git a/core/src/components/metrics/src/main.rs b/core/src/components/metrics/src/main.rs index 9648e8a2..e8677fb9 100644 --- a/core/src/components/metrics/src/main.rs +++ b/core/src/components/metrics/src/main.rs @@ -54,9 +54,9 @@ async fn main() -> Result<(), anyhow::Error> { "An error occured during the execution of load_program function", )?; - load_program(tcp_bpf,"tcp_connect","tcp_v4_connect") + load_program(tcp_bpf,"tcp_v4_connect","tcp_v4_connect") .context("An error occured during the execution of load_and_attach_tcp_programs function")?; - load_program(tcp_v6_bpf,"tcp_connect","tcp_v6_connect") + load_program(tcp_v6_bpf,"tcp_v6_connect","tcp_v6_connect") .context("An error occured during the execution of load_and_attach_tcp_programs function")?; load_program( diff --git a/core/src/components/metrics_tracer/src/main.rs b/core/src/components/metrics_tracer/src/main.rs index 2f5e5a14..216a6aca 100644 --- a/core/src/components/metrics_tracer/src/main.rs +++ b/core/src/components/metrics_tracer/src/main.rs @@ -78,7 +78,13 @@ fn try_metrics_tracer(ctx: ProbeContext) -> Result { // Monitor on tcp_sendmsg, tcp_v4_connect #[kprobe] -fn tcp_connect(ctx: ProbeContext) -> u32 { +fn tcp_v6_connect(ctx: ProbeContext) -> u32 { + match on_connect(ctx) { Ok(_) => 0, Err(e) => e as u32 } +} + +// Monitor on tcp_sendmsg, tcp_v4_connect +#[kprobe] +fn tcp_v4_connect(ctx: ProbeContext) -> u32 { match on_connect(ctx) { Ok(_) => 0, Err(e) => e as u32 } } diff --git a/core/src/testing/identity.yaml b/core/src/testing/identity.yaml index 1b77d607..38bf1978 100644 --- a/core/src/testing/identity.yaml +++ b/core/src/testing/identity.yaml @@ -53,7 +53,7 @@ spec: - SYS_PTRACE containers: - name: identity - image: lorenzotettamanti/cortexflow-identity:0.1.5-refcount7 + image: lorenzotettamanti/cortexflow-identity:0.1.5-refcount9 command: ["/bin/bash", "-c"] args: - | diff --git a/core/src/testing/metrics.yaml b/core/src/testing/metrics.yaml index 1c1ecf83..262b28f7 100644 --- a/core/src/testing/metrics.yaml +++ b/core/src/testing/metrics.yaml @@ -19,7 +19,7 @@ spec: hostNetwork: true containers: - name: metrics - image: lorenzotettamanti/cortexflow-metrics:0.1.2-test8 + image: lorenzotettamanti/cortexflow-metrics:0.1.2-test12 command: ["/bin/bash", "-c"] args: - | From 2bd44b6da99188d7086141f1ded53b77a56c085e Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Sun, 25 Jan 2026 15:38:51 +0100 Subject: [PATCH 07/24] [#174]: added open telemetry (otel) logger for logs. Added otel daemonset with otel agent and collector --- core/common/src/logger.rs | 46 ++++- core/src/components/identity/src/helpers.rs | 107 ++++++++-- core/src/components/identity/src/main.rs | 8 +- core/src/testing/otel_agent.yaml | 210 ++++++++++++++++++++ 4 files changed, 345 insertions(+), 26 deletions(-) create mode 100644 core/src/testing/otel_agent.yaml diff --git a/core/common/src/logger.rs b/core/common/src/logger.rs index 5a1b8906..9dd0646e 100644 --- a/core/common/src/logger.rs +++ b/core/common/src/logger.rs @@ -1,4 +1,7 @@ -use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter}; +use tracing_subscriber::Layer; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::util::SubscriberInitExt; +use tracing_subscriber::{EnvFilter, fmt::format::FmtSpan}; /// Initialize the default logger configuration used across CortexBrain components. /// @@ -35,3 +38,44 @@ pub fn init_logger_without_time() { .with_line_number(false) .init(); } + +use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge; +use opentelemetry_otlp::{LogExporter, WithExportConfig}; +use opentelemetry_sdk::Resource; +use opentelemetry_sdk::logs::SdkLoggerProvider; + +pub fn otlp_logger_init(service_name: String) -> SdkLoggerProvider { + //exporter and provider initialization + let otlp_endpoint = std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT") + .unwrap_or_else(|_| "http://localhost:4317".to_string()); + + let exporter = LogExporter::builder() + .with_tonic() + .with_endpoint(otlp_endpoint) + .build() + .expect("Failed to create OTLP exporter"); + + //needs a service name + let provider = SdkLoggerProvider::builder() + .with_resource(Resource::builder().with_service_name(service_name).build()) + .with_batch_exporter(exporter) + .build(); + + //maybe we will need some filter later + //init otel_filter and layer + let otel_layer = OpenTelemetryTracingBridge::new(&provider); + + // init fmt filter and layer + let fmt_filter = EnvFilter::new("info").add_directive("opentelemetry=debug".parse().unwrap()); + let fmt_layer = tracing_subscriber::fmt::layer() + .with_thread_names(true) + .with_filter(fmt_filter); + + //init tracing subscriber with otel layer + tracing_subscriber::registry() + .with(otel_layer) + .with(fmt_layer) + .init(); + + provider +} diff --git a/core/src/components/identity/src/helpers.rs b/core/src/components/identity/src/helpers.rs index 95127893..2ca66060 100644 --- a/core/src/components/identity/src/helpers.rs +++ b/core/src/components/identity/src/helpers.rs @@ -26,7 +26,7 @@ use std::{ }, }; use tokio::time; -use tracing::{debug, error, info, warn}; +use tracing::{debug, error, event, info, span, warn}; /* * TryFrom Trait implementation for IpProtocols enum @@ -106,7 +106,6 @@ pub fn reverse_be_addr(addr: u32) -> Ipv4Addr { pub async fn display_veth_events>( bpf: Arc>, mut perf_buffers: Vec>, - //running: Arc, mut buffers: Vec, mut link_ids: Arc>>, ) { @@ -115,15 +114,36 @@ pub async fn display_veth_events>( for buf in perf_buffers.iter_mut() { match buf.read_events(&mut buffers) { std::result::Result::Ok(events) => { - for i in 0..events.read { + // debug: log the readed events + if events.read > 0 { + info!("Read {} veth events", events.read); + } + // debug: log the lost events + if events.lost > 0 { + warn!("Lost {} veth events", events.lost); + } + let offset = 0 ; + for i in offset..events.read { let data = &buffers[i]; + // error: data is smaller that the vethlog structure + if data.len() < std::mem::size_of::() { + warn!( + "Corrupted data. data_len = {} data_ptr = {}. Min size required: {} bytes", + data.len(), + data.as_ptr() as usize, + std::mem::size_of::() + ); + continue; + } + // correct size: data is logged correctly if data.len() >= std::mem::size_of::() { let vethlog: VethLog = - unsafe { std::ptr::read(data.as_ptr() as *const _) }; + unsafe { std::ptr::read_unaligned(data.as_ptr() as *const _) }; + //TODO: can this pattern be safe instead of using unsafe? let name_bytes = vethlog.name; - let dev_addr_bytes = vethlog.dev_addr.to_vec(); + let dev_addr_bytes = vethlog.dev_addr; let name = std::str::from_utf8(&name_bytes); let state = vethlog.state; @@ -141,13 +161,17 @@ pub async fn display_veth_events>( } match name { std::result::Result::Ok(veth_name) => { - info!( - "[{}] Triggered action: register_netdevice event_type:{:?} Manipulated veth: {:?} state:{:?} dev_addr:{:?}", + //TODO: create a span for this events, then enter the span, log the events and close the span + let veth_span = span!(tracing::Level::INFO, "veth_event", veth_name = %veth_name.trim_end_matches("\0"), event_type = %event_type.as_str()); + let _enter = veth_span.enter(); + event!( + tracing::Level::INFO, + "[{}] Veth Event: Type: {} Name: {} Dev_addr: {:x?} State: {}", netns, event_type, - veth_name.trim_end_matches("\0").to_string(), - state, - dev_addr + veth_name.trim_end_matches("\0"), + dev_addr, + state ); match attach_detach_veth( bpf.clone(), @@ -158,18 +182,34 @@ pub async fn display_veth_events>( .await { std::result::Result::Ok(_) => { - info!("Attach/Detach veth function attached correctly"); + //info!("Attach/Detach veth function attached correctly"); + event!( + tracing::Level::INFO, + "[{}] Successfully attached Attach/Detach function for veth: {}", + netns, + veth_name.trim_end_matches("\0") + ); + } + Err(e) => + //error!( + // "Error attaching Attach/Detach function. Error : {}", + // e + //), + { + event!( + tracing::Level::ERROR, + "[{}] Error attaching Attach/Detach function. Error : {}", + netns, + e + ) } - Err(e) => error!( - "Error attaching Attach/Detach function. Error : {}", - e - ), } } - Err(_) => info!("Unknown name or corrupted field"), + Err(_) => { + //info!("Unknown name or corrupted field") + event!(tracing::Level::WARN, "Corrupted veth name field"); + } } - } else { - warn!("Corrupted data"); } } } @@ -280,6 +320,7 @@ pub async fn display_tcp_registry_events>( if data.len() >= std::mem::size_of::() { let tcp_pl: TcpPacketRegistry = unsafe { std::ptr::read(data.as_ptr() as *const _) }; + //TODO: can this pattern be safe? let src = reverse_be_addr(tcp_pl.src_ip); let dst = reverse_be_addr(tcp_pl.dst_ip); let src_port = u16::from_be(tcp_pl.src_port); @@ -295,7 +336,10 @@ pub async fn display_tcp_registry_events>( match IpProtocols::try_from(tcp_pl.proto) { std::result::Result::Ok(proto) => { - info!( + let tcp_events_span = span!(tracing::Level::INFO, "tcp_registry_event", command = %command_str.as_str(), cgroup_id = %cgroup_id); + let _enter = tcp_events_span.enter(); + event!( + tracing::Level::INFO, "Event Id: {} Protocol: {:?} SRC: {}:{} -> DST: {}:{} Command: {} Cgroup_id: {}", event_id, proto, @@ -306,12 +350,31 @@ pub async fn display_tcp_registry_events>( command_str, cgroup_id //proc_content ); + //info!( + // "Event Id: {} Protocol: {:?} SRC: {}:{} -> DST: {}:{} Command: {} Cgroup_id: {}", + // event_id, + // proto, + // src, + // src_port, + // dst, + // dst_port, + // command_str, + // cgroup_id //proc_content + //); } Err(_) => { - info!( - "Event Id: {} Protocol: Unknown ({})", - event_id, tcp_pl.proto + event!( + tracing::Level::INFO, + "Event Id: {} Protocol: Unknown ({}) Command: {} Cgroup_id: {}", + event_id, + tcp_pl.proto, + command_str, + cgroup_id ); + //info!( + // "Event Id: {} Protocol: Unknown ({})", + // event_id, tcp_pl.proto + //); } }; } else { diff --git a/core/src/components/identity/src/main.rs b/core/src/components/identity/src/main.rs index 56f81d68..829fdedd 100644 --- a/core/src/components/identity/src/main.rs +++ b/core/src/components/identity/src/main.rs @@ -18,7 +18,7 @@ use crate::helpers::{ use aya::{ Ebpf, maps::{Map, perf::PerfEventArray}, - programs::{SchedClassifier, TcAttachType, tc::SchedClassifierLinkId}, + programs::{KProbe, SchedClassifier, TcAttachType, tc::SchedClassifierLinkId}, util::online_cpus, }; @@ -44,7 +44,8 @@ use std::collections::HashMap; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { //init tracing subscriber - logger::init_default_logger(); + //logger::init_default_logger(); + let otlp_provider = logger::otlp_logger_init("identity_service-OTLP".to_string()); info!("Starting identity service..."); info!("fetching data"); @@ -115,6 +116,7 @@ async fn main() -> Result<(), anyhow::Error> { Err(e) => { error!("Error while loading bpf maps {}", e); let _ = signal::ctrl_c().await; + let _ = otlp_provider.shutdown(); } } @@ -248,7 +250,7 @@ async fn event_listener( .expect("Cannot create tcp_registry buffer"); // init output buffers - let veth_buffers = vec![BytesMut::with_capacity(1024); 10]; + let veth_buffers = vec![BytesMut::with_capacity(1024); online_cpus().iter().len()]; let events_buffers = vec![BytesMut::with_capacity(1024); online_cpus().iter().len()]; let tcp_buffers = vec![BytesMut::with_capacity(1024); online_cpus().iter().len()]; diff --git a/core/src/testing/otel_agent.yaml b/core/src/testing/otel_agent.yaml new file mode 100644 index 00000000..71b7e08c --- /dev/null +++ b/core/src/testing/otel_agent.yaml @@ -0,0 +1,210 @@ +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: otel-agent-conf + namespace: cortexflow + labels: + app: opentelemetry + component: otel-agent-conf +data: + otel-agent-config: | + receivers: + otlp: + protocols: + grpc: + endpoint: 0.0.0.0:4317 + http: + endpoint: 0.0.0.0:4318 + + exporters: + otlp: + endpoint: otel-collector.cortexflow.svc.cluster.local:4317 + tls: + insecure: true + logging: + loglevel: info + + service: + pipelines: + traces: + receivers: [otlp] + exporters: [otlp, logging] + logs: + receivers: [otlp] + exporters: [otlp, logging] + +--- +apiVersion: apps/v1 +kind: DaemonSet +metadata: + name: otel-agent + namespace: cortexflow + labels: + app: opentelemetry + component: otel-agent +spec: + selector: + matchLabels: + app: opentelemetry + component: otel-agent + template: + metadata: + labels: + app: opentelemetry + component: otel-agent + spec: + hostNetwork: true + dnsPolicy: ClusterFirstWithHostNet + containers: + - name: otel-agent + image: otel/opentelemetry-collector:0.95.0 + command: + - "/otelcol" + - "--config=/conf/otel-agent-config.yaml" + resources: + limits: + cpu: 500m + memory: 500Mi + requests: + cpu: 100m + memory: 100Mi + ports: + - containerPort: 4317 + hostPort: 4317 + protocol: TCP + - containerPort: 4318 + hostPort: 4318 + protocol: TCP + env: + - name: GOMEMLIMIT + value: 400MiB + volumeMounts: + - name: otel-agent-config-vol + mountPath: /conf + volumes: + - name: otel-agent-config-vol + configMap: + name: otel-agent-conf + items: + - key: otel-agent-config + path: otel-agent-config.yaml + +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: otel-collector-conf + namespace: cortexflow + labels: + app: opentelemetry + component: otel-collector-conf +data: + otel-collector-config: | + receivers: + otlp: + protocols: + grpc: + endpoint: 0.0.0.0:4317 + http: + endpoint: 0.0.0.0:4318 + + processors: + memory_limiter: + limit_mib: 1500 + spike_limit_mib: 512 + check_interval: 5s + + exporters: + # otlp: + # endpoint: otel-collector.cortexflow.svc.cluster.local:4317 + # tls: + # insecure: true + logging: {} + + service: + pipelines: + traces: + receivers: [otlp] + processors: [memory_limiter] + exporters: [logging] + logs: + receivers: [otlp] + processors: [memory_limiter] + exporters: [logging] + +--- +apiVersion: v1 +kind: Service +metadata: + name: otel-collector + namespace: cortexflow + labels: + app: opentelemetry + component: otel-collector +spec: + selector: + app: opentelemetry + component: otel-collector + ports: + - name: otlp-grpc + port: 4317 + targetPort: 4317 + - name: otlp-http + port: 4318 + targetPort: 4318 + - name: metrics + port: 8888 + targetPort: 8888 + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: otel-collector + namespace: cortexflow + labels: + app: opentelemetry + component: otel-collector +spec: + replicas: 1 + selector: + matchLabels: + app: opentelemetry + component: otel-collector + template: + metadata: + labels: + app: opentelemetry + component: otel-collector + spec: + containers: + - name: otel-collector + image: otel/opentelemetry-collector:0.95.0 + command: + - "/otelcol" + - "--config=/conf/otel-collector-config.yaml" + resources: + limits: + cpu: "1" + memory: 2Gi + requests: + cpu: 200m + memory: 400Mi + ports: + - containerPort: 4317 + - containerPort: 4318 + - containerPort: 8888 + env: + - name: GOMEMLIMIT + value: 1600MiB + volumeMounts: + - name: otel-collector-config-vol + mountPath: /conf + volumes: + - name: otel-collector-config-vol + configMap: + name: otel-collector-conf + items: + - key: otel-collector-config + path: otel-collector-config.yaml \ No newline at end of file From cd4687fc3d076f9e558914a924932bb6dcde4eb7 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Sun, 25 Jan 2026 15:40:46 +0100 Subject: [PATCH 08/24] [#158]: improved docs for the conntracker data structures. removed useless conversion from u8 to 64 with .into() for state variable --- .../conntracker/src/data_structures.rs | 17 +++++++++-------- .../components/conntracker/src/veth_tracer.rs | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/core/src/components/conntracker/src/data_structures.rs b/core/src/components/conntracker/src/data_structures.rs index 4de05cca..2a7f1c0c 100644 --- a/core/src/components/conntracker/src/data_structures.rs +++ b/core/src/components/conntracker/src/data_structures.rs @@ -50,17 +50,18 @@ pub struct ConnArray { // #[repr(C)] -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy)] pub struct VethLog { - pub name: [u8; 16], - pub state: u64, // state var type: long unsigned int - pub dev_addr: [u32; 8], - pub event_type: u8, // i choose 1 for veth creation or 2 for veth destruction - pub netns: u32, - pub pid: u32 - + pub name: [u8; 16], // 16 bytes: veth interface name + pub state: u64, // 8 bytes: state variable (unsigned long in kernel) + pub dev_addr: [u32; 8], // 32 bytes: device address + pub event_type: u8, // 1 byte: 1 for veth creation, 2 for veth destruction + pub netns: u32, // 4 bytes: network namespace inode number + pub pid: u32, // 4 bytes: PID that triggered the event + // padding automatically added by Rust for alignment } + // TODO: write documentation about this structure #[repr(C)] #[derive(Clone,Copy,Debug)] diff --git a/core/src/components/conntracker/src/veth_tracer.rs b/core/src/components/conntracker/src/veth_tracer.rs index e2f07e7c..146b66de 100644 --- a/core/src/components/conntracker/src/veth_tracer.rs +++ b/core/src/components/conntracker/src/veth_tracer.rs @@ -35,7 +35,7 @@ pub fn try_veth_tracer(ctx: ProbeContext, mode: u8) -> Result { // state field let state_offset = 168; - let state: u8 = read_linux_inner_value::(net_device_pointer as *const u8, state_offset)?; + let state: u64 = read_linux_inner_value::(net_device_pointer as *const u8, state_offset)?; // dev_addr let dev_addr_offset = 1080; @@ -52,7 +52,7 @@ pub fn try_veth_tracer(ctx: ProbeContext, mode: u8) -> Result { // compose the structure let veth_data = VethLog { name: name_buf, - state: state.into(), + state: state, dev_addr: dev_addr_buf, event_type: mode, netns: inum, From b21a58a5dbaf4d23ac04975547d71f2188b9ce4b Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Sun, 25 Jan 2026 15:42:42 +0100 Subject: [PATCH 09/24] [#174]: Added otel libraries and features in the common crate. .update identity kubernetes manifest with the otel env variables. --- core/Cargo.lock | 535 +++++++++++++++++++++++++++++++++ core/common/Cargo.toml | 9 +- core/src/testing/identity.yaml | 13 +- 3 files changed, 553 insertions(+), 4 deletions(-) diff --git a/core/Cargo.lock b/core/Cargo.lock index e980659b..e00948ba 100644 --- a/core/Cargo.lock +++ b/core/Cargo.lock @@ -409,6 +409,11 @@ dependencies = [ "aya", "k8s-openapi", "kube", + "opentelemetry", + "opentelemetry-appender-tracing", + "opentelemetry-otlp", + "opentelemetry-stdout", + "opentelemetry_sdk", "tracing", "tracing-subscriber", ] @@ -544,6 +549,17 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "either" version = "1.15.0" @@ -641,12 +657,34 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "futures-sink" version = "0.3.31" @@ -668,6 +706,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-io", + "futures-macro", "futures-sink", "futures-task", "memchr", @@ -866,6 +905,7 @@ version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c6995591a8f1380fcb4ba966a252a4b29188d51d2b89e3a252f5305be65aea8" dependencies = [ + "base64", "bytes", "futures-channel", "futures-core", @@ -873,7 +913,9 @@ dependencies = [ "http", "http-body", "hyper", + "ipnet", "libc", + "percent-encoding", "pin-project-lite", "socket2", "tokio", @@ -905,6 +947,108 @@ dependencies = [ "cc", ] +[[package]] +name = "icu_collections" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6b649701667bbe825c3b7e6388cb521c23d88644678e83c0c4d0a621a34b43" +dependencies = [ + "displaydoc", + "potential_utf", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edba7861004dd3714265b4db54a3c390e880ab658fec5f7db895fae2046b5bb6" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f6c8828b67bf8908d82127b2054ea1b4427ff0230ee9141c54251934ab1b599" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7aedcccd01fc5fe81e6b489c15b247b8b0690feb23304303a9e560f37efc560a" + +[[package]] +name = "icu_properties" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "020bfc02fe870ec3a66d93e677ccca0562506e5872c650f893269e08615d74ec" +dependencies = [ + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "616c294cf8d725c6afcd8f55abc17c56464ef6211f9ed59cccffe534129c77af" + +[[package]] +name = "icu_provider" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85962cf0ce02e1e0a629cc34e7ca3e373ce20dda4c4d7294bbd0bf1fdb59e614" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acae9609540aa318d1bc588455225fb2085b9ed0c4f6bd0d9d5bcd86f1a0344" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + [[package]] name = "indexmap" version = "2.12.0" @@ -915,6 +1059,22 @@ dependencies = [ "hashbrown 0.16.0", ] +[[package]] +name = "ipnet" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" + +[[package]] +name = "iri-string" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c91338f0783edbd6195decb37bae672fd3b165faffb89bf7b9e6942f8b1a731a" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "itertools" version = "0.14.0" @@ -1047,6 +1207,12 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" +[[package]] +name = "litemap" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77" + [[package]] name = "lock_api" version = "0.4.14" @@ -1218,6 +1384,105 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" +[[package]] +name = "opentelemetry" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b84bcd6ae87133e903af7ef497404dda70c60d0ea14895fc8a5e6722754fc2a0" +dependencies = [ + "futures-core", + "futures-sink", + "js-sys", + "pin-project-lite", + "thiserror 2.0.17", + "tracing", +] + +[[package]] +name = "opentelemetry-appender-tracing" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef6a1ac5ca3accf562b8c306fa8483c85f4390f768185ab775f242f7fe8fdcc2" +dependencies = [ + "opentelemetry", + "tracing", + "tracing-core", + "tracing-subscriber", +] + +[[package]] +name = "opentelemetry-http" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7a6d09a73194e6b66df7c8f1b680f156d916a1a942abf2de06823dd02b7855d" +dependencies = [ + "async-trait", + "bytes", + "http", + "opentelemetry", + "reqwest", +] + +[[package]] +name = "opentelemetry-otlp" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2366db2dca4d2ad033cad11e6ee42844fd727007af5ad04a1730f4cb8163bf" +dependencies = [ + "http", + "opentelemetry", + "opentelemetry-http", + "opentelemetry-proto", + "opentelemetry_sdk", + "prost", + "reqwest", + "thiserror 2.0.17", + "tokio", + "tonic", + "tracing", +] + +[[package]] +name = "opentelemetry-proto" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7175df06de5eaee9909d4805a3d07e28bb752c34cab57fa9cff549da596b30f" +dependencies = [ + "opentelemetry", + "opentelemetry_sdk", + "prost", + "tonic", + "tonic-prost", +] + +[[package]] +name = "opentelemetry-stdout" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc8887887e169414f637b18751487cce4e095be787d23fad13c454e2fb1b3811" +dependencies = [ + "chrono", + "opentelemetry", + "opentelemetry_sdk", +] + +[[package]] +name = "opentelemetry_sdk" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e14ae4f5991976fd48df6d843de219ca6d31b01daaab2dad5af2badeded372bd" +dependencies = [ + "futures-channel", + "futures-executor", + "futures-util", + "opentelemetry", + "percent-encoding", + "rand", + "thiserror 2.0.17", + "tokio", + "tokio-stream", +] + [[package]] name = "ordered-float" version = "2.10.1" @@ -1351,6 +1616,24 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "potential_utf" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b73949432f5e2a09657003c25bca5e19a0e9c84f8058ca374f49e0ebe605af77" +dependencies = [ + "zerovec", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + [[package]] name = "prettyplease" version = "0.2.37" @@ -1471,6 +1754,35 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +[[package]] +name = "rand" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + [[package]] name = "redox_syscall" version = "0.5.18" @@ -1509,6 +1821,40 @@ version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" +[[package]] +name = "reqwest" +version = "0.12.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d0946410b9f7b082a427e4ef5c8ff541a88b357bc6c637c40db3a68ac70a36f" +dependencies = [ + "base64", + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "js-sys", + "log", + "percent-encoding", + "pin-project-lite", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "ring" version = "0.17.14" @@ -1705,6 +2051,18 @@ dependencies = [ "serde_core", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_yaml" version = "0.9.34+deprecated" @@ -1775,6 +2133,12 @@ dependencies = [ "windows-sys 0.60.2", ] +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + [[package]] name = "subtle" version = "2.6.1" @@ -1797,6 +2161,20 @@ name = "sync_wrapper" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "tempfile" @@ -1860,6 +2238,16 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "tinystr" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42d3e9c45c09de15d06dd8acf5f4e0e399e85927b7f00711024eb7ae10fa4869" +dependencies = [ + "displaydoc", + "zerovec", +] + [[package]] name = "tokio" version = "1.48.0" @@ -2032,10 +2420,13 @@ dependencies = [ "base64", "bitflags", "bytes", + "futures-util", "http", "http-body", + "iri-string", "mime", "pin-project-lite", + "tower", "tower-layer", "tower-service", "tracing", @@ -2157,6 +2548,24 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + [[package]] name = "valuable" version = "0.1.1" @@ -2206,6 +2615,19 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "551f88106c6d5e7ccc7cd9a16f312dd3b5d36ea8b4954304657d5dfba115d4a0" +dependencies = [ + "cfg-if", + "js-sys", + "once_cell", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.105" @@ -2238,6 +2660,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "web-sys" +version = "0.3.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a1f95c0d03a47f4ae1f7a64643a6bb97465d9b740f0fa8f90ea33915c99a9a1" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "which" version = "7.0.3" @@ -2477,8 +2909,111 @@ version = "0.46.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" +[[package]] +name = "writeable" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9" + +[[package]] +name = "yoke" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72d6e5c6afb84d73944e5cedb052c4680d5657337201555f9f2a16b7406d4954" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerocopy" +version = "0.8.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "668f5168d10b9ee831de31933dc111a459c97ec93225beb307aed970d1372dfd" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c7962b26b0a8685668b671ee4b54d007a67d4eaf05fda79ac0ecf41e32270f1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zerofrom" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + [[package]] name = "zeroize" version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" + +[[package]] +name = "zerotrie" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a59c17a5562d507e4b54960e8569ebee33bee890c70aa3fe7b97e85a9fd7851" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c28719294829477f525be0186d13efa9a3c602f7ec202ca9e353d310fb9a002" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/core/common/Cargo.toml b/core/common/Cargo.toml index 854c04e5..eb94abec 100644 --- a/core/common/Cargo.toml +++ b/core/common/Cargo.toml @@ -10,13 +10,18 @@ homepage = "https://docs.cortexflow.org" repository = "https://github.com/CortexFlow/CortexBrain" [dependencies] -tracing = "0.1" +tracing = { version = "0.1", features = ["std"] } tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } anyhow = "1.0" kube = { version = "2.0.1", features = ["client"] } k8s-openapi = { version = "0.26.0", features = ["v1_34"] } aya = "0.13.1" +opentelemetry = "0.31.0" +opentelemetry_sdk = { version = "0.31.0", features = ["logs", "rt-tokio"] } +opentelemetry-stdout = { version = "0.31.0", features = ["logs"] } +opentelemetry-appender-tracing = "0.31.1" +opentelemetry-otlp = { version = "0.31.0", features = ["logs", "grpc-tonic"] } [features] map-handlers = [] -program-handlers = [] +program-handlers = [] \ No newline at end of file diff --git a/core/src/testing/identity.yaml b/core/src/testing/identity.yaml index 38bf1978..43e6c928 100644 --- a/core/src/testing/identity.yaml +++ b/core/src/testing/identity.yaml @@ -28,7 +28,6 @@ spec: echo "checking permissions" ls -ld /sys/fs/bpf - volumeMounts: - name: bpf mountPath: /sys/fs/bpf @@ -53,7 +52,7 @@ spec: - SYS_PTRACE containers: - name: identity - image: lorenzotettamanti/cortexflow-identity:0.1.5-refcount9 + image: ghcr.io/cortexflow/identity:latest command: ["/bin/bash", "-c"] args: - | @@ -70,6 +69,16 @@ spec: echo "Running application..." exec /usr/local/bin/cortexflow-identity-service || echo "Application exited with code $?" + env: + - name: OTEL_SERVICE_NAME + value: cortexflow-identity + - name: OTEL_EXPORTER_OTLP_ENDPOINT + value: http://localhost:4317 + - name: OTEL_EXPORTER_OTLP_PROTOCOL + value: grpc + - name: OTEL_RESOURCE_ATTRIBUTES + value: service.namespace=cortexflow,service.version=0.1.5 + resources: limits: cpu: "1" From 69863bb1347d7bc00447dfdd78d586350a75991a Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Sun, 25 Jan 2026 15:43:25 +0100 Subject: [PATCH 10/24] [#158]: imroved documentation in the user space for the identity (VethLog) data structure --- core/src/components/identity/src/structs.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/core/src/components/identity/src/structs.rs b/core/src/components/identity/src/structs.rs index 7e2aa2b0..766a7768 100644 --- a/core/src/components/identity/src/structs.rs +++ b/core/src/components/identity/src/structs.rs @@ -34,14 +34,15 @@ unsafe impl aya::Pod for PacketLog {} #[repr(C)] #[derive(Clone, Copy)] pub struct VethLog { - pub name: [u8; 16], - pub state: u64, - pub dev_addr: [u32; 8], - pub event_type: u8, - pub netns: u32, - pub pid: u32, + pub name: [u8; 16], // 16 bytes: veth interface name + pub state: u64, // 8 bytes: state variable (unsigned long in kernel) + pub dev_addr: [u32; 8], // 32 bytes: device address + pub event_type: u8, // 1 byte: 1 for veth creation, 2 for veth destruction + pub netns: u32, // 4 bytes: network namespace inode number + pub pid: u32, // 4 bytes: PID that triggered the event } + #[repr(C)] #[derive(Clone, Copy)] pub struct TcpPacketRegistry { From af85614fab184b6d9a482f4c71ab58c0d4505a5e Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Mon, 26 Jan 2026 13:10:19 +0100 Subject: [PATCH 11/24] [#158]: restored blocklist map initialization --- core/common/src/map_handlers.rs | 13 ++++++++++--- .../components/conntracker/src/data_structures.rs | 2 +- core/src/components/identity/src/main.rs | 8 ++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/core/common/src/map_handlers.rs b/core/common/src/map_handlers.rs index 2e22736a..0bbf606c 100644 --- a/core/common/src/map_handlers.rs +++ b/core/common/src/map_handlers.rs @@ -86,16 +86,23 @@ pub fn map_pinner(maps: BpfMapsData, path: &PathBuf) -> Result, Error> Ok(owned_maps) } +use aya::maps::MapData; #[cfg(feature = "map-handlers")] -pub async fn populate_blocklist(map: &mut Map) -> Result<(), Error> { +pub async fn populate_blocklist() -> Result<(), Error> { + // load mapdata from path + + let mapdata = MapData::from_pin("/sys/fs/bpf/maps/Blocklist") + .map_err(|e| anyhow::anyhow!("Failed to load blocklist_map: {}", e))?; + + let map = Map::HashMap(mapdata); + let mut blocklist_map = HashMap::<_, [u8; 4], [u8; 4]>::try_from(map)?; + let client = Client::try_default() .await .expect("Cannot connect to Kubernetes Client"); let namespace = "cortexflow"; let configmap = "cortexbrain-client-config"; - let mut blocklist_map = HashMap::<_, [u8; 4], [u8; 4]>::try_from(map)?; - let api: Api = Api::namespaced(client, namespace); match api.get(configmap).await { std::result::Result::Ok(configs) => { diff --git a/core/src/components/conntracker/src/data_structures.rs b/core/src/components/conntracker/src/data_structures.rs index 2a7f1c0c..41a9552b 100644 --- a/core/src/components/conntracker/src/data_structures.rs +++ b/core/src/components/conntracker/src/data_structures.rs @@ -104,7 +104,7 @@ pub static mut CONNTRACKER: LruPerCpuHashMap = #[map(name = "veth_identity_map")] pub static mut VETH_EVENTS: PerfEventArray = PerfEventArray::new(0); -#[map(name = "Blocklist")] +#[map(name = "Blocklist", pinning = "by_name")] pub static mut BLOCKLIST: HashMap<[u8;4], [u8;4]> = HashMap::<[u8;4], [u8;4]>::with_max_entries(1024, 0); //here i need to pass an address like this: [135,171,168,192] diff --git a/core/src/components/identity/src/main.rs b/core/src/components/identity/src/main.rs index 829fdedd..b477388b 100644 --- a/core/src/components/identity/src/main.rs +++ b/core/src/components/identity/src/main.rs @@ -18,7 +18,7 @@ use crate::helpers::{ use aya::{ Ebpf, maps::{Map, perf::PerfEventArray}, - programs::{KProbe, SchedClassifier, TcAttachType, tc::SchedClassifierLinkId}, + programs::{SchedClassifier, TcAttachType, tc::SchedClassifierLinkId}, util::online_cpus, }; @@ -87,9 +87,9 @@ async fn main() -> Result<(), anyhow::Error> { info!("Found interfaces: {:?}", interfaces); - //{ FIXME: paused for testing the other features - // populate_blocklist(&mut maps.2).await?; - //} + { + populate_blocklist().await?; + } { init_tc_classifier(bpf.clone(), interfaces, link_ids.clone()).await.context( From 1cbd9f544a2c2116b8d6ad9a1d48f35d2c4ea2fd Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Tue, 27 Jan 2026 14:04:16 +0100 Subject: [PATCH 12/24] [#158]: added better docs. Updated while true pattern with "loop" pattern. Code cleaning --- core/src/components/identity/src/helpers.rs | 216 +++++++++++++------- 1 file changed, 137 insertions(+), 79 deletions(-) diff --git a/core/src/components/identity/src/helpers.rs b/core/src/components/identity/src/helpers.rs index 2ca66060..a0aa72ea 100644 --- a/core/src/components/identity/src/helpers.rs +++ b/core/src/components/identity/src/helpers.rs @@ -1,38 +1,25 @@ -#![allow(warnings)] use crate::enums::IpProtocols; use crate::structs::{PacketLog, TcpPacketRegistry, VethLog}; -use anyhow::Error; + +use aya::Ebpf; use aya::programs::tc::SchedClassifierLinkId; use aya::{ - Bpf, maps::{MapData, perf::PerfEventArrayBuffer}, programs::{SchedClassifier, TcAttachType}, }; use bytes::BytesMut; -use k8s_openapi::api::core::v1::Pod; -use kube::api::ObjectList; -use kube::{Api, Client}; use nix::net::if_::if_nameindex; -use std::collections::HashMap; -use std::fs; -use std::result::Result::Ok; -use std::sync::Mutex; use std::{ - borrow::BorrowMut, - net::Ipv4Addr, - sync::{ - Arc, - atomic::{AtomicBool, Ordering}, - }, + borrow::BorrowMut, collections::HashMap, net::Ipv4Addr, result::Result::Ok, sync::Arc, + sync::Mutex, }; -use tokio::time; use tracing::{debug, error, event, info, span, warn}; -/* - * TryFrom Trait implementation for IpProtocols enum - * This is used to reconstruct the packet protocol based on the - * IPV4 Header Protocol code - */ +// +// TryFrom Trait implementation for IpProtocols enum +// This is used to reconstruct the packet protocol based on the +// IPV4 Header Protocol code +// impl TryFrom for IpProtocols { type Error = (); @@ -49,16 +36,35 @@ impl TryFrom for IpProtocols { /* helper functions to read and log net events in the container */ pub async fn display_events>( mut perf_buffers: Vec>, - //running: Arc, mut buffers: Vec, ) { // FIXME: here maybe we need to use a loop with tokio::select - while true { + loop { for buf in perf_buffers.iter_mut() { match buf.read_events(&mut buffers) { std::result::Result::Ok(events) => { - for i in 0..events.read { + let offset = 0 as usize; + if events.read > 0 { + debug!("Read {} events", events.read); + } + if events.lost > 0 { + debug!("Lost events: {}", events.lost); + } + for i in offset..events.read { let data = &buffers[i]; + if data.len() < std::mem::size_of::() { + let failed_events_span = + span!(tracing::Level::INFO, "corrupted_packets_events"); + let _enter: span::Entered<'_> = failed_events_span.enter(); + event!( + tracing::Level::WARN, + "Corrupted data. data_len = {} data_ptr = {}. Min size required: {} bytes", + data.len(), + data.as_ptr() as usize, + std::mem::size_of::() + ); + continue; + } if data.len() >= std::mem::size_of::() { let pl: PacketLog = unsafe { std::ptr::read(data.as_ptr() as *const _) }; @@ -70,16 +76,29 @@ pub async fn display_events>( match IpProtocols::try_from(pl.proto) { std::result::Result::Ok(proto) => { - info!( + let packets_events_span = span!(tracing::Level::INFO, "packets_event",event_id=%event_id, protocol = %format!("{:?}", proto)); + let _enter = packets_events_span.enter(); + event!( + tracing::Level::INFO, "Event Id: {} Protocol: {:?} SRC: {}:{} -> DST: {}:{}", - event_id, proto, src, src_port, dst, dst_port + event_id, + proto, + src, + src_port, + dst, + dst_port ); } - Err(_) => { - info!( - "Event Id: {} Protocol: Unknown ({})", - event_id, pl.proto - ); + Err(e) => { + let failed_packets_events_span = span!(tracing::Level::INFO, "failed_packets_event", event_id=%event_id, protocol = %pl.proto); + let _enter = failed_packets_events_span.enter(); + event!( + tracing::Level::INFO, + "Event Id: {} Protocol: Unknown ({}). Error: {:?}", + event_id, + pl.proto, + e + ) } }; } else { @@ -96,36 +115,44 @@ pub async fn display_events>( } } +// docs: +// This function perform a byte swap from little-endian to big-endian +// It's used to reconstruct the correct IPv4 address from the u32 representation +// +// Takes a u32 address in big-endian format and returns a Ipv4Addr with reversed octets +// pub fn reverse_be_addr(addr: u32) -> Ipv4Addr { - let mut octects = addr.to_be_bytes(); + let octects = addr.to_be_bytes(); let [a, b, c, d] = [octects[3], octects[2], octects[1], octects[0]]; let reversed_ip = Ipv4Addr::new(a, b, c, d); reversed_ip } pub async fn display_veth_events>( - bpf: Arc>, + bpf: Arc>, mut perf_buffers: Vec>, mut buffers: Vec, - mut link_ids: Arc>>, + link_ids: Arc>>, ) { // FIXME: here maybe we need to use a loop with tokio::select - while true { + loop { for buf in perf_buffers.iter_mut() { match buf.read_events(&mut buffers) { std::result::Result::Ok(events) => { // debug: log the readed events if events.read > 0 { - info!("Read {} veth events", events.read); + debug!("Read {} veth events", events.read); } // debug: log the lost events if events.lost > 0 { - warn!("Lost {} veth events", events.lost); + debug!("Lost {} veth events", events.lost); } - let offset = 0 ; + let offset = 0 as usize; for i in offset..events.read { let data = &buffers[i]; + let veth_events_span = span!(tracing::Level::INFO, "corrupted_veth_events"); // error: data is smaller that the vethlog structure + let _enter = veth_events_span.enter(); if data.len() < std::mem::size_of::() { warn!( "Corrupted data. data_len = {} data_ptr = {}. Min size required: {} bytes", @@ -161,9 +188,8 @@ pub async fn display_veth_events>( } match name { std::result::Result::Ok(veth_name) => { - //TODO: create a span for this events, then enter the span, log the events and close the span - let veth_span = span!(tracing::Level::INFO, "veth_event", veth_name = %veth_name.trim_end_matches("\0"), event_type = %event_type.as_str()); - let _enter = veth_span.enter(); + let veth_events_span = span!(tracing::Level::INFO, "veth_event", veth_name = %veth_name.trim_end_matches("\0"), event_type = %event_type.as_str()); + let _enter = veth_events_span.enter(); event!( tracing::Level::INFO, "[{}] Veth Event: Type: {} Name: {} Dev_addr: {:x?} State: {}", @@ -182,7 +208,6 @@ pub async fn display_veth_events>( .await { std::result::Result::Ok(_) => { - //info!("Attach/Detach veth function attached correctly"); event!( tracing::Level::INFO, "[{}] Successfully attached Attach/Detach function for veth: {}", @@ -190,12 +215,9 @@ pub async fn display_veth_events>( veth_name.trim_end_matches("\0") ); } - Err(e) => - //error!( - // "Error attaching Attach/Detach function. Error : {}", - // e - //), - { + Err(e) => { + let failed_veth_events_span = span!(tracing::Level::ERROR, "failed_veth_event_attach_detach", veth_name = %veth_name.trim_end_matches("\0")); + let _enter = failed_veth_events_span.enter(); event!( tracing::Level::ERROR, "[{}] Error attaching Attach/Detach function. Error : {}", @@ -205,9 +227,12 @@ pub async fn display_veth_events>( } } } - Err(_) => { - //info!("Unknown name or corrupted field") - event!(tracing::Level::WARN, "Corrupted veth name field"); + Err(e) => { + event!( + tracing::Level::WARN, + "Corrupted veth name field. Error: {:?}", + e + ); } } } @@ -222,12 +247,20 @@ pub async fn display_veth_events>( } } +// docs: +// This function checks if the given interface name is in the list of ignored interfaces +// Takes a interface name (iface) as &str and returns true if the interface should be ignored +// Typically we want to ignore eth0,docker0,tunl0,lo interfaces because they are not relevant for the internal monitoring +// pub fn ignore_iface(iface: &str) -> bool { let ignored_interfaces = ["eth0", "docker0", "tunl0", "lo"]; ignored_interfaces.contains(&iface) } -//filter the interfaces,exclude docker0,eth0,lo interfaces +// docs: +// This function retrieves the list of veth interfaces on the system, filtering out ignored interfaces with +// the ignore_iface function. +// pub fn get_veth_channels() -> Vec { //filter interfaces and save the output in the let mut interfaces: Vec = Vec::new(); @@ -247,7 +280,7 @@ pub fn get_veth_channels() -> Vec { } async fn attach_detach_veth( - bpf: Arc>, + bpf: Arc>, event_type: u8, iface: &str, link_ids: Arc>>, @@ -258,7 +291,13 @@ async fn attach_detach_veth( ); match event_type { 1 => { - let mut bpf = bpf.lock().unwrap(); + // + // EVENT_TYPE 1: Attach the program to the veth inferfaces + // + + let mut bpf = bpf + .lock() + .map_err(|e| anyhow::anyhow!("Cannot get value from lock : {}", e))?; let program: &mut SchedClassifier = bpf .program_mut("identity_classifier") .ok_or_else(|| anyhow::anyhow!("program 'identity_classifier' not found"))? @@ -271,7 +310,9 @@ async fn attach_detach_veth( return Ok(()); } - let mut link_ids = link_ids.lock().unwrap(); + let mut link_ids = link_ids + .lock() + .map_err(|e| anyhow::anyhow!("Cannot get value from lock when attaching: {}", e))?; match program.attach(iface, TcAttachType::Ingress) { std::result::Result::Ok(link_id) => { info!( @@ -284,8 +325,14 @@ async fn attach_detach_veth( } } 2 => { + // + // EVENT_TYPE 2: Detach the program from the veth interfaces // INFO: Detaching occurs automatically when veth is deleted by kernel itself - let mut link_ids = link_ids.lock().unwrap(); + // + + let mut link_ids = link_ids + .lock() + .map_err(|e| anyhow::anyhow!("Cannot get value from lock when detaching: {}", e))?; match link_ids.remove(iface) { Some(_) => { info!("Successfully detached program from interface {}", iface); @@ -303,20 +350,32 @@ async fn attach_detach_veth( Ok(()) } -// CHECK THIS DIR: /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-besteffort.slice /* helper functions to display events from the TcpPacketRegistry structure */ pub async fn display_tcp_registry_events>( mut perf_buffers: Vec>, - //running: Arc, mut buffers: Vec, ) { // FIXME: here maybe we need to use a loop with tokio::select - while true { + loop { for buf in perf_buffers.iter_mut() { match buf.read_events(&mut buffers) { std::result::Result::Ok(events) => { - for i in 0..events.read { + let offset = 0; + for i in offset..events.read { let data = &buffers[i]; + if data.len() < std::mem::size_of::() { + let failed_tcp_events_span = + span!(tracing::Level::INFO, "failed_tcp_registry_event"); + let _enter: span::Entered<'_> = failed_tcp_events_span.enter(); + event!( + tracing::Level::WARN, + "Corrupted data. data_len = {} data_ptr = {}. Min size required: {} bytes", + data.len(), + data.as_ptr() as usize, + std::mem::size_of::() + ); + continue; + } if data.len() >= std::mem::size_of::() { let tcp_pl: TcpPacketRegistry = unsafe { std::ptr::read(data.as_ptr() as *const _) }; @@ -350,31 +409,17 @@ pub async fn display_tcp_registry_events>( command_str, cgroup_id //proc_content ); - //info!( - // "Event Id: {} Protocol: {:?} SRC: {}:{} -> DST: {}:{} Command: {} Cgroup_id: {}", - // event_id, - // proto, - // src, - // src_port, - // dst, - // dst_port, - // command_str, - // cgroup_id //proc_content - //); } - Err(_) => { + Err(e) => { event!( tracing::Level::INFO, - "Event Id: {} Protocol: Unknown ({}) Command: {} Cgroup_id: {}", + "Event Id: {} Protocol: Unknown ({}) Command: {} Cgroup_id: {} Error: {:?}", event_id, tcp_pl.proto, command_str, - cgroup_id + cgroup_id, + e ); - //info!( - // "Event Id: {} Protocol: Unknown ({})", - // event_id, tcp_pl.proto - //); } }; } else { @@ -391,6 +436,19 @@ pub async fn display_tcp_registry_events>( } } +#[cfg(feature = "experimental")] +use anyhow::Error; +#[cfg(feature = "experimental")] +use k8s_openapi::api::core::v1::Pod; +#[cfg(feature = "experimental")] +use kube::api::ObjectList; +#[cfg(feature = "experimental")] +use kube::{Api, Client}; +#[cfg(feature = "experimental")] +use std::fs; +#[cfg(feature = "experimental")] +use tokio::time; + #[cfg(feature = "experimental")] pub async fn scan_cgroup_paths(path: String) -> Result, Error> { let mut cgroup_paths: Vec = Vec::new(); From 440d44d7df91e017f53ccb8aa7f3ff72c12d3169 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Tue, 27 Jan 2026 14:05:02 +0100 Subject: [PATCH 13/24] [#174]: added prettify to logger --- core/common/src/logger.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/common/src/logger.rs b/core/common/src/logger.rs index 9dd0646e..ab06f79b 100644 --- a/core/common/src/logger.rs +++ b/core/common/src/logger.rs @@ -69,6 +69,9 @@ pub fn otlp_logger_init(service_name: String) -> SdkLoggerProvider { let fmt_filter = EnvFilter::new("info").add_directive("opentelemetry=debug".parse().unwrap()); let fmt_layer = tracing_subscriber::fmt::layer() .with_thread_names(true) + .with_line_number(false) + .with_target(false) + .pretty() .with_filter(fmt_filter); //init tracing subscriber with otel layer From c0afdf4b10985774835b4e5fe23a1e51fc50e524 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Tue, 27 Jan 2026 23:16:53 +0100 Subject: [PATCH 14/24] [#181]: added command to repair blocklist configmaps --- cli/src/install.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++- cli/src/main.rs | 7 +++- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/cli/src/install.rs b/cli/src/install.rs index bdb1ea12..105853c0 100644 --- a/cli/src/install.rs +++ b/cli/src/install.rs @@ -2,8 +2,10 @@ use crate::errors::CliError; use crate::essential::{BASE_COMMAND, connect_to_client, create_config_file, create_configs}; use clap::{Args, Subcommand}; use colored::Colorize; -use kube::Error; +use k8s_openapi::api::core::v1::ConfigMap; use kube::core::ErrorResponse; +use kube::{Api, Client, Error}; +use std::thread::sleep; use std::{process::Command, thread, time::Duration}; // docs: @@ -38,6 +40,8 @@ pub enum InstallCommands { about = "Deploys a simple example contained in deploy-test-pod.yaml" )] TestPods, + #[command(name = "blocklist", about = "Install or Repair blocklist configmap")] + Blocklist, } //install args @@ -206,6 +210,84 @@ async fn install_simple_example_component() -> Result<(), CliError> { } } +// docs: +pub async fn install_blocklist_configmap() -> Result<(), CliError> { + match connect_to_client().await { + Ok(client) => { + println!( + "{} {}", + "=====>".blue().bold(), + "Checking if the Blocklist configmap exists" + ); + sleep(Duration::from_secs(1)); + let blocklist_exists = check_if_blocklist_exists(client).await?; + if !blocklist_exists { + println!( + "{} {}", + "=====>".blue().bold(), + "Blocklist configmap does not exist".red().bold() + ); + sleep(Duration::from_secs(1)); + println!("{} {}", "=====>".bold().blue(), "Creating configmap"); + let metdata_configs = create_configs(); + sleep(Duration::from_secs(1)); + match create_config_file(metdata_configs).await { + Ok(_) => { + println!( + "{} {}", + "=====>".bold().blue(), + "Configmap created/repaired successfully".bold().green() + ) + } + Err(e) => { + return Err(CliError::InstallerError { + reason: e.to_string(), + }); + } + } + return Ok(()); + } else { + println!() + } + + Ok(()) + } + Err(e) => { + return Err(CliError::ClientError(Error::Api(ErrorResponse { + status: "failed".to_string(), + message: "Failed to connect to kubernetes client".to_string(), + reason: e.to_string(), + code: 404, + }))); + } + } +} + +// docs: +async fn check_if_blocklist_exists(client: Client) -> Result { + let namespace = "cortexflow"; + let name = "cortexbrain-client-config"; + let api: Api = Api::namespaced(client, namespace); + match api.get(name).await { + Ok(_) => { + println!( + "{} {}", + "=====>".bold().blue(), + "Blocklist configmap exists".green().bold() + ); + Ok(true) + } + Err(_) => { + println!( + "{} {}", + "=====>".bold().blue(), + "Blocklist configmap doesn not exists".red().bold(), + ); + Ok(false) + } + } +} + //docs: // // This is an auxiliary function to help manage the cortexflow components during the installation diff --git a/cli/src/main.rs b/cli/src/main.rs index 0a5ac46e..68e52f66 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -68,7 +68,8 @@ enum Commands { struct SetArgs { val: String, } - +//TODO: add command for monitoring veth interfaces +//TODO: add command to repair the blocklist map async fn args_parser() -> Result<(), CliError> { let args = Cli::parse(); debug!("Arguments {:?}", args.cmd); @@ -80,6 +81,10 @@ async fn args_parser() -> Result<(), CliError> { InstallCommands::TestPods => { install_simple_example().await?; } + InstallCommands::Blocklist => { + //install or repair blocklist configmap + let _ = install::install_blocklist_configmap().await?; + } }, Some(Commands::Uninstall) => { uninstall().await?; From c3e47b177e47961be5c86090596ed6d9346fcb7c Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Fri, 30 Jan 2026 15:17:09 +0100 Subject: [PATCH 15/24] [#182]: added GetTrackedVeth grpc endpoint definition --- cli/src/main.rs | 1 - core/api/protos/agent.proto | 14 ++++- core/api/src/agent.rs | 82 ++++++++++++++++++++++++-- core/api/src/api.rs | 102 ++++++++++++++++++++++----------- core/src/testing/identity.yaml | 2 +- 5 files changed, 161 insertions(+), 40 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 68e52f66..bf434e0d 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -69,7 +69,6 @@ struct SetArgs { val: String, } //TODO: add command for monitoring veth interfaces -//TODO: add command to repair the blocklist map async fn args_parser() -> Result<(), CliError> { let args = Cli::parse(); debug!("Arguments {:?}", args.cmd); diff --git a/core/api/protos/agent.proto b/core/api/protos/agent.proto index 3cd236b3..345ad403 100644 --- a/core/api/protos/agent.proto +++ b/core/api/protos/agent.proto @@ -68,6 +68,13 @@ message DroppedPacketsResponse { uint32 total_drops = 3; // Total drops across all connections } +// Veth Info + +message VethResponse{ + string status = 1; + repeated string veth_names = 2; // List of active veth interface names + int32 tot_monitored_veth = 3; +} //declare agent api service Agent{ @@ -81,11 +88,14 @@ service Agent{ // remove ip from blocklist endpoint rpc RmIpFromBlocklist(RmIpFromBlocklistRequest) returns (RmIpFromBlocklistResponse); - // metrics data + // metrics data endpoint rpc GetLatencyMetrics(google.protobuf.Empty) returns (LatencyMetricsResponse); - // dropped packets + // dropped packets endpoint rpc GetDroppedPacketsMetrics(google.protobuf.Empty) returns (DroppedPacketsResponse); + + // active veth info endpoint + rpc GetTrackedVeth(google.protobuf.Empty) returns (VethResponse); } message AddIpToBlocklistRequest{ diff --git a/core/api/src/agent.rs b/core/api/src/agent.rs index c6f5126d..32c823fe 100644 --- a/core/api/src/agent.rs +++ b/core/api/src/agent.rs @@ -121,6 +121,14 @@ pub struct DroppedPacketsResponse { pub total_drops: u32, } #[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)] +pub struct VethResponse { + #[prost(string, tag = "1")] + pub status: ::prost::alloc::string::String, + /// List of active veth interface names + #[prost(string, repeated, tag = "2")] + pub veth_names: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, +} +#[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)] pub struct AddIpToBlocklistRequest { #[prost(string, optional, tag = "1")] pub ip: ::core::option::Option<::prost::alloc::string::String>, @@ -341,7 +349,7 @@ pub mod agent_client { .insert(GrpcMethod::new("agent.Agent", "RmIpFromBlocklist")); self.inner.unary(req, path, codec).await } - /// metrics data + /// metrics data endpoint pub async fn get_latency_metrics( &mut self, request: impl tonic::IntoRequest<()>, @@ -366,7 +374,7 @@ pub mod agent_client { .insert(GrpcMethod::new("agent.Agent", "GetLatencyMetrics")); self.inner.unary(req, path, codec).await } - /// dropped packets + /// dropped packets endpoint pub async fn get_dropped_packets_metrics( &mut self, request: impl tonic::IntoRequest<()>, @@ -391,6 +399,27 @@ pub mod agent_client { .insert(GrpcMethod::new("agent.Agent", "GetDroppedPacketsMetrics")); self.inner.unary(req, path, codec).await } + /// active veth info endpoint + pub async fn get_active_veth( + &mut self, + request: impl tonic::IntoRequest<()>, + ) -> std::result::Result, tonic::Status> { + self.inner + .ready() + .await + .map_err(|e| { + tonic::Status::unknown( + format!("Service was not ready: {}", e.into()), + ) + })?; + let codec = tonic_prost::ProstCodec::default(); + let path = http::uri::PathAndQuery::from_static( + "/agent.Agent/GetActiveVeth", + ); + let mut req = request.into_request(); + req.extensions_mut().insert(GrpcMethod::new("agent.Agent", "GetActiveVeth")); + self.inner.unary(req, path, codec).await + } } } /// Generated server implementations. @@ -437,7 +466,7 @@ pub mod agent_server { tonic::Response, tonic::Status, >; - /// metrics data + /// metrics data endpoint async fn get_latency_metrics( &self, request: tonic::Request<()>, @@ -445,7 +474,7 @@ pub mod agent_server { tonic::Response, tonic::Status, >; - /// dropped packets + /// dropped packets endpoint async fn get_dropped_packets_metrics( &self, request: tonic::Request<()>, @@ -453,6 +482,11 @@ pub mod agent_server { tonic::Response, tonic::Status, >; + /// active veth info endpoint + async fn get_active_veth( + &self, + request: tonic::Request<()>, + ) -> std::result::Result, tonic::Status>; } /// declare agent api #[derive(Debug)] @@ -787,6 +821,46 @@ pub mod agent_server { }; Box::pin(fut) } + "/agent.Agent/GetActiveVeth" => { + #[allow(non_camel_case_types)] + struct GetActiveVethSvc(pub Arc); + impl tonic::server::UnaryService<()> + for GetActiveVethSvc { + type Response = super::VethResponse; + type Future = BoxFuture< + tonic::Response, + tonic::Status, + >; + fn call(&mut self, request: tonic::Request<()>) -> Self::Future { + let inner = Arc::clone(&self.0); + let fut = async move { + ::get_active_veth(&inner, request).await + }; + Box::pin(fut) + } + } + let accept_compression_encodings = self.accept_compression_encodings; + let send_compression_encodings = self.send_compression_encodings; + let max_decoding_message_size = self.max_decoding_message_size; + let max_encoding_message_size = self.max_encoding_message_size; + let inner = self.inner.clone(); + let fut = async move { + let method = GetActiveVethSvc(inner); + let codec = tonic_prost::ProstCodec::default(); + let mut grpc = tonic::server::Grpc::new(codec) + .apply_compression_config( + accept_compression_encodings, + send_compression_encodings, + ) + .apply_max_message_size_config( + max_decoding_message_size, + max_encoding_message_size, + ); + let res = grpc.unary(method, req).await; + Ok(res) + }; + Box::pin(fut) + } _ => { Box::pin(async move { let mut response = http::Response::new( diff --git a/core/api/src/api.rs b/core/api/src/api.rs index 27641b40..57164837 100644 --- a/core/api/src/api.rs +++ b/core/api/src/api.rs @@ -1,12 +1,10 @@ #![allow(warnings)] use anyhow::Context; use chrono::Local; -use cortexbrain_common::{ - formatters::{format_ipv4, format_ipv6}, -}; +use cortexbrain_common::formatters::{format_ipv4, format_ipv6}; use prost::bytes::BytesMut; -use std::{str::FromStr, sync::Arc}; use std::sync::Mutex; +use std::{str::FromStr, sync::Arc}; use tonic::{Request, Response, Status}; use tracing::info; @@ -22,19 +20,17 @@ use std::collections::HashMap; use tokio::sync::mpsc; use tokio::task; -use crate::{ - agent::{ - ConnectionEvent, DroppedPacketMetric, DroppedPacketsResponse, - LatencyMetric, LatencyMetricsResponse, - }, +use crate::agent::{ + ConnectionEvent, DroppedPacketMetric, DroppedPacketsResponse, LatencyMetric, + LatencyMetricsResponse, }; use crate::structs::{NetworkMetrics, PacketLog, TimeStampMetrics}; // * contains agent api configuration use crate::agent::{ - agent_server::Agent, ActiveConnectionResponse, AddIpToBlocklistRequest, BlocklistResponse, - RequestActiveConnections, RmIpFromBlocklistRequest, RmIpFromBlocklistResponse, + ActiveConnectionResponse, AddIpToBlocklistRequest, BlocklistResponse, RequestActiveConnections, + RmIpFromBlocklistRequest, RmIpFromBlocklistResponse, VethResponse, agent_server::Agent, }; use crate::constants::PIN_BLOCKLIST_MAP_PATH; @@ -54,6 +50,8 @@ pub struct AgentApi { latency_metrics_tx: mpsc::Sender, Status>>, dropped_packet_metrics_rx: Mutex, Status>>>, dropped_packet_metrics_tx: mpsc::Sender, Status>>, + tracked_veth_rx: Mutex, Status>>>, + tracked_veth_tx: mpsc::Sender, Status>>, } //* Event sender trait. Takes an event from a map and send that to the mpsc channel @@ -94,6 +92,7 @@ pub trait EventSender: Send + Sync + 'static { let _ = tx.send(event).await; } + // TODO: add the event sender for the tracked veth } // send event function. takes an HashMap and send that using mpsc event_tx @@ -120,6 +119,10 @@ impl EventSender for AgentApi { impl Default for AgentApi { //TODO:this part needs a better error handling fn default() -> Self { + // + // init MapData from the kernel space + // + // load connections maps mapdata let active_connection_mapdata = MapData::from_pin("/sys/fs/bpf/maps/events_map") .expect("cannot open events_map Mapdata"); @@ -136,16 +139,29 @@ impl Default for AgentApi { .expect("Error while initializing network metrics array"); // load time stamp events maps mapdata - let time_stamp_events_mapdata = MapData::from_pin("/sys/fs/bpf/trace_maps/time_stamp_events") - .expect("cannot open time_stamp_events Mapdata"); + let time_stamp_events_mapdata = + MapData::from_pin("/sys/fs/bpf/trace_maps/time_stamp_events") + .expect("cannot open time_stamp_events Mapdata"); let time_stamp_events_map = Map::PerfEventArray(time_stamp_events_mapdata); // let mut time_stamp_events_array = PerfEventArray::try_from(time_stamp_events_map) .expect("Error while initializing time stamp events array"); - //init a mpsc channel + // load veth maps + let tracked_veth_mapdata = MapData::from_pin("/sys/fs/bpf/maps/tracked_veth_map") + .expect("cannot open tracked_veth_map Mapdata"); + let tracked_veth_map = Map::HashMap(tracked_veth_mapdata); //creates a HashMap from the mapdata + let mut tracked_veth_hashmap = + ayaHashMap::::try_from(tracked_veth_map) + .expect("Error while initializing tracked veth hashmap"); + + // + // init a mpsc channels with TX (transmission) and RX(Receiver) components + // + let (conn_tx, conn_rx) = mpsc::channel(1024); let (lat_tx, lat_rx) = mpsc::channel(2048); let (drop_tx, drop_rx) = mpsc::channel(2048); + let (tracked_veth_tx, tracked_veth_rx) = mpsc::channel(1024); let api = AgentApi { active_connection_event_rx: conn_rx.into(), @@ -154,6 +170,8 @@ impl Default for AgentApi { latency_metrics_tx: lat_tx.clone(), dropped_packet_metrics_rx: Mutex::new(drop_rx), dropped_packet_metrics_tx: drop_tx.clone(), + tracked_veth_rx: Mutex::new(tracked_veth_rx), + tracked_veth_tx: tracked_veth_tx.clone(), }; // For network metrics @@ -198,12 +216,7 @@ impl Default for AgentApi { Ok(proto) => { info!( "Event Id: {} Protocol: {:?} SRC: {}:{} -> DST: {}:{}", - event_id, - proto, - src, - src_port, - dst, - dst_port + event_id, proto, src, src_port, dst, dst_port ); info!("creating vector for the aggregated data"); let mut evt = Vec::new(); @@ -296,18 +309,18 @@ impl Default for AgentApi { if dropped_packet_metrics.sk_drops > 0 { let mut evt = Vec::new(); info!( - "Dropped Packet Metric - tgid: {}, process_name: {}, sk_drops: {}, sk_err: {}, sk_err_soft: {}, sk_backlog_len: {}, sk_wmem_queued: {}, sk_rcvbuf: {}, sk_ack_backlog: {}, timestamp_us: {}", - dropped_packet_metrics.tgid, - dropped_packet_metrics.process_name, - dropped_packet_metrics.sk_drops, - dropped_packet_metrics.sk_err, - dropped_packet_metrics.sk_err_soft, - dropped_packet_metrics.sk_backlog_len, - dropped_packet_metrics.sk_wmem_queued, - dropped_packet_metrics.sk_rcvbuf, - dropped_packet_metrics.sk_ack_backlog, - dropped_packet_metrics.timestamp_us - ); + "Dropped Packet Metric - tgid: {}, process_name: {}, sk_drops: {}, sk_err: {}, sk_err_soft: {}, sk_backlog_len: {}, sk_wmem_queued: {}, sk_rcvbuf: {}, sk_ack_backlog: {}, timestamp_us: {}", + dropped_packet_metrics.tgid, + dropped_packet_metrics.process_name, + dropped_packet_metrics.sk_drops, + dropped_packet_metrics.sk_err, + dropped_packet_metrics.sk_err_soft, + dropped_packet_metrics.sk_backlog_len, + dropped_packet_metrics.sk_wmem_queued, + dropped_packet_metrics.sk_rcvbuf, + dropped_packet_metrics.sk_ack_backlog, + dropped_packet_metrics.timestamp_us + ); evt.push(dropped_packet_metrics.clone()); let _ = drop_tx.send(Ok(evt)).await; } @@ -408,6 +421,8 @@ impl Default for AgentApi { } }); + // TODO: spawn a task to read the events from the maps and send the events using the EventSender trait + api } } @@ -659,4 +674,27 @@ impl Agent for AgentApi { Ok(Response::new(response)) } + + async fn get_active_veth( + &self, + request: Request<()>, + ) -> Result, Status> { + let req = request.into_inner(); + info!("Getting tracked veth metrics"); + let mut tracked_veth = Vec::::new(); + + while let Ok(evt) = self.tracked_veth_rx.lock().unwrap().try_recv() { + if let Ok(vec) = evt { + tracked_veth.extend(vec); + } + } + info!("Tracked veth: {:?}", &tracked_veth); + + let response = VethResponse { + status: "success".to_string(), + veth_names: tracked_veth, + }; + + Ok(Response::new(response)) + } } diff --git a/core/src/testing/identity.yaml b/core/src/testing/identity.yaml index 43e6c928..3239f3e0 100644 --- a/core/src/testing/identity.yaml +++ b/core/src/testing/identity.yaml @@ -52,7 +52,7 @@ spec: - SYS_PTRACE containers: - name: identity - image: ghcr.io/cortexflow/identity:latest + image: lorenzotettamanti/cortexflow-identity:0.1.5-otlp17 command: ["/bin/bash", "-c"] args: - | From 8d6541beadabd63ccc7fe0f413d91aece83fa8a2 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Sat, 31 Jan 2026 11:14:02 +0100 Subject: [PATCH 16/24] [#158]: added load_perf_event_array_from_mapdata function in map_handlers.rs --- core/common/src/map_handlers.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/core/common/src/map_handlers.rs b/core/common/src/map_handlers.rs index 0bbf606c..fc2ef8dd 100644 --- a/core/common/src/map_handlers.rs +++ b/core/common/src/map_handlers.rs @@ -86,9 +86,9 @@ pub fn map_pinner(maps: BpfMapsData, path: &PathBuf) -> Result, Error> Ok(owned_maps) } -use aya::maps::MapData; #[cfg(feature = "map-handlers")] pub async fn populate_blocklist() -> Result<(), Error> { + use aya::maps::MapData; // load mapdata from path let mapdata = MapData::from_pin("/sys/fs/bpf/maps/Blocklist") @@ -131,3 +131,21 @@ pub async fn populate_blocklist() -> Result<(), Error> { } } } + +#[cfg(feature = "map-handlers")] +pub fn load_perf_event_array_from_mapdata( + path: &'static str, +) -> Result, Error> { + use aya::maps::MapData; + use aya::maps::PerfEventArray; + + let map_data = MapData::from_pin(path) + .map_err(|e| anyhow::anyhow!("Cannot load mapdata from pin {:?} .Reason: {}", &path, e))?; + + let map = Map::PerfEventArray(map_data); + + let perf_event_array = PerfEventArray::try_from(map).map_err(|e| { + anyhow::anyhow!("Cannot initialize perf_event_array from map. Reason: {}", e) + })?; + Ok(perf_event_array) +} From 1804aabcb6f38dc75708bbb23f167499be4aa538 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Sat, 31 Jan 2026 11:16:04 +0100 Subject: [PATCH 17/24] [#182]: added total monitored veth_events (tot_monitored_veth) --- core/Cargo.lock | 2 +- core/api/Cargo.toml | 14 +++++++--- core/api/src/agent.rs | 21 ++++++++------- core/api/src/api.rs | 63 +++++++++++++++++++++---------------------- 4 files changed, 54 insertions(+), 46 deletions(-) diff --git a/core/Cargo.lock b/core/Cargo.lock index e00948ba..af06c43f 100644 --- a/core/Cargo.lock +++ b/core/Cargo.lock @@ -438,7 +438,7 @@ dependencies = [ "bytemuck", "bytemuck_derive", "chrono", - "cortexbrain-common 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cortexbrain-common 0.1.0", "cortexflow_identity 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "prost", "tokio", diff --git a/core/api/Cargo.toml b/core/api/Cargo.toml index 988ac46d..9706e639 100644 --- a/core/api/Cargo.toml +++ b/core/api/Cargo.toml @@ -3,7 +3,13 @@ name = "cortexflow_agent_api" version = "0.1.1" edition = "2024" description = "CortexFlow agent API" -authors = ["Lorenzo Tettamanti", "Pranav Verma", "Lorenzo Bradanini","Siddharth Sutar","Andrea Bozzo"] +authors = [ + "Lorenzo Tettamanti", + "Pranav Verma", + "Lorenzo Bradanini", + "Siddharth Sutar", + "Andrea Bozzo", +] documentation = "https://docs.cortexflow.org" homepage = "https://docs.cortexflow.org" repository = "https://github.com/CortexFlow/CortexBrain" @@ -23,14 +29,14 @@ tonic = "0.14.0" tonic-prost = "0.14.0" tracing = "0.1.41" aya = "0.13.1" -cortexbrain-common = "0.1.0" +cortexbrain-common = { path = "../common", features = ["map-handlers"] } tonic-reflection = "0.14.0" tonic-build = "0.14.0" tracing-subscriber = "0.3.19" tokio-stream = "0.1.17" -bytemuck = {version ="1.23.0"} +bytemuck = { version = "1.23.0" } bytemuck_derive = "1.10.1" -cortexflow_identity = {version = "0.1.1", features = ["enums"]} +cortexflow_identity = { version = "0.1.1", features = ["enums"] } chrono = "0.4.42" [build-dependencies] diff --git a/core/api/src/agent.rs b/core/api/src/agent.rs index 32c823fe..03b103d6 100644 --- a/core/api/src/agent.rs +++ b/core/api/src/agent.rs @@ -127,6 +127,8 @@ pub struct VethResponse { /// List of active veth interface names #[prost(string, repeated, tag = "2")] pub veth_names: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, + #[prost(int32, tag = "3")] + pub tot_monitored_veth: i32, } #[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)] pub struct AddIpToBlocklistRequest { @@ -400,7 +402,7 @@ pub mod agent_client { self.inner.unary(req, path, codec).await } /// active veth info endpoint - pub async fn get_active_veth( + pub async fn get_tracked_veth( &mut self, request: impl tonic::IntoRequest<()>, ) -> std::result::Result, tonic::Status> { @@ -414,10 +416,11 @@ pub mod agent_client { })?; let codec = tonic_prost::ProstCodec::default(); let path = http::uri::PathAndQuery::from_static( - "/agent.Agent/GetActiveVeth", + "/agent.Agent/GetTrackedVeth", ); let mut req = request.into_request(); - req.extensions_mut().insert(GrpcMethod::new("agent.Agent", "GetActiveVeth")); + req.extensions_mut() + .insert(GrpcMethod::new("agent.Agent", "GetTrackedVeth")); self.inner.unary(req, path, codec).await } } @@ -483,7 +486,7 @@ pub mod agent_server { tonic::Status, >; /// active veth info endpoint - async fn get_active_veth( + async fn get_tracked_veth( &self, request: tonic::Request<()>, ) -> std::result::Result, tonic::Status>; @@ -821,11 +824,11 @@ pub mod agent_server { }; Box::pin(fut) } - "/agent.Agent/GetActiveVeth" => { + "/agent.Agent/GetTrackedVeth" => { #[allow(non_camel_case_types)] - struct GetActiveVethSvc(pub Arc); + struct GetTrackedVethSvc(pub Arc); impl tonic::server::UnaryService<()> - for GetActiveVethSvc { + for GetTrackedVethSvc { type Response = super::VethResponse; type Future = BoxFuture< tonic::Response, @@ -834,7 +837,7 @@ pub mod agent_server { fn call(&mut self, request: tonic::Request<()>) -> Self::Future { let inner = Arc::clone(&self.0); let fut = async move { - ::get_active_veth(&inner, request).await + ::get_tracked_veth(&inner, request).await }; Box::pin(fut) } @@ -845,7 +848,7 @@ pub mod agent_server { let max_encoding_message_size = self.max_encoding_message_size; let inner = self.inner.clone(); let fut = async move { - let method = GetActiveVethSvc(inner); + let method = GetTrackedVethSvc(inner); let codec = tonic_prost::ProstCodec::default(); let mut grpc = tonic::server::Grpc::new(codec) .apply_compression_config( diff --git a/core/api/src/api.rs b/core/api/src/api.rs index 57164837..ce1bae3f 100644 --- a/core/api/src/api.rs +++ b/core/api/src/api.rs @@ -2,6 +2,7 @@ use anyhow::Context; use chrono::Local; use cortexbrain_common::formatters::{format_ipv4, format_ipv6}; +use cortexbrain_common::map_handlers::load_perf_event_array_from_mapdata; use prost::bytes::BytesMut; use std::sync::Mutex; use std::{str::FromStr, sync::Arc}; @@ -92,6 +93,17 @@ pub trait EventSender: Send + Sync + 'static { let _ = tx.send(event).await; } + async fn send_tracked_veth_event(&self, event: Vec); + async fn send_tracked_veth_event_map( + &self, + map: Vec, + tx: mpsc::Sender, Status>>, + ) { + let status = Status::new(tonic::Code::Ok, "success"); + let event = Ok(map); + let _ = tx.send(event).await; + } + // TODO: add the event sender for the tracked veth } @@ -112,47 +124,29 @@ impl EventSender for AgentApi { self.send_dropped_packet_metrics_event_map(event, self.dropped_packet_metrics_tx.clone()) .await; } + async fn send_tracked_veth_event(&self, event: Vec) { + self.send_tracked_veth_event_map(event, self.tracked_veth_tx.clone()) + .await; + } } //initialize a default trait for AgentApi. Loads a name and a bpf istance. //this trait is essential for init the Agent. impl Default for AgentApi { - //TODO:this part needs a better error handling fn default() -> Self { // // init MapData from the kernel space // - // load connections maps mapdata - let active_connection_mapdata = MapData::from_pin("/sys/fs/bpf/maps/events_map") - .expect("cannot open events_map Mapdata"); - let active_connection_map = Map::PerfEventArray(active_connection_mapdata); //creates a PerfEventArray from the mapdata - - let mut active_connection_events_array = PerfEventArray::try_from(active_connection_map) - .expect("Error while initializing events array"); - - // load network metrics maps mapdata - let network_metrics_mapdata = MapData::from_pin("/sys/fs/bpf/trace_maps/net_metrics") - .expect("cannot open net_metrics Mapdata"); - let network_metrics_map = Map::PerfEventArray(network_metrics_mapdata); //creates a PerfEventArray from the mapdata - let mut network_metrics_events_array = PerfEventArray::try_from(network_metrics_map) - .expect("Error while initializing network metrics array"); - - // load time stamp events maps mapdata - let time_stamp_events_mapdata = - MapData::from_pin("/sys/fs/bpf/trace_maps/time_stamp_events") - .expect("cannot open time_stamp_events Mapdata"); - let time_stamp_events_map = Map::PerfEventArray(time_stamp_events_mapdata); // - let mut time_stamp_events_array = PerfEventArray::try_from(time_stamp_events_map) - .expect("Error while initializing time stamp events array"); - - // load veth maps - let tracked_veth_mapdata = MapData::from_pin("/sys/fs/bpf/maps/tracked_veth_map") - .expect("cannot open tracked_veth_map Mapdata"); - let tracked_veth_map = Map::HashMap(tracked_veth_mapdata); //creates a HashMap from the mapdata - let mut tracked_veth_hashmap = - ayaHashMap::::try_from(tracked_veth_map) - .expect("Error while initializing tracked veth hashmap"); + // TODO: in the future will be better to not use .unwrap() + let mut active_connection_events_array = + load_perf_event_array_from_mapdata("/sys/fs/bpf/maps/events_map").unwrap(); + let mut network_metrics_events_array = + load_perf_event_array_from_mapdata("/sys/fs/bpf/trace_maps/net_metrics").unwrap(); + let mut time_stamp_events_array = + load_perf_event_array_from_mapdata("/sys/fs/bpf/trace_maps/time_stamp_events").unwrap(); + let mut tracked_veth_events_array = + load_perf_event_array_from_mapdata("/sys/fs/bpf/maps/tracked_veth_map").unwrap(); // // init a mpsc channels with TX (transmission) and RX(Receiver) components @@ -675,24 +669,29 @@ impl Agent for AgentApi { Ok(Response::new(response)) } - async fn get_active_veth( + async fn get_tracked_veth( &self, request: Request<()>, ) -> Result, Status> { let req = request.into_inner(); info!("Getting tracked veth metrics"); let mut tracked_veth = Vec::::new(); + let mut tot_veth = 0 as i32; while let Ok(evt) = self.tracked_veth_rx.lock().unwrap().try_recv() { if let Ok(vec) = evt { tracked_veth.extend(vec); } } + tot_veth = tracked_veth.len() as i32; + + info!("Total tracked veth events: {}", tot_veth); info!("Tracked veth: {:?}", &tracked_veth); let response = VethResponse { status: "success".to_string(), veth_names: tracked_veth, + tot_monitored_veth: tot_veth, }; Ok(Response::new(response)) From 8bfa3664a6a6664580a6bd3c90372d2c9778c8d9 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Sat, 31 Jan 2026 14:03:44 +0100 Subject: [PATCH 18/24] [#182]: added "cfcli monitoring veth" command frontend. added send_tracked_veth_requests function in api/requests.rs --- cli/Cargo.lock | 553 ++++++++++++++++++++++++++++++++++++++- cli/Cargo.toml | 2 +- cli/src/main.rs | 5 +- cli/src/monitoring.rs | 60 ++++- core/api/src/requests.rs | 54 ++-- 5 files changed, 640 insertions(+), 34 deletions(-) diff --git a/cli/Cargo.lock b/cli/Cargo.lock index 6e951cad..df205899 100644 --- a/cli/Cargo.lock +++ b/cli/Cargo.lock @@ -356,6 +356,23 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "cortexbrain-common" +version = "0.1.0" +dependencies = [ + "anyhow", + "aya", + "k8s-openapi", + "kube", + "opentelemetry", + "opentelemetry-appender-tracing", + "opentelemetry-otlp", + "opentelemetry-stdout", + "opentelemetry_sdk", + "tracing", + "tracing-subscriber", +] + [[package]] name = "cortexbrain-common" version = "0.1.0" @@ -390,15 +407,13 @@ dependencies = [ [[package]] name = "cortexflow_agent_api" version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bfebbb2894a8d2edec3c4f3631952860c34706b798aa8d77ea2806ddd6fc476" dependencies = [ "anyhow", "aya", "bytemuck", "bytemuck_derive", "chrono", - "cortexbrain-common", + "cortexbrain-common 0.1.0", "cortexflow_identity", "prost", "tokio", @@ -423,7 +438,7 @@ dependencies = [ "bytemuck", "bytemuck_derive", "bytes", - "cortexbrain-common", + "cortexbrain-common 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "k8s-openapi", "kube", "libc", @@ -512,6 +527,17 @@ dependencies = [ "windows-sys 0.61.1", ] +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "either" version = "1.15.0" @@ -603,12 +629,34 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "futures-sink" version = "0.3.31" @@ -630,6 +678,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-io", + "futures-macro", "futures-sink", "futures-task", "memchr", @@ -822,6 +871,7 @@ version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d9b05277c7e8da2c93a568989bb6207bef0112e8d17df7a6eda4a3cf143bc5e" dependencies = [ + "base64", "bytes", "futures-channel", "futures-core", @@ -829,7 +879,9 @@ dependencies = [ "http", "http-body", "hyper", + "ipnet", "libc", + "percent-encoding", "pin-project-lite", "socket2", "tokio", @@ -861,6 +913,108 @@ dependencies = [ "cc", ] +[[package]] +name = "icu_collections" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6b649701667bbe825c3b7e6388cb521c23d88644678e83c0c4d0a621a34b43" +dependencies = [ + "displaydoc", + "potential_utf", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edba7861004dd3714265b4db54a3c390e880ab658fec5f7db895fae2046b5bb6" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f6c8828b67bf8908d82127b2054ea1b4427ff0230ee9141c54251934ab1b599" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7aedcccd01fc5fe81e6b489c15b247b8b0690feb23304303a9e560f37efc560a" + +[[package]] +name = "icu_properties" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "020bfc02fe870ec3a66d93e677ccca0562506e5872c650f893269e08615d74ec" +dependencies = [ + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "616c294cf8d725c6afcd8f55abc17c56464ef6211f9ed59cccffe534129c77af" + +[[package]] +name = "icu_provider" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85962cf0ce02e1e0a629cc34e7ca3e373ce20dda4c4d7294bbd0bf1fdb59e614" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acae9609540aa318d1bc588455225fb2085b9ed0c4f6bd0d9d5bcd86f1a0344" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + [[package]] name = "indexmap" version = "2.11.0" @@ -871,6 +1025,22 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "ipnet" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" + +[[package]] +name = "iri-string" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c91338f0783edbd6195decb37bae672fd3b165faffb89bf7b9e6942f8b1a731a" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -1019,6 +1189,12 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" +[[package]] +name = "litemap" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77" + [[package]] name = "lock_api" version = "0.4.13" @@ -1149,6 +1325,105 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" +[[package]] +name = "opentelemetry" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b84bcd6ae87133e903af7ef497404dda70c60d0ea14895fc8a5e6722754fc2a0" +dependencies = [ + "futures-core", + "futures-sink", + "js-sys", + "pin-project-lite", + "thiserror 2.0.16", + "tracing", +] + +[[package]] +name = "opentelemetry-appender-tracing" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef6a1ac5ca3accf562b8c306fa8483c85f4390f768185ab775f242f7fe8fdcc2" +dependencies = [ + "opentelemetry", + "tracing", + "tracing-core", + "tracing-subscriber", +] + +[[package]] +name = "opentelemetry-http" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7a6d09a73194e6b66df7c8f1b680f156d916a1a942abf2de06823dd02b7855d" +dependencies = [ + "async-trait", + "bytes", + "http", + "opentelemetry", + "reqwest", +] + +[[package]] +name = "opentelemetry-otlp" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2366db2dca4d2ad033cad11e6ee42844fd727007af5ad04a1730f4cb8163bf" +dependencies = [ + "http", + "opentelemetry", + "opentelemetry-http", + "opentelemetry-proto", + "opentelemetry_sdk", + "prost", + "reqwest", + "thiserror 2.0.16", + "tokio", + "tonic", + "tracing", +] + +[[package]] +name = "opentelemetry-proto" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7175df06de5eaee9909d4805a3d07e28bb752c34cab57fa9cff549da596b30f" +dependencies = [ + "opentelemetry", + "opentelemetry_sdk", + "prost", + "tonic", + "tonic-prost", +] + +[[package]] +name = "opentelemetry-stdout" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc8887887e169414f637b18751487cce4e095be787d23fad13c454e2fb1b3811" +dependencies = [ + "chrono", + "opentelemetry", + "opentelemetry_sdk", +] + +[[package]] +name = "opentelemetry_sdk" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e14ae4f5991976fd48df6d843de219ca6d31b01daaab2dad5af2badeded372bd" +dependencies = [ + "futures-channel", + "futures-executor", + "futures-util", + "opentelemetry", + "percent-encoding", + "rand", + "thiserror 2.0.16", + "tokio", + "tokio-stream", +] + [[package]] name = "option-ext" version = "0.2.0" @@ -1288,6 +1563,24 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "potential_utf" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b73949432f5e2a09657003c25bca5e19a0e9c84f8058ca374f49e0ebe605af77" +dependencies = [ + "zerovec", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + [[package]] name = "prettyplease" version = "0.2.37" @@ -1396,6 +1689,35 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +[[package]] +name = "rand" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.3", +] + [[package]] name = "redox_syscall" version = "0.5.17" @@ -1445,6 +1767,40 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "caf4aa5b0f434c91fe5c7f1ecb6a5ece2130b02ad2a590589dda5146df959001" +[[package]] +name = "reqwest" +version = "0.12.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d0946410b9f7b082a427e4ef5c8ff541a88b357bc6c637c40db3a68ac70a36f" +dependencies = [ + "base64", + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "js-sys", + "log", + "percent-encoding", + "pin-project-lite", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "ring" version = "0.17.14" @@ -1631,6 +1987,18 @@ dependencies = [ "serde_core", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_yaml" version = "0.9.34+deprecated" @@ -1701,6 +2069,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + [[package]] name = "strsim" version = "0.11.1" @@ -1729,6 +2103,20 @@ name = "sync_wrapper" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "tempfile" @@ -1792,6 +2180,16 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "tinystr" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42d3e9c45c09de15d06dd8acf5f4e0e399e85927b7f00711024eb7ae10fa4869" +dependencies = [ + "displaydoc", + "zerovec", +] + [[package]] name = "tokio" version = "1.49.0" @@ -1964,10 +2362,13 @@ dependencies = [ "base64", "bitflags", "bytes", + "futures-util", "http", "http-body", + "iri-string", "mime", "pin-project-lite", + "tower", "tower-layer", "tower-service", "tracing", @@ -2089,6 +2490,24 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + [[package]] name = "utf8parse" version = "0.2.2" @@ -2158,6 +2577,19 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e038d41e478cc73bae0ff9b36c60cff1c98b8f38f8d7e8061e79ee63608ac5c" +dependencies = [ + "cfg-if", + "js-sys", + "once_cell", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.104" @@ -2190,6 +2622,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "web-sys" +version = "0.3.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9367c417a924a74cae129e6a2ae3b47fabb1f8995595ab474029da749a8be120" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "windows-core" version = "0.62.1" @@ -2426,8 +2868,111 @@ version = "0.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "052283831dbae3d879dc7f51f3d92703a316ca49f91540417d38591826127814" +[[package]] +name = "writeable" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9" + +[[package]] +name = "yoke" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72d6e5c6afb84d73944e5cedb052c4680d5657337201555f9f2a16b7406d4954" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerocopy" +version = "0.8.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7456cf00f0685ad319c5b1693f291a650eaf345e941d082fc4e03df8a03996ac" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1328722bbf2115db7e19d69ebcc15e795719e2d66b60827c6a69a117365e37a0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zerofrom" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + [[package]] name = "zeroize" version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" + +[[package]] +name = "zerotrie" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a59c17a5562d507e4b54960e8569ebee33bee890c70aa3fe7b97e85a9fd7851" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c28719294829477f525be0186d13efa9a3c602f7ec202ca9e353d310fb9a002" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index cfbcae0d..a14b5273 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -22,7 +22,7 @@ tonic = "0.14.2" tonic-reflection = "0.14.2" prost-types = "0.14.3" prost = "0.14.3" -cortexflow_agent_api = {version = "0.1.1",features = ["client"]} +cortexflow_agent_api = {path = "../core/api",features = ["client"]} kube = "2.0.1" k8s-openapi = {version = "0.26.0", features = ["v1_34"]} diff --git a/cli/src/main.rs b/cli/src/main.rs index bf434e0d..8d543cd1 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -18,7 +18,7 @@ use crate::install::{InstallArgs, InstallCommands, install_cortexflow, install_s use crate::logs::{LogsArgs, logs_command}; use crate::monitoring::{ MonitorArgs, MonitorCommands, list_features, monitor_dropped_packets, monitor_identity_events, - monitor_latency_metrics, + monitor_latency_metrics, monitor_tracked_veth, }; use crate::policies::{ PoliciesArgs, PoliciesCommands, check_blocklist, create_blocklist, remove_ip, @@ -124,6 +124,9 @@ async fn args_parser() -> Result<(), CliError> { MonitorCommands::Droppedpackets => { let _ = monitor_dropped_packets().await?; } + MonitorCommands::Veth => { + let _ = monitor_tracked_veth().await?; + } }, Some(Commands::Policies(policies_args)) => { match policies_args.policy_cmd { diff --git a/cli/src/monitoring.rs b/cli/src/monitoring.rs index b7cf3e2e..72a94b87 100644 --- a/cli/src/monitoring.rs +++ b/cli/src/monitoring.rs @@ -8,7 +8,10 @@ use std::result::Result::Ok; use tonic_reflection::pb::v1::server_reflection_response::MessageResponse; use agent_api::client::{connect_to_client, connect_to_server_reflection}; -use agent_api::requests::{get_all_features, send_active_connection_request}; +use agent_api::requests::{ + get_all_features, send_active_connection_request, send_dropped_packets_request, + send_latency_metrics_request, send_tracked_veth_request, +}; use crate::errors::CliError; use clap::{Args, Subcommand}; @@ -33,6 +36,11 @@ pub enum MonitorCommands { about = "Monitor the dropped packets metrics detected by the metrics service" )] Droppedpackets, + #[command( + name = "veth", + about = "Monitor tracked veth interfaces from the identity service" + )] + Veth, } // cfcli monitor @@ -40,8 +48,6 @@ pub enum MonitorCommands { pub struct MonitorArgs { #[command(subcommand)] pub monitor_cmd: MonitorCommands, - //#[arg(long, short)] - //pub flags: Option, } pub async fn list_features() -> Result<(), CliError> { @@ -168,7 +174,7 @@ pub async fn monitor_latency_metrics() -> Result<(), CliError> { "Connected to CortexFlow Client".green() ); //send request to get latency metrics - match agent_api::requests::send_latency_metrics_request(client).await { + match send_latency_metrics_request(client).await { Ok(response) => { let resp = response.into_inner(); if resp.metrics.is_empty() { @@ -237,7 +243,7 @@ pub async fn monitor_dropped_packets() -> Result<(), CliError> { "Connected to CortexFlow Client".green() ); //send request to get dropped packets metrics - match agent_api::requests::send_dropped_packets_request(client).await { + match send_dropped_packets_request(client).await { Ok(response) => { let resp = response.into_inner(); if resp.metrics.is_empty() { @@ -291,6 +297,50 @@ pub async fn monitor_dropped_packets() -> Result<(), CliError> { Ok(()) } +pub async fn monitor_tracked_veth() -> Result<(), CliError> { + println!( + "{} {}", + "=====>".blue().bold(), + "Connecting to cortexflow Client".white() + ); + match connect_to_client().await { + Ok(client) => match send_tracked_veth_request(client).await { + Ok(response) => { + let veth_response = response.into_inner(); + if veth_response.tot_monitored_veth == 0 { + println!("{} {} ", "=====>".blue().bold(), "No tracked veth found"); + Ok(()) + } else { + println!( + "{} {} {} {} ", + "=====>".blue().bold(), + "Found:", + &veth_response.tot_monitored_veth, + "tracked veth" + ); + for veth in veth_response.veth_names.iter() { + println!("{} {}", "=====>".blue().bold(), &veth); + } + Ok(()) + } + } + Err(e) => { + return Err(CliError::AgentError( + tonic_reflection::server::Error::InvalidFileDescriptorSet(e.to_string()), + )); + } + }, + Err(e) => { + return Err(CliError::ClientError(kube::Error::Api(ErrorResponse { + status: "failed".to_string(), + message: "Failed to connect to kubernetes client".to_string(), + reason: e.to_string(), + code: 404, + }))); + } + } +} + fn convert_timestamp_to_date(timestamp: u64) -> String { DateTime::from_timestamp_micros(timestamp as i64) .map(|dt| dt.to_string()) diff --git a/core/api/src/requests.rs b/core/api/src/requests.rs index a518f4af..06a40302 100644 --- a/core/api/src/requests.rs +++ b/core/api/src/requests.rs @@ -1,26 +1,25 @@ use anyhow::Error; use std::result::Result::Ok; -use tonic::{ Request, Response, Streaming, transport::Channel }; +use tonic::{Request, Response, Streaming, transport::Channel}; use tonic_reflection::pb::v1::{ - ServerReflectionRequest, - ServerReflectionResponse, - server_reflection_client::ServerReflectionClient, - server_reflection_request::MessageRequest, + ServerReflectionRequest, ServerReflectionResponse, + server_reflection_client::ServerReflectionClient, server_reflection_request::MessageRequest, }; -use crate::agent::agent_client::AgentClient; use crate::agent::ActiveConnectionResponse; -use crate::agent::RequestActiveConnections; -use crate::agent::BlocklistResponse; use crate::agent::AddIpToBlocklistRequest; -use crate::agent::RmIpFromBlocklistRequest; -use crate::agent::RmIpFromBlocklistResponse; +use crate::agent::BlocklistResponse; use crate::agent::DroppedPacketsResponse; use crate::agent::LatencyMetricsResponse; +use crate::agent::RequestActiveConnections; +use crate::agent::RmIpFromBlocklistRequest; +use crate::agent::RmIpFromBlocklistResponse; +use crate::agent::VethResponse; +use crate::agent::agent_client::AgentClient; #[cfg(feature = "client")] pub async fn send_active_connection_request( - mut client: AgentClient + mut client: AgentClient, ) -> Result, Error> { let request = Request::new(RequestActiveConnections { pod_ip: None }); let response = client.active_connections(request).await?; @@ -29,13 +28,17 @@ pub async fn send_active_connection_request( #[cfg(feature = "client")] pub async fn get_all_features( - mut client: ServerReflectionClient + mut client: ServerReflectionClient, ) -> Result>, Error> { let request = ServerReflectionRequest { host: "".to_string(), - message_request: Some(MessageRequest::FileContainingSymbol("agent.Agent".to_string())), + message_request: Some(MessageRequest::FileContainingSymbol( + "agent.Agent".to_string(), + )), }; - let response = client.server_reflection_info(tokio_stream::iter(vec![request])).await?; + let response = client + .server_reflection_info(tokio_stream::iter(vec![request])) + .await?; Ok(response) } @@ -43,7 +46,7 @@ pub async fn get_all_features( #[cfg(feature = "client")] pub async fn send_create_blocklist_request( mut client: AgentClient, - ip: &str + ip: &str, ) -> Result, Error> { let ip = Some(ip.to_string()); let request = Request::new(AddIpToBlocklistRequest { ip }); @@ -53,7 +56,7 @@ pub async fn send_create_blocklist_request( #[cfg(feature = "client")] pub async fn send_check_blocklist_request( - mut client: AgentClient + mut client: AgentClient, ) -> Result, Error> { let request = Request::new(()); let response = client.check_blocklist(request).await?; @@ -63,7 +66,7 @@ pub async fn send_check_blocklist_request( #[cfg(feature = "client")] pub async fn remove_ip_from_blocklist_request( mut client: AgentClient, - ip: &str + ip: &str, ) -> Result, Error> { let ip = ip.to_string(); let request = Request::new(RmIpFromBlocklistRequest { ip }); @@ -76,9 +79,7 @@ pub async fn send_dropped_packets_request( mut client: AgentClient, ) -> Result, Error> { let request = Request::new(()); - let response = client.get_dropped_packets_metrics( - request - ).await?; + let response = client.get_dropped_packets_metrics(request).await?; Ok(response) } @@ -87,8 +88,15 @@ pub async fn send_latency_metrics_request( mut client: AgentClient, ) -> Result, Error> { let request = Request::new(()); - let response = client.get_latency_metrics( - request - ).await?; + let response = client.get_latency_metrics(request).await?; + Ok(response) +} + +#[cfg(feature = "client")] +pub async fn send_tracked_veth_request( + mut client: AgentClient, +) -> Result, Error> { + let request = Request::new(()); + let response = client.get_tracked_veth(request).await?; Ok(response) } From 074cd72d62da6a89e3fcaa362fec744ceda92219 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Sun, 1 Feb 2026 16:37:17 +0100 Subject: [PATCH 19/24] [refactoring]: separate experimental service discovery from the helpers in the identity service --- core/src/components/identity/src/mod.rs | 4 +- .../identity/src/service_discovery.rs | 297 ++++++++++++++++++ 2 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 core/src/components/identity/src/service_discovery.rs diff --git a/core/src/components/identity/src/mod.rs b/core/src/components/identity/src/mod.rs index 54134144..f957b31e 100644 --- a/core/src/components/identity/src/mod.rs +++ b/core/src/components/identity/src/mod.rs @@ -1,3 +1,5 @@ pub mod helpers; pub mod structs; -pub mod enums; \ No newline at end of file +pub mod enums; +#[cfg(feature = "experimental")] +pub mod service_discovery; \ No newline at end of file diff --git a/core/src/components/identity/src/service_discovery.rs b/core/src/components/identity/src/service_discovery.rs new file mode 100644 index 00000000..bc43f3d7 --- /dev/null +++ b/core/src/components/identity/src/service_discovery.rs @@ -0,0 +1,297 @@ +#[cfg(feature = "experimental")] +use anyhow::Error; +#[cfg(feature = "experimental")] +use k8s_openapi::api::core::v1::Pod; +#[cfg(feature = "experimental")] +use kube::api::ObjectList; +#[cfg(feature = "experimental")] +use kube::{Api, Client}; +#[cfg(feature = "experimental")] +use std::fs; +#[cfg(feature = "experimental")] +use tokio::time; + +#[cfg(feature = "experimental")] +pub async fn scan_cgroup_paths(path: String) -> Result, Error> { + let mut cgroup_paths: Vec = Vec::new(); + let default_path = "/sys/fs/cgroup/kubepods.slice".to_string(); + + let target_path = if fs::metadata(&path).is_err() { + error!("Using default path: {}", &default_path); + default_path + } else { + path + }; + let entries = match fs::read_dir(&target_path) { + Ok(entries) => entries, + Err(e) => { + error!( + "Error reading cgroup directory {:?}: {}", + &target_path.clone(), + e + ); + return Ok(cgroup_paths); + } + }; + for entry in entries { + if let Ok(entry) = entry { + let path = entry.path(); + if path.is_dir() { + if let Some(path_str) = path.to_str() { + cgroup_paths.push(path_str.to_string()); + } + } + } + } + + Ok(cgroup_paths) +} + +#[cfg(feature = "experimental")] +struct ServiceIdentity { + uid: String, + container_id: String, +} + +#[cfg(feature = "experimental")] +pub async fn scan_cgroup_cronjob(time_delta: u64) -> Result<(), Error> { + let interval = std::time::Duration::from_secs(time_delta); + loop { + let scanned_paths = scan_cgroup_paths("/sys/fs/cgroup/kubelet.slice".to_string()) + .await + .expect("An error occured during the cgroup scan"); + //--> this should return : + // /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice + // /sys/fs/cgroup/kubelet.slice/kubelet.service + let mut scanned_subpaths = Vec::::new(); + for path in scanned_paths { + //info!("Scanned cgroup path: {}", path); + // scan the subgroups + let subpaths = scan_cgroup_paths(path.to_string()).await; + match subpaths { + Ok(paths) => { + for subpath in paths { + scanned_subpaths.push(subpath); + } + // ---> this should return the cgroups files and also : + // kubelet-kubepods-burstable.slice + // kubelet-kubepods-besteffort.slice + + // this directories needs to be scanned again to get further information about the pods + // for example: + // kubelet-kubepods-besteffort-pod088f8704_24f0_4636_a8e2_13f75646f370.slice + // where pod088f8704_24f0_4636_a8e2_13f75646f370 is the pod UID + } + Err(e) => { + error!("An error occured during the cgroup subpath scan: {}", e); + continue; + } + } + } + + let mut scanned_subpaths_v2 = Vec::::new(); + // second cgroup scan level to get the pod UIDs + for scanned_subpath in &scanned_subpaths { + let subpaths_v2 = scan_cgroup_paths(scanned_subpath.to_string()).await; + match subpaths_v2 { + Ok(paths) => { + for sub2 in paths { + info!("Debugging sub2: {}", &sub2); //return e.g. /sys/fs/cgroup/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-podb8701d38_3791_422d_ad15_890ad1a0844b.slice/docker-f2e265659293676231ecb38fafccc97b1a42b75be192c32a602bc8ea579dc866.scope + scanned_subpaths_v2.push(sub2); + // this contains the addressed like this + //kubelet-kubepods-besteffort-pod088f8704_24f0_4636_a8e2_13f75646f370.slice + } + } + Err(e) => { + error!("An error occured during the cgroup subpath v2 scan: {}", e); + continue; + } + } + } + + let mut uids = Vec::::new(); + let mut identites = Vec::::new(); + + //read the subpaths to extract the pod uid + for subpath in scanned_subpaths_v2 { + let uid = extract_pod_uid(subpath.clone()) + .expect("An error occured during the extraction of pod UIDs"); + let container_id = extract_container_id(subpath.clone()) + .expect("An error occured during the extraction of the docker container id"); + debug!("Debugging extracted UID: {:?}", &uid); + // create a linked list for each service + let service_identity = ServiceIdentity { uid, container_id }; + identites.push(service_identity); //push the linked list in a vector of ServiceIdentity structure. Each struct contains the uid and the container id + } + + // get pod information from UID and store the info in an HashMqp for O(1) access + let service_map = get_pod_info().await?; + + //info!("Debugging Identites vector: {:?}", identites); + for service in identites { + let name = service_cache(service_map.clone(), service.uid.clone()); + let uid = service.uid; + let id = service.container_id; + info!( + "[Identity]: name: {:?} uid: {:?} docker container id {:?} ", + name, uid, id + ); + } + + info!( + "Cronjob completed a cgroup scan cycle. Next scan will be in {} seconds", + time_delta + ); + time::sleep(interval).await; + } +} +#[cfg(feature = "experimental")] +fn service_cache(service_map: HashMap, uid: String) -> String { + service_map.get(&uid).cloned().unwrap_or_else(|| { + error!("Service not found for uid: {}", uid); + "unknown".to_string() + }) +} +#[cfg(feature = "experimental")] +fn extract_container_id(cgroup_path: String) -> Result { + let splits: Vec<&str> = cgroup_path.split("/").collect(); + + let index = extract_target_from_splits(splits.clone(), "docker-")?; + let docker_id_split = splits[index] + .trim_start_matches("docker-") + .trim_end_matches(".scope"); + Ok(docker_id_split.to_string()) +} + +// IDEA: add cgroup docker process mapping in ServiceIdentity structure +#[cfg(feature = "experimental")] +fn extract_pod_uid(cgroup_path: String) -> Result { + // example of cgroup path: + // /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-besteffort.slice/kubelet-kubepods-besteffort-pod93580201_87d5_44e6_9779_f6153ca17637.slice + // or + // /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-burstable.slice/kubelet-kubepods-burstable-poddd3a1c6b_af40_41b1_8e1c_9e31fe8d96cb.slice + + // split the path by "/" + let splits: Vec<&str> = cgroup_path.split("/").collect(); + debug!("Debugging splits: {:?}", &splits); + + let index = extract_target_from_splits(splits.clone(), "-pod")?; + + let pod_split = splits[index] + .trim_start_matches("kubelet-kubepods-besteffort-") + .trim_start_matches("kubelet-kubepods-burstable-") + .trim_start_matches("kubepods-besteffort-") + .trim_start_matches("kubepods-burstable-"); + + let uid_ = pod_split + .trim_start_matches("pod") + .trim_end_matches(".slice"); //return uids with underscore (_) [ex.dd3a1c6b_af40_41b1_8e1c_9e31fe8d96cb] + + let uid = uid_.replace("_", "-"); + Ok(uid.to_string()) +} +#[cfg(feature = "experimental")] +fn extract_target_from_splits(splits: Vec<&str>, target: &str) -> Result { + for (index, split) in splits.iter().enumerate() { + // find the split that contains the word 'pod' + if split.contains(target) { + debug!("Target index; {}", index); + return Ok(index); + } + } + Err(Error::msg("'-pod' word not found in split")) +} + +/* unfortunately you cannot query the pods using the uids directly from ListParams */ +#[cfg(feature = "experimental")] +async fn query_all_pods() -> Result, Error> { + let client = Client::try_default() + .await + .expect("Cannot connect to kubernetes client"); + let pods: Api = Api::all(client); + let lp = kube::api::ListParams::default(); // default list params + let pod_list = pods + .list(&lp) + .await + .expect("An error occured during the pod list extraction"); + + Ok(pod_list) +} + +// fast pod caching system +#[cfg(feature = "experimental")] +async fn get_pod_info() -> Result, Error> { + let all_pods = query_all_pods().await?; + + let mut service_map = HashMap::::new(); + + for pod in all_pods { + if let (Some(name), Some(uid)) = (pod.metadata.name, pod.metadata.uid) { + service_map.insert(uid, name); + } + } // insert the pod name and uid from the KubeAPI + + Ok(service_map) +} + +#[cfg(feature = "experimental")] +mod tests { + use tracing_subscriber::fmt::format; + + use crate::helpers::{extract_container_id, extract_pod_uid, extract_target_from_splits}; + + #[test] + fn extract_uid_from_string() { + let cgroup_paths = vec!["/sys/fs/cgroup/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod231bd2d7_0f09_4781_a4e1_e4ea026342dd.slice".to_string(), + "/sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-besteffort.slice/kubelet-kubepods-besteffort-pod231bd2d7_0f09_4781_a4e1_e4ea026342dd.slice".to_string()]; + + let mut uid_vec = Vec::::new(); + + for cgroup_path in cgroup_paths { + let uid = extract_pod_uid(cgroup_path) + .map_err(|e| format!("An error occured {}", e)) + .unwrap(); + uid_vec.push(uid); + } + + let check = vec![ + "231bd2d7-0f09-4781-a4e1-e4ea026342dd".to_string(), + "231bd2d7-0f09-4781-a4e1-e4ea026342dd".to_string(), + ]; + + assert_eq!(uid_vec, check); + } + + #[test] + fn test_extract_target_index() { + let cgroup_paths = vec!["/sys/fs/cgroup/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod231bd2d7_0f09_4781_a4e1_e4ea026342dd.slice".to_string(), + "/sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-besteffort.slice/kubelet-kubepods-besteffort-pod231bd2d7_0f09_4781_a4e1_e4ea026342dd.slice".to_string()]; + + let mut index_vec = Vec::::new(); + for cgroup_path in cgroup_paths { + let splits: Vec<&str> = cgroup_path.split("/").collect(); + + let target_index = extract_target_from_splits(splits, "-pod").unwrap(); + index_vec.push(target_index); + } + let index_check = vec![6, 7]; + assert_eq!(index_vec, index_check); + } + + #[test] + fn extract_docker_id() { + let cgroup_paths = vec!["/sys/fs/cgroup/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod17fd3f7c_37e4_4009_8c38_e58b30691af3.slice/docker-13abd64c0ba349975a762476c9703b642d18077eabeb3aa1d941132048afc861.scope".to_string(), + "/sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-besteffort.slice/kubelet-kubepods-besteffort-pod17fd3f7c_37e4_4009_8c38_e58b30691af3.slice/docker-13abd64c0ba349975a762476c9703b642d18077eabeb3aa1d941132048afc861.scope".to_string()]; + + let mut id_vec = Vec::::new(); + for cgroup_path in cgroup_paths { + let id = extract_container_id(cgroup_path).unwrap(); + id_vec.push(id); + } + let id_check = vec![ + "13abd64c0ba349975a762476c9703b642d18077eabeb3aa1d941132048afc861".to_string(), + "13abd64c0ba349975a762476c9703b642d18077eabeb3aa1d941132048afc861".to_string(), + ]; + assert_eq!(id_vec, id_check); + } +} From 370ca7875de59dc86ef95e86ab76ef64deef8bbb Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Sun, 1 Feb 2026 16:53:18 +0100 Subject: [PATCH 20/24] [refactoring]: created BufferType enum to centralize the event readers for PacketLog, VethLog and TcpPacketRegistry --- core/src/components/identity/src/helpers.rs | 702 ++++++++------------ core/src/components/identity/src/main.rs | 26 +- 2 files changed, 279 insertions(+), 449 deletions(-) diff --git a/core/src/components/identity/src/helpers.rs b/core/src/components/identity/src/helpers.rs index a0aa72ea..8e33db74 100644 --- a/core/src/components/identity/src/helpers.rs +++ b/core/src/components/identity/src/helpers.rs @@ -33,88 +33,6 @@ impl TryFrom for IpProtocols { } } -/* helper functions to read and log net events in the container */ -pub async fn display_events>( - mut perf_buffers: Vec>, - mut buffers: Vec, -) { - // FIXME: here maybe we need to use a loop with tokio::select - loop { - for buf in perf_buffers.iter_mut() { - match buf.read_events(&mut buffers) { - std::result::Result::Ok(events) => { - let offset = 0 as usize; - if events.read > 0 { - debug!("Read {} events", events.read); - } - if events.lost > 0 { - debug!("Lost events: {}", events.lost); - } - for i in offset..events.read { - let data = &buffers[i]; - if data.len() < std::mem::size_of::() { - let failed_events_span = - span!(tracing::Level::INFO, "corrupted_packets_events"); - let _enter: span::Entered<'_> = failed_events_span.enter(); - event!( - tracing::Level::WARN, - "Corrupted data. data_len = {} data_ptr = {}. Min size required: {} bytes", - data.len(), - data.as_ptr() as usize, - std::mem::size_of::() - ); - continue; - } - if data.len() >= std::mem::size_of::() { - let pl: PacketLog = - unsafe { std::ptr::read(data.as_ptr() as *const _) }; - let src = reverse_be_addr(pl.src_ip); - let dst = reverse_be_addr(pl.dst_ip); - let src_port = u16::from_be(pl.src_port); - let dst_port = u16::from_be(pl.dst_port); - let event_id = pl.pid; - - match IpProtocols::try_from(pl.proto) { - std::result::Result::Ok(proto) => { - let packets_events_span = span!(tracing::Level::INFO, "packets_event",event_id=%event_id, protocol = %format!("{:?}", proto)); - let _enter = packets_events_span.enter(); - event!( - tracing::Level::INFO, - "Event Id: {} Protocol: {:?} SRC: {}:{} -> DST: {}:{}", - event_id, - proto, - src, - src_port, - dst, - dst_port - ); - } - Err(e) => { - let failed_packets_events_span = span!(tracing::Level::INFO, "failed_packets_event", event_id=%event_id, protocol = %pl.proto); - let _enter = failed_packets_events_span.enter(); - event!( - tracing::Level::INFO, - "Event Id: {} Protocol: Unknown ({}). Error: {:?}", - event_id, - pl.proto, - e - ) - } - }; - } else { - warn!("Received packet data too small: {} bytes", data.len()); - } - } - } - Err(e) => { - error!("Error reading events: {:?}", e); - } - } - } - tokio::time::sleep(std::time::Duration::from_millis(100)).await; - } -} - // docs: // This function perform a byte swap from little-endian to big-endian // It's used to reconstruct the correct IPv4 address from the u32 representation @@ -350,386 +268,286 @@ async fn attach_detach_veth( Ok(()) } -/* helper functions to display events from the TcpPacketRegistry structure */ -pub async fn display_tcp_registry_events>( - mut perf_buffers: Vec>, - mut buffers: Vec, -) { - // FIXME: here maybe we need to use a loop with tokio::select - loop { - for buf in perf_buffers.iter_mut() { - match buf.read_events(&mut buffers) { - std::result::Result::Ok(events) => { - let offset = 0; - for i in offset..events.read { - let data = &buffers[i]; - if data.len() < std::mem::size_of::() { - let failed_tcp_events_span = - span!(tracing::Level::INFO, "failed_tcp_registry_event"); - let _enter: span::Entered<'_> = failed_tcp_events_span.enter(); - event!( - tracing::Level::WARN, - "Corrupted data. data_len = {} data_ptr = {}. Min size required: {} bytes", - data.len(), - data.as_ptr() as usize, - std::mem::size_of::() - ); - continue; - } - if data.len() >= std::mem::size_of::() { - let tcp_pl: TcpPacketRegistry = - unsafe { std::ptr::read(data.as_ptr() as *const _) }; - //TODO: can this pattern be safe? - let src = reverse_be_addr(tcp_pl.src_ip); - let dst = reverse_be_addr(tcp_pl.dst_ip); - let src_port = u16::from_be(tcp_pl.src_port); - let dst_port = u16::from_be(tcp_pl.dst_port); - let event_id = tcp_pl.pid; - let command = tcp_pl.command.to_vec(); - let end = command - .iter() - .position(|&x| x == 0) - .unwrap_or(command.len()); - let command_str = String::from_utf8_lossy(&command[..end]).to_string(); - let cgroup_id = tcp_pl.cgroup_id; - - match IpProtocols::try_from(tcp_pl.proto) { - std::result::Result::Ok(proto) => { - let tcp_events_span = span!(tracing::Level::INFO, "tcp_registry_event", command = %command_str.as_str(), cgroup_id = %cgroup_id); - let _enter = tcp_events_span.enter(); - event!( - tracing::Level::INFO, - "Event Id: {} Protocol: {:?} SRC: {}:{} -> DST: {}:{} Command: {} Cgroup_id: {}", - event_id, - proto, - src, - src_port, - dst, - dst_port, - command_str, - cgroup_id //proc_content - ); - } - Err(e) => { - event!( - tracing::Level::INFO, - "Event Id: {} Protocol: Unknown ({}) Command: {} Cgroup_id: {} Error: {:?}", - event_id, - tcp_pl.proto, - command_str, - cgroup_id, - e - ); - } - }; - } else { - warn!("Received packet data too small: {} bytes", data.len()); - } + +// enum BuffersType +pub enum BufferType { + PacketLog, + TcpPacketRegistry, + VethLog, +} + +// TODO: add variant for OTEL log exporters +impl BufferType { + async fn read_packet_log(buffers: &mut [BytesMut], tot_events: i32, offset: i32) { + for i in offset..tot_events { + let vec_bytes = &buffers[i as usize]; + if vec_bytes.len() < std::mem::size_of::() { + error!( + "Corrupted data. Readed {:?} bytes expected {} bytes", + vec_bytes, + std::mem::size_of::() + ) + } + if vec_bytes.len() >= std::mem::size_of::() { + let pl: PacketLog = + unsafe { std::ptr::read_unaligned(vec_bytes.as_ptr() as *const _) }; // reading raw bytes + + // extracting struct info from bytes + let src_ip = reverse_be_addr(pl.src_ip); + let dst_ip = reverse_be_addr(pl.dst_ip); + let src_port = u16::from_be(pl.src_port); + let dst_port = u16::from_be(pl.dst_port); + let event_id = pl.pid; + let protocol = pl.proto; + + // protocol extraction + match IpProtocols::try_from(protocol) { + Ok(proto) => { + info!( + "Event Id: {} Protocol: {:?} SRC: {}:{} -> DST: {}:{}", + event_id, proto, src_ip, src_port, dst_ip, dst_port + ); + } + Err(e) => { + error!("Unknown protocol. Data maybe corrupted. Reason:{:?}", e); } - } - Err(e) => { - error!("Error reading events: {:?}", e); } } } - tokio::time::sleep(std::time::Duration::from_millis(100)).await; } -} - -#[cfg(feature = "experimental")] -use anyhow::Error; -#[cfg(feature = "experimental")] -use k8s_openapi::api::core::v1::Pod; -#[cfg(feature = "experimental")] -use kube::api::ObjectList; -#[cfg(feature = "experimental")] -use kube::{Api, Client}; -#[cfg(feature = "experimental")] -use std::fs; -#[cfg(feature = "experimental")] -use tokio::time; - -#[cfg(feature = "experimental")] -pub async fn scan_cgroup_paths(path: String) -> Result, Error> { - let mut cgroup_paths: Vec = Vec::new(); - let default_path = "/sys/fs/cgroup/kubepods.slice".to_string(); - - let target_path = if fs::metadata(&path).is_err() { - error!("Using default path: {}", &default_path); - default_path - } else { - path - }; - let entries = match fs::read_dir(&target_path) { - Ok(entries) => entries, - Err(e) => { - error!( - "Error reading cgroup directory {:?}: {}", - &target_path.clone(), - e - ); - return Ok(cgroup_paths); + async fn read_tcp_registry_log(buffers: &mut [BytesMut], tot_events: i32, offset: i32) { + for i in offset..tot_events { + let vec_bytes = &buffers[i as usize]; + if vec_bytes.len() < std::mem::size_of::() { + error!( + "Corrupted data. Readed {:?} bytes expected {} bytes", + vec_bytes, + std::mem::size_of::() + ) + } + if vec_bytes.len() >= std::mem::size_of::() { + let pl: TcpPacketRegistry = + unsafe { std::ptr::read_unaligned(vec_bytes.as_ptr() as *const _) }; // reading raw bytes + + // extracting struct info from bytes + let src = reverse_be_addr(pl.src_ip); + let dst = reverse_be_addr(pl.dst_ip); + let src_port = u16::from_be(pl.src_port); + let dst_port = u16::from_be(pl.dst_port); + let event_id = pl.pid; + let command = pl.command.to_vec(); + let end = command + .iter() + .position(|&x| x == 0) + .unwrap_or(command.len()); + let command_str = String::from_utf8_lossy(&command[..end]).to_string(); + let cgroup_id = pl.cgroup_id; + let protocol = pl.proto; + + // protocol extraction + match IpProtocols::try_from(protocol) { + Ok(proto) => { + info!( + "Event Id: {} Protocol: {:?} SRC: {}:{} -> DST: {}:{} Command: {} Cgroup_id: {}", + event_id, + proto, + src, + src_port, + dst, + dst_port, + command_str, + cgroup_id //proc_content + ); + } + Err(e) => { + error!("Unknown protocol. Data maybe corrupted. Reason:{:?}", e); + } + } + } } - }; - for entry in entries { - if let Ok(entry) = entry { - let path = entry.path(); - if path.is_dir() { - if let Some(path_str) = path.to_str() { - cgroup_paths.push(path_str.to_string()); + } + async fn read_and_handle_veth_log( + //link_ids: Arc>>, + //bpf: Arc>, + buffers: &mut [BytesMut], + tot_events: i32, + offset: i32, + ) { + for i in offset..tot_events { + let vec_bytes = &buffers[i as usize]; + if vec_bytes.len() < std::mem::size_of::() { + error!( + "Corrupted data. Readed {:?} bytes expected {} bytes", + vec_bytes, + std::mem::size_of::() + ) + } + if vec_bytes.len() >= std::mem::size_of::() { + let pl: VethLog = + unsafe { std::ptr::read_unaligned(vec_bytes.as_ptr() as *const _) }; // reading raw bytes + + // extracting struct info from bytes + let name_bytes = pl.name; + + let dev_addr_bytes = pl.dev_addr; + let name = std::str::from_utf8(&name_bytes); + let state = pl.state; + + let dev_addr = dev_addr_bytes; + let netns = pl.netns; + let mut event_type = String::new(); + + // event_type extraction + match pl.event_type { + 1 => { + event_type = "creation".to_string(); + match name { + Ok(veth_name) => { + info!( + "[{}] Veth Event: Type: {} Name: {} Dev_addr: {:x?} State: {}", + netns, + event_type, + veth_name.trim_end_matches("\0"), + dev_addr, + state + ); + // TODO: this logic needs to live in a separate space + //match attach_detach_veth( + // bpf.clone(), + // 1, + // veth_name, + // link_ids.clone(), + //) + //.await + //{ + // Ok(_) => { + // info!( + // "[{}] Successfully attached Attach/Detach function for veth: {}", + // netns, + // veth_name.trim_end_matches("\0") + // ); + // } + // Err(e) => { + // info!( + // "[{}] Error attaching Attach/Detach function. Error : {}", + // netns, e + // ); + // } + //} + } + Err(e) => { + error!( + "Failed to extract veth name during event_type = creation (1).Reason:{}", + e + ); + } + } + } + 2 => { + event_type = "deletion".to_string(); + match name { + Ok(veth_name) => { + info!( + "[{}] Veth Event: Type: {} Name: {} Dev_addr: {:x?} State: {}", + netns, + event_type, + veth_name.trim_end_matches("\0"), + dev_addr, + state + ); + // TODO: this logic needs to live in a separate space + //match attach_detach_veth( + // bpf.clone(), + // 2, + // veth_name, + // link_ids.clone(), + //) + //.await + //{ + // Ok(_) => { + // info!( + // "[{}] Successfully attached Attach/Detach function for veth: {}", + // netns, + // veth_name.trim_end_matches("\0") + // ); + // } + // Err(e) => { + // info!( + // "[{}] Error attaching Attach/Detach function. Error : {}", + // netns, e + // ); + // } + // } + } + Err(e) => { + error!( + "Failed to extract veth name during event_type = deletion (2).Reason:{}", + e + ); + } + } + } + _ => { + warn!("Unknown event type") + } } } } } - - Ok(cgroup_paths) } -#[cfg(feature = "experimental")] -struct ServiceIdentity { - uid: String, - container_id: String, -} +// docs: read buffer function: +// template function that take a mut perf_event_array_buffer of type T and a mutable buffer of Vec -#[cfg(feature = "experimental")] -pub async fn scan_cgroup_cronjob(time_delta: u64) -> Result<(), Error> { - let interval = std::time::Duration::from_secs(time_delta); +pub async fn read_perf_buffer>( + //bpf: Arc>, // this is only for read_and_handle_veth_logs fn + //link_ids: Arc>>, // this is only for read_and_handle_veth_logs fn + mut array_buffers: Vec>, + mut buffers: Vec, + buffer_type: BufferType, +) { + // loop over the buffers loop { - let scanned_paths = scan_cgroup_paths("/sys/fs/cgroup/kubelet.slice".to_string()) - .await - .expect("An error occured during the cgroup scan"); - //--> this should return : - // /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice - // /sys/fs/cgroup/kubelet.slice/kubelet.service - let mut scanned_subpaths = Vec::::new(); - for path in scanned_paths { - //info!("Scanned cgroup path: {}", path); - // scan the subgroups - let subpaths = scan_cgroup_paths(path.to_string()).await; - match subpaths { - Ok(paths) => { - for subpath in paths { - scanned_subpaths.push(subpath); + for buf in array_buffers.iter_mut() { + match buf.read_events(&mut buffers) { + Ok(events) => { + // triggered if some events are lost + if events.lost > 0 { + tracing::debug!("Lost events: {} ", events.lost); } - // ---> this should return the cgroups files and also : - // kubelet-kubepods-burstable.slice - // kubelet-kubepods-besteffort.slice - - // this directories needs to be scanned again to get further information about the pods - // for example: - // kubelet-kubepods-besteffort-pod088f8704_24f0_4636_a8e2_13f75646f370.slice - // where pod088f8704_24f0_4636_a8e2_13f75646f370 is the pod UID - } - Err(e) => { - error!("An error occured during the cgroup subpath scan: {}", e); - continue; - } - } - } - - let mut scanned_subpaths_v2 = Vec::::new(); - // second cgroup scan level to get the pod UIDs - for scanned_subpath in &scanned_subpaths { - let subpaths_v2 = scan_cgroup_paths(scanned_subpath.to_string()).await; - match subpaths_v2 { - Ok(paths) => { - for sub2 in paths { - info!("Debugging sub2: {}", &sub2); //return e.g. /sys/fs/cgroup/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-podb8701d38_3791_422d_ad15_890ad1a0844b.slice/docker-f2e265659293676231ecb38fafccc97b1a42b75be192c32a602bc8ea579dc866.scope - scanned_subpaths_v2.push(sub2); - // this contains the addressed like this - //kubelet-kubepods-besteffort-pod088f8704_24f0_4636_a8e2_13f75646f370.slice + // triggered if some events are readed + if events.read > 0 { + tracing::debug!("Readed events: {}", events.read); + let offset = 0; + let tot_events = events.read as i32; + + //read the events in the buffer + match buffer_type { + BufferType::PacketLog => { + BufferType::read_packet_log(&mut buffers, tot_events, offset).await + } + BufferType::TcpPacketRegistry => { + BufferType::read_tcp_registry_log(&mut buffers, tot_events, offset) + .await + } + BufferType::VethLog => { + BufferType::read_and_handle_veth_log( + //link_ids.clone(), + //bpf.clone(), + &mut buffers, + tot_events, + offset, + ) + .await + } + } } } Err(e) => { - error!("An error occured during the cgroup subpath v2 scan: {}", e); - continue; + error!("Cannot read events from buffer. Reason: {} ", e); } } } - - let mut uids = Vec::::new(); - let mut identites = Vec::::new(); - - //read the subpaths to extract the pod uid - for subpath in scanned_subpaths_v2 { - let uid = extract_pod_uid(subpath.clone()) - .expect("An error occured during the extraction of pod UIDs"); - let container_id = extract_container_id(subpath.clone()) - .expect("An error occured during the extraction of the docker container id"); - debug!("Debugging extracted UID: {:?}", &uid); - // create a linked list for each service - let service_identity = ServiceIdentity { uid, container_id }; - identites.push(service_identity); //push the linked list in a vector of ServiceIdentity structure. Each struct contains the uid and the container id - } - - // get pod information from UID and store the info in an HashMqp for O(1) access - let service_map = get_pod_info().await?; - - //info!("Debugging Identites vector: {:?}", identites); - for service in identites { - let name = service_cache(service_map.clone(), service.uid.clone()); - let uid = service.uid; - let id = service.container_id; - info!( - "[Identity]: name: {:?} uid: {:?} docker container id {:?} ", - name, uid, id - ); - } - - info!( - "Cronjob completed a cgroup scan cycle. Next scan will be in {} seconds", - time_delta - ); - time::sleep(interval).await; - } -} -#[cfg(feature = "experimental")] -fn service_cache(service_map: HashMap, uid: String) -> String { - service_map.get(&uid).cloned().unwrap_or_else(|| { - error!("Service not found for uid: {}", uid); - "unknown".to_string() - }) -} -#[cfg(feature = "experimental")] -fn extract_container_id(cgroup_path: String) -> Result { - let splits: Vec<&str> = cgroup_path.split("/").collect(); - - let index = extract_target_from_splits(splits.clone(), "docker-")?; - let docker_id_split = splits[index] - .trim_start_matches("docker-") - .trim_end_matches(".scope"); - Ok(docker_id_split.to_string()) -} - -// IDEA: add cgroup docker process mapping in ServiceIdentity structure -#[cfg(feature = "experimental")] -fn extract_pod_uid(cgroup_path: String) -> Result { - // example of cgroup path: - // /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-besteffort.slice/kubelet-kubepods-besteffort-pod93580201_87d5_44e6_9779_f6153ca17637.slice - // or - // /sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-burstable.slice/kubelet-kubepods-burstable-poddd3a1c6b_af40_41b1_8e1c_9e31fe8d96cb.slice - - // split the path by "/" - let splits: Vec<&str> = cgroup_path.split("/").collect(); - debug!("Debugging splits: {:?}", &splits); - - let index = extract_target_from_splits(splits.clone(), "-pod")?; - - let pod_split = splits[index] - .trim_start_matches("kubelet-kubepods-besteffort-") - .trim_start_matches("kubelet-kubepods-burstable-") - .trim_start_matches("kubepods-besteffort-") - .trim_start_matches("kubepods-burstable-"); - - let uid_ = pod_split - .trim_start_matches("pod") - .trim_end_matches(".slice"); //return uids with underscore (_) [ex.dd3a1c6b_af40_41b1_8e1c_9e31fe8d96cb] - - let uid = uid_.replace("_", "-"); - Ok(uid.to_string()) -} -#[cfg(feature = "experimental")] -fn extract_target_from_splits(splits: Vec<&str>, target: &str) -> Result { - for (index, split) in splits.iter().enumerate() { - // find the split that contains the word 'pod' - if split.contains(target) { - debug!("Target index; {}", index); - return Ok(index); - } - } - Err(Error::msg("'-pod' word not found in split")) -} - -/* unfortunately you cannot query the pods using the uids directly from ListParams */ -#[cfg(feature = "experimental")] -async fn query_all_pods() -> Result, Error> { - let client = Client::try_default() - .await - .expect("Cannot connect to kubernetes client"); - let pods: Api = Api::all(client); - let lp = kube::api::ListParams::default(); // default list params - let pod_list = pods - .list(&lp) - .await - .expect("An error occured during the pod list extraction"); - - Ok(pod_list) -} - -// fast pod caching system -#[cfg(feature = "experimental")] -async fn get_pod_info() -> Result, Error> { - let all_pods = query_all_pods().await?; - - let mut service_map = HashMap::::new(); - - for pod in all_pods { - if let (Some(name), Some(uid)) = (pod.metadata.name, pod.metadata.uid) { - service_map.insert(uid, name); - } - } // insert the pod name and uid from the KubeAPI - - Ok(service_map) -} - -#[cfg(feature = "experimental")] -mod tests { - use tracing_subscriber::fmt::format; - - use crate::helpers::{extract_container_id, extract_pod_uid, extract_target_from_splits}; - - #[test] - fn extract_uid_from_string() { - let cgroup_paths = vec!["/sys/fs/cgroup/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod231bd2d7_0f09_4781_a4e1_e4ea026342dd.slice".to_string(), - "/sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-besteffort.slice/kubelet-kubepods-besteffort-pod231bd2d7_0f09_4781_a4e1_e4ea026342dd.slice".to_string()]; - - let mut uid_vec = Vec::::new(); - - for cgroup_path in cgroup_paths { - let uid = extract_pod_uid(cgroup_path) - .map_err(|e| format!("An error occured {}", e)) - .unwrap(); - uid_vec.push(uid); - } - - let check = vec![ - "231bd2d7-0f09-4781-a4e1-e4ea026342dd".to_string(), - "231bd2d7-0f09-4781-a4e1-e4ea026342dd".to_string(), - ]; - - assert_eq!(uid_vec, check); - } - - #[test] - fn test_extract_target_index() { - let cgroup_paths = vec!["/sys/fs/cgroup/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod231bd2d7_0f09_4781_a4e1_e4ea026342dd.slice".to_string(), - "/sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-besteffort.slice/kubelet-kubepods-besteffort-pod231bd2d7_0f09_4781_a4e1_e4ea026342dd.slice".to_string()]; - - let mut index_vec = Vec::::new(); - for cgroup_path in cgroup_paths { - let splits: Vec<&str> = cgroup_path.split("/").collect(); - - let target_index = extract_target_from_splits(splits, "-pod").unwrap(); - index_vec.push(target_index); - } - let index_check = vec![6, 7]; - assert_eq!(index_vec, index_check); - } - - #[test] - fn extract_docker_id() { - let cgroup_paths = vec!["/sys/fs/cgroup/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod17fd3f7c_37e4_4009_8c38_e58b30691af3.slice/docker-13abd64c0ba349975a762476c9703b642d18077eabeb3aa1d941132048afc861.scope".to_string(), - "/sys/fs/cgroup/kubelet.slice/kubelet-kubepods.slice/kubelet-kubepods-besteffort.slice/kubelet-kubepods-besteffort-pod17fd3f7c_37e4_4009_8c38_e58b30691af3.slice/docker-13abd64c0ba349975a762476c9703b642d18077eabeb3aa1d941132048afc861.scope".to_string()]; - - let mut id_vec = Vec::::new(); - for cgroup_path in cgroup_paths { - let id = extract_container_id(cgroup_path).unwrap(); - id_vec.push(id); - } - let id_check = vec![ - "13abd64c0ba349975a762476c9703b642d18077eabeb3aa1d941132048afc861".to_string(), - "13abd64c0ba349975a762476c9703b642d18077eabeb3aa1d941132048afc861".to_string(), - ]; - assert_eq!(id_vec, id_check); + tokio::time::sleep(std::time::Duration::from_millis(100)).await; // small sleep } } diff --git a/core/src/components/identity/src/main.rs b/core/src/components/identity/src/main.rs index b477388b..c1efb6c1 100644 --- a/core/src/components/identity/src/main.rs +++ b/core/src/components/identity/src/main.rs @@ -11,10 +11,9 @@ mod enums; mod helpers; mod structs; +mod service_discovery; -use crate::helpers::{ - display_events, display_tcp_registry_events, display_veth_events, get_veth_channels, -}; +use crate::helpers::{get_veth_channels, read_perf_buffer}; use aya::{ Ebpf, maps::{Map, perf::PerfEventArray}, @@ -87,7 +86,7 @@ async fn main() -> Result<(), anyhow::Error> { info!("Found interfaces: {:?}", interfaces); - { + { populate_blocklist().await?; } @@ -259,15 +258,28 @@ async fn event_listener( // spawn async tasks let veth_events_displayer = tokio::spawn(async move { - display_veth_events(bpf.clone(), perf_veth_buffer, veth_buffers, veth_link_ids).await; + //display_veth_events(bpf.clone(), perf_veth_buffer, veth_buffers, veth_link_ids).await; + read_perf_buffer(perf_veth_buffer, veth_buffers, helpers::BufferType::VethLog).await; }); let net_events_displayer = tokio::spawn(async move { - display_events(perf_net_events_buffer, events_buffers).await; + //display_events(perf_net_events_buffer, events_buffers).await; + read_perf_buffer( + perf_net_events_buffer, + events_buffers, + helpers::BufferType::PacketLog, + ) + .await; }); let tcp_registry_events_displayer: tokio::task::JoinHandle<()> = tokio::spawn(async move { - display_tcp_registry_events(tcp_registry_buffer, tcp_buffers).await; + //display_tcp_registry_events(tcp_registry_buffer, tcp_buffers).await; + read_perf_buffer( + tcp_registry_buffer, + tcp_buffers, + helpers::BufferType::TcpPacketRegistry, + ) + .await; }); #[cfg(feature = "experimental")] From 57cb4bb6a25274335abafb01cca5172d8d77c73d Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Sun, 1 Feb 2026 16:53:42 +0100 Subject: [PATCH 21/24] updated common cargo.toml --- core/common/Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/common/Cargo.toml b/core/common/Cargo.toml index eb94abec..8098c1fd 100644 --- a/core/common/Cargo.toml +++ b/core/common/Cargo.toml @@ -21,7 +21,9 @@ opentelemetry_sdk = { version = "0.31.0", features = ["logs", "rt-tokio"] } opentelemetry-stdout = { version = "0.31.0", features = ["logs"] } opentelemetry-appender-tracing = "0.31.1" opentelemetry-otlp = { version = "0.31.0", features = ["logs", "grpc-tonic"] } +bytemuck = "1.25.0" +bytes = "1.11.0" [features] map-handlers = [] -program-handlers = [] \ No newline at end of file +program-handlers = [] From 43fef8d3f3eb9e97cd13ba4abdb657a6185c2070 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Tue, 3 Feb 2026 11:28:48 +0100 Subject: [PATCH 22/24] [#158]: moved IpProtocols and network structures from the identity crate to the common crate. Added BufferType enum to list different buffers readers. Added buffer_type module in cortexbrain common (experimental) --- core/common/Cargo.toml | 4 + core/common/src/buffer_type.rs | 325 ++++++++++++++++++++ core/common/src/lib.rs | 5 +- core/src/components/identity/Cargo.toml | 12 +- core/src/components/identity/src/enums.rs | 12 - core/src/components/identity/src/helpers.rs | 39 +-- core/src/components/identity/src/lib.rs | 4 +- core/src/components/identity/src/main.rs | 16 +- core/src/components/identity/src/mod.rs | 2 - core/src/components/identity/src/structs.rs | 57 ---- 10 files changed, 353 insertions(+), 123 deletions(-) create mode 100644 core/common/src/buffer_type.rs delete mode 100644 core/src/components/identity/src/enums.rs delete mode 100644 core/src/components/identity/src/structs.rs diff --git a/core/common/Cargo.toml b/core/common/Cargo.toml index 8098c1fd..b8e840d0 100644 --- a/core/common/Cargo.toml +++ b/core/common/Cargo.toml @@ -23,7 +23,11 @@ opentelemetry-appender-tracing = "0.31.1" opentelemetry-otlp = { version = "0.31.0", features = ["logs", "grpc-tonic"] } bytemuck = "1.25.0" bytes = "1.11.0" +bytemuck_derive = "1.10.2" [features] map-handlers = [] program-handlers = [] +network-structs = [] +buffer-reader = [] +experimental = [] diff --git a/core/common/src/buffer_type.rs b/core/common/src/buffer_type.rs new file mode 100644 index 00000000..2c25ada3 --- /dev/null +++ b/core/common/src/buffer_type.rs @@ -0,0 +1,325 @@ +use bytemuck_derive::Zeroable; +use bytes::BytesMut; +use std::net::Ipv4Addr; +use tracing::{error, info, warn}; + +// +// IpProtocols enum to reconstruct the packet protocol based on the +// IPV4 Header Protocol code +// + +#[derive(Debug)] +#[repr(u8)] +pub enum IpProtocols { + ICMP = 1, + TCP = 6, + UDP = 17, +} + +// +// TryFrom Trait implementation for IpProtocols enum +// This is used to reconstruct the packet protocol based on the +// IPV4 Header Protocol code +// + +impl TryFrom for IpProtocols { + type Error = (); + fn try_from(proto: u8) -> Result { + match proto { + 1 => Ok(IpProtocols::ICMP), + 6 => Ok(IpProtocols::TCP), + 17 => Ok(IpProtocols::UDP), + _ => Err(()), + } + } +} + +// +// Structure PacketLog +//This structure is used to store the packet information +// +#[cfg(feature = "network-structs")] +#[repr(C)] +#[derive(Clone, Copy, Zeroable)] +pub struct PacketLog { + pub proto: u8, + pub src_ip: u32, + pub src_port: u16, + pub dst_ip: u32, + pub dst_port: u16, + pub pid: u32, +} +#[cfg(feature = "network-structs")] +unsafe impl aya::Pod for PacketLog {} + +#[cfg(feature = "network-structs")] +#[repr(C)] +#[derive(Clone, Copy)] +pub struct VethLog { + pub name: [u8; 16], // 16 bytes: veth interface name + pub state: u64, // 8 bytes: state variable (unsigned long in kernel) + pub dev_addr: [u32; 8], // 32 bytes: device address + pub event_type: u8, // 1 byte: 1 for veth creation, 2 for veth destruction + pub netns: u32, // 4 bytes: network namespace inode number + pub pid: u32, // 4 bytes: PID that triggered the event +} + +#[cfg(feature = "network-structs")] +#[repr(C)] +#[derive(Clone, Copy)] +pub struct TcpPacketRegistry { + pub proto: u8, + pub src_ip: u32, + pub dst_ip: u32, + pub src_port: u16, + pub dst_port: u16, + pub pid: u32, + pub command: [u8; 16], + pub cgroup_id: u64, +} + +// docs: +// This function perform a byte swap from little-endian to big-endian +// It's used to reconstruct the correct IPv4 address from the u32 representation +// +// Takes a u32 address in big-endian format and returns a Ipv4Addr with reversed octets +// +#[inline(always)] +pub fn reverse_be_addr(addr: u32) -> Ipv4Addr { + let octects = addr.to_be_bytes(); + let [a, b, c, d] = [octects[3], octects[2], octects[1], octects[0]]; + let reversed_ip = Ipv4Addr::new(a, b, c, d); + reversed_ip +} + + +// enum BuffersType +#[cfg(feature = "buffer-reader")] +pub enum BufferType { + PacketLog, + TcpPacketRegistry, + VethLog, +} + +// IDEA: this is an experimental implementation to centralize buffer reading logic +// TODO: add variant for cortexflow API exporter +#[cfg(feature = "buffer-reader")] +impl BufferType { + pub async fn read_packet_log(buffers: &mut [BytesMut], tot_events: i32, offset: i32) { + for i in offset..tot_events { + let vec_bytes = &buffers[i as usize]; + if vec_bytes.len() < std::mem::size_of::() { + error!( + "Corrupted data. Readed {:?} bytes expected {} bytes", + vec_bytes, + std::mem::size_of::() + ) + } + if vec_bytes.len() >= std::mem::size_of::() { + let pl: PacketLog = + unsafe { std::ptr::read_unaligned(vec_bytes.as_ptr() as *const _) }; // reading raw bytes + + // extracting struct info from bytes + let src_ip = reverse_be_addr(pl.src_ip); + let dst_ip = reverse_be_addr(pl.dst_ip); + let src_port = u16::from_be(pl.src_port); + let dst_port = u16::from_be(pl.dst_port); + let event_id = pl.pid; + let protocol = pl.proto; + + // protocol extraction + match IpProtocols::try_from(protocol) { + Ok(proto) => { + info!( + "Event Id: {} Protocol: {:?} SRC: {}:{} -> DST: {}:{}", + event_id, proto, src_ip, src_port, dst_ip, dst_port + ); + } + Err(e) => { + error!("Unknown protocol. Data maybe corrupted. Reason:{:?}", e); + } + } + } + } + } + pub async fn read_tcp_registry_log(buffers: &mut [BytesMut], tot_events: i32, offset: i32) { + for i in offset..tot_events { + let vec_bytes = &buffers[i as usize]; + if vec_bytes.len() < std::mem::size_of::() { + error!( + "Corrupted data. Readed {:?} bytes expected {} bytes", + vec_bytes, + std::mem::size_of::() + ) + } + if vec_bytes.len() >= std::mem::size_of::() { + let pl: TcpPacketRegistry = + unsafe { std::ptr::read_unaligned(vec_bytes.as_ptr() as *const _) }; // reading raw bytes + + // extracting struct info from bytes + let src = reverse_be_addr(pl.src_ip); + let dst = reverse_be_addr(pl.dst_ip); + let src_port = u16::from_be(pl.src_port); + let dst_port = u16::from_be(pl.dst_port); + let event_id = pl.pid; + let command = pl.command.to_vec(); + let end = command + .iter() + .position(|&x| x == 0) + .unwrap_or(command.len()); + let command_str = String::from_utf8_lossy(&command[..end]).to_string(); + let cgroup_id = pl.cgroup_id; + let protocol = pl.proto; + + // protocol extraction + match IpProtocols::try_from(protocol) { + Ok(proto) => { + info!( + "Event Id: {} Protocol: {:?} SRC: {}:{} -> DST: {}:{} Command: {} Cgroup_id: {}", + event_id, + proto, + src, + src_port, + dst, + dst_port, + command_str, + cgroup_id //proc_content + ); + } + Err(e) => { + error!("Unknown protocol. Data maybe corrupted. Reason:{:?}", e); + } + } + } + } + } + pub async fn read_and_handle_veth_log( + //link_ids: Arc>>, + //bpf: Arc>, + buffers: &mut [BytesMut], + tot_events: i32, + offset: i32, + ) { + for i in offset..tot_events { + let vec_bytes = &buffers[i as usize]; + if vec_bytes.len() < std::mem::size_of::() { + error!( + "Corrupted data. Readed {:?} bytes expected {} bytes", + vec_bytes, + std::mem::size_of::() + ) + } + if vec_bytes.len() >= std::mem::size_of::() { + let pl: VethLog = + unsafe { std::ptr::read_unaligned(vec_bytes.as_ptr() as *const _) }; // reading raw bytes + + // extracting struct info from bytes + let name_bytes = pl.name; + + let dev_addr_bytes = pl.dev_addr; + let name = std::str::from_utf8(&name_bytes); + let state = pl.state; + + let dev_addr = dev_addr_bytes; + let netns = pl.netns; + let mut event_type = String::new(); + + // event_type extraction + match pl.event_type { + 1 => { + event_type = "creation".to_string(); + match name { + Ok(veth_name) => { + info!( + "[{}] Veth Event: Type: {} Name: {} Dev_addr: {:x?} State: {}", + netns, + event_type, + veth_name.trim_end_matches("\0"), + dev_addr, + state + ); + // TODO: this logic needs to live in a separate space + // FIXME: consider to update this logic to reduce the overhead. + //match attach_detach_veth( + // bpf.clone(), + // 1, + // veth_name, + // link_ids.clone(), + //) + //.await + //{ + // Ok(_) => { + // info!( + // "[{}] Successfully attached Attach/Detach function for veth: {}", + // netns, + // veth_name.trim_end_matches("\0") + // ); + // } + // Err(e) => { + // info!( + // "[{}] Error attaching Attach/Detach function. Error : {}", + // netns, e + // ); + // } + //} + } + Err(e) => { + error!( + "Failed to extract veth name during event_type = creation (1).Reason:{}", + e + ); + } + } + } + 2 => { + event_type = "deletion".to_string(); + match name { + Ok(veth_name) => { + info!( + "[{}] Veth Event: Type: {} Name: {} Dev_addr: {:x?} State: {}", + netns, + event_type, + veth_name.trim_end_matches("\0"), + dev_addr, + state + ); + // TODO: this logic needs to live in a separate space + //match attach_detach_veth( + // bpf.clone(), + // 2, + // veth_name, + // link_ids.clone(), + //) + //.await + //{ + // Ok(_) => { + // info!( + // "[{}] Successfully attached Attach/Detach function for veth: {}", + // netns, + // veth_name.trim_end_matches("\0") + // ); + // } + // Err(e) => { + // info!( + // "[{}] Error attaching Attach/Detach function. Error : {}", + // netns, e + // ); + // } + // } + } + Err(e) => { + error!( + "Failed to extract veth name during event_type = deletion (2).Reason:{}", + e + ); + } + } + } + _ => { + warn!("Unknown event type") + } + } + } + } + } +} diff --git a/core/common/src/lib.rs b/core/common/src/lib.rs index 1d015a27..9623afd8 100644 --- a/core/common/src/lib.rs +++ b/core/common/src/lib.rs @@ -1,7 +1,10 @@ +#[cfg(feature = "buffer-reader")] +#[cfg(feature = "kernel-structs")] +pub mod buffer_type; pub mod constants; pub mod formatters; pub mod logger; #[cfg(feature = "map-handlers")] pub mod map_handlers; #[cfg(feature = "program-handlers")] -pub mod program_handlers; \ No newline at end of file +pub mod program_handlers; diff --git a/core/src/components/identity/Cargo.toml b/core/src/components/identity/Cargo.toml index f5bdb378..bb554120 100644 --- a/core/src/components/identity/Cargo.toml +++ b/core/src/components/identity/Cargo.toml @@ -15,7 +15,6 @@ struct = [] enums = [] experimental = ["struct", "enums"] - [dependencies] aya = "0.13.1" bytes = "1.4" @@ -27,12 +26,15 @@ tokio = { version = "1.48.0", features = [ "time", "macros", ] } -anyhow = "1.0" tracing = "0.1.41" -tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } bytemuck = { version = "1.23.0", features = ["derive"] } -bytemuck_derive = "1.10.1" -cortexbrain-common = { path = "../../../common/", features = ["map-handlers","program-handlers"] } +cortexbrain-common = { path = "../../../common/", features = [ + "map-handlers", + "program-handlers", + "network-structs", + "buffer-reader", +] } nix = { version = "0.30.1", features = ["net"] } kube = { version = "2.0.1", features = ["client"] } k8s-openapi = { version = "0.26.0", features = ["v1_34"] } +bytemuck_derive = "1.10.2" diff --git a/core/src/components/identity/src/enums.rs b/core/src/components/identity/src/enums.rs deleted file mode 100644 index b0b271ba..00000000 --- a/core/src/components/identity/src/enums.rs +++ /dev/null @@ -1,12 +0,0 @@ -/* - * IpProtocols enum to reconstruct the packet protocol based on the - * IPV4 Header Protocol code - */ -#[cfg(feature="enums")] -#[derive(Debug)] -#[repr(u8)] -pub enum IpProtocols { - ICMP = 1, - TCP = 6, - UDP = 17, -} \ No newline at end of file diff --git a/core/src/components/identity/src/helpers.rs b/core/src/components/identity/src/helpers.rs index 8e33db74..6439e61b 100644 --- a/core/src/components/identity/src/helpers.rs +++ b/core/src/components/identity/src/helpers.rs @@ -1,5 +1,5 @@ -use crate::enums::IpProtocols; use crate::structs::{PacketLog, TcpPacketRegistry, VethLog}; +use cortexbrain_common::buffer_type::{reverse_be_addr,IpProtocols}; use aya::Ebpf; use aya::programs::tc::SchedClassifierLinkId; @@ -15,37 +15,7 @@ use std::{ }; use tracing::{debug, error, event, info, span, warn}; -// -// TryFrom Trait implementation for IpProtocols enum -// This is used to reconstruct the packet protocol based on the -// IPV4 Header Protocol code -// - -impl TryFrom for IpProtocols { - type Error = (); - fn try_from(proto: u8) -> Result { - match proto { - 1 => Ok(IpProtocols::ICMP), - 6 => Ok(IpProtocols::TCP), - 17 => Ok(IpProtocols::UDP), - _ => Err(()), - } - } -} - -// docs: -// This function perform a byte swap from little-endian to big-endian -// It's used to reconstruct the correct IPv4 address from the u32 representation -// -// Takes a u32 address in big-endian format and returns a Ipv4Addr with reversed octets -// -pub fn reverse_be_addr(addr: u32) -> Ipv4Addr { - let octects = addr.to_be_bytes(); - let [a, b, c, d] = [octects[3], octects[2], octects[1], octects[0]]; - let reversed_ip = Ipv4Addr::new(a, b, c, d); - reversed_ip -} - +/* pub async fn display_veth_events>( bpf: Arc>, mut perf_buffers: Vec>, @@ -165,6 +135,7 @@ pub async fn display_veth_events>( } } + */ // docs: // This function checks if the given interface name is in the list of ignored interfaces // Takes a interface name (iface) as &str and returns true if the interface should be ignored @@ -196,7 +167,7 @@ pub fn get_veth_channels() -> Vec { interfaces } - +/* async fn attach_detach_veth( bpf: Arc>, event_type: u8, @@ -268,7 +239,7 @@ async fn attach_detach_veth( Ok(()) } - + */ // enum BuffersType pub enum BufferType { PacketLog, diff --git a/core/src/components/identity/src/lib.rs b/core/src/components/identity/src/lib.rs index 54134144..ceaedc26 100644 --- a/core/src/components/identity/src/lib.rs +++ b/core/src/components/identity/src/lib.rs @@ -1,3 +1,3 @@ pub mod helpers; -pub mod structs; -pub mod enums; \ No newline at end of file +#[cfg(feature = "experimental")] +pub mod service_discovery; \ No newline at end of file diff --git a/core/src/components/identity/src/main.rs b/core/src/components/identity/src/main.rs index c1efb6c1..cf517f74 100644 --- a/core/src/components/identity/src/main.rs +++ b/core/src/components/identity/src/main.rs @@ -8,9 +8,7 @@ * */ -mod enums; mod helpers; -mod structs; mod service_discovery; use crate::helpers::{get_veth_channels, read_perf_buffer}; @@ -101,11 +99,9 @@ async fn main() -> Result<(), anyhow::Error> { )?; } - event_listener(maps, link_ids.clone(), bpf.clone()) - .await - .map_err(|e| { - anyhow::anyhow!("Error inizializing event_listener. Reason: {}", e) - })?; + event_listener(maps).await.map_err(|e| { + anyhow::anyhow!("Error inizializing event_listener. Reason: {}", e) + })?; } Err(e) => { error!("Error while pinning bpf_maps: {}", e); @@ -201,8 +197,8 @@ async fn init_tcp_registry(bpf: Arc>) -> Result<(), anyhow::Error> { // async fn event_listener( bpf_maps: Vec, - link_ids: Arc>>, - bpf: Arc>, + //link_ids: Arc>>, + //bpf: Arc>, ) -> Result<(), anyhow::Error> { info!("Preparing perf_buffers and perf_arrays"); @@ -254,7 +250,7 @@ async fn event_listener( let tcp_buffers = vec![BytesMut::with_capacity(1024); online_cpus().iter().len()]; // init veth link ids - let veth_link_ids = link_ids; + //let veth_link_ids = link_ids; // spawn async tasks let veth_events_displayer = tokio::spawn(async move { diff --git a/core/src/components/identity/src/mod.rs b/core/src/components/identity/src/mod.rs index f957b31e..ceaedc26 100644 --- a/core/src/components/identity/src/mod.rs +++ b/core/src/components/identity/src/mod.rs @@ -1,5 +1,3 @@ pub mod helpers; -pub mod structs; -pub mod enums; #[cfg(feature = "experimental")] pub mod service_discovery; \ No newline at end of file diff --git a/core/src/components/identity/src/structs.rs b/core/src/components/identity/src/structs.rs deleted file mode 100644 index 766a7768..00000000 --- a/core/src/components/identity/src/structs.rs +++ /dev/null @@ -1,57 +0,0 @@ -use bytemuck_derive::Zeroable; - -/* - * Structure PacketLog - * This structure is used to store the packet information - */ -#[repr(C)] -#[derive(Clone, Copy, Zeroable)] -pub struct PacketLog { - pub proto: u8, - pub src_ip: u32, - pub src_port: u16, - pub dst_ip: u32, - pub dst_port: u16, - pub pid: u32, -} -unsafe impl aya::Pod for PacketLog {} - -/* - * Connection Array that contains the hash_id associated with an active connection - */ -//#[repr(C)] -//#[derive(Clone, Copy, Zeroable)] -//pub struct ConnArray { -// pub src_ip: u32, -// pub dst_ip: u32, -// pub src_port: u16, -// pub dst_port: u16, -// pub proto: u8, -//} - -//unsafe impl aya::Pod for ConnArray {} - -#[repr(C)] -#[derive(Clone, Copy)] -pub struct VethLog { - pub name: [u8; 16], // 16 bytes: veth interface name - pub state: u64, // 8 bytes: state variable (unsigned long in kernel) - pub dev_addr: [u32; 8], // 32 bytes: device address - pub event_type: u8, // 1 byte: 1 for veth creation, 2 for veth destruction - pub netns: u32, // 4 bytes: network namespace inode number - pub pid: u32, // 4 bytes: PID that triggered the event -} - - -#[repr(C)] -#[derive(Clone, Copy)] -pub struct TcpPacketRegistry { - pub proto: u8, - pub src_ip: u32, - pub dst_ip: u32, - pub src_port: u16, - pub dst_port: u16, - pub pid: u32, - pub command: [u8; 16], - pub cgroup_id: u64, -} From 062b6260b4aa674c17a231895d60e8f036267ee8 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Tue, 3 Feb 2026 11:29:14 +0100 Subject: [PATCH 23/24] [update]: cli packages update --- cli/Cargo.lock | 81 +++++++------------------------------------------- 1 file changed, 10 insertions(+), 71 deletions(-) diff --git a/cli/Cargo.lock b/cli/Cargo.lock index df205899..0fea51dd 100644 --- a/cli/Cargo.lock +++ b/cli/Cargo.lock @@ -216,18 +216,15 @@ checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" [[package]] name = "bytemuck" -version = "1.23.2" +version = "1.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3995eaeebcdf32f91f980d360f78732ddc061097ab4e39991ae7a6ace9194677" -dependencies = [ - "bytemuck_derive", -] +checksum = "c8efb64bd706a16a1bdde310ae86b351e4d21550d98d056f22f8a7f7a2183fec" [[package]] name = "bytemuck_derive" -version = "1.10.1" +version = "1.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f154e572231cb6ba2bd1176980827e3d5dc04cc183a75dea38109fbdd672d29" +checksum = "f9abbd1bc6865053c427f7198e6af43bfdedc55ab791faed4fbd361d789575ff" dependencies = [ "proc-macro2", "quote", @@ -236,9 +233,9 @@ dependencies = [ [[package]] name = "bytes" -version = "1.10.1" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" +checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" [[package]] name = "cc" @@ -256,12 +253,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9" -[[package]] -name = "cfg_aliases" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" - [[package]] name = "chrono" version = "0.4.42" @@ -362,6 +353,9 @@ version = "0.1.0" dependencies = [ "anyhow", "aya", + "bytemuck", + "bytemuck_derive", + "bytes", "k8s-openapi", "kube", "opentelemetry", @@ -373,17 +367,6 @@ dependencies = [ "tracing-subscriber", ] -[[package]] -name = "cortexbrain-common" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5daea06747f06e000deaa52b7aceb504ddc309c061badf76e0b4b3d146ebf3a4" -dependencies = [ - "anyhow", - "tracing", - "tracing-subscriber", -] - [[package]] name = "cortexflow-cli" version = "0.1.4-bf1" @@ -413,8 +396,7 @@ dependencies = [ "bytemuck", "bytemuck_derive", "chrono", - "cortexbrain-common 0.1.0", - "cortexflow_identity", + "cortexbrain-common", "prost", "tokio", "tokio-stream", @@ -427,27 +409,6 @@ dependencies = [ "tracing-subscriber", ] -[[package]] -name = "cortexflow_identity" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5725a802e4f494b5fab4c69b1455a32dd3804b52a58c665a7d751eeae93ddfca" -dependencies = [ - "anyhow", - "aya", - "bytemuck", - "bytemuck_derive", - "bytes", - "cortexbrain-common 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "k8s-openapi", - "kube", - "libc", - "nix", - "tokio", - "tracing", - "tracing-subscriber", -] - [[package]] name = "cpufeatures" version = "0.2.17" @@ -1232,15 +1193,6 @@ version = "2.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" -[[package]] -name = "memoffset" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" -dependencies = [ - "autocfg", -] - [[package]] name = "mime" version = "0.3.17" @@ -1264,19 +1216,6 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084" -[[package]] -name = "nix" -version = "0.30.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" -dependencies = [ - "bitflags", - "cfg-if", - "cfg_aliases", - "libc", - "memoffset", -] - [[package]] name = "nu-ansi-term" version = "0.50.1" From 8e49cc8cd5c0cd79c44f58b44368e8484552e2b2 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Tue, 3 Feb 2026 11:31:01 +0100 Subject: [PATCH 24/24] [update]: IpProtocols update . Btter code formatting --- core/Cargo.lock | 52 ++++++++---------------------------------- core/api/Cargo.toml | 7 ++++-- core/api/src/api.rs | 4 ++-- core/api/src/client.rs | 22 +++++++----------- 4 files changed, 25 insertions(+), 60 deletions(-) diff --git a/core/Cargo.lock b/core/Cargo.lock index af06c43f..23ea7518 100644 --- a/core/Cargo.lock +++ b/core/Cargo.lock @@ -275,9 +275,9 @@ checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" [[package]] name = "bytemuck" -version = "1.24.0" +version = "1.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fbdf580320f38b612e485521afda1ee26d10cc9884efaaa750d383e13e3c5f4" +checksum = "c8efb64bd706a16a1bdde310ae86b351e4d21550d98d056f22f8a7f7a2183fec" dependencies = [ "bytemuck_derive", ] @@ -295,9 +295,9 @@ dependencies = [ [[package]] name = "bytes" -version = "1.10.1" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" +checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" [[package]] name = "camino" @@ -407,6 +407,9 @@ version = "0.1.0" dependencies = [ "anyhow", "aya", + "bytemuck", + "bytemuck_derive", + "bytes", "k8s-openapi", "kube", "opentelemetry", @@ -418,17 +421,6 @@ dependencies = [ "tracing-subscriber", ] -[[package]] -name = "cortexbrain-common" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5daea06747f06e000deaa52b7aceb504ddc309c061badf76e0b4b3d146ebf3a4" -dependencies = [ - "anyhow", - "tracing", - "tracing-subscriber", -] - [[package]] name = "cortexflow_agent_api" version = "0.1.1" @@ -438,8 +430,7 @@ dependencies = [ "bytemuck", "bytemuck_derive", "chrono", - "cortexbrain-common 0.1.0", - "cortexflow_identity 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cortexbrain-common", "prost", "tokio", "tokio-stream", @@ -456,39 +447,16 @@ dependencies = [ name = "cortexflow_identity" version = "0.1.1" dependencies = [ - "anyhow", "aya", "bytemuck", "bytemuck_derive", "bytes", - "cortexbrain-common 0.1.0", + "cortexbrain-common", "k8s-openapi", "kube", "nix", "tokio", "tracing", - "tracing-subscriber", -] - -[[package]] -name = "cortexflow_identity" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5725a802e4f494b5fab4c69b1455a32dd3804b52a58c665a7d751eeae93ddfca" -dependencies = [ - "anyhow", - "aya", - "bytemuck", - "bytemuck_derive", - "bytes", - "cortexbrain-common 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "k8s-openapi", - "kube", - "libc", - "nix", - "tokio", - "tracing", - "tracing-subscriber", ] [[package]] @@ -1267,7 +1235,7 @@ dependencies = [ "aya-log", "bytemuck", "bytes", - "cortexbrain-common 0.1.0", + "cortexbrain-common", "libc", "nix", "tokio", diff --git a/core/api/Cargo.toml b/core/api/Cargo.toml index 9706e639..3fd811d4 100644 --- a/core/api/Cargo.toml +++ b/core/api/Cargo.toml @@ -29,14 +29,17 @@ tonic = "0.14.0" tonic-prost = "0.14.0" tracing = "0.1.41" aya = "0.13.1" -cortexbrain-common = { path = "../common", features = ["map-handlers"] } +cortexbrain-common = { path = "../common", features = [ + "map-handlers", + "kernel-structs", + "buffer-reader" +] } tonic-reflection = "0.14.0" tonic-build = "0.14.0" tracing-subscriber = "0.3.19" tokio-stream = "0.1.17" bytemuck = { version = "1.23.0" } bytemuck_derive = "1.10.1" -cortexflow_identity = { version = "0.1.1", features = ["enums"] } chrono = "0.4.42" [build-dependencies] diff --git a/core/api/src/api.rs b/core/api/src/api.rs index ce1bae3f..3d9ac32d 100644 --- a/core/api/src/api.rs +++ b/core/api/src/api.rs @@ -37,8 +37,8 @@ use crate::constants::PIN_BLOCKLIST_MAP_PATH; use crate::helpers::comm_to_string; use aya::maps::Map; +use cortexbrain_common::buffer_type::IpProtocols; use cortexbrain_common::constants::BPF_PATH; -use cortexflow_identity::enums::IpProtocols; use std::net::Ipv4Addr; use tracing::warn; @@ -138,7 +138,7 @@ impl Default for AgentApi { // init MapData from the kernel space // - // TODO: in the future will be better to not use .unwrap() + // TODO: in the future will be better to not use .unwrap() let mut active_connection_events_array = load_perf_event_array_from_mapdata("/sys/fs/bpf/maps/events_map").unwrap(); let mut network_metrics_events_array = diff --git a/core/api/src/client.rs b/core/api/src/client.rs index 844ea75e..096b176c 100644 --- a/core/api/src/client.rs +++ b/core/api/src/client.rs @@ -1,29 +1,23 @@ +use crate::agent::agent_client::AgentClient; use anyhow::Error; use std::result::Result::Ok; -use tonic::{transport::Channel}; -use tonic_reflection::pb::v1::{ - server_reflection_client::ServerReflectionClient, -}; -use crate::agent::agent_client::AgentClient; +use tonic::transport::Channel; +use tonic_reflection::pb::v1::server_reflection_client::ServerReflectionClient; -const AGENT_IP : &str = "http://127.0.0.1:9090"; +const AGENT_IP: &str = "http://127.0.0.1:9090"; -#[cfg(feature="client")] +#[cfg(feature = "client")] pub async fn connect_to_client() -> Result, Error> { //this methods force a HTTP/2 connection from a static string //FIXME: this will require an update to ensure a protected connection - let channel = Channel::from_static(AGENT_IP) - .connect() - .await?; + let channel = Channel::from_static(AGENT_IP).connect().await?; let client = AgentClient::new(channel); Ok(client) } -#[cfg(feature="client")] +#[cfg(feature = "client")] pub async fn connect_to_server_reflection() -> Result, Error> { //this methods force a HTTP/2 connection from a static string - let channel = Channel::from_static(AGENT_IP) - .connect() - .await?; + let channel = Channel::from_static(AGENT_IP).connect().await?; let client = ServerReflectionClient::new(channel); Ok(client) }