1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
//
//
// Copyright 2021 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#include <grpc/support/port_platform.h>
#include "src/core/ext/xds/xds_routing.h"
#include <stdint.h>
#include <stdlib.h>
#include <algorithm>
#include <cctype>
#include <utility>
#include "y_absl/status/status.h"
#include "y_absl/status/statusor.h"
#include "y_absl/strings/match.h"
#include "y_absl/strings/str_cat.h"
#include <grpc/support/log.h>
#include "src/core/ext/xds/xds_http_filters.h"
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/matchers/matchers.h"
namespace grpc_core {
namespace {
enum MatchType {
EXACT_MATCH,
SUFFIX_MATCH,
PREFIX_MATCH,
UNIVERSE_MATCH,
INVALID_MATCH,
};
// Returns true if match succeeds.
bool DomainMatch(MatchType match_type, y_absl::string_view domain_pattern_in,
y_absl::string_view expected_host_name_in) {
// Normalize the args to lower-case. Domain matching is case-insensitive.
TString domain_pattern = TString(domain_pattern_in);
TString expected_host_name = TString(expected_host_name_in);
std::transform(domain_pattern.cbegin(), domain_pattern.cend(),
domain_pattern.begin(),
[](unsigned char c) { return std::tolower(c); });
std::transform(expected_host_name.cbegin(), expected_host_name.cend(),
expected_host_name.begin(),
[](unsigned char c) { return std::tolower(c); });
if (match_type == EXACT_MATCH) {
return domain_pattern == expected_host_name;
} else if (match_type == SUFFIX_MATCH) {
// Asterisk must match at least one char.
if (expected_host_name.size() < domain_pattern.size()) return false;
y_absl::string_view pattern_suffix(domain_pattern.c_str() + 1);
y_absl::string_view host_suffix(expected_host_name.c_str() +
expected_host_name.size() -
pattern_suffix.size());
return pattern_suffix == host_suffix;
} else if (match_type == PREFIX_MATCH) {
// Asterisk must match at least one char.
if (expected_host_name.size() < domain_pattern.size()) return false;
y_absl::string_view pattern_prefix(domain_pattern.c_str(),
domain_pattern.size() - 1);
y_absl::string_view host_prefix(expected_host_name.c_str(),
pattern_prefix.size());
return pattern_prefix == host_prefix;
} else {
return match_type == UNIVERSE_MATCH;
}
}
MatchType DomainPatternMatchType(y_absl::string_view domain_pattern) {
if (domain_pattern.empty()) return INVALID_MATCH;
if (!y_absl::StrContains(domain_pattern, '*')) return EXACT_MATCH;
if (domain_pattern == "*") return UNIVERSE_MATCH;
if (domain_pattern[0] == '*') return SUFFIX_MATCH;
if (domain_pattern[domain_pattern.size() - 1] == '*') return PREFIX_MATCH;
return INVALID_MATCH;
}
} // namespace
y_absl::optional<size_t> XdsRouting::FindVirtualHostForDomain(
const VirtualHostListIterator& vhost_iterator, y_absl::string_view domain) {
// Find the best matched virtual host.
// The search order for 4 groups of domain patterns:
// 1. Exact match.
// 2. Suffix match (e.g., "*ABC").
// 3. Prefix match (e.g., "ABC*").
// 4. Universe match (i.e., "*").
// Within each group, longest match wins.
// If the same best matched domain pattern appears in multiple virtual
// hosts, the first matched virtual host wins.
y_absl::optional<size_t> target_index;
MatchType best_match_type = INVALID_MATCH;
size_t longest_match = 0;
// Check each domain pattern in each virtual host to determine the best
// matched virtual host.
for (size_t i = 0; i < vhost_iterator.Size(); ++i) {
const auto& domains = vhost_iterator.GetDomainsForVirtualHost(i);
for (const TString& domain_pattern : domains) {
// Check the match type first. Skip the pattern if it's not better
// than current match.
const MatchType match_type = DomainPatternMatchType(domain_pattern);
// This should be caught by RouteConfigParse().
GPR_ASSERT(match_type != INVALID_MATCH);
if (match_type > best_match_type) continue;
if (match_type == best_match_type &&
domain_pattern.size() <= longest_match) {
continue;
}
// Skip if match fails.
if (!DomainMatch(match_type, domain_pattern, domain)) continue;
// Choose this match.
target_index = i;
best_match_type = match_type;
longest_match = domain_pattern.size();
if (best_match_type == EXACT_MATCH) break;
}
if (best_match_type == EXACT_MATCH) break;
}
return target_index;
}
namespace {
bool HeadersMatch(const std::vector<HeaderMatcher>& header_matchers,
grpc_metadata_batch* initial_metadata) {
for (const auto& header_matcher : header_matchers) {
TString concatenated_value;
if (!header_matcher.Match(XdsRouting::GetHeaderValue(
initial_metadata, header_matcher.name(), &concatenated_value))) {
return false;
}
}
return true;
}
bool UnderFraction(const uint32_t fraction_per_million) {
// Generate a random number in [0, 1000000).
const uint32_t random_number = rand() % 1000000;
return random_number < fraction_per_million;
}
} // namespace
y_absl::optional<size_t> XdsRouting::GetRouteForRequest(
const RouteListIterator& route_list_iterator, y_absl::string_view path,
grpc_metadata_batch* initial_metadata) {
for (size_t i = 0; i < route_list_iterator.Size(); ++i) {
const XdsRouteConfigResource::Route::Matchers& matchers =
route_list_iterator.GetMatchersForRoute(i);
if (matchers.path_matcher.Match(path) &&
HeadersMatch(matchers.header_matchers, initial_metadata) &&
(!matchers.fraction_per_million.has_value() ||
UnderFraction(*matchers.fraction_per_million))) {
return i;
}
}
return y_absl::nullopt;
}
bool XdsRouting::IsValidDomainPattern(y_absl::string_view domain_pattern) {
return DomainPatternMatchType(domain_pattern) != INVALID_MATCH;
}
y_absl::optional<y_absl::string_view> XdsRouting::GetHeaderValue(
grpc_metadata_batch* initial_metadata, y_absl::string_view header_name,
TString* concatenated_value) {
// Note: If we ever allow binary headers here, we still need to
// special-case ignore "grpc-tags-bin" and "grpc-trace-bin", since
// they are not visible to the LB policy in grpc-go.
if (y_absl::EndsWith(header_name, "-bin")) {
return y_absl::nullopt;
} else if (header_name == "content-type") {
return "application/grpc";
}
return initial_metadata->GetStringValue(header_name, concatenated_value);
}
namespace {
const XdsHttpFilterImpl::FilterConfig* FindFilterConfigOverride(
const TString& instance_name,
const XdsRouteConfigResource::VirtualHost& vhost,
const XdsRouteConfigResource::Route& route,
const XdsRouteConfigResource::Route::RouteAction::ClusterWeight*
cluster_weight) {
// Check ClusterWeight, if any.
if (cluster_weight != nullptr) {
auto it = cluster_weight->typed_per_filter_config.find(instance_name);
if (it != cluster_weight->typed_per_filter_config.end()) return &it->second;
}
// Check Route.
auto it = route.typed_per_filter_config.find(instance_name);
if (it != route.typed_per_filter_config.end()) return &it->second;
// Check VirtualHost.
it = vhost.typed_per_filter_config.find(instance_name);
if (it != vhost.typed_per_filter_config.end()) return &it->second;
// Not found.
return nullptr;
}
} // namespace
y_absl::StatusOr<XdsRouting::GeneratePerHttpFilterConfigsResult>
XdsRouting::GeneratePerHTTPFilterConfigs(
const XdsHttpFilterRegistry& http_filter_registry,
const std::vector<XdsListenerResource::HttpConnectionManager::HttpFilter>&
http_filters,
const XdsRouteConfigResource::VirtualHost& vhost,
const XdsRouteConfigResource::Route& route,
const XdsRouteConfigResource::Route::RouteAction::ClusterWeight*
cluster_weight,
const ChannelArgs& args) {
GeneratePerHttpFilterConfigsResult result;
result.args = args;
for (const auto& http_filter : http_filters) {
// Find filter. This is guaranteed to succeed, because it's checked
// at config validation time in the XdsApi code.
const XdsHttpFilterImpl* filter_impl =
http_filter_registry.GetFilterForType(
http_filter.config.config_proto_type_name);
GPR_ASSERT(filter_impl != nullptr);
// If there is not actually any C-core filter associated with this
// xDS filter, then it won't need any config, so skip it.
if (filter_impl->channel_filter() == nullptr) continue;
// Allow filter to add channel args that may affect service config
// parsing.
result.args = filter_impl->ModifyChannelArgs(result.args);
// Find config override, if any.
const XdsHttpFilterImpl::FilterConfig* config_override =
FindFilterConfigOverride(http_filter.name, vhost, route,
cluster_weight);
// Generate service config for filter.
auto method_config_field =
filter_impl->GenerateServiceConfig(http_filter.config, config_override);
if (!method_config_field.ok()) {
return y_absl::FailedPreconditionError(y_absl::StrCat(
"failed to generate method config for HTTP filter ", http_filter.name,
": ", method_config_field.status().ToString()));
}
result.per_filter_configs[method_config_field->service_config_field_name]
.push_back(method_config_field->element);
}
return result;
}
} // namespace grpc_core
|