| 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 |
#ifndef GRPC_CORE_EXT_XDS_XDS_ROUTE_CONFIG_H |
| 18 |
#define GRPC_CORE_EXT_XDS_XDS_ROUTE_CONFIG_H |
| 19 |
|
| 20 |
#include <grpc/support/port_platform.h> |
| 21 |
|
| 22 |
#include <stdint.h> |
| 23 |
|
| 24 |
#include <algorithm> |
| 25 |
#include <map> |
| 26 |
#include <memory> |
| 27 |
#include <string> |
| 28 |
#include <vector> |
| 29 |
|
| 30 |
#include "absl/status/statusor.h" |
| 31 |
#include "absl/strings/string_view.h" |
| 32 |
#include "absl/types/optional.h" |
| 33 |
#include "absl/types/variant.h" |
| 34 |
#include "envoy/config/route/v3/route.upb.h" |
| 35 |
#include "envoy/config/route/v3/route.upbdefs.h" |
| 36 |
#include "re2/re2.h" |
| 37 |
#include "upb/def.h" |
| 38 |
|
| 39 |
#include "src/core/ext/xds/xds_bootstrap_grpc.h" |
| 40 |
#include "src/core/ext/xds/xds_client.h" |
| 41 |
#include "src/core/ext/xds/xds_cluster_specifier_plugin.h" |
| 42 |
#include "src/core/ext/xds/xds_http_filters.h" |
| 43 |
#include "src/core/ext/xds/xds_resource_type.h" |
| 44 |
#include "src/core/ext/xds/xds_resource_type_impl.h" |
| 45 |
#include "src/core/lib/channel/status_util.h" |
| 46 |
#include "src/core/lib/gprpp/time.h" |
| 47 |
#include "src/core/lib/matchers/matchers.h" |
| 48 |
|
| 49 |
namespace grpc_core { |
| 50 |
|
| 51 |
struct XdsRouteConfigResource : public XdsResourceType::ResourceData { |
| 52 |
using TypedPerFilterConfig = |
| 53 |
std::map<std::string, XdsHttpFilterImpl::FilterConfig>; |
| 54 |
|
| 55 |
using ClusterSpecifierPluginMap = |
| 56 |
std::map<std::string /*cluster_specifier_plugin_name*/, |
| 57 |
std::string /*LB policy config*/>; |
| 58 |
|
| 59 |
struct RetryPolicy { |
| 60 |
internal::StatusCodeSet retry_on; |
| 61 |
uint32_t num_retries; |
| 62 |
|
| 63 |
struct RetryBackOff { |
| 64 |
Duration base_interval; |
| 65 |
Duration max_interval; |
| 66 |
|
| 67 |
bool operator==(const RetryBackOff& other) const { |
| 68 |
return base_interval == other.base_interval && |
| 69 |
max_interval == other.max_interval; |
| 70 |
} |
| 71 |
std::string ToString() const; |
| 72 |
}; |
| 73 |
RetryBackOff retry_back_off; |
| 74 |
|
| 75 |
bool operator==(const RetryPolicy& other) const { |
| 76 |
return (retry_on == other.retry_on && num_retries == other.num_retries && |
| 77 |
retry_back_off == other.retry_back_off); |
| 78 |
} |
| 79 |
std::string ToString() const; |
| 80 |
}; |
| 81 |
|
| 82 |
struct Route { |
| 83 |
// Matchers for this route. |
| 84 |
struct Matchers { |
| 85 |
StringMatcher path_matcher; |
| 86 |
std::vector<HeaderMatcher> header_matchers; |
| 87 |
absl::optional<uint32_t> fraction_per_million; |
| 88 |
|
| 89 |
bool operator==(const Matchers& other) const { |
| 90 |
return path_matcher == other.path_matcher && |
| 91 |
header_matchers == other.header_matchers && |
| 92 |
fraction_per_million == other.fraction_per_million; |
| 93 |
} |
| 94 |
std::string ToString() const; |
| 95 |
}; |
| 96 |
|
| 97 |
Matchers matchers; |
| 98 |
|
| 99 |
struct UnknownAction { |
| 100 |
bool operator==(const UnknownAction& /* other */) const { return true; } |
| 101 |
}; |
| 102 |
|
| 103 |
struct RouteAction { |
| 104 |
struct HashPolicy { |
| 105 |
enum Type { HEADER, CHANNEL_ID }; |
| 106 |
Type type; |
| 107 |
bool terminal = false; |
| 108 |
// Fields used for type HEADER. |
| 109 |
std::string header_name; |
| 110 |
std::unique_ptr<RE2> regex = nullptr; |
| 111 |
std::string regex_substitution; |
| 112 |
|
| 113 |
HashPolicy() {} |
| 114 |
|
| 115 |
// Copyable. |
| 116 |
HashPolicy(const HashPolicy& other); |
| 117 |
HashPolicy& operator=(const HashPolicy& other); |
| 118 |
|
| 119 |
// Moveable. |
| 120 |
HashPolicy(HashPolicy&& other) noexcept; |
| 121 |
HashPolicy& operator=(HashPolicy&& other) noexcept; |
| 122 |
|
| 123 |
bool operator==(const HashPolicy& other) const; |
| 124 |
std::string ToString() const; |
| 125 |
}; |
| 126 |
|
| 127 |
struct ClusterName { |
| 128 |
std::string cluster_name; |
| 129 |
|
| 130 |
bool operator==(const ClusterName& other) const { |
| 131 |
return cluster_name == other.cluster_name; |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
struct ClusterWeight { |
| 136 |
std::string name; |
| 137 |
uint32_t weight; |
| 138 |
TypedPerFilterConfig typed_per_filter_config; |
| 139 |
|
| 140 |
bool operator==(const ClusterWeight& other) const { |
| 141 |
return name == other.name && weight == other.weight && |
| 142 |
typed_per_filter_config == other.typed_per_filter_config; |
| 143 |
} |
| 144 |
std::string ToString() const; |
| 145 |
}; |
| 146 |
|
| 147 |
struct ClusterSpecifierPluginName { |
| 148 |
std::string cluster_specifier_plugin_name; |
| 149 |
|
| 150 |
bool operator==(const ClusterSpecifierPluginName& other) const { |
| 151 |
return cluster_specifier_plugin_name == |
| 152 |
other.cluster_specifier_plugin_name; |
| 153 |
} |
| 154 |
}; |
| 155 |
|
| 156 |
std::vector<HashPolicy> hash_policies; |
| 157 |
absl::optional<RetryPolicy> retry_policy; |
| 158 |
|
| 159 |
// Action for this route. |
| 160 |
absl::variant<ClusterName, std::vector<ClusterWeight>, |
| 161 |
ClusterSpecifierPluginName> |
| 162 |
action; |
| 163 |
// Storing the timeout duration from route action: |
| 164 |
// RouteAction.max_stream_duration.grpc_timeout_header_max or |
| 165 |
// RouteAction.max_stream_duration.max_stream_duration if the former is |
| 166 |
// not set. |
| 167 |
absl::optional<Duration> max_stream_duration; |
| 168 |
|
| 169 |
bool operator==(const RouteAction& other) const { |
| 170 |
return hash_policies == other.hash_policies && |
| 171 |
retry_policy == other.retry_policy && action == other.action && |
| 172 |
max_stream_duration == other.max_stream_duration; |
| 173 |
} |
| 174 |
std::string ToString() const; |
| 175 |
}; |
| 176 |
|
| 177 |
struct NonForwardingAction { |
| 178 |
bool operator==(const NonForwardingAction& /* other */) const { |
| 179 |
return true; |
| 180 |
} |
| 181 |
}; |
| 182 |
|
| 183 |
absl::variant<UnknownAction, RouteAction, NonForwardingAction> action; |
| 184 |
TypedPerFilterConfig typed_per_filter_config; |
| 185 |
|
| 186 |
bool operator==(const Route& other) const { |
| 187 |
return matchers == other.matchers && action == other.action && |
| 188 |
typed_per_filter_config == other.typed_per_filter_config; |
| 189 |
} |
| 190 |
std::string ToString() const; |
| 191 |
}; |
| 192 |
|
| 193 |
struct VirtualHost { |
| 194 |
std::vector<std::string> domains; |
| 195 |
std::vector<Route> routes; |
| 196 |
TypedPerFilterConfig typed_per_filter_config; |
| 197 |
|
| 198 |
bool operator==(const VirtualHost& other) const { |
| 199 |
return domains == other.domains && routes == other.routes && |
| 200 |
typed_per_filter_config == other.typed_per_filter_config; |
| 201 |
} |
| 202 |
}; |
| 203 |
|
| 204 |
std::vector<VirtualHost> virtual_hosts; |
| 205 |
ClusterSpecifierPluginMap cluster_specifier_plugin_map; |
| 206 |
|
| 207 |
bool operator==(const XdsRouteConfigResource& other) const { |
| 208 |
return virtual_hosts == other.virtual_hosts && |
| 209 |
cluster_specifier_plugin_map == other.cluster_specifier_plugin_map; |
| 210 |
} |
| 211 |
std::string ToString() const; |
| 212 |
|
| 213 |
static absl::StatusOr<XdsRouteConfigResource> Parse( |
| 214 |
const XdsResourceType::DecodeContext& context, |
| 215 |
const envoy_config_route_v3_RouteConfiguration* route_config); |
| 216 |
}; |
| 217 |
|
| 218 |
class XdsRouteConfigResourceType |
| 219 |
: public XdsResourceTypeImpl<XdsRouteConfigResourceType, |
| 220 |
XdsRouteConfigResource> { |
| 221 |
public: |
| 222 |
absl::string_view type_url() const override { |
| 223 |
return "envoy.config.route.v3.RouteConfiguration"; |
| 224 |
} |
| 225 |
|
| 226 |
DecodeResult Decode(const XdsResourceType::DecodeContext& context, |
| 227 |
absl::string_view serialized_resource) const override; |
| 228 |
|
| 229 |
void InitUpbSymtab(XdsClient* xds_client, |
| 230 |
upb_DefPool* symtab) const override { |
| 231 |
envoy_config_route_v3_RouteConfiguration_getmsgdef(symtab); |
| 232 |
const auto& cluster_specifier_plugin_registry = |
| 233 |
static_cast<const GrpcXdsBootstrap&>(xds_client->bootstrap()) |
| 234 |
.cluster_specifier_plugin_registry(); |
| 235 |
cluster_specifier_plugin_registry.PopulateSymtab(symtab); |
| 236 |
} |
| 237 |
}; |
| 238 |
|
| 239 |
} // namespace grpc_core |
| 240 |
|
| 241 |
#endif // GRPC_CORE_EXT_XDS_XDS_ROUTE_CONFIG_H |