thunderbolt: Add support for USB4STREAM

Introduce USB4STREAM protocol and Linux implementation. This allows two
(or more) hosts to transfer data directly over Thunderbolt/USB4 cable
through a character device without need to go through the network stack.

Any application that supports read(2) and write(2) in some form should
be able to use the device without changes. The data is sent out to the
other side over a tunnel inside Thunderbolt/USB4 fabric. The character
device is called /dev/tbstreamX where X is the minor number starting
from 0.

All stream devices need to be configured first. This is done through
ConfigFS interface. There can be multiple streams at the same time (this
depends on number of DMA rings and available HopIDs) and a single stream
supports traffic in both directions. For example there could be an
application that uses one stream as control channel and another one as
bi-directional data channel.

A real use-case for this is to take a backup as a part of recovery
initramfs tooling (no need to setup networking or have ssh or similar
tooling as part of the initramfs). Say we want to backup the disk of
host1 to host2. First Thunderbolt/USB4 cable is connected between the
hosts (there can be devices in the middle too) then the receiving side
configures the stream:

  host2 # mkdir /sys/kernel/config/thunderbolt/stream/0-1.0
  host2 # mkdir /sys/kernel/config/thunderbolt/stream/0-1.0/backup
  host2 # echo -1 > /sys/kernel/config/thunderbolt/stream/0-1.0/backup/in_hopid
  host2 # echo -1 > /sys/kernel/config/thunderbolt/stream/0-1.0/backup/out_hopid

We use automatic HopID allocation (writing -1 to HopIDs) for simplicity.
From this point forward the /dev/tbstream0 can be used pretty much as
regular file:

  host2 # dd if=/dev/tbstream0 of=/tmp/host1.nvme0n1.backup-$(date +%F) bs=256k

The host that is being backed up then configures the stream accordingly:

  host1 # mkdir /sys/kernel/config/thunderbolt/stream/0-503.0
  host1 # mkdir /sys/kernel/config/thunderbolt/stream/0-503.0/backup

Here we take advantage of the fact that host2 also announces the active
streams through XDomain properties so the name "backup" gives us the
HopIDs. It is also possible to configure them manually in the same way
we did for host2.

Then it is just a matter of copying the data over:

  host1 # dd if=/dev/nvme0n1 of=/dev/tbstream0 bs=256k

Similarly it is possible to transfer parts of the filesystem. For
example copy contents of mydir over to the host2:

  host2 # gunzip < /dev/tbstream0 | tar xf -
  host1 # tar cf - mydir | gzip > /dev/tbstream0

Other end of the spectrum use-case is "borrowing" laptop (host1) camera
to desktop (host2):

  host2 # gst-launch-1.0 filesrc location=/dev/tbstream0 ! jpegdec ! videoconvert ! \
                         autovideosink

  host1 # gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,width=1920,height=1080 ! \
                         jpegenc quality=90 ! filesink location=/dev/tbstream0

Once the streams are no longer needed they can be removed:

  host1 # cd /sys/kernel/config/thunderbolt/stream/
  host1 # rmdir -p 0-503.0/backup

  host2 # cd /sys/kernel/config/thunderbolt/stream
  host2 # rmdir -p 0-1.0/backup

