mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Merge branch 'master' into bcc_examples
This commit is contained in:
@ -1,23 +1,9 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
#include <linux/bpf.h>
|
||||
#include "vmlinux.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include <bpf/bpf_tracing.h>
|
||||
|
||||
struct trace_entry {
|
||||
short unsigned int type;
|
||||
unsigned char flags;
|
||||
unsigned char preempt_count;
|
||||
int pid;
|
||||
};
|
||||
|
||||
struct trace_event_raw_sys_enter {
|
||||
struct trace_entry ent;
|
||||
long int id;
|
||||
long unsigned int args[6];
|
||||
char __data[0];
|
||||
};
|
||||
|
||||
struct event {
|
||||
__u32 pid;
|
||||
__u32 uid;
|
||||
@ -33,7 +19,7 @@ struct {
|
||||
SEC("tp/syscalls/sys_enter_setuid")
|
||||
int handle_setuid_entry(struct trace_event_raw_sys_enter *ctx) {
|
||||
struct event data = {};
|
||||
|
||||
struct blk_integrity_iter it = {};
|
||||
// Extract UID from the syscall arguments
|
||||
data.uid = (unsigned int)ctx->args[0];
|
||||
data.ts = bpf_ktime_get_ns();
|
||||
|
||||
@ -1,10 +1,17 @@
|
||||
from pythonbpf import bpf, map, section, bpfglobal, compile_to_ir
|
||||
from pythonbpf.maps import HashMap
|
||||
from pythonbpf.helper import XDP_PASS
|
||||
from vmlinux import struct_xdp_md
|
||||
from vmlinux import struct_xdp_buff # noqa: F401
|
||||
from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
|
||||
from vmlinux import TASK_COMM_LEN # noqa: F401
|
||||
|
||||
from vmlinux import struct_qspinlock # noqa: F401
|
||||
|
||||
# from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401
|
||||
# from vmlinux import struct_posix_cputimers # noqa: F401
|
||||
from vmlinux import struct_xdp_md
|
||||
|
||||
# from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401
|
||||
# from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
|
||||
# from vmlinux import struct_request # noqa: F401
|
||||
from ctypes import c_int64
|
||||
|
||||
# Instructions to how to run this program
|
||||
|
||||
47
tests/passing_tests/vmlinux/simple_struct_test.py
Normal file
47
tests/passing_tests/vmlinux/simple_struct_test.py
Normal file
@ -0,0 +1,47 @@
|
||||
import logging
|
||||
|
||||
from pythonbpf import bpf, section, bpfglobal, compile_to_ir, map
|
||||
from pythonbpf import compile # noqa: F401
|
||||
from vmlinux import TASK_COMM_LEN # noqa: F401
|
||||
from vmlinux import struct_trace_event_raw_sys_enter # noqa: F401
|
||||
from ctypes import c_uint64, c_int32, c_int64
|
||||
from pythonbpf.maps import HashMap
|
||||
|
||||
# from vmlinux import struct_uinput_device
|
||||
# from vmlinux import struct_blk_integrity_iter
|
||||
|
||||
|
||||
@bpf
|
||||
@map
|
||||
def mymap() -> HashMap:
|
||||
return HashMap(key=c_int32, value=c_uint64, max_entries=TASK_COMM_LEN)
|
||||
|
||||
|
||||
@bpf
|
||||
@map
|
||||
def mymap2() -> HashMap:
|
||||
return HashMap(key=c_int32, value=c_uint64, max_entries=18)
|
||||
|
||||
|
||||
# Instructions to how to run this program
|
||||
# 1. Install PythonBPF: pip install pythonbpf
|
||||
# 2. Run the program: python examples/simple_struct_test.py
|
||||
# 3. Run the program with sudo: sudo tools/check.sh run examples/simple_struct_test.o
|
||||
# 4. Attach object file to any network device with something like ./check.sh run examples/simple_struct_test.o tailscale0
|
||||
# 5. send traffic through the device and observe effects
|
||||
@bpf
|
||||
@section("tracepoint/syscalls/sys_enter_execve")
|
||||
def hello_world(ctx: struct_trace_event_raw_sys_enter) -> c_int64:
|
||||
a = 2 + TASK_COMM_LEN + TASK_COMM_LEN
|
||||
print(f"Hello, World{TASK_COMM_LEN} and {a}")
|
||||
return c_int64(TASK_COMM_LEN + 2)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
|
||||
compile_to_ir("simple_struct_test.py", "simple_struct_test.ll", loglevel=logging.DEBUG)
|
||||
# compile()
|
||||
Reference in New Issue
Block a user