惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园_首页
V
Visual Studio Blog
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
月光博客
月光博客

KittenLabs

Casiomania Marantz UD5007 power supply replacement AnotterKiosk GPD Pocket 4 Speaker DSP FX3 LVDS Frame grabber Extreme Pi Boot Optimization WiFi auth with OsmoHLR/SIM cards Windows 11 tweaks & usability improvements Using custom domains as a Fediverse redirect 25GBit/s on macOS & iOS Router overclocking Real gaming router Manage RDP certificates on Windows using SSH 20 port USB-C charger Reviving a dead Gigabyte MJ11-EC1 mainboard NVMe BIOS Option ROM 2.5GbE PCIe NIC mod board Blinkekatze ThinkPad T41/T43 USB-C HomeSwitch 4 DC UPS (Lithium) OLED nametag Thermal camera macro Blåmba LPT printer emulator PC104 ISA adapter Palm IIIc USB-C OtterCast Gigaset DECT debug adapter M.2 NVMe -> miniPCIe
IP-over-Toslink
By Manawyrm | Thursday, January 09, 2025 · 2025-01-09 · via KittenLabs

2 channels @ 44kHz/16bit = 1536kbit/s digital data…

Backstory

At the 38th Chaos Communication Congress (38C3) benjojo gave a very interesting talk titled:
Going Long! Sending weird signals over long haul optical networks

Coincidentally, ember posted this joke to the Fediverse:

Fediverse post by ember, OH: Wir machen jetzt IP-over-Toslink

(Translation: “Overheard: We’re doing IP-over-Toslink now”)

After thinking about it for a while, this idea didn’t seem very far-fetched.
Toslink is a fully digital, bit-perfect way to transport digital PCM audio – aka data. Why not transport IP packets instead?
We could even go full-circle and transport IP-over-Toslink over single-mode fiber!

Implementation

Toslink is really just S/PDIF (aka IEC 958) transmitted over light instead of coaxial cable.
As such, it typically carries 48kHz of uncompressed PCM audio at 16bits in 2 channels:
48000 Hz * 16 bits * 2 channels = 1 536 000 bits/second

1536 kbit/s sounds like a usable connection speed after all…
Fun fact: That’s about the same throughput as a T1 line (1544 kbit/s).

In audio setups, Toslink/SPDIF is sometimes used for digital data as well, to transport digital (often multi-channel) compressed audio formats like AC-3 or DTS.

We need a way to create a continuous bitstream of 1.536 MBit/s and some way to signal framing over a serial connection.

Luckily for us, this problem was solved in the past:
Back in the days of ISDN, HDLC framing was used to transport packets over a synchronous serial connection.
It was also common to use the Point-to-Point Protocol (PPP) over such a link to exchange link parameters, handle compression and do authentication-related tasks.

About 2 years ago, I had already written some code that starts & configures a PPP daemon on Linux, takes the asynchronous framing of the packets and encodes them into synchronous HDLC.
This was released as the yate-ras project, an open source ISDN/synchronous network access server.
yate-ras can be used on a virtual telephony exchange (PBX) to handle & terminate incoming dial-up/ISDN phone calls and provide internet access via them.

This software could be quickly modified to accept data on STDIN and STDOUT.
It was then just a matter of building a pipe between arecord, the hdlc_ppp encap tool and aplay:

arecord -f dat -t raw -D hw:CARD=ICUSBAUDIO7D,DEV=0 --buffer-size 1024 | \
src/yate_hdlc_ppp options.server | \
aplay -f dat -t raw -D iec958:CARD=ICUSBAUDIO7D,DEV=0 --buffer-size 1024

This setup not only has about the same bandwidth as a T1 line, it’s also using the exact same bitstream/protocol on the wire!

arecord & aplay are both standard utilities from ALSA (the Linux audio stack).

-f dat configures 48kHz, 16bit little endian audio.
-t raw disables generation of any .wav headers and just outputs raw interleaved PCM frames.
--buffer-size 1024 sets the internal buffer size to 1024µs, way quicker than the default maximum, reducing latency.

