1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
use super::SocketHandle;
use crate::{
socket::PollAt,
time::{Duration, Instant},
wire::IpAddress,
};
/// Neighbor dependency.
///
/// This enum tracks whether the socket should be polled based on the neighbor
/// it is going to send packets to.
#[derive(Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
enum NeighborState {
/// Socket can be polled immediately.
#[default]
Active,
/// Socket should not be polled until either `silent_until` passes or
/// `neighbor` appears in the neighbor cache.
Waiting {
neighbor: IpAddress,
silent_until: Instant,
},
}
/// Network socket metadata.
///
/// This includes things that only external (to the socket, that is) code
/// is interested in, but which are more conveniently stored inside the socket
/// itself.
#[derive(Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub(crate) struct Meta {
/// Handle of this socket within its enclosing `SocketSet`.
/// Mainly useful for debug output.
pub(crate) handle: SocketHandle,
/// See [NeighborState](struct.NeighborState.html).
neighbor_state: NeighborState,
}
impl Meta {
/// Minimum delay between neighbor discovery requests for this particular
/// socket, in milliseconds.
///
/// See also `iface::NeighborCache::SILENT_TIME`.
pub(crate) const DISCOVERY_SILENT_TIME: Duration = Duration::from_millis(1_000);
pub(crate) fn poll_at<F>(&self, socket_poll_at: PollAt, has_neighbor: F) -> PollAt
where
F: Fn(IpAddress) -> bool,
{
match self.neighbor_state {
NeighborState::Active => socket_poll_at,
NeighborState::Waiting { neighbor, .. } if has_neighbor(neighbor) => socket_poll_at,
NeighborState::Waiting { silent_until, .. } => PollAt::Time(silent_until),
}
}
pub(crate) fn egress_permitted<F>(&mut self, timestamp: Instant, has_neighbor: F) -> bool
where
F: Fn(IpAddress) -> bool,
{
match self.neighbor_state {
NeighborState::Active => true,
NeighborState::Waiting {
neighbor,
silent_until,
} => {
if has_neighbor(neighbor) {
net_trace!(
"{}: neighbor {} discovered, unsilencing",
self.handle,
neighbor
);
self.neighbor_state = NeighborState::Active;
true
} else if timestamp >= silent_until {
net_trace!(
"{}: neighbor {} silence timer expired, rediscovering",
self.handle,
neighbor
);
true
} else {
false
}
}
}
}
pub(crate) fn neighbor_missing(&mut self, timestamp: Instant, neighbor: IpAddress) {
net_trace!(
"{}: neighbor {} missing, silencing until t+{}",
self.handle,
neighbor,
Self::DISCOVERY_SILENT_TIME
);
self.neighbor_state = NeighborState::Waiting {
neighbor,
silent_until: timestamp + Self::DISCOVERY_SILENT_TIME,
};
}
}
|