Skip to content

xds: avoid unnecessary dns lookup #11932

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

Merged
merged 5 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions xds/src/main/java/io/grpc/xds/EnvoyServerProtoData.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.grpc.xds.client.EnvoyProtoData;
import io.grpc.xds.internal.security.SslContextProviderSupplier;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Objects;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -150,9 +149,9 @@ abstract static class CidrRange {

abstract int prefixLen();

static CidrRange create(String addressPrefix, int prefixLen) throws UnknownHostException {
static CidrRange create(InetAddress addressPrefix, int prefixLen) {
Copy link
Member

Choose a reason for hiding this comment

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

Why did you change the signature? String addressPrefix constructor seems pretty useful, given that we get a string in a proto.

return new AutoValue_EnvoyServerProtoData_CidrRange(
InetAddress.getByName(addressPrefix), prefixLen);
addressPrefix, prefixLen);
}
}

Expand Down
14 changes: 8 additions & 6 deletions xds/src/main/java/io/grpc/xds/XdsListenerResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.net.InetAddresses;
import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
Expand All @@ -43,7 +44,6 @@
import io.grpc.xds.Filter.FilterConfig;
import io.grpc.xds.XdsListenerResource.LdsUpdate;
import io.grpc.xds.client.XdsResourceType;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -446,16 +446,18 @@
try {
for (io.envoyproxy.envoy.config.core.v3.CidrRange range : proto.getPrefixRangesList()) {
prefixRanges.add(
CidrRange.create(range.getAddressPrefix(), range.getPrefixLen().getValue()));
CidrRange.create(InetAddresses.forString(range.getAddressPrefix()),
range.getPrefixLen().getValue()));
}
for (io.envoyproxy.envoy.config.core.v3.CidrRange range
: proto.getSourcePrefixRangesList()) {
sourcePrefixRanges.add(
CidrRange.create(range.getAddressPrefix(), range.getPrefixLen().getValue()));
sourcePrefixRanges.add(CidrRange.create(
InetAddresses.forString(range.getAddressPrefix()), range.getPrefixLen().getValue()));

Check warning on line 455 in xds/src/main/java/io/grpc/xds/XdsListenerResource.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/XdsListenerResource.java#L454-L455

Added lines #L454 - L455 were not covered by tests
}
} catch (UnknownHostException e) {
throw new ResourceInvalidException("Failed to create CidrRange", e);
} catch (IllegalArgumentException ex) {
throw new ResourceInvalidException("Failed to create CidrRange", ex);

Check warning on line 458 in xds/src/main/java/io/grpc/xds/XdsListenerResource.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/XdsListenerResource.java#L457-L458

Added lines #L457 - L458 were not covered by tests
}

