Skip to content

Commit 8f8343b

Browse files
committed
Connect to gvproxy socket
Connecting to the socket set the remote address so we can use send()/recv() or write()/read() instead of sendto()/recvfrom(). Advantages: - Simpler code, no need to keep the remote address. - The server will want to connect to the client address to ensure it receives frames only from the connected client. Such server will want to remove the unix socket once the client connected[2], which doe snot work with current code. - Once the socket is connected, the same backend can be used to handle passed file descriptor[1]. - iperf3 -R is 1.33 times faster (46.6 Gbits/s vs 35.0 Gbits/s). Tested with: - [x] gvproxy - [x] vmnet-helper For testing results see #264 (comment) [1] containers/krunkit#24 [2] https://github.com/nirs/vmnet-helper/blob/5c6a595ba3e76314e1d0bef2b0160388439d69ec/helper.c#L475 Signed-off-by: Nir Soffer <[email protected]>
1 parent 57a5a6b commit 8f8343b

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/devices/src/virtio/net/gvproxy.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use nix::fcntl::{fcntl, FcntlArg, OFlag};
22
use nix::sys::socket::{
3-
bind, getsockopt, recv, sendto, setsockopt, socket, sockopt, AddressFamily, MsgFlags, SockFlag,
4-
SockType, UnixAddr,
3+
bind, connect, getsockopt, recv, send, setsockopt, socket, sockopt, AddressFamily, MsgFlags,
4+
SockFlag, SockType, UnixAddr,
55
};
66
use nix::unistd::unlink;
77
use std::os::fd::{AsRawFd, RawFd};
@@ -13,7 +13,6 @@ const VFKIT_MAGIC: [u8; 4] = *b"VFKT";
1313

1414
pub struct Gvproxy {
1515
fd: RawFd,
16-
peer_addr: UnixAddr,
1716
}
1817

1918
impl Gvproxy {
@@ -34,8 +33,11 @@ impl Gvproxy {
3433
}
3534
bind(fd, &local_addr).map_err(ConnectError::Binding)?;
3635

37-
sendto(fd, &VFKIT_MAGIC, &peer_addr, MsgFlags::empty())
38-
.map_err(ConnectError::SendingMagic)?;
36+
// connect so we dn't need to use peer address again. This also allows the
37+
// server to remove the socket after the connection.
38+
connect(fd, &peer_addr).map_err(ConnectError::Binding)?;
39+
40+
send(fd, &VFKIT_MAGIC, MsgFlags::empty()).map_err(ConnectError::SendingMagic)?;
3941

4042
// macOS forces us to do this here instead of just using SockFlag::SOCK_NONBLOCK above.
4143
match fcntl(fd, FcntlArg::F_GETFL) {
@@ -78,7 +80,7 @@ impl Gvproxy {
7880
getsockopt(fd, sockopt::RcvBuf)
7981
);
8082

81-
Ok(Self { fd, peer_addr })
83+
Ok(Self { fd })
8284
}
8385
}
8486

@@ -110,8 +112,8 @@ impl NetBackend for Gvproxy {
110112
/// If this function returns WriteError::PartialWrite, you have to finish the write using
111113
/// try_finish_write.
112114
fn write_frame(&mut self, hdr_len: usize, buf: &mut [u8]) -> Result<(), WriteError> {
113-
let ret = sendto(self.fd, &buf[hdr_len..], &self.peer_addr, MsgFlags::empty())
114-
.map_err(WriteError::Internal)?;
115+
let ret =
116+
send(self.fd, &buf[hdr_len..], MsgFlags::empty()).map_err(WriteError::Internal)?;
115117
debug!(
116118
"Written frame size={}, written={}",
117119
buf.len() - hdr_len,

0 commit comments

Comments
 (0)