Skip to content

Added the CDC NCM device class - #304

Open
spir6s wants to merge 2 commits into
eclipse-threadx:devfrom
spir6s:device-class-cdc-ncm
Open

spir6s wants to merge 2 commits into
eclipse-threadx:devfrom
spir6s:device-class-cdc-ncm

Conversation

@spir6s

@spir6s spir6s commented Dec 3, 2026 •

Copy link
Copy Markdown

USBX had no device class for the USB Network Control Model (CDC NCM 1.1). NCM is the network class with built-in host drivers on Windows 11 (UsbNcm), Linux (cdc_ncm) and macOS; RNDIS is deprecated on several hosts and CDC ECM has no built-in Windows driver. Closes #93.

The new class ux_device_class_cdc_ncm follows the structure of the CDC ECM device class (entry, activate/change/deactivate, control requests, bulk IN, bulk OUT and interrupt threads) and connects to NetX Duo through the USBX network driver. It implements the NCM 1.0 function behaviour that every NCM 1.0 and 1.1 host driver supports: NTB16 only, no CRC on IN datagrams, and the requests GET_NTB_PARAMETERS, GET/SET_NTB_FORMAT, GET/SET_NTB_INPUT_SIZE, SET_ETHERNET_PACKET_FILTER and SET_ETHERNET_MULTICAST_FILTERS. NETWORK_CONNECTION and CONNECTION_SPEED_CHANGE are sent on link changes.

Small datagrams are aggregated into one NTB. Datagrams longer than UX_DEVICE_CLASS_CDC_NCM_NTB_IN_AGGREGATE_MAX_LENGTH (default: no limit) are sent in their own NTB, because Windows delivers the TCP segments of one NTB together and then acknowledges them only after its delayed ACK timer (about 40 ms per ACK); with a limit of 512 bytes, HTTP transfers to Windows are about as fast as with RNDIS while small frames are still aggregated. Received datagrams longer than an Ethernet frame (plus the optional CRC) are treated as a malformed NTB and dropped, and chained NDPs must follow each other forward in the block, so a looping chain cannot return datagrams more than once. If no USBX network driver instance is free, the data interface activation fails and alternate setting 1 is refused, so the class never passes a null network handle to the network driver. The class is not available in standalone mode, like CDC ECM.

The code was written from the NCM 1.1 specification and the existing USBX CDC ECM class; no code was taken from another NCM implementation.

Verification: the regression suite ran on Linux with GCC 14.2 and gcovr 8.6 (TX_COVERAGE=ON scripts/build.sh all and scripts/test.sh all): all 17 configurations pass and the build has no new warnings. The new tests usbx_device_class_cdc_ncm_basic_test (class requests, alternate settings, notifications, frames in both directions, entry and initialization failures, write with the link down, receive with an empty packet pool, no free network driver instance) and usbx_device_class_cdc_ncm_ntb16_test (NTB16 build and parse, malformed and oversized datagrams, NDP chains) cover 94 % of the lines and 77 % of the branches of the class. The uncovered parts are mainly USB transfer failures inside the class threads and corrupted descriptors. The Windows simulator was not run.

Hardware: Renesas RX64M (USB full speed, r_usb_basic) with the built-in NCM drivers of Windows 11 and Ubuntu (ping, HTTP and continuous MQTT traffic in both directions) and macOS (ping and HTTP). Compared with the USBX RNDIS class on the same firmware and a Windows 11 host, MQTT throughput from the device rose from about 2,600 to 3,360 messages/s (+29 %) and small HTTP requests from 83 to 95 requests/s; ping latency and a 34 KB HTTP download are about the same.

The second commit addresses findings of an internal review: oversized datagrams, looping NDP chains and the failed network driver activation described above. With it, the device was tested again on the RX64M with a Windows 11 host: ping, HTTP and 3 × 40 s of MQTT traffic with 600 incoming messages/s, without loss.

Documentation: eclipse-threadx/rtos-docs-asciidoc#124 documents the class in chapter 5 of the USBX device stack guide.

The class was written with AI assistance (GitHub Copilot with Claude Opus 5.5). I reviewed and tested it; the new files carry the AI disclosure header and both commits the Assisted-by trailer.

USBX had no device class for the USB Network Control Model (CDC NCM 1.1). Devices that
need a network interface with built-in host drivers had to use RNDIS or CDC ECM.

The new class carries Ethernet frames in NTB16 transfer blocks and connects to NetX Duo
through the USBX network driver, like the CDC ECM class. It handles the NCM class
requests and sends the network connection and speed change notifications. Small
datagrams are aggregated into one NTB. Datagrams longer than
UX_DEVICE_CLASS_CDC_NCM_NTB_IN_AGGREGATE_MAX_LENGTH are sent in their own NTB, because a
host may coalesce the TCP segments of one NTB and delay its ACKs.

The new regression tests usbx_device_class_cdc_ncm_basic_test and
usbx_device_class_cdc_ncm_ntb16_test cover 94 % of the lines and 76 % of the branches of
the class. All 17 regression configurations pass. The class was also tested on a Renesas
RX64M (full speed) with Windows 11, Ubuntu and macOS hosts. Compared with the RNDIS
class on the same device, the MQTT throughput to a Windows 11 host rose by 29 %.

Assisted-by: Copilot (Claude Opus 5.5) <[email protected]>
The NTB parser accepted datagrams up to the NTB size, although the class advertises a
maximum segment size of 1514 bytes, and a looping NDP chain returned the datagrams of
an NDP up to 128 times before it was detected. If no USBX network driver instance was
free, the class ignored the failed activation and later passed a null network handle
to _ux_network_driver_link_up when the host selected the data alternate setting.

Datagrams longer than an Ethernet frame plus the optional CRC are now treated as a
malformed NTB, and chained NDPs must follow each other forward in the block. A failed
network driver activation fails the data interface activation, and alternate setting
1 is refused without a network handle.

The CDC NCM regression tests cover these cases and pass in the default, error check
and device buffer owner configurations.

Assisted-by: Copilot (Claude Opus 5.5) <[email protected]>
@spir6s
spir6s force-pushed the device-class-cdc-ncm branch from 2ab12db to 3257a60 Compare December 3, 2026 19:13
@spir6s
spir6s marked this pull request as ready for review December 4, 2026 07:44
@spir6s

spir6s commented Dec 4, 2026

Copy link
Copy Markdown
Author

The failing auto_tests / run_tests job is unrelated to this change: standalone_build_coverage::usbx_device_dfu_basic_test crashed with a segfault, and the coverage steps failed only as a consequence. The DFU test does not use the CDC NCM class, which is not functional in standalone builds. The same test failed in the same configuration on a dev push without this change (run 40094653283), and the previous CI run of this pull request passed all configurations. All 17 configurations also pass locally with GCC 14.2.

Could a maintainer re-run the failed job?

This branch has not been deployed

No deployments
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.

1 participant