| 1 |
// |
| 2 |
// Copyright 2022 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_lb_policy_registry.h" |
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
#include <stdint.h> |
| 23 |
|
| 24 |
#include <string> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
#include "absl/strings/str_cat.h" |
| 28 |
#include "absl/types/optional.h" |
| 29 |
#include "absl/types/variant.h" |
| 30 |
#include "envoy/config/core/v3/extension.upb.h" |
| 31 |
#include "envoy/extensions/load_balancing_policies/ring_hash/v3/ring_hash.upb.h" |
| 32 |
#include "envoy/extensions/load_balancing_policies/wrr_locality/v3/wrr_locality.upb.h" |
| 33 |
#include "google/protobuf/wrappers.upb.h" |
| 34 |
|
| 35 |
#include "src/core/ext/xds/xds_common_types.h" |
| 36 |
#include "src/core/lib/config/core_configuration.h" |
| 37 |
#include "src/core/lib/gprpp/validation_errors.h" |
| 38 |
#include "src/core/lib/load_balancing/lb_policy_registry.h" |
| 39 |
|
| 40 |
namespace grpc_core { |
| 41 |
|
| 42 |
namespace { |
| 43 |
|
| 44 |
class RoundRobinLbPolicyConfigFactory |
| 45 |
: public XdsLbPolicyRegistry::ConfigFactory { |
| 46 |
public: |
| 47 |
Json::Object ConvertXdsLbPolicyConfig( |
| 48 |
const XdsLbPolicyRegistry* /*registry*/, |
| 49 |
const XdsResourceType::DecodeContext& /*context*/, |
| 50 |
absl::string_view /*configuration*/, ValidationErrors* /*errors*/, |
| 51 |
int /*recursion_depth*/) override { |
| 52 |
return Json::Object{{"round_robin", Json::Object()}}; |
| 53 |
} |
| 54 |
|
| 55 |
absl::string_view type() override { return Type(); } |
| 56 |
|
| 57 |
static absl::string_view Type() { |
| 58 |
return "envoy.extensions.load_balancing_policies.round_robin.v3.RoundRobin"; |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
class RingHashLbPolicyConfigFactory |
| 63 |
: public XdsLbPolicyRegistry::ConfigFactory { |
| 64 |
public: |
| 65 |
Json::Object ConvertXdsLbPolicyConfig( |
| 66 |
const XdsLbPolicyRegistry* /*registry*/, |
| 67 |
const XdsResourceType::DecodeContext& context, |
| 68 |
absl::string_view configuration, ValidationErrors* errors, |
| 69 |
int /*recursion_depth*/) override { |
| 70 |
const auto* resource = |
| 71 |
envoy_extensions_load_balancing_policies_ring_hash_v3_RingHash_parse( |
| 72 |
configuration.data(), configuration.size(), context.arena); |
| 73 |
if (resource == nullptr) { |
| 74 |
errors->AddError("can't decode RingHash LB policy config"); |
| 75 |
return {}; |
| 76 |
} |
| 77 |
if (envoy_extensions_load_balancing_policies_ring_hash_v3_RingHash_hash_function( |
| 78 |
resource) != |
| 79 |
envoy_extensions_load_balancing_policies_ring_hash_v3_RingHash_XX_HASH && |
| 80 |
envoy_extensions_load_balancing_policies_ring_hash_v3_RingHash_hash_function( |
| 81 |
resource) != |
| 82 |
envoy_extensions_load_balancing_policies_ring_hash_v3_RingHash_DEFAULT_HASH) { |
| 83 |
ValidationErrors::ScopedField field(errors, ".hash_function"); |
| 84 |
errors->AddError("unsupported value (must be XX_HASH)"); |
| 85 |
} |
| 86 |
uint64_t max_ring_size = 8388608; |
| 87 |
const auto* uint64_value = |
| 88 |
envoy_extensions_load_balancing_policies_ring_hash_v3_RingHash_maximum_ring_size( |
| 89 |
resource); |
| 90 |
if (uint64_value != nullptr) { |
| 91 |
max_ring_size = google_protobuf_UInt64Value_value(uint64_value); |
| 92 |
if (max_ring_size == 0 || max_ring_size > 8388608) { |
| 93 |
ValidationErrors::ScopedField field(errors, ".maximum_ring_size"); |
| 94 |
errors->AddError("value must be in the range [1, 8388608]"); |
| 95 |
} |
| 96 |
} |
| 97 |
uint64_t min_ring_size = 1024; |
| 98 |
uint64_value = |
| 99 |
envoy_extensions_load_balancing_policies_ring_hash_v3_RingHash_minimum_ring_size( |
| 100 |
resource); |
| 101 |
if (uint64_value != nullptr) { |
| 102 |
min_ring_size = google_protobuf_UInt64Value_value(uint64_value); |
| 103 |
ValidationErrors::ScopedField field(errors, ".minimum_ring_size"); |
| 104 |
if (min_ring_size == 0 || min_ring_size > 8388608) { |
| 105 |
errors->AddError("value must be in the range [1, 8388608]"); |
| 106 |
} |
| 107 |
if (min_ring_size > max_ring_size) { |
| 108 |
errors->AddError("cannot be greater than maximum_ring_size"); |
| 109 |
} |
| 110 |
} |
| 111 |
return Json::Object{ |
| 112 |
{"ring_hash_experimental", |
| 113 |
Json::Object{ |
| 114 |
{"minRingSize", min_ring_size}, |
| 115 |
{"maxRingSize", max_ring_size}, |
| 116 |
}}, |
| 117 |
}; |
| 118 |
} |
| 119 |
|
| 120 |
absl::string_view type() override { return Type(); } |
| 121 |
|
| 122 |
static absl::string_view Type() { |
| 123 |
return "envoy.extensions.load_balancing_policies.ring_hash.v3.RingHash"; |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
class WrrLocalityLbPolicyConfigFactory |
| 128 |
: public XdsLbPolicyRegistry::ConfigFactory { |
| 129 |
public: |
| 130 |
Json::Object ConvertXdsLbPolicyConfig( |
| 131 |
const XdsLbPolicyRegistry* registry, |
| 132 |
const XdsResourceType::DecodeContext& context, |
| 133 |
absl::string_view configuration, ValidationErrors* errors, |
| 134 |
int recursion_depth) override { |
| 135 |
const auto* resource = |
| 136 |
envoy_extensions_load_balancing_policies_wrr_locality_v3_WrrLocality_parse( |
| 137 |
configuration.data(), configuration.size(), context.arena); |
| 138 |
if (resource == nullptr) { |
| 139 |
errors->AddError("can't decode WrrLocality LB policy config"); |
| 140 |
return {}; |
| 141 |
} |
| 142 |
ValidationErrors::ScopedField field(errors, ".endpoint_picking_policy"); |
| 143 |
const auto* endpoint_picking_policy = |
| 144 |
envoy_extensions_load_balancing_policies_wrr_locality_v3_WrrLocality_endpoint_picking_policy( |
| 145 |
resource); |
| 146 |
if (endpoint_picking_policy == nullptr) { |
| 147 |
errors->AddError("field not present"); |
| 148 |
return {}; |
| 149 |
} |
| 150 |
auto child_policy = registry->ConvertXdsLbPolicyConfig( |
| 151 |
context, endpoint_picking_policy, errors, recursion_depth + 1); |
| 152 |
return Json::Object{ |
| 153 |
{"xds_wrr_locality_experimental", |
| 154 |
Json::Object{{"childPolicy", std::move(child_policy)}}}}; |
| 155 |
} |
| 156 |
|
| 157 |
absl::string_view type() override { return Type(); } |
| 158 |
|
| 159 |
static absl::string_view Type() { |
| 160 |
return "envoy.extensions.load_balancing_policies.wrr_locality.v3." |
| 161 |
"WrrLocality"; |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
} // namespace |
| 166 |
|
| 167 |
// |
| 168 |
// XdsLbPolicyRegistry |
| 169 |
// |
| 170 |
|
| 171 |
XdsLbPolicyRegistry::XdsLbPolicyRegistry() { |
| 172 |
policy_config_factories_.emplace( |
| 173 |
RingHashLbPolicyConfigFactory::Type(), |
| 174 |
std::make_unique<RingHashLbPolicyConfigFactory>()); |
| 175 |
policy_config_factories_.emplace( |
| 176 |
RoundRobinLbPolicyConfigFactory::Type(), |
| 177 |
std::make_unique<RoundRobinLbPolicyConfigFactory>()); |
| 178 |
policy_config_factories_.emplace( |
| 179 |
WrrLocalityLbPolicyConfigFactory::Type(), |
| 180 |
std::make_unique<WrrLocalityLbPolicyConfigFactory>()); |
| 181 |
} |
| 182 |
|
| 183 |
Json::Array XdsLbPolicyRegistry::ConvertXdsLbPolicyConfig( |
| 184 |
const XdsResourceType::DecodeContext& context, |
| 185 |
const envoy_config_cluster_v3_LoadBalancingPolicy* lb_policy, |
| 186 |
ValidationErrors* errors, int recursion_depth) const { |
| 187 |
constexpr int kMaxRecursionDepth = 16; |
| 188 |
if (recursion_depth >= kMaxRecursionDepth) { |
| 189 |
errors->AddError( |
| 190 |
absl::StrCat("exceeded max recursion depth of ", kMaxRecursionDepth)); |
| 191 |
return {}; |
| 192 |
} |
| 193 |
const size_t original_error_size = errors->size(); |
| 194 |
size_t size = 0; |
| 195 |
const auto* policies = |
| 196 |
envoy_config_cluster_v3_LoadBalancingPolicy_policies(lb_policy, &size); |
| 197 |
for (size_t i = 0; i < size; ++i) { |
| 198 |
ValidationErrors::ScopedField field( |
| 199 |
errors, absl::StrCat(".policies[", i, "].typed_extension_config")); |
| 200 |
const auto* typed_extension_config = |
| 201 |
envoy_config_cluster_v3_LoadBalancingPolicy_Policy_typed_extension_config( |
| 202 |
policies[i]); |
| 203 |
if (typed_extension_config == nullptr) { |
| 204 |
errors->AddError("field not present"); |
| 205 |
return {}; |
| 206 |
} |
| 207 |
ValidationErrors::ScopedField field2(errors, ".typed_config"); |
| 208 |
const auto* typed_config = |
| 209 |
envoy_config_core_v3_TypedExtensionConfig_typed_config( |
| 210 |
typed_extension_config); |
| 211 |
if (typed_config == nullptr) { |
| 212 |
errors->AddError("field not present"); |
| 213 |
return {}; |
| 214 |
} |
| 215 |
auto extension = ExtractXdsExtension(context, typed_config, errors); |
| 216 |
if (!extension.has_value()) return {}; |
| 217 |
// Check for registered LB policy type. |
| 218 |
absl::string_view* serialized_value = |
| 219 |
absl::get_if<absl::string_view>(&extension->value); |
| 220 |
if (serialized_value != nullptr) { |
| 221 |
auto config_factory_it = policy_config_factories_.find(extension->type); |
| 222 |
if (config_factory_it != policy_config_factories_.end()) { |
| 223 |
return Json::Array{config_factory_it->second->ConvertXdsLbPolicyConfig( |
| 224 |
this, context, *serialized_value, errors, recursion_depth)}; |
| 225 |
} |
| 226 |
} |
| 227 |
// Check for custom LB policy type. |
| 228 |
Json* json = absl::get_if<Json>(&extension->value); |
| 229 |
if (json != nullptr && |
| 230 |
CoreConfiguration::Get().lb_policy_registry().LoadBalancingPolicyExists( |
| 231 |
extension->type, nullptr)) { |
| 232 |
return Json::Array{ |
| 233 |
Json::Object{{std::string(extension->type), std::move(*json)}}}; |
| 234 |
} |
| 235 |
// Unsupported type. Continue to next entry. |
| 236 |
} |
| 237 |
if (original_error_size == errors->size()) { |
| 238 |
errors->AddError("no supported load balancing policy config found"); |
| 239 |
} |
| 240 |
return {}; |
| 241 |
} |
| 242 |
|
| 243 |
} // namespace grpc_core |