Line data Source code
1 : #pragma once 2 : 3 : #include <vector> 4 : 5 : #include "envoy/admin/v3/config_dump.pb.h" 6 : #include "envoy/config/core/v3/config_source.pb.h" 7 : #include "envoy/config/listener/v3/listener.pb.h" 8 : #include "envoy/config/listener/v3/listener_components.pb.h" 9 : #include "envoy/filter/config_provider_manager.h" 10 : #include "envoy/network/filter.h" 11 : #include "envoy/network/listen_socket.h" 12 : #include "envoy/network/listener.h" 13 : #include "envoy/network/socket_interface.h" 14 : #include "envoy/server/api_listener.h" 15 : #include "envoy/server/drain_manager.h" 16 : #include "envoy/server/filter_config.h" 17 : #include "envoy/server/guarddog.h" 18 : 19 : #include "source/common/protobuf/protobuf.h" 20 : 21 : namespace Envoy { 22 : namespace Filter { 23 : class TcpListenerFilterConfigProviderManagerImpl; 24 : } // namespace Filter 25 : 26 : namespace Server { 27 : 28 : /** 29 : * Interface for an LDS API provider. 30 : */ 31 : class LdsApi { 32 : public: 33 98 : virtual ~LdsApi() = default; 34 : 35 : /** 36 : * @return std::string the last received version by the xDS API for LDS. 37 : */ 38 : virtual std::string versionInfo() const PURE; 39 : }; 40 : 41 : using LdsApiPtr = std::unique_ptr<LdsApi>; 42 : 43 : /** 44 : * Factory for creating listener components. 45 : */ 46 : class ListenerComponentFactory { 47 : public: 48 131 : virtual ~ListenerComponentFactory() = default; 49 : 50 : /** 51 : * @return an LDS API provider. 52 : * @param lds_config supplies the management server configuration. 53 : * @param lds_resources_locator xds::core::v3::ResourceLocator for listener collection. 54 : */ 55 : virtual LdsApiPtr createLdsApi(const envoy::config::core::v3::ConfigSource& lds_config, 56 : const xds::core::v3::ResourceLocator* lds_resources_locator) PURE; 57 : 58 : enum class BindType { 59 : // The listener will not bind. 60 : NoBind, 61 : // The listener will bind a socket shared by all workers. 62 : NoReusePort, 63 : // The listener will use reuse_port sockets independently on each worker. 64 : ReusePort 65 : }; 66 : 67 : /** 68 : * Creates a socket. 69 : * @param address supplies the socket's address. 70 : * @param socket_type the type of socket (stream or datagram) to create. 71 : * @param options to be set on the created socket just before calling 'bind()'. 72 : * @param bind_type supplies the bind type of the listen socket. 73 : * @param creation_options additional options for how to create the socket. 74 : * @param worker_index supplies the socket/worker index of the new socket. 75 : * @return Network::SocketSharedPtr an initialized and potentially bound socket. 76 : */ 77 : virtual Network::SocketSharedPtr createListenSocket( 78 : Network::Address::InstanceConstSharedPtr address, Network::Socket::Type socket_type, 79 : const Network::Socket::OptionsSharedPtr& options, BindType bind_type, 80 : const Network::SocketCreationOptions& creation_options, uint32_t worker_index) PURE; 81 : 82 : /** 83 : * Creates a list of filter factories. 84 : * @param filters supplies the proto configuration. 85 : * @param context supplies the factory creation context. 86 : * @return Filter::NetworkFilterFactoriesList the list of filter factories. 87 : */ 88 : virtual Filter::NetworkFilterFactoriesList createNetworkFilterFactoryList( 89 : const Protobuf::RepeatedPtrField<envoy::config::listener::v3::Filter>& filters, 90 : Server::Configuration::FilterChainFactoryContext& filter_chain_factory_context) PURE; 91 : 92 : /** 93 : * Creates a list of listener filter factories. 94 : * @param filters supplies the JSON configuration. 95 : * @param context supplies the factory creation context. 96 : * @return Filter::ListenerFilterFactoriesList the list of filter factories. 97 : */ 98 : virtual Filter::ListenerFilterFactoriesList createListenerFilterFactoryList( 99 : const Protobuf::RepeatedPtrField<envoy::config::listener::v3::ListenerFilter>& filters, 100 : Configuration::ListenerFactoryContext& context) PURE; 101 : 102 : /** 103 : * Creates a list of UDP listener filter factories. 104 : * @param filters supplies the configuration. 105 : * @param context supplies the factory creation context. 106 : * @return std::vector<Network::UdpListenerFilterFactoryCb> the list of filter factories. 107 : */ 108 : virtual std::vector<Network::UdpListenerFilterFactoryCb> createUdpListenerFilterFactoryList( 109 : const Protobuf::RepeatedPtrField<envoy::config::listener::v3::ListenerFilter>& filters, 110 : Configuration::ListenerFactoryContext& context) PURE; 111 : 112 : /** 113 : * Creates a list of QUIC listener filter factories. 114 : * @param filters supplies the JSON configuration. 115 : * @param context supplies the factory creation context. 116 : * @return Filter::ListenerFilterFactoriesList the list of filter factories. 117 : */ 118 : virtual Filter::QuicListenerFilterFactoriesList createQuicListenerFilterFactoryList( 119 : const Protobuf::RepeatedPtrField<envoy::config::listener::v3::ListenerFilter>& filters, 120 : Configuration::ListenerFactoryContext& context) PURE; 121 : 122 : /** 123 : * @return DrainManagerPtr a new drain manager. 124 : * @param drain_type supplies the type of draining to do for the owning listener. 125 : */ 126 : virtual DrainManagerPtr 127 : createDrainManager(envoy::config::listener::v3::Listener::DrainType drain_type) PURE; 128 : 129 : /** 130 : * @return uint64_t a listener tag usable for connection handler tracking. 131 : */ 132 : virtual uint64_t nextListenerTag() PURE; 133 : 134 : /** 135 : * @return Filter::TcpListenerFilterConfigProviderManagerImpl* the pointer of the TCP listener 136 : * config provider manager. 137 : */ 138 : virtual Filter::TcpListenerFilterConfigProviderManagerImpl* 139 : getTcpListenerConfigProviderManager() PURE; 140 : }; 141 : 142 : /** 143 : * A manager for all listeners and all threaded connection handling workers. 144 : */ 145 : class ListenerManager { 146 : public: 147 : // Indicates listeners to stop. 148 : enum class StopListenersType { 149 : // Listeners in the inbound direction are only stopped. 150 : InboundOnly, 151 : // All listeners are stopped. 152 : All, 153 : }; 154 : 155 : // The types of listeners to be returned from listeners(ListenerState). 156 : // An enum instead of enum class so the underlying type is an int and bitwise operations can be 157 : // used without casting. 158 : enum ListenerState : uint8_t { 159 : ACTIVE = 1 << 0, 160 : WARMING = 1 << 1, 161 : DRAINING = 1 << 2, 162 : ALL = ACTIVE | WARMING | DRAINING 163 : }; 164 : 165 131 : virtual ~ListenerManager() = default; 166 : 167 : /** 168 : * Add or update a listener. Listeners are referenced by a unique name. If no name is provided, 169 : * the manager will allocate a UUID. Listeners that expect to be dynamically updated should 170 : * provide a unique name. The manager will search by name to find the existing listener that 171 : * should be updated. The old listener will be gracefully drained once the new listener is ready 172 : * to take traffic (e.g. when RDS has been initialized). 173 : * @param config supplies the configuration proto. 174 : * @param version_info supplies the xDS version of the listener. 175 : * @param modifiable supplies whether the added listener can be updated or removed. If the 176 : * listener is not modifiable, future calls to this function or removeListener() on behalf 177 : * of this listener will return false. 178 : * @return TRUE if a listener was added or FALSE if the listener was not updated because it is 179 : * a duplicate of the existing listener. This routine will return 180 : * absl::InvalidArgumentError if there is a fundamental error preventing the listener 181 : * from being added or updated. 182 : */ 183 : virtual absl::StatusOr<bool> 184 : addOrUpdateListener(const envoy::config::listener::v3::Listener& config, 185 : const std::string& version_info, bool modifiable) PURE; 186 : 187 : /** 188 : * Instruct the listener manager to create an LDS API provider. This is a separate operation 189 : * during server initialization because the listener manager is created prior to several core 190 : * pieces of the server existing. 191 : * @param lds_config supplies the management server configuration. 192 : * @param lds_resources_locator xds::core::v3::ResourceLocator for listener collection. 193 : */ 194 : virtual void createLdsApi(const envoy::config::core::v3::ConfigSource& lds_config, 195 : const xds::core::v3::ResourceLocator* lds_resources_locator) PURE; 196 : 197 : /** 198 : * @param state the type of listener to be returned (defaults to ACTIVE), states can be OR'd 199 : * together to return multiple different types 200 : * @return std::vector<std::reference_wrapper<Network::ListenerConfig>> a list of currently known 201 : * listeners in the requested state. Note that this routine returns references to the existing 202 : * listeners. The references are only valid in the context of the current call stack and should 203 : * not be stored. 204 : */ 205 : virtual std::vector<std::reference_wrapper<Network::ListenerConfig>> 206 : listeners(ListenerState state = ListenerState::ACTIVE) PURE; 207 : 208 : /** 209 : * @return uint64_t the total number of connections owned by all listeners across all workers. 210 : */ 211 : virtual uint64_t numConnections() const PURE; 212 : 213 : /** 214 : * Remove a listener by name. 215 : * @param name supplies the listener name to remove. 216 : * @return TRUE if the listener was found and removed. Note that when this routine returns TRUE, 217 : * the listener has not necessarily been actually deleted right away. The listener will be 218 : * drained and fully removed at some later time. 219 : */ 220 : virtual bool removeListener(const std::string& name) PURE; 221 : 222 : /** 223 : * Start all workers accepting new connections on all added listeners. 224 : * @param guard_dog supplies the optional guard dog to use for thread watching. 225 : * @param callback supplies the callback to complete server initialization. 226 : */ 227 : virtual void startWorkers(OptRef<GuardDog> guard_dog, std::function<void()> callback) PURE; 228 : 229 : /** 230 : * Stop all listeners from accepting new connections without actually removing any of them. This 231 : * is used for server draining and /drain_listeners admin endpoint. This method directly stops the 232 : * listeners on workers. Once a listener is stopped, any listener modifications are not allowed. 233 : * @param stop_listeners_type indicates listeners to stop. 234 : * @param options additional options passed through to shutdownListener. 235 : */ 236 : virtual void stopListeners(StopListenersType stop_listeners_type, 237 : const Network::ExtraShutdownListenerOptions& options) PURE; 238 : 239 : /** 240 : * Stop all threaded workers from running. When this routine returns all worker threads will 241 : * have exited. 242 : */ 243 : virtual void stopWorkers() PURE; 244 : 245 : /* 246 : * Warn the listener manager of an impending update. This allows the listener to clear per-update 247 : * state. 248 : */ 249 : virtual void beginListenerUpdate() PURE; 250 : 251 : /* 252 : * Inform the listener manager that the update has completed, and informs the listener of any 253 : * errors handled by the reload source. 254 : */ 255 : using FailureStates = std::vector<std::unique_ptr<envoy::admin::v3::UpdateFailureState>>; 256 : virtual void endListenerUpdate(FailureStates&& failure_states) PURE; 257 : 258 : // TODO(junr03): once ApiListeners support warming and draining, this function should return a 259 : // weak_ptr to its caller. This would allow the caller to verify if the 260 : // ApiListener is available to receive API calls on it. 261 : /** 262 : * @return the server's API Listener if it exists, nullopt if it does not. 263 : */ 264 : virtual ApiListenerOptRef apiListener() PURE; 265 : 266 : /* 267 : * @return TRUE if the worker has started or FALSE if not. 268 : */ 269 : virtual bool isWorkerStarted() PURE; 270 : }; 271 : 272 : // overload operator| to allow ListenerManager::listeners(ListenerState) to be called using a 273 : // combination of flags, such as listeners(ListenerState::WARMING|ListenerState::ACTIVE) 274 : constexpr ListenerManager::ListenerState operator|(const ListenerManager::ListenerState lhs, 275 144 : const ListenerManager::ListenerState rhs) { 276 144 : return static_cast<ListenerManager::ListenerState>(static_cast<uint8_t>(lhs) | 277 144 : static_cast<uint8_t>(rhs)); 278 144 : } 279 : 280 : } // namespace Server 281 : } // namespace Envoy