Skip to content

Commit

Permalink
quic: add scaffolding for http3 internals
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Dec 29, 2023
1 parent 2057b2b commit 1ae7f4b
Show file tree
Hide file tree
Showing 16 changed files with 1,079 additions and 154 deletions.
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@
'src/quic/cid.cc',
'src/quic/data.cc',
'src/quic/endpoint.cc',
'src/quic/http3.cc',
'src/quic/logstream.cc',
'src/quic/packet.cc',
'src/quic/preferredaddress.cc',
Expand All @@ -368,6 +369,7 @@
'src/quic/cid.h',
'src/quic/data.h',
'src/quic/endpoint.h',
'src/quic/http3.h',
'src/quic/logstream.h',
'src/quic/packet.h',
'src/quic/preferredaddress.h',
Expand Down
69 changes: 36 additions & 33 deletions src/quic/application.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#if HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC

#include "application.h"
#include <debug_utils-inl.h>
#include <async_wrap-inl.h>
#include <debug_utils-inl.h>
#include <node_bob.h>
#include <node_sockaddr-inl.h>
#include <uv.h>
#include <v8.h>
#include "defs.h"
#include "endpoint.h"
#include "http3.h"
#include "packet.h"
#include "session.h"

Expand All @@ -23,33 +24,20 @@ using v8::Value;