ConnectionSourceType sourceType;
switch (proto.getSourceType()) {
case ANY:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.InetAddresses;
import com.google.common.util.concurrent.SettableFuture;
import io.grpc.ServerInterceptor;
import io.grpc.internal.TestUtils.NoopChannelLogger;
Expand Down Expand Up @@ -58,7 +59,6 @@
import io.netty.handler.codec.http2.Http2Settings;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -318,7 +318,8 @@ public void destPrefixRangeMatch() throws Exception {
EnvoyServerProtoData.FilterChainMatch filterChainMatchWithMatch =
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.1.2.0", 24)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.2.0"), 24)),
ImmutableList.of(),
ImmutableList.of(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Expand Down Expand Up @@ -360,7 +361,8 @@ public void destPrefixRangeMismatch_returnDefaultFilterChain()
EnvoyServerProtoData.FilterChainMatch filterChainMatchWithMismatch =
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.2.2.0", 24)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.2.2.0"), 24)),
ImmutableList.of(),
ImmutableList.of(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Expand Down Expand Up @@ -403,7 +405,8 @@ public void dest0LengthPrefixRange()
EnvoyServerProtoData.FilterChainMatch filterChainMatch0Length =
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.2.2.0", 0)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.2.2.0"), 0)),
ImmutableList.of(),
ImmutableList.of(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Expand Down Expand Up @@ -444,7 +447,8 @@ public void destPrefixRange_moreSpecificWins()
EnvoyServerProtoData.FilterChainMatch filterChainMatchLessSpecific =
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.1.2.0", 24)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.2.0"), 24)),
ImmutableList.of(),
ImmutableList.of(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Expand All @@ -461,7 +465,8 @@ public void destPrefixRange_moreSpecificWins()
EnvoyServerProtoData.FilterChainMatch filterChainMatchMoreSpecific =
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.1.2.2", 31)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.2.2"), 31)),
ImmutableList.of(),
ImmutableList.of(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Expand Down Expand Up @@ -519,7 +524,8 @@ public void destPrefixRange_emptyListLessSpecific()
EnvoyServerProtoData.FilterChainMatch filterChainMatchMoreSpecific =
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("8.0.0.0", 5)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("8.0.0.0"), 5)),
ImmutableList.of(),
ImmutableList.of(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Expand Down Expand Up @@ -559,7 +565,8 @@ public void destPrefixRangeIpv6_moreSpecificWins()
EnvoyServerProtoData.FilterChainMatch filterChainMatchLessSpecific =
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("FE80:0:0:0:0:0:0:0", 60)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("FE80:0:0:0:0:0:0:0"), 60)),
ImmutableList.of(),
ImmutableList.of(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Expand All @@ -577,7 +584,8 @@ public void destPrefixRangeIpv6_moreSpecificWins()
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(
EnvoyServerProtoData.CidrRange.create("FE80:0000:0000:0000:0202:0:0:0", 80)),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("FE80:0000:0000:0000:0202:0:0:0"), 80)),
ImmutableList.of(),
ImmutableList.of(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Expand Down Expand Up @@ -620,8 +628,10 @@ public void destPrefixRange_moreSpecificWith2Wins()
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(
EnvoyServerProtoData.CidrRange.create("10.1.2.0", 24),
EnvoyServerProtoData.CidrRange.create(LOCAL_IP, 32)),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.2.0"), 24),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString(LOCAL_IP), 32)),
ImmutableList.of(),
ImmutableList.of(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Expand All @@ -638,7 +648,8 @@ public void destPrefixRange_moreSpecificWith2Wins()
EnvoyServerProtoData.FilterChainMatch filterChainMatchLessSpecific =
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.1.2.2", 31)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.2.2"), 31)),
ImmutableList.of(),
ImmutableList.of(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Expand Down Expand Up @@ -763,8 +774,10 @@ public void sourcePrefixRange_moreSpecificWith2Wins()
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(
EnvoyServerProtoData.CidrRange.create("10.4.2.0", 24),
EnvoyServerProtoData.CidrRange.create(REMOTE_IP, 32)),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.4.2.0"), 24),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString(REMOTE_IP), 32)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
ImmutableList.of(),
ImmutableList.of(),
Expand All @@ -781,7 +794,8 @@ public void sourcePrefixRange_moreSpecificWith2Wins()
0,
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.4.2.2", 31)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.4.2.2"), 31)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
ImmutableList.of(),
ImmutableList.of(),
Expand Down Expand Up @@ -811,8 +825,7 @@ filterChainLessSpecific, randomConfig("no-match")),
}

