Coverage Report

Created: 2026-11-20 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pdns/pdns/dnsdistdist/dnsdist-tcp-downstream.hh
Line
Count
Source
1
#pragma once
2
3
#include "sstuff.hh"
4
#include "tcpiohandler-mplexer.hh"
5
#include "dnsdist.hh"
6
#include "dnsdist-tcp.hh"
7
8
class ConnectionToBackend : public std::enable_shared_from_this<ConnectionToBackend>
9
{
10
public:
11
  ConnectionToBackend(const std::shared_ptr<DownstreamState>& ds, std::unique_ptr<FDMultiplexer>& mplexer, const struct timeval& now) :
12
0
    d_connectionStartTime(now), d_lastDataReceivedTime(now), d_ds(ds), d_mplexer(mplexer), d_enableFastOpen(ds->d_config.tcpFastOpen)
13
0
  {
14
0
    reconnect();
15
0
  }
16
17
  virtual ~ConnectionToBackend();
18
19
  int getHandle() const
20
0
  {
21
0
    if (!d_handler) {
22
0
      throw std::runtime_error("Attempt to get the socket handle from a non-established TCP connection");
23
0
    }
24
25
0
    return d_handler->getDescriptor();
26
0
  }
27
28
  /* whether the underlying socket has been closed under our feet, basically */
29
  bool isUsable()
30
0
  {
31
0
    if (!d_handler) {
32
0
      d_connectionDied = true;
33
0
      return false;
34
0
    }
35
36
0
    if (d_handler->isUsable()) {
37
0
      return true;
38
0
    }
39
40
0
    d_connectionDied = true;
41
0
    return false;
42
0
  }
43
44
  const std::shared_ptr<DownstreamState>& getDS() const
45
0
  {
46
0
    return d_ds;
47
0
  }
48
49
  const ComboAddress& getRemote() const
50
0
  {
51
0
    return d_ds->d_config.remote;
52
0
  }
53
54
  const std::string& getBackendName() const
55
0
  {
56
0
    return d_ds->getName();
57
0
  }
58
59
  bool isFresh() const
60
0
  {
61
0
    return d_fresh;
62
0
  }
63
64
  void setReused()
65
0
  {
66
0
    d_fresh = false;
67
0
  }
68
69
  void disableFastOpen()
70
0
  {
71
0
    d_enableFastOpen = false;
72
0
  }
73
74
  bool isFastOpenEnabled()
75
0
  {
76
0
    return d_enableFastOpen;
77
0
  }
78
79
  /* whether a connection can be used now */
80
  bool canBeReused(bool sameClient = false) const
81
0
  {
82
0
    if (d_connectionDied) {
83
0
      return false;
84
0
    }
85
86
    /* we can't reuse a connection where a proxy protocol payload has been sent,
87
       since:
88
       - it cannot be reused for a different client
89
       - we might have different TLV values for each query
90
    */
91
0
    if (d_ds && d_ds->d_config.useProxyProtocol == true && !sameClient) {
92
0
      return false;
93
0
    }
94
95
0
    if (reachedMaxStreamID()) {
96
0
      return false;
97
0
    }
98
99
0
    if (reachedMaxConcurrentQueries()) {
100
0
      return false;
101
0
    }
102
103
0
    return true;
104
0
  }
105
106
  /* full now but will become usable later */
107
  bool willBeReusable(bool sameClient) const
108
0
  {
109
0
    if (d_connectionDied || reachedMaxStreamID()) {
110
0
      return false;
111
0
    }
112
113
0
    if (d_ds && d_ds->d_config.useProxyProtocol == true) {
114
0
      return sameClient;
115
0
    }
116
117
0
    return true;
118
0
  }
119
120
  virtual bool reachedMaxStreamID() const = 0;
121
  virtual bool reachedMaxConcurrentQueries() const = 0;
122
  virtual bool isIdle() const = 0;
123
  virtual void release(bool removeFromCache) = 0;
124
  virtual void stopIO()
125
0
  {
126
0
  }
127
128
  bool matches(const std::shared_ptr<DownstreamState>& ds) const
129
0
  {
130
0
    if (!ds || !d_ds) {
131
0
      return false;
132
0
    }
133
0
    return ds == d_ds;
134
0
  }
135
136
  virtual void queueQuery(std::shared_ptr<TCPQuerySender>& sender, TCPQuery&& query) = 0;
137
  virtual void handleTimeout(const struct timeval& now, bool write) = 0;
138
139
  struct timeval getLastDataReceivedTime() const
140
0
  {
141
0
    return d_lastDataReceivedTime;
142
0
  }
143
144
  virtual std::string toString() const = 0;
145
146
  virtual std::shared_ptr<const Logr::Logger> getLogger() const;
147
148
protected:
149
  bool reconnect();
150
151
  std::optional<struct timeval> getBackendHealthCheckTTD(const struct timeval& now) const
152
0
  {
153
0
    if (d_ds == nullptr) {
154
0
      throw std::runtime_error("getBackendReadTTD() without any backend selected");
155
0
    }
156
0
    if (d_ds->d_config.checkTimeout == 0) {
157
0
      return std::nullopt;
158
0
    }
159
0
160
0
    struct timeval res = now;
161
0
    res.tv_sec += d_ds->d_config.checkTimeout / 1000; /* ms to s */
162
0
    res.tv_usec += (d_ds->d_config.checkTimeout % 1000) * 1000; /* remaining ms to µs */
163
0
    normalizeTV(res);
164
0
165
0
    return res;
166
0
  }
167
168
  std::optional<struct timeval> getBackendReadTTD(const struct timeval& now) const
169
0
  {
170
0
    if (d_ds == nullptr) {
171
0
      throw std::runtime_error("getBackendReadTTD() without any backend selected");
172
0
    }
173
0
    if (d_ds->d_config.tcpRecvTimeout == 0) {
174
0
      return std::nullopt;
175
0
    }
176
177
0
    struct timeval res = now;
178
0
    res.tv_sec += d_ds->d_config.tcpRecvTimeout;
179
180
0
    return res;
181
0
  }
182
183
  std::optional<struct timeval> getBackendWriteTTD(const struct timeval& now) const
184
0
  {
185
0
    if (d_ds == nullptr) {
186
0
      throw std::runtime_error("getBackendWriteTTD() called without any backend selected");
187
0
    }
188
0
    if (d_ds->d_config.tcpSendTimeout == 0) {
189
0
      return std::nullopt;
190
0
    }
191
192
0
    struct timeval res = now;
193
0
    res.tv_sec += d_ds->d_config.tcpSendTimeout;
194
195
0
    return res;
196
0
  }
197
198
  std::optional<struct timeval> getBackendConnectTTD(const struct timeval& now) const
199
0
  {
200
0
    if (d_ds == nullptr) {
201
0
      throw std::runtime_error("getBackendConnectTTD() called without any backend selected");
202
0
    }
203
0
    if (d_ds->d_config.tcpConnectTimeout == 0) {
204
0
      return std::nullopt;
205
0
    }
206
207
0
    struct timeval res = now;
208
0
    res.tv_sec += d_ds->d_config.tcpConnectTimeout;
209
210
0
    return res;
211
0
  }
212
213
  struct timeval d_connectionStartTime;
214
  struct timeval d_lastDataReceivedTime;
215
  const std::shared_ptr<DownstreamState> d_ds{nullptr};
216
  std::shared_ptr<TCPQuerySender> d_sender{nullptr};
217
  std::unique_ptr<FDMultiplexer>& d_mplexer;
218
  std::unique_ptr<TCPIOHandler> d_handler{nullptr};
219
  std::unique_ptr<IOStateHandler> d_ioState{nullptr};
220
  uint64_t d_queries{0};
221
  uint32_t d_highestStreamID{0};
222
  uint16_t d_downstreamFailures{0};
223
  bool d_proxyProtocolPayloadSent{false};
224
  bool d_enableFastOpen{false};
225
  bool d_connectionDied{false};
226
  bool d_fresh{true};
227
};
228
229
class TCPConnectionToBackend : public ConnectionToBackend
230
{
231
public:
232
  TCPConnectionToBackend(const std::shared_ptr<DownstreamState>& ds, std::unique_ptr<FDMultiplexer>& mplexer, const struct timeval& now, std::string&& /* proxyProtocolPayload*, unused but there to match the HTTP2 connections, so we can use the same templated connections manager class */) :
233
0
    ConnectionToBackend(ds, mplexer, now), d_responseBuffer(512)
234
0
  {
235
0
  }
236
237
  virtual ~TCPConnectionToBackend();
238
239
  bool isIdle() const override
240
0
  {
241
0
    return d_state == State::idle && d_pendingQueries.size() == 0 && d_pendingResponses.size() == 0;
242
0
  }
243
244
  bool reachedMaxStreamID() const override;
245
246
  size_t getConcurrentQueriesCount() const
247
0
  {
248
0
    return d_pendingQueries.size() + d_pendingResponses.size() + (d_state == State::sendingQueryToBackend ? 1 : 0);
249
0
  }
250
251
  bool reachedMaxConcurrentQueries() const override
252
0
  {
253
0
    const auto concurrent = getConcurrentQueriesCount();
254
0
    if (concurrent > 0 && concurrent >= d_ds->d_config.d_maxInFlightQueriesPerConn) {
255
0
      return true;
256
0
    }
257
0
    return false;
258
0
  }
259
  bool matchesTLVs(const std::unique_ptr<std::vector<ProxyProtocolValue>>& tlvs) const;
260
261
  void queueQuery(std::shared_ptr<TCPQuerySender>& sender, TCPQuery&& query) override;
262
  void handleTimeout(const struct timeval& now, bool write) override;
263
  void release(bool removeFromCache) override;
264
265
  std::string toString() const override
266
0
  {
267
0
    ostringstream o;
268
0
    o << "TCP connection to backend " << (d_ds ? d_ds->getName() : "empty") << " over FD " << (d_handler ? std::to_string(d_handler->getDescriptor()) : "no socket") << ", state is " << (int)d_state << ", io state is " << (d_ioState ? d_ioState->getState() : "empty") << ", queries count is " << d_queries << ", pending queries count is " << d_pendingQueries.size() << ", " << d_pendingResponses.size() << " pending responses";
269
0
    return o.str();
270
0
  }
271
272
  void setProxyProtocolValuesSent(std::unique_ptr<std::vector<ProxyProtocolValue>>&& proxyProtocolValuesSent);
273
274
  std::shared_ptr<const Logr::Logger> getLogger() const override;
275
276
private:
277
  /* waitingForResponseFromBackend is a state where we have not yet started reading the size,
278
     so we can still switch to sending instead */
279
  enum class State : uint8_t
280
  {
281
    idle,
282
    sendingQueryToBackend,
283
    waitingForResponseFromBackend,
284
    readingResponseSizeFromBackend,
285
    readingResponseFromBackend
286
  };
287
  enum class FailureReason : uint8_t
288
  { /* too many attempts */ gaveUp,
289
    timeout,
290
    unexpectedQueryID };
291
292
  static void handleIO(std::shared_ptr<TCPConnectionToBackend>& conn, const struct timeval& now);
293
  static void handleIOCallback(int fd, FDMultiplexer::funcparam_t& param);
294
  static void handleReconnectionAttempt(std::shared_ptr<TCPConnectionToBackend>& conn, const struct timeval& now, IOStateGuard& ioGuard, IOState& iostate, bool& reconnected, bool& connectionDied);
295
  static IOState queueNextQuery(std::shared_ptr<TCPConnectionToBackend>& conn);
296
  static IOState sendQuery(std::shared_ptr<TCPConnectionToBackend>& conn, const struct timeval& now);
297
  static bool isXFRFinished(const TCPResponse& response, TCPQuery& query);
298
299
  IOState handleResponse(std::shared_ptr<TCPConnectionToBackend>& conn, const struct timeval& now);
300
  uint16_t getQueryIdFromResponse() const;
301
  void notifyAllQueriesFailed(const struct timeval& now, FailureReason reason);
302
  bool needProxyProtocolPayload() const
303
0
  {
304
0
    return !d_proxyProtocolPayloadSent && (d_ds && d_ds->d_config.useProxyProtocol);
305
0
  }
306
307
  class PendingRequest
308
  {
309
  public:
310
    std::shared_ptr<TCPQuerySender> d_sender{nullptr};
311
    TCPQuery d_query;
312
  };
313
314
  PacketBuffer d_responseBuffer;
315
  std::list<PendingRequest> d_pendingQueries;
316
  std::unordered_map<uint16_t, PendingRequest> d_pendingResponses;
317
  std::unique_ptr<std::vector<ProxyProtocolValue>> d_proxyProtocolValuesSent{nullptr};
318
  PendingRequest d_currentQuery;
319
  size_t d_currentPos{0};
320
  uint16_t d_responseSize{0};
321
  State d_state{State::idle};
322
  bool d_handlingIO{false};
323
  bool d_lastIOBlocked{false};
324
};
325
326
void setTCPDownstreamMaxIdleConnectionsPerBackend(uint64_t max);
327
void setTCPDownstreamCleanupInterval(uint64_t interval);
328
void setTCPDownstreamMaxIdleTime(uint64_t max);