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

feat: early muxer negotiation #274

Merged
merged 7 commits into from
Jan 11, 2023
Merged
Changes from 4 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
46 changes: 46 additions & 0 deletions content/concepts/multiplex/early-negotiation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: "Early Multiplexer Negotiation"
description: "Early stream multiplexer negotiation is an optimization in libp2p where peers can negotiate which multiplexer to use during the security protocol handshake, saving one round trip."
weight: 162
---

## Vanilla stream multiplexer selection process

Peers upgrade raw transport connections by using the same
[multistream-selection](https://github.com/multiformats/multistream-select)
protocol to negotiate security and stream multiplexing.
salmad3 marked this conversation as resolved.
Show resolved Hide resolved

First, the security protocol is negotiated, then this protocol is used to perform a cryptographic handshake. libp2p currently supports [Noise](../secure-comm/noise) and [TLS 1.3](../secure-comm/tls).
Once the cryptographic handshake completes, multistream-select runs again on top of
the secured connection to negotiate a steam multiplexer, like [yamux](yamux) or [mplex](mplex).

<!-- ADD DIAGRAM -->
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we planning to merge without resolving this TODO? Should we create an issue so we don't forget?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have them noted down, and will also add to the issue open about unified diagrams.


## Early muxer negotiation

salmad3 marked this conversation as resolved.
Show resolved Hide resolved
Early muxer negotiation is possible through the handshake of security protocols being able to negotiate higher-level
protocols. The early negotiation takes place as a list of supported stream muxers is shared during the security protocol
handshake, and a security protocol extension handles the stream muxer negotiation while it negotiates the secure channel
establishment. This saves 1 RTT during the libp2p handshake and, as a result, reduces the TTFB (time to first byte).
salmad3 marked this conversation as resolved.
Show resolved Hide resolved

<!-- ADD DIAGRAM -->

### ALPN extension in TLS

The [Application-Layer Protocol Negotiation (ALPN) extension](https://datatracker.ietf.org/doc/html/rfc7301) is a feature of
TLS that allows for the negotiation of application-layer protocols during the TLS handshake. This allows the client and server
to agree on the application-layer protocol for the rest of the TLS session. ALPN is typically used to negotiate the application-layer protocol
for applications that use TLS, such as HTTP/2 or QUIC. libp2p uses ALPN to negotiate the stream muxer and saves a roundtrip when
upgrading a raw connection.

### Extension registry in Noise

Since there's no commonly used extension mechanism in Noise, libp2p defines an extension registry. We then defined an extension to negotiate the stream multiplexer, that is conceptually the equivalent of the ALPN extension in TLS.

> The extension registry is modeled after
> [RFC 6066](https://www.rfc-editor.org/rfc/rfc6066) (for TLS) and
> [RFC 9000](https://datatracker.ietf.org/doc/html/rfc9000#section-19.21)
> (for QUIC).

More information is available in the
[Noise specification](https://github.com/libp2p/specs/blob/master/noise/README.md#libp2p-data-in-handshake-messages).
salmad3 marked this conversation as resolved.
Show resolved Hide resolved