The rx/tx code in pkg/tap/switch.go is a bit hard to follow, with the protocol.Stream() tests, and the Buf()/Read()/Write() code in the stream case.
What the code is doing is to send data to/from the hypervisor. For some hypervisors (qemu, hyperkit, stdio), the size must be sent before the data, either as a 16 bit little-endian, or as a 32 bit little-endian data. This is what the code calls 'stream', and the Buf()/Read()/Write() methods from the streamProtocol interface are only used to write this 2 or 4 bytes for the size.
The
rx
/tx
code inpkg/tap/switch.go
is a bit hard to follow, with theprotocol.Stream()
tests, and theBuf()
/Read()
/Write()
code in the stream case. What the code is doing is to send data to/from the hypervisor. For some hypervisors (qemu, hyperkit, stdio), the size must be sent before the data, either as a 16 bit little-endian, or as a 32 bit little-endian data. This is what the code calls 'stream', and theBuf()
/Read()
/Write()
methods from thestreamProtocol
interface are only used to write this 2 or 4 bytes for the size.I've tried to rework this code in https://github.com/cfergeau/gvisor-tap-vsock/tree/rxtx:
streamProtocol
interface is nowReadSize
/WriteSize
as I believe this makes the code easier to understandhypervisorConnection
interface to replace theif conn.protocolImpl.Stream()
testsThis seems to simplify the code, but is untested, and I don't want to spend more time on this right away. What needs to be done to complete this:
One potential further improvement is to see if we can remove this allocation from
rxStream
using https://pkg.go.dev/bytes#Buffer