{% extends "_layout.html" %} {% block title %}Streaming Lib{% endblock %} {% block content %}

Overview

The streaming library is technically part of the "application" layer, as it is not a core router function. In practice, however, it provides a vital function for almost all existing I2P applications, by providing a TCP-like streams over I2P, and allowing existing apps to be easily ported to I2P.

The streaming library is a layer on top of the core I2CP that allows reliable, in order, and authenticated streams of messages to operate across an unreliable, unordered, and unauthenticated message layer. Just like the TCP to IP relationship, this streaming functionality has a whole series of tradeoffs and optimizations available, but rather than embed that functionality into the base I2P code, it has been factored off into its own library both to keep the TCP-esque complexities separate and to allow alternative optimized implementations.

History

The streaming library has grown organically for I2P - first mihi implemented the "mini streaming library" as part of I2PTunnel, which was limited to a window size of 1 message (requiring an ACK before sending the next one), and then it was refactored out into a generic streaming interface (mirroring TCP sockets) and the full streaming implementation was deployed with a sliding window protocol and optimizations to take into account the high bandwidth x delay product. Individual streams may adjust the maximum packet size and other options. The default message size is selected to fit precisely in two 1K I2NP tunnel messages, and is a reasonable tradeoff between the bandwidth costs of retransmitting lost messages and the latency of multiple messages.

In addition, in consideration of the relatively high cost of subsequent messages, the streaming library's protocol for scheduling and delivering messages has been optimized to allow individual messages passed to contain as much information as is available. For instance, a small HTTP transaction proxied through the streaming library can be completed in a single round trip - the first messages bundle a SYN, FIN, and the small HTTP request payload, and the reply bundles the SYN, FIN, ACK, and the HTTP response payload. While an additional ACK must be transmitted to tell the HTTP server that the SYN/FIN/ACK has been received, the local HTTP proxy can often deliver the full response to the browser immediately.

On the whole, however, the streaming library bears much resemblance to an abstraction of TCP, with its sliding windows, congestion control algorithms (both slow start and congestion avoidance), and general packet behavior (ACK, SYN, FIN, RST, rto calculation, etc).

Usage

The standard interface to the streaming lib is for the application to set up a I2PSocketManagerFactory from the ministreaming lib. Only I2PSocketManagerFactory is used here - everything else is from the full streaming lib (I2PSocketManagerFull, not I2PSocketManagerImpl, and I2PSocketFull, not I2PSocketImpl). The remainder of the ministreaming lib is not normally used - don't be confused.

For a good example of usage, see the i2psnark code.

Advantages

The streaming lib has many advantages over the ministreaming library written by mihi as a part of his I2PTunnel application. The streaming library is a more robust streaming library which is further optimized for operation over I2P. The two main issues with the ministreaming library are its use of the traditional TCP two phase establishment protocol and the current fixed window size of 1. The streaming lib fixes both of these issues - it has a one-phase setup, and it contains a full windowing implementation.

Significant tuning of the streaming lib parameters, greatly increasing outbound performance, was implemented in 0.6.1.28. Subsequent releases include additional tuning and bug fixes.

Default Parameters

The current default values are listed below. Lower case values are streaming lib parameters that can changed on a per-connection basis. These values are tuned for HTTP performance over typical I2P conditions. Other applications such as peer-to-peer services are strongly encouraged to modify as necessary, by setting the options and passing them via the call to I2PSocketManagerFactory.createManager(_i2cpHost, _i2cpPort, opts). Time values are in ms.

The streaming lib uses standard slow-start (exponential window growth) and congestion avoidance (linear window growth) phases. However, before the 0.6.1.33 release, window growth was substantially slower than optimal; these issues were fixed in release 0.6.1.33.

The maximum message size (also called the MTU / MRU) is negotiated to the lower value supported by the two peers. As tunnel messages are padded to 1KB, a poor MTU selection will lead to a large amount of overhead. The MTU is chosen to fit precisely in an integral number of 1K I2NP tunnel messages, including overhead for the typical case. The first message in a connection includes a 387 byte (typical) Destination added by the streaming layer, and usually a 898 byte (typical) LeaseSet bundled in the Garlic message. Therefore, the goal of fitting a complete HTTP request in a single 1KB I2NP message is not realistic. However, the selection of the MTU, together with careful implementation of fragmentation and batching strategies in the tunnel gateway processor, are important factors in network bandwidth, latency, reliability, and efficiency, especially for long-lived connections.

