Skip to content
Merged
Show file tree
Hide file tree
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
39 changes: 0 additions & 39 deletions issues/getsockname-answers-an-address-nobody-asked-netstack-for.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ test depends on the public internet.

The first paired session (`ToyOSOrg/evidence` branch `pr-1065`, `r3/`) put
ToyOS's T14 LAN download and 64 B round trip at Linux's and its upload at
a third of it (`issues/tcp-opens-a-connections-window-slower-than-linux-in-its-first-300-ms.md`,
where what remains of that is now recorded).
a third of it. That gap was closed by #1069, #1102, #1112 and #1114, and
the T14 parity session of #1147 read the upload and its first 300 ms at
Linux's ([evidence](https://github.com/ToyOSOrg/evidence/tree/1c9f4c83c362a1b30746f1e096ad412a2d5f25f9/r2/session)).

The cryptographic rows are not osbench's. AES-GCM, ChaCha20-Poly1305,
SHA-256 and the rest were measured under Linux on both benches against ring,
Expand Down

This file was deleted.

2 changes: 0 additions & 2 deletions issues/toyos-runs-in-any-open-source-hypervisor.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ and, filed before this track:
- `issues/a-machine-without-an-iommu-refuses-every-claim.md` — bhyve; crosvm
for every device it emulates; and VirtualBox, QEMU and Cloud Hypervisor by
default (`issues/no-claim-gets-an-address-space-under-virtio-iommu.md`).
- `issues/getsockname-answers-an-address-nobody-asked-netstack-for.md` —
QEMU's user-mode network address in `sdk/libc`.
- `issues/pci-is-enumerated-on-segment-group-0-alone.md` — Cloud
Hypervisor's `num_pci_segments`.

Expand Down
12 changes: 6 additions & 6 deletions sdk/libc/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,23 +825,23 @@ pub unsafe extern "C" fn getsockname(
addr: *mut Sockaddr,
addrlen: *mut SocklenT,
) -> i32 {
let local_port = match sock_from_fd(fd) {
let (ip, port) = match sock_from_fd(fd) {
Some(Sock::Stream(held)) => match stream::endpoints(held.handle()) {
Ok((_, local_port)) => local_port,
Ok((_, local)) => local,
Err(refused) => { set_errno(crate::errcode::of_stream(refused)); return -1; }
},
Some(Sock::Listener(held) | Sock::Datagram(held)) => match socket::local(held.handle()) {
Ok((_, port)) => port,
Ok(local) => local,
Err(e) => { set_errno(net_err_to_errno(e)); return -1; }
},
// Neither bound nor connected: the unspecified address, and no port.
Some(Sock::Entry(mut socket)) => match *socket.slot() {
Some(_) => 0,
Some(_) => ([0; 4], 0),
None => { set_errno(EBADF); return -1; }
},
None => { set_errno(EBADF); return -1; }
};
// Local address: 10.0.2.15 (QEMU default)
fill_sockaddr(addr, addrlen, [10, 0, 2, 15], local_port);
fill_sockaddr(addr, addrlen, ip, port);
0
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/std/sys/net/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ impl TcpStream {
}

pub fn socket_addr(&self) -> io::Result<SocketAddr> {
let (_, local_port) = stream::endpoints(self.handle()).map_err(stream_refused)?;
Ok(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(10, 0, 2, 15), local_port)))
let (_, (addr, port)) = stream::endpoints(self.handle()).map_err(stream_refused)?;
Ok(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::from(addr), port)))
}

pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
Expand Down
27 changes: 20 additions & 7 deletions sdk/toyos/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ pub enum NetError {
Io,
}

/// Whether `addr` can be a host's own: neither unspecified, multicast nor broadcast, the
/// check an answer naming this machine's end of a stream or a connected socket passes.
fn unicast(addr: [u8; 4]) -> bool {
let addr = core::net::Ipv4Addr::from(addr);
!addr.is_unspecified() && !addr.is_multicast() && !addr.is_broadcast()
}

