Skip to content

Commit 0bf1548

Browse files
committed
quic: update quic impl to use latest ngtcp2/nghttp3
PR-URL: #51291 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Jiawen Geng <[email protected]>
1 parent 86ca72a commit 0bf1548

14 files changed

+237
-200
lines changed

src/quic/application.cc

+13-13
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ BaseObjectPtr<Packet> Session::Application::CreateStreamDataPacket() {
151151
return Packet::Create(env(),
152152
session_->endpoint_.get(),
153153
session_->remote_address_,
154-
ngtcp2_conn_get_max_udp_payload_size(*session_),
154+
ngtcp2_conn_get_max_tx_udp_payload_size(*session_),
155155
"stream data");
156156
}
157157

@@ -291,18 +291,18 @@ ssize_t Session::Application::WriteVStream(PathStorage* path,
291291
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_NONE;
292292
if (stream_data.remaining > 0) flags |= NGTCP2_WRITE_STREAM_FLAG_MORE;
293293
if (stream_data.fin) flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
294-
ssize_t ret =
295-
ngtcp2_conn_writev_stream(*session_,
296-
&path->path,
297-
nullptr,
298-
buf,
299-
ngtcp2_conn_get_max_udp_payload_size(*session_),
300-
ndatalen,
301-
flags,
302-
stream_data.id,
303-
stream_data.buf,
304-
stream_data.count,
305-
uv_hrtime());
294+
ssize_t ret = ngtcp2_conn_writev_stream(
295+
*session_,
296+
&path->path,
297+
nullptr,
298+
buf,
299+
ngtcp2_conn_get_max_tx_udp_payload_size(*session_),
300+
ndatalen,
301+
flags,
302+
stream_data.id,
303+
stream_data.buf,
304+
stream_data.count,
305+
uv_hrtime());
306306
return ret;
307307
}
308308

src/quic/bindingdata.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class Endpoint;
2323
class Packet;
2424

2525
enum class Side {
26-
CLIENT = NGTCP2_CRYPTO_SIDE_CLIENT,
27-
SERVER = NGTCP2_CRYPTO_SIDE_SERVER,
26+
CLIENT,
27+
SERVER,
2828
};
2929

