|
36 | 36 | //! ```
|
37 | 37 |
|
38 | 38 | use std::{
|
39 |
| - collections::{hash_map::Entry, HashMap, HashSet}, |
| 39 | + collections::{HashMap, HashSet}, |
40 | 40 | io,
|
41 | 41 | net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs},
|
42 | 42 | os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd},
|
43 |
| - sync::{Arc, Mutex}, |
| 43 | + sync::{Arc, atomic::{AtomicU32, Ordering}, Mutex}, |
44 | 44 | task::Context,
|
45 | 45 | };
|
46 | 46 | use tap::TapFallible;
|
@@ -70,7 +70,7 @@ pub struct NetSim {
|
70 | 70 | host_state: Mutex<HostNetworkState>,
|
71 | 71 | rand: GlobalRng,
|
72 | 72 | time: TimeHandle,
|
73 |
| - next_tcp_id_map: Mutex<HashMap<NodeId, u32>>, |
| 73 | + next_tcp_id: AtomicU32, // We always allocate new globally unique tcp id. |
74 | 74 | }
|
75 | 75 |
|
76 | 76 | #[derive(Debug)]
|
@@ -896,7 +896,8 @@ impl plugin::Simulator for NetSim {
|
896 | 896 | rand: rand.clone(),
|
897 | 897 | time: time.clone(),
|
898 | 898 | host_state: Default::default(),
|
899 |
| - next_tcp_id_map: Mutex::new(HashMap::new()), |
| 899 | + // tcp ids start at 1, 0 is used for new connections (see poll_accept_internal) |
| 900 | + next_tcp_id: AtomicU32::new(1), |
900 | 901 | }
|
901 | 902 | }
|
902 | 903 |
|
@@ -944,7 +945,7 @@ impl NetSim {
|
944 | 945 | let mut host_state = self.host_state.lock().unwrap();
|
945 | 946 | host_state.delete_node(id);
|
946 | 947 |
|
947 |
| - // We do not reset self.next_tcp_id_map - we do not want to re-use tcp ids after a node is |
| 948 | + // We do not reset self.next_tcp_id - we do not want to re-use tcp ids after a node is |
948 | 949 | // restarted.
|
949 | 950 | }
|
950 | 951 |
|
@@ -991,22 +992,9 @@ impl NetSim {
|
991 | 992 | self.time.sleep(delay).await;
|
992 | 993 | }
|
993 | 994 |
|
994 |
| - /// Get the next unused tcp id for this node. |
995 |
| - pub fn next_tcp_id(&self, node: NodeId) -> u32 { |
996 |
| - let mut map = self.next_tcp_id_map.lock().unwrap(); |
997 |
| - match map.entry(node) { |
998 |
| - Entry::Occupied(mut cur) => { |
999 |
| - let cur = cur.get_mut(); |
1000 |
| - // limited to 2^32 - 1 tcp sessions per node per simulation run. |
1001 |
| - *cur = cur.checked_add(1).unwrap(); |
1002 |
| - *cur |
1003 |
| - } |
1004 |
| - Entry::Vacant(e) => { |
1005 |
| - // tcp ids start at 1, 0 is used for new connections (see poll_accept_internal) |
1006 |
| - e.insert(1); |
1007 |
| - 1 |
1008 |
| - } |
1009 |
| - } |
| 995 | + /// Get the next unused tcp id. |
| 996 | + pub fn next_tcp_id(&self) -> u32 { |
| 997 | + self.next_tcp_id.fetch_add(1, Ordering::SeqCst) |
1010 | 998 | }
|
1011 | 999 | }
|
1012 | 1000 |
|
@@ -1111,7 +1099,7 @@ impl Endpoint {
|
1111 | 1099 |
|
1112 | 1100 | /// Allocate a new tcp id number for this node. Ids are never reused.
|
1113 | 1101 | pub fn allocate_local_tcp_id(&self) -> u32 {
|
1114 |
| - let id = self.net.next_tcp_id(self.node); |
| 1102 | + let id = self.net.next_tcp_id(); |
1115 | 1103 | trace!(
|
1116 | 1104 | "Allocate local tcp id {} to node {} address {}",
|
1117 | 1105 | id,
|
|
0 commit comments