mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
The selftest spike substituted a one-entry HashMap for every upstream global because PythonBPF had no global variable support. Integer-scalar @bpfglobal support has since landed, so the three affected ports now declare the upstream globals directly: get_cgroup_id reads expected_pid and writes cg_id, autoattach shares prog1_called/prog2_called between two programs, and perf_skip reads ip. perf_skip stays a strict xfail on the nested ctx.regs.ip access. Update the tests README and porting notes accordingly; the WORKAROUND tag no longer exists in the tree. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DLMW3X7opqAPH6bjmTVkr7
56 lines
1.2 KiB
Python
56 lines
1.2 KiB
Python
# Ported from Linux tools/testing/selftests/bpf/progs/get_cgroup_id_kern.c
|
|
#
|
|
# Upstream records the cgroup id of a process whose pid matches one the
|
|
# userspace half of the test set beforehand:
|
|
#
|
|
# __u64 cg_id;
|
|
# __u64 expected_pid;
|
|
#
|
|
# SEC("tracepoint/syscalls/sys_enter_nanosleep")
|
|
# int trace(void *ctx)
|
|
# {
|
|
# __u32 pid = bpf_get_current_pid_tgid();
|
|
#
|
|
# if (expected_pid == pid)
|
|
# cg_id = bpf_get_current_cgroup_id();
|
|
#
|
|
# return 0;
|
|
# }
|
|
#
|
|
# Both file-scope variables are @bpfglobal scalars, so the userspace half reads
|
|
# `cg_id` back out of the object's .bss exactly as the kernel's driver does.
|
|
|
|
from pythonbpf import bpf, section, bpfglobal, compile
|
|
from pythonbpf.helper import pid, get_current_cgroup_id
|
|
from ctypes import c_void_p, c_int64, c_uint64
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def cg_id() -> c_uint64:
|
|
return c_uint64(0)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def expected_pid() -> c_uint64:
|
|
return c_uint64(0)
|
|
|
|
|
|
@bpf
|
|
@section("tracepoint/syscalls/sys_enter_nanosleep")
|
|
def trace(ctx: c_void_p) -> c_int64:
|
|
global cg_id
|
|
if expected_pid == pid():
|
|
cg_id = get_current_cgroup_id()
|
|
return c_int64(0)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def LICENSE() -> str:
|
|
return "GPL"
|
|
|
|
|
|
compile()
|