Skip to content

Commit e35311f

Browse files
committed
edriver-rust: change lost_cnt to atomic
1 parent bc3712b commit e35311f

File tree

4 files changed

+115
-25
lines changed

4 files changed

+115
-25
lines changed

plugins/edriver-rust/src/bpf/common/common.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ static inline struct ipv6_pinfo *get_inet_pinet6(struct inet_sock *inet)
3838
return pinet6_own_impl;
3939
}
4040

41-
4241
static inline struct ipv6_pinfo *inet6_sk_own_impl(struct sock *__sk, struct inet_sock *inet)
4342
{
4443
volatile unsigned char sk_state_own_impl;
@@ -246,7 +245,6 @@ static __always_inline int get_sock_v6(struct sock *sk, struct hds_socket_info_v
246245

247246
/* ===== END ===== */
248247

249-
250248
static __always_inline struct file *fget_raw(struct task_struct *task, u64 fd_num)
251249
{
252250
struct file **fd = BPF_CORE_READ(task, files, fdt, fd);
@@ -356,4 +354,33 @@ static __always_inline void *get_fd(struct task_struct *task, u64 num)
356354
return get_path(__builtin_preserve_access_index(&path));
357355
}
358356

357+
static __always_inline bool is_x86_compat(struct task_struct *task)
358+
{
359+
#if defined(bpf_target_x86)
360+
return READ_KERN(task->thread_info.status) & TS_COMPAT;
361+
#else
362+
return false;
363+
#endif
364+
}
365+
366+
static __always_inline bool is_arm64_compat(struct task_struct *task)
367+
{
368+
#if defined(bpf_target_arm64)
369+
return READ_KERN(task->thread_info.flags) & _TIF_32BIT;
370+
#else
371+
return false;
372+
#endif
373+
}
374+
375+
static __always_inline bool is_compat(struct task_struct *task)
376+
{
377+
#if defined(bpf_target_x86)
378+
return is_x86_compat(task);
379+
#elif defined(bpf_target_arm64)
380+
return is_arm64_compat(task);
381+
#else
382+
return false;
383+
#endif
384+
}
385+
359386
#endif

plugins/edriver-rust/src/bpf/common/consts.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,65 @@
5858

5959
/* consts: vmlinux contants fix */
6060
#define PF_KTHREAD 0x00200000
61+
#define TS_COMPAT 0x0002 /* 32bit syscall active (64BIT)*/
62+
63+
/* system call mapping */
64+
#if defined(bpf_target_x86)
65+
#define NR_exit 80
66+
#define NR_exit_group 231
67+
/* execve */
68+
#define NR_execve 59
69+
#define NR_execveat 322
70+
/* ptrace & prctl */
71+
#define NR_ptrace 101
72+
#define NR_prctl 157
73+
/* mount */
74+
#define NR_mount 40
75+
/* kernel module init */
76+
#define NR_init_module 175
77+
#define NR_finit_module 313
78+
/* memfd */
79+
#define NR_memfd_create 319
80+
/* sockets */
81+
#define NR_connect 42
82+
#define NR_accept 43
83+
#define NR_accept4 288
84+
#define NR_listen 50
85+
#define NR_bind 49
86+
#define NR_recvfrom 45
87+
#define NR_recvmsg 47
88+
#define NR_recvmmsg 299
89+
/* bpf */
90+
#define NR_bpf 321
91+
#elif defined(bpf_target_arm64)
92+
#define NR_exit 93
93+
#define NR_exit_group 94
94+
/* execve */
95+
#define NR_execve 221
96+
#define NR_execveat 281
97+
/* ptrace & prctl */
98+
#define NR_ptrace 117
99+
#define NR_prctl 167
100+
/* mount */
101+
#define NR_mount 40
102+
/* kernel module init */
103+
#define NR_init_module 105
104+
#define NR_finit_module 273
105+
/* memfd */
106+
#define NR_memfd_create 279
107+
/* sockets */
108+
#define NR_connect 203
109+
#define NR_accept 202
110+
#define NR_accept4 242
111+
#define NR_listen 201
112+
#define NR_bind 200
113+
#define NR_recvfrom 207
114+
#define NR_recvmsg 212
115+
#define NR_recvmmsg 243
116+
/* bpf */
117+
#define NR_bpf 280
118+
#endif
119+
61120

62121
enum buf_index
63122
{

plugins/edriver-rust/src/bpf/common/edriver.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,23 @@ int rtp__process_exec(struct bpf_raw_tracepoint_args *ctx)
8282
return report_event(&c);
8383
}
8484

85-
// SEC("raw_tracepoint/sys_exit")
86-
// int rtp__sys_exit(struct bpf_raw_tracepoint_args *ctx)
87-
// {
88-
// /* skip failed syscalls */
89-
// if (ctx->args[1])
90-
// return 0;
91-
// return 0;
92-
// }
85+
SEC("raw_tracepoint/sys_exit")
86+
int rtp__sys_exit(struct bpf_raw_tracepoint_args *ctx)
87+
{
88+
/* skip failed syscalls */
89+
if (ctx->args[1])
90+
return 0;
91+
92+
/* task bit transform */
93+
struct task_struct *task = (struct task_struct *)bpf_get_current_task();
94+
95+
/* arch */
96+
if (is_x86_compat(task)) {
97+
98+
}
99+
100+
return 0;
101+
}
93102

94103
/* proc_info init */
95104
static struct proc_info *proc_info_init(struct task_struct *task)

plugins/edriver-rust/src/bpfmgr.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use libbpf_rs::{
88
};
99
use log::*;
1010
use sdk::{Client, Record};
11+
use std::sync::atomic::{AtomicU64, Ordering};
1112
use std::{
12-
sync::{Arc, Mutex},
13+
sync::Arc,
1314
thread,
1415
time::{Duration, SystemTime, UNIX_EPOCH},
1516
};
@@ -24,7 +25,7 @@ mod hades_skel {
2425
use hades_skel::*;
2526

2627
lazy_static! {
27-
pub static ref LOSS_CNT: Arc<Mutex<u64>> = Arc::new(Mutex::new(0));
28+
pub static ref LOSS_CNT: Arc<AtomicU64> = Arc::new(AtomicU64::new(0));
2829
}
2930

3031
pub struct Bpfmanager {}
@@ -39,7 +40,6 @@ impl Bpfmanager {
3940

4041
skel.attach().context("Skel attach failed")?;
4142

42-
let loss_cnt_c = LOSS_CNT.clone();
4343
let mut trans = Transformer::new();
4444

4545
/* event handle wrap */
@@ -48,7 +48,7 @@ impl Bpfmanager {
4848
println!("{:?}", map);
4949
};
5050

51-
Self::start_heartbeat_thread(client, loss_cnt_c)?;
51+
Self::start_heartbeat_thread(client)?;
5252

5353
let binding = skel.maps();
5454
let map = binding.events();
@@ -63,8 +63,7 @@ impl Bpfmanager {
6363
}
6464

6565
fn handle_lost_events(_cpu: i32, cnt: u64) {
66-
let mut loss_count = LOSS_CNT.lock().unwrap();
67-
*loss_count += cnt;
66+
LOSS_CNT.fetch_add(cnt, Ordering::SeqCst);
6867
}
6968

7069
fn bump_rlimit() -> Result<()> {
@@ -78,7 +77,7 @@ impl Bpfmanager {
7877
Ok(())
7978
}
8079

81-
fn start_heartbeat_thread(mut client: Client, loss_counter: Arc<Mutex<u64>>) -> Result<()> {
80+
fn start_heartbeat_thread(mut client: Client) -> Result<()> {
8281
thread::Builder::new()
8382
.name("heartbeat".to_string())
8483
.spawn(move || loop {
@@ -92,20 +91,16 @@ impl Bpfmanager {
9291
rec.data_type = 900;
9392

9493
let pld = rec.mut_data();
95-
if let Ok(loss_count) = loss_counter.lock() {
96-
pld.fields
97-
.insert("loss_cnt".to_string(), loss_count.to_string());
98-
} else {
99-
warn!("Failed to lock loss_counter");
100-
continue;
101-
}
94+
let loss_count = LOSS_CNT.load(Ordering::SeqCst);
95+
pld.fields
96+
.insert("loss_cnt".to_string(), loss_count.to_string());
10297

10398
if let Err(err) = client.send_record(&rec) {
10499
warn!("Heartbeat will exit: {}", err);
105100
break;
106101
}
107102

108-
*loss_counter.lock().unwrap() = 0;
103+
LOSS_CNT.store(0, Ordering::SeqCst); // Reset the loss counter
109104
thread::sleep(Duration::from_secs(30));
110105
})
111106
.context("Failed to spawn heartbeat thread")?;

0 commit comments

Comments
 (0)