meta.rs 3.3 KB

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