@Test
public void sourcePrefixRange_2Matchers_expectException()
throws UnknownHostException {
public void sourcePrefixRange_2Matchers_expectException() {
ChannelHandler next = new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
Expand All @@ -831,8 +844,10 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(
EnvoyServerProtoData.CidrRange.create("10.4.2.0", 24),
EnvoyServerProtoData.CidrRange.create("192.168.10.2", 32)),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.4.2.0"), 24),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("192.168.10.2"), 32)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
ImmutableList.of(),
ImmutableList.of(),
Expand All @@ -848,7 +863,8 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
0,
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.4.2.0", 24)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.4.2.0"), 24)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
ImmutableList.of(),
ImmutableList.of(),
Expand Down Expand Up @@ -890,8 +906,10 @@ public void sourcePortMatch_exactMatchWinsOverEmptyList() throws Exception {
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(
EnvoyServerProtoData.CidrRange.create("10.4.2.0", 24),
EnvoyServerProtoData.CidrRange.create("10.4.2.2", 31)),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.4.2.0"), 24),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.4.2.2"), 31)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
ImmutableList.of(),
ImmutableList.of(),
Expand All @@ -908,7 +926,8 @@ public void sourcePortMatch_exactMatchWinsOverEmptyList() throws Exception {
0,
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.4.2.2", 31)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.4.2.2"), 31)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
ImmutableList.of(7000, 15000),
ImmutableList.of(),
Expand Down Expand Up @@ -966,7 +985,8 @@ public void filterChain_5stepMatch() throws Exception {
PORT,
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(REMOTE_IP, 32)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString(REMOTE_IP), 32)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
ImmutableList.of(),
ImmutableList.of(),
Expand All @@ -981,9 +1001,11 @@ public void filterChain_5stepMatch() throws Exception {
EnvoyServerProtoData.FilterChainMatch filterChainMatch2 =
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.1.2.0", 30)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.2.0"), 30)),
ImmutableList.of(),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.4.0.0", 16)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.4.0.0"), 16)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
ImmutableList.of(),
ImmutableList.of(),
Expand All @@ -997,8 +1019,10 @@ public void filterChain_5stepMatch() throws Exception {
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(
EnvoyServerProtoData.CidrRange.create("192.168.2.0", 24),
EnvoyServerProtoData.CidrRange.create("10.1.2.0", 30)),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("192.168.2.0"), 24),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.2.0"), 30)),
ImmutableList.of(),
ImmutableList.of(),
EnvoyServerProtoData.ConnectionSourceType.SAME_IP_OR_LOOPBACK,
Expand All @@ -1015,10 +1039,13 @@ public void filterChain_5stepMatch() throws Exception {
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(
EnvoyServerProtoData.CidrRange.create("10.1.0.0", 16),
EnvoyServerProtoData.CidrRange.create("10.1.2.0", 30)),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.0.0"), 16),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.2.0"), 30)),
ImmutableList.of(),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.4.2.0", 24)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.4.2.0"), 24)),
EnvoyServerProtoData.ConnectionSourceType.EXTERNAL,
ImmutableList.of(16000, 9000),
ImmutableList.of(),
Expand All @@ -1034,12 +1061,16 @@ public void filterChain_5stepMatch() throws Exception {
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(
EnvoyServerProtoData.CidrRange.create("10.1.0.0", 16),
EnvoyServerProtoData.CidrRange.create("10.1.2.0", 30)),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.0.0"), 16),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.2.0"), 30)),
ImmutableList.of(),
ImmutableList.of(
EnvoyServerProtoData.CidrRange.create("10.4.2.0", 24),
EnvoyServerProtoData.CidrRange.create("192.168.2.0", 24)),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.4.2.0"), 24),
EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("192.168.2.0"), 24)),
EnvoyServerProtoData.ConnectionSourceType.ANY,
ImmutableList.of(15000, 8000),
ImmutableList.of(),
Expand All @@ -1053,7 +1084,8 @@ public void filterChain_5stepMatch() throws Exception {
EnvoyServerProtoData.FilterChainMatch filterChainMatch6 =
EnvoyServerProtoData.FilterChainMatch.create(
0,
ImmutableList.of(EnvoyServerProtoData.CidrRange.create("10.1.2.0", 29)),
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.2.0"), 29)),
ImmutableList.of(),
ImmutableList.of(),
EnvoyServerProtoData.ConnectionSourceType.ANY,
Expand Down Expand Up @@ -1105,8 +1137,8 @@ public void filterChainMatch_unsupportedMatchers() throws Exception {
EnvoyServerProtoData.FilterChainMatch filterChainMatch1 =
EnvoyServerProtoData.FilterChainMatch.create(
0 /* destinationPort */,
ImmutableList.of(
EnvoyServerProtoData.CidrRange.create("10.1.0.0", 16)) /* prefixRange */,
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.1.0.0"), 16)) /* prefixRange */,
ImmutableList.of("managed-mtls", "h2") /* applicationProtocol */,
ImmutableList.of() /* sourcePrefixRanges */,
EnvoyServerProtoData.ConnectionSourceType.ANY /* sourceType */,
Expand All @@ -1117,8 +1149,8 @@ public void filterChainMatch_unsupportedMatchers() throws Exception {
EnvoyServerProtoData.FilterChainMatch filterChainMatch2 =
EnvoyServerProtoData.FilterChainMatch.create(
0 /* destinationPort */,
ImmutableList.of(
EnvoyServerProtoData.CidrRange.create("10.0.0.0", 8)) /* prefixRange */,
ImmutableList.of(EnvoyServerProtoData.CidrRange.create(
InetAddresses.forString("10.0.0.0"), 8)) /* prefixRange */,
ImmutableList.of() /* applicationProtocol */,
ImmutableList.of() /* sourcePrefixRanges */,
EnvoyServerProtoData.ConnectionSourceType.ANY /* sourceType */,
Expand Down