FwdState.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_SRC_FWDSTATE_H
10 #define SQUID_SRC_FWDSTATE_H
11 
12 #include "base/forward.h"
13 #include "base/JobWait.h"
14 #include "base/RefCount.h"
15 #include "clients/forward.h"
16 #include "comm.h"
17 #include "comm/Connection.h"
18 #include "error/forward.h"
19 #include "fde.h"
20 #include "http/StatusCode.h"
21 #include "ip/Address.h"
22 #include "ip/forward.h"
23 #include "PeerSelectState.h"
24 #include "ResolvedPeers.h"
25 #include "security/forward.h"
26 #if USE_OPENSSL
27 #include "ssl/support.h"
28 #endif
29 
30 /* forward decls */
31 
34 class HttpRequest;
35 class PconnPool;
38 
39 class HappyConnOpener;
41 
45 
49 
50 class HelperReply;
51 
58 {
59 public:
62 
64  void stop()
65  {
66  if (!stopped) {
67  timer().pause();
68  stopped = true;
69  }
70  }
71 
72 private:
74  Stopwatch &timer();
75 
78 
79  // We cannot rely on timer().ran(): This class eliminates excessive calls
80  // within a single task (e.g., an AsyncJob) while the timer (and its ran()
81  // state) may be shared/affected by multiple concurrent tasks.
83  bool stopped = false;
84 };
85 
87 {
89 
90 public:
92  ~FwdState() override;
93  static void initModule();
94 
96  static void Start(const Comm::ConnectionPointer &client, StoreEntry *, HttpRequest *, const AccessLogEntryPointer &alp);
98  static void fwdStart(const Comm::ConnectionPointer &client, StoreEntry *, HttpRequest *);
100  static time_t ForwardTimeout(const time_t fwdStart);
103  static bool EnoughTimeToReForward(const time_t fwdStart);
104 
108  void useDestinations();
109 
110  void fail(ErrorState *err);
112  void unregister(int fd);
113  void complete();
114 
117  void markStoredReplyAsWhole(const char *whyWeAreSure);
118 
120  int reforward();
121  void serverClosed();
122  void connectStart();
123  void connectDone(const Comm::ConnectionPointer & conn, Comm::Flag status, int xerrno);
124  bool checkRetry();
125  bool checkRetriable();
126  void dispatch();
127 
128  void pconnPush(Comm::ConnectionPointer & conn, const char *domain);
129 
130  bool dontRetry() { return flags.dont_retry; }
131 
132  void dontRetry(bool val) { flags.dont_retry = val; }
133 
135  void closePendingConnection(const Comm::ConnectionPointer &conn, const char *reason);
136 
139 
140 private:
141  // hidden for safer management of self; use static fwdStart
143  void start(Pointer aSelf);
144  void stopAndDestroy(const char *reason);
145 
146  /* PeerSelectionInitiator API */
147  void noteDestination(Comm::ConnectionPointer conn) override;
148  void noteDestinationsEnd(ErrorState *selectionError) override;
149 
150  bool transporting() const;
151 
153 
154 #if STRICT_ORIGINAL_DST
155  void selectPeerForIntercepted();
156 #endif
157  static void logReplyStatus(int tries, const Http::StatusCode status);
158  void doneWithRetries();
159  void completed();
160  void retryOrBail();
161 
162  void usePinned();
163 
166  bool pinnedCanRetry() const;
167 
168  template <typename StepStart>
169  void advanceDestination(const char *stepDescription, const Comm::ConnectionPointer &conn, const StepStart &startStep);
170 
171  ErrorState *makeConnectingError(const err_type type) const;
173  static void RegisterWithCacheManager(void);
174 
180 
182  void closeServerConnection(const char *reason);
183 
184  void syncWithServerConn(const Comm::ConnectionPointer &server, const char *host, const bool reused);
185  void syncHierNote(const Comm::ConnectionPointer &server, const char *host);
186 
188  bool exhaustedTries() const;
189  void updateAttempts(int);
190 
192  time_t connectingTimeout(const Comm::ConnectionPointer &conn) const;
193 
194  void cancelStep(const char *reason);
195 
196  void notifyConnOpener();
197  void reactToZeroSizeObject();
198 
200 
201 public:
205 
207  static void HandleStoreAbort(FwdState *);
208 
209 private:
210  Pointer self;
213  time_t start_t;
214  int n_tries;
215 
216  struct {
221  } flags;
222 
225 
228 
232 
235 
239 
241 
245 
248  const char *storedWholeReply_;
249 
252 };
253 
254 class acl_tos;
256 
259 
261 extern PconnPool *fwdPconnPool;
262 
263 #endif /* SQUID_SRC_FWDSTATE_H */
264 
Ip::NfMarkConfig aclFindNfMarkConfig(acl_nfmark *, ACLChecklist *)
Checks for a netfilter mark value to apply depending on the ACL.
Definition: FwdState.cc:1470
void noteConnection(HappyConnOpenerAnswer &)
Definition: FwdState.cc:840
Stopwatch & timer()
managed Stopwatch object within HierarchyLogEntry
Definition: FwdState.cc:1597
~PeeringActivityTimer()
pauses timer if stop() has not been called
Definition: FwdState.cc:1591
CBDATA_CHILD(FwdState)
Comm::ConnectionPointer serverConn
a successfully opened connection to a server.
Definition: FwdState.h:237
void pause()
ends the current measurement period if needed; requires prior resume()
Definition: Stopwatch.cc:41
time_t start_t
Definition: FwdState.h:213
void dontRetry(bool val)
Definition: FwdState.h:132
Interface for those who need a list of peers to forward a request to.
void ResetMarkingsToServer(HttpRequest *, Comm::Connection &)
Definition: FwdState.cc:1569
void reactToZeroSizeObject()
ERR_ZERO_SIZE_OBJECT requires special adjustments.
Definition: FwdState.cc:476
JobWait< Http::Tunneler > peerWait
Definition: FwdState.h:231
HttpRequestPointer request
the owner of managed HierarchyLogEntry
Definition: FwdState.h:77
bool destinationsFound
at least one candidate path found
Definition: FwdState.h:220
static void initModule()
Definition: FwdState.cc:1390
void syncWithServerConn(const Comm::ConnectionPointer &server, const char *host, const bool reused)
commits to using the given open to-peer connection
Definition: FwdState.cc:1065
void useDestinations()
Definition: FwdState.cc:437
void secureConnectionToPeerIfNeeded(const Comm::ConnectionPointer &)
handles an established TCP connection to peer (including origin servers)
Definition: FwdState.cc:967
AccessLogEntryPointer al
info for the future access.log entry
Definition: FwdState.h:204
struct FwdState::@59 flags
@ raceHappened
Definition: FwdState.h:243
JobWait< HappyConnOpener > transportWait
waits for a transport connection to the peer to be established/opened
Definition: FwdState.h:224
time_t connectingTimeout(const Comm::ConnectionPointer &conn) const
Definition: FwdState.cc:1444
bool connected_okay
TCP link ever opened properly. This affects retry of POST,PUT,CONNECT,etc.
Definition: FwdState.h:217
void advanceDestination(const char *stepDescription, const Comm::ConnectionPointer &conn, const StepStart &startStep)
starts a preparation step for an established connection; retries on failures
Definition: FwdState.cc:820
void connectStart()
Definition: FwdState.cc:1115
unsigned char tos_t
Definition: forward.h:27
tos_t aclMapTOS(acl_tos *, ACLChecklist *)
Checks for a TOS value to apply depending on the ACL.
Definition: FwdState.cc:1458
void usePinned()
send request on an existing connection dedicated to the requesting client
Definition: FwdState.cc:1149
void syncHierNote(const Comm::ConnectionPointer &server, const char *host)
Definition: FwdState.cc:1085
void tunnelEstablishmentDone(Http::TunnelerAnswer &answer)
resumes operations after the (possibly failed) HTTP CONNECT exchange
Definition: FwdState.cc:929
bool dont_retry
Definition: FwdState.h:218
void cancelStep(const char *reason)
Definition: FwdState.cc:201
StatusCode
Definition: StatusCode.h:20
err_type
Definition: forward.h:14
void unregister(Comm::ConnectionPointer &conn)
Definition: FwdState.cc:499
RefCount< FwdState > Pointer
Definition: FwdState.h:91
void fail(ErrorState *err)
Definition: FwdState.cc:458
static void Start(const Comm::ConnectionPointer &client, StoreEntry *, HttpRequest *, const AccessLogEntryPointer &alp)
Initiates request forwarding to a peer or origin server.
Definition: FwdState.cc:338
static void RegisterWithCacheManager(void)
Definition: FwdState.cc:1396
static void HandleStoreAbort(FwdState *)
called by Store if the entry is no longer usable
Definition: FwdState.cc:83
@ racePossible
Definition: FwdState.h:243
void noteDestination(Comm::ConnectionPointer conn) override
called when a new unique destination has been found
Definition: FwdState.cc:587
void stopAndDestroy(const char *reason)
ends forwarding; relies on refcounting so the effect may not be immediate
Definition: FwdState.cc:184
void closeServerConnection(const char *reason)
stops monitoring server connection for closure and updates pconn stats
Definition: FwdState.cc:108
bool exhaustedTries() const
whether we have used up all permitted forwarding attempts
Definition: FwdState.cc:1416
bool forward_completed
Definition: FwdState.h:219
PconnRace
possible pconn race states
Definition: FwdState.h:243
bool dontRetry()
Definition: FwdState.h:130
void dispatch()
Definition: FwdState.cc:1186
bool pinnedCanRetry() const
Definition: FwdState.cc:1422
void updateAleWithFinalError()
updates ALE when we finalize the transaction error (if any)
Definition: FwdState.cc:241
const Comm::ConnectionPointer & serverConnection() const
Definition: FwdState.h:138
void doneWithRetries()
Definition: FwdState.cc:796
void getOutgoingAddress(HttpRequest *, const Comm::ConnectionPointer &)
Definition: FwdState.cc:1481
void successfullyConnectedToPeer(const Comm::ConnectionPointer &)
called when all negotiations with the peer have been completed
Definition: FwdState.cc:1050
void updateAttempts(int)
sets n_tries to the given value (while keeping ALE, if any, in sync)
Definition: FwdState.cc:1095
void establishTunnelThruProxy(const Comm::ConnectionPointer &)
Definition: FwdState.cc:908
JobWait< Security::PeerConnector > encryptionWait
waits for the established transport connection to be secured/encrypted
Definition: FwdState.h:227
bool waitingForDispatched
whether we are waiting for the last dispatch()ed activity to end
Definition: FwdState.h:234
Comm::ConnectionPointer clientConn
a possibly open connection to the client.
Definition: FwdState.h:212
void noteDestinationsEnd(ErrorState *selectionError) override
Definition: FwdState.cc:616
FwdState(const Comm::ConnectionPointer &client, StoreEntry *, HttpRequest *, const AccessLogEntryPointer &alp)
Definition: FwdState.cc:120
AsyncCall::Pointer closeHandler
The serverConn close handler.
Definition: FwdState.h:240
int n_tries
the number of forwarding attempts so far
Definition: FwdState.h:214
PeeringActivityTimer(const HttpRequestPointer &)
resumes timer
Definition: FwdState.cc:1585
RefCount< AccessLogEntry > AccessLogEntryPointer
Definition: FwdState.h:32
~FwdState() override
Definition: FwdState.cc:305
static bool EnoughTimeToReForward(const time_t fwdStart)
Definition: FwdState.cc:431
static time_t ForwardTimeout(const time_t fwdStart)
time left to finish the whole forwarding process (which started at fwdStart)
Definition: FwdState.cc:423
void retryOrBail()
Definition: FwdState.cc:773
bool checkRetry()
Definition: FwdState.cc:691
ErrorState * err
Definition: FwdState.h:211
void completed()
Definition: FwdState.cc:256
ErrorState * makeConnectingError(const err_type type) const
static void logReplyStatus(int tries, const Http::StatusCode status)
Definition: FwdState.cc:1402
void GetMarkingsToServer(HttpRequest *request, Comm::Connection &conn)
Definition: FwdState.cc:1560
Final result (an open connection or an error) sent to the job initiator.
Flag
Definition: Flag.h:15
PeerConnectionPointer conn
const char * storedWholeReply_
Definition: FwdState.h:248
a netfilter mark/mask pair
Definition: NfMarkConfig.h:22
void complete()
Definition: FwdState.cc:526
int reforward()
Definition: FwdState.cc:1308
@ raceImpossible
Definition: FwdState.h:243
static char server[MAXLINE]
void connectedToPeer(Security::EncryptorAnswer &answer)
called when all negotiations with the TLS-speaking peer have been completed
Definition: FwdState.cc:1014
RefCount< ResolvedPeers > ResolvedPeersPointer
Definition: FwdState.h:36
PeeringActivityTimer peeringTimer
Measures time spent on selecting and communicating with peers.
Definition: FwdState.h:251
PeerConnectionPointer destinationReceipt
peer selection result (or nil)
Definition: FwdState.h:238
StoreEntry * entry
Definition: FwdState.h:202
void pconnPush(Comm::ConnectionPointer &conn, const char *domain)
void secureConnectionToPeer(const Comm::ConnectionPointer &)
encrypts an established TCP connection to peer (including origin servers)
Definition: FwdState.cc:996
static void fwdStart(const Comm::ConnectionPointer &client, StoreEntry *, HttpRequest *)
Same as Start() but no master xaction info (AccessLogEntry) available.
Definition: FwdState.cc:407
PconnRace pconnRace
current pconn race state
Definition: FwdState.h:244
void connectDone(const Comm::ConnectionPointer &conn, Comm::Flag status, int xerrno)
void markStoredReplyAsWhole(const char *whyWeAreSure)
Definition: FwdState.cc:575
bool transporting() const
Definition: FwdState.cc:569
void start(Pointer aSelf)
Definition: FwdState.cc:145
bool stopped
Whether the task is done participating in the managed activity.
Definition: FwdState.h:83
HttpRequest * request
Definition: FwdState.h:203
void serverClosed()
Definition: FwdState.cc:748
void closePendingConnection(const Comm::ConnectionPointer &conn, const char *reason)
get rid of a to-server connection that failed to become serverConn
Definition: FwdState.cc:96
PconnPool * fwdPconnPool
a collection of previously used persistent Squid-to-peer HTTP(S) connections
Definition: FwdState.cc:78
void stop()
pauses timer if stop() has not been called
Definition: FwdState.h:64
bool checkRetriable()
Whether we may try sending this request again after a failure.
Definition: FwdState.cc:735
void handleUnregisteredServerEnd()
Definition: FwdState.cc:804
ResolvedPeersPointer destinations
paths for forwarding the request
Definition: FwdState.h:236
void notifyConnOpener()
makes sure connection opener knows that the destinations have changed
Definition: FwdState.cc:661

 

Introduction

Documentation

Support

Miscellaneous