Skip to content

Commit bb63011

Browse files
committed
devices/vsock: unix domain sockets backend
This is the first commit in the final season of the vsock saga. Building on top of the vhost-less vsock device and the vsock connection state machine, this brings the Unix Domain Sockets backend for vsock to Firecracker. The UDS backend-specific functionality resides mostly with the `VsockMuxer`, in `devices/src/virtio/vsock/unix/muxer.rs` - a vsock connection multiplexer, and a translator between guest-side AF_VSOCK and host-side AF_UNIX streams. Signed-off-by: Dan Horobeanu <[email protected]>
1 parent de83a9e commit bb63011

File tree

8 files changed

+1106
-43
lines changed

8 files changed

+1106
-43
lines changed

devices/src/virtio/vsock/csm/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
/// This module implements our vsock connection state machine. The heavy lifting is done by
55
/// `connection::VsockConnection`, while this file only defines some constants and helper structs.
66
///
7-
#[allow(dead_code)]
87
mod connection;
98
mod txbuf;
109

devices/src/virtio/vsock/mod.rs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ mod csm;
99
mod device;
1010
mod epoll_handler;
1111
mod packet;
12+
mod unix;
1213

1314
pub use self::defs::uapi::VIRTIO_ID_VSOCK as TYPE_VSOCK;
1415
pub use self::defs::EVENT_COUNT as VSOCK_EVENTS_COUNT;
1516
pub use self::device::Vsock;
16-
pub use DummyBackend as VsockUnixBackend;
17+
pub use self::unix::VsockUnixBackend;
1718

1819
use std::os::unix::io::RawFd;
1920
use std::sync::mpsc;
@@ -24,7 +25,6 @@ use super::super::EpollHandler;
2425
use super::EpollConfigConstructor;
2526
use packet::VsockPacket;
2627

27-
#[allow(dead_code)]
2828
mod defs {
2929
use crate::DeviceEventT;
3030

@@ -189,35 +189,6 @@ pub trait VsockChannel {
189189
/// translates guest-side vsock connections to host-side Unix domain socket connections.
190190
pub trait VsockBackend: VsockChannel + VsockEpollListener + Send {}
191191

192-
/// Placeholder implementor for a future vsock backend.
193-
pub struct DummyBackend {}
194-
impl DummyBackend {
195-
pub fn new(_cid: u64, _path: String) -> Result<Self> {
196-
Ok(Self {})
197-
}
198-
}
199-
impl VsockEpollListener for DummyBackend {
200-
fn get_polled_fd(&self) -> RawFd {
201-
-1
202-
}
203-
fn get_polled_evset(&self) -> epoll::Events {
204-
epoll::Events::empty()
205-
}
206-
fn notify(&mut self, _evset: epoll::Events) {}
207-
}
208-
impl VsockChannel for DummyBackend {
209-
fn recv_pkt(&mut self, _pkt: &mut VsockPacket) -> Result<()> {
210-
Ok(())
211-
}
212-
fn send_pkt(&mut self, _pkt: &VsockPacket) -> Result<()> {
213-
Ok(())
214-
}
215-
fn has_pending_rx(&self) -> bool {
216-
false
217-
}
218-
}
219-
impl VsockBackend for DummyBackend {}
220-
221192
#[cfg(test)]
222193
mod tests {
223194
use super::epoll_handler::VsockEpollHandler;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
5+
/// This module implements the Unix Domain Sockets backend for vsock - a mediator between
6+
/// guest-side AF_VSOCK sockets and host-side AF_UNIX sockets. The heavy lifting is performed by
7+
/// `muxer::VsockMuxer`, a connection multiplexer that uses `super::csm::VsockConnection` for
8+
/// handling vsock connection states.
9+
/// Check out `muxer.rs` for a more detailed explanation of the inner workings of this backend.
10+
///
11+
mod muxer;
12+
mod muxer_killq;
13+
mod muxer_rxq;
14+
15+
pub use muxer::VsockMuxer as VsockUnixBackend;
16+
17+
mod defs {
18+
/// Maximum number of established connections that we can handle.
19+
pub const MAX_CONNECTIONS: usize = 1023;
20+
21+
/// Size of the muxer RX packet queue.
22+
pub const MUXER_RXQ_SIZE: usize = 256;
23+
24+
/// Size of the muxer connection kill queue.
25+
pub const MUXER_KILLQ_SIZE: usize = 128;
26+
}
27+
28+
#[derive(Debug)]
29+
pub enum Error {
30+
/// Error registering a new epoll-listening FD.
31+
EpollAdd(std::io::Error),
32+
/// Error creating an epoll FD.
33+
EpollFdCreate(std::io::Error),
34+
/// The host made an invalid vsock port connection request.
35+
InvalidPortRequest,
36+
/// Error accepting a new connection from the host-side Unix socket.
37+
UnixAccept(std::io::Error),
38+
/// Error binding to the host-side Unix socket.
39+
UnixBind(std::io::Error),
40+
/// Error connecting to a host-side Unix socket.
41+
UnixConnect(std::io::Error),
42+
/// Error reading from host-side Unix socket.
43+
UnixRead(std::io::Error),
44+
/// Muxer connection limit reached.
45+
TooManyConnections,
46+
}
47+
48+
type Result<T> = std::result::Result<T, Error>;
49+
type MuxerConnection = super::csm::VsockConnection<std::os::unix::net::UnixStream>;

0 commit comments

Comments
 (0)