From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F75D30103B for ; Fri, 21 Nov 2025 05:45:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763703911; cv=none; b=Zs2ASTgzFMc42Dx9PP0RaG9Y0+RW0omTkf7HqiS9Pm13iihCgVRbs1rJtHlOFDBUG+ZJm9WtxFDBQEW633lIeNY7WDZ8IR+k/YJOJHZABUQkXEk9qrIvmzpEOFyqxJPzmRfYHIkYODf8HGOmhnBw/XrOCko8W9qlPXPBYoK4Vvc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763703911; c=relaxed/simple; bh=LWCJX/byO6UT+q76eS2OBJSBBF4gv+i35nuRb2OqTDY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=p1bWiD6SIOZhFoWlX77/auPnrg70nJQiQATA95lCC7J5QwTHmU4LNy1yXdq/dmBvGE96QfeJSMgUAt5QC17VCdDeRkrs0ONjXaOElqBDqT6TtEXQS2y8ZG8E4L6fHgwMmB+YoqxeOg/IUhUEL0Q1OPPZ4J3oof1DTTC/t9v7FKA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XsHbQSGI; arc=none smtp.client-ip=209.85.214.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XsHbQSGI" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-297dc3e299bso17335775ad.1 for ; Thu, 20 Nov 2025 21:45:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763703901; x=1764308701; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=zs6xAt2Exwcvl73D0Naa+Oua/oZSvlnc8CEdm60k1A4=; b=XsHbQSGIyHFOcTINzOuCv2oxvrbYyZ8GbLANbkI0rpKVDTPe5czprM2awBSAdYXnJk ZY3n+acZD9GW1vquE6U3vohcOovBEwo1or988Y1jDYm9Mn06crDAhu4bZ7yA2jj+aclV AA5YFfHGLU7cc5p3CcL6ojI+51Ajlr40KuV2GE3TQ6ncPO4hnZY6+SFa6L4t6XjmGV4/ aHnzzc4xvtCOKbZJXrf6IyCc4OWNB+jHYu6v+Qy9Axrx0xnaGa2SMaSV9YpfYODylehc xcIJARIXpK2iapy4htu92G6ZA3b4vCayETIERrTX5QMQc87kk74QgwgIcBsZ73K4c7w+ Tlng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763703901; x=1764308701; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=zs6xAt2Exwcvl73D0Naa+Oua/oZSvlnc8CEdm60k1A4=; b=v1pR3AIUfwQk0Rk20XBLLstyL1nGycQcJ5rzcBeT+zCPYt29uR8ZcBnrwAEdvOq4vm G6vkKSnZw1MhYFiXttlCQFUBy1ZOUwsJ7RjEnfy2M4EZG1uZ3tVvSl4VdYhDH7pjGGsN bz0HAEEgoJm90wKGoNjA1vKY1A9QldlLfX2TBJ05QvBCxs4lef17227TT3XRsp+2QegI xNP/7hTyeBm861KROJfv6o9CwATND3hlOaoBqPRrqsBipc6zWSYSXkqrNcCzc34CnosP YUeX2XyzEYZBZTDXiy/3iX7u3Gy8lvmrzkGEUzLEmrntqDqVHPWuuIZdIMiAHTAOyD7e reCg== X-Forwarded-Encrypted: i=1; AJvYcCWpb78ErXEIXmskkIFJXE3giTFIhbK73c6MFMYEn8a8TakPECMTvNvdCHSKZ24EWP5eWKUilrzhb5P2Le4vwuY=@vger.kernel.org X-Gm-Message-State: AOJu0Yxu8aIsNvlzRWlsz+9NKSDR0Kn5eMfvbhMxv7EK6WQAmhkxjhBI WVKSUcFbmlUq224aZUHRYT/FUK2eq5vUfWhNbl5vkqn1tz84EjH9k9EI X-Gm-Gg: ASbGncvOscfWnCvh29aMK0HthOcZ2sMM+YQ6efYFkGu7yBzAw5EYYekmVh/qdcLxKcM 6jZVGt57fd4FeMkavvuCewnNcSux7XoQTNubSDKdbEajRWll5KuVYo7Ps9bTvBRuoblBqCVYn4e 078mUcY0xNuIsf92JfKnFZ+yKbhwp6IbMquPbzd8JkjDEoGjgfGidfeC4+M3vyDlr7kf2cWIAuQ sFUhwTCS5JGsU2rT/BsvXpNlzgEYWyq4Jku+WMOBXbvgqLA+6P6LHcYKUDyjS2BWCkkDBz89I6s 8qbnV6KNjL/veg8OphGyOhWTYUiKF+5y774/db1tOogVZsSR024uSWtcPx/p9hc5aZwKCuoeLQR 16IYo5KgyCYAM1kuJBgQ/QWkGT5bq+Rym3/DRPUAyTsOHcP5GJHVMx+LzE2D3xM2z+VkHNX/HgE QY1yjtxSTFqHV0tHN/jTE/ X-Google-Smtp-Source: AGHT+IGn57Do3B+4FXiqsfIwgYl7pgWu9SjAS1ObYcIb+VIFdYb1GrE+1aqSdJXSuLR056m2+Bwhjw== X-Received: by 2002:a17:903:1666:b0:297:dfae:1524 with SMTP id d9443c01a7336-29b6beba2c9mr15580275ad.16.1763703900501; Thu, 20 Nov 2025 21:45:00 -0800 (PST) Received: from localhost ([2a03:2880:2ff:45::]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29b5b25e579sm44377955ad.51.2025.11.20.21.45.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Nov 2025 21:45:00 -0800 (PST) From: Bobby Eshleman Date: Thu, 20 Nov 2025 21:44:42 -0800 Subject: [PATCH net-next v11 10/13] selftests/vsock: add tests for proc sys vsock ns_mode Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20251120-vsock-vmtest-v11-10-55cbc80249a7@meta.com> References: <20251120-vsock-vmtest-v11-0-55cbc80249a7@meta.com> In-Reply-To: <20251120-vsock-vmtest-v11-0-55cbc80249a7@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, berrange@redhat.com, Sargun Dhillon , Bobby Eshleman , Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Add tests for the /proc/sys/net/vsock/ns_mode interface. Namely, that it accepts "global" and "local" strings and enforces a write-once policy. Start a convention of commenting the test name over the test description. Add test name comments over test descriptions that existed before this convention. Add a check_netns() function that checks if the test requires namespaces and if the current kernel supports namespaces. Skip tests that require namespaces if the system does not have namespace support. Add a test to verify that guest VMs with an active G2H transport (virtio-vsock) cannot set namespace mode to 'local'. This validates the mutual exclusion between G2H transports and LOCAL mode. This patch is the first to add tests that do *not* re-use the same shared VM. For that reason, it adds a run_tests() function to run these tests and filter out the shared VM tests. Signed-off-by: Bobby Eshleman --- Changes in v11: - Document ns_ prefix above TEST_NAMES (Stefano) Changes in v10: - Remove extraneous add_namespaces/del_namespaces calls. - Rename run_tests() to run_ns_tests() since it is designed to only run ns tests. Changes in v9: - add test ns_vm_local_mode_rejected to check that guests cannot use local mode --- tools/testing/selftests/vsock/vmtest.sh | 143 +++++++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vsock/vmtest.sh b/tools/testing/selftests/vsock/vmtest.sh index e32997db322d..2e077e8a1777 100755 --- a/tools/testing/selftests/vsock/vmtest.sh +++ b/tools/testing/selftests/vsock/vmtest.sh @@ -41,14 +41,43 @@ readonly KERNEL_CMDLINE="\ virtme.ssh virtme_ssh_channel=tcp virtme_ssh_user=$USER \ " readonly LOG=$(mktemp /tmp/vsock_vmtest_XXXX.log) -readonly TEST_NAMES=(vm_server_host_client vm_client_host_server vm_loopback) + +# Namespace tests must use the ns_ prefix. This is checked in check_netns() and +# is used to determine if a test needs namespace setup before test execution. +readonly TEST_NAMES=( + vm_server_host_client + vm_client_host_server + vm_loopback + ns_host_vsock_ns_mode_ok + ns_host_vsock_ns_mode_write_once_ok + ns_vm_local_mode_rejected +) readonly TEST_DESCS=( + # vm_server_host_client "Run vsock_test in server mode on the VM and in client mode on the host." + + # vm_client_host_server "Run vsock_test in client mode on the VM and in server mode on the host." + + # vm_loopback "Run vsock_test using the loopback transport in the VM." + + # ns_host_vsock_ns_mode_ok + "Check /proc/sys/net/vsock/ns_mode strings on the host." + + # ns_host_vsock_ns_mode_write_once_ok + "Check /proc/sys/net/vsock/ns_mode is write-once on the host." + + # ns_vm_local_mode_rejected + "Test that guest VM with G2H transport cannot set namespace mode to 'local'" ) -readonly USE_SHARED_VM=(vm_server_host_client vm_client_host_server vm_loopback) +readonly USE_SHARED_VM=( + vm_server_host_client + vm_client_host_server + vm_loopback + ns_vm_local_mode_rejected +) readonly NS_MODES=("local" "global") VERBOSE=0 @@ -205,6 +234,20 @@ check_deps() { fi } +check_netns() { + local tname=$1 + + # If the test requires NS support, check if NS support exists + # using /proc/self/ns + if [[ "${tname}" =~ ^ns_ ]] && + [[ ! -e /proc/self/ns ]]; then + log_host "No NS support detected for test ${tname}" + return 1 + fi + + return 0 +} + check_vng() { local tested_versions local version @@ -528,6 +571,32 @@ log_guest() { LOG_PREFIX=guest log "$@" } +test_ns_host_vsock_ns_mode_ok() { + for mode in "${NS_MODES[@]}"; do + if ! ns_set_mode "${mode}0" "${mode}"; then + return "${KSFT_FAIL}" + fi + done + + return "${KSFT_PASS}" +} + +test_ns_host_vsock_ns_mode_write_once_ok() { + for mode in "${NS_MODES[@]}"; do + local ns="${mode}0" + if ! ns_set_mode "${ns}" "${mode}"; then + return "${KSFT_FAIL}" + fi + + # try writing again and expect failure + if ns_set_mode "${ns}" "${mode}"; then + return "${KSFT_FAIL}" + fi + done + + return "${KSFT_PASS}" +} + test_vm_server_host_client() { if ! vm_vsock_test "init_ns" "server" 2 "${TEST_GUEST_PORT}"; then return "${KSFT_FAIL}" @@ -569,6 +638,26 @@ test_vm_loopback() { return "${KSFT_PASS}" } +test_ns_vm_local_mode_rejected() { + # Guest VMs have a G2H transport (virtio-vsock) active, so they + # should not be able to set namespace mode to 'local'. + # This test verifies that the sysctl write fails as expected. + + # Try to set local mode in the guest's init_ns + if vm_ssh init_ns "echo local | tee /proc/sys/net/vsock/ns_mode &>/dev/null"; then + return "${KSFT_FAIL}" + fi + + # Verify mode is still 'global' + local mode + mode=$(vm_ssh init_ns "cat /proc/sys/net/vsock/ns_mode") + if [[ "${mode}" != "global" ]]; then + return "${KSFT_FAIL}" + fi + + return "${KSFT_PASS}" +} + shared_vm_test() { local tname @@ -601,6 +690,11 @@ run_shared_vm_tests() { continue fi + if ! check_netns "${arg}"; then + check_result "${KSFT_SKIP}" "${arg}" + continue + fi + run_shared_vm_test "${arg}" check_result "$?" "${arg}" done @@ -654,6 +748,49 @@ run_shared_vm_test() { return "${rc}" } +run_ns_tests() { + for arg in "${ARGS[@]}"; do + if shared_vm_test "${arg}"; then + continue + fi + + if ! check_netns "${arg}"; then + check_result "${KSFT_SKIP}" "${arg}" + continue + fi + + add_namespaces + + name=$(echo "${arg}" | awk '{ print $1 }') + log_host "Executing test_${name}" + + host_oops_before=$(dmesg 2>/dev/null | grep -c -i 'Oops') + host_warn_before=$(dmesg --level=warn 2>/dev/null | grep -c -i 'vsock') + eval test_"${name}" + rc=$? + + host_oops_after=$(dmesg 2>/dev/null | grep -c -i 'Oops') + if [[ "${host_oops_after}" -gt "${host_oops_before}" ]]; then + echo "FAIL: kernel oops detected on host" | log_host + check_result "${KSFT_FAIL}" "${name}" + del_namespaces + continue + fi + + host_warn_after=$(dmesg --level=warn 2>/dev/null | grep -c -i 'vsock') + if [[ "${host_warn_after}" -gt "${host_warn_before}" ]]; then + echo "FAIL: kernel warning detected on host" | log_host + check_result "${KSFT_FAIL}" "${name}" + del_namespaces + continue + fi + + check_result "${rc}" "${name}" + + del_namespaces + done +} + BUILD=0 QEMU="qemu-system-$(uname -m)" @@ -699,6 +836,8 @@ if shared_vm_tests_requested "${ARGS[@]}"; then terminate_pidfiles "${pidfile}" fi +run_ns_tests "${ARGS[@]}" + echo "SUMMARY: PASS=${cnt_pass} SKIP=${cnt_skip} FAIL=${cnt_fail}" echo "Log: ${LOG}" -- 2.47.3