Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/server/configuration_impl.h
Line
Count
Source (jump to first uncovered line)
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
  ~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.
41
   * @param config supplies the custom proto configuration for the Stats::Sink
42
   * @param server supplies the server instance
43
   */
44
  virtual Stats::SinkPtr createStatsSink(const Protobuf::Message& config,
45
                                         Server::Configuration::ServerFactoryContext& server) PURE;
46
47
32
  std::string category() const override { return "envoy.stats_sinks"; }
48
};
49
50
class StatsConfigImpl : public StatsConfig {
51
public:
52
  StatsConfigImpl(const envoy::config::bootstrap::v3::Bootstrap& bootstrap);
53
54
7.60k
  const std::list<Stats::SinkPtr>& sinks() const override { return sinks_; }
55
7.54k
  std::chrono::milliseconds flushInterval() const override { return flush_interval_; }
56
3.81k
  bool flushOnAdmin() const override { return flush_on_admin_; }
57
58
0
  void addSink(Stats::SinkPtr sink) { sinks_.emplace_back(std::move(sink)); }
59
3.26k
  bool enableDeferredCreationStats() const override {
60
3.26k
    return deferred_stat_options_.enable_deferred_creation_stats();
61
3.26k
  }
62
63
private:
64
  std::list<Stats::SinkPtr> sinks_;
65
  std::chrono::milliseconds flush_interval_;
66
  bool flush_on_admin_{false};
67
  const envoy::config::bootstrap::v3::Bootstrap::DeferredStatOptions deferred_stat_options_;
68
};
69
70
/**
71
 * Utilities for creating a filter chain for a network connection.
72
 */
73
class FilterChainUtility {
74
public:
75
  /**
76
   * Given a connection and a list of factories, create a new filter chain. Chain creation will
77
   * exit early if any filters immediately close the connection.
78
   */
79
  static bool buildFilterChain(Network::FilterManager& filter_manager,
80
                               const Filter::NetworkFilterFactoriesList& factories);
81
82
  /**
83
   * Given a ListenerFilterManager and a list of factories, create a new filter chain. Chain
84
   * creation will exit early if any filters immediately close the connection.
85
   *
86
   * TODO(sumukhs): Coalesce with the above as they are very similar
87
   */
88
  static bool buildFilterChain(Network::ListenerFilterManager& filter_manager,
89
                               const Filter::ListenerFilterFactoriesList& factories);
90
91
  /**
92
   * Given a UdpListenerFilterManager and a list of factories, create a new filter chain. Chain
93
   * creation will exit early if any filters immediately close the connection.
94
   */
95
  static void
96
  buildUdpFilterChain(Network::UdpListenerFilterManager& filter_manager,
97
                      Network::UdpReadFilterCallbacks& callbacks,
98
                      const std::vector<Network::UdpListenerFilterFactoryCb>& factories);
99
100
  /**
101
   * Given a QuicListenerFilterManager and a list of factories, create a new filter chain. Chain
102
   * creation will exit early if any filters don't have a valid config.
103
   *
104
   * TODO(sumukhs): Coalesce with the above as they are very similar
105
   */
106
  static bool buildQuicFilterChain(Network::QuicListenerFilterManager& filter_manager,
107
                                   const Filter::QuicListenerFilterFactoriesList& factories);
108
};
109
110
/**
111
 * Implementation of Server::Configuration::Main that reads a configuration from
112
 * a JSON file.
113
 */
