Skip to content

encapsulate and forward udp packets from parent instance to child - #29585

Merged
ggreenway merged 11 commits into
envoyproxy:mainfrom
ravenblackx:hot_restart_2
Oct 10, 2023
Merged

ggreenway merged 11 commits into
envoyproxy:mainfrom
ravenblackx:hot_restart_2

Conversation

@ravenblackx

Copy link
Copy Markdown
Contributor

Commit Message: encapsulate and forward udp packets from parent instance to child
Additional Description: During hot restart, forward UDP packets as delivered to HotRestartingParent (from ActiveQuicListeners) to HotRestartingChild, for delivery to the appropriate listener in the new instance.
Risk Level: Some risk as hot restart behavior is changed by this.
Testing: Unit tests validate the packet delivery.
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

@repokitteh-read-only

Copy link
Copy Markdown

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #29585 was opened by ravenblackx.

see: more, trace.

Signed-off-by: Raven Black <[email protected]>
@ravenblackx
ravenblackx marked this pull request as ready for review September 15, 2023 18:40

@ggreenway ggreenway left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

During hot restart (while both parent and child are doing useful work), are the UDP listeners open/accepting on both parent and child? Or is it only on one of them? If they're open on both, don't the packets need to potentially be sent in both directions (parent->child and child->parent)?

/wait

Comment thread source/server/hot_restart.proto Outdated
string local_addr = 1;
string peer_addr = 2;
uint64 receive_time_epoch_microseconds = 3;
bytes packet = 4;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
bytes packet = 4;
bytes data = 4;

or maybe payload

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Made it payload, and elsewhere where a thing was a packet and I locally-named it data, it's now named packet.

