2 * DEBUG: section 44 Peer Selection Algorithm
3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 #include "acl/FilledChecklist.h"
35 #include "CachePeer.h"
37 #include "client_side.h"
38 #include "DnsLookupDetails.h"
39 #include "errorpage.h"
43 #include "hier_code.h"
45 #include "HttpRequest.h"
46 #include "icmp/net_db.h"
51 #include "neighbors.h"
52 #include "peer_sourcehash.h"
53 #include "peer_userhash.h"
54 #include "PeerSelectState.h"
55 #include "SquidConfig.h"
56 #include "SquidTime.h"
64 static const char *DirectStr
[] = {
71 static void peerSelectFoo(ps_state
*);
72 static void peerPingTimeout(void *data
);
73 static IRCB peerHandlePingReply
;
74 static void peerIcpParentMiss(CachePeer
*, icp_common_t
*, ps_state
*);
76 static void peerHtcpParentMiss(CachePeer
*, HtcpReplyData
*, ps_state
*);
77 static void peerHandleHtcpReply(CachePeer
*, peer_t
, HtcpReplyData
*, void *);
79 static int peerCheckNetdbDirect(ps_state
* psstate
);
80 static void peerGetSomeNeighbor(ps_state
*);
81 static void peerGetSomeNeighborReplies(ps_state
*);
82 static void peerGetSomeDirect(ps_state
*);
83 static void peerGetSomeParent(ps_state
*);
84 static void peerGetAllParents(ps_state
*);
85 static void peerAddFwdServer(FwdServer
**, CachePeer
*, hier_code
);
86 static void peerSelectPinned(ps_state
* ps
);
87 static void peerSelectDnsResults(const ipcache_addrs
*ia
, const DnsLookupDetails
&details
, void *data
);
89 CBDATA_CLASS_INIT(ps_state
);
94 FwdServer
*next
= servers
->next
;
95 cbdataReferenceDone(servers
->_peer
);
96 memFree(servers
, MEM_FWD_SERVER
);
101 debugs(44, 3, entry
->url());
103 if (entry
->ping_status
== PING_WAITING
)
104 eventDelete(peerPingTimeout
, this);
106 entry
->ping_status
= PING_DONE
;
110 debugs(44, DBG_IMPORTANT
, "calling aclChecklistFree() from ps_state destructor");
111 delete acl_checklist
;
114 HTTPMSGUNLOCK(request
);
117 assert(entry
->ping_status
!= PING_WAITING
);
118 entry
->unlock("peerSelect");
126 peerSelectIcpPing(HttpRequest
* request
, int direct
, StoreEntry
* entry
)
130 assert(entry
->ping_status
== PING_NONE
);
131 assert(direct
!= DIRECT_YES
);
132 debugs(44, 3, "peerSelectIcpPing: " << entry
->url() );
134 if (!request
->flags
.hierarchical
&& direct
!= DIRECT_NO
)
137 if (EBIT_TEST(entry
->flags
, KEY_PRIVATE
) && !neighbors_do_private_keys
)
138 if (direct
!= DIRECT_NO
)
141 n
= neighborsCount(request
);
143 debugs(44, 3, "peerSelectIcpPing: counted " << n
<< " neighbors");
149 peerSelect(Comm::ConnectionList
* paths
,
150 HttpRequest
* request
,
151 AccessLogEntry::Pointer
const &al
,
159 debugs(44, 3, "peerSelect: " << entry
->url() );
161 debugs(44, 3, "peerSelect: " << RequestMethodStr(request
->method
));
163 psstate
= new ps_state
;
165 psstate
->request
= request
;
166 HTTPMSGLOCK(psstate
->request
);
169 psstate
->entry
= entry
;
170 psstate
->paths
= paths
;
172 psstate
->callback
= callback
;
174 psstate
->callback_data
= cbdataReference(callback_data
);
176 #if USE_CACHE_DIGESTS
178 request
->hier
.peer_select_start
= current_time
;
183 psstate
->entry
->lock("peerSelect");
185 peerSelectFoo(psstate
);
189 peerCheckNeverDirectDone(allow_t answer
, void *data
)
191 ps_state
*psstate
= (ps_state
*) data
;
192 psstate
->acl_checklist
= NULL
;
193 debugs(44, 3, "peerCheckNeverDirectDone: " << answer
);
194 psstate
->never_direct
= answer
;
197 /** if never_direct says YES, do that. */
198 psstate
->direct
= DIRECT_NO
;
199 debugs(44, 3, HERE
<< "direct = " << DirectStr
[psstate
->direct
] << " (never_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: never_direct resulted in " << answer
<< ". Username ACLs are not reliable here.");
208 peerSelectFoo(psstate
);
212 peerCheckAlwaysDirectDone(allow_t answer
, void *data
)
214 ps_state
*psstate
= (ps_state
*)data
;
215 psstate
->acl_checklist
= NULL
;
216 debugs(44, 3, "peerCheckAlwaysDirectDone: " << answer
);
217 psstate
->always_direct
= answer
;
220 /** if always_direct says YES, do that. */
221 psstate
->direct
= DIRECT_YES
;
222 debugs(44, 3, HERE
<< "direct = " << DirectStr
[psstate
->direct
] << " (always_direct allow)");
224 case ACCESS_DENIED
: // not relevant.
225 case ACCESS_DUNNO
: // not relevant.
227 case ACCESS_AUTH_REQUIRED
:
228 debugs(44, DBG_IMPORTANT
, "WARNING: always_direct resulted in " << answer
<< ". Username ACLs are not reliable here.");
231 peerSelectFoo(psstate
);
235 peerSelectDnsPaths(ps_state
*psstate
)
237 FwdServer
*fs
= psstate
->servers
;
239 if (!cbdataReferenceValid(psstate
->callback_data
)) {
240 debugs(44, 3, "Aborting peer selection. Parent Job went away.");
245 // Bug 3243: CVE 2009-0801
246 // Bypass of browser same-origin access control in intercepted communication
247 // To resolve this we must use only the original client destination when going DIRECT
248 // on intercepted traffic which failed Host verification
249 const HttpRequest
*req
= psstate
->request
;
250 const bool isIntercepted
= !req
->flags
.redirected
&&
251 (req
->flags
.intercepted
|| req
->flags
.interceptTproxy
);
252 const bool useOriginalDst
= Config
.onoff
.client_dst_passthru
|| !req
->flags
.hostVerified
;
253 const bool choseDirect
= fs
&& fs
->code
== HIER_DIRECT
;
254 if (isIntercepted
&& useOriginalDst
&& choseDirect
) {
255 // check the client is still around before using any of its details
256 if (req
->clientConnectionManager
.valid()) {
257 // construct a "result" adding the ORIGINAL_DST to the set instead of DIRECT
258 Comm::ConnectionPointer p
= new Comm::Connection();
259 p
->remote
= req
->clientConnectionManager
->clientConnection
->local
;
260 p
->peerType
= ORIGINAL_DST
; // fs->code is DIRECT. This fixes the display.
261 p
->setPeer(fs
->_peer
);
263 // check for a configured outgoing address for this destination...
264 getOutgoingAddress(psstate
->request
, p
);
265 psstate
->paths
->push_back(p
);
268 // clear the used fs and continue
269 psstate
->servers
= fs
->next
;
270 cbdataReferenceDone(fs
->_peer
);
271 memFree(fs
, MEM_FWD_SERVER
);
272 peerSelectDnsPaths(psstate
);
276 // convert the list of FwdServer destinations into destinations IP addresses
277 if (fs
&& psstate
->paths
->size() < (unsigned int)Config
.forward_max_tries
) {
278 // send the next one off for DNS lookup.
279 const char *host
= fs
->_peer
? fs
->_peer
->host
: psstate
->request
->GetHost();
280 debugs(44, 2, "Find IP destination for: " << psstate
->entry
->url() << "' via " << host
);
281 ipcache_nbgethostbyname(host
, peerSelectDnsResults
, psstate
);
285 // Bug 3605: clear any extra listed FwdServer destinations, when the options exceeds max_foward_tries.
286 // due to the allocation method of fs, we must deallocate each manually.
287 // TODO: use a std::list so we can get the size and abort adding whenever the selection loops reach Config.forward_max_tries
288 if (fs
&& psstate
->paths
->size() >= (unsigned int)Config
.forward_max_tries
) {
290 FwdServer
*next
= fs
->next
;
291 cbdataReferenceDone(fs
->_peer
);
292 memFree(fs
, MEM_FWD_SERVER
);
297 // done with DNS lookups. pass back to caller
298 PSC
*callback
= psstate
->callback
;
299 psstate
->callback
= NULL
;
301 debugs(44, 2, (psstate
->paths
->size()<1?"Failed to select source":"Found sources") << " for '" << psstate
->url() << "'");
302 debugs(44, 2, " always_direct = " << psstate
->always_direct
);
303 debugs(44, 2, " never_direct = " << psstate
->never_direct
);
304 if (psstate
->paths
) {
305 for (size_t i
= 0; i
< psstate
->paths
->size(); ++i
) {
306 if ((*psstate
->paths
)[i
]->peerType
== HIER_DIRECT
)
307 debugs(44, 2, " DIRECT = " << (*psstate
->paths
)[i
]);
308 else if ((*psstate
->paths
)[i
]->peerType
== ORIGINAL_DST
)
309 debugs(44, 2, " ORIGINAL_DST = " << (*psstate
->paths
)[i
]);
310 else if ((*psstate
->paths
)[i
]->peerType
== PINNED
)
311 debugs(44, 2, " PINNED = " << (*psstate
->paths
)[i
]);
313 debugs(44, 2, " cache_peer = " << (*psstate
->paths
)[i
]);
316 debugs(44, 2, " timedout = " << psstate
->ping
.timedout
);
318 psstate
->ping
.stop
= current_time
;
319 psstate
->request
->hier
.ping
= psstate
->ping
;
322 if (cbdataReferenceValidDone(psstate
->callback_data
, &cbdata
)) {
323 callback(psstate
->paths
, psstate
->lastError
, cbdata
);
324 psstate
->lastError
= NULL
; // FwdState has taken control over the ErrorState object.
331 peerSelectDnsResults(const ipcache_addrs
*ia
, const DnsLookupDetails
&details
, void *data
)
333 ps_state
*psstate
= (ps_state
*)data
;
335 if (!cbdataReferenceValid(psstate
->callback_data
)) {
336 debugs(44, 3, "Aborting peer selection. Parent Job went away.");
341 psstate
->request
->recordLookup(details
);
343 FwdServer
*fs
= psstate
->servers
;
346 assert(ia
->cur
< ia
->count
);
348 // loop over each result address, adding to the possible destinations.
350 for (int n
= 0; n
< ia
->count
; ++n
, ++ip
) {
351 Comm::ConnectionPointer p
;
353 if (ip
>= ia
->count
) ip
= 0; // looped back to zero.
355 // Enforce forward_max_tries configuration.
356 if (psstate
->paths
->size() >= (unsigned int)Config
.forward_max_tries
)
359 // for TPROXY spoofing we must skip unusable addresses.
360 if (psstate
->request
->flags
.spoofClientIp
&& !(fs
->_peer
&& fs
->_peer
->options
.no_tproxy
) ) {
361 if (ia
->in_addrs
[ip
].isIPv4() != psstate
->request
->client_addr
.isIPv4()) {
362 // we CAN'T spoof the address on this link. find another.
367 p
= new Comm::Connection();
368 p
->remote
= ia
->in_addrs
[ip
];
370 // when IPv6 is disabled we cannot use it
371 if (!Ip::EnableIpv6
&& p
->remote
.isIPv6()) {
372 const char *host
= (fs
->_peer
? fs
->_peer
->host
: psstate
->request
->GetHost());
373 ipcacheMarkBadAddr(host
, p
->remote
);
378 p
->remote
.port(fs
->_peer
->http_port
);
380 p
->remote
.port(psstate
->request
->port
);
381 p
->peerType
= fs
->code
;
382 p
->setPeer(fs
->_peer
);
384 // check for a configured outgoing address for this destination...
385 getOutgoingAddress(psstate
->request
, p
);
386 psstate
->paths
->push_back(p
);
389 debugs(44, 3, HERE
<< "Unknown host: " << (fs
->_peer
? fs
->_peer
->host
: psstate
->request
->GetHost()));
390 // discard any previous error.
391 delete psstate
->lastError
;
392 psstate
->lastError
= NULL
;
393 if (fs
->code
== HIER_DIRECT
) {
394 psstate
->lastError
= new ErrorState(ERR_DNS_FAIL
, Http::scServiceUnavailable
, psstate
->request
);
395 psstate
->lastError
->dnsError
= details
.error
;
399 psstate
->servers
= fs
->next
;
400 cbdataReferenceDone(fs
->_peer
);
401 memFree(fs
, MEM_FWD_SERVER
);
403 // see if more paths can be found
404 peerSelectDnsPaths(psstate
);
408 peerCheckNetdbDirect(ps_state
* psstate
)
415 if (psstate
->direct
== DIRECT_NO
)
418 /* base lookup on RTT and Hops if ICMP NetDB is enabled. */
420 myrtt
= netdbHostRtt(psstate
->request
->GetHost());
422 debugs(44, 3, "peerCheckNetdbDirect: MY RTT = " << myrtt
<< " msec");
423 debugs(44, 3, "peerCheckNetdbDirect: minimum_direct_rtt = " << Config
.minDirectRtt
<< " msec");
425 if (myrtt
&& myrtt
<= Config
.minDirectRtt
)
428 myhops
= netdbHostHops(psstate
->request
->GetHost());
430 debugs(44, 3, "peerCheckNetdbDirect: MY hops = " << myhops
);
431 debugs(44, 3, "peerCheckNetdbDirect: minimum_direct_hops = " << Config
.minDirectHops
);
433 if (myhops
&& myhops
<= Config
.minDirectHops
)
436 p
= whichPeer(psstate
->closest_parent_miss
);
441 debugs(44, 3, "peerCheckNetdbDirect: closest_parent_miss RTT = " << psstate
->ping
.p_rtt
<< " msec");
443 if (myrtt
&& myrtt
<= psstate
->ping
.p_rtt
)
446 #endif /* USE_ICMP */
452 peerSelectFoo(ps_state
* ps
)
454 if (!cbdataReferenceValid(ps
->callback_data
)) {
455 debugs(44, 3, "Aborting peer selection. Parent Job went away.");
460 StoreEntry
*entry
= ps
->entry
;
461 HttpRequest
*request
= ps
->request
;
462 debugs(44, 3, "peerSelectFoo: '" << RequestMethodStr(request
->method
) << " " << request
->GetHost() << "'");
464 /** If we don't know whether DIRECT is permitted ... */
465 if (ps
->direct
== DIRECT_UNKNOWN
) {
466 if (ps
->always_direct
== ACCESS_DUNNO
) {
467 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (always_direct to be checked)");
468 /** check always_direct; */
469 ACLFilledChecklist
*ch
= new ACLFilledChecklist(Config
.accessList
.AlwaysDirect
, request
, NULL
);
471 ps
->acl_checklist
= ch
;
472 ps
->acl_checklist
->nonBlockingCheck(peerCheckAlwaysDirectDone
, ps
);
474 } else if (ps
->never_direct
== ACCESS_DUNNO
) {
475 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (never_direct to be checked)");
476 /** check never_direct; */
477 ACLFilledChecklist
*ch
= new ACLFilledChecklist(Config
.accessList
.NeverDirect
, request
, NULL
);
479 ps
->acl_checklist
= ch
;
480 ps
->acl_checklist
->nonBlockingCheck(peerCheckNeverDirectDone
, ps
);
482 } else if (request
->flags
.noDirect
) {
483 /** if we are accelerating, direct is not an option. */
484 ps
->direct
= DIRECT_NO
;
485 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (forced non-direct)");
486 } else if (request
->flags
.loopDetected
) {
487 /** if we are in a forwarding-loop, direct is not an option. */
488 ps
->direct
= DIRECT_YES
;
489 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (forwarding loop detected)");
490 } else if (peerCheckNetdbDirect(ps
)) {
491 ps
->direct
= DIRECT_YES
;
492 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (checkNetdbDirect)");
494 ps
->direct
= DIRECT_MAYBE
;
495 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
] << " (default)");
498 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr
[ps
->direct
]);
501 if (!entry
|| entry
->ping_status
== PING_NONE
)
502 peerSelectPinned(ps
);
505 } else if (entry
->ping_status
== PING_NONE
) {
506 peerGetSomeNeighbor(ps
);
508 if (entry
->ping_status
== PING_WAITING
)
510 } else if (entry
->ping_status
== PING_WAITING
) {
511 peerGetSomeNeighborReplies(ps
);
512 entry
->ping_status
= PING_DONE
;
515 switch (ps
->direct
) {
518 peerGetSomeDirect(ps
);
522 peerGetSomeParent(ps
);
523 peerGetAllParents(ps
);
528 if (Config
.onoff
.prefer_direct
)
529 peerGetSomeDirect(ps
);
531 if (request
->flags
.hierarchical
|| !Config
.onoff
.nonhierarchical_direct
) {
532 peerGetSomeParent(ps
);
533 peerGetAllParents(ps
);
536 if (!Config
.onoff
.prefer_direct
)
537 peerGetSomeDirect(ps
);
542 // resolve the possible peers
543 peerSelectDnsPaths(ps
);
546 bool peerAllowedToUse(const CachePeer
* p
, HttpRequest
* request
);
551 * Selects a pinned connection.
554 peerSelectPinned(ps_state
* ps
)
556 HttpRequest
*request
= ps
->request
;
557 if (!request
->pinnedConnection())
559 CachePeer
*pear
= request
->pinnedConnection()->pinnedPeer();
560 if (Comm::IsConnOpen(request
->pinnedConnection()->validatePinnedConnection(request
, pear
))) {
561 if (pear
&& peerAllowedToUse(pear
, request
)) {
562 peerAddFwdServer(&ps
->servers
, pear
, PINNED
);
564 ps
->entry
->ping_status
= PING_DONE
; /* Skip ICP */
565 } else if (!pear
&& ps
->direct
!= DIRECT_NO
) {
566 peerAddFwdServer(&ps
->servers
, NULL
, PINNED
);
568 ps
->entry
->ping_status
= PING_DONE
; /* Skip ICP */
574 * peerGetSomeNeighbor
576 * Selects a neighbor (parent or sibling) based on one of the
580 * ICMP Netdb RTT estimates
584 peerGetSomeNeighbor(ps_state
* ps
)
586 StoreEntry
*entry
= ps
->entry
;
587 HttpRequest
*request
= ps
->request
;
589 hier_code code
= HIER_NONE
;
590 assert(entry
->ping_status
== PING_NONE
);
592 if (ps
->direct
== DIRECT_YES
) {
593 entry
->ping_status
= PING_DONE
;
597 #if USE_CACHE_DIGESTS
598 if ((p
= neighborsDigestSelect(request
))) {
599 if (neighborType(p
, request
) == PEER_PARENT
)
600 code
= CD_PARENT_HIT
;
602 code
= CD_SIBLING_HIT
;
605 if ((p
= netdbClosestParent(request
))) {
606 code
= CLOSEST_PARENT
;
607 } else if (peerSelectIcpPing(request
, ps
->direct
, entry
)) {
608 debugs(44, 3, "peerSelect: Doing ICP pings");
609 ps
->ping
.start
= current_time
;
610 ps
->ping
.n_sent
= neighborsUdpPing(request
,
614 &ps
->ping
.n_replies_expected
,
617 if (ps
->ping
.n_sent
== 0)
618 debugs(44, DBG_CRITICAL
, "WARNING: neighborsUdpPing returned 0");
619 debugs(44, 3, "peerSelect: " << ps
->ping
.n_replies_expected
<<
620 " ICP replies expected, RTT " << ps
->ping
.timeout
<<
623 if (ps
->ping
.n_replies_expected
> 0) {
624 entry
->ping_status
= PING_WAITING
;
625 eventAdd("peerPingTimeout",
628 0.001 * ps
->ping
.timeout
,
634 if (code
!= HIER_NONE
) {
636 debugs(44, 3, "peerSelect: " << hier_code_str
[code
] << "/" << p
->host
);
637 peerAddFwdServer(&ps
->servers
, p
, code
);
640 entry
->ping_status
= PING_DONE
;
644 * peerGetSomeNeighborReplies
646 * Selects a neighbor (parent or sibling) based on ICP/HTCP replies.
649 peerGetSomeNeighborReplies(ps_state
* ps
)
651 HttpRequest
*request
= ps
->request
;
653 hier_code code
= HIER_NONE
;
654 assert(ps
->entry
->ping_status
== PING_WAITING
);
655 assert(ps
->direct
!= DIRECT_YES
);
657 if (peerCheckNetdbDirect(ps
)) {
658 code
= CLOSEST_DIRECT
;
659 debugs(44, 3, "peerSelect: " << hier_code_str
[code
] << "/" << request
->GetHost());
660 peerAddFwdServer(&ps
->servers
, NULL
, code
);
665 code
= ps
->hit_type
== PEER_PARENT
? PARENT_HIT
: SIBLING_HIT
;
667 if (!ps
->closest_parent_miss
.isAnyAddr()) {
668 p
= whichPeer(ps
->closest_parent_miss
);
669 code
= CLOSEST_PARENT_MISS
;
670 } else if (!ps
->first_parent_miss
.isAnyAddr()) {
671 p
= whichPeer(ps
->first_parent_miss
);
672 code
= FIRST_PARENT_MISS
;
675 if (p
&& code
!= HIER_NONE
) {
676 debugs(44, 3, "peerSelect: " << hier_code_str
[code
] << "/" << p
->host
);
677 peerAddFwdServer(&ps
->servers
, p
, code
);
684 * Simply adds a 'direct' entry to the FwdServers list if this
685 * request can be forwarded directly to the origin server
688 peerGetSomeDirect(ps_state
* ps
)
690 if (ps
->direct
== DIRECT_NO
)
693 /* WAIS is not implemented natively */
694 if (ps
->request
->protocol
== AnyP::PROTO_WAIS
)
697 peerAddFwdServer(&ps
->servers
, NULL
, HIER_DIRECT
);
701 peerGetSomeParent(ps_state
* ps
)
704 HttpRequest
*request
= ps
->request
;
705 hier_code code
= HIER_NONE
;
706 debugs(44, 3, "peerGetSomeParent: " << RequestMethodStr(request
->method
) << " " << request
->GetHost());
708 if (ps
->direct
== DIRECT_YES
)
711 if ((p
= peerSourceHashSelectParent(request
))) {
712 code
= SOURCEHASH_PARENT
;
714 } else if ((p
= peerUserHashSelectParent(request
))) {
715 code
= USERHASH_PARENT
;
717 } else if ((p
= carpSelectParent(request
))) {
719 } else if ((p
= getRoundRobinParent(request
))) {
720 code
= ROUNDROBIN_PARENT
;
721 } else if ((p
= getWeightedRoundRobinParent(request
))) {
722 code
= ROUNDROBIN_PARENT
;
723 } else if ((p
= getFirstUpParent(request
))) {
724 code
= FIRSTUP_PARENT
;
725 } else if ((p
= getDefaultParent(request
))) {
726 code
= DEFAULT_PARENT
;
729 if (code
!= HIER_NONE
) {
730 debugs(44, 3, "peerSelect: " << hier_code_str
[code
] << "/" << p
->host
);
731 peerAddFwdServer(&ps
->servers
, p
, code
);
735 /* Adds alive parents. Used as a last resort for never_direct.
738 peerGetAllParents(ps_state
* ps
)
741 HttpRequest
*request
= ps
->request
;
742 /* Add all alive parents */
744 for (p
= Config
.peers
; p
; p
= p
->next
) {
745 /* XXX: neighbors.c lacks a public interface for enumerating
746 * parents to a request so we have to dig some here..
749 if (neighborType(p
, request
) != PEER_PARENT
)
752 if (!peerHTTPOkay(p
, request
))
755 debugs(15, 3, "peerGetAllParents: adding alive parent " << p
->host
);
757 peerAddFwdServer(&ps
->servers
, p
, ANY_OLD_PARENT
);
760 /* XXX: should add dead parents here, but it is currently
761 * not possible to find out which parents are dead or which
762 * simply are not configured to handle the request.
764 /* Add default parent as a last resort */
765 if ((p
= getDefaultParent(request
))) {
766 peerAddFwdServer(&ps
->servers
, p
, DEFAULT_PARENT
);
771 peerPingTimeout(void *data
)
773 ps_state
*psstate
= (ps_state
*)data
;
774 StoreEntry
*entry
= psstate
->entry
;
777 debugs(44, 3, "peerPingTimeout: '" << entry
->url() << "'" );
779 if (!cbdataReferenceValid(psstate
->callback_data
)) {
780 /* request aborted */
781 entry
->ping_status
= PING_DONE
;
782 cbdataReferenceDone(psstate
->callback_data
);
787 ++PeerStats
.timeouts
;
788 psstate
->ping
.timedout
= 1;
789 peerSelectFoo(psstate
);
795 memset(&PeerStats
, '\0', sizeof(PeerStats
));
796 memDataInit(MEM_FWD_SERVER
, "FwdServer", sizeof(FwdServer
), 0);
800 peerIcpParentMiss(CachePeer
* p
, icp_common_t
* header
, ps_state
* ps
)
805 if (Config
.onoff
.query_icmp
) {
806 if (header
->flags
& ICP_FLAG_SRC_RTT
) {
807 rtt
= header
->pad
& 0xFFFF;
808 int hops
= (header
->pad
>> 16) & 0xFFFF;
810 if (rtt
> 0 && rtt
< 0xFFFF)
811 netdbUpdatePeer(ps
->request
, p
, rtt
, hops
);
813 if (rtt
&& (ps
->ping
.p_rtt
== 0 || rtt
< ps
->ping
.p_rtt
)) {
814 ps
->closest_parent_miss
= p
->in_addr
;
815 ps
->ping
.p_rtt
= rtt
;
819 #endif /* USE_ICMP */
821 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
822 if (p
->options
.closest_only
)
825 /* set FIRST_MISS if there is no CLOSEST parent */
826 if (!ps
->closest_parent_miss
.isAnyAddr())
829 rtt
= (tvSubMsec(ps
->ping
.start
, current_time
) - p
->basetime
) / p
->weight
;
834 if (ps
->first_parent_miss
.isAnyAddr() || rtt
< ps
->ping
.w_rtt
) {
835 ps
->first_parent_miss
= p
->in_addr
;
836 ps
->ping
.w_rtt
= rtt
;
841 peerHandleIcpReply(CachePeer
* p
, peer_t type
, icp_common_t
* header
, void *data
)
843 ps_state
*psstate
= (ps_state
*)data
;
844 icp_opcode op
= header
->getOpCode();
845 debugs(44, 3, "peerHandleIcpReply: " << icp_opcode_str
[op
] << " " << psstate
->entry
->url() );
846 #if USE_CACHE_DIGESTS && 0
847 /* do cd lookup to count false misses */
850 peerNoteDigestLookup(request
, p
,
851 peerDigestLookup(p
, request
, psstate
->entry
));
855 ++ psstate
->ping
.n_recv
;
857 if (op
== ICP_MISS
|| op
== ICP_DECHO
) {
858 if (type
== PEER_PARENT
)
859 peerIcpParentMiss(p
, header
, psstate
);
860 } else if (op
== ICP_HIT
) {
862 psstate
->hit_type
= type
;
863 peerSelectFoo(psstate
);
867 if (psstate
->ping
.n_recv
< psstate
->ping
.n_replies_expected
)
870 peerSelectFoo(psstate
);
875 peerHandleHtcpReply(CachePeer
* p
, peer_t type
, HtcpReplyData
* htcp
, void *data
)
877 ps_state
*psstate
= (ps_state
*)data
;
878 debugs(44, 3, "peerHandleHtcpReply: " <<
879 (htcp
->hit
? "HIT" : "MISS") << " " <<
880 psstate
->entry
->url() );
881 ++ psstate
->ping
.n_recv
;
885 psstate
->hit_type
= type
;
886 peerSelectFoo(psstate
);
890 if (type
== PEER_PARENT
)
891 peerHtcpParentMiss(p
, htcp
, psstate
);
893 if (psstate
->ping
.n_recv
< psstate
->ping
.n_replies_expected
)
896 peerSelectFoo(psstate
);
900 peerHtcpParentMiss(CachePeer
* p
, HtcpReplyData
* htcp
, ps_state
* ps
)
905 if (Config
.onoff
.query_icmp
) {
906 if (htcp
->cto
.rtt
> 0) {
907 rtt
= (int) htcp
->cto
.rtt
* 1000;
908 int hops
= (int) htcp
->cto
.hops
* 1000;
909 netdbUpdatePeer(ps
->request
, p
, rtt
, hops
);
911 if (rtt
&& (ps
->ping
.p_rtt
== 0 || rtt
< ps
->ping
.p_rtt
)) {
912 ps
->closest_parent_miss
= p
->in_addr
;
913 ps
->ping
.p_rtt
= rtt
;
917 #endif /* USE_ICMP */
919 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
920 if (p
->options
.closest_only
)
923 /* set FIRST_MISS if there is no CLOSEST parent */
924 if (!ps
->closest_parent_miss
.isAnyAddr())
927 rtt
= (tvSubMsec(ps
->ping
.start
, current_time
) - p
->basetime
) / p
->weight
;
932 if (ps
->first_parent_miss
.isAnyAddr() || rtt
< ps
->ping
.w_rtt
) {
933 ps
->first_parent_miss
= p
->in_addr
;
934 ps
->ping
.w_rtt
= rtt
;
941 peerHandlePingReply(CachePeer
* p
, peer_t type
, AnyP::ProtocolType proto
, void *pingdata
, void *data
)
943 if (proto
== AnyP::PROTO_ICP
)
944 peerHandleIcpReply(p
, type
, (icp_common_t
*)pingdata
, data
);
948 else if (proto
== AnyP::PROTO_HTCP
)
949 peerHandleHtcpReply(p
, type
, (HtcpReplyData
*)pingdata
, data
);
954 debugs(44, DBG_IMPORTANT
, "peerHandlePingReply: unknown protocol " << proto
);
958 peerAddFwdServer(FwdServer
** FSVR
, CachePeer
* p
, hier_code code
)
960 FwdServer
*fs
= (FwdServer
*)memAllocate(MEM_FWD_SERVER
);
961 debugs(44, 5, "peerAddFwdServer: adding " <<
962 (p
? p
->host
: "DIRECT") << " " <<
963 hier_code_str
[code
] );
964 fs
->_peer
= cbdataReference(p
);
968 FSVR
= &(*FSVR
)->next
;
973 ps_state::ps_state() : request (NULL
),
975 always_direct(Config
.accessList
.AlwaysDirect
?ACCESS_DUNNO
:ACCESS_DENIED
),
976 never_direct(Config
.accessList
.NeverDirect
?ACCESS_DUNNO
:ACCESS_DENIED
),
977 direct(DIRECT_UNKNOWN
),
979 callback_data (NULL
),
983 closest_parent_miss(),
988 ; // no local defaults.
992 ps_state::url() const
998 return urlCanonical(request
);
1003 ping_data::ping_data() :
1006 n_replies_expected(0),