Skip to content

Commit 47cd33f

Browse files
[#105]: added extract_netns_inum function to extract the network namespace inode. added PID parameter in VethLog structure
1 parent 12806f9 commit 47cd33f

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

core/src/components/conntracker/src/data_structures.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ pub struct PacketLog {
1111
pub pid: u64,
1212
}
1313

14-
#[repr(C)]
15-
#[derive(Clone, Copy)]
16-
pub struct NetnsLog {
17-
pub netns: u32,
18-
pub pid: u64,
19-
}
20-
2114
// This structure is only for active connections
2215
#[repr(C)]
2316
#[derive(Clone, Copy)]
@@ -36,15 +29,14 @@ pub struct VethLog {
3629
pub state: u64, //state var type: long unsigned int
3730
pub dev_addr: [u32; 8],
3831
pub event_type: u8, //i choose 1 for veth creation or 2 for veth destruction
39-
//pub netns: u32,
40-
//pub ns_common_ptr : usize,
32+
pub netns: u32,
33+
pub pid: u32
34+
4135
}
4236

4337
#[map(name = "EventsMap")]
4438
pub static mut EVENTS: PerfEventArray<PacketLog> = PerfEventArray::new(0);
4539

46-
#[map(name = "NetnsMap")]
47-
pub static mut NET_EVENTS: PerfEventArray<NetnsLog> = PerfEventArray::new(0);
4840
//TODO: ConnectionMap needs a rework after implementing issue #105
4941
#[map(name = "ConnectionMap")]
5042
pub static mut ACTIVE_CONNECTIONS: LruPerCpuHashMap<u16, ConnArray> =

core/src/components/conntracker/src/main.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ use aya_ebpf::{
2828
use aya_log_ebpf::info;
2929

3030
use crate::bindings::{net, net_device, ns_common, possible_net_t};
31-
use crate::data_structures::{ConnArray, NetnsLog, PacketLog, VethLog};
32-
use crate::data_structures::{
33-
ACTIVE_CONNECTIONS, CONNTRACKER, EVENTS, NET_EVENTS, VETH_EVENTS,
34-
};
31+
use crate::data_structures::{ConnArray, PacketLog, VethLog};
32+
use crate::data_structures::{ACTIVE_CONNECTIONS, CONNTRACKER, EVENTS, VETH_EVENTS};
3533
/*
3634
* ETHERNET TYPE II FRAME:
3735
* Reference: https://it.wikipedia.org/wiki/Frame_Ethernet
@@ -151,6 +149,18 @@ fn read_linux_inner_value<T: Copy>(ptr: *const u8, offset: usize) -> Result<T, i
151149
Ok(inner_value)
152150
}
153151

152+
fn extract_netns_inum(net_device_pointer: *const u8) -> Result<u32, i64> {
153+
let possible_net_t_offset = 280;
154+
155+
let net = read_linux_inner_struct::<net>(net_device_pointer, possible_net_t_offset)?;
156+
157+
let ns_common_offset = 120;
158+
159+
let inum_offset = 16;
160+
let inum_ptr = read_linux_inner_value::<u32>(net as *const u8, ns_common_offset + inum_offset)?;
161+
Ok(inum_ptr)
162+
}
163+
154164
//mode selection:
155165
//1->veth_creation_tracer
156166
//2->veth_deletion_tracer
@@ -180,6 +190,9 @@ pub fn try_veth_tracer(ctx: ProbeContext, mode: u8) -> Result<u32, i64> {
180190
let dev_addr_array: [u32; 8] =
181191
read_linux_inner_value::<[u32; 8]>(net_device_pointer as *const u8, dev_addr_offset)?;
182192

193+
let inum: u32 = extract_netns_inum(net_device_pointer as *const u8)?;
194+
let pid: u32 = (bpf_get_current_pid_tgid() << 32) as u32; //extracting lower 32 bit corresponding to the PID
195+
183196
//buffer copying for array types
184197
name_buf.copy_from_slice(&name_array);
185198
dev_addr_buf.copy_from_slice(&dev_addr_array);
@@ -190,7 +203,8 @@ pub fn try_veth_tracer(ctx: ProbeContext, mode: u8) -> Result<u32, i64> {
190203
state: state.into(),
191204
dev_addr: dev_addr_buf,
192205
event_type: mode,
193-
//netns: inum,
206+
netns: inum,
207+
pid,
194208
};
195209

196210
//send the data to the userspace

0 commit comments

Comments
 (0)