]> git.ipfire.org Git - thirdparty/squid.git/blame - src/neighbors.cc
Boilerplate: update copyright blurbs on Squid helpers
[thirdparty/squid.git] / src / neighbors.cc
CommitLineData
30a4f2a8 1/*
30a4f2a8 2 * DEBUG: section 15 Neighbor Routines
3 * AUTHOR: Harvest Derived
4 *
2b6662ba 5 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 6 * ----------------------------------------------------------
30a4f2a8 7 *
2b6662ba 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.
30a4f2a8 16 *
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.
26ac0430 21 *
30a4f2a8 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.
26ac0430 26 *
30a4f2a8 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
cbdec147 29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 30 *
019dd986 31 */
090089c4 32
582c2af2 33#include "squid.h"
c0941a6a 34#include "acl/FilledChecklist.h"
65d448bc 35#include "anyp/PortCfg.h"
b814e8d4 36#include "CacheDigest.h"
a011edee 37#include "CachePeer.h"
b802d2cb 38#include "CachePeerDomainList.h"
55cbb02b 39#include "comm/Connection.h"
aed188fd 40#include "comm/ConnOpener.h"
a553a5a3 41#include "event.h"
eb13c21e 42#include "FwdState.h"
582c2af2 43#include "globals.h"
aa839030 44#include "htcp.h"
528b2c61 45#include "HttpRequest.h"
582c2af2 46#include "icmp/net_db.h"
aa839030 47#include "ICP.h"
f9b6ff6e 48#include "int.h"
582c2af2 49#include "ip/Address.h"
b283ea3f 50#include "ip/tools.h"
714e68b7 51#include "ipcache.h"
528b2c61 52#include "MemObject.h"
582c2af2 53#include "mgr/Registration.h"
afabcc13 54#include "multicast.h"
f0ba2534 55#include "neighbors.h"
602d9612 56#include "NeighborTypeDomainList.h"
e8dca475 57#include "pconn.h"
aa839030 58#include "PeerDigest.h"
e8dca475 59#include "PeerPoolMgr.h"
b24880fe 60#include "PeerSelectState.h"
f206b652 61#include "RequestFlags.h"
4d5904f7 62#include "SquidConfig.h"
a98bcbee 63#include "SquidMath.h"
985c86bc 64#include "SquidTime.h"
6ea5959e 65#include "stat.h"
aa839030 66#include "Store.h"
fb548aaf 67#include "store_key_md5.h"
8d03bdb4 68#include "tools.h"
b1bd952a 69#include "URL.h"
090089c4 70
429fdbec 71/* count mcast group peers every 15 minutes */
72#define MCAST_COUNT_RATE 900
73
a3c6762c
FC
74bool peerAllowedToUse(const CachePeer *, HttpRequest *);
75static int peerWouldBePinged(const CachePeer *, HttpRequest *);
76static void neighborRemove(CachePeer *);
77static void neighborAlive(CachePeer *, const MemObject *, const icp_common_t *);
399cabec 78#if USE_HTCP
fad2588a 79static void neighborAliveHtcp(CachePeer *, const MemObject *, const HtcpReplyData *);
399cabec 80#endif
a3c6762c 81static void neighborCountIgnored(CachePeer *);
f5b8bbc4 82static void peerRefreshDNS(void *);
b69f7771 83static IPH peerDNSConfigure;
a3c6762c 84static bool peerProbeConnect(CachePeer *);
eb406bb7 85static CNCB peerProbeConnectDone;
f5b8bbc4 86static void peerCountMcastPeersDone(void *data);
87static void peerCountMcastPeersStart(void *data);
a3c6762c 88static void peerCountMcastPeersSchedule(CachePeer * p, time_t when);
b3264694 89static IRCB peerCountHandleIcpReply;
62e76326 90
b7ac5457 91static void neighborIgnoreNonPeer(const Ip::Address &, icp_opcode);
ed7f5615 92static OBJH neighborDumpPeers;
93static OBJH neighborDumpNonPeers;
a3c6762c 94static void dump_peers(StoreEntry * sentry, CachePeer * peers);
090089c4 95
f45dd259 96static unsigned short echo_port;
30a4f2a8 97
c7a3724d 98static int NLateReplies = 0;
a3c6762c 99static CachePeer *first_ping = NULL;
28070024 100
a2c963ae 101const char *
a3c6762c 102neighborTypeStr(const CachePeer * p)
69a71bd7 103{
e102ebda 104 if (p->type == PEER_NONE)
62e76326 105 return "Non-Peer";
106
b69f7771 107 if (p->type == PEER_SIBLING)
62e76326 108 return "Sibling";
109
b69f7771 110 if (p->type == PEER_MULTICAST)
62e76326 111 return "Multicast Group";
112
69a71bd7 113 return "Parent";
114}
115
a3c6762c 116CachePeer *
b7ac5457 117whichPeer(const Ip::Address &from)
090089c4 118{
22e4fa85 119 int j;
62e76326 120
a3c6762c 121 CachePeer *p = NULL;
cc192b50 122 debugs(15, 3, "whichPeer: from " << from);
62e76326 123
26ac0430 124 for (p = Config.peers; p; p = p->next) {
5db6bf73 125 for (j = 0; j < p->n_addresses; ++j) {
4dd643d5 126 if (from == p->addresses[j] && from.port() == p->icp.port) {
62e76326 127 return p;
128 }
129 }
090089c4 130 }
62e76326 131
4eda6afe 132 return NULL;
090089c4 133}
134
701a8572 135peer_t
a3c6762c 136neighborType(const CachePeer * p, const HttpRequest * request)
24a1003d 137{
62e76326 138
5844d003 139 const NeighborTypeDomainList *d = NULL;
62e76326 140
b69f7771 141 for (d = p->typelist; d; d = d->next) {
cc192b50 142 if (0 == matchDomainName(request->GetHost(), d->domain))
62e76326 143 if (d->type != PEER_NONE)
144 return d->type;
24a1003d 145 }
8a368316
AJ
146#if PEER_MULTICAST_SIBLINGS
147 if (p->type == PEER_MULTICAST)
148 if (p->options.mcast_siblings)
149 return PEER_SIBLING;
150#endif
62e76326 151
b69f7771 152 return p->type;
24a1003d 153}
154
2efeb0b7
AJ
155/**
156 * \return Whether it is appropriate to fetch REQUEST from PEER.
7b3bd12c 157 */
2efeb0b7 158bool
a3c6762c 159peerAllowedToUse(const CachePeer * p, HttpRequest * request)
090089c4 160{
62e76326 161
b802d2cb 162 const CachePeerDomainList *d = NULL;
ce66013b 163 assert(request != NULL);
62e76326 164
9689d97c 165 if (neighborType(p, request) == PEER_SIBLING) {
8a368316
AJ
166#if PEER_MULTICAST_SIBLINGS
167 if (p->type == PEER_MULTICAST && p->options.mcast_siblings &&
450fe1cb 168 (request->flags.noCache || request->flags.refresh || request->flags.loopDetected || request->flags.needValidation))
11992b6f 169 debugs(15, 2, "peerAllowedToUse(" << p->name << ", " << request->GetHost() << ") : multicast-siblings optimization match");
8a368316 170#endif
450fe1cb 171 if (request->flags.noCache)
2efeb0b7 172 return false;
62e76326 173
45e5102d 174 if (request->flags.refresh)
2efeb0b7 175 return false;
62e76326 176
450fe1cb 177 if (request->flags.loopDetected)
2efeb0b7 178 return false;
62e76326 179
450fe1cb 180 if (request->flags.needValidation)
2efeb0b7 181 return false;
9689d97c 182 }
62e76326 183
ad46590b 184 // CONNECT requests are proxy requests. Not to be forwarded to origin servers.
a3c6762c 185 // Unless the destination port matches, in which case we MAY perform a 'DIRECT' to this CachePeer.
4dd643d5 186 if (p->options.originserver && request->method == Http::METHOD_CONNECT && request->port != p->in_addr.port())
2efeb0b7 187 return false;
ad46590b 188
b6a2f15e 189 if (p->peer_domain == NULL && p->access == NULL)
2efeb0b7 190 return true;
62e76326 191
2efeb0b7 192 bool do_ping = false;
b6a2f15e 193 for (d = p->peer_domain; d; d = d->next) {
cc192b50 194 if (0 == matchDomainName(request->GetHost(), d->domain)) {
62e76326 195 do_ping = d->do_ping;
196 break;
197 }
198
199 do_ping = !d->do_ping;
30a4f2a8 200 }
62e76326 201
2efeb0b7
AJ
202 if (p->peer_domain && !do_ping)
203 return false;
62e76326 204
505e35db 205 if (p->access == NULL)
62e76326 206 return do_ping;
207
c0941a6a 208 ACLFilledChecklist checklist(p->access, request, NULL);
62e76326 209
2efeb0b7 210 return (checklist.fastCheck() == ACCESS_ALLOWED);
090089c4 211}
212
a3c6762c 213/* Return TRUE if it is okay to send an ICP request to this CachePeer. */
62fd6124 214static int
a3c6762c 215peerWouldBePinged(const CachePeer * p, HttpRequest * request)
62fd6124 216{
6315602c 217 if (p->icp.port == 0)
62e76326 218 return 0;
219
cd196bc8 220 if (p->options.no_query)
62e76326 221 return 0;
222
cd196bc8 223 if (p->options.mcast_responder)
62e76326 224 return 0;
225
83b381d5 226 if (p->n_addresses == 0)
62e76326 227 return 0;
228
6315602c 229 if (p->options.background_ping && (squid_curtime - p->stats.last_query < Config.backgroundPingRate))
62e76326 230 return 0;
231
62fd6124 232 /* the case below seems strange, but can happen if the
233 * URL host is on the other side of a firewall */
b69f7771 234 if (p->type == PEER_SIBLING)
45e5102d 235 if (!request->flags.hierarchical)
62e76326 236 return 0;
237
6315602c
JGD
238 if (!peerAllowedToUse(p, request))
239 return 0;
240
83b381d5 241 /* Ping dead peers every timeout interval */
242 if (squid_curtime - p->stats.last_query > Config.Timeout.deadPeer)
62e76326 243 return 1;
244
5537c535 245 if (!neighborUp(p))
246 return 0;
62e76326 247
62fd6124 248 return 1;
249}
250
e8dca475
CT
251bool
252peerCanOpenMore(const CachePeer *p)
253{
254 const int effectiveLimit = p->max_conn <= 0 ? Squid_MaxFD : p->max_conn;
255 const int remaining = effectiveLimit - p->stats.conn_open;
256 debugs(15, 7, remaining << '=' << effectiveLimit << '-' << p->stats.conn_open);
257 return remaining > 0;
258}
259
260bool
261peerHasConnAvailable(const CachePeer *p)
262{
263 // Standby connections can be used without opening new connections.
264 const int standbys = p->standby.pool ? p->standby.pool->count() : 0;
265
266 // XXX: Some idle pconns can be used without opening new connections.
267 // Complication: Idle pconns cannot be reused for some requests.
268 const int usableIdles = 0;
269
270 const int available = standbys + usableIdles;
271 debugs(15, 7, available << '=' << standbys << '+' << usableIdles);
272 return available > 0;
273}
274
275void
276peerConnClosed(CachePeer *p)
277{
278 --p->stats.conn_open;
279 if (p->standby.waitingForClose && peerCanOpenMore(p)) {
280 p->standby.waitingForClose = false;
281 PeerPoolMgr::Checkpoint(p->standby.mgr, "conn closed");
282 }
283}
284
a3c6762c 285/* Return TRUE if it is okay to send an HTTP request to this CachePeer. */
8ff4505b 286int
a3c6762c 287peerHTTPOkay(const CachePeer * p, HttpRequest * request)
62fd6124 288{
e8dca475
CT
289 if (!peerCanOpenMore(p) && !peerHasConnAvailable(p))
290 return 0;
6315602c 291
b69f7771 292 if (!peerAllowedToUse(p, request))
62e76326 293 return 0;
294
b69f7771 295 if (!neighborUp(p))
62e76326 296 return 0;
297
62fd6124 298 return 1;
299}
300
3c6e634f 301int
190154cf 302neighborsCount(HttpRequest * request)
090089c4 303{
a3c6762c 304 CachePeer *p = NULL;
090089c4 305 int count = 0;
62e76326 306
40a1495e 307 for (p = Config.peers; p; p = p->next)
62e76326 308 if (peerWouldBePinged(p, request))
5db6bf73 309 ++count;
62e76326 310
bf8fe701 311 debugs(15, 3, "neighborsCount: " << count);
62e76326 312
3c6e634f 313 return count;
314}
090089c4 315
a3c6762c 316CachePeer *
190154cf 317getFirstUpParent(HttpRequest * request)
090089c4 318{
a3c6762c 319 CachePeer *p = NULL;
62e76326 320
40a1495e 321 for (p = Config.peers; p; p = p->next) {
62e76326 322 if (!neighborUp(p))
323 continue;
324
325 if (neighborType(p, request) != PEER_PARENT)
326 continue;
327
328 if (!peerHTTPOkay(p, request))
329 continue;
330
331 break;
090089c4 332 }
62e76326 333
bf8fe701 334 debugs(15, 3, "getFirstUpParent: returning " << (p ? p->host : "NULL"));
b69f7771 335 return p;
090089c4 336}
337
a3c6762c 338CachePeer *
190154cf 339getRoundRobinParent(HttpRequest * request)
48b38d01 340{
a3c6762c
FC
341 CachePeer *p;
342 CachePeer *q = NULL;
62e76326 343
40a1495e 344 for (p = Config.peers; p; p = p->next) {
62e76326 345 if (!p->options.roundrobin)
346 continue;
347
348 if (neighborType(p, request) != PEER_PARENT)
349 continue;
350
351 if (!peerHTTPOkay(p, request))
352 continue;
353
1d0bef67 354 if (p->weight == 0)
0d5a2006 355 continue;
1d0bef67
AJ
356
357 if (q) {
358 if (p->weight == q->weight) {
359 if (q->rr_count < p->rr_count)
360 continue;
2d3d0b4e 361 } else if ( ((double) q->rr_count / q->weight) < ((double) p->rr_count / p->weight)) {
1d0bef67
AJ
362 continue;
363 }
0d5a2006 364 }
62e76326 365
366 q = p;
48b38d01 367 }
62e76326 368
b69f7771 369 if (q)
5db6bf73 370 ++ q->rr_count;
62e76326 371
1d0bef67 372 debugs(15, 3, HERE << "returning " << (q ? q->host : "NULL"));
62e76326 373
b69f7771 374 return q;
48b38d01 375}
376
a3c6762c 377CachePeer *
190154cf 378getWeightedRoundRobinParent(HttpRequest * request)
d1b63fc8 379{
a3c6762c
FC
380 CachePeer *p;
381 CachePeer *q = NULL;
d1b63fc8 382 int weighted_rtt;
62e76326 383
d1b63fc8 384 for (p = Config.peers; p; p = p->next) {
62e76326 385 if (!p->options.weighted_roundrobin)
386 continue;
387
388 if (neighborType(p, request) != PEER_PARENT)
389 continue;
390
391 if (!peerHTTPOkay(p, request))
392 continue;
393
394 if (q && q->rr_count < p->rr_count)
395 continue;
396
397 q = p;
d1b63fc8 398 }
62e76326 399
d1b63fc8 400 if (q && q->rr_count > 1000000)
62e76326 401 for (p = Config.peers; p; p = p->next) {
402 if (!p->options.weighted_roundrobin)
403 continue;
404
405 if (neighborType(p, request) != PEER_PARENT)
406 continue;
407
408 p->rr_count = 0;
409 }
410
d1b63fc8 411 if (q) {
62e76326 412 weighted_rtt = (q->stats.rtt - q->basetime) / q->weight;
d1b63fc8 413
62e76326 414 if (weighted_rtt < 1)
415 weighted_rtt = 1;
416
417 q->rr_count += weighted_rtt;
418
4a7a3d56 419 debugs(15, 3, "getWeightedRoundRobinParent: weighted_rtt " << weighted_rtt);
d1b63fc8 420 }
62e76326 421
bf8fe701 422 debugs(15, 3, "getWeightedRoundRobinParent: returning " << (q ? q->host : "NULL"));
d1b63fc8 423 return q;
424}
425
32a47e3e
AJ
426/**
427 * This gets called every 5 minutes to clear the round-robin counter.
428 * The exact timing is an arbitrary default, set on estimate timing of a
429 * large number of requests in a high-performance environment during the
430 * period. The larger the number of requests between cycled resets the
431 * more balanced the operations.
432 *
433 \param data unused.
434 \todo Make the reset timing a selectable parameter in squid.conf
435 */
26ac0430 436static void
32a47e3e
AJ
437peerClearRRLoop(void *data)
438{
439 peerClearRR();
440 eventAdd("peerClearRR", peerClearRRLoop, data, 5 * 60.0, 0);
441}
442
443/**
a3c6762c 444 * This gets called on startup and restart to kick off the CachePeer round-robin
32a47e3e
AJ
445 * maintenance event. It ensures that no matter how many times its called
446 * no more than one event is scheduled.
447 */
82056f1e 448void
32a47e3e 449peerClearRRStart(void)
82056f1e 450{
7a3f0061 451 static bool event_added = false;
32a47e3e
AJ
452 if (!event_added) {
453 peerClearRRLoop(NULL);
7a3f0061 454 event_added=true;
32a47e3e
AJ
455 }
456}
62e76326 457
32a47e3e
AJ
458/**
459 * Called whenever the round-robin counters need to be reset to a sane state.
460 * So far those times are:
d85b8894 461 * - On startup and reconfigure - to set the counters to sane initial settings.
a3c6762c 462 * - When a CachePeer has revived from dead, to prevent the revived CachePeer being
32a47e3e
AJ
463 * flooded with requests which it has 'missed' during the down period.
464 */
465void
466peerClearRR()
467{
a3c6762c 468 CachePeer *p = NULL;
32a47e3e 469 for (p = Config.peers; p; p = p->next) {
2da06da0 470 p->rr_count = 1;
32a47e3e
AJ
471 }
472}
62e76326 473
32a47e3e 474/**
a3c6762c 475 * Perform all actions when a CachePeer is detected revived.
32a47e3e
AJ
476 */
477void
a3c6762c 478peerAlive(CachePeer *p)
32a47e3e
AJ
479{
480 if (p->stats.logged_state == PEER_DEAD && p->tcp_up) {
e0236918 481 debugs(15, DBG_IMPORTANT, "Detected REVIVED " << neighborTypeStr(p) << ": " << p->name);
32a47e3e
AJ
482 p->stats.logged_state = PEER_ALIVE;
483 peerClearRR();
e8dca475
CT
484 if (p->standby.mgr.valid())
485 PeerPoolMgr::Checkpoint(p->standby.mgr, "revived peer");
32a47e3e 486 }
62e76326 487
32a47e3e
AJ
488 p->stats.last_reply = squid_curtime;
489 p->stats.probe_start = 0;
82056f1e 490}
491
a3c6762c 492CachePeer *
190154cf 493getDefaultParent(HttpRequest * request)
5269d0bd 494{
a3c6762c 495 CachePeer *p = NULL;
62e76326 496
40a1495e 497 for (p = Config.peers; p; p = p->next) {
62e76326 498 if (neighborType(p, request) != PEER_PARENT)
499 continue;
500
501 if (!p->options.default_parent)
502 continue;
503
504 if (!peerHTTPOkay(p, request))
505 continue;
506
bf8fe701 507 debugs(15, 3, "getDefaultParent: returning " << p->host);
62e76326 508
509 return p;
5269d0bd 510 }
62e76326 511
bf8fe701 512 debugs(15, 3, "getDefaultParent: returning NULL");
5269d0bd 513 return NULL;
514}
515
a3c6762c
FC
516CachePeer *
517getNextPeer(CachePeer * p)
090089c4 518{
b69f7771 519 return p->next;
090089c4 520}
521
a3c6762c 522CachePeer *
deb79f06 523getFirstPeer(void)
090089c4 524{
40a1495e 525 return Config.peers;
090089c4 526}
527
b8d8561b 528static void
a3c6762c 529neighborRemove(CachePeer * target)
30a4f2a8 530{
a3c6762c
FC
531 CachePeer *p = NULL;
532 CachePeer **P = NULL;
40a1495e 533 p = Config.peers;
534 P = &Config.peers;
62e76326 535
b69f7771 536 while (p) {
62e76326 537 if (target == p)
538 break;
539
540 P = &p->next;
541
542 p = p->next;
b69f7771 543 }
62e76326 544
b69f7771 545 if (p) {
62e76326 546 *P = p->next;
547 cbdataFree(p);
5e263176 548 --Config.npeers;
4d64d74a 549 }
62e76326 550
40a1495e 551 first_ping = Config.peers;
4d64d74a 552}
553
5f5e883f
FC
554static void
555neighborsRegisterWithCacheManager()
556{
8822ebee 557 Mgr::RegisterAction("server_list",
d9fc6862
A
558 "Peer Cache Statistics",
559 neighborDumpPeers, 0, 1);
5f5e883f 560
e0d28505 561 if (Comm::IsConnOpen(icpIncomingConn)) {
8822ebee 562 Mgr::RegisterAction("non_peers",
d9fc6862
A
563 "List of Unknown sites sending ICP messages",
564 neighborDumpNonPeers, 0, 1);
5f5e883f
FC
565 }
566}
567
b8d8561b 568void
a86029b9 569neighbors_init(void)
090089c4 570{
30a4f2a8 571 struct servent *sep = NULL;
b263bd3f 572 const char *me = getMyHostname();
a3c6762c
FC
573 CachePeer *thisPeer = NULL;
574 CachePeer *next = NULL;
62e76326 575
6fdc2d18
FC
576 neighborsRegisterWithCacheManager();
577
e0d28505 578 if (Comm::IsConnOpen(icpIncomingConn)) {
62e76326 579
580 for (thisPeer = Config.peers; thisPeer; thisPeer = next) {
62e76326 581 next = thisPeer->next;
582
583 if (0 != strcmp(thisPeer->host, me))
584 continue;
585
fa720bfb 586 for (AnyP::PortCfgPointer s = HttpPortList; s != NULL; s = s->next) {
4dd643d5 587 if (thisPeer->http_port != s->s.port())
62e76326 588 continue;
589
27774cee 590 debugs(15, DBG_IMPORTANT, "WARNING: Peer looks like this host");
bf8fe701 591
27774cee 592 debugs(15, DBG_IMPORTANT, " Ignoring " <<
bf8fe701 593 neighborTypeStr(thisPeer) << " " << thisPeer->host <<
594 "/" << thisPeer->http_port << "/" <<
595 thisPeer->icp.port);
62e76326 596
62e76326 597 neighborRemove(thisPeer);
598 }
599 }
b263bd3f 600 }
62e76326 601
5999b776 602 peerRefreshDNS((void *) 1);
62e76326 603
9ca56f05 604 sep = getservbyname("echo", "udp");
f45dd259 605 echo_port = sep ? ntohs((unsigned short) sep->s_port) : 7;
62e76326 606
c68e4e90 607 first_ping = Config.peers;
62ee09ca 608}
609
b8d8561b 610int
190154cf 611neighborsUdpPing(HttpRequest * request,
62e76326 612 StoreEntry * entry,
613 IRCB * callback,
614 void *callback_data,
615 int *exprep,
616 int *timeout)
090089c4 617{
3900307b 618 const char *url = entry->url();
b6c0e933 619 MemObject *mem = entry->mem_obj;
a3c6762c 620 CachePeer *p = NULL;
090089c4 621 int i;
1061b406 622 int reqnum = 0;
6d2296d4 623 int flags;
9dee2904 624 icp_common_t *query;
429fdbec 625 int queries_sent = 0;
0a0bf5db 626 int peers_pinged = 0;
a8c926ff 627 int parent_timeout = 0, parent_exprep = 0;
628 int sibling_timeout = 0, sibling_exprep = 0;
1bf2cd49 629 int mcast_timeout = 0, mcast_exprep = 0;
090089c4 630
40a1495e 631 if (Config.peers == NULL)
62e76326 632 return 0;
633
9fb13bb6 634 assert(entry->swap_status == SWAPOUT_NONE);
62e76326 635
b6c0e933 636 mem->start_ping = current_time;
62e76326 637
b4e7f82d 638 mem->ping_reply_callback = callback;
62e76326 639
b6c0e933 640 mem->ircb_data = callback_data;
62e76326 641
332dafa2 642 reqnum = icpSetCacheKey((const cache_key *)entry->key);
62e76326 643
40a1495e 644 for (i = 0, p = first_ping; i++ < Config.npeers; p = p->next) {
62e76326 645 if (p == NULL)
646 p = Config.peers;
647
bf8fe701 648 debugs(15, 5, "neighborsUdpPing: Peer " << p->host);
62e76326 649
650 if (!peerWouldBePinged(p, request))
a3c6762c 651 continue; /* next CachePeer */
62e76326 652
5db6bf73 653 ++peers_pinged;
62e76326 654
bf8fe701 655 debugs(15, 4, "neighborsUdpPing: pinging peer " << p->host << " for '" << url << "'");
62e76326 656
bf8fe701 657 debugs(15, 3, "neighborsUdpPing: key = '" << entry->getMD5Text() << "'");
62e76326 658
bf8fe701 659 debugs(15, 3, "neighborsUdpPing: reqnum = " << reqnum);
090089c4 660
dc9d133b 661#if USE_HTCP
4f4fa815 662 if (p->options.htcp && !p->options.htcp_only_clr) {
bebf08ff
AJ
663 if (Config.Port.htcp <= 0) {
664 debugs(15, DBG_CRITICAL, "HTCP is disabled! Cannot send HTCP request to peer.");
665 continue;
666 }
667
bf8fe701 668 debugs(15, 3, "neighborsUdpPing: sending HTCP query");
65d448bc
AJ
669 if (htcpQuery(entry, request, p) <= 0)
670 continue; // unable to send.
62e76326 671 } else
dc9d133b 672#endif
bebf08ff 673 {
e0d28505 674 if (Config.Port.icp <= 0 || !Comm::IsConnOpen(icpOutgoingConn)) {
bebf08ff
AJ
675 debugs(15, DBG_CRITICAL, "ICP is disabled! Cannot send ICP request to peer.");
676 continue;
62e76326 677 } else {
62e76326 678
bebf08ff 679 if (p->type == PEER_MULTICAST)
e0d28505 680 mcastSetTtl(icpOutgoingConn->fd, p->mcast.ttl);
bebf08ff
AJ
681
682 if (p->icp.port == echo_port) {
683 debugs(15, 4, "neighborsUdpPing: Looks like a dumb cache, send DECHO ping");
bebf08ff 684 query = _icp_common_t::createMessage(ICP_DECHO, 0, url, reqnum, 0);
e0d28505 685 icpUdpSend(icpOutgoingConn->fd, p->in_addr, query, LOG_ICP_QUERY, 0);
bebf08ff
AJ
686 } else {
687 flags = 0;
62e76326 688
bebf08ff
AJ
689 if (Config.onoff.query_icmp)
690 if (p->icp.version == ICP_VERSION_2)
691 flags |= ICP_FLAG_SRC_RTT;
62e76326 692
bebf08ff
AJ
693 query = _icp_common_t::createMessage(ICP_QUERY, flags, url, reqnum, 0);
694
e0d28505 695 icpUdpSend(icpOutgoingConn->fd, p->in_addr, query, LOG_ICP_QUERY, 0);
bebf08ff 696 }
62e76326 697 }
bebf08ff 698 }
62e76326 699
5db6bf73 700 ++queries_sent;
62e76326 701
5db6bf73 702 ++ p->stats.pings_sent;
62e76326 703
704 if (p->type == PEER_MULTICAST) {
1bf2cd49 705 mcast_exprep += p->mcast.n_replies_expected;
706 mcast_timeout += (p->stats.rtt * p->mcast.n_replies_expected);
62e76326 707 } else if (neighborUp(p)) {
708 /* its alive, expect a reply from it */
709
710 if (neighborType(p, request) == PEER_PARENT) {
5db6bf73 711 ++parent_exprep;
62e76326 712 parent_timeout += p->stats.rtt;
713 } else {
5db6bf73 714 ++sibling_exprep;
62e76326 715 sibling_timeout += p->stats.rtt;
716 }
717 } else {
718 /* Neighbor is dead; ping it anyway, but don't expect a reply */
719 /* log it once at the threshold */
720
721 if (p->stats.logged_state == PEER_ALIVE) {
e0236918 722 debugs(15, DBG_IMPORTANT, "Detected DEAD " << neighborTypeStr(p) << ": " << p->name);
62e76326 723 p->stats.logged_state = PEER_DEAD;
724 }
725 }
726
727 p->stats.last_query = squid_curtime;
728
67c8308c 729 /*
a3c6762c
FC
730 * keep probe_start == 0 for a multicast CachePeer,
731 * so neighborUp() never says this CachePeer is dead.
67c8308c 732 */
733
734 if ((p->type != PEER_MULTICAST) && (p->stats.probe_start == 0))
62e76326 735 p->stats.probe_start = squid_curtime;
090089c4 736 }
62e76326 737
40a1495e 738 if ((first_ping = first_ping->next) == NULL)
62e76326 739 first_ping = Config.peers;
090089c4 740
a8c926ff 741 /*
742 * How many replies to expect?
743 */
1bf2cd49 744 *exprep = parent_exprep + sibling_exprep + mcast_exprep;
a8c926ff 745
52040193 746 /*
465dc415 747 * If there is a configured timeout, use it
52040193 748 */
465dc415 749 if (Config.Timeout.icp_query)
62e76326 750 *timeout = Config.Timeout.icp_query;
28993292 751 else {
62e76326 752 if (*exprep > 0) {
753 if (parent_exprep)
754 *timeout = 2 * parent_timeout / parent_exprep;
1bf2cd49 755 else if (mcast_exprep)
756 *timeout = 2 * mcast_timeout / mcast_exprep;
62e76326 757 else
758 *timeout = 2 * sibling_timeout / sibling_exprep;
759 } else
760 *timeout = 2000; /* 2 seconds */
761
762 if (Config.Timeout.icp_query_max)
763 if (*timeout > Config.Timeout.icp_query_max)
764 *timeout = Config.Timeout.icp_query_max;
765
766 if (*timeout < Config.Timeout.icp_query_min)
767 *timeout = Config.Timeout.icp_query_min;
28993292 768 }
62e76326 769
0a0bf5db 770 return peers_pinged;
090089c4 771}
772
a3c6762c 773/* lookup the digest of a given CachePeer */
26b164ac 774lookup_t
a3c6762c 775peerDigestLookup(CachePeer * p, HttpRequest * request)
26b164ac 776{
6cfa8966 777#if USE_CACHE_DIGESTS
f66a9ef4 778 const cache_key *key = request ? storeKeyPublicByRequest(request) : NULL;
26b164ac 779 assert(p);
0c511722 780 assert(request);
bf8fe701 781 debugs(15, 5, "peerDigestLookup: peer " << p->host);
4b4cd312 782 /* does the peeer have a valid digest? */
62e76326 783
e13ee7ad 784 if (!p->digest) {
bf8fe701 785 debugs(15, 5, "peerDigestLookup: gone!");
62e76326 786 return LOOKUP_NONE;
6755df2e 787 } else if (!peerHTTPOkay(p, request)) {
bf8fe701 788 debugs(15, 5, "peerDigestLookup: !peerHTTPOkay");
6755df2e 789 return LOOKUP_NONE;
e13ee7ad 790 } else if (!p->digest->flags.needed) {
bf8fe701 791 debugs(15, 5, "peerDigestLookup: note need");
62e76326 792 peerDigestNeeded(p->digest);
793 return LOOKUP_NONE;
4ed0e075 794 } else if (!p->digest->flags.usable) {
bf8fe701 795 debugs(15, 5, "peerDigestLookup: !ready && " << (p->digest->flags.requested ? "" : "!") << "requested");
62e76326 796 return LOOKUP_NONE;
26b164ac 797 }
62e76326 798
bf8fe701 799 debugs(15, 5, "peerDigestLookup: OK to lookup peer " << p->host);
e13ee7ad 800 assert(p->digest->cd);
26b164ac 801 /* does digest predict a hit? */
62e76326 802
e13ee7ad 803 if (!cacheDigestTest(p->digest->cd, key))
62e76326 804 return LOOKUP_MISS;
805
bf8fe701 806 debugs(15, 5, "peerDigestLookup: peer " << p->host << " says HIT!");
62e76326 807
26b164ac 808 return LOOKUP_HIT;
62e76326 809
26b164ac 810#endif
62e76326 811
26b164ac 812 return LOOKUP_NONE;
813}
814
a3c6762c
FC
815/* select best CachePeer based on cache digests */
816CachePeer *
190154cf 817neighborsDigestSelect(HttpRequest * request)
a54d3f8e 818{
a3c6762c 819 CachePeer *best_p = NULL;
6cfa8966 820#if USE_CACHE_DIGESTS
62e76326 821
a54d3f8e 822 int best_rtt = 0;
823 int choice_count = 0;
824 int ichoice_count = 0;
a3c6762c 825 CachePeer *p;
a54d3f8e 826 int p_rtt;
827 int i;
62e76326 828
45e5102d 829 if (!request->flags.hierarchical)
62e76326 830 return NULL;
831
e91e2a72 832 storeKeyPublicByRequest(request);
62e76326 833
a54d3f8e 834 for (i = 0, p = first_ping; i++ < Config.npeers; p = p->next) {
62e76326 835 lookup_t lookup;
836
837 if (!p)
838 p = Config.peers;
839
840 if (i == 1)
841 first_ping = p;
842
843 lookup = peerDigestLookup(p, request);
844
845 if (lookup == LOOKUP_NONE)
846 continue;
847
5db6bf73 848 ++choice_count;
62e76326 849
850 if (lookup == LOOKUP_MISS)
851 continue;
852
853 p_rtt = netdbHostRtt(p->host);
854
bf8fe701 855 debugs(15, 5, "neighborsDigestSelect: peer " << p->host << " rtt: " << p_rtt);
62e76326 856
a3c6762c 857 /* is this CachePeer better than others in terms of rtt ? */
62e76326 858 if (!best_p || (p_rtt && p_rtt < best_rtt)) {
859 best_p = p;
860 best_rtt = p_rtt;
861
862 if (p_rtt) /* informative choice (aka educated guess) */
5db6bf73 863 ++ichoice_count;
62e76326 864
bf8fe701 865 debugs(15, 4, "neighborsDigestSelect: peer " << p->host << " leads with rtt " << best_rtt);
62e76326 866 }
a54d3f8e 867 }
62e76326 868
bf8fe701 869 debugs(15, 4, "neighborsDigestSelect: choices: " << choice_count << " (" << ichoice_count << ")");
26b164ac 870 peerNoteDigestLookup(request, best_p,
62e76326 871 best_p ? LOOKUP_HIT : (choice_count ? LOOKUP_MISS : LOOKUP_NONE));
a54d3f8e 872 request->hier.n_choices = choice_count;
873 request->hier.n_ichoices = ichoice_count;
874#endif
62e76326 875
a54d3f8e 876 return best_p;
877}
878
26b164ac 879void
a3c6762c 880peerNoteDigestLookup(HttpRequest * request, CachePeer * p, lookup_t lookup)
26b164ac 881{
6cfa8966 882#if USE_CACHE_DIGESTS
26b164ac 883 if (p)
0a84e4fb 884 strncpy(request->hier.cd_host, p->host, sizeof(request->hier.cd_host)-1);
26b164ac 885 else
62e76326 886 *request->hier.cd_host = '\0';
887
26b164ac 888 request->hier.cd_lookup = lookup;
4b981814 889 debugs(15, 4, "peerNoteDigestLookup: peer " << (p? p->host : "<none>") << ", lookup: " << lookup_t_str[lookup] );
26b164ac 890#endif
891}
892
d73844ca 893static void
a3c6762c 894neighborAlive(CachePeer * p, const MemObject * mem, const icp_common_t * header)
4eda6afe 895{
32a47e3e 896 peerAlive(p);
5db6bf73 897 ++ p->stats.pings_acked;
62e76326 898
27cd7235 899 if ((icp_opcode) header->opcode <= ICP_END)
5db6bf73 900 ++ p->icp.counts[header->opcode];
62e76326 901
44f2f2e4 902 p->icp.version = (int) header->version;
903}
904
905static void
a3c6762c 906neighborUpdateRtt(CachePeer * p, MemObject * mem)
44f2f2e4 907{
d1b63fc8 908 int rtt, rtt_av_factor;
62e76326 909
d2cd6935 910 if (!mem)
62e76326 911 return;
912
44f2f2e4 913 if (!mem->start_ping.tv_sec)
62e76326 914 return;
915
44f2f2e4 916 rtt = tvSubMsec(mem->start_ping, current_time);
62e76326 917
1b6ef2d2 918 if (rtt < 1 || rtt > 10000)
62e76326 919 return;
920
d1b63fc8 921 rtt_av_factor = RTT_AV_FACTOR;
62e76326 922
d1b63fc8 923 if (p->options.weighted_roundrobin)
62e76326 924 rtt_av_factor = RTT_BACKGROUND_AV_FACTOR;
925
a98bcbee 926 p->stats.rtt = Math::intAverage(p->stats.rtt, rtt, p->stats.pings_acked, rtt_av_factor);
4eda6afe 927}
090089c4 928
399cabec 929#if USE_HTCP
930static void
fad2588a 931neighborAliveHtcp(CachePeer * p, const MemObject * mem, const HtcpReplyData * htcp)
399cabec 932{
32a47e3e 933 peerAlive(p);
5db6bf73
FC
934 ++ p->stats.pings_acked;
935 ++ p->htcp.counts[htcp->hit ? 1 : 0];
44f2f2e4 936 p->htcp.version = htcp->version;
399cabec 937}
62e76326 938
399cabec 939#endif
940
38792624 941static void
a3c6762c 942neighborCountIgnored(CachePeer * p)
a7e59001 943{
b69f7771 944 if (p == NULL)
62e76326 945 return;
946
5db6bf73 947 ++ p->stats.ignored_replies;
62e76326 948
5db6bf73 949 ++NLateReplies;
a7e59001 950}
951
a3c6762c 952static CachePeer *non_peers = NULL;
e102ebda 953
954static void
b7ac5457 955neighborIgnoreNonPeer(const Ip::Address &from, icp_opcode opcode)
e102ebda 956{
a3c6762c 957 CachePeer *np;
62e76326 958
26ac0430 959 for (np = non_peers; np; np = np->next) {
cc192b50 960 if (np->in_addr != from)
62e76326 961 continue;
962
4dd643d5 963 if (np->in_addr.port() != from.port())
62e76326 964 continue;
965
966 break;
e102ebda 967 }
62e76326 968
26ac0430 969 if (np == NULL) {
a3c6762c 970 np = (CachePeer *)xcalloc(1, sizeof(CachePeer));
cc192b50 971 np->in_addr = from;
4dd643d5 972 np->icp.port = from.port();
62e76326 973 np->type = PEER_NONE;
cc192b50 974 np->host = new char[MAX_IPSTRLEN];
4dd643d5 975 from.toStr(np->host,MAX_IPSTRLEN);
62e76326 976 np->next = non_peers;
977 non_peers = np;
e102ebda 978 }
62e76326 979
5db6bf73 980 ++ np->icp.counts[opcode];
62e76326 981
e4cc2fdf 982 if (isPowTen(++np->stats.ignored_replies))
e0236918 983 debugs(15, DBG_IMPORTANT, "WARNING: Ignored " << np->stats.ignored_replies << " replies from non-peer " << np->host);
e102ebda 984}
985
429fdbec 986/* ignoreMulticastReply
26ac0430 987 *
d6827718 988 * * We want to ignore replies from multicast peers if the
a3c6762c 989 * * cache_host_domain rules would normally prevent the CachePeer
d6827718 990 * * from being used
429fdbec 991 */
992static int
a3c6762c 993ignoreMulticastReply(CachePeer * p, MemObject * mem)
429fdbec 994{
b69f7771 995 if (p == NULL)
62e76326 996 return 0;
997
cd196bc8 998 if (!p->options.mcast_responder)
62e76326 999 return 0;
1000
b69f7771 1001 if (peerHTTPOkay(p, mem->request))
62e76326 1002 return 0;
1003
429fdbec 1004 return 1;
1005}
1006
4b981814
AJ
1007/**
1008 * I should attach these records to the entry. We take the first
429fdbec 1009 * hit we get our wait until everyone misses. The timeout handler
1010 * call needs to nip this shopping list or call one of the misses.
26ac0430 1011 *
429fdbec 1012 * If a hit process is already started, then sobeit
1013 */
b8d8561b 1014void
b7ac5457 1015neighborsUdpAck(const cache_key * key, icp_common_t * header, const Ip::Address &from)
090089c4 1016{
a3c6762c 1017 CachePeer *p = NULL;
5ad33356 1018 StoreEntry *entry;
1019 MemObject *mem = NULL;
b6c0e933 1020 peer_t ntype = PEER_NONE;
a7e59001 1021 icp_opcode opcode = (icp_opcode) header->opcode;
090089c4 1022
4a7a3d56 1023 debugs(15, 6, "neighborsUdpAck: opcode " << opcode << " '" << storeKeyText(key) << "'");
62e76326 1024
c8f4eac4 1025 if (NULL != (entry = Store::Root().get(key)))
62e76326 1026 mem = entry->mem_obj;
1027
b69f7771 1028 if ((p = whichPeer(from)))
62e76326 1029 neighborAlive(p, mem, header);
1030
27cd7235 1031 if (opcode > ICP_END)
62e76326 1032 return;
1033
4b981814 1034 const char *opcode_d = icp_opcode_str[opcode];
62e76326 1035
41587298 1036 if (p)
62e76326 1037 neighborUpdateRtt(p, mem);
1038
5ad33356 1039 /* Does the entry exist? */
26ac0430 1040 if (NULL == entry) {
bf8fe701 1041 debugs(12, 3, "neighborsUdpAck: Cache key '" << storeKeyText(key) << "' not found");
62e76326 1042 neighborCountIgnored(p);
1043 return;
5ad33356 1044 }
62e76326 1045
2d1c6a4f 1046 /* check if someone is already fetching it */
26ac0430 1047 if (EBIT_TEST(entry->flags, ENTRY_DISPATCHED)) {
bf8fe701 1048 debugs(15, 3, "neighborsUdpAck: '" << storeKeyText(key) << "' already being fetched.");
62e76326 1049 neighborCountIgnored(p);
1050 return;
d2af9477 1051 }
62e76326 1052
26ac0430 1053 if (mem == NULL) {
bf8fe701 1054 debugs(15, 2, "Ignoring " << opcode_d << " for missing mem_obj: " << storeKeyText(key));
62e76326 1055 neighborCountIgnored(p);
1056 return;
8de2f7ad 1057 }
62e76326 1058
26ac0430 1059 if (entry->ping_status != PING_WAITING) {
bf8fe701 1060 debugs(15, 2, "neighborsUdpAck: Late " << opcode_d << " for " << storeKeyText(key));
62e76326 1061 neighborCountIgnored(p);
1062 return;
8de2f7ad 1063 }
62e76326 1064
1bfe9ade 1065 if (!entry->locked()) {
5bd484b5 1066 // TODO: many entries are unlocked; why is this reported at level 1?
e0236918 1067 debugs(12, DBG_IMPORTANT, "neighborsUdpAck: '" << storeKeyText(key) << "' has no locks");
62e76326 1068 neighborCountIgnored(p);
1069 return;
090089c4 1070 }
62e76326 1071
bf8fe701 1072 debugs(15, 3, "neighborsUdpAck: " << opcode_d << " for '" << storeKeyText(key) << "' from " << (p ? p->host : "source") << " ");
62e76326 1073
26ac0430 1074 if (p) {
62e76326 1075 ntype = neighborType(p, mem->request);
44f2f2e4 1076 }
62e76326 1077
26ac0430 1078 if (ignoreMulticastReply(p, mem)) {
62e76326 1079 neighborCountIgnored(p);
26ac0430 1080 } else if (opcode == ICP_MISS) {
62e76326 1081 if (p == NULL) {
1082 neighborIgnoreNonPeer(from, opcode);
1083 } else {
0c3d3f65 1084 mem->ping_reply_callback(p, ntype, AnyP::PROTO_ICP, header, mem->ircb_data);
62e76326 1085 }
26ac0430 1086 } else if (opcode == ICP_HIT) {
62e76326 1087 if (p == NULL) {
1088 neighborIgnoreNonPeer(from, opcode);
1089 } else {
1090 header->opcode = ICP_HIT;
0c3d3f65 1091 mem->ping_reply_callback(p, ntype, AnyP::PROTO_ICP, header, mem->ircb_data);
62e76326 1092 }
26ac0430 1093 } else if (opcode == ICP_DECHO) {
62e76326 1094 if (p == NULL) {
1095 neighborIgnoreNonPeer(from, opcode);
1096 } else if (ntype == PEER_SIBLING) {
1097 debug_trap("neighborsUdpAck: Found non-ICP cache as SIBLING\n");
1098 debug_trap("neighborsUdpAck: non-ICP neighbors must be a PARENT\n");
1099 } else {
0c3d3f65 1100 mem->ping_reply_callback(p, ntype, AnyP::PROTO_ICP, header, mem->ircb_data);
62e76326 1101 }
26ac0430 1102 } else if (opcode == ICP_SECHO) {
62e76326 1103 if (p) {
e0236918 1104 debugs(15, DBG_IMPORTANT, "Ignoring SECHO from neighbor " << p->host);
62e76326 1105 neighborCountIgnored(p);
62e76326 1106 } else {
e0236918 1107 debugs(15, DBG_IMPORTANT, "Unsolicited SECHO from " << from);
62e76326 1108 }
26ac0430 1109 } else if (opcode == ICP_DENIED) {
62e76326 1110 if (p == NULL) {
1111 neighborIgnoreNonPeer(from, opcode);
1112 } else if (p->stats.pings_acked > 100) {
1113 if (100 * p->icp.counts[ICP_DENIED] / p->stats.pings_acked > 95) {
fa84c01d
FC
1114 debugs(15, DBG_CRITICAL, "95%% of replies from '" << p->host << "' are UDP_DENIED");
1115 debugs(15, DBG_CRITICAL, "Disabling '" << p->host << "', please check your configuration.");
62e76326 1116 neighborRemove(p);
1117 p = NULL;
1118 } else {
1119 neighborCountIgnored(p);
1120 }
1121 }
26ac0430 1122 } else if (opcode == ICP_MISS_NOFETCH) {
0c3d3f65 1123 mem->ping_reply_callback(p, ntype, AnyP::PROTO_ICP, header, mem->ircb_data);
26ac0430 1124 } else {
fa84c01d 1125 debugs(15, DBG_CRITICAL, "neighborsUdpAck: Unexpected ICP reply: " << opcode_d);
d2af9477 1126 }
090089c4 1127}
1128
a3c6762c 1129CachePeer *
40a1495e 1130peerFindByName(const char *name)
98ffb7e4 1131{
a3c6762c 1132 CachePeer *p = NULL;
62e76326 1133
40a1495e 1134 for (p = Config.peers; p; p = p->next) {
62e76326 1135 if (!strcasecmp(name, p->name))
1136 break;
98ffb7e4 1137 }
62e76326 1138
b69f7771 1139 return p;
98ffb7e4 1140}
b012353a 1141
a3c6762c 1142CachePeer *
db1cd23c 1143peerFindByNameAndPort(const char *name, unsigned short port)
1144{
a3c6762c 1145 CachePeer *p = NULL;
62e76326 1146
db1cd23c 1147 for (p = Config.peers; p; p = p->next) {
62e76326 1148 if (strcasecmp(name, p->name))
1149 continue;
1150
1151 if (port != p->http_port)
1152 continue;
1153
1154 break;
db1cd23c 1155 }
62e76326 1156
db1cd23c 1157 return p;
1158}
1159
5269d0bd 1160int
a3c6762c 1161neighborUp(const CachePeer * p)
5269d0bd 1162{
eb406bb7 1163 if (!p->tcp_up) {
a3c6762c 1164 if (!peerProbeConnect((CachePeer *) p)) {
cc192b50 1165 debugs(15, 8, "neighborUp: DOWN (probed): " << p->host << " (" << p->in_addr << ")");
4ed0e075 1166 return 0;
cc192b50 1167 }
eb406bb7 1168 }
62e76326 1169
64b8103b 1170 /*
a3c6762c 1171 * The CachePeer can not be UP if we don't have any IP addresses
26ac0430 1172 * for it.
64b8103b 1173 */
cc192b50 1174 if (0 == p->n_addresses) {
1175 debugs(15, 8, "neighborUp: DOWN (no-ip): " << p->host << " (" << p->in_addr << ")");
62e76326 1176 return 0;
cc192b50 1177 }
62e76326 1178
cc192b50 1179 if (p->options.no_query) {
1180 debugs(15, 8, "neighborUp: UP (no-query): " << p->host << " (" << p->in_addr << ")");
67c8308c 1181 return 1;
cc192b50 1182 }
67c8308c 1183
1184 if (p->stats.probe_start != 0 &&
cc192b50 1185 squid_curtime - p->stats.probe_start > Config.Timeout.deadPeer) {
1186 debugs(15, 8, "neighborUp: DOWN (dead): " << p->host << " (" << p->in_addr << ")");
67c8308c 1187 return 0;
cc192b50 1188 }
67c8308c 1189
cc192b50 1190 debugs(15, 8, "neighborUp: UP: " << p->host << " (" << p->in_addr << ")");
03b82057 1191 return 1;
5269d0bd 1192}
1193
e6e3b09b 1194void
28c60158 1195peerDestroy(void *data)
e6e3b09b 1196{
a3c6762c 1197 CachePeer *p = (CachePeer *)data;
62e76326 1198
b69f7771 1199 if (p == NULL)
62e76326 1200 return;
1201
b802d2cb 1202 CachePeerDomainList *nl = NULL;
b115733c 1203
b802d2cb 1204 for (CachePeerDomainList *l = p->peer_domain; l; l = nl) {
62e76326 1205 nl = l->next;
1206 safe_free(l->domain);
b115733c 1207 xfree(l);
ee4a1f5d 1208 }
62e76326 1209
b69f7771 1210 safe_free(p->host);
be753325 1211 safe_free(p->name);
1212 safe_free(p->domain);
e13ee7ad 1213#if USE_CACHE_DIGESTS
62e76326 1214
fa80a8ef 1215 cbdataReferenceDone(p->digest);
e13ee7ad 1216#endif
e6e3b09b 1217}
c7a3724d 1218
e13ee7ad 1219void
a3c6762c 1220peerNoteDigestGone(CachePeer * p)
e13ee7ad 1221{
1222#if USE_CACHE_DIGESTS
fa80a8ef 1223 cbdataReferenceDone(p->digest);
e13ee7ad 1224#endif
1225}
1226
dd4bd44e 1227static void
3ff65596 1228peerDNSConfigure(const ipcache_addrs *ia, const DnsLookupDetails &, void *data)
dd4bd44e 1229{
e8dca475
CT
1230 // TODO: connections to no-longer valid IP addresses should be
1231 // closed when we can detect such IP addresses.
1232
a3c6762c 1233 CachePeer *p = (CachePeer *)data;
62e76326 1234
dd4bd44e 1235 int j;
62e76326 1236
b69f7771 1237 if (p->n_addresses == 0) {
e0236918 1238 debugs(15, DBG_IMPORTANT, "Configuring " << neighborTypeStr(p) << " " << p->host << "/" << p->http_port << "/" << p->icp.port);
62e76326 1239
1240 if (p->type == PEER_MULTICAST)
e0236918 1241 debugs(15, DBG_IMPORTANT, " Multicast TTL = " << p->mcast.ttl);
b69f7771 1242 }
62e76326 1243
b69f7771 1244 p->n_addresses = 0;
62e76326 1245
dd4bd44e 1246 if (ia == NULL) {
fa84c01d 1247 debugs(0, DBG_CRITICAL, "WARNING: DNS lookup for '" << p->host << "' failed!");
62e76326 1248 return;
dd4bd44e 1249 }
62e76326 1250
c04fe656 1251 if ((int) ia->count < 1) {
fa84c01d 1252 debugs(0, DBG_CRITICAL, "WARNING: No IP address found for '" << p->host << "'!");
62e76326 1253 return;
dd4bd44e 1254 }
62e76326 1255
ff9970cc 1256 p->tcp_up = p->connect_fail_limit;
dc3a5177 1257
5db6bf73 1258 for (j = 0; j < (int) ia->count && j < PEER_MAX_ADDRESSES; ++j) {
62e76326 1259 p->addresses[j] = ia->in_addrs[j];
cc192b50 1260 debugs(15, 2, "--> IP address #" << j << ": " << p->addresses[j]);
5db6bf73 1261 ++ p->n_addresses;
dd4bd44e 1262 }
62e76326 1263
4dd643d5 1264 p->in_addr.setEmpty();
cc192b50 1265 p->in_addr = p->addresses[0];
4dd643d5 1266 p->in_addr.port(p->icp.port);
62e76326 1267
b69f7771 1268 if (p->type == PEER_MULTICAST)
62e76326 1269 peerCountMcastPeersSchedule(p, 10);
1270
9b5c4a9a 1271#if USE_ICMP
e51c8197 1272 if (p->type != PEER_MULTICAST && IamWorkerProcess())
62e76326 1273 if (!p->options.no_netdb_exchange)
1274 eventAddIsh("netdbExchangeStart", netdbExchangeStart, p, 30.0, 1);
9b5c4a9a
AJ
1275#endif
1276
e8dca475
CT
1277 if (p->standby.mgr.valid())
1278 PeerPoolMgr::Checkpoint(p->standby.mgr, "resolved peer");
dd4bd44e 1279}
1280
1281static void
51242273 1282peerRefreshDNS(void *data)
dd4bd44e 1283{
a3c6762c 1284 CachePeer *p = NULL;
62e76326 1285
46ca5fc6 1286 if (eventFind(peerRefreshDNS, NULL))
62e76326 1287 eventDelete(peerRefreshDNS, NULL);
1288
51242273 1289 if (!data && 0 == stat5minClientRequests()) {
62e76326 1290 /* no recent client traffic, wait a bit */
1291 eventAddIsh("peerRefreshDNS", peerRefreshDNS, NULL, 180.0, 1);
1292 return;
dd4bd44e 1293 }
62e76326 1294
1f3c4622 1295 for (p = Config.peers; p; p = p->next)
62e76326 1296 ipcache_nbgethostbyname(p->host, peerDNSConfigure, p);
1297
dd4bd44e 1298 /* Reconfigure the peers every hour */
52040193 1299 eventAddIsh("peerRefreshDNS", peerRefreshDNS, NULL, 3600.0, 1);
dd4bd44e 1300}
e924600d 1301
4ed0e075 1302static void
a3c6762c 1303peerConnectFailedSilent(CachePeer * p)
eb406bb7 1304{
1305 p->stats.last_connect_failure = squid_curtime;
62e76326 1306
eb406bb7 1307 if (!p->tcp_up) {
bf8fe701 1308 debugs(15, 2, "TCP connection to " << p->host << "/" << p->http_port <<
1309 " dead");
62e76326 1310 return;
eb406bb7 1311 }
62e76326 1312
5e263176 1313 -- p->tcp_up;
62e76326 1314
eb406bb7 1315 if (!p->tcp_up) {
e0236918 1316 debugs(15, DBG_IMPORTANT, "Detected DEAD " << neighborTypeStr(p) << ": " << p->name);
62e76326 1317 p->stats.logged_state = PEER_DEAD;
eb406bb7 1318 }
1319}
1320
4ed0e075 1321void
a3c6762c 1322peerConnectFailed(CachePeer *p)
4ed0e075 1323{
e0236918 1324 debugs(15, DBG_IMPORTANT, "TCP connection to " << p->host << "/" << p->http_port << " failed");
4ed0e075 1325 peerConnectFailedSilent(p);
1326}
1327
eb406bb7 1328void
a3c6762c 1329peerConnectSucceded(CachePeer * p)
eb406bb7 1330{
1331 if (!p->tcp_up) {
bf8fe701 1332 debugs(15, 2, "TCP connection to " << p->host << "/" << p->http_port << " succeded");
ff9970cc 1333 p->tcp_up = p->connect_fail_limit; // NP: so peerAlive(p) works properly.
32a47e3e 1334 peerAlive(p);
26ac0430
AJ
1335 if (!p->n_addresses)
1336 ipcache_nbgethostbyname(p->host, peerDNSConfigure, p);
1337 } else
ff9970cc 1338 p->tcp_up = p->connect_fail_limit;
eb406bb7 1339}
1340
4ed0e075 1341/*
26ac0430 1342* peerProbeConnect will be called on dead peers by neighborUp
4ed0e075 1343*/
cfd66529 1344static bool
a3c6762c 1345peerProbeConnect(CachePeer * p)
e924600d 1346{
cfd66529
AJ
1347 time_t ctimeout = p->connect_timeout > 0 ? p->connect_timeout : Config.Timeout.peer_connect;
1348 bool ret = (squid_curtime - p->stats.last_connect_failure) > (ctimeout * 10);
62e76326 1349
aed188fd 1350 if (p->testing_now > 0)
4ed0e075 1351 return ret;/* probe already running */
62e76326 1352
4ed0e075 1353 if (squid_curtime - p->stats.last_connect_probe == 0)
1354 return ret;/* don't probe to often */
62e76326 1355
a3c6762c 1356 /* for each IP address of this CachePeer. find one that we can connect to and probe it. */
5db6bf73 1357 for (int i = 0; i < p->n_addresses; ++i) {
f9b72e0c 1358 Comm::ConnectionPointer conn = new Comm::Connection;
cfd66529 1359 conn->remote = p->addresses[i];
4dd643d5 1360 conn->remote.port(p->http_port);
06c40c46 1361 conn->setPeer(p);
cfd66529 1362 getOutgoingAddress(NULL, conn);
cc192b50 1363
5db6bf73 1364 ++ p->testing_now;
715b5def 1365
aed188fd 1366 AsyncCall::Pointer call = commCbCall(15,3, "peerProbeConnectDone", CommConnectCbPtrFun(peerProbeConnectDone, p));
4accd6d8 1367 Comm::ConnOpener *cs = new Comm::ConnOpener(conn, call, ctimeout);
aed188fd 1368 cs->setHost(p->host);
855150a4 1369 AsyncJob::Start(cs);
715b5def
AJ
1370 }
1371
eb406bb7 1372 p->stats.last_connect_probe = squid_curtime;
62e76326 1373
4ed0e075 1374 return ret;
e924600d 1375}
1376
1377static void
c8407295 1378peerProbeConnectDone(const Comm::ConnectionPointer &conn, Comm::Flag status, int xerrno, void *data)
e924600d 1379{
a3c6762c 1380 CachePeer *p = (CachePeer*)data;
62e76326 1381
c8407295 1382 if (status == Comm::OK) {
62e76326 1383 peerConnectSucceded(p);
e924600d 1384 } else {
4ed0e075 1385 peerConnectFailedSilent(p);
e924600d 1386 }
62e76326 1387
5e263176 1388 -- p->testing_now;
db9bf30d
AJ
1389 conn->close();
1390 // TODO: log this traffic.
e924600d 1391}
1392
429fdbec 1393static void
a3c6762c 1394peerCountMcastPeersSchedule(CachePeer * p, time_t when)
429fdbec 1395{
b515fc11 1396 if (p->mcast.flags.count_event_pending)
62e76326 1397 return;
1398
429fdbec 1399 eventAdd("peerCountMcastPeersStart",
62e76326 1400 peerCountMcastPeersStart,
1401 p,
1402 (double) when, 1);
1403
ccdf4138 1404 p->mcast.flags.count_event_pending = true;
429fdbec 1405}
1406
1407static void
1408peerCountMcastPeersStart(void *data)
1409{
a3c6762c 1410 CachePeer *p = (CachePeer *)data;
28c60158 1411 ps_state *psstate;
429fdbec 1412 StoreEntry *fake;
1413 MemObject *mem;
1414 icp_common_t *query;
007b8be4 1415 int reqnum;
429fdbec 1416 LOCAL_ARRAY(char, url, MAX_URL);
ce66013b 1417 assert(p->type == PEER_MULTICAST);
ccdf4138 1418 p->mcast.flags.count_event_pending = false;
cc192b50 1419 snprintf(url, MAX_URL, "http://");
4dd643d5 1420 p->in_addr.toUrl(url+7, MAX_URL -8 );
cc192b50 1421 strcat(url, "/");
c2a7cefd 1422 fake = storeCreateEntry(url, url, RequestFlags(), Http::METHOD_GET);
c21ad0f5 1423 HttpRequest *req = HttpRequest::CreateFromUrl(url);
b24880fe 1424 psstate = new ps_state;
b248c2a3
AJ
1425 psstate->request = req;
1426 HTTPMSGLOCK(psstate->request);
429fdbec 1427 psstate->entry = fake;
1428 psstate->callback = NULL;
99117d82 1429 psstate->callback_data = cbdataReference(p);
b4e7f82d 1430 psstate->ping.start = current_time;
429fdbec 1431 mem = fake->mem_obj;
b248c2a3
AJ
1432 mem->request = psstate->request;
1433 HTTPMSGLOCK(mem->request);
429fdbec 1434 mem->start_ping = current_time;
b4e7f82d 1435 mem->ping_reply_callback = peerCountHandleIcpReply;
429fdbec 1436 mem->ircb_data = psstate;
e0d28505 1437 mcastSetTtl(icpOutgoingConn->fd, p->mcast.ttl);
007b8be4 1438 p->mcast.id = mem->id;
332dafa2 1439 reqnum = icpSetCacheKey((const cache_key *)fake->key);
e6ccf245 1440 query = _icp_common_t::createMessage(ICP_QUERY, 0, url, reqnum, 0);
e0d28505 1441 icpUdpSend(icpOutgoingConn->fd, p->in_addr, query, LOG_ICP_QUERY, 0);
429fdbec 1442 fake->ping_status = PING_WAITING;
1443 eventAdd("peerCountMcastPeersDone",
62e76326 1444 peerCountMcastPeersDone,
1445 psstate,
99117d82 1446 Config.Timeout.mcast_icp_query / 1000.0, 1);
ccdf4138 1447 p->mcast.flags.counting = true;
429fdbec 1448 peerCountMcastPeersSchedule(p, MCAST_COUNT_RATE);
1449}
1450
1451static void
1452peerCountMcastPeersDone(void *data)
1453{
e6ccf245 1454 ps_state *psstate = (ps_state *)data;
429fdbec 1455 StoreEntry *fake = psstate->entry;
99117d82 1456
1457 if (cbdataReferenceValid(psstate->callback_data)) {
a3c6762c 1458 CachePeer *p = (CachePeer *)psstate->callback_data;
ccdf4138 1459 p->mcast.flags.counting = false;
a98bcbee 1460 p->mcast.avg_n_members = Math::doubleAverage(p->mcast.avg_n_members, (double) psstate->ping.n_recv, ++p->mcast.n_times_counted, 10);
e0236918 1461 debugs(15, DBG_IMPORTANT, "Group " << p->host << ": " << psstate->ping.n_recv <<
bf8fe701 1462 " replies, "<< std::setw(4)<< std::setprecision(2) <<
1463 p->mcast.avg_n_members <<" average, RTT " << p->stats.rtt);
99117d82 1464 p->mcast.n_replies_expected = (int) p->mcast.avg_n_members;
1465 }
1466
1467 cbdataReferenceDone(psstate->callback_data);
1468
bc52369a 1469 fake->abort(); // sets ENTRY_ABORTED and initiates releated cleanup
6dd9f4bd 1470 HTTPMSGUNLOCK(fake->mem_obj->request);
acc5dc4c 1471 fake->unlock("peerCountMcastPeersDone");
6dd9f4bd 1472 HTTPMSGUNLOCK(psstate->request);
1a827bc0 1473 cbdataFree(psstate);
429fdbec 1474}
1475
1476static void
a3c6762c 1477peerCountHandleIcpReply(CachePeer * p, peer_t type, AnyP::ProtocolType proto, void *hdrnotused, void *data)
429fdbec 1478{
d1b63fc8 1479 int rtt_av_factor;
1480
e6ccf245 1481 ps_state *psstate = (ps_state *)data;
1a827bc0 1482 StoreEntry *fake = psstate->entry;
1483 MemObject *mem = fake->mem_obj;
1484 int rtt = tvSubMsec(mem->start_ping, current_time);
0c3d3f65 1485 assert(proto == AnyP::PROTO_ICP);
1a827bc0 1486 assert(fake);
1487 assert(mem);
5db6bf73 1488 ++ psstate->ping.n_recv;
d1b63fc8 1489 rtt_av_factor = RTT_AV_FACTOR;
62e76326 1490
d1b63fc8 1491 if (p->options.weighted_roundrobin)
62e76326 1492 rtt_av_factor = RTT_BACKGROUND_AV_FACTOR;
1493
a98bcbee 1494 p->stats.rtt = Math::intAverage(p->stats.rtt, rtt, psstate->ping.n_recv, rtt_av_factor);
429fdbec 1495}
ed7f5615 1496
1497static void
1498neighborDumpPeers(StoreEntry * sentry)
1499{
1500 dump_peers(sentry, Config.peers);
1501}
1502
1503static void
1504neighborDumpNonPeers(StoreEntry * sentry)
1505{
1506 dump_peers(sentry, non_peers);
1507}
1508
a369131d 1509void
a3c6762c 1510dump_peer_options(StoreEntry * sentry, CachePeer * p)
a369131d 1511{
cd196bc8 1512 if (p->options.proxy_only)
62e76326 1513 storeAppendPrintf(sentry, " proxy-only");
1514
cd196bc8 1515 if (p->options.no_query)
62e76326 1516 storeAppendPrintf(sentry, " no-query");
1517
d1b63fc8 1518 if (p->options.background_ping)
62e76326 1519 storeAppendPrintf(sentry, " background-ping");
1520
cd196bc8 1521 if (p->options.no_digest)
62e76326 1522 storeAppendPrintf(sentry, " no-digest");
1523
cd196bc8 1524 if (p->options.default_parent)
62e76326 1525 storeAppendPrintf(sentry, " default");
1526
cd196bc8 1527 if (p->options.roundrobin)
62e76326 1528 storeAppendPrintf(sentry, " round-robin");
1529
f7e1d9ce
HN
1530 if (p->options.carp)
1531 storeAppendPrintf(sentry, " carp");
354b5a2b 1532
2f1431ea 1533#if USE_AUTH
f7e1d9ce
HN
1534 if (p->options.userhash)
1535 storeAppendPrintf(sentry, " userhash");
354b5a2b 1536#endif
f7e1d9ce 1537
354b5a2b 1538 if (p->options.sourcehash)
f7e1d9ce 1539 storeAppendPrintf(sentry, " sourcehash");
354b5a2b 1540
d1b63fc8 1541 if (p->options.weighted_roundrobin)
62e76326 1542 storeAppendPrintf(sentry, " weighted-round-robin");
1543
cd196bc8 1544 if (p->options.mcast_responder)
62e76326 1545 storeAppendPrintf(sentry, " multicast-responder");
1546
8a368316
AJ
1547#if PEER_MULTICAST_SIBLINGS
1548 if (p->options.mcast_siblings)
1549 storeAppendPrintf(sentry, " multicast-siblings");
1550#endif
1551
678a066c 1552 if (p->weight != 1)
62e76326 1553 storeAppendPrintf(sentry, " weight=%d", p->weight);
1554
cd196bc8 1555 if (p->options.closest_only)
62e76326 1556 storeAppendPrintf(sentry, " closest-only");
1557
dc9d133b 1558#if USE_HTCP
18191440 1559 if (p->options.htcp) {
62e76326 1560 storeAppendPrintf(sentry, " htcp");
18191440
AJ
1561 if (p->options.htcp_oldsquid || p->options.htcp_no_clr || p->options.htcp_no_purge_clr || p->options.htcp_only_clr) {
1562 int doneopts=0;
1563 if (p->options.htcp_oldsquid)
1564 storeAppendPrintf(sentry, "%soldsquid",(doneopts++>0?",":"="));
1565 if (p->options.htcp_no_clr)
1566 storeAppendPrintf(sentry, "%sno-clr",(doneopts++>0?",":"="));
1567 if (p->options.htcp_no_purge_clr)
1568 storeAppendPrintf(sentry, "%sno-purge-clr",(doneopts++>0?",":"="));
1569 if (p->options.htcp_only_clr)
1570 storeAppendPrintf(sentry, "%sonly-clr",(doneopts++>0?",":"="));
1571 }
1572 }
dc9d133b 1573#endif
62e76326 1574
cd196bc8 1575 if (p->options.no_netdb_exchange)
62e76326 1576 storeAppendPrintf(sentry, " no-netdb-exchange");
1577
9a0a18de 1578#if USE_DELAY_POOLS
cd196bc8 1579 if (p->options.no_delay)
62e76326 1580 storeAppendPrintf(sentry, " no-delay");
95e36d02 1581#endif
62e76326 1582
c68e9c6b 1583 if (p->login)
62e76326 1584 storeAppendPrintf(sentry, " login=%s", p->login);
1585
a369131d 1586 if (p->mcast.ttl > 0)
62e76326 1587 storeAppendPrintf(sentry, " ttl=%d", p->mcast.ttl);
1588
be753325 1589 if (p->connect_timeout > 0)
62e76326 1590 storeAppendPrintf(sentry, " connect-timeout=%d", (int) p->connect_timeout);
1591
ff9970cc
AJ
1592 if (p->connect_fail_limit != PEER_TCP_MAGIC_COUNT)
1593 storeAppendPrintf(sentry, " connect-fail-limit=%d", p->connect_fail_limit);
1594
be753325 1595#if USE_CACHE_DIGESTS
62e76326 1596
be753325 1597 if (p->digest_url)
62e76326 1598 storeAppendPrintf(sentry, " digest-url=%s", p->digest_url);
1599
be753325 1600#endif
62e76326 1601
be753325 1602 if (p->options.allow_miss)
62e76326 1603 storeAppendPrintf(sentry, " allow-miss");
1604
b0758e04
AJ
1605 if (p->options.no_tproxy)
1606 storeAppendPrintf(sentry, " no-tproxy");
1607
be753325 1608 if (p->max_conn > 0)
62e76326 1609 storeAppendPrintf(sentry, " max-conn=%d", p->max_conn);
e8dca475
CT
1610 if (p->standby.limit > 0)
1611 storeAppendPrintf(sentry, " standby=%d", p->standby.limit);
62e76326 1612
be753325 1613 if (p->options.originserver)
62e76326 1614 storeAppendPrintf(sentry, " originserver");
1615
be753325 1616 if (p->domain)
62e76326 1617 storeAppendPrintf(sentry, " forceddomain=%s", p->domain);
1618
26ac0430
AJ
1619 if (p->connection_auth == 0)
1620 storeAppendPrintf(sentry, " connection-auth=off");
1621 else if (p->connection_auth == 1)
1622 storeAppendPrintf(sentry, " connection-auth=on");
1623 else if (p->connection_auth == 2)
1624 storeAppendPrintf(sentry, " connection-auth=auto");
d67acb4e 1625
a369131d 1626 storeAppendPrintf(sentry, "\n");
1627}
1628
ed7f5615 1629static void
a3c6762c 1630dump_peers(StoreEntry * sentry, CachePeer * peers)
ed7f5615 1631{
a3c6762c 1632 CachePeer *e = NULL;
cc192b50 1633 char ntoabuf[MAX_IPSTRLEN];
b802d2cb 1634 CachePeerDomainList *d = NULL;
ed7f5615 1635 icp_opcode op;
06e6d12a 1636 int i;
62e76326 1637
ed7f5615 1638 if (peers == NULL)
62e76326 1639 storeAppendPrintf(sentry, "There are no neighbors installed.\n");
1640
ed7f5615 1641 for (e = peers; e; e = e->next) {
62e76326 1642 assert(e->host != NULL);
1643 storeAppendPrintf(sentry, "\n%-11.11s: %s\n",
1644 neighborTypeStr(e),
1645 e->name);
1646 storeAppendPrintf(sentry, "Host : %s/%d/%d\n",
1647 e->host,
1648 e->http_port,
1649 e->icp.port);
1650 storeAppendPrintf(sentry, "Flags :");
1651 dump_peer_options(sentry, e);
1652
5db6bf73 1653 for (i = 0; i < e->n_addresses; ++i) {
62e76326 1654 storeAppendPrintf(sentry, "Address[%d] : %s\n", i,
4dd643d5 1655 e->addresses[i].toStr(ntoabuf,MAX_IPSTRLEN) );
62e76326 1656 }
1657
1658 storeAppendPrintf(sentry, "Status : %s\n",
1659 neighborUp(e) ? "Up" : "Down");
1660 storeAppendPrintf(sentry, "FETCHES : %d\n", e->stats.fetches);
1661 storeAppendPrintf(sentry, "OPEN CONNS : %d\n", e->stats.conn_open);
1662 storeAppendPrintf(sentry, "AVG RTT : %d msec\n", e->stats.rtt);
1663
1664 if (!e->options.no_query) {
1665 storeAppendPrintf(sentry, "LAST QUERY : %8d seconds ago\n",
1666 (int) (squid_curtime - e->stats.last_query));
1667
1668 if (e->stats.last_reply > 0)
1669 storeAppendPrintf(sentry, "LAST REPLY : %8d seconds ago\n",
1670 (int) (squid_curtime - e->stats.last_reply));
1671 else
1672 storeAppendPrintf(sentry, "LAST REPLY : none received\n");
1673
1674 storeAppendPrintf(sentry, "PINGS SENT : %8d\n", e->stats.pings_sent);
1675
1676 storeAppendPrintf(sentry, "PINGS ACKED: %8d %3d%%\n",
1677 e->stats.pings_acked,
a98bcbee 1678 Math::intPercent(e->stats.pings_acked, e->stats.pings_sent));
62e76326 1679 }
1680
a98bcbee 1681 storeAppendPrintf(sentry, "IGNORED : %8d %3d%%\n", e->stats.ignored_replies, Math::intPercent(e->stats.ignored_replies, e->stats.pings_acked));
62e76326 1682
1683 if (!e->options.no_query) {
1684 storeAppendPrintf(sentry, "Histogram of PINGS ACKED:\n");
1994bb24 1685#if USE_HTCP
62e76326 1686
1687 if (e->options.htcp) {
1688 storeAppendPrintf(sentry, "\tMisses\t%8d %3d%%\n",
1689 e->htcp.counts[0],
a98bcbee 1690 Math::intPercent(e->htcp.counts[0], e->stats.pings_acked));
62e76326 1691 storeAppendPrintf(sentry, "\tHits\t%8d %3d%%\n",
1692 e->htcp.counts[1],
a98bcbee 1693 Math::intPercent(e->htcp.counts[1], e->stats.pings_acked));
62e76326 1694 } else {
1994bb24 1695#endif
62e76326 1696
1697 for (op = ICP_INVALID; op < ICP_END; ++op) {
1698 if (e->icp.counts[op] == 0)
1699 continue;
1700
1701 storeAppendPrintf(sentry, " %12.12s : %8d %3d%%\n",
1702 icp_opcode_str[op],
1703 e->icp.counts[op],
a98bcbee 1704 Math::intPercent(e->icp.counts[op], e->stats.pings_acked));
62e76326 1705 }
1706
1994bb24 1707#if USE_HTCP
62e76326 1708
1709 }
1710
1994bb24 1711#endif
62e76326 1712
1713 }
1714
1715 if (e->stats.last_connect_failure) {
1716 storeAppendPrintf(sentry, "Last failed connect() at: %s\n",
20efa1c2 1717 Time::FormatHttpd(e->stats.last_connect_failure));
62e76326 1718 }
1719
1720 if (e->peer_domain != NULL) {
1721 storeAppendPrintf(sentry, "DOMAIN LIST: ");
1722
1723 for (d = e->peer_domain; d; d = d->next) {
1724 storeAppendPrintf(sentry, "%s%s ",
1725 d->do_ping ? null_string : "!", d->domain);
1726 }
1727
1728 storeAppendPrintf(sentry, "\n");
1729 }
1730
a98bcbee 1731 storeAppendPrintf(sentry, "keep-alive ratio: %d%%\n", Math::intPercent(e->stats.n_keepalives_recv, e->stats.n_keepalives_sent));
ed7f5615 1732 }
1733}
86aebcda 1734
1735#if USE_HTCP
1736void
fad2588a 1737neighborsHtcpReply(const cache_key * key, HtcpReplyData * htcp, const Ip::Address &from)
86aebcda 1738{
53525d53 1739 StoreEntry *e = Store::Root().get(key);
399cabec 1740 MemObject *mem = NULL;
a3c6762c 1741 CachePeer *p;
399cabec 1742 peer_t ntype = PEER_NONE;
26ac0430
AJ
1743 debugs(15, 6, "neighborsHtcpReply: " <<
1744 (htcp->hit ? "HIT" : "MISS") << " " <<
1745 storeKeyText(key) );
62e76326 1746
88e13392 1747 if (NULL != e)
62e76326 1748 mem = e->mem_obj;
1749
399cabec 1750 if ((p = whichPeer(from)))
62e76326 1751 neighborAliveHtcp(p, mem, htcp);
1752
399cabec 1753 /* Does the entry exist? */
26ac0430 1754 if (NULL == e) {
bf8fe701 1755 debugs(12, 3, "neighyborsHtcpReply: Cache key '" << storeKeyText(key) << "' not found");
62e76326 1756 neighborCountIgnored(p);
1757 return;
399cabec 1758 }
62e76326 1759
399cabec 1760 /* check if someone is already fetching it */
26ac0430 1761 if (EBIT_TEST(e->flags, ENTRY_DISPATCHED)) {
bf8fe701 1762 debugs(15, 3, "neighborsUdpAck: '" << storeKeyText(key) << "' already being fetched.");
62e76326 1763 neighborCountIgnored(p);
1764 return;
399cabec 1765 }
62e76326 1766
26ac0430 1767 if (mem == NULL) {
bf8fe701 1768 debugs(15, 2, "Ignoring reply for missing mem_obj: " << storeKeyText(key));
62e76326 1769 neighborCountIgnored(p);
1770 return;
399cabec 1771 }
62e76326 1772
26ac0430 1773 if (e->ping_status != PING_WAITING) {
bf8fe701 1774 debugs(15, 2, "neighborsUdpAck: Entry " << storeKeyText(key) << " is not PING_WAITING");
62e76326 1775 neighborCountIgnored(p);
1776 return;
399cabec 1777 }
62e76326 1778
1bfe9ade 1779 if (!e->locked()) {
5bd484b5 1780 // TODO: many entries are unlocked; why is this reported at level 1?
e0236918 1781 debugs(12, DBG_IMPORTANT, "neighborsUdpAck: '" << storeKeyText(key) << "' has no locks");
62e76326 1782 neighborCountIgnored(p);
1783 return;
399cabec 1784 }
62e76326 1785
26ac0430 1786 if (p) {
62e76326 1787 ntype = neighborType(p, mem->request);
1788 neighborUpdateRtt(p, mem);
44f2f2e4 1789 }
62e76326 1790
26ac0430 1791 if (ignoreMulticastReply(p, mem)) {
62e76326 1792 neighborCountIgnored(p);
1793 return;
399cabec 1794 }
62e76326 1795
bf8fe701 1796 debugs(15, 3, "neighborsHtcpReply: e = " << e);
0c3d3f65 1797 mem->ping_reply_callback(p, ntype, AnyP::PROTO_HTCP, htcp, mem->ircb_data);
86aebcda 1798}
62e76326 1799
1bd06eff
BR
1800/*
1801 * Send HTCP CLR messages to all peers configured to receive them.
1802 */
4f4fa815 1803void
8dceeee3 1804neighborsHtcpClear(StoreEntry * e, const char *uri, HttpRequest * req, const HttpRequestMethod &method, htcp_clr_reason reason)
4f4fa815 1805{
a3c6762c 1806 CachePeer *p;
4f4fa815
BR
1807 char buf[128];
1808
4f4fa815
BR
1809 for (p = Config.peers; p; p = p->next) {
1810 if (!p->options.htcp) {
1811 continue;
1812 }
1813 if (p->options.htcp_no_clr) {
1814 continue;
1815 }
1816 if (p->options.htcp_no_purge_clr && reason == HTCP_CLR_PURGE) {
1817 continue;
1818 }
4dd643d5 1819 debugs(15, 3, "neighborsHtcpClear: sending CLR to " << p->in_addr.toUrl(buf, 128));
4f4fa815
BR
1820 htcpClear(e, uri, req, method, p, reason);
1821 }
1822}
1823
86aebcda 1824#endif