From patchwork Thu May 19 23:30:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mat Martineau X-Patchwork-Id: 12856131 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 2A76CC433EF for ; Thu, 19 May 2022 23:30:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343615AbiESXae (ORCPT ); Thu, 19 May 2022 19:30:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343581AbiESXa1 (ORCPT ); Thu, 19 May 2022 19:30:27 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 549381116EB; Thu, 19 May 2022 16:30:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653003026; x=1684539026; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AT4YZbah9POI/kqLzzON06I7kPuIBXR3qzkfdO8kWdo=; b=PJHualrG6BUX6W38Vy3VGw5ZVE05uAoMaeEVgOewypKsEbqj0V42A6JC 2b6l77U+xuqq/+I3NxphKpL9gQdCgu1aiP+D5QrrxVe5oCN3IzjOZvmFM 2xFv/TmrBIgWjQYNiVnJzOqRgfmFTN3dF3h2wIiRFdQ29L0aDx49t261O MvhREtYtgzg6XQmDwGhKFGK96wlSZqGJoNL2I8y3fgyqjCRJXwGrSPzjW X6OA3rXKCN29lmlDFoKJ0+O/gF4P2TgY0KGn//lJrBIk1CcBZuMoDD6Xq c6CFkQJLtDOcktvzjLagBRXBWA8HsC/wA6cJxroNQ0YK65BNz9rqm+oiL w==; X-IronPort-AV: E=McAfee;i="6400,9594,10352"; a="272547207" X-IronPort-AV: E=Sophos;i="5.91,238,1647327600"; d="scan'208";a="272547207" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 May 2022 16:30:22 -0700 X-IronPort-AV: E=Sophos;i="5.91,238,1647327600"; d="scan'208";a="570491201" Received: from mjmartin-desk2.amr.corp.intel.com (HELO mjmartin-desk2.intel.com) ([10.252.132.179]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 May 2022 16:30:22 -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 v5 6/7] selftests/bpf: verify ca_name of struct mptcp_sock Date: Thu, 19 May 2022 16:30:15 -0700 Message-Id: <20220519233016.105670-7-mathew.j.martineau@linux.intel.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220519233016.105670-1-mathew.j.martineau@linux.intel.com> References: <20220519233016.105670-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 another member of struct mptcp_sock, ca_name. Add a new function get_msk_ca_name() to read the sysctl tcp_congestion_control and verify it in verify_msk(). v3: Access the sysctl through the filesystem to avoid compatibility issues with the busybox sysctl command. v4: use ASSERT_* instead of CHECK_FAIL (Andrii) v5: use ASSERT_STRNEQ() instead of strncmp() (Andrii) Acked-by: Matthieu Baerts Signed-off-by: Geliang Tang Signed-off-by: Mat Martineau --- tools/testing/selftests/bpf/bpf_tcp_helpers.h | 5 +++ .../testing/selftests/bpf/prog_tests/mptcp.c | 31 +++++++++++++++++++ .../testing/selftests/bpf/progs/mptcp_sock.c | 4 +++ 3 files changed, 40 insertions(+) diff --git a/tools/testing/selftests/bpf/bpf_tcp_helpers.h b/tools/testing/selftests/bpf/bpf_tcp_helpers.h index 422491872619..c38c66d5c1e6 100644 --- a/tools/testing/selftests/bpf/bpf_tcp_helpers.h +++ b/tools/testing/selftests/bpf/bpf_tcp_helpers.h @@ -16,6 +16,10 @@ BPF_PROG(name, args) #define SOL_TCP 6 #endif +#ifndef TCP_CA_NAME_MAX +#define TCP_CA_NAME_MAX 16 +#endif + #define tcp_jiffies32 ((__u32)bpf_jiffies64()) struct sock_common { @@ -230,6 +234,7 @@ struct mptcp_sock { struct inet_connection_sock sk; __u32 token; + char ca_name[TCP_CA_NAME_MAX]; } __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 138bcc80ab9e..17ad1cb07339 100644 --- a/tools/testing/selftests/bpf/prog_tests/mptcp.c +++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c @@ -7,10 +7,15 @@ #include "network_helpers.h" #include "mptcp_sock.skel.h" +#ifndef TCP_CA_NAME_MAX +#define TCP_CA_NAME_MAX 16 +#endif + struct mptcp_storage { __u32 invoked; __u32 is_mptcp; __u32 token; + char ca_name[TCP_CA_NAME_MAX]; }; static int verify_tsk(int map_fd, int client_fd) @@ -31,14 +36,37 @@ static int verify_tsk(int map_fd, int client_fd) return err; } +void get_msk_ca_name(char ca_name[]) +{ + size_t len; + int fd; + + fd = open("/proc/sys/net/ipv4/tcp_congestion_control", O_RDONLY); + if (!ASSERT_GE(fd, 0, "failed to open tcp_congestion_control")) + return; + + len = read(fd, ca_name, TCP_CA_NAME_MAX); + if (!ASSERT_GT(len, 0, "failed to read ca_name")) + goto err; + + if (len > 0 && ca_name[len - 1] == '\n') + ca_name[len - 1] = '\0'; + +err: + close(fd); +} + static int verify_msk(int map_fd, int client_fd, __u32 token) { + char ca_name[TCP_CA_NAME_MAX]; int err, cfd = client_fd; struct mptcp_storage val; if (!ASSERT_GT(token, 0, "invalid token")) return -1; + get_msk_ca_name(ca_name); + err = bpf_map_lookup_elem(map_fd, &cfd, &val); if (!ASSERT_OK(err, "bpf_map_lookup_elem")) return err; @@ -52,6 +80,9 @@ static int verify_msk(int map_fd, int client_fd, __u32 token) if (!ASSERT_EQ(val.token, token, "unexpected token")) err++; + if (!ASSERT_STRNEQ(val.ca_name, ca_name, TCP_CA_NAME_MAX, "unexpected ca_name")) + err++; + return err; } diff --git a/tools/testing/selftests/bpf/progs/mptcp_sock.c b/tools/testing/selftests/bpf/progs/mptcp_sock.c index f038b0e699a2..6a78e7b754a8 100644 --- a/tools/testing/selftests/bpf/progs/mptcp_sock.c +++ b/tools/testing/selftests/bpf/progs/mptcp_sock.c @@ -2,6 +2,7 @@ /* Copyright (c) 2020, Tessares SA. */ /* Copyright (c) 2022, SUSE. */ +#include #include #include #include "bpf_tcp_helpers.h" @@ -13,6 +14,7 @@ struct mptcp_storage { __u32 invoked; __u32 is_mptcp; __u32 token; + char ca_name[TCP_CA_NAME_MAX]; }; struct { @@ -51,6 +53,7 @@ int _sockops(struct bpf_sock_ops *ctx) return 1; storage->token = 0; + bzero(storage->ca_name, TCP_CA_NAME_MAX); } else { msk = bpf_skc_to_mptcp_sock(sk); if (!msk) @@ -62,6 +65,7 @@ int _sockops(struct bpf_sock_ops *ctx) return 1; storage->token = msk->token; + memcpy(storage->ca_name, msk->ca_name, TCP_CA_NAME_MAX); } storage->invoked++; storage->is_mptcp = is_mptcp;