From patchwork Wed Oct 28 01:47:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11864141 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=-9.6 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 9DFA0C4363A for ; Wed, 28 Oct 2020 23:48:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 36CDA207CD for ; Wed, 28 Oct 2020 23:48:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GYiwevX/" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729187AbgJ1Xsv (ORCPT ); Wed, 28 Oct 2020 19:48:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389258AbgJ1Xsu (ORCPT ); Wed, 28 Oct 2020 19:48:50 -0400 Received: from mail-vs1-xe2c.google.com (mail-vs1-xe2c.google.com [IPv6:2607:f8b0:4864:20::e2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FCACC0613CF; Wed, 28 Oct 2020 16:48:50 -0700 (PDT) Received: by mail-vs1-xe2c.google.com with SMTP id n18so558088vsl.2; Wed, 28 Oct 2020 16:48:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=L7flsRsQxIpBLiql8dmiQFwWEZHi3eNldjPGgTqCKcw=; b=GYiwevX/H0DvZ/srmuL+lI0XJJ5/J34TCvRWFvZSt77kbuLc/jxBxCYG066nnvPRNl 6dUljXutH2zENIlFRVcvg5MdNqXbyefoQVd78DqAIjqfF/CMgLz2tgHpMBERQQY/fgZM jrIz0UwwSDH5BZHOyyrB/eD4AMOVDnsGIe3Qzi7SUq+5/KSt1aca6HII4ntouJLafTq7 1HYgyzEC5f9WvqSaGrzrXbPdmueBlx8770yGfTTRrcAv7WIJuoTOOVDcCBUpgSJ9jIk8 8wnYUCA2MJnURUokCw1sK8obKfBF6OjNdLAfpYW98QinXFuJABDktcYQW5IBTdobjGCx HIxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=L7flsRsQxIpBLiql8dmiQFwWEZHi3eNldjPGgTqCKcw=; b=dRnr2/oN0z8JUjBCihMiZhq+va5pLfiZ5c+s9Zyz3/yd5+62pRIpDQ9YnSP+juGE+l 1Z037li0gh3B9frQ+W6g2gJbYWGmfnntGwQzNLIujL0NQFb7RMQ8mDQP21CA96an1A3B ANc+I9mlSVQIRQOEPOXYSHLvR8KiRuhHc9EA3m9PXnYs9IPA8wgIM6bLO41FklU1PgEg EMNqni0P4cClXkAn0daMsdUBNG8o0LDYumabTT10lq2ay7PuYadJKM1LCoFn9n53k4qm B9e81hRMozI7cfv9ChwahmE8ooHJk5TNXfJ7zrwZNP21iKo7Mfk8ifSE9D+wpUO2B6KO QGDQ== X-Gm-Message-State: AOAM531me7gCT/kesrM9hhDZ5+CiTY/rJcsy2yNWVZcrZ0YeNQHb62/f FtVisBTNVXxzeckesjSfdA0BF10bylIq1g== X-Google-Smtp-Source: ABdhPJzHUDRHU4/MFY9RcnJq7v8pC0Zu8Xz8EH8Eiz/ye0ylVUT/gfMQRz3lfyVSuYibm5bbLwMSAw== X-Received: by 2002:a0c:8d05:: with SMTP id r5mr5509623qvb.31.1603849627928; Tue, 27 Oct 2020 18:47:07 -0700 (PDT) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id h125sm2107799qkc.36.2020.10.27.18.47.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Oct 2020 18:47:07 -0700 (PDT) Subject: [bpf-next PATCH 1/4] selftests/bpf: Move test_tcppbf_user into test_progs From: Alexander Duyck To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, kafai@fb.com, john.fastabend@gmail.com, kernel-team@fb.com, netdev@vger.kernel.org, edumazet@google.com, brakmo@fb.com, alexanderduyck@fb.com Date: Tue, 27 Oct 2020 18:47:05 -0700 Message-ID: <160384962569.698509.4528110378641773523.stgit@localhost.localdomain> In-Reply-To: <160384954046.698509.132709669068189999.stgit@localhost.localdomain> References: <160384954046.698509.132709669068189999.stgit@localhost.localdomain> User-Agent: StGit/0.23 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexander Duyck Recently a bug was missed due to the fact that test_tcpbpf_user is not a part of test_progs. In order to prevent similar issues in the future move the test functionality into test_progs. By doing this we can make certain that it is a part of standard testing and will not be overlooked. As a part of moving the functionality into test_progs it is necessary to integrate with the test_progs framework and to drop any redundant code. This patch: 1. Cleans up the include headers 2. Dropped a duplicate definition of bpf_find_map 3. Replaced printf calls with fprintf to stderr 4. Renamed main to test_tcpbpf_user 5. Dropped return value in favor of CHECK calls to check for errors Signed-off-by: Alexander Duyck --- tools/testing/selftests/bpf/Makefile | 3 .../testing/selftests/bpf/prog_tests/tcpbpf_user.c | 138 +++++++++++++++++ tools/testing/selftests/bpf/test_tcpbpf_user.c | 165 -------------------- 3 files changed, 139 insertions(+), 167 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c delete mode 100644 tools/testing/selftests/bpf/test_tcpbpf_user.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 542768f5195b..50e5b18fc455 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -32,7 +32,7 @@ LDLIBS += -lcap -lelf -lz -lrt -lpthread # Order correspond to 'make run_tests' order TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \ - test_verifier_log test_dev_cgroup test_tcpbpf_user \ + test_verifier_log test_dev_cgroup \ test_sock test_sockmap get_cgroup_id_user test_socket_cookie \ test_cgroup_storage \ test_netcnt test_tcpnotify_user test_sysctl \ @@ -163,7 +163,6 @@ $(OUTPUT)/test_sock: cgroup_helpers.c $(OUTPUT)/test_sock_addr: cgroup_helpers.c $(OUTPUT)/test_socket_cookie: cgroup_helpers.c $(OUTPUT)/test_sockmap: cgroup_helpers.c -$(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c $(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c $(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c $(OUTPUT)/test_cgroup_storage: cgroup_helpers.c diff --git a/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c b/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c new file mode 100644 index 000000000000..5becab8b04e3 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c @@ -0,0 +1,138 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +#include "test_tcpbpf.h" +#include "cgroup_helpers.h" + +#define CG_NAME "/tcpbpf-user-test" + +/* 3 comes from one listening socket + both ends of the connection */ +#define EXPECTED_CLOSE_EVENTS 3 + +#define EXPECT_EQ(expected, actual, fmt) \ + do { \ + if ((expected) != (actual)) { \ + fprintf(stderr, " Value of: " #actual "\n" \ + " Actual: %" fmt "\n" \ + " Expected: %" fmt "\n", \ + (actual), (expected)); \ + ret--; \ + } \ + } while (0) + +int verify_result(const struct tcpbpf_globals *result) +{ + __u32 expected_events; + int ret = 0; + + expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) | + (1 << BPF_SOCK_OPS_RWND_INIT) | + (1 << BPF_SOCK_OPS_TCP_CONNECT_CB) | + (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) | + (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) | + (1 << BPF_SOCK_OPS_NEEDS_ECN) | + (1 << BPF_SOCK_OPS_STATE_CB) | + (1 << BPF_SOCK_OPS_TCP_LISTEN_CB)); + + EXPECT_EQ(expected_events, result->event_map, "#" PRIx32); + EXPECT_EQ(501ULL, result->bytes_received, "llu"); + EXPECT_EQ(1002ULL, result->bytes_acked, "llu"); + EXPECT_EQ(1, result->data_segs_in, PRIu32); + EXPECT_EQ(1, result->data_segs_out, PRIu32); + EXPECT_EQ(0x80, result->bad_cb_test_rv, PRIu32); + EXPECT_EQ(0, result->good_cb_test_rv, PRIu32); + EXPECT_EQ(1, result->num_listen, PRIu32); + EXPECT_EQ(EXPECTED_CLOSE_EVENTS, result->num_close_events, PRIu32); + + return ret; +} + +int verify_sockopt_result(int sock_map_fd) +{ + __u32 key = 0; + int ret = 0; + int res; + int rv; + + /* check setsockopt for SAVE_SYN */ + rv = bpf_map_lookup_elem(sock_map_fd, &key, &res); + EXPECT_EQ(0, rv, "d"); + EXPECT_EQ(0, res, "d"); + key = 1; + /* check getsockopt for SAVED_SYN */ + rv = bpf_map_lookup_elem(sock_map_fd, &key, &res); + EXPECT_EQ(0, rv, "d"); + EXPECT_EQ(1, res, "d"); + return ret; +} + +void test_tcpbpf_user(void) +{ + const char *file = "test_tcpbpf_kern.o"; + int prog_fd, map_fd, sock_map_fd; + struct tcpbpf_globals g = {0}; + struct bpf_object *obj; + int cg_fd = -1; + int retry = 10; + __u32 key = 0; + int rv; + + cg_fd = cgroup_setup_and_join(CG_NAME); + if (CHECK_FAIL(cg_fd < 0)) + goto err; + + if (CHECK_FAIL(bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd))) { + fprintf(stderr, "FAILED: load_bpf_file failed for: %s\n", file); + goto err; + } + + rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0); + if (CHECK_FAIL(rv)) { + fprintf(stderr, "FAILED: bpf_prog_attach: %d (%s)\n", + errno, strerror(errno)); + goto err; + } + + if (CHECK_FAIL(system("./tcp_server.py"))) { + fprintf(stderr, "FAILED: TCP server\n"); + goto err; + } + + map_fd = bpf_find_map(__func__, obj, "global_map"); + if (CHECK_FAIL(map_fd < 0)) + goto err; + + sock_map_fd = bpf_find_map(__func__, obj, "sockopt_results"); + if (CHECK_FAIL(sock_map_fd < 0)) + goto err; + +retry_lookup: + rv = bpf_map_lookup_elem(map_fd, &key, &g); + if (CHECK_FAIL(rv != 0)) { + fprintf(stderr, "FAILED: bpf_map_lookup_elem returns %d\n", rv); + goto err; + } + + if (g.num_close_events != EXPECTED_CLOSE_EVENTS && retry--) { + fprintf(stderr, + "Unexpected number of close events (%d), retrying!\n", + g.num_close_events); + usleep(100); + goto retry_lookup; + } + + if (CHECK_FAIL(verify_result(&g))) { + fprintf(stderr, "FAILED: Wrong stats\n"); + goto err; + } + + if (CHECK_FAIL(verify_sockopt_result(sock_map_fd))) { + fprintf(stderr, "FAILED: Wrong sockopt stats\n"); + goto err; + } +err: + bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS); + close(cg_fd); + cleanup_cgroup_environment(); +} diff --git a/tools/testing/selftests/bpf/test_tcpbpf_user.c b/tools/testing/selftests/bpf/test_tcpbpf_user.c deleted file mode 100644 index 74a9e49988b6..000000000000 --- a/tools/testing/selftests/bpf/test_tcpbpf_user.c +++ /dev/null @@ -1,165 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "bpf_rlimit.h" -#include "bpf_util.h" -#include "cgroup_helpers.h" - -#include "test_tcpbpf.h" - -/* 3 comes from one listening socket + both ends of the connection */ -#define EXPECTED_CLOSE_EVENTS 3 - -#define EXPECT_EQ(expected, actual, fmt) \ - do { \ - if ((expected) != (actual)) { \ - printf(" Value of: " #actual "\n" \ - " Actual: %" fmt "\n" \ - " Expected: %" fmt "\n", \ - (actual), (expected)); \ - ret--; \ - } \ - } while (0) - -int verify_result(const struct tcpbpf_globals *result) -{ - __u32 expected_events; - int ret = 0; - - expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) | - (1 << BPF_SOCK_OPS_RWND_INIT) | - (1 << BPF_SOCK_OPS_TCP_CONNECT_CB) | - (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) | - (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) | - (1 << BPF_SOCK_OPS_NEEDS_ECN) | - (1 << BPF_SOCK_OPS_STATE_CB) | - (1 << BPF_SOCK_OPS_TCP_LISTEN_CB)); - - EXPECT_EQ(expected_events, result->event_map, "#" PRIx32); - EXPECT_EQ(501ULL, result->bytes_received, "llu"); - EXPECT_EQ(1002ULL, result->bytes_acked, "llu"); - EXPECT_EQ(1, result->data_segs_in, PRIu32); - EXPECT_EQ(1, result->data_segs_out, PRIu32); - EXPECT_EQ(0x80, result->bad_cb_test_rv, PRIu32); - EXPECT_EQ(0, result->good_cb_test_rv, PRIu32); - EXPECT_EQ(1, result->num_listen, PRIu32); - EXPECT_EQ(EXPECTED_CLOSE_EVENTS, result->num_close_events, PRIu32); - - return ret; -} - -int verify_sockopt_result(int sock_map_fd) -{ - __u32 key = 0; - int ret = 0; - int res; - int rv; - - /* check setsockopt for SAVE_SYN */ - rv = bpf_map_lookup_elem(sock_map_fd, &key, &res); - EXPECT_EQ(0, rv, "d"); - EXPECT_EQ(0, res, "d"); - key = 1; - /* check getsockopt for SAVED_SYN */ - rv = bpf_map_lookup_elem(sock_map_fd, &key, &res); - EXPECT_EQ(0, rv, "d"); - EXPECT_EQ(1, res, "d"); - return ret; -} - -static int bpf_find_map(const char *test, struct bpf_object *obj, - const char *name) -{ - struct bpf_map *map; - - map = bpf_object__find_map_by_name(obj, name); - if (!map) { - printf("%s:FAIL:map '%s' not found\n", test, name); - return -1; - } - return bpf_map__fd(map); -} - -int main(int argc, char **argv) -{ - const char *file = "test_tcpbpf_kern.o"; - int prog_fd, map_fd, sock_map_fd; - struct tcpbpf_globals g = {0}; - const char *cg_path = "/foo"; - int error = EXIT_FAILURE; - struct bpf_object *obj; - int cg_fd = -1; - int retry = 10; - __u32 key = 0; - int rv; - - cg_fd = cgroup_setup_and_join(cg_path); - if (cg_fd < 0) - goto err; - - if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) { - printf("FAILED: load_bpf_file failed for: %s\n", file); - goto err; - } - - rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0); - if (rv) { - printf("FAILED: bpf_prog_attach: %d (%s)\n", - error, strerror(errno)); - goto err; - } - - if (system("./tcp_server.py")) { - printf("FAILED: TCP server\n"); - goto err; - } - - map_fd = bpf_find_map(__func__, obj, "global_map"); - if (map_fd < 0) - goto err; - - sock_map_fd = bpf_find_map(__func__, obj, "sockopt_results"); - if (sock_map_fd < 0) - goto err; - -retry_lookup: - rv = bpf_map_lookup_elem(map_fd, &key, &g); - if (rv != 0) { - printf("FAILED: bpf_map_lookup_elem returns %d\n", rv); - goto err; - } - - if (g.num_close_events != EXPECTED_CLOSE_EVENTS && retry--) { - printf("Unexpected number of close events (%d), retrying!\n", - g.num_close_events); - usleep(100); - goto retry_lookup; - } - - if (verify_result(&g)) { - printf("FAILED: Wrong stats\n"); - goto err; - } - - if (verify_sockopt_result(sock_map_fd)) { - printf("FAILED: Wrong sockopt stats\n"); - goto err; - } - - printf("PASSED!\n"); - error = 0; -err: - bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS); - close(cg_fd); - cleanup_cgroup_environment(); - return error; -} From patchwork Wed Oct 28 01:47:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11863341 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=-9.6 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 A1F7EC4363A for ; Wed, 28 Oct 2020 21:52:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 38AF1247E9 for ; Wed, 28 Oct 2020 21:52:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="u7r1f/kz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728267AbgJ1VwE (ORCPT ); Wed, 28 Oct 2020 17:52:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728236AbgJ1VwA (ORCPT ); Wed, 28 Oct 2020 17:52:00 -0400 Received: from mail-vk1-xa44.google.com (mail-vk1-xa44.google.com [IPv6:2607:f8b0:4864:20::a44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65E01C0613CF; Wed, 28 Oct 2020 14:52:00 -0700 (PDT) Received: by mail-vk1-xa44.google.com with SMTP id y10so261177vkl.5; Wed, 28 Oct 2020 14:52:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=YIOUqWyBZIfavDIAu4dN9j2zqYJXRubizG/6KqaU4Tc=; b=u7r1f/kzAgex8NQbV5A+cSGwPBWrxSgYHYVYq3UTXzrwJ4YU7nfo8dsp5WcVZcEfCI GulNQZmgev3dGGvhpv4oYCC464jFTl/7vW9BIQKFLSAQTmPT91nIdCkC6Nd+CADn8mPR 2IIMGprRctKbhZzAVQgXCc+dNRPfaqGCwoS7cMqAW5UW16E6AsIOEyzJ9XRGxHXzUNVg toRtxgmDmA4xsib5NYUFPHkAZs3Rlh+KLhIbuGqzwOAAORcSc5F/NLpsEbMxu0P8C7kD w/4UfmzCkvd+K+HmcKsM9z5mcU5jRFdBATK99B/APFNKVp4sFORJlGIlbwq9dNIrTjru ks1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=YIOUqWyBZIfavDIAu4dN9j2zqYJXRubizG/6KqaU4Tc=; b=mXpW5pYE8VcNoF1+rKje5RPHPvnTb/3gmv25vYvYadnmXthio5zpZjY2EKDywHzJGE F7/re78ognjxeQlwPCrdgX+idH1a72lzmIg2/K5rG8Wnlw5SBQ+DViUOj7y6W0oiRjpl ad7khHQLXPCS4X03PE3BTLsCod4br4+LzrjBhD72ehKo3hBahQcO+pB3NELXEXwwkhNl Cg5jyzBr/1arrAVX6rMlvqAcVmNwzflf1v2a/ed5Z7TAZteJZu01ZD7pAS6JGxFmFjeV 5ZIjvauW/+BYb4agKckZE2Y/bT9ZAV4ad60PIQVKglbTXNj88BVpgn7PIVgw1vM2+GdK gC2g== X-Gm-Message-State: AOAM531mACDltVVRkiuj+h5nXrFxU6YWUOvOnseUPFQs9TBmSn00sKbk 8s2ZeFgWgRfK9B/F9aGZ15ytin2EsLjVkg== X-Google-Smtp-Source: ABdhPJzoSPlqBzE395zc0+3gCtAIjluHEduOK2pyLmq6FNkdc50liDSOjjTgzYbtBsYk4mWYTOtnEA== X-Received: by 2002:a0c:9e53:: with SMTP id z19mr5356512qve.23.1603849635467; Tue, 27 Oct 2020 18:47:15 -0700 (PDT) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id 185sm2080485qke.16.2020.10.27.18.47.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Oct 2020 18:47:14 -0700 (PDT) Subject: [bpf-next PATCH 2/4] selftests/bpf: Drop python client/server in favor of threads From: Alexander Duyck To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, kafai@fb.com, john.fastabend@gmail.com, kernel-team@fb.com, netdev@vger.kernel.org, edumazet@google.com, brakmo@fb.com, alexanderduyck@fb.com Date: Tue, 27 Oct 2020 18:47:13 -0700 Message-ID: <160384963313.698509.13129692731727238158.stgit@localhost.localdomain> In-Reply-To: <160384954046.698509.132709669068189999.stgit@localhost.localdomain> References: <160384954046.698509.132709669068189999.stgit@localhost.localdomain> User-Agent: StGit/0.23 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexander Duyck Drop the tcp_client/server.py files in favor of using a client and server thread within the test case. Specifically we spawn a new thread to play the role of the server, and the main testing thread plays the role of client. Doing this we are able to reduce overhead since we don't have two python workers possibly floating around. In addition we don't have to worry about synchronization issues and as such the retry loop waiting for the threads to close the sockets can be dropped as we will have already closed the sockets in the local executable and synchronized the server thread. Signed-off-by: Alexander Duyck --- .../testing/selftests/bpf/prog_tests/tcpbpf_user.c | 125 +++++++++++++++++--- tools/testing/selftests/bpf/tcp_client.py | 50 -------- tools/testing/selftests/bpf/tcp_server.py | 80 ------------- 3 files changed, 107 insertions(+), 148 deletions(-) delete mode 100755 tools/testing/selftests/bpf/tcp_client.py delete mode 100755 tools/testing/selftests/bpf/tcp_server.py diff --git a/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c b/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c index 5becab8b04e3..71ab82e37eb7 100644 --- a/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c +++ b/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c @@ -1,14 +1,65 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include #include "test_tcpbpf.h" #include "cgroup_helpers.h" +#define LO_ADDR6 "::1" #define CG_NAME "/tcpbpf-user-test" -/* 3 comes from one listening socket + both ends of the connection */ -#define EXPECTED_CLOSE_EVENTS 3 +static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER; + +static void *server_thread(void *arg) +{ + struct sockaddr_storage addr; + socklen_t len = sizeof(addr); + int fd = *(int *)arg; + char buf[1000]; + int client_fd; + int err = 0; + int i; + + err = listen(fd, 1); + + pthread_mutex_lock(&server_started_mtx); + pthread_cond_signal(&server_started); + pthread_mutex_unlock(&server_started_mtx); + + if (err < 0) { + perror("Failed to listen on socket"); + err = errno; + goto err; + } + + client_fd = accept(fd, (struct sockaddr *)&addr, &len); + if (client_fd < 0) { + perror("Failed to accept client"); + err = errno; + goto err; + } + + if (recv(client_fd, buf, 1000, 0) < 1000) { + perror("failed/partial recv"); + err = errno; + goto out_clean; + } + + for (i = 0; i < 500; i++) + buf[i] = '.'; + + if (send(client_fd, buf, 500, 0) < 500) { + perror("failed/partial send"); + err = errno; + goto out_clean; + } +out_clean: + close(client_fd); +err: + return (void *)(long)err; +} #define EXPECT_EQ(expected, actual, fmt) \ do { \ @@ -43,7 +94,9 @@ int verify_result(const struct tcpbpf_globals *result) EXPECT_EQ(0x80, result->bad_cb_test_rv, PRIu32); EXPECT_EQ(0, result->good_cb_test_rv, PRIu32); EXPECT_EQ(1, result->num_listen, PRIu32); - EXPECT_EQ(EXPECTED_CLOSE_EVENTS, result->num_close_events, PRIu32); + + /* 3 comes from one listening socket + both ends of the connection */ + EXPECT_EQ(3, result->num_close_events, PRIu32); return ret; } @@ -67,6 +120,52 @@ int verify_sockopt_result(int sock_map_fd) return ret; } +static int run_test(void) +{ + int server_fd, client_fd; + void *server_err; + char buf[1000]; + pthread_t tid; + int err = -1; + int i; + + server_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0); + if (CHECK_FAIL(server_fd < 0)) + return err; + + pthread_mutex_lock(&server_started_mtx); + if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread, + (void *)&server_fd))) + goto close_server_fd; + + pthread_cond_wait(&server_started, &server_started_mtx); + pthread_mutex_unlock(&server_started_mtx); + + client_fd = connect_to_fd(server_fd, 0); + if (client_fd < 0) + goto close_server_fd; + + for (i = 0; i < 1000; i++) + buf[i] = '+'; + + if (CHECK_FAIL(send(client_fd, buf, 1000, 0) < 1000)) + goto close_client_fd; + + if (CHECK_FAIL(recv(client_fd, buf, 500, 0) < 500)) + goto close_client_fd; + + pthread_join(tid, &server_err); + + err = (int)(long)server_err; + CHECK_FAIL(err); + +close_client_fd: + close(client_fd); +close_server_fd: + close(server_fd); + return err; +} + void test_tcpbpf_user(void) { const char *file = "test_tcpbpf_kern.o"; @@ -74,7 +173,6 @@ void test_tcpbpf_user(void) struct tcpbpf_globals g = {0}; struct bpf_object *obj; int cg_fd = -1; - int retry = 10; __u32 key = 0; int rv; @@ -94,11 +192,6 @@ void test_tcpbpf_user(void) goto err; } - if (CHECK_FAIL(system("./tcp_server.py"))) { - fprintf(stderr, "FAILED: TCP server\n"); - goto err; - } - map_fd = bpf_find_map(__func__, obj, "global_map"); if (CHECK_FAIL(map_fd < 0)) goto err; @@ -107,21 +200,17 @@ void test_tcpbpf_user(void) if (CHECK_FAIL(sock_map_fd < 0)) goto err; -retry_lookup: + if (run_test()) { + fprintf(stderr, "FAILED: TCP server\n"); + goto err; + } + rv = bpf_map_lookup_elem(map_fd, &key, &g); if (CHECK_FAIL(rv != 0)) { fprintf(stderr, "FAILED: bpf_map_lookup_elem returns %d\n", rv); goto err; } - if (g.num_close_events != EXPECTED_CLOSE_EVENTS && retry--) { - fprintf(stderr, - "Unexpected number of close events (%d), retrying!\n", - g.num_close_events); - usleep(100); - goto retry_lookup; - } - if (CHECK_FAIL(verify_result(&g))) { fprintf(stderr, "FAILED: Wrong stats\n"); goto err; diff --git a/tools/testing/selftests/bpf/tcp_client.py b/tools/testing/selftests/bpf/tcp_client.py deleted file mode 100755 index bfff82be3fc1..000000000000 --- a/tools/testing/selftests/bpf/tcp_client.py +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env python3 -# -# SPDX-License-Identifier: GPL-2.0 -# - -import sys, os, os.path, getopt -import socket, time -import subprocess -import select - -def read(sock, n): - buf = b'' - while len(buf) < n: - rem = n - len(buf) - try: s = sock.recv(rem) - except (socket.error) as e: return b'' - buf += s - return buf - -def send(sock, s): - total = len(s) - count = 0 - while count < total: - try: n = sock.send(s) - except (socket.error) as e: n = 0 - if n == 0: - return count; - count += n - return count - - -serverPort = int(sys.argv[1]) - -# create active socket -sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) -try: - sock.connect(('::1', serverPort)) -except socket.error as e: - sys.exit(1) - -buf = b'' -n = 0 -while n < 1000: - buf += b'+' - n += 1 - -sock.settimeout(1); -n = send(sock, buf) -n = read(sock, 500) -sys.exit(0) diff --git a/tools/testing/selftests/bpf/tcp_server.py b/tools/testing/selftests/bpf/tcp_server.py deleted file mode 100755 index 42ab8882f00f..000000000000 --- a/tools/testing/selftests/bpf/tcp_server.py +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/env python3 -# -# SPDX-License-Identifier: GPL-2.0 -# - -import sys, os, os.path, getopt -import socket, time -import subprocess -import select - -def read(sock, n): - buf = b'' - while len(buf) < n: - rem = n - len(buf) - try: s = sock.recv(rem) - except (socket.error) as e: return b'' - buf += s - return buf - -def send(sock, s): - total = len(s) - count = 0 - while count < total: - try: n = sock.send(s) - except (socket.error) as e: n = 0 - if n == 0: - return count; - count += n - return count - - -SERVER_PORT = 12877 -MAX_PORTS = 2 - -serverPort = SERVER_PORT -serverSocket = None - -# create passive socket -serverSocket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) - -try: serverSocket.bind(('::1', 0)) -except socket.error as msg: - print('bind fails: ' + str(msg)) - -sn = serverSocket.getsockname() -serverPort = sn[1] - -cmdStr = ("./tcp_client.py %d &") % (serverPort) -os.system(cmdStr) - -buf = b'' -n = 0 -while n < 500: - buf += b'.' - n += 1 - -serverSocket.listen(MAX_PORTS) -readList = [serverSocket] - -while True: - readyRead, readyWrite, inError = \ - select.select(readList, [], [], 2) - - if len(readyRead) > 0: - waitCount = 0 - for sock in readyRead: - if sock == serverSocket: - (clientSocket, address) = serverSocket.accept() - address = str(address[0]) - readList.append(clientSocket) - else: - sock.settimeout(1); - s = read(sock, 1000) - n = send(sock, buf) - sock.close() - serverSocket.close() - sys.exit(0) - else: - print('Select timeout!') - sys.exit(1) From patchwork Wed Oct 28 01:47:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11864429 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=-9.6 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 B23DDC55179 for ; Thu, 29 Oct 2020 00:35:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5A25520809 for ; Thu, 29 Oct 2020 00:35:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="g3SD6DYk" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732132AbgJ2Aff (ORCPT ); Wed, 28 Oct 2020 20:35:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731908AbgJ2Afa (ORCPT ); Wed, 28 Oct 2020 20:35:30 -0400 Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 808F6C0613CF; Wed, 28 Oct 2020 17:35:30 -0700 (PDT) Received: by mail-qt1-x843.google.com with SMTP id c5so884002qtw.3; Wed, 28 Oct 2020 17:35:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=/SBE/N6wC5pb5zMXd0P9mHhhKJmvr+mqaknVNcM8uzs=; b=g3SD6DYkK8LyNocK567KMX8sX1PJCKtQY6OCyqMOJVPSteHKrWF8UYe4YHaEp5/stR 0CZ0Ypp/qdZ0b2djhecL6y+Q3NRjVeZTG2KBk4xyxO0O5LnRrner9rCU3mtk2XYT8ck9 8G+qjTV07v9/WTwmu/f+ZOmzexcLbLz6o9mdTVT9IKsAn1WKJS3OEzus/sokyJINzml3 FHdpiEr03Q/0hBMgKfuCK9BPpL+caPRx44Rx3ypYaQ47r10YUG47kLejtLScU8wOJCA4 LVPlQj/DItvIyOaJ+qHzzBSOl5EWlhEILTYSvJkWWAx34WVLewD6uZ+IxCOX12sdsDeU 37Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=/SBE/N6wC5pb5zMXd0P9mHhhKJmvr+mqaknVNcM8uzs=; b=da1XPeaTnUGHqULWsDAgLzBDg4doY1oBFHc0LlG0ABDcE4qAbfE+aJbSNAqPSjbHTy Z45aUINxmXtWolkhNtsSavCfp6u1ihL0GN+wwmm/MWrEdRNeYKUr7luGp+cfVWb3+76H lPC6TnJnGxvT9IAGG1jlweCdGBDpuhR27q7GPVAcY6+Z56OHBCbB7jojwlYKNAR0YHCN yYWUXelMqUMD5SM31ArEzF/FOKwUZ9FRO1FeLP9hc4wh181yCpmb7PdQ8YHKTlMbL7cY dG31JJU2qU1Z1isKTqhq0quS1Y/D2nJscS8jYiAbK0+ZLM48wZtFSLU3rUhtZXej/4tA oodQ== X-Gm-Message-State: AOAM530x9STZsGo/y1w2uMhD+JNTFoboQTzLbp6keZHeeQz2dTnlRM1h RhXj1AtiALb6qImiedEUEgMlD9TmG/1edw== X-Google-Smtp-Source: ABdhPJyjndBMQ8ltWfDHE9gHyFeddGorj/Lw8KM28vIkcT0oNNZsPnPPzLDp11TQaiA8eTfCv+GS5Q== X-Received: by 2002:ac8:2bf7:: with SMTP id n52mr3975768qtn.164.1603849642858; Tue, 27 Oct 2020 18:47:22 -0700 (PDT) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id d10sm2040306qkl.73.2020.10.27.18.47.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Oct 2020 18:47:22 -0700 (PDT) Subject: [bpf-next PATCH 3/4] selftests/bpf: Replace EXPECT_EQ with ASSERT_EQ and refactor verify_results From: Alexander Duyck To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, kafai@fb.com, john.fastabend@gmail.com, kernel-team@fb.com, netdev@vger.kernel.org, edumazet@google.com, brakmo@fb.com, alexanderduyck@fb.com Date: Tue, 27 Oct 2020 18:47:20 -0700 Message-ID: <160384964061.698509.7299229987872061758.stgit@localhost.localdomain> In-Reply-To: <160384954046.698509.132709669068189999.stgit@localhost.localdomain> References: <160384954046.698509.132709669068189999.stgit@localhost.localdomain> User-Agent: StGit/0.23 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexander Duyck There is already logic in test_progs.h for asserting that a value is expected to be another value. So instead of reinventing it we should just make use of ASSERT_EQ in tcpbpf_user.c. This will allow for better debugging and integrates much more closely with the test_progs framework. In addition we can refactor the code a bit to merge together the two verify functions and tie them together into a single function. Doing this helps to clean the code up a bit and makes it more readable as all the verification is now done in one function. Lastly we can relocate the verification to the end of the run_test since it is logically part of the test itself. With this we can drop the need for a return value from run_test since verification becomes the last step of the call and then immediately following is the tear down of the test setup. Signed-off-by: Alexander Duyck --- .../testing/selftests/bpf/prog_tests/tcpbpf_user.c | 119 ++++++++------------ 1 file changed, 46 insertions(+), 73 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c b/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c index 71ab82e37eb7..4e1190894e1e 100644 --- a/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c +++ b/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c @@ -11,6 +11,7 @@ static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER; +static __u32 duration; static void *server_thread(void *arg) { @@ -61,66 +62,57 @@ static void *server_thread(void *arg) return (void *)(long)err; } -#define EXPECT_EQ(expected, actual, fmt) \ - do { \ - if ((expected) != (actual)) { \ - fprintf(stderr, " Value of: " #actual "\n" \ - " Actual: %" fmt "\n" \ - " Expected: %" fmt "\n", \ - (actual), (expected)); \ - ret--; \ - } \ - } while (0) - -int verify_result(const struct tcpbpf_globals *result) -{ - __u32 expected_events; - int ret = 0; - - expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) | - (1 << BPF_SOCK_OPS_RWND_INIT) | - (1 << BPF_SOCK_OPS_TCP_CONNECT_CB) | - (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) | - (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) | - (1 << BPF_SOCK_OPS_NEEDS_ECN) | - (1 << BPF_SOCK_OPS_STATE_CB) | - (1 << BPF_SOCK_OPS_TCP_LISTEN_CB)); - - EXPECT_EQ(expected_events, result->event_map, "#" PRIx32); - EXPECT_EQ(501ULL, result->bytes_received, "llu"); - EXPECT_EQ(1002ULL, result->bytes_acked, "llu"); - EXPECT_EQ(1, result->data_segs_in, PRIu32); - EXPECT_EQ(1, result->data_segs_out, PRIu32); - EXPECT_EQ(0x80, result->bad_cb_test_rv, PRIu32); - EXPECT_EQ(0, result->good_cb_test_rv, PRIu32); - EXPECT_EQ(1, result->num_listen, PRIu32); - - /* 3 comes from one listening socket + both ends of the connection */ - EXPECT_EQ(3, result->num_close_events, PRIu32); - - return ret; -} - -int verify_sockopt_result(int sock_map_fd) +static void verify_result(int map_fd, int sock_map_fd) { + __u32 expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) | + (1 << BPF_SOCK_OPS_RWND_INIT) | + (1 << BPF_SOCK_OPS_TCP_CONNECT_CB) | + (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) | + (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) | + (1 << BPF_SOCK_OPS_NEEDS_ECN) | + (1 << BPF_SOCK_OPS_STATE_CB) | + (1 << BPF_SOCK_OPS_TCP_LISTEN_CB)); + struct tcpbpf_globals result = { 0 }; __u32 key = 0; - int ret = 0; int res; int rv; + rv = bpf_map_lookup_elem(map_fd, &key, &result); + if (CHECK_FAIL(rv != 0)) { + fprintf(stderr, "FAILED: bpf_map_lookup_elem returns %d\n", rv); + return; + } + + /* check global map */ + CHECK(expected_events != result.event_map, "event_map", + "unexpected event_map: actual %#" PRIx32" != expected %#" PRIx32 "\n", + result.event_map, expected_events); + + ASSERT_EQ(result.bytes_received, 501, "bytes_received"); + ASSERT_EQ(result.bytes_acked, 1002, "bytes_acked"); + ASSERT_EQ(result.data_segs_in, 1, "data_segs_in"); + ASSERT_EQ(result.data_segs_out, 1, "data_segs_out"); + ASSERT_EQ(result.bad_cb_test_rv, 0x80, "bad_cb_test_rv"); + ASSERT_EQ(result.good_cb_test_rv, 0, "good_cb_test_rv"); + ASSERT_EQ(result.num_listen, 1, "num_listen"); + + /* 3 comes from one listening socket + both ends of the connection */ + ASSERT_EQ(result.num_close_events, 3, "num_close_events"); + /* check setsockopt for SAVE_SYN */ + key = 0; rv = bpf_map_lookup_elem(sock_map_fd, &key, &res); - EXPECT_EQ(0, rv, "d"); - EXPECT_EQ(0, res, "d"); - key = 1; + ASSERT_EQ(rv, 0, "rv"); + ASSERT_EQ(res, 0, "res"); + /* check getsockopt for SAVED_SYN */ + key = 1; rv = bpf_map_lookup_elem(sock_map_fd, &key, &res); - EXPECT_EQ(0, rv, "d"); - EXPECT_EQ(1, res, "d"); - return ret; + ASSERT_EQ(rv, 0, "rv"); + ASSERT_EQ(res, 1, "res"); } -static int run_test(void) +static void run_test(int map_fd, int sock_map_fd) { int server_fd, client_fd; void *server_err; @@ -131,7 +123,7 @@ static int run_test(void) server_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0); if (CHECK_FAIL(server_fd < 0)) - return err; + return; pthread_mutex_lock(&server_started_mtx); if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread, @@ -163,17 +155,17 @@ static int run_test(void) close(client_fd); close_server_fd: close(server_fd); - return err; + + if (!err) + verify_result(map_fd, sock_map_fd); } void test_tcpbpf_user(void) { const char *file = "test_tcpbpf_kern.o"; int prog_fd, map_fd, sock_map_fd; - struct tcpbpf_globals g = {0}; struct bpf_object *obj; int cg_fd = -1; - __u32 key = 0; int rv; cg_fd = cgroup_setup_and_join(CG_NAME); @@ -200,26 +192,7 @@ void test_tcpbpf_user(void) if (CHECK_FAIL(sock_map_fd < 0)) goto err; - if (run_test()) { - fprintf(stderr, "FAILED: TCP server\n"); - goto err; - } - - rv = bpf_map_lookup_elem(map_fd, &key, &g); - if (CHECK_FAIL(rv != 0)) { - fprintf(stderr, "FAILED: bpf_map_lookup_elem returns %d\n", rv); - goto err; - } - - if (CHECK_FAIL(verify_result(&g))) { - fprintf(stderr, "FAILED: Wrong stats\n"); - goto err; - } - - if (CHECK_FAIL(verify_sockopt_result(sock_map_fd))) { - fprintf(stderr, "FAILED: Wrong sockopt stats\n"); - goto err; - } + run_test(map_fd, sock_map_fd); err: bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS); close(cg_fd); From patchwork Wed Oct 28 01:47:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11864065 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=-9.6 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 E0311C55179 for ; Wed, 28 Oct 2020 23:24:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 84B38207CD for ; Wed, 28 Oct 2020 23:24:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="n8Vc74Uq" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390329AbgJ1XYA (ORCPT ); Wed, 28 Oct 2020 19:24:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729360AbgJ1XKw (ORCPT ); Wed, 28 Oct 2020 19:10:52 -0400 Received: from mail-qv1-xf44.google.com (mail-qv1-xf44.google.com [IPv6:2607:f8b0:4864:20::f44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45D98C0613CF; Wed, 28 Oct 2020 16:10:52 -0700 (PDT) Received: by mail-qv1-xf44.google.com with SMTP id s1so595240qvm.13; Wed, 28 Oct 2020 16:10:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=cRfSQUYS268mMkMdpuJD7g3guRW6OCmdfQPtqkCWnpU=; b=n8Vc74UqdI/YeHoTm62iXVn0PvblfBRU+46Q82PQqHCs6re5G9TnYx334jGiMJPUM9 rQee7dOBddbSgBcsT0O1LxuHrmerrQyrJw/l/sjjk6xmmNa7wXDq2La7mtBu2osVP9ZU 4XOG+Wr/RamzEhycvjJ1THDTWP37yiQEqUdNQSpKF53J+bw8Pq1qA6fEpUS9tkG2vWqT t3ow+IcC57WRO0L0dPTTZTt2Iw5i62+/7kfa422czopXy2WivOQlmTZBG5p9pH1kUwBy OpI+9FHeVGvRAIQ5Avhr9G8UfRfikYXn7AHc54jtXppTPjTeqxBn+Jx2Zwy4XrQny7Hi tbJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=cRfSQUYS268mMkMdpuJD7g3guRW6OCmdfQPtqkCWnpU=; b=Q1bD7uHMmBv4ItzYsSz19W4vsueqgLI9Gz2jZOLZr7r5ncbXuNv9O/V/ink8zVLaOI FdbThO6YXEyVjPxR/EaBPvdcJdlNrZ4ppNNcu4tmEdyXiFXvgvN+qOOOKFyGiM1HBsf2 NINtRj871tcg33CyOgDn20u6xRqSvnsR+/tudad3L4L7Mon12oWThbLrpT9rfZ3gQKY2 NWx+3/xYMw9Z50rIKG3AY0APU4m0rHVBsFPrLtqPLFWD9ECNMkqqmOfOHwTs1jIbD/my PclvwTsFcn6FJVnlsNUeP0PMCSaM47Rx99B232G1ghF3Mdfcmlt1c2bEVRgxNNF/Wx4m e4lQ== X-Gm-Message-State: AOAM533IKAEyRsYIJhmwotLfzlnlAeoqz+18FrZlni0Ln1i2gptd1qSD oJbeA2CHASiHeScuyxkyDic0JR2Ity26ew== X-Google-Smtp-Source: ABdhPJzMkPwhmgHMRvZPZwx03cinaxgWHj1FYsMWP41zZeQipwbZNpH8nvMN/gwhK+wzIR8geQOGuQ== X-Received: by 2002:a05:6214:d42:: with SMTP id 2mr5666442qvr.29.1603849650218; Tue, 27 Oct 2020 18:47:30 -0700 (PDT) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id x6sm2120649qti.77.2020.10.27.18.47.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Oct 2020 18:47:29 -0700 (PDT) Subject: [bpf-next PATCH 4/4] selftests/bpf: Migrate tcpbpf_user.c to use BPF skeleton From: Alexander Duyck To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, kafai@fb.com, john.fastabend@gmail.com, kernel-team@fb.com, netdev@vger.kernel.org, edumazet@google.com, brakmo@fb.com, alexanderduyck@fb.com Date: Tue, 27 Oct 2020 18:47:28 -0700 Message-ID: <160384964803.698509.11020605670605638967.stgit@localhost.localdomain> In-Reply-To: <160384954046.698509.132709669068189999.stgit@localhost.localdomain> References: <160384954046.698509.132709669068189999.stgit@localhost.localdomain> User-Agent: StGit/0.23 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Alexander Duyck Update tcpbpf_user.c to make use of the BPF skeleton. Doing this we can simplify test_tcpbpf_user and reduce the overhead involved in setting up the test. Signed-off-by: Alexander Duyck Acked-by: Andrii Nakryiko --- .../testing/selftests/bpf/prog_tests/tcpbpf_user.c | 48 +++++++++----------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c b/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c index 4e1190894e1e..7e92c37976ac 100644 --- a/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c +++ b/tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c @@ -5,6 +5,7 @@ #include "test_tcpbpf.h" #include "cgroup_helpers.h" +#include "test_tcpbpf_kern.skel.h" #define LO_ADDR6 "::1" #define CG_NAME "/tcpbpf-user-test" @@ -162,39 +163,32 @@ static void run_test(int map_fd, int sock_map_fd) void test_tcpbpf_user(void) { - const char *file = "test_tcpbpf_kern.o"; - int prog_fd, map_fd, sock_map_fd; - struct bpf_object *obj; + struct test_tcpbpf_kern *skel; + int map_fd, sock_map_fd; + struct bpf_link *link; int cg_fd = -1; - int rv; - - cg_fd = cgroup_setup_and_join(CG_NAME); - if (CHECK_FAIL(cg_fd < 0)) - goto err; - if (CHECK_FAIL(bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd))) { - fprintf(stderr, "FAILED: load_bpf_file failed for: %s\n", file); - goto err; - } + skel = test_tcpbpf_kern__open_and_load(); + if (CHECK(!skel, "open and load skel", "failed")) + return; - rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0); - if (CHECK_FAIL(rv)) { - fprintf(stderr, "FAILED: bpf_prog_attach: %d (%s)\n", - errno, strerror(errno)); - goto err; - } + cg_fd = test__join_cgroup(CG_NAME); + if (CHECK_FAIL(cg_fd < 0)) + goto cleanup_skel; - map_fd = bpf_find_map(__func__, obj, "global_map"); - if (CHECK_FAIL(map_fd < 0)) - goto err; + map_fd = bpf_map__fd(skel->maps.global_map); + sock_map_fd = bpf_map__fd(skel->maps.sockopt_results); - sock_map_fd = bpf_find_map(__func__, obj, "sockopt_results"); - if (CHECK_FAIL(sock_map_fd < 0)) - goto err; + link = bpf_program__attach_cgroup(skel->progs.bpf_testcb, cg_fd); + if (CHECK(IS_ERR(link), "attach_cgroup(estab)", "err: %ld\n", + PTR_ERR(link))) + goto cleanup_namespace; run_test(map_fd, sock_map_fd); -err: - bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS); + + bpf_link__destroy(link); +cleanup_namespace: close(cg_fd); - cleanup_cgroup_environment(); +cleanup_skel: + test_tcpbpf_kern__destroy(skel); }