-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
dns: destroy/reinitialize c-ares channel on ARES_ECONNREFUSED #9899
Merged
+125
−18
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,9 +55,9 @@ enum class RecordType { A, AAAA }; | |
class TestDnsServerQuery { | ||
public: | ||
TestDnsServerQuery(ConnectionPtr connection, const HostMap& hosts_a, const HostMap& hosts_aaaa, | ||
const CNameMap& cnames, const std::chrono::seconds& record_ttl) | ||
const CNameMap& cnames, const std::chrono::seconds& record_ttl, bool refused) | ||
: connection_(std::move(connection)), hosts_a_(hosts_a), hosts_aaaa_(hosts_aaaa), | ||
cnames_(cnames), record_ttl_(record_ttl) { | ||
cnames_(cnames), record_ttl_(record_ttl), refused_(refused) { | ||
connection_->addReadFilter(Network::ReadFilterSharedPtr{new ReadFilter(*this)}); | ||
} | ||
|
||
|
@@ -171,7 +171,11 @@ class TestDnsServerQuery { | |
memcpy(response_base, request, response_base_len); | ||
DNS_HEADER_SET_QR(response_base, 1); | ||
DNS_HEADER_SET_AA(response_base, 0); | ||
DNS_HEADER_SET_RCODE(response_base, answer_size > 0 ? NOERROR : NXDOMAIN); | ||
if (parent_.refused_) { | ||
DNS_HEADER_SET_RCODE(response_base, REFUSED); | ||
} else { | ||
DNS_HEADER_SET_RCODE(response_base, answer_size > 0 ? NOERROR : NXDOMAIN); | ||
} | ||
DNS_HEADER_SET_ANCOUNT(response_base, answer_size); | ||
DNS_HEADER_SET_NSCOUNT(response_base, 0); | ||
DNS_HEADER_SET_ARCOUNT(response_base, 0); | ||
|
@@ -253,6 +257,7 @@ class TestDnsServerQuery { | |
const HostMap& hosts_aaaa_; | ||
const CNameMap& cnames_; | ||
const std::chrono::seconds& record_ttl_; | ||
bool refused_{}; | ||
}; | ||
|
||
class TestDnsServer : public ListenerCallbacks { | ||
|
@@ -263,7 +268,7 @@ class TestDnsServer : public ListenerCallbacks { | |
Network::ConnectionPtr new_connection = dispatcher_.createServerConnection( | ||
std::move(socket), Network::Test::createRawBufferSocket()); | ||
TestDnsServerQuery* query = new TestDnsServerQuery(std::move(new_connection), hosts_a_, | ||
hosts_aaaa_, cnames_, record_ttl_); | ||
hosts_aaaa_, cnames_, record_ttl_, refused_); | ||
queries_.emplace_back(query); | ||
} | ||
|
||
|
@@ -280,6 +285,7 @@ class TestDnsServer : public ListenerCallbacks { | |
} | ||
|
||
void setRecordTtl(const std::chrono::seconds& ttl) { record_ttl_ = ttl; } | ||
void setRefused(bool refused) { refused_ = refused; } | ||
|
||
private: | ||
Event::Dispatcher& dispatcher_; | ||
|
@@ -288,6 +294,7 @@ class TestDnsServer : public ListenerCallbacks { | |
HostMap hosts_aaaa_; | ||
CNameMap cnames_; | ||
std::chrono::seconds record_ttl_; | ||
bool refused_{}; | ||
// All queries are tracked so we can do resource reclamation when the test is | ||
// over. | ||
std::vector<std::unique_ptr<TestDnsServerQuery>> queries_; | ||
|
@@ -300,6 +307,7 @@ class DnsResolverImplPeer { | |
DnsResolverImplPeer(DnsResolverImpl* resolver) : resolver_(resolver) {} | ||
|
||
ares_channel channel() const { return resolver_->channel_; } | ||
bool isChannelDirty() const { return resolver_->dirty_channel_; } | ||
const std::unordered_map<int, Event::FileEventPtr>& events() { return resolver_->events_; } | ||
// Reset the channel state for a DnsResolverImpl such that it will only use | ||
// TCP and optionally has a zero timeout (for validating timeout behavior). | ||
|
@@ -582,6 +590,64 @@ TEST_P(DnsImplTest, CallbackException) { | |
"unknown"); | ||
} | ||
|
||
// Validate that the c-ares channel is destroyed and re-initialized when c-ares returns | ||
// ARES_ECONNREFUSED as its callback status. | ||
TEST_P(DnsImplTest, DestroyChannelOnRefused) { | ||
ASSERT_FALSE(peer_->isChannelDirty()); | ||
server_->addHosts("some.good.domain", {"201.134.56.7"}, RecordType::A); | ||
server_->setRefused(true); | ||
|
||
std::list<Address::InstanceConstSharedPtr> address_list; | ||
EXPECT_NE(nullptr, resolver_->resolve("", DnsLookupFamily::V4Only, | ||
[&](std::list<DnsResponse>&& results) -> void { | ||
address_list = getAddressList(results); | ||
dispatcher_->exit(); | ||
})); | ||
|
||
dispatcher_->run(Event::Dispatcher::RunType::Block); | ||
// The c-ares channel should be dirty because the TestDnsServer replied with return code REFUSED; | ||
// This test, and the way the TestDnsServerQuery is setup, relies on the fact that Envoy's | ||
// c-ares channel is configured **without** the ARES_FLAG_NOCHECKRESP flag. This causes c-ares to | ||
// discard packets with REFUSED, and thus Envoy receives ARES_ECONNREFUSED due to the code here: | ||
// https://github.com/c-ares/c-ares/blob/d7e070e7283f822b1d2787903cce3615536c5610/ares_process.c#L654 | ||
// If that flag needs to be set, or c-ares changes its handling this test will need to be updated | ||
// to create another condition where c-ares invokes onAresGetAddrInfoCallback with status == | ||
// ARES_ECONNREFUSED. | ||
Comment on lines
+613
to
+615
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed this is a little fragile but I think it's OK for now. Nice work figuring out a way to test this! |
||
EXPECT_TRUE(peer_->isChannelDirty()); | ||
EXPECT_TRUE(address_list.empty()); | ||
|
||
server_->setRefused(false); | ||
|
||
// Resolve will destroy the original channel and create a new one. | ||
EXPECT_NE(nullptr, resolver_->resolve("some.good.domain", DnsLookupFamily::V4Only, | ||
[&](std::list<DnsResponse>&& results) -> void { | ||
address_list = getAddressList(results); | ||
dispatcher_->exit(); | ||
})); | ||
|
||
dispatcher_->run(Event::Dispatcher::RunType::Block); | ||
// However, the fresh channel initialized by production code does not point to the TestDnsServer. | ||
// This means that resolution will return ARES_ENOTFOUND. This should not dirty the channel. | ||
EXPECT_FALSE(peer_->isChannelDirty()); | ||
EXPECT_TRUE(address_list.empty()); | ||
|
||
// Reset the channel to point to the TestDnsServer, and make sure resolution is healthy. | ||
if (tcp_only()) { | ||
peer_->resetChannelTcpOnly(zero_timeout()); | ||
} | ||
ares_set_servers_ports_csv(peer_->channel(), socket_->localAddress()->asString().c_str()); | ||
|
||
EXPECT_NE(nullptr, resolver_->resolve("some.good.domain", DnsLookupFamily::Auto, | ||
[&](std::list<DnsResponse>&& results) -> void { | ||
address_list = getAddressList(results); | ||
dispatcher_->exit(); | ||
})); | ||
|
||
dispatcher_->run(Event::Dispatcher::RunType::Block); | ||
EXPECT_FALSE(peer_->isChannelDirty()); | ||
EXPECT_TRUE(hasAddress(address_list, "201.134.56.7")); | ||
} | ||
|
||
TEST_P(DnsImplTest, DnsIpAddressVersion) { | ||
std::list<Address::InstanceConstSharedPtr> address_list; | ||
server_->addHosts("some.good.domain", {"1.2.3.4"}, RecordType::A); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about this for a bit and debated between returning here, vs. allowing execution to continue and eventually call the callback_.
I ended up going for allowing the callback for a couple reasons:
wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I agree we need to call the callback. As you already pointed out this is related to the other issue, so let's just fix the callbacks to expose errors in a future change.