The interaction of the routing algorithms with the streaming lib strongly affects performance. In particular, random distribution of messages to multiple tunnels in a pool leads to a high degree of out-of-order delivery which results in smaller window sizes than would otherwise be the case. In release 0.6.1.30, the routing of messages to the outbound tunnels was made consistent, with pushback when a tunnel was backlogged. This had a significant positive impact on bandwidths. The pushback code was reverted in release 0.6.1.31 due to anonymity concerns. Consistent message routing to inbound tunnels was implemented in release 0.6.1.32.

Another area for research is the interaction of the streaming lib with the NTCP and SSU transport layers. See the NTCP page for a discussion.

Packet Format

Here is the format of a single packet transferred as part of a streaming connection.
FieldLengthContents
sendStreamId 4 byte valueRandom number selected by the connection recipient and constant for the life of the connection. 0 in the SYN message sent by the originator.
receiveStreamId 4 byte valueRandom number selected by the connection originator and constant for the life of the connection.
sequenceNum 4 byte unsigned integer The sequence for this message, starting at 0 in the SYN message, and incremented by 1 in each message except for plain ACKs and retransmissions. If the sequenceNum is 0 and the SYN is not set, this is a plain ACK packet that should not be ACKed.
ackThrough 4 byte unsigned integer The highest packet sequence number that was received on the receiveStreamId. This field is ignored on the initial connection packet (where receiveStreamId is the unknown id) or if FLAG_NO_ACK is set. All packets up to and including this sequence number are ACKed, EXCEPT for those listed in NACKs below.
number of NACKs 1 byte unsigned integer
that many NACKs n * 4 byte unsigned integers Sequence numbers less than ackThrough that are not yet received. Two NACKs of a packet is a request for a 'fast retransmit' of that packet.
resendDelay 1 byte unsigned integer How long is the creator of this packet going to wait before resending this packet (if it hasn't yet been ACKed). The value is seconds since the packet was created. Ignored on receive. Broken on send before release 0.7.8 (the sender did not divide by 1000, and the default is 1000 ms, so the included value was 1000 & 0xff = 0xe8 = 232 seconds.
flags 2 byte value See below.
option data size 2 byte unsigned integer See below.
option data specified by those flags 0 or more bytes See below.
payload remaining packet size

The flags field above specifies some metadata about the packet, and in turn may require certain additional data to be included. The flags are as follows (with any data structures specified added to the options area in the given order):

Bit NumberFlagOption DataFunction
0FLAG_SYNCHRONIZEno option data Similar to TCP SYN.
1FLAG_CLOSEno option data Similar to TCP FIN. If the response to a SYN fits in a single message, the response will contain both FLAG_SYNCHRONIZE and FLAG_CLOSE.
2FLAG_RESETno option data Abnormal close.
3FLAG_SIGNATURE_INCLUDED40 bytesnet.i2p.data.Signature Typically sent only with FLAG_SYNCHRONIZE and FLAG_CLOSE, where it is required. If the signature is included, it uses the Destination's DSA key to sign the entire header and payload with the space in the options for the signature being set to all zeroes.
4FLAG_SIGNATURE_REQUESTEDno option data Unused. Requests every packet in the other direction to have FLAG_SIGNATURE_INCLUDED
5FLAG_FROM_INCLUDEDtyp. 387 bytesnet.i2p.data.Destination Typically sent only with FLAG_SYNCHRONIZE.
6FLAG_DELAY_REQUESTED2 byte integer Optional delay. How many milliseconds the sender of this packet wants the recipient to wait before sending any more data. A value greater than 60000 indicates choking.
7FLAG_MAX_PACKET_SIZE_INCLUDED2 byte integer Sent with FLAG_SYNCHRONIZE or with a retransmission, could be optimized to only send with a SYN.
8FLAG_PROFILE_INTERACTIVEno option data Apparently unused or ignored
9FLAG_ECHOno option data Unused except by ping programs
10FLAG_NO_ACKno option data Apparently unused, an ACK is always included. This flag simply tells the recipient to ignore the ackThrough field in the header.
11-15unused

Control Block Sharing

As of release 0.7.1, the streaming lib supports "TCP" Control Block sharing. This shares two important streaming lib parameters (window size and round trip time) across connections to the same remote peer. This is used for "temporal" sharing at connection open/close time, not "ensemble" sharing during a connection (See RFC 2140). There is a separate share per ConnectionManager (i.e. per local Destination) so that there is no information leakage to other Destinations on the same router.

Future Work and Proposals

There are proposals to replace the streaming lib with standard TCP (or perhaps a null layer together with raw sockets). This would unfortunately be incompatible with the streaming lib but it would be good to compare the performance of the two.

{% endblock %}