2 * Copyright (C) 1996-2017 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 "CachePeer.h"
15 #include "client_side.h"
16 #include "dns/LookupDetails.h"
17 #include "errorpage.h"
21 #include "hier_code.h"
23 #include "http/Stream.h"
24 #include "HttpRequest.h"
25 #include "icmp/net_db.h"
29 #include "neighbors.h"
30 #include "peer_sourcehash.h"
31 #include "peer_userhash.h"
32 #include "PeerSelectState.h"
33 #include "SquidConfig.h"
34 #include "SquidTime.h"
42 static const char *DirectStr
[] = {
49 static void peerSelectFoo(ps_state
*);
50 static void peerPingTimeout(void *data
);
51 static IRCB peerHandlePingReply
;
52 static void peerIcpParentMiss(CachePeer
*, icp_common_t
*, ps_state
*);
54 static void peerHtcpParentMiss(CachePeer
*, HtcpReplyData
*, ps_state
*);
55 static void peerHandleHtcpReply(CachePeer
*, peer_t
, HtcpReplyData
*, void *);
57 static int peerCheckNetdbDirect(ps_state
* psstate
);
58 static void peerGetSomeNeighbor(ps_state
*);
59 static void peerGetSomeNeighborReplies(ps_state
*);
60 static void peerGetSomeDirect(ps_state
*);
61 static void peerGetSomeParent(ps_state
*);
62 static void peerGetAllParents(ps_state
*);
63 static void peerAddFwdServer(FwdServer
**, CachePeer
*, hier_code
);
64 static void peerSelectPinned(ps_state
* ps
);
65 static void peerSelectDnsResults(const ipcache_addrs
*ia
, const Dns::LookupDetails
&details
, void *data
);
67 CBDATA_CLASS_INIT(ps_state
);
72 FwdServer
*next
= servers
->next
;
78 debugs(44, 3, entry
->url());
80 if (entry
->ping_status
== PING_WAITING
)
81 eventDelete(peerPingTimeout
, this);
83 entry
->ping_status
= PING_DONE
;
87 debugs(44, DBG_IMPORTANT
, "calling aclChecklistFree() from ps_state destructor");
91 HTTPMSGUNLOCK(request
);
94 assert(entry
->ping_status
!= PING_WAITING
);
95 entry
->unlock("peerSelect");
103 peerSelectIcpPing(HttpRequest
* request
, int direct
, StoreEntry
* entry
)
107 assert(entry
->ping_status
== PING_NONE
);
108 assert(direct
!= DIRECT_YES
);
109 debugs(44, 3, "peerSelectIcpPing: " << entry
->url());
111 if (!request
->flags
.hierarchical
&& direct
!= DIRECT_NO
)
114 if (EBIT_TEST(entry
->flags
, KEY_PRIVATE
) && !neighbors_do_private_keys
)
115 if (direct
!= DIRECT_NO
)
118 n
= neighborsCount(request
);
120 debugs(44, 3, "peerSelectIcpPing: counted " << n
<< " neighbors");
126 peerSelect(Comm::ConnectionList
* paths
,
127 HttpRequest
* request
,
128 AccessLogEntry::Pointer
const &al
,
136 debugs(44, 3, *entry
<< ' ' << entry
->url());
138 debugs(44, 3, request
->method
);
140 psstate
= new ps_state
;
142 psstate
->request
= request
;
143 HTTPMSGLOCK(psstate
->request
);
146 psstate
->entry
= entry
;
147 psstate
->paths
= paths
;
149 psstate
->callback
= callback
;
151 psstate
->callback_data
= cbdataReference(callback_data
);
153 #if USE_CACHE_DIGESTS
155 request
->hier
.peer_select_start
= current_time
;
160 psstate
->entry
->lock("peerSelect");
162 peerSelectFoo(psstate
);
166 peerCheckNeverDirectDone(allow_t answer
, void *data
)
168 ps_state
*psstate
= (ps_state
*) data
;
169 psstate
->acl_checklist
= NULL
;
170 debugs(44, 3, "peerCheckNeverDirectDone: " << answer
);
171 psstate
->never_direct
= answer
;
174 /** if never_direct says YES, do that. */
175 psstate
->direct
= DIRECT_NO
;
176 debugs(44, 3, HERE
<< "direct = " << DirectStr
[psstate
->direct
] << " (never_direct allow)");
178 case ACCESS_DENIED
: // not relevant.
179 case ACCESS_DUNNO
: // not relevant.
181 case ACCESS_AUTH_REQUIRED
:
182 debugs(44, DBG_IMPORTANT
, "WARNING: never_direct resulted in " << answer
<< ". Username ACLs are not reliable here.");
185 peerSelectFoo(psstate
);
189 peerCheckAlwaysDirectDone(allow_t answer
, void *data
)
191 ps_state
*psstate
= (ps_state
*)data
;
192 psstate
->acl_checklist
= NULL
;
193 debugs(44, 3, "peerCheckAlwaysDirectDone: " << answer
);
194 psstate
->always_direct
= answer
;
197 /** if always_direct says YES, do that. */
198 psstate
->direct
= DIRECT_YES
;
199 debugs(44, 3, HERE
<< "direct = " << DirectStr
[psstate
->direct
] << " (always_direct allow)");
201 case ACCESS_DENIED
: // not relevant.
202 case ACCESS_DUNNO
: // not relevant.
204 case ACCESS_AUTH_REQUIRED
:
205 debugs(44, DBG_IMPORTANT
, "WARNING: always_direct resulted in " << answer
<< ". Username ACLs are not reliable here.");
208 peerSelectFoo(psstate
);
212 peerSelectDnsPaths(ps_state
*psstate
)
214 FwdServer
*fs
= psstate
->servers
;
216 if (!cbdataReferenceValid(psstate
->callback_data
)) {
217 debugs(44, 3, "Aborting peer selection. Parent Job went away.");
222 // Bug 3243: CVE 2009-0801
223 // Bypass of browser same-origin access control in intercepted communication
224 // To resolve this we must use only the original client destination when going DIRECT
225 // on intercepted traffic which failed Host verification
226 const HttpRequest
*req
= psstate
->request
;
227 const bool isIntercepted
= !req
->flags
.redirected
&&
228 (req
->flags
.intercepted
|| req
->flags
.interceptTproxy
);
229 const bool useOriginalDst
= Config
.onoff
.client_dst_passthru
|| !req
->flags
.hostVerified
;
230 const bool choseDirect
= fs
&& fs
->code
== HIER_DIRECT
;
231 if (isIntercepted
&& useOriginalDst
&& choseDirect
) {
232 // check the client is still around before using any of its details
233 if (req
->clientConnectionManager
.valid()) {
234 // construct a "result" adding the ORIGINAL_DST to the set instead of DIRECT
235 Comm::ConnectionPointer p
= new Comm::Connection();
236 p
->remote
= req
->clientConnectionManager
->clientConnection
->local
;
237 p
->peerType
= ORIGINAL_DST
; // fs->code is DIRECT. This fixes the display.
238 p
->setPeer(fs
->_peer
.get());
240 // check for a configured outgoing address for this destination...
241 getOutgoingAddress(psstate
->request
, p
);
242 psstate
->paths
->push_back(p
);
245 // clear the used fs and continue
246 psstate
->servers
= fs
->next
;
248 peerSelectDnsPaths(psstate
);
252 // convert the list of FwdServer destinations into destinations IP addresses
253 if (fs
&& psstate
->paths
->size() < (unsigned int)Config
.forward_max_tries
) {
254 // send the next one off for DNS lookup.
255 const char *host
= fs
->_peer
.valid() ? fs
->_peer
->host
: psstate
->request
->url
.host();
256 debugs(44, 2, "Find IP destination for: " << psstate
->url() << "' via " << host
);
257 ipcache_nbgethostbyname(host
, peerSelectDnsResults
, psstate
);
261 // Bug 3605: clear any extra listed FwdServer destinations, when the options exceeds max_foward_tries.
262 // due to the allocation method of fs, we must deallocate each manually.
263 // TODO: use a std::list so we can get the size and abort adding whenever the selection loops reach Config.forward_max_tries
264 if (fs
&& psstate
->paths
->size() >= (unsigned int)Config
.forward_max_tries
) {
265 assert(fs
== psstate
->servers
);
267 psstate
->servers
= fs
->next
;
269 fs
= psstate
->servers
;
273 // done with DNS lookups. pass back to caller
274 PSC
*callback
= psstate
->callback
;
275 psstate
->callback
= NULL
;
277 debugs(44, 2, (psstate
->paths
->size()<1?"Failed to select source":"Found sources") << " for '" << psstate
->url() << "'");
278 debugs(44, 2, " always_direct = " << psstate
->always_direct
);
279 debugs(44, 2, " never_direct = " << psstate
->never_direct
);
280 if (psstate
->paths
) {
281 for (size_t i
= 0; i
< psstate
->paths
->size(); ++i
) {
282 if ((*psstate
->paths
)[i
]->peerType
== HIER_DIRECT
)
283 debugs(44, 2, " DIRECT = " << (*psstate
->paths
)[i
]);
284 else if ((*psstate
->paths
)[i
]->peerType
== ORIGINAL_DST
)
285 debugs(44, 2, " ORIGINAL_DST = " << (*psstate
->paths
)[i
]);
286 else if ((*psstate
->paths
)[i
]->peerType
== PINNED
)
287 debugs(44, 2, " PINNED = " << (*psstate
->paths
)[i
]);
289 debugs(44, 2, " cache_peer = " << (*psstate
->paths
)[i
]);
292 debugs(44, 2, " timedout = " << psstate
->ping
.timedout
);
294 psstate
->ping
.stop
= current_time
;
295 psstate
->request
->hier
.ping
= psstate
->ping
;
298 if (cbdataReferenceValidDone(psstate
->callback_data
, &cbdata
)) {
299 callback(psstate
->paths
, psstate
->lastError
, cbdata
);
300 psstate
->lastError
= NULL
; // FwdState has taken control over the ErrorState object.
307 peerSelectDnsResults(const ipcache_addrs
*ia
, const Dns::LookupDetails
&details
, void *data
)
309 ps_state
*psstate
= (ps_state
*)data
;
311 if (!cbdataReferenceValid(psstate
->callback_data
)) {
312 debugs(44, 3, "Aborting peer selection. Parent Job went away.");
317 psstate
->request
->recordLookup(details
);
319 FwdServer
*fs
= psstate
->servers
;
322 assert(ia
->cur
< ia
->count
);
324 // loop over each result address, adding to the possible destinations.
326 for (int n
= 0; n
< ia
->count
; ++n
, ++ip
) {
327 Comm::ConnectionPointer p
;
329 if (ip
>= ia
->count
) ip
= 0; // looped back to zero.
331 // Enforce forward_max_tries configuration.
332 if (psstate
->paths
->size() >= (unsigned int)Config
.forward_max_tries
)
335 // for TPROXY spoofing we must skip unusable addresses.
336 if (psstate
->request
->flags
.spoofClientIp
&& !(fs
->_peer
.valid() && fs
->_peer
->options
.no_tproxy
) ) {
337 if (ia
->in_addrs
[ip
].isIPv4() != psstate
->request
->client_addr
.isIPv4()) {
338 // we CAN'T spoof the address on this link. find another.
343 p
= new Comm::Connection();
344 p
->remote
= ia
->in_addrs
[ip
];
346 // when IPv6 is disabled we cannot use it
347 if (!Ip::EnableIpv6
&& p
->remote
.isIPv6()) {
348 const char *host
= (fs
->_peer
.valid() ? fs
->_peer
->host
: psstate
->request
->url
.host());
349 ipcacheMarkBadAddr(host
, p
->remote
);
353 p
->remote
.port(fs
->_peer
.valid() ? fs
->_peer
->http_port
: psstate
->request
->url
.port());
354 p
->peerType
= fs
->code
;
355 p
->setPeer(fs
->_peer
.get());
357 // check for a configured outgoing address for this destination...
358 getOutgoingAddress(psstate
->request
, p
);
359 psstate
->paths
->push_back(p
);
362 debugs(44, 3, "Unknown host: " << (fs
->_peer
.valid() ? fs
->_peer
->host
: psstate
->request
->url
.host()));
363 // discard any previous error.
364 delete psstate
->lastError
;
365 psstate
->lastError
= NULL
;
366 if (fs
->code
== HIER_DIRECT
) {
367 psstate
->lastError
= new ErrorState(ERR_DNS_FAIL
, Http::scServiceUnavailable
, psstate
->request
);
368 psstate
->lastError
->dnsError
= details
.error
;
372 psstate
->servers
= fs
->next
;
375 // see if more paths can be found
376 peerSelectDnsPaths(psstate
);
380 peerCheckNetdbDirect(ps_state
* psstate
)
387 if (psstate
->direct
== DIRECT_NO
)
390 /* base lookup on RTT and Hops if ICMP NetDB is enabled. */
392 myrtt
= netdbHostRtt(psstate
->request
->url
.host());
393 debugs(44, 3, "MY RTT = " << myrtt
<< " msec");
394 debugs(44, 3, "minimum_direct_rtt = " << Config
.minDirectRtt
<< " msec");
396 if (myrtt
&& myrtt
<= Config
.minDirectRtt
)
399 myhops
= netdbHostHops(psstate
->request
->url
.host());
401 debugs(44, 3, "peerCheckNetdbDirect: MY hops = " << myhops
);
402 debugs(44, 3, "peerCheckNetdbDirect: minimum_direct_hops = " << Config
.minDirectHops
);
404 if (myhops
&& myhops
<= Config
.minDirectHops
)
407 p
= whichPeer(psstate
->closest_parent_miss
);
412 debugs(44, 3, "peerCheckNetdbDirect: closest_parent_miss RTT = " << psstate
->ping
.p_rtt
<< " msec");
414 if (myrtt
&& myrtt
<= psstate
->ping
.p_rtt
)
417 #endif /* USE_ICMP */
423 peerSelectFoo(ps_state
* ps
)
425 if (!cbdataReferenceValid(ps
->callback_data
)) {
426 debugs(44, 3, "Aborting peer selection. Parent Job went away.");
431 StoreEntry
*entry
= ps
->entry
;
432 HttpRequest
*request
= ps
->request
;
433 debugs(44, 3, request
->method
<< ' ' << request
->url
.host());
435 /** If we don't know whether DIRECT is permitted ... */
436 if (ps
->direct
== DIRECT_UNKNOWN
) {
437 if (ps
->always_direct
== ACCESS_DUNNO
) {
438 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (always_direct to be checked)");
439 /** check always_direct; */
440 ACLFilledChecklist
*ch
= new ACLFilledChecklist(Config
.accessList
.AlwaysDirect
, request
, NULL
);
442 ps
->acl_checklist
= ch
;
443 ps
->acl_checklist
->nonBlockingCheck(peerCheckAlwaysDirectDone
, ps
);
445 } else if (ps
->never_direct
== ACCESS_DUNNO
) {
446 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (never_direct to be checked)");
447 /** check never_direct; */
448 ACLFilledChecklist
*ch
= new ACLFilledChecklist(Config
.accessList
.NeverDirect
, request
, NULL
);
450 ps
->acl_checklist
= ch
;
451 ps
->acl_checklist
->nonBlockingCheck(peerCheckNeverDirectDone
, ps
);
453 } else if (request
->flags
.noDirect
) {
454 /** if we are accelerating, direct is not an option. */
455 ps
->direct
= DIRECT_NO
;
456 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (forced non-direct)");
457 } else if (request
->flags
.loopDetected
) {
458 /** if we are in a forwarding-loop, direct is not an option. */
459 ps
->direct
= DIRECT_YES
;
460 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (forwarding loop detected)");
461 } else if (peerCheckNetdbDirect(ps
)) {
462 ps
->direct
= DIRECT_YES
;
463 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (checkNetdbDirect)");
465 ps
->direct
= DIRECT_MAYBE
;
466 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (default)");
469 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
]);
472 if (!entry
|| entry
->ping_status
== PING_NONE
)
473 peerSelectPinned(ps
);
476 } else if (entry
->ping_status
== PING_NONE
) {
477 peerGetSomeNeighbor(ps
);
479 if (entry
->ping_status
== PING_WAITING
)
481 } else if (entry
->ping_status
== PING_WAITING
) {
482 peerGetSomeNeighborReplies(ps
);
483 entry
->ping_status
= PING_DONE
;
486 switch (ps
->direct
) {
489 peerGetSomeDirect(ps
);
493 peerGetSomeParent(ps
);
494 peerGetAllParents(ps
);
499 if (Config
.onoff
.prefer_direct
)
500 peerGetSomeDirect(ps
);
502 if (request
->flags
.hierarchical
|| !Config
.onoff
.nonhierarchical_direct
) {
503 peerGetSomeParent(ps
);
504 peerGetAllParents(ps
);
507 if (!Config
.onoff
.prefer_direct
)
508 peerGetSomeDirect(ps
);
513 // resolve the possible peers
514 peerSelectDnsPaths(ps
);
517 bool peerAllowedToUse(const CachePeer
* p
, HttpRequest
* request
);
522 * Selects a pinned connection.
525 peerSelectPinned(ps_state
* ps
)
527 HttpRequest
*request
= ps
->request
;
528 if (!request
->pinnedConnection())
530 CachePeer
*pear
= request
->pinnedConnection()->pinnedPeer();
531 if (Comm::IsConnOpen(request
->pinnedConnection()->validatePinnedConnection(request
, pear
))) {
532 if (pear
&& peerAllowedToUse(pear
, request
)) {
533 peerAddFwdServer(&ps
->servers
, pear
, PINNED
);
535 ps
->entry
->ping_status
= PING_DONE
; /* Skip ICP */
536 } else if (!pear
&& ps
->direct
!= DIRECT_NO
) {
537 peerAddFwdServer(&ps
->servers
, NULL
, PINNED
);
539 ps
->entry
->ping_status
= PING_DONE
; /* Skip ICP */
545 * peerGetSomeNeighbor
547 * Selects a neighbor (parent or sibling) based on one of the
551 * ICMP Netdb RTT estimates
555 peerGetSomeNeighbor(ps_state
* ps
)
557 StoreEntry
*entry
= ps
->entry
;
558 HttpRequest
*request
= ps
->request
;
560 hier_code code
= HIER_NONE
;
561 assert(entry
->ping_status
== PING_NONE
);
563 if (ps
->direct
== DIRECT_YES
) {
564 entry
->ping_status
= PING_DONE
;
568 #if USE_CACHE_DIGESTS
569 if ((p
= neighborsDigestSelect(request
))) {
570 if (neighborType(p
, request
->url
) == PEER_PARENT
)
571 code
= CD_PARENT_HIT
;
573 code
= CD_SIBLING_HIT
;
576 if ((p
= netdbClosestParent(request
))) {
577 code
= CLOSEST_PARENT
;
578 } else if (peerSelectIcpPing(request
, ps
->direct
, entry
)) {
579 debugs(44, 3, "peerSelect: Doing ICP pings");
580 ps
->ping
.start
= current_time
;
581 ps
->ping
.n_sent
= neighborsUdpPing(request
,
585 &ps
->ping
.n_replies_expected
,
588 if (ps
->ping
.n_sent
== 0)
589 debugs(44, DBG_CRITICAL
, "WARNING: neighborsUdpPing returned 0");
590 debugs(44, 3, "peerSelect: " << ps
->ping
.n_replies_expected
<<
591 " ICP replies expected, RTT " << ps
->ping
.timeout
<<
594 if (ps
->ping
.n_replies_expected
> 0) {
595 entry
->ping_status
= PING_WAITING
;
596 eventAdd("peerPingTimeout",
599 0.001 * ps
->ping
.timeout
,
605 if (code
!= HIER_NONE
) {
607 debugs(44, 3, "peerSelect: " << hier_code_str
[code
] << "/" << p
->host
);
608 peerAddFwdServer(&ps
->servers
, p
, code
);
611 entry
->ping_status
= PING_DONE
;
615 * peerGetSomeNeighborReplies
617 * Selects a neighbor (parent or sibling) based on ICP/HTCP replies.
620 peerGetSomeNeighborReplies(ps_state
* ps
)
622 HttpRequest
*request
= ps
->request
;
624 hier_code code
= HIER_NONE
;
625 assert(ps
->entry
->ping_status
== PING_WAITING
);
626 assert(ps
->direct
!= DIRECT_YES
);
628 if (peerCheckNetdbDirect(ps
)) {
629 code
= CLOSEST_DIRECT
;
630 debugs(44, 3, hier_code_str
[code
] << "/" << request
->url
.host());
631 peerAddFwdServer(&ps
->servers
, NULL
, code
);
636 code
= ps
->hit_type
== PEER_PARENT
? PARENT_HIT
: SIBLING_HIT
;
638 if (!ps
->closest_parent_miss
.isAnyAddr()) {
639 p
= whichPeer(ps
->closest_parent_miss
);
640 code
= CLOSEST_PARENT_MISS
;
641 } else if (!ps
->first_parent_miss
.isAnyAddr()) {
642 p
= whichPeer(ps
->first_parent_miss
);
643 code
= FIRST_PARENT_MISS
;
646 if (p
&& code
!= HIER_NONE
) {
647 debugs(44, 3, hier_code_str
[code
] << "/" << p
->host
);
648 peerAddFwdServer(&ps
->servers
, p
, code
);
655 * Simply adds a 'direct' entry to the FwdServers list if this
656 * request can be forwarded directly to the origin server
659 peerGetSomeDirect(ps_state
* ps
)
661 if (ps
->direct
== DIRECT_NO
)
664 /* WAIS is not implemented natively */
665 if (ps
->request
->url
.getScheme() == AnyP::PROTO_WAIS
)
668 peerAddFwdServer(&ps
->servers
, NULL
, HIER_DIRECT
);
672 peerGetSomeParent(ps_state
* ps
)
675 HttpRequest
*request
= ps
->request
;
676 hier_code code
= HIER_NONE
;
677 debugs(44, 3, request
->method
<< ' ' << request
->url
.host());
679 if (ps
->direct
== DIRECT_YES
)
682 if ((p
= peerSourceHashSelectParent(request
))) {
683 code
= SOURCEHASH_PARENT
;
685 } else if ((p
= peerUserHashSelectParent(request
))) {
686 code
= USERHASH_PARENT
;
688 } else if ((p
= carpSelectParent(request
))) {
690 } else if ((p
= getRoundRobinParent(request
))) {
691 code
= ROUNDROBIN_PARENT
;
692 } else if ((p
= getWeightedRoundRobinParent(request
))) {
693 code
= ROUNDROBIN_PARENT
;
694 } else if ((p
= getFirstUpParent(request
))) {
695 code
= FIRSTUP_PARENT
;
696 } else if ((p
= getDefaultParent(request
))) {
697 code
= DEFAULT_PARENT
;
700 if (code
!= HIER_NONE
) {
701 debugs(44, 3, "peerSelect: " << hier_code_str
[code
] << "/" << p
->host
);
702 peerAddFwdServer(&ps
->servers
, p
, code
);
706 /* Adds alive parents. Used as a last resort for never_direct.
709 peerGetAllParents(ps_state
* ps
)
712 HttpRequest
*request
= ps
->request
;
713 /* Add all alive parents */
715 for (p
= Config
.peers
; p
; p
= p
->next
) {
716 /* XXX: neighbors.c lacks a public interface for enumerating
717 * parents to a request so we have to dig some here..
720 if (neighborType(p
, request
->url
) != PEER_PARENT
)
723 if (!peerHTTPOkay(p
, request
))
726 debugs(15, 3, "peerGetAllParents: adding alive parent " << p
->host
);
728 peerAddFwdServer(&ps
->servers
, p
, ANY_OLD_PARENT
);
731 /* XXX: should add dead parents here, but it is currently
732 * not possible to find out which parents are dead or which
733 * simply are not configured to handle the request.
735 /* Add default parent as a last resort */
736 if ((p
= getDefaultParent(request
))) {
737 peerAddFwdServer(&ps
->servers
, p
, DEFAULT_PARENT
);
742 peerPingTimeout(void *data
)
744 ps_state
*psstate
= (ps_state
*)data
;
745 StoreEntry
*entry
= psstate
->entry
;
748 debugs(44, 3, psstate
->url());
750 if (!cbdataReferenceValid(psstate
->callback_data
)) {
751 /* request aborted */
753 entry
->ping_status
= PING_DONE
;
754 cbdataReferenceDone(psstate
->callback_data
);
759 ++PeerStats
.timeouts
;
760 psstate
->ping
.timedout
= 1;
761 peerSelectFoo(psstate
);
767 memset(&PeerStats
, '\0', sizeof(PeerStats
));
771 peerIcpParentMiss(CachePeer
* p
, icp_common_t
* header
, ps_state
* ps
)
776 if (Config
.onoff
.query_icmp
) {
777 if (header
->flags
& ICP_FLAG_SRC_RTT
) {
778 rtt
= header
->pad
& 0xFFFF;
779 int hops
= (header
->pad
>> 16) & 0xFFFF;
781 if (rtt
> 0 && rtt
< 0xFFFF)
782 netdbUpdatePeer(ps
->request
->url
, p
, rtt
, hops
);
784 if (rtt
&& (ps
->ping
.p_rtt
== 0 || rtt
< ps
->ping
.p_rtt
)) {
785 ps
->closest_parent_miss
= p
->in_addr
;
786 ps
->ping
.p_rtt
= rtt
;
790 #endif /* USE_ICMP */
792 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
793 if (p
->options
.closest_only
)
796 /* set FIRST_MISS if there is no CLOSEST parent */
797 if (!ps
->closest_parent_miss
.isAnyAddr())
800 rtt
= (tvSubMsec(ps
->ping
.start
, current_time
) - p
->basetime
) / p
->weight
;
805 if (ps
->first_parent_miss
.isAnyAddr() || rtt
< ps
->ping
.w_rtt
) {
806 ps
->first_parent_miss
= p
->in_addr
;
807 ps
->ping
.w_rtt
= rtt
;
812 peerHandleIcpReply(CachePeer
* p
, peer_t type
, icp_common_t
* header
, void *data
)
814 ps_state
*psstate
= (ps_state
*)data
;
815 icp_opcode op
= header
->getOpCode();
816 debugs(44, 3, "peerHandleIcpReply: " << icp_opcode_str
[op
] << " " << psstate
->url() );
817 #if USE_CACHE_DIGESTS && 0
818 /* do cd lookup to count false misses */
821 peerNoteDigestLookup(request
, p
,
822 peerDigestLookup(p
, request
, psstate
->entry
));
826 ++ psstate
->ping
.n_recv
;
828 if (op
== ICP_MISS
|| op
== ICP_DECHO
) {
829 if (type
== PEER_PARENT
)
830 peerIcpParentMiss(p
, header
, psstate
);
831 } else if (op
== ICP_HIT
) {
833 psstate
->hit_type
= type
;
834 peerSelectFoo(psstate
);
838 if (psstate
->ping
.n_recv
< psstate
->ping
.n_replies_expected
)
841 peerSelectFoo(psstate
);
846 peerHandleHtcpReply(CachePeer
* p
, peer_t type
, HtcpReplyData
* htcp
, void *data
)
848 ps_state
*psstate
= (ps_state
*)data
;
849 debugs(44, 3, "" << (htcp
->hit
? "HIT" : "MISS") << " " << psstate
->url());
850 ++ psstate
->ping
.n_recv
;
854 psstate
->hit_type
= type
;
855 peerSelectFoo(psstate
);
859 if (type
== PEER_PARENT
)
860 peerHtcpParentMiss(p
, htcp
, psstate
);
862 if (psstate
->ping
.n_recv
< psstate
->ping
.n_replies_expected
)
865 peerSelectFoo(psstate
);
869 peerHtcpParentMiss(CachePeer
* p
, HtcpReplyData
* htcp
, ps_state
* ps
)
874 if (Config
.onoff
.query_icmp
) {
875 if (htcp
->cto
.rtt
> 0) {
876 rtt
= (int) htcp
->cto
.rtt
* 1000;
877 int hops
= (int) htcp
->cto
.hops
* 1000;
878 netdbUpdatePeer(ps
->request
->url
, p
, rtt
, hops
);
880 if (rtt
&& (ps
->ping
.p_rtt
== 0 || rtt
< ps
->ping
.p_rtt
)) {
881 ps
->closest_parent_miss
= p
->in_addr
;
882 ps
->ping
.p_rtt
= rtt
;
886 #endif /* USE_ICMP */
888 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
889 if (p
->options
.closest_only
)
892 /* set FIRST_MISS if there is no CLOSEST parent */
893 if (!ps
->closest_parent_miss
.isAnyAddr())
896 rtt
= (tvSubMsec(ps
->ping
.start
, current_time
) - p
->basetime
) / p
->weight
;
901 if (ps
->first_parent_miss
.isAnyAddr() || rtt
< ps
->ping
.w_rtt
) {
902 ps
->first_parent_miss
= p
->in_addr
;
903 ps
->ping
.w_rtt
= rtt
;
910 peerHandlePingReply(CachePeer
* p
, peer_t type
, AnyP::ProtocolType proto
, void *pingdata
, void *data
)
912 if (proto
== AnyP::PROTO_ICP
)
913 peerHandleIcpReply(p
, type
, (icp_common_t
*)pingdata
, data
);
917 else if (proto
== AnyP::PROTO_HTCP
)
918 peerHandleHtcpReply(p
, type
, (HtcpReplyData
*)pingdata
, data
);
923 debugs(44, DBG_IMPORTANT
, "peerHandlePingReply: unknown protocol " << proto
);
927 peerAddFwdServer(FwdServer
** FSVR
, CachePeer
* p
, hier_code code
)
929 debugs(44, 5, "peerAddFwdServer: adding " <<
930 (p
? p
->host
: "DIRECT") << " " <<
931 hier_code_str
[code
] );
932 FwdServer
*fs
= new FwdServer(p
, code
);
935 FSVR
= &(*FSVR
)->next
;
940 ps_state::ps_state() : request (NULL
),
942 always_direct(Config
.accessList
.AlwaysDirect
?ACCESS_DUNNO
:ACCESS_DENIED
),
943 never_direct(Config
.accessList
.NeverDirect
?ACCESS_DUNNO
:ACCESS_DENIED
),
944 direct(DIRECT_UNKNOWN
),
946 callback_data (NULL
),
951 closest_parent_miss(),
956 ; // no local defaults.
960 ps_state::url() const
963 return SBuf(entry
->url());
966 return request
->effectiveRequestUri();
968 static const SBuf
noUrl("[no URL]");
972 ping_data::ping_data() :
975 n_replies_expected(0),