]> git.ipfire.org Git - thirdparty/squid.git/blame - src/peer_select.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / peer_select.cc
CommitLineData
062e2281 1/*
262a0e14 2 * $Id$
062e2281 3 *
4 * DEBUG: section 44 Peer Selection Algorithm
5 * AUTHOR: Duane Wessels
6 *
2b6662ba 7 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 8 * ----------------------------------------------------------
062e2281 9 *
2b6662ba 10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
062e2281 18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
26ac0430 23 *
062e2281 24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
26ac0430 28 *
062e2281 29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
cbdec147 31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 32 *
062e2281 33 */
34
f7f3304a 35#include "squid-old.h"
cfd66529 36#include "DnsLookupDetails.h"
a37fdd8a 37#include "errorpage.h"
a553a5a3 38#include "event.h"
b24880fe 39#include "PeerSelectState.h"
e6ccf245 40#include "Store.h"
bbaf2685 41#include "hier_code.h"
cfd66529 42#include "ICP.h"
528b2c61 43#include "HttpRequest.h"
127dce76 44#include "acl/FilledChecklist.h"
924f73bc 45#include "htcp.h"
b6b6f466 46#include "forward.h"
cfd66529 47#include "SquidTime.h"
9b5c4a9a 48#include "icmp/net_db.h"
714e68b7 49#include "ipcache.h"
062e2281 50
26ac0430 51static struct {
75e88d56 52 int timeouts;
2fadd50d 53} PeerStats;
062e2281 54
26ac0430
AJ
55static const char *DirectStr[] = {
56 "DIRECT_UNKNOWN",
57 "DIRECT_NO",
58 "DIRECT_MAYBE",
59 "DIRECT_YES"
60};
75e88d56 61
f5b8bbc4 62static void peerSelectFoo(ps_state *);
63static void peerPingTimeout(void *data);
86aebcda 64static IRCB peerHandlePingReply;
f5b8bbc4 65static void peerSelectStateFree(ps_state * psstate);
66static void peerIcpParentMiss(peer *, icp_common_t *, ps_state *);
44e237d0 67#if USE_HTCP
68static void peerHtcpParentMiss(peer *, htcpReplyData *, ps_state *);
69static void peerHandleHtcpReply(peer *, peer_t, htcpReplyData *, void *);
70#endif
f5b8bbc4 71static int peerCheckNetdbDirect(ps_state * psstate);
db1cd23c 72static void peerGetSomeNeighbor(ps_state *);
73static void peerGetSomeNeighborReplies(ps_state *);
74static void peerGetSomeDirect(ps_state *);
75static void peerGetSomeParent(ps_state *);
168dfda9 76static void peerGetAllParents(ps_state *);
db1cd23c 77static void peerAddFwdServer(FwdServer **, peer *, hier_code);
d67acb4e 78static void peerSelectPinned(ps_state * ps);
cfd66529
AJ
79static void peerSelectDnsResults(const ipcache_addrs *ia, const DnsLookupDetails &details, void *data);
80
aa839030 81CBDATA_CLASS_INIT(ps_state);
82
348b2031 83static void
84peerSelectStateFree(ps_state * psstate)
85{
5ae21d99
AJ
86 if (psstate->entry) {
87 debugs(44, 3, HERE << psstate->entry->url());
88
89 if (psstate->entry->ping_status == PING_WAITING)
90 eventDelete(peerPingTimeout, psstate);
91
92 psstate->entry->ping_status = PING_DONE;
93 }
94
348b2031 95 if (psstate->acl_checklist) {
bf8fe701 96 debugs(44, 1, "calling aclChecklistFree() from peerSelectStateFree");
62e76326 97 delete (psstate->acl_checklist);
348b2031 98 }
62e76326 99
6dd9f4bd 100 HTTPMSGUNLOCK(psstate->request);
62e76326 101
73a201f8 102 if (psstate->entry) {
62e76326 103 assert(psstate->entry->ping_status != PING_WAITING);
97b5e68f 104 psstate->entry->unlock();
62e76326 105 psstate->entry = NULL;
73a201f8 106 }
62e76326 107
a37fdd8a
AJ
108 delete psstate->lastError;
109
8407afee 110 cbdataFree(psstate);
348b2031 111}
062e2281 112
2d72d4fd 113static int
190154cf 114peerSelectIcpPing(HttpRequest * request, int direct, StoreEntry * entry)
062e2281 115{
7b665aeb 116 int n;
db1cd23c 117 assert(entry);
118 assert(entry->ping_status == PING_NONE);
9bd6a36d 119 assert(direct != DIRECT_YES);
bf8fe701 120 debugs(44, 3, "peerSelectIcpPing: " << entry->url() );
62e76326 121
92695e5e 122 if (!request->flags.hierarchical && direct != DIRECT_NO)
62e76326 123 return 0;
124
d46a87a8 125 if (EBIT_TEST(entry->flags, KEY_PRIVATE) && !neighbors_do_private_keys)
62e76326 126 if (direct != DIRECT_NO)
127 return 0;
128
7b665aeb 129 n = neighborsCount(request);
62e76326 130
bf8fe701 131 debugs(44, 3, "peerSelectIcpPing: counted " << n << " neighbors");
62e76326 132
7b665aeb 133 return n;
062e2281 134}
135
136
062e2281 137void
00ae51e4 138peerSelect(Comm::ConnectionList * paths,
cfd66529 139 HttpRequest * request,
62e76326 140 StoreEntry * entry,
141 PSC * callback,
142 void *callback_data)
75e88d56 143{
28c60158 144 ps_state *psstate;
62e76326 145
86b389fc 146 if (entry)
bf8fe701 147 debugs(44, 3, "peerSelect: " << entry->url() );
86b389fc 148 else
60745f24 149 debugs(44, 3, "peerSelect: " << RequestMethodStr(request->method));
62e76326 150
b24880fe 151 psstate = new ps_state;
62e76326 152
6dd9f4bd 153 psstate->request = HTTPMSGLOCK(request);
62e76326 154
b6c0e933 155 psstate->entry = entry;
cfd66529
AJ
156 psstate->paths = paths;
157
b6c0e933 158 psstate->callback = callback;
62e76326 159
fa80a8ef 160 psstate->callback_data = cbdataReference(callback_data);
62e76326 161
6cfa8966 162#if USE_CACHE_DIGESTS
62e76326 163
39edba21 164 request->hier.peer_select_start = current_time;
62e76326 165
39edba21 166#endif
62e76326 167
2395cb21 168 if (psstate->entry)
cc192b50 169 psstate->entry->lock();
62e76326 170
b6c0e933 171 peerSelectFoo(psstate);
75e88d56 172}
173
174static void
2efeb0b7 175peerCheckNeverDirectDone(allow_t answer, void *data)
75e88d56 176{
e6ccf245 177 ps_state *psstate = (ps_state *) data;
348b2031 178 psstate->acl_checklist = NULL;
bf8fe701 179 debugs(44, 3, "peerCheckNeverDirectDone: " << answer);
2efeb0b7 180 psstate->never_direct = answer;
3985b3f8 181 switch (answer) {
ae026ec6 182 case ACCESS_ALLOWED:
f365d297
AJ
183 /** if never_direct says YES, do that. */
184 psstate->direct = DIRECT_NO;
ae026ec6
AJ
185 debugs(44, 3, HERE << "direct = " << DirectStr[psstate->direct] << " (never_direct allow)");
186 break;
187 case ACCESS_DENIED: // not relevant.
1f585949 188 case ACCESS_DUNNO: // not relevant.
ae026ec6 189 break;
1f585949
AJ
190 case ACCESS_AUTH_REQUIRED:
191 case ACCESS_AUTH_EXPIRED_OK:
192 case ACCESS_AUTH_EXPIRED_BAD:
ae026ec6 193 debugs(44, DBG_IMPORTANT, "WARNING: never_direct resulted in " << answer << ". Username ACLs are not reliable here.");
1f585949 194 break;
ae026ec6 195 }
b6c0e933 196 peerSelectFoo(psstate);
75e88d56 197}
198
199static void
2efeb0b7 200peerCheckAlwaysDirectDone(allow_t answer, void *data)
75e88d56 201{
e6ccf245 202 ps_state *psstate = (ps_state *)data;
348b2031 203 psstate->acl_checklist = NULL;
bf8fe701 204 debugs(44, 3, "peerCheckAlwaysDirectDone: " << answer);
2efeb0b7 205 psstate->always_direct = answer;
3985b3f8 206 switch (answer) {
ae026ec6
AJ
207 case ACCESS_ALLOWED:
208 /** if always_direct says YES, do that. */
209 psstate->direct = DIRECT_YES;
210 debugs(44, 3, HERE << "direct = " << DirectStr[psstate->direct] << " (always_direct allow)");
211 break;
212 case ACCESS_DENIED: // not relevant.
1f585949 213 case ACCESS_DUNNO: // not relevant.
ae026ec6 214 break;
1f585949
AJ
215 case ACCESS_AUTH_REQUIRED:
216 case ACCESS_AUTH_EXPIRED_OK:
217 case ACCESS_AUTH_EXPIRED_BAD:
ae026ec6 218 debugs(44, DBG_IMPORTANT, "WARNING: always_direct resulted in " << answer << ". Username ACLs are not reliable here.");
1f585949 219 break;
ae026ec6 220 }
b6c0e933 221 peerSelectFoo(psstate);
75e88d56 222}
223
cfd66529
AJ
224void
225peerSelectDnsPaths(ps_state *psstate)
226{
227 FwdServer *fs = psstate->servers;
228
cfd66529 229 // convert the list of FwdServer destinations into destinations IP addresses
d6327017 230 if (fs && psstate->paths->size() < (unsigned int)Config.forward_max_tries) {
cfd66529
AJ
231 // send the next one off for DNS lookup.
232 const char *host = fs->_peer ? fs->_peer->host : psstate->request->GetHost();
3dfe8371 233 debugs(44, 2, "Find IP destination for: " << psstate->entry->url() << "' via " << host);
cfd66529
AJ
234 ipcache_nbgethostbyname(host, peerSelectDnsResults, psstate);
235 return;
236 }
237
238 // done with DNS lookups. pass back to caller
3dfe8371 239 PSC *callback = psstate->callback;
fa80a8ef 240 psstate->callback = NULL;
62e76326 241
5ae21d99 242 if (psstate->paths->size() < 1) {
ce63af7f
AJ
243 debugs(44, DBG_IMPORTANT, "Failed to select source for '" << psstate->entry->url() << "'");
244 debugs(44, DBG_IMPORTANT, " always_direct = " << psstate->always_direct);
245 debugs(44, DBG_IMPORTANT, " never_direct = " << psstate->never_direct);
246 debugs(44, DBG_IMPORTANT, " timedout = " << psstate->ping.timedout);
5ae21d99 247 } else {
ce63af7f
AJ
248 debugs(44, 2, "Found sources for '" << psstate->entry->url() << "'");
249 debugs(44, 2, " always_direct = " << psstate->always_direct);
250 debugs(44, 2, " never_direct = " << psstate->never_direct);
efbd2436 251 for (size_t i = 0; i < psstate->paths->size(); i++) {
ce63af7f
AJ
252 if ((*psstate->paths)[i]->peerType == HIER_DIRECT)
253 debugs(44, 2, " DIRECT = " << (*psstate->paths)[i]);
254 else
255 debugs(44, 2, " cache_peer = " << (*psstate->paths)[i]);
efbd2436 256 }
ce63af7f 257 debugs(44, 2, " timedout = " << psstate->ping.timedout);
5ae21d99
AJ
258 }
259
260 psstate->ping.stop = current_time;
261 psstate->request->hier.ping = psstate->ping;
3dfe8371 262
cfd66529 263 void *cbdata;
fa80a8ef 264 if (cbdataReferenceValidDone(psstate->callback_data, &cbdata)) {
a37fdd8a
AJ
265 callback(psstate->paths, psstate->lastError, cbdata);
266 psstate->lastError = NULL; // FwdState has taken control over the ErrorState object.
db1cd23c 267 }
62e76326 268
348b2031 269 peerSelectStateFree(psstate);
93775f90 270}
271
cfd66529
AJ
272static void
273peerSelectDnsResults(const ipcache_addrs *ia, const DnsLookupDetails &details, void *data)
274{
275 ps_state *psstate = (ps_state *)data;
276
277 psstate->request->recordLookup(details);
278
279 FwdServer *fs = psstate->servers;
280 if (ia != NULL) {
281
282 assert(ia->cur < ia->count);
283
284 // loop over each result address, adding to the possible destinations.
cfd66529
AJ
285 int ip = ia->cur;
286 for (int n = 0; n < ia->count; n++, ip++) {
5229395c
AJ
287 Comm::ConnectionPointer p;
288
cfd66529
AJ
289 if (ip >= ia->count) ip = 0; // looped back to zero.
290
27d1f0a0 291 // Enforce forward_max_tries configuration.
d6327017 292 if (psstate->paths->size() >= (unsigned int)Config.forward_max_tries)
27d1f0a0
AJ
293 break;
294
cfd66529
AJ
295 // for TPROXY we must skip unusable addresses.
296 if (psstate->request->flags.spoof_client_ip && !(fs->_peer && fs->_peer->options.no_tproxy) ) {
dc49061a 297 if (ia->in_addrs[n].IsIPv4() != psstate->request->client_addr.IsIPv4()) {
cfd66529
AJ
298 // we CAN'T spoof the address on this link. find another.
299 continue;
300 }
301 }
302
303 p = new Comm::Connection();
304 p->remote = ia->in_addrs[n];
cb443714
AJ
305 if (fs->_peer)
306 p->remote.SetPort(fs->_peer->http_port);
307 else
308 p->remote.SetPort(psstate->request->port);
5229395c 309 p->peerType = fs->code;
ce63af7f 310 p->setPeer(fs->_peer);
cfd66529
AJ
311
312 // check for a configured outgoing address for this destination...
313 getOutgoingAddress(psstate->request, p);
07f889c1 314 psstate->paths->push_back(p);
cfd66529
AJ
315 }
316 } else {
317 debugs(44, 3, HERE << "Unknown host: " << fs->_peer ? fs->_peer->host : psstate->request->GetHost());
a37fdd8a
AJ
318 // discard any previous error.
319 delete psstate->lastError;
320 psstate->lastError = NULL;
321 if (fs->code == HIER_DIRECT) {
322 psstate->lastError = new ErrorState(ERR_DNS_FAIL, HTTP_SERVICE_UNAVAILABLE, psstate->request);
323 psstate->lastError->dnsError = details.error;
324 }
cfd66529
AJ
325 }
326
327 psstate->servers = fs->next;
328 cbdataReferenceDone(fs->_peer);
329 memFree(fs, MEM_FWD_SERVER);
330
331 // see if more paths can be found
332 peerSelectDnsPaths(psstate);
333}
334
b3264694 335static int
336peerCheckNetdbDirect(ps_state * psstate)
337{
9b5c4a9a 338#if USE_ICMP
5f84d830 339 peer *p;
b3264694 340 int myrtt;
341 int myhops;
62e76326 342
0886a797 343 if (psstate->direct == DIRECT_NO)
62e76326 344 return 0;
345
9b5c4a9a
AJ
346 /* base lookup on RTT and Hops if ICMP NetDB is enabled. */
347
cc192b50 348 myrtt = netdbHostRtt(psstate->request->GetHost());
62e76326 349
bf8fe701 350 debugs(44, 3, "peerCheckNetdbDirect: MY RTT = " << myrtt << " msec");
351 debugs(44, 3, "peerCheckNetdbDirect: minimum_direct_rtt = " << Config.minDirectRtt << " msec");
62e76326 352
5f84d830 353 if (myrtt && myrtt <= Config.minDirectRtt)
62e76326 354 return 1;
355
cc192b50 356 myhops = netdbHostHops(psstate->request->GetHost());
62e76326 357
bf8fe701 358 debugs(44, 3, "peerCheckNetdbDirect: MY hops = " << myhops);
359 debugs(44, 3, "peerCheckNetdbDirect: minimum_direct_hops = " << Config.minDirectHops);
62e76326 360
b3264694 361 if (myhops && myhops <= Config.minDirectHops)
62e76326 362 return 1;
363
cc192b50 364 p = whichPeer(psstate->closest_parent_miss);
62e76326 365
5f84d830 366 if (p == NULL)
62e76326 367 return 0;
368
bf8fe701 369 debugs(44, 3, "peerCheckNetdbDirect: closest_parent_miss RTT = " << psstate->ping.p_rtt << " msec");
62e76326 370
5f84d830 371 if (myrtt && myrtt <= psstate->ping.p_rtt)
62e76326 372 return 1;
373
9b5c4a9a
AJ
374#endif /* USE_ICMP */
375
b3264694 376 return 0;
377}
378
93775f90 379static void
511f47bb 380peerSelectFoo(ps_state * ps)
062e2281 381{
511f47bb 382 StoreEntry *entry = ps->entry;
190154cf 383 HttpRequest *request = ps->request;
60745f24 384 debugs(44, 3, "peerSelectFoo: '" << RequestMethodStr(request->method) << " " << request->GetHost() << "'");
62e76326 385
ae026ec6 386 /** If we don't know whether DIRECT is permitted ... */
511f47bb 387 if (ps->direct == DIRECT_UNKNOWN) {
ae026ec6
AJ
388 if (ps->always_direct == ACCESS_DUNNO) {
389 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr[ps->direct] << " (always_direct to be checked)");
b50e327b 390 /** check always_direct; */
ae026ec6 391 ps->acl_checklist = new ACLFilledChecklist(Config.accessList.AlwaysDirect, request, NULL);
b50e327b 392 ps->acl_checklist->nonBlockingCheck(peerCheckAlwaysDirectDone, ps);
62e76326 393 return;
ae026ec6
AJ
394 } else if (ps->never_direct == ACCESS_DUNNO) {
395 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr[ps->direct] << " (never_direct to be checked)");
b50e327b 396 /** check never_direct; */
ae026ec6
AJ
397 ps->acl_checklist = new ACLFilledChecklist(Config.accessList.NeverDirect, request, NULL);
398 ps->acl_checklist->nonBlockingCheck(peerCheckNeverDirectDone, ps);
62e76326 399 return;
7f7bdd96 400 } else if (request->flags.no_direct) {
b50e327b 401 /** if we are accelerating, direct is not an option. */
62e76326 402 ps->direct = DIRECT_NO;
ae026ec6 403 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr[ps->direct] << " (forced non-direct)");
62e76326 404 } else if (request->flags.loopdetect) {
b50e327b 405 /** if we are in a forwarding-loop, direct is not an option. */
62e76326 406 ps->direct = DIRECT_YES;
ae026ec6 407 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr[ps->direct] << " (forwarding loop detected)");
62e76326 408 } else if (peerCheckNetdbDirect(ps)) {
409 ps->direct = DIRECT_YES;
ae026ec6 410 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr[ps->direct] << " (checkNetdbDirect)");
62e76326 411 } else {
412 ps->direct = DIRECT_MAYBE;
ae026ec6 413 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr[ps->direct] << " (default)");
62e76326 414 }
415
bf8fe701 416 debugs(44, 3, "peerSelectFoo: direct = " << DirectStr[ps->direct]);
db1cd23c 417 }
62e76326 418
d67acb4e
AJ
419 if (!entry || entry->ping_status == PING_NONE)
420 peerSelectPinned(ps);
df503d6c 421 if (entry == NULL) {
62e76326 422 (void) 0;
df503d6c 423 } else if (entry->ping_status == PING_NONE) {
62e76326 424 peerGetSomeNeighbor(ps);
425
426 if (entry->ping_status == PING_WAITING)
427 return;
db1cd23c 428 } else if (entry->ping_status == PING_WAITING) {
62e76326 429 peerGetSomeNeighborReplies(ps);
430 entry->ping_status = PING_DONE;
db1cd23c 431 }
62e76326 432
168dfda9 433 switch (ps->direct) {
62e76326 434
168dfda9 435 case DIRECT_YES:
62e76326 436 peerGetSomeDirect(ps);
437 break;
438
168dfda9 439 case DIRECT_NO:
62e76326 440 peerGetSomeParent(ps);
441 peerGetAllParents(ps);
442 break;
443
168dfda9 444 default:
62e76326 445
446 if (Config.onoff.prefer_direct)
447 peerGetSomeDirect(ps);
448
8b9a89c9 449 if (request->flags.hierarchical || !Config.onoff.nonhierarchical_direct) {
62e76326 450 peerGetSomeParent(ps);
8b9a89c9
AJ
451 peerGetAllParents(ps);
452 }
62e76326 453
454 if (!Config.onoff.prefer_direct)
455 peerGetSomeDirect(ps);
456
457 break;
168dfda9 458 }
62e76326 459
3dfe8371
AJ
460 // resolve the possible peers
461 peerSelectDnsPaths(ps);
db1cd23c 462}
463
2efeb0b7 464bool peerAllowedToUse(const peer * p, HttpRequest * request);
cfd66529 465
27774cee 466/**
d67acb4e
AJ
467 * peerSelectPinned
468 *
27774cee 469 * Selects a pinned connection.
d67acb4e 470 */
d67acb4e
AJ
471static void
472peerSelectPinned(ps_state * ps)
473{
474 HttpRequest *request = ps->request;
d67acb4e
AJ
475 if (!request->pinnedConnection())
476 return;
e3a4aecc
AJ
477 peer *pear = request->pinnedConnection()->pinnedPeer();
478 if (Comm::IsConnOpen(request->pinnedConnection()->validatePinnedConnection(request, pear))) {
479 if (pear && peerAllowedToUse(pear, request)) {
480 peerAddFwdServer(&ps->servers, pear, PINNED);
d67acb4e
AJ
481 if (ps->entry)
482 ps->entry->ping_status = PING_DONE; /* Skip ICP */
e3a4aecc 483 } else if (!pear && ps->direct != DIRECT_NO) {
d67acb4e
AJ
484 peerAddFwdServer(&ps->servers, NULL, PINNED);
485 if (ps->entry)
486 ps->entry->ping_status = PING_DONE; /* Skip ICP */
487 }
488 }
489}
490
27774cee 491/**
db1cd23c 492 * peerGetSomeNeighbor
26ac0430 493 *
db1cd23c 494 * Selects a neighbor (parent or sibling) based on one of the
495 * following methods:
496 * Cache Digests
497 * CARP
9b5c4a9a 498 * ICMP Netdb RTT estimates
db1cd23c 499 * ICP/HTCP queries
500 */
501static void
502peerGetSomeNeighbor(ps_state * ps)
503{
504 StoreEntry *entry = ps->entry;
190154cf 505 HttpRequest *request = ps->request;
db1cd23c 506 peer *p;
507 hier_code code = HIER_NONE;
508 assert(entry->ping_status == PING_NONE);
62e76326 509
db1cd23c 510 if (ps->direct == DIRECT_YES) {
62e76326 511 entry->ping_status = PING_DONE;
512 return;
124511e5 513 }
62e76326 514
6cfa8966 515#if USE_CACHE_DIGESTS
f66a9ef4 516 if ((p = neighborsDigestSelect(request))) {
62e76326 517 if (neighborType(p, request) == PEER_PARENT)
518 code = CD_PARENT_HIT;
519 else
520 code = CD_SIBLING_HIT;
db1cd23c 521 } else
c127134a 522#endif
62e76326 523 if ((p = netdbClosestParent(request))) {
524 code = CLOSEST_PARENT;
525 } else if (peerSelectIcpPing(request, ps->direct, entry)) {
bf8fe701 526 debugs(44, 3, "peerSelect: Doing ICP pings");
62e76326 527 ps->ping.start = current_time;
528 ps->ping.n_sent = neighborsUdpPing(request,
529 entry,
530 peerHandlePingReply,
531 ps,
532 &ps->ping.n_replies_expected,
533 &ps->ping.timeout);
534
535 if (ps->ping.n_sent == 0)
bf8fe701 536 debugs(44, 0, "WARNING: neighborsUdpPing returned 0");
26ac0430
AJ
537 debugs(44, 3, "peerSelect: " << ps->ping.n_replies_expected <<
538 " ICP replies expected, RTT " << ps->ping.timeout <<
539 " msec");
62e76326 540
62e76326 541
542 if (ps->ping.n_replies_expected > 0) {
543 entry->ping_status = PING_WAITING;
544 eventAdd("peerPingTimeout",
545 peerPingTimeout,
546 ps,
547 0.001 * ps->ping.timeout,
548 0);
549 return;
550 }
551 }
552
db1cd23c 553 if (code != HIER_NONE) {
62e76326 554 assert(p);
4b981814 555 debugs(44, 3, "peerSelect: " << hier_code_str[code] << "/" << p->host);
62e76326 556 peerAddFwdServer(&ps->servers, p, code);
db1cd23c 557 }
62e76326 558
db1cd23c 559 entry->ping_status = PING_DONE;
560}
561
562/*
563 * peerGetSomeNeighborReplies
26ac0430 564 *
db1cd23c 565 * Selects a neighbor (parent or sibling) based on ICP/HTCP replies.
566 */
567static void
568peerGetSomeNeighborReplies(ps_state * ps)
569{
190154cf 570 HttpRequest *request = ps->request;
db1cd23c 571 peer *p = NULL;
572 hier_code code = HIER_NONE;
4c5a1592 573 assert(ps->entry->ping_status == PING_WAITING);
db1cd23c 574 assert(ps->direct != DIRECT_YES);
62e76326 575
db1cd23c 576 if (peerCheckNetdbDirect(ps)) {
62e76326 577 code = CLOSEST_DIRECT;
4b981814 578 debugs(44, 3, "peerSelect: " << hier_code_str[code] << "/" << request->GetHost());
62e76326 579 peerAddFwdServer(&ps->servers, NULL, code);
580 return;
db1cd23c 581 }
62e76326 582
db1cd23c 583 if ((p = ps->hit)) {
62e76326 584 code = ps->hit_type == PEER_PARENT ? PARENT_HIT : SIBLING_HIT;
26ac0430 585 } else {
cc192b50 586 if (!ps->closest_parent_miss.IsAnyAddr()) {
587 p = whichPeer(ps->closest_parent_miss);
588 code = CLOSEST_PARENT_MISS;
589 } else if (!ps->first_parent_miss.IsAnyAddr()) {
590 p = whichPeer(ps->first_parent_miss);
591 code = FIRST_PARENT_MISS;
592 }
593 }
db1cd23c 594 if (p && code != HIER_NONE) {
4b981814 595 debugs(44, 3, "peerSelect: " << hier_code_str[code] << "/" << p->host);
62e76326 596 peerAddFwdServer(&ps->servers, p, code);
db1cd23c 597 }
598}
599
600
601/*
602 * peerGetSomeDirect
26ac0430 603 *
db1cd23c 604 * Simply adds a 'direct' entry to the FwdServers list if this
605 * request can be forwarded directly to the origin server
606 */
607static void
608peerGetSomeDirect(ps_state * ps)
609{
610 if (ps->direct == DIRECT_NO)
62e76326 611 return;
612
db80e881 613 /* WAIS is not implemented natively */
0c3d3f65 614 if (ps->request->protocol == AnyP::PROTO_WAIS)
26ac0430 615 return;
db80e881 616
617 peerAddFwdServer(&ps->servers, NULL, HIER_DIRECT);
db1cd23c 618}
619
620static void
621peerGetSomeParent(ps_state * ps)
622{
623 peer *p;
190154cf 624 HttpRequest *request = ps->request;
db1cd23c 625 hier_code code = HIER_NONE;
60745f24 626 debugs(44, 3, "peerGetSomeParent: " << RequestMethodStr(request->method) << " " << request->GetHost());
62e76326 627
6b8e7481 628 if (ps->direct == DIRECT_YES)
62e76326 629 return;
630
b4ab8666
AJ
631 if ((p = peerSourceHashSelectParent(request))) {
632 code = SOURCEHASH_PARENT;
2f1431ea 633#if USE_AUTH
f7e1d9ce
HN
634 } else if ((p = peerUserHashSelectParent(request))) {
635 code = USERHASH_PARENT;
2f1431ea 636#endif
b3995439 637 } else if ((p = carpSelectParent(request))) {
62e76326 638 code = CARP;
db1cd23c 639 } else if ((p = getRoundRobinParent(request))) {
62e76326 640 code = ROUNDROBIN_PARENT;
d1b63fc8 641 } else if ((p = getWeightedRoundRobinParent(request))) {
62e76326 642 code = ROUNDROBIN_PARENT;
db1cd23c 643 } else if ((p = getFirstUpParent(request))) {
62e76326 644 code = FIRSTUP_PARENT;
b4ab8666
AJ
645 } else if ((p = getDefaultParent(request))) {
646 code = DEFAULT_PARENT;
db1cd23c 647 }
62e76326 648
db1cd23c 649 if (code != HIER_NONE) {
4b981814 650 debugs(44, 3, "peerSelect: " << hier_code_str[code] << "/" << p->host);
62e76326 651 peerAddFwdServer(&ps->servers, p, code);
062e2281 652 }
653}
654
168dfda9 655/* Adds alive parents. Used as a last resort for never_direct.
656 */
657static void
658peerGetAllParents(ps_state * ps)
659{
660 peer *p;
190154cf 661 HttpRequest *request = ps->request;
168dfda9 662 /* Add all alive parents */
62e76326 663
168dfda9 664 for (p = Config.peers; p; p = p->next) {
62e76326 665 /* XXX: neighbors.c lacks a public interface for enumerating
666 * parents to a request so we have to dig some here..
667 */
668
669 if (neighborType(p, request) != PEER_PARENT)
670 continue;
671
672 if (!peerHTTPOkay(p, request))
673 continue;
674
bf8fe701 675 debugs(15, 3, "peerGetAllParents: adding alive parent " << p->host);
62e76326 676
677 peerAddFwdServer(&ps->servers, p, ANY_OLD_PARENT);
168dfda9 678 }
62e76326 679
168dfda9 680 /* XXX: should add dead parents here, but it is currently
681 * not possible to find out which parents are dead or which
682 * simply are not configured to handle the request.
683 */
684 /* Add default parent as a last resort */
685 if ((p = getDefaultParent(request))) {
62e76326 686 peerAddFwdServer(&ps->servers, p, DEFAULT_PARENT);
168dfda9 687 }
688}
689
d9586c3c 690static void
93775f90 691peerPingTimeout(void *data)
062e2281 692{
e6ccf245 693 ps_state *psstate = (ps_state *)data;
b6c0e933 694 StoreEntry *entry = psstate->entry;
62e76326 695
fefb0227 696 if (entry)
bf8fe701 697 debugs(44, 3, "peerPingTimeout: '" << entry->url() << "'" );
62e76326 698
fa80a8ef 699 if (!cbdataReferenceValid(psstate->callback_data)) {
62e76326 700 /* request aborted */
701 entry->ping_status = PING_DONE;
702 cbdataReferenceDone(psstate->callback_data);
703 peerSelectStateFree(psstate);
704 return;
73a201f8 705 }
62e76326 706
75e88d56 707 PeerStats.timeouts++;
44e237d0 708 psstate->ping.timedout = 1;
b6c0e933 709 peerSelectFoo(psstate);
85034133 710}
711
712void
713peerSelectInit(void)
714{
75e88d56 715 memset(&PeerStats, '\0', sizeof(PeerStats));
cfd66529 716 memDataInit(MEM_FWD_SERVER, "FwdServer", sizeof(FwdServer), 0);
062e2281 717}
93775f90 718
b3264694 719static void
720peerIcpParentMiss(peer * p, icp_common_t * header, ps_state * ps)
721{
722 int rtt;
62e76326 723
9b5c4a9a 724#if USE_ICMP
b3264694 725 if (Config.onoff.query_icmp) {
62e76326 726 if (header->flags & ICP_FLAG_SRC_RTT) {
727 rtt = header->pad & 0xFFFF;
9b5c4a9a 728 int hops = (header->pad >> 16) & 0xFFFF;
62e76326 729
730 if (rtt > 0 && rtt < 0xFFFF)
731 netdbUpdatePeer(ps->request, p, rtt, hops);
732
733 if (rtt && (ps->ping.p_rtt == 0 || rtt < ps->ping.p_rtt)) {
734 ps->closest_parent_miss = p->in_addr;
735 ps->ping.p_rtt = rtt;
736 }
737 }
b3264694 738 }
9b5c4a9a 739#endif /* USE_ICMP */
62e76326 740
18ec72b2 741 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
cd196bc8 742 if (p->options.closest_only)
62e76326 743 return;
744
85223cd7 745 /* set FIRST_MISS if there is no CLOSEST parent */
cc192b50 746 if (!ps->closest_parent_miss.IsAnyAddr())
62e76326 747 return;
748
d1b63fc8 749 rtt = (tvSubMsec(ps->ping.start, current_time) - p->basetime) / p->weight;
62e76326 750
d1b63fc8 751 if (rtt < 1)
62e76326 752 rtt = 1;
753
cc192b50 754 if (ps->first_parent_miss.IsAnyAddr() || rtt < ps->ping.w_rtt) {
62e76326 755 ps->first_parent_miss = p->in_addr;
756 ps->ping.w_rtt = rtt;
b3264694 757 }
758}
93775f90 759
b6c0e933 760static void
b3264694 761peerHandleIcpReply(peer * p, peer_t type, icp_common_t * header, void *data)
93775f90 762{
e6ccf245 763 ps_state *psstate = (ps_state *)data;
764 icp_opcode op = header->getOpCode();
bf8fe701 765 debugs(44, 3, "peerHandleIcpReply: " << icp_opcode_str[op] << " " << psstate->entry->url() );
69c95dd3 766#if USE_CACHE_DIGESTS && 0
26b164ac 767 /* do cd lookup to count false misses */
62e76326 768
3ab66981 769 if (p && request)
62e76326 770 peerNoteDigestLookup(request, p,
771 peerDigestLookup(p, request, psstate->entry));
772
26b164ac 773#endif
62e76326 774
44e237d0 775 psstate->ping.n_recv++;
62e76326 776
27cd7235 777 if (op == ICP_MISS || op == ICP_DECHO) {
62e76326 778 if (type == PEER_PARENT)
779 peerIcpParentMiss(p, header, psstate);
a7c05555 780 } else if (op == ICP_HIT) {
62e76326 781 psstate->hit = p;
782 psstate->hit_type = type;
783 peerSelectFoo(psstate);
784 return;
db1cd23c 785 }
62e76326 786
44e237d0 787 if (psstate->ping.n_recv < psstate->ping.n_replies_expected)
62e76326 788 return;
789
b6c0e933 790 peerSelectFoo(psstate);
93775f90 791}
86aebcda 792
793#if USE_HTCP
794static void
44e237d0 795peerHandleHtcpReply(peer * p, peer_t type, htcpReplyData * htcp, void *data)
86aebcda 796{
e6ccf245 797 ps_state *psstate = (ps_state *)data;
26ac0430
AJ
798 debugs(44, 3, "peerHandleHtcpReply: " <<
799 (htcp->hit ? "HIT" : "MISS") << " " <<
800 psstate->entry->url() );
44e237d0 801 psstate->ping.n_recv++;
62e76326 802
44e237d0 803 if (htcp->hit) {
62e76326 804 psstate->hit = p;
805 psstate->hit_type = type;
806 peerSelectFoo(psstate);
807 return;
44e237d0 808 }
62e76326 809
44e237d0 810 if (type == PEER_PARENT)
62e76326 811 peerHtcpParentMiss(p, htcp, psstate);
812
44e237d0 813 if (psstate->ping.n_recv < psstate->ping.n_replies_expected)
62e76326 814 return;
815
44e237d0 816 peerSelectFoo(psstate);
817}
818
819static void
820peerHtcpParentMiss(peer * p, htcpReplyData * htcp, ps_state * ps)
821{
822 int rtt;
62e76326 823
9b5c4a9a 824#if USE_ICMP
44e237d0 825 if (Config.onoff.query_icmp) {
62e76326 826 if (htcp->cto.rtt > 0) {
827 rtt = (int) htcp->cto.rtt * 1000;
9b5c4a9a 828 int hops = (int) htcp->cto.hops * 1000;
62e76326 829 netdbUpdatePeer(ps->request, p, rtt, hops);
830
831 if (rtt && (ps->ping.p_rtt == 0 || rtt < ps->ping.p_rtt)) {
832 ps->closest_parent_miss = p->in_addr;
833 ps->ping.p_rtt = rtt;
834 }
835 }
44e237d0 836 }
9b5c4a9a 837#endif /* USE_ICMP */
62e76326 838
44e237d0 839 /* if closest-only is set, then don't allow FIRST_PARENT_MISS */
cd196bc8 840 if (p->options.closest_only)
62e76326 841 return;
842
44e237d0 843 /* set FIRST_MISS if there is no CLOSEST parent */
cc192b50 844 if (!ps->closest_parent_miss.IsAnyAddr())
62e76326 845 return;
846
d1b63fc8 847 rtt = (tvSubMsec(ps->ping.start, current_time) - p->basetime) / p->weight;
62e76326 848
d1b63fc8 849 if (rtt < 1)
62e76326 850 rtt = 1;
851
cc192b50 852 if (ps->first_parent_miss.IsAnyAddr() || rtt < ps->ping.w_rtt) {
62e76326 853 ps->first_parent_miss = p->in_addr;
854 ps->ping.w_rtt = rtt;
44e237d0 855 }
86aebcda 856}
62e76326 857
86aebcda 858#endif
859
860static void
0c3d3f65 861peerHandlePingReply(peer * p, peer_t type, AnyP::ProtocolType proto, void *pingdata, void *data)
86aebcda 862{
0c3d3f65 863 if (proto == AnyP::PROTO_ICP)
62e76326 864 peerHandleIcpReply(p, type, (icp_common_t *)pingdata, data);
865
86aebcda 866#if USE_HTCP
62e76326 867
0c3d3f65 868 else if (proto == AnyP::PROTO_HTCP)
62e76326 869 peerHandleHtcpReply(p, type, (htcpReplyData *)pingdata, data);
870
86aebcda 871#endif
62e76326 872
86aebcda 873 else
0c3d3f65 874 debugs(44, 1, "peerHandlePingReply: unknown protocol " << proto);
86aebcda 875}
db1cd23c 876
877static void
b73f343e 878peerAddFwdServer(FwdServer ** FSVR, peer * p, hier_code code)
db1cd23c 879{
e6ccf245 880 FwdServer *fs = (FwdServer *)memAllocate(MEM_FWD_SERVER);
26ac0430
AJ
881 debugs(44, 5, "peerAddFwdServer: adding " <<
882 (p ? p->host : "DIRECT") << " " <<
4b981814 883 hier_code_str[code] );
29b8d8d6 884 fs->_peer = cbdataReference(p);
db1cd23c 885 fs->code = code;
62e76326 886
b73f343e 887 while (*FSVR)
888 FSVR = &(*FSVR)->next;
62e76326 889
b73f343e 890 *FSVR = fs;
db1cd23c 891}
b24880fe 892
893void *
894ps_state::operator new(size_t)
895{
aa839030 896 CBDATA_INIT_TYPE(ps_state);
b24880fe 897 return cbdataAlloc(ps_state);
898}
899
900ps_state::ps_state() : request (NULL),
901 entry (NULL),
ae026ec6
AJ
902 always_direct(Config.accessList.AlwaysDirect?ACCESS_DUNNO:ACCESS_DENIED),
903 never_direct(Config.accessList.NeverDirect?ACCESS_DUNNO:ACCESS_DENIED),
904 direct(DIRECT_UNKNOWN),
b24880fe 905 callback (NULL),
906 callback_data (NULL),
a37fdd8a 907 lastError(NULL),
b24880fe 908 servers (NULL),
cc192b50 909 first_parent_miss(),
910 closest_parent_miss(),
b24880fe 911 hit(NULL),
912 hit_type(PEER_NONE),
b24880fe 913 acl_checklist (NULL)
914{
cc192b50 915 ; // no local defaults.
b24880fe 916}
917
918ping_data::ping_data() :
919 n_sent(0),
920 n_recv(0),
921 n_replies_expected(0),
922 timeout(0),
923 timedout(0),
924 w_rtt(0),
925 p_rtt(0)
926{
927 start.tv_sec = 0;
928 start.tv_usec = 0;
929 stop.tv_sec = 0;
930 stop.tv_usec = 0;
931}