From 340e2ff355000de232aebcf9b322afaf8b859d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Tue, 3 Oct 2023 07:25:38 +0000 Subject: [PATCH] BpfSyscallWrappers: add queryProgram() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test: TreeHugger Bug: 292156770 Signed-off-by: Maciej Żenczykowski Change-Id: I0f77220a6c4b13efb5630d7a5ade1db39c59901e --- .../include/BpfSyscallWrappers.h | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/staticlibs/native/bpf_syscall_wrappers/include/BpfSyscallWrappers.h b/staticlibs/native/bpf_syscall_wrappers/include/BpfSyscallWrappers.h index 13f7cb396c..9995cb9eb1 100644 --- a/staticlibs/native/bpf_syscall_wrappers/include/BpfSyscallWrappers.h +++ b/staticlibs/native/bpf_syscall_wrappers/include/BpfSyscallWrappers.h @@ -44,6 +44,11 @@ inline int bpf(enum bpf_cmd cmd, const bpf_attr& attr) { return syscall(__NR_bpf, cmd, &attr, sizeof(attr)); } +// this version is meant for use with cmd's which mutate the argument +inline int bpf(enum bpf_cmd cmd, bpf_attr *attr) { + return syscall(__NR_bpf, cmd, attr, sizeof(*attr)); +} + inline int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries, uint32_t map_flags) { return bpf(BPF_MAP_CREATE, { @@ -160,6 +165,27 @@ inline int detachProgram(bpf_attach_type type, const BPF_FD_TYPE cg_fd) { }); } +inline int queryProgram(const BPF_FD_TYPE cg_fd, + enum bpf_attach_type attach_type, + __u32 query_flags = 0, + __u32 attach_flags = 0) { + int prog_id = -1; // equivalent to an array of one integer. + bpf_attr arg = { + .query = { + .target_fd = BPF_FD_TO_U32(cg_fd), + .attach_type = attach_type, + .query_flags = query_flags, + .attach_flags = attach_flags, + .prog_ids = ptr_to_u64(&prog_id), // pointer to output array + .prog_cnt = 1, // in: space - nr of ints in the array, out: used + } + }; + int v = bpf(BPF_PROG_QUERY, &arg); + if (v) return v; // error case + if (!arg.query.prog_cnt) return 0; // no program, kernel never returns zero id + return prog_id; // return actual id +} + inline int detachSingleProgram(bpf_attach_type type, const BPF_FD_TYPE prog_fd, const BPF_FD_TYPE cg_fd) { return bpf(BPF_PROG_DETACH, {