From nobody Sun Dec 14 01:53:26 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8A26FC25B0E for ; Wed, 10 Aug 2022 17:19:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233553AbiHJRTb (ORCPT ); Wed, 10 Aug 2022 13:19:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233557AbiHJRTL (ORCPT ); Wed, 10 Aug 2022 13:19:11 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 615B166108; Wed, 10 Aug 2022 10:19:10 -0700 (PDT) Received: from pwmachine.numericable.fr (85-170-37-153.rev.numericable.fr [85.170.37.153]) by linux.microsoft.com (Postfix) with ESMTPSA id AD3B5210C8B3; Wed, 10 Aug 2022 10:19:06 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com AD3B5210C8B3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1660151950; bh=I9tivlRZmScwBqrzns7/IP6T5HhyGbJqTY+zREu0RMY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RFll4N5NoZv1YGWF+Um4IKPdaf6s8oYssU0udmYz7UjNpEuq73LDcNdlvGxZncL1E sawqjXjcZ+PHk2XH/ynJzhKeoiqgZut2x+YtoPJTTRlvvlPjWBB9FPxCcedg1soDrD 4949XW8tsSdTK48Mpo0WSU0zrlT0f6OeHxIHJqew= From: Francis Laniel To: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Francis Laniel , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Joanne Koong , Dave Marchevsky , Lorenzo Bianconi , Geliang Tang , Hengqi Chen Subject: [PATCH] for test purpose only: Add toy to play with BPF ring buffer. Date: Wed, 10 Aug 2022 19:16:55 +0200 Message-Id: <20220810171702.74932-5-flaniel@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220810171702.74932-1-flaniel@linux.microsoft.com> References: <20220810171702.74932-1-flaniel@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This patch should be applied on iovisor/bcc. Signed-off-by: Francis Laniel --- libbpf-tools/Makefile | 1 + libbpf-tools/toy.bpf.c | 32 ++++++++++++++++++++ libbpf-tools/toy.c | 67 ++++++++++++++++++++++++++++++++++++++++++ libbpf-tools/toy.h | 4 +++ 4 files changed, 104 insertions(+) create mode 100644 libbpf-tools/toy.bpf.c create mode 100644 libbpf-tools/toy.c create mode 100644 libbpf-tools/toy.h diff --git a/libbpf-tools/Makefile b/libbpf-tools/Makefile index c3bbac27..904e7712 100644 --- a/libbpf-tools/Makefile +++ b/libbpf-tools/Makefile @@ -62,6 +62,7 @@ APPS =3D \ tcplife \ tcprtt \ tcpsynbl \ + toy \ vfsstat \ # diff --git a/libbpf-tools/toy.bpf.c b/libbpf-tools/toy.bpf.c new file mode 100644 index 00000000..b6b8f92b --- /dev/null +++ b/libbpf-tools/toy.bpf.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include "toy.h" + + +struct { + __uint(type, BPF_MAP_TYPE_RINGBUF); + __uint(max_entries, 4096); + __uint(map_flags, 1U << 13); +} buffer SEC(".maps"); + +static __u32 count =3D 0; + +SEC("tracepoint/syscalls/sys_enter_execve") +int sys_enter_execve(void) { + count++; + struct event *event =3D bpf_ringbuf_reserve(&buffer, sizeof(struct event)= , 0); + if (!event) { + return 1; + } + + event->count =3D count; + bpf_ringbuf_submit(event, 0); + + bpf_printk("addr: %p; count: %u\n", event, count); + bpf_printk("available: %lu; cons pos: %lu; prod pos: %lu\n", bpf_ringbuf_= query(&buffer, 0), bpf_ringbuf_query(&buffer, BPF_RB_CONS_POS), bpf_ringbu= f_query(&buffer, BPF_RB_PROD_POS)); + + return 0; +} + +char _license[] SEC("license") =3D "GPL"; \ No newline at end of file diff --git a/libbpf-tools/toy.c b/libbpf-tools/toy.c new file mode 100644 index 00000000..7e4f7fdf --- /dev/null +++ b/libbpf-tools/toy.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include "toy.h" +#include "toy.skel.h" +#include "btf_helpers.h" + + +static int buf_process_sample(void *ctx, void *data, size_t len) { + struct event *evt =3D (struct event *)data; + printf("%d\n", evt->count); + + return 0; +} + +int main(void) { + LIBBPF_OPTS(bpf_object_open_opts, open_opts); + int buffer_map_fd =3D -1; + struct toy_bpf *obj; + int err; + + libbpf_set_strict_mode(LIBBPF_STRICT_ALL); + + err =3D ensure_core_btf(&open_opts); + if (err) { + fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerro= r(-err)); + return 1; + } + + obj =3D toy_bpf__open_opts(&open_opts); + if (!obj) { + fprintf(stderr, "failed to open BPF object\n"); + return 1; + } + + err =3D toy_bpf__load(obj); + if (err) { + fprintf(stderr, "failed to load BPF object: %d\n", err); + return 1; + } + + struct ring_buffer *ring_buffer; + + buffer_map_fd =3D bpf_object__find_map_fd_by_name(obj->obj, "buffer"); + ring_buffer =3D ring_buffer__new(buffer_map_fd, buf_process_sample, NULL,= NULL); + + if(!ring_buffer) { + fprintf(stderr, "failed to create ring buffer\n"); + return 1; + } + + err =3D toy_bpf__attach(obj); + if (err) { + fprintf(stderr, "failed to attach BPF programs\n"); + return 1; + } + + puts("Press any key to begin consuming!"); + getchar(); + + while(1) { + ring_buffer__consume(ring_buffer); + sleep(1); + } + + return 0; +} diff --git a/libbpf-tools/toy.h b/libbpf-tools/toy.h new file mode 100644 index 00000000..36998170 --- /dev/null +++ b/libbpf-tools/toy.h @@ -0,0 +1,4 @@ +struct event { + __u32 count; + char filler[4096 / 8 - sizeof(__u32)]; +}; \ No newline at end of file -- 2.25.1