From patchwork Wed Apr 20 22:24:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mat Martineau X-Patchwork-Id: 12820862 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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6EBA4C433F5 for ; Wed, 20 Apr 2022 22:25:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356318AbiDTW2O (ORCPT ); Wed, 20 Apr 2022 18:28:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356074AbiDTW2F (ORCPT ); Wed, 20 Apr 2022 18:28:05 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1783B40A1C; Wed, 20 Apr 2022 15:25:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1650493518; x=1682029518; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3+l/q0awMNcnjC8pV+An6v6Ed4WAzYGKwHgbYfPu7PI=; b=WMNv1EmwX5kRLQA4zOd8j/FF2Uhz0AE+2ChOlJx7Rc2dj8uMb+s2nDnf dH6zTdEgVMC1UUeOB8Z8yvwP4c+YGjuyYNq4FgJqKyZrYzbTe3MqtUkgM W4p8mFpaKOQvTUwwD2urPZhjw6CCe27rAQa7vQBhDSMY48ww0IU0rsTpm WUclWx5olLj379oE1DHGNvgVK3uIJBOvMNCvmPnYKxlXgojEc8ceKnPRE mCxhP992jMggi3li0uDkUlDq2B6f1dWH7u//u0P1eWmRZFRfWmgv8/IRP Rct5NZkROOrEhPZlLabEvHzHBQVDH6W3scf5RcjmvsZreSQVw8PiCdd8B g==; X-IronPort-AV: E=McAfee;i="6400,9594,10323"; a="289277604" X-IronPort-AV: E=Sophos;i="5.90,276,1643702400"; d="scan'208";a="289277604" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Apr 2022 15:25:17 -0700 X-IronPort-AV: E=Sophos;i="5.90,276,1643702400"; d="scan'208";a="555422604" Received: from mjmartin-desk2.amr.corp.intel.com (HELO mjmartin-desk2.intel.com) ([10.209.100.38]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Apr 2022 15:25:17 -0700 From: Mat Martineau To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Geliang Tang , ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, mptcp@lists.linux.dev, Matthieu Baerts , Mat Martineau Subject: [PATCH bpf-next 5/7] selftests: bpf: verify token of struct mptcp_sock Date: Wed, 20 Apr 2022 15:24:57 -0700 Message-Id: <20220420222459.307649-6-mathew.j.martineau@linux.intel.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220420222459.307649-1-mathew.j.martineau@linux.intel.com> References: <20220420222459.307649-1-mathew.j.martineau@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang This patch verifies the struct member token of struct mptcp_sock. Add a new function get_msk_token() to parse the msk token from the output of the command 'ip mptcp monitor', and verify it in verify_msk(). Acked-by: Matthieu Baerts Signed-off-by: Geliang Tang Signed-off-by: Mat Martineau --- .../testing/selftests/bpf/bpf_mptcp_helpers.h | 1 + .../testing/selftests/bpf/prog_tests/mptcp.c | 66 +++++++++++++++++++ .../testing/selftests/bpf/progs/mptcp_sock.c | 5 ++ 3 files changed, 72 insertions(+) diff --git a/tools/testing/selftests/bpf/bpf_mptcp_helpers.h b/tools/testing/selftests/bpf/bpf_mptcp_helpers.h index 18da4cc65e89..87e15810997d 100644 --- a/tools/testing/selftests/bpf/bpf_mptcp_helpers.h +++ b/tools/testing/selftests/bpf/bpf_mptcp_helpers.h @@ -9,6 +9,7 @@ struct mptcp_sock { struct inet_connection_sock sk; + __u32 token; } __attribute__((preserve_access_index)); #endif diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c index 4b40bbdaf91f..c5d96ba81e04 100644 --- a/tools/testing/selftests/bpf/prog_tests/mptcp.c +++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c @@ -8,8 +8,11 @@ struct mptcp_storage { __u32 invoked; __u32 is_mptcp; + __u32 token; }; +static char monitor_log_path[64]; + static int verify_tsk(int map_fd, int client_fd) { char *msg = "plain TCP socket"; @@ -36,11 +39,58 @@ static int verify_tsk(int map_fd, int client_fd) return err; } +/* + * Parse the token from the output of 'ip mptcp monitor': + * + * [ CREATED] token=3ca933d3 remid=0 locid=0 saddr4=127.0.0.1 ... + * [ CREATED] token=2ab57040 remid=0 locid=0 saddr4=127.0.0.1 ... + */ +static __u32 get_msk_token(void) +{ + char *prefix = "[ CREATED] token="; + char buf[BUFSIZ] = {}; + __u32 token = 0; + ssize_t len; + int fd; + + sync(); + + fd = open(monitor_log_path, O_RDONLY); + if (CHECK_FAIL(fd < 0)) { + log_err("Failed to open %s", monitor_log_path); + return token; + } + + len = read(fd, buf, sizeof(buf)); + if (CHECK_FAIL(len < 0)) { + log_err("Failed to read %s", monitor_log_path); + goto err; + } + + if (strncmp(buf, prefix, strlen(prefix))) { + log_err("Invalid prefix %s", buf); + goto err; + } + + token = strtol(buf + strlen(prefix), NULL, 16); + +err: + close(fd); + return token; +} + static int verify_msk(int map_fd, int client_fd) { char *msg = "MPTCP subflow socket"; int err = 0, cfd = client_fd; struct mptcp_storage val; + __u32 token; + + token = get_msk_token(); + if (token <= 0) { + log_err("Unexpected token %x", token); + return -1; + } if (CHECK_FAIL(bpf_map_lookup_elem(map_fd, &cfd, &val) < 0)) { perror("Failed to read socket storage"); @@ -59,6 +109,12 @@ static int verify_msk(int map_fd, int client_fd) err++; } + if (val.token != token) { + log_err("Unexpected mptcp_sock.token %x != %x", + val.token, token); + err++; + } + return err; } @@ -124,6 +180,7 @@ static int run_test(int cgroup_fd, int server_fd, bool is_mptcp) void test_base(void) { + char cmd[256], tmp_dir[] = "/tmp/XXXXXX"; int server_fd, cgroup_fd; cgroup_fd = test__join_cgroup("/mptcp"); @@ -141,6 +198,13 @@ void test_base(void) with_mptcp: /* with MPTCP */ + if (CHECK_FAIL(!mkdtemp(tmp_dir))) + goto close_cgroup_fd; + snprintf(monitor_log_path, sizeof(monitor_log_path), + "%s/ip_mptcp_monitor", tmp_dir); + snprintf(cmd, sizeof(cmd), "ip mptcp monitor > %s &", monitor_log_path); + if (CHECK_FAIL(system(cmd))) + goto close_cgroup_fd; server_fd = start_mptcp_server(AF_INET, NULL, 0, 0); if (CHECK_FAIL(server_fd < 0)) goto close_cgroup_fd; @@ -148,6 +212,8 @@ void test_base(void) CHECK_FAIL(run_test(cgroup_fd, server_fd, true)); close(server_fd); + snprintf(cmd, sizeof(cmd), "rm -rf %s", tmp_dir); + system(cmd); close_cgroup_fd: close(cgroup_fd); diff --git a/tools/testing/selftests/bpf/progs/mptcp_sock.c b/tools/testing/selftests/bpf/progs/mptcp_sock.c index 5cfaec4e7245..56cc7c61131b 100644 --- a/tools/testing/selftests/bpf/progs/mptcp_sock.c +++ b/tools/testing/selftests/bpf/progs/mptcp_sock.c @@ -11,6 +11,7 @@ __u32 _version SEC("version") = 1; struct mptcp_storage { __u32 invoked; __u32 is_mptcp; + __u32 token; }; struct { @@ -45,6 +46,8 @@ int _sockops(struct bpf_sock_ops *ctx) BPF_SK_STORAGE_GET_F_CREATE); if (!storage) return 1; + + storage->token = 0; } else { msk = bpf_skc_to_mptcp_sock(sk); if (!msk) @@ -54,6 +57,8 @@ int _sockops(struct bpf_sock_ops *ctx) BPF_SK_STORAGE_GET_F_CREATE); if (!storage) return 1; + + storage->token = msk->token; } storage->invoked++; storage->is_mptcp = tcp_sk->is_mptcp;