From patchwork Sat Apr 6 09:15:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Righi X-Patchwork-Id: 13619707 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) (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 EE2AF3B18D for ; Sat, 6 Apr 2024 09:20:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395234; cv=none; b=ppM56kIpTd2TjX+a0izuJpsq6EiBfU6WAeVn3vJpkHOt2qH8PgNBF0TmNfpj8zlqge3qsB+HFQuNpux/dsZk7CtuhjWN8DNDdFo6UbtKPpC7+vzz1whNMdI8m10QiXc/PMv7BlXzTk1q08mNOPXqd/2BT63IvwPq76pM6SGaSvE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395234; c=relaxed/simple; bh=yt9tmTIBJzVdCFZ4PnU57SVH6MzeinR1L4YoDUXNjsE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nT7Mv7/hI6GgNfbyreP0JQ+JlpSyr7GgMd+UNqsK4Qs6hwXnyPZUCBUodYCRrrb97/9zAuXuUTHksUizGFWjPr2UOazDm7VuOiO47MlbyffOVGBbQek+zvZky3cAPVjnSnsLvfKfhVUE8+bxnbEZ0escWzhpie8ZoqSs4VCq6oI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=J+Zuk4Yy; arc=none smtp.client-ip=185.125.188.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="J+Zuk4Yy" Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id C2ABE3FE51 for ; Sat, 6 Apr 2024 09:20:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1712395219; bh=G6LgQlJD57yHhfXydvKzs8A9bUpRZ0ooDJmyE1zvH0s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J+Zuk4Yy5gSmsqWfBTQzGNBP9MKi6Fwugsk9M6+BxHKQUK9x+2BaGbHHF5Pv/TXU9 6hiXNOBU1pB4eVHo3/2LTp9ZzkRDHhpo399gL/7dnjdPG0seXW3H9bO9fsb5xp85WE 7fgn3DKNu5xu8WxZLSzLwNO/F4sDp0yzQeDV53xD+o9Vgb9d7mznvTQtwuE0AhGke3 cLP4QPjDo9TtPVRJbc70oKmmrZMQYLuVFw38rnzNayNYozuXsgax/ZFQ/OyZR/41Ah HxYyZjIIlER5X4AXDBca/2q7ZdB27fMSW25Uc4cesNYDLTES3hL8PvmrAr755qFwFC VTiVlVJJl7ymg== Received: by mail-lj1-f197.google.com with SMTP id 38308e7fff4ca-2d6c94d98afso26915181fa.3 for ; Sat, 06 Apr 2024 02:20:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712395214; x=1713000014; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=G6LgQlJD57yHhfXydvKzs8A9bUpRZ0ooDJmyE1zvH0s=; b=Jn2e9/nNMOEGWah2PMuS4q5cYIfznsAjR3COZeEpYvBNhdUzVBmUPE83QCAGui4HgP luqVkIq+rWwQI8eQJuUoAyEM5UNlGb6BbbC6HUlRmQzavozU9u9PJ0PFte8wMQYLpBFM 3rRuERcm9Wu4K3QZlLIyApbFprtvpowme1txq/4xi2mfEYMv3FMjfi1i68dx9krV1TaW XtY6V0E/TrIhDjCrD3PB1/k3Bwhd0W1yIE87ggKVlJW0zRaW/nl0CinQCHdd98LWuFzV NqaRsxKBazlEIcLfXPcbmizkKXBTIpVfcVxhNY6lEdUiUBjiCf/k36B5v+NIGlK/Ps/A u0KQ== X-Forwarded-Encrypted: i=1; AJvYcCWnq9afBisiuxVGsyEG0K0K8EcZPFdwmq0eiXapHJfE1BOhrv/IhAk3VJ9uD04WcCk85p8llU67ETP8/+NfJ6O8ThWb X-Gm-Message-State: AOJu0YxWfdY7Eh39zxHF5WpcTNnJYKKSx3e+gQt7v0tBVOSiuyYjAtsg 74oCpdrmyTs6mPrJtHSqpkqynqkJNX8Woh9CGaSiyFTVbHaFCcOqWd1RqQt4zipYpf3m1EZnUIT 6ApHDRm7HbKgw6+rQAf/15pXMiTIz4kwnuZWnaqLfgru5FInZixkPl+yb0ez5IhYCUQ== X-Received: by 2002:a2e:98d9:0:b0:2d6:d351:78ae with SMTP id s25-20020a2e98d9000000b002d6d35178aemr3079649ljj.29.1712395214340; Sat, 06 Apr 2024 02:20:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEC5CR3YqSgu+J4/e2RoJd1lpNEBaMZEgCrY3TxjTsm1+4G8Z80bdHexcZXeNzlB0Tjro1Qiw== X-Received: by 2002:a2e:98d9:0:b0:2d6:d351:78ae with SMTP id s25-20020a2e98d9000000b002d6d35178aemr3079628ljj.29.1712395213639; Sat, 06 Apr 2024 02:20:13 -0700 (PDT) Received: from gpd.station (net-2-39-142-110.cust.vodafonedsl.it. [2.39.142.110]) by smtp.gmail.com with ESMTPSA id l23-20020a7bc457000000b00414924f307csm5779993wmi.26.2024.04.06.02.20.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Apr 2024 02:20:13 -0700 (PDT) From: Andrea Righi To: Andrii Nakryiko , Eduard Zingerman Cc: Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , David Vernet , Tejun Heo , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/4] libbpf: Start v1.5 development cycle Date: Sat, 6 Apr 2024 11:15:41 +0200 Message-ID: <20240406092005.92399-2-andrea.righi@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240406092005.92399-1-andrea.righi@canonical.com> References: <20240406092005.92399-1-andrea.righi@canonical.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Bump libbpf.map to v1.5.0 to start a new libbpf version cycle. Signed-off-by: Andrea Righi --- tools/lib/bpf/libbpf.map | 3 +++ tools/lib/bpf/libbpf_version.h | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 51732ecb1385..5dd81a7b96b5 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -416,3 +416,6 @@ LIBBPF_1.4.0 { btf__new_split; btf_ext__raw_data; } LIBBPF_1.3.0; + +LIBBPF_1.5.0 { +} LIBBPF_1.4.0; diff --git a/tools/lib/bpf/libbpf_version.h b/tools/lib/bpf/libbpf_version.h index e783a47da815..d6e5eff967cb 100644 --- a/tools/lib/bpf/libbpf_version.h +++ b/tools/lib/bpf/libbpf_version.h @@ -4,6 +4,6 @@ #define __LIBBPF_VERSION_H #define LIBBPF_MAJOR_VERSION 1 -#define LIBBPF_MINOR_VERSION 4 +#define LIBBPF_MINOR_VERSION 5 #endif /* __LIBBPF_VERSION_H */ From patchwork Sat Apr 6 09:15:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Righi X-Patchwork-Id: 13619704 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) (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 539BF3B2A2 for ; Sat, 6 Apr 2024 09:20:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.122 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395223; cv=none; b=hZwo/7v6LR56t5s6nc+xmU0QaHK5U4qYY/ZfdDpDgHLici3UbErAmWsFFcV9qK1UqqTyjZK6JYvXREv5L0gUD2JKtbvoBttMCepB4wkDUn0KEkd33Ec7x0vC8XTm4scJItlrtQcBuVI2osOhT4c8AnuwFO54LRy6+Nb9bpM8c4E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395223; c=relaxed/simple; bh=a78vMWglkqZNxVpizm3bz2mABJk4IrXHntVwOv9hkR0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uOf+ynDH40QtDS1r1nmWMUl9/rKZN3D7vG5IITKAEZT1Ag2ztHNYdVPNPZrU+v3kH3bvfda204r+JNxCRWZigdTMshtNOGPeBud1DWUn9SFJacZdwDVuqmtxpRw2hLs7CyifZ3tsrwP5YuF06TTNq0JfXRNlQQifRguyxep+/Yg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=vmsuGGo2; arc=none smtp.client-ip=185.125.188.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="vmsuGGo2" Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id DF6EC424A4 for ; Sat, 6 Apr 2024 09:20:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1712395217; bh=U9JiGxHu1Zu7lOUTn6nQmFFrRqhFXaTmBwUIearvaJY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vmsuGGo24oaZbKZFIXkYa+05DycEitw/wz+rIRGEtHAWz87kgV3BH0MMHcZK9e9F3 E3XVfTxcX8aSUK/bXlJ4sQozTXugcN4TEv5vwegJ16njQ9OlX9JTIeFHVazRRwNbIz MV5T0qlB8Qr6TDX6P0of5FteLf+mJdyUr4MIn526exZyEMVmVM4Jzs3J6FQppFhLlB i1HBPLh9EpzmXtOka7Hmi1OivoYYn8tr1qArtId03O6wuHmZsVb903qljbkSwSITgo 2q4mQK03TDHzSww2tJcTllLjcx5kmol0aUnkm5u3O8EtU6tkSmP7pcCBFCQENAk86i DDu4C/LjPAfJA== Received: by mail-lf1-f70.google.com with SMTP id 2adb3069b0e04-516be44ea1dso2472119e87.1 for ; Sat, 06 Apr 2024 02:20:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712395216; x=1713000016; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=U9JiGxHu1Zu7lOUTn6nQmFFrRqhFXaTmBwUIearvaJY=; b=GdnrDn5n/aawccRWT31UlZEIb79UoFa8p7mb9wWhZ+g7ragMsefEXruFHIxb/ma2G2 XE7x/n96u2n2vgIMBSb7NwE+YgNcCUMZOhnKTu5wZRg9Zfg5dsR8xTabvqTr9fx26ZCX 9v0Q4hhZ+H2Pn+CUhZCsS/116bJZM0ygzBRWSBY8t4Tc5dZMBHM3bqHF9AiPk1I1k3GJ /hQtg1ZZd+nBJclADnycv0qCtUNcSPIoTfZM49Q93wpZ+Dg4S6phn192hW3EtIBvyCXn i6Ittk10CN6QB0pOqxj7ko9SjhgLYfUhO2eAph6M904iYoa86DzRzFI+18Ao7uKsKe6C tnPw== X-Forwarded-Encrypted: i=1; AJvYcCWhl9J+WyqHjKjnkGblzDjhHr6i/6eS3LJVz0Pf/dPMchQjUZUBEiVVcM9gnRaG96Chuc7YAWQURymiLyhRRbVfHO1k X-Gm-Message-State: AOJu0Yw5lWin3U6FX/7r9N6B7Lbl6Hnd/+YPaoormMTsPPtJX6MNX/4V oURVwB7GEWpbmByZBL4m+vWa1LLBLZUWb8tT8ZOrZeZr4OEPZDAO0tbj5Ijq/qGB3tYs4Pj2Z+W woy5AiArMTTdsF6EjM00tyK416EyHj27TVgPkEn+Kc0/hM0qxBGlvzAETVvFkGf2FuQ== X-Received: by 2002:ac2:5df9:0:b0:515:d50f:c6cc with SMTP id z25-20020ac25df9000000b00515d50fc6ccmr2674255lfq.63.1712395215921; Sat, 06 Apr 2024 02:20:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHS/Wj4fTtj4EhH4SwuK4rc7frejoX2SMVVjV8lwzSRp5JOOCrfUKz3Ui9mytME96ZdhQmWHg== X-Received: by 2002:ac2:5df9:0:b0:515:d50f:c6cc with SMTP id z25-20020ac25df9000000b00515d50fc6ccmr2674231lfq.63.1712395215460; Sat, 06 Apr 2024 02:20:15 -0700 (PDT) Received: from gpd.station (net-2-39-142-110.cust.vodafonedsl.it. [2.39.142.110]) by smtp.gmail.com with ESMTPSA id l23-20020a7bc457000000b00414924f307csm5779993wmi.26.2024.04.06.02.20.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Apr 2024 02:20:14 -0700 (PDT) From: Andrea Righi To: Andrii Nakryiko , Eduard Zingerman Cc: Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , David Vernet , Tejun Heo , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/4] libbpf: ringbuf: allow to consume up to a certain amount of items Date: Sat, 6 Apr 2024 11:15:42 +0200 Message-ID: <20240406092005.92399-3-andrea.righi@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240406092005.92399-1-andrea.righi@canonical.com> References: <20240406092005.92399-1-andrea.righi@canonical.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net In some cases, instead of always consuming all items from ring buffers in a greedy way, we may want to consume up to a certain amount of items, for example when we need to copy items from the BPF ring buffer to a limited user buffer. This change allows to set an upper limit to the amount of items consumed from one or more ring buffers. Signed-off-by: Andrea Righi --- tools/lib/bpf/ringbuf.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index aacb64278a01..2c4031168413 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -231,7 +231,7 @@ static inline int roundup_len(__u32 len) return (len + 7) / 8 * 8; } -static int64_t ringbuf_process_ring(struct ring *r) +static int64_t ringbuf_process_ring(struct ring *r, size_t n) { int *len_ptr, len, err; /* 64-bit to avoid overflow in case of extreme application behavior */ @@ -268,6 +268,9 @@ static int64_t ringbuf_process_ring(struct ring *r) } smp_store_release(r->consumer_pos, cons_pos); + + if (cnt >= n) + goto done; } } while (got_new_data); done: @@ -287,13 +290,15 @@ int ring_buffer__consume(struct ring_buffer *rb) for (i = 0; i < rb->ring_cnt; i++) { struct ring *ring = rb->rings[i]; - err = ringbuf_process_ring(ring); + err = ringbuf_process_ring(ring, INT_MAX); if (err < 0) return libbpf_err(err); res += err; + if (res > INT_MAX) { + res = INT_MAX; + break; + } } - if (res > INT_MAX) - return INT_MAX; return res; } @@ -314,13 +319,15 @@ int ring_buffer__poll(struct ring_buffer *rb, int timeout_ms) __u32 ring_id = rb->events[i].data.fd; struct ring *ring = rb->rings[ring_id]; - err = ringbuf_process_ring(ring); + err = ringbuf_process_ring(ring, INT_MAX); if (err < 0) return libbpf_err(err); res += err; + if (res > INT_MAX) { + res = INT_MAX; + break; + } } - if (res > INT_MAX) - return INT_MAX; return res; } @@ -375,7 +382,7 @@ int ring__consume(struct ring *r) { int64_t res; - res = ringbuf_process_ring(r); + res = ringbuf_process_ring(r, INT_MAX); if (res < 0) return libbpf_err(res); From patchwork Sat Apr 6 09:15:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Righi X-Patchwork-Id: 13619708 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) (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 2C73F3FBA2 for ; Sat, 6 Apr 2024 09:20:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395238; cv=none; b=nGgMTJkrUlHY6ge3AuWet/yU1xQHobiFxRJlABe+o5FdobU0M4bLtWjgsxRMku7gO2psbUJmKSjWtI1Tuz1O6FAMpn8yNbVqY9oq/rtUGx1sbPxOcX56Lh8CprycOAFqFEdSba105Mfmi8LXM4LJa2Ua+gwxh3eVuSjQuEx03cE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395238; c=relaxed/simple; bh=WxyORd5UGrvrhH3ovdgj29XhigFycIY9JFgRu0PCY/A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=euU0/XDUC154CLEZ1NCKG6FZbHQn/nT/DagKp6vnx0UCJ9xwW+gzBlxSnA9rfiAdead5XWWesyF630VcbyQPUUFkm7BKMy8x2a4UNSS1/hWcFnF4NVIjA5haKeszhlYslZMpU+V/EaMm3Y2WFfINC66+s6XehiKnybO7CqNymDg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=FKZhXKdn; arc=none smtp.client-ip=185.125.188.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="FKZhXKdn" Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 010AE3FE65 for ; Sat, 6 Apr 2024 09:20:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1712395224; bh=OGumsLBWYsNiBqaQkxgfB4Yj3V+oiBGI9bnDO7L7IE8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FKZhXKdnqeowQv0waOcRef6TIIWf8DZwwosHupqS/iFa0vVTAGbEHCWCBtimSKvST LBPjTWRoyQY2QzJTLSn7yJBpJN3DhtxLk55ZGLwkJwyReZyeTNp75LjFMcbtX/GX9t yR6GkLOcv/IzneyAGtsEj0QERz1OqLqGNiHTRjwi7ie+H+Kl5hk2bNc8bz3YQ1dlzt kYC7fTsAtQ9c4/gm5G3506V0n0Qmw+ecMFxCUsIzAqlqi2xdshQ4fhh9GU9cC6TmTk ZBRnppVTi/dXkPvzH6/4fgvKYmADevchJc3XjNlbkpqQyl28pNA4voUUP+arFiZ/mA yTm7FRUygKDkQ== Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-41552c04845so14445345e9.2 for ; Sat, 06 Apr 2024 02:20:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712395218; x=1713000018; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OGumsLBWYsNiBqaQkxgfB4Yj3V+oiBGI9bnDO7L7IE8=; b=p/UwflbgnBrnTiAgn2QIkcWGm3f9CeGWd9rLUG+pOjI+RF24jIGeKDLdCI8QrFFBFs LGHixST3O+9B26jJ6RS0+fcipPbaU1aKu/kwqYHIz0+Q7xUEAI1A9VM8WLp1UCRtTCMf /IoMnAa8tY4VXipN82+unGYkVmabpyib/qiQXX5KR6kVSlCc3wDe0R+8Gh2DRFopKVCB /ZqCAfZmHuS08YC3PPPTjRFTaJpb3H7z2X1pi+PiII9doXXEuaGPcQQRr7QYdY8Aoney 8u3w18uewUejJr4m/obPXyKVbu9iPx03arbAJA4rFz5fanUOednKzN0ECcnJ3e85yo4D I3tw== X-Forwarded-Encrypted: i=1; AJvYcCX9M9Y63n+i34ia5kQEvB0iR0D5bXs0/inXRNfY5ZJEKmURMxhDnMJ4U5R60SKHdIEgXcfELGBJ1tBNmEjQ/WhcT1Vj X-Gm-Message-State: AOJu0YyGlvdfxtQRKAtWkdgQujWDC6yzuC83FK1uVrrweaTVp1fDNWKm 6rVFydxXKw68fBS2lQritlGFavudJdpJ8KINDnmHPKzpybNB/C+mt1vf8NDysca0fVUzAcrH44s w6fTxszPDXmFSS2+Ts1pi8Ou1ajqbAF6MaMH+ALBgtDZojUo4ZxWcICZ84cjaEeowQA== X-Received: by 2002:a05:600c:4e8c:b0:415:6728:a584 with SMTP id f12-20020a05600c4e8c00b004156728a584mr2494544wmq.25.1712395217761; Sat, 06 Apr 2024 02:20:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGNXztDhLsiKAKWeuFLp95WeOaSFimKeT6ittgqyCRbNIAyGvgIZJkAN7068+uWqmdKvBO5fg== X-Received: by 2002:a05:600c:4e8c:b0:415:6728:a584 with SMTP id f12-20020a05600c4e8c00b004156728a584mr2494512wmq.25.1712395217054; Sat, 06 Apr 2024 02:20:17 -0700 (PDT) Received: from gpd.station (net-2-39-142-110.cust.vodafonedsl.it. [2.39.142.110]) by smtp.gmail.com with ESMTPSA id l23-20020a7bc457000000b00414924f307csm5779993wmi.26.2024.04.06.02.20.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Apr 2024 02:20:16 -0700 (PDT) From: Andrea Righi To: Andrii Nakryiko , Eduard Zingerman Cc: Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , David Vernet , Tejun Heo , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/4] libbpf: Add ring__consume_n / ring_buffer__consume_n Date: Sat, 6 Apr 2024 11:15:43 +0200 Message-ID: <20240406092005.92399-4-andrea.righi@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240406092005.92399-1-andrea.righi@canonical.com> References: <20240406092005.92399-1-andrea.righi@canonical.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Introduce a new API to consume items from a ring buffer, limited to a specified amount, and return to the caller the actual number of items consumed. Link: https://lore.kernel.org/lkml/20240310154726.734289-1-andrea.righi@canonical.com/T Signed-off-by: Andrea Righi --- tools/lib/bpf/libbpf.h | 12 ++++++++++++ tools/lib/bpf/libbpf.map | 3 +++ tools/lib/bpf/ringbuf.c | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index f88ab50c0229..4f775a6dcaa0 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -1293,6 +1293,7 @@ LIBBPF_API int ring_buffer__add(struct ring_buffer *rb, int map_fd, ring_buffer_sample_fn sample_cb, void *ctx); 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__consume_n(struct ring_buffer *rb, size_t n); LIBBPF_API int ring_buffer__epoll_fd(const struct ring_buffer *rb); /** @@ -1367,6 +1368,17 @@ LIBBPF_API int ring__map_fd(const struct ring *r); */ LIBBPF_API int ring__consume(struct ring *r); +/** + * @brief **ring__consume_n()** consumes up to a requested amount of items from + * a ringbuffer without event polling. + * + * @param r A ringbuffer object. + * @param n Maximum amount of items to consume. + * @return The number of items consumed, or a negative number if any of the + * callbacks return an error. + */ +LIBBPF_API int ring__consume_n(struct ring *r, size_t n); + 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 5dd81a7b96b5..23d82bba021a 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -418,4 +418,7 @@ LIBBPF_1.4.0 { } LIBBPF_1.3.0; LIBBPF_1.5.0 { + global: + ring__consume_n; + ring_buffer__consume_n; } LIBBPF_1.4.0; diff --git a/tools/lib/bpf/ringbuf.c b/tools/lib/bpf/ringbuf.c index 2c4031168413..19cd34883011 100644 --- a/tools/lib/bpf/ringbuf.c +++ b/tools/lib/bpf/ringbuf.c @@ -277,6 +277,33 @@ static int64_t ringbuf_process_ring(struct ring *r, size_t n) return cnt; } +/* Consume available ring buffer(s) data without event polling, up to n + * records. + * + * Returns number of records consumed across all registered ring buffers (or + * n, whichever is less), or negative number if any of the callbacks return + * error. + */ +int ring_buffer__consume_n(struct ring_buffer *rb, size_t n) +{ + int64_t err, res = 0; + int i; + + for (i = 0; i < rb->ring_cnt; i++) { + struct ring *ring = rb->rings[i]; + + err = ringbuf_process_ring(ring, n); + if (err < 0) + return libbpf_err(err); + res += err; + n -= err; + + if (n == 0) + break; + } + return res; +} + /* Consume available ring buffer(s) data without event polling. * Returns number of records consumed across all registered ring buffers (or * INT_MAX, whichever is less), or negative number if any of the callbacks @@ -378,17 +405,22 @@ int ring__map_fd(const struct ring *r) return r->map_fd; } -int ring__consume(struct ring *r) +int ring__consume_n(struct ring *r, size_t n) { - int64_t res; + int res; - res = ringbuf_process_ring(r, INT_MAX); + res = ringbuf_process_ring(r, n); if (res < 0) return libbpf_err(res); return res > INT_MAX ? INT_MAX : res; } +int ring__consume(struct ring *r) +{ + return ring__consume_n(r, INT_MAX); +} + static void user_ringbuf_unmap_ring(struct user_ring_buffer *rb) { if (rb->consumer_pos) { From patchwork Sat Apr 6 09:15:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Righi X-Patchwork-Id: 13619706 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) (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 3DB1D3DBB3 for ; Sat, 6 Apr 2024 09:20:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.122 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395226; cv=none; b=HExi4hAGaspO4MP+7sonRwvpOiqJtMagFcwUIH+qJ8s773xOWceXKLZzVKT3uJUlAKAaStKWpuo/zMVRTXNGXZ5YHjTEDbcZ/qBN43T0srJyfKDP8TkNzK3OYh6keK8J6v3A42ShDJnOcXMMKxcKErhNqurax5fZg7Ph5ST/LIE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712395226; c=relaxed/simple; bh=V866d7v0EUjhFe+hV0qrTNaTJ3Gb92BOJZHI+Gz3L24=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qBqZdAaUB2koPExX4o7HZ0aqjoUKw5VC5txmdBUVKb0Rlg3u8p+CHPz+NTuzPIGD8/rBiDFRvQ11BK5ZddD239/1M8kMdL/lzcaGQ0YzfWizCh27Gp8pXVuPef9KoLCRoaxKbtwJ0j3kzfGM500aTMdwVKss4l5S3DRIxqMQu0Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=rxTQMXIl; arc=none smtp.client-ip=185.125.188.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="rxTQMXIl" Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id BFAE63FAEA for ; Sat, 6 Apr 2024 09:20:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1712395221; bh=qzaONF2ufkMLEwxVO924M3LwWzApnPaibbBgBkLZGxs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rxTQMXIl+s9TlEyQ/YloVV5GPHN+LVpAWmUybYi2D8xTXVG6RaMO/+wHr8JRlQ7Jx eFa1Fc6S2xkUJzf//TSPgv83cDrqQg3kND/SyG7jJwmDmHu7XsgurXNpDodgdGtAZk JtDUhw4AP9CW6VB2g+N7ZuJY89TQ/gqDpXO37wIdLO4OQgI/HC3XKTR5gC1dbbVJpB Rsik+xpLEvjvorVay8OXTmi97B1yr29uOuV180Kt16wDdVTGvA8Bp/QoKOoa0D0zwi XDitxMooq3vQJNgOxLn7/A9x+NvDqdVaxqbl9Jj1RcZhvM3ZimZ+RQFkYMvRK89EDD X1wscRL8YyjFQ== Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-515cddd7960so2168725e87.3 for ; Sat, 06 Apr 2024 02:20:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712395219; x=1713000019; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qzaONF2ufkMLEwxVO924M3LwWzApnPaibbBgBkLZGxs=; b=ApiyVVAF0N5ByWF6h8cthLleaYPGphG2CdMXWCaqm22ZFDcaRnwVn+J4h8egUK25to iy3E3G4WkVAIRA2G266T9/fj/NzHcO6WQprhq1Ph2lpEC6QEE4YND8R8LWoWwlBiGQph OuXKyDArh28S4R1H5BuluDxcsCG/oXgd6h5JnvzGHqLO/mMA9thSkcx74+1EOmf433cE 9e/cGEVdB867GVnCzDW/88TrqIYhzvXjtaULkaZojDJdexWLnb0DU+DijAix2myerCHB KUrR7bWUbODMyAYQCVUJ4IkW5VhSJeuxw9WLFamuYurZBWNoIwbVV0V8dWxn1OiUEETU 4W0w== X-Forwarded-Encrypted: i=1; AJvYcCX2L6MAIpVvPC5XpkYhycAeyF+Ko6mAqnu8jrzGrxcE0uMSTUsZLi8K34mVbJmxOcJ+Hj01+yQFaawGGfgOICOKg+Oc X-Gm-Message-State: AOJu0YzIAG1glBoSfly2a/ydAJ40OJaAYEiJjcT2CzTeCszMNCvfY5+8 JA32O1yGQMX1cFv2xcb+DDtYf7UmFS3pnUcEQSY1auplBOfJ/ivUblKmmCAeBfk3FOCxMXmNcy/ BF+n6ohyi2yzN3TyC8QIXyqUuUbIHLlSMgfZNkgQegr8UylifFykPcpLxBEuQA8ASpg== X-Received: by 2002:ac2:528f:0:b0:516:ace0:9a75 with SMTP id q15-20020ac2528f000000b00516ace09a75mr2341490lfm.23.1712395219278; Sat, 06 Apr 2024 02:20:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHZAyOXnlJzHnF6UAxzVX17DxYL4mhxCDf8C91bLhJCKP1I0WnSc9EcZ/D+EvzXCHwk6NrkuA== X-Received: by 2002:ac2:528f:0:b0:516:ace0:9a75 with SMTP id q15-20020ac2528f000000b00516ace09a75mr2341471lfm.23.1712395218819; Sat, 06 Apr 2024 02:20:18 -0700 (PDT) Received: from gpd.station (net-2-39-142-110.cust.vodafonedsl.it. [2.39.142.110]) by smtp.gmail.com with ESMTPSA id l23-20020a7bc457000000b00414924f307csm5779993wmi.26.2024.04.06.02.20.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Apr 2024 02:20:18 -0700 (PDT) From: Andrea Righi To: Andrii Nakryiko , Eduard Zingerman Cc: Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , David Vernet , Tejun Heo , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 4/4] selftests/bpf: Add tests for ring__consume_n and ring_buffer__consume_n Date: Sat, 6 Apr 2024 11:15:44 +0200 Message-ID: <20240406092005.92399-5-andrea.righi@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240406092005.92399-1-andrea.righi@canonical.com> References: <20240406092005.92399-1-andrea.righi@canonical.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add tests for new API ring__consume_n() and ring_buffer__consume_n(). Signed-off-by: Andrea Righi --- tools/testing/selftests/bpf/prog_tests/ringbuf.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/ringbuf.c b/tools/testing/selftests/bpf/prog_tests/ringbuf.c index 48c5695b7abf..33aba7684ab9 100644 --- a/tools/testing/selftests/bpf/prog_tests/ringbuf.c +++ b/tools/testing/selftests/bpf/prog_tests/ringbuf.c @@ -304,10 +304,18 @@ static void ringbuf_subtest(void) err = ring_buffer__consume(ringbuf); CHECK(err < 0, "rb_consume", "failed: %d\b", err); + /* try to consume up to one item */ + err = ring_buffer__consume_n(ringbuf, 1); + CHECK(err < 0 || err > 1, "rb_consume_n", "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"); + /* try to consume up to one item */ + err = ring__consume_n(ring, 1); + CHECK(err < 0 || err > 1, "ring_consume_n", "failed: %d\b", err); + /* 3 rounds, 2 samples each */ cnt = atomic_xchg(&sample_cnt, 0); CHECK(cnt != 6, "cnt", "exp %d samples, got %d\n", 6, cnt);