Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multigram draft 1 #123

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions multigram/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# Multigram -- protocol negotiation and multiplexing over datagrams

This document describes:
- Multigram, a self-describing packet format for multiplexing different protocols on the same datagram connection.
- Multigram-Setup, a protocol for negotiating a shared table of protocol identifiers for use with Multigram.

Multigram is part of the [Multiformats family][multiformats].

- Introduction
- Protocol table
- Multigram-Setup
- Implementations

Note: this document makes use of the [Multiaddr format][multiaddr] whenever it mentions network addresses.

[multiformats]: https://github.com/multiformats
[multiaddr]: https://github.com/multiformats/multiaddr


## Introduction

Multigram operates on datagrams, which can be UDP packets, Ethernet frames, etc. and which are unreliable and unordered.
All it does is prepend a field to the packet, which signifies the protocol of this packet.
The endpoints of the connection can then use different packet handlers per protocol.

If you're looking for similar functionality on top of reliable streams, check out the [Multistream format][multistream].

[multistream]: https://github.com/multistream


## Packet layout

```
1 2 3 4
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0 | Table Index | |
+-+-+-+-+-+-+-+-+ Packet Payload +
4 | |
+
```

- **Table Index:**
- Type: `varint`
- The numerical identifier signifying the protocol of this packet, as per the protocol table.
- **Packet Payload:**
- Type: `[]byte`
- The raw data of this packet. This is what the packet handler gets to see.






For multiplexing different protocols on the same datagram connection, multigram prepends a 1-byte header to every packet. This header represents an index in a table of protocols shared between both endpoints. This protocol table is negotiated by exchanging the intersection of the endpoint's supported protocols. The protocol table's size of 256 tuples can be increased by nesting multiple multigram headers.

TODO: analyze the properties vs. other approaches (e.g. an identifier on every packet).

- whyrusleeping
- setup packets and data packets are always separate (proto =0 vs. proto >0)
- just start sending data packets
- the other end will respond with error packets
- the other end MAY buffer packets with a proto it doesn't know yet
- until you get an ack, send a setup packet for every packet of the given protocol
- dont do table exchange, setup protocols as needed
- jbenet
- include checksum
- so we can work on raw ip
- udp checksums suck
- would be great to fit in 3/7/11/15 bytes, to fit 4-byte word length
- out of scope, should be another format: multisum
- we'll have multiple mutligrams per packet, one checksum for each is a MUST NOT


## Protocol table

Multigram assumes an independent protocol table for each remote address.
For example, datagrams from/to `/ip4/1.2.3.4/udp/4737` will build up their own protocol table
independent from datagrams from/to `/ip4/5.6.7.8/udp/4737`.

The protocol table MUST be append-only and immutable. It MUST initially contain exactly one tuple:

```
0x00,/multigram-setup/0.1.0
```

The `/multigram-setup` protocol is used for appending to the shared protocol table.

```
1 2 3 4
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0 | 0x00 | |
+-+-+-+-+-+-+-+-+ Operation as Multicodec +
4 | |
+
```

- Note how the Table Index field is set to `0x00`, selecting the `/multigram-setup` protocol.
- The Operation field MUST support at least the `/cbor/` multicodec format. It SHOULD support the `/protobuf/` and `/json/` formats.
Copy link
Member

@Kubuxu Kubuxu Jul 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to see bencode as main protocol for it simplicity which fits the data we will be sending in this protocol. CBOR, protobuf and JSON definitions are all tenths of pages long. In contrast bencode is both short to define and to write parser for (most implementations are sub 500 LOC).

http://jonas.nitro.dk/bittorrent/bittorrent-rfc.html#anchor7

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we should totally discuss which formats MUST and SHOULD be included, and I agree that bencode should be in there somewhere.

- In the future, an `/ipfs/` format can be used to resolve code and specification for supporting the format.

## Protocol table operations / multigram-setup

Either endpoint can send `append` proposals, and the other endpoint will reply with the result, based on their own supported protocols.

(1) Endpoint A sends a proposal to Endpoint B. A doesn't commit this proposal to its own view of the protocol table. It dismisses it right away and waits for B's reply.
```
0x00
/json/
{"0x01":"/foo/1.0.0","0x02":"/bar/1.0.0","0x03":"/baz/1.0.0"}
```

(2) Endpoint B forms the intersection of this proposal and its own supported protocols, appends to its own view of the protocol table, and replies.
Copy link
Member

@Kubuxu Kubuxu Jul 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem I see with this is how we react when two sides start appending at the same time?
Also as we will be using it over bare UDP so we should deal with datagram loss and reordering.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The endpoint should respond with some kind of error if it gets a proposal with conflicts with its own pending proposal. So these two would cancel each other out.

```
0x00
/json/
{"0x01":"/foo/1.0.0","0x02":"/bar/1.0.0"}
```

(3) We can list the table by sending the other endpoint an empty proposal.
```
0x00
/json/
{}
```

(4) The protocol table now contains `0x00`, `0x01`, and `0x02`.
```
0x00
/json/
{"0x01":"/foo/1.0.0","0x02":"/bar/1.0.0"}
```

Any field in the operation data starting in `0x` is considered for being appended.
Any other field names can be used e.g. for checksums, detecting packet loss, or communicating errors.
This is subject to updates of the multigram-setup protocol.

## Nested multigrams

The protocol tables of nested multigrams can be set up within one packet.
This works because any trailing data will be processed after the table operation.

```
0x00
/json/
{"0x01":"/multigram/0.1.0"}
0x0100
/json/
{"0x01":"/multigram/0.1.0"}
0x010100
/json
{"0x01":"/ipfs/identify/1.0.0","0x02":"/fc00/iptunnel/0.1.0","0x03":"/fc00/pathfinder/0.1.0"}
```

Packets starting in `0x010103` would belong to `/fc00/pathfinder`.