11use anyhow:: Context ;
2+ use anyhow:: anyhow;
23use chrono:: Local ;
34use cortexbrain_common:: formatters:: { format_ipv4, format_ipv6} ;
45use cortexbrain_common:: map_handlers:: load_perf_event_array_from_mapdata;
5- use prost:: bytes:: BytesMut ;
66use std:: str:: FromStr ;
77use std:: sync:: Mutex ;
88use tonic:: { Request , Response , Status } ;
@@ -28,7 +28,8 @@ use cortexbrain_common::buffer_type::VethLog;
2828// * contains agent api configuration
2929use crate :: agent:: {
3030 ActiveConnectionResponse , AddIpToBlocklistRequest , BlocklistResponse , RequestActiveConnections ,
31- RmIpFromBlocklistRequest , RmIpFromBlocklistResponse , VethResponse , agent_server:: Agent ,
31+ RmIpFromBlocklistRequest , RmIpFromBlocklistResponse , VethHashMapResponse , VethResponse ,
32+ agent_server:: Agent ,
3233} ;
3334use crate :: constants:: PIN_BLOCKLIST_MAP_PATH ;
3435
@@ -38,6 +39,9 @@ use cortexbrain_common::buffer_type::IpProtocols;
3839use std:: net:: Ipv4Addr ;
3940use tracing:: warn;
4041
42+ use cortexbrain_common:: buffer_type:: BufferSize ;
43+ use cortexbrain_common:: map_handlers:: map_manager;
44+
4145pub struct AgentApi {
4246 //* event_rx is an istance of a mpsc receiver.
4347 //* is used to receive the data from the transmitter (tx)
@@ -162,6 +166,9 @@ impl Default for AgentApi {
162166 tracked_veth_tx : veth_tx. clone ( ) ,
163167 } ;
164168
169+ // init map manager
170+ //let map_manager = map_manager(maps)?
171+
165172 // For network metrics
166173
167174 //spawn an event readers
@@ -177,7 +184,7 @@ impl Default for AgentApi {
177184 . open ( cpu_id, None )
178185 . expect ( "Error during the creation of net_events_buf structure" ) ;
179186
180- let buffers = vec ! [ BytesMut :: with_capacity ( 4096 ) ; 8 ] ;
187+ let buffers = BufferSize :: ClassifierNetEvents . set_buffer ( ) ;
181188 net_events_buffer. push ( ( buf, buffers) ) ;
182189 }
183190
@@ -262,7 +269,7 @@ impl Default for AgentApi {
262269 . open ( cpu_id, None )
263270 . expect ( "Error during the creation of net_metrics_buf structure" ) ;
264271
265- let buffers = vec ! [ BytesMut :: with_capacity ( 4096 ) ; 8 ] ;
272+ let buffers = BufferSize :: NetworkMetricsEvents . set_buffer ( ) ;
266273 net_metrics_buffer. push ( ( buf, buffers) ) ;
267274 }
268275
@@ -343,7 +350,7 @@ impl Default for AgentApi {
343350 . open ( cpu_id, None )
344351 . expect ( "Error during the creation of time stamp events buf structure" ) ;
345352
346- let buffers = vec ! [ BytesMut :: with_capacity ( 4096 ) ; 8 ] ;
353+ let buffers = BufferSize :: TimeMetricsEvents . set_buffer ( ) ;
347354 ts_events_buffer. push ( ( buf, buffers) ) ;
348355 }
349356
@@ -421,7 +428,7 @@ impl Default for AgentApi {
421428 . open ( cpu_id, None )
422429 . expect ( "Error during the creation of time stamp events buf structure" ) ;
423430
424- let buffers = vec ! [ BytesMut :: with_capacity ( 4096 ) ; 8 ] ;
431+ let buffers = BufferSize :: VethEvents . set_buffer ( ) ;
425432 veth_events_buffer. push ( ( buf, buffers) ) ;
426433 }
427434
@@ -560,7 +567,10 @@ impl Agent for AgentApi {
560567 //convert ip from string to [u8;4] type and insert into the bpf map
561568 let u8_4_ip = Ipv4Addr :: from_str ( & ip) . unwrap ( ) . octets ( ) ;
562569 //TODO: convert datetime in a kernel compatible format
563- blocklist_map. insert ( u8_4_ip, u8_4_ip, 0 ) ;
570+ blocklist_map
571+ . insert ( u8_4_ip, u8_4_ip, 0 )
572+ . map_err ( |e| anyhow ! ( "Cannot insert address in the blocklist. Reason: {}" , e) )
573+ . unwrap ( ) ;
564574 info ! ( "CURRENT BLOCKLIST: {:?}" , blocklist_map) ;
565575 }
566576 let path = std:: env:: var ( PIN_BLOCKLIST_MAP_PATH )
@@ -774,4 +784,33 @@ impl Agent for AgentApi {
774784
775785 Ok ( Response :: new ( response) )
776786 }
787+
788+ async fn get_tracked_veth_from_hash_map (
789+ & self ,
790+ request : Request < ( ) > ,
791+ ) -> Result < Response < VethHashMapResponse > , Status > {
792+ info ! ( "Returning veth hashmap" ) ;
793+ //open blocklist map
794+ let mapdata = MapData :: from_pin ( "/sys/fs/bpf/maps/tracked_veth" )
795+ . expect ( "cannot open tracked_veth Mapdata" ) ;
796+ let tracked_veth_mapdata = Map :: HashMap ( mapdata) ; //load mapdata
797+
798+ let tracked_veth_map: ayaHashMap < MapData , [ u8 ; 16 ] , [ u8 ; 8 ] > =
799+ ayaHashMap:: try_from ( tracked_veth_mapdata) . unwrap ( ) ;
800+
801+ //convert the maps with a buffer to match the protobuffer types
802+
803+ let mut converted_tracked_veth_map: HashMap < String , String > = HashMap :: new ( ) ;
804+ for item in tracked_veth_map. iter ( ) {
805+ let ( k, v) = item. unwrap ( ) ;
806+ // convert keys and values from [u8;4] to String
807+ let key = String :: from_utf8 ( k. to_vec ( ) ) . unwrap ( ) ;
808+ let value = String :: from_utf8 ( v. to_vec ( ) ) . unwrap ( ) ;
809+ converted_tracked_veth_map. insert ( key, value) ;
810+ }
811+ Ok ( Response :: new ( VethHashMapResponse {
812+ status : "success" . to_string ( ) ,
813+ veths : converted_tracked_veth_map,
814+ } ) )
815+ }
777816}
0 commit comments