Received: by 2002:a05:6359:322:b0:b3:69d0:12d8 with SMTP id ef34csp448938rwb; Wed, 10 Aug 2022 10:48:43 -0700 (PDT) X-Google-Smtp-Source: AA6agR7EaXcEkZJibkDnmxaUcEuPV5e6ZIimbjkmv8LIAOGlrIXA/TeZLXIZOsxyrCizeqBqhYsY X-Received: by 2002:a17:906:8a73:b0:730:9af8:b335 with SMTP id hy19-20020a1709068a7300b007309af8b335mr21615164ejc.14.1660153723207; Wed, 10 Aug 2022 10:48:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660153723; cv=none; d=google.com; s=arc-20160816; b=dFFGdUDF9iuU7q9gShomgpBy0eSuMblxjuC7fgbLlezVJaXkqoIVFV55D76xT01hh0 7iJtCqROTcpBrFgcN3Nb8f8sCbwE+VEtiUM87NRatDMuMcG8TMcCzQiqkK/nGoTroKf1 hOw1u9tnrNQdMCNziKt+WKcL3ARDdH3VgEPGwbDFF8v9LJAILKfqxMLwptOrMCCTmjVg D4v50hPfxZ8GxXTdq6SPjKYzH7TtjPJTa/TMgfKoiC90Oga581tfaOE9iEGFQ6zIbBc2 KJFCkAPjWBMHjmO3xSnOtDI2BmbqaDavQwWO+2OuQurQeVsEbfgrWmW/fSGbg3asGtkO PeNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=I9tivlRZmScwBqrzns7/IP6T5HhyGbJqTY+zREu0RMY=; b=j8A6Nky/3EBPGb2fLfRugdDyL0LW0qJIh6zRNyrWK+EzEEQUaxmGDGGnCb+9nlkSCv +TDEYgjZCej6S8wRhORUeUSZYbcN/cTo60IqpeoxaQS9UKPDbV15O/lBSdXKe1DOnv1t tXiNre7L3xfH4FH6RX5X005ot6F1xEAaqoGXwm3V8Q6fO0Ho2SNxlqpIToqSB3NsoYVD tMZWmDcFfUOYrBu2gD4MDfk+7CUbY0DbIOqmt0oqx2L966S/Ny/5DR2zwEQLPC44YRpL NzjnCKqIkfoC7km5fswzHOqPxWbPXIfFyY3lIdnXuKoEss/M6wAFF2dYhvnP4PBr5uYS uJCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=RFll4N5N; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hu8-20020a170907a08800b0072efc11c532si5129434ejc.41.2022.08.10.10.48.17; Wed, 10 Aug 2022 10:48:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=RFll4N5N; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233551AbiHJRTb (ORCPT + 99 others); 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: 8bit X-Spam-Status: No, score=-19.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 = \ 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 = 0; + +SEC("tracepoint/syscalls/sys_enter_execve") +int sys_enter_execve(void) { + count++; + struct event *event = bpf_ringbuf_reserve(&buffer, sizeof(struct event), 0); + if (!event) { + return 1; + } + + event->count = 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_ringbuf_query(&buffer, BPF_RB_PROD_POS)); + + return 0; +} + +char _license[] SEC("license") = "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 = (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 = -1; + struct toy_bpf *obj; + int err; + + libbpf_set_strict_mode(LIBBPF_STRICT_ALL); + + err = ensure_core_btf(&open_opts); + if (err) { + fprintf(stderr, "failed to fetch necessary BTF for CO-RE: %s\n", strerror(-err)); + return 1; + } + + obj = toy_bpf__open_opts(&open_opts); + if (!obj) { + fprintf(stderr, "failed to open BPF object\n"); + return 1; + } + + err = 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 = bpf_object__find_map_fd_by_name(obj->obj, "buffer"); + ring_buffer = 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 = 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