Repository navigation
feat(session): answer bandwidth measurements during the session #2012
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
Open
AKolenda
wants to merge
5
commits into
Devolutions:master
Choose a base branch
from
AKolenda:feat/session-continuous-bandwidth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+609
−39
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a9fb422
feat(session): answer bandwidth measurements during the session
4cdfb7f
refactor(session): answer auto-detect requests through one responder
2974819
refactor(session): tidy the bandwidth responder
4a873c0
fix(session): count complete bandwidth frames and reject untimed esti…
93509fa
fix(session): keep a bandwidth window across a mismatched Stop
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| //! Client answers to the network auto-detect requests of [MS-RDPBCGR] 2.2.14. | ||
| //! | ||
| //! [MS-RDPBCGR]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/dc672839-4f4e-40b1-a71c-cd6a959baa38 | ||
|
|
||
| use ironrdp_core::MonotonicInstant; | ||
| use ironrdp_pdu::rdp::autodetect::{ | ||
| AutoDetectRequest, AutoDetectResponse, BW_RESULTS_CONNECT_TIME, BW_RESULTS_CONTINUOUS, BW_START_CONNECT_TIME, | ||
| BW_START_RELIABLE_UDP, BW_STOP_CONNECT_TIME, BW_STOP_RELIABLE_UDP, | ||
| }; | ||
| use tracing::debug; | ||
|
|
||
| /// Answers the RTT and bandwidth measurement requests that arrive on one transport. | ||
| /// | ||
| /// Each transport keeps its own, because a continuous measurement counts the data received on | ||
| /// the transport it runs on ([MS-RDPBCGR] 3.2.5.14). Timestamps come from the caller, from the | ||
| /// same monotonic clock for every request on that transport. | ||
| /// | ||
| /// [MS-RDPBCGR]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/16ffa852-8aa7-481c-99a0-36c1a9a198f6 | ||
| #[derive(Debug, Default)] | ||
| pub(crate) struct AutoDetectResponder { | ||
| bandwidth: Option<BandwidthMeasurement>, | ||
| } | ||
|
|
||
| /// Size of the auto-detect header fields [MS-RDPBCGR] 3.2.5.14 counts along with payloadLength: | ||
| /// headerLength, headerTypeId, sequenceNumber, requestType and payloadLength itself. | ||
| /// | ||
| /// [MS-RDPBCGR]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpbcgr/16ffa852-8aa7-481c-99a0-36c1a9a198f6 | ||
| const AUTO_DETECT_HEADER_LEN: u32 = 8; | ||
|
|
||
| /// Reported as `timeDelta` for a window that was not timed, and the floor for one that was: | ||
| /// a server computing `byteCount * 8 / timeDelta` divides by it ([MS-RDPBCGR] 3.3.5.14). | ||
| const UNMEASURABLE_INTERVAL_MS: u32 = 1; | ||
|
|
||
| /// Bytes a connect-time Payload or Stop adds to the count: payloadLength plus the auto-detect | ||
| /// header, but not the security header. This is the rule the connector applies to the same PDUs. | ||
| fn counted_len(payload_len: usize) -> u32 { | ||
| u32::try_from(payload_len) | ||
| .unwrap_or(u32::MAX) | ||
| .saturating_add(AUTO_DETECT_HEADER_LEN) | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct BandwidthMeasurement { | ||
| started_at: MonotonicInstant, | ||
| bytes: u32, | ||
| continuous: bool, | ||
| } | ||
|
|
||
| impl AutoDetectResponder { | ||
| /// Counts received bytes while a continuous bandwidth window is open. | ||
| pub(crate) fn record_bytes(&mut self, bytes: usize) { | ||
| if let Some(measurement) = self.bandwidth.as_mut().filter(|measurement| measurement.continuous) { | ||
| measurement.bytes = measurement | ||
| .bytes | ||
| .saturating_add(u32::try_from(bytes).unwrap_or(u32::MAX)); | ||
| } | ||
| } | ||
|
|
||
| /// Returns the response `request` calls for, if any. | ||
| /// | ||
| /// Without an arrival time, a bandwidth measurement is answered with an untimed, zero-byte | ||
| /// result. | ||
| pub(crate) fn respond( | ||
| &mut self, | ||
| request: AutoDetectRequest, | ||
| received_at: Option<MonotonicInstant>, | ||
| ) -> Option<AutoDetectResponse> { | ||
| match request { | ||
| AutoDetectRequest::RttRequest { sequence_number, .. } => { | ||
| Some(AutoDetectResponse::RttResponse { sequence_number }) | ||
| } | ||
| AutoDetectRequest::BandwidthMeasureStart { request_type, .. } | ||
| if matches!(request_type, BW_START_CONNECT_TIME | BW_START_RELIABLE_UDP) => | ||
| { | ||
| self.bandwidth = received_at.map(|started_at| BandwidthMeasurement { | ||
| started_at, | ||
| bytes: 0, | ||
| continuous: request_type == BW_START_RELIABLE_UDP, | ||
| }); | ||
| None | ||
| } | ||
| AutoDetectRequest::BandwidthMeasurePayload { payload, .. } => { | ||
| if let Some(measurement) = self.bandwidth.as_mut().filter(|measurement| !measurement.continuous) { | ||
| measurement.bytes = measurement.bytes.saturating_add(counted_len(payload.len())); | ||
| } | ||
| None | ||
| } | ||
| AutoDetectRequest::BandwidthMeasureStop { | ||
| sequence_number, | ||
| request_type, | ||
| payload, | ||
| } if matches!(request_type, BW_STOP_CONNECT_TIME | BW_STOP_RELIABLE_UDP) => { | ||
| let continuous = request_type == BW_STOP_RELIABLE_UDP; | ||
| // A Stop of the other measurement type leaves the open window in place, so a | ||
| // later Stop of the matching type still reports it. | ||
| let measurement = if self | ||
| .bandwidth | ||
| .as_ref() | ||
| .is_some_and(|measurement| measurement.continuous == continuous) | ||
| { | ||
| self.bandwidth.take() | ||
| } else { | ||
| None | ||
| }; | ||
| let stop_bytes = if continuous { | ||
| 0 | ||
| } else { | ||
| counted_len(payload.as_ref().map_or(0, Vec::len)) | ||
| }; | ||
| let (time_delta_ms, byte_count) = match (measurement, received_at) { | ||
| (Some(measurement), Some(stopped_at)) => ( | ||
| u32::try_from(stopped_at.duration_since(measurement.started_at).as_millis()) | ||
| .unwrap_or(u32::MAX) | ||
| .max(UNMEASURABLE_INTERVAL_MS), | ||
| measurement.bytes.saturating_add(stop_bytes), | ||
| ), | ||
| (Some(measurement), None) => { | ||
| // The window was timed but this Stop was not, so there is nothing to | ||
| // divide the count by. Log the drop so it does not look like the | ||
| // ordinary no-window case. | ||
| debug!( | ||
| dropped_bytes = measurement.bytes, | ||
| "Bandwidth Measure Stop arrived with no arrival time although its window was open; \ | ||
| dropping the accumulated count" | ||
| ); | ||
| (UNMEASURABLE_INTERVAL_MS, 0) | ||
| } | ||
| (None, _) => { | ||
| debug!( | ||
| continuous, | ||
| "Bandwidth Measure Stop arrived with no matching open window; reporting no measurement" | ||
| ); | ||
| (UNMEASURABLE_INTERVAL_MS, 0) | ||
| } | ||
| }; | ||
|
AKolenda marked this conversation as resolved.
|
||
| Some(AutoDetectResponse::BandwidthMeasureResults { | ||
| sequence_number, | ||
| response_type: if continuous { | ||
| BW_RESULTS_CONTINUOUS | ||
| } else { | ||
| BW_RESULTS_CONNECT_TIME | ||
| }, | ||
| time_delta_ms, | ||
| byte_count, | ||
| }) | ||
|
AKolenda marked this conversation as resolved.
|
||
| } | ||
| request => { | ||
| // Lossy-transport measurements are not answered here, and a Network | ||
| // Characteristics Result needs no answer. | ||
| debug!(?request, "Not answering auto-detect request"); | ||
| None | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.