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