Co-developed-by: Alan Borzeszkowski <alan.borzeszkowski@linux.intel.com>
Signed-off-by: Alan Borzeszkowski <alan.borzeszkowski@linux.intel.com>
Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
This commit is contained in:
Mika Westerberg
2025-08-12 13:56:30 +03:00
parent cba57ed6f1
commit 6db21d817b
4 changed files with 1795 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
What: /sys/kernel/config/thunderbolt/stream/<xdomain>.<service>
Date: Sep 2026
KernelVersion: v7.2
Contact: Mika Westerberg <mika.westerberg@linux.intel.com>
Description:
Configuration group for a stream Thunderbolt/USB4
service. It is possible to create groups even if there
is no connection yet to the other host. Once a
connection established and there is stream service on
the remote side that matches, this configuration is
applied to it.
To find the service name you can run tblist from tbtools [1]:
# tblist -A
...
Domain 0 Route 3 Index 0: stream
[1] https://github.com/intel/tbtools
What: /sys/kernel/config/thunderbolt/stream/<xdomain>.<service>/$name
Date: Sep 2026
KernelVersion: v7.2
Contact: Mika Westerberg <mika.westerberg@linux.intel.com>
Description:
Creates new stream with $name and fills it with the
default values. If there is an advertised remote stream
with the same name, uses its values as the default.
What: /sys/kernel/config/thunderbolt/stream/<xdomain>.<service>/$name/index
Date: Sep 2026
KernelVersion: v7.2
Contact: Mika Westerberg <mika.westerberg@linux.intel.com>
Description:
This matches the X in /dev/tbstreamX and allows userspace
to map the configfs directory to the corresponding character
device.
What: /sys/kernel/config/thunderbolt/stream/<xdomain>.<service>/$name/in_hopid
Date: Sep 2026
KernelVersion: v7.2
Contact: Mika Westerberg <mika.westerberg@linux.intel.com>
Description:
In HopID used with the read path of the tunnel. Available HopIDs
for tunneling start from 8. You can pass also -1 for automatic
allocation. The allocated value can be read here. Writing 0 will
de-allocate if the stream is not in use.
To figure out the maximum HopID you can run tbget from
tbtools for the lane adapter. For example below we check
for lane adapter number 1 (first USB4 port):
# tbget -r 0 -a 1 -D ADP_CS_5.Max\ Input\ HopID
19
This allows to use anything between 8 and 19 inclusive.
What: /sys/kernel/config/thunderbolt/stream/<xdomain>.<service>/$name/out_hopid
Date: Sep 2026
KernelVersion: v7.2
Contact: Mika Westerberg <mika.westerberg@linux.intel.com>
Description:
Out HopID used with the write path of the tunnel. Available HopIDs
for tunneling start from 8. You can pass also -1 for automatic
allocation. The allocated value can be read here. Writing 0 will
de-allocate if the stream is not in use. See @in_hopid
for how to figure out the maximum HopID.
What: /sys/kernel/config/thunderbolt/stream/<xdomain>.<service>/$name/ring_size
Date: Sep 2026
KernelVersion: v7.2
Contact: Mika Westerberg <mika.westerberg@linux.intel.com>
Description:
Size of the TX/RX rings. Can be adjusted between 32 and
4096. The default is 256.
What: /sys/kernel/config/thunderbolt/stream/<xdomain>.<service>/$name/throttling
Date: Sep 2026
KernelVersion: v7.2
Contact: Mika Westerberg <mika.westerberg@linux.intel.com>
Description:
Interrupt throttling rate in ns. Lower values can give
better latency. The default is 8192 ns.

View File

@@ -64,4 +64,15 @@ config USB4_DMA_TEST
To compile this driver a module, choose M here. The module will be
called thunderbolt_dma_test.
config USB4_STREAM
tristate "Stream data over Thunderbolt/USB4 cable"
depends on USB4_CONFIGFS
help
This adds support for USB4STREAM protocol that allows two
hosts to stream data directly over Thunderbolt/USB4 cable
through /dev/tbstreamX devices.
To compile this driver a module, choose M here. The module will be
called thunderbolt_stream.
endif # USB4

View File

@@ -13,3 +13,6 @@ CFLAGS_test.o += $(DISABLE_STRUCTLEAK_PLUGIN)
thunderbolt_dma_test-${CONFIG_USB4_DMA_TEST} += dma_test.o
obj-$(CONFIG_USB4_DMA_TEST) += thunderbolt_dma_test.o
thunderbolt_stream-${CONFIG_USB4_STREAM} += stream.o
obj-$(CONFIG_USB4_STREAM) += thunderbolt_stream.o

1698
drivers/thunderbolt/stream.c Normal file

File diff suppressed because it is too large Load Diff