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 4 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,148 @@ | ||
| //! 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 | ||
| } | ||
|
AKolenda marked this conversation as resolved.
|
||
| 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; | ||
| let measurement = self | ||
| .bandwidth | ||
| .take() | ||
| .filter(|measurement| measurement.continuous == continuous); | ||
|
AKolenda marked this conversation as resolved.
Outdated
|
||
| 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, _) => (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 @ AutoDetectRequest::NetworkCharacteristicsResult { .. } => { | ||
| // The TCP message-channel processor surfaces this request itself. Keep this | ||
| // arm for the UDP tunnel responder introduced in PR #2009, which handles | ||
| // auto-detect requests without passing through that processor. | ||
| debug!(?request, "Received network characteristics from server"); | ||
| None | ||
| } | ||
|
AKolenda marked this conversation as resolved.
Outdated
|
||
| request => { | ||
| // Measurements for a lossy transport are not answered here. | ||
| debug!(?request, "Ignoring auto-detect request for another transport"); | ||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[protocol] Fast-path frames counted in full even when an RDP Security Header is present — low 🟡 — The continuous byte count records frame.len() unconditionally, assuming Enhanced RDP Security. Under Standard RDP Security (PROTOCOL_RDP, a supported IronRDP path) fast-path data carries the security flags byte and data signature, which MS-RDPBCGR 3.2.5.14 excludes from the count, inflating byteCount and skewing server-computed bandwidth. The added comment acknowledges the TLS-only assumption.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a real case. IronRDP supports Standard RDP Security only at ENCRYPTION_LEVEL_NONE (see
enable_standard_rdp_security). At that level the fast-path header has no FASTPATH_OUTPUT_ENCRYPTED flag, and there is no fipsInformation or dataSignature, so the whole frame is data after any security header. The fast-path decoder also never skips a data signature, so an encrypted frame would fail to decode before it could be counted. In 93509fa I reworded the comment so it no longer suggests TLS is the only case.