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

Make tcp id globally unique #50

Merged
merged 1 commit into from
Jun 21, 2024
Merged
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
32 changes: 10 additions & 22 deletions msim/src/sim/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
//! ```

use std::{
collections::{hash_map::Entry, HashMap, HashSet},
collections::{HashMap, HashSet},
io,
net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs},
os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd},
sync::{Arc, Mutex},
sync::{Arc, atomic::{AtomicU32, Ordering}, Mutex},
task::Context,
};
use tap::TapFallible;
Expand Down Expand Up @@ -70,7 +70,7 @@ pub struct NetSim {
host_state: Mutex<HostNetworkState>,
rand: GlobalRng,
time: TimeHandle,
next_tcp_id_map: Mutex<HashMap<NodeId, u32>>,
next_tcp_id: AtomicU32, // We always allocate new globally unique tcp id.
}

#[derive(Debug)]
Expand Down Expand Up @@ -896,7 +896,8 @@ impl plugin::Simulator for NetSim {
rand: rand.clone(),
time: time.clone(),
host_state: Default::default(),
next_tcp_id_map: Mutex::new(HashMap::new()),
// tcp ids start at 1, 0 is used for new connections (see poll_accept_internal)
next_tcp_id: AtomicU32::new(1),
}
}

Expand Down Expand Up @@ -944,7 +945,7 @@ impl NetSim {
let mut host_state = self.host_state.lock().unwrap();
host_state.delete_node(id);

// We do not reset self.next_tcp_id_map - we do not want to re-use tcp ids after a node is
// We do not reset self.next_tcp_id - we do not want to re-use tcp ids after a node is
// restarted.
}

Expand Down Expand Up @@ -991,22 +992,9 @@ impl NetSim {
self.time.sleep(delay).await;
}

/// Get the next unused tcp id for this node.
pub fn next_tcp_id(&self, node: NodeId) -> u32 {
let mut map = self.next_tcp_id_map.lock().unwrap();
match map.entry(node) {
Entry::Occupied(mut cur) => {
let cur = cur.get_mut();
// limited to 2^32 - 1 tcp sessions per node per simulation run.
*cur = cur.checked_add(1).unwrap();
*cur
}
Entry::Vacant(e) => {
// tcp ids start at 1, 0 is used for new connections (see poll_accept_internal)
e.insert(1);
1
}
}
/// Get the next unused tcp id.
pub fn next_tcp_id(&self) -> u32 {
self.next_tcp_id.fetch_add(1, Ordering::SeqCst)
}
}

Expand Down Expand Up @@ -1111,7 +1099,7 @@ impl Endpoint {

/// Allocate a new tcp id number for this node. Ids are never reused.
pub fn allocate_local_tcp_id(&self) -> u32 {
let id = self.net.next_tcp_id(self.node);
let id = self.net.next_tcp_id();
trace!(
"Allocate local tcp id {} to node {} address {}",
id,
Expand Down
Loading