@@ -607,55 +607,16 @@ void WslCoreVm::Initialize(const GUID& VmId, const wil::shared_handle& UserToken
607607 }
608608 else if (m_vmConfig.NetworkingMode == NetworkingMode::VirtioProxy)
609609 {
610- auto virtioNetworkingEngine = std::make_unique<wsl::core::VirtioNetworking>(std::move (gnsChannel), m_vmConfig);
611- virtioNetworkingEngine->OnAddGuestDevice ([&](const GUID& Clsid, const GUID& DeviceId, PCWSTR Tag, PCWSTR Options) {
612- auto guestDeviceLock = m_guestDeviceLock.lock_exclusive ();
613- return AddHdvShareWithOptions (DeviceId, Clsid, Tag, {}, Options, 0 , m_userToken.get ());
614- });
615-
616- virtioNetworkingEngine->OnModifyOpenPorts ([&](const GUID& Clsid, PCWSTR Tag, const SOCKADDR_INET& addr, int protocol, bool isOpen) {
617- if (protocol != IPPROTO_TCP && protocol != IPPROTO_UDP)
618- {
619- LOG_HR_MSG (HRESULT_FROM_WIN32 (ERROR_NOT_SUPPORTED), " Unsupported bind protocol %d" , protocol);
620- return 0 ;
621- }
622- else if (addr.si_family == AF_INET6)
623- {
624- // The virtio net adapter does not yet support IPv6 packets, so any traffic would arrive via
625- // IPv4. If the caller wants IPv4 they will also likely listen on an IPv4 address, which will
626- // be handled as a separate callback to this same code.
627- return 0 ;
628- }
629-
630- auto guestDeviceLock = m_guestDeviceLock.lock_exclusive ();
631- const auto server = m_deviceHostSupport->GetRemoteFileSystem (Clsid, c_defaultTag);
632- if (server)
633- {
634- std::wstring portString (L" tag=" );
635- portString += Tag;
636- portString += L" ;port_number=" ;
637- portString += std::to_wstring (addr.Ipv4 .sin_port );
638- if (protocol == IPPROTO_UDP)
639- {
640- portString += L" ;udp" ;
641- }
642- if (!isOpen)
643- {
644- portString += L" ;allocate=false" ;
645- }
646- else
647- {
648- std::wstring addrStr (L" 000.000.000.000\0 " );
649- RtlIpv4AddressToStringW (&addr.Ipv4 .sin_addr , addrStr.data ());
650- portString += L" ;listen_addr=" ;
651- portString += addrStr;
652- }
653- LOG_IF_FAILED (server->AddShare (portString.c_str (), nullptr , 0 ));
654- }
655- return 0 ;
656- });
657- virtioNetworkingEngine->OnGuestInterfaceStateChanged ([&](const std::string& name, bool isUp) {});
658- m_networkingEngine.reset (virtioNetworkingEngine.release ());
610+ m_networkingEngine = std::make_unique<wsl::core::VirtioNetworking>(
611+ std::move (gnsChannel),
612+ m_vmConfig.EnableLocalhostRelay ,
613+ [this ](const GUID& Clsid, const GUID& DeviceId, PCWSTR Tag, PCWSTR Options) {
614+ return HandleVirtioAddGuestDevice (Clsid, DeviceId, Tag, Options);
615+ },
616+ [this ](const GUID& Clsid, PCWSTR Tag, const SOCKADDR_INET& Addr, int Protocol, bool IsOpen) {
617+ return HandleVirtioModifyOpenPorts (Clsid, Tag, Addr, Protocol, IsOpen);
618+ },
619+ [](const std::string&, bool ) {});
659620 }
660621 else if (m_vmConfig.NetworkingMode == NetworkingMode::Bridged)
661622 {
@@ -2037,6 +1998,59 @@ bool WslCoreVm::IsDnsTunnelingSupported() const
20371998 return SUCCEEDED_LOG (wsl::core::networking::DnsResolver::LoadDnsResolverMethods ());
20381999}
20392000
2001+ bool WslCoreVm::IsVhdAttached (_In_ PCWSTR VhdPath)
2002+ {
2003+ auto lock = m_lock.lock_exclusive ();
2004+ return m_attachedDisks.contains ({DiskType::VHD, VhdPath});
2005+ }
2006+
2007+ GUID WslCoreVm::HandleVirtioAddGuestDevice (_In_ const GUID& Clsid, _In_ const GUID& DeviceId, _In_ PCWSTR Tag, _In_ PCWSTR Options)
2008+ {
2009+ auto guestDeviceLock = m_guestDeviceLock.lock_exclusive ();
2010+ return AddHdvShareWithOptions (DeviceId, Clsid, Tag, {}, Options, 0 , m_userToken.get ());
2011+ }
2012+
2013+ int WslCoreVm::HandleVirtioModifyOpenPorts (_In_ const GUID& Clsid, _In_ PCWSTR Tag, _In_ const SOCKADDR_INET& Addr, _In_ int Protocol, _In_ bool IsOpen)
2014+ {
2015+ if (Protocol != IPPROTO_TCP && Protocol != IPPROTO_UDP)
2016+ {
2017+ LOG_HR_MSG (HRESULT_FROM_WIN32 (ERROR_NOT_SUPPORTED), " Unsupported bind protocol %d" , Protocol);
2018+ return 0 ;
2019+ }
2020+ else if (Addr.si_family == AF_INET6)
2021+ {
2022+ // The virtio net adapter does not yet support IPv6 packets, so any traffic would arrive via
2023+ // IPv4. If the caller wants IPv4 they will also likely listen on an IPv4 address, which will
2024+ // be handled as a separate callback to this same code.
2025+ return 0 ;
2026+ }
2027+
2028+ auto guestDeviceLock = m_guestDeviceLock.lock_exclusive ();
2029+ const auto server = m_deviceHostSupport->GetRemoteFileSystem (Clsid, c_defaultTag);
2030+ if (server)
2031+ {
2032+ std::wstring portString = std::format (L" tag={};port_number={}" , Tag, Addr.Ipv4 .sin_port );
2033+ if (Protocol == IPPROTO_UDP)
2034+ {
2035+ portString += L" ;udp" ;
2036+ }
2037+
2038+ if (!IsOpen)
2039+ {
2040+ portString += L" ;allocate=false" ;
2041+ }
2042+ else
2043+ {
2044+ wchar_t addrStr[16 ]; // "000.000.000.000" + null terminator
2045+ RtlIpv4AddressToStringW (&Addr.Ipv4 .sin_addr , addrStr);
2046+ portString += std::format (L" ;listen_addr={}" , addrStr);
2047+ }
2048+
2049+ LOG_IF_FAILED (server->AddShare (portString.c_str (), nullptr , 0 ));
2050+ }
2051+ return 0 ;
2052+ }
2053+
20402054WslCoreVm::DiskMountResult WslCoreVm::MountDisk (
20412055 _In_ PCWSTR Disk, _In_ DiskType MountDiskType, _In_ ULONG PartitionIndex, _In_opt_ PCWSTR Name, _In_opt_ PCWSTR Type, _In_opt_ PCWSTR Options)
20422056{
@@ -2846,12 +2860,6 @@ LX_INIT_DRVFS_MOUNT WslCoreVm::s_InitializeDrvFs(_Inout_ WslCoreVm* VmContext, _
28462860 }
28472861}
28482862
2849- bool WslCoreVm::IsVhdAttached (_In_ PCWSTR VhdPath)
2850- {
2851- auto lock = m_lock.lock_exclusive ();
2852- return m_attachedDisks.contains ({DiskType::VHD, VhdPath});
2853- }
2854-
28552863void CALLBACK WslCoreVm::s_OnExit (_In_ HCS_EVENT* Event, _In_opt_ void * Context)
28562864try
28572865{
0 commit comments