// SPDX-License-Identifier: BSD-3-Clause use devices::virtio::{ InterruptProxy, InterruptProxyEvent, RemotePciCapability, Request, Response, VirtioDevice, Wl, }; use msg_socket::MsgSocket; use std::collections::BTreeMap; use std::fs::remove_file; use sys_util::{error, net::UnixSeqpacketListener, GuestMemory}; #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] pub use aarch64::arch_memory_regions; #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] pub use x86_64::arch_memory_regions; type Socket = msg_socket2::Socket; fn main() { eprintln!("hello world"); // Create and display the socket. let mut path = std::env::var("XDG_RUNTIME_DIR").expect("XDG_RUNTIME_DIR missing"); path.push_str("/crosvm-wl.sock"); let _ = remove_file(&path); let server = UnixSeqpacketListener::bind(&path).expect("failed to create control socket"); println!("{}", path); // Receive connection from crosvm. let conn = server.accept().expect("accept failed"); let msg_socket: Socket = msg_socket2::Socket::new(conn); let (vm_socket, memory_params) = match msg_socket.recv() { Ok(Request::Create { vm_socket, memory_params, }) => (MsgSocket::new(vm_socket.owned()), memory_params), Ok(msg) => { panic!("received unexpected message: {:?}", msg); } Err(e) => { panic!("recv error: {}", e); } }; let mut wayland_paths = BTreeMap::new(); wayland_paths.insert("".into(), "/run/user/1000/wayland-0".into()); let mut wl = Wl::new(wayland_paths, vm_socket, None).unwrap(); loop { match msg_socket.recv() { Ok(Request::DebugLabel) => { let result = wl.debug_label(); if let Err(e) = msg_socket.send(Response::DebugLabel(result)) { panic!("responding to DebugLabel failed: {}", e); } } Ok(Request::DeviceType) => { let result = wl.device_type(); if let Err(e) = msg_socket.send(Response::DeviceType(result)) { panic!("responding to DeviceType failed: {}", e); } } Ok(Request::QueueMaxSizes) => { let result = wl.queue_max_sizes(); if let Err(e) = msg_socket.send(Response::QueueMaxSizes(result)) { panic!("responding to QueueMaxSizes failed: {}", e); } } Ok(Request::Features) => { let result = wl.features(); if let Err(e) = msg_socket.send(Response::Features(result)) { panic!("responding to Features failed: {}", e); } } Ok(Request::AckFeatures(value)) => wl.ack_features(value), Ok(Request::ReadConfig { offset, len }) => { let mut data = vec![0; len]; wl.read_config(offset, &mut data); if let Err(e) = msg_socket.send(Response::ReadConfig(data)) { panic!("responding to ReadConfig failed: {}", e); } } Ok(Request::WriteConfig { offset, ref data }) => wl.write_config(offset, data), Ok(Request::Activate { shm, interrupt, interrupt_resample_evt, in_queue, out_queue, in_queue_evt, out_queue_evt, }) => { let shm = shm.owned(); let regions = arch_memory_regions(memory_params); let mem = GuestMemory::with_memfd(®ions, shm).expect("GuestMemory::with_memfd failed"); let interrupt: MsgSocket = MsgSocket::new(interrupt.owned()); wl.activate( mem, Box::new(InterruptProxy::new( interrupt, interrupt_resample_evt.owned(), )), vec![in_queue, out_queue], vec![in_queue_evt.owned(), out_queue_evt.owned()], ); println!("activated Wl"); } Ok(Request::Reset) => { let result = wl.reset(); if let Err(e) = msg_socket.send(Response::Reset(result)) { panic!("responding to Reset failed: {}", e); } } Ok(Request::GetDeviceBars(address)) => { let result = wl.get_device_bars(address); if let Err(e) = msg_socket.send(Response::GetDeviceBars(result)) { panic!("responding to GetDeviceBars failed: {}", e); } } Ok(Request::GetDeviceCaps) => { let result = wl .get_device_caps() .into_iter() .map(|c| RemotePciCapability::from(&*c)) .collect(); if let Err(e) = msg_socket.send(Response::GetDeviceCaps(result)) { panic!("responding to GetDeviceCaps failed: {}", e); } } Ok(Request::Kill) => { // Will block until worker shuts down. drop(wl); if let Err(e) = msg_socket.send(Response::Kill) { error!("responding to Kill failed: {}", e); } break; } Ok(msg @ Request::Create { .. }) => panic!("unexpected message {:?}", msg), Err(e) => panic!("recv failed: {}", e), } } }