Skip to content

Commit

Permalink
Merge pull request #1251 from microsoft/bugfix-inetstack-check-local-…
Browse files Browse the repository at this point in the history
…addr

[inetstack] Check bind to local IP
  • Loading branch information
ppenna committed May 3, 2024
2 parents 5b5bf06 + e2c8373 commit 2b949e3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 21 deletions.
15 changes: 10 additions & 5 deletions src/rust/demikernel/libos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl LibOS {
LibOSName::Catnap => Self::NetworkLibOS(NetworkLibOSWrapper::Catnap(SharedNetworkLibOS::<
SharedCatnapTransport,
>::new(
config.local_ipv4_addr(),
runtime.clone(),
SharedCatnapTransport::new(&config, &mut runtime),
))),
Expand All @@ -133,7 +134,9 @@ impl LibOS {
Self::NetworkLibOS(NetworkLibOSWrapper::Catpowder(SharedNetworkLibOS::<
SharedInetStack<LinuxRuntime>,
>::new(
runtime.clone(), inetstack
config.local_ipv4_addr(),
runtime.clone(),
inetstack,
)))
},
#[cfg(feature = "catnip-libos")]
Expand All @@ -146,7 +149,9 @@ impl LibOS {
Self::NetworkLibOS(NetworkLibOSWrapper::Catnip(SharedNetworkLibOS::<
SharedInetStack<SharedDPDKRuntime>,
>::new(
runtime.clone(), inetstack
config.local_ipv4_addr(),
runtime.clone(),
inetstack,
)))
},
#[cfg(feature = "catmem-libos")]
Expand All @@ -157,6 +162,7 @@ impl LibOS {
LibOSName::Catloop => Self::NetworkLibOS(NetworkLibOSWrapper::Catloop(SharedNetworkLibOS::<
SharedCatloopTransport,
>::new(
config.local_ipv4_addr(),
runtime.clone(),
SharedCatloopTransport::new(&config, runtime.clone()),
))),
Expand Down Expand Up @@ -512,9 +518,8 @@ impl LibOS {
pub fn wait_next_n<Acceptor: FnMut(demi_qresult_t) -> bool>(
&mut self,
acceptor: Acceptor,
timeout: Option<Duration>
) -> Result<(), Fail>
{
timeout: Option<Duration>,
) -> Result<(), Fail> {
timer!("demikernel::wait_next_n");
match self {
#[cfg(any(
Expand Down
35 changes: 24 additions & 11 deletions src/rust/demikernel/libos/network/libos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ use crate::pal::linux::socketaddrv4_to_sockaddr;
/// Catnap libOS. All state is kept in the [runtime] and [qtable].
/// TODO: Move [qtable] into [runtime] so all state is contained in the PosixRuntime.
pub struct NetworkLibOS<T: NetworkTransport> {
local_ipv4_addr: Ipv4Addr,
/// Underlying runtime.
runtime: SharedDemiRuntime,
/// Underlying network transport.
Expand All @@ -92,8 +93,9 @@ pub struct SharedNetworkLibOS<T: NetworkTransport>(SharedObject<NetworkLibOS<T>>
/// Associate Functions for Catnap LibOS
impl<T: NetworkTransport> SharedNetworkLibOS<T> {
/// Instantiates a Catnap LibOS.
pub fn new(runtime: SharedDemiRuntime, transport: T) -> Self {
pub fn new(local_ipv4_addr: Ipv4Addr, runtime: SharedDemiRuntime, transport: T) -> Self {
Self(SharedObject::new(NetworkLibOS::<T> {
local_ipv4_addr,
runtime: runtime.clone(),
transport,
}))
Expand Down Expand Up @@ -127,13 +129,24 @@ impl<T: NetworkTransport> SharedNetworkLibOS<T> {
pub fn bind(&mut self, qd: QDesc, mut local: SocketAddr) -> Result<(), Fail> {
trace!("bind() qd={:?}, local={:?}", qd, local);

// We only support IPv4 addresses right now.
let localv4: SocketAddrV4 = unwrap_socketaddr(local)?;
// Check if we are binding to the wildcard address. We only support this for UDP sockets right now.

// Check address that we are using to bind. We only support the wildcard address for UDP sockets right now.
// FIXME: https://github.com/demikernel/demikernel/issues/189
if localv4.ip() == &Ipv4Addr::UNSPECIFIED && self.get_shared_queue(&qd)?.get_qtype() != QType::UdpSocket {
let cause: String = format!("cannot bind to wildcard address (qd={:?})", qd);
error!("bind(): {}", cause);
return Err(Fail::new(libc::ENOTSUP, &cause));
match *localv4.ip() {
Ipv4Addr::UNSPECIFIED if self.get_shared_queue(&qd)?.get_qtype() == QType::UdpSocket => (),
Ipv4Addr::UNSPECIFIED => {
let cause: String = format!("cannot bind to wildcard address (qd={:?})", qd);
error!("bind(): {}", cause);
return Err(Fail::new(libc::ENOTSUP, &cause));
},
addr if addr != self.local_ipv4_addr => {
let cause: String = format!("cannot bind to non-local address: {:?}", addr);
error!("bind(): {}", &cause);
return Err(Fail::new(libc::EADDRNOTAVAIL, &cause));
},
_ => (),
}

// Check if this is an ephemeral port.
Expand Down Expand Up @@ -508,11 +521,11 @@ impl<T: NetworkTransport> SharedNetworkLibOS<T> {
pub fn wait_next_n<Acceptor: FnMut(demi_qresult_t) -> bool>(
&mut self,
mut acceptor: Acceptor,
timeout: Duration
) -> Result<(), Fail>
{
self.runtime.clone().wait_next_n(
|qt, qd, result| acceptor(self.create_result(result, qd, qt)), timeout)
timeout: Duration,
) -> Result<(), Fail> {
self.runtime
.clone()
.wait_next_n(|qt, qd, result| acceptor(self.create_result(result, qd, qt)), timeout)
}

pub fn create_result(&self, result: OperationResult, qd: QDesc, qt: QToken) -> demi_qresult_t {
Expand Down
6 changes: 5 additions & 1 deletion src/rust/inetstack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ pub struct InetStack<N: NetworkRuntime> {
runtime: SharedDemiRuntime,
network: N,
local_link_addr: MacAddress,
// Keeping this here for now in case we want to use it.
#[allow(unused)]
local_ipv4_addr: Ipv4Addr,
}

#[derive(Clone)]
Expand Down Expand Up @@ -141,7 +144,8 @@ impl<N: NetworkRuntime> SharedInetStack<N> {
ipv4,
runtime: runtime.clone(),
network,
local_link_addr: local_link_addr,
local_link_addr,
local_ipv4_addr,
}));
runtime.insert_background_coroutine("inetstack::poll_recv", Box::pin(me.clone().poll().fuse()))?;
Ok(me)
Expand Down
7 changes: 5 additions & 2 deletions src/rust/inetstack/protocols/tcp/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,12 @@ impl<N: NetworkRuntime> SharedTcpPeer<N> {
// Wait for accept to complete.
match socket.accept().await {
Ok(socket) => {
self.addresses.insert(SocketId::Active(socket.local().unwrap(), socket.remote().unwrap()), socket.clone());
self.addresses.insert(
SocketId::Active(socket.local().unwrap(), socket.remote().unwrap()),
socket.clone(),
);
Ok(socket)
}
},
Err(e) => Err(e),
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/rust/inetstack/test_helpers/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ impl SharedEngine {
)?;

Ok(Self(SharedNetworkLibOS::<SharedInetStack<SharedTestRuntime>>::new(
runtime, transport,
test_rig.get_ip_addr(),
runtime,
transport,
)))
}

Expand Down
2 changes: 1 addition & 1 deletion tests/rust/common/libos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl DummyLibOS {
logging::initialize();
let transport = SharedInetStack::new_test(runtime.clone(), network, link_addr, ipv4_addr)?;
Ok(Self(SharedNetworkLibOS::<SharedInetStack<SharedDummyRuntime>>::new(
runtime, transport,
ipv4_addr, runtime, transport,
)))
}

Expand Down

0 comments on commit 2b949e3

Please sign in to comment.