2 * Copyright (C) 1996-2015 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 "HttpRequest.h"
24 #include "icmp/net_db.h"
28 #include "neighbors.h"
29 #include "peer_sourcehash.h"
30 #include "peer_userhash.h"
31 #include "PeerSelectState.h"
32 #include "SquidConfig.h"
33 #include "SquidTime.h"
41 static const char *DirectStr
[] = {
48 static void peerSelectFoo(ps_state
*);
49 static void peerPingTimeout(void *data
);
50 static IRCB peerHandlePingReply
;
51 static void peerIcpParentMiss(CachePeer
*, icp_common_t
*, ps_state
*);
53 static void peerHtcpParentMiss(CachePeer
*, HtcpReplyData
*, ps_state
*);
54 static void peerHandleHtcpReply(CachePeer
*, peer_t
, HtcpReplyData
*, void *);
56 static int peerCheckNetdbDirect(ps_state
* psstate
);
57 static void peerGetSomeNeighbor(ps_state
*);
58 static void peerGetSomeNeighborReplies(ps_state
*);
59 static void peerGetSomeDirect(ps_state
*);
60 static void peerGetSomeParent(ps_state
*);
61 static void peerGetAllParents(ps_state
*);
62 static void peerAddFwdServer(FwdServer
**, CachePeer
*, hier_code
);
63 static void peerSelectPinned(ps_state
* ps
);
64 static void peerSelectDnsResults(const ipcache_addrs
*ia
, const Dns::LookupDetails
&details
, void *data
);
66 CBDATA_CLASS_INIT(ps_state
);
71 FwdServer
*next
= servers
->next
;
72 cbdataReferenceDone(servers
->_peer
);
73 memFree(servers
, MEM_FWD_SERVER
);
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
);
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
;
247 cbdataReferenceDone(fs
->_peer
);
248 memFree(fs
, MEM_FWD_SERVER
);
249 peerSelectDnsPaths(psstate
);
253 // convert the list of FwdServer destinations into destinations IP addresses
254 if (fs
&& psstate
->paths
->size() < (unsigned int)Config
.forward_max_tries
) {
255 // send the next one off for DNS lookup.
256 const char *host
= fs
->_peer
? fs
->_peer
->host
: psstate
->request
->url
.host();
257 debugs(44, 2, "Find IP destination for: " << psstate
->url() << "' via " << host
);
258 ipcache_nbgethostbyname(host
, peerSelectDnsResults
, psstate
);
262 // Bug 3605: clear any extra listed FwdServer destinations, when the options exceeds max_foward_tries.
263 // due to the allocation method of fs, we must deallocate each manually.
264 // TODO: use a std::list so we can get the size and abort adding whenever the selection loops reach Config.forward_max_tries
265 if (fs
&& psstate
->paths
->size() >= (unsigned int)Config
.forward_max_tries
) {
266 assert(fs
== psstate
->servers
);
268 psstate
->servers
= fs
->next
;
269 cbdataReferenceDone(fs
->_peer
);
270 memFree(fs
, MEM_FWD_SERVER
);
271 fs
= psstate
->servers
;
275 // done with DNS lookups. pass back to caller
276 PSC
*callback
= psstate
->callback
;
277 psstate
->callback
= NULL
;
279 debugs(44, 2, (psstate
->paths
->size()<1?"Failed to select source":"Found sources") << " for '" << psstate
->url() << "'");
280 debugs(44, 2, " always_direct = " << psstate
->always_direct
);
281 debugs(44, 2, " never_direct = " << psstate
->never_direct
);
282 if (psstate
->paths
) {
283 for (size_t i
= 0; i
< psstate
->paths
->size(); ++i
) {
284 if ((*psstate
->paths
)[i
]->peerType
== HIER_DIRECT
)
285 debugs(44, 2, " DIRECT = " << (*psstate
->paths
)[i
]);
286 else if ((*psstate
->paths
)[i
]->peerType
== ORIGINAL_DST
)
287 debugs(44, 2, " ORIGINAL_DST = " << (*psstate
->paths
)[i
]);
288 else if ((*psstate
->paths
)[i
]->peerType
== PINNED
)
289 debugs(44, 2, " PINNED = " << (*psstate
->paths
)[i
]);
291 debugs(44, 2, " cache_peer = " << (*psstate
->paths
)[i
]);
294 debugs(44, 2, " timedout = " << psstate
->ping
.timedout
);
296 psstate
->ping
.stop
= current_time
;
297 psstate
->request
->hier
.ping
= psstate
->ping
;
300 if (cbdataReferenceValidDone(psstate
->callback_data
, &cbdata
)) {
301 callback(psstate
->paths
, psstate
->lastError
, cbdata
);
302 psstate
->lastError
= NULL
; // FwdState has taken control over the ErrorState object.
309 peerSelectDnsResults(const ipcache_addrs
*ia
, const Dns::LookupDetails
&details
, void *data
)
311 ps_state
*psstate
= (ps_state
*)data
;
313 if (!cbdataReferenceValid(psstate
->callback_data
)) {
314 debugs(44, 3, "Aborting peer selection. Parent Job went away.");
319 psstate
->request
->recordLookup(details
);
321 FwdServer
*fs
= psstate
->servers
;
324 assert(ia
->cur
< ia
->count
);
326 // loop over each result address, adding to the possible destinations.
328 for (int n
= 0; n
< ia
->count
; ++n
, ++ip
) {
329 Comm::ConnectionPointer p
;
331 if (ip
>= ia
->count
) ip
= 0; // looped back to zero.
333 // Enforce forward_max_tries configuration.
334 if (psstate
->paths
->size() >= (unsigned int)Config
.forward_max_tries
)
337 // for TPROXY spoofing we must skip unusable addresses.
338 if (psstate
->request
->flags
.spoofClientIp
&& !(fs
->_peer
&& fs
->_peer
->options
.no_tproxy
) ) {
339 if (ia
->in_addrs
[ip
].isIPv4() != psstate
->request
->client_addr
.isIPv4()) {
340 // we CAN'T spoof the address on this link. find another.
345 p
= new Comm::Connection();
346 p
->remote
= ia
->in_addrs
[ip
];
348 // when IPv6 is disabled we cannot use it
349 if (!Ip::EnableIpv6
&& p
->remote
.isIPv6()) {
350 const char *host
= (fs
->_peer
? fs
->_peer
->host
: psstate
->request
->url
.host());
351 ipcacheMarkBadAddr(host
, p
->remote
);
355 p
->remote
.port(fs
->_peer
? fs
->_peer
->http_port
: psstate
->request
->url
.port());
356 p
->peerType
= fs
->code
;
357 p
->setPeer(fs
->_peer
);
359 // check for a configured outgoing address for this destination...
360 getOutgoingAddress(psstate
->request
, p
);
361 psstate
->paths
->push_back(p
);
364 debugs(44, 3, "Unknown host: " << (fs
->_peer
? fs
->_peer
->host
: psstate
->request
->url
.host()));
365 // discard any previous error.
366 delete psstate
->lastError
;
367 psstate
->lastError
= NULL
;
368 if (fs
->code
== HIER_DIRECT
) {
369 psstate
->lastError
= new ErrorState(ERR_DNS_FAIL
, Http::scServiceUnavailable
, psstate
->request
);
370 psstate
->lastError
->dnsError
= details
.error
;
374 psstate
->servers
= fs
->next
;
375 cbdataReferenceDone(fs
->_peer
);
376 memFree(fs
, MEM_FWD_SERVER
);
378 // see if more paths can be found
379 peerSelectDnsPaths(psstate
);
383 peerCheckNetdbDirect(ps_state
* psstate
)
390 if (psstate
->direct
== DIRECT_NO
)
393 /* base lookup on RTT and Hops if ICMP NetDB is enabled. */
395 myrtt
= netdbHostRtt(psstate
->request
->url
.host());
396 debugs(44, 3, "MY RTT = " << myrtt
<< " msec");
397 debugs(44, 3, "minimum_direct_rtt = " << Config
.minDirectRtt
<< " msec");
399 if (myrtt
&& myrtt
<= Config
.minDirectRtt
)
402 myhops
= netdbHostHops(psstate
->request
->url
.host());
404 debugs(44, 3, "peerCheckNetdbDirect: MY hops = " << myhops
);
405 debugs(44, 3, "peerCheckNetdbDirect: minimum_direct_hops = " << Config
.minDirectHops
);
407 if (myhops
&& myhops
<= Config
.minDirectHops
)
410 p
= whichPeer(psstate
->closest_parent_miss
);
415 debugs(44, 3, "peerCheckNetdbDirect: closest_parent_miss RTT = " << psstate
->ping
.p_rtt
<< " msec");
417 if (myrtt
&& myrtt
<= psstate
->ping
.p_rtt
)
420 #endif /* USE_ICMP */
426 peerSelectFoo(ps_state
* ps
)
428 if (!cbdataReferenceValid(ps
->callback_data
)) {
429 debugs(44, 3, "Aborting peer selection. Parent Job went away.");
434 StoreEntry
*entry
= ps
->entry
;
435 HttpRequest
*request
= ps
->request
;
436 debugs(44, 3, request
->method
<< ' ' << request
->url
.host());
438 /** If we don't know whether DIRECT is permitted ... */
439 if (ps
->direct
== DIRECT_UNKNOWN
) {
440 if (ps
->always_direct
== ACCESS_DUNNO
) {
441 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (always_direct to be checked)");
442 /** check always_direct; */
443 ACLFilledChecklist
*ch
= new ACLFilledChecklist(Config
.accessList
.AlwaysDirect
, request
, NULL
);
445 ps
->acl_checklist
= ch
;
446 ps
->acl_checklist
->nonBlockingCheck(peerCheckAlwaysDirectDone
, ps
);
448 } else if (ps
->never_direct
== ACCESS_DUNNO
) {
449 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (never_direct to be checked)");
450 /** check never_direct; */
451 ACLFilledChecklist
*ch
= new ACLFilledChecklist(Config
.accessList
.NeverDirect
, request
, NULL
);
453 ps
->acl_checklist
= ch
;
454 ps
->acl_checklist
->nonBlockingCheck(peerCheckNeverDirectDone
, ps
);
456 } else if (request
->flags
.noDirect
) {
457 /** if we are accelerating, direct is not an option. */
458 ps
->direct
= DIRECT_NO
;
459 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (forced non-direct)");
460 } else if (request
->flags
.loopDetected
) {
461 /** if we are in a forwarding-loop, direct is not an option. */
462 ps
->direct
= DIRECT_YES
;
463 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (forwarding loop detected)");
464 } else if (peerCheckNetdbDirect(ps
)) {
465 ps
->direct
= DIRECT_YES
;
466 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (checkNetdbDirect)");
468 ps
->direct
= DIRECT_MAYBE
;
469 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (default)");
472 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
]);
475 if (!entry
|| entry
->ping_status
== PING_NONE
)
476 peerSelectPinned(ps
);
479 } else if (entry
->ping_status
== PING_NONE
) {
480 peerGetSomeNeighbor(ps
);
482 if (entry
->ping_status
== PING_WAITING
)
484 } else if (entry
->ping_status
== PING_WAITING
) {
485 peerGetSomeNeighborReplies(ps
);
486 entry
->ping_status
= PING_DONE
;
489 switch (ps
->direct
) {
492 peerGetSomeDirect(ps
);
496 peerGetSomeParent(ps
);
497 peerGetAllParents(ps
);
502 if (Config
.onoff
.prefer_direct
)
503 peerGetSomeDirect(ps
);
505 if (request
->flags
.hierarchical
|| !Config
.onoff
.nonhierarchical_direct
) {
506 peerGetSomeParent(ps
);
507 peerGetAllParents(ps
);
510 if (!Config
.onoff
.prefer_direct
)
511 peerGetSomeDirect(ps
);
516 // resolve the possible peers
517 peerSelectDnsPaths(ps
);
520 bool peerAllowedToUse(const CachePeer
* p
, HttpRequest
* request
);
525 * Selects a pinned connection.
528 peerSelectPinned(ps_state
* ps
)
530 HttpRequest
*request
= ps
->request
;
531 if (!request
->pinnedConnection())
533 CachePeer
*pear
= request
->pinnedConnection()->pinnedPeer();
534 if (Comm::IsConnOpen(request
->pinnedConnection()->validatePinnedConnection(request
, pear
))) {
535 if (pear
&& peerAllowedToUse(pear
, request
)) {
536 peerAddFwdServer(&ps
->servers
, pear
, PINNED
);
538 ps
->entry
->ping_status
= PING_DONE
; /* Skip ICP */
539 } else if (!pear
&& ps
->direct
!= DIRECT_NO
) {
540 peerAddFwdServer(&ps
->servers
, NULL
, PINNED
);
542 ps
->entry
->ping_status
= PING_DONE
; /* Skip ICP */
548 * peerGetSomeNeighbor
550 * Selects a neighbor (parent or sibling) based on one of the
554 * ICMP Netdb RTT estimates
558 peerGetSomeNeighbor(ps_state
* ps
)
560 StoreEntry
*entry
= ps
->entry
;
561 HttpRequest
*request
= ps
->request
;
563 hier_code code
= HIER_NONE
;
564 assert(entry
->ping_status
== PING_NONE
);
566 if (ps
->direct
== DIRECT_YES
) {
567 entry
->ping_status
= PING_DONE
;
571 #if USE_CACHE_DIGESTS
572 if ((p
= neighborsDigestSelect(request
))) {
573 if (neighborType(p
, request
->url
) == PEER_PARENT
)
574 code
= CD_PARENT_HIT
;
576 code
= CD_SIBLING_HIT
;
579 if ((p
= netdbClosestParent(request
))) {
580 code
= CLOSEST_PARENT
;
581 } else if (peerSelectIcpPing(request
, ps
->direct
, entry
)) {
582 debugs(44, 3, "peerSelect: Doing ICP pings");
583 ps
->ping
.start
= current_time
;
584 ps
->ping
.n_sent
= neighborsUdpPing(request
,
588 &ps
->ping
.n_replies_expected
,
591 if (ps
->ping
.n_sent
== 0)
592 debugs(44, DBG_CRITICAL
, "WARNING: neighborsUdpPing returned 0");
593 debugs(44, 3, "peerSelect: " << ps
->ping
.n_replies_expected
<<
594 " ICP replies expected, RTT " << ps
->ping
.timeout
<<
597 if (ps
->ping
.n_replies_expected
> 0) {
598 entry
->ping_status
= PING_WAITING
;
599 eventAdd("peerPingTimeout",
602 0.001 * ps
->ping
.timeout
,
608 if (code
!= HIER_NONE
) {
610 debugs(44, 3, "peerSelect: " << hier_code_str
[code
] << "/" << p
->host
);
611 peerAddFwdServer(&ps
->servers
, p
, code
);
614 entry
->ping_status
= PING_DONE
;
618 * peerGetSomeNeighborReplies
620 * Selects a neighbor (parent or sibling) based on ICP/HTCP replies.
623 peerGetSomeNeighborReplies(ps_state
* ps
)
625 HttpRequest
*request
= ps
->request
;
627 hier_code code
= HIER_NONE
;
628 assert(ps
->entry
->ping_status
== PING_WAITING
);
629 assert(ps
->direct
!= DIRECT_YES
);
631 if (peerCheckNetdbDirect(ps
)) {
632 code
= CLOSEST_DIRECT
;
633 debugs(44, 3, hier_code_str
[code
] << "/" << request
->url
.host());
634 peerAddFwdServer(&ps
->servers
, NULL
, code
);
639 code
= ps
->hit_type
== PEER_PARENT
? PARENT_HIT
: SIBLING_HIT
;
641 if (!ps
->closest_parent_miss
.isAnyAddr()) {
642 p
= whichPeer(ps
->closest_parent_miss
);
643 code
= CLOSEST_PARENT_MISS
;
644 } else if (!ps
->first_parent_miss
.isAnyAddr()) {
645 p
= whichPeer(ps
->first_parent_miss
);
646 code
= FIRST_PARENT_MISS
;
649 if (p
&& code
!= HIER_NONE
) {
650 debugs(44, 3, hier_code_str
[code
] << "/" << p
->host
);
651 peerAddFwdServer(&ps
->servers
, p
, code
);
658 * Simply adds a 'direct' entry to the FwdServers list if this
659 * request can be forwarded directly to the origin server
662 peerGetSomeDirect(ps_state
* ps
)
664 if (ps
->direct
== DIRECT_NO
)
667 /* WAIS is not implemented natively */
668 if (ps
->request
->url
.getScheme() == AnyP::PROTO_WAIS
)
671 peerAddFwdServer(&ps
->servers
, NULL
, HIER_DIRECT
);
675 peerGetSomeParent(ps_state
* ps
)
678 HttpRequest
*request
= ps
->request
;
679 hier_code code
= HIER_NONE
;
680 debugs(44, 3, request
->method
<< ' ' << request
->url
.host());
682 if (ps
->direct
== DIRECT_YES
)
685 if ((p
= peerSourceHashSelectParent(request
))) {
686 code
= SOURCEHASH_PARENT
;
688 } else if ((p
= peerUserHashSelectParent(request
))) {
689 code
= USERHASH_PARENT
;
691 } else if ((p
= carpSelectParent(request
))) {
693 } else if ((p
= getRoundRobinParent(request
))) {
694 code
= ROUNDROBIN_PARENT
;
695 } else if ((p
= getWeightedRoundRobinParent(request
))) {
696 code
= ROUNDROBIN_PARENT
;
697 } else if ((p
= getFirstUpParent(request
))) {
698 code
= FIRSTUP_PARENT
;
699 } else if ((p
= getDefaultParent(request
))) {
700 code
= DEFAULT_PARENT
;
703 if (code
!= HIER_NONE
) {
704 debugs(44, 3, "peerSelect: " << hier_code_str
[code
] << "/" << p
->host
);
705 peerAddFwdServer(&ps
->servers
, p
, code
);
709 /* Adds alive parents. Used as a last resort for never_direct.
712 peerGetAllParents(ps_state
* ps
)
715 HttpRequest
*request
= ps
->request
;
716 /* Add all alive parents */
718 for (p
= Config
.peers
; p
; p
= p
->next
) {
719 /* XXX: neighbors.c lacks a public interface for enumerating
720 * parents to a request so we have to dig some here..
723 if (neighborType(p
, request
->url
) != PEER_PARENT
)
726 if (!peerHTTPOkay(p
, request
))
729 debugs(15, 3, "peerGetAllParents: adding alive parent " << p
->host
);
731 peerAddFwdServer(&ps
->servers
, p
, ANY_OLD_PARENT
);
734 /* XXX: should add dead parents here, but it is currently
735 * not possible to find out which parents are dead or which
736 * simply are not configured to handle the request.
738 /* Add default parent as a last resort */
739 if ((p
= getDefaultParent(request
))) {
740 peerAddFwdServer(&ps
->servers
, p
, DEFAULT_PARENT
);
745 peerPingTimeout(void *data
)
747 ps_state
*psstate
= (ps_state
*)data
;
748 StoreEntry
*entry
= psstate
->entry
;
751 debugs(44, 3, psstate
->url());
753 if (!cbdataReferenceValid(psstate
->callback_data
)) {
754 /* request aborted */
756 entry
->ping_status
= PING_DONE
;
757 cbdataReferenceDone(psstate
->callback_data
);
762 ++PeerStats
.timeouts
;
763 psstate
->ping
.timedout
= 1;
764 peerSelectFoo(psstate
);
770 memset(&PeerStats
, '\0', sizeof(PeerStats
));
771 memDataInit(MEM_FWD_SERVER
, "FwdServer", sizeof(FwdServer
), 0);
775 peerIcpParentMiss(CachePeer
* p
, icp_common_t
* header
, ps_state
* ps
)
780 if (Config
.onoff
.query_icmp
) {
781 if (header
->flags
& ICP_FLAG_SRC_RTT
) {
782 rtt
= header
->pad
& 0xFFFF;
783 int hops
= (header
->pad
>> 16) & 0xFFFF;
785 if (rtt
> 0 && rtt
< 0xFFFF)
786 netdbUpdatePeer(ps
->request
->url
, p
, rtt
, hops
);
788 if (rtt
&& (ps
->ping
.p_rtt
== 0 || rtt
< ps
->ping
.p_rtt
)) {
789 ps
->closest_parent_miss
= p
->in_addr
;
790 ps
->ping
.p_rtt
= rtt
;
794 #endif /* USE_ICMP */
796 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
797 if (p
->options
.closest_only
)
800 /* set FIRST_MISS if there is no CLOSEST parent */
801 if (!ps
->closest_parent_miss
.isAnyAddr())
804 rtt
= (tvSubMsec(ps
->ping
.start
, current_time
) - p
->basetime
) / p
->weight
;
809 if (ps
->first_parent_miss
.isAnyAddr() || rtt
< ps
->ping
.w_rtt
) {
810 ps
->first_parent_miss
= p
->in_addr
;
811 ps
->ping
.w_rtt
= rtt
;
816 peerHandleIcpReply(CachePeer
* p
, peer_t type
, icp_common_t
* header
, void *data
)
818 ps_state
*psstate
= (ps_state
*)data
;
819 icp_opcode op
= header
->getOpCode();
820 debugs(44, 3, "peerHandleIcpReply: " << icp_opcode_str
[op
] << " " << psstate
->url() );
821 #if USE_CACHE_DIGESTS && 0
822 /* do cd lookup to count false misses */
825 peerNoteDigestLookup(request
, p
,
826 peerDigestLookup(p
, request
, psstate
->entry
));
830 ++ psstate
->ping
.n_recv
;
832 if (op
== ICP_MISS
|| op
== ICP_DECHO
) {
833 if (type
== PEER_PARENT
)
834 peerIcpParentMiss(p
, header
, psstate
);
835 } else if (op
== ICP_HIT
) {
837 psstate
->hit_type
= type
;
838 peerSelectFoo(psstate
);
842 if (psstate
->ping
.n_recv
< psstate
->ping
.n_replies_expected
)
845 peerSelectFoo(psstate
);
850 peerHandleHtcpReply(CachePeer
* p
, peer_t type
, HtcpReplyData
* htcp
, void *data
)
852 ps_state
*psstate
= (ps_state
*)data
;
853 debugs(44, 3, "" << (htcp
->hit
? "HIT" : "MISS") << " " << psstate
->url());
854 ++ psstate
->ping
.n_recv
;
858 psstate
->hit_type
= type
;
859 peerSelectFoo(psstate
);
863 if (type
== PEER_PARENT
)
864 peerHtcpParentMiss(p
, htcp
, psstate
);
866 if (psstate
->ping
.n_recv
< psstate
->ping
.n_replies_expected
)
869 peerSelectFoo(psstate
);
873 peerHtcpParentMiss(CachePeer
* p
, HtcpReplyData
* htcp
, ps_state
* ps
)
878 if (Config
.onoff
.query_icmp
) {
879 if (htcp
->cto
.rtt
> 0) {
880 rtt
= (int) htcp
->cto
.rtt
* 1000;
881 int hops
= (int) htcp
->cto
.hops
* 1000;
882 netdbUpdatePeer(ps
->request
->url
, p
, rtt
, hops
);
884 if (rtt
&& (ps
->ping
.p_rtt
== 0 || rtt
< ps
->ping
.p_rtt
)) {
885 ps
->closest_parent_miss
= p
->in_addr
;
886 ps
->ping
.p_rtt
= rtt
;
890 #endif /* USE_ICMP */
892 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
893 if (p
->options
.closest_only
)
896 /* set FIRST_MISS if there is no CLOSEST parent */
897 if (!ps
->closest_parent_miss
.isAnyAddr())
900 rtt
= (tvSubMsec(ps
->ping
.start
, current_time
) - p
->basetime
) / p
->weight
;
905 if (ps
->first_parent_miss
.isAnyAddr() || rtt
< ps
->ping
.w_rtt
) {
906 ps
->first_parent_miss
= p
->in_addr
;
907 ps
->ping
.w_rtt
= rtt
;
914 peerHandlePingReply(CachePeer
* p
, peer_t type
, AnyP::ProtocolType proto
, void *pingdata
, void *data
)
916 if (proto
== AnyP::PROTO_ICP
)
917 peerHandleIcpReply(p
, type
, (icp_common_t
*)pingdata
, data
);
921 else if (proto
== AnyP::PROTO_HTCP
)
922 peerHandleHtcpReply(p
, type
, (HtcpReplyData
*)pingdata
, data
);
927 debugs(44, DBG_IMPORTANT
, "peerHandlePingReply: unknown protocol " << proto
);
931 peerAddFwdServer(FwdServer
** FSVR
, CachePeer
* p
, hier_code code
)
933 FwdServer
*fs
= (FwdServer
*)memAllocate(MEM_FWD_SERVER
);
934 debugs(44, 5, "peerAddFwdServer: adding " <<
935 (p
? p
->host
: "DIRECT") << " " <<
936 hier_code_str
[code
] );
937 fs
->_peer
= cbdataReference(p
);
941 FSVR
= &(*FSVR
)->next
;
946 ps_state::ps_state() : request (NULL
),
948 always_direct(Config
.accessList
.AlwaysDirect
?ACCESS_DUNNO
:ACCESS_DENIED
),
949 never_direct(Config
.accessList
.NeverDirect
?ACCESS_DUNNO
:ACCESS_DENIED
),
950 direct(DIRECT_UNKNOWN
),
952 callback_data (NULL
),
957 closest_parent_miss(),
962 ; // no local defaults.
966 ps_state::url() const
969 return SBuf(entry
->url());
972 return request
->effectiveRequestUri();
974 static const SBuf
noUrl("[no URL]");
978 ping_data::ping_data() :
981 n_replies_expected(0),