Line data Source code
1 : #pragma once 2 : 3 : #include <cstdint> 4 : #include <memory> 5 : 6 : #include "envoy/common/random_generator.h" 7 : #include "envoy/network/address.h" 8 : #include "envoy/network/connection.h" 9 : #include "envoy/network/connection_balancer.h" 10 : #include "envoy/network/filter.h" 11 : #include "envoy/network/listen_socket.h" 12 : #include "envoy/network/listener.h" 13 : #include "envoy/runtime/runtime.h" 14 : #include "envoy/server/overload/thread_local_overload_state.h" 15 : #include "envoy/ssl/context.h" 16 : 17 : #include "source/common/common/interval_value.h" 18 : 19 : namespace Envoy { 20 : namespace Network { 21 : 22 : // This interface allows for a listener to perform an alternative behavior when a 23 : // packet can't be routed correctly during draining; for example QUIC packets that 24 : // are not for an existing connection. 25 : // This is currently supported for QUIC listeners to forward packets to the child instance. 26 : // TODO(mattklein123): determine if other UDP listeners have a reason to do this. 27 : class NonDispatchedUdpPacketHandler { 28 : public: 29 : virtual ~NonDispatchedUdpPacketHandler() = default; 30 : virtual void handle(uint32_t worker_index, const Network::UdpRecvData& packet) PURE; 31 : }; 32 : 33 : // Additional options for ConnectionHandler::ActiveListener::shutdownListener. 34 : // As a struct so that in the event of future additional parameters, the change 35 : // is isolated rather than cascading through all layers, mocks, etc. 36 : struct ExtraShutdownListenerOptions { 37 : OptRef<NonDispatchedUdpPacketHandler> non_dispatched_udp_packet_handler_; 38 : }; 39 : 40 : /** 41 : * Abstract connection handler. 42 : */ 43 : class ConnectionHandler { 44 : public: 45 665 : virtual ~ConnectionHandler() = default; 46 : 47 : /** 48 : * @return uint64_t the number of active connections owned by the handler. 49 : */ 50 : virtual uint64_t numConnections() const PURE; 51 : 52 : /** 53 : * Increment the return value of numConnections() by one. 54 : * TODO(mattklein123): re-visit the connection accounting interface. Make TCP 55 : * listener to do accounting through these interfaces instead of directly 56 : * access the counter. 57 : */ 58 : virtual void incNumConnections() PURE; 59 : 60 : /** 61 : * Decrement the return value of numConnections() by one. 62 : */ 63 : virtual void decNumConnections() PURE; 64 : 65 : /** 66 : * Adds a listener to the handler, optionally replacing the existing listener. 67 : * @param overridden_listener tag of the existing listener. nullopt if no previous listener. 68 : * @param config listener configuration options. 69 : * @param runtime the runtime for the server. 70 : * @param random a random number generator. 71 : */ 72 : virtual void addListener(absl::optional<uint64_t> overridden_listener, ListenerConfig& config, 73 : Runtime::Loader& runtime, Random::RandomGenerator& random) PURE; 74 : 75 : /** 76 : * Remove listeners using the listener tag as a key. All connections owned by the removed 77 : * listeners will be closed. 78 : * @param listener_tag supplies the tag passed to addListener(). 79 : */ 80 : virtual void removeListeners(uint64_t listener_tag) PURE; 81 : 82 : /** 83 : * Remove the filter chains and the connections in the listener. All connections owned 84 : * by the filter chains will be closed. Once all the connections are destroyed(connections 85 : * could be deferred deleted!), invoke the completion. 86 : * @param listener_tag supplies the tag passed to addListener(). 87 : * @param filter_chains supplies the filter chains to be removed. 88 : */ 89 : virtual void removeFilterChains(uint64_t listener_tag, 90 : const std::list<const FilterChain*>& filter_chains, 91 : std::function<void()> completion) PURE; 92 : 93 : /** 94 : * Stop listeners using the listener tag as a key. This will not close any connections and is used 95 : * for draining. 96 : * @param listener_tag supplies the tag passed to addListener(). 97 : * @param options additional options to be passed through to shutdownListener. 98 : */ 99 : virtual void stopListeners(uint64_t listener_tag, 100 : const Network::ExtraShutdownListenerOptions& options) PURE; 101 : 102 : /** 103 : * Stop all listeners. This will not close any connections and is used for draining. 104 : */ 105 : virtual void stopListeners() PURE; 106 : 107 : /** 108 : * Disable all listeners. This will not close any connections and is used to temporarily 109 : * stop accepting connections on all listeners. 110 : */ 111 : virtual void disableListeners() PURE; 112 : 113 : /** 114 : * Enable all listeners. This is used to re-enable accepting connections on all listeners 115 : * after they have been temporarily disabled. 116 : */ 117 : virtual void enableListeners() PURE; 118 : 119 : /** 120 : * Set the fraction of connections the listeners should reject. 121 : * @param reject_fraction a value between 0 (reject none) and 1 (reject all). 122 : */ 123 : virtual void setListenerRejectFraction(UnitFloat reject_fraction) PURE; 124 : 125 : /** 126 : * @return the stat prefix used for per-handler stats. 127 : */ 128 : virtual const std::string& statPrefix() const PURE; 129 : 130 : /** 131 : * Used by ConnectionHandler to manage listeners. 132 : */ 133 : class ActiveListener { 134 : public: 135 607 : virtual ~ActiveListener() = default; 136 : 137 : /** 138 : * @return the tag value as configured. 139 : */ 140 : virtual uint64_t listenerTag() PURE; 141 : 142 : /** 143 : * @return the actual Listener object. 144 : */ 145 : virtual Listener* listener() PURE; 146 : 147 : /** 148 : * Temporarily stop listening according to implementation's own definition. 149 : */ 150 : virtual void pauseListening() PURE; 151 : 152 : /** 153 : * Resume listening according to implementation's own definition. 154 : */ 155 : virtual void resumeListening() PURE; 156 : 157 : /** 158 : * Stop listening according to implementation's own definition. 159 : * @param options provides extra options that some subset of listeners might 160 : * use, e.g. Quic listeners may need to configure packet forwarding 161 : * during hot restart. 162 : */ 163 : virtual void shutdownListener(const ExtraShutdownListenerOptions& options) PURE; 164 : 165 : /** 166 : * Update the listener config. 167 : */ 168 : virtual void updateListenerConfig(Network::ListenerConfig& config) PURE; 169 : 170 : /** 171 : * Called when the given filter chains are about to be removed. 172 : */ 173 : virtual void onFilterChainDraining( 174 : const std::list<const Network::FilterChain*>& draining_filter_chains) PURE; 175 : }; 176 : 177 : using ActiveListenerPtr = std::unique_ptr<ActiveListener>; 178 : 179 : /** 180 : * Used by ConnectionHandler to manage UDP listeners. 181 : */ 182 : class ActiveUdpListener : public virtual ActiveListener, public Network::UdpListenerCallbacks { 183 : public: 184 0 : ~ActiveUdpListener() override = default; 185 : 186 : /** 187 : * Returns the worker index that ``data`` should be delivered to. The return value must be in 188 : * the range [0, concurrency). 189 : */ 190 : virtual uint32_t destination(const Network::UdpRecvData& data) const PURE; 191 : }; 192 : 193 : using ActiveUdpListenerPtr = std::unique_ptr<ActiveUdpListener>; 194 : }; 195 : 196 : using ConnectionHandlerPtr = std::unique_ptr<ConnectionHandler>; 197 : 198 : /** 199 : * The connection handler from the view of a tcp listener. 200 : */ 201 : class TcpConnectionHandler : public virtual ConnectionHandler { 202 : public: 203 : virtual Event::Dispatcher& dispatcher() PURE; 204 : 205 : /** 206 : * Obtain the rebalancer of the tcp listener. 207 : * @param listener_tag supplies the tag of the tcp listener that was passed to addListener(). 208 : * @param address is used to query the address specific handler. 209 : * @return BalancedConnectionHandlerOptRef the balancer attached to the listener. `nullopt` if 210 : * listener doesn't exist or rebalancer doesn't exist. 211 : */ 212 : virtual BalancedConnectionHandlerOptRef 213 : getBalancedHandlerByTag(uint64_t listener_tag, const Network::Address::Instance& address) PURE; 214 : 215 : /** 216 : * Obtain the rebalancer of the tcp listener. 217 : * @param address supplies the address of the tcp listener. 218 : * @return BalancedConnectionHandlerOptRef the balancer attached to the listener. ``nullopt`` if 219 : * listener doesn't exist or rebalancer doesn't exist. 220 : */ 221 : virtual BalancedConnectionHandlerOptRef 222 : getBalancedHandlerByAddress(const Network::Address::Instance& address) PURE; 223 : 224 : /** 225 : * Creates a TCP listener on a specific port. 226 : * @param socket supplies the socket to listen on. 227 : * @param cb supplies the callbacks to invoke for listener events. 228 : * @param runtime supplies the runtime for this server. 229 : * @param listener_config configuration for the TCP listener to be created. 230 : * @return Network::ListenerPtr a new listener that is owned by the caller. 231 : */ 232 : virtual Network::ListenerPtr 233 : createListener(Network::SocketSharedPtr&& socket, Network::TcpListenerCallbacks& cb, 234 : Runtime::Loader& runtime, Random::RandomGenerator& random, 235 : const Network::ListenerConfig& listener_config, 236 : Server::ThreadLocalOverloadStateOptRef overload_state) PURE; 237 : }; 238 : 239 : /** 240 : * The connection handler from the view of a udp listener. 241 : */ 242 : class UdpConnectionHandler : public virtual ConnectionHandler { 243 : public: 244 : /** 245 : * Get the ``UdpListenerCallbacks`` associated with ``listener_tag`` and ``address``. This will be 246 : * absl::nullopt for non-UDP listeners and for ``listener_tag`` values that have already been 247 : * removed. 248 : */ 249 : virtual UdpListenerCallbacksOptRef 250 : getUdpListenerCallbacks(uint64_t listener_tag, const Network::Address::Instance& address) PURE; 251 : }; 252 : 253 : /** 254 : * A registered factory interface to create different kinds of ActiveUdpListener. 255 : */ 256 : class ActiveUdpListenerFactory { 257 : public: 258 126 : virtual ~ActiveUdpListenerFactory() = default; 259 : 260 : /** 261 : * Creates an ActiveUdpListener object and a corresponding UdpListener 262 : * according to given config. 263 : * @param runtime the runtime for this server. 264 : * @param worker_index The index of the worker this listener is being created on. 265 : * @param parent is the owner of the created ActiveListener objects. 266 : * @param listen_socket_ptr is the UDP socket. 267 : * @param dispatcher is used to create actual UDP listener. 268 : * @param config provides information needed to create ActiveUdpListener and 269 : * UdpListener objects. 270 : * @return the ActiveUdpListener created. 271 : */ 272 : virtual ConnectionHandler::ActiveUdpListenerPtr 273 : createActiveUdpListener(Runtime::Loader& runtime, uint32_t worker_index, 274 : UdpConnectionHandler& parent, 275 : Network::SocketSharedPtr&& listen_socket_ptr, 276 : Event::Dispatcher& dispatcher, Network::ListenerConfig& config) PURE; 277 : 278 : /** 279 : * @return true if the UDP passing through listener doesn't form stateful connections. 280 : */ 281 : virtual bool isTransportConnectionless() const PURE; 282 : 283 : /** 284 : * @return socket options specific to this factory that should be applied to all sockets. 285 : */ 286 : virtual const Network::Socket::OptionsSharedPtr& socketOptions() const PURE; 287 : }; 288 : 289 : using ActiveUdpListenerFactoryPtr = std::unique_ptr<ActiveUdpListenerFactory>; 290 : 291 : /** 292 : * Internal listener callbacks. 293 : */ 294 : class InternalListener : public virtual ConnectionHandler::ActiveListener { 295 : public: 296 : /** 297 : * Called when a new connection is accepted. 298 : * @param socket supplies the socket that is moved into the callee. 299 : */ 300 : virtual void onAccept(ConnectionSocketPtr&& socket) PURE; 301 : }; 302 : 303 : using InternalListenerPtr = std::unique_ptr<InternalListener>; 304 : using InternalListenerOptRef = OptRef<InternalListener>; 305 : 306 : /** 307 : * The query interface of the registered internal listener callbacks. 308 : */ 309 : class InternalListenerManager { 310 : public: 311 665 : virtual ~InternalListenerManager() = default; 312 : 313 : /** 314 : * Return the internal listener binding the listener address. 315 : * 316 : * @param listen_address the internal address of the expected internal listener. 317 : */ 318 : virtual InternalListenerOptRef 319 : findByAddress(const Address::InstanceConstSharedPtr& listen_address) PURE; 320 : }; 321 : 322 : using InternalListenerManagerOptRef = 323 : absl::optional<std::reference_wrapper<InternalListenerManager>>; 324 : 325 : // The thread local registry. 326 : class LocalInternalListenerRegistry { 327 : public: 328 0 : virtual ~LocalInternalListenerRegistry() = default; 329 : 330 : // Set the internal listener manager which maintains life of internal listeners. Called by 331 : // connection handler. 332 : virtual void setInternalListenerManager(InternalListenerManager& internal_listener_manager) PURE; 333 : 334 : // Get the internal listener manager to obtain a listener. Called by client connection factory. 335 : virtual InternalListenerManagerOptRef getInternalListenerManager() PURE; 336 : 337 : // Create a new active internal listener. Called by the server connection handler. 338 : virtual InternalListenerPtr createActiveInternalListener(ConnectionHandler& conn_handler, 339 : ListenerConfig& config, 340 : Event::Dispatcher& dispatcher) PURE; 341 : }; 342 : 343 : // The central internal listener registry interface providing the thread local accessor. 344 : class InternalListenerRegistry { 345 : public: 346 0 : virtual ~InternalListenerRegistry() = default; 347 : 348 : /** 349 : * @return The thread local registry. 350 : */ 351 : virtual LocalInternalListenerRegistry* getLocalRegistry() PURE; 352 : }; 353 : 354 : } // namespace Network 355 : } // namespace Envoy