Skip to content

Commit 58e279f

Browse files
committed
unixgram: Make fd non-blocking
When creating unixgram backend from existing fd we need to make it non-blocking so the user does not have to care about this implementation detail. Disabling SIGPIPE is needed on the existing fd for the same reason. Fix by moving these steps from open() to new(). Authored-by: Nir Soffer <nirsof@gmail.com> Signed-off-by: Nir Soffer <nirsof@gmail.com> Signed-off-by: Jake Correnti <jakecorrenti+github@proton.me>
1 parent 57eeb22 commit 58e279f

1 file changed

Lines changed: 29 additions & 28 deletions

File tree

src/devices/src/virtio/net/unixgram.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,6 @@ pub struct Unixgram {
1919
impl Unixgram {
2020
/// Create the backend with a pre-established connection to the userspace network proxy.
2121
pub fn new(fd: RawFd) -> Self {
22-
Self { fd }
23-
}
24-
25-
/// Create the backend opening a connection to the userspace network proxy.
26-
pub fn open(path: PathBuf, send_vfkit_magic: bool) -> Result<Self, ConnectError> {
27-
let fd = socket(
28-
AddressFamily::Unix,
29-
SockType::Datagram,
30-
SockFlag::empty(),
31-
None,
32-
)
33-
.map_err(ConnectError::CreateSocket)?;
34-
let peer_addr = UnixAddr::new(&path).map_err(ConnectError::InvalidAddress)?;
35-
let local_addr = UnixAddr::new(&PathBuf::from(format!("{}-krun.sock", path.display())))
36-
.map_err(ConnectError::InvalidAddress)?;
37-
if let Some(path) = local_addr.path() {
38-
_ = unlink(path);
39-
}
40-
bind(fd, &local_addr).map_err(ConnectError::Binding)?;
41-
42-
// Connect so we don't need to use the peer address again. This also
43-
// allows the server to remove the socket after the connection.
44-
connect(fd, &peer_addr).map_err(ConnectError::Binding)?;
45-
46-
if send_vfkit_magic {
47-
send(fd, &VFKIT_MAGIC, MsgFlags::empty()).map_err(ConnectError::SendingMagic)?;
48-
}
49-
5022
// macOS forces us to do this here instead of just using SockFlag::SOCK_NONBLOCK above.
5123
match fcntl(fd, FcntlArg::F_GETFL) {
5224
Ok(flags) => match OFlag::from_bits(flags) {
@@ -75,6 +47,35 @@ impl Unixgram {
7547
};
7648
}
7749

50+
Self { fd }
51+
}
52+
53+
/// Create the backend opening a connection to the userspace network proxy.
54+
pub fn open(path: PathBuf, send_vfkit_magic: bool) -> Result<Self, ConnectError> {
55+
// We cannot create a non-blocking socket on macOS here. This is done later in new().
56+
let fd = socket(
57+
AddressFamily::Unix,
58+
SockType::Datagram,
59+
SockFlag::empty(),
60+
None,
61+
)
62+
.map_err(ConnectError::CreateSocket)?;
63+
let peer_addr = UnixAddr::new(&path).map_err(ConnectError::InvalidAddress)?;
64+
let local_addr = UnixAddr::new(&PathBuf::from(format!("{}-krun.sock", path.display())))
65+
.map_err(ConnectError::InvalidAddress)?;
66+
if let Some(path) = local_addr.path() {
67+
_ = unlink(path);
68+
}
69+
bind(fd, &local_addr).map_err(ConnectError::Binding)?;
70+
71+
// Connect so we don't need to use the peer address again. This also
72+
// allows the server to remove the socket after the connection.
73+
connect(fd, &peer_addr).map_err(ConnectError::Binding)?;
74+
75+
if send_vfkit_magic {
76+
send(fd, &VFKIT_MAGIC, MsgFlags::empty()).map_err(ConnectError::SendingMagic)?;
77+
}
78+
7879
if let Err(e) = setsockopt(fd, sockopt::SndBuf, &(7 * 1024 * 1024)) {
7980
log::warn!("Failed to increase SO_SNDBUF (performance may be decreased): {e}");
8081
}

0 commit comments

Comments
 (0)