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