| 1 |
// |
| 2 |
// Copyright 2018 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include <grpc/support/port_platform.h> |
| 18 |
|
| 19 |
#include "src/core/ext/xds/xds_route_config.h" |
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
#include <stdint.h> |
| 23 |
|
| 24 |
#include <map> |
| 25 |
#include <memory> |
| 26 |
#include <set> |
| 27 |
#include <string> |
| 28 |
#include <utility> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
#include "absl/status/status.h" |
| 32 |
#include "absl/status/statusor.h" |
| 33 |
#include "absl/strings/str_cat.h" |
| 34 |
#include "absl/strings/str_format.h" |
| 35 |
#include "absl/strings/str_join.h" |
| 36 |
#include "absl/strings/str_split.h" |
| 37 |
#include "absl/strings/string_view.h" |
| 38 |
#include "absl/types/optional.h" |
| 39 |
#include "absl/types/variant.h" |
| 40 |
#include "envoy/config/core/v3/base.upb.h" |
| 41 |
#include "envoy/config/core/v3/extension.upb.h" |
| 42 |
#include "envoy/config/route/v3/route.upb.h" |
| 43 |
#include "envoy/config/route/v3/route.upbdefs.h" |
| 44 |
#include "envoy/config/route/v3/route_components.upb.h" |
| 45 |
#include "envoy/type/matcher/v3/regex.upb.h" |
| 46 |
#include "envoy/type/v3/percent.upb.h" |
| 47 |
#include "envoy/type/v3/range.upb.h" |
| 48 |
#include "google/protobuf/any.upb.h" |
| 49 |
#include "google/protobuf/duration.upb.h" |
| 50 |
#include "google/protobuf/wrappers.upb.h" |
| 51 |
#include "re2/re2.h" |
| 52 |
#include "upb/def.h" |
| 53 |
#include "upb/text_encode.h" |
| 54 |
#include "upb/upb.h" |
| 55 |
|
| 56 |
#include <grpc/status.h> |
| 57 |
#include <grpc/support/log.h> |
| 58 |
|
| 59 |
#include "src/core/ext/xds/upb_utils.h" |
| 60 |
#include "src/core/ext/xds/xds_cluster_specifier_plugin.h" |
| 61 |
#include "src/core/ext/xds/xds_common_types.h" |
| 62 |
#include "src/core/ext/xds/xds_http_filters.h" |
| 63 |
#include "src/core/ext/xds/xds_resource_type.h" |
| 64 |
#include "src/core/ext/xds/xds_routing.h" |
| 65 |
#include "src/core/lib/channel/status_util.h" |
| 66 |
#include "src/core/lib/debug/trace.h" |
| 67 |
#include "src/core/lib/gpr/string.h" |
| 68 |
#include "src/core/lib/gprpp/env.h" |
| 69 |
#include "src/core/lib/gprpp/match.h" |
| 70 |
#include "src/core/lib/gprpp/time.h" |
| 71 |
#include "src/core/lib/gprpp/validation_errors.h" |
| 72 |
#include "src/core/lib/matchers/matchers.h" |
| 73 |
|
| 74 |
namespace grpc_core { |
| 75 |
|
| 76 |
// TODO(donnadionne): Remove once RLS is no longer experimental |
| 77 |
bool XdsRlsEnabled() { |
| 78 |
auto value = GetEnv("GRPC_EXPERIMENTAL_XDS_RLS_LB"); |
| 79 |
if (!value.has_value()) return false; |
| 80 |
bool parsed_value; |
| 81 |
bool parse_succeeded = gpr_parse_bool_value(value->c_str(), &parsed_value); |
| 82 |
return parse_succeeded && parsed_value; |
| 83 |
} |
| 84 |
|
| 85 |
// |
| 86 |
// XdsRouteConfigResource::RetryPolicy |
| 87 |
// |
| 88 |
|
| 89 |
std::string XdsRouteConfigResource::RetryPolicy::RetryBackOff::ToString() |
| 90 |
const { |
| 91 |
std::vector<std::string> contents; |
| 92 |
contents.push_back( |
| 93 |
absl::StrCat("RetryBackOff Base: ", base_interval.ToString())); |
| 94 |
contents.push_back( |
| 95 |
absl::StrCat("RetryBackOff max: ", max_interval.ToString())); |
| 96 |
return absl::StrJoin(contents, ","); |
| 97 |
} |
| 98 |
|
| 99 |
std::string XdsRouteConfigResource::RetryPolicy::ToString() const { |
| 100 |
std::vector<std::string> contents; |
| 101 |
contents.push_back(absl::StrFormat("num_retries=%d", num_retries)); |
| 102 |
contents.push_back(retry_back_off.ToString()); |
| 103 |
return absl::StrCat("{", absl::StrJoin(contents, ","), "}"); |
| 104 |
} |
| 105 |
|
| 106 |
// |
| 107 |
// XdsRouteConfigResource::Route::Matchers |
| 108 |
// |
| 109 |
|
| 110 |
std::string XdsRouteConfigResource::Route::Matchers::ToString() const { |
| 111 |
std::vector<std::string> contents; |
| 112 |
contents.push_back( |
| 113 |
absl::StrFormat("PathMatcher{%s}", path_matcher.ToString())); |
| 114 |
for (const HeaderMatcher& header_matcher : header_matchers) { |
| 115 |
contents.push_back(header_matcher.ToString()); |
| 116 |
} |
| 117 |
if (fraction_per_million.has_value()) { |
| 118 |
contents.push_back(absl::StrFormat("Fraction Per Million %d", |
| 119 |
fraction_per_million.value())); |
| 120 |
} |
| 121 |
return absl::StrJoin(contents, "\n"); |
| 122 |
} |
| 123 |
|
| 124 |
// |
| 125 |
// XdsRouteConfigResource::Route::RouteAction::HashPolicy |
| 126 |
// |
| 127 |
|
| 128 |
XdsRouteConfigResource::Route::RouteAction::HashPolicy::HashPolicy( |
| 129 |
const HashPolicy& other) |
| 130 |
: type(other.type), |
| 131 |
header_name(other.header_name), |
| 132 |
regex_substitution(other.regex_substitution) { |
| 133 |
if (other.regex != nullptr) { |
| 134 |
regex = |
| 135 |
std::make_unique<RE2>(other.regex->pattern(), other.regex->options()); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
XdsRouteConfigResource::Route::RouteAction::HashPolicy& |
| 140 |
XdsRouteConfigResource::Route::RouteAction::HashPolicy::operator=( |
| 141 |
const HashPolicy& other) { |
| 142 |
type = other.type; |
| 143 |
header_name = other.header_name; |
| 144 |
if (other.regex != nullptr) { |
| 145 |
regex = |
| 146 |
std::make_unique<RE2>(other.regex->pattern(), other.regex->options()); |
| 147 |
} |
| 148 |
regex_substitution = other.regex_substitution; |
| 149 |
return *this; |
| 150 |
} |
| 151 |
|
| 152 |
XdsRouteConfigResource::Route::RouteAction::HashPolicy::HashPolicy( |
| 153 |
HashPolicy&& other) noexcept |
| 154 |
: type(other.type), |
| 155 |
header_name(std::move(other.header_name)), |
| 156 |
regex(std::move(other.regex)), |
| 157 |
regex_substitution(std::move(other.regex_substitution)) {} |
| 158 |
|
| 159 |
XdsRouteConfigResource::Route::RouteAction::HashPolicy& |
| 160 |
XdsRouteConfigResource::Route::RouteAction::HashPolicy::operator=( |
| 161 |
HashPolicy&& other) noexcept { |
| 162 |
type = other.type; |
| 163 |
header_name = std::move(other.header_name); |
| 164 |
regex = std::move(other.regex); |
| 165 |
regex_substitution = std::move(other.regex_substitution); |
| 166 |
return *this; |
| 167 |
} |
| 168 |
|
| 169 |
bool XdsRouteConfigResource::Route::RouteAction::HashPolicy::HashPolicy:: |
| 170 |
operator==(const HashPolicy& other) const { |
| 171 |
if (type != other.type) return false; |
| 172 |
if (type == Type::HEADER) { |
| 173 |
if (regex == nullptr) { |
| 174 |
if (other.regex != nullptr) return false; |
| 175 |
} else { |
| 176 |
if (other.regex == nullptr) return false; |
| 177 |
return header_name == other.header_name && |
| 178 |
regex->pattern() == other.regex->pattern() && |
| 179 |
regex_substitution == other.regex_substitution; |
| 180 |
} |
| 181 |
} |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
std::string XdsRouteConfigResource::Route::RouteAction::HashPolicy::ToString() |
| 186 |
const { |
| 187 |
std::vector<std::string> contents; |
| 188 |
switch (type) { |
| 189 |
case Type::HEADER: |
| 190 |
contents.push_back("type=HEADER"); |
| 191 |
break; |
| 192 |
case Type::CHANNEL_ID: |
| 193 |
contents.push_back("type=CHANNEL_ID"); |
| 194 |
break; |
| 195 |
} |
| 196 |
contents.push_back( |
| 197 |
absl::StrFormat("terminal=%s", terminal ? "true" : "false")); |
| 198 |
if (type == Type::HEADER) { |
| 199 |
contents.push_back(absl::StrFormat( |
| 200 |
"Header %s:/%s/%s", header_name, |
| 201 |
(regex == nullptr) ? "" : regex->pattern(), regex_substitution)); |
| 202 |
} |
| 203 |
return absl::StrCat("{", absl::StrJoin(contents, ", "), "}"); |
| 204 |
} |
| 205 |
|
| 206 |
// |
| 207 |
// XdsRouteConfigResource::Route::RouteAction::ClusterWeight |
| 208 |
// |
| 209 |
|
| 210 |
std::string |
| 211 |
XdsRouteConfigResource::Route::RouteAction::ClusterWeight::ToString() const { |
| 212 |
std::vector<std::string> contents; |
| 213 |
contents.push_back(absl::StrCat("cluster=", name)); |
| 214 |
contents.push_back(absl::StrCat("weight=", weight)); |
| 215 |
if (!typed_per_filter_config.empty()) { |
| 216 |
std::vector<std::string> parts; |
| 217 |
for (const auto& p : typed_per_filter_config) { |
| 218 |
const std::string& key = p.first; |
| 219 |
const auto& config = p.second; |
| 220 |
parts.push_back(absl::StrCat(key, "=", config.ToString())); |
| 221 |
} |
| 222 |
contents.push_back(absl::StrCat("typed_per_filter_config={", |
| 223 |
absl::StrJoin(parts, ", "), "}")); |
| 224 |
} |
| 225 |
return absl::StrCat("{", absl::StrJoin(contents, ", "), "}"); |
| 226 |
} |
| 227 |
|
| 228 |
// |
| 229 |
// XdsRouteConfigResource::Route::RouteAction |
| 230 |
// |
| 231 |
|
| 232 |
std::string XdsRouteConfigResource::Route::RouteAction::ToString() const { |
| 233 |
std::vector<std::string> contents; |
| 234 |
contents.reserve(hash_policies.size()); |
| 235 |
for (const HashPolicy& hash_policy : hash_policies) { |
| 236 |
contents.push_back(absl::StrCat("hash_policy=", hash_policy.ToString())); |
| 237 |
} |
| 238 |
if (retry_policy.has_value()) { |
| 239 |
contents.push_back(absl::StrCat("retry_policy=", retry_policy->ToString())); |
| 240 |
} |
| 241 |
Match( |
| 242 |
action, |
| 243 |
[&contents](const ClusterName& cluster_name) { |
| 244 |
contents.push_back( |
| 245 |
absl::StrFormat("Cluster name: %s", cluster_name.cluster_name)); |
| 246 |
}, |
| 247 |
[&contents](const std::vector<ClusterWeight>& weighted_clusters) { |
| 248 |
for (const ClusterWeight& cluster_weight : weighted_clusters) { |
| 249 |
contents.push_back(cluster_weight.ToString()); |
| 250 |
} |
| 251 |
}, |
| 252 |
[&contents]( |
| 253 |
const ClusterSpecifierPluginName& cluster_specifier_plugin_name) { |
| 254 |
contents.push_back(absl::StrFormat( |
| 255 |
"Cluster specifier plugin name: %s", |
| 256 |
cluster_specifier_plugin_name.cluster_specifier_plugin_name)); |
| 257 |
}); |
| 258 |
if (max_stream_duration.has_value()) { |
| 259 |
contents.push_back(max_stream_duration->ToString()); |
| 260 |
} |
| 261 |
return absl::StrCat("{", absl::StrJoin(contents, ", "), "}"); |
| 262 |
} |
| 263 |
|
| 264 |
// |
| 265 |
// XdsRouteConfigResource::Route |
| 266 |
// |
| 267 |
|
| 268 |
std::string XdsRouteConfigResource::Route::ToString() const { |
| 269 |
std::vector<std::string> contents; |
| 270 |
contents.push_back(matchers.ToString()); |
| 271 |
auto* route_action = |
| 272 |
absl::get_if<XdsRouteConfigResource::Route::RouteAction>(&action); |
| 273 |
if (route_action != nullptr) { |
| 274 |
contents.push_back(absl::StrCat("route=", route_action->ToString())); |
| 275 |
} else if (absl::holds_alternative< |
| 276 |
XdsRouteConfigResource::Route::NonForwardingAction>(action)) { |
| 277 |
contents.push_back("non_forwarding_action={}"); |
| 278 |
} else { |
| 279 |
contents.push_back("unknown_action={}"); |
| 280 |
} |
| 281 |
if (!typed_per_filter_config.empty()) { |
| 282 |
contents.push_back("typed_per_filter_config={"); |
| 283 |
for (const auto& p : typed_per_filter_config) { |
| 284 |
const std::string& name = p.first; |
| 285 |
const auto& config = p.second; |
| 286 |
contents.push_back(absl::StrCat(" ", name, "=", config.ToString())); |
| 287 |
} |
| 288 |
contents.push_back("}"); |
| 289 |
} |
| 290 |
return absl::StrJoin(contents, "\n"); |
| 291 |
} |
| 292 |
|
| 293 |
// |
| 294 |
// XdsRouteConfigResource |
| 295 |
// |
| 296 |
|
| 297 |
std::string XdsRouteConfigResource::ToString() const { |
| 298 |
std::vector<std::string> parts; |
| 299 |
for (const VirtualHost& vhost : virtual_hosts) { |
| 300 |
parts.push_back( |
| 301 |
absl::StrCat("vhost={\n" |
| 302 |
" domains=[", |
| 303 |
absl::StrJoin(vhost.domains, ", "), |
| 304 |
"]\n" |
| 305 |
" routes=[\n")); |
| 306 |
for (const XdsRouteConfigResource::Route& route : vhost.routes) { |
| 307 |
parts.push_back(" {\n"); |
| 308 |
parts.push_back(route.ToString()); |
| 309 |
parts.push_back("\n }\n"); |
| 310 |
} |
| 311 |
parts.push_back(" ]\n"); |
| 312 |
parts.push_back(" typed_per_filter_config={\n"); |
| 313 |
for (const auto& p : vhost.typed_per_filter_config) { |
| 314 |
const std::string& name = p.first; |
| 315 |
const auto& config = p.second; |
| 316 |
parts.push_back(absl::StrCat(" ", name, "=", config.ToString(), "\n")); |
| 317 |
} |
| 318 |
parts.push_back(" }\n"); |
| 319 |
parts.push_back("]\n"); |
| 320 |
} |
| 321 |
parts.push_back("cluster_specifier_plugins={\n"); |
| 322 |
for (const auto& it : cluster_specifier_plugin_map) { |
| 323 |
parts.push_back(absl::StrFormat("%s={%s}\n", it.first, it.second)); |
| 324 |
} |
| 325 |
parts.push_back("}"); |
| 326 |
return absl::StrJoin(parts, ""); |
| 327 |
} |
| 328 |
|
| 329 |
namespace { |
| 330 |
|
| 331 |
absl::StatusOr<XdsRouteConfigResource::ClusterSpecifierPluginMap> |
| 332 |
ClusterSpecifierPluginParse( |
| 333 |
const XdsResourceType::DecodeContext& context, |
| 334 |
const envoy_config_route_v3_RouteConfiguration* route_config) { |
| 335 |
XdsRouteConfigResource::ClusterSpecifierPluginMap |
| 336 |
cluster_specifier_plugin_map; |
| 337 |
const auto& cluster_specifier_plugin_registry = |
| 338 |
static_cast<const GrpcXdsBootstrap&>(context.client->bootstrap()) |
| 339 |
.cluster_specifier_plugin_registry(); |
| 340 |
size_t num_cluster_specifier_plugins; |
| 341 |
const envoy_config_route_v3_ClusterSpecifierPlugin* const* |
| 342 |
cluster_specifier_plugin = |
| 343 |
envoy_config_route_v3_RouteConfiguration_cluster_specifier_plugins( |
| 344 |
route_config, &num_cluster_specifier_plugins); |
| 345 |
for (size_t i = 0; i < num_cluster_specifier_plugins; ++i) { |
| 346 |
const envoy_config_core_v3_TypedExtensionConfig* typed_extension_config = |
| 347 |
envoy_config_route_v3_ClusterSpecifierPlugin_extension( |
| 348 |
cluster_specifier_plugin[i]); |
| 349 |
std::string name = UpbStringToStdString( |
| 350 |
envoy_config_core_v3_TypedExtensionConfig_name(typed_extension_config)); |
| 351 |
if (cluster_specifier_plugin_map.find(name) != |
| 352 |
cluster_specifier_plugin_map.end()) { |
| 353 |
return absl::InvalidArgumentError(absl::StrCat( |
| 354 |
"Duplicated definition of cluster_specifier_plugin ", name)); |
| 355 |
} |
| 356 |
const google_protobuf_Any* any = |
| 357 |
envoy_config_core_v3_TypedExtensionConfig_typed_config( |
| 358 |
typed_extension_config); |
| 359 |
if (any == nullptr) { |
| 360 |
return absl::InvalidArgumentError( |
| 361 |
"Could not obtrain TypedExtensionConfig for plugin config."); |
| 362 |
} |
| 363 |
ValidationErrors validation_errors; |
| 364 |
ValidationErrors::ScopedField field( |
| 365 |
&validation_errors, absl::StrCat(".cluster_specifier_plugins[", i, |
| 366 |
"].extension.typed_config")); |
| 367 |
auto extension = ExtractXdsExtension(context, any, &validation_errors); |
| 368 |
if (!validation_errors.ok()) { |
| 369 |
return validation_errors.status("could not determine extension type"); |
| 370 |
} |
| 371 |
GPR_ASSERT(extension.has_value()); |
| 372 |
bool is_optional = envoy_config_route_v3_ClusterSpecifierPlugin_is_optional( |
| 373 |
cluster_specifier_plugin[i]); |
| 374 |
const XdsClusterSpecifierPluginImpl* cluster_specifier_plugin_impl = |
| 375 |
cluster_specifier_plugin_registry.GetPluginForType(extension->type); |
| 376 |
std::string lb_policy_config; |
| 377 |
if (cluster_specifier_plugin_impl == nullptr) { |
| 378 |
if (!is_optional) { |
| 379 |
return absl::InvalidArgumentError(absl::StrCat( |
| 380 |
"Unknown ClusterSpecifierPlugin type ", extension->type)); |
| 381 |
} |
| 382 |
// Optional plugin, leave lb_policy_config empty. |
| 383 |
} else { |
| 384 |
auto config = |
| 385 |
cluster_specifier_plugin_impl->GenerateLoadBalancingPolicyConfig( |
| 386 |
std::move(*extension), context.arena, context.symtab); |
| 387 |
if (!config.ok()) return config.status(); |
| 388 |
lb_policy_config = std::move(*config); |
| 389 |
} |
| 390 |
cluster_specifier_plugin_map[std::move(name)] = std::move(lb_policy_config); |
| 391 |
} |
| 392 |
return cluster_specifier_plugin_map; |
| 393 |
} |
| 394 |
|
| 395 |
absl::Status RoutePathMatchParse(const envoy_config_route_v3_RouteMatch* match, |
| 396 |
XdsRouteConfigResource::Route* route, |
| 397 |
bool* ignore_route) { |
| 398 |
auto* case_sensitive_ptr = |
| 399 |
envoy_config_route_v3_RouteMatch_case_sensitive(match); |
| 400 |
bool case_sensitive = true; |
| 401 |
if (case_sensitive_ptr != nullptr) { |
| 402 |
case_sensitive = google_protobuf_BoolValue_value(case_sensitive_ptr); |
| 403 |
} |
| 404 |
StringMatcher::Type type; |
| 405 |
std::string match_string; |
| 406 |
if (envoy_config_route_v3_RouteMatch_has_prefix(match)) { |
| 407 |
absl::string_view prefix = |
| 408 |
UpbStringToAbsl(envoy_config_route_v3_RouteMatch_prefix(match)); |
| 409 |
// Empty prefix "" is accepted. |
| 410 |
if (!prefix.empty()) { |
| 411 |
// Prefix "/" is accepted. |
| 412 |
if (prefix[0] != '/') { |
| 413 |
// Prefix which does not start with a / will never match anything, so |
| 414 |
// ignore this route. |
| 415 |
*ignore_route = true; |
| 416 |
return absl::OkStatus(); |
| 417 |
} |
| 418 |
std::vector<absl::string_view> prefix_elements = |
| 419 |
absl::StrSplit(prefix.substr(1), absl::MaxSplits('/', 2)); |
| 420 |
if (prefix_elements.size() > 2) { |
| 421 |
// Prefix cannot have more than 2 slashes. |
| 422 |
*ignore_route = true; |
| 423 |
return absl::OkStatus(); |
| 424 |
} else if (prefix_elements.size() == 2 && prefix_elements[0].empty()) { |
| 425 |
// Prefix contains empty string between the 2 slashes |
| 426 |
*ignore_route = true; |
| 427 |
return absl::OkStatus(); |
| 428 |
} |
| 429 |
} |
| 430 |
type = StringMatcher::Type::kPrefix; |
| 431 |
match_string = std::string(prefix); |
| 432 |
} else if (envoy_config_route_v3_RouteMatch_has_path(match)) { |
| 433 |
absl::string_view path = |
| 434 |
UpbStringToAbsl(envoy_config_route_v3_RouteMatch_path(match)); |
| 435 |
if (path.empty()) { |
| 436 |
// Path that is empty will never match anything, so ignore this route. |
| 437 |
*ignore_route = true; |
| 438 |
return absl::OkStatus(); |
| 439 |
} |
| 440 |
if (path[0] != '/') { |
| 441 |
// Path which does not start with a / will never match anything, so |
| 442 |
// ignore this route. |
| 443 |
*ignore_route = true; |
| 444 |
return absl::OkStatus(); |
| 445 |
} |
| 446 |
std::vector<absl::string_view> path_elements = |
| 447 |
absl::StrSplit(path.substr(1), absl::MaxSplits('/', 2)); |
| 448 |
if (path_elements.size() != 2) { |
| 449 |
// Path not in the required format of /service/method will never match |
| 450 |
// anything, so ignore this route. |
| 451 |
*ignore_route = true; |
| 452 |
return absl::OkStatus(); |
| 453 |
} else if (path_elements[0].empty()) { |
| 454 |
// Path contains empty service name will never match anything, so ignore |
| 455 |
// this route. |
| 456 |
*ignore_route = true; |
| 457 |
return absl::OkStatus(); |
| 458 |
} else if (path_elements[1].empty()) { |
| 459 |
// Path contains empty method name will never match anything, so ignore |
| 460 |
// this route. |
| 461 |
*ignore_route = true; |
| 462 |
return absl::OkStatus(); |
| 463 |
} |
| 464 |
type = StringMatcher::Type::kExact; |
| 465 |
match_string = std::string(path); |
| 466 |
} else if (envoy_config_route_v3_RouteMatch_has_safe_regex(match)) { |
| 467 |
const envoy_type_matcher_v3_RegexMatcher* regex_matcher = |
| 468 |
envoy_config_route_v3_RouteMatch_safe_regex(match); |
| 469 |
GPR_ASSERT(regex_matcher != nullptr); |
| 470 |
type = StringMatcher::Type::kSafeRegex; |
| 471 |
match_string = UpbStringToStdString( |
| 472 |
envoy_type_matcher_v3_RegexMatcher_regex(regex_matcher)); |
| 473 |
} else { |
| 474 |
return absl::InvalidArgumentError( |
| 475 |
"Invalid route path specifier specified."); |
| 476 |
} |
| 477 |
absl::StatusOr<StringMatcher> string_matcher = |
| 478 |
StringMatcher::Create(type, match_string, case_sensitive); |
| 479 |
if (!string_matcher.ok()) { |
| 480 |
return absl::InvalidArgumentError( |
| 481 |
absl::StrCat("path matcher: ", string_matcher.status().message())); |
| 482 |
} |
| 483 |
route->matchers.path_matcher = std::move(string_matcher.value()); |
| 484 |
return absl::OkStatus(); |
| 485 |
} |
| 486 |
|
| 487 |
absl::Status RouteHeaderMatchersParse( |
| 488 |
const envoy_config_route_v3_RouteMatch* match, |
| 489 |
XdsRouteConfigResource::Route* route) { |
| 490 |
size_t size; |
| 491 |
const envoy_config_route_v3_HeaderMatcher* const* headers = |
| 492 |
envoy_config_route_v3_RouteMatch_headers(match, &size); |
| 493 |
for (size_t i = 0; i < size; ++i) { |
| 494 |
const envoy_config_route_v3_HeaderMatcher* header = headers[i]; |
| 495 |
const std::string name = |
| 496 |
UpbStringToStdString(envoy_config_route_v3_HeaderMatcher_name(header)); |
| 497 |
HeaderMatcher::Type type; |
| 498 |
std::string match_string; |
| 499 |
int64_t range_start = 0; |
| 500 |
int64_t range_end = 0; |
| 501 |
bool present_match = false; |
| 502 |
if (envoy_config_route_v3_HeaderMatcher_has_exact_match(header)) { |
| 503 |
type = HeaderMatcher::Type::kExact; |
| 504 |
match_string = UpbStringToStdString( |
| 505 |
envoy_config_route_v3_HeaderMatcher_exact_match(header)); |
| 506 |
} else if (envoy_config_route_v3_HeaderMatcher_has_safe_regex_match( |
| 507 |
header)) { |
| 508 |
const envoy_type_matcher_v3_RegexMatcher* regex_matcher = |
| 509 |
envoy_config_route_v3_HeaderMatcher_safe_regex_match(header); |
| 510 |
GPR_ASSERT(regex_matcher != nullptr); |
| 511 |
type = HeaderMatcher::Type::kSafeRegex; |
| 512 |
match_string = UpbStringToStdString( |
| 513 |
envoy_type_matcher_v3_RegexMatcher_regex(regex_matcher)); |
| 514 |
} else if (envoy_config_route_v3_HeaderMatcher_has_range_match(header)) { |
| 515 |
type = HeaderMatcher::Type::kRange; |
| 516 |
const envoy_type_v3_Int64Range* range_matcher = |
| 517 |
envoy_config_route_v3_HeaderMatcher_range_match(header); |
| 518 |
range_start = envoy_type_v3_Int64Range_start(range_matcher); |
| 519 |
range_end = envoy_type_v3_Int64Range_end(range_matcher); |
| 520 |
} else if (envoy_config_route_v3_HeaderMatcher_has_present_match(header)) { |
| 521 |
type = HeaderMatcher::Type::kPresent; |
| 522 |
present_match = envoy_config_route_v3_HeaderMatcher_present_match(header); |
| 523 |
} else if (envoy_config_route_v3_HeaderMatcher_has_prefix_match(header)) { |
| 524 |
type = HeaderMatcher::Type::kPrefix; |
| 525 |
match_string = UpbStringToStdString( |
| 526 |
envoy_config_route_v3_HeaderMatcher_prefix_match(header)); |
| 527 |
} else if (envoy_config_route_v3_HeaderMatcher_has_suffix_match(header)) { |
| 528 |
type = HeaderMatcher::Type::kSuffix; |
| 529 |
match_string = UpbStringToStdString( |
| 530 |
envoy_config_route_v3_HeaderMatcher_suffix_match(header)); |
| 531 |
} else if (envoy_config_route_v3_HeaderMatcher_has_contains_match(header)) { |
| 532 |
type = HeaderMatcher::Type::kContains; |
| 533 |
match_string = UpbStringToStdString( |
| 534 |
envoy_config_route_v3_HeaderMatcher_contains_match(header)); |
| 535 |
} else { |
| 536 |
return absl::InvalidArgumentError( |
| 537 |
"Invalid route header matcher specified."); |
| 538 |
} |
| 539 |
bool invert_match = |
| 540 |
envoy_config_route_v3_HeaderMatcher_invert_match(header); |
| 541 |
absl::StatusOr<HeaderMatcher> header_matcher = |
| 542 |
HeaderMatcher::Create(name, type, match_string, range_start, range_end, |
| 543 |
present_match, invert_match); |
| 544 |
if (!header_matcher.ok()) { |
| 545 |
return absl::InvalidArgumentError( |
| 546 |
absl::StrCat("header matcher: ", header_matcher.status().message())); |
| 547 |
} |
| 548 |
route->matchers.header_matchers.emplace_back( |
| 549 |
std::move(header_matcher.value())); |
| 550 |
} |
| 551 |
return absl::OkStatus(); |
| 552 |
} |
| 553 |
|
| 554 |
absl::Status RouteRuntimeFractionParse( |
| 555 |
const envoy_config_route_v3_RouteMatch* match, |
| 556 |
XdsRouteConfigResource::Route* route) { |
| 557 |
const envoy_config_core_v3_RuntimeFractionalPercent* runtime_fraction = |
| 558 |
envoy_config_route_v3_RouteMatch_runtime_fraction(match); |
| 559 |
if (runtime_fraction != nullptr) { |
| 560 |
const envoy_type_v3_FractionalPercent* fraction = |
| 561 |
envoy_config_core_v3_RuntimeFractionalPercent_default_value( |
| 562 |
runtime_fraction); |
| 563 |
if (fraction != nullptr) { |
| 564 |
uint32_t numerator = envoy_type_v3_FractionalPercent_numerator(fraction); |
| 565 |
const auto denominator = |
| 566 |
static_cast<envoy_type_v3_FractionalPercent_DenominatorType>( |
| 567 |
envoy_type_v3_FractionalPercent_denominator(fraction)); |
| 568 |
// Normalize to million. |
| 569 |
switch (denominator) { |
| 570 |
case envoy_type_v3_FractionalPercent_HUNDRED: |
| 571 |
numerator *= 10000; |
| 572 |
break; |
| 573 |
case envoy_type_v3_FractionalPercent_TEN_THOUSAND: |
| 574 |
numerator *= 100; |
| 575 |
break; |
| 576 |
case envoy_type_v3_FractionalPercent_MILLION: |
| 577 |
break; |
| 578 |
default: |
| 579 |
return absl::InvalidArgumentError("Unknown denominator type"); |
| 580 |
} |
| 581 |
route->matchers.fraction_per_million = numerator; |
| 582 |
} |
| 583 |
} |
| 584 |
return absl::OkStatus(); |
| 585 |
} |
| 586 |
|
| 587 |
template <typename ParentType, typename EntryType> |
| 588 |
absl::StatusOr<XdsRouteConfigResource::TypedPerFilterConfig> |
| 589 |
ParseTypedPerFilterConfig( |
| 590 |
const XdsResourceType::DecodeContext& context, const ParentType* parent, |
| 591 |
const EntryType* (*entry_func)(const ParentType*, size_t*), |
| 592 |
upb_StringView (*key_func)(const EntryType*), |
| 593 |
const google_protobuf_Any* (*value_func)(const EntryType*)) { |
| 594 |
XdsRouteConfigResource::TypedPerFilterConfig typed_per_filter_config; |
| 595 |
size_t filter_it = kUpb_Map_Begin; |
| 596 |
while (true) { |
| 597 |
const auto* filter_entry = entry_func(parent, &filter_it); |
| 598 |
if (filter_entry == nullptr) break; |
| 599 |
absl::string_view key = UpbStringToAbsl(key_func(filter_entry)); |
| 600 |
if (key.empty()) { |
| 601 |
return absl::InvalidArgumentError("empty filter name in map"); |
| 602 |
} |
| 603 |
const google_protobuf_Any* any = value_func(filter_entry); |
| 604 |
GPR_ASSERT(any != nullptr); |
| 605 |
absl::string_view filter_type = |
| 606 |
UpbStringToAbsl(google_protobuf_Any_type_url(any)); |
| 607 |
if (filter_type.empty()) { |
| 608 |
return absl::InvalidArgumentError( |
| 609 |
absl::StrCat("no filter config specified for filter name ", key)); |
| 610 |
} |
| 611 |
bool is_optional = false; |
| 612 |
if (filter_type == |
| 613 |
"type.googleapis.com/envoy.config.route.v3.FilterConfig") { |
| 614 |
upb_StringView any_value = google_protobuf_Any_value(any); |
| 615 |
const auto* filter_config = envoy_config_route_v3_FilterConfig_parse( |
| 616 |
any_value.data, any_value.size, context.arena); |
| 617 |
if (filter_config == nullptr) { |
| 618 |
return absl::InvalidArgumentError( |
| 619 |
absl::StrCat("could not parse FilterConfig wrapper for ", key)); |
| 620 |
} |
| 621 |
is_optional = |
| 622 |
envoy_config_route_v3_FilterConfig_is_optional(filter_config); |
| 623 |
any = envoy_config_route_v3_FilterConfig_config(filter_config); |
| 624 |
if (any == nullptr) { |
| 625 |
if (is_optional) continue; |
| 626 |
return absl::InvalidArgumentError( |
| 627 |
absl::StrCat("no filter config specified for filter name ", key)); |
| 628 |
} |
| 629 |
} |
| 630 |
ValidationErrors errors; |
| 631 |
ValidationErrors::ScopedField field( |
| 632 |
&errors, absl::StrCat(".typed_per_filter_config[", key, "]")); |
| 633 |
auto extension = ExtractXdsExtension(context, any, &errors); |
| 634 |
if (!errors.ok()) { |
| 635 |
return errors.status("could not determine extension type"); |
| 636 |
} |
| 637 |
GPR_ASSERT(extension.has_value()); |
| 638 |
const auto& http_filter_registry = |
| 639 |
static_cast<const GrpcXdsBootstrap&>(context.client->bootstrap()) |
| 640 |
.http_filter_registry(); |
| 641 |
const XdsHttpFilterImpl* filter_impl = |
| 642 |
http_filter_registry.GetFilterForType(extension->type); |
| 643 |
if (filter_impl == nullptr) { |
| 644 |
if (is_optional) continue; |
| 645 |
return absl::InvalidArgumentError(absl::StrCat( |
| 646 |
"no filter registered for config type ", extension->type)); |
| 647 |
} |
| 648 |
absl::optional<XdsHttpFilterImpl::FilterConfig> filter_config = |
| 649 |
filter_impl->GenerateFilterConfigOverride(std::move(*extension), |
| 650 |
context.arena, &errors); |
| 651 |
if (!errors.ok()) { |
| 652 |
return errors.status("errors validation extension"); |
| 653 |
} |
| 654 |
GPR_ASSERT(filter_config.has_value()); |
| 655 |
typed_per_filter_config[std::string(key)] = std::move(*filter_config); |
| 656 |
} |
| 657 |
return typed_per_filter_config; |
| 658 |
} |
| 659 |
|
| 660 |
absl::Status RetryPolicyParse( |
| 661 |
const XdsResourceType::DecodeContext& context, |
| 662 |
const envoy_config_route_v3_RetryPolicy* retry_policy, |
| 663 |
absl::optional<XdsRouteConfigResource::RetryPolicy>* retry) { |
| 664 |
std::vector<std::string> errors; |
| 665 |
XdsRouteConfigResource::RetryPolicy retry_to_return; |
| 666 |
auto retry_on = UpbStringToStdString( |
| 667 |
envoy_config_route_v3_RetryPolicy_retry_on(retry_policy)); |
| 668 |
std::vector<absl::string_view> codes = absl::StrSplit(retry_on, ','); |
| 669 |
for (const auto& code : codes) { |
| 670 |
if (code == "cancelled") { |
| 671 |
retry_to_return.retry_on.Add(GRPC_STATUS_CANCELLED); |
| 672 |
} else if (code == "deadline-exceeded") { |
| 673 |
retry_to_return.retry_on.Add(GRPC_STATUS_DEADLINE_EXCEEDED); |
| 674 |
} else if (code == "internal") { |
| 675 |
retry_to_return.retry_on.Add(GRPC_STATUS_INTERNAL); |
| 676 |
} else if (code == "resource-exhausted") { |
| 677 |
retry_to_return.retry_on.Add(GRPC_STATUS_RESOURCE_EXHAUSTED); |
| 678 |
} else if (code == "unavailable") { |
| 679 |
retry_to_return.retry_on.Add(GRPC_STATUS_UNAVAILABLE); |
| 680 |
} else { |
| 681 |
if (GRPC_TRACE_FLAG_ENABLED(*context.tracer)) { |
| 682 |
gpr_log(GPR_INFO, "Unsupported retry_on policy %s.", |
| 683 |
std::string(code).c_str()); |
| 684 |
} |
| 685 |
} |
| 686 |
} |
| 687 |
const google_protobuf_UInt32Value* num_retries = |
| 688 |
envoy_config_route_v3_RetryPolicy_num_retries(retry_policy); |
| 689 |
if (num_retries != nullptr) { |
| 690 |
uint32_t num_retries_value = google_protobuf_UInt32Value_value(num_retries); |
| 691 |
if (num_retries_value == 0) { |
| 692 |
errors.emplace_back( |
| 693 |
"RouteAction RetryPolicy num_retries set to invalid value 0."); |
| 694 |
} else { |
| 695 |
retry_to_return.num_retries = num_retries_value; |
| 696 |
} |
| 697 |
} else { |
| 698 |
retry_to_return.num_retries = 1; |
| 699 |
} |
| 700 |
const envoy_config_route_v3_RetryPolicy_RetryBackOff* backoff = |
| 701 |
envoy_config_route_v3_RetryPolicy_retry_back_off(retry_policy); |
| 702 |
if (backoff != nullptr) { |
| 703 |
const google_protobuf_Duration* base_interval = |
| 704 |
envoy_config_route_v3_RetryPolicy_RetryBackOff_base_interval(backoff); |
| 705 |
if (base_interval == nullptr) { |
| 706 |
errors.emplace_back( |
| 707 |
"RouteAction RetryPolicy RetryBackoff missing base interval."); |
| 708 |
} else { |
| 709 |
ValidationErrors validation_errors; |
| 710 |
retry_to_return.retry_back_off.base_interval = |
| 711 |
ParseDuration(base_interval, &validation_errors); |
| 712 |
if (!validation_errors.ok()) { |
| 713 |
errors.emplace_back( |
| 714 |
validation_errors.status("base_interval").message()); |
| 715 |
} |
| 716 |
} |
| 717 |
const google_protobuf_Duration* max_interval = |
| 718 |
envoy_config_route_v3_RetryPolicy_RetryBackOff_max_interval(backoff); |
| 719 |
Duration max; |
| 720 |
if (max_interval != nullptr) { |
| 721 |
ValidationErrors validation_errors; |
| 722 |
max = ParseDuration(max_interval, &validation_errors); |
| 723 |
if (!validation_errors.ok()) { |
| 724 |
errors.emplace_back(validation_errors.status("max_interval").message()); |
| 725 |
} |
| 726 |
} else { |
| 727 |
// if max interval is not set, it is 10x the base. |
| 728 |
max = 10 * retry_to_return.retry_back_off.base_interval; |
| 729 |
} |
| 730 |
retry_to_return.retry_back_off.max_interval = max; |
| 731 |
} else { |
| 732 |
retry_to_return.retry_back_off.base_interval = Duration::Milliseconds(25); |
| 733 |
retry_to_return.retry_back_off.max_interval = Duration::Milliseconds(250); |
| 734 |
} |
| 735 |
// Return result. |
| 736 |
if (!errors.empty()) { |
| 737 |
return absl::InvalidArgumentError(absl::StrCat( |
| 738 |
"Errors parsing retry policy: [", absl::StrJoin(errors, "; "), "]")); |
| 739 |
} |
| 740 |
*retry = retry_to_return; |
| 741 |
return absl::OkStatus(); |
| 742 |
} |
| 743 |
|
| 744 |
absl::StatusOr<XdsRouteConfigResource::Route::RouteAction> RouteActionParse( |
| 745 |
const XdsResourceType::DecodeContext& context, |
| 746 |
const envoy_config_route_v3_Route* route_msg, |
| 747 |
const std::map<std::string /*cluster_specifier_plugin_name*/, |
| 748 |
std::string /*LB policy config*/>& |
| 749 |
cluster_specifier_plugin_map, |
| 750 |
bool* ignore_route) { |
| 751 |
XdsRouteConfigResource::Route::RouteAction route; |
| 752 |
const envoy_config_route_v3_RouteAction* route_action = |
| 753 |
envoy_config_route_v3_Route_route(route_msg); |
| 754 |
// Get the cluster or weighted_clusters in the RouteAction. |
| 755 |
if (envoy_config_route_v3_RouteAction_has_cluster(route_action)) { |
| 756 |
std::string cluster_name = UpbStringToStdString( |
| 757 |
envoy_config_route_v3_RouteAction_cluster(route_action)); |
| 758 |
if (cluster_name.empty()) { |
| 759 |
return absl::InvalidArgumentError( |
| 760 |
"RouteAction cluster contains empty cluster name."); |
| 761 |
} |
| 762 |
route.action = XdsRouteConfigResource::Route::RouteAction::ClusterName{ |
| 763 |
std::move(cluster_name)}; |
| 764 |
} else if (envoy_config_route_v3_RouteAction_has_weighted_clusters( |
| 765 |
route_action)) { |
| 766 |
std::vector<XdsRouteConfigResource::Route::RouteAction::ClusterWeight> |
| 767 |
action_weighted_clusters; |
| 768 |
const envoy_config_route_v3_WeightedCluster* weighted_cluster = |
| 769 |
envoy_config_route_v3_RouteAction_weighted_clusters(route_action); |
| 770 |
uint32_t total_weight = 100; |
| 771 |
const google_protobuf_UInt32Value* weight = |
| 772 |
envoy_config_route_v3_WeightedCluster_total_weight(weighted_cluster); |
| 773 |
if (weight != nullptr) { |
| 774 |
total_weight = google_protobuf_UInt32Value_value(weight); |
| 775 |
} |
| 776 |
size_t clusters_size; |
| 777 |
const envoy_config_route_v3_WeightedCluster_ClusterWeight* const* clusters = |
| 778 |
envoy_config_route_v3_WeightedCluster_clusters(weighted_cluster, |
| 779 |
&clusters_size); |
| 780 |
uint32_t sum_of_weights = 0; |
| 781 |
for (size_t j = 0; j < clusters_size; ++j) { |
| 782 |
const envoy_config_route_v3_WeightedCluster_ClusterWeight* |
| 783 |
cluster_weight = clusters[j]; |
| 784 |
XdsRouteConfigResource::Route::RouteAction::ClusterWeight cluster; |
| 785 |
cluster.name = UpbStringToStdString( |
| 786 |
envoy_config_route_v3_WeightedCluster_ClusterWeight_name( |
| 787 |
cluster_weight)); |
| 788 |
if (cluster.name.empty()) { |
| 789 |
return absl::InvalidArgumentError( |
| 790 |
"RouteAction weighted_cluster cluster contains empty cluster " |
| 791 |
"name."); |
| 792 |
} |
| 793 |
const google_protobuf_UInt32Value* weight = |
| 794 |
envoy_config_route_v3_WeightedCluster_ClusterWeight_weight( |
| 795 |
cluster_weight); |
| 796 |
if (weight == nullptr) { |
| 797 |
return absl::InvalidArgumentError( |
| 798 |
"RouteAction weighted_cluster cluster missing weight"); |
| 799 |
} |
| 800 |
cluster.weight = google_protobuf_UInt32Value_value(weight); |
| 801 |
if (cluster.weight == 0) continue; |
| 802 |
sum_of_weights += cluster.weight; |
| 803 |
auto typed_per_filter_config = ParseTypedPerFilterConfig< |
| 804 |
envoy_config_route_v3_WeightedCluster_ClusterWeight, |
| 805 |
envoy_config_route_v3_WeightedCluster_ClusterWeight_TypedPerFilterConfigEntry>( |
| 806 |
context, cluster_weight, |
| 807 |
envoy_config_route_v3_WeightedCluster_ClusterWeight_typed_per_filter_config_next, |
| 808 |
envoy_config_route_v3_WeightedCluster_ClusterWeight_TypedPerFilterConfigEntry_key, |
| 809 |
envoy_config_route_v3_WeightedCluster_ClusterWeight_TypedPerFilterConfigEntry_value); |
| 810 |
if (!typed_per_filter_config.ok()) { |
| 811 |
return typed_per_filter_config.status(); |
| 812 |
} |
| 813 |
cluster.typed_per_filter_config = std::move(*typed_per_filter_config); |
| 814 |
action_weighted_clusters.emplace_back(std::move(cluster)); |
| 815 |
} |
| 816 |
if (total_weight != sum_of_weights) { |
| 817 |
return absl::InvalidArgumentError( |
| 818 |
"RouteAction weighted_cluster has incorrect total weight"); |
| 819 |
} |
| 820 |
if (action_weighted_clusters.empty()) { |
| 821 |
return absl::InvalidArgumentError( |
| 822 |
"RouteAction weighted_cluster has no valid clusters specified."); |
| 823 |
} |
| 824 |
route.action = std::move(action_weighted_clusters); |
| 825 |
} else if (XdsRlsEnabled() && |
| 826 |
envoy_config_route_v3_RouteAction_has_cluster_specifier_plugin( |
| 827 |
route_action)) { |
| 828 |
std::string plugin_name = UpbStringToStdString( |
| 829 |
envoy_config_route_v3_RouteAction_cluster_specifier_plugin( |
| 830 |
route_action)); |
| 831 |
if (plugin_name.empty()) { |
| 832 |
return absl::InvalidArgumentError( |
| 833 |
"RouteAction cluster contains empty cluster specifier plugin name."); |
| 834 |
} |
| 835 |
auto it = cluster_specifier_plugin_map.find(plugin_name); |
| 836 |
if (it == cluster_specifier_plugin_map.end()) { |
| 837 |
return absl::InvalidArgumentError( |
| 838 |
absl::StrCat("RouteAction cluster contains cluster specifier plugin " |
| 839 |
"name not configured: ", |
| 840 |
plugin_name)); |
| 841 |
} |
| 842 |
if (it->second.empty()) *ignore_route = true; |
| 843 |
route.action = |
| 844 |
XdsRouteConfigResource::Route::RouteAction::ClusterSpecifierPluginName{ |
| 845 |
std::move(plugin_name)}; |
| 846 |
} else { |
| 847 |
// No cluster or weighted_clusters or plugin found in RouteAction, ignore |
| 848 |
// this route. |
| 849 |
*ignore_route = true; |
| 850 |
} |
| 851 |
if (!*ignore_route) { |
| 852 |
const envoy_config_route_v3_RouteAction_MaxStreamDuration* |
| 853 |
max_stream_duration = |
| 854 |
envoy_config_route_v3_RouteAction_max_stream_duration(route_action); |
| 855 |
if (max_stream_duration != nullptr) { |
| 856 |
const google_protobuf_Duration* duration = |
| 857 |
envoy_config_route_v3_RouteAction_MaxStreamDuration_grpc_timeout_header_max( |
| 858 |
max_stream_duration); |
| 859 |
if (duration == nullptr) { |
| 860 |
duration = |
| 861 |
envoy_config_route_v3_RouteAction_MaxStreamDuration_max_stream_duration( |
| 862 |
max_stream_duration); |
| 863 |
} |
| 864 |
if (duration != nullptr) { |
| 865 |
ValidationErrors validation_errors; |
| 866 |
route.max_stream_duration = ParseDuration(duration, &validation_errors); |
| 867 |
if (!validation_errors.ok()) { |
| 868 |
return validation_errors.status("max_stream_duration"); |
| 869 |
} |
| 870 |
} |
| 871 |
} |
| 872 |
} |
| 873 |
// Get HashPolicy from RouteAction |
| 874 |
size_t size = 0; |
| 875 |
const envoy_config_route_v3_RouteAction_HashPolicy* const* hash_policies = |
| 876 |
envoy_config_route_v3_RouteAction_hash_policy(route_action, &size); |
| 877 |
for (size_t i = 0; i < size; ++i) { |
| 878 |
const envoy_config_route_v3_RouteAction_HashPolicy* hash_policy = |
| 879 |
hash_policies[i]; |
| 880 |
XdsRouteConfigResource::Route::RouteAction::HashPolicy policy; |
| 881 |
policy.terminal = |
| 882 |
envoy_config_route_v3_RouteAction_HashPolicy_terminal(hash_policy); |
| 883 |
const envoy_config_route_v3_RouteAction_HashPolicy_Header* header; |
| 884 |
const envoy_config_route_v3_RouteAction_HashPolicy_FilterState* |
| 885 |
filter_state; |
| 886 |
if ((header = envoy_config_route_v3_RouteAction_HashPolicy_header( |
| 887 |
hash_policy)) != nullptr) { |
| 888 |
policy.type = |
| 889 |
XdsRouteConfigResource::Route::RouteAction::HashPolicy::Type::HEADER; |
| 890 |
policy.header_name = UpbStringToStdString( |
| 891 |
envoy_config_route_v3_RouteAction_HashPolicy_Header_header_name( |
| 892 |
header)); |
| 893 |
const struct envoy_type_matcher_v3_RegexMatchAndSubstitute* |
| 894 |
regex_rewrite = |
| 895 |
envoy_config_route_v3_RouteAction_HashPolicy_Header_regex_rewrite( |
| 896 |
header); |
| 897 |
if (regex_rewrite != nullptr) { |
| 898 |
const envoy_type_matcher_v3_RegexMatcher* regex_matcher = |
| 899 |
envoy_type_matcher_v3_RegexMatchAndSubstitute_pattern( |
| 900 |
regex_rewrite); |
| 901 |
if (regex_matcher == nullptr) { |
| 902 |
gpr_log( |
| 903 |
GPR_DEBUG, |
| 904 |
"RouteAction HashPolicy contains policy specifier Header with " |
| 905 |
"RegexMatchAndSubstitution but RegexMatcher pattern is " |
| 906 |
"missing"); |
| 907 |
continue; |
| 908 |
} |
| 909 |
RE2::Options options; |
| 910 |
policy.regex = std::make_unique<RE2>( |
| 911 |
UpbStringToStdString( |
| 912 |
envoy_type_matcher_v3_RegexMatcher_regex(regex_matcher)), |
| 913 |
options); |
| 914 |
if (!policy.regex->ok()) { |
| 915 |
gpr_log( |
| 916 |
GPR_DEBUG, |
| 917 |
"RouteAction HashPolicy contains policy specifier Header with " |
| 918 |
"RegexMatchAndSubstitution but RegexMatcher pattern does not " |
| 919 |
"compile"); |
| 920 |
continue; |
| 921 |
} |
| 922 |
policy.regex_substitution = UpbStringToStdString( |
| 923 |
envoy_type_matcher_v3_RegexMatchAndSubstitute_substitution( |
| 924 |
regex_rewrite)); |
| 925 |
} |
| 926 |
} else if ((filter_state = |
| 927 |
envoy_config_route_v3_RouteAction_HashPolicy_filter_state( |
| 928 |
hash_policy)) != nullptr) { |
| 929 |
std::string key = UpbStringToStdString( |
| 930 |
envoy_config_route_v3_RouteAction_HashPolicy_FilterState_key( |
| 931 |
filter_state)); |
| 932 |
if (key == "io.grpc.channel_id") { |
| 933 |
policy.type = XdsRouteConfigResource::Route::RouteAction::HashPolicy:: |
| 934 |
Type::CHANNEL_ID; |
| 935 |
} else { |
| 936 |
gpr_log(GPR_DEBUG, |
| 937 |
"RouteAction HashPolicy contains policy specifier " |
| 938 |
"FilterState but " |
| 939 |
"key is not io.grpc.channel_id."); |
| 940 |
continue; |
| 941 |
} |
| 942 |
} else { |
| 943 |
gpr_log(GPR_DEBUG, |
| 944 |
"RouteAction HashPolicy contains unsupported policy specifier."); |
| 945 |
continue; |
| 946 |
} |
| 947 |
route.hash_policies.emplace_back(std::move(policy)); |
| 948 |
} |
| 949 |
// Get retry policy |
| 950 |
const envoy_config_route_v3_RetryPolicy* retry_policy = |
| 951 |
envoy_config_route_v3_RouteAction_retry_policy(route_action); |
| 952 |
if (retry_policy != nullptr) { |
| 953 |
absl::optional<XdsRouteConfigResource::RetryPolicy> retry; |
| 954 |
absl::Status status = RetryPolicyParse(context, retry_policy, &retry); |
| 955 |
if (!status.ok()) return status; |
| 956 |
route.retry_policy = retry; |
| 957 |
} |
| 958 |
return route; |
| 959 |
} |
| 960 |
|
| 961 |
} // namespace |
| 962 |
|
| 963 |
absl::StatusOr<XdsRouteConfigResource> XdsRouteConfigResource::Parse( |
| 964 |
const XdsResourceType::DecodeContext& context, |
| 965 |
const envoy_config_route_v3_RouteConfiguration* route_config) { |
| 966 |
XdsRouteConfigResource rds_update; |
| 967 |
// Get the cluster spcifier plugins |
| 968 |
if (XdsRlsEnabled()) { |
| 969 |
auto cluster_specifier_plugin_map = |
| 970 |
ClusterSpecifierPluginParse(context, route_config); |
| 971 |
if (!cluster_specifier_plugin_map.ok()) { |
| 972 |
return cluster_specifier_plugin_map.status(); |
| 973 |
} |
| 974 |
rds_update.cluster_specifier_plugin_map = |
| 975 |
std::move(*cluster_specifier_plugin_map); |
| 976 |
} |
| 977 |
// Get the virtual hosts. |
| 978 |
size_t num_virtual_hosts; |
| 979 |
const envoy_config_route_v3_VirtualHost* const* virtual_hosts = |
| 980 |
envoy_config_route_v3_RouteConfiguration_virtual_hosts( |
| 981 |
route_config, &num_virtual_hosts); |
| 982 |
for (size_t i = 0; i < num_virtual_hosts; ++i) { |
| 983 |
rds_update.virtual_hosts.emplace_back(); |
| 984 |
XdsRouteConfigResource::VirtualHost& vhost = |
| 985 |
rds_update.virtual_hosts.back(); |
| 986 |
// Parse domains. |
| 987 |
size_t domain_size; |
| 988 |
upb_StringView const* domains = envoy_config_route_v3_VirtualHost_domains( |
| 989 |
virtual_hosts[i], &domain_size); |
| 990 |
for (size_t j = 0; j < domain_size; ++j) { |
| 991 |
std::string domain_pattern = UpbStringToStdString(domains[j]); |
| 992 |
if (!XdsRouting::IsValidDomainPattern(domain_pattern)) { |
| 993 |
return absl::InvalidArgumentError( |
| 994 |
absl::StrCat("Invalid domain pattern \"", domain_pattern, "\".")); |
| 995 |
} |
| 996 |
vhost.domains.emplace_back(std::move(domain_pattern)); |
| 997 |
} |
| 998 |
if (vhost.domains.empty()) { |
| 999 |
return absl::InvalidArgumentError("VirtualHost has no domains"); |
| 1000 |
} |
| 1001 |
// Parse typed_per_filter_config. |
| 1002 |
auto typed_per_filter_config = ParseTypedPerFilterConfig< |
| 1003 |
envoy_config_route_v3_VirtualHost, |
| 1004 |
envoy_config_route_v3_VirtualHost_TypedPerFilterConfigEntry>( |
| 1005 |
context, virtual_hosts[i], |
| 1006 |
envoy_config_route_v3_VirtualHost_typed_per_filter_config_next, |
| 1007 |
envoy_config_route_v3_VirtualHost_TypedPerFilterConfigEntry_key, |
| 1008 |
envoy_config_route_v3_VirtualHost_TypedPerFilterConfigEntry_value); |
| 1009 |
if (!typed_per_filter_config.ok()) { |
| 1010 |
return typed_per_filter_config.status(); |
| 1011 |
} |
| 1012 |
vhost.typed_per_filter_config = std::move(*typed_per_filter_config); |
| 1013 |
// Parse retry policy. |
| 1014 |
absl::optional<XdsRouteConfigResource::RetryPolicy> |
| 1015 |
virtual_host_retry_policy; |
| 1016 |
const envoy_config_route_v3_RetryPolicy* retry_policy = |
| 1017 |
envoy_config_route_v3_VirtualHost_retry_policy(virtual_hosts[i]); |
| 1018 |
if (retry_policy != nullptr) { |
| 1019 |
absl::Status status = |
| 1020 |
RetryPolicyParse(context, retry_policy, &virtual_host_retry_policy); |
| 1021 |
if (!status.ok()) return status; |
| 1022 |
} |
| 1023 |
// Parse routes. |
| 1024 |
size_t num_routes; |
| 1025 |
const envoy_config_route_v3_Route* const* routes = |
| 1026 |
envoy_config_route_v3_VirtualHost_routes(virtual_hosts[i], &num_routes); |
| 1027 |
if (num_routes < 1) { |
| 1028 |
return absl::InvalidArgumentError("No route found in the virtual host."); |
| 1029 |
} |
| 1030 |
// Build a set of cluster_specifier_plugin configured to make sure each is |
| 1031 |
// actually referenced by a route action. |
| 1032 |
std::set<absl::string_view> cluster_specifier_plugins; |
| 1033 |
for (auto& plugin : rds_update.cluster_specifier_plugin_map) { |
| 1034 |
cluster_specifier_plugins.emplace(plugin.first); |
| 1035 |
} |
| 1036 |
// Loop over the whole list of routes |
| 1037 |
for (size_t j = 0; j < num_routes; ++j) { |
| 1038 |
const envoy_config_route_v3_RouteMatch* match = |
| 1039 |
envoy_config_route_v3_Route_match(routes[j]); |
| 1040 |
if (match == nullptr) { |
| 1041 |
return absl::InvalidArgumentError("Match can't be null."); |
| 1042 |
} |
| 1043 |
size_t query_parameters_size; |
| 1044 |
static_cast<void>(envoy_config_route_v3_RouteMatch_query_parameters( |
| 1045 |
match, &query_parameters_size)); |
| 1046 |
if (query_parameters_size > 0) { |
| 1047 |
continue; |
| 1048 |
} |
| 1049 |
XdsRouteConfigResource::Route route; |
| 1050 |
bool ignore_route = false; |
| 1051 |
absl::Status status = RoutePathMatchParse(match, &route, &ignore_route); |
| 1052 |
if (!status.ok()) return status; |
| 1053 |
if (ignore_route) continue; |
| 1054 |
status = RouteHeaderMatchersParse(match, &route); |
| 1055 |
if (!status.ok()) return status; |
| 1056 |
status = RouteRuntimeFractionParse(match, &route); |
| 1057 |
if (!status.ok()) return status; |
| 1058 |
if (envoy_config_route_v3_Route_has_route(routes[j])) { |
| 1059 |
route.action.emplace<XdsRouteConfigResource::Route::RouteAction>(); |
| 1060 |
auto route_action = RouteActionParse( |
| 1061 |
context, routes[j], rds_update.cluster_specifier_plugin_map, |
| 1062 |
&ignore_route); |
| 1063 |
if (!route_action.ok()) return route_action.status(); |
| 1064 |
if (ignore_route) continue; |
| 1065 |
if (route_action->retry_policy == absl::nullopt && |
| 1066 |
retry_policy != nullptr) { |
| 1067 |
route_action->retry_policy = virtual_host_retry_policy; |
| 1068 |
} |
| 1069 |
// Mark off plugins used in route action. |
| 1070 |
auto* cluster_specifier_action = |
| 1071 |
absl::get_if<XdsRouteConfigResource::Route::RouteAction:: |
| 1072 |
ClusterSpecifierPluginName>(&route_action->action); |
| 1073 |
if (cluster_specifier_action != nullptr) { |
| 1074 |
cluster_specifier_plugins.erase( |
| 1075 |
cluster_specifier_action->cluster_specifier_plugin_name); |
| 1076 |
} |
| 1077 |
route.action = std::move(*route_action); |
| 1078 |
} else if (envoy_config_route_v3_Route_has_non_forwarding_action( |
| 1079 |
routes[j])) { |
| 1080 |
route.action |
| 1081 |
.emplace<XdsRouteConfigResource::Route::NonForwardingAction>(); |
| 1082 |
} |
| 1083 |
auto typed_per_filter_config = ParseTypedPerFilterConfig< |
| 1084 |
envoy_config_route_v3_Route, |
| 1085 |
envoy_config_route_v3_Route_TypedPerFilterConfigEntry>( |
| 1086 |
context, routes[j], |
| 1087 |
envoy_config_route_v3_Route_typed_per_filter_config_next, |
| 1088 |
envoy_config_route_v3_Route_TypedPerFilterConfigEntry_key, |
| 1089 |
envoy_config_route_v3_Route_TypedPerFilterConfigEntry_value); |
| 1090 |
if (!typed_per_filter_config.ok()) { |
| 1091 |
return typed_per_filter_config.status(); |
| 1092 |
} |
| 1093 |
route.typed_per_filter_config = std::move(*typed_per_filter_config); |
| 1094 |
vhost.routes.emplace_back(std::move(route)); |
| 1095 |
} |
| 1096 |
if (vhost.routes.empty()) { |
| 1097 |
return absl::InvalidArgumentError("No valid routes specified."); |
| 1098 |
} |
| 1099 |
// For plugins not used in route action, delete from the update to prevent |
| 1100 |
// further use. |
| 1101 |
for (auto& unused_plugin : cluster_specifier_plugins) { |
| 1102 |
rds_update.cluster_specifier_plugin_map.erase(std::string(unused_plugin)); |
| 1103 |
} |
| 1104 |
} |
| 1105 |
return rds_update; |
| 1106 |
} |
| 1107 |
|
| 1108 |
// |
| 1109 |
// XdsRouteConfigResourceType |
| 1110 |
// |
| 1111 |
|
| 1112 |
namespace { |
| 1113 |
|
| 1114 |
void MaybeLogRouteConfiguration( |
| 1115 |
const XdsResourceType::DecodeContext& context, |
| 1116 |
const envoy_config_route_v3_RouteConfiguration* route_config) { |
| 1117 |
if (GRPC_TRACE_FLAG_ENABLED(*context.tracer) && |
| 1118 |
gpr_should_log(GPR_LOG_SEVERITY_DEBUG)) { |
| 1119 |
const upb_MessageDef* msg_type = |
| 1120 |
envoy_config_route_v3_RouteConfiguration_getmsgdef(context.symtab); |
| 1121 |
char buf[10240]; |
| 1122 |
upb_TextEncode(route_config, msg_type, nullptr, 0, buf, sizeof(buf)); |
| 1123 |
gpr_log(GPR_DEBUG, "[xds_client %p] RouteConfiguration: %s", context.client, |
| 1124 |
buf); |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
} // namespace |
| 1129 |
|
| 1130 |
XdsResourceType::DecodeResult XdsRouteConfigResourceType::Decode( |
| 1131 |
const XdsResourceType::DecodeContext& context, |
| 1132 |
absl::string_view serialized_resource) const { |
| 1133 |
DecodeResult result; |
| 1134 |
// Parse serialized proto. |
| 1135 |
auto* resource = envoy_config_route_v3_RouteConfiguration_parse( |
| 1136 |
serialized_resource.data(), serialized_resource.size(), context.arena); |
| 1137 |
if (resource == nullptr) { |
| 1138 |
result.resource = |
| 1139 |
absl::InvalidArgumentError("Can't parse RouteConfiguration resource."); |
| 1140 |
return result; |
| 1141 |
} |
| 1142 |
MaybeLogRouteConfiguration(context, resource); |
| 1143 |
// Validate resource. |
| 1144 |
result.name = UpbStringToStdString( |
| 1145 |
envoy_config_route_v3_RouteConfiguration_name(resource)); |
| 1146 |
auto rds_update = XdsRouteConfigResource::Parse(context, resource); |
| 1147 |
if (!rds_update.ok()) { |
| 1148 |
if (GRPC_TRACE_FLAG_ENABLED(*context.tracer)) { |
| 1149 |
gpr_log(GPR_ERROR, "[xds_client %p] invalid RouteConfiguration %s: %s", |
| 1150 |
context.client, result.name->c_str(), |
| 1151 |
rds_update.status().ToString().c_str()); |
| 1152 |
} |
| 1153 |
result.resource = rds_update.status(); |
| 1154 |
} else { |
| 1155 |
if (GRPC_TRACE_FLAG_ENABLED(*context.tracer)) { |
| 1156 |
gpr_log(GPR_INFO, "[xds_client %p] parsed RouteConfiguration %s: %s", |
| 1157 |
context.client, result.name->c_str(), |
| 1158 |
rds_update->ToString().c_str()); |
| 1159 |
} |
| 1160 |
result.resource = |
| 1161 |
std::make_unique<XdsRouteConfigResource>(std::move(*rds_update)); |
| 1162 |
} |
| 1163 |
return result; |
| 1164 |
} |
| 1165 |
|
| 1166 |
} // namespace grpc_core |