| 1 |
// |
| 2 |
// |
| 3 |
// Copyright 2020 gRPC authors. |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
// you may not use this file except in compliance with the License. |
| 7 |
// You may obtain a copy of the License at |
| 8 |
// |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
// |
| 11 |
// Unless required by applicable law or agreed to in writing, software |
| 12 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
// See the License for the specific language governing permissions and |
| 15 |
// limitations under the License. |
| 16 |
// |
| 17 |
// |
| 18 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <map> |
| 25 |
#include <memory> |
| 26 |
#include <set> |
| 27 |
#include <string> |
| 28 |
#include <utility> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
#include "absl/base/thread_annotations.h" |
| 32 |
#include "absl/status/status.h" |
| 33 |
#include "absl/status/statusor.h" |
| 34 |
#include "absl/strings/match.h" |
| 35 |
#include "absl/strings/numbers.h" |
| 36 |
#include "absl/strings/str_cat.h" |
| 37 |
#include "absl/strings/str_join.h" |
| 38 |
#include "absl/strings/str_replace.h" |
| 39 |
#include "absl/strings/string_view.h" |
| 40 |
#include "absl/types/optional.h" |
| 41 |
#include "absl/types/variant.h" |
| 42 |
|
| 43 |
#include <grpc/grpc.h> |
| 44 |
#include <grpc/grpc_security.h> |
| 45 |
#include <grpc/impl/codegen/grpc_types.h> |
| 46 |
#include <grpc/slice.h> |
| 47 |
#include <grpc/status.h> |
| 48 |
#include <grpc/support/log.h> |
| 49 |
|
| 50 |
#include "src/core/ext/filters/server_config_selector/server_config_selector.h" |
| 51 |
#include "src/core/ext/filters/server_config_selector/server_config_selector_filter.h" |
| 52 |
#include "src/core/ext/xds/certificate_provider_store.h" |
| 53 |
#include "src/core/ext/xds/xds_bootstrap_grpc.h" |
| 54 |
#include "src/core/ext/xds/xds_certificate_provider.h" |
| 55 |
#include "src/core/ext/xds/xds_channel_stack_modifier.h" |
| 56 |
#include "src/core/ext/xds/xds_client_grpc.h" |
| 57 |
#include "src/core/ext/xds/xds_common_types.h" |
| 58 |
#include "src/core/ext/xds/xds_http_filters.h" |
| 59 |
#include "src/core/ext/xds/xds_listener.h" |
| 60 |
#include "src/core/ext/xds/xds_route_config.h" |
| 61 |
#include "src/core/ext/xds/xds_routing.h" |
| 62 |
#include "src/core/lib/address_utils/parse_address.h" |
| 63 |
#include "src/core/lib/address_utils/sockaddr_utils.h" |
| 64 |
#include "src/core/lib/channel/channel_args.h" |
| 65 |
#include "src/core/lib/channel/channel_args_preconditioning.h" |
| 66 |
#include "src/core/lib/channel/channel_fwd.h" |
| 67 |
#include "src/core/lib/config/core_configuration.h" |
| 68 |
#include "src/core/lib/debug/trace.h" |
| 69 |
#include "src/core/lib/gprpp/debug_location.h" |
| 70 |
#include "src/core/lib/gprpp/host_port.h" |
| 71 |
#include "src/core/lib/gprpp/match.h" |
| 72 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 73 |
#include "src/core/lib/gprpp/status_helper.h" |
| 74 |
#include "src/core/lib/gprpp/sync.h" |
| 75 |
#include "src/core/lib/gprpp/unique_type_name.h" |
| 76 |
#include "src/core/lib/iomgr/endpoint.h" |
| 77 |
#include "src/core/lib/iomgr/error.h" |
| 78 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 79 |
#include "src/core/lib/iomgr/iomgr_fwd.h" |
| 80 |
#include "src/core/lib/iomgr/resolved_address.h" |
| 81 |
#include "src/core/lib/iomgr/sockaddr.h" |
| 82 |
#include "src/core/lib/iomgr/socket_utils.h" |
| 83 |
#include "src/core/lib/security/credentials/credentials.h" |
| 84 |
#include "src/core/lib/security/credentials/tls/grpc_tls_certificate_distributor.h" |
| 85 |
#include "src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.h" |
| 86 |
#include "src/core/lib/security/credentials/xds/xds_credentials.h" |
| 87 |
#include "src/core/lib/service_config/service_config.h" |
| 88 |
#include "src/core/lib/service_config/service_config_impl.h" |
| 89 |
#include "src/core/lib/surface/api_trace.h" |
| 90 |
#include "src/core/lib/surface/server.h" |
| 91 |
#include "src/core/lib/transport/metadata_batch.h" |
| 92 |
#include "src/core/lib/uri/uri_parser.h" |
| 93 |
|
| 94 |
namespace grpc_core { |
| 95 |
namespace { |
| 96 |
|
| 97 |
TraceFlag grpc_xds_server_config_fetcher_trace(false, |
| 98 |
"xds_server_config_fetcher"); |
| 99 |
|
| 100 |
// A server config fetcher that fetches the information for configuring server |
| 101 |
// listeners from the xDS control plane. |
| 102 |
class XdsServerConfigFetcher : public grpc_server_config_fetcher { |
| 103 |
public: |
| 104 |
XdsServerConfigFetcher(RefCountedPtr<GrpcXdsClient> xds_client, |
| 105 |
grpc_server_xds_status_notifier notifier); |
| 106 |
|
| 107 |
~XdsServerConfigFetcher() override { |
| 108 |
xds_client_.reset(DEBUG_LOCATION, "XdsServerConfigFetcher"); |
| 109 |
} |
| 110 |
|
| 111 |
void StartWatch(std::string listening_address, |
| 112 |
std::unique_ptr<grpc_server_config_fetcher::WatcherInterface> |
| 113 |
watcher) override; |
| 114 |
|
| 115 |
void CancelWatch( |
| 116 |
grpc_server_config_fetcher::WatcherInterface* watcher) override; |
| 117 |
|
| 118 |
// Return the interested parties from the xds client so that it can be polled. |
| 119 |
grpc_pollset_set* interested_parties() override { |
| 120 |
return xds_client_->interested_parties(); |
| 121 |
} |
| 122 |
|
| 123 |
private: |
| 124 |
class ListenerWatcher; |
| 125 |
|
| 126 |
RefCountedPtr<GrpcXdsClient> xds_client_; |
| 127 |
const grpc_server_xds_status_notifier serving_status_notifier_; |
| 128 |
Mutex mu_; |
| 129 |
std::map<grpc_server_config_fetcher::WatcherInterface*, ListenerWatcher*> |
| 130 |
listener_watchers_ ABSL_GUARDED_BY(mu_); |
| 131 |
}; |
| 132 |
|
| 133 |
// A watcher implementation for listening on LDS updates from the xDS control |
| 134 |
// plane. When a good LDS update is received, it creates a |
| 135 |
// FilterChainMatchManager object that would replace the existing (if any) |
| 136 |
// FilterChainMatchManager object after all referenced RDS resources are |
| 137 |
// fetched. Note that a good update also causes the server listener to start |
| 138 |
// listening if it isn't already. If an error LDS update is received (NACKed |
| 139 |
// resource, timeouts), the previous good FilterChainMatchManager, if any, |
| 140 |
// continues to be used. If there isn't any previous good update or if the |
| 141 |
// update received was a fatal error (resource does not exist), the server |
| 142 |
// listener is made to stop listening. |
| 143 |
class XdsServerConfigFetcher::ListenerWatcher |
| 144 |
: public XdsListenerResourceType::WatcherInterface { |
| 145 |
public: |
| 146 |
ListenerWatcher(RefCountedPtr<GrpcXdsClient> xds_client, |
| 147 |
std::unique_ptr<grpc_server_config_fetcher::WatcherInterface> |
| 148 |
server_config_watcher, |
| 149 |
grpc_server_xds_status_notifier serving_status_notifier, |
| 150 |
std::string listening_address); |
| 151 |
|
| 152 |
~ListenerWatcher() override { |
| 153 |
xds_client_.reset(DEBUG_LOCATION, "ListenerWatcher"); |
| 154 |
} |
| 155 |
|
| 156 |
void OnResourceChanged(XdsListenerResource listener) override; |
| 157 |
|
| 158 |
void OnError(absl::Status status) override; |
| 159 |
|
| 160 |
void OnResourceDoesNotExist() override; |
| 161 |
|
| 162 |
const std::string& listening_address() const { return listening_address_; } |
| 163 |
|
| 164 |
private: |
| 165 |
class FilterChainMatchManager; |
| 166 |
|
| 167 |
void OnFatalError(absl::Status status) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 168 |
|
| 169 |
// Invoked by FilterChainMatchManager that is done fetching all referenced RDS |
| 170 |
// resources. If the calling FilterChainMatchManager is the |
| 171 |
// pending_filter_chain_match_manager_, it is promoted to be the |
| 172 |
// filter_chain_match_manager_ in use. |
| 173 |
void PendingFilterChainMatchManagerReady( |
| 174 |
FilterChainMatchManager* filter_chain_match_manager) { |
| 175 |
MutexLock lock(&mu_); |
| 176 |
PendingFilterChainMatchManagerReadyLocked(filter_chain_match_manager); |
| 177 |
} |
| 178 |
void PendingFilterChainMatchManagerReadyLocked( |
| 179 |
FilterChainMatchManager* filter_chain_match_manager) |
| 180 |
ABSL_EXCLUSIVE_LOCKS_REQUIRED(&mu_); |
| 181 |
|
| 182 |
RefCountedPtr<GrpcXdsClient> xds_client_; |
| 183 |
const std::unique_ptr<grpc_server_config_fetcher::WatcherInterface> |
| 184 |
server_config_watcher_; |
| 185 |
const grpc_server_xds_status_notifier serving_status_notifier_; |
| 186 |
const std::string listening_address_; |
| 187 |
Mutex mu_; |
| 188 |
RefCountedPtr<FilterChainMatchManager> filter_chain_match_manager_ |
| 189 |
ABSL_GUARDED_BY(mu_); |
| 190 |
RefCountedPtr<FilterChainMatchManager> pending_filter_chain_match_manager_ |
| 191 |
ABSL_GUARDED_BY(mu_); |
| 192 |
}; |
| 193 |
|
| 194 |
// A connection manager used by the server listener code to inject channel args |
| 195 |
// to be used for each incoming connection. This implementation chooses the |
| 196 |
// appropriate filter chain from the xDS Listener resource and injects channel |
| 197 |
// args that configure the right mTLS certs and cause the right set of HTTP |
| 198 |
// filters to be injected. |
| 199 |
class XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager |
| 200 |
: public grpc_server_config_fetcher::ConnectionManager { |
| 201 |
public: |
| 202 |
FilterChainMatchManager(RefCountedPtr<GrpcXdsClient> xds_client, |
| 203 |
XdsListenerResource::FilterChainMap filter_chain_map, |
| 204 |
absl::optional<XdsListenerResource::FilterChainData> |
| 205 |
default_filter_chain); |
| 206 |
|
| 207 |
~FilterChainMatchManager() override { |
| 208 |
xds_client_.reset(DEBUG_LOCATION, "FilterChainMatchManager"); |
| 209 |
} |
| 210 |
|
| 211 |
absl::StatusOr<ChannelArgs> UpdateChannelArgsForConnection( |
| 212 |
const ChannelArgs& args, grpc_endpoint* tcp) override; |
| 213 |
|
| 214 |
void Orphan() override; |
| 215 |
|
| 216 |
// Invoked by ListenerWatcher to start fetching referenced RDS resources. |
| 217 |
void StartRdsWatch(RefCountedPtr<ListenerWatcher> listener_watcher) |
| 218 |
ABSL_EXCLUSIVE_LOCKS_REQUIRED(&ListenerWatcher::mu_); |
| 219 |
|
| 220 |
const XdsListenerResource::FilterChainMap& filter_chain_map() const { |
| 221 |
return filter_chain_map_; |
| 222 |
} |
| 223 |
|
| 224 |
const absl::optional<XdsListenerResource::FilterChainData>& |
| 225 |
default_filter_chain() const { |
| 226 |
return default_filter_chain_; |
| 227 |
} |
| 228 |
|
| 229 |
private: |
| 230 |
struct CertificateProviders { |
| 231 |
// We need to save our own refs to the root and instance certificate |
| 232 |
// providers since the xds certificate provider just stores a ref to their |
| 233 |
// distributors. |
| 234 |
RefCountedPtr<grpc_tls_certificate_provider> root; |
| 235 |
RefCountedPtr<grpc_tls_certificate_provider> instance; |
| 236 |
RefCountedPtr<XdsCertificateProvider> xds; |
| 237 |
}; |
| 238 |
|
| 239 |
class RouteConfigWatcher; |
| 240 |
struct RdsUpdateState { |
| 241 |
RouteConfigWatcher* watcher; |
| 242 |
absl::optional<absl::StatusOr<XdsRouteConfigResource>> rds_update; |
| 243 |
}; |
| 244 |
|
| 245 |
class XdsServerConfigSelector; |
| 246 |
class StaticXdsServerConfigSelectorProvider; |
| 247 |
class DynamicXdsServerConfigSelectorProvider; |
| 248 |
|
| 249 |
absl::StatusOr<RefCountedPtr<XdsCertificateProvider>> |
| 250 |
CreateOrGetXdsCertificateProviderFromFilterChainData( |
| 251 |
const XdsListenerResource::FilterChainData* filter_chain); |
| 252 |
|
| 253 |
// Helper functions invoked by RouteConfigWatcher when there are updates to |
| 254 |
// RDS resources. |
| 255 |
void OnRouteConfigChanged(const std::string& resource_name, |
| 256 |
XdsRouteConfigResource route_config); |
| 257 |
void OnError(const std::string& resource_name, absl::Status status); |
| 258 |
void OnResourceDoesNotExist(const std::string& resource_name); |
| 259 |
|
| 260 |
RefCountedPtr<GrpcXdsClient> xds_client_; |
| 261 |
// This ref is only kept around till the FilterChainMatchManager becomes |
| 262 |
// ready. |
| 263 |
RefCountedPtr<ListenerWatcher> listener_watcher_; |
| 264 |
XdsListenerResource::FilterChainMap filter_chain_map_; |
| 265 |
absl::optional<XdsListenerResource::FilterChainData> default_filter_chain_; |
| 266 |
Mutex mu_; |
| 267 |
size_t rds_resources_yet_to_fetch_ ABSL_GUARDED_BY(mu_) = 0; |
| 268 |
std::map<std::string /* resource_name */, RdsUpdateState> rds_map_ |
| 269 |
ABSL_GUARDED_BY(mu_); |
| 270 |
std::map<const XdsListenerResource::FilterChainData*, CertificateProviders> |
| 271 |
certificate_providers_map_ ABSL_GUARDED_BY(mu_); |
| 272 |
}; |
| 273 |
|
| 274 |
// A watcher implementation for listening on RDS updates referenced to by a |
| 275 |
// FilterChainMatchManager object. After all referenced RDS resources are |
| 276 |
// fetched (errors are allowed), the FilterChainMatchManager tries to replace |
| 277 |
// the current object. The watcher continues to update the referenced RDS |
| 278 |
// resources so that new XdsServerConfigSelectorProvider objects are created |
| 279 |
// with the latest updates and new connections do not need to wait for the RDS |
| 280 |
// resources to be fetched. |
| 281 |
class XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 282 |
RouteConfigWatcher : public XdsRouteConfigResourceType::WatcherInterface { |
| 283 |
public: |
| 284 |
RouteConfigWatcher( |
| 285 |
std::string resource_name, |
| 286 |
WeakRefCountedPtr<FilterChainMatchManager> filter_chain_match_manager) |
| 287 |
: resource_name_(std::move(resource_name)), |
| 288 |
filter_chain_match_manager_(std::move(filter_chain_match_manager)) {} |
| 289 |
|
| 290 |
void OnResourceChanged(XdsRouteConfigResource route_config) override { |
| 291 |
filter_chain_match_manager_->OnRouteConfigChanged(resource_name_, |
| 292 |
std::move(route_config)); |
| 293 |
} |
| 294 |
|
| 295 |
void OnError(absl::Status status) override { |
| 296 |
filter_chain_match_manager_->OnError(resource_name_, status); |
| 297 |
} |
| 298 |
|
| 299 |
void OnResourceDoesNotExist() override { |
| 300 |
filter_chain_match_manager_->OnResourceDoesNotExist(resource_name_); |
| 301 |
} |
| 302 |
|
| 303 |
private: |
| 304 |
std::string resource_name_; |
| 305 |
WeakRefCountedPtr<FilterChainMatchManager> filter_chain_match_manager_; |
| 306 |
}; |
| 307 |
|
| 308 |
// An implementation of ServerConfigSelector used by |
| 309 |
// StaticXdsServerConfigSelectorProvider and |
| 310 |
// DynamicXdsServerConfigSelectorProvider to parse the RDS update and get |
| 311 |
// per-call configuration based on incoming metadata. |
| 312 |
class XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 313 |
XdsServerConfigSelector : public ServerConfigSelector { |
| 314 |
public: |
| 315 |
static absl::StatusOr<RefCountedPtr<XdsServerConfigSelector>> Create( |
| 316 |
const XdsHttpFilterRegistry& http_filter_registry, |
| 317 |
XdsRouteConfigResource rds_update, |
| 318 |
const std::vector<XdsListenerResource::HttpConnectionManager::HttpFilter>& |
| 319 |
http_filters); |
| 320 |
~XdsServerConfigSelector() override = default; |
| 321 |
|
| 322 |
CallConfig GetCallConfig(grpc_metadata_batch* metadata) override; |
| 323 |
|
| 324 |
private: |
| 325 |
struct VirtualHost { |
| 326 |
struct Route { |
| 327 |
// true if an action other than kNonForwardingAction is configured. |
| 328 |
bool unsupported_action; |
| 329 |
XdsRouteConfigResource::Route::Matchers matchers; |
| 330 |
RefCountedPtr<ServiceConfig> method_config; |
| 331 |
}; |
| 332 |
|
| 333 |
class RouteListIterator : public XdsRouting::RouteListIterator { |
| 334 |
public: |
| 335 |
explicit RouteListIterator(const std::vector<Route>* routes) |
| 336 |
: routes_(routes) {} |
| 337 |
|
| 338 |
size_t Size() const override { return routes_->size(); } |
| 339 |
|
| 340 |
const XdsRouteConfigResource::Route::Matchers& GetMatchersForRoute( |
| 341 |
size_t index) const override { |
| 342 |
return (*routes_)[index].matchers; |
| 343 |
} |
| 344 |
|
| 345 |
private: |
| 346 |
const std::vector<Route>* routes_; |
| 347 |
}; |
| 348 |
|
| 349 |
std::vector<std::string> domains; |
| 350 |
std::vector<Route> routes; |
| 351 |
}; |
| 352 |
|
| 353 |
class VirtualHostListIterator : public XdsRouting::VirtualHostListIterator { |
| 354 |
public: |
| 355 |
explicit VirtualHostListIterator( |
| 356 |
const std::vector<VirtualHost>* virtual_hosts) |
| 357 |
: virtual_hosts_(virtual_hosts) {} |
| 358 |
|
| 359 |
size_t Size() const override { return virtual_hosts_->size(); } |
| 360 |
|
| 361 |
const std::vector<std::string>& GetDomainsForVirtualHost( |
| 362 |
size_t index) const override { |
| 363 |
return (*virtual_hosts_)[index].domains; |
| 364 |
} |
| 365 |
|
| 366 |
private: |
| 367 |
const std::vector<VirtualHost>* virtual_hosts_; |
| 368 |
}; |
| 369 |
|
| 370 |
std::vector<VirtualHost> virtual_hosts_; |
| 371 |
}; |
| 372 |
|
| 373 |
// An XdsServerConfigSelectorProvider implementation for when the |
| 374 |
// RouteConfiguration is available inline. |
| 375 |
class XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 376 |
StaticXdsServerConfigSelectorProvider |
| 377 |
: public ServerConfigSelectorProvider { |
| 378 |
public: |
| 379 |
StaticXdsServerConfigSelectorProvider( |
| 380 |
RefCountedPtr<GrpcXdsClient> xds_client, |
| 381 |
absl::StatusOr<XdsRouteConfigResource> static_resource, |
| 382 |
std::vector<XdsListenerResource::HttpConnectionManager::HttpFilter> |
| 383 |
http_filters) |
| 384 |
: xds_client_(std::move(xds_client)), |
| 385 |
static_resource_(std::move(static_resource)), |
| 386 |
http_filters_(std::move(http_filters)) {} |
| 387 |
|
| 388 |
~StaticXdsServerConfigSelectorProvider() override { |
| 389 |
xds_client_.reset(DEBUG_LOCATION, "StaticXdsServerConfigSelectorProvider"); |
| 390 |
} |
| 391 |
|
| 392 |
absl::StatusOr<RefCountedPtr<ServerConfigSelector>> Watch( |
| 393 |
std::unique_ptr<ServerConfigSelectorProvider::ServerConfigSelectorWatcher> |
| 394 |
watcher) override { |
| 395 |
GPR_ASSERT(watcher_ == nullptr); |
| 396 |
watcher_ = std::move(watcher); |
| 397 |
if (!static_resource_.ok()) { |
| 398 |
return static_resource_.status(); |
| 399 |
} |
| 400 |
return XdsServerConfigSelector::Create( |
| 401 |
static_cast<const GrpcXdsBootstrap&>(xds_client_->bootstrap()) |
| 402 |
.http_filter_registry(), |
| 403 |
static_resource_.value(), http_filters_); |
| 404 |
} |
| 405 |
|
| 406 |
void Orphan() override {} |
| 407 |
|
| 408 |
void CancelWatch() override { watcher_.reset(); } |
| 409 |
|
| 410 |
private: |
| 411 |
RefCountedPtr<GrpcXdsClient> xds_client_; |
| 412 |
absl::StatusOr<XdsRouteConfigResource> static_resource_; |
| 413 |
std::vector<XdsListenerResource::HttpConnectionManager::HttpFilter> |
| 414 |
http_filters_; |
| 415 |
std::unique_ptr<ServerConfigSelectorProvider::ServerConfigSelectorWatcher> |
| 416 |
watcher_; |
| 417 |
}; |
| 418 |
|
| 419 |
// An XdsServerConfigSelectorProvider implementation for when the |
| 420 |
// RouteConfiguration is to be fetched separately via RDS. |
| 421 |
class XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 422 |
DynamicXdsServerConfigSelectorProvider |
| 423 |
: public ServerConfigSelectorProvider { |
| 424 |
public: |
| 425 |
DynamicXdsServerConfigSelectorProvider( |
| 426 |
RefCountedPtr<GrpcXdsClient> xds_client, std::string resource_name, |
| 427 |
absl::StatusOr<XdsRouteConfigResource> initial_resource, |
| 428 |
std::vector<XdsListenerResource::HttpConnectionManager::HttpFilter> |
| 429 |
http_filters); |
| 430 |
|
| 431 |
~DynamicXdsServerConfigSelectorProvider() override { |
| 432 |
xds_client_.reset(DEBUG_LOCATION, "DynamicXdsServerConfigSelectorProvider"); |
| 433 |
} |
| 434 |
|
| 435 |
void Orphan() override; |
| 436 |
|
| 437 |
absl::StatusOr<RefCountedPtr<ServerConfigSelector>> Watch( |
| 438 |
std::unique_ptr<ServerConfigSelectorProvider::ServerConfigSelectorWatcher> |
| 439 |
watcher) override; |
| 440 |
void CancelWatch() override; |
| 441 |
|
| 442 |
private: |
| 443 |
class RouteConfigWatcher; |
| 444 |
|
| 445 |
void OnRouteConfigChanged(XdsRouteConfigResource rds_update); |
| 446 |
void OnError(absl::Status status); |
| 447 |
void OnResourceDoesNotExist(); |
| 448 |
|
| 449 |
RefCountedPtr<GrpcXdsClient> xds_client_; |
| 450 |
std::string resource_name_; |
| 451 |
std::vector<XdsListenerResource::HttpConnectionManager::HttpFilter> |
| 452 |
http_filters_; |
| 453 |
RouteConfigWatcher* route_config_watcher_ = nullptr; |
| 454 |
Mutex mu_; |
| 455 |
std::unique_ptr<ServerConfigSelectorProvider::ServerConfigSelectorWatcher> |
| 456 |
watcher_ ABSL_GUARDED_BY(mu_); |
| 457 |
absl::StatusOr<XdsRouteConfigResource> resource_ ABSL_GUARDED_BY(mu_); |
| 458 |
}; |
| 459 |
|
| 460 |
// A watcher implementation for updating the RDS resource used by |
| 461 |
// DynamicXdsServerConfigSelectorProvider |
| 462 |
class XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 463 |
DynamicXdsServerConfigSelectorProvider::RouteConfigWatcher |
| 464 |
: public XdsRouteConfigResourceType::WatcherInterface { |
| 465 |
public: |
| 466 |
explicit RouteConfigWatcher( |
| 467 |
WeakRefCountedPtr<DynamicXdsServerConfigSelectorProvider> parent) |
| 468 |
: parent_(std::move(parent)) {} |
| 469 |
|
| 470 |
void OnResourceChanged(XdsRouteConfigResource route_config) override { |
| 471 |
parent_->OnRouteConfigChanged(std::move(route_config)); |
| 472 |
} |
| 473 |
|
| 474 |
void OnError(absl::Status status) override { parent_->OnError(status); } |
| 475 |
|
| 476 |
void OnResourceDoesNotExist() override { parent_->OnResourceDoesNotExist(); } |
| 477 |
|
| 478 |
private: |
| 479 |
WeakRefCountedPtr<DynamicXdsServerConfigSelectorProvider> parent_; |
| 480 |
}; |
| 481 |
|
| 482 |
// |
| 483 |
// XdsServerConfigFetcher |
| 484 |
// |
| 485 |
|
| 486 |
XdsServerConfigFetcher::XdsServerConfigFetcher( |
| 487 |
RefCountedPtr<GrpcXdsClient> xds_client, |
| 488 |
grpc_server_xds_status_notifier notifier) |
| 489 |
: xds_client_(std::move(xds_client)), serving_status_notifier_(notifier) { |
| 490 |
GPR_ASSERT(xds_client_ != nullptr); |
| 491 |
} |
| 492 |
|
| 493 |
std::string ListenerResourceName(absl::string_view resource_name_template, |
| 494 |
absl::string_view listening_address) { |
| 495 |
std::string tmp; |
| 496 |
if (absl::StartsWith(resource_name_template, "xdstp:")) { |
| 497 |
tmp = URI::PercentEncodePath(listening_address); |
| 498 |
listening_address = tmp; |
| 499 |
} |
| 500 |
return absl::StrReplaceAll(resource_name_template, |
| 501 |
{{"%s", listening_address}}); |
| 502 |
} |
| 503 |
|
| 504 |
void XdsServerConfigFetcher::StartWatch( |
| 505 |
std::string listening_address, |
| 506 |
std::unique_ptr<grpc_server_config_fetcher::WatcherInterface> watcher) { |
| 507 |
grpc_server_config_fetcher::WatcherInterface* watcher_ptr = watcher.get(); |
| 508 |
auto listener_watcher = MakeRefCounted<ListenerWatcher>( |
| 509 |
xds_client_->Ref(DEBUG_LOCATION, "ListenerWatcher"), std::move(watcher), |
| 510 |
serving_status_notifier_, listening_address); |
| 511 |
auto* listener_watcher_ptr = listener_watcher.get(); |
| 512 |
XdsListenerResourceType::StartWatch( |
| 513 |
xds_client_.get(), |
| 514 |
ListenerResourceName( |
| 515 |
static_cast<const GrpcXdsBootstrap&>(xds_client_->bootstrap()) |
| 516 |
.server_listener_resource_name_template(), |
| 517 |
listening_address), |
| 518 |
std::move(listener_watcher)); |
| 519 |
MutexLock lock(&mu_); |
| 520 |
listener_watchers_.emplace(watcher_ptr, listener_watcher_ptr); |
| 521 |
} |
| 522 |
|
| 523 |
void XdsServerConfigFetcher::CancelWatch( |
| 524 |
grpc_server_config_fetcher::WatcherInterface* watcher) { |
| 525 |
MutexLock lock(&mu_); |
| 526 |
auto it = listener_watchers_.find(watcher); |
| 527 |
if (it != listener_watchers_.end()) { |
| 528 |
// Cancel the watch on the listener before erasing |
| 529 |
XdsListenerResourceType::CancelWatch( |
| 530 |
xds_client_.get(), |
| 531 |
ListenerResourceName( |
| 532 |
static_cast<const GrpcXdsBootstrap&>(xds_client_->bootstrap()) |
| 533 |
.server_listener_resource_name_template(), |
| 534 |
it->second->listening_address()), |
| 535 |
it->second, false /* delay_unsubscription */); |
| 536 |
listener_watchers_.erase(it); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
// |
| 541 |
// XdsServerConfigFetcher::ListenerWatcher |
| 542 |
// |
| 543 |
|
| 544 |
XdsServerConfigFetcher::ListenerWatcher::ListenerWatcher( |
| 545 |
RefCountedPtr<GrpcXdsClient> xds_client, |
| 546 |
std::unique_ptr<grpc_server_config_fetcher::WatcherInterface> |
| 547 |
server_config_watcher, |
| 548 |
grpc_server_xds_status_notifier serving_status_notifier, |
| 549 |
std::string listening_address) |
| 550 |
: xds_client_(std::move(xds_client)), |
| 551 |
server_config_watcher_(std::move(server_config_watcher)), |
| 552 |
serving_status_notifier_(serving_status_notifier), |
| 553 |
listening_address_(std::move(listening_address)) {} |
| 554 |
|
| 555 |
void XdsServerConfigFetcher::ListenerWatcher::OnResourceChanged( |
| 556 |
XdsListenerResource listener) { |
| 557 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_xds_server_config_fetcher_trace)) { |
| 558 |
gpr_log(GPR_INFO, |
| 559 |
"[ListenerWatcher %p] Received LDS update from xds client %p: %s", |
| 560 |
this, xds_client_.get(), listener.ToString().c_str()); |
| 561 |
} |
| 562 |
auto* tcp_listener = |
| 563 |
absl::get_if<XdsListenerResource::TcpListener>(&listener.listener); |
| 564 |
if (tcp_listener == nullptr) { |
| 565 |
MutexLock lock(&mu_); |
| 566 |
OnFatalError( |
| 567 |
absl::FailedPreconditionError("LDS resource is not a TCP listener")); |
| 568 |
return; |
| 569 |
} |
| 570 |
if (tcp_listener->address != listening_address_) { |
| 571 |
MutexLock lock(&mu_); |
| 572 |
OnFatalError(absl::FailedPreconditionError( |
| 573 |
"Address in LDS update does not match listening address")); |
| 574 |
return; |
| 575 |
} |
| 576 |
auto new_filter_chain_match_manager = MakeRefCounted<FilterChainMatchManager>( |
| 577 |
xds_client_->Ref(DEBUG_LOCATION, "FilterChainMatchManager"), |
| 578 |
std::move(tcp_listener->filter_chain_map), |
| 579 |
std::move(tcp_listener->default_filter_chain)); |
| 580 |
MutexLock lock(&mu_); |
| 581 |
if (filter_chain_match_manager_ == nullptr || |
| 582 |
!(new_filter_chain_match_manager->filter_chain_map() == |
| 583 |
filter_chain_match_manager_->filter_chain_map() && |
| 584 |
new_filter_chain_match_manager->default_filter_chain() == |
| 585 |
filter_chain_match_manager_->default_filter_chain())) { |
| 586 |
pending_filter_chain_match_manager_ = |
| 587 |
std::move(new_filter_chain_match_manager); |
| 588 |
pending_filter_chain_match_manager_->StartRdsWatch(Ref()); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
void XdsServerConfigFetcher::ListenerWatcher::OnError(absl::Status status) { |
| 593 |
MutexLock lock(&mu_); |
| 594 |
if (filter_chain_match_manager_ != nullptr || |
| 595 |
pending_filter_chain_match_manager_ != nullptr) { |
| 596 |
gpr_log(GPR_ERROR, |
| 597 |
"ListenerWatcher:%p XdsClient reports error: %s for %s; " |
| 598 |
"ignoring in favor of existing resource", |
| 599 |
this, status.ToString().c_str(), listening_address_.c_str()); |
| 600 |
} else { |
| 601 |
if (serving_status_notifier_.on_serving_status_update != nullptr) { |
| 602 |
serving_status_notifier_.on_serving_status_update( |
| 603 |
serving_status_notifier_.user_data, listening_address_.c_str(), |
| 604 |
{GRPC_STATUS_UNAVAILABLE, status.ToString().c_str()}); |
| 605 |
} else { |
| 606 |
gpr_log(GPR_ERROR, |
| 607 |
"ListenerWatcher:%p error obtaining xDS Listener resource: %s; " |
| 608 |
"not serving on %s", |
| 609 |
this, status.ToString().c_str(), listening_address_.c_str()); |
| 610 |
} |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
void XdsServerConfigFetcher::ListenerWatcher::OnFatalError( |
| 615 |
absl::Status status) { |
| 616 |
pending_filter_chain_match_manager_.reset(); |
| 617 |
if (filter_chain_match_manager_ != nullptr) { |
| 618 |
// The server has started listening already, so we need to gracefully |
| 619 |
// stop serving. |
| 620 |
server_config_watcher_->StopServing(); |
| 621 |
filter_chain_match_manager_.reset(); |
| 622 |
} |
| 623 |
if (serving_status_notifier_.on_serving_status_update != nullptr) { |
| 624 |
serving_status_notifier_.on_serving_status_update( |
| 625 |
serving_status_notifier_.user_data, listening_address_.c_str(), |
| 626 |
{static_cast<grpc_status_code>(status.raw_code()), |
| 627 |
std::string(status.message()).c_str()}); |
| 628 |
} else { |
| 629 |
gpr_log(GPR_ERROR, |
| 630 |
"ListenerWatcher:%p Encountered fatal error %s; not serving on %s", |
| 631 |
this, status.ToString().c_str(), listening_address_.c_str()); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
void XdsServerConfigFetcher::ListenerWatcher::OnResourceDoesNotExist() { |
| 636 |
MutexLock lock(&mu_); |
| 637 |
OnFatalError(absl::NotFoundError("Requested listener does not exist")); |
| 638 |
} |
| 639 |
|
| 640 |
void XdsServerConfigFetcher::ListenerWatcher:: |
| 641 |
PendingFilterChainMatchManagerReadyLocked( |
| 642 |
XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager* |
| 643 |
filter_chain_match_manager) { |
| 644 |
if (pending_filter_chain_match_manager_ != filter_chain_match_manager) { |
| 645 |
// This FilterChainMatchManager is no longer the current pending resource. |
| 646 |
// It should get cleaned up eventually. Ignore this update. |
| 647 |
return; |
| 648 |
} |
| 649 |
// Let the logger know about the update if there was no previous good update. |
| 650 |
if (filter_chain_match_manager_ == nullptr) { |
| 651 |
if (serving_status_notifier_.on_serving_status_update != nullptr) { |
| 652 |
serving_status_notifier_.on_serving_status_update( |
| 653 |
serving_status_notifier_.user_data, listening_address_.c_str(), |
| 654 |
{GRPC_STATUS_OK, ""}); |
| 655 |
} else { |
| 656 |
gpr_log(GPR_INFO, |
| 657 |
"xDS Listener resource obtained; will start serving on %s", |
| 658 |
listening_address_.c_str()); |
| 659 |
} |
| 660 |
} |
| 661 |
// Promote the pending FilterChainMatchManager |
| 662 |
filter_chain_match_manager_ = std::move(pending_filter_chain_match_manager_); |
| 663 |
// TODO(yashykt): Right now, the server_config_watcher_ does not invoke |
| 664 |
// XdsServerConfigFetcher while holding a lock, but that might change in the |
| 665 |
// future in which case we would want to execute this update outside the |
| 666 |
// critical region through a WorkSerializer similar to XdsClient. |
| 667 |
server_config_watcher_->UpdateConnectionManager(filter_chain_match_manager_); |
| 668 |
} |
| 669 |
|
| 670 |
// |
| 671 |
// XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager |
| 672 |
// |
| 673 |
|
| 674 |
XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 675 |
FilterChainMatchManager( |
| 676 |
RefCountedPtr<GrpcXdsClient> xds_client, |
| 677 |
XdsListenerResource::FilterChainMap filter_chain_map, |
| 678 |
absl::optional<XdsListenerResource::FilterChainData> |
| 679 |
default_filter_chain) |
| 680 |
: xds_client_(std::move(xds_client)), |
| 681 |
filter_chain_map_(std::move(filter_chain_map)), |
| 682 |
default_filter_chain_(std::move(default_filter_chain)) {} |
| 683 |
|
| 684 |
void XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 685 |
StartRdsWatch(RefCountedPtr<ListenerWatcher> listener_watcher) { |
| 686 |
// Get the set of RDS resources to watch on. Also get the set of |
| 687 |
// FilterChainData so that we can reverse the list of HTTP filters since |
| 688 |
// received data moves *up* the stack in Core. |
| 689 |
std::set<std::string> resource_names; |
| 690 |
std::set<XdsListenerResource::FilterChainData*> filter_chain_data_set; |
| 691 |
for (const auto& destination_ip : filter_chain_map_.destination_ip_vector) { |
| 692 |
for (const auto& source_type : destination_ip.source_types_array) { |
| 693 |
for (const auto& source_ip : source_type) { |
| 694 |
for (const auto& source_port_pair : source_ip.ports_map) { |
| 695 |
auto* filter_chain_data = source_port_pair.second.data.get(); |
| 696 |
const auto* rds_name = absl::get_if<std::string>( |
| 697 |
&filter_chain_data->http_connection_manager.route_config); |
| 698 |
if (rds_name != nullptr) resource_names.insert(*rds_name); |
| 699 |
filter_chain_data_set.insert(filter_chain_data); |
| 700 |
} |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
if (default_filter_chain_.has_value()) { |
| 705 |
auto& hcm = default_filter_chain_->http_connection_manager; |
| 706 |
const auto* rds_name = absl::get_if<std::string>(&hcm.route_config); |
| 707 |
if (rds_name != nullptr) resource_names.insert(*rds_name); |
| 708 |
std::reverse(hcm.http_filters.begin(), hcm.http_filters.end()); |
| 709 |
} |
| 710 |
// Reverse the lists of HTTP filters in all the filter chains |
| 711 |
for (auto* filter_chain_data : filter_chain_data_set) { |
| 712 |
auto& hcm = filter_chain_data->http_connection_manager; |
| 713 |
std::reverse(hcm.http_filters.begin(), hcm.http_filters.end()); |
| 714 |
} |
| 715 |
// Start watching on referenced RDS resources |
| 716 |
struct WatcherToStart { |
| 717 |
std::string resource_name; |
| 718 |
RefCountedPtr<RouteConfigWatcher> watcher; |
| 719 |
}; |
| 720 |
std::vector<WatcherToStart> watchers_to_start; |
| 721 |
watchers_to_start.reserve(resource_names.size()); |
| 722 |
{ |
| 723 |
MutexLock lock(&mu_); |
| 724 |
for (const auto& resource_name : resource_names) { |
| 725 |
++rds_resources_yet_to_fetch_; |
| 726 |
auto route_config_watcher = |
| 727 |
MakeRefCounted<RouteConfigWatcher>(resource_name, WeakRef()); |
| 728 |
rds_map_.emplace(resource_name, RdsUpdateState{route_config_watcher.get(), |
| 729 |
absl::nullopt}); |
| 730 |
watchers_to_start.push_back( |
| 731 |
WatcherToStart{resource_name, std::move(route_config_watcher)}); |
| 732 |
} |
| 733 |
if (rds_resources_yet_to_fetch_ != 0) { |
| 734 |
listener_watcher_ = std::move(listener_watcher); |
| 735 |
listener_watcher = nullptr; |
| 736 |
} |
| 737 |
} |
| 738 |
for (auto& watcher_to_start : watchers_to_start) { |
| 739 |
XdsRouteConfigResourceType::StartWatch(xds_client_.get(), |
| 740 |
watcher_to_start.resource_name, |
| 741 |
std::move(watcher_to_start.watcher)); |
| 742 |
} |
| 743 |
// Promote this filter chain match manager if all referenced resources are |
| 744 |
// fetched. |
| 745 |
if (listener_watcher != nullptr) { |
| 746 |
listener_watcher->PendingFilterChainMatchManagerReadyLocked(this); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
void XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 751 |
Orphan() { |
| 752 |
MutexLock lock(&mu_); |
| 753 |
// Cancel the RDS watches to clear up the weak refs |
| 754 |
for (const auto& entry : rds_map_) { |
| 755 |
XdsRouteConfigResourceType::CancelWatch(xds_client_.get(), entry.first, |
| 756 |
entry.second.watcher, |
| 757 |
false /* delay_unsubscription */); |
| 758 |
} |
| 759 |
// Also give up the ref on ListenerWatcher since it won't be needed anymore |
| 760 |
listener_watcher_.reset(); |
| 761 |
} |
| 762 |
|
| 763 |
absl::StatusOr<RefCountedPtr<XdsCertificateProvider>> |
| 764 |
XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 765 |
CreateOrGetXdsCertificateProviderFromFilterChainData( |
| 766 |
const XdsListenerResource::FilterChainData* filter_chain) { |
| 767 |
MutexLock lock(&mu_); |
| 768 |
auto it = certificate_providers_map_.find(filter_chain); |
| 769 |
if (it != certificate_providers_map_.end()) { |
| 770 |
return it->second.xds; |
| 771 |
} |
| 772 |
CertificateProviders certificate_providers; |
| 773 |
// Configure root cert. |
| 774 |
absl::string_view root_provider_instance_name = |
| 775 |
filter_chain->downstream_tls_context.common_tls_context |
| 776 |
.certificate_validation_context.ca_certificate_provider_instance |
| 777 |
.instance_name; |
| 778 |
absl::string_view root_provider_cert_name = |
| 779 |
filter_chain->downstream_tls_context.common_tls_context |
| 780 |
.certificate_validation_context.ca_certificate_provider_instance |
| 781 |
.certificate_name; |
| 782 |
if (!root_provider_instance_name.empty()) { |
| 783 |
certificate_providers.root = |
| 784 |
xds_client_->certificate_provider_store() |
| 785 |
.CreateOrGetCertificateProvider(root_provider_instance_name); |
| 786 |
if (certificate_providers.root == nullptr) { |
| 787 |
return absl::NotFoundError( |
| 788 |
absl::StrCat("Certificate provider instance name: \"", |
| 789 |
root_provider_instance_name, "\" not recognized.")); |
| 790 |
} |
| 791 |
} |
| 792 |
// Configure identity cert. |
| 793 |
absl::string_view identity_provider_instance_name = |
| 794 |
filter_chain->downstream_tls_context.common_tls_context |
| 795 |
.tls_certificate_provider_instance.instance_name; |
| 796 |
absl::string_view identity_provider_cert_name = |
| 797 |
filter_chain->downstream_tls_context.common_tls_context |
| 798 |
.tls_certificate_provider_instance.certificate_name; |
| 799 |
if (!identity_provider_instance_name.empty()) { |
| 800 |
certificate_providers.instance = |
| 801 |
xds_client_->certificate_provider_store() |
| 802 |
.CreateOrGetCertificateProvider(identity_provider_instance_name); |
| 803 |
if (certificate_providers.instance == nullptr) { |
| 804 |
return absl::NotFoundError( |
| 805 |
absl::StrCat("Certificate provider instance name: \"", |
| 806 |
identity_provider_instance_name, "\" not recognized.")); |
| 807 |
} |
| 808 |
} |
| 809 |
certificate_providers.xds = MakeRefCounted<XdsCertificateProvider>(); |
| 810 |
certificate_providers.xds->UpdateRootCertNameAndDistributor( |
| 811 |
"", root_provider_cert_name, |
| 812 |
certificate_providers.root == nullptr |
| 813 |
? nullptr |
| 814 |
: certificate_providers.root->distributor()); |
| 815 |
certificate_providers.xds->UpdateIdentityCertNameAndDistributor( |
| 816 |
"", identity_provider_cert_name, |
| 817 |
certificate_providers.instance == nullptr |
| 818 |
? nullptr |
| 819 |
: certificate_providers.instance->distributor()); |
| 820 |
certificate_providers.xds->UpdateRequireClientCertificate( |
| 821 |
"", filter_chain->downstream_tls_context.require_client_certificate); |
| 822 |
auto xds_certificate_provider = certificate_providers.xds; |
| 823 |
certificate_providers_map_.emplace(filter_chain, |
| 824 |
std::move(certificate_providers)); |
| 825 |
return xds_certificate_provider; |
| 826 |
} |
| 827 |
|
| 828 |
void XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 829 |
OnRouteConfigChanged(const std::string& resource_name, |
| 830 |
XdsRouteConfigResource route_config) { |
| 831 |
RefCountedPtr<ListenerWatcher> listener_watcher; |
| 832 |
{ |
| 833 |
MutexLock lock(&mu_); |
| 834 |
auto& state = rds_map_[resource_name]; |
| 835 |
if (!state.rds_update.has_value()) { |
| 836 |
if (--rds_resources_yet_to_fetch_ == 0) { |
| 837 |
listener_watcher = std::move(listener_watcher_); |
| 838 |
} |
| 839 |
} |
| 840 |
state.rds_update = std::move(route_config); |
| 841 |
} |
| 842 |
// Promote the filter chain match manager object if all the referenced |
| 843 |
// resources are fetched. |
| 844 |
if (listener_watcher != nullptr) { |
| 845 |
listener_watcher->PendingFilterChainMatchManagerReady(this); |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
void XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager::OnError( |
| 850 |
const std::string& resource_name, absl::Status status) { |
| 851 |
RefCountedPtr<ListenerWatcher> listener_watcher; |
| 852 |
{ |
| 853 |
MutexLock lock(&mu_); |
| 854 |
auto& state = rds_map_[resource_name]; |
| 855 |
if (!state.rds_update.has_value()) { |
| 856 |
if (--rds_resources_yet_to_fetch_ == 0) { |
| 857 |
listener_watcher = std::move(listener_watcher_); |
| 858 |
} |
| 859 |
state.rds_update = status; |
| 860 |
} else { |
| 861 |
// Prefer existing good version over current errored version |
| 862 |
if (!state.rds_update->ok()) { |
| 863 |
state.rds_update = status; |
| 864 |
} |
| 865 |
} |
| 866 |
} |
| 867 |
// Promote the filter chain match manager object if all the referenced |
| 868 |
// resources are fetched. |
| 869 |
if (listener_watcher != nullptr) { |
| 870 |
listener_watcher->PendingFilterChainMatchManagerReady(this); |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
void XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 875 |
OnResourceDoesNotExist(const std::string& resource_name) { |
| 876 |
RefCountedPtr<ListenerWatcher> listener_watcher; |
| 877 |
{ |
| 878 |
MutexLock lock(&mu_); |
| 879 |
auto& state = rds_map_[resource_name]; |
| 880 |
if (!state.rds_update.has_value()) { |
| 881 |
if (--rds_resources_yet_to_fetch_ == 0) { |
| 882 |
listener_watcher = std::move(listener_watcher_); |
| 883 |
} |
| 884 |
} |
| 885 |
state.rds_update = |
| 886 |
absl::NotFoundError("Requested route config does not exist"); |
| 887 |
} |
| 888 |
// Promote the filter chain match manager object if all the referenced |
| 889 |
// resources are fetched. |
| 890 |
if (listener_watcher != nullptr) { |
| 891 |
listener_watcher->PendingFilterChainMatchManagerReady(this); |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
const XdsListenerResource::FilterChainData* FindFilterChainDataForSourcePort( |
| 896 |
const XdsListenerResource::FilterChainMap::SourcePortsMap& source_ports_map, |
| 897 |
absl::string_view port_str) { |
| 898 |
int port = 0; |
| 899 |
if (!absl::SimpleAtoi(port_str, &port)) return nullptr; |
| 900 |
auto it = source_ports_map.find(port); |
| 901 |
if (it != source_ports_map.end()) { |
| 902 |
return it->second.data.get(); |
| 903 |
} |
| 904 |
// Search for the catch-all port 0 since we didn't get a direct match |
| 905 |
it = source_ports_map.find(0); |
| 906 |
if (it != source_ports_map.end()) { |
| 907 |
return it->second.data.get(); |
| 908 |
} |
| 909 |
return nullptr; |
| 910 |
} |
| 911 |
|
| 912 |
const XdsListenerResource::FilterChainData* FindFilterChainDataForSourceIp( |
| 913 |
const XdsListenerResource::FilterChainMap::SourceIpVector& source_ip_vector, |
| 914 |
const grpc_resolved_address* source_ip, absl::string_view port) { |
| 915 |
const XdsListenerResource::FilterChainMap::SourceIp* best_match = nullptr; |
| 916 |
for (const auto& entry : source_ip_vector) { |
| 917 |
// Special case for catch-all |
| 918 |
if (!entry.prefix_range.has_value()) { |
| 919 |
if (best_match == nullptr) { |
| 920 |
best_match = &entry; |
| 921 |
} |
| 922 |
continue; |
| 923 |
} |
| 924 |
if (best_match != nullptr && best_match->prefix_range.has_value() && |
| 925 |
best_match->prefix_range->prefix_len >= |
| 926 |
entry.prefix_range->prefix_len) { |
| 927 |
continue; |
| 928 |
} |
| 929 |
if (grpc_sockaddr_match_subnet(source_ip, &entry.prefix_range->address, |
| 930 |
entry.prefix_range->prefix_len)) { |
| 931 |
best_match = &entry; |
| 932 |
} |
| 933 |
} |
| 934 |
if (best_match == nullptr) return nullptr; |
| 935 |
return FindFilterChainDataForSourcePort(best_match->ports_map, port); |
| 936 |
} |
| 937 |
|
| 938 |
bool IsLoopbackIp(const grpc_resolved_address* address) { |
| 939 |
const grpc_sockaddr* sock_addr = |
| 940 |
reinterpret_cast<const grpc_sockaddr*>(&address->addr); |
| 941 |
if (sock_addr->sa_family == GRPC_AF_INET) { |
| 942 |
const grpc_sockaddr_in* addr4 = |
| 943 |
reinterpret_cast<const grpc_sockaddr_in*>(sock_addr); |
| 944 |
if (addr4->sin_addr.s_addr == grpc_htonl(INADDR_LOOPBACK)) { |
| 945 |
return true; |
| 946 |
} |
| 947 |
} else if (sock_addr->sa_family == GRPC_AF_INET6) { |
| 948 |
const grpc_sockaddr_in6* addr6 = |
| 949 |
reinterpret_cast<const grpc_sockaddr_in6*>(sock_addr); |
| 950 |
if (memcmp(&addr6->sin6_addr, &in6addr_loopback, |
| 951 |
sizeof(in6addr_loopback)) == 0) { |
| 952 |
return true; |
| 953 |
} |
| 954 |
} |
| 955 |
return false; |
| 956 |
} |
| 957 |
|
| 958 |
const XdsListenerResource::FilterChainData* FindFilterChainDataForSourceType( |
| 959 |
const XdsListenerResource::FilterChainMap::ConnectionSourceTypesArray& |
| 960 |
source_types_array, |
| 961 |
grpc_endpoint* tcp, absl::string_view destination_ip) { |
| 962 |
auto source_uri = URI::Parse(grpc_endpoint_get_peer(tcp)); |
| 963 |
if (!source_uri.ok() || |
| 964 |
(source_uri->scheme() != "ipv4" && source_uri->scheme() != "ipv6")) { |
| 965 |
return nullptr; |
| 966 |
} |
| 967 |
std::string host; |
| 968 |
std::string port; |
| 969 |
if (!SplitHostPort(source_uri->path(), &host, &port)) { |
| 970 |
return nullptr; |
| 971 |
} |
| 972 |
auto source_addr = StringToSockaddr(host, 0); // Port doesn't matter here. |
| 973 |
if (!source_addr.ok()) { |
| 974 |
gpr_log(GPR_DEBUG, "Could not parse \"%s\" as socket address: %s", |
| 975 |
host.c_str(), source_addr.status().ToString().c_str()); |
| 976 |
return nullptr; |
| 977 |
} |
| 978 |
// Use kAny only if kSameIporLoopback and kExternal are empty |
| 979 |
if (source_types_array[static_cast<int>( |
| 980 |
XdsListenerResource::FilterChainMap:: |
| 981 |
ConnectionSourceType::kSameIpOrLoopback)] |
| 982 |
.empty() && |
| 983 |
source_types_array[static_cast<int>(XdsListenerResource::FilterChainMap:: |
| 984 |
ConnectionSourceType::kExternal)] |
| 985 |
.empty()) { |
| 986 |
return FindFilterChainDataForSourceIp( |
| 987 |
source_types_array[static_cast<int>( |
| 988 |
XdsListenerResource::FilterChainMap::ConnectionSourceType::kAny)], |
| 989 |
&*source_addr, port); |
| 990 |
} |
| 991 |
if (IsLoopbackIp(&*source_addr) || host == destination_ip) { |
| 992 |
return FindFilterChainDataForSourceIp( |
| 993 |
source_types_array[static_cast<int>( |
| 994 |
XdsListenerResource::FilterChainMap::ConnectionSourceType:: |
| 995 |
kSameIpOrLoopback)], |
| 996 |
&*source_addr, port); |
| 997 |
} else { |
| 998 |
return FindFilterChainDataForSourceIp( |
| 999 |
source_types_array[static_cast<int>( |
| 1000 |
XdsListenerResource::FilterChainMap::ConnectionSourceType:: |
| 1001 |
kExternal)], |
| 1002 |
&*source_addr, port); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
const XdsListenerResource::FilterChainData* FindFilterChainDataForDestinationIp( |
| 1007 |
const XdsListenerResource::FilterChainMap::DestinationIpVector |
| 1008 |
destination_ip_vector, |
| 1009 |
grpc_endpoint* tcp) { |
| 1010 |
auto destination_uri = URI::Parse(grpc_endpoint_get_local_address(tcp)); |
| 1011 |
if (!destination_uri.ok() || (destination_uri->scheme() != "ipv4" && |
| 1012 |
destination_uri->scheme() != "ipv6")) { |
| 1013 |
return nullptr; |
| 1014 |
} |
| 1015 |
std::string host; |
| 1016 |
std::string port; |
| 1017 |
if (!SplitHostPort(destination_uri->path(), &host, &port)) { |
| 1018 |
return nullptr; |
| 1019 |
} |
| 1020 |
auto destination_addr = |
| 1021 |
StringToSockaddr(host, 0); // Port doesn't matter here. |
| 1022 |
if (!destination_addr.ok()) { |
| 1023 |
gpr_log(GPR_DEBUG, "Could not parse \"%s\" as socket address: %s", |
| 1024 |
host.c_str(), destination_addr.status().ToString().c_str()); |
| 1025 |
return nullptr; |
| 1026 |
} |
| 1027 |
const XdsListenerResource::FilterChainMap::DestinationIp* best_match = |
| 1028 |
nullptr; |
| 1029 |
for (const auto& entry : destination_ip_vector) { |
| 1030 |
// Special case for catch-all |
| 1031 |
if (!entry.prefix_range.has_value()) { |
| 1032 |
if (best_match == nullptr) { |
| 1033 |
best_match = &entry; |
| 1034 |
} |
| 1035 |
continue; |
| 1036 |
} |
| 1037 |
if (best_match != nullptr && best_match->prefix_range.has_value() && |
| 1038 |
best_match->prefix_range->prefix_len >= |
| 1039 |
entry.prefix_range->prefix_len) { |
| 1040 |
continue; |
| 1041 |
} |
| 1042 |
if (grpc_sockaddr_match_subnet(&*destination_addr, |
| 1043 |
&entry.prefix_range->address, |
| 1044 |
entry.prefix_range->prefix_len)) { |
| 1045 |
best_match = &entry; |
| 1046 |
} |
| 1047 |
} |
| 1048 |
if (best_match == nullptr) return nullptr; |
| 1049 |
return FindFilterChainDataForSourceType(best_match->source_types_array, tcp, |
| 1050 |
host); |
| 1051 |
} |
| 1052 |
|
| 1053 |
absl::StatusOr<ChannelArgs> XdsServerConfigFetcher::ListenerWatcher:: |
| 1054 |
FilterChainMatchManager::UpdateChannelArgsForConnection( |
| 1055 |
const ChannelArgs& input_args, grpc_endpoint* tcp) { |
| 1056 |
ChannelArgs args = input_args; |
| 1057 |
const auto* filter_chain = FindFilterChainDataForDestinationIp( |
| 1058 |
filter_chain_map_.destination_ip_vector, tcp); |
| 1059 |
if (filter_chain == nullptr && default_filter_chain_.has_value()) { |
| 1060 |
filter_chain = &default_filter_chain_.value(); |
| 1061 |
} |
| 1062 |
if (filter_chain == nullptr) { |
| 1063 |
return absl::UnavailableError("No matching filter chain found"); |
| 1064 |
} |
| 1065 |
RefCountedPtr<ServerConfigSelectorProvider> server_config_selector_provider; |
| 1066 |
RefCountedPtr<XdsChannelStackModifier> channel_stack_modifier; |
| 1067 |
RefCountedPtr<XdsCertificateProvider> xds_certificate_provider; |
| 1068 |
// Iterate the list of HTTP filters in reverse since in Core, received data |
| 1069 |
// flows *up* the stack. |
| 1070 |
std::vector<const grpc_channel_filter*> filters; |
| 1071 |
const auto& http_filter_registry = |
| 1072 |
static_cast<const GrpcXdsBootstrap&>(xds_client_->bootstrap()) |
| 1073 |
.http_filter_registry(); |
| 1074 |
for (const auto& http_filter : |
| 1075 |
filter_chain->http_connection_manager.http_filters) { |
| 1076 |
// Find filter. This is guaranteed to succeed, because it's checked |
| 1077 |
// at config validation time in the XdsApi code. |
| 1078 |
const XdsHttpFilterImpl* filter_impl = |
| 1079 |
http_filter_registry.GetFilterForType( |
| 1080 |
http_filter.config.config_proto_type_name); |
| 1081 |
GPR_ASSERT(filter_impl != nullptr); |
| 1082 |
// Some filters like the router filter are no-op filters and do not have |
| 1083 |
// an implementation. |
| 1084 |
if (filter_impl->channel_filter() != nullptr) { |
| 1085 |
filters.push_back(filter_impl->channel_filter()); |
| 1086 |
} |
| 1087 |
} |
| 1088 |
// Add config selector filter. |
| 1089 |
filters.push_back(&kServerConfigSelectorFilter); |
| 1090 |
channel_stack_modifier = |
| 1091 |
MakeRefCounted<XdsChannelStackModifier>(std::move(filters)); |
| 1092 |
Match( |
| 1093 |
filter_chain->http_connection_manager.route_config, |
| 1094 |
// RDS resource name |
| 1095 |
[&](const std::string& rds_name) { |
| 1096 |
absl::StatusOr<XdsRouteConfigResource> initial_resource; |
| 1097 |
{ |
| 1098 |
MutexLock lock(&mu_); |
| 1099 |
initial_resource = rds_map_[rds_name].rds_update.value(); |
| 1100 |
} |
| 1101 |
server_config_selector_provider = |
| 1102 |
MakeRefCounted<DynamicXdsServerConfigSelectorProvider>( |
| 1103 |
xds_client_->Ref(DEBUG_LOCATION, |
| 1104 |
"DynamicXdsServerConfigSelectorProvider"), |
| 1105 |
rds_name, std::move(initial_resource), |
| 1106 |
filter_chain->http_connection_manager.http_filters); |
| 1107 |
}, |
| 1108 |
// inline RouteConfig |
| 1109 |
[&](const XdsRouteConfigResource& route_config) { |
| 1110 |
server_config_selector_provider = |
| 1111 |
MakeRefCounted<StaticXdsServerConfigSelectorProvider>( |
| 1112 |
xds_client_->Ref(DEBUG_LOCATION, |
| 1113 |
"StaticXdsServerConfigSelectorProvider"), |
| 1114 |
route_config, |
| 1115 |
filter_chain->http_connection_manager.http_filters); |
| 1116 |
}); |
| 1117 |
args = args.SetObject(server_config_selector_provider) |
| 1118 |
.SetObject(channel_stack_modifier); |
| 1119 |
// Add XdsCertificateProvider if credentials are xDS. |
| 1120 |
auto* server_creds = args.GetObject<grpc_server_credentials>(); |
| 1121 |
if (server_creds != nullptr && |
| 1122 |
server_creds->type() == XdsServerCredentials::Type()) { |
| 1123 |
absl::StatusOr<RefCountedPtr<XdsCertificateProvider>> result = |
| 1124 |
CreateOrGetXdsCertificateProviderFromFilterChainData(filter_chain); |
| 1125 |
if (!result.ok()) { |
| 1126 |
return result.status(); |
| 1127 |
} |
| 1128 |
xds_certificate_provider = std::move(*result); |
| 1129 |
GPR_ASSERT(xds_certificate_provider != nullptr); |
| 1130 |
args = args.SetObject(xds_certificate_provider); |
| 1131 |
} |
| 1132 |
return args; |
| 1133 |
} |
| 1134 |
|
| 1135 |
// |
| 1136 |
// XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager::XdsServerConfigSelector |
| 1137 |
// |
| 1138 |
|
| 1139 |
absl::StatusOr< |
| 1140 |
RefCountedPtr<XdsServerConfigFetcher::ListenerWatcher:: |
| 1141 |
FilterChainMatchManager::XdsServerConfigSelector>> |
| 1142 |
XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 1143 |
XdsServerConfigSelector::Create( |
| 1144 |
const XdsHttpFilterRegistry& http_filter_registry, |
| 1145 |
XdsRouteConfigResource rds_update, |
| 1146 |
const std::vector< |
| 1147 |
XdsListenerResource::HttpConnectionManager::HttpFilter>& |
| 1148 |
http_filters) { |
| 1149 |
auto config_selector = MakeRefCounted<XdsServerConfigSelector>(); |
| 1150 |
for (auto& vhost : rds_update.virtual_hosts) { |
| 1151 |
config_selector->virtual_hosts_.emplace_back(); |
| 1152 |
auto& virtual_host = config_selector->virtual_hosts_.back(); |
| 1153 |
virtual_host.domains = std::move(vhost.domains); |
| 1154 |
for (auto& route : vhost.routes) { |
| 1155 |
virtual_host.routes.emplace_back(); |
| 1156 |
auto& config_selector_route = virtual_host.routes.back(); |
| 1157 |
config_selector_route.matchers = std::move(route.matchers); |
| 1158 |
config_selector_route.unsupported_action = |
| 1159 |
absl::get_if<XdsRouteConfigResource::Route::NonForwardingAction>( |
| 1160 |
&route.action) == nullptr; |
| 1161 |
auto result = XdsRouting::GeneratePerHTTPFilterConfigs( |
| 1162 |
http_filter_registry, http_filters, vhost, route, nullptr, |
| 1163 |
ChannelArgs()); |
| 1164 |
if (!result.ok()) return result.status(); |
| 1165 |
std::vector<std::string> fields; |
| 1166 |
fields.reserve(result->per_filter_configs.size()); |
| 1167 |
for (const auto& p : result->per_filter_configs) { |
| 1168 |
fields.emplace_back(absl::StrCat(" \"", p.first, "\": [\n", |
| 1169 |
absl::StrJoin(p.second, ",\n"), |
| 1170 |
"\n ]")); |
| 1171 |
} |
| 1172 |
if (!fields.empty()) { |
| 1173 |
std::string json = absl::StrCat( |
| 1174 |
"{\n" |
| 1175 |
" \"methodConfig\": [ {\n" |
| 1176 |
" \"name\": [\n" |
| 1177 |
" {}\n" |
| 1178 |
" ],\n" |
| 1179 |
" ", |
| 1180 |
absl::StrJoin(fields, ",\n"), |
| 1181 |
"\n } ]\n" |
| 1182 |
"}"); |
| 1183 |
config_selector_route.method_config = |
| 1184 |
ServiceConfigImpl::Create(result->args, json.c_str()).value(); |
| 1185 |
} |
| 1186 |
} |
| 1187 |
} |
| 1188 |
return config_selector; |
| 1189 |
} |
| 1190 |
|
| 1191 |
ServerConfigSelector::CallConfig XdsServerConfigFetcher::ListenerWatcher:: |
| 1192 |
FilterChainMatchManager::XdsServerConfigSelector::GetCallConfig( |
| 1193 |
grpc_metadata_batch* metadata) { |
| 1194 |
CallConfig call_config; |
| 1195 |
if (metadata->get_pointer(HttpPathMetadata()) == nullptr) { |
| 1196 |
call_config.error = GRPC_ERROR_CREATE("No path found"); |
| 1197 |
return call_config; |
| 1198 |
} |
| 1199 |
absl::string_view path = |
| 1200 |
metadata->get_pointer(HttpPathMetadata())->as_string_view(); |
| 1201 |
if (metadata->get_pointer(HttpAuthorityMetadata()) == nullptr) { |
| 1202 |
call_config.error = GRPC_ERROR_CREATE("No authority found"); |
| 1203 |
return call_config; |
| 1204 |
} |
| 1205 |
absl::string_view authority = |
| 1206 |
metadata->get_pointer(HttpAuthorityMetadata())->as_string_view(); |
| 1207 |
auto vhost_index = XdsRouting::FindVirtualHostForDomain( |
| 1208 |
VirtualHostListIterator(&virtual_hosts_), authority); |
| 1209 |
if (!vhost_index.has_value()) { |
| 1210 |
call_config.error = grpc_error_set_int( |
| 1211 |
GRPC_ERROR_CREATE(absl::StrCat("could not find VirtualHost for ", |
| 1212 |
authority, " in RouteConfiguration")), |
| 1213 |
StatusIntProperty::kRpcStatus, GRPC_STATUS_UNAVAILABLE); |
| 1214 |
return call_config; |
| 1215 |
} |
| 1216 |
auto& virtual_host = virtual_hosts_[vhost_index.value()]; |
| 1217 |
auto route_index = XdsRouting::GetRouteForRequest( |
| 1218 |
VirtualHost::RouteListIterator(&virtual_host.routes), path, metadata); |
| 1219 |
if (route_index.has_value()) { |
| 1220 |
auto& route = virtual_host.routes[route_index.value()]; |
| 1221 |
// Found the matching route |
| 1222 |
if (route.unsupported_action) { |
| 1223 |
call_config.error = grpc_error_set_int( |
| 1224 |
GRPC_ERROR_CREATE("Matching route has unsupported action"), |
| 1225 |
StatusIntProperty::kRpcStatus, GRPC_STATUS_UNAVAILABLE); |
| 1226 |
return call_config; |
| 1227 |
} |
| 1228 |
if (route.method_config != nullptr) { |
| 1229 |
call_config.method_configs = |
| 1230 |
route.method_config->GetMethodParsedConfigVector(grpc_empty_slice()); |
| 1231 |
call_config.service_config = route.method_config; |
| 1232 |
} |
| 1233 |
return call_config; |
| 1234 |
} |
| 1235 |
call_config.error = grpc_error_set_int(GRPC_ERROR_CREATE("No route matched"), |
| 1236 |
StatusIntProperty::kRpcStatus, |
| 1237 |
GRPC_STATUS_UNAVAILABLE); |
| 1238 |
return call_config; |
| 1239 |
} |
| 1240 |
|
| 1241 |
// |
| 1242 |
// XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager::DynamicXdsServerConfigSelectorProvider |
| 1243 |
// |
| 1244 |
|
| 1245 |
XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 1246 |
DynamicXdsServerConfigSelectorProvider:: |
| 1247 |
DynamicXdsServerConfigSelectorProvider( |
| 1248 |
RefCountedPtr<GrpcXdsClient> xds_client, std::string resource_name, |
| 1249 |
absl::StatusOr<XdsRouteConfigResource> initial_resource, |
| 1250 |
std::vector<XdsListenerResource::HttpConnectionManager::HttpFilter> |
| 1251 |
http_filters) |
| 1252 |
: xds_client_(std::move(xds_client)), |
| 1253 |
resource_name_(std::move(resource_name)), |
| 1254 |
http_filters_(std::move(http_filters)), |
| 1255 |
resource_(std::move(initial_resource)) { |
| 1256 |
GPR_ASSERT(!resource_name_.empty()); |
| 1257 |
// RouteConfigWatcher is being created here instead of in Watch() to avoid |
| 1258 |
// deadlocks from invoking XdsRouteConfigResourceType::StartWatch whilst in a |
| 1259 |
// critical region. |
| 1260 |
auto route_config_watcher = MakeRefCounted<RouteConfigWatcher>(WeakRef()); |
| 1261 |
route_config_watcher_ = route_config_watcher.get(); |
| 1262 |
XdsRouteConfigResourceType::StartWatch(xds_client_.get(), resource_name_, |
| 1263 |
std::move(route_config_watcher)); |
| 1264 |
} |
| 1265 |
|
| 1266 |
void XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 1267 |
DynamicXdsServerConfigSelectorProvider::Orphan() { |
| 1268 |
XdsRouteConfigResourceType::CancelWatch(xds_client_.get(), resource_name_, |
| 1269 |
route_config_watcher_, |
| 1270 |
false /* delay_unsubscription */); |
| 1271 |
} |
| 1272 |
|
| 1273 |
absl::StatusOr<RefCountedPtr<ServerConfigSelector>> |
| 1274 |
XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 1275 |
DynamicXdsServerConfigSelectorProvider::Watch( |
| 1276 |
std::unique_ptr< |
| 1277 |
ServerConfigSelectorProvider::ServerConfigSelectorWatcher> |
| 1278 |
watcher) { |
| 1279 |
absl::StatusOr<XdsRouteConfigResource> resource; |
| 1280 |
{ |
| 1281 |
MutexLock lock(&mu_); |
| 1282 |
GPR_ASSERT(watcher_ == nullptr); |
| 1283 |
watcher_ = std::move(watcher); |
| 1284 |
resource = resource_; |
| 1285 |
} |
| 1286 |
if (!resource.ok()) { |
| 1287 |
return resource.status(); |
| 1288 |
} |
| 1289 |
return XdsServerConfigSelector::Create( |
| 1290 |
static_cast<const GrpcXdsBootstrap&>(xds_client_->bootstrap()) |
| 1291 |
.http_filter_registry(), |
| 1292 |
resource.value(), http_filters_); |
| 1293 |
} |
| 1294 |
|
| 1295 |
void XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 1296 |
DynamicXdsServerConfigSelectorProvider::CancelWatch() { |
| 1297 |
MutexLock lock(&mu_); |
| 1298 |
watcher_.reset(); |
| 1299 |
} |
| 1300 |
|
| 1301 |
void XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 1302 |
DynamicXdsServerConfigSelectorProvider::OnRouteConfigChanged( |
| 1303 |
XdsRouteConfigResource rds_update) { |
| 1304 |
MutexLock lock(&mu_); |
| 1305 |
resource_ = std::move(rds_update); |
| 1306 |
if (watcher_ == nullptr) { |
| 1307 |
return; |
| 1308 |
} |
| 1309 |
// Currently server_config_selector_filter does not call into |
| 1310 |
// DynamicXdsServerConfigSelectorProvider while holding a lock, but if that |
| 1311 |
// ever changes, we would want to invoke the update outside the critical |
| 1312 |
// region with the use of a WorkSerializer. |
| 1313 |
watcher_->OnServerConfigSelectorUpdate(XdsServerConfigSelector::Create( |
| 1314 |
static_cast<const GrpcXdsBootstrap&>(xds_client_->bootstrap()) |
| 1315 |
.http_filter_registry(), |
| 1316 |
*resource_, http_filters_)); |
| 1317 |
} |
| 1318 |
|
| 1319 |
void XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 1320 |
DynamicXdsServerConfigSelectorProvider::OnError(absl::Status status) { |
| 1321 |
MutexLock lock(&mu_); |
| 1322 |
// Prefer existing good update. |
| 1323 |
if (resource_.ok()) { |
| 1324 |
return; |
| 1325 |
} |
| 1326 |
resource_ = status; |
| 1327 |
if (watcher_ == nullptr) { |
| 1328 |
return; |
| 1329 |
} |
| 1330 |
watcher_->OnServerConfigSelectorUpdate(resource_.status()); |
| 1331 |
} |
| 1332 |
|
| 1333 |
void XdsServerConfigFetcher::ListenerWatcher::FilterChainMatchManager:: |
| 1334 |
DynamicXdsServerConfigSelectorProvider::OnResourceDoesNotExist() { |
| 1335 |
MutexLock lock(&mu_); |
| 1336 |
resource_ = absl::NotFoundError("Requested route config does not exist"); |
| 1337 |
if (watcher_ == nullptr) { |
| 1338 |
return; |
| 1339 |
} |
| 1340 |
watcher_->OnServerConfigSelectorUpdate(resource_.status()); |
| 1341 |
} |
| 1342 |
|
| 1343 |
} // namespace |
| 1344 |
} // namespace grpc_core |
| 1345 |
|
| 1346 |
grpc_server_config_fetcher* grpc_server_config_fetcher_xds_create( |
| 1347 |
grpc_server_xds_status_notifier notifier, const grpc_channel_args* args) { |
| 1348 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 1349 |
grpc_core::ExecCtx exec_ctx; |
| 1350 |
grpc_core::ChannelArgs channel_args = grpc_core::CoreConfiguration::Get() |
| 1351 |
.channel_args_preconditioning() |
| 1352 |
.PreconditionChannelArgs(args); |
| 1353 |
GRPC_API_TRACE( |
| 1354 |
"grpc_server_config_fetcher_xds_create(notifier={on_serving_status_" |
| 1355 |
"update=%p, user_data=%p}, args=%p)", |
| 1356 |
3, (notifier.on_serving_status_update, notifier.user_data, args)); |
| 1357 |
auto xds_client = grpc_core::GrpcXdsClient::GetOrCreate( |
| 1358 |
channel_args, "XdsServerConfigFetcher"); |
| 1359 |
if (!xds_client.ok()) { |
| 1360 |
gpr_log(GPR_ERROR, "Failed to create xds client: %s", |
| 1361 |
xds_client.status().ToString().c_str()); |
| 1362 |
return nullptr; |
| 1363 |
} |
| 1364 |
if (static_cast<const grpc_core::GrpcXdsBootstrap&>( |
| 1365 |
(*xds_client)->bootstrap()) |
| 1366 |
.server_listener_resource_name_template() |
| 1367 |
.empty()) { |
| 1368 |
gpr_log(GPR_ERROR, |
| 1369 |
"server_listener_resource_name_template not provided in bootstrap " |
| 1370 |
"file."); |
| 1371 |
return nullptr; |
| 1372 |
} |
| 1373 |
return new grpc_core::XdsServerConfigFetcher(std::move(*xds_client), |
| 1374 |
notifier); |
| 1375 |
} |