Coverage Report

Created: 2024-09-19 09:45

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