vsock_service_lib/
lib.rs

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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#![recursion_limit = "512"]

mod addr;
mod port;
mod service;

pub use self::service::Vsock;

#[cfg(test)]
mod tests {
    use super::*;
    use fidl::endpoints::{self, Proxy};
    use fidl_fuchsia_hardware_vsock::{
        CallbacksProxy, DeviceMarker, DeviceRequest, DeviceRequestStream, VMADDR_CID_HOST,
        VMADDR_CID_LOCAL,
    };
    use fidl_fuchsia_vsock::{
        AcceptorMarker, AcceptorRequest, ConnectionMarker, ConnectionProxy, ConnectionTransport,
        ConnectorMarker, ConnectorProxy, ListenerMarker,
    };
    use fuchsia_async as fasync;
    use futures::{channel, future, FutureExt, StreamExt, TryFutureExt};
    struct MockDriver {
        client: DeviceRequestStream,
        callbacks: CallbacksProxy,
    }

    impl MockDriver {
        fn new(client: DeviceRequestStream, callbacks: CallbacksProxy) -> Self {
            MockDriver { client, callbacks }
        }
    }

    macro_rules! unwrap_msg {
        ($msg:path{$($bindings:tt)*} from $stream:expr) => {
            if let Some(Ok($msg{$($bindings)*})) = $stream.next().await {
                ($($bindings)*)
            } else {
                panic!("Expected msg {}", stringify!($msg));
            }
        }
    }

    async fn common_setup() -> Result<(MockDriver, Vsock), anyhow::Error> {
        let (driver_client, driver_server) = endpoints::create_endpoints::<DeviceMarker>();
        let mut driver_server = driver_server.into_stream();

        // Vsock::new expects to be able to communication with a running driver instance.
        // As we don't have a driver instance we spin up an asynchronous thread that will
        // momentarily pretend to be the driver to receive the callbacks, and then send
        // those callbacks over the below oneshot channel that we can then receive after
        // Vsock::new completes.
        let (tx, rx) = channel::oneshot::channel();
        fasync::Task::spawn(async move {
            let (cb, responder) =
                unwrap_msg!(DeviceRequest::Start{cb, responder} from driver_server);
            let driver_callbacks = cb.into_proxy();
            responder.send(Ok(())).unwrap();
            let _ = tx.send((driver_server, driver_callbacks));
        })
        .detach();

        let (service, event_loop) = Vsock::new(None, Some(driver_client.into_proxy())).await?;
        fasync::Task::local(event_loop.map_err(|x| panic!("Event loop stopped {}", x)).map(|_| ()))
            .detach();
        let (driver_server, driver_callbacks) = rx.await?;
        let driver = MockDriver::new(driver_server, driver_callbacks);
        Ok((driver, service))
    }

    fn make_con() -> Result<(zx::Socket, ConnectionProxy, ConnectionTransport), anyhow::Error> {
        let (client_socket, server_socket) = zx::Socket::create_stream();
        let (client_end, server_end) = endpoints::create_endpoints::<ConnectionMarker>();
        let client_end = client_end.into_proxy();
        let con = ConnectionTransport { data: server_socket, con: server_end };
        Ok((client_socket, client_end, con))
    }

    fn make_client(service: &Vsock) -> Result<ConnectorProxy, anyhow::Error> {
        let (app_client, app_remote) = endpoints::create_endpoints::<ConnectorMarker>();
        let app_client = app_client.into_proxy();
        // Run the client
        fasync::Task::local(
            Vsock::run_client_connection(service.clone(), app_remote.into_stream())
                .then(|_| future::ready(())),
        )
        .detach();
        Ok(app_client)
    }

    #[fasync::run_until_stalled(test)]
    async fn basic_listen() -> Result<(), anyhow::Error> {
        let (mut driver, service) = common_setup().await?;

        let app_client = make_client(&service)?;

        // Should reject listening at the ephemeral port ranges.
        {
            let (acceptor_remote, _acceptor_client) =
                endpoints::create_endpoints::<AcceptorMarker>();
            assert_eq!(
                app_client.listen(49152, acceptor_remote).await?,
                Err(zx::sys::ZX_ERR_UNAVAILABLE)
            );
        }

        // Listen on a reasonable value.
        let (acceptor_remote, acceptor_client) = endpoints::create_endpoints::<AcceptorMarker>();
        assert_eq!(app_client.listen(8000, acceptor_remote).await?, Ok(()));
        let mut acceptor_client = acceptor_client.into_stream();

        // Validate that we cannot listen twice
        {
            let (acceptor_remote, _acceptor_client) =
                endpoints::create_endpoints::<AcceptorMarker>();
            assert_eq!(
                app_client.listen(8000, acceptor_remote).await?,
                Err(zx::sys::ZX_ERR_ALREADY_BOUND)
            );
        }

        // Create a connection from the driver
        driver.callbacks.request(&*addr::Vsock::new(8000, 80, VMADDR_CID_HOST))?;
        let (_data_socket, _client_end, con) = make_con()?;

        let (_, responder) =
            unwrap_msg!(AcceptorRequest::Accept{addr, responder} from acceptor_client);
        responder.send(Some(con))?;

        // expect a response
        let (_, _server_data_socket, responder) =
            unwrap_msg!(DeviceRequest::SendResponse{addr, data, responder} from driver.client);
        responder.send(Ok(()))?;

        Ok(())
    }

