Skip to content

Commit 84ad58b

Browse files
Add ringbuf type hinting.
1 parent 5c8b132 commit 84ad58b

File tree

7 files changed

+119
-50
lines changed

7 files changed

+119
-50
lines changed

pythonbpf/maps/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .maps import HashMap, PerfEventArray
1+
from .maps import HashMap, PerfEventArray, RingBuf
22
from .maps_pass import maps_proc
33

4-
__all__ = ["HashMap", "PerfEventArray", "maps_proc"]
4+
__all__ = ["HashMap", "PerfEventArray", "maps_proc", "RingBuf"]

pythonbpf/maps/maps.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# This file provides type and function hints only and does not actually give any functionality.
12
class HashMap:
23
def __init__(self, key, value, max_entries):
34
self.key = key
@@ -33,3 +34,18 @@ def __init__(self, key_size, value_size):
3334

3435
def output(self, data):
3536
pass # Placeholder for output method
37+
38+
39+
class RingBuf:
40+
def __init__(self, max_entries):
41+
self.max_entries = max_entries
42+
43+
def reserve(self, size: int, flags=0):
44+
if size > self.max_entries:
45+
raise ValueError("size cannot be greater than set maximum entries")
46+
return 0
47+
48+
def submit(self, data, flags=0):
49+
pass
50+
51+
# add discard, output and also give names to flags and stuff

pythonbpf/maps/maps_pass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def create_bpf_map(module, map_name, map_params):
5151

5252

5353
def create_map_debug_info(module, map_global, map_name, map_params):
54-
"""Generate debug information metadata for BPF map"""
54+
"""Generate debug information metadata for BPF maps HASH and PERF_EVENT_ARRAY"""
5555
generator = DebugInfoGenerator(module)
5656

5757
uint_type = generator.get_uint32_type()

tests/c-form/ex8.bpf.c

Lines changed: 0 additions & 47 deletions
This file was deleted.

tests/c-form/ringbuf.bpf.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "vmlinux.h"
2+
#include <bpf/bpf_helpers.h>
3+
#include <bpf/bpf_tracing.h>
4+
#include <bpf/bpf_core_read.h>
5+
6+
struct {
7+
__uint(type, BPF_MAP_TYPE_RINGBUF);
8+
__uint(max_entries, 1 << 24); // 16 MB
9+
} rb SEC(".maps");
10+
11+
//struct msg {
12+
// u32 pid;
13+
// char comm[16];
14+
//};
15+
16+
//SEC("tracepoint/syscalls/sys_enter_execve")
17+
//int handle_execve(struct trace_event_raw_sys_enter *ctx)
18+
//{
19+
// struct msg *m;
20+
// m = bpf_ringbuf_reserve(&rb, sizeof(*m), 0);
21+
// if (!m)
22+
// return 0;
23+
//
24+
// m->pid = bpf_get_current_pid_tgid() >> 32;
25+
// bpf_get_current_comm(&m->comm, sizeof(m->comm));
26+
// bpf_ringbuf_submit(m, 0);
27+
// return 0;
28+
//}
29+
30+
//char LICENSE[] SEC("license") = "GPL";
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from pythonbpf import bpf, map, struct, section, bpfglobal, compile
2+
from pythonbpf.helpers import ktime, pid
3+
from pythonbpf.maps import PerfEventArray
4+
5+
from ctypes import c_void_p, c_int32, c_uint64
6+
7+
8+
# PLACEHOLDER EXAMPLE. THIS SHOULD TECHNICALLY STILL FAIL TESTS
9+
@bpf
10+
@struct
11+
class data_t:
12+
pid: c_uint64
13+
ts: c_uint64
14+
comm: str(16)
15+
16+
17+
@bpf
18+
@map
19+
def events() -> PerfEventArray:
20+
return PerfEventArray(key_size=c_int32, value_size=c_int32)
21+
22+
23+
@bpf
24+
@section("tracepoint/syscalls/sys_enter_clone")
25+
def hello(ctx: c_void_p) -> c_int32:
26+
dataobj = data_t()
27+
ts = ktime()
28+
strobj = "hellohellohello"
29+
dataobj.pid = pid()
30+
dataobj.ts = ktime()
31+
# dataobj.comm = strobj
32+
print(
33+
f"clone called at {dataobj.ts} by pid {dataobj.pid}, comm {strobj} at time {ts}"
34+
)
35+
events.output(dataobj)
36+
return c_int32(0)
37+
38+
39+
@bpf
40+
@bpfglobal
41+
def LICENSE() -> str:
42+
return "GPL"
43+
44+
45+
compile()

tests/failing_tests/ringbuf.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from pythonbpf import bpf, map, bpfglobal, section
2+
from pythonbpf.maps import RingBuf
3+
from ctypes import c_int32, c_void_p
4+
5+
6+
# Define a map
7+
@bpf
8+
@map
9+
def mymap() -> RingBuf:
10+
return RingBuf(max_entries=(1 << 24))
11+
12+
13+
@bpf
14+
@section("tracepoint/syscalls/sys_enter_clone")
15+
def testing(ctx: c_void_p) -> c_int32:
16+
return c_int32(0)
17+
18+
19+
@bpf
20+
@bpfglobal
21+
def LICENSE() -> str:
22+
return "GPL"
23+
24+
25+
compile()

0 commit comments

Comments
 (0)