3030
enum class EndpointLabel {
@@ -119,7 +119,6 @@ constexpr size_t kMaxVectorCount = 16;
119119
V(alpn, "alpn") \
120120
V(application_options, "application") \
121121
V(bbr, "bbr") \
122-
V(bbr2, "bbr2") \
123122
V(ca, "ca") \
124123
V(certs, "certs") \
125124
V(cc_algorithm, "cc") \

src/quic/data.cc

+12-12
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,15 @@ std::string TypeName(QuicError::Type type) {
126126
} // namespace
127127

128128
QuicError::QuicError(const std::string_view reason)
129-
: reason_(reason), ptr_(&error_) {}
129+
: reason_(reason), error_(), ptr_(&error_) {
130+
ngtcp2_ccerr_default(&error_);
131+
}
130132

131-
QuicError::QuicError(const ngtcp2_connection_close_error* ptr)
133+
QuicError::QuicError(const ngtcp2_ccerr* ptr)
132134
: reason_(reinterpret_cast<const char*>(ptr->reason), ptr->reasonlen),
133135
ptr_(ptr) {}
134136

135-
QuicError::QuicError(const ngtcp2_connection_close_error& error)
137+
QuicError::QuicError(const ngtcp2_ccerr& error)
136138
: reason_(reinterpret_cast<const char*>(error.reason), error.reasonlen),
137139
error_(error),
138140
ptr_(&error_) {}
@@ -175,11 +177,11 @@ const std::string_view QuicError::reason() const {
175177
return reason_;
176178
}
177179

178-
QuicError::operator const ngtcp2_connection_close_error&() const {
180+
QuicError::operator const ngtcp2_ccerr&() const {
179181
return *ptr_;
180182
}
181183

182-
QuicError::operator const ngtcp2_connection_close_error*() const {
184+
QuicError::operator const ngtcp2_ccerr*() const {
183185
return ptr_;
184186
}
185187

@@ -212,15 +214,15 @@ void QuicError::MemoryInfo(MemoryTracker* tracker) const {
212214
QuicError QuicError::ForTransport(error_code code,
213215
const std::string_view reason) {
214216
QuicError error(reason);
215-
ngtcp2_connection_close_error_set_transport_error(
217+
ngtcp2_ccerr_set_transport_error(
216218
&error.error_, code, error.reason_c_str(), reason.length());
217219
return error;
218220
}
219221

220222
QuicError QuicError::ForApplication(error_code code,
221223
const std::string_view reason) {
222224
QuicError error(reason);
223-
ngtcp2_connection_close_error_set_application_error(
225+
ngtcp2_ccerr_set_application_error(
224226
&error.error_, code, error.reason_c_str(), reason.length());
225227
return error;
226228
}
@@ -235,22 +237,20 @@ QuicError QuicError::ForIdleClose(const std::string_view reason) {
235237

236238
QuicError QuicError::ForNgtcp2Error(int code, const std::string_view reason) {
237239
QuicError error(reason);
238-
ngtcp2_connection_close_error_set_transport_error_liberr(
240+
ngtcp2_ccerr_set_liberr(
239241
&error.error_, code, error.reason_c_str(), reason.length());
240242
return error;
241243
}
242244

243245
QuicError QuicError::ForTlsAlert(int code, const std::string_view reason) {
244246
QuicError error(reason);
245-
ngtcp2_connection_close_error_set_transport_error_tls_alert(
247+
ngtcp2_ccerr_set_tls_alert(
246248
&error.error_, code, error.reason_c_str(), reason.length());
247249
return error;
248250
}
249251

250252
QuicError QuicError::FromConnectionClose(ngtcp2_conn* session) {
251-
QuicError error;
252-
ngtcp2_conn_get_connection_close_error(session, &error.error_);
253-
return error;
253+
return QuicError(ngtcp2_conn_get_ccerr(session));
254254
}
255255

256256
QuicError QuicError::TRANSPORT_NO_ERROR =

src/quic/data.h

+12-13
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,28 @@ class QuicError final : public MemoryRetainer {
7171
static constexpr error_code QUIC_APP_NO_ERROR = 65280;
7272

7373
enum class Type {
74-
TRANSPORT = NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT,
75-
APPLICATION = NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_APPLICATION,
76-
VERSION_NEGOTIATION =
77-
NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT_VERSION_NEGOTIATION,
78-
IDLE_CLOSE = NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT_IDLE_CLOSE,
74+
TRANSPORT = NGTCP2_CCERR_TYPE_TRANSPORT,
75+
APPLICATION = NGTCP2_CCERR_TYPE_APPLICATION,
76+
VERSION_NEGOTIATION = NGTCP2_CCERR_TYPE_VERSION_NEGOTIATION,
77+
IDLE_CLOSE = NGTCP2_CCERR_TYPE_IDLE_CLOSE,
7978
};
8079

8180
static constexpr error_code QUIC_ERROR_TYPE_TRANSPORT =
82-
NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_TRANSPORT;
81+
NGTCP2_CCERR_TYPE_TRANSPORT;
8382
static constexpr error_code QUIC_ERROR_TYPE_APPLICATION =
84-
NGTCP2_CONNECTION_CLOSE_ERROR_CODE_TYPE_APPLICATION;
83+
NGTCP2_CCERR_TYPE_APPLICATION;
8584

8685
explicit QuicError(const std::string_view reason = "");
87-
explicit QuicError(const ngtcp2_connection_close_error* ptr);
88-
explicit QuicError(const ngtcp2_connection_close_error& error);
86+
explicit QuicError(const ngtcp2_ccerr* ptr);
87+
explicit QuicError(const ngtcp2_ccerr& error);
8988

9089
Type type() const;
9190
error_code code() const;
9291
const std::string_view reason() const;
9392
uint64_t frame_type() const;
9493

95-
operator const ngtcp2_connection_close_error&() const;
96-
operator const ngtcp2_connection_close_error*() const;
94+
operator const ngtcp2_ccerr&() const;
95+
operator const ngtcp2_ccerr*() const;
9796

9897
// Returns false if the QuicError uses a no_error code with type
9998
// transport or application.
@@ -130,8 +129,8 @@ class QuicError final : public MemoryRetainer {
130129
const uint8_t* reason_c_str() const;
131130

132131
std::string reason_;
133-
ngtcp2_connection_close_error error_ = ngtcp2_connection_close_error();
134-
const ngtcp2_connection_close_error* ptr_ = nullptr;
132+
ngtcp2_ccerr error_;
133+
const ngtcp2_ccerr* ptr_ = nullptr;
135134
};
136135

137136
} // namespace quic

src/quic/endpoint.cc

+15-12
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ namespace quic {
7373
#define ENDPOINT_CC(V) \
7474
V(RENO, reno) \
7575
V(CUBIC, cubic) \
76-
V(BBR, bbr) \
77-
V(BBR2, bbr2)
76+
V(BBR, bbr)
7877

7978
struct Endpoint::State {
8079
#define V(_, name, type) type name;
@@ -427,11 +426,15 @@ int Endpoint::UDP::Bind(const Endpoint::Options& options) {
427426
}
428427

429428
void Endpoint::UDP::Ref() {
430-
if (!is_closed()) uv_ref(reinterpret_cast<uv_handle_t*>(&impl_->handle_));
429+
if (!is_closed_or_closing()) {
430+
uv_ref(reinterpret_cast<uv_handle_t*>(&impl_->handle_));
431+
}
431432
}
432433

433434
void Endpoint::UDP::Unref() {
434-
if (!is_closed()) uv_unref(reinterpret_cast<uv_handle_t*>(&impl_->handle_));
435+
if (!is_closed_or_closing()) {
436+
uv_unref(reinterpret_cast<uv_handle_t*>(&impl_->handle_));
437+
}
435438
}
436439

437440
int Endpoint::UDP::Start() {
@@ -476,7 +479,7 @@ Endpoint::UDP::operator bool() const {
476479
}
477480

478481
SocketAddress Endpoint::UDP::local_address() const {
479-
DCHECK(!is_closed() && is_bound());
482+
DCHECK(!is_closed_or_closing() && is_bound());
480483
return SocketAddress::FromSockName(impl_->handle_);
481484
}
482485

@@ -1012,7 +1015,7 @@ void Endpoint::Receive(const uv_buf_t& buf,
10121015

10131016
if (options_.validate_address) {
10141017
// If there is no token, generate and send one.
1015-
if (hd.token.len == 0) {
1018+
if (hd.tokenlen == 0) {
10161019
SendRetry(PathDescriptor{
10171020
version,
10181021
dcid,
@@ -1027,9 +1030,9 @@ void Endpoint::Receive(const uv_buf_t& buf,
10271030

10281031
// We have two kinds of tokens, each prefixed with a different magic
10291032
// byte.
1030-
switch (hd.token.base[0]) {
1033+
switch (hd.token[0]) {
10311034
case RetryToken::kTokenMagic: {
1032-
RetryToken token(hd.token.base, hd.token.len);
1035+
RetryToken token(hd.token, hd.tokenlen);
10331036
auto ocid = token.Validate(
10341037
version,
10351038
remote_address,
@@ -1055,7 +1058,7 @@ void Endpoint::Receive(const uv_buf_t& buf,
10551058
break;
10561059
}
10571060
case RegularToken::kTokenMagic: {
1058-
RegularToken token(hd.token.base, hd.token.len);
1061+
RegularToken token(hd.token, hd.tokenlen);
10591062
if (!token.Validate(
10601063
version,
10611064
remote_address,
@@ -1072,8 +1075,8 @@ void Endpoint::Receive(const uv_buf_t& buf,
10721075
// if a retry is sent.
10731076
return true;
10741077
}
1075-
hd.token.base = nullptr;
1076-
hd.token.len = 0;
1078+
hd.token = nullptr;
1079+
hd.tokenlen = 0;
10771080
break;
10781081
}
10791082
default: {
@@ -1093,7 +1096,7 @@ void Endpoint::Receive(const uv_buf_t& buf,
10931096
// so we don't have to do this dance again for this endpoint
10941097
// instance.
10951098
addrLRU_.Upsert(remote_address)->validated = true;
1096-
} else if (hd.token.len > 0) {
1099+
} else if (hd.tokenlen > 0) {
10971100
// If validation is turned off and there is a token, that's weird.
10981101
// The peer should only have a token if we sent it to them and we
10991102
// wouldn't have sent it unless validation was turned on. Let's

src/quic/endpoint.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
3636
static constexpr auto QUIC_CC_ALGO_RENO = NGTCP2_CC_ALGO_RENO;
3737
static constexpr auto QUIC_CC_ALGO_CUBIC = NGTCP2_CC_ALGO_CUBIC;
3838
static constexpr auto QUIC_CC_ALGO_BBR = NGTCP2_CC_ALGO_BBR;
39-
static constexpr auto QUIC_CC_ALGO_BBR2 = NGTCP2_CC_ALGO_BBR2;
4039

4140
// Endpoint configuration options
4241
struct Options final : public MemoryRetainer {
@@ -131,9 +130,9 @@ class Endpoint final : public AsyncWrap, public Packet::Listener {
131130
#endif // DEBUG
132131

133132
// There are several common congestion control algorithms that ngtcp2 uses
134-
// to determine how it manages the flow control window: RENO, CUBIC, BBR,
135-
// and BBR2. The details of how each works is not relevant here. The choice
136-
// of which to use by default is arbitrary and we can choose whichever we'd
133+
// to determine how it manages the flow control window: RENO, CUBIC, and
134+
// BBR. The details of how each works is not relevant here. The choice of
135+
// which to use by default is arbitrary and we can choose whichever we'd
137136
// like. Additional performance profiling will be needed to determine which
138137
// is the better of the two for our needs.
139138
ngtcp2_cc_algo cc_algorithm = NGTCP2_CC_ALGO_CUBIC;

src/quic/preferredaddress.cc

+15-12
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@ std::optional<const PreferredAddress::AddressInfo> get_address_info(
2828
if (!paddr.ipv4_present) return std::nullopt;
2929
PreferredAddress::AddressInfo address;
3030
address.family = FAMILY;
31-
address.port = paddr.ipv4_port;
31+
address.port = paddr.ipv4.sin_port;
3232
if (uv_inet_ntop(
33-
FAMILY, paddr.ipv4_addr, address.host, sizeof(address.host)) == 0) {
33+
FAMILY, &paddr.ipv4.sin_addr, address.host, sizeof(address.host)) ==
34+
0) {
3435
address.address = address.host;
3536
}
3637
return address;
3738
} else {
3839
if (!paddr.ipv6_present) return std::nullopt;
3940
PreferredAddress::AddressInfo address;
4041
address.family = FAMILY;
41-
address.port = paddr.ipv6_port;
42-
if (uv_inet_ntop(
43-
FAMILY, paddr.ipv6_addr, address.host, sizeof(address.host)) == 0) {
42+
address.port = paddr.ipv6.sin6_port;
43+
if (uv_inet_ntop(FAMILY,
44+
&paddr.ipv6.sin6_addr,
45+
address.host,
46+
sizeof(address.host)) == 0) {
4447
address.address = address.host;
4548
}
4649
return address;
@@ -50,20 +53,20 @@ std::optional<const PreferredAddress::AddressInfo> get_address_info(
5053
template <int FAMILY>
5154
void copy_to_transport_params(ngtcp2_transport_params* params,
5255
const sockaddr* addr) {
53-
params->preferred_address_present = true;
56+
params->preferred_addr_present = true;
5457
if constexpr (FAMILY == AF_INET) {
5558
const sockaddr_in* src = reinterpret_cast<const sockaddr_in*>(addr);
56-
params->preferred_address.ipv4_port = SocketAddress::GetPort(addr);
57-
memcpy(params->preferred_address.ipv4_addr,
59+
params->preferred_addr.ipv4.sin_port = SocketAddress::GetPort(addr);
60+
memcpy(&params->preferred_addr.ipv4.sin_addr,
5861
&src->sin_addr,
59-
sizeof(params->preferred_address.ipv4_addr));
62+
sizeof(params->preferred_addr.ipv4.sin_addr));
6063
} else {
6164
DCHECK_EQ(FAMILY, AF_INET6);
6265
const sockaddr_in6* src = reinterpret_cast<const sockaddr_in6*>(addr);
63-
params->preferred_address.ipv6_port = SocketAddress::GetPort(addr);
64-
memcpy(params->preferred_address.ipv6_addr,
66+
params->preferred_addr.ipv6.sin6_port = SocketAddress::GetPort(addr);
67+
memcpy(&params->preferred_addr.ipv6.sin6_addr,
6568
&src->sin6_addr,
66-
sizeof(params->preferred_address.ipv4_addr));
69+
sizeof(params->preferred_addr.ipv4.sin_addr));
6770
}
6871
UNREACHABLE();
6972
}

0 commit comments

Comments
 (0)