Skip to content

Provide a listener to hot restart for udp packet forwarding - #29137

Merged
ravenblackx merged 22 commits into
envoyproxy:mainfrom
ravenblackx:hot_restart_udp
Sep 11, 2023
Merged

ravenblackx merged 22 commits into
envoyproxy:mainfrom
ravenblackx:hot_restart_udp

Conversation

@ravenblackx

@ravenblackx ravenblackx commented Aug 18, 2023 •

Copy link
Copy Markdown
Contributor

Commit Message: Provide a listener to hot restart for udp packet forwarding
Additional Description: A part of #19454 - in order to be able to route udp packets delivered via a back channel, the hot restart child instance needs sufficient access to the quic listeners to be able to deliver those packets. The data passed to duplicateParentListenSocket is already sufficient to uniquely identify a socket; this adds a UdpListenerConfig reference such that the hot restart child class can build a hashmap from addresses to UdpListenerConfigs. Upon receiving a forwarded packet, it will thereby be able to call deliver on the appropriate UdpListenerConfig to get that packet delivered.
Risk Level: Low, essentially a no-op at this point.
Testing: Should be covered by existing tests.
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

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

phlax commented Aug 21, 2023

Copy link
Copy Markdown
Member

/retest to check arm failure

Signed-off-by: Raven Black <[email protected]>
Signed-off-by: Raven Black <[email protected]>
@RyanTheOptimist RyanTheOptimist self-assigned this Aug 23, 2023
}
const std::string addr = fmt::format("unix://{}", address->asString());
const int fd = server_.hotRestart().duplicateParentListenSocket(addr, worker_index);
const int fd = server_.hotRestart().duplicateParentListenSocket(addr, worker_index, listener);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This listener is the ListenerConfig object not the active TCP/QUIC listener. I'm wondering which field of this object do you want to build the hash map upon, i.e. name or tag? Could you pass in that instead?

And please keep in mind that the address configured can be Any. When you do the mapping, this need special handling.

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.

Oh, this is the "no dynamic cast" problem again and I think I misread the chain of inheritance because of two completely different types of things being called listeners, and the class hierarchy in this area being a real spaghetti. :(

I definitely can't pass the keys in instead because the values of the map have to be the objects on which onData can be called. So I need, I guess, the UdpListenerCallbacks, as the base-level type that will do what's needed. (The key would be a tuple of the addr and worker index, which are already the arguments to duplicateParentListenSocket.)

Good point about the Any address, I'll add that to the notes.

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.

Turns out no I didn't misread the chain of things, I was thinking of dynamic_casting to UdpListenerConfig, which has listenerWorkerRouter() on it, which would allow us to deliver packets to the appropriate worker via deliver(). So my mistake was that dynamic_casting isn't an option so I'd need to instead make it an OptRef<UdpListenerConfig> that gets passed through all these layers.

Alternative option is to capture either a UdpListenerConfig or an ActiveUdpListener somewhere else in the process and pass it in via a different function, which makes less of a mess (see #29249 for example); but then the question is do we have a preference between:

  1. map[addr] -> udpListenerConfig.listenerWorkerRouter(addr).deliver(worker_id, packet)
    or
  2. map[addr+worker_index] -> ActiveUdpListener.post(packet)

Option 1 transforms addresses to strings in listenerWorkerRouter, and appears to not have a special case for 0.0.0.0 addresses, so maybe option 2 is necessarily the way.

@danzh2010 danzh2010 Aug 29, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think option 1 looks cleaner, given that you don't need to call registerUdpForwardingListener() in worker threads, but on main thread. But I'm still not very clear about the thread model in hot restart. Ideally we can build a map from listening address to listener config in main thread at start up, and use it against forwarded packets' destination address during hot restart by the hot restart thread (may be it's main thread?).

Either way you need special handling for 0.0.0.0 in the map and probably in listenerWorkerRouter() because it is originally passed in with configured listening address instead of packet destination 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.

Well option 1 turns out to be much cleaner now that I'm passing it from a bit further up the stack. The call is made from the main thread (a few calls up it's in a TRY_ASSERT_MAIN_THREAD block), which I think is also the thread hot restart mostly runs on. I assume tsan will yell at me at some point in this process if that's incorrect.

