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/config/xds_manager.h"
13
#include "envoy/grpc/context.h"
14
#include "envoy/http/codes.h"
15
#include "envoy/http/context.h"
16
#include "envoy/http/filter.h"
17
#include "envoy/http/http_server_properties_cache.h"
18
#include "envoy/init/manager.h"
19
#include "envoy/network/drain_decision.h"
20
#include "envoy/network/filter.h"
21
#include "envoy/router/context.h"
22
#include "envoy/runtime/runtime.h"
23
#include "envoy/server/admin.h"
24
#include "envoy/server/configuration.h"
25
#include "envoy/server/drain_manager.h"
26
#include "envoy/server/lifecycle_notifier.h"
27
#include "envoy/server/options.h"
28
#include "envoy/server/overload/overload_manager.h"
29
#include "envoy/server/process_context.h"
30
#include "envoy/singleton/manager.h"
31
#include "envoy/stats/scope.h"
32
#include "envoy/thread_local/thread_local.h"
33
#include "envoy/tracing/tracer.h"
34
#include "envoy/upstream/cluster_manager.h"
35

            
36
#include "source/common/common/assert.h"
37
#include "source/common/common/macros.h"
38
#include "source/common/protobuf/protobuf.h"
39
#include "source/common/singleton/threadsafe_singleton.h"
40

            
41
namespace Envoy {
42

            
43
namespace Regex {
44
class Engine;
45
}
46

            
47
namespace Server {
48
namespace Configuration {
49

            
50
/**
51
 * Common interface for downstream and upstream network filters to access server
52
 * wide resources. This could be treated as limited form of server factory context.
53
 */
54
class CommonFactoryContext {
55
public:
56
49912
  virtual ~CommonFactoryContext() = default;
57

            
58
  /**
59
   * @return Server::Options& the command-line options that Envoy was started with.
60
   */
61
  virtual const Options& options() PURE;
62

            
63
  /**
64
   * @return Event::Dispatcher& the main thread's dispatcher. This dispatcher should be used
65
   *         for all singleton processing.
66
   */
67
  virtual Event::Dispatcher& mainThreadDispatcher() PURE;
68

            
69
  /**
70
   * @return Api::Api& a reference to the api object.
71
   */
72
  virtual Api::Api& api() PURE;
73

            
74
  /**
75
   * @return information about the local environment the server is running in.
76
   */
77
  virtual const LocalInfo::LocalInfo& localInfo() const PURE;
78

            
79
  /**
80
   * @return OptRef<Server::Admin> the global HTTP admin endpoint for the server.
81
   */
82
  virtual OptRef<Server::Admin> admin() PURE;
83

            
84
  /**
85
   * @return Runtime::Loader& the singleton runtime loader for the server.
86
   */
87
  virtual Envoy::Runtime::Loader& runtime() PURE;
88

            
89
  /**
90
   * @return Singleton::Manager& the server-wide singleton manager.
91
   */
92
  virtual Singleton::Manager& singletonManager() PURE;
93

            
94
  /**
95
   * @return ProtobufMessage::ValidationContext& validation visitor for xDS and static configuration
96
   *         messages.
97
   */
98
  virtual ProtobufMessage::ValidationContext& messageValidationContext() PURE;
99

            
100
  /**
101
   * @return ProtobufMessage::ValidationVisitor& validation visitor for configuration messages.
102
   */
103
  virtual ProtobufMessage::ValidationVisitor& messageValidationVisitor() PURE;
104

            
105
  /**
106
   * @return Stats::Scope& the context's stats scope.
107
   */
108
  virtual Stats::Scope& scope() PURE;
109

            
110
  /**
111
   * @return Stats::Scope& the server wide stats scope.
112
   */
113
  virtual Stats::Scope& serverScope() PURE;
114

            
115
  /**
116
   * @return ThreadLocal::Instance& the thread local storage engine for the server. This is
117
   *         used to allow runtime lockless updates to configuration, etc. across multiple threads.
118
   */
119
  virtual ThreadLocal::Instance& threadLocal() PURE;
120

            
121
  /**
122
   * @return Upstream::ClusterManager& singleton for use by the entire server.
123
   */
124
  virtual Upstream::ClusterManager& clusterManager() PURE;
125

            
126
  /**
127
   * @return Config::XdsManager& singleton for use by the entire server.
128
   */
129
  virtual Config::XdsManager& xdsManager() PURE;
130

            
131
  /**
132
   * @return const Http::HttpServerPropertiesCacheManager& instance for use by the entire server.
133
   */
134
  virtual Http::HttpServerPropertiesCacheManager& httpServerPropertiesCacheManager() PURE;
135

            
136
  /**
137
   * @return TimeSource& a reference to the time source.
138
   */
139
  virtual TimeSource& timeSource() PURE;
140

            
141
  /**
142
   * @return AccessLogManager for use by the entire server.
143
   */
144
  virtual AccessLog::AccessLogManager& accessLogManager() PURE;
145

            
146
  /**
147
   * @return ServerLifecycleNotifier& the lifecycle notifier for the server.
148
   */
149
  virtual ServerLifecycleNotifier& lifecycleNotifier() PURE;
150

            
151
  /**
152
   * @return the server regex engine.
153
   */
154
  virtual Regex::Engine& regexEngine() PURE;
155
};
156

            
157
/**
158
 * ServerFactoryContext is an specialization of common interface for downstream and upstream network
159
 * filters. The implementation guarantees the lifetime is no shorter than server. It could be used
160
 * across listeners.
161
 */
162
class ServerFactoryContext : public virtual CommonFactoryContext {
163
public:
164
49912
  ~ServerFactoryContext() override = default;
165

            
166
  /**
167
   * @return Http::Context& the server-wide HTTP context.
168
   */
169
  virtual Http::Context& httpContext() PURE;
170

            
171
  /**
172
   * @return Grpc::Context& the server-wide grpc context.
173
   */
174
  virtual Grpc::Context& grpcContext() PURE;
175

            
176
  /**
177
   * @return Router::Context& the server-wide router context.
178
   */
179
  virtual Router::Context& routerContext() PURE;
180

            
181
  /**
182
   * @return ProcessContextOptRef an optional reference to the
183
   * process context. Will be unset when running in validation mode.
184
   */
185
  virtual ProcessContextOptRef processContext() PURE;
186

            
187
  /**
188
   * @return the init manager of the cluster. This can be used for extensions that need
189
   *         to initialize after cluster manager init but before the server starts listening.
190
   *         All extensions should register themselves during configuration load. initialize()
191
   *         will be called on  each registered target after cluster manager init but before the
192
   *         server starts listening. Once all targets have initialized and invoked their callbacks,
193
   *         the server will start listening.
194
   */
195
  virtual Init::Manager& initManager() PURE;
196

            
197
  /**
198
   * @return DrainManager& the server-wide drain manager.
199
   */
200
  virtual Envoy::Server::DrainManager& drainManager() PURE;
201

            
202
  /**
203
   * @return StatsConfig& the servers stats configuration.
204
   */
205
  virtual StatsConfig& statsConfig() PURE;
206

            
207
  /**
208
   * @return envoy::config::bootstrap::v3::Bootstrap& the servers bootstrap configuration.
209
   */
210
  virtual envoy::config::bootstrap::v3::Bootstrap& bootstrap() PURE;
211

            
212
  /**
213
   * @return OverloadManager& the overload manager for the server.
214
   */
215
  virtual OverloadManager& overloadManager() PURE;
216

            
217
  /**
218
   * @return NullOverloadManager& the dummy overload manager for the server for
219
   * listeners that are bypassing a configured OverloadManager
220
   */
221
  virtual OverloadManager& nullOverloadManager() PURE;
222

            
223
  /**
224
   * @return whether external healthchecks are currently failed or not.
225
   */
226
  virtual bool healthCheckFailed() const PURE;
227

            
228
  /**
229
   * @return Ssl::ContextManager& the SSL context manager.
230
   */
231
  virtual Ssl::ContextManager& sslContextManager() PURE;
232

            
233
  /**
234
   * Return the instance of secret manager.
235
   */
236
  virtual Secret::SecretManager& secretManager() PURE;
237
};
238

            
239
// ServerFactoryContextInstance is a thread local singleton that provides access to the
240
// ServerFactoryContext. This will be initialized once the server is created at the start of the
241
// main thread and will be available at the main thread for the lifetime of the server.
242
using ServerFactoryContextInstance = ThreadLocalInjectableSingleton<ServerFactoryContext>;
243

            
244
/**
245
 * Generic factory context for multiple scenarios. This context provides a server factory context
246
 * reference and other resources. Note that except for server factory context, other resources are
247
 * not guaranteed to be available for the entire server lifetime. For example, context powered by a
248
 * listener is only available for the lifetime of the listener.
249
 */
250
class GenericFactoryContext {
251
public:
252
107005
  virtual ~GenericFactoryContext() = default;
253

            
254
  /**
255
   * @return ServerFactoryContext which lifetime is no shorter than the server and provides
256
   *         access to the server's resources.
257
   */
258
  virtual ServerFactoryContext& serverFactoryContext() PURE;
259

            
260
  /**
261
   * @return ProtobufMessage::ValidationVisitor& validation visitor for configuration messages.
262
   */
263
  virtual ProtobufMessage::ValidationVisitor& messageValidationVisitor() PURE;
264

            
265
  /**
266
   * @return Init::Manager& the init manager of the server/listener/cluster/etc, depending on the
267
   *         backend implementation.
268
   */
269
  virtual Init::Manager& initManager() PURE;
270

            
271
  /**
272
   * @return Stats::Scope& the stats scope of the server/listener/cluster/etc, depending on the
273
   *         backend implementation.
274
   */
275
  virtual Stats::Scope& scope() PURE;
276

            
277
  /**
278
   * @return Stats::Scope& the stats scope of the server/listener/cluster/etc, depending on the
279
   *         backend implementation.
280
   * TODO(wbpcode): move all scope() calling to this method.
281
   */
282
4354
  virtual Stats::Scope& statsScope() { return scope(); }
283
};
284

            
285
/**
286
 * Context passed to network and HTTP filters to access server resources.
287
 * TODO(mattklein123): When we lock down visibility of the rest of the code, filters should only
288
 * access the rest of the server via interfaces exposed here.
289
 */
290
class FactoryContext : public virtual GenericFactoryContext {
291
public:
292
51172
  ~FactoryContext() override = default;
293

            
294
  /**
295
   * @return Stats::Scope& the listener's stats scope.
296
   */
297
  virtual Stats::Scope& listenerScope() PURE;
298

            
299
  /**
300
   * @return const Network::DrainDecision& a drain decision that filters can use to determine if
301
   *         they should be doing graceful closes on connections when possible.
302
   */
303
  virtual const Network::DrainDecision& drainDecision() PURE;
304

            
305
  /**
306
   * @return ListenerInfo description of the listener.
307
   */
308
  virtual const Network::ListenerInfo& listenerInfo() const PURE;
309
};
310

            
311
/**
312
 * An implementation of FactoryContext. The life time is no shorter than the created filter chains.
313
 * The life time is no longer than the owning listener. It should be used to create
314
 * NetworkFilterChain.
315
 */
316
class FilterChainFactoryContext : public virtual FactoryContext {
317
public:
318
  /**
319
   * Set the flag that all attached filter chains will be destroyed.
320
   */
321
  virtual void startDraining() PURE;
322
};
323

            
324
using FilterChainFactoryContextPtr = std::unique_ptr<FilterChainFactoryContext>;
325
using FilterChainsByName = absl::flat_hash_map<std::string, Network::DrainableFilterChainSharedPtr>;
326

            
327
// This allows matchers to select the correct filter chain for a route.
328
class FilterChainBaseAction : public Matcher::Action {
329
public:
330
  /**
331
   * Get the filter chain for this request
332
   * @param filter_chains_by_name the configured filter chains
333
   * @param info the stream info for this request
334
   * @ return Network::FilterChain* a pointer to the filter chain for this request.
335
   */
336
  virtual const Network::FilterChain* get(const FilterChainsByName& filter_chains_by_name,
337
                                          const StreamInfo::StreamInfo& info) const PURE;
338
};
339

            
340
/**
341
 * An implementation of FactoryContext. The life time should cover the lifetime of the filter chains
342
 * and connections. It can be used to create ListenerFilterChain.
343
 */
344
class ListenerFactoryContext : public virtual FactoryContext {};
345

            
346
/**
347
 * FactoryContext for ProtocolOptionsFactory.
348
 */
349
using ProtocolOptionsFactoryContext = Server::Configuration::TransportSocketFactoryContext;
350

            
351
/**
352
 * FactoryContext for upstream HTTP filters.
353
 */
354
class UpstreamFactoryContext {
355
public:
356
18276
  virtual ~UpstreamFactoryContext() = default;
357

            
358
  /**
359
   * @return ServerFactoryContext which lifetime is no shorter than the server.
360
   */
361
  virtual ServerFactoryContext& serverFactoryContext() PURE;
362

            
363
  /**
364
   * @return the init manager of the particular context. This can be used for extensions that need
365
   *         to initialize after cluster manager init but before the server starts listening.
366
   *         All extensions should register themselves during configuration load. initialize()
367
   *         will be called on  each registered target after cluster manager init but before the
368
   *         server starts listening. Once all targets have initialized and invoked their callbacks,
369
   *         the server will start listening.
370
   */
371
  virtual Init::Manager& initManager() PURE;
372

            
373
  /*
374
   * @return the stats scope of the cluster. This will last as long as the cluster is valid
375
   * */
376
  virtual Stats::Scope& scope() PURE;
377
};
378

            
379
} // namespace Configuration
380
} // namespace Server
381
} // namespace Envoy