Line data Source code
1 : #pragma once 2 : 3 : #include <chrono> 4 : #include <functional> 5 : #include <memory> 6 : 7 : #include "envoy/access_log/access_log.h" 8 : #include "envoy/common/random_generator.h" 9 : #include "envoy/config/core/v3/base.pb.h" 10 : #include "envoy/config/typed_config.h" 11 : #include "envoy/config/typed_metadata.h" 12 : #include "envoy/grpc/context.h" 13 : #include "envoy/http/codes.h" 14 : #include "envoy/http/context.h" 15 : #include "envoy/http/filter.h" 16 : #include "envoy/init/manager.h" 17 : #include "envoy/network/drain_decision.h" 18 : #include "envoy/network/filter.h" 19 : #include "envoy/router/context.h" 20 : #include "envoy/runtime/runtime.h" 21 : #include "envoy/server/admin.h" 22 : #include "envoy/server/configuration.h" 23 : #include "envoy/server/drain_manager.h" 24 : #include "envoy/server/lifecycle_notifier.h" 25 : #include "envoy/server/options.h" 26 : #include "envoy/server/overload/overload_manager.h" 27 : #include "envoy/server/process_context.h" 28 : #include "envoy/singleton/manager.h" 29 : #include "envoy/stats/scope.h" 30 : #include "envoy/thread_local/thread_local.h" 31 : #include "envoy/tracing/tracer.h" 32 : #include "envoy/upstream/cluster_manager.h" 33 : 34 : #include "source/common/common/assert.h" 35 : #include "source/common/common/macros.h" 36 : #include "source/common/protobuf/protobuf.h" 37 : 38 : namespace Envoy { 39 : namespace Server { 40 : namespace Configuration { 41 : 42 : /** 43 : * Common interface for downstream and upstream network filters to access server 44 : * wide resources. This could be treated as limited form of server factory context. 45 : */ 46 : class CommonFactoryContext { 47 : public: 48 604 : virtual ~CommonFactoryContext() = default; 49 : 50 : /** 51 : * @return Server::Options& the command-line options that Envoy was started with. 52 : */ 53 : virtual const Options& options() PURE; 54 : 55 : /** 56 : * @return Event::Dispatcher& the main thread's dispatcher. This dispatcher should be used 57 : * for all singleton processing. 58 : */ 59 : virtual Event::Dispatcher& mainThreadDispatcher() PURE; 60 : 61 : /** 62 : * @return Api::Api& a reference to the api object. 63 : */ 64 : virtual Api::Api& api() PURE; 65 : 66 : /** 67 : * @return information about the local environment the server is running in. 68 : */ 69 : virtual const LocalInfo::LocalInfo& localInfo() const PURE; 70 : 71 : /** 72 : * @return OptRef<Server::Admin> the global HTTP admin endpoint for the server. 73 : */ 74 : virtual OptRef<Server::Admin> admin() PURE; 75 : 76 : /** 77 : * @return Runtime::Loader& the singleton runtime loader for the server. 78 : */ 79 : virtual Envoy::Runtime::Loader& runtime() PURE; 80 : 81 : /** 82 : * @return Singleton::Manager& the server-wide singleton manager. 83 : */ 84 : virtual Singleton::Manager& singletonManager() PURE; 85 : 86 : /** 87 : * @return ProtobufMessage::ValidationContext& validation visitor for xDS and static configuration 88 : * messages. 89 : */ 90 : virtual ProtobufMessage::ValidationContext& messageValidationContext() PURE; 91 : 92 : /** 93 : * @return ProtobufMessage::ValidationVisitor& validation visitor for configuration messages. 94 : */ 95 : virtual ProtobufMessage::ValidationVisitor& messageValidationVisitor() PURE; 96 : 97 : /** 98 : * @return Stats::Scope& the context's stats scope. 99 : */ 100 : virtual Stats::Scope& scope() PURE; 101 : 102 : /** 103 : * @return Stats::Scope& the server wide stats scope. 104 : */ 105 : virtual Stats::Scope& serverScope() PURE; 106 : 107 : /** 108 : * @return ThreadLocal::SlotAllocator& the thread local storage engine for the server. This is 109 : * used to allow runtime lockless updates to configuration, etc. across multiple threads. 110 : */ 111 : virtual ThreadLocal::SlotAllocator& threadLocal() PURE; 112 : 113 : /** 114 : * @return Upstream::ClusterManager& singleton for use by the entire server. 115 : */ 116 : virtual Upstream::ClusterManager& clusterManager() PURE; 117 : 118 : /** 119 : * @return TimeSource& a reference to the time source. 120 : */ 121 : virtual TimeSource& timeSource() PURE; 122 : 123 : /** 124 : * @return AccessLogManager for use by the entire server. 125 : */ 126 : virtual AccessLog::AccessLogManager& accessLogManager() PURE; 127 : 128 : /** 129 : * @return ServerLifecycleNotifier& the lifecycle notifier for the server. 130 : */ 131 : virtual ServerLifecycleNotifier& lifecycleNotifier() PURE; 132 : }; 133 : 134 : /** 135 : * ServerFactoryContext is an specialization of common interface for downstream and upstream network 136 : * filters. The implementation guarantees the lifetime is no shorter than server. It could be used 137 : * across listeners. 138 : */ 139 : class ServerFactoryContext : public virtual CommonFactoryContext { 140 : public: 141 604 : ~ServerFactoryContext() override = default; 142 : 143 : /** 144 : * @return Http::Context& the server-wide HTTP context. 145 : */ 146 : virtual Http::Context& httpContext() PURE; 147 : 148 : /** 149 : * @return Grpc::Context& the server-wide grpc context. 150 : */ 151 : virtual Grpc::Context& grpcContext() PURE; 152 : 153 : /** 154 : * @return Router::Context& the server-wide router context. 155 : */ 156 : virtual Router::Context& routerContext() PURE; 157 : 158 : /** 159 : * @return ProcessContextOptRef an optional reference to the 160 : * process context. Will be unset when running in validation mode. 161 : */ 162 : virtual ProcessContextOptRef processContext() PURE; 163 : 164 : /** 165 : * @return the init manager of the cluster. This can be used for extensions that need 166 : * to initialize after cluster manager init but before the server starts listening. 167 : * All extensions should register themselves during configuration load. initialize() 168 : * will be called on each registered target after cluster manager init but before the 169 : * server starts listening. Once all targets have initialized and invoked their callbacks, 170 : * the server will start listening. 171 : */ 172 : virtual Init::Manager& initManager() PURE; 173 : 174 : /** 175 : * @return DrainManager& the server-wide drain manager. 176 : */ 177 : virtual Envoy::Server::DrainManager& drainManager() PURE; 178 : 179 : /** 180 : * @return StatsConfig& the servers stats configuration. 181 : */ 182 : virtual StatsConfig& statsConfig() PURE; 183 : 184 : /** 185 : * @return envoy::config::bootstrap::v3::Bootstrap& the servers bootstrap configuration. 186 : */ 187 : virtual envoy::config::bootstrap::v3::Bootstrap& bootstrap() PURE; 188 : 189 : /** 190 : * @return OverloadManager& the overload manager for the server. 191 : */ 192 : virtual OverloadManager& overloadManager() PURE; 193 : 194 : /** 195 : * @return whether external healthchecks are currently failed or not. 196 : */ 197 : virtual bool healthCheckFailed() const PURE; 198 : }; 199 : 200 : /** 201 : * Generic factory context for multiple scenarios. This context provides a server factory context 202 : * reference and other resources. Note that except for server factory context, other resources are 203 : * not guaranteed to be available for the entire server lifetime. For example, context powered by a 204 : * listener is only available for the lifetime of the listener. 205 : */ 206 : class GenericFactoryContext { 207 : public: 208 715 : virtual ~GenericFactoryContext() = default; 209 : 210 : /** 211 : * @return ServerFactoryContext which lifetime is no shorter than the server and provides 212 : * access to the server's resources. 213 : */ 214 : virtual ServerFactoryContext& serverFactoryContext() const PURE; 215 : 216 : /** 217 : * @return ProtobufMessage::ValidationVisitor& validation visitor for configuration messages. 218 : */ 219 : virtual ProtobufMessage::ValidationVisitor& messageValidationVisitor() const PURE; 220 : 221 : /** 222 : * @return Init::Manager& the init manager of the server/listener/cluster/etc, depending on the 223 : * backend implementation. 224 : */ 225 : virtual Init::Manager& initManager() PURE; 226 : 227 : /** 228 : * @return Stats::Scope& the stats scope of the server/listener/cluster/etc, depending on the 229 : * backend implementation. 230 : */ 231 : virtual Stats::Scope& scope() PURE; 232 : }; 233 : 234 : /** 235 : * Context passed to network and HTTP filters to access server resources. 236 : * TODO(mattklein123): When we lock down visibility of the rest of the code, filters should only 237 : * access the rest of the server via interfaces exposed here. 238 : */ 239 : class FactoryContext : public virtual GenericFactoryContext { 240 : public: 241 705 : ~FactoryContext() override = default; 242 : 243 : /** 244 : * @return Stats::Scope& the listener's stats scope. 245 : */ 246 : virtual Stats::Scope& listenerScope() PURE; 247 : 248 : /** 249 : * @return TransportSocketFactoryContext which lifetime is no shorter than the server. 250 : */ 251 : virtual TransportSocketFactoryContext& getTransportSocketFactoryContext() const PURE; 252 : 253 : /** 254 : * @return const Network::DrainDecision& a drain decision that filters can use to determine if 255 : * they should be doing graceful closes on connections when possible. 256 : */ 257 : virtual const Network::DrainDecision& drainDecision() PURE; 258 : 259 : /** 260 : * @return ListenerInfo description of the listener. 261 : */ 262 : virtual const Network::ListenerInfo& listenerInfo() const PURE; 263 : }; 264 : 265 : /** 266 : * An implementation of FactoryContext. The life time is no shorter than the created filter chains. 267 : * The life time is no longer than the owning listener. It should be used to create 268 : * NetworkFilterChain. 269 : */ 270 : class FilterChainFactoryContext : public virtual FactoryContext { 271 : public: 272 : /** 273 : * Set the flag that all attached filter chains will be destroyed. 274 : */ 275 : virtual void startDraining() PURE; 276 : }; 277 : 278 : using FilterChainFactoryContextPtr = std::unique_ptr<FilterChainFactoryContext>; 279 : using FilterChainsByName = absl::flat_hash_map<std::string, Network::DrainableFilterChainSharedPtr>; 280 : 281 : // This allows matchers to select the correct filter chain for a route. 282 : class FilterChainBaseAction : public Matcher::Action { 283 : public: 284 : /** 285 : * Get the filter chain for this request 286 : * @param filter_chains_by_name the configured filter chains 287 : * @param info the stream info for this request 288 : * @ return Network::FilterChain* a pointer to the filter chain for this request. 289 : */ 290 : virtual const Network::FilterChain* get(const FilterChainsByName& filter_chains_by_name, 291 : const StreamInfo::StreamInfo& info) const PURE; 292 : }; 293 : 294 : /** 295 : * An implementation of FactoryContext. The life time should cover the lifetime of the filter chains 296 : * and connections. It can be used to create ListenerFilterChain. 297 : */ 298 : class ListenerFactoryContext : public virtual FactoryContext {}; 299 : 300 : /** 301 : * FactoryContext for ProtocolOptionsFactory. 302 : */ 303 : using ProtocolOptionsFactoryContext = Server::Configuration::TransportSocketFactoryContext; 304 : 305 : /** 306 : * FactoryContext for upstream HTTP filters. 307 : */ 308 : class UpstreamFactoryContext { 309 : public: 310 159 : virtual ~UpstreamFactoryContext() = default; 311 : 312 : /** 313 : * @return ServerFactoryContext which lifetime is no shorter than the server. 314 : */ 315 : virtual ServerFactoryContext& serverFactoryContext() const PURE; 316 : 317 : /** 318 : * @return the init manager of the particular context. This can be used for extensions that need 319 : * to initialize after cluster manager init but before the server starts listening. 320 : * All extensions should register themselves during configuration load. initialize() 321 : * will be called on each registered target after cluster manager init but before the 322 : * server starts listening. Once all targets have initialized and invoked their callbacks, 323 : * the server will start listening. 324 : */ 325 : virtual Init::Manager& initManager() PURE; 326 : 327 : /* 328 : * @return the stats scope of the cluster. This will last as long as the cluster is valid 329 : * */ 330 : virtual Stats::Scope& scope() PURE; 331 : }; 332 : 333 : } // namespace Configuration 334 : } // namespace Server 335 : } // namespace Envoy