impl NetError {
pub fn from_error_code(code: u32) -> Self {
match code {
Expand Down Expand Up @@ -185,8 +192,9 @@ ipc_payload! {
}

/// The answer to [`MsgType::TcpConnect`], with the stream's bells
/// (`net::stream::BELLS`).
/// (`net::stream::BELLS`): the address and port the stream left from.
pub struct TcpConnectResponse {
pub local_addr: [u8; 4],
pub local_port: u16,
pub _pad: u16,
}
Expand Down Expand Up @@ -221,6 +229,7 @@ ipc_payload! {
pub remote_addr: [u8; 4],
pub remote_port: u16,
pub local_port: u16,
pub local_addr: [u8; 4],
pub nodelay: u32,
}

Expand Down Expand Up @@ -539,19 +548,23 @@ mod tests {
));
}

/// The accept's answer as a client decodes it: the peer, the port, and
/// `nodelay` last, any non-zero word on, and an answer without it refused.
/// The accept's answer as a client decodes it: the peer, the local port
/// and address, and `nodelay` last, any non-zero word on, and an answer
/// without it refused.
#[test]
fn an_accepts_answer_ends_in_its_streams_nodelay() {
let mut wire = [192, 0, 2, 7, 0x39, 0x30, 0x16, 0, 0, 0, 0, 0];
let mut wire = [192, 0, 2, 7, 0x39, 0x30, 0x16, 0, 198, 51, 100, 9, 0, 0, 0, 0];
let answer: TcpAcceptResponse = crate::ipc::decode_payload(&wire).unwrap();
assert_eq!((answer.remote_addr, answer.remote_port, answer.local_port), ([192, 0, 2, 7], 12345, 22));
assert_eq!(
(answer.remote_addr, answer.remote_port, answer.local_port, answer.local_addr),
([192, 0, 2, 7], 12345, 22, [198, 51, 100, 9])
);
assert_eq!(answer.nodelay, 0);
wire[8..].copy_from_slice(&[0, 0, 0, 0x80]);
wire[12..].copy_from_slice(&[0, 0, 0, 0x80]);
let answer: TcpAcceptResponse = crate::ipc::decode_payload(&wire).unwrap();
assert_ne!(answer.nodelay, 0);
assert!(matches!(
crate::ipc::decode_payload::<TcpAcceptResponse>(&wire[..8]),
crate::ipc::decode_payload::<TcpAcceptResponse>(&wire[..12]),
Err(IpcError::Malformed)
));
}
Expand Down
46 changes: 39 additions & 7 deletions sdk/toyos/src/net/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ crate::ipc_payload! {
pub value: u32,
}

/// [`PEER`]'s answer: the peer a connect named, and the address the socket sends there from,
/// which the connect fixed.
#[derive(Debug, PartialEq, Eq)]
pub struct Peer {
pub addr: [u8; 4],
pub port: u16,
pub _pad: u16,
pub local_addr: [u8; 4],
}

/// An exported end's [`ADOPT`]: what kind of socket it holds, and the
/// address and port it is at.
pub struct Adopt {
Expand Down Expand Up @@ -371,10 +381,16 @@ pub fn is_socket(handle: BorrowedHandle<'_>) -> bool {
kind(handle).is_some()
}

/// The address the socket `handle` reaches is at, as its listen or bind named
/// it, and the port it holds.
/// The address the socket `handle` reaches is at and the port it holds: a
/// connected datagram socket's is the one its connect fixed, as netstack answers
/// it, and any other's is the one its listen or bind named.
pub fn local(handle: BorrowedHandle<'_>) -> Result<([u8; 4], u16), NetError> {
TABLE.lock().get(handle.raw()).map(|end| (end.addr, end.port)).ok_or(NetError::NotASocket)
let end = TABLE.lock().get(handle.raw()).ok_or(NetError::NotASocket)?;
let connected = match end.kind {
Kind::Datagram => connected(handle)?.map(|peer| peer.local_addr),
Kind::Listener => None,
};
Ok((connected.unwrap_or(end.addr), end.port))
}

/// `handle`, which reaches one of this process's sockets, as a descriptor's object: let go by
Expand Down Expand Up @@ -459,15 +475,21 @@ pub fn connect(handle: BorrowedHandle<'_>, addr: [u8; 4], port: u16) -> Result<(

/// The peer the socket `handle` reaches is connected to, by any of its holders.
pub fn peer(handle: BorrowedHandle<'_>) -> Result<Option<([u8; 4], u16)>, NetError> {
Ok(connected(handle)?.map(|peer| (peer.addr, peer.port)))
}

/// netstack's [`PEER`] answer for the datagram socket `handle` reaches, `None` while no
/// holder connected it. One naming a local address no socket sends from is refused.
fn connected(handle: BorrowedHandle<'_>) -> Result<Option<Peer>, NetError> {
call_on(handle, Kind::Datagram, |slot, end| {
let _turn = TURNS[slot].lock();
ipc::signal(end.connection, PEER).map_err(hangup)?;
let mut rx = FrameRx::<MAX_ANSWER>::new();
match answer(&end.connection, &mut rx) {
Ok(frame) => {
let peer: Datagram = ipc::decode_payload(rx.payload(frame.payload_len)).map_err(|_| NetError::Protocol(PEER))?;
Ok(Some((peer.addr, peer.port)))
}
Ok(frame) => match ipc::decode_payload::<Peer>(rx.payload(frame.payload_len)) {
Ok(peer) if super::unicast(peer.local_addr) => Ok(Some(peer)),
_ => Err(NetError::Protocol(PEER)),
},
Err(NetError::NotConnected) => Ok(None),
Err(e) => Err(e),
}
Expand Down Expand Up @@ -579,6 +601,16 @@ pub(super) fn adopted(connection: Connection, payload: &[u8], handles: ipc::Batc
mod tests {
use super::*;

/// [`PEER`]'s answer as a holder decodes it: the peer, its port, then the address the
/// socket sends from; an answer without that address is refused.
#[test]
fn a_peers_answer_ends_in_the_address_it_is_sent_from() {
let wire = [192, 0, 2, 7, 53, 0, 0, 0, 198, 51, 100, 9];
let peer: Peer = ipc::decode_payload(&wire).unwrap();
assert_eq!(peer, Peer { addr: [192, 0, 2, 7], port: 53, _pad: 0, local_addr: [198, 51, 100, 9] });
assert!(ipc::decode_payload::<Peer>(&wire[..8]).is_err());
}

/// A datagram is one frame's payload: its far end, then exactly its bytes, read back as it
/// was written, whatever bytes it carries, an empty one too.
#[test]
Expand Down
Loading