Skip to content

Commit 075f3c5

Browse files
Merge pull request #5 from SentinalFS/feat/ringbuffer
feat: ring buffer
2 parents 5cc8b8f + 8c9a071 commit 075f3c5

5 files changed

Lines changed: 30 additions & 21 deletions

File tree

.github/workflows/releases.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ jobs:
6060
with:
6161
tag_name: v${{ env.version }}
6262
name: Release v${{ env.version }}
63-
files: build/monitor.bpf.o
63+
files: build/monitor.bpf.o
64+
# generate_release_notes: true
6465
env:
6566
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6667

CHANGELOG.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
[
1+
[ {
2+
"version": "0.2.0",
3+
"changes": [
4+
"Implimented ring buffer for file events"
5+
]
6+
},
27
{
38
"version": "0.1.2",
49
"changes": [

VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.2
1+
0.2.0

common.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ static __always_inline int trace_file_operation(struct pt_regs *ctx, struct file
66
if (!file)
77
return 0;
88

9-
u32 zero = 0;
10-
struct data_t *data = bpf_map_lookup_elem(&logs_data, &zero);
11-
if (!data)
9+
struct data_t *data = bpf_ringbuf_reserve(&events, sizeof(struct data_t), 0);
10+
if (!data) {
1211
return 0;
12+
}
1313
__builtin_memset(data, 0, sizeof(*data));
1414

15+
1516
struct dentry *de = NULL;
1617
bpf_core_read(&de, sizeof(de), &file->f_path.dentry);
17-
if (!de)
18+
if (!de) {
19+
bpf_ringbuf_discard(data, 0);
1820
return 0;
21+
}
1922

2023
struct qstr d_name = {};
2124
bpf_core_read(&d_name, sizeof(d_name), &de->d_name);
22-
if (d_name.len == 0)
25+
if (d_name.len == 0) {
26+
bpf_ringbuf_discard(data, 0);
2327
return 0;
28+
}
2429

2530
char fname[128] = {};
2631
bpf_core_read_str(fname, sizeof(fname), d_name.name);
@@ -35,8 +40,10 @@ static __always_inline int trace_file_operation(struct pt_regs *ctx, struct file
3540

3641
struct inode *inode = NULL;
3742
bpf_core_read(&inode, sizeof(inode), &file->f_inode);
38-
if (!inode)
43+
if (!inode) {
44+
bpf_ringbuf_discard(data, 0);
3945
return 0;
46+
}
4047

4148
u32 inode_num = 0;
4249
bpf_core_read(&inode_num, sizeof(inode_num), &inode->i_ino);
@@ -47,9 +54,12 @@ static __always_inline int trace_file_operation(struct pt_regs *ctx, struct file
4754
bpf_trace_printk("LOG: inode=%u\n", sizeof("LOG: inode=%u\n"), inode_num);
4855

4956
u32 *monitored = bpf_map_lookup_elem(&monitored_inodes, &key);
50-
if (!monitored)
57+
if (!monitored) {
58+
bpf_ringbuf_discard(data, 0);
5159
return 0;
60+
}
61+
62+
bpf_ringbuf_submit(data, 0);
5263

53-
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, data, sizeof(*data));
5464
return 0;
5565
}

data_types.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "headers.h"
22

3-
struct
4-
{
5-
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
3+
struct {
4+
__uint(type, BPF_MAP_TYPE_RINGBUF);
5+
__uint(max_entries, 256 * 1024);
66
} events SEC(".maps");
77

88
struct
@@ -13,13 +13,6 @@ struct
1313
__uint(max_entries, 256);
1414
} monitored_inodes SEC(".maps");
1515

16-
struct
17-
{
18-
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
19-
__type(key, u32);
20-
__type(value, struct data_t);
21-
__uint(max_entries, 1);
22-
} logs_data SEC(".maps");
2316

2417
struct inode_key
2518
{

0 commit comments

Comments
 (0)