Skip to content
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

Stream synchronization fixes #34

Merged
merged 3 commits into from
Feb 12, 2024
Merged

Stream synchronization fixes #34

merged 3 commits into from
Feb 12, 2024

Conversation

desowin
Copy link
Contributor

@desowin desowin commented Feb 9, 2024

Read cha data in max packet size chunks

Commit 56c5d37 ("Refactor cha_loop") changed libusb bulk transfer size from endpoint max packet size to 4096. This change inadvertently leads to RX stream desynchronization when there is large traffic on captured link. The data stream gets desynchronized because FTDI FT2232H prepends Modem Status and Line Status on every DATA packet it sends to capture host and the 4096 bytes buffer, when OpenVizsla is connected to High-Speed capture host, can hold up to 8 DATA packets and therefore the completed transfer can contain up to 16 status bytes, but transfer callback only skips the first two status bytes.

Fix the issue by skipping the Modem Status and Line Status on every max packet size chunk within the received buffer.

Do not lose synchronization on truncated packets

In current bitstream introduced in commit d0192dc ("Update to latest FPGA bitstream") only 1027 bytes are captured when HF0_TRUNC flag is set. HF0_TRUNC can happen either when there is babble packet on the bus or when the gateware incoming ULPI buffer gets full. When HF0_TRUNC is set, the packet size stored in header is guaranteed to be larger than 1027 but only 1027 bytes are actually captured. The next packet data are then incorrectly treated as remainder of the packet and therefore the stream gets out of sync.

Fix the issue by respecting gateware maximum captured packet size.

Commit 56c5d37 ("Refactor cha_loop") changed libusb bulk transfer
size from endpoint max packet size to 4096. This change inadvertently
leads to RX stream desynchronization when there is large traffic on
captured link. The data stream gets desynchronized because FTDI FT2232H
prepends Modem Status and Line Status on every DATA packet it sends to
capture host and the 4096 bytes buffer, when OpenVizsla is connected to
High-Speed capture host, can hold up to 8 DATA packets and therefore the
completed transfer can contain up to 16 status bytes, but transfer
callback only skips the first two status bytes.

Fix the issue by skipping the Modem Status and Line Status on every max
packet size chunk within the received buffer.
In current bitstream introduced in commit d0192dc ("Update to
latest FPGA bitstream") only 1027 bytes are captured when HF0_TRUNC flag
is set. HF0_TRUNC can happen either when there is babble packet on the
bus or when the gateware incoming ULPI buffer gets full. When HF0_TRUNC
is set, the packet size stored in header is guaranteed to be larger than
1027 but only 1027 bytes are actually captured. The next packet data are
then incorrectly treated as remainder of the packet and therefore the
stream gets out of sync.

Fix the issue by respecting gateware maximum captured packet size.
@desowin desowin mentioned this pull request Feb 9, 2024
Copy link
Owner

@matwey matwey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask you to write a unit test for packet_decoder_proc() demonstrating expected correct behavior for truncated data?

Truncated packets have HF0_TRUNC flag set and their size is greater than
OV_MAX_PACKET_SIZE. Current gateware captures packets up to 1027 bytes
long and therefore the truncated packet can only be observed when either
there is a babble on the bus or there is capture buffer overflow (but
not all capture overflows result in truncated packets because it all
depends on the original traffic and what data was dropped).
@desowin
Copy link
Contributor Author

desowin commented Feb 12, 2024

May I ask you to write a unit test for packet_decoder_proc() demonstrating expected correct behavior for truncated data?

Added a simple test case. This is really hard to come by in practice though because devices generally don't babble and the High-Speed endpoints with 1024 bytes wMaxPacketSize are pretty rare. Overflows don't generally cause truncated data because when overflow happens it can not only end up as "larger than real packet" but also as "shorter than real packet" because from perspective of input buffer there's no difference between dropping Start of Packet and End of Packet.

Note that this doesn't completely eliminate all stream synchronization issues, but the remaining issue is in gateware. With the bitstream from OpenVizslaTNG/ov_ftdi#21 I am not able to get the stream to lose synchronization even under extreme loads (dd if=/dev/sda of=/dev/null on 2TB external SSD connected through OpenVizsla).

@matwey matwey merged commit 3c19e8a into matwey:master Feb 12, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants