Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/windows/common/WslCoreNetworkingSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ inline constexpr auto* c_ipv4TestRequestTargetA = "www.msftconnecttest.com";
inline constexpr auto* c_ipv6TestRequestTarget = L"ipv6.msftconnecttest.com";
inline constexpr auto* c_ipv6TestRequestTargetA = "ipv6.msftconnecttest.com";

inline HRESULT GetGnsCallbackResult(LX_MESSAGE_TYPE messageType, HRESULT transportResult, int linuxResultCode) noexcept
{
if (FAILED(transportResult) || messageType == LxGnsMessageConnectTestRequest || linuxResultCode == 0)
{
return transportResult;
}

return E_FAIL;
}

inline constexpr GUID c_wslFirewallVmCreatorId = {0x40E0AC32, 0x46A5, 0x438A, {0xA0, 0xB2, 0x2B, 0x47, 0x9E, 0x8F, 0x2E, 0x90}};

inline constexpr auto c_networkAdapterPrefix = L"VirtualMachine/Devices/NetworkAdapters/";
Expand Down
72 changes: 37 additions & 35 deletions src/windows/service/exe/MirroredNetworking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,41 +667,43 @@ try
WI_ASSERT(WI_IsFlagSet(callbackFlags, wsl::core::networking::GnsCallbackFlags::Wait));
}

auto sendGnsMessage =
[this, messageType, capturedNotificationString = std::move(notificationString), callbackFlags, returnedValueFromGns]() mutable {
try
{
auto retryCount = 0ul;
// RetryWithTimeout throws if fails after the timeout has elapsed - which is caught and returned by m_gnsMessageQueue below
return wsl::shared::retry::RetryWithTimeout<HRESULT>(
[&]() {
const auto hr = wil::ResultFromException([&] {
if (returnedValueFromGns && WI_IsFlagSet(callbackFlags, wsl::core::networking::GnsCallbackFlags::Wait))
{
*returnedValueFromGns =
m_gnsChannel.SendNetworkDeviceMessageReturnResult(messageType, capturedNotificationString.c_str());
}
else
{
m_gnsChannel.SendNetworkDeviceMessage(messageType, capturedNotificationString.c_str());
}
});
WSL_LOG(
"MirroredNetworking::NetworkManagerGnsMessageCallback",
TraceLoggingValue(ToString(messageType), "messageType"),
TraceLoggingValue(capturedNotificationString.c_str(), "notificationString"),
TraceLoggingValue(hr, "hr"),
TraceLoggingValue(returnedValueFromGns ? *returnedValueFromGns : 0xFFFFFFFF, "returnedValueFromGns"),
TraceLoggingValue(retryCount, "retryCount"));

++retryCount;
return hr;
},
std::chrono::milliseconds(100),
std::chrono::seconds(3));
}
CATCH_RETURN()
};
auto sendGnsMessage = [this, messageType, capturedNotificationString = std::move(notificationString), callbackFlags, returnedValueFromGns]() mutable {
try
{
auto retryCount = 0ul;
auto sendMessage = [&]() {
const auto hr = wil::ResultFromException([&] {
if (returnedValueFromGns && WI_IsFlagSet(callbackFlags, wsl::core::networking::GnsCallbackFlags::Wait))
{
*returnedValueFromGns =
m_gnsChannel.SendNetworkDeviceMessageReturnResult(messageType, capturedNotificationString.c_str());
}
else
{
m_gnsChannel.SendNetworkDeviceMessage(messageType, capturedNotificationString.c_str());
}
});
const bool hasLinuxResult = returnedValueFromGns != nullptr;
const int linuxResultCode = hasLinuxResult ? *returnedValueFromGns : 0;
WSL_LOG(
"MirroredNetworking::NetworkManagerGnsMessageCallback",
TraceLoggingValue(ToString(messageType), "messageType"),
TraceLoggingValue(capturedNotificationString.c_str(), "notificationString"),
TraceLoggingValue(hr, "hr"),
TraceLoggingValue(hasLinuxResult, "hasLinuxResult"),
TraceLoggingValue(linuxResultCode, "linuxResultCode"),
TraceLoggingValue(retryCount, "retryCount"));

++retryCount;
THROW_IF_FAILED(hr);
return networking::GetGnsCallbackResult(messageType, hr, linuxResultCode);
};

// RetryWithTimeout throws if transport fails after the timeout has elapsed.
return wsl::shared::retry::RetryWithTimeout<HRESULT>(sendMessage, std::chrono::milliseconds(100), std::chrono::seconds(3));
}
CATCH_RETURN()
};

if (WI_IsFlagSet(callbackFlags, wsl::core::networking::GnsCallbackFlags::Wait))
{
Expand Down
31 changes: 31 additions & 0 deletions test/windows/NetworkTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Module Name:
#include "wslpolicies.h"
#include "hns_schema.h"
#include "WslCoreNetworkEndpointSettings.h"
#include "WslCoreNetworkingSupport.h"

#include <mstcpip.h>
#include <winhttp.h>
Expand Down Expand Up @@ -3950,6 +3951,36 @@ class NetworkTests
}
};

class GnsCallbackResultTests
{
WSL_TEST_CLASS(GnsCallbackResultTests)

TEST_METHOD(GnsCallbackSuccessfulTransportAndLinuxResultSucceeds)
{
VERIFY_SUCCEEDED(wsl::core::networking::GetGnsCallbackResult(LxGnsMessageDeviceSettingRequest, S_OK, 0));
}

TEST_METHOD(GnsCallbackSuccessfulTransportAndLinuxFailureFails)
{
VERIFY_ARE_EQUAL(E_FAIL, wsl::core::networking::GetGnsCallbackResult(LxGnsMessageDeviceSettingRequest, S_OK, -1));
}

TEST_METHOD(GnsCallbackTransportFailureFails)
{
VERIFY_ARE_EQUAL(E_ABORT, wsl::core::networking::GetGnsCallbackResult(LxGnsMessageDeviceSettingRequest, E_ABORT, 0));
}

TEST_METHOD(GnsCallbackConnectTestBusinessResultSucceeds)
{
VERIFY_SUCCEEDED(wsl::core::networking::GetGnsCallbackResult(LxGnsMessageConnectTestRequest, S_OK, -1));
}

TEST_METHOD(GnsCallbackConnectTestTransportFailureFails)
{
VERIFY_ARE_EQUAL(E_ABORT, wsl::core::networking::GetGnsCallbackResult(LxGnsMessageConnectTestRequest, E_ABORT, -1));
}
};
Comment thread
Copilot marked this conversation as resolved.

class MirroredTests
{
WSL_TEST_CLASS(MirroredTests)
Expand Down