hyper_client_sockets/
firecracker.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use std::{
    error::Error,
    io::ErrorKind,
    path::{Path, PathBuf},
    pin::Pin,
    task::{Context, Poll},
};

use futures_util::{io::BufReader, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, StreamExt};
use hex::FromHex;
use hyper::Uri;
use pin_project::pin_project;
#[cfg(feature = "tokio-backend")]
use tokio_util::compat::TokioAsyncReadCompatExt;

use crate::{io_input_err, Backend};

/// An extension trait for hyper URI allowing work with Firecracker URIs
pub trait FirecrackerUriExt {
    /// Create a new Firecracker URI with the given host socket path, guest port and in-socket URL
    fn firecracker(
        host_socket_path: impl AsRef<Path>,
        guest_port: u32,
        url: impl AsRef<str>,
    ) -> Result<Uri, Box<dyn Error>>;

    /// Deconstruct this Firecracker URI into its host socket path and guest port
    fn parse_firecracker(&self) -> Result<(PathBuf, u32), std::io::Error>;
}

impl FirecrackerUriExt for Uri {
    fn firecracker(
        host_socket_path: impl AsRef<Path>,
        guest_port: u32,
        url: impl AsRef<str>,
    ) -> Result<Uri, Box<dyn Error>> {
        let host = hex::encode(format!(
            "{}:{guest_port}",
            host_socket_path.as_ref().to_string_lossy().to_string()
        ));
        let uri_str = format!("fc://{host}/{}", url.as_ref().trim_start_matches('/'));
        let uri = uri_str.parse::<Uri>().map_err(|err| Box::new(err))?;
        Ok(uri)
    }

    fn parse_firecracker(&self) -> Result<(PathBuf, u32), std::io::Error> {
        if self.scheme_str() != Some("fc") {
            return Err(io_input_err("URI scheme on a Firecracker socket must be fc://"));
        }

        let host = self.host().ok_or_else(|| io_input_err("URI host must be present"))?;
        let hex_decoded = Vec::from_hex(host).map_err(|_| io_input_err("URI host must be hex"))?;
        let full_str = String::from_utf8_lossy(&hex_decoded).into_owned();
        let splits = full_str
            .split_once(':')
            .ok_or_else(|| io_input_err("URI host could not be split in halves with a ."))?;
        let host_socket_path = PathBuf::try_from(splits.0)
            .map_err(|_| io_input_err("URI socket path could not be converted to a path"))?;
        let guest_port: u32 = splits
            .1
            .parse()
            .map_err(|_| io_input_err("URI guest port could not converted to u32"))?;

        Ok((host_socket_path, guest_port))
    }
}

/// A hyper-compatible Firecracker Unix socket connection.
#[pin_project]
#[derive(Debug)]
pub struct HyperFirecrackerStream {
    #[pin]
    inner: FirecrackerStreamInner,
}

#[pin_project(project = FirecrackerStreamProj)]
#[derive(Debug)]
enum FirecrackerStreamInner {
    #[cfg(feature = "tokio-backend")]
    Tokio {
        #[pin]
        stream: hyper_util::rt::TokioIo<tokio::net::UnixStream>,
    },
    #[cfg(feature = "async-io-backend")]
    AsyncIo {
        #[pin]
        stream: smol_hyper::rt::FuturesIo<async_io::Async<std::os::unix::net::UnixStream>>,
    },
    #[cfg(all(not(feature = "tokio-backend"), not(feature = "async-io-backend")))]
    None(()),
}

impl HyperFirecrackerStream {
    /// Connect to the host socket path and tunnel to the guest port using the given [Backend].
    pub async fn connect(
        host_socket_path: impl AsRef<Path>,
        guest_port: u32,
        backend: Backend,
    ) -> Result<Self, std::io::Error> {
        match backend {
            #[cfg(feature = "tokio-backend")]
            Backend::Tokio => {
                let mut stream = tokio::net::UnixStream::connect(host_socket_path.as_ref())
                    .await?
                    .compat();
                authenticate(guest_port, &mut stream).await?;

                Ok(Self {
                    inner: FirecrackerStreamInner::Tokio {
                        stream: hyper_util::rt::TokioIo::new(stream.into_inner()),
                    },
                })
            }
            #[cfg(feature = "async-io-backend")]
            Backend::AsyncIo => {
                let mut async_stream =
                    async_io::Async::<std::os::unix::net::UnixStream>::connect(host_socket_path.as_ref()).await?;
                authenticate(guest_port, &mut async_stream).await?;

                Ok(Self {
                    inner: FirecrackerStreamInner::AsyncIo {
                        stream: smol_hyper::rt::FuturesIo::new(async_stream),
                    },
                })
            }
            #[allow(unreachable_patterns)]
            _ => panic!("No hyper-client-sockets backend was configured"),
        }
    }
}

