From patchwork Mon Sep 25 21:50:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398461 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63DA5241FA for ; Mon, 25 Sep 2023 21:52:26 +0000 (UTC) Received: from mx0a-00206402.pphosted.com (mx0a-00206402.pphosted.com [148.163.148.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57436AF for ; Mon, 25 Sep 2023 14:52:25 -0700 (PDT) Received: from pps.filterd (m0354651.ppops.net [127.0.0.1]) by mx0a-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PF7NxV006685; Mon, 25 Sep 2023 21:52:06 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=ZYvawROEYx3id48z8q6ojMBqyEktlKiFNiYZTTR9uDE=; b=EPbMBlg+bnE2 JVvh38SuR4mt+UZvuC3RstqC34Grw/Gz2OWBxXfZMkJelH0JZ39ji624FqynktjW YXGIDLs+YvcjOOK+dI85WQk8X60AZiP4nPULwjyiyofdtFGP2ujWP8JdV0+u37go 9e4L+4Lp5sHR2v5ATTcBtillKzUmylmBN1Aq0JtJX/UiyH4O6++o8366KakQJyMa YczMFIwgB6Rhv7FthLdudpsqeX8P3y26XY4HXoQ5wGJ3Tw0NUbo5NrLdpPL3slBe lmBiVxffaYwKw1+p/ax4PFpLj+XahkPNzmtknl8R8GfolccmzEg0qA168lDVG3jo 0AYEUQc5kw== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0a-00206402.pphosted.com (PPS) with ESMTPS id 3tab8xcpjk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:06 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:04 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 01/14] libbpf: refactor cleanup in ring_buffer__add Date: Mon, 25 Sep 2023 14:50:32 -0700 Message-ID: <20230925215045.2375758-2-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-GUID: 4ytP_lTS83IDNhAVEl2tCf1q9erCdQpY X-Proofpoint-ORIG-GUID: 4ytP_lTS83IDNhAVEl2tCf1q9erCdQpY X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 mlxscore=0 spamscore=0 suspectscore=0 bulkscore=0 adultscore=0 malwarescore=0 priorityscore=1501 impostorscore=0 phishscore=0 lowpriorityscore=0 mlxlogscore=640 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Refactor the cleanup code in ring_buffer__add to use a unified err_out label. This reduces code duplication, as well as plugging a potential leak if mmap_sz != (__u64)(size_t)mmap_sz (currently this would miss unmapping tmp because ringbuf_unmap_ring isn't called). Signed-off-by: Martin Kelly --- tools/lib/bpf/ringbuf.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index 02199364db13..29b0d19d920f 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -121,7 +121,7 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd, err = -errno; pr_warn("ringbuf: failed to mmap consumer page for map fd=%d: %d\n", map_fd, err); - return libbpf_err(err); + goto err_out; } r->consumer_pos = tmp; @@ -131,16 +131,16 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd, */ mmap_sz = rb->page_size + 2 * (__u64)info.max_entries; if (mmap_sz != (__u64)(size_t)mmap_sz) { + err = -E2BIG; pr_warn("ringbuf: ring buffer size (%u) is too big\n", info.max_entries); - return libbpf_err(-E2BIG); + goto err_out; } tmp = mmap(NULL, (size_t)mmap_sz, PROT_READ, MAP_SHARED, map_fd, rb->page_size); if (tmp == MAP_FAILED) { err = -errno; - ringbuf_unmap_ring(rb, r); pr_warn("ringbuf: failed to mmap data pages for map fd=%d: %d\n", map_fd, err); - return libbpf_err(err); + goto err_out; } r->producer_pos = tmp; r->data = tmp + rb->page_size; @@ -152,14 +152,17 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd, e->data.fd = rb->ring_cnt; if (epoll_ctl(rb->epoll_fd, EPOLL_CTL_ADD, map_fd, e) < 0) { err = -errno; - ringbuf_unmap_ring(rb, r); pr_warn("ringbuf: failed to epoll add map fd=%d: %d\n", map_fd, err); - return libbpf_err(err); + goto err_out; } rb->ring_cnt++; return 0; + +err_out: + ringbuf_unmap_ring(rb, r); + return libbpf_err(err); } void ring_buffer__free(struct ring_buffer *rb) From patchwork Mon Sep 25 21:50:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398465 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7441124212 for ; Mon, 25 Sep 2023 21:52:30 +0000 (UTC) Received: from mx0b-00206402.pphosted.com (mx0b-00206402.pphosted.com [148.163.152.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FA3E11F for ; Mon, 25 Sep 2023 14:52:28 -0700 (PDT) Received: from pps.filterd (m0354653.ppops.net [127.0.0.1]) by mx0b-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PEYNow032155; Mon, 25 Sep 2023 21:52:07 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=aW0HDDBA4c83GEOOknjkukcLXuT9U0g63V3k85Li8jc=; b=LpObpGzwwvmQ pMZy5jqjvnDcYTYlG40GSK9CdNS6nsi1eFeTK4/NUrPbDvyeEhJ8FdhwvZCtSVfX 9T4BOCMqHIL8M0ANH+wk5EXabIu6l3oxMIqG+G5VCora4wMl+bIgbqSxFNgTHt/A dDboocPQF03fRCvwYJjSej074h0XAuRvv+u2uxwK4ctA184Z21v6LYV1b96Tc7uh NBGOt99tNA1UiDJniLtaniPOoLKG5JCSbZVRfpPuybTOjFdQWYK6VosyxMNTJcuF 4LnIjJ9k3t/WuosMV/epnhERSXytI9dC/28kejkc0UaLljoOhpQMJPyp5kdoM59P AyXEj1frbg== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0b-00206402.pphosted.com (PPS) with ESMTPS id 3taayjmyxw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:07 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:05 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 02/14] libbpf: switch rings to array of pointers Date: Mon, 25 Sep 2023 14:50:33 -0700 Message-ID: <20230925215045.2375758-3-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-ORIG-GUID: -0ejOJh8YP1_pnM3TiT3hq9ehan_Wt34 X-Proofpoint-GUID: -0ejOJh8YP1_pnM3TiT3hq9ehan_Wt34 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 bulkscore=0 clxscore=1015 priorityscore=1501 mlxlogscore=999 spamscore=0 malwarescore=0 suspectscore=0 phishscore=0 impostorscore=0 adultscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Switch rb->rings to be an array of pointers instead of a contiguous block. This allows for each ring pointer to be stable after ring_buffer__add is called, which allows us to expose struct ring * to the user without gotchas. Without this change, the realloc in ring_buffer__add could invalidate a struct ring *, making it unsafe to give to the user. Signed-off-by: Martin Kelly --- tools/lib/bpf/ringbuf.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index 29b0d19d920f..94d11fb44a49 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -34,7 +34,7 @@ struct ring { struct ring_buffer { struct epoll_event *events; - struct ring *rings; + struct ring **rings; size_t page_size; int epoll_fd; int ring_cnt; @@ -57,7 +57,7 @@ struct ringbuf_hdr { __u32 pad; }; -static void ringbuf_unmap_ring(struct ring_buffer *rb, struct ring *r) +static void ringbuf_free_ring(struct ring_buffer *rb, struct ring *r) { if (r->consumer_pos) { munmap(r->consumer_pos, rb->page_size); @@ -67,6 +67,8 @@ static void ringbuf_unmap_ring(struct ring_buffer *rb, struct ring *r) munmap(r->producer_pos, rb->page_size + 2 * (r->mask + 1)); r->producer_pos = NULL; } + + free(r); } /* Add extra RINGBUF maps to this ring buffer manager */ @@ -107,8 +109,10 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd, return libbpf_err(-ENOMEM); rb->events = tmp; - r = &rb->rings[rb->ring_cnt]; - memset(r, 0, sizeof(*r)); + r = calloc(1, sizeof(*r)); + if (!r) + return libbpf_err(-ENOMEM); + rb->rings[rb->ring_cnt] = r; r->map_fd = map_fd; r->sample_cb = sample_cb; @@ -161,7 +165,7 @@ int ring_buffer__add(struct ring_buffer *rb, int map_fd, return 0; err_out: - ringbuf_unmap_ring(rb, r); + ringbuf_free_ring(rb, r); return libbpf_err(err); } @@ -173,7 +177,7 @@ void ring_buffer__free(struct ring_buffer *rb) return; for (i = 0; i < rb->ring_cnt; ++i) - ringbuf_unmap_ring(rb, &rb->rings[i]); + ringbuf_free_ring(rb, rb->rings[i]); if (rb->epoll_fd >= 0) close(rb->epoll_fd); @@ -281,7 +285,7 @@ int ring_buffer__consume(struct ring_buffer *rb) int i; for (i = 0; i < rb->ring_cnt; i++) { - struct ring *ring = &rb->rings[i]; + struct ring *ring = rb->rings[i]; err = ringbuf_process_ring(ring); if (err < 0) @@ -308,7 +312,7 @@ int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms) for (i = 0; i < cnt; i++) { __u32 ring_id = rb->events[i].data.fd; - struct ring *ring = &rb->rings[ring_id]; + struct ring *ring = rb->rings[ring_id]; err = ringbuf_process_ring(ring); if (err < 0) From patchwork Mon Sep 25 21:50:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398467 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8265324216 for ; Mon, 25 Sep 2023 21:52:30 +0000 (UTC) Received: from mx0a-00206402.pphosted.com (mx0a-00206402.pphosted.com [148.163.148.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1EAC124 for ; Mon, 25 Sep 2023 14:52:28 -0700 (PDT) Received: from pps.filterd (m0354651.ppops.net [127.0.0.1]) by mx0a-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PEQxeW025692; Mon, 25 Sep 2023 21:52:08 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=AfSJ6rNMJSOm7YNpC/uAinPGG72J5dg3ndCToaP6cFA=; b=rPNxX4G9zI+E e2bruCf4LfTW8Ke0lnPCHbfIuf6eRiAhG5X5eehVxdbYLqxdkMg8wg+qegG8odNY 81abnAsA0k/kRv3Mb+6N5W7NbqI9XyFNjCyU0Ua6h6D6Hq/rFyQIfWuV097z8qc+ ONAEhhw7qTj57IBgIESz8Lvsnz0yqFa3H5He/jafx3tw2HpqyKlxrqyV6IDC8atA IRMQh2nQkiJs4sWQpkZw7FwaFvCL3fin6xYtzWdRjuxXS/wWimnY/2eKnta12Lg5 Y6Bycbfdc03E4JI1nZMAoABvMwji8P+EHTym3QOyapNJWNLCTPq7cW4c8kFQ3cnw xBRPMRlvQQ== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0a-00206402.pphosted.com (PPS) with ESMTPS id 3tab8xcpjn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:08 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:06 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 03/14] libbpf: add ring_buffer__ring Date: Mon, 25 Sep 2023 14:50:34 -0700 Message-ID: <20230925215045.2375758-4-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-GUID: VM1ZWLZWHyIMWrRL-TSW_k5xNxPS4Y4a X-Proofpoint-ORIG-GUID: VM1ZWLZWHyIMWrRL-TSW_k5xNxPS4Y4a X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 mlxscore=0 spamscore=0 suspectscore=0 bulkscore=0 adultscore=0 malwarescore=0 priorityscore=1501 impostorscore=0 phishscore=0 lowpriorityscore=0 mlxlogscore=940 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add a new function ring_buffer__ring, which exposes struct ring * to the user, representing a single ringbuffer. Signed-off-by: Martin Kelly --- tools/lib/bpf/libbpf.h | 15 +++++++++++++++ tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/ringbuf.c | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 0e52621cba43..de3ef59b9641 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1229,6 +1229,7 @@ LIBBPF_API int bpf_tc_query(const struct bpf_tc_hook *hook, /* Ring buffer APIs */ struct ring_buffer; +struct ring; struct user_ring_buffer; typedef int (*ring_buffer_sample_fn)(void *ctx, void *data, size_t size); @@ -1249,6 +1250,20 @@ LIBBPF_API int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms); LIBBPF_API int ring_buffer__consume(struct ring_buffer *rb); LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb); +/** + * @brief **ring_buffer__ring()** returns the ringbuffer object inside a given + * ringbuffer manager representing a single BPF_MAP_TYPE_RINGBUF map instance. + * + * @param rb A ringbuffer manager object. + * @param idx An index into the ringbuffers contained within the ringbuffer + * manager object. The index is 0-based and corresponds to the order in which + * ring_buffer__add was called. + * @return A ringbuffer object on success; NULL and errno set if the index is + * invalid. + */ +LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb, + unsigned int idx); + struct user_ring_buffer_opts { size_t sz; /* size of this struct, for forward/backward compatibility */ }; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 57712321490f..7a7370c2bc25 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -400,4 +400,5 @@ LIBBPF_1.3.0 { bpf_program__attach_netfilter; bpf_program__attach_tcx; bpf_program__attach_uprobe_multi; + ring_buffer__ring; } LIBBPF_1.2.0; diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index 94d11fb44a49..efde453395b0 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -330,6 +330,14 @@ int ring_buffer__epoll_fd(const struct ring_buffer *rb) return rb->epoll_fd; } +struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx) +{ + if (idx >= rb->ring_cnt) + return errno = ERANGE, NULL; + + return rb->rings[idx]; +} + static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb) { if (rb->consumer_pos) { From patchwork Mon Sep 25 21:50:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398471 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F09062420F for ; Mon, 25 Sep 2023 21:52:32 +0000 (UTC) Received: from mx0b-00206402.pphosted.com (mx0b-00206402.pphosted.com [148.163.152.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06EACAF for ; Mon, 25 Sep 2023 14:52:30 -0700 (PDT) Received: from pps.filterd (m0354655.ppops.net [127.0.0.1]) by mx0b-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PE1xYp020891; Mon, 25 Sep 2023 21:52:09 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=P5GUbZOx+qmnaD17cFxjA7z9CKXPoygw9BNJj8DGy1A=; b=ZNPNq6ulLykT DmpBV0drYnshNbWHcdN02Mr272aZKHUJBtQTfBLy+jxhS0bRvGG0yuoaTsVJz20u 34/twRv7AGai6KxBizn6zjC02vptEXR2bZLr979/TBp5wE+4NlwHhN+nkF5h3jbP qNnKZlv+GcNbVYCoOcIqFQwoK0GgdeqR8P6rcNV49ut2iUi6N3CSx4kK9irpPEFm 3HlRsvNF/p1YiwqDwbKX24g6Pw9fZkHOFQME2oIf1bdQRF/PJNFdavZgEPVotOQZ rpwhjPvzu/DYiOsismMg0A2q3gR4ej9WxhaS2BCbEmm//+MplmKcwqgZCY02ISCl yIhO5YDdxw== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0b-00206402.pphosted.com (PPS) with ESMTPS id 3tac99mtfh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:09 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:07 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 04/14] selftests/bpf: add tests for ring_buffer__ring Date: Mon, 25 Sep 2023 14:50:35 -0700 Message-ID: <20230925215045.2375758-5-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-ORIG-GUID: q9gSQyKjdjeCb4VI17fKVkM7zXnpw6Se X-Proofpoint-GUID: q9gSQyKjdjeCb4VI17fKVkM7zXnpw6Se X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 impostorscore=0 adultscore=0 suspectscore=0 lowpriorityscore=0 bulkscore=0 mlxscore=0 priorityscore=1501 malwarescore=0 phishscore=0 spamscore=0 mlxlogscore=698 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add tests for the new API ring_buffer__ring. Signed-off-by: Martin Kelly --- .../selftests/bpf/prog_tests/ringbuf_multi.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c b/tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c index 1455911d9fcb..58522195081b 100644 --- a/tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c +++ b/tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c @@ -42,6 +42,8 @@ void test_ringbuf_multi(void) { struct test_ringbuf_multi *skel; struct ring_buffer *ringbuf = NULL; + struct ring *ring_old; + struct ring *ring; int err; int page_size = getpagesize(); int proto_fd = -1; @@ -84,11 +86,24 @@ void test_ringbuf_multi(void) if (CHECK(!ringbuf, "ringbuf_create", "failed to create ringbuf\n")) goto cleanup; + /* verify ring_buffer__ring returns expected results */ + ring = ring_buffer__ring(ringbuf, 0); + if (!ASSERT_OK_PTR(ring, "ring_buffer__ring_idx_0")) + goto cleanup; + ring_old = ring; + ring = ring_buffer__ring(ringbuf, 1); + ASSERT_ERR_PTR(ring, "ring_buffer__ring_idx_1"); + err = ring_buffer__add(ringbuf, bpf_map__fd(skel->maps.ringbuf2), process_sample, (void *)(long)2); if (CHECK(err, "ringbuf_add", "failed to add another ring\n")) goto cleanup; + /* verify adding a new ring didn't invalidate our older pointer */ + ring = ring_buffer__ring(ringbuf, 0); + if (!ASSERT_EQ(ring, ring_old, "ring_buffer__ring_again")) + goto cleanup; + err = test_ringbuf_multi__attach(skel); if (CHECK(err, "skel_attach", "skeleton attachment failed: %d\n", err)) goto cleanup; From patchwork Mon Sep 25 21:50:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398468 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D49E2421E for ; Mon, 25 Sep 2023 21:52:31 +0000 (UTC) Received: from mx0a-00206402.pphosted.com (mx0a-00206402.pphosted.com [148.163.148.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 315A6126 for ; Mon, 25 Sep 2023 14:52:29 -0700 (PDT) Received: from pps.filterd (m0354651.ppops.net [127.0.0.1]) by mx0a-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PFRvLr007108; Mon, 25 Sep 2023 21:52:11 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=39xGN0YT7tclBFE+BEooISMWhOCQKPLKqMXlIycZuUs=; b=uMaFzrLE/Pvu cRt0WrswyJzqGl+HSx2nAuQFlYhK2Z1XSUxttFL0vFkbkZUFpifUqLaxehJSEbNZ /y/6LLc9ZLe9t7WscbtTn9NYVJIrIxVSQaW+He7TJNj9QmsYrevgGS3V/818/3C3 DRBnGWpaiuGqtDM7iAmD/Y3HYzGCaGFX34xHWakcjwhpUjKlOXl+0jvxEQ6prXPm gKd4S9D3cc9gVorctcvZbT4rfR9/DB1QyVkSdXVzt7fOeKj4dleeIXQ9P3zjptju DXJq189wMxnOYfIBRxvlkKvE4Gee5xJfgC5EOJ+TtGJ+0uEJVO6EieU9aWQMijy/ eu7FVJ+eJg== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0a-00206402.pphosted.com (PPS) with ESMTPS id 3tab8xcpjt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:10 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:09 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 05/14] libbpf: add ring__producer_pos, ring__consumer_pos Date: Mon, 25 Sep 2023 14:50:36 -0700 Message-ID: <20230925215045.2375758-6-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-GUID: uc80B6T8sl-WOCH6jH1fwcm6nx3vOKqy X-Proofpoint-ORIG-GUID: uc80B6T8sl-WOCH6jH1fwcm6nx3vOKqy X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 mlxscore=0 spamscore=0 suspectscore=0 bulkscore=0 adultscore=0 malwarescore=0 priorityscore=1501 impostorscore=0 phishscore=0 lowpriorityscore=0 mlxlogscore=830 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add APIs to get the producer and consumer position for a given ringbuffer. Signed-off-by: Martin Kelly --- tools/lib/bpf/libbpf.h | 18 ++++++++++++++++++ tools/lib/bpf/libbpf.map | 2 ++ tools/lib/bpf/ringbuf.c | 14 ++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index de3ef59b9641..ab470179b7fe 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1264,6 +1264,24 @@ LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb); LIBBPF_API struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx); +/** + * @brief **ring__consumer_pos()** returns the current consumer position in the + * given ringbuffer. + * + * @param r A ringbuffer object. + * @return The current consumer position. + */ +LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r); + +/** + * @brief **ring__producer_pos()** returns the current producer position in the + * given ringbuffer. + * + * @param r A ringbuffer object. + * @return The current producer position. + */ +LIBBPF_API unsigned long ring__producer_pos(const struct ring *r); + struct user_ring_buffer_opts { size_t sz; /* size of this struct, for forward/backward compatibility */ }; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 7a7370c2bc25..3bec002449d5 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -400,5 +400,7 @@ LIBBPF_1.3.0 { bpf_program__attach_netfilter; bpf_program__attach_tcx; bpf_program__attach_uprobe_multi; + ring__consumer_pos; + ring__producer_pos; ring_buffer__ring; } LIBBPF_1.2.0; diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index efde453395b0..d14a607f6b66 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -338,6 +338,20 @@ struct ring *ring_buffer__ring(struct ring_buffer *rb, unsigned int idx) return rb->rings[idx]; } +unsigned long ring__consumer_pos(const struct ring *r) +{ + /* Synchronizes with smp_store_release() in ringbuf_process_ring(). */ + return smp_load_acquire(r->consumer_pos); +} + +unsigned long ring__producer_pos(const struct ring *r) +{ + /* Synchronizes with smp_store_release() in __bpf_ringbuf_reserve() in + * the kernel. + */ + return smp_load_acquire(r->producer_pos); +} + static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb) { if (rb->consumer_pos) { From patchwork Mon Sep 25 21:50:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398473 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 350102421C for ; Mon, 25 Sep 2023 21:52:31 +0000 (UTC) Received: from mx0a-00206402.pphosted.com (mx0a-00206402.pphosted.com [148.163.148.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15307116 for ; Mon, 25 Sep 2023 14:52:30 -0700 (PDT) Received: from pps.filterd (m0354651.ppops.net [127.0.0.1]) by mx0a-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PFRvLs007108; Mon, 25 Sep 2023 21:52:11 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=tIaiACjOb4wLrjbzF7OFknVvow8wjMS/QHTg2pjKZf0=; b=x2+DcDlW1Shs EE2GTlB2Onp868Chz8ZlrIBxAMVaGW7HFcmn9oD7aigJmWdZLgOb04xMjIhlgU9P JnPpO4ctizSZNZjAoXs786hCYvCKGF/aKCM6myJtived62YgyrF0yuLQlUKak1uS z1wFch57MaDp8X993CQYVBXR2VOiJCOjNzktGG1TSn6du5aBfnaHGMcuEgSuEn8o cX0WM68eiHlQa4F7sTvqSGTA6d0VbVk9uOEH0v/9mPHJCdlrM6+WDK2ciYXE4D3z uXL6ULzYz6OSPMcUO/BK+bS2t9DTNhiFHXPDgKWbiUSSjMyC0TVMCL8IJJdbRFgw ggLHTFFLKw== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0a-00206402.pphosted.com (PPS) with ESMTPS id 3tab8xcpjt-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:11 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:10 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 06/14] selftests/bpf: add tests for ring__*_pos Date: Mon, 25 Sep 2023 14:50:37 -0700 Message-ID: <20230925215045.2375758-7-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-GUID: n3cszQsS290a4hWoMD2DVYad3803kAjn X-Proofpoint-ORIG-GUID: n3cszQsS290a4hWoMD2DVYad3803kAjn X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 mlxscore=0 spamscore=0 suspectscore=0 bulkscore=0 adultscore=0 malwarescore=0 priorityscore=1501 impostorscore=0 phishscore=0 lowpriorityscore=0 mlxlogscore=961 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add tests for the new APIs ring__producer_pos and ring__consumer_pos. Signed-off-by: Martin Kelly --- tools/testing/selftests/bpf/prog_tests/ringbuf.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf.c b/tools/testing/selftests/bpf/prog_tests/ringbuf.c index ac104dc652e3..994a36a2b589 100644 --- a/tools/testing/selftests/bpf/prog_tests/ringbuf.c +++ b/tools/testing/selftests/bpf/prog_tests/ringbuf.c @@ -91,6 +91,8 @@ static void ringbuf_subtest(void) int err, cnt, rb_fd; int page_size = getpagesize(); void *mmap_ptr, *tmp_ptr; + struct ring *ring; + unsigned long cons_pos, prod_pos; skel = test_ringbuf_lskel__open(); if (CHECK(!skel, "skel_open", "skeleton open failed\n")) @@ -162,6 +164,10 @@ static void ringbuf_subtest(void) trigger_samples(); + ring = ring_buffer__ring(ringbuf, 0); + if (!ASSERT_OK_PTR(ring, "ring_buffer__ring_idx_0")) + goto cleanup; + /* 2 submitted + 1 discarded records */ CHECK(skel->bss->avail_data != 3 * rec_sz, "err_avail_size", "exp %ld, got %ld\n", @@ -176,6 +182,14 @@ static void ringbuf_subtest(void) "err_prod_pos", "exp %ld, got %ld\n", 3L * rec_sz, skel->bss->prod_pos); + /* verify getting this data directly via the ring object yields the same + * results + */ + cons_pos = ring__consumer_pos(ring); + ASSERT_EQ(cons_pos, 0, "ring_cons_pos"); + prod_pos = ring__producer_pos(ring); + ASSERT_EQ(prod_pos, 3 * rec_sz, "ring_prod_pos"); + /* poll for samples */ err = ring_buffer__poll(ringbuf, -1); From patchwork Mon Sep 25 21:50:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398472 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76D5324213 for ; Mon, 25 Sep 2023 21:52:30 +0000 (UTC) Received: from mx0b-00206402.pphosted.com (mx0b-00206402.pphosted.com [148.163.152.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F94D11C for ; Mon, 25 Sep 2023 14:52:28 -0700 (PDT) Received: from pps.filterd (m0354654.ppops.net [127.0.0.1]) by mx0b-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PEC9Wi023153; Mon, 25 Sep 2023 21:52:13 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=MC0uvYMO2FCgf65F6KKYGwogxSRr8eNxjVlVQ6tS7/M=; b=DH8emgZnpSc7 f3EbBaGUm0utnddjD+ikUHTLQLC9pKlG3Fy//SoIC0bTFz5rSwnsQw/Y61GZx1AC /B9Ar1WM0PFeFkl7xO/KfKEtVF4qGEnzBk6BxzUSmFg+f3PVkz0+1zDJ5qzPO8V/ Xs88u1kwLEhXfI/FaQKTRRDhkowQ3uTaIab/g7amMgK9Zi9YsDay1Isj0FiMqyVA mEZSqFXOs5gZ1yG3+Vv8mWHv+X+sRHEX3ulsQyIGU89+n6G9r/Q/QKmqFgylhqTH boy1rVToCFMqMZnIs7db2efE7/5X6Up2hy5ZyXh0nx4V38DuEspxMNXX0k14Tpsr WFPohzYZ9A== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0b-00206402.pphosted.com (PPS) with ESMTPS id 3taatvvybr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:13 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:11 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 07/14] libbpf: add ring__avail_data_size Date: Mon, 25 Sep 2023 14:50:38 -0700 Message-ID: <20230925215045.2375758-8-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-GUID: 8EMmCCmMKtBspnmYE-CwWG5B9roTInju X-Proofpoint-ORIG-GUID: 8EMmCCmMKtBspnmYE-CwWG5B9roTInju X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 impostorscore=0 mlxscore=0 bulkscore=0 phishscore=0 priorityscore=1501 mlxlogscore=965 adultscore=0 malwarescore=0 clxscore=1015 lowpriorityscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add ring__avail_data_size for querying the currently available data in the ringbuffer, similar to the BPF_RB_AVAIL_DATA flag in bpf_ringbuf_query. This is racy during ongoing operations but is still useful for overall information on how a ringbuffer is behaving. Signed-off-by: Martin Kelly --- tools/lib/bpf/libbpf.h | 11 +++++++++++ tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/ringbuf.c | 9 +++++++++ 3 files changed, 21 insertions(+) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index ab470179b7fe..d60254c5edc6 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1282,6 +1282,17 @@ LIBBPF_API unsigned long ring__consumer_pos(const struct ring *r); */ LIBBPF_API unsigned long ring__producer_pos(const struct ring *r); +/** + * @brief **ring__avail_data_size()** returns the number of bytes in the + * ringbuffer not yet consumed. This has no locking associated with it, so it + * can be inaccurate if operations are ongoing while this is called. However, it + * should still show the correct trend over the long-term. + * + * @param r A ringbuffer object. + * @return The number of bytes not yet consumed. + */ +LIBBPF_API size_t ring__avail_data_size(const struct ring *r); + struct user_ring_buffer_opts { size_t sz; /* size of this struct, for forward/backward compatibility */ }; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 3bec002449d5..5184c94c0502 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -400,6 +400,7 @@ LIBBPF_1.3.0 { bpf_program__attach_netfilter; bpf_program__attach_tcx; bpf_program__attach_uprobe_multi; + ring__avail_data_size; ring__consumer_pos; ring__producer_pos; ring_buffer__ring; diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index d14a607f6b66..07fbc6adcdd9 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -352,6 +352,15 @@ unsigned long ring__producer_pos(const struct ring *r) return smp_load_acquire(r->producer_pos); } +size_t ring__avail_data_size(const struct ring *r) +{ + unsigned long cons_pos, prod_pos; + + cons_pos = ring__consumer_pos(r); + prod_pos = ring__producer_pos(r); + return prod_pos - cons_pos; +} + static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb) { if (rb->consumer_pos) { From patchwork Mon Sep 25 21:50:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398462 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DF579241FA for ; Mon, 25 Sep 2023 21:52:28 +0000 (UTC) Received: from mx0a-00206402.pphosted.com (mx0a-00206402.pphosted.com [148.163.148.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3AD5112 for ; Mon, 25 Sep 2023 14:52:27 -0700 (PDT) Received: from pps.filterd (m0354652.ppops.net [127.0.0.1]) by mx0a-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PF9OQ0013015; Mon, 25 Sep 2023 21:52:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=coNKnvrGrLzhWwWTVSxnSYop38OComxV5QGmRauq1ww=; b=n7nPd6CIho4d lIxhBuimulhLj1GAiMwAkicvUyivsj+JPogjYn7Jo3cn5pDv4Sc0HporYQZYGlTf N4zsHWiz0/30bO/qrCw/WPAkXi4dPwykg4G4Nwg6nsxGoYFPS4DcqlsBWNlUoyRY QPsXxqgHbq8bjfaP7f/RvglS4oHX2+rwLlk9VA/JsjdTxfYEQGz60Y19sQ+nZjLP aiqu4m9fOLE/JQKIgSL2h2F+oS1x9/j07odRkUAYgBf+5tK+UqNzsNmS24jdakXm JW48Jp+8cgk5WVapI411EjiQNRUZme/gvVrc+aVYaVsGeVB20ZuI+Tb44MdHByL9 H2OJq0DC3g== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0a-00206402.pphosted.com (PPS) with ESMTPS id 3tadu1ce3c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:14 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:12 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 08/14] selftests/bpf: add tests for ring__avail_data_size Date: Mon, 25 Sep 2023 14:50:39 -0700 Message-ID: <20230925215045.2375758-9-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-ORIG-GUID: WKAraEtZcHiwzhKiqXPYpf8UPps206nj X-Proofpoint-GUID: WKAraEtZcHiwzhKiqXPYpf8UPps206nj X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 spamscore=0 impostorscore=0 bulkscore=0 clxscore=1015 priorityscore=1501 mlxscore=0 lowpriorityscore=0 malwarescore=0 suspectscore=0 mlxlogscore=871 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add test for the new API ring__avail_data_size. Signed-off-by: Martin Kelly --- tools/testing/selftests/bpf/prog_tests/ringbuf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf.c b/tools/testing/selftests/bpf/prog_tests/ringbuf.c index 994a36a2b589..254b25b8614c 100644 --- a/tools/testing/selftests/bpf/prog_tests/ringbuf.c +++ b/tools/testing/selftests/bpf/prog_tests/ringbuf.c @@ -92,7 +92,7 @@ static void ringbuf_subtest(void) int page_size = getpagesize(); void *mmap_ptr, *tmp_ptr; struct ring *ring; - unsigned long cons_pos, prod_pos; + unsigned long avail_data, cons_pos, prod_pos; skel = test_ringbuf_lskel__open(); if (CHECK(!skel, "skel_open", "skeleton open failed\n")) @@ -185,6 +185,8 @@ static void ringbuf_subtest(void) /* verify getting this data directly via the ring object yields the same * results */ + avail_data = ring__avail_data_size(ring); + ASSERT_EQ(avail_data, 3 * rec_sz, "ring_avail_size"); cons_pos = ring__consumer_pos(ring); ASSERT_EQ(cons_pos, 0, "ring_cons_pos"); prod_pos = ring__producer_pos(ring); From patchwork Mon Sep 25 21:50:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398466 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84B5124217 for ; Mon, 25 Sep 2023 21:52:30 +0000 (UTC) Received: from mx0b-00206402.pphosted.com (mx0b-00206402.pphosted.com [148.163.152.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8ECC1120 for ; Mon, 25 Sep 2023 14:52:28 -0700 (PDT) Received: from pps.filterd (m0354653.ppops.net [127.0.0.1]) by mx0b-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PEjCW7013860; Mon, 25 Sep 2023 21:52:15 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=G/8+UC2KpNIesKKY6ckrfrdiwxKXxs3rfIs2PjH6V8I=; b=gjD/OftGQCEm lK64eOHDS8qVLln4+jvRgnW7eHZQ5yS6XRGIg5syz1yeAnAKfvjDbfZfV1KGyzNM +M4ajfpgE1/8wrJSvt8TlATi4KvrElD/+ka7LsW45FLlj6R5E6MCSHUs8rwPDiOo hQGwtNHh6pUqwutFdH+wuaY75TLv15ie3Vdg2RscrT4eOp3Wl+lcp5XkDHHpJcCA K1LESdlxFigMmkCgylbntpgACS0dItnTLuBSNFuLTmIreL8lwFnzTEVKoHNvM57z ojMMjLAuaXZ0ZGo6sZXDrVeGG49EyI9pS31iQccXER+SQYjxUJuSR2JWux1uxxIM WVSfnZ25Qw== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0b-00206402.pphosted.com (PPS) with ESMTPS id 3taayjmyy6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:15 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:13 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 09/14] libbpf: add ring__size Date: Mon, 25 Sep 2023 14:50:40 -0700 Message-ID: <20230925215045.2375758-10-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-ORIG-GUID: bcOrXlenA69PSpF5SgoUuvHlz0YedpnY X-Proofpoint-GUID: bcOrXlenA69PSpF5SgoUuvHlz0YedpnY X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 bulkscore=0 clxscore=1015 priorityscore=1501 mlxlogscore=956 spamscore=0 malwarescore=0 suspectscore=0 phishscore=0 impostorscore=0 adultscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add ring__size to get the total size of a given ringbuffer. Signed-off-by: Martin Kelly --- tools/lib/bpf/libbpf.h | 10 ++++++++++ tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/ringbuf.c | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index d60254c5edc6..53e2a645c560 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1293,6 +1293,16 @@ LIBBPF_API unsigned long ring__producer_pos(const struct ring *r); */ LIBBPF_API size_t ring__avail_data_size(const struct ring *r); +/** + * @brief **ring__size()** returns the total size of the ringbuffer's map data + * area (excluding special producer/consumer pages). Effectively this gives the + * amount of usable bytes of data inside the ringbuffer. + * + * @param r A ringbuffer object. + * @return The total size of the ringbuffer map data area. + */ +LIBBPF_API size_t ring__size(const struct ring *r); + struct user_ring_buffer_opts { size_t sz; /* size of this struct, for forward/backward compatibility */ }; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 5184c94c0502..a116d0bb3c5d 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -403,5 +403,6 @@ LIBBPF_1.3.0 { ring__avail_data_size; ring__consumer_pos; ring__producer_pos; + ring__size; ring_buffer__ring; } LIBBPF_1.2.0; diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index 07fbc6adcdd9..98d0767dbb50 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -361,6 +361,11 @@ size_t ring__avail_data_size(const struct ring *r) return prod_pos - cons_pos; } +size_t ring__size(const struct ring *r) +{ + return r->mask + 1; +} + static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb) { if (rb->consumer_pos) { From patchwork Mon Sep 25 21:50:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398464 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 209CC2420D for ; Mon, 25 Sep 2023 21:52:29 +0000 (UTC) Received: from mx0a-00206402.pphosted.com (mx0a-00206402.pphosted.com [148.163.148.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F013121 for ; Mon, 25 Sep 2023 14:52:28 -0700 (PDT) Received: from pps.filterd (m0354650.ppops.net [127.0.0.1]) by mx0a-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PJcluV030865; Mon, 25 Sep 2023 21:52:16 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=a+rXgYO4GK65zO6ggrR0jbICTPYtZIKKvieFi1uaE34=; b=QkjnMiDdwdT0 w9l2/Nh1zGsZjcaMr3bhjfkQ0QdkRxEWmtW0iT9V/Blnn1rsrOplfBwGe/k91pvI 28i4VMMTaZzKURO9Ck/2ec0i4AmIHkxRPCtJP4uAkgTOBswSMkw8BdR68Q3bQUa5 SXluolFrC/084T3M4zixyX29XOJ9NwMX30kWW7TG5eS7dxyfXV57k4CdsV0QJFEI mxxVg+AmEFevtWqRJjVoLhAlLTZuj1H23U6jKgzC2mc0ErlVy0iJhr7zR0tTayki kkvc+E522x37D889eNvC5nJgoNSgSbqeFtUQBu4I9HGphRhPyRiaOoJ8Pcbxfs0U feZQCvKUtA== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0a-00206402.pphosted.com (PPS) with ESMTPS id 3tb9ae9ngr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:16 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:14 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 10/14] selftests/bpf: add tests for ring__size Date: Mon, 25 Sep 2023 14:50:41 -0700 Message-ID: <20230925215045.2375758-11-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-ORIG-GUID: Lxyxr6MzenBjiSfWbBWpWsb_4jsPTC3Z X-Proofpoint-GUID: Lxyxr6MzenBjiSfWbBWpWsb_4jsPTC3Z X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=823 adultscore=0 priorityscore=1501 suspectscore=0 bulkscore=0 impostorscore=0 malwarescore=0 phishscore=0 mlxscore=0 clxscore=1015 spamscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add tests for the new API ring__size. Signed-off-by: Martin Kelly --- tools/testing/selftests/bpf/prog_tests/ringbuf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf.c b/tools/testing/selftests/bpf/prog_tests/ringbuf.c index 254b25b8614c..c5be480a6ef6 100644 --- a/tools/testing/selftests/bpf/prog_tests/ringbuf.c +++ b/tools/testing/selftests/bpf/prog_tests/ringbuf.c @@ -92,7 +92,7 @@ static void ringbuf_subtest(void) int page_size = getpagesize(); void *mmap_ptr, *tmp_ptr; struct ring *ring; - unsigned long avail_data, cons_pos, prod_pos; + unsigned long avail_data, ring_size, cons_pos, prod_pos; skel = test_ringbuf_lskel__open(); if (CHECK(!skel, "skel_open", "skeleton open failed\n")) @@ -187,6 +187,8 @@ static void ringbuf_subtest(void) */ avail_data = ring__avail_data_size(ring); ASSERT_EQ(avail_data, 3 * rec_sz, "ring_avail_size"); + ring_size = ring__size(ring); + ASSERT_EQ(ring_size, page_size, "ring_ring_size"); cons_pos = ring__consumer_pos(ring); ASSERT_EQ(cons_pos, 0, "ring_cons_pos"); prod_pos = ring__producer_pos(ring); From patchwork Mon Sep 25 21:50:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398470 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 811C5250E3 for ; Mon, 25 Sep 2023 21:52:32 +0000 (UTC) Received: from mx0b-00206402.pphosted.com (mx0b-00206402.pphosted.com [148.163.152.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C45C112 for ; Mon, 25 Sep 2023 14:52:30 -0700 (PDT) Received: from pps.filterd (m0354654.ppops.net [127.0.0.1]) by mx0b-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PEC9Wj023153; Mon, 25 Sep 2023 21:52:17 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=DekCw+Oeo9KDW9kCjnfpOUz6j1k/JxNmOvdZTxQuiMQ=; b=AWSNUSLauJEi woZmEz6dkAvNlwyrrNuDSvnfcH0I3GMQmR41yhs6PZwfDNAR8w5FkuPzWAYZYoii OPDFGg7KBuQbc7LzxC5RltyDRxksESmBcJavrGTswtg0ugMiESqUsA9kQh31mX5O wXYDvhHpaeTREWaOP0WH4g6uMjtVys3BFwLsmIphGivfbf81Xp8E7duQgw8+59b+ Cbe8W9U6KtIedgWi3I0k0HNngjsRjIcwXc8snGHbbc2L1nHOsWAOXnZ2DDuroZIt gaOro00CZ/Y4yUqhMuTWBDDk8AMqS/5lQJ1wBlJE77+c/60/Y5KeWrWoyrJTko30 zhRSlh9yLA== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0b-00206402.pphosted.com (PPS) with ESMTPS id 3taatvvybx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:17 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:15 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 11/14] libbpf: add ring__map_fd Date: Mon, 25 Sep 2023 14:50:42 -0700 Message-ID: <20230925215045.2375758-12-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-GUID: NrOmeAaS2k9nDNv-gd9sANbACGEMUk4U X-Proofpoint-ORIG-GUID: NrOmeAaS2k9nDNv-gd9sANbACGEMUk4U X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 impostorscore=0 mlxscore=0 bulkscore=0 phishscore=0 priorityscore=1501 mlxlogscore=895 adultscore=0 malwarescore=0 clxscore=1015 lowpriorityscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add ring__map_fd to get the file descriptor underlying a given ringbuffer. Signed-off-by: Martin Kelly --- tools/lib/bpf/libbpf.h | 9 +++++++++ tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/ringbuf.c | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 53e2a645c560..114e306c6507 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1303,6 +1303,15 @@ LIBBPF_API size_t ring__avail_data_size(const struct ring *r); */ LIBBPF_API size_t ring__size(const struct ring *r); +/** + * @brief **ring__map_fd()** returns the file descriptor underlying the given + * ringbuffer. + * + * @param r A ringbuffer object. + * @return The underlying ringbuffer file descriptor + */ +LIBBPF_API int ring__map_fd(const struct ring *r); + struct user_ring_buffer_opts { size_t sz; /* size of this struct, for forward/backward compatibility */ }; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index a116d0bb3c5d..1b4225327ab6 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -402,6 +402,7 @@ LIBBPF_1.3.0 { bpf_program__attach_uprobe_multi; ring__avail_data_size; ring__consumer_pos; + ring__map_fd; ring__producer_pos; ring__size; ring_buffer__ring; diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index 98d0767dbb50..8aec20216b7b 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -366,6 +366,11 @@ size_t ring__size(const struct ring *r) return r->mask + 1; } +int ring__map_fd(const struct ring *r) +{ + return r->map_fd; +} + static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb) { if (rb->consumer_pos) { From patchwork Mon Sep 25 21:50:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398469 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FDC0250E2 for ; Mon, 25 Sep 2023 21:52:32 +0000 (UTC) Received: from mx0a-00206402.pphosted.com (mx0a-00206402.pphosted.com [148.163.148.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE91A12A for ; Mon, 25 Sep 2023 14:52:30 -0700 (PDT) Received: from pps.filterd (m0354650.ppops.net [127.0.0.1]) by mx0a-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PJFxSJ030102; Mon, 25 Sep 2023 21:52:19 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=6yVD38foJioR1q1/KQa/mnHLBQN7O89reLG1wFD/esQ=; b=OTH4IyY7NNUz c5GFQuw/VGF2G6nzvZO4yqBOao35fPho/7f5D8RQ/qcKoh8H6H/V4rxy9Jq/h0Ie lRXFVhcma81cyIYg5TgBzXGMvAJMS0zDR2St9zFEjFvI38m584HbE5VOZfD3/6Ye 86E1HZ9BTAYDihXAFE2/Mcv4MmDcvj9+7VZjZh/WinyoRBL7PAmB+Z7b1JB6cnub 7TTsamNikEfS0DR82243Q/dLVNEOGbAirex9+5PVONyym6bPaBGtnS7SoJmyUlXm lqSyq72mewS5sNKzXeLmesP/PzOxijcGFiRoTjUli/Sbh51zI6Qc9hYPK1ewQ4WF u5qxZJT3eQ== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0a-00206402.pphosted.com (PPS) with ESMTPS id 3tb9ae9ngs-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:18 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:16 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 12/14] selftests/bpf: add tests for ring__map_fd Date: Mon, 25 Sep 2023 14:50:43 -0700 Message-ID: <20230925215045.2375758-13-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-ORIG-GUID: 2J6lNs2w_7pbwEVFbtq9NpWMjUUOX5bR X-Proofpoint-GUID: 2J6lNs2w_7pbwEVFbtq9NpWMjUUOX5bR X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 adultscore=0 priorityscore=1501 suspectscore=0 bulkscore=0 impostorscore=0 malwarescore=0 phishscore=0 mlxscore=0 clxscore=1015 spamscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add tests for the new API ring__map_fd. Signed-off-by: Martin Kelly --- tools/testing/selftests/bpf/prog_tests/ringbuf.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf.c b/tools/testing/selftests/bpf/prog_tests/ringbuf.c index c5be480a6ef6..c23f6c54b373 100644 --- a/tools/testing/selftests/bpf/prog_tests/ringbuf.c +++ b/tools/testing/selftests/bpf/prog_tests/ringbuf.c @@ -92,6 +92,7 @@ static void ringbuf_subtest(void) int page_size = getpagesize(); void *mmap_ptr, *tmp_ptr; struct ring *ring; + int map_fd; unsigned long avail_data, ring_size, cons_pos, prod_pos; skel = test_ringbuf_lskel__open(); @@ -168,6 +169,9 @@ static void ringbuf_subtest(void) if (!ASSERT_OK_PTR(ring, "ring_buffer__ring_idx_0")) goto cleanup; + map_fd = ring__map_fd(ring); + ASSERT_EQ(map_fd, skel->maps.ringbuf.map_fd, "ring_map_fd"); + /* 2 submitted + 1 discarded records */ CHECK(skel->bss->avail_data != 3 * rec_sz, "err_avail_size", "exp %ld, got %ld\n", From patchwork Mon Sep 25 21:50:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398475 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9C3F250E9 for ; Mon, 25 Sep 2023 21:52:32 +0000 (UTC) Received: from mx0a-00206402.pphosted.com (mx0a-00206402.pphosted.com [148.163.148.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DD31136 for ; Mon, 25 Sep 2023 14:52:31 -0700 (PDT) Received: from pps.filterd (m0354650.ppops.net [127.0.0.1]) by mx0a-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PJFxSK030102; Mon, 25 Sep 2023 21:52:19 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=MojYFuQZ3STTWPBmwog9T1+O4UwDNT8jEysIWkPA6rE=; b=fUCvvoRxpn2a GPqP1t/QaBYnKQ1TS3LasWRIDHbER6dxm4540Rmmxn19oq9s7IQZNGeRoi5tLuYt 7gThNmCusxsFxk6UCL9pVZZKPhHpHdGOrNcFzzcOsqzBKuPEWIzMH7GwViOwpNAD p7IJZNv3i+5iDdLcpWm8pOqcIgp9ZSSaD+YzlJLz9T6LMnb0wkdNsA53CDMyUCed UAjrjgw80sEIDtt38AVey++8vUpQczJE03nNEEqcn07JISKgRYtCYGucylgSvOf1 eJFBNrRbH0UR0H+P5U9NovpB4TcZ7iL8UmeBZU8fCLVQ/R5N5Lm502rcaMFMSLUi gqPHRTLwrw== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0a-00206402.pphosted.com (PPS) with ESMTPS id 3tb9ae9ngs-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:19 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:17 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 13/14] libbpf: add ring__consume Date: Mon, 25 Sep 2023 14:50:44 -0700 Message-ID: <20230925215045.2375758-14-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-ORIG-GUID: MS4bQb90NrH5h2hAwOv7uUDyjyESTDsB X-Proofpoint-GUID: MS4bQb90NrH5h2hAwOv7uUDyjyESTDsB X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=795 adultscore=0 priorityscore=1501 suspectscore=0 bulkscore=0 impostorscore=0 malwarescore=0 phishscore=0 mlxscore=0 clxscore=1015 spamscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add ring__consume to consume a single ringbuffer, analogous to ring_buffer__consume. Signed-off-by: Martin Kelly --- tools/lib/bpf/libbpf.h | 10 ++++++++++ tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/ringbuf.c | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 114e306c6507..475378438545 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1312,6 +1312,16 @@ LIBBPF_API size_t ring__size(const struct ring *r); */ LIBBPF_API int ring__map_fd(const struct ring *r); +/** + * @brief **ring__consume()** consumes available ringbuffer data without event + * polling. + * + * @param r A ringbuffer object. + * @return The number of records consumed (or INT_MAX, whichever is less), or + * a negative number if any of the callbacks return an error. + */ +LIBBPF_API int ring__consume(struct ring *r); + struct user_ring_buffer_opts { size_t sz; /* size of this struct, for forward/backward compatibility */ }; diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 1b4225327ab6..cc973b678a39 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -401,6 +401,7 @@ LIBBPF_1.3.0 { bpf_program__attach_tcx; bpf_program__attach_uprobe_multi; ring__avail_data_size; + ring__consume; ring__consumer_pos; ring__map_fd; ring__producer_pos; diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index 8aec20216b7b..aacb64278a01 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -371,6 +371,17 @@ int ring__map_fd(const struct ring *r) return r->map_fd; } +int ring__consume(struct ring *r) +{ + int64_t res; + + res = ringbuf_process_ring(r); + if (res < 0) + return libbpf_err(res); + + return res > INT_MAX ? INT_MAX : res; +} + static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb) { if (rb->consumer_pos) { From patchwork Mon Sep 25 21:50:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Kelly X-Patchwork-Id: 13398474 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 66405250FC for ; Mon, 25 Sep 2023 21:52:34 +0000 (UTC) Received: from mx0b-00206402.pphosted.com (mx0b-00206402.pphosted.com [148.163.152.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CAAA11F for ; Mon, 25 Sep 2023 14:52:33 -0700 (PDT) Received: from pps.filterd (m0354653.ppops.net [127.0.0.1]) by mx0b-00206402.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 38PEER51001812; Mon, 25 Sep 2023 21:52:20 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crowdstrike.com; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s=default; bh=bDw2Pl2awiN60nMJK0eNIWeQ3BOshAh1vlLDBof1OTk=; b=tMW3cE6qAbPA xdAPq99OzHXsY2sWPM0XVhsqrlZItXE3vK8ygx19lGM0w+CzifHCsot8YQTRYHlG bFvEBoJi6lEXZnyOVDWgYsDgcSpgQ3Y/+NaoVfzvh2eD6/hrigaEc0BvdCXM8Kuy k0/Zh/X7qKtMD2nUDWW4ttw3iEJudXvTq/nRDapIhZe0VHtdNu6l3iblv5pRsss7 qVSDP3ck7J+rFDrTqohUTd2ksRpJ8Z5SN1oaFmxDtJJOFgHD+qmF89ULihpTb/2r yavE8/q3JsP41ICAWyosDZzJSxPhTetDwfZBgRKZ85fd2DghfSyyK3KYob6c5jw3 d/NL5/McZQ== Received: from 04wpexch06.crowdstrike.sys (dragosx.crowdstrike.com [208.42.231.60]) by mx0b-00206402.pphosted.com (PPS) with ESMTPS id 3taayjmyy9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 25 Sep 2023 21:52:20 +0000 (GMT) Received: from LL-556NGK3.crowdstrike.sys (10.100.11.122) by 04wpexch06.crowdstrike.sys (10.100.11.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.25; Mon, 25 Sep 2023 21:52:18 +0000 From: Martin Kelly To: CC: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Martin Kelly Subject: [PATCH bpf-next v2 14/14] selftests/bpf: add tests for ring__consume Date: Mon, 25 Sep 2023 14:50:45 -0700 Message-ID: <20230925215045.2375758-15-martin.kelly@crowdstrike.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> References: <20230925215045.2375758-1-martin.kelly@crowdstrike.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Originating-IP: [10.100.11.122] X-ClientProxiedBy: 04WPEXCH12.crowdstrike.sys (10.100.11.116) To 04wpexch06.crowdstrike.sys (10.100.11.99) X-Disclaimer: USA X-Proofpoint-ORIG-GUID: M7-iYYctAQngJH85ccRidHlNNazM4TrA X-Proofpoint-GUID: M7-iYYctAQngJH85ccRidHlNNazM4TrA X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-25_18,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 bulkscore=0 clxscore=1015 priorityscore=1501 mlxlogscore=843 spamscore=0 malwarescore=0 suspectscore=0 phishscore=0 impostorscore=0 adultscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2309180000 definitions=main-2309250168 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net Add tests for new API ring__consume. Signed-off-by: Martin Kelly --- tools/testing/selftests/bpf/prog_tests/ringbuf.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf.c b/tools/testing/selftests/bpf/prog_tests/ringbuf.c index c23f6c54b373..48c5695b7abf 100644 --- a/tools/testing/selftests/bpf/prog_tests/ringbuf.c +++ b/tools/testing/selftests/bpf/prog_tests/ringbuf.c @@ -304,6 +304,10 @@ static void ringbuf_subtest(void) err = ring_buffer__consume(ringbuf); CHECK(err < 0, "rb_consume", "failed: %d\b", err); + /* also consume using ring__consume to make sure it works the same */ + err = ring__consume(ring); + ASSERT_GE(err, 0, "ring_consume"); + /* 3 rounds, 2 samples each */ cnt = atomic_xchg(&sample_cnt, 0); CHECK(cnt != 6, "cnt", "exp %d samples, got %d\n", 6, cnt);