namespace quic {

struct Session::Application::StreamData final {
// The actual number of vectors in the struct, up to kMaxVectorCount.
size_t count = 0;
size_t remaining = 0;
// The stream identifier. If this is a negative value then no stream is
// identified.
int64_t id = -1;
int fin = 0;
ngtcp2_vec data[kMaxVectorCount]{};
ngtcp2_vec* buf = data;
BaseObjectPtr<Stream> stream;
};

// ============================================================================
// Session::Application_Options
const Session::Application_Options Session::Application_Options::kDefault = {};

Session::Application_Options::operator const nghttp3_settings() const {
// In theory, Application_Options might contain options for more than just
// HTTP/3. Here we extract only the properties that are relevant to HTTP/3.
return nghttp3_settings {
max_field_section_size,
qpack_max_dtable_capacity,
qpack_encoder_max_dtable_capacity,
qpack_blocked_streams,
enable_connect_protocol,
enable_datagrams,
return nghttp3_settings{
max_field_section_size,
qpack_max_dtable_capacity,
qpack_encoder_max_dtable_capacity,
qpack_blocked_streams,
enable_connect_protocol,
enable_datagrams,
};
}

Expand All @@ -59,13 +47,14 @@ std::string Session::Application_Options::ToString() const {
std::string res("{");
res += prefix + "max header pairs: " + std::to_string(max_header_pairs);
res += prefix + "max header length: " + std::to_string(max_header_length);
res += prefix + "max field section size: " +
std::to_string(max_field_section_size);
res += prefix +
"max field section size: " + std::to_string(max_field_section_size);
res += prefix + "qpack max dtable capacity: " +
std::to_string(qpack_max_dtable_capacity);
res += prefix + "qpack encoder max dtable capacity: " +
std::to_string(qpack_encoder_max_dtable_capacity);
res += prefix + "qpack blocked streams: " + std::to_string(qpack_blocked_streams);
res += prefix +
"qpack blocked streams: " + std::to_string(qpack_blocked_streams);
res += prefix + "enable connect protocol: " +
(enable_connect_protocol ? std::string("yes") : std::string("no"));
res += prefix + "enable datagrams: " +
Expand Down Expand Up @@ -118,7 +107,10 @@ bool Session::Application::Start() {

void Session::Application::AcknowledgeStreamData(Stream* stream,
size_t datalen) {
Debug(session_, "Application acknowledging stream %" PRIi64 " data: %zu", stream->id(), datalen);
Debug(session_,
"Application acknowledging stream %" PRIi64 " data: %zu",
stream->id(),
datalen);
DCHECK_NOT_NULL(stream);
stream->Acknowledge(datalen);
}
Expand Down Expand Up @@ -184,7 +176,8 @@ Session::Application::ExtractSessionTicketAppData(
void Session::Application::SetStreamPriority(const Stream& stream,
StreamPriority priority,
StreamPriorityFlags flags) {
Debug(session_, "Application setting stream %" PRIi64 " priority", stream.id());
Debug(
session_, "Application setting stream %" PRIi64 " priority", stream.id());
// By default do nothing.
}

Expand All @@ -201,20 +194,29 @@ BaseObjectPtr<Packet> Session::Application::CreateStreamDataPacket() {
}

void Session::Application::StreamClose(Stream* stream, QuicError error) {
Debug(session_, "Application closing stream %" PRIi64 " with error %s", stream->id(), error);
Debug(session_,
"Application closing stream %" PRIi64 " with error %s",
stream->id(),
error);
stream->Destroy(error);
}

void Session::Application::StreamStopSending(Stream* stream, QuicError error) {
Debug(session_, "Application stopping sending on stream %" PRIi64 " with error %s", stream->id(), error);
Debug(session_,
"Application stopping sending on stream %" PRIi64 " with error %s",
stream->id(),
error);
DCHECK_NOT_NULL(stream);
stream->ReceiveStopSending(error);
}

void Session::Application::StreamReset(Stream* stream,
uint64_t final_size,
QuicError error) {
Debug(session_, "Application resetting stream %" PRIi64 " with error %s", stream->id(), error);
Debug(session_,
"Application resetting stream %" PRIi64 " with error %s",
stream->id(),
error);
stream->ReceiveStreamReset(final_size, error);
}

Expand Down Expand Up @@ -498,13 +500,14 @@ class DefaultApplication final : public Session::Application {
};

std::unique_ptr<Session::Application> Session::select_application() {
// if (config.options.crypto_options.alpn == NGHTTP3_ALPN_H3)
// return std::make_unique<Http3>(session,
// config.options.application_options);

// In the future, we may end up supporting additional QUIC protocols. As they
// are added, extend the cases here to create and return them.

if (config_.options.tls_options.alpn == NGHTTP3_ALPN_H3) {
Debug(this, "Selecting HTTP/3 application");
return createHttp3Application(this, config_.options.application_options);
}

Debug(this, "Selecting default application");
return std::make_unique<DefaultApplication>(
this, config_.options.application_options);
Expand Down
16 changes: 16 additions & 0 deletions src/quic/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class Session::Application : public MemoryRetainer {
protected:
inline Environment* env() const { return session_->env(); }
inline Session& session() { return *session_; }
inline const Session& session() const { return *session_; }

BaseObjectPtr<Packet> CreateStreamDataPacket();

Expand All @@ -137,6 +138,21 @@ class Session::Application : public MemoryRetainer {
Session* session_;
};

struct Session::Application::StreamData final {
// The actual number of vectors in the struct, up to kMaxVectorCount.
size_t count = 0;
size_t remaining = 0;
// The stream identifier. If this is a negative value then no stream is
// identified.
int64_t id = -1;
int fin = 0;
ngtcp2_vec data[kMaxVectorCount]{};
ngtcp2_vec* buf = data;
BaseObjectPtr<Stream> stream;

inline operator nghttp3_vec() const { return {data[0].base, data[0].len}; }
};

} // namespace quic
} // namespace node

Expand Down
3 changes: 2 additions & 1 deletion src/quic/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace quic {
#define NGTCP2_ERR(V) (V != NGTCP2_SUCCESS)
#define NGTCP2_OK(V) (V == NGTCP2_SUCCESS)

#define IF_QUIC_DEBUG(env) \
#define IF_QUIC_DEBUG(env) \
if (UNLIKELY(env->enabled_debug_list()->enabled(DebugCategory::QUIC)))

template <typename Opt, std::string Opt::*member>
Expand Down Expand Up @@ -169,6 +169,7 @@ class DebugIndentScope {
res += "}";
return res;
}

private:
static int indent_;
};
Expand Down
Loading

0 comments on commit 1ae7f4b

Please sign in to comment.