async fn authenticate(
    guest_port: u32,
    mut stream: impl AsyncReadExt + AsyncWriteExt + Unpin,
) -> Result<(), std::io::Error> {
    stream.write_all(format!("CONNECT {guest_port}\n").as_bytes()).await?;
    let mut buf_reader = BufReader::new(&mut stream).lines();
    match buf_reader.next().await {
        Some(Ok(line)) => {
            if !line.starts_with("OK") {
                return Err(std::io::Error::new(
                    ErrorKind::ConnectionRefused,
                    "Firecracker refused to establish a tunnel to the given guest port",
                ));
            }
        }
        _ => return Err(io_input_err("Could not read response")),
    };

    Ok(())
}

impl hyper::rt::Read for HyperFirecrackerStream {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: hyper::rt::ReadBufCursor<'_>,
    ) -> Poll<Result<(), std::io::Error>> {
        match self.project().inner.project() {
            #[cfg(feature = "tokio-backend")]
            FirecrackerStreamProj::Tokio { stream } => stream.poll_read(cx, buf),
            #[cfg(feature = "async-io-backend")]
            FirecrackerStreamProj::AsyncIo { stream } => stream.poll_read(cx, buf),
            #[allow(unreachable_patterns)]
            _ => panic!("No hyper-client-sockets backend was configured"),
        }
    }
}

impl hyper::rt::Write for HyperFirecrackerStream {
    fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize, std::io::Error>> {
        match self.project().inner.project() {
            #[cfg(feature = "tokio-backend")]
            FirecrackerStreamProj::Tokio { stream } => stream.poll_write(cx, buf),
            #[cfg(feature = "async-io-backend")]
            FirecrackerStreamProj::AsyncIo { stream } => stream.poll_write(cx, buf),
            #[allow(unreachable_patterns)]
            _ => panic!("No hyper-client-sockets backend was configured"),
        }
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), std::io::Error>> {
        match self.project().inner.project() {
            #[cfg(feature = "tokio-backend")]
            FirecrackerStreamProj::Tokio { stream } => stream.poll_flush(cx),
            #[cfg(feature = "async-io-backend")]
            FirecrackerStreamProj::AsyncIo { stream } => stream.poll_flush(cx),
            #[allow(unreachable_patterns)]
            _ => panic!("No hyper-client-sockets backend was configured"),
        }
    }

    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), std::io::Error>> {
        match self.project().inner.project() {
            #[cfg(feature = "tokio-backend")]
            FirecrackerStreamProj::Tokio { stream } => stream.poll_shutdown(cx),
            #[cfg(feature = "async-io-backend")]
            FirecrackerStreamProj::AsyncIo { stream } => stream.poll_shutdown(cx),
            #[allow(unreachable_patterns)]
            _ => panic!("No hyper-client-sockets backend was configured"),
        }
    }
}

#[cfg_attr(docsrs, doc(cfg(feature = "connector")))]
#[cfg(feature = "connector")]
pub mod connector {
    use std::{future::Future, pin::Pin, task::Poll};

    use http::Uri;
    use hyper_util::client::legacy::connect::{Connected, Connection};
    use tower_service::Service;

    use crate::Backend;

    use super::{FirecrackerUriExt, HyperFirecrackerStream};

    /// A hyper-util connector for Firecracker sockets.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct HyperFirecrackerConnector {
        /// The [Backend] to use when performing connections.
        pub backend: Backend,
    }

    impl Unpin for HyperFirecrackerConnector {}

    impl Connection for HyperFirecrackerStream {
        fn connected(&self) -> hyper_util::client::legacy::connect::Connected {
            Connected::new()
        }
    }

    impl Service<Uri> for HyperFirecrackerConnector {
        type Response = HyperFirecrackerStream;

        type Error = std::io::Error;

        type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

        fn poll_ready(&mut self, _cx: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
            Poll::Ready(Ok(()))
        }

        fn call(&mut self, req: Uri) -> Self::Future {
            let backend = self.backend;

            Box::pin(async move {
                let (host_socket_path, guest_port) = req.parse_firecracker()?;
                HyperFirecrackerStream::connect(host_socket_path, guest_port, backend).await
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use hyper::Uri;

    use crate::firecracker::FirecrackerUriExt;

    #[test]
    fn firecracker_uri_should_be_constructed_correctly() {
        let uri_str = format!("fc://{}/route", hex::encode("/tmp/socket.sock:1000"));
        assert_eq!(
            Uri::firecracker("/tmp/socket.sock", 1000, "/route").unwrap(),
            uri_str.parse::<Uri>().unwrap()
        );
    }

    #[test]
    fn firecracker_uri_should_be_deconstructed_correctly() {
        let uri = format!("fc://{}/route", hex::encode("/tmp/socket.sock:1000"))
            .parse::<Uri>()
            .unwrap();
        let (socket_path, port) = uri.parse_firecracker().unwrap();
        assert_eq!(socket_path, PathBuf::from("/tmp/socket.sock"));
        assert_eq!(port, 1000);
    }
}