string peer_addr = 2;
uint64 receive_time_epoch_microseconds = 3;
bytes packet = 4;
uint32 worker_index = 5;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this the source or destination worker_index? What happens if source and dest have different numbers of workers? (Is that even supported in hot-restart? I can't remember)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a giant comment in the code (but not in the proto) about why it's like this.

Comment thread source/server/hot_restart_impl.cc Resolved
static Stats::Gauge& hotRestartGeneration(Stats::Scope& scope);

RpcStream main_rpc_stream_;
RpcStream udp_forwarding_rpc_stream_;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this a separate stream? Is there any risk of unexpected behavior/bugs due to undefined ordering between the two stream?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's a separate stream because udp packets can be forwarded at any time after the drain begins, and main_rpc_stream_ communication is still potentially happening at that point. Single threading of the HotRestarter should be enough to prevent individual messages from comingling, but there's nothing stopping parent instance from sending a UDP packet while child instance is sending a request for which it will expect to read a response. A distinct channel prevents any conflicts of that nature.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Makes sense. Can you add a comment to that effect somewhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Commented both the RpcStreams.

Comment thread source/server/hot_restarting_child.cc Resolved
Comment thread source/server/hot_restarting_child.cc Outdated
case HotRestartMessage::Request::kForwardedUdpPacket: {
const auto& req = wrapped_request->request().forwarded_udp_packet();
Network::UdpRecvData data;
data.addresses_.local_ = Network::Utility::parseInternetAddressAndPort(req.local_addr());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What if the listener was on a unix-domain socket (and so the address isn't an internet address)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Can there be quic packets on unix domain sockets?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe? Not sure. You can have a datagram UDS on unix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you have a suggestion, is there a better helper function I could call that would parse out all the kinds of address?

@ravenblackx

Copy link
Copy Markdown
Contributor Author

During hot restart (while both parent and child are doing useful work), are the UDP listeners open/accepting on both parent and child? Or is it only on one of them? If they're open on both, don't the packets need to potentially be sent in both directions (parent->child and child->parent)?

At the moment they are both listening and accepting packets; the next change in the series is to pause the child instance from accepting packets (via UDP) until the parent instance stops.

I don't think that has to be done all in one, because this intermediate state is already an improvement over the current state (where packets are delivered to both instances randomly-ish) - now the same problem still happens, but for half the wrong packets it should auto-recover.

Signed-off-by: Raven Black <[email protected]>
@ggreenway

Copy link
Copy Markdown
Member

the next change in the series is to pause the child instance from accepting packets (via UDP) until the parent instance stops.

Shouldn't it go the other direction: child accepts packets and parent stops? That way, new connections land on the child, not the parent, which is also what happens for tcp connections: all new connections go to the child.

@ravenblackx

Copy link
Copy Markdown
Contributor Author

the next change in the series is to pause the child instance from accepting packets (via UDP) until the parent instance stops.

Shouldn't it go the other direction: child accepts packets and parent stops? That way, new connections land on the child, not the parent, which is also what happens for tcp connections: all new connections go to the child.

If you do it that way around you can't as easily tell which packets should be forwarded to the parent instance - the quiche interface gives you a convenient hook to intercept "packets that can't be routed when you're not listening for new connections" so you can intercept them in the parent, but the child is listening for new connections so you can't do the same there. Overall it makes ~no difference which way you do it, since for the duration of the draining, towards one end most of your packets will be wrong and towards the other end few of your packets will be wrong.

Also, more importantly, it's already 60% implemented and merged this way around now. :)

@ggreenway

Copy link
Copy Markdown
Member

The biggest advantage of having the client be the acceptor is that when it's time to terminate the parent, nothing needs to change; the child is already doing that job. With the opposite direction, how do you do the switch without losing anything in the process?

@ravenblackx

Copy link
Copy Markdown
Contributor Author

The biggest advantage of having the client be the acceptor is that when it's time to terminate the parent, nothing needs to change; the child is already doing that job. With the opposite direction, how do you do the switch without losing anything in the process?

The child is already listening at that point, just not consuming, so there's no packet rejection just buffering if the child starts listening after the termination. And, the child is the one to call for the termination, and at that moment the parent is about to abandon any connections anyway, so there's no surprising packet rejection if the child starts consuming packets immediately before the termination either. (I would be inclined to go with the latter to avoid the brief "hiccup" of no active reading.)

@ggreenway

Copy link
Copy Markdown
Member

The child is already listening at that point, just not consuming

Help me refresh my memory: I believe we require SO_REUSEPORT to be enabled when doing UDP, and both the parent and the child use the same underlying file descriptor (for each worker). So in the kernel, both the parent and child are using the same queue of incoming packets. Is that all correct?

@ravenblackx

Copy link
Copy Markdown
Contributor Author

The child is already listening at that point, just not consuming

Help me refresh my memory: I believe we require SO_REUSEPORT to be enabled when doing UDP, and both the parent and the child use the same underlying file descriptor (for each worker). So in the kernel, both the parent and child are using the same queue of incoming packets. Is that all correct?

That is my understanding, yes.

@ravenblackx
ravenblackx requested a review from ggreenway October 2, 2023 16:03

@ggreenway ggreenway left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/wait

Comment thread source/server/hot_restarting_child.cc Outdated
while ((wrapped_request =
udp_forwarding_rpc_stream_.receiveHotRestartMessage(RpcStream::Blocking::No))) {
if (wrapped_request->requestreply_case() == HotRestartMessage::kReply) {
ENVOY_LOG(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use ENVOY_LOG_PERIODIC or similar; if this prints, it could blow up the process with logging because it will probably happen A LOT if it happens at all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Stats stats = 3;
DrainListeners drain_listeners = 4;
Terminate terminate = 5;
ForwardedUdpPacket forwarded_udp_packet = 6;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given that these are sent on a separate UDS, I don't think this should be part of this oneof. I think it can be a new separate message. And maybe in it's own oneof (with only 1 entry for now) to allow future expansion. WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Conceptually agree, but the whole existing mini-RPC framework relies on this being the message type, so using a different type would make for a bunch of duplicate code or significant changes to stuff I'm trying not to impact.

Comment thread source/server/hot_restarting_child.cc Outdated
const auto& req = wrapped_request->request().forwarded_udp_packet();
Network::UdpRecvData packet;
packet.addresses_.local_ =
Network::Utility::parseInternetAddressAndPortNoThrow(req.local_addr());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think if you use Utility::resolveUrl() (and format them that way on the sending side) that unix-domain sockets will work, with no additional work needed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, with adding a new helper function for the inverse of resolveUrl, and a test for that helper.

@ravenblackx

Copy link
Copy Markdown
Contributor Author

/retest

@ravenblackx
ravenblackx requested a review from ggreenway October 4, 2023 17:24

@ggreenway ggreenway left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Apologies for the delay.

@ggreenway
ggreenway merged commit be820c7 into envoyproxy:main Oct 10, 2023
@ravenblackx
ravenblackx deleted the hot_restart_2 branch October 10, 2023 20:32
RyanTheOptimist pushed a commit that referenced this pull request Oct 13, 2026
…estart parent drains (#48206)

Commit Message: quic: leave time-wait connections to the time-wait list
while a hot restart parent drains
Additional Description:

While a hot restart parent drains, its QUIC dispatcher stops accepting
new connections and `EnvoyQuicDispatcher::OnFailedToDispatchPacket`
reports every packet without a live session as not dispatched, so
`ActiveQuicListener` forwards it to the new process (#29585). That hook
runs before quiche looks at its time-wait list, so "no live session"
includes the connections this dispatcher has just closed itself, which
is most of them during a drain. A late packet for one of those (a
retransmission, or data sent before the peer saw the close) is forwarded
to the child, which never knew the connection and answers with a
stateless reset. Without the forwarding the parent's time-wait list
would have re-sent the stored `CONNECTION_CLOSE`, which tells the peer
why the connection ended.

Fix: while not accepting, a packet whose destination connection ID is in
the time-wait list is left to quiche (the hook returns false and the
dispatcher goes on to the time-wait list as usual). Only packets for
connections the dispatcher does not know at all are forwarded. The
change is in the parent, so it applies from the first hot restart in
which the draining process has it.

Runtime guard:
`envoy.reloadable_features.quic_hot_restart_time_wait_before_forward`
(default true).

Testing:
`EnvoyQuicDispatcherTest.ProcessPacketHandlesTimeWaitConnectionLocallyWhenNotAccepting`
creates a session, closes it with a `CONNECTION_CLOSE` so its ID is in
the time-wait list, stops accepting, and checks that a later packet for
that ID is handled by the dispatcher while a packet for an unknown ID is
still reported for forwarding.
`ProcessPacketForwardsTimeWaitConnectionWhenGuardDisabled` checks the
previous behavior with the guard off. `active_quic_listener_test`,
`quic_http_integration_test` and `hotrestart_test` pass.

Observed on a deployment running HTTP/3 under load across hot restarts,
as stateless resets around each restart. It was deployed there together
with #47868, which fixes the larger source of those resets (the paused
child listener reading the shared socket); this change has not been
measured in isolation from that one, so its own share rests on the
mechanism above and the unit tests.

Risk Level: low (QUIC listeners of a draining hot restart parent only;
runtime guarded)
Testing: unit tests (new), QUIC listener and integration tests, hot
restart integration test
Docs Changes: N/A
Release Notes: bug_fixes
Platform Specific Features: N/A
Runtime guard:
envoy.reloadable_features.quic_hot_restart_time_wait_before_forward

Signed-off-by: Bruno Palermo <[email protected]>
mattklein123 pushed a commit that referenced this pull request Oct 13, 2026
…47868)

Commit Message: udp: a paused hot restart listener must not read the
parent's socket
Additional Description:

A UDP listener created from a socket inherited during a hot restart
stays paused until the parent is gone (`UdpListenerImpl`, `paused()`):
its read events are not enabled, the parent keeps serving the socket,
and the packets that belong to the child arrive over the hot restart
forwarding channel (#29585). That single-reader rule is what makes
sharing the socket safe: a packet of a parent-owned QUIC connection
dequeued by the child is an unknown connection to the child's dispatcher
and gets a stateless reset.

The read handler did not know about the pause. When the parent forwards
a new connection's first packet, `ActiveQuicListener` buffers the
handshake and calls `activateRead()` on its listener to process buffered
handshakes on the next loop iteration. An injected activation is
delivered regardless of the enabled events
(`FileEventImpl::mergeInjectedEventsAndRunCb`), and
`UdpListenerImpl::handleReadCallback` then ran `onReadReady()`, which
was the intent, and **read the socket**, which is the parent's. Under
load this shows up as a burst of stateless resets on client connections
to the parent right after the parent starts draining: each reset makes
the client reconnect, the new handshake is forwarded, the read fires
again, until every client has moved to the child. It needs new
connections during the drain window (a forwarded handshake is the
trigger), so it is invisible without traffic.

Fix: while paused, an injected read runs the read-ready callback and
returns without reading the socket or re-arming the read. Unpausing is
unchanged.

Testing:
`QuicHotRestartIntegrationTest.PausedListenerLeavesTheParentsPacketsOnTheSocket`
(new, `test/integration/quic_hot_restart_integration_test.cc`) runs a
real server whose QUIC listener gets its UDP socket "inherited" from a
parent the test plays: the test binds the socket, hands the server a
duplicate through `duplicateParentListenSocket`, keeps the listener
paused through the parent-drained registrar, and forwards packets
through the listener's worker router as a parent does. With a packet of
a parent-owned connection waiting on the shared socket, it forwards two
new connections and waits for the server to answer both, then checks
that the parent's packet is still on the socket and that its sender got
no stateless reset; once the parent is drained, the same kind of packet
reaches the server and is reset. Without the fix the listener dequeues
the parent's packet (fails in both IP versions). To let a test supply
the hot restart implementation, `IntegrationTestServer::create` takes an
optional `Server::HotRestart*` (set from
`BaseIntegrationTest::hot_restart_`; default unchanged).
`HotRestartedUdpListenerImplTest.InjectedReadWhilePausedDoesNotReadTheSocket`
covers the listener in isolation: the read-ready callback runs, the
socket is not read and the read is not re-armed, and reading starts once
the parent-drained callback fires. Existing pause tests,
`active_quic_listener_test`, `quic_http_integration_test` and
`hotrestart_test` pass. Verified on a deployment that reproduced the
resets under HTTP/3 load across hot restarts, with socket inheritance
confirmed correct beforehand.

Risk Level: low (hot restart child, paused UDP listeners only)
Testing: integration test (new), unit test (new), QUIC listener and
integration tests, hot restart integration test, real deployment as
described
Docs Changes: N/A
Release Notes: bug_fixes
Platform Specific Features: N/A

---------

Signed-off-by: Bruno Palermo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants