Skip to content

Commit c4217cd

Browse files
committed
Update spec to use stream ID pointer as protocol ID and version
1 parent 3f0745c commit c4217cd

1 file changed

Lines changed: 50 additions & 22 deletions

File tree

datagrams/datagrams.md

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,74 @@ and spec status.
2222

2323
- [Introduction](#introduction)
2424
- [Native support at the transport level](#native-support-at-the-transport-level)
25-
- [Encoding Datagrams](#encoding-datagrams)
25+
- [Datagrams on QUIC](#datagrams-on-quic)
26+
- [Encoding](#encoding)
27+
- [Datagrams on WebTransport](#datagrams-on-webtransport)
2628
- [Datagrams on top of a multiplexed stream on top of TCP](#datagrams-on-top-of-a-multiplexed-stream-on-top-of-tcp)
2729
- [Datagrams with WebRTC](#datagrams-with-webrtc)
2830

2931
## Introduction
3032

31-
This specification defines the datagrams on libp2p, which enable the
32-
transmission of MTU-sized, unreliable, and low latency messages between peers.
33+
This specification defines libp2p datagrams, which enable the transmission of
34+
MTU-sized, unreliable, and low latency messages between peers.
3335

3436
TODO write more about the tradeoffs here as well as the kinds of applications that would benefit from using datagrams.
3537

3638
## Native support at the transport level
3739

38-
Some libp2p transports natively support datagrams, such as QUIC and WebTransport. libp2p implementations MUST use the native datagram support to send datagrams.
40+
Some libp2p transports natively support datagrams, such as QUIC and
41+
WebTransport. libp2p implementations MUST use the native datagram support to
42+
send datagrams.
3943

40-
QUIC has an Unreliable Datagram Extension [RFC 9221]
41-
WebTransport supports Datagrams though HTTP Datagrams [WebTransport draft RFC](https://www.ietf.org/archive/id/draft-ietf-webtrans-http3-12.html#name-datagrams)
44+
- QUIC has an Unreliable Datagram Extension [RFC 9221].
45+
- WebTransport supports Datagrams though HTTP Datagrams [WebTransport draft RFC](https://www.ietf.org/archive/id/draft-ietf-webtrans-http3-12.html#name-datagrams)
4246

43-
## Encoding Datagrams
47+
## Datagrams on QUIC
4448

45-
The libp2p datagram encoding introduces minimal framing overhead to application payloads. Each datagram MUST be encoded with the following structure to ensure proper protocol identification and version compatibility.
49+
Each datagram flow MUST be associated with a control stream. A datagram flow is
50+
defined as a logical flow of datagrams for a specific application protocol. A
51+
control stream is a QUIC bidirectional stream that has negotiated the application
52+
protocol ID. The control stream MUST stay open for the duration of the datagram
53+
flow. Implementation MAY create the control stream and start sending datagrams
54+
at once.
4655

47-
The datagram frame consists of the following components:
56+
There is currently no other use for the control stream besides negotiating the
57+
application protocol ID.
4858

49-
| Field Name | Data Type | Length (bits) | Description |
50-
| ------------------ | ---------------- | ------------- | ---------------------------------------------------- |
51-
| version | unsigned integer | 8 | Protocol version identifier |
52-
| protocol id length | [uvarint] | 8..72 | Length of the protocol identifier field |
53-
| protocol id | byte sequence | variable | Protocol identifier as defined by protocol id length |
54-
| application data | byte sequence | variable | Application payload |
59+
Receipt of a QUIC DATAGRAM frame whose payload is too short to allow parsing the
60+
Control Stream ID field MUST be treated as a connection error of type
61+
PROTOCOL_VIOLATION (0x1003).
5562

56-
The complete datagram frame is constructed as follows:
63+
libp2p datagrams MUST NOT be sent unless the control stream's send side is
64+
open. If a datagram is received after the corresponding stream's receive side is
65+
closed, the received datagrams MUST be silently dropped.
5766

58-
```
59-
version || protocol id length || protocol id || application data
60-
```
67+
If a libp2p datagram is received and its Control Stream ID field maps to a
68+
stream that has not yet been created, the receiver SHALL either drop that
69+
datagram silently or buffer it temporarily (on the order of a round trip) while
70+
awaiting the creation of the corresponding stream.
6171

62-
Where "||" denotes byte concatenation.
72+
If a libp2p datagram is received and its Control Stream ID field maps to a
73+
stream that cannot be created due to client-initiated bidirectional stream
74+
limits, it MUST be treated as a connection error of type PROTOCOL_VIOLATION
75+
(0x1003).
6376

64-
The version field provides extensibility for future encoding modifications while maintaining backward compatibility. Implementations MUST verify the version field before processing the remainder of the datagram.
77+
### Encoding
78+
79+
Each libp2p datagram SHALL be encoded with the following structure.
80+
81+
| Field Name | Data Type | Length (bits) | Description |
82+
| ---------------- | ------------- | ------------- | --------------------------------------------------------------------- |
83+
| Control StreamID | [QUIC varint] | 8..64 | The stream ID of the associated control stream for this datagram flow |
84+
| Application Data | byte sequence | variable | Application payload |
85+
86+
## Datagrams on WebTransport
87+
88+
libp2p datagrams on WebTransport behave the same as libp2p datagrams on QUIC.
89+
However, the application data may be further limited by the overhead of HTTP
90+
Datagram's Quarter Stream ID field.
91+
92+
In the future, a separate spec may be able to remove this overhead.
6593

6694
## Datagrams on top of a multiplexed stream on top of TCP
6795

@@ -71,5 +99,5 @@ This is currently not specified.
7199

72100
This is currently not specified.
73101

74-
[uvarint]: https://github.com/multiformats/unsigned-varint
75102
[RFC 9221]: https://www.rfc-editor.org/rfc/rfc9221
103+
[QUIC varint]: https://www.rfc-editor.org/rfc/rfc9000.html#name-variable-length-integer-enc

0 commit comments

Comments
 (0)