Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/envoy/tcp/upstream.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include "envoy/buffer/buffer.h"
4
#include "envoy/extensions/filters/network/tcp_proxy/v3/tcp_proxy.pb.h"
5
#include "envoy/http/filter.h"
6
#include "envoy/http/header_evaluator.h"
7
#include "envoy/stream_info/stream_info.h"
8
#include "envoy/tcp/conn_pool.h"
9
#include "envoy/upstream/upstream.h"
10
11
#include "source/common/router/router.h"
12
13
namespace Envoy {
14
15
namespace Upstream {
16
class LoadBalancerContext;
17
class ThreadLocalCluster;
18
} // namespace Upstream
19
20
namespace TcpProxy {
21
22
class GenericConnectionPoolCallbacks;
23
class GenericUpstream;
24
25
/**
26
 * A configuration for an individual tunneling TCP over HTTP protocols.
27
 */
28
class TunnelingConfigHelper {
29
public:
30
0
  virtual ~TunnelingConfigHelper() = default;
31
32
  // The host name of the tunneling upstream HTTP request.
33
  // This function evaluates command operators if specified. Otherwise it returns host name as is.
34
  virtual std::string host(const StreamInfo::StreamInfo& stream_info) const PURE;
35
36
  // The method of the upstream HTTP request. True if using POST method, CONNECT otherwise.
37
  virtual bool usePost() const PURE;
38
39
  // The path used for POST method.
40
  virtual const std::string& postPath() const PURE;
41
42
  // The evaluator to add additional HTTP request headers to the upstream request.
43
  virtual Envoy::Http::HeaderEvaluator& headerEvaluator() const PURE;
44
45
  // Save HTTP response headers to the downstream filter state.
46
  virtual void
47
  propagateResponseHeaders(Http::ResponseHeaderMapPtr&& headers,
48
                           const StreamInfo::FilterStateSharedPtr& filter_state) const PURE;
49
50
  // Save HTTP response trailers to the downstream filter state.
51
  virtual void
52
  propagateResponseTrailers(Http::ResponseTrailerMapPtr&& trailers,
53
                            const StreamInfo::FilterStateSharedPtr& filter_state) const PURE;
54
  virtual const Envoy::Router::FilterConfig& routerFilterConfig() const PURE;
55
  virtual Server::Configuration::ServerFactoryContext& serverFactoryContext() const PURE;
56
};
57
58
using TunnelingConfigHelperOptConstRef = OptRef<const TunnelingConfigHelper>;
59
60
// An API for wrapping either a TCP or an HTTP connection pool.
61
class GenericConnPool : public Event::DeferredDeletable,
62
                        public Logger::Loggable<Logger::Id::router> {
63
public:
64
  ~GenericConnPool() override = default;
65
66
  /**
67
   * Called to create a TCP connection or HTTP stream for "CONNECT" streams.
68
   *
69
   * The implementation is then responsible for calling either onGenericPoolReady or
70
   * onGenericPoolFailure on the supplied GenericConnectionPoolCallbacks.
71
   *
72
   * @param callbacks callbacks to communicate stream failure or creation on.
73
   */
74
  virtual void newStream(GenericConnectionPoolCallbacks& callbacks) PURE;
75
};
76
77
// An API for the UpstreamRequest to get callbacks from either an HTTP or TCP
78
// connection pool.
79
class GenericConnectionPoolCallbacks {
80
public:
81
0
  virtual ~GenericConnectionPoolCallbacks() = default;
82
83
  /**
84
   * Called when GenericConnPool::newStream has established a new stream.
85
   *
86
   * @param info supplies the stream info object associated with the upstream connection.
87
   * @param upstream supplies the generic upstream for the stream.
88
   * @param host supplies the description of the host that will carry the request.
89
   * @param address_provider supplies the address provider of the upstream connection.
90
   * @param ssl_info supplies the ssl information of the upstream connection.
91
   */
92
  virtual void onGenericPoolReady(StreamInfo::StreamInfo* info,
93
                                  std::unique_ptr<GenericUpstream>&& upstream,
94
                                  Upstream::HostDescriptionConstSharedPtr& host,
95
                                  const Network::ConnectionInfoProvider& address_provider,
96
                                  Ssl::ConnectionInfoConstSharedPtr ssl_info) PURE;
97
98
  /**
99
   * Called to indicate a failure for GenericConnPool::newStream to establish a stream.
100
   *
101
   * @param reason supplies the failure reason.
102
   * @param failure_reason failure reason string (Note: it is expected that the caller will provide
103
   * matching `reason` and `failure_reason`).
104
   * @param host supplies the description of the host that caused the failure. This may be nullptr
105
   *             if no host was involved in the failure (for example overflow).
106
   */
107
  virtual void onGenericPoolFailure(ConnectionPool::PoolFailureReason reason,
108
                                    absl::string_view failure_reason,
109
                                    Upstream::HostDescriptionConstSharedPtr host) PURE;
110
};
111
112
// Interface for a generic Upstream, which can communicate with a TCP or HTTP
113
// upstream.
114
class GenericUpstream : public Event::DeferredDeletable {
115
public:
116
  ~GenericUpstream() override = default;
117
118
  /**
119
   * Enable/disable further data from this stream.
120
   *
121
   * @param disable true if the stream should be read disabled, false otherwise.
122
   * @return returns true if the disable is performed, false otherwise
123
   *         (e.g. if the connection is closed)
124
   */
125
  virtual bool readDisable(bool disable) PURE;
126
127
  /**
128
   * Encodes data upstream.
129
   * @param data supplies the data to encode. The data may be moved by the encoder.
130
   * @param end_stream supplies whether this is the last data to encode.
131
   */
132
  virtual void encodeData(Buffer::Instance& data, bool end_stream) PURE;
133
134
  /**
135
   * Adds a callback to be called when the data is sent to the kernel.
136
   * @param cb supplies the callback to be called
137
   */
138
  virtual void addBytesSentCallback(Network::Connection::BytesSentCb cb) PURE;
139
140
  /**
141
   * Called when an event is received on the downstream connection
142
   * @param event supplies the event which occurred.
143
   * @return the underlying ConnectionData if the event is not "Connected" and draining
144
             is supported for this upstream.
145
   */
146
  virtual Tcp::ConnectionPool::ConnectionData*
147
  onDownstreamEvent(Network::ConnectionEvent event) PURE;
148
149
  /* Called to convert underlying transport socket from non-secure mode
150
   * to secure mode. Implemented only by start_tls transport socket.
151
   */
152
  virtual bool startUpstreamSecureTransport() PURE;
153
154
  /**
155
   * Called when upstream starttls socket is converted to tls and upstream ssl info
156
   * needs to be set in the connection's stream_info.
157
   * @return the const SSL connection data of upstream.
158
   */
159
  virtual Ssl::ConnectionInfoConstSharedPtr getUpstreamConnectionSslInfo() PURE;
160
};
161
162
using GenericConnPoolPtr = std::unique_ptr<GenericConnPool>;
163
164
/*
165
 * A factory for creating generic connection pools.
166
 */
167
class GenericConnPoolFactory : public Envoy::Config::TypedFactory {
168
public:
169
  ~GenericConnPoolFactory() override = default;
170
171
  /*
172
   * @param thread_local_cluster the thread local cluster to use for conn pool creation.
173
   * @param config the tunneling config, if doing connect tunneling.
174
   * @param context the load balancing context for this connection.
175
   * @param upstream_callbacks the callbacks to provide to the connection if successfully created.
176
   * @param downstream_info is the downstream connection stream info.
177
   * @return may be null if there is no cluster with the given name.
178
   */
179
  virtual GenericConnPoolPtr
180
  createGenericConnPool(Upstream::ThreadLocalCluster& thread_local_cluster,
181
                        TunnelingConfigHelperOptConstRef config,
182
                        Upstream::LoadBalancerContext* context,
183
                        Tcp::ConnectionPool::UpstreamCallbacks& upstream_callbacks,
184
                        Http::StreamDecoderFilterCallbacks& stream_decoder_callbacks,
185
                        StreamInfo::StreamInfo& downstream_info) const PURE;
186
};
187
188
using GenericConnPoolFactoryPtr = std::unique_ptr<GenericConnPoolFactory>;
189
190
} // namespace TcpProxy
191
} // namespace Envoy