summary refs log tree commit diff
path: root/src/wl.rs
blob: 7177f7446a09322c146bebb7d0481f06ae695313 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// SPDX-License-Identifier: BSD-3-Clause

use devices::virtio::{
    BincodeRequest, BincodeResponse, InterruptProxy, InterruptProxyEvent, MsgOnSocketRequest,
    MsgOnSocketResponse, RemotePciCapability, VirtioDevice, Wl,
};
use msg_socket::MsgSocket;
use poly_msg_socket::PolyMsgSocket;
use std::collections::BTreeMap;
use std::fs::remove_file;
use sys_util::{error, net::UnixSeqpacketListener, warn, 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 =
    PolyMsgSocket<MsgOnSocketResponse, MsgOnSocketRequest, BincodeResponse, BincodeRequest>;

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 = PolyMsgSocket::new(conn);

    let (vm_socket, memory_params) = match msg_socket.recv() {
        Ok(poly_msg_socket::Value::MsgOnSocket(MsgOnSocketRequest::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 {
        use poly_msg_socket::Value::*;
        match msg_socket.recv() {
            Ok(MsgOnSocket(MsgOnSocketRequest::Kill)) => {
                // Will block until worker shuts down.
                drop(wl);

                if let Err(e) = msg_socket.send(MsgOnSocketResponse::Kill) {
                    error!("responding to Kill failed: {}", e);
                }

                break;
            }

            Ok(MsgOnSocket(MsgOnSocketRequest::AckFeatures(value))) => wl.ack_features(value),

            Ok(Bincode(BincodeRequest::ReadConfig { offset, len })) => {
                let mut data = vec![0; len];
                wl.read_config(offset, &mut data);
                if let Err(e) = msg_socket.send(BincodeResponse::ReadConfig(data)) {
                    panic!("responding to ReadConfig failed: {}", e);
                }
            }

            Ok(Bincode(BincodeRequest::WriteConfig { offset, ref data })) => {
                wl.write_config(offset, data)
            }

            Ok(MsgOnSocket(MsgOnSocketRequest::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(&regions, shm).expect("GuestMemory::with_memfd failed");

                let interrupt: MsgSocket<InterruptProxyEvent, ()> =
                    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(MsgOnSocket(MsgOnSocketRequest::Reset)) => {
                let result = wl.reset();
                if let Err(e) = msg_socket.send(MsgOnSocketResponse::Reset(result)) {
                    panic!("responding to Reset failed: {}", e);
                }
            }

            Ok(Bincode(BincodeRequest::GetDeviceBars(address))) => {
                let result = wl.get_device_bars(address);
                if let Err(e) = msg_socket.send(BincodeResponse::GetDeviceBars(result)) {
                    panic!("responding to GetDeviceBars failed: {}", e);
                }
            }

            Ok(Bincode(BincodeRequest::GetDeviceCaps)) => {
                let result = wl
                    .get_device_caps()
                    .into_iter()
                    .map(|c| RemotePciCapability::from(&*c))
                    .collect();
                if let Err(e) = msg_socket.send(BincodeResponse::GetDeviceCaps(result)) {
                    panic!("responding to GetDeviceCaps failed: {}", e);
                }
            }

            Ok(MsgOnSocket(msg @ MsgOnSocketRequest::Create { .. })) => {
                panic!("unexpected message {:?}", msg)
            }

            Err(e) => panic!("recv failed: {}", e),
        }
    }
}