From f35d54745fc541de3fe32f7e4ceb305cb1557cdc Mon Sep 17 00:00:00 2001 From: Andriy Sultanov Date: Tue, 19 Nov 2024 20:26:23 +0000 Subject: [PATCH] control_plane_agent: detach stale faux-mgs clients on new attach requests Factors out existing check that was used when sending new packets to also detach stale clients in serial_console_attach() As suggested in #1796. Signed-off-by: Andriy Sultanov --- .../src/mgs_compute_sled.rs | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/task/control-plane-agent/src/mgs_compute_sled.rs b/task/control-plane-agent/src/mgs_compute_sled.rs index e013d136e1..290c54c1e7 100644 --- a/task/control-plane-agent/src/mgs_compute_sled.rs +++ b/task/control-plane-agent/src/mgs_compute_sled.rs @@ -259,6 +259,25 @@ impl MgsHandler { id } + fn get_attached_nonidle_client( + &mut self, + ) -> &Option { + if let Some(attached) = &self.attached_serial_console_mgs { + // Check whether we think this client has disappeared + let client_age_ms = sys_get_timer() + .now + .saturating_sub(attached.last_keepalive_received); + if Duration::from_millis(client_age_ms) + > SERIAL_CONSOLE_IDLE_TIMEOUT + { + self.usart.clear_rx_data(); + self.attached_serial_console_mgs = None; + } + } + + return &self.attached_serial_console_mgs; + } + pub(crate) fn packet_to_mgs( &mut self, tx_buf: &mut [u8; gateway_messages::MAX_SERIALIZED_SIZE], @@ -279,21 +298,8 @@ impl MgsHandler { } // Do we have an attached MGS instance that hasn't gone stale? - let sender = match &self.attached_serial_console_mgs { - Some(attached) => { - // Check whether we think this client has disappeared - let client_age_ms = sys_get_timer() - .now - .saturating_sub(attached.last_keepalive_received); - if Duration::from_millis(client_age_ms) - > SERIAL_CONSOLE_IDLE_TIMEOUT - { - self.usart.clear_rx_data(); - self.attached_serial_console_mgs = None; - return None; - } - attached.sender - } + let sender = match &self.get_attached_nonidle_client() { + Some(attached) => attached.sender, None => { // Discard any buffered data and reset any usart-related timers. self.usart.clear_rx_data(); @@ -746,7 +752,7 @@ impl SpHandler for MgsHandler { return Err(SpError::RequestUnsupportedForComponent); } - if self.attached_serial_console_mgs.is_some() { + if self.get_attached_nonidle_client().is_some() { return Err(SpError::SerialConsoleAlreadyAttached); }