    #[fasync::run_until_stalled(test)]
    async fn basic_bind_and_listen() -> Result<(), anyhow::Error> {
        let (mut driver, service) = common_setup().await?;
        let app_client = make_client(&service)?;
        let (_, listener_remote) = endpoints::create_endpoints::<ListenerMarker>();

        // Should reject listening at the ephemeral port ranges.
        assert_eq!(
            app_client.bind(VMADDR_CID_LOCAL, 49152, listener_remote).await?,
            Err(zx::sys::ZX_ERR_UNAVAILABLE)
        );

        let (listener_client, listener_remote) = endpoints::create_endpoints::<ListenerMarker>();
        // Listen on a reasonable value.
        assert_eq!(app_client.bind(VMADDR_CID_LOCAL, 8000, listener_remote).await?, Ok(()));

        // Validate that we cannot listen twice
        let (_, listener_remote2) = endpoints::create_endpoints::<ListenerMarker>();
        assert_eq!(
            app_client.bind(VMADDR_CID_LOCAL, 8000, listener_remote2).await?,
            Err(zx::sys::ZX_ERR_ALREADY_BOUND)
        );

        let listener_client = listener_client.into_proxy();
        assert_eq!(listener_client.listen(1).await?, Ok(()));

        // Create a connection from the driver
        driver.callbacks.request(&*addr::Vsock::new(8000, 80, VMADDR_CID_LOCAL))?;
        let (_data_socket, _client_end, con) = make_con()?;

        let accept_fut = listener_client.accept(con);

        // expect a response
        let (_, _server_data_socket, responder) =
            unwrap_msg!(DeviceRequest::SendResponse{addr, data, responder} from driver.client);
        responder.send(Ok(()))?;

        assert_eq!(accept_fut.await?, Ok(*addr::Vsock::new(8000, 80, VMADDR_CID_LOCAL)));

        Ok(())
    }

    #[fasync::run_until_stalled(test)]
    async fn reject_connection() -> Result<(), anyhow::Error> {
        let (mut driver, service) = common_setup().await?;

        let app_client = make_client(&service)?;

        // Send a connection request
        let (_data_socket, _client_end, con) = make_con()?;
        let request = app_client.connect(VMADDR_CID_HOST, 8000, con);

        // Expect a driver message
        {
            let (addr, _server_data_socket, responder) =
                unwrap_msg!(DeviceRequest::SendRequest{addr, data, responder} from driver.client);
            responder.send(Ok(()))?;
            // Now simulate an incoming RST for a rejected connection
            driver.callbacks.rst(&addr)?;
            // Leave this scope to drop the server_data_socket
        }
        // Request should resolve to an error
        assert_eq!(request.await?, Err(zx::sys::ZX_ERR_UNAVAILABLE));
        Ok(())
    }

    #[fasync::run_until_stalled(test)]
    async fn transport_reset() -> Result<(), anyhow::Error> {
        let (mut driver, service) = common_setup().await?;

        let app_client = make_client(&service)?;

        // Create a connection.
        let (_data_socket_request, client_end_request, con) = make_con()?;
        let request = app_client.connect(VMADDR_CID_HOST, 8000, con);
        let (addr, server_data_socket_request, responder) =
            unwrap_msg!(DeviceRequest::SendRequest{addr, data, responder} from driver.client);
        responder.send(Ok(()))?;
        driver.callbacks.response(&addr)?;
        let _ = request.await?.map_err(zx::Status::from_raw)?;

        // Start a listener
        let (acceptor_remote, acceptor_client) = endpoints::create_endpoints::<AcceptorMarker>();
        assert_eq!(app_client.listen(9000, acceptor_remote).await?, Ok(()));
        let mut acceptor_client = acceptor_client.into_stream();

        // Perform a transport reset
        drop(server_data_socket_request);
        driver.callbacks.transport_reset(7).await?;

        // Connection should be closed
        client_end_request.on_closed().await?;

        // Listener should still be active and receive a connection
        driver.callbacks.request(&*addr::Vsock::new(9000, 80, VMADDR_CID_HOST))?;
        let (_data_socket, _client_end, _con) = make_con()?;

        let (_addr, responder) =
            unwrap_msg!(AcceptorRequest::Accept{addr, responder} from acceptor_client);
        drop(responder); // don't bother responding, we're done

        Ok(())
    }
}