v1

v1 defines the first production-ready version of the WebAssembly Transport Module (WATM) specification.

API

The current version of WATM utilizes the WebAssembly System Interface (WASI) Preview 1 to provide the basic functionalities to interact between the WebAssembly module and the host environment. In addition, we define the following API for the transport module.

Imports

WATM imports the following functions from the host environment:

Function Signature Parameters Results Description
water_dial network_iovs: ciovec_array, address_iovs: ciovec_array fd: s32 Dial a remote address specified by network and address
water_dial_fixed - fd: s32 Dial a remote address controlled by WATER Runtime
water_accept - net_fd: s32 Accept an incoming connection from the listener managed by WATER Runtime

Exports

WATM exports the following functions to the host environment:

Function Signature Parameters Results Description
watm_init_v1 - errno: s32 Initialize the transport module
watm_ctrlpipe_v1 ctrl_fd: s32 errno: s32 Set the control pipe to the given file descriptor. Called before watm_start_v1
watm_dial_v1 internal_fd: s32 net_fd: s32 Connect to a remote server specified by WATER Runtime
watm_dial_fixed_v1 internal_fd: s32 net_fd: s32 Connect to a remote server specified by WATM
watm_accept_v1 internal_fd: s32 net_fd: s32 Accept an incoming connection
watm_associate_v1 - errno: s32 Associate the next incoming connection with an outgoing connection (relay)
watm_start_v1 - errno: s32 Start the (blocking) worker thread

Behavior

This section describes how the WATER runtime in Go (water-go) implements v1. The runtime is called the host below.

Module lifecycle

The host creates a new instance of the WATM for every connection and never reuses it. For a relay, one instance serves one incoming and one outgoing connection. The host does the following, one step after the other:

  1. It instantiates the WATM with WASI Preview 1 and the three imports above (import module env). The WATM must import at least one of the three, and must import water_accept to act as a listener or a relay. If the WATM exports _start, the host calls it once. _start must return, and it cannot use the three imports yet. The host does not call _initialize.
  2. It checks the exports. watm_init_v1, watm_ctrlpipe_v1 and watm_start_v1 are mandatory, and each role that the WATM supports needs its role export (see step 4). Parameters and results must match the table above.
  3. It calls watm_init_v1.
  4. It calls watm_dial_v1, watm_dial_fixed_v1, watm_accept_v1 or watm_associate_v1, depending on what the application asked for. Only one of these role exports is called per instance.
  5. It calls watm_ctrlpipe_v1.
  6. It calls watm_start_v1 in the background. This is the worker. It has to move the data in both directions, because the host does not. The host calls no other export while the worker runs or after it has returned.

internal_fd is the WATM’s end of a local TCP connection. The application reads from and writes to the other end. For a dialer, a fixed dialer or a listener, the worker moves data between internal_fd and the network connection. For a relay, it moves data between the incoming and the outgoing connection.

Dialing and composition

WASI Preview 1 has no call to open or connect a socket. A WATM therefore asks the host for a connection with water_dial, water_dial_fixed or water_accept, and gets a file descriptor. The application controls the dialer and the listener behind these imports. In Go, it sets them with NetworkDialerFunc and NetworkListener in water.Config. This is comparable to the composable dialers of the Outline SDK, where a transport is given a StreamDialer and wraps it: the WATM adds its protocol, and the application controls how the connection below it is made.

The role decides which imports the WATM has to use:

Role Role export Imports Address chosen by
Dialer watm_dial_v1 water_dial_fixed Application
Fixed dialer watm_dial_fixed_v1 water_dial WATM
Listener watm_accept_v1 water_accept Application
Relay watm_associate_v1 water_accept and water_dial_fixed Application
  • “Fixed” has two meanings. In watm_dial_fixed_v1, the WATM fixes the address. In water_dial_fixed, the application does.
  • water_dial takes the network and the address as text, for example tcp and example.com:443. Before the host dials, it asks the address validator of the application (in Go, DialedAddressValidator in water.Config). If there is no validator, every address is refused. Only a fixed dialer can use water_dial. In the other roles it always fails, even if there is a validator.
  • water_accept waits until a client connects.
  • The role export returns the file descriptor of the network connection. watm_associate_v1 returns 0 instead.
  • Limit: in water_dial, the network and the address can have at most 256 bytes each.
  • Limit: the host can only hand over TCP sockets (in Go, *net.TCPConn). For any other connection, the import returns an error. There are no datagrams, and a WATM cannot run directly on top of another transport or WATM: the application has to connect the two with a local TCP connection. Other layers can use the connection that a WATM provides.
  • Limit: the application cannot close only one direction of its connection (half-close).