Included a comment about 0.0.0.0, implementation for it will be later, and I think can stay out of listenerWorkerRouter (I just have to be sure to pass 0.0.0.0 into listenerWorkerRouter if that's the listener the hot restart function chooses.)

@RyanTheOptimist

Copy link
Copy Markdown
Contributor

/wait

Comment thread envoy/server/hot_restart.h Outdated
virtual int duplicateParentListenSocket(const std::string& address, uint32_t worker_index) PURE;

/**
* Registers an ActiveUdpListener as a possible receiver of udp packets forwarded from the parent

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

s/ActiveUdpListener/UdpListenerCallbacks

listener_config, quic_config, kernel_worker_routing, enabled, quic_stat_names,
packets_to_read_to_connection_count_ratio, crypto_server_stream_factory, proof_source_factory,
std::move(cid_generator), worker_selector_);
server_.hotRestart().registerUdpForwardingListener(std::move(addr), worker_index, *listener);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is server_ shared across the workers? (I'm not very clear about which part is done in the main thread and which part is done on worker threads.) Should we post the registration to the main thread here?

@ravenblackx ravenblackx Aug 28, 2023 •

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.

I'm going to stack this onto #29306 which will make hotRestart() part of quic_context_ rather than adding server_ for access to it, but yes, either way it's a reference to a common object.

It looks like these operations are indeed on a worker thread, which I didn't realize - how is posting to the main thread done? Does hot restart even run on the main thread? I think it would be okay to make any thread-resolving part of the work of registerUdpForwardingListener rather than doing it here, anyway. (Thinking if posting isn't easy, mutex-guarding a map would work for simplicity, since after the registration is complete there'll be no contention so it shouldn't be a significant performance burden.)

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.

(Holding off on the stacking until the other one lands because doing it now would probably be disruptive to review.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't know if hot restart runs on main thread or other stand-alone thread, but definitely not on worker threads.

Dispatcher has an interface post() to post callback function to the thread the Dispatcher object runs event loop on, which is thread safe.

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.

But the only dispatcher we have access to here is the worker thread's dispatcher, isn't it? So it wouldn't be useful for getting something onto the hot restarter's thread.

@ravenblackx ravenblackx mentioned this pull request Aug 29, 2023
6 tasks done
Comment thread source/server/hot_restarting_child.cc Outdated
const Network::Address::Instance& /* address */,
Network::UdpListenerConfig& /* listener_config */) {
ASSERT_IS_MAIN_OR_TEST_THREAD();
// TODO(ravenblack): Actually register it in the next PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thoughts on implementing it in the current PR, so that we don't need to guess how it would look like or questioning if this is gonna work during code review? At this point the PR doesn't look too big anyway.

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.

If this is a tacit approval of the thing so far then I'm okay with extending it in-place - just trying to avoid cascading hideous refactors by going one step at a time (which has already been a good idea three times in this PR!)

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.

To some extent I'd rather not though, because I have work in progress on one of the adjoining pieces, which already declares a structure into which this registration will occur. I'm trying to leave the joins until last so the work can be parallelized.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I had a hard time to review this PR because none of us can confidently tell if the current implementation would work. So I'd prefer to as least have a proof of concept and then looking into how to cover the corner cases and cut it into smaller changes for review. For this PR to make sense, I think we would like to see at least the child process ends up with a map of listening address to the corresponding listener config's worker router during hot restart. It can be extending this PR in place, or implementing this method in a stand-alone PR before hooking it in this PR.

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 that, and the retrieval-per-address part with handling for 'any' addresses since I know you're especially interested in reviewing that. :)
And some testing.

@RyanTheOptimist

Copy link
Copy Markdown
Contributor

/wait

@danzh2010 danzh2010 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks sensible!

Comment thread source/server/hot_restarting_child.cc Resolved
Comment thread source/server/hot_restarting_child.cc Resolved
Comment thread source/server/hot_restarting_child.h Resolved
Comment thread source/server/hot_restart_impl.h Resolved
Comment thread test/server/hot_restart_impl_test.cc Resolved
Comment thread source/server/hot_restarting_child.h Resolved
@RyanTheOptimist

Copy link
Copy Markdown
Contributor

/wait

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

Copy link
Copy Markdown
Contributor

this adds a listener reference such that the hot restart child class can build a hashmap from addresses to listeners. Upon receiving a forwarded packet, it will thereby be able to call onData on the appropriate listener to get that packet delivered.

Is this outdated?

@ravenblackx

Copy link
Copy Markdown
Contributor Author

this adds a listener reference such that the hot restart child class can build a hashmap from addresses to listeners. Upon receiving a forwarded packet, it will thereby be able to call onData on the appropriate listener to get that packet delivered.

Is this outdated?

Updated.

Comment thread source/server/hot_restarting_child.h Outdated
// to support QUIC packet forwarding.
class UdpForwardingContext {
public:
using ForwardEntry = std::pair<const Network::Address::Instance*, Network::UdpListenerConfig*>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's scary to store address pointer here. Do you know its life time? I'm less concerned about UdpListenerConfig lifetime.

Or is it needed given it's the listener address in UdpListenerConfig?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, UdpListenerConfig knows nothing about the listening socket. Could you clone the socket?

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 needed because the call later is UdpListenerConfig::listenerWorkerRouter(addr).deliver(...) - the UdpListenerConfig instance contains multiple addresses.

}
udp_listener_config_->listener_worker_routers_.emplace(
address.asString(), std::make_unique<Network::UdpListenerWorkerRouterImpl>(concurrency));
parent_.server_.hotRestart().registerUdpForwardingListener(address, *udp_listener_config_);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do think it make sense to call registerUdpForwardingListener() outside of this method where you have access to the address shared_ptr?

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.

I don't think it's necessary/useful since we have no choice but to index the map by string anyway (like the line above also does!)
(Rather, there is actually a choice, but if we were to index with an address and a custom hash function then the same treatment could be given to listener_worker_routers_ and it would turn into a big refactor that I'd very much rather not do at this time.)

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.

Ah, I see, you were thinking it could be a shared_ptr in the ForwardEntry rather than a bare pointer. Will take a moment to look into it.

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.

Signed-off-by: Raven Black <[email protected]>
class UdpForwardingContext {
public:
using ForwardEntry = std::pair<const Network::Address::Instance*, Network::UdpListenerConfig*>;
using ForwardEntry = std::pair<Network::Address::InstanceConstSharedPtr,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks safer now. Thanks!

Do you think we can key the map on Network::Address::InstanceConstSharedPtr by providing a customized Hash?

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.

I would prefer not to at this time since listener_worker_routers_ could also have benefited from doing that, and didn't do it - I'd tend to assume there's a reason for that.

It does look like Address::Instance has an equality operator that would mostly do the right thing, but given earlier conversations about dynamic_cast on the previous PR, there might be some issue around dynamic_cast e.g. here where if true dynamic_cast isn't available then this would provoke an error if you were to look up an ipv4 address in a hash_map where it shares a hash with an ipv6 addresses.

Maybe worth filing an issue to update both things to key on an InstanceConstSharedPtr though.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would prefer not to at this time since listener_worker_routers_ could also have benefited from doing that, and didn't do it - I'd tend to assume there's a reason for that.

It does look like Address::Instance has an equality operator that would mostly do the right thing, but given earlier conversations about dynamic_cast on the previous PR, there might be some issue around dynamic_cast e.g. here where if true dynamic_cast isn't available then this would provoke an error if you were to look up an ipv4 address in a hash_map where it shares a hash with an ipv6 addresses.

This depends on how you implement Hash. But filing a issue for both and handle it when needed sounds good.

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.

Also would be awkward if you keyed on shared pointers, when you want to find() an address and what you have is a reference or local instance. I don't think there's a way to use alternative hash and equality operators for different candidate types.

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.

#29529 for a tracking issue.

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

@danzh2010 danzh2010 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, @RyanTheOptimist over to you.

@RyanTheOptimist RyanTheOptimist left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great, modulo one nit.

Comment thread source/server/hot_restart_impl.h Outdated
SharedMemory* shmem_;
ProcessSharedMutex log_lock_;
ProcessSharedMutex access_log_lock_;
friend class HotRestartUdpForwardingTestHelper;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Is it envoy policy to obey all google style rules?
I would expect this one would be enforced by a linter if we really want it enforced, but okay, done anyway.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, that's the policy:

Beyond code formatting, for the most part Envoy uses the Google C++ style guidelines. The following section covers the major areas where we deviate from the Google guidelines.

https://github.com/envoyproxy/envoy/blob/main/STYLE.md

The Style Guide go well beyond things which can be enforces with the tooling we have, annoyingly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you push a new commit? I'm not seeing a change, fwiw.

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.

Sorry about that, I had done the relevant git push but replied too quickly, before the git hooks had finished!

std::shared_ptr<Network::UdpListenerConfig> listener_config) {
const bool inserted =
listener_map_.try_emplace(address->asString(), ForwardEntry{address, listener_config}).second;
ASSERT(inserted, "Two udp listeners on the same address shouldn't be possible");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, what enforces the invariant that there only is a single listener per interface?

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.

Per interface and port. I'm not sure if envoy enforces that you can't, and SO_REUSEPORT means the operating system won't enforce that you can't, but I hope envoy enforces that you can't because if you do have multiple listeners with different behaviors on the same interface and port you'd get horrific mixed delivery. (And if you have multiple listeners on the same interface+port with identical behavior then why?!)

Signed-off-by: Raven Black <[email protected]>
@ravenblackx
ravenblackx merged commit bd7225e into envoyproxy:main Sep 11, 2023
@ravenblackx
ravenblackx deleted the hot_restart_udp branch September 12, 2023 16:10
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.

4 participants