2 * Copyright (C) 1996-2020 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 /* DEBUG: section 44 Peer Selection Algorithm */
12 #include "acl/FilledChecklist.h"
13 #include "base/AsyncCbdataCalls.h"
14 #include "base/InstanceId.h"
15 #include "CachePeer.h"
17 #include "client_side.h"
18 #include "dns/LookupDetails.h"
19 #include "errorpage.h"
23 #include "hier_code.h"
25 #include "http/Stream.h"
26 #include "HttpRequest.h"
27 #include "icmp/net_db.h"
31 #include "neighbors.h"
32 #include "peer_sourcehash.h"
33 #include "peer_userhash.h"
34 #include "PeerSelectState.h"
35 #include "SquidConfig.h"
36 #include "SquidTime.h"
38 #include "util.h" // for tvSubDsec() which should be in SquidTime.h
41 * A CachePeer which has been selected as a possible destination.
42 * Listed as pointers here so as to prevent duplicates being added but will
43 * be converted to a set of IP address path options before handing back out
46 * Certain connection flags and outgoing settings will also be looked up and
47 * set based on the received request and CachePeer settings before handing back.
51 MEMPROXY_CLASS(FwdServer
);
54 FwdServer(CachePeer
*p
, hier_code c
) :
60 CbcPointer
<CachePeer
> _peer
; /* NULL --> origin server */
69 static const char *DirectStr
[] = {
76 /// a helper class to report a selected destination (for debugging)
77 class PeerSelectionDumper
80 PeerSelectionDumper(const PeerSelector
* const aSelector
, const CachePeer
* const aPeer
, const hier_code aCode
):
81 selector(aSelector
), peer(aPeer
), code(aCode
) {}
83 const PeerSelector
* const selector
; ///< selection parameters
84 const CachePeer
* const peer
; ///< successful selection info
85 const hier_code code
; ///< selection algorithm
88 CBDATA_CLASS_INIT(PeerSelector
);
90 /// prints PeerSelectionDumper (for debugging)
92 operator <<(std::ostream
&os
, const PeerSelectionDumper
&fsd
)
94 os
<< hier_code_str
[fsd
.code
];
97 os
<< '/' << fsd
.peer
->host
;
98 else if (fsd
.selector
) // useful for DIRECT and gone PINNED destinations
99 os
<< '#' << fsd
.selector
->request
->url
.host();
104 /// An ICP ping timeout service.
105 /// Protects event.cc (which is designed to handle a few unrelated timeouts)
106 /// from exposure to thousands of ping-related timeouts on busy proxies.
107 class PeerSelectorPingMonitor
110 /// registers the given selector to be notified about the IPC ping timeout
111 void monitor(PeerSelector
*);
113 /// removes a PeerSelector from the waiting list
114 void forget(PeerSelector
*);
116 /// \returns a (nil) registration of a non-waiting peer selector
117 WaitingPeerSelectorPosition
npos() { return selectors
.end(); }
120 static void NoteWaitOver(void *monitor
);
126 WaitingPeerSelectors selectors
; ///< \see WaitingPeerSelectors
129 /// monitors all PeerSelector ICP ping timeouts
130 static PeerSelectorPingMonitor
&
133 static const auto Instance
= new PeerSelectorPingMonitor();
137 /* PeerSelectorPingMonitor */
139 /// PeerSelectorPingMonitor::noteWaitOver() wrapper
141 PeerSelectorPingMonitor::NoteWaitOver(void *raw
)
144 static_cast<PeerSelectorPingMonitor
*>(raw
)->noteWaitOver();
147 /// schedules a single event to represent all waiting selectors
149 PeerSelectorPingMonitor::startWaiting()
151 assert(!selectors
.empty());
152 const auto interval
= tvSubDsec(current_time
, selectors
.begin()->first
);
153 eventAdd("PeerSelectorPingMonitor::NoteWaitOver", &PeerSelectorPingMonitor::NoteWaitOver
, this, interval
, 0, false);
156 /// undoes an earlier startWaiting() call
158 PeerSelectorPingMonitor::abortWaiting()
160 // our event may be already in the AsyncCallQueue but that is OK:
161 // such queued calls cannot accumulate, and we ignore any stale ones
162 eventDelete(&PeerSelectorPingMonitor::NoteWaitOver
, nullptr);
165 /// calls back all ready PeerSelectors and continues to wait for others
167 PeerSelectorPingMonitor::noteWaitOver()
169 while (!selectors
.empty() && current_time
>= selectors
.begin()->first
) {
170 const auto selector
= selectors
.begin()->second
;
171 CallBack(selector
->al
, [selector
,this] {
172 selector
->ping
.monitorRegistration
= npos();
173 AsyncCall::Pointer callback
= asyncCall(44, 4, "PeerSelector::HandlePingTimeout",
174 cbdataDialer(PeerSelector::HandlePingTimeout
, selector
));
175 ScheduleCallHere(callback
);
177 selectors
.erase(selectors
.begin());
180 if (!selectors
.empty()) {
181 // Since abortWaiting() is unreliable, we may have been awakened by a
182 // stale event A after event B has been scheduled. Now we are going to
183 // schedule event C. Prevent event accumulation by deleting B (if any).
191 PeerSelectorPingMonitor::monitor(PeerSelector
*selector
)
195 const auto deadline
= selector
->ping
.deadline();
196 const auto position
= selectors
.emplace(deadline
, selector
);
197 selector
->ping
.monitorRegistration
= position
;
199 if (position
== selectors
.begin()) {
200 if (selectors
.size() > 1)
201 abortWaiting(); // remove the previously scheduled earlier event
203 } // else the already scheduled event is still the earliest one
207 PeerSelectorPingMonitor::forget(PeerSelector
*selector
)
211 if (selector
->ping
.monitorRegistration
== npos())
212 return; // already forgotten
214 const auto wasFirst
= selector
->ping
.monitorRegistration
== selectors
.begin();
215 selectors
.erase(selector
->ping
.monitorRegistration
);
216 selector
->ping
.monitorRegistration
= npos();
219 // do not reschedule if there are still elements with the same deadline
220 if (!selectors
.empty() && selectors
.begin()->first
== selector
->ping
.deadline())
223 if (!selectors
.empty())
225 } // else do nothing since the old scheduled event is still the earliest one
230 PeerSelector::~PeerSelector()
233 FwdServer
*next
= servers
->next
;
238 cancelPingTimeoutMonitoring();
241 debugs(44, 3, entry
->url());
242 entry
->ping_status
= PING_DONE
;
246 debugs(44, DBG_IMPORTANT
, "BUG: peer selector gone while waiting for a slow ACL");
247 delete acl_checklist
;
250 HTTPMSGUNLOCK(request
);
253 assert(entry
->ping_status
!= PING_WAITING
);
254 entry
->unlock("peerSelect");
262 PeerSelector::startPingWaiting()
265 assert(entry
->ping_status
!= PING_WAITING
);
266 PingMonitor().monitor(this);
267 entry
->ping_status
= PING_WAITING
;
271 PeerSelector::cancelPingTimeoutMonitoring()
273 PingMonitor().forget(this);
277 peerSelectIcpPing(PeerSelector
*ps
, int direct
, StoreEntry
* entry
)
280 HttpRequest
*request
= ps
->request
;
284 assert(entry
->ping_status
== PING_NONE
);
285 assert(direct
!= DIRECT_YES
);
286 debugs(44, 3, entry
->url());
288 if (!request
->flags
.hierarchical
&& direct
!= DIRECT_NO
)
291 if (EBIT_TEST(entry
->flags
, KEY_PRIVATE
) && !neighbors_do_private_keys
)
292 if (direct
!= DIRECT_NO
)
295 n
= neighborsCount(ps
);
297 debugs(44, 3, "counted " << n
<< " neighbors");
303 peerSelect(PeerSelectionInitiator
*initiator
,
304 HttpRequest
* request
,
305 AccessLogEntry::Pointer
const &al
,
309 debugs(44, 3, *entry
<< ' ' << entry
->url());
311 debugs(44, 3, request
->method
);
313 const auto selector
= new PeerSelector(initiator
);
315 selector
->request
= request
;
316 HTTPMSGLOCK(selector
->request
);
319 selector
->entry
= entry
;
321 #if USE_CACHE_DIGESTS
323 request
->hier
.peer_select_start
= current_time
;
328 selector
->entry
->lock("peerSelect");
330 selector
->selectMore();
334 PeerSelectionInitiator::startSelectingDestinations(HttpRequest
*request
, const AccessLogEntry::Pointer
&ale
, StoreEntry
*entry
)
337 peerSelect(this, request
, ale
, entry
);
338 // and wait for noteDestination() and/or noteDestinationsEnd() calls
342 PeerSelector::checkNeverDirectDone(const Acl::Answer answer
)
344 acl_checklist
= nullptr;
345 debugs(44, 3, answer
);
346 never_direct
= answer
;
349 /** if never_direct says YES, do that. */
351 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (never_direct allow)");
353 case ACCESS_DENIED
: // not relevant.
354 case ACCESS_DUNNO
: // not relevant.
356 case ACCESS_AUTH_REQUIRED
:
357 debugs(44, DBG_IMPORTANT
, "WARNING: never_direct resulted in " << answer
<< ". Username ACLs are not reliable here.");
364 PeerSelector::CheckNeverDirectDone(Acl::Answer answer
, void *data
)
366 static_cast<PeerSelector
*>(data
)->checkNeverDirectDone(answer
);
370 PeerSelector::checkAlwaysDirectDone(const Acl::Answer answer
)
372 acl_checklist
= nullptr;
373 debugs(44, 3, answer
);
374 always_direct
= answer
;
377 /** if always_direct says YES, do that. */
379 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (always_direct allow)");
381 case ACCESS_DENIED
: // not relevant.
382 case ACCESS_DUNNO
: // not relevant.
384 case ACCESS_AUTH_REQUIRED
:
385 debugs(44, DBG_IMPORTANT
, "WARNING: always_direct resulted in " << answer
<< ". Username ACLs are not reliable here.");
392 PeerSelector::CheckAlwaysDirectDone(Acl::Answer answer
, void *data
)
394 static_cast<PeerSelector
*>(data
)->checkAlwaysDirectDone(answer
);
397 /// \returns true (after destroying "this") if the peer initiator is gone
398 /// \returns false (without side effects) otherwise
400 PeerSelector::selectionAborted()
402 if (interestedInitiator())
405 debugs(44, 3, "Aborting peer selection: Initiator gone or lost interest.");
410 /// A single DNS resolution loop iteration: Converts selected FwdServer to IPs.
412 PeerSelector::resolveSelected()
414 if (selectionAborted())
417 FwdServer
*fs
= servers
;
419 // Bug 3243: CVE 2009-0801
420 // Bypass of browser same-origin access control in intercepted communication
421 // To resolve this we must use only the original client destination when going DIRECT
422 // on intercepted traffic which failed Host verification
423 const HttpRequest
*req
= request
;
424 const bool isIntercepted
= !req
->flags
.redirected
&&
425 (req
->flags
.intercepted
|| req
->flags
.interceptTproxy
);
426 const bool useOriginalDst
= Config
.onoff
.client_dst_passthru
|| !req
->flags
.hostVerified
;
427 const bool choseDirect
= fs
&& fs
->code
== HIER_DIRECT
;
428 if (isIntercepted
&& useOriginalDst
&& choseDirect
) {
429 // check the client is still around before using any of its details
430 if (req
->clientConnectionManager
.valid()) {
431 // construct a "result" adding the ORIGINAL_DST to the set instead of DIRECT
432 Comm::ConnectionPointer p
= new Comm::Connection();
433 p
->remote
= req
->clientConnectionManager
->clientConnection
->local
;
434 fs
->code
= ORIGINAL_DST
; // fs->code is DIRECT. This fixes the display.
438 // clear the used fs and continue
445 if (fs
&& fs
->code
== PINNED
) {
446 // Nil path signals a PINNED destination selection. Our initiator should
447 // borrow and use clientConnectionManager's pinned connection object
448 // (regardless of that connection destination).
449 handlePath(nullptr, *fs
);
456 // convert the list of FwdServer destinations into destinations IP addresses
457 if (fs
&& wantsMoreDestinations()) {
458 // send the next one off for DNS lookup.
459 const char *host
= fs
->_peer
.valid() ? fs
->_peer
->host
: request
->url
.host();
460 debugs(44, 2, "Find IP destination for: " << url() << "' via " << host
);
461 Dns::nbgethostbyname(host
, this);
465 // Bug 3605: clear any extra listed FwdServer destinations, when the options exceeds max_foward_tries.
466 // due to the allocation method of fs, we must deallocate each manually.
467 // TODO: use a std::list so we can get the size and abort adding whenever the selection loops reach Config.forward_max_tries
469 assert(fs
== servers
);
477 // done with DNS lookups. pass back to caller
479 debugs(44, 2, id
<< " found all " << foundPaths
<< " destinations for " << url());
480 debugs(44, 2, " always_direct = " << always_direct
);
481 debugs(44, 2, " never_direct = " << never_direct
);
482 debugs(44, 2, " timedout = " << ping
.timedout
);
484 ping
.stop
= current_time
;
485 request
->hier
.ping
= ping
; // final result
487 if (lastError
&& foundPaths
) {
488 // nobody cares about errors if we found destinations despite them
489 debugs(44, 3, "forgetting the last error");
494 if (const auto initiator
= interestedInitiator())
495 initiator
->noteDestinationsEnd(lastError
);
496 lastError
= nullptr; // initiator owns the ErrorState object now
501 PeerSelector::noteLookup(const Dns::LookupDetails
&details
)
503 /* ignore lookup delays that occurred after the initiator moved on */
505 if (selectionAborted())
508 if (!wantsMoreDestinations())
511 request
->recordLookup(details
);
515 PeerSelector::noteIp(const Ip::Address
&ip
)
517 if (selectionAborted())
520 if (!wantsMoreDestinations())
523 const auto peer
= servers
->_peer
.valid();
525 // for TPROXY spoofing, we must skip unusable addresses
526 if (request
->flags
.spoofClientIp
&& !(peer
&& peer
->options
.no_tproxy
) ) {
527 if (ip
.isIPv4() != request
->client_addr
.isIPv4())
528 return; // cannot spoof the client address on this link
531 Comm::ConnectionPointer p
= new Comm::Connection();
533 p
->remote
.port(peer
? peer
->http_port
: request
->url
.port());
534 handlePath(p
, *servers
);
538 PeerSelector::noteIps(const Dns::CachedIps
*ia
, const Dns::LookupDetails
&details
)
540 if (selectionAborted())
543 FwdServer
*fs
= servers
;
545 debugs(44, 3, "Unknown host: " << (fs
->_peer
.valid() ? fs
->_peer
->host
: request
->url
.host()));
546 // discard any previous error.
549 if (fs
->code
== HIER_DIRECT
) {
550 lastError
= new ErrorState(ERR_DNS_FAIL
, Http::scServiceUnavailable
, request
, al
);
551 lastError
->dnsError
= details
.error
;
554 // else noteIp() calls have already processed all IPs in *ia
559 // continue resolving selected peers
564 PeerSelector::checkNetdbDirect()
571 if (direct
== DIRECT_NO
)
574 /* base lookup on RTT and Hops if ICMP NetDB is enabled. */
576 myrtt
= netdbHostRtt(request
->url
.host());
577 debugs(44, 3, "MY RTT = " << myrtt
<< " msec");
578 debugs(44, 3, "minimum_direct_rtt = " << Config
.minDirectRtt
<< " msec");
580 if (myrtt
&& myrtt
<= Config
.minDirectRtt
)
583 myhops
= netdbHostHops(request
->url
.host());
585 debugs(44, 3, "MY hops = " << myhops
);
586 debugs(44, 3, "minimum_direct_hops = " << Config
.minDirectHops
);
588 if (myhops
&& myhops
<= Config
.minDirectHops
)
591 p
= whichPeer(closest_parent_miss
);
596 debugs(44, 3, "closest_parent_miss RTT = " << ping
.p_rtt
<< " msec");
598 if (myrtt
&& myrtt
<= ping
.p_rtt
)
601 #endif /* USE_ICMP */
607 PeerSelector::selectMore()
609 if (selectionAborted())
612 debugs(44, 3, request
->method
<< ' ' << request
->url
.host());
614 /** If we don't know whether DIRECT is permitted ... */
615 if (direct
== DIRECT_UNKNOWN
) {
616 if (always_direct
== ACCESS_DUNNO
) {
617 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (always_direct to be checked)");
618 /** check always_direct; */
619 ACLFilledChecklist
*ch
= new ACLFilledChecklist(Config
.accessList
.AlwaysDirect
, request
, NULL
);
622 acl_checklist
->syncAle(request
, nullptr);
623 acl_checklist
->nonBlockingCheck(CheckAlwaysDirectDone
, this);
625 } else if (never_direct
== ACCESS_DUNNO
) {
626 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (never_direct to be checked)");
627 /** check never_direct; */
628 ACLFilledChecklist
*ch
= new ACLFilledChecklist(Config
.accessList
.NeverDirect
, request
, NULL
);
631 acl_checklist
->syncAle(request
, nullptr);
632 acl_checklist
->nonBlockingCheck(CheckNeverDirectDone
, this);
634 } else if (request
->flags
.noDirect
) {
635 /** if we are accelerating, direct is not an option. */
637 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (forced non-direct)");
638 } else if (request
->flags
.loopDetected
) {
639 /** if we are in a forwarding-loop, direct is not an option. */
641 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (forwarding loop detected)");
642 } else if (checkNetdbDirect()) {
644 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (checkNetdbDirect)");
646 direct
= DIRECT_MAYBE
;
647 debugs(44, 3, "direct = " << DirectStr
[direct
] << " (default)");
650 debugs(44, 3, "direct = " << DirectStr
[direct
]);
653 if (!entry
|| entry
->ping_status
== PING_NONE
)
657 } else if (entry
->ping_status
== PING_NONE
) {
658 selectSomeNeighbor();
660 if (entry
->ping_status
== PING_WAITING
)
662 } else if (entry
->ping_status
== PING_WAITING
) {
663 selectSomeNeighborReplies();
664 cancelPingTimeoutMonitoring();
665 entry
->ping_status
= PING_DONE
;
681 if (Config
.onoff
.prefer_direct
)
684 if (request
->flags
.hierarchical
|| !Config
.onoff
.nonhierarchical_direct
) {
689 if (!Config
.onoff
.prefer_direct
)
695 // end peer selection; start resolving selected peers
699 bool peerAllowedToUse(const CachePeer
*, PeerSelector
*);
701 /// Selects a pinned connection if it exists, is valid, and is allowed.
703 PeerSelector::selectPinned()
705 // TODO: Avoid all repeated calls. Relying on PING_DONE is not enough.
706 if (!request
->pinnedConnection())
709 const auto peer
= request
->pinnedConnection()->pinnedPeer();
710 const auto usePinned
= peer
? peerAllowedToUse(peer
, this) : (direct
!= DIRECT_NO
);
711 // If the pinned connection is prohibited (for this request) then
712 // the initiator must decide whether it is OK to open a new one instead.
713 request
->pinnedConnection()->pinning
.peerAccessDenied
= !usePinned
;
715 addSelection(peer
, PINNED
);
717 entry
->ping_status
= PING_DONE
; // skip ICP
721 * Selects a neighbor (parent or sibling) based on one of the
725 * ICMP Netdb RTT estimates
729 PeerSelector::selectSomeNeighbor()
732 hier_code code
= HIER_NONE
;
733 assert(entry
->ping_status
== PING_NONE
);
735 if (direct
== DIRECT_YES
) {
736 entry
->ping_status
= PING_DONE
;
740 #if USE_CACHE_DIGESTS
741 if ((p
= neighborsDigestSelect(this))) {
742 if (neighborType(p
, request
->url
) == PEER_PARENT
)
743 code
= CD_PARENT_HIT
;
745 code
= CD_SIBLING_HIT
;
748 if ((p
= netdbClosestParent(this))) {
749 code
= CLOSEST_PARENT
;
750 } else if (peerSelectIcpPing(this, direct
, entry
)) {
751 debugs(44, 3, "Doing ICP pings");
752 ping
.start
= current_time
;
753 ping
.n_sent
= neighborsUdpPing(request
,
757 &ping
.n_replies_expected
,
759 // TODO: Refactor neighborsUdpPing() to guarantee positive timeouts.
760 if (ping
.timeout
< 0)
763 if (ping
.n_sent
== 0)
764 debugs(44, DBG_CRITICAL
, "WARNING: neighborsUdpPing returned 0");
765 debugs(44, 3, ping
.n_replies_expected
<<
766 " ICP replies expected, RTT " << ping
.timeout
<<
769 if (ping
.n_replies_expected
> 0) {
775 if (code
!= HIER_NONE
) {
777 addSelection(p
, code
);
780 entry
->ping_status
= PING_DONE
;
783 /// Selects a neighbor (parent or sibling) based on ICP/HTCP replies.
785 PeerSelector::selectSomeNeighborReplies()
788 hier_code code
= HIER_NONE
;
789 assert(entry
->ping_status
== PING_WAITING
);
790 assert(direct
!= DIRECT_YES
);
792 if (checkNetdbDirect()) {
793 code
= CLOSEST_DIRECT
;
794 addSelection(nullptr, code
);
799 code
= hit_type
== PEER_PARENT
? PARENT_HIT
: SIBLING_HIT
;
801 if (!closest_parent_miss
.isAnyAddr()) {
802 p
= whichPeer(closest_parent_miss
);
803 code
= CLOSEST_PARENT_MISS
;
804 } else if (!first_parent_miss
.isAnyAddr()) {
805 p
= whichPeer(first_parent_miss
);
806 code
= FIRST_PARENT_MISS
;
809 if (p
&& code
!= HIER_NONE
) {
810 addSelection(p
, code
);
814 /// Adds a "direct" entry if the request can be forwarded to the origin server.
816 PeerSelector::selectSomeDirect()
818 if (direct
== DIRECT_NO
)
821 /* WAIS is not implemented natively */
822 if (request
->url
.getScheme() == AnyP::PROTO_WAIS
)
825 addSelection(nullptr, HIER_DIRECT
);
829 PeerSelector::selectSomeParent()
832 hier_code code
= HIER_NONE
;
833 debugs(44, 3, request
->method
<< ' ' << request
->url
.host());
835 if (direct
== DIRECT_YES
)
838 if ((p
= peerSourceHashSelectParent(this))) {
839 code
= SOURCEHASH_PARENT
;
841 } else if ((p
= peerUserHashSelectParent(this))) {
842 code
= USERHASH_PARENT
;
844 } else if ((p
= carpSelectParent(this))) {
846 } else if ((p
= getRoundRobinParent(this))) {
847 code
= ROUNDROBIN_PARENT
;
848 } else if ((p
= getWeightedRoundRobinParent(this))) {
849 code
= ROUNDROBIN_PARENT
;
850 } else if ((p
= getFirstUpParent(this))) {
851 code
= FIRSTUP_PARENT
;
852 } else if ((p
= getDefaultParent(this))) {
853 code
= DEFAULT_PARENT
;
856 if (code
!= HIER_NONE
) {
857 addSelection(p
, code
);
861 /// Adds alive parents. Used as a last resort for never_direct.
863 PeerSelector::selectAllParents()
866 /* Add all alive parents */
868 for (p
= Config
.peers
; p
; p
= p
->next
) {
869 /* XXX: neighbors.c lacks a public interface for enumerating
870 * parents to a request so we have to dig some here..
873 if (neighborType(p
, request
->url
) != PEER_PARENT
)
876 if (!peerHTTPOkay(p
, this))
879 addSelection(p
, ANY_OLD_PARENT
);
882 /* XXX: should add dead parents here, but it is currently
883 * not possible to find out which parents are dead or which
884 * simply are not configured to handle the request.
886 /* Add default parent as a last resort */
887 if ((p
= getDefaultParent(this))) {
888 addSelection(p
, DEFAULT_PARENT
);
893 PeerSelector::handlePingTimeout()
895 debugs(44, 3, url());
897 // do nothing if ping reply came while handlePingTimeout() was queued
898 if (!entry
|| entry
->ping_status
!= PING_WAITING
)
901 entry
->ping_status
= PING_DONE
;
903 if (selectionAborted())
906 ++PeerStats
.timeouts
;
912 PeerSelector::HandlePingTimeout(PeerSelector
*selector
)
914 selector
->handlePingTimeout();
920 memset(&PeerStats
, '\0', sizeof(PeerStats
));
924 PeerSelector::handleIcpParentMiss(CachePeer
*p
, icp_common_t
*header
)
929 if (Config
.onoff
.query_icmp
) {
930 if (header
->flags
& ICP_FLAG_SRC_RTT
) {
931 rtt
= header
->pad
& 0xFFFF;
932 int hops
= (header
->pad
>> 16) & 0xFFFF;
934 if (rtt
> 0 && rtt
< 0xFFFF)
935 netdbUpdatePeer(request
->url
, p
, rtt
, hops
);
937 if (rtt
&& (ping
.p_rtt
== 0 || rtt
< ping
.p_rtt
)) {
938 closest_parent_miss
= p
->in_addr
;
943 #endif /* USE_ICMP */
945 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
946 if (p
->options
.closest_only
)
949 /* set FIRST_MISS if there is no CLOSEST parent */
950 if (!closest_parent_miss
.isAnyAddr())
953 rtt
= (tvSubMsec(ping
.start
, current_time
) - p
->basetime
) / p
->weight
;
958 if (first_parent_miss
.isAnyAddr() || rtt
< ping
.w_rtt
) {
959 first_parent_miss
= p
->in_addr
;
965 PeerSelector::handleIcpReply(CachePeer
*p
, const peer_t type
, icp_common_t
*header
)
967 icp_opcode op
= header
->getOpCode();
968 debugs(44, 3, icp_opcode_str
[op
] << ' ' << url());
969 #if USE_CACHE_DIGESTS && 0
970 /* do cd lookup to count false misses */
973 peerNoteDigestLookup(request
, p
,
974 peerDigestLookup(p
, this));
980 if (op
== ICP_MISS
|| op
== ICP_DECHO
) {
981 if (type
== PEER_PARENT
)
982 handleIcpParentMiss(p
, header
);
983 } else if (op
== ICP_HIT
) {
990 if (ping
.n_recv
< ping
.n_replies_expected
)
998 PeerSelector::handleHtcpReply(CachePeer
*p
, const peer_t type
, HtcpReplyData
*htcp
)
1000 debugs(44, 3, (htcp
->hit
? "HIT" : "MISS") << ' ' << url());
1010 if (type
== PEER_PARENT
)
1011 handleHtcpParentMiss(p
, htcp
);
1013 if (ping
.n_recv
< ping
.n_replies_expected
)
1020 PeerSelector::handleHtcpParentMiss(CachePeer
*p
, HtcpReplyData
*htcp
)
1025 if (Config
.onoff
.query_icmp
) {
1026 if (htcp
->cto
.rtt
> 0) {
1027 rtt
= (int) htcp
->cto
.rtt
* 1000;
1028 int hops
= (int) htcp
->cto
.hops
* 1000;
1029 netdbUpdatePeer(request
->url
, p
, rtt
, hops
);
1031 if (rtt
&& (ping
.p_rtt
== 0 || rtt
< ping
.p_rtt
)) {
1032 closest_parent_miss
= p
->in_addr
;
1037 #endif /* USE_ICMP */
1039 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
1040 if (p
->options
.closest_only
)
1043 /* set FIRST_MISS if there is no CLOSEST parent */
1044 if (!closest_parent_miss
.isAnyAddr())
1047 rtt
= (tvSubMsec(ping
.start
, current_time
) - p
->basetime
) / p
->weight
;
1052 if (first_parent_miss
.isAnyAddr() || rtt
< ping
.w_rtt
) {
1053 first_parent_miss
= p
->in_addr
;
1061 PeerSelector::HandlePingReply(CachePeer
* p
, peer_t type
, AnyP::ProtocolType proto
, void *pingdata
, void *data
)
1063 if (proto
== AnyP::PROTO_ICP
)
1064 static_cast<PeerSelector
*>(data
)->handleIcpReply(p
, type
, static_cast<icp_common_t
*>(pingdata
));
1068 else if (proto
== AnyP::PROTO_HTCP
)
1069 static_cast<PeerSelector
*>(data
)->handleHtcpReply(p
, type
, static_cast<HtcpReplyData
*>(pingdata
));
1074 debugs(44, DBG_IMPORTANT
, "ERROR: ignoring an ICP reply with unknown protocol " << proto
);
1078 PeerSelector::addSelection(CachePeer
*peer
, const hier_code code
)
1080 // Find the end of the servers list. Bail on a duplicate destination.
1081 auto **serversTail
= &servers
;
1082 while (const auto server
= *serversTail
) {
1083 // There can be at most one PINNED destination.
1084 // Non-PINNED destinations are uniquely identified by their CachePeer
1085 // (even though a DIRECT destination might match a cache_peer address).
1086 const bool duplicate
= (server
->code
== PINNED
) ?
1087 (code
== PINNED
) : (server
->_peer
== peer
);
1089 debugs(44, 3, "skipping " << PeerSelectionDumper(this, peer
, code
) <<
1090 "; have " << PeerSelectionDumper(this, server
->_peer
.get(), server
->code
));
1093 serversTail
= &server
->next
;
1096 debugs(44, 3, "adding " << PeerSelectionDumper(this, peer
, code
));
1097 *serversTail
= new FwdServer(peer
, code
);
1100 PeerSelector::PeerSelector(PeerSelectionInitiator
*initiator
):
1103 always_direct(Config
.accessList
.AlwaysDirect
?ACCESS_DUNNO
:ACCESS_DENIED
),
1104 never_direct(Config
.accessList
.NeverDirect
?ACCESS_DUNNO
:ACCESS_DENIED
),
1105 direct(DIRECT_UNKNOWN
),
1108 first_parent_miss(),
1109 closest_parent_miss(),
1111 hit_type(PEER_NONE
),
1112 acl_checklist (NULL
),
1113 initiator_(initiator
)
1115 ; // no local defaults.
1119 PeerSelector::url() const
1122 return SBuf(entry
->url());
1125 return request
->effectiveRequestUri();
1127 static const SBuf
noUrl("[no URL]");
1131 /// \returns valid/interested peer initiator or nil
1132 PeerSelectionInitiator
*
1133 PeerSelector::interestedInitiator()
1135 const auto initiator
= initiator_
.valid();
1138 debugs(44, 3, id
<< " initiator gone");
1142 if (!initiator
->subscribed
) {
1143 debugs(44, 3, id
<< " initiator lost interest");
1152 PeerSelector::wantsMoreDestinations() const {
1153 const auto maxCount
= Config
.forward_max_tries
;
1154 return maxCount
>= 0 && foundPaths
<
1155 static_cast<std::make_unsigned
<decltype(maxCount
)>::type
>(maxCount
);
1159 PeerSelector::handlePath(const Comm::ConnectionPointer
&path
, FwdServer
&fs
)
1164 path
->peerType
= fs
.code
;
1165 path
->setPeer(fs
._peer
.get());
1167 // check for a configured outgoing address for this destination...
1168 getOutgoingAddress(request
, path
);
1169 debugs(44, 2, id
<< " found " << path
<< ", destination #" << foundPaths
<< " for " << url());
1171 debugs(44, 2, id
<< " found pinned, destination #" << foundPaths
<< " for " << url());
1173 request
->hier
.ping
= ping
; // may be updated later
1175 debugs(44, 2, " always_direct = " << always_direct
);
1176 debugs(44, 2, " never_direct = " << never_direct
);
1177 debugs(44, 2, " timedout = " << ping
.timedout
);
1179 if (const auto initiator
= interestedInitiator())
1180 initiator
->noteDestination(path
);
1183 InstanceIdDefinitions(PeerSelector
, "PeerSelector");
1185 ping_data::ping_data() :
1188 n_replies_expected(0),
1193 monitorRegistration(PingMonitor().npos())
1202 ping_data::deadline() const
1204 timeval timeInterval
;
1205 timeInterval
.tv_sec
= timeout
/ 1000;
1206 timeInterval
.tv_usec
= (timeout
% 1000) * 1000;
1209 tvAdd(result
, start
, timeInterval
);