Skip to content

Commit eb59238

Browse files
committed
Use AF_UNIX for OpenVMM RPC connections
1 parent 180e104 commit eb59238

8 files changed

Lines changed: 77 additions & 53 deletions

File tree

‎src/windows/wslopenvmm/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ add_custom_command(
2626
OUTPUT ${WSLOPENVMM_DLL}
2727
COMMAND ${CMAKE_COMMAND} -E env "VM_SERVICE_PROTO=${VM_SERVICE_PROTO}" ${CARGO_EXECUTABLE} build --manifest-path ${CMAKE_CURRENT_LIST_DIR}/Cargo.toml --target ${RUST_TARGET} --target-dir ${CARGO_TARGET_DIR} ${CARGO_BUILD_ARGUMENTS}
2828
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CARGO_OUTPUT} ${WSLOPENVMM_DLL}
29-
DEPENDS ${VM_SERVICE_PROTO} Cargo.lock Cargo.toml build.rs src/client.rs src/lib.rs src/named_pipe.rs wslopenvmm.h
29+
DEPENDS ${VM_SERVICE_PROTO} Cargo.lock Cargo.toml build.rs src/client.rs src/lib.rs src/af_unix.rs wslopenvmm.h
3030
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
3131
VERBATIM)
3232

‎src/windows/wslopenvmm/Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/windows/wslopenvmm/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ crate-type = ["cdylib"]
1111
[dependencies]
1212
hyper-util = { version = "0.1", features = ["tokio"] }
1313
parking_lot = "0.12"
14+
socket2 = "0.6"
1415
prost = "0.14"
1516
prost-types = "0.14"
1617
tonic = "0.14"
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (C) Microsoft Corporation. All rights reserved.
2+
3+
use std::io;
4+
use std::path::{Path, PathBuf};
5+
use std::time::Duration;
6+
7+
use hyper_util::rt::TokioIo;
8+
use socket2::{Domain, SockAddr, Socket, Type};
9+
use tokio::net::TcpStream;
10+
use tonic::transport::{Channel, Endpoint};
11+
use tower::service_fn;
12+
13+
/// Connects a Tonic channel to the OpenVMM AF_UNIX listener.
14+
pub async fn connect_channel(
15+
socket_path: PathBuf,
16+
timeout: Duration,
17+
) -> Result<Channel, tonic::transport::Error> {
18+
Endpoint::from_static("http://[::]:50051")
19+
.connect_timeout(timeout)
20+
.connect_with_connector(service_fn(move |_| {
21+
let socket_path = socket_path.clone();
22+
async move { open_socket(&socket_path, timeout).await.map(TokioIo::new) }
23+
}))
24+
.await
25+
}
26+
27+
async fn open_socket(path: &Path, timeout: Duration) -> io::Result<TcpStream> {
28+
let address = SockAddr::unix(path)?;
29+
tokio::time::timeout(timeout, async {
30+
loop {
31+
match connect_socket(&address).await {
32+
Ok(stream) => return Ok(stream),
33+
Err(error)
34+
if matches!(
35+
error.kind(),
36+
io::ErrorKind::NotFound | io::ErrorKind::ConnectionRefused
37+
) => {}
38+
Err(error) => return Err(error),
39+
}
40+
tokio::time::sleep(Duration::from_millis(20)).await;
41+
}
42+
})
43+
.await
44+
.map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "timed out connecting to OpenVMM"))?
45+
}
46+
47+
/// Registers the Winsock AF_UNIX socket with Tokio's Windows socket reactor.
48+
/// The socket remains AF_UNIX; TCP-specific options and address queries must
49+
/// not be used on the returned stream.
50+
async fn connect_socket(address: &SockAddr) -> io::Result<TcpStream> {
51+
let socket = Socket::new(Domain::UNIX, Type::STREAM, None)?;
52+
socket.set_nonblocking(true)?;
53+
match socket.connect(address) {
54+
Ok(()) => {}
55+
Err(error) if error.kind() == io::ErrorKind::WouldBlock => {}
56+
Err(error) => return Err(error),
57+
}
58+
59+
let stream = TcpStream::from_std(socket.into())?;
60+
stream.writable().await?;
61+
if let Some(error) = stream.take_error()? {
62+
return Err(error);
63+
}
64+
Ok(stream)
65+
}

