diff --git a/core/common/Cargo.toml b/core/common/Cargo.toml index b8e840d..ee50e2b 100644 --- a/core/common/Cargo.toml +++ b/core/common/Cargo.toml @@ -24,10 +24,12 @@ opentelemetry-otlp = { version = "0.31.0", features = ["logs", "grpc-tonic"] } bytemuck = "1.25.0" bytes = "1.11.0" bytemuck_derive = "1.10.2" +tokio = "1.49.0" [features] map-handlers = [] program-handlers = [] network-structs = [] +monitoring-structs = [] buffer-reader = [] experimental = [] diff --git a/core/common/src/buffer_type.rs b/core/common/src/buffer_type.rs index 9fc7828..ac0d600 100644 --- a/core/common/src/buffer_type.rs +++ b/core/common/src/buffer_type.rs @@ -1,3 +1,4 @@ +use aya::{maps::perf::PerfEventArrayBuffer, util::online_cpus}; use bytemuck_derive::Zeroable; use bytes::BytesMut; use std::net::Ipv4Addr; @@ -54,19 +55,21 @@ unsafe impl aya::Pod for PacketLog {} #[cfg(feature = "network-structs")] #[repr(C, packed)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Zeroable)] 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: [u8; 6], // 32 bytes: device address + pub dev_addr: [u8; 6], // 6 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")] +unsafe impl aya::Pod for VethLog {} #[cfg(feature = "network-structs")] #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Zeroable)] pub struct TcpPacketRegistry { pub proto: u8, pub src_ip: u32, @@ -77,6 +80,47 @@ pub struct TcpPacketRegistry { pub command: [u8; 16], pub cgroup_id: u64, } +#[cfg(feature = "network-structs")] +unsafe impl aya::Pod for TcpPacketRegistry {} + +#[cfg(feature = "monitoring-structs")] +pub const TASK_COMM_LEN: usize = 16; // linux/sched.h +#[cfg(feature = "monitoring-structs")] +#[repr(C, packed)] +#[derive(Clone, Copy, Zeroable)] +pub struct NetworkMetrics { + pub tgid: u32, + pub comm: [u8; TASK_COMM_LEN], + pub ts_us: u64, + pub sk_err: i32, // Offset 284 + pub sk_err_soft: i32, // Offset 600 + pub sk_backlog_len: i32, // Offset 196 + pub sk_write_memory_queued: i32, // Offset 376 + pub sk_receive_buffer_size: i32, // Offset 244 + pub sk_ack_backlog: u32, // Offset 604 + pub sk_drops: i32, // Offset 136 +} +#[cfg(feature = "monitoring-structs")] +unsafe impl aya::Pod for NetworkMetrics {} + +#[cfg(feature = "monitoring-structs")] +#[repr(C, packed)] +#[derive(Clone, Copy, Zeroable)] +pub struct TimeStampMetrics { + pub delta_us: u64, + pub ts_us: u64, + pub tgid: u32, + pub comm: [u8; TASK_COMM_LEN], + pub lport: u16, + pub dport_be: u16, + pub af: u16, + pub saddr_v4: u32, + pub daddr_v4: u32, + pub saddr_v6: [u32; 4], + pub daddr_v6: [u32; 4], +} +#[cfg(feature = "monitoring-structs")] +unsafe impl aya::Pod for TimeStampMetrics {} // docs: // This function perform a byte swap from little-endian to big-endian @@ -95,15 +139,23 @@ pub fn reverse_be_addr(addr: u32) -> Ipv4Addr { // enum BuffersType #[cfg(feature = "buffer-reader")] pub enum BufferType { + #[cfg(feature = "network-structs")] PacketLog, + #[cfg(feature = "network-structs")] TcpPacketRegistry, + #[cfg(feature = "network-structs")] VethLog, + #[cfg(feature = "monitoring-structs")] + NetworkMetrics, + #[cfg(feature = "monitoring-structs")] + TimeStampMetrics, } // IDEA: this is an experimental implementation to centralize buffer reading logic // TODO: add variant for cortexflow API exporter #[cfg(feature = "buffer-reader")] impl BufferType { + #[cfg(feature = "network-structs")] 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]; @@ -147,6 +199,7 @@ impl BufferType { } } } + #[cfg(feature = "network-structs")] 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]; @@ -204,11 +257,8 @@ impl BufferType { } } } - pub async fn read_and_handle_veth_log( - buffers: &mut [BytesMut], - tot_events: i32, - offset: i32, - ) { + #[cfg(feature = "network-structs")] + pub async fn read_and_handle_veth_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::() { @@ -289,4 +339,224 @@ impl BufferType { } } } + #[cfg(feature = "monitoring-structs")] + pub async fn read_network_metrics(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 Network Metrics data. Raw data: {}. Readed {} bytes expected {} bytes", + vec_bytes + .iter() + .map(|b| format!("{:02x}", b)) + .collect::>() + .join(" "), + vec_bytes.len(), + std::mem::size_of::() + ); + continue; + } + if vec_bytes.len() >= std::mem::size_of::() { + let net_metrics: NetworkMetrics = + unsafe { std::ptr::read_unaligned(vec_bytes.as_ptr() as *const _) }; + let tgid = net_metrics.tgid; + let comm = String::from_utf8_lossy(&net_metrics.comm); + let ts_us = net_metrics.ts_us; + let sk_drop_count = net_metrics.sk_drops; + let sk_err = net_metrics.sk_err; + let sk_err_soft = net_metrics.sk_err_soft; + let sk_backlog_len = net_metrics.sk_backlog_len; + let sk_write_memory_queued = net_metrics.sk_write_memory_queued; + let sk_ack_backlog = net_metrics.sk_ack_backlog; + 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 + ); + } + } + } + #[cfg(feature = "monitoring-structs")] + pub async fn read_timestamp_metrics(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 Network Metrics data. Raw data: {}. Readed {} bytes expected {} bytes", + vec_bytes + .iter() + .map(|b| format!("{:02x}", b)) + .collect::>() + .join(" "), + vec_bytes.len(), + std::mem::size_of::() + ); + continue; + } + if vec_bytes.len() >= std::mem::size_of::() { + let time_stamp_event: TimeStampMetrics = + unsafe { std::ptr::read_unaligned(vec_bytes.as_ptr() as *const _) }; + let delta_us = time_stamp_event.delta_us; + let ts_us = time_stamp_event.ts_us; + let tgid = time_stamp_event.tgid; + let comm = String::from_utf8_lossy(&time_stamp_event.comm); + let lport = time_stamp_event.lport; + let dport_be = time_stamp_event.dport_be; + let af = time_stamp_event.af; + info!( + "TimeStampEvent - delta_us: {}, ts_us: {}, tgid: {}, comm: {}, lport: {}, dport_be: {}, af: {}", + delta_us, ts_us, tgid, comm, lport, dport_be, af + ); + } + } + } +} + +// 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 = "buffer-reader")] +pub async fn read_perf_buffer>( + mut array_buffers: Vec>, + mut buffers: Vec, + buffer_type: BufferType, +) { + // loop over the buffers + loop { + 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); + } + // 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 { + #[cfg(feature = "network-structs")] + BufferType::PacketLog => { + BufferType::read_packet_log(&mut buffers, tot_events, offset).await + } + #[cfg(feature = "network-structs")] + BufferType::TcpPacketRegistry => { + BufferType::read_tcp_registry_log(&mut buffers, tot_events, offset) + .await + } + #[cfg(feature = "network-structs")] + BufferType::VethLog => { + BufferType::read_and_handle_veth_log( + &mut buffers, + tot_events, + offset, + ) + .await + } + #[cfg(feature = "monitoring-structs")] + BufferType::NetworkMetrics => { + BufferType::read_network_metrics(&mut buffers, tot_events, offset) + .await + } + #[cfg(feature = "monitoring-structs")] + BufferType::TimeStampMetrics => { + BufferType::read_timestamp_metrics(&mut buffers, tot_events, offset) + .await + } + } + } + } + Err(e) => { + error!("Cannot read events from buffer. Reason: {} ", e); + } + } + } + tokio::time::sleep(std::time::Duration::from_millis(100)).await; // small sleep + } +} + +#[cfg(feature = "buffer-reader")] +pub enum BufferSize { + #[cfg(feature = "network-structs")] + ClassifierNetEvents, + #[cfg(feature = "network-structs")] + VethEvents, + #[cfg(feature = "network-structs")] + TcpEvents, + #[cfg(feature = "monitoring-structs")] + NetworkMetricsEvents, + #[cfg(feature = "monitoring-structs")] + TimeMetricsEvents, +} +#[cfg(feature = "buffer-reader")] +impl BufferSize { + pub fn get_size(&self) -> usize { + match self { + #[cfg(feature = "network-structs")] + BufferSize::ClassifierNetEvents => std::mem::size_of::(), + #[cfg(feature = "network-structs")] + BufferSize::VethEvents => std::mem::size_of::(), + #[cfg(feature = "network-structs")] + BufferSize::TcpEvents => std::mem::size_of::(), + #[cfg(feature = "monitoring-structs")] + BufferSize::NetworkMetricsEvents => std::mem::size_of::(), + #[cfg(feature = "monitoring-structs")] + BufferSize::TimeMetricsEvents => std::mem::size_of::(), + } + } + pub fn set_buffer(&self) -> Vec { + // iter returns and iterator of cpu ids, + // we need only the total number of cpus to set the buffer size so we use .len() to get + // the count of total cpus and then we allocate a buffer for each cpu with a capacity + // based on the structure size * a factor to have a bigger buffer to avoid overflows and lost events + + // Old buffers where 1024 bytes long. Now we set different buffer size based on + // the frequence of the events. + // ClassifierNetEvents are triggered by the TC classifier program, events has high frequency + // VethEvents are triggered by the creation and deletion of veth interfaces, events has small frequency compared to classifier events + // TcpEvents are triggered by TCP events and connections. Events has similar frequency to ClassifierNetEvents. + + let tot_cpu = online_cpus().iter().len(); // total number of cpus + + // TODO: finish to do all the calculations for the buffer sizes + match self { + #[cfg(feature = "network-structs")] + BufferSize::ClassifierNetEvents => { + let capacity = self.get_size() * 200; + return vec![BytesMut::with_capacity(capacity); tot_cpu]; + } + #[cfg(feature = "network-structs")] + BufferSize::VethEvents => { + let capacity = self.get_size() * 100; // Allocates 4Kb of memory for the buffers + return vec![BytesMut::with_capacity(capacity); tot_cpu]; + } + #[cfg(feature = "network-structs")] + BufferSize::TcpEvents => { + let capacity = self.get_size() * 200; + return vec![BytesMut::with_capacity(capacity); tot_cpu]; + } + #[cfg(feature = "monitoring-structs")] + BufferSize::NetworkMetricsEvents => { + let capacity = self.get_size() * 1024; + return vec![BytesMut::with_capacity(capacity); tot_cpu]; + } + #[cfg(feature = "monitoring-structs")] + BufferSize::TimeMetricsEvents => { + let capacity = self.get_size() * 1024; + return vec![BytesMut::with_capacity(capacity); tot_cpu]; + } + } + } } diff --git a/core/common/src/lib.rs b/core/common/src/lib.rs index d88c1db..d7e48b0 100644 --- a/core/common/src/lib.rs +++ b/core/common/src/lib.rs @@ -1,5 +1,8 @@ -#[cfg(feature = "buffer-reader")] -#[cfg(feature = "network-structs")] +#[cfg(any( + feature = "buffer-reader", + feature = "network-structs", + feature = "monitoring-structs" +))] pub mod buffer_type; pub mod constants; pub mod formatters; diff --git a/core/common/src/map_handlers.rs b/core/common/src/map_handlers.rs index 19d4e20..b246b70 100644 --- a/core/common/src/map_handlers.rs +++ b/core/common/src/map_handlers.rs @@ -121,8 +121,11 @@ pub async fn populate_blocklist() -> Result<(), Error> { .filter(|s| !s.is_empty()) .collect(); //String parsing from "x y" to ["x","y"] - info!("Inserting addresses: {:?}", addresses); - for item in addresses { + if addresses.is_empty() { + warn!("No addresses found in the blocklist. Skipping load"); + } + for item in &addresses { + info!("Inserting addresses: {:?}", &item); let addr = Ipv4Addr::from_str(&item)?.octets(); let _ = blocklist_map.insert(addr, addr, 0); } @@ -138,6 +141,7 @@ pub async fn populate_blocklist() -> Result<(), Error> { } #[cfg(feature = "map-handlers")] +// TODO: modify this to accept also HashMap types pub fn load_perf_event_array_from_mapdata( path: &'static str, ) -> Result, Error> { @@ -154,3 +158,57 @@ pub fn load_perf_event_array_from_mapdata( })?; Ok(perf_event_array) } + +#[cfg(feature = "map-handlers")] +pub fn map_manager( + maps: BpfMapsData, +) -> Result< + std::collections::HashMap< + String, + ( + aya::maps::PerfEventArray, + Vec>, + ), + >, + Error, +> { + use aya::maps::PerfEventArray; + use aya::maps::{MapData, perf::PerfEventArrayBuffer}; + use tracing::debug; + + let mut map_manager = std::collections::HashMap::< + String, // this will store the bpf map name + (PerfEventArray, Vec>), // this will manage the BPF_MAP_TYPE_PERF_EVENT_ARRAY and its buffer + >::new(); + + // map_manager creates an hashmap that contains: + // MAP NAME as String (KEY) + // + // VALUES (tuple) + // a PERF_EVENT_ARRAY + // a vector of PERF_EVENT_ARRAY_BUFFER + // + // the map manager helps the event listener to specifically call a map by its pinned name + // e.g. veth_identity_map and returns the associated PERF_EVENT_ARRAY and PERF_EVENT_ARRAY_BUFFERS (1 per CPU) + // also the map manager helps to write a more complete debug context by linking map names with arrays and buffers. + // actually i cannot return the extact information using only the Aya library + + // create the PerfEventArrays and the buffers from the BpfMapsData Objects + for (map, name) in maps + .bpf_obj_map + .into_iter() + .zip(maps.bpf_obj_names.into_iter()) + // zip two iterators at the same time for map object and map names + { + debug!("Debugging map type:{:?} for map name {:?}", map, &name); + info!("Creating PerfEventArray for map name {:?}", &name); + + // save the map in a registry if is a PerfEventArray to access them by name + if let std::result::Result::Ok(perf_event_array) = PerfEventArray::try_from(map) { + map_manager.insert(name.clone(), (perf_event_array, Vec::new())); + } else { + warn!("Map {:?} is not a PerfEventArray, skipping load", &name); + } + } + Ok(map_manager) +} diff --git a/core/common/src/program_handlers.rs b/core/common/src/program_handlers.rs index 42cd3ba..347be51 100644 --- a/core/common/src/program_handlers.rs +++ b/core/common/src/program_handlers.rs @@ -13,32 +13,38 @@ pub fn load_program( .lock() .map_err(|e| anyhow::anyhow!("Cannot get value from lock. Reason: {}", e))?; - // Load and attach the eBPF programs + // Load and attach the eBPF program 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))?; + // STEP 1: load program + program .load() .map_err(|e| anyhow::anyhow!("Cannot load program: {}. Error: {}", &program_name, e))?; + // STEP 2: Attach the loaded program to kernel symbol match program.attach(kernel_symbol, 0) { - Ok(_) => info!("{} program attached successfully", kernel_symbol), + Ok(_) => info!( + "{} program attached successfully to kernel symbol {}", + &program_name, &kernel_symbol + ), Err(e) => { - error!("Error attaching {} program {:?}", kernel_symbol, e); + error!( + "Error attaching {} program to kernel symbol {}. Reason: {:?}", + &program_name, &kernel_symbol, e + ); return Err(anyhow::anyhow!( - "Failed to attach {}: {:?}", - kernel_symbol, + "Failed to attach program {} to kernel symbol {}. Reason {:?}", + &program_name, + &kernel_symbol, e )); } }; - info!( - "eBPF program {} loaded and attached successfully", - program_name - ); Ok(()) } diff --git a/core/src/components/conntracker/src/data_structures.rs b/core/src/components/conntracker/src/data_structures.rs index f4c5047..c55cd3f 100644 --- a/core/src/components/conntracker/src/data_structures.rs +++ b/core/src/components/conntracker/src/data_structures.rs @@ -47,7 +47,7 @@ pub struct ConnArray { // pid: kernel process ID // -#[repr(C,packed)] +#[repr(C, packed)] #[derive(Clone, Copy)] pub struct VethLog { pub name: [u8; 16], // 16 bytes: veth interface name @@ -94,9 +94,13 @@ pub static mut CONNTRACKER: LruPerCpuHashMap = pub static mut VETH_EVENTS: PerfEventArray = PerfEventArray::new(0); #[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); +pub static mut BLOCKLIST: HashMap<[u8; 4], [u8; 4]> = HashMap::with_max_entries(1024, 0); //here i need to pass an address like this: [135,171,168,192] #[map(name = "TcpPacketRegistry", pinning = "by_name")] pub static mut PACKET_REGISTRY: PerfEventArray = PerfEventArray::new(0); + +#[map(name = "tracked_veth", pinning = "by_name")] +// This map takes a registry of tracked veth interfaces +// The maximum number of characters is 16 of type u8 +pub static mut TRACKED_VETH: HashMap<[u8; 16], [u8; 8]> = HashMap::with_max_entries(1024, 0); diff --git a/core/src/components/conntracker/src/main.rs b/core/src/components/conntracker/src/main.rs index e723e4b..8438838 100644 --- a/core/src/components/conntracker/src/main.rs +++ b/core/src/components/conntracker/src/main.rs @@ -32,6 +32,10 @@ use crate::tc::try_identity_classifier; use crate::tcp_analyzer::try_tcp_analyzer; use crate::veth_tracer::try_veth_tracer; +// TODO: add function to track +// 1. kprobe:tcp_enter_memory_pressure +// 2. kprobe:tcp_create_openreq_child (https://elixir.bootlin.com/linux/v6.18.6/source/net/ipv4/tcp_ipv4.c#L1776) [function: *tcp_v4_syn_recv_sock] + // docs: // // virtual ethernet (veth) interface tracer: diff --git a/core/src/components/identity/src/helpers.rs b/core/src/components/identity/src/helpers.rs index bd76a29..50414bf 100644 --- a/core/src/components/identity/src/helpers.rs +++ b/core/src/components/identity/src/helpers.rs @@ -1,14 +1,13 @@ -use aya::maps::perf::PerfEventArrayBuffer; -use cortexbrain_common::buffer_type::BufferType; use nix::net::if_::if_nameindex; use std::result::Result::Ok; -use tracing::{error, info}; +use tracing::info; // 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 // +#[inline(always)] pub fn ignore_iface(iface: &str) -> bool { let ignored_interfaces = ["eth0", "docker0", "tunl0", "lo"]; ignored_interfaces.contains(&iface) @@ -18,6 +17,7 @@ pub fn ignore_iface(iface: &str) -> bool { // This function retrieves the list of veth interfaces on the system, filtering out ignored interfaces with // the ignore_iface function. // +#[inline(always)] pub fn get_veth_channels() -> Vec { //filter interfaces and save the output in the let mut interfaces: Vec = Vec::new(); @@ -36,58 +36,6 @@ pub fn get_veth_channels() -> Vec { interfaces } -// docs: read buffer function: -// template function that take a mut perf_event_array_buffer of type T and a mutable buffer of Vec - -pub async fn read_perf_buffer>( - mut array_buffers: Vec>, - mut buffers: Vec, - buffer_type: BufferType, -) { - // loop over the buffers - loop { - 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); - } - // 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( - &mut buffers, - tot_events, - offset, - ) - .await - } - } - } - } - Err(e) => { - error!("Cannot read events from buffer. Reason: {} ", e); - } - } - } - tokio::time::sleep(std::time::Duration::from_millis(100)).await; // small sleep - } -} - #[cfg(test)] mod tests { use cortexbrain_common::buffer_type::VethLog; diff --git a/core/src/components/identity/src/main.rs b/core/src/components/identity/src/main.rs index 598b964..8d13e22 100644 --- a/core/src/components/identity/src/main.rs +++ b/core/src/components/identity/src/main.rs @@ -11,36 +11,35 @@ mod helpers; mod service_discovery; -use crate::helpers::{get_veth_channels, read_perf_buffer}; +use crate::helpers::get_veth_channels; use aya::{ Ebpf, - maps::{ - MapData, - perf::{PerfEventArray, PerfEventArrayBuffer}, - }, - programs::{SchedClassifier, TcAttachType, tc::SchedClassifierLinkId}, + maps::{Map, MapData}, + programs::{SchedClassifier, TcAttachType}, util::online_cpus, }; #[cfg(feature = "experimental")] 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 cortexbrain_common::{buffer_type::BufferType, map_handlers::BpfMapsData}; +use cortexbrain_common::{ + buffer_type::{BufferSize, BufferType, read_perf_buffer}, + constants, logger, + map_handlers::BpfMapsData, + map_handlers::{init_bpf_maps, map_manager, map_pinner, populate_blocklist}, + program_handlers::load_program, +}; use std::{ convert::TryInto, path::Path, sync::{Arc, Mutex}, }; -use anyhow::{Context, Ok}; -use cortexbrain_common::{constants, logger}; -use tokio::{fs, signal}; -use tracing::{debug, error, info, warn}; +use anyhow::{Context, Ok, anyhow}; -use std::collections::HashMap; +//use std::collections::HashMap; +use tokio::{fs, signal}; +use tracing::{error, info}; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { @@ -51,7 +50,7 @@ async fn main() -> Result<(), anyhow::Error> { info!("fetching data"); // To Store link_ids they can be used to detach tc - let link_ids = Arc::new(Mutex::new(HashMap::::new())); + //let mut link_ids = HashMap::::new(); //init conntracker data path let bpf_path = @@ -69,6 +68,7 @@ async fn main() -> Result<(), anyhow::Error> { "veth_identity_map".to_string(), "TcpPacketRegistry".to_string(), "Blocklist".to_string(), + "tracked_veth".to_string(), ]; match init_bpf_maps(bpf.clone(), map_data) { std::result::Result::Ok(bpf_maps) => { @@ -92,8 +92,8 @@ async fn main() -> Result<(), anyhow::Error> { } { - init_tc_classifier(bpf.clone(), interfaces, link_ids.clone()).await.context( - "An error occured during the execution of attach_bpf_program function" + init_tc_classifier(bpf.clone(), interfaces).await.context( + "An error occured during the execution of attach_bpf_program function", )?; } { @@ -122,10 +122,10 @@ async fn main() -> Result<(), anyhow::Error> { } //attach the tc classifier program to a vector of interfaces +// TODO: consider to create a load schedule classifier in the common functions async fn init_tc_classifier( bpf: Arc>, ifaces: Vec, - link_ids: Arc>>, ) -> Result<(), anyhow::Error> { //this funtion initialize the tc classifier program info!("Loading programs"); @@ -140,10 +140,33 @@ async fn init_tc_classifier( .try_into() .context("Failed to init SchedClassifier program")?; + // load classifier program + program .load() .context("Failed to load identity_classifier program")?; + // attach program only to desired interfaces. We can skip the dock0,tunl0,lo and eth0 interface + // we also save the interfaces to a BPF_HASH_MAP to easily monitor the interfaces using the agent + + // decleare link_ids HashMap which is a shared hashmap between kernel and userspace + // Link_ids hashmap has type of HashMap<[u8; 16], [u8; 8]>. The key is the program name and the value is the state + + // at this point the pinning is already successfull so we can invoque the maps from the pin + + let link_ids_mapdata = MapData::from_pin("/sys/fs/bpf/maps/tracked_veth") + .map_err(|e| anyhow!("Cannot return link_ids_mapdata. Reason: {}", e))?; + + let link_ids_map = Map::HashMap(link_ids_mapdata); + + let mut link_ids: aya::maps::HashMap = + aya::maps::HashMap::try_from(link_ids_map).map_err(|e| { + anyhow!( + "Cannot create link_ids HashMap from link_ids_map. Reason:{}", + e + ) + })?; + for interface in ifaces { match program.attach(&interface, TcAttachType::Ingress) { std::result::Result::Ok(link_id) => { @@ -151,10 +174,34 @@ async fn init_tc_classifier( "Program 'identity_classifier' attached to interface {}", interface ); - let mut map = link_ids - .lock() - .map_err(|e| anyhow::anyhow!("Cannot get value from lock. Reason: {}", e))?; - map.insert(interface.clone(), link_id); + let interface_bytes = interface.as_bytes(); + + let mut if_bytes = [0u8; 16]; + + // to set the len compare the interface_bytes.len() with the if_bytes.len() [16] and take the minimum + // if we have interface_bytes.len() < than 16 we set the len + let len = interface_bytes.len().min(if_bytes.len()); + + // now we can copy the bytes from the slice into the if_bytes variable + if_bytes[..len].copy_from_slice(&interface_bytes[..len]); + + // we compute the same process for the state_bytes + let mut state_bytes = [0u8; 8]; + let state = b"attached"; // prints "attached" as [u8;8] sequence of bytes + let state_len = state.len().min(state_bytes.len()); + state_bytes[..state_len].copy_from_slice(&state[..state_len]); + + match link_ids.insert(if_bytes, state_bytes, 0) { + std::result::Result::Ok(_) => { + info!("Veth interface {} added into map", &interface); + } + Err(e) => { + error!( + "Cannot add Veth interface {} into map. Reason: {}", + &interface, e + ); + } + } } Err(e) => error!( "Error attaching program to interface {}: {:?}", @@ -203,35 +250,17 @@ async fn event_listener(bpf_maps: BpfMapsData) -> Result<(), anyhow::Error> { //TODO: try to change from PerfEventArray to a RingBuffer data structure - let mut map_manager = - HashMap::, Vec>)>::new(); - - // create the PerfEventArrays and the buffers from the BpfMapsData Objects - for (map, name) in bpf_maps - .bpf_obj_map - .into_iter() - .zip(bpf_maps.bpf_obj_names.into_iter()) - // zip two iterators at the same time for map and mapnames - { - debug!("Debugging map type:{:?} for map name {:?}", map, &name); - info!("Creating PerfEventArray for map name {:?}", &name); - - // save the map in a registry if is a PerfEventArray to access them by name - if let std::result::Result::Ok(perf_event_array) = PerfEventArray::try_from(map) { - map_manager.insert(name.clone(), (perf_event_array, Vec::new())); - - // 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", &name); - } - } + let mut maps = map_manager(bpf_maps)?; // fill the input buffers with data from the PerfEventArrays for cpu_id in online_cpus().map_err(|e| anyhow::anyhow!("Error {:?}", e))? { - for (name, (perf_evt_array, perf_evt_array_buffer)) in map_manager.iter_mut() { - let buf = perf_evt_array.open(cpu_id, None)?; + for (name, (perf_evt_array, perf_evt_array_buffer)) in maps.iter_mut() { + let buf = perf_evt_array.open(cpu_id, None).map_err(|e| { + anyhow!( + "Cannot create perf_event_array buffer from perf_event_array. Reason: {}", + e + ) + })?; info!( "Buffer created for map {:?} on cpu_id {:?}. Buffer size: {}", name, @@ -245,23 +274,20 @@ async fn event_listener(bpf_maps: BpfMapsData) -> Result<(), anyhow::Error> { info!("Listening for events..."); // i need to use remove to move the values from the Map Manager to the the async tasks - let (perf_veth_array, perf_veth_buffers) = map_manager + let (perf_veth_array, perf_veth_buffers) = maps .remove("veth_identity_map") .expect("Cannot create perf_veth buffer"); - let (perf_net_events_array, perf_net_events_buffers) = map_manager + let (perf_net_events_array, perf_net_events_buffers) = maps .remove("events_map") .expect("Cannot create perf_net_events buffer"); - let (tcp_registry_array, tcp_registry_buffers) = map_manager + let (tcp_registry_array, tcp_registry_buffers) = maps .remove("TcpPacketRegistry") .expect("Cannot create tcp_registry buffer"); // init output buffers - let veth_buffers = vec![BytesMut::with_capacity(10 * 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()]; - - // init veth link ids - //let veth_link_ids = link_ids; + let veth_buffers = BufferSize::VethEvents.set_buffer(); + let events_buffers = BufferSize::ClassifierNetEvents.set_buffer(); + let tcp_buffers = BufferSize::TcpEvents.set_buffer(); // spawn async tasks let veth_events_displayer = tokio::spawn(async move { diff --git a/core/src/components/metrics/Cargo.toml b/core/src/components/metrics/Cargo.toml index 0e88d8c..c8dcb5b 100644 --- a/core/src/components/metrics/Cargo.toml +++ b/core/src/components/metrics/Cargo.toml @@ -20,8 +20,11 @@ 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", features = [ +cortexbrain-common = { path = "../../../common/", features = [ "map-handlers", "program-handlers", + "buffer-reader", + "monitoring-structs", + "network-structs" ] } 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 a67b607..843f45d 100644 --- a/core/src/components/metrics/src/helpers.rs +++ b/core/src/components/metrics/src/helpers.rs @@ -1,185 +1,68 @@ -use aya::{ - maps::{Map, MapData, PerfEventArray, perf::PerfEventArrayBuffer}, - util::online_cpus, -}; - -use bytes::BytesMut; -use std::sync::{ - Arc, - atomic::{AtomicBool, Ordering}, +use anyhow::anyhow; +use aya::util::online_cpus; +use cortexbrain_common::map_handlers::map_manager; +use cortexbrain_common::{ + buffer_type::{BufferSize, BufferType, read_perf_buffer}, + map_handlers::BpfMapsData, }; use tokio::signal; +use tracing::{error, info}; -use tracing::{debug, error, info, warn}; - -use crate::structs::NetworkMetrics; -use crate::structs::TimeStampMetrics; - -pub async fn display_metrics_map( - mut perf_buffers: Vec>, - running: Arc, // Changed to Arc - mut buffers: Vec, -) { - info!("Starting metrics event listener..."); - while running.load(Ordering::SeqCst) { - for buf in perf_buffers.iter_mut() { - match buf.read_events(&mut buffers) { - std::result::Result::Ok(events) => { - if events.read > 0 { - info!("Read {} metric events", events.read); - } - for i in 0..events.read { - let data = &buffers[i]; - if data.len() >= std::mem::size_of::() { - let net_metrics: NetworkMetrics = - unsafe { std::ptr::read_unaligned(data.as_ptr() as *const _) }; - let tgid = net_metrics.tgid; - let comm = String::from_utf8_lossy(&net_metrics.comm); - let ts_us = net_metrics.ts_us; - let sk_drop_count = net_metrics.sk_drops; - let sk_err = net_metrics.sk_err; - let sk_err_soft = net_metrics.sk_err_soft; - let sk_backlog_len = net_metrics.sk_backlog_len; - let sk_write_memory_queued = net_metrics.sk_write_memory_queued; - let sk_ack_backlog = net_metrics.sk_ack_backlog; - 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 - ); - } else { - info!( - "Received data too small: {} bytes, expected: {}", - data.len(), - std::mem::size_of::() - ); - } - } - } - Err(e) => { - error!("Error reading events: {:?}", e); - } - } - } - tokio::time::sleep(std::time::Duration::from_millis(100)).await; - } - info!("Metrics event listener stopped"); -} - -pub async fn display_time_stamp_events_map( - mut perf_buffers: Vec>, - running: Arc, // Changed to Arc - mut buffers: Vec, -) { - info!("Starting timestamp event listener..."); - while running.load(Ordering::SeqCst) { - for buf in perf_buffers.iter_mut() { - match buf.read_events(&mut buffers) { - std::result::Result::Ok(events) => { - if events.read > 0 { - info!("Read {} timestamp events", events.read); - } - for i in 0..events.read { - let data = &buffers[i]; - if data.len() >= std::mem::size_of::() { - let time_stamp_event: TimeStampMetrics = - unsafe { std::ptr::read_unaligned(data.as_ptr() as *const _) }; - let delta_us = time_stamp_event.delta_us; - let ts_us = time_stamp_event.ts_us; - let tgid = time_stamp_event.tgid; - let comm = String::from_utf8_lossy(&time_stamp_event.comm); - let lport = time_stamp_event.lport; - let dport_be = time_stamp_event.dport_be; - let af = time_stamp_event.af; - info!( - "TimeStampEvent - delta_us: {}, ts_us: {}, tgid: {}, comm: {}, lport: {}, dport_be: {}, af: {}", - delta_us, ts_us, tgid, comm, lport, dport_be, af - ); - } else { - info!("Received timestamp data too small: {} bytes", data.len()); - } - } - } - Err(e) => { - error!("Error reading timestamp events: {:?}", e); - } - } - } - tokio::time::sleep(std::time::Duration::from_millis(100)).await; - } - info!("Timestamp event listener stopped"); -} - -pub async fn event_listener(bpf_maps: Vec) -> Result<(), anyhow::Error> { +pub async fn event_listener(bpf_maps: BpfMapsData) -> Result<(), anyhow::Error> { info!("Getting 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..."); - for map in bpf_maps { - debug!("Debugging map type:{:?}", map); - 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 mut maps = map_manager(bpf_maps)?; 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); + for cpu_id in cpu_count { + for (name, (perf_event_array, perf_event_buffer)) in maps.iter_mut() { + let buf = perf_event_array.open(cpu_id, None).map_err(|e| { + anyhow!( + "Cannot create perf_event_array buffer from perf_event_array. Reason: {}", + e + ) + })?; + info!( + "Buffer created for map {:?} on cpu_id {:?}. Buffer size: {}", + name, + cpu_id, + std::mem::size_of_val(&buf) + ); + perf_event_buffer.push(buf); } } - //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)); + let (time_stamp_events_array, time_stamp_events_perf_buffer) = maps + .remove("time_stamp_events") + .expect("Cannot create time_stamp_events_buffer"); + let (net_perf_array, net_perf_buffer) = maps + .remove("net_metrics") + .expect("Cannot create net_perf_buffer"); // Create proper sized buffers - 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(); + let net_metrics_buffers = BufferSize::NetworkMetricsEvents.set_buffer(); + let time_stamp_events_buffers = BufferSize::TimeMetricsEvents.set_buffer(); 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; + read_perf_buffer( + net_perf_buffer, + net_metrics_buffers, + BufferType::NetworkMetrics, + ) + .await; }); let time_stamp_events_displayer = tokio::spawn(async move { - display_time_stamp_events_map( + read_perf_buffer( time_stamp_events_perf_buffer, - time_stamp_events_running, time_stamp_events_buffers, + BufferType::TimeStampMetrics, ) - .await + .await; }); info!("Event listeners started, entering main loop..."); @@ -199,9 +82,6 @@ pub async fn event_listener(bpf_maps: Vec) -> Result<(), anyhow::Error> { _ = signal::ctrl_c() => { info!("Ctrl-C received, shutting down..."); - // Stop the event loops - net_metrics_running_signal.store(false, std::sync::atomic::Ordering::SeqCst); - time_stamp_events_running_signal.store(false, std::sync::atomic::Ordering::SeqCst); } } diff --git a/core/src/components/metrics/src/main.rs b/core/src/components/metrics/src/main.rs index e8677fb..e5558eb 100644 --- a/core/src/components/metrics/src/main.rs +++ b/core/src/components/metrics/src/main.rs @@ -1,6 +1,5 @@ use anyhow::{Context, Ok}; use aya::Ebpf; -use cortexbrain_common::{constants, logger}; use std::{ env, fs, path::Path, @@ -11,15 +10,17 @@ use tracing::{error, info}; mod helpers; use crate::helpers::event_listener; -use cortexbrain_common::map_handlers::{init_bpf_maps, map_pinner}; -use cortexbrain_common::program_handlers::load_program; - -mod structs; +use cortexbrain_common::{ + constants, + logger::otlp_logger_init, + map_handlers::{init_bpf_maps, map_pinner}, + program_handlers::load_program, +}; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { //init tracing subscriber - logger::init_default_logger(); + let otlp_provider = otlp_logger_init("metrics-service".to_string()); info!("Starting metrics service..."); info!("fetching data"); @@ -78,6 +79,7 @@ async fn main() -> Result<(), anyhow::Error> { } Err(e) => { error!("Error initializing BPF maps: {:?}", e); + let _ = otlp_provider.shutdown(); return Err(e); } } diff --git a/core/src/components/metrics/src/structs.rs b/core/src/components/metrics/src/structs.rs deleted file mode 100644 index dc63ace..0000000 --- a/core/src/components/metrics/src/structs.rs +++ /dev/null @@ -1,33 +0,0 @@ - -pub const TASK_COMM_LEN: usize = 16; // linux/sched.h - -#[repr(C, packed)] -#[derive(Clone, Copy)] -pub struct NetworkMetrics { - pub tgid: u32, - pub comm: [u8; TASK_COMM_LEN], - pub ts_us: u64, - pub sk_err: i32, // Offset 284 - pub sk_err_soft: i32, // Offset 600 - pub sk_backlog_len: i32, // Offset 196 - pub sk_write_memory_queued: i32, // Offset 376 - pub sk_receive_buffer_size: i32, // Offset 244 - pub sk_ack_backlog: u32, // Offset 604 - pub sk_drops: i32, // Offset 136 -} - -#[repr(C)] -#[derive(Clone, Copy)] -pub struct TimeStampMetrics { - pub delta_us: u64, - pub ts_us: u64, - pub tgid: u32, - pub comm: [u8; TASK_COMM_LEN], - pub lport: u16, - pub dport_be: u16, - pub af: u16, - pub saddr_v4: u32, - pub daddr_v4: u32, - pub saddr_v6: [u32; 4], - pub daddr_v6: [u32; 4], -} \ No newline at end of file diff --git a/core/src/components/metrics_tracer/src/data_structures.rs b/core/src/components/metrics_tracer/src/data_structures.rs index f6d7afe..e9866a8 100644 --- a/core/src/components/metrics_tracer/src/data_structures.rs +++ b/core/src/components/metrics_tracer/src/data_structures.rs @@ -2,7 +2,7 @@ use aya_ebpf::{macros::map, maps::{LruPerCpuHashMap, HashMap, PerfEventArray}}; pub const TASK_COMM_LEN: usize = 16; - +#[repr(C,packed)] pub struct NetworkMetrics { pub tgid: u32, pub comm: [u8; TASK_COMM_LEN], @@ -16,7 +16,7 @@ pub struct NetworkMetrics { pub sk_drops: i32, // Offset 136 } -#[repr(C)] +#[repr(C,packed)] #[derive(Copy, Clone)] pub struct TimeStampStartInfo { pub comm: [u8; TASK_COMM_LEN], @@ -25,7 +25,7 @@ pub struct TimeStampStartInfo { } // Event we send to userspace when latency is computed -#[repr(C)] +#[repr(C,packed)] #[derive(Copy, Clone)] pub struct TimeStampEvent { pub delta_us: u64,