1
#pragma once
2

            
3
#include <chrono>
4
#include <cstdint>
5
#include <functional>
6
#include <list>
7
#include <memory>
8
#include <string>
9
#include <utility>
10

            
11
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
12
#include "envoy/config/trace/v3/http_tracer.pb.h"
13
#include "envoy/config/typed_config.h"
14
#include "envoy/filter/config_provider_manager.h"
15
#include "envoy/http/filter.h"
16
#include "envoy/network/filter.h"
17
#include "envoy/server/configuration.h"
18
#include "envoy/server/filter_config.h"
19
#include "envoy/server/instance.h"
20

            
21
#include "source/common/common/logger.h"
22
#include "source/common/network/resolver_impl.h"
23
#include "source/common/network/utility.h"
24

            
25
namespace Envoy {
26
namespace Server {
27
namespace Configuration {
28

            
29
/**
30
 * Implemented for each Stats::Sink and registered via Registry::registerFactory() or
31
 * the convenience class RegisterFactory.
32
 */
33
class StatsSinkFactory : public Config::TypedFactory {
34
public:
35
3
  ~StatsSinkFactory() override = default;
36

            
37
  /**
38
   * Create a particular Stats::Sink implementation. If the implementation is unable to produce a
39
   * Stats::Sink with the provided parameters, it should throw an EnvoyException. The returned
40
   * pointer should always be valid when status is OK.
41
   * @param config supplies the custom proto configuration for the Stats::Sink
42
   * @param server supplies the server instance
43
   */
44
  virtual absl::StatusOr<Stats::SinkPtr>
45
  createStatsSink(const Protobuf::Message& config,
46
                  Server::Configuration::ServerFactoryContext& server) PURE;
47

            
48
53
  std::string category() const override { return "envoy.stats_sinks"; }
49
};
50

            
51
class StatsConfigImpl : public StatsConfig {
52
public:
53
  StatsConfigImpl(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
54
                  absl::Status& creation_status);
55

            
56
22556
  const std::list<Stats::SinkPtr>& sinks() const override { return sinks_; }
57
22560
  std::chrono::milliseconds flushInterval() const override { return flush_interval_; }
58
10724
  bool flushOnAdmin() const override { return flush_on_admin_; }
59
11894
  uint32_t evictOnFlush() const override { return evict_on_flush_; }
60

            
61
27
  void addSink(Stats::SinkPtr sink) { sinks_.emplace_back(std::move(sink)); }
62
17243
  bool enableDeferredCreationStats() const override {
63
17243
    return deferred_stat_options_.enable_deferred_creation_stats();
64
17243
  }
65

            
66
private:
67
  std::list<Stats::SinkPtr> sinks_;
68
  std::chrono::milliseconds flush_interval_;
69
  bool flush_on_admin_{false};
70
  const envoy::config::bootstrap::v3::Bootstrap::DeferredStatOptions deferred_stat_options_;
71
  uint32_t evict_on_flush_{0};
72
};
73

            
74
/**
75
 * Utilities for creating a filter chain for a network connection.
76
 */
77
class FilterChainUtility {
78
public:
79
  /**
80
   * Given a connection and a list of factories, create a new filter chain. Chain creation will
81
   * exit early if any filters immediately close the connection.
82
   */
83
  static bool buildFilterChain(Network::FilterManager& filter_manager,
84
                               const Filter::NetworkFilterFactoriesList& factories);
85

            
86
  /**
87
   * Given a ListenerFilterManager and a list of factories, create a new filter chain. Chain
88
   * creation will exit early if any filters immediately close the connection.
89
   *
90
   * TODO(sumukhs): Coalesce with the above as they are very similar
91
   */
92
  static bool buildFilterChain(Network::ListenerFilterManager& filter_manager,
93
                               const Filter::ListenerFilterFactoriesList& factories);
94

            
95
  /**
96
   * Given a UdpListenerFilterManager and a list of factories, create a new filter chain. Chain
97
   * creation will exit early if any filters immediately close the connection.
98
   */
99
  static void
100
  buildUdpFilterChain(Network::UdpListenerFilterManager& filter_manager,
101
                      Network::UdpReadFilterCallbacks& callbacks,
102
                      const std::vector<Network::UdpListenerFilterFactoryCb>& factories);
103

            
104
  /**
105
   * Given a QuicListenerFilterManager and a list of factories, create a new filter chain. Chain
106
   * creation will exit early if any filters don't have a valid config.
107
   *
108
   * TODO(sumukhs): Coalesce with the above as they are very similar
109
   */
110
  static bool buildQuicFilterChain(Network::QuicListenerFilterManager& filter_manager,
111
                                   const Filter::QuicListenerFilterFactoriesList& factories);
112
};
113

            
114
/**
115
 * Implementation of Server::Configuration::Main that reads a configuration from
116
 * a JSON file.
117
 */
118
class MainImpl : Logger::Loggable<Logger::Id::config>, public Main {
119
public:
120
  /**
121
   * MainImpl is created in two phases. In the first phase it is
122
   * default-constructed without a configuration as part of the server. The
123
   * server won't be fully populated yet. initialize() applies the
124
   * configuration in the second phase, as it requires a fully populated server.
125
   *
126
   * @param bootstrap v2 bootstrap proto.
127
   * @param server supplies the owning server.
128
   * @param cluster_manager_factory supplies the cluster manager creation factory.
129
   * @return a status indicating initialization success or failure.
130
   */
131
  absl::Status initialize(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
132
                          Instance& server,
133
                          Upstream::ClusterManagerFactory& cluster_manager_factory);
134

            
135
  // Server::Configuration::Main
136
317790
  Upstream::ClusterManager* clusterManager() override { return cluster_manager_.get(); }
137
8
  const Upstream::ClusterManager* clusterManager() const override { return cluster_manager_.get(); }
138
39876
  StatsConfig& statsConfig() override { return *stats_config_; }
139
10677
  const Watchdog& mainThreadWatchdogConfig() const override { return *main_thread_watchdog_; }
140
10661
  const Watchdog& workerWatchdogConfig() const override { return *worker_watchdog_; }
141

            
142
private:
143
  /**
144
   * Initialize tracers and corresponding sinks.
145
   */
146
  void initializeTracers(const envoy::config::trace::v3::Tracing& configuration, Instance& server);
147

            
148
  /**
149
   * Initialize stats configuration.
150
   */
151
  absl::Status initializeStatsConfig(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
152
                                     Instance& server);
153

            
154
  /**
155
   * Initialize watchdog(s). Call before accessing any watchdog configuration.
156
   */
157
  absl::Status initializeWatchdogs(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
158
                                   Instance& server);
159

            
160
  std::unique_ptr<Upstream::ClusterManager> cluster_manager_;
161
  std::unique_ptr<StatsConfigImpl> stats_config_;
162
  std::unique_ptr<Watchdog> main_thread_watchdog_;
163
  std::unique_ptr<Watchdog> worker_watchdog_;
164
};
165

            
166
class WatchdogImpl : public Watchdog {
167
public:
168
  WatchdogImpl(const envoy::config::bootstrap::v3::Watchdog& watchdog, Instance& server);
169

            
170
21330
  std::chrono::milliseconds missTimeout() const override { return miss_timeout_; }
171
21328
  std::chrono::milliseconds megaMissTimeout() const override { return megamiss_timeout_; }
172
42664
  std::chrono::milliseconds killTimeout() const override { return kill_timeout_; }
173
42656
  std::chrono::milliseconds multiKillTimeout() const override { return multikill_timeout_; }
174
21328
  double multiKillThreshold() const override { return multikill_threshold_; }
175
  Protobuf::RepeatedPtrField<envoy::config::bootstrap::v3::Watchdog::WatchdogAction>
176
21328
  actions() const override {
177
21328
    return actions_;
178
21328
  }
179

            
180
private:
181
  std::chrono::milliseconds miss_timeout_;
182
  std::chrono::milliseconds megamiss_timeout_;
183
  std::chrono::milliseconds kill_timeout_;
184
  std::chrono::milliseconds multikill_timeout_;
185
  double multikill_threshold_;
186
  Protobuf::RepeatedPtrField<envoy::config::bootstrap::v3::Watchdog::WatchdogAction> actions_;
187
};
188

            
189
/**
190
 * Initial configuration that reads from JSON.
191
 */
192
class InitialImpl : public Initial {
193
public:
194
  InitialImpl(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
195
              absl::Status& creation_status);
196

            
197
  // Server::Configuration::Initial
198
74753
  Admin& admin() override { return admin_; }
199
10671
  absl::optional<std::string> flagsPath() const override { return flags_path_; }
200
10857
  const envoy::config::bootstrap::v3::LayeredRuntime& runtime() override {
201
10857
    return layered_runtime_;
202
10857
  }
203

            
204
  /**
205
   * Initialize admin access log.
206
   */
207
  void initAdminAccessLog(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
208
                          FactoryContext& factory_context);
209

            
210
private:
211
  struct AdminImpl : public Admin {
212
    // Server::Configuration::Initial::Admin
213
10671
    const std::string& profilePath() const override { return profile_path_; }
214
32136
    Network::Address::InstanceConstSharedPtr address() override { return address_; }
215
10636
    Network::Socket::OptionsSharedPtr socketOptions() override { return socket_options_; }
216
10639
    AccessLog::InstanceSharedPtrVector accessLogs() const override { return access_logs_; }
217
10671
    bool ignoreGlobalConnLimit() const override { return ignore_global_conn_limit_; }
218

            
219
    std::string profile_path_;
220
    AccessLog::InstanceSharedPtrVector access_logs_;
221
    Network::Address::InstanceConstSharedPtr address_;
222
    Network::Socket::OptionsSharedPtr socket_options_;
223
    bool ignore_global_conn_limit_;
224
  };
225

            
226
  AdminImpl admin_;
227
  absl::optional<std::string> flags_path_;
228
  envoy::config::bootstrap::v3::LayeredRuntime layered_runtime_;
229
};
230

            
231
} // namespace Configuration
232
} // namespace Server
233
} // namespace Envoy