114
class MainImpl : Logger::Loggable<Logger::Id::config>, public Main {
115
public:
116
  /**
117
   * MainImpl is created in two phases. In the first phase it is
118
   * default-constructed without a configuration as part of the server. The
119
   * server won't be fully populated yet. initialize() applies the
120
   * configuration in the second phase, as it requires a fully populated server.
121
   *
122
   * @param bootstrap v2 bootstrap proto.
123
   * @param server supplies the owning server.
124
   * @param cluster_manager_factory supplies the cluster manager creation factory.
125
   */
126
  void initialize(const envoy::config::bootstrap::v3::Bootstrap& bootstrap, Instance& server,
127
                  Upstream::ClusterManagerFactory& cluster_manager_factory);
128
129
  // Server::Configuration::Main
130
106k
  Upstream::ClusterManager* clusterManager() override { return cluster_manager_.get(); }
131
0
  const Upstream::ClusterManager* clusterManager() const override { return cluster_manager_.get(); }
132
10.8k
  StatsConfig& statsConfig() override { return *stats_config_; }
133
3.81k
  const Watchdog& mainThreadWatchdogConfig() const override { return *main_thread_watchdog_; }
134
3.81k
  const Watchdog& workerWatchdogConfig() const override { return *worker_watchdog_; }
135
136
private:
137
  /**
138
   * Initialize tracers and corresponding sinks.
139
   */
140
  void initializeTracers(const envoy::config::trace::v3::Tracing& configuration, Instance& server);
141
142
  /**
143
   * Initialize stats configuration.
144
   */
145
  void initializeStatsConfig(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
146
                             Instance& server);
147
148
  /**
149
   * Initialize watchdog(s). Call before accessing any watchdog configuration.
150
   */
151
  void initializeWatchdogs(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
152
                           Instance& server);
153
154
  std::unique_ptr<Upstream::ClusterManager> cluster_manager_;
155
  std::unique_ptr<StatsConfigImpl> stats_config_;
156
  std::unique_ptr<Watchdog> main_thread_watchdog_;
157
  std::unique_ptr<Watchdog> worker_watchdog_;
158
};
159
160
class WatchdogImpl : public Watchdog {
161
public:
162
  WatchdogImpl(const envoy::config::bootstrap::v3::Watchdog& watchdog, Instance& server);
163
164
7.62k
  std::chrono::milliseconds missTimeout() const override { return miss_timeout_; }
165
7.62k
  std::chrono::milliseconds megaMissTimeout() const override { return megamiss_timeout_; }
166
15.2k
  std::chrono::milliseconds killTimeout() const override { return kill_timeout_; }
167
15.2k
  std::chrono::milliseconds multiKillTimeout() const override { return multikill_timeout_; }
168
7.62k
  double multiKillThreshold() const override { return multikill_threshold_; }
169
  Protobuf::RepeatedPtrField<envoy::config::bootstrap::v3::Watchdog::WatchdogAction>
170
7.62k
  actions() const override {
171
7.62k
    return actions_;
172
7.62k
  }
173
174
private:
175
  std::chrono::milliseconds miss_timeout_;
176
  std::chrono::milliseconds megamiss_timeout_;
177
  std::chrono::milliseconds kill_timeout_;
178
  std::chrono::milliseconds multikill_timeout_;
179
  double multikill_threshold_;
180
  Protobuf::RepeatedPtrField<envoy::config::bootstrap::v3::Watchdog::WatchdogAction> actions_;
181
};
182
183
/**
184
 * Initial configuration that reads from JSON.
185
 */
186
class InitialImpl : public Initial {
187
public:
188
  InitialImpl(const envoy::config::bootstrap::v3::Bootstrap& bootstrap);
189
190
  // Server::Configuration::Initial
191
32.7k
  Admin& admin() override { return admin_; }
192
5.34k
  absl::optional<std::string> flagsPath() const override { return flags_path_; }
193
5.33k
  const envoy::config::bootstrap::v3::LayeredRuntime& runtime() override {
194
5.33k
    return layered_runtime_;
195
5.33k
  }
196
197
  /**
198
   * Initialize admin access log.
199
   */
200
  void initAdminAccessLog(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
201
                          Instance& server);
202
203
private:
204
  struct AdminImpl : public Admin {
205
    // Server::Configuration::Initial::Admin
206
5.34k
    const std::string& profilePath() const override { return profile_path_; }
207
16.7k
    Network::Address::InstanceConstSharedPtr address() override { return address_; }
208
2.64k
    Network::Socket::OptionsSharedPtr socketOptions() override { return socket_options_; }
209
2.64k
    std::list<AccessLog::InstanceSharedPtr> accessLogs() const override { return access_logs_; }
210
5.34k
    bool ignoreGlobalConnLimit() const override { return ignore_global_conn_limit_; }
211
212
    std::string profile_path_;
213
    std::list<AccessLog::InstanceSharedPtr> access_logs_;
214
    Network::Address::InstanceConstSharedPtr address_;
215
    Network::Socket::OptionsSharedPtr socket_options_;
216
    bool ignore_global_conn_limit_;
217
  };
218
219
  AdminImpl admin_;
220
  absl::optional<std::string> flags_path_;
221
  envoy::config::bootstrap::v3::LayeredRuntime layered_runtime_;
222
};
223
224
} // namespace Configuration
225
} // namespace Server
226
} // namespace Envoy