Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to gvproxy socket #263

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/devices/src/virtio/net/gvproxy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use nix::fcntl::{fcntl, FcntlArg, OFlag};
use nix::sys::socket::{
bind, getsockopt, recv, sendto, setsockopt, socket, sockopt, AddressFamily, MsgFlags, SockFlag,
SockType, UnixAddr,
bind, connect, getsockopt, recv, send, setsockopt, socket, sockopt, AddressFamily, MsgFlags,
SockFlag, SockType, UnixAddr,
};
use nix::unistd::unlink;
use std::os::fd::{AsRawFd, RawFd};
Expand All @@ -13,7 +13,6 @@ const VFKIT_MAGIC: [u8; 4] = *b"VFKT";

pub struct Gvproxy {
fd: RawFd,
peer_addr: UnixAddr,
}

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

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

send(fd, &VFKIT_MAGIC, MsgFlags::empty()).map_err(ConnectError::SendingMagic)?;

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

Ok(Self { fd, peer_addr })
Ok(Self { fd })
}
}

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