From patchwork Tue Feb 16 10:57:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12089809 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9CEADC433DB for ; Tue, 16 Feb 2021 10:59:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 61C6164DE0 for ; Tue, 16 Feb 2021 10:59:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229954AbhBPK7p (ORCPT ); Tue, 16 Feb 2021 05:59:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229635AbhBPK7N (ORCPT ); Tue, 16 Feb 2021 05:59:13 -0500 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AC9EC0613D6 for ; Tue, 16 Feb 2021 02:58:18 -0800 (PST) Received: by mail-wm1-x334.google.com with SMTP id o10so5367026wmc.1 for ; Tue, 16 Feb 2021 02:58:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7nhGdH2jRUj/QZlW4iWuCSizyYaXZl3nI4YhKo/LbqY=; b=mwGWoSTICerhgD4ACWwc1oV746IpOBFVKvdPStAMRucN2NP0sqclUIuXrGuFqoxyvO SCWD9eZh96/+ctBR9edJC4k4dx2gag0A31WpAigNDNw7jT8P0NhraIaNwHN7uTjZq6OO indy9aaixC9XZue2Hesf/pxgLtFLisBibWZNI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7nhGdH2jRUj/QZlW4iWuCSizyYaXZl3nI4YhKo/LbqY=; b=HJ3Ali7ooBVBGyToHR7vCGMPJnY7igsMVjpgNi5USG2lDZ/2AZEFKoaAKAoD7ozu9u AxlU5D2JJ6fM3Zey6QO7y3z8GwQSeNR3zWWa3yyX9u+dNm6lPIYJoGwKsohwaA7V3rpB eDDBB+XvwKOxPOzG5uECgyFz3xxRic3rVy3YJOwEfgvSYrz9u6Jnq0i+AYEervNBHBY8 Mu0vGqhV12aYVztlH3UsvcVj3TwppuXJHHOEo8bm3EU3Yt4t41Inl0ZNGtgsaSAoRAmW y6J5ygf71IHo+4Se/52A6YXP3le2NNiNG4Eo7adQ2oJ8TmlE+Gt+qWttJVCQfLcXNe5U hhKA== X-Gm-Message-State: AOAM5317tApCzQ2xa/2Ruk2QTym1LuIgc9hHKYodDbm9W/w7zMpH5paj zgLmlKqm6zkxWZWqBC04jKSS2g== X-Google-Smtp-Source: ABdhPJwP/8rfhpUy+Y14AYtpcc+vn+nJ6fF2LYDcRz95lqgP9lYxW5nNE6lXZrFTz5GVcSJ4SZvXPQ== X-Received: by 2002:a7b:c4cb:: with SMTP id g11mr2801795wmk.99.1613473096807; Tue, 16 Feb 2021 02:58:16 -0800 (PST) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id l1sm2820238wmi.48.2021.02.16.02.58.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 02:58:16 -0800 (PST) From: Lorenz Bauer To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, jakub@cloudflare.com Cc: kernel-team@cloudflare.com, bpf@vger.kernel.org, netdev@vger.kernel.org, Lorenz Bauer Subject: [PATCH bpf-next 1/8] bpf: consolidate shared test timing code Date: Tue, 16 Feb 2021 10:57:06 +0000 Message-Id: <20210216105713.45052-2-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210216105713.45052-1-lmb@cloudflare.com> References: <20210216105713.45052-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Share the timing / signal interruption logic between different implementations of PROG_TEST_RUN. There is a change in behaviour as well. We check the loop exit condition before checking for pending signals. This resolves an edge case where a signal arrives during the last iteration. Instead of aborting with EINTR we return the successful result to user space. Signed-off-by: Lorenz Bauer --- net/bpf/test_run.c | 137 +++++++++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 61 deletions(-) diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index 58bcb8c849d5..33bd2f67e259 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -16,14 +16,82 @@ #define CREATE_TRACE_POINTS #include +struct test_timer { + enum { NO_PREEMPT, NO_MIGRATE } mode; + u32 i; + u64 time_start, time_spent; +}; + +static inline void t_enter(struct test_timer *t) +{ + rcu_read_lock(); + if (t->mode == NO_PREEMPT) + preempt_disable(); + else + migrate_disable(); + + t->time_start = ktime_get_ns(); +} + +static inline void t_leave(struct test_timer *t) +{ + t->time_spent += ktime_get_ns() - t->time_start; + t->time_start = 0; + + if (t->mode == NO_PREEMPT) + preempt_enable(); + else + migrate_enable(); + rcu_read_unlock(); +} + +static inline bool t_check(struct test_timer *t, u32 repeat, int *err, u32 *duration) +{ + if (!t->time_start) { + /* Enter protected section before first iteration. */ + t_enter(t); + return true; + } + + t->i++; + if (t->i >= repeat) { + /* Leave the protected section after the last iteration. */ + t_leave(t); + do_div(t->time_spent, t->i); + *duration = t->time_spent > U32_MAX ? U32_MAX : (u32)t->time_spent; + *err = 0; + goto reset; + } + + if (signal_pending(current)) { + /* During iteration: we've been cancelled, abort. */ + t_leave(t); + *err = -EINTR; + goto reset; + } + + if (need_resched()) { + /* During iteration: we need to reschedule between runs. */ + t_leave(t); + cond_resched(); + t_enter(t); + } + + /* Do another round. */ + return true; + +reset: + t->time_spent = t->i = 0; + return false; +} + static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *retval, u32 *time, bool xdp) { struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = { NULL }; + struct test_timer t = { NO_MIGRATE }; enum bpf_cgroup_storage_type stype; - u64 time_start, time_spent = 0; - int ret = 0; - u32 i; + int ret; for_each_cgroup_storage_type(stype) { storage[stype] = bpf_cgroup_storage_alloc(prog, stype); @@ -38,40 +106,14 @@ static int bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, if (!repeat) repeat = 1; - rcu_read_lock(); - migrate_disable(); - time_start = ktime_get_ns(); - for (i = 0; i < repeat; i++) { + while (t_check(&t, repeat, &ret, time)) { bpf_cgroup_storage_set(storage); if (xdp) *retval = bpf_prog_run_xdp(prog, ctx); else *retval = BPF_PROG_RUN(prog, ctx); - - if (signal_pending(current)) { - ret = -EINTR; - break; - } - - if (need_resched()) { - time_spent += ktime_get_ns() - time_start; - migrate_enable(); - rcu_read_unlock(); - - cond_resched(); - - rcu_read_lock(); - migrate_disable(); - time_start = ktime_get_ns(); - } } - time_spent += ktime_get_ns() - time_start; - migrate_enable(); - rcu_read_unlock(); - - do_div(time_spent, repeat); - *time = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; for_each_cgroup_storage_type(stype) bpf_cgroup_storage_free(storage[stype]); @@ -674,18 +716,17 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, const union bpf_attr *kattr, union bpf_attr __user *uattr) { + struct test_timer t = { NO_PREEMPT }; u32 size = kattr->test.data_size_in; struct bpf_flow_dissector ctx = {}; u32 repeat = kattr->test.repeat; struct bpf_flow_keys *user_ctx; struct bpf_flow_keys flow_keys; - u64 time_start, time_spent = 0; const struct ethhdr *eth; unsigned int flags = 0; u32 retval, duration; void *data; int ret; - u32 i; if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR) return -EINVAL; @@ -721,39 +762,13 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, ctx.data = data; ctx.data_end = (__u8 *)data + size; - rcu_read_lock(); - preempt_disable(); - time_start = ktime_get_ns(); - for (i = 0; i < repeat; i++) { + while (t_check(&t, repeat, &ret, &duration)) { retval = bpf_flow_dissect(prog, &ctx, eth->h_proto, ETH_HLEN, size, flags); - - if (signal_pending(current)) { - preempt_enable(); - rcu_read_unlock(); - - ret = -EINTR; - goto out; - } - - if (need_resched()) { - time_spent += ktime_get_ns() - time_start; - preempt_enable(); - rcu_read_unlock(); - - cond_resched(); - - rcu_read_lock(); - preempt_disable(); - time_start = ktime_get_ns(); - } } - time_spent += ktime_get_ns() - time_start; - preempt_enable(); - rcu_read_unlock(); - do_div(time_spent, repeat); - duration = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; + if (ret < 0) + goto out; ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys), retval, duration); From patchwork Tue Feb 16 10:57:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12089811 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88ED4C433E0 for ; Tue, 16 Feb 2021 10:59:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5049B64DE0 for ; Tue, 16 Feb 2021 10:59:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229635AbhBPK7t (ORCPT ); Tue, 16 Feb 2021 05:59:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229662AbhBPK7O (ORCPT ); Tue, 16 Feb 2021 05:59:14 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2621C061786 for ; Tue, 16 Feb 2021 02:58:18 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id n8so12342248wrm.10 for ; Tue, 16 Feb 2021 02:58:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=i2yCinIxoFBwLow1XBzdxruWNWA8TGQLQmqlS/4jbqc=; b=FmEdQ8d8GxPQx369N6ZOkR3BwvDEXImJV05u2dufOfs9WfqKyH9pm9y2zG6W1L1i6J g38eklpk2UJntpaKyYcczEbS0at70uRxtTNOY0DQGAmA9ep/II3tARqWvGtAUC3y6Kfc GzkLh5MOkcjWyqKaTubz9OSpWPlzJq3BbrpyE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=i2yCinIxoFBwLow1XBzdxruWNWA8TGQLQmqlS/4jbqc=; b=sA3+t0sN7y22CDJdoMFNtaZpTBaVsWxo24hu7DtBtkjPIf43W/Ks13BH2GuFqYi4Ww sH4KVGrsjaWX6wQ+sOPEPPrh1IuqesCM3qeU8GOd/lYx/a6YHLCGji5FdqrKXqsFvX49 W5P1AJYxZc96cJBvs87nLGGq3gG0w4DbFQZpbNUJDt1RaKZgCGVQM5XwDjKeFIzvl4Hi V262z9bItki2+8n4qDP+KxOBC9NukEf50GrHG/GGEyiJCnddg7esrJq5tdF7Ja3FkZ54 qpFJ360MIIM7iJf8EGjAJcqR3+gabBgTKxv20TcYhjc2ct2kmBwezAq4GrjfnGe9xrw9 NfQQ== X-Gm-Message-State: AOAM530KTYKp2uyziYzOeUudPVMvn1zKbZ36MC3fOxXUwbCLw3HxCNoi jj4b8fuQLyallpEa5hBQ/aTaAg== X-Google-Smtp-Source: ABdhPJxg8Srplv+YucwvNv1NCoeTVROtLTTL5owoswv45Q2dRu+KjoCT/2o2Ej7i1B1BSepK/WJeUQ== X-Received: by 2002:adf:ee84:: with SMTP id b4mr22799029wro.339.1613473097526; Tue, 16 Feb 2021 02:58:17 -0800 (PST) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id l1sm2820238wmi.48.2021.02.16.02.58.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 02:58:17 -0800 (PST) From: Lorenz Bauer To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, jakub@cloudflare.com Cc: kernel-team@cloudflare.com, bpf@vger.kernel.org, netdev@vger.kernel.org, Lorenz Bauer Subject: [PATCH bpf-next 2/8] bpf: add for_each_bpf_prog helper Date: Tue, 16 Feb 2021 10:57:07 +0000 Message-Id: <20210216105713.45052-3-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210216105713.45052-1-lmb@cloudflare.com> References: <20210216105713.45052-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add a helper to iterate bpf_prog_arrays, which are a hybrid between and array and a linked list. Hide this behind a for each macro. Signed-off-by: Lorenz Bauer --- include/linux/bpf.h | 11 +++++------ include/linux/filter.h | 4 +--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index cccaef1088ea..875f6bc4bf1d 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1070,6 +1070,9 @@ int bpf_prog_array_copy(struct bpf_prog_array *old_array, /* BPF program asks to set CN on the packet. */ #define BPF_RET_SET_CN (1 << 0) +#define for_each_bpf_prog(_array, _item, _prog) \ + for ((_item) = &(_array)->items[0]; ((_prog) = READ_ONCE((_item)->prog)); (_item)++) + #define BPF_PROG_RUN_ARRAY_FLAGS(array, ctx, func, ret_flags) \ ({ \ struct bpf_prog_array_item *_item; \ @@ -1080,13 +1083,11 @@ int bpf_prog_array_copy(struct bpf_prog_array *old_array, migrate_disable(); \ rcu_read_lock(); \ _array = rcu_dereference(array); \ - _item = &_array->items[0]; \ - while ((_prog = READ_ONCE(_item->prog))) { \ + for_each_bpf_prog(_array, _item, _prog) { \ bpf_cgroup_storage_set(_item->cgroup_storage); \ func_ret = func(_prog, ctx); \ _ret &= (func_ret & 1); \ *(ret_flags) |= (func_ret >> 1); \ - _item++; \ } \ rcu_read_unlock(); \ migrate_enable(); \ @@ -1104,11 +1105,9 @@ int bpf_prog_array_copy(struct bpf_prog_array *old_array, _array = rcu_dereference(array); \ if (unlikely(check_non_null && !_array))\ goto _out; \ - _item = &_array->items[0]; \ - while ((_prog = READ_ONCE(_item->prog))) { \ + for_each_bpf_prog(_array, _item, _prog) { \ bpf_cgroup_storage_set(_item->cgroup_storage); \ _ret &= func(_prog, ctx); \ - _item++; \ } \ _out: \ rcu_read_unlock(); \ diff --git a/include/linux/filter.h b/include/linux/filter.h index 3b00fc906ccd..9ed20ff29d9a 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -1376,8 +1376,7 @@ extern struct static_key_false bpf_sk_lookup_enabled; u32 _ret; \ \ migrate_disable(); \ - _item = &(array)->items[0]; \ - while ((_prog = READ_ONCE(_item->prog))) { \ + for_each_bpf_prog(array, _item, _prog) { \ /* restore most recent selection */ \ _ctx->selected_sk = _selected_sk; \ _ctx->no_reuseport = _no_reuseport; \ @@ -1390,7 +1389,6 @@ extern struct static_key_false bpf_sk_lookup_enabled; } else if (_ret == SK_DROP && _all_pass) { \ _all_pass = false; \ } \ - _item++; \ } \ _ctx->selected_sk = _selected_sk; \ _ctx->no_reuseport = _no_reuseport; \ From patchwork Tue Feb 16 10:57:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12089813 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9CA9CC433DB for ; Tue, 16 Feb 2021 11:00:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4A44B64DE0 for ; Tue, 16 Feb 2021 11:00:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230111AbhBPLAU (ORCPT ); Tue, 16 Feb 2021 06:00:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230001AbhBPLAB (ORCPT ); Tue, 16 Feb 2021 06:00:01 -0500 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69738C06178A for ; Tue, 16 Feb 2021 02:58:19 -0800 (PST) Received: by mail-wr1-x42f.google.com with SMTP id g6so12343949wrs.11 for ; Tue, 16 Feb 2021 02:58:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Q0s0jRVh4fmabtbnB7JD1SFlIfwz3xHcNE/fUPVDxS0=; b=d+RFUH4N4NGX1v46Twg+3dLpVZfcawJEhH0/Eos1zK274D3RGpUtWKLXGa0j3bXSst 4077rnKGeNGMdy54jJ0pZ2CzwBvVIz/qP1unc2y6y8WBsaCHbVp1uRDF7tz4Kc/dP0Z8 oetgRRBQq10qz34vP4nuXkpc7yi5ewwHmYxl0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Q0s0jRVh4fmabtbnB7JD1SFlIfwz3xHcNE/fUPVDxS0=; b=InLpxcZYHPrtfyotoAa2G+pVBxcw9j6v9KdtDCQAQEj2damZqR0CL9OQsc9Q0ySVv5 7PxeWCZNORCP1yTuzsfOuuS5iVBgRSbKFGm4SpzTcwE4deIzgCkqjipprtyZEKb5axCs k22x/X44WGCsQgbqWLX3KzPEWO2hRj7ir9+OIza1iVdJxUr6M9M1RPo3BS/0xY5l5zjf yMjtVPlAjvdiA5ErjSDd945HptFJRqQgLdrsTWcqNmaH8jsFz+Xk/altSkm+vHpWEzND LcII7PaiOM98PR805axCrrIc1QRpcPV9FsebmqldKjLRMoqBfXCi0i3uthZkb858EmVS PSpQ== X-Gm-Message-State: AOAM5308I2xUyr+ywx4bqhC0fTr3jAEdrBnmgl/yBHXg/3Nr3j8uafXD 6iEyoQG88KV7ENqdzoGeLHSbww== X-Google-Smtp-Source: ABdhPJzrFbinBJ2DdaUSzv+7CVJgxLz+JkwyJ4jhKfBByX/xqSkex2y8BqOaTUIGwM96wDtQwT8jpA== X-Received: by 2002:a5d:4987:: with SMTP id r7mr22631619wrq.423.1613473098197; Tue, 16 Feb 2021 02:58:18 -0800 (PST) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id l1sm2820238wmi.48.2021.02.16.02.58.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 02:58:17 -0800 (PST) From: Lorenz Bauer To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, jakub@cloudflare.com Cc: kernel-team@cloudflare.com, bpf@vger.kernel.org, netdev@vger.kernel.org, Lorenz Bauer Subject: [PATCH bpf-next 3/8] bpf: allow multiple programs in BPF_PROG_TEST_RUN Date: Tue, 16 Feb 2021 10:57:08 +0000 Message-Id: <20210216105713.45052-4-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210216105713.45052-1-lmb@cloudflare.com> References: <20210216105713.45052-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The sk_lookup hook allows installing multiple BPF programs simultaneously and has defined semantics. We therefore need to be able to test multiple programs with one PROG_TEST_RUN call. Extend the UAPI to include a prog_fds array which enables this case. Passing an array with a single fd falls back to current behaviour. Program types that allow multiple programs have to provide a new test_run_array callback. Signed-off-by: Lorenz Bauer --- include/linux/bpf-netns.h | 2 + include/linux/bpf.h | 3 ++ include/uapi/linux/bpf.h | 6 ++- kernel/bpf/net_namespace.c | 2 +- kernel/bpf/syscall.c | 73 ++++++++++++++++++++++++++++++---- tools/include/uapi/linux/bpf.h | 6 ++- 6 files changed, 81 insertions(+), 11 deletions(-) diff --git a/include/linux/bpf-netns.h b/include/linux/bpf-netns.h index 722f799c1a2e..f34800cd7017 100644 --- a/include/linux/bpf-netns.h +++ b/include/linux/bpf-netns.h @@ -5,6 +5,8 @@ #include #include +#define BPF_SK_LOOKUP_MAX_PROGS 64 + enum netns_bpf_attach_type { NETNS_BPF_INVALID = -1, NETNS_BPF_FLOW_DISSECTOR = 0, diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 875f6bc4bf1d..67c21c8ba7cc 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -26,6 +26,7 @@ struct bpf_verifier_env; struct bpf_verifier_log; struct perf_event; struct bpf_prog; +struct bpf_prog_array; struct bpf_prog_aux; struct bpf_map; struct sock; @@ -437,6 +438,8 @@ bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size) struct bpf_prog_ops { int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr, union bpf_attr __user *uattr); + int (*test_run_array)(struct bpf_prog_array *progs, const union bpf_attr *kattr, + union bpf_attr __user *uattr); }; struct bpf_verifier_ops { diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 4c24daa43bac..b37a0f39b95f 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -589,7 +589,9 @@ union bpf_attr { }; struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */ - __u32 prog_fd; + __u32 prog_fd; /* input: program to test. mutually exclusive with + * prog_fds. + */ __u32 retval; __u32 data_size_in; /* input: len of data_in */ __u32 data_size_out; /* input/output: len of data_out @@ -609,6 +611,8 @@ union bpf_attr { __aligned_u64 ctx_out; __u32 flags; __u32 cpu; + __aligned_u64 prog_fds; + __u32 prog_fds_cnt; } test; struct { /* anonymous struct used by BPF_*_GET_*_ID */ diff --git a/kernel/bpf/net_namespace.c b/kernel/bpf/net_namespace.c index 542f275bf252..61e4769f0110 100644 --- a/kernel/bpf/net_namespace.c +++ b/kernel/bpf/net_namespace.c @@ -411,7 +411,7 @@ static int netns_bpf_max_progs(enum netns_bpf_attach_type type) case NETNS_BPF_FLOW_DISSECTOR: return 1; case NETNS_BPF_SK_LOOKUP: - return 64; + return BPF_SK_LOOKUP_MAX_PROGS; default: return 0; } diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index c859bc46d06c..f8c7b9d86b3f 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3100,13 +3100,17 @@ static int bpf_prog_query(const union bpf_attr *attr, } } -#define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu +#define BPF_PROG_TEST_RUN_LAST_FIELD test.prog_fds_cnt static int bpf_prog_test_run(const union bpf_attr *attr, union bpf_attr __user *uattr) { + enum bpf_prog_type prog_type = BPF_PROG_TYPE_UNSPEC; + u32 prog_fds[BPF_SK_LOOKUP_MAX_PROGS]; + struct bpf_prog_array *progs = NULL; struct bpf_prog *prog; - int ret = -ENOTSUPP; + u32 prog_cnt; + int i, ret; if (CHECK_ATTR(BPF_PROG_TEST_RUN)) return -EINVAL; @@ -3119,14 +3123,67 @@ static int bpf_prog_test_run(const union bpf_attr *attr, (!attr->test.ctx_size_out && attr->test.ctx_out)) return -EINVAL; - prog = bpf_prog_get(attr->test.prog_fd); - if (IS_ERR(prog)) - return PTR_ERR(prog); + if ((attr->test.prog_fds && !attr->test.prog_fds_cnt) || + (!attr->test.prog_fds && attr->test.prog_fds_cnt)) + return -EINVAL; - if (prog->aux->ops->test_run) - ret = prog->aux->ops->test_run(prog, attr, uattr); + if (attr->test.prog_fds) { + u32 __user *uprog_fds = u64_to_user_ptr(attr->test.prog_fds); - bpf_prog_put(prog); + if (attr->test.prog_fds_cnt >= ARRAY_SIZE(prog_fds)) + return -EINVAL; + + if (attr->test.prog_fd) + return -EINVAL; + + prog_cnt = attr->test.prog_fds_cnt; + if (copy_from_user(prog_fds, uprog_fds, prog_cnt * sizeof(prog_fds[0]))) + return -EFAULT; + } else { + prog_cnt = 1; + prog_fds[0] = attr->test.prog_fd; + } + + progs = bpf_prog_array_alloc(prog_cnt, GFP_KERNEL); + if (!progs) + return -ENOMEM; + + for (i = 0; i < prog_cnt; i++) { + prog = bpf_prog_get(prog_fds[i]); + if (IS_ERR(prog)) { + ret = PTR_ERR(prog); + goto out; + } + + progs->items[i].prog = prog; + + if (prog_type && prog->type != prog_type) { + ret = -EINVAL; + goto out; + } + + prog_type = prog->type; + } + + prog = progs->items[0].prog; + if (prog->aux->ops->test_run_array) { + ret = prog->aux->ops->test_run_array(progs, attr, uattr); + } else if (prog->aux->ops->test_run) { + if (prog_cnt > 1) { + ret = -EOPNOTSUPP; + goto out; + } + + ret = prog->aux->ops->test_run(progs->items[0].prog, attr, uattr); + } else { + ret = -ENOTSUPP; + } + +out: + for (i = 0; i < prog_cnt; i++) + if (progs->items[i].prog) + bpf_prog_put(progs->items[i].prog); + bpf_prog_array_free(progs); return ret; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 4c24daa43bac..b37a0f39b95f 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -589,7 +589,9 @@ union bpf_attr { }; struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */ - __u32 prog_fd; + __u32 prog_fd; /* input: program to test. mutually exclusive with + * prog_fds. + */ __u32 retval; __u32 data_size_in; /* input: len of data_in */ __u32 data_size_out; /* input/output: len of data_out @@ -609,6 +611,8 @@ union bpf_attr { __aligned_u64 ctx_out; __u32 flags; __u32 cpu; + __aligned_u64 prog_fds; + __u32 prog_fds_cnt; } test; struct { /* anonymous struct used by BPF_*_GET_*_ID */ From patchwork Tue Feb 16 10:57:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12089815 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E14B8C433E0 for ; Tue, 16 Feb 2021 11:00:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A2C6A64DEC for ; Tue, 16 Feb 2021 11:00:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230104AbhBPLAQ (ORCPT ); Tue, 16 Feb 2021 06:00:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229989AbhBPK76 (ORCPT ); Tue, 16 Feb 2021 05:59:58 -0500 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D484C06178C for ; Tue, 16 Feb 2021 02:58:20 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id v62so4493967wmg.4 for ; Tue, 16 Feb 2021 02:58:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=736Q/Aggc5c2j08pkKQ+AVlMQY2/agbGbM41sgkyMCQ=; b=DXkTnFe2p1olBkyELWf8N7H6Srh7WFWzpQ1O4u3zHUjEjbhcf63dMgBdpnBb7CT9tK tmfeMBQqkzBKAcKa33NOAYOUo7tSr4KpPJ4xQfXehNbqIP/Lg1BBY1qWQXV4wz5sDeR+ iESfJC1EUgwoY9WBf6JqLnqPZrLSaO9Cy/h0Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=736Q/Aggc5c2j08pkKQ+AVlMQY2/agbGbM41sgkyMCQ=; b=NAc/R8ND5A8kVUSaCAKDKOELVasoLqPoHshApc+iidWOTOBJG2Gh1tX3H0S/2wUP4y lDbXnXfgvdPO5jSymUoYBJBsCR60dJ2E71+CR56Z+ukk4HiRC4ggmvaGs6kCJ6PudWsQ eDHGZd9QWxTGWgdMQxoLdNEyi7GR35vq4P0eg+oXEPnqaoZvdyhohkyKST9iYIyhd7td 6dqBUO+HTG86/zjEanVw2OB0Sf1GhWL+/UrGJYYawWP2a3pk1pnfpFWGjuk8/aMoNYzO 82NHEoytac8KGw8b8W9Zq/y7koCqnkJO81f4nhMKCDzvHWIB0Y5seSiM5C12DtDce2vx CpLw== X-Gm-Message-State: AOAM5304VzaAXgWmoAgoVE5SFOvFKvV8FTlGTk/AzkH4nB/ZwdYmDd1d AyglAlQBXBsWsc2MbB6HPzpWvg== X-Google-Smtp-Source: ABdhPJxJz7QAeIKGWCRlwIme7qHMRmtZipIeHW0+zq/zcZip7SrSaq0q3ZKcrHYIfi9frcpy0+0Ydg== X-Received: by 2002:a05:600c:230c:: with SMTP id 12mr2863068wmo.30.1613473098956; Tue, 16 Feb 2021 02:58:18 -0800 (PST) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id l1sm2820238wmi.48.2021.02.16.02.58.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 02:58:18 -0800 (PST) From: Lorenz Bauer To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, jakub@cloudflare.com Cc: kernel-team@cloudflare.com, bpf@vger.kernel.org, netdev@vger.kernel.org, Lorenz Bauer Subject: [PATCH bpf-next 4/8] bpf: add PROG_TEST_RUN support for sk_lookup programs Date: Tue, 16 Feb 2021 10:57:09 +0000 Message-Id: <20210216105713.45052-5-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210216105713.45052-1-lmb@cloudflare.com> References: <20210216105713.45052-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Allow to pass (multiple) sk_lookup programs to PROG_TEST_RUN. User space provides the full bpf_sk_lookup struct as context. Since the context includes a socket pointer that can't be exposed to user space we define that PROG_TEST_RUN returns the cookie of the selected socket or zero in place of the socket pointer. We don't support testing programs that select a reuseport socket, since this would mean running another (unrelated) BPF program from the sk_lookup test handler. Signed-off-by: Lorenz Bauer --- include/linux/bpf.h | 10 ++++ include/uapi/linux/bpf.h | 5 +- net/bpf/test_run.c | 93 ++++++++++++++++++++++++++++++++++ net/core/filter.c | 1 + tools/include/uapi/linux/bpf.h | 5 +- 5 files changed, 112 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 67c21c8ba7cc..d251db1354ec 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1472,6 +1472,9 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, int bpf_prog_test_run_raw_tp(struct bpf_prog *prog, const union bpf_attr *kattr, union bpf_attr __user *uattr); +int bpf_prog_test_run_sk_lookup(struct bpf_prog_array *progs, + const union bpf_attr *kattr, + union bpf_attr __user *uattr); bool btf_ctx_access(int off, int size, enum bpf_access_type type, const struct bpf_prog *prog, struct bpf_insn_access_aux *info); @@ -1672,6 +1675,13 @@ static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, return -ENOTSUPP; } +static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog_array *progs, + const union bpf_attr *kattr, + union bpf_attr __user *uattr) +{ + return -ENOTSUPP; +} + static inline void bpf_map_put(struct bpf_map *map) { } diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index b37a0f39b95f..078ad0b8d1a7 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5210,7 +5210,10 @@ struct bpf_pidns_info { /* User accessible data for SK_LOOKUP programs. Add new fields at the end. */ struct bpf_sk_lookup { - __bpf_md_ptr(struct bpf_sock *, sk); /* Selected socket */ + union { + __bpf_md_ptr(struct bpf_sock *, sk); /* Selected socket */ + __u64 cookie; /* Non-zero if socket was selected in PROG_TEST_RUN */ + }; __u32 family; /* Protocol family (AF_INET, AF_INET6) */ __u32 protocol; /* IP protocol (IPPROTO_TCP, IPPROTO_UDP) */ diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index 33bd2f67e259..932c8e036b0a 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -10,8 +10,10 @@ #include #include #include +#include #include #include +#include #define CREATE_TRACE_POINTS #include @@ -781,3 +783,94 @@ int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, kfree(data); return ret; } + +int bpf_prog_test_run_sk_lookup(struct bpf_prog_array *progs, const union bpf_attr *kattr, + union bpf_attr __user *uattr) +{ + struct test_timer t = { NO_PREEMPT }; + struct bpf_sk_lookup_kern ctx = {}; + u32 repeat = kattr->test.repeat; + struct bpf_sk_lookup *user_ctx; + u32 retval, duration; + int ret = -EINVAL; + + if (bpf_prog_array_length(progs) >= BPF_SK_LOOKUP_MAX_PROGS) + return -E2BIG; + + if (kattr->test.flags || kattr->test.cpu) + return -EINVAL; + + if (kattr->test.data_in || kattr->test.data_size_in || kattr->test.data_out || + kattr->test.data_size_out) + return -EINVAL; + + if (!repeat) + repeat = 1; + + user_ctx = bpf_ctx_init(kattr, sizeof(*user_ctx)); + if (IS_ERR(user_ctx)) + return PTR_ERR(user_ctx); + + if (!user_ctx) + return -EINVAL; + + if (user_ctx->sk) + goto out; + + if (!range_is_zero(user_ctx, offsetofend(typeof(*user_ctx), local_port), sizeof(*user_ctx))) + goto out; + + if (user_ctx->local_port > U16_MAX || user_ctx->remote_port > U16_MAX) { + ret = -ERANGE; + goto out; + } + + ctx.family = user_ctx->family; + ctx.protocol = user_ctx->protocol; + ctx.dport = user_ctx->local_port; + ctx.sport = user_ctx->remote_port; + + switch (ctx.family) { + case AF_INET: + ctx.v4.daddr = user_ctx->local_ip4; + ctx.v4.saddr = user_ctx->remote_ip4; + break; + +#if IS_ENABLED(CONFIG_IPV6) + case AF_INET6: + ctx.v6.daddr = (struct in6_addr *)user_ctx->local_ip6; + ctx.v6.saddr = (struct in6_addr *)user_ctx->remote_ip6; + break; +#endif + + default: + ret = -EAFNOSUPPORT; + goto out; + } + + while (t_check(&t, repeat, &ret, &duration)) { + ctx.selected_sk = NULL; + retval = BPF_PROG_SK_LOOKUP_RUN_ARRAY(progs, ctx, BPF_PROG_RUN); + } + + if (ret < 0) + goto out; + + user_ctx->cookie = 0; + if (ctx.selected_sk) { + if (ctx.selected_sk->sk_reuseport && !ctx.no_reuseport) { + ret = -EOPNOTSUPP; + goto out; + } + + user_ctx->cookie = sock_gen_cookie(ctx.selected_sk); + } + + ret = bpf_test_finish(kattr, uattr, NULL, 0, retval, duration); + if (!ret) + ret = bpf_ctx_finish(kattr, uattr, user_ctx, sizeof(*user_ctx)); + +out: + kfree(user_ctx); + return ret; +} diff --git a/net/core/filter.c b/net/core/filter.c index 7059cf604d94..978cea941268 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -10451,6 +10451,7 @@ static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type, } const struct bpf_prog_ops sk_lookup_prog_ops = { + .test_run_array = bpf_prog_test_run_sk_lookup, }; const struct bpf_verifier_ops sk_lookup_verifier_ops = { diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index b37a0f39b95f..078ad0b8d1a7 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5210,7 +5210,10 @@ struct bpf_pidns_info { /* User accessible data for SK_LOOKUP programs. Add new fields at the end. */ struct bpf_sk_lookup { - __bpf_md_ptr(struct bpf_sock *, sk); /* Selected socket */ + union { + __bpf_md_ptr(struct bpf_sock *, sk); /* Selected socket */ + __u64 cookie; /* Non-zero if socket was selected in PROG_TEST_RUN */ + }; __u32 family; /* Protocol family (AF_INET, AF_INET6) */ __u32 protocol; /* IP protocol (IPPROTO_TCP, IPPROTO_UDP) */ From patchwork Tue Feb 16 10:57:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12089821 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 98CC5C433DB for ; Tue, 16 Feb 2021 11:02:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5FC4664DF0 for ; Tue, 16 Feb 2021 11:02:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230221AbhBPLCj (ORCPT ); Tue, 16 Feb 2021 06:02:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230122AbhBPLAg (ORCPT ); Tue, 16 Feb 2021 06:00:36 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F8E7C061797 for ; Tue, 16 Feb 2021 02:58:21 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id n6so12357304wrv.8 for ; Tue, 16 Feb 2021 02:58:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jBv+5MRiPKNFrO7CQEt+NYcgc+kgvHjv52FGJd36h2Q=; b=i86ku6Ry9W6yu653Vazsiiih6hdh/ynxtulsqZuUH0AWZgDZ0M3qAcGjws9m1v69pc UqjOogxVEz+LxC7z2B2pNDXrqjmnnopYCOhrU5TDtJ6Pm8UgBwiCvc3GGxyEVlU4nvM/ +qS9+DYO8ChiKKBNJcRoB2i1fVnHDHX/j/Zbo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jBv+5MRiPKNFrO7CQEt+NYcgc+kgvHjv52FGJd36h2Q=; b=FBdy3nXID1GuSoHSXuK8zyZ2+PkbeXg1XnApm/GKYNaj3k1fJQ6R0/61y59L2kT63g cxyxXSfrjtaQjjGlFZni8mp3muw85Aj1vM5mIyuK7R6Wyvuwe2QQIJ+eltp7xZQbmvE1 LCMOoZmciKXokdSBjNkMBfb11T+lQ2N/kTbMkZ7gMEsqS0WyS8sJ3Sx28leNPIPxxD4y lfjL6Vh988UrLeuiTDXHpSE+FHMs78LCtJa3ueeYsyXdMUFYh3DOEDgcb0nO/DsPN8EF Z1vK/8Np8WzDKXyM0DvAQYueXCFkPm8jlhNp2wQCuh7RZ+AryET4HLhV2C2f/ZgQNfak Od+w== X-Gm-Message-State: AOAM532iWHGws9aEXg8WaxwfZHKmD9AKy0LLjtYKwKrfRjg7vRoCztay 9FG65J6POwJLRTIr8hIE75e1Yg== X-Google-Smtp-Source: ABdhPJyNZL0iXaYzqkb2LjvXU4CpSaA/7gfgQSux3cXygbPa3aJKaVL3dVFgsVBRKqw/UbIUsELxxA== X-Received: by 2002:adf:c6c1:: with SMTP id c1mr23474650wrh.326.1613473099806; Tue, 16 Feb 2021 02:58:19 -0800 (PST) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id l1sm2820238wmi.48.2021.02.16.02.58.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 02:58:19 -0800 (PST) From: Lorenz Bauer To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, jakub@cloudflare.com Cc: kernel-team@cloudflare.com, bpf@vger.kernel.org, netdev@vger.kernel.org, Lorenz Bauer Subject: [PATCH bpf-next 5/8] tools: libbpf: allow testing program types with multi-prog semantics Date: Tue, 16 Feb 2021 10:57:10 +0000 Message-Id: <20210216105713.45052-6-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210216105713.45052-1-lmb@cloudflare.com> References: <20210216105713.45052-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add a wrapper bpf_prog_test_run_array that allows testing multiple programs for supported program types. Signed-off-by: Lorenz Bauer --- tools/lib/bpf/bpf.c | 16 +++++++++++++++- tools/lib/bpf/bpf.h | 3 +++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index bba48ff4c5c0..95ec1a3f0931 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -765,6 +765,14 @@ int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr) } int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts) +{ + __u32 fd = prog_fd; + + return bpf_prog_test_run_array(&fd, 1, opts); +} + +int bpf_prog_test_run_array(__u32 *prog_fds, __u32 prog_fds_cnt, + struct bpf_test_run_opts *opts) { union bpf_attr attr; int ret; @@ -773,7 +781,6 @@ int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts) return -EINVAL; memset(&attr, 0, sizeof(attr)); - attr.test.prog_fd = prog_fd; attr.test.cpu = OPTS_GET(opts, cpu, 0); attr.test.flags = OPTS_GET(opts, flags, 0); attr.test.repeat = OPTS_GET(opts, repeat, 0); @@ -787,6 +794,13 @@ int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts) attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL)); attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL)); + if (prog_fds_cnt == 1) { + attr.test.prog_fd = prog_fds[0]; + } else { + attr.test.prog_fds = ptr_to_u64(prog_fds); + attr.test.prog_fds_cnt = prog_fds_cnt; + } + ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr)); OPTS_SET(opts, data_size_out, attr.test.data_size_out); OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out); diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 875dde20d56e..47a05fdc9867 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -278,6 +278,9 @@ struct bpf_test_run_opts { LIBBPF_API int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts); +LIBBPF_API int bpf_prog_test_run_array(__u32 *prog_fds, __u32 prog_fds_cnt, + struct bpf_test_run_opts *opts); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 1c0fd2dd233a..bc3a0b2d645f 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -340,6 +340,7 @@ LIBBPF_0.2.0 { LIBBPF_0.3.0 { global: + bpf_prog_test_run_array; btf__base_btf; btf__parse_elf_split; btf__parse_raw_split; From patchwork Tue Feb 16 10:57:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12089823 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3BCC8C433DB for ; Tue, 16 Feb 2021 11:02:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 00CC164DFF for ; Tue, 16 Feb 2021 11:02:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230001AbhBPLCl (ORCPT ); Tue, 16 Feb 2021 06:02:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229890AbhBPLAh (ORCPT ); Tue, 16 Feb 2021 06:00:37 -0500 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4C59C0617A7 for ; Tue, 16 Feb 2021 02:58:21 -0800 (PST) Received: by mail-wm1-x332.google.com with SMTP id o82so1431590wme.1 for ; Tue, 16 Feb 2021 02:58:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0vsKJ/qcSYRSovInQOksHAVnokYKEDDhJ+tl+RlMl7A=; b=ppRZdQAZe+N7aXM4BQ96wdgVafPGAko/k1BkXbTOFiAwragBIfHpFPlamW0cSyi4kw 4GbnT155B8ysgjVoIXtG++3iJtlz14sdzI2l39T0hQdHCW5++MwctT6kcNLMv/aDknt0 BM3vT2gJwqkc9YmtZF8xV63kDPDuUh/e9/fvc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0vsKJ/qcSYRSovInQOksHAVnokYKEDDhJ+tl+RlMl7A=; b=Ck0baSCzTEbwR9mgfd7ECoLWEyvZYXqj0sCDxRaUX44zHdfpf+AiDNkhBnQb02/1ar wC5xVi6Cd2gqK7RehS2y8Cy//LekWkabrfUAM7NOIBsYJL82gPyhVNJ437Y4I/Fo9Kp3 geY+y+sf02XO3rNCBXUM0vqJPb+t6Qqi9hdry1dLkTBYvZKxAG1GwE/W/QZ8ePN2Ql3P 3Z/+1dqZrf0Pt34IlvT9qKkAu9EJgrfKTSTNUVQzsZgL8dBIMxsoQ2cxc7Fzr6oAdUuJ +o4Ed1/9X4r+rV+Eo5XS0M2UZZuEYZQH4E8hS35LNtcyZiQcCzuNTknNI5mdqJYhmtQ2 GXhg== X-Gm-Message-State: AOAM5315YSlF4F3oDKIsGuIvqdCoSI+//JTATedQzrtB55mnSxUk1bj+ GwgISLOc9Lh9cWw3imzJcSrsJQ== X-Google-Smtp-Source: ABdhPJyK8cymYaPqJEbDId1h7FaAFsC/dWT2XFzvvLWTKTulJa1fHBKGxmJFxSNFGR/Hc6+IcqeLTA== X-Received: by 2002:a1c:81d4:: with SMTP id c203mr2763553wmd.76.1613473100686; Tue, 16 Feb 2021 02:58:20 -0800 (PST) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id l1sm2820238wmi.48.2021.02.16.02.58.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 02:58:20 -0800 (PST) From: Lorenz Bauer To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, jakub@cloudflare.com Cc: kernel-team@cloudflare.com, bpf@vger.kernel.org, netdev@vger.kernel.org, Lorenz Bauer Subject: [PATCH bpf-next 6/8] selftests: bpf: convert sk_lookup multi prog tests to PROG_TEST_RUN Date: Tue, 16 Feb 2021 10:57:11 +0000 Message-Id: <20210216105713.45052-7-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210216105713.45052-1-lmb@cloudflare.com> References: <20210216105713.45052-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Make the tests for multi program sk_lookup semantics use bpf_prog_run_array. This simplifies the test a bit and adds coverage to the new libbpf function. Signed-off-by: Lorenz Bauer --- .../selftests/bpf/prog_tests/sk_lookup.c | 100 ++++++++++++------ 1 file changed, 65 insertions(+), 35 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index 9ff0412e1fd3..a8e4a2044170 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -267,6 +267,17 @@ static int recv_byte(int fd) return 0; } +static __u64 socket_cookie(int fd) +{ + __u64 cookie; + socklen_t cookie_len = sizeof(cookie); + + if (CHECK(getsockopt(fd, SOL_SOCKET, SO_COOKIE, &cookie, &cookie_len) < 0, + "getsockopt(SO_COOKIE)", "%s\n", strerror(errno))) + return 0; + return cookie; +} + static int tcp_recv_send(int server_fd) { char buf[1]; @@ -1128,17 +1139,27 @@ struct test_multi_prog { struct bpf_program *prog2; struct bpf_map *redir_map; struct bpf_map *run_map; - int expect_errno; + enum sk_action result; struct inet_addr listen_at; + bool redirect; }; static void run_multi_prog_lookup(const struct test_multi_prog *t) { - struct sockaddr_storage dst = {}; - int map_fd, server_fd, client_fd; - struct bpf_link *link1, *link2; + int map_fd, server_fd; + struct bpf_sk_lookup ctx = {}; int prog_idx, done, err; + __u32 prog_fds[2]; + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, + .ctx_in = &ctx, + .ctx_size_in = sizeof(ctx), + .ctx_out = &ctx, + .ctx_size_out = sizeof(ctx), + ); + + prog_fds[0] = bpf_program__fd(t->prog1); + prog_fds[1] = bpf_program__fd(t->prog2); map_fd = bpf_map__fd(t->run_map); done = 0; @@ -1151,33 +1172,37 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) if (CHECK(err, "bpf_map_update_elem", "failed\n")) return; - link1 = attach_lookup_prog(t->prog1); - if (!link1) - return; - link2 = attach_lookup_prog(t->prog2); - if (!link2) - goto out_unlink1; - server_fd = make_server(SOCK_STREAM, t->listen_at.ip, t->listen_at.port, NULL); if (server_fd < 0) - goto out_unlink2; + return; err = update_lookup_map(t->redir_map, SERVER_A, server_fd); if (err) - goto out_close_server; - - client_fd = make_socket(SOCK_STREAM, EXT_IP4, EXT_PORT, &dst); - if (client_fd < 0) - goto out_close_server; - - err = connect(client_fd, (void *)&dst, inetaddr_len(&dst)); - if (CHECK(err && !t->expect_errno, "connect", - "unexpected error %d\n", errno)) - goto out_close_client; - if (CHECK(err && t->expect_errno && errno != t->expect_errno, - "connect", "unexpected error %d\n", errno)) - goto out_close_client; + goto out; + + ctx.family = AF_INET; + ctx.protocol = IPPROTO_TCP; + + err = bpf_prog_test_run_array(prog_fds, ARRAY_SIZE(prog_fds), &opts); + if (CHECK(err, "test_run_array", "failed with error %d\n", errno)) + goto out; + + if (CHECK(opts.retval != t->result, "test_run", "unexpected result %d\n", opts.retval)) + goto out; + + if (t->redirect) { + __u64 cookie = socket_cookie(server_fd); + + if (!cookie) + goto out; + + if (CHECK(ctx.cookie != cookie, "redirect", + "selected sk:%llu instead of sk:%llu\n", ctx.cookie, cookie)) + goto out; + } else if (CHECK(ctx.cookie, "redirect", "selected unexpected sk:%llu\n", ctx.cookie)) { + goto out; + } done = 0; prog_idx = PROG1; @@ -1191,14 +1216,8 @@ static void run_multi_prog_lookup(const struct test_multi_prog *t) CHECK(err, "bpf_map_lookup_elem", "failed\n"); CHECK(!done, "bpf_map_lookup_elem", "PROG2 !done\n"); -out_close_client: - close(client_fd); -out_close_server: +out: close(server_fd); -out_unlink2: - bpf_link__destroy(link2); -out_unlink1: - bpf_link__destroy(link1); } static void test_multi_prog_lookup(struct test_sk_lookup *skel) @@ -1209,57 +1228,68 @@ static void test_multi_prog_lookup(struct test_sk_lookup *skel) .prog1 = skel->progs.multi_prog_pass1, .prog2 = skel->progs.multi_prog_pass2, .listen_at = { EXT_IP4, EXT_PORT }, + .result = SK_PASS, }, { .desc = "multi prog - drop, drop", .prog1 = skel->progs.multi_prog_drop1, .prog2 = skel->progs.multi_prog_drop2, .listen_at = { EXT_IP4, EXT_PORT }, - .expect_errno = ECONNREFUSED, + .result = SK_DROP, }, { .desc = "multi prog - pass, drop", .prog1 = skel->progs.multi_prog_pass1, .prog2 = skel->progs.multi_prog_drop2, .listen_at = { EXT_IP4, EXT_PORT }, - .expect_errno = ECONNREFUSED, + .result = SK_DROP, }, { .desc = "multi prog - drop, pass", .prog1 = skel->progs.multi_prog_drop1, .prog2 = skel->progs.multi_prog_pass2, .listen_at = { EXT_IP4, EXT_PORT }, - .expect_errno = ECONNREFUSED, + .result = SK_DROP, }, { .desc = "multi prog - pass, redir", .prog1 = skel->progs.multi_prog_pass1, .prog2 = skel->progs.multi_prog_redir2, .listen_at = { INT_IP4, INT_PORT }, + .result = SK_PASS, + .redirect = true, }, { .desc = "multi prog - redir, pass", .prog1 = skel->progs.multi_prog_redir1, .prog2 = skel->progs.multi_prog_pass2, .listen_at = { INT_IP4, INT_PORT }, + .result = SK_PASS, + .redirect = true, }, { .desc = "multi prog - drop, redir", .prog1 = skel->progs.multi_prog_drop1, .prog2 = skel->progs.multi_prog_redir2, .listen_at = { INT_IP4, INT_PORT }, + .result = SK_PASS, + .redirect = true, }, { .desc = "multi prog - redir, drop", .prog1 = skel->progs.multi_prog_redir1, .prog2 = skel->progs.multi_prog_drop2, .listen_at = { INT_IP4, INT_PORT }, + .result = SK_PASS, + .redirect = true, }, { .desc = "multi prog - redir, redir", .prog1 = skel->progs.multi_prog_redir1, .prog2 = skel->progs.multi_prog_redir2, .listen_at = { INT_IP4, INT_PORT }, + .result = SK_PASS, + .redirect = true, }, }; struct test_multi_prog *t; From patchwork Tue Feb 16 10:57:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12089819 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1457AC433E6 for ; Tue, 16 Feb 2021 11:02:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D34EC64DFF for ; Tue, 16 Feb 2021 11:02:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230210AbhBPLCR (ORCPT ); Tue, 16 Feb 2021 06:02:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230038AbhBPLAg (ORCPT ); Tue, 16 Feb 2021 06:00:36 -0500 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9CE1C0617AB for ; Tue, 16 Feb 2021 02:58:22 -0800 (PST) Received: by mail-wm1-x32a.google.com with SMTP id x4so14007780wmi.3 for ; Tue, 16 Feb 2021 02:58:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9itnGMNRrLfIQfTToGhLcxe5CIelofdkEOca4lbkXKU=; b=d1LXmUjU+o1TvgaeibJ49zxFqcdfTZi3jXPWwEJDWAZyC55RGsP1eHsZBi0D42XFNE cAlHO0ERDiT4KTdDRRbQNY0e1i7WBULsG8fZW8o5zTO2bLFvNhLMIM8M7I/xgey0j4ty Wv8jw0l2l+bdT4GBjnmaUwAMqrw5uh1qihZuk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9itnGMNRrLfIQfTToGhLcxe5CIelofdkEOca4lbkXKU=; b=Xcwnbocs8ECfDZ5E5wGe5vOS51ysoMemQoabtMolfLfNPwFU0sbNUgpmdEQ5Jl6Hzj g4mkxjGC2B2k5L47A8+dRBivLYAYu9LfCzLpHyUvfmdVPMEDar3Ohp87pawxLZ4l0q93 EAc5D5LgP3o7xmqzp+VRrT+LCL1IWKHUPn23mSVGisVO9AuTVZYH/ZGwdJkYFt85fpdi mxtcJfkumphJbfxZyaPElH7k5CZMSASZztD3PCtG1QcmCKuNQQ1GdGfZuIxFXumGxywf u297NOqY71goIXvG3CRhrXhovOLuaMoXEZkHT1U1Kr/dXfJ9p1Vp14I25cPbSd/srov5 xTQA== X-Gm-Message-State: AOAM5302oCd5hV+2Ydd0Xf58pyiRIJqEP6Rfj3KtxFt3OQ0Wq7ND5a/o qWZ6QRQkUU5VIk+HpfxjqCXEp4Ubqn3a0Q== X-Google-Smtp-Source: ABdhPJxFBJ8gVlub40t8diuQltLKKhC6eSlxM8sfMGsb2Wtirr2s/r4PM6h++YfAc1C2QbmTSJ9ESw== X-Received: by 2002:a7b:ce12:: with SMTP id m18mr2910994wmc.148.1613473101559; Tue, 16 Feb 2021 02:58:21 -0800 (PST) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id l1sm2820238wmi.48.2021.02.16.02.58.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 02:58:21 -0800 (PST) From: Lorenz Bauer To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, jakub@cloudflare.com Cc: kernel-team@cloudflare.com, bpf@vger.kernel.org, netdev@vger.kernel.org, Lorenz Bauer Subject: [PATCH bpf-next 7/8] selftests: bpf: convert sk_lookup ctx access tests to PROG_TEST_RUN Date: Tue, 16 Feb 2021 10:57:12 +0000 Message-Id: <20210216105713.45052-8-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210216105713.45052-1-lmb@cloudflare.com> References: <20210216105713.45052-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Convert the selftests for sk_lookup narrow context access to use PROG_TEST_RUN instead of creating actual sockets. This ensures that ctx is populated correctly when using PROG_TEST_RUN. Assert concrete values since we now control remote_ip and remote_port. Signed-off-by: Lorenz Bauer --- .../selftests/bpf/prog_tests/sk_lookup.c | 72 +++++++++++++++---- .../selftests/bpf/progs/test_sk_lookup.c | 62 ++++++++++------ 2 files changed, 98 insertions(+), 36 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c index a8e4a2044170..eb8906ace5a9 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -241,6 +241,37 @@ static int make_client(int sotype, const char *ip, int port) return -1; } +static int fill_sk_lookup_ctx(struct bpf_sk_lookup *ctx, const char *local_ip, __u16 local_port, + const char *remote_ip, __u16 remote_port) +{ + void *local, *remote; + int err; + + memset(ctx, 0, sizeof(*ctx)); + ctx->local_port = local_port; + ctx->remote_port = htons(remote_port); + + if (is_ipv6(local_ip)) { + ctx->family = AF_INET6; + local = &ctx->local_ip6[0]; + remote = &ctx->remote_ip6[0]; + } else { + ctx->family = AF_INET; + local = &ctx->local_ip4; + remote = &ctx->remote_ip4; + } + + err = inet_pton(ctx->family, local_ip, local); + if (CHECK(err != 1, "inet_pton", "local_ip failed\n")) + return 1; + + err = inet_pton(ctx->family, remote_ip, remote); + if (CHECK(err != 1, "inet_pton", "remote_ip failed\n")) + return 1; + + return 0; +} + static int send_byte(int fd) { ssize_t n; @@ -1020,18 +1051,27 @@ static void test_drop_on_reuseport(struct test_sk_lookup *skel) static void run_sk_assign(struct test_sk_lookup *skel, struct bpf_program *lookup_prog, - const char *listen_ip, const char *connect_ip) + const char *remote_ip, const char *local_ip) { - int client_fd, peer_fd, server_fds[MAX_SERVERS] = { -1 }; - struct bpf_link *lookup_link; + int server_fds[MAX_SERVERS] = { -1 }; + struct bpf_sk_lookup ctx; + __u64 server_cookie; int i, err; - lookup_link = attach_lookup_prog(lookup_prog); - if (!lookup_link) + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, + .ctx_in = &ctx, + .ctx_size_in = sizeof(ctx), + .ctx_out = &ctx, + .ctx_size_out = sizeof(ctx), + ); + + if (fill_sk_lookup_ctx(&ctx, local_ip, EXT_PORT, remote_ip, INT_PORT)) return; + ctx.protocol = IPPROTO_TCP; + for (i = 0; i < ARRAY_SIZE(server_fds); i++) { - server_fds[i] = make_server(SOCK_STREAM, listen_ip, 0, NULL); + server_fds[i] = make_server(SOCK_STREAM, local_ip, 0, NULL); if (server_fds[i] < 0) goto close_servers; @@ -1041,23 +1081,25 @@ static void run_sk_assign(struct test_sk_lookup *skel, goto close_servers; } - client_fd = make_client(SOCK_STREAM, connect_ip, EXT_PORT); - if (client_fd < 0) + server_cookie = socket_cookie(server_fds[SERVER_B]); + if (!server_cookie) + return; + + err = bpf_prog_test_run_opts(bpf_program__fd(lookup_prog), &opts); + if (CHECK(err, "test_run", "failed with error %d\n", errno)) + goto close_servers; + + if (CHECK(ctx.cookie == 0, "ctx.cookie", "no socket selected\n")) goto close_servers; - peer_fd = accept(server_fds[SERVER_B], NULL, NULL); - if (CHECK(peer_fd < 0, "accept", "failed\n")) - goto close_client; + CHECK(ctx.cookie != server_cookie, "ctx.cookie", + "selected sk %llu instead of %llu\n", ctx.cookie, server_cookie); - close(peer_fd); -close_client: - close(client_fd); close_servers: for (i = 0; i < ARRAY_SIZE(server_fds); i++) { if (server_fds[i] != -1) close(server_fds[i]); } - bpf_link__destroy(lookup_link); } static void run_sk_assign_v4(struct test_sk_lookup *skel, diff --git a/tools/testing/selftests/bpf/progs/test_sk_lookup.c b/tools/testing/selftests/bpf/progs/test_sk_lookup.c index 1032b292af5b..ac6f7f205e25 100644 --- a/tools/testing/selftests/bpf/progs/test_sk_lookup.c +++ b/tools/testing/selftests/bpf/progs/test_sk_lookup.c @@ -64,6 +64,10 @@ static const int PROG_DONE = 1; static const __u32 KEY_SERVER_A = SERVER_A; static const __u32 KEY_SERVER_B = SERVER_B; +static const __u16 SRC_PORT = bpf_htons(8008); +static const __u32 SRC_IP4 = IP4(127, 0, 0, 2); +static const __u32 SRC_IP6[] = IP6(0xfd000000, 0x0, 0x0, 0x00000002); + static const __u16 DST_PORT = 7007; /* Host byte order */ static const __u32 DST_IP4 = IP4(127, 0, 0, 1); static const __u32 DST_IP6[] = IP6(0xfd000000, 0x0, 0x0, 0x00000001); @@ -398,11 +402,12 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx) if (LSW(ctx->protocol, 0) != IPPROTO_TCP) return SK_DROP; - /* Narrow loads from remote_port field. Expect non-0 value. */ - if (LSB(ctx->remote_port, 0) == 0 && LSB(ctx->remote_port, 1) == 0 && - LSB(ctx->remote_port, 2) == 0 && LSB(ctx->remote_port, 3) == 0) + /* Narrow loads from remote_port field. Expect SRC_PORT. */ + if (LSB(ctx->remote_port, 0) != ((SRC_PORT >> 0) & 0xff) || + LSB(ctx->remote_port, 1) != ((SRC_PORT >> 8) & 0xff) || + LSB(ctx->remote_port, 2) != 0 || LSB(ctx->remote_port, 3) != 0) return SK_DROP; - if (LSW(ctx->remote_port, 0) == 0) + if (LSW(ctx->remote_port, 0) != SRC_PORT) return SK_DROP; /* Narrow loads from local_port field. Expect DST_PORT. */ @@ -415,11 +420,14 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx) /* Narrow loads from IPv4 fields */ if (v4) { - /* Expect non-0.0.0.0 in remote_ip4 */ - if (LSB(ctx->remote_ip4, 0) == 0 && LSB(ctx->remote_ip4, 1) == 0 && - LSB(ctx->remote_ip4, 2) == 0 && LSB(ctx->remote_ip4, 3) == 0) + /* Expect SRC_IP4 in remote_ip4 */ + if (LSB(ctx->remote_ip4, 0) != ((SRC_IP4 >> 0) & 0xff) || + LSB(ctx->remote_ip4, 1) != ((SRC_IP4 >> 8) & 0xff) || + LSB(ctx->remote_ip4, 2) != ((SRC_IP4 >> 16) & 0xff) || + LSB(ctx->remote_ip4, 3) != ((SRC_IP4 >> 24) & 0xff)) return SK_DROP; - if (LSW(ctx->remote_ip4, 0) == 0 && LSW(ctx->remote_ip4, 1) == 0) + if (LSW(ctx->remote_ip4, 0) != ((SRC_IP4 >> 0) & 0xffff) || + LSW(ctx->remote_ip4, 1) != ((SRC_IP4 >> 16) & 0xffff)) return SK_DROP; /* Expect DST_IP4 in local_ip4 */ @@ -448,20 +456,32 @@ int ctx_narrow_access(struct bpf_sk_lookup *ctx) /* Narrow loads from IPv6 fields */ if (!v4) { - /* Expect non-:: IP in remote_ip6 */ - if (LSB(ctx->remote_ip6[0], 0) == 0 && LSB(ctx->remote_ip6[0], 1) == 0 && - LSB(ctx->remote_ip6[0], 2) == 0 && LSB(ctx->remote_ip6[0], 3) == 0 && - LSB(ctx->remote_ip6[1], 0) == 0 && LSB(ctx->remote_ip6[1], 1) == 0 && - LSB(ctx->remote_ip6[1], 2) == 0 && LSB(ctx->remote_ip6[1], 3) == 0 && - LSB(ctx->remote_ip6[2], 0) == 0 && LSB(ctx->remote_ip6[2], 1) == 0 && - LSB(ctx->remote_ip6[2], 2) == 0 && LSB(ctx->remote_ip6[2], 3) == 0 && - LSB(ctx->remote_ip6[3], 0) == 0 && LSB(ctx->remote_ip6[3], 1) == 0 && - LSB(ctx->remote_ip6[3], 2) == 0 && LSB(ctx->remote_ip6[3], 3) == 0) + /* Expect SRC_IP6 in remote_ip6 */ + if (LSB(ctx->remote_ip6[0], 0) != ((SRC_IP6[0] >> 0) & 0xff) || + LSB(ctx->remote_ip6[0], 1) != ((SRC_IP6[0] >> 8) & 0xff) || + LSB(ctx->remote_ip6[0], 2) != ((SRC_IP6[0] >> 16) & 0xff) || + LSB(ctx->remote_ip6[0], 3) != ((SRC_IP6[0] >> 24) & 0xff) || + LSB(ctx->remote_ip6[1], 0) != ((SRC_IP6[1] >> 0) & 0xff) || + LSB(ctx->remote_ip6[1], 1) != ((SRC_IP6[1] >> 8) & 0xff) || + LSB(ctx->remote_ip6[1], 2) != ((SRC_IP6[1] >> 16) & 0xff) || + LSB(ctx->remote_ip6[1], 3) != ((SRC_IP6[1] >> 24) & 0xff) || + LSB(ctx->remote_ip6[2], 0) != ((SRC_IP6[2] >> 0) & 0xff) || + LSB(ctx->remote_ip6[2], 1) != ((SRC_IP6[2] >> 8) & 0xff) || + LSB(ctx->remote_ip6[2], 2) != ((SRC_IP6[2] >> 16) & 0xff) || + LSB(ctx->remote_ip6[2], 3) != ((SRC_IP6[2] >> 24) & 0xff) || + LSB(ctx->remote_ip6[3], 0) != ((SRC_IP6[3] >> 0) & 0xff) || + LSB(ctx->remote_ip6[3], 1) != ((SRC_IP6[3] >> 8) & 0xff) || + LSB(ctx->remote_ip6[3], 2) != ((SRC_IP6[3] >> 16) & 0xff) || + LSB(ctx->remote_ip6[3], 3) != ((SRC_IP6[3] >> 24) & 0xff)) return SK_DROP; - if (LSW(ctx->remote_ip6[0], 0) == 0 && LSW(ctx->remote_ip6[0], 1) == 0 && - LSW(ctx->remote_ip6[1], 0) == 0 && LSW(ctx->remote_ip6[1], 1) == 0 && - LSW(ctx->remote_ip6[2], 0) == 0 && LSW(ctx->remote_ip6[2], 1) == 0 && - LSW(ctx->remote_ip6[3], 0) == 0 && LSW(ctx->remote_ip6[3], 1) == 0) + if (LSW(ctx->remote_ip6[0], 0) != ((SRC_IP6[0] >> 0) & 0xffff) || + LSW(ctx->remote_ip6[0], 1) != ((SRC_IP6[0] >> 16) & 0xffff) || + LSW(ctx->remote_ip6[1], 0) != ((SRC_IP6[1] >> 0) & 0xffff) || + LSW(ctx->remote_ip6[1], 1) != ((SRC_IP6[1] >> 16) & 0xffff) || + LSW(ctx->remote_ip6[2], 0) != ((SRC_IP6[2] >> 0) & 0xffff) || + LSW(ctx->remote_ip6[2], 1) != ((SRC_IP6[2] >> 16) & 0xffff) || + LSW(ctx->remote_ip6[3], 0) != ((SRC_IP6[3] >> 0) & 0xffff) || + LSW(ctx->remote_ip6[3], 1) != ((SRC_IP6[3] >> 16) & 0xffff)) return SK_DROP; /* Expect DST_IP6 in local_ip6 */ if (LSB(ctx->local_ip6[0], 0) != ((DST_IP6[0] >> 0) & 0xff) || From patchwork Tue Feb 16 10:57:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenz Bauer X-Patchwork-Id: 12089825 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7156C433E0 for ; Tue, 16 Feb 2021 11:03:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9523E64DF0 for ; Tue, 16 Feb 2021 11:03:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230223AbhBPLCp (ORCPT ); Tue, 16 Feb 2021 06:02:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230139AbhBPLAk (ORCPT ); Tue, 16 Feb 2021 06:00:40 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B26E8C06121C for ; Tue, 16 Feb 2021 02:58:23 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id l12so12412023wry.2 for ; Tue, 16 Feb 2021 02:58:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0cxlavmTIp/zACJRiVyk8HpP0iu1cIzGXAbVh/+DaW8=; b=psWWGXhZG0sX29DCrH5g7znf9h/J9mzSMMEUHll4znSBD2SIgJQg5dnfGiy5AubW15 o8x5arIpflHOq2dOaIZoKMUJITZucX0rPHpJ4IXAyiEuVtPnyqUCfPZs4THHNVCe1aga ci3YzzLJXC+o3XwovwZ5iL9Pla+em8PzguanY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0cxlavmTIp/zACJRiVyk8HpP0iu1cIzGXAbVh/+DaW8=; b=JIVav+XlodlIGCpWU9MNcX3z6jBEJ3Jy2Ceb+3CYBM4ut0qYddGUuOLwP8t331ppX7 jmsBdJN1/LkfPRjbMo8m32ADQcLseiBoHcCcb8Wh/yQKegaJ0zq1zHjEue0bUEq/U0JM tcM3oYjwxDDpBl0Hph/7JuTj2gc52nuEtmatMqo/FiJRKaYjS9lu3y4RGyIMgriMDnzr jqdDipBK2sHIkNSgHaUvhDe5icO1NS8OZ9Ibv/7WdGjz4bG4lHia1covuhv/5vOrvdzR +bVnv9rFSu2wun5p9REocaC7PksA8lavw/QuXcDseTvzUuoYX0AwGAAqeO+P299wxHuD vURw== X-Gm-Message-State: AOAM530xUugCVGRMox5S7Wvp3LHNCOfj9d2qa6ytxDDUn7V3x1UnSsw4 Tf5bqNELUmed5T8TThgMYmwgjg== X-Google-Smtp-Source: ABdhPJwuZq/E2OsTx9xBGvIw9iUNdaB4ReccTUzvSxX4rEcvZuFONO/HooyK552+zygq5WnUiBCqng== X-Received: by 2002:adf:bb54:: with SMTP id x20mr23823050wrg.112.1613473102467; Tue, 16 Feb 2021 02:58:22 -0800 (PST) Received: from antares.lan (111.253.187.81.in-addr.arpa. [81.187.253.111]) by smtp.gmail.com with ESMTPSA id l1sm2820238wmi.48.2021.02.16.02.58.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 02:58:22 -0800 (PST) From: Lorenz Bauer To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, jakub@cloudflare.com Cc: kernel-team@cloudflare.com, bpf@vger.kernel.org, netdev@vger.kernel.org, Lorenz Bauer Subject: [PATCH bpf-next 8/8] selftests: bpf: check that PROG_TEST_RUN repeats as requested Date: Tue, 16 Feb 2021 10:57:13 +0000 Message-Id: <20210216105713.45052-9-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210216105713.45052-1-lmb@cloudflare.com> References: <20210216105713.45052-1-lmb@cloudflare.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Extend a simple prog_run test to check that PROG_TEST_RUN adheres to the requested repetitions. Convert it to use BPF skeleton. Signed-off-by: Lorenz Bauer --- .../selftests/bpf/prog_tests/prog_run_xattr.c | 51 +++++++++++++++---- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/prog_run_xattr.c b/tools/testing/selftests/bpf/prog_tests/prog_run_xattr.c index 935a294f049a..131d7f7eeb42 100644 --- a/tools/testing/selftests/bpf/prog_tests/prog_run_xattr.c +++ b/tools/testing/selftests/bpf/prog_tests/prog_run_xattr.c @@ -2,12 +2,31 @@ #include #include -void test_prog_run_xattr(void) +#include "test_pkt_access.skel.h" + +static const __u32 duration; + +static void check_run_cnt(int prog_fd, __u64 run_cnt) { - const char *file = "./test_pkt_access.o"; - struct bpf_object *obj; - char buf[10]; + struct bpf_prog_info info = {}; + __u32 info_len = sizeof(info); int err; + + err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len); + if (CHECK(err, "get_prog_info", "failed to get bpf_prog_info for fd %d\n", prog_fd)) + return; + + CHECK(run_cnt != info.run_cnt, "run_cnt", + "incorrect number of repetitions, want %llu have %llu\n", run_cnt, info.run_cnt); +} + +void test_prog_run_xattr(void) +{ + struct test_pkt_access *skel; + int err, stats_fd = -1; + char buf[10] = {}; + __u64 run_cnt = 0; + struct bpf_prog_test_run_attr tattr = { .repeat = 1, .data_in = &pkt_v4, @@ -16,12 +35,15 @@ void test_prog_run_xattr(void) .data_size_out = 5, }; - err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, - &tattr.prog_fd); - if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno)) + stats_fd = bpf_enable_stats(BPF_STATS_RUN_TIME); + if (CHECK_ATTR(stats_fd < 0, "enable_stats", "failed %d\n", errno)) return; - memset(buf, 0, sizeof(buf)); + skel = test_pkt_access__open_and_load(); + if (CHECK_ATTR(!skel, "open_and_load", "failed\n")) + goto cleanup; + + tattr.prog_fd = bpf_program__fd(skel->progs.test_pkt_access); err = bpf_prog_test_run_xattr(&tattr); CHECK_ATTR(err != -1 || errno != ENOSPC || tattr.retval, "run", @@ -34,8 +56,12 @@ void test_prog_run_xattr(void) CHECK_ATTR(buf[5] != 0, "overflow", "BPF_PROG_TEST_RUN ignored size hint\n"); + run_cnt += tattr.repeat; + check_run_cnt(tattr.prog_fd, run_cnt); + tattr.data_out = NULL; tattr.data_size_out = 0; + tattr.repeat = 2; errno = 0; err = bpf_prog_test_run_xattr(&tattr); @@ -46,5 +72,12 @@ void test_prog_run_xattr(void) err = bpf_prog_test_run_xattr(&tattr); CHECK_ATTR(err != -EINVAL, "run_wrong_size_out", "err %d\n", err); - bpf_object__close(obj); + run_cnt += tattr.repeat; + check_run_cnt(tattr.prog_fd, run_cnt); + +cleanup: + if (skel) + test_pkt_access__destroy(skel); + if (stats_fd != -1) + close(stats_fd); }