Configuration

If the application sets a configuration (in Go, TransportModuleConfig in water.Config), the host provides its bytes, unchanged, as the file /conf/watm.cfg. The file is in memory and exists before _start is called. Its format is up to the WATM, which has to read the file itself. If no configuration is set, the host does not create the file.

Errors

Every import and export in the tables above returns one s32. A value >= 0 means success, and is a file descriptor where the tables say fd or net_fd. A negative value means an error. It is the negated errno of WASI Preview 1, for example -8 for EBADF. The host returns these errors from the imports:

Error Value Meaning
EBADF -8 The connection cannot be handed over, for example because it is not a TCP socket
EINVAL -28 The network or the address is too long or cannot be read (water_dial only)
ENODEV -43 The role does not have this import. A dialer or a fixed dialer cannot accept, and a listener cannot dial
ENOTCONN -53 Dialing or accepting failed, or the address was refused. Only the log of the host has the cause

If watm_init_v1, a role export or watm_ctrlpipe_v1 returns an error or traps, the application gets an error instead of a connection. Limit: for a relay, this error ends the whole relay, which then serves no new connections.

Cancellation and closing

The control pipe is a local TCP connection. The host keeps one end and passes the other end to watm_ctrlpipe_v1.

  1. When the application closes its connection, the host writes one byte, 0x00, to the control pipe. Then it waits for watm_start_v1 to return.
  2. The worker must wait on the control pipe together with its two connections, and must return when the control pipe becomes readable. The value of the byte has no meaning. The worker can return ECANCELED (-11), which the host does not treat as an error.
  3. After the worker has returned, the host closes all connections it gave to the WATM and destroys the instance.

The worker can also return first, for example because one of its connections has ended. If it returns an error other than ECANCELED, or traps, the host closes the connections and destroys the instance. If it returns ECANCELED or a value >= 0 first, what the host does is not specified.

  • Limit: if the worker ignores the control pipe, closing the connection blocks until the worker returns or the host terminates it.
  • Limit: in Go, canceling a context that was passed to the host does not wake up a worker that waits in poll_oneoff.
  • Limit: closing a relay stops accepting connections, but does not stop the workers that are already running.

API Represented in Different Programming Languages

To help developers understand the API, we provide the following representations of the API in different programming languages that could be more intuitive than the above table.

C representation

(planned feature)

Go representation

Imports
//go:wasmimport env water_dial
//go:noescape
func water_dial(
	networkIovs unsafe.Pointer, networkIovsLen size,
	addressIovs unsafe.Pointer, addressIovsLen size,
) (fd int32)

//go:wasmimport env water_dial_fixed
//go:noescape
func water_dial_fixed() (fd int32)

//go:wasmimport env water_accept
//go:noescape
func water_accept() (fd int32)
Exports
//export watm_init_v1
func _init() int32

//export watm_ctrlpipe_v1
func _ctrlpipe(ctrlFd int32) int32

//export watm_dial_v1
func _dial(internalFd int32) (networkFd int32)

//export watm_dial_fixed_v1
func _dial_fixed(internalFd int32) (networkFd int32)

//export watm_accept_v1
func _accept(internalFd int32) (networkFd int32)

//export watm_associate_v1
func _associate() int32 

//export watm_start_v1
func _start() int32

Rust representation

(to be added)

WIT representation

Similarly, such API can be represented in WIT.

Note that the following is a simplified example and may not strictly follow the WIT syntax. In other words, we do not guarantee the following code to be compilable.

Imports
// imports.wit
package env;

interface host {
    water_dial: func(network_iovs: ciovec_array, addr_iovs: ciovec_array) -> (net_fd: s32);
    water_dial_fixed: func() -> (net_fd: s32);
    water_accept: func() -> (net_fd: s32);
}

world host-world {
    import host;
}
Exports

// exports.wit
interface wasm {
    watm_init_v1: func() -> (errno: s32);
    watm_ctrlpipe_v1: func(ctrl_fd: s32) -> (errno: s32);
    watm_dial_v1: func(internal_fd: s32) -> (net_fd: s32);
    watm_dial_fixed_v1: func(internal_fd: s32) -> (net_fd: s32);
    watm_accept_v1: func(internal_fd: s32) -> (net_fd: s32);
    watm_associate_v1: func() -> (errno: s32); 
    watm_start_v1: func() -> (errno: s32);
}

world wasm-world {
    export wasm;
}

This site uses Just the Docs, a documentation theme for Jekyll.