Skip to content

Commit 536ea48

Browse files
add cgroup helper
1 parent 5ba29db commit 536ea48

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

pythonbpf/helper/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
smp_processor_id,
1717
uid,
1818
skb_store_bytes,
19+
get_current_cgroup_id,
1920
get_stack,
2021
XDP_DROP,
2122
XDP_PASS,
@@ -79,6 +80,7 @@ def helper_call_handler(
7980
"handle_helper_call",
8081
"emit_probe_read_kernel_str_call",
8182
"emit_probe_read_kernel_call",
83+
"get_current_cgroup_id",
8284
"ktime",
8385
"pid",
8486
"deref",

pythonbpf/helper/bpf_helper_handler.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class BPFHelperID(Enum):
3030
BPF_SKB_STORE_BYTES = 9
3131
BPF_GET_CURRENT_PID_TGID = 14
3232
BPF_GET_CURRENT_UID_GID = 15
33+
BPF_GET_CURRENT_CGROUP_ID = 80
3334
BPF_GET_CURRENT_COMM = 16
3435
BPF_PERF_EVENT_OUTPUT = 25
3536
BPF_GET_STACK = 67
@@ -67,6 +68,32 @@ def bpf_ktime_get_ns_emitter(
6768
result = builder.call(fn_ptr, [], tail=False)
6869
return result, ir.IntType(64)
6970

71+
@HelperHandlerRegistry.register(
72+
"get_current_cgroup_id",
73+
param_types=[],
74+
return_type=ir.IntType(64),
75+
)
76+
def bpf_get_current_cgroup_id(
77+
call,
78+
map_ptr,
79+
module,
80+
builder,
81+
func,
82+
local_sym_tab=None,
83+
struct_sym_tab=None,
84+
map_sym_tab=None,
85+
):
86+
"""
87+
Emit LLVM IR for bpf_get_current_cgroup_id helper function call.
88+
"""
89+
# func is an arg to just have a uniform signature with other emitters
90+
helper_id = ir.Constant(ir.IntType(64), BPFHelperID.BPF_GET_CURRENT_CGROUP_ID.value)
91+
fn_type = ir.FunctionType(ir.IntType(64), [], var_arg=False)
92+
fn_ptr_type = ir.PointerType(fn_type)
93+
fn_ptr = builder.inttoptr(helper_id, fn_ptr_type)
94+
result = builder.call(fn_ptr, [], tail=False)
95+
return result, ir.IntType(64)
96+
7097

7198
@HelperHandlerRegistry.register(
7299
"lookup",

pythonbpf/helper/helpers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ def get_stack(buf, flags=0):
5757
return ctypes.c_int64(0)
5858

5959

60+
def get_current_cgroup_id():
61+
"""Get the current cgroup ID"""
62+
return ctypes.c_int64(0)
63+
64+
6065
XDP_ABORTED = ctypes.c_int64(0)
6166
XDP_DROP = ctypes.c_int64(1)
6267
XDP_PASS = ctypes.c_int64(2)

0 commit comments

Comments
 (0)