Both client & server side look identical, just with different pppd configuration files.

Just like in yate-ras, sqm_cake is used as a congestion control algorithm to avoid buffer bloat:

#!/bin/bash
# /etc/ppp/ip-up.d/cake
tc qdisc add dev $1 root cake bandwidth 1536kbit

NAT & MSS clamping is configured using:

sysctl -w net.ipv4.ip_forward=1
iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 500
iptables -t nat -A POSTROUTING -o wlxdc15c89bb228 -j MASQUERADE

Clamping the MSS to about 500 bytes reduces worst-case latency on low-throughput links (at the cost of max. throughput).

Hardware setup

I bought 2 of the cheapest (sub-10$) USB sound cards with a Toslink in- & output.

Those typically use the C-Media CM6206 chipset, which is a fully integrated, single-chip USB sound card.
Just add an external crystal oscillator, a 3.3V regulator and some capacitors and you’re done: USB sound card, with 5 3.5mm headphone jacks and 2 Toslink ports, cables attached PCB of that sound card, only a single QFP CM6206 chip, 12 MHz crystal oscillator and a 3.3V regulator

The resulting setup between 2 laptops looked like this:

2 ThinkPad laptops, with 2 USB soundcards, connected with 2 TOSLINK fiber optic cables, 1 laptop is showing a speedtest with 960 kbit/s, the other is showing Wireshark

The ThinkPad T14 (on the right) shares its internet connection (via ip_forwarding/MASQUERADE NAT) via the PPP link.
The ThinkPad T430 (on the left) is only connected via the Toslink networking connection.

Benchmarks

manawyrm@nodeA:~$ ping 172.21.118.5
PING 172.21.118.5 (172.21.118.5) 56(84) bytes of data.
64 bytes from 172.21.118.5: icmp_seq=1 ttl=64 time=59.0 ms
64 bytes from 172.21.118.5: icmp_seq=2 ttl=64 time=54.7 ms
64 bytes from 172.21.118.5: icmp_seq=3 ttl=64 time=56.4 ms
64 bytes from 172.21.118.5: icmp_seq=4 ttl=64 time=57.9 ms
64 bytes from 172.21.118.5: icmp_seq=5 ttl=64 time=53.9 ms
64 bytes from 172.21.118.5: icmp_seq=6 ttl=64 time=55.4 ms
^C
--- 172.21.118.5 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5006ms
rtt min/avg/max/mdev = 53.899/56.217/59.007/1.787 ms
manawyrm@nodeB:~$ iperf3 -s
Accepted connection from 172.21.118.5, port 38226
[  5] local 172.21.118.1 port 5201 connected to 172.21.118.5 port 38240
[ ID] Interval           Transfer     Bitrate
[  5]   0.00-1.00   sec   168 KBytes  1.38 Mbits/sec
[  5]   1.00-2.00   sec   181 KBytes  1.48 Mbits/sec
[  5]   2.00-3.00   sec   180 KBytes  1.47 Mbits/sec
[  5]   3.00-4.00   sec   181 KBytes  1.48 Mbits/sec
[  5]   4.00-5.00   sec   180 KBytes  1.47 Mbits/sec
[  5]   5.00-6.00   sec   181 KBytes  1.48 Mbits/sec

As you can see, we’re reaching a real-world TCP throughput of around 1.47 Mbit/s, which is about 95% of the theoretical limit :) The latency (while not great) is also totally usable. If you really wanted to use this (don’t!), it would be possible to use libusb to interface directly with the CM6202 chip from userspace, which will probably decrease latency even further.

If you’re wondering what HDLC/PPP/IP packets sound like when played back as PCM audio: short bursts of static/noise.

Full PPP configuration dump

Server (“nodeA”)

mtu 1500
ms-dns 8.8.8.8
ms-dns 8.8.4.4

debug
nodetach
noauth
nodeflate

172.21.118.1:172.21.118.5

Client (“nodeB”)

mtu 1500

debug
nodetach
noauth
nodeflate

usepeerdns
defaultroute