‎src/windows/wslopenvmm/src/client.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use windows::Win32::Foundation::{E_FAIL, E_INVALIDARG, ERROR_ALREADY_EXISTS, ERR
1313
use windows::core::{GUID, HRESULT};
1414
use windows_sys::Win32::Networking::WinSock::{AF_INET, AF_INET6};
1515

16-
use crate::{named_pipe, vmservice};
16+
use crate::{af_unix, vmservice};
1717

1818
#[cfg(test)]
1919
mod tests;
@@ -40,7 +40,7 @@ impl VmConfigBuilder {
4040
}
4141
}
4242

43-
pub fn create_vm(&self, pipe_name: String, timeout_ms: u32) -> Result<VmHandle, HRESULT> {
43+
pub fn create_vm(&self, socket_path: String, timeout_ms: u32) -> Result<VmHandle, HRESULT> {
4444
let runtime = match tokio::runtime::Builder::new_current_thread()
4545
.enable_all()
4646
.build()
@@ -49,7 +49,7 @@ impl VmConfigBuilder {
4949
Err(_) => return Err(E_FAIL),
5050
};
5151
let timeout = Duration::from_millis(u64::from(timeout_ms));
52-
let channel = match runtime.block_on(named_pipe::connect_channel(pipe_name, timeout)) {
52+
let channel = match runtime.block_on(af_unix::connect_channel(socket_path.into(), timeout)) {
5353
Ok(channel) => channel,
5454
Err(error) if error.to_string().contains("timed out") => {
5555
return Err(HRESULT::from_win32(WAIT_TIMEOUT.0));

‎src/windows/wslopenvmm/src/lib.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![expect(clippy::missing_safety_doc)]
44

55
mod client;
6-
pub mod named_pipe;
6+
pub mod af_unix;
77

88
pub mod vmservice {
99
tonic::include_proto!("vmservice");
@@ -200,7 +200,7 @@ pub unsafe extern "C" fn WslOpenVmmConfigSetVirtioConsolePath(
200200
#[unsafe(no_mangle)]
201201
pub unsafe extern "C" fn WslOpenVmmCreateVm(
202202
config: *mut *mut WslOpenVmmConfig,
203-
pipe_name: *const u16,
203+
socket_path: *const u16,
204204
timeout_ms: u32,
205205
vm: *mut *mut WslOpenVmmVm,
206206
) -> i32 {
@@ -215,13 +215,13 @@ pub unsafe extern "C" fn WslOpenVmmCreateVm(
215215
if config_handle.is_null() {
216216
return E_POINTER.0;
217217
}
218-
let pipe_name = match unsafe { string_from_wide(pipe_name) } {
219-
Ok(pipe_name) => pipe_name,
218+
let socket_path = match unsafe { string_from_wide(socket_path) } {
219+
Ok(socket_path) => socket_path,
220220
Err(error) => return error.0,
221221
};
222222

223223
let config_box = unsafe { Box::from_raw(config_handle) };
224-
let result = config_box.0.lock().create_vm(pipe_name, timeout_ms);
224+
let result = config_box.0.lock().create_vm(socket_path, timeout_ms);
225225
let client = match result {
226226
Ok(client) => client,
227227
Err(error) => {

‎src/windows/wslopenvmm/src/named_pipe.rs‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

‎src/windows/wslopenvmm/wslopenvmm.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ __declspec(dllimport) HRESULT WslOpenVmmConfigSetConsommeNic(_Inout_ WslOpenVmmC
2626
__declspec(dllimport) HRESULT WslOpenVmmConfigAddSerialPort(_Inout_ WslOpenVmmConfig* Config, _In_ UINT32 Port, _In_ LPCWSTR PipeName);
2727
__declspec(dllimport) HRESULT WslOpenVmmConfigSetVirtioConsolePath(_Inout_ WslOpenVmmConfig* Config, _In_ LPCWSTR Path);
2828
// On success, this consumes Config and sets it to nullptr. On failure, Config remains valid and Vm is nullptr.
29-
__declspec(dllimport) HRESULT WslOpenVmmCreateVm(_Inout_ WslOpenVmmConfig** Config, _In_ LPCWSTR PipeName, _In_ UINT32 TimeoutMs, _Out_ WslOpenVmmVm** Vm);
29+
__declspec(dllimport) HRESULT WslOpenVmmCreateVm(_Inout_ WslOpenVmmConfig** Config, _In_ LPCWSTR SocketPath, _In_ UINT32 TimeoutMs, _Out_ WslOpenVmmVm** Vm);
3030

3131
__declspec(dllimport) void WslOpenVmmDestroyVm(_In_opt_ WslOpenVmmVm* Vm);
3232
__declspec(dllimport) HRESULT WslOpenVmmVmResume(_Inout_ WslOpenVmmVm* Vm);

0 commit comments

Comments
 (0)