nvx/apertureAll write-ups
No. 05ENGINEERING · 4 min

One dropped packet left my keys stuck down

Part two of building nvx_kinetix, my KVM for Windows PCs: how a mouse, a keyboard and multi-gigabyte folders share one QUIC connection, and the one-line queue decision that left keys held down on the other machine.

By Cyber security, Monash University

In v1 of the input queue in nvx_kinetix, a full queue dropped the frame that had just arrived. Under load that frame was sometimes a key-up, and the key stayed held down on the other PC until I pressed it again.

One 1-pixel mouse move
14 bytes
Biggest input frame
23 bytes
Of every key and button event
3 copies
Of silence and the peer is gone
3 s

Part one ended with "The file bytes travel over QUIC with device-key pinning, which needs a write-up of its own". This is that, minus the parts I found boring.

One connection, two lanes

A KVM moves two very different things, and TCP gives you one ordered pipe. One lost packet freezes everything behind it, and a mouse waiting on a retransmit just feels frozen.

One QUIC connection, two lanes3 stages
  1. 01

    Mouse + keys

    Tiny, constant, useless if late. Unreliable datagrams, so late means drop it

  2. 02

    One UDP socket

    TLS 1.3, both machines prove who they are

  3. 03

    Clipboard + files

    Very large, must arrive whole. Reliable streams, retransmit without blocking input

Machine identity

How two machines come to trust each other4 stages
  1. 01

    First run

    Each machine makes an Ed25519 key

  2. 02

    The key is its identity

    No certificate authority

  3. 03

    First meeting

    You compare a short code on both screens

  4. 04

    After that

    The pinned key is the trust. Anything else gets refused

A mouse move is 14 bytes

One 1-pixel mouse move on the wire14 bytes
AVersion
BKind
CSequence
DTimestamp
Edx, dy
  • AVersion1 B
  • BKind1 B
  • CSequence2 B16-bit, wraps
  • DTimestamp8 B
  • Edx, dy2 BZigzag varints
Moves are sent as deltas, never screen positions. The biggest input frame is 23 bytes, nowhere near splitting a packet.

The queue decision that broke keyboards

Version one: drop the newest4 steps
  1. 01Incoming input waits in a small queue before it is replayed on the other machine. The first version held 16 frames, and under load it fills. Input queue: queue 16/16.
  2. 02s to q: Key-up. A key-up arrives.
  3. 03The queue is full, so v1 drops the frame that just arrived. For a mouse that sounds harmless. Input queue: key-up dropped.
  4. 04For a keyboard it is not. The key stays held down on the other PC until you press it again. Picture Shift refusing to let go. Other PC: key held down.
When the queue is full

Queue of 2,048, about a quarter second of an 8 kHz gaming mouse. A stale mouse move is the right thing to lose.

Why a repeated key needs a sequence check5 steps
  1. 01s to r: Key-down x3. A key-down goes out three times, all with the same sequence number.
  2. 02The receiver keeps the first copy that shows up and applies it. Receiver: newest = key-down.
  3. 03s to r: Key-up x3. The key-up goes out the same way and is applied. Receiver: newest = key-up.
  4. 04s to r: Late key-down. A late copy of the key-down lands after the key-up. Applied, it would press the key again.
  5. 05It is older than the newest sequence the receiver has applied, so it is ignored. Receiver: late copy ignored.

Sequence numbers are 16-bit and wrap, so "older" needs wrapping maths.

newer-than, with 16-bit wraparound
1fn is_newer(seq: u16, last: u16) -> bool {
2    let ahead = seq.wrapping_sub(last);
3    ahead != 0 && ahead < 0x8000
4}
  1. 1L265535 then 0 is one step forward, not 65,535 back.
  2. 2L3Less than half the range ahead counts as newer. Dupes and late arrivals fall outside.
Where a frame lands, counted from the newest one applied
  1. 1Same as the newest: ignored0
  2. Newer: applied1 to 32,767
  3. Older or late: ignored32,768 to 65,535
A frame's sequence number measured from the newest one applied. 0x8000 is 32,768, so 1 to 32,767 counts as newer.

A dead peer or a quiet one

How long until it notices the other PC is gone3 events
  1. v1

    Pull the cable, wait out QUIC's long default

    Your keyboard and mouse stay captured. Task Manager time.

  2. Fix 1

    6 s idle timeout, 2 s keep-alive

    Then healthy links dropped when nobody touched the mouse

  3. Fix 2

    20 s idle, 2 runtime threads, 1 s app heartbeat

    3 s of silence = gone. Release every held key first.

The second bug was in the runtime. With it on one thread, a single stall could delay the keep-alive long enough to lose the race against the idle timer. A quiet link is not a dead link, and it needs a heartbeat that isn't fighting for the same thread.

Folders bigger than memory

File transfer

Each stream starts with a one-byte kind, and files stream off disk in 256 KiB chunks.

The receiver reads exactly the declared byte count for every file, even ones it rejects, so one bad filename can't desync the rest.

Names are treated as hostile: .., drive letters, alternate data streams, CON, NUL, and trailing dots and spaces, because Windows silently trims those and .. turns back into ...

My VMware adapter kept stealing the connection

What my Alienware advertised over mDNS, paraphrased3 lines
192.168.0.213   real LAN
192.168.240.1   VMware host adapter        <- sometimes dialled this
dial 192.168.240.1 ... timed out
ChangeDetail
Which address to dialPrefer an IPv4 on one of the dialling machine's own subnets.
Which adapters to advertiseStop advertising adapters named like VMware, VirtualBox, Hyper-V, WSL or Docker.
TailscaleStays on, on purpose. People use it to reach their own boxes.

Gaps

Still open
  1. 01

    Resume

    No resume yet. A dropped link restarts the transfer.

    Open
  2. 02

    Latency numbers

    None measured in the repo. The app shows QUIC's live round trip instead. Next to measure: p50/p99 input latency over Wi-Fi.

    Open
  3. 03

    Network changes

    A network change means re-link, not migrate.

    Open

No comments yet

Comments are read before they appear.