2 * Copyright (C) 1996-2019 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_PEERSELECTSTATE_H
10 #define SQUID_PEERSELECTSTATE_H
12 #include "AccessLogEntry.h"
13 #include "acl/Checklist.h"
14 #include "base/CbcPointer.h"
15 #include "comm/forward.h"
16 #include "hier_code.h"
17 #include "ip/Address.h"
19 #include "mem/forward.h"
28 void peerSelectInit(void);
30 /// Interface for those who need a list of peers to forward a request to.
31 class PeerSelectionInitiator
: public CbdataParent
34 virtual ~PeerSelectionInitiator() = default;
36 /// called when a new unique destination has been found
37 virtual void noteDestination(Comm::ConnectionPointer path
) = 0;
39 /// called when there will be no more noteDestination() calls
40 /// \param error is a possible reason why no destinations were found; it is
41 /// guaranteed to be nil if there was at least one noteDestination() call
42 virtual void noteDestinationsEnd(ErrorState
*error
) = 0;
44 /// whether noteDestination() and noteDestinationsEnd() calls are allowed
45 bool subscribed
= false;
48 /// Initiates asynchronous peer selection that eventually
49 /// results in zero or more noteDestination() calls and
50 /// exactly one noteDestinationsEnd() call.
51 void startSelectingDestinations(HttpRequest
*request
, const AccessLogEntry::Pointer
&ale
, StoreEntry
*entry
);
56 /// Finds peer (including origin server) IPs for forwarding a single request.
57 /// Gives PeerSelectionInitiator each found destination, in the right order.
58 class PeerSelector
: public Dns::IpReceiver
60 CBDATA_CHILD(PeerSelector
);
63 explicit PeerSelector(PeerSelectionInitiator
*);
64 virtual ~PeerSelector() override
;
66 /* Dns::IpReceiver API */
67 virtual void noteIp(const Ip::Address
&ip
) override
;
68 virtual void noteIps(const Dns::CachedIps
*ips
, const Dns::LookupDetails
&details
) override
;
69 virtual void noteLookup(const Dns::LookupDetails
&details
) override
;
71 // Produce a URL for display identifying the transaction we are
72 // trying to locate a peer for.
73 const SBuf
url() const;
75 /// \returns valid/interested peer initiator or nil
76 PeerSelectionInitiator
*interestedInitiator();
78 /// \returns whether the initiator may use more destinations
79 bool wantsMoreDestinations() const;
81 /// processes a newly discovered/finalized path
82 void handlePath(const Comm::ConnectionPointer
&path
, FwdServer
&fs
);
84 /// a single selection loop iteration: attempts to add more destinations
88 AccessLogEntry::Pointer al
; ///< info for the future access.log entry
91 void *peerCountMcastPeerXXX
= nullptr; ///< a hack to help peerCountMcastPeersStart()
96 bool selectionAborted();
98 void handlePingTimeout();
99 void handleIcpReply(CachePeer
*, const peer_t
, icp_common_t
*header
);
100 void handleIcpParentMiss(CachePeer
*, icp_common_t
*);
102 void handleHtcpParentMiss(CachePeer
*, HtcpReplyData
*);
103 void handleHtcpReply(CachePeer
*, const peer_t
, HtcpReplyData
*);
106 int checkNetdbDirect();
107 void checkAlwaysDirectDone(const allow_t answer
);
108 void checkNeverDirectDone(const allow_t answer
);
110 void selectSomeNeighbor();
111 void selectSomeNeighborReplies();
112 void selectSomeDirect();
113 void selectSomeParent();
114 void selectAllParents();
117 void addSelection(CachePeer
*, const hier_code
);
119 void resolveSelected();
121 static IRCB HandlePingReply
;
122 static ACLCB CheckAlwaysDirectDone
;
123 static ACLCB CheckNeverDirectDone
;
124 static EVH HandlePingTimeout
;
127 allow_t always_direct
;
128 allow_t never_direct
;
129 int direct
; // TODO: fold always_direct/never_direct/prefer_direct into this now that ACL can do a multi-state result.
130 size_t foundPaths
= 0; ///< number of unique destinations identified so far
131 ErrorState
*lastError
;
133 FwdServer
*servers
; ///< a linked list of (unresolved) selected peers
136 * Why are these Ip::Address instead of CachePeer *? Because a
137 * CachePeer structure can become invalid during the CachePeer selection
138 * phase, specifically after a reconfigure. Thus we need to lookup
139 * the CachePeer * based on the address when we are finally ready to
140 * reference the CachePeer structure.
143 Ip::Address first_parent_miss
;
145 Ip::Address closest_parent_miss
;
147 * ->hit can be CachePeer* because it should only be
148 * accessed during the thread when it is set
152 ACLChecklist
*acl_checklist
;
154 typedef CbcPointer
<PeerSelectionInitiator
> Initiator
;
155 Initiator initiator_
; ///< recipient of the destinations we select; use interestedInitiator() to access
157 const InstanceId
<PeerSelector
> id
; ///< unique identification in worker log
160 #endif /* SQUID_PEERSELECTSTATE_H */