]> git.ipfire.org Git - thirdparty/squid.git/blame - src/neighbors.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / neighbors.cc
CommitLineData
30a4f2a8 1/*
77b1029d 2 * Copyright (C) 1996-2020 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 170 checklist.al = ps->al;
82a9ea6c
EB
171 if (ps->al && ps->al->reply) {
172 checklist.reply = ps->al->reply.getRaw();
173 HTTPMSGLOCK(checklist.reply);
174 }
cb365059 175 checklist.syncAle(request, nullptr);
06bf5384 176 return checklist.fastCheck().allowed();
090089c4 177}
178
a3c6762c 179/* Return TRUE if it is okay to send an ICP request to this CachePeer. */
62fd6124 180static int
cb365059 181peerWouldBePinged(const CachePeer * p, PeerSelector * ps)
62fd6124 182{
cb365059
EB
183 assert(ps);
184 HttpRequest *request = ps->request;
185
6315602c 186 if (p->icp.port == 0)
62e76326 187 return 0;
188
cd196bc8 189 if (p->options.no_query)
62e76326 190 return 0;
191
cd196bc8 192 if (p->options.mcast_responder)
62e76326 193 return 0;
194
83b381d5 195 if (p->n_addresses == 0)
62e76326 196 return 0;
197
6315602c 198 if (p->options.background_ping && (squid_curtime - p->stats.last_query < Config.backgroundPingRate))
62e76326 199 return 0;
200
62fd6124 201 /* the case below seems strange, but can happen if the
202 * URL host is on the other side of a firewall */
b69f7771 203 if (p->type == PEER_SIBLING)
45e5102d 204 if (!request->flags.hierarchical)
62e76326 205 return 0;
206
cb365059 207 if (!peerAllowedToUse(p, ps))
6315602c
JGD
208 return 0;
209
83b381d5 210 /* Ping dead peers every timeout interval */
211 if (squid_curtime - p->stats.last_query > Config.Timeout.deadPeer)
62e76326 212 return 1;
213
5537c535 214 if (!neighborUp(p))
215 return 0;
62e76326 216
62fd6124 217 return 1;
218}
219
e8dca475
CT
220bool
221peerCanOpenMore(const CachePeer *p)
222{
223 const int effectiveLimit = p->max_conn <= 0 ? Squid_MaxFD : p->max_conn;
224 const int remaining = effectiveLimit - p->stats.conn_open;
225 debugs(15, 7, remaining << '=' << effectiveLimit << '-' << p->stats.conn_open);
226 return remaining > 0;
227}
228
229bool
230peerHasConnAvailable(const CachePeer *p)
231{
232 // Standby connections can be used without opening new connections.
233 const int standbys = p->standby.pool ? p->standby.pool->count() : 0;
234
235 // XXX: Some idle pconns can be used without opening new connections.
236 // Complication: Idle pconns cannot be reused for some requests.
237 const int usableIdles = 0;
238
239 const int available = standbys + usableIdles;
240 debugs(15, 7, available << '=' << standbys << '+' << usableIdles);
241 return available > 0;
242}
243
244void
245peerConnClosed(CachePeer *p)
246{
247 --p->stats.conn_open;
248 if (p->standby.waitingForClose && peerCanOpenMore(p)) {
249 p->standby.waitingForClose = false;
250 PeerPoolMgr::Checkpoint(p->standby.mgr, "conn closed");
251 }
252}
253
a3c6762c 254/* Return TRUE if it is okay to send an HTTP request to this CachePeer. */
8ff4505b 255int
cb365059 256peerHTTPOkay(const CachePeer * p, PeerSelector * ps)
62fd6124 257{
e8dca475
CT
258 if (!peerCanOpenMore(p) && !peerHasConnAvailable(p))
259 return 0;
6315602c 260
cb365059 261 if (!peerAllowedToUse(p, ps))
62e76326 262 return 0;
263
b69f7771 264 if (!neighborUp(p))
62e76326 265 return 0;
266
62fd6124 267 return 1;
268}
269
3c6e634f 270int
cb365059 271neighborsCount(PeerSelector *ps)
090089c4 272{
a3c6762c 273 CachePeer *p = NULL;
090089c4 274 int count = 0;
62e76326 275
40a1495e 276 for (p = Config.peers; p; p = p->next)
cb365059 277 if (peerWouldBePinged(p, ps))
5db6bf73 278 ++count;
62e76326 279
bf8fe701 280 debugs(15, 3, "neighborsCount: " << count);
62e76326 281
3c6e634f 282 return count;
283}
090089c4 284
a3c6762c 285CachePeer *
cb365059 286getFirstUpParent(PeerSelector *ps)
090089c4 287{
cb365059
EB
288 assert(ps);
289 HttpRequest *request = ps->request;
290
a3c6762c 291 CachePeer *p = NULL;
62e76326 292
40a1495e 293 for (p = Config.peers; p; p = p->next) {
62e76326 294 if (!neighborUp(p))
295 continue;
296
5c51bffb 297 if (neighborType(p, request->url) != PEER_PARENT)
62e76326 298 continue;
299
cb365059 300 if (!peerHTTPOkay(p, ps))
62e76326 301 continue;
302
303 break;
090089c4 304 }
62e76326 305
bf8fe701 306 debugs(15, 3, "getFirstUpParent: returning " << (p ? p->host : "NULL"));
b69f7771 307 return p;
090089c4 308}
309
a3c6762c 310CachePeer *
cb365059 311getRoundRobinParent(PeerSelector *ps)
48b38d01 312{
cb365059
EB
313 assert(ps);
314 HttpRequest *request = ps->request;
315
a3c6762c
FC
316 CachePeer *p;
317 CachePeer *q = NULL;
62e76326 318
40a1495e 319 for (p = Config.peers; p; p = p->next) {
62e76326 320 if (!p->options.roundrobin)
321 continue;
322
5c51bffb 323 if (neighborType(p, request->url) != PEER_PARENT)
62e76326 324 continue;
325
cb365059 326 if (!peerHTTPOkay(p, ps))
62e76326 327 continue;
328
1d0bef67 329 if (p->weight == 0)
0d5a2006 330 continue;
1d0bef67
AJ
331
332 if (q) {
333 if (p->weight == q->weight) {
334 if (q->rr_count < p->rr_count)
335 continue;
2d3d0b4e 336 } else if ( ((double) q->rr_count / q->weight) < ((double) p->rr_count / p->weight)) {
1d0bef67
AJ
337 continue;
338 }
0d5a2006 339 }
62e76326 340
341 q = p;
48b38d01 342 }
62e76326 343
b69f7771 344 if (q)
5db6bf73 345 ++ q->rr_count;
62e76326 346
1d0bef67 347 debugs(15, 3, HERE << "returning " << (q ? q->host : "NULL"));
62e76326 348
b69f7771 349 return q;
48b38d01 350}
351
a3c6762c 352CachePeer *
cb365059 353getWeightedRoundRobinParent(PeerSelector *ps)
d1b63fc8 354{
cb365059
EB
355 assert(ps);
356 HttpRequest *request = ps->request;
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
5c51bffb 366 if (neighborType(p, request->url) != PEER_PARENT)
62e76326 367 continue;
368
cb365059 369 if (!peerHTTPOkay(p, ps))
62e76326 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
5c51bffb 383 if (neighborType(p, request->url) != PEER_PARENT)
62e76326 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 *
cb365059 471getDefaultParent(PeerSelector *ps)
5269d0bd 472{
cb365059
EB
473 assert(ps);
474 HttpRequest *request = ps->request;
475
a3c6762c 476 CachePeer *p = NULL;
62e76326 477
40a1495e 478 for (p = Config.peers; p; p = p->next) {
5c51bffb 479 if (neighborType(p, request->url) != PEER_PARENT)
62e76326 480 continue;
481
482 if (!p->options.default_parent)
483 continue;
484
cb365059 485 if (!peerHTTPOkay(p, ps))
62e76326 486 continue;
487
bf8fe701 488 debugs(15, 3, "getDefaultParent: returning " << p->host);
62e76326 489
490 return p;
5269d0bd 491 }
62e76326 492
bf8fe701 493 debugs(15, 3, "getDefaultParent: returning NULL");
5269d0bd 494 return NULL;
495}
496
a3c6762c
FC
497CachePeer *
498getNextPeer(CachePeer * p)
090089c4 499{
b69f7771 500 return p->next;
090089c4 501}
502
a3c6762c 503CachePeer *
deb79f06 504getFirstPeer(void)
090089c4 505{
40a1495e 506 return Config.peers;
090089c4 507}
508
b8d8561b 509static void
a3c6762c 510neighborRemove(CachePeer * target)
30a4f2a8 511{
a3c6762c
FC
512 CachePeer *p = NULL;
513 CachePeer **P = NULL;
40a1495e 514 p = Config.peers;
515 P = &Config.peers;
62e76326 516
b69f7771 517 while (p) {
62e76326 518 if (target == p)
519 break;
520
521 P = &p->next;
522
523 p = p->next;
b69f7771 524 }
62e76326 525
b69f7771 526 if (p) {
62e76326 527 *P = p->next;
719815a0
AJ
528 p->next = NULL;
529 delete p;
5e263176 530 --Config.npeers;
4d64d74a 531 }
62e76326 532
40a1495e 533 first_ping = Config.peers;
4d64d74a 534}
535
5f5e883f
FC
536static void
537neighborsRegisterWithCacheManager()
538{
8822ebee 539 Mgr::RegisterAction("server_list",
d9fc6862
A
540 "Peer Cache Statistics",
541 neighborDumpPeers, 0, 1);
5f5e883f 542
e0d28505 543 if (Comm::IsConnOpen(icpIncomingConn)) {
8822ebee 544 Mgr::RegisterAction("non_peers",
d9fc6862
A
545 "List of Unknown sites sending ICP messages",
546 neighborDumpNonPeers, 0, 1);
5f5e883f
FC
547 }
548}
549
b8d8561b 550void
a86029b9 551neighbors_init(void)
090089c4 552{
30a4f2a8 553 struct servent *sep = NULL;
b263bd3f 554 const char *me = getMyHostname();
a3c6762c
FC
555 CachePeer *thisPeer = NULL;
556 CachePeer *next = NULL;
62e76326 557
6fdc2d18
FC
558 neighborsRegisterWithCacheManager();
559
e0d28505 560 if (Comm::IsConnOpen(icpIncomingConn)) {
62e76326 561
562 for (thisPeer = Config.peers; thisPeer; thisPeer = next) {
62e76326 563 next = thisPeer->next;
564
565 if (0 != strcmp(thisPeer->host, me))
566 continue;
567
fa720bfb 568 for (AnyP::PortCfgPointer s = HttpPortList; s != NULL; s = s->next) {
4dd643d5 569 if (thisPeer->http_port != s->s.port())
62e76326 570 continue;
571
ccfbe8f4
AR
572 debugs(15, DBG_IMPORTANT, "WARNING: Peer looks like this host." <<
573 Debug::Extra << "Ignoring " <<
bf8fe701 574 neighborTypeStr(thisPeer) << " " << thisPeer->host <<
575 "/" << thisPeer->http_port << "/" <<
576 thisPeer->icp.port);
62e76326 577
62e76326 578 neighborRemove(thisPeer);
579 }
580 }
b263bd3f 581 }
62e76326 582
5999b776 583 peerRefreshDNS((void *) 1);
62e76326 584
9ca56f05 585 sep = getservbyname("echo", "udp");
f45dd259 586 echo_port = sep ? ntohs((unsigned short) sep->s_port) : 7;
62e76326 587
c68e4e90 588 first_ping = Config.peers;
62ee09ca 589}
590
b8d8561b 591int
190154cf 592neighborsUdpPing(HttpRequest * request,
62e76326 593 StoreEntry * entry,
594 IRCB * callback,
cb365059 595 PeerSelector *ps,
62e76326 596 int *exprep,
597 int *timeout)
090089c4 598{
3900307b 599 const char *url = entry->url();
b6c0e933 600 MemObject *mem = entry->mem_obj;
a3c6762c 601 CachePeer *p = NULL;
090089c4 602 int i;
1061b406 603 int reqnum = 0;
6d2296d4 604 int flags;
429fdbec 605 int queries_sent = 0;
0a0bf5db 606 int peers_pinged = 0;
a8c926ff 607 int parent_timeout = 0, parent_exprep = 0;
608 int sibling_timeout = 0, sibling_exprep = 0;
1bf2cd49 609 int mcast_timeout = 0, mcast_exprep = 0;
090089c4 610
40a1495e 611 if (Config.peers == NULL)
62e76326 612 return 0;
613
4310f8b0 614 assert(!entry->hasDisk());
62e76326 615
b6c0e933 616 mem->start_ping = current_time;
62e76326 617
b4e7f82d 618 mem->ping_reply_callback = callback;
62e76326 619
cb365059 620 mem->ircb_data = ps;
62e76326 621
332dafa2 622 reqnum = icpSetCacheKey((const cache_key *)entry->key);
62e76326 623
40a1495e 624 for (i = 0, p = first_ping; i++ < Config.npeers; p = p->next) {
62e76326 625 if (p == NULL)
626 p = Config.peers;
627
bf8fe701 628 debugs(15, 5, "neighborsUdpPing: Peer " << p->host);
62e76326 629
cb365059 630 if (!peerWouldBePinged(p, ps))
f53969cc 631 continue; /* next CachePeer */
62e76326 632
5db6bf73 633 ++peers_pinged;
62e76326 634
bf8fe701 635 debugs(15, 4, "neighborsUdpPing: pinging peer " << p->host << " for '" << url << "'");
62e76326 636
bf8fe701 637 debugs(15, 3, "neighborsUdpPing: key = '" << entry->getMD5Text() << "'");
62e76326 638
bf8fe701 639 debugs(15, 3, "neighborsUdpPing: reqnum = " << reqnum);
090089c4 640
dc9d133b 641#if USE_HTCP
4f4fa815 642 if (p->options.htcp && !p->options.htcp_only_clr) {
bebf08ff
AJ
643 if (Config.Port.htcp <= 0) {
644 debugs(15, DBG_CRITICAL, "HTCP is disabled! Cannot send HTCP request to peer.");
645 continue;
646 }
647
bf8fe701 648 debugs(15, 3, "neighborsUdpPing: sending HTCP query");
65d448bc
AJ
649 if (htcpQuery(entry, request, p) <= 0)
650 continue; // unable to send.
62e76326 651 } else
dc9d133b 652#endif
bebf08ff 653 {
e0d28505 654 if (Config.Port.icp <= 0 || !Comm::IsConnOpen(icpOutgoingConn)) {
bebf08ff
AJ
655 debugs(15, DBG_CRITICAL, "ICP is disabled! Cannot send ICP request to peer.");
656 continue;
62e76326 657 } else {
62e76326 658
bebf08ff 659 if (p->type == PEER_MULTICAST)
e0d28505 660 mcastSetTtl(icpOutgoingConn->fd, p->mcast.ttl);
bebf08ff
AJ
661
662 if (p->icp.port == echo_port) {
663 debugs(15, 4, "neighborsUdpPing: Looks like a dumb cache, send DECHO ping");
d2a6dcba
EB
664 // TODO: Get ALE from callback_data if possible.
665 icpCreateAndSend(ICP_DECHO, 0, url, reqnum, 0,
666 icpOutgoingConn->fd, p->in_addr, nullptr);
bebf08ff
AJ
667 } else {
668 flags = 0;
62e76326 669
bebf08ff
AJ
670 if (Config.onoff.query_icmp)
671 if (p->icp.version == ICP_VERSION_2)
672 flags |= ICP_FLAG_SRC_RTT;
62e76326 673
d2a6dcba
EB
674 // TODO: Get ALE from callback_data if possible.
675 icpCreateAndSend(ICP_QUERY, flags, url, reqnum, 0,
676 icpOutgoingConn->fd, p->in_addr, nullptr);
bebf08ff 677 }
62e76326 678 }
bebf08ff 679 }
62e76326 680
5db6bf73 681 ++queries_sent;
62e76326 682
5db6bf73 683 ++ p->stats.pings_sent;
62e76326 684
685 if (p->type == PEER_MULTICAST) {
1bf2cd49 686 mcast_exprep += p->mcast.n_replies_expected;
687 mcast_timeout += (p->stats.rtt * p->mcast.n_replies_expected);
62e76326 688 } else if (neighborUp(p)) {
689 /* its alive, expect a reply from it */
690
5c51bffb 691 if (neighborType(p, request->url) == PEER_PARENT) {
5db6bf73 692 ++parent_exprep;
62e76326 693 parent_timeout += p->stats.rtt;
694 } else {
5db6bf73 695 ++sibling_exprep;
62e76326 696 sibling_timeout += p->stats.rtt;
697 }
698 } else {
699 /* Neighbor is dead; ping it anyway, but don't expect a reply */
700 /* log it once at the threshold */
701
702 if (p->stats.logged_state == PEER_ALIVE) {
e0236918 703 debugs(15, DBG_IMPORTANT, "Detected DEAD " << neighborTypeStr(p) << ": " << p->name);
62e76326 704 p->stats.logged_state = PEER_DEAD;
705 }
706 }
707
708 p->stats.last_query = squid_curtime;
709
67c8308c 710 /*
a3c6762c
FC
711 * keep probe_start == 0 for a multicast CachePeer,
712 * so neighborUp() never says this CachePeer is dead.
67c8308c 713 */
714
715 if ((p->type != PEER_MULTICAST) && (p->stats.probe_start == 0))
62e76326 716 p->stats.probe_start = squid_curtime;
090089c4 717 }
62e76326 718
40a1495e 719 if ((first_ping = first_ping->next) == NULL)
62e76326 720 first_ping = Config.peers;
090089c4 721
a8c926ff 722 /*
723 * How many replies to expect?
724 */
1bf2cd49 725 *exprep = parent_exprep + sibling_exprep + mcast_exprep;
a8c926ff 726
52040193 727 /*
465dc415 728 * If there is a configured timeout, use it
52040193 729 */
465dc415 730 if (Config.Timeout.icp_query)
62e76326 731 *timeout = Config.Timeout.icp_query;
28993292 732 else {
62e76326 733 if (*exprep > 0) {
734 if (parent_exprep)
735 *timeout = 2 * parent_timeout / parent_exprep;
1bf2cd49 736 else if (mcast_exprep)
737 *timeout = 2 * mcast_timeout / mcast_exprep;
62e76326 738 else
739 *timeout = 2 * sibling_timeout / sibling_exprep;
740 } else
f53969cc 741 *timeout = 2000; /* 2 seconds */
62e76326 742
743 if (Config.Timeout.icp_query_max)
744 if (*timeout > Config.Timeout.icp_query_max)
745 *timeout = Config.Timeout.icp_query_max;
746
747 if (*timeout < Config.Timeout.icp_query_min)
748 *timeout = Config.Timeout.icp_query_min;
28993292 749 }
62e76326 750
0a0bf5db 751 return peers_pinged;
090089c4 752}
753
a3c6762c 754/* lookup the digest of a given CachePeer */
26b164ac 755lookup_t
cb365059 756peerDigestLookup(CachePeer * p, PeerSelector * ps)
26b164ac 757{
6cfa8966 758#if USE_CACHE_DIGESTS
cb365059
EB
759 assert(ps);
760 HttpRequest *request = ps->request;
f66a9ef4 761 const cache_key *key = request ? storeKeyPublicByRequest(request) : NULL;
26b164ac 762 assert(p);
0c511722 763 assert(request);
bf8fe701 764 debugs(15, 5, "peerDigestLookup: peer " << p->host);
4b4cd312 765 /* does the peeer have a valid digest? */
62e76326 766
e13ee7ad 767 if (!p->digest) {
bf8fe701 768 debugs(15, 5, "peerDigestLookup: gone!");
62e76326 769 return LOOKUP_NONE;
cb365059 770 } else if (!peerHTTPOkay(p, ps)) {
bf8fe701 771 debugs(15, 5, "peerDigestLookup: !peerHTTPOkay");
6755df2e 772 return LOOKUP_NONE;
e13ee7ad 773 } else if (!p->digest->flags.needed) {
bf8fe701 774 debugs(15, 5, "peerDigestLookup: note need");
62e76326 775 peerDigestNeeded(p->digest);
776 return LOOKUP_NONE;
4ed0e075 777 } else if (!p->digest->flags.usable) {
bf8fe701 778 debugs(15, 5, "peerDigestLookup: !ready && " << (p->digest->flags.requested ? "" : "!") << "requested");
62e76326 779 return LOOKUP_NONE;
26b164ac 780 }
62e76326 781
bf8fe701 782 debugs(15, 5, "peerDigestLookup: OK to lookup peer " << p->host);
e13ee7ad 783 assert(p->digest->cd);
26b164ac 784 /* does digest predict a hit? */
62e76326 785
6fc4e508 786 if (!p->digest->cd->contains(key))
62e76326 787 return LOOKUP_MISS;
788
bf8fe701 789 debugs(15, 5, "peerDigestLookup: peer " << p->host << " says HIT!");
62e76326 790
26b164ac 791 return LOOKUP_HIT;
62e76326 792
26b164ac 793#endif
62e76326 794
26b164ac 795 return LOOKUP_NONE;
796}
797
a3c6762c
FC
798/* select best CachePeer based on cache digests */
799CachePeer *
cb365059 800neighborsDigestSelect(PeerSelector *ps)
a54d3f8e 801{
a3c6762c 802 CachePeer *best_p = NULL;
6cfa8966 803#if USE_CACHE_DIGESTS
cb365059
EB
804 assert(ps);
805 HttpRequest *request = ps->request;
62e76326 806
a54d3f8e 807 int best_rtt = 0;
808 int choice_count = 0;
809 int ichoice_count = 0;
a3c6762c 810 CachePeer *p;
a54d3f8e 811 int p_rtt;
812 int i;
62e76326 813
45e5102d 814 if (!request->flags.hierarchical)
62e76326 815 return NULL;
816
e91e2a72 817 storeKeyPublicByRequest(request);
62e76326 818
a54d3f8e 819 for (i = 0, p = first_ping; i++ < Config.npeers; p = p->next) {
62e76326 820 lookup_t lookup;
821
822 if (!p)
823 p = Config.peers;
824
825 if (i == 1)
826 first_ping = p;
827
cb365059 828 lookup = peerDigestLookup(p, ps);
62e76326 829
830 if (lookup == LOOKUP_NONE)
831 continue;
832
5db6bf73 833 ++choice_count;
62e76326 834
835 if (lookup == LOOKUP_MISS)
836 continue;
837
838 p_rtt = netdbHostRtt(p->host);
839
bf8fe701 840 debugs(15, 5, "neighborsDigestSelect: peer " << p->host << " rtt: " << p_rtt);
62e76326 841
a3c6762c 842 /* is this CachePeer better than others in terms of rtt ? */
62e76326 843 if (!best_p || (p_rtt && p_rtt < best_rtt)) {
844 best_p = p;
845 best_rtt = p_rtt;
846
f53969cc 847 if (p_rtt) /* informative choice (aka educated guess) */
5db6bf73 848 ++ichoice_count;
62e76326 849
bf8fe701 850 debugs(15, 4, "neighborsDigestSelect: peer " << p->host << " leads with rtt " << best_rtt);
62e76326 851 }
a54d3f8e 852 }
62e76326 853
bf8fe701 854 debugs(15, 4, "neighborsDigestSelect: choices: " << choice_count << " (" << ichoice_count << ")");
26b164ac 855 peerNoteDigestLookup(request, best_p,
62e76326 856 best_p ? LOOKUP_HIT : (choice_count ? LOOKUP_MISS : LOOKUP_NONE));
a54d3f8e 857 request->hier.n_choices = choice_count;
858 request->hier.n_ichoices = ichoice_count;
859#endif
62e76326 860
a54d3f8e 861 return best_p;
862}
863
26b164ac 864void
a3c6762c 865peerNoteDigestLookup(HttpRequest * request, CachePeer * p, lookup_t lookup)
26b164ac 866{
6cfa8966 867#if USE_CACHE_DIGESTS
26b164ac 868 if (p)
0a84e4fb 869 strncpy(request->hier.cd_host, p->host, sizeof(request->hier.cd_host)-1);
26b164ac 870 else
62e76326 871 *request->hier.cd_host = '\0';
872
26b164ac 873 request->hier.cd_lookup = lookup;
4b981814 874 debugs(15, 4, "peerNoteDigestLookup: peer " << (p? p->host : "<none>") << ", lookup: " << lookup_t_str[lookup] );
26b164ac 875#endif
876}
877
d73844ca 878static void
ced8def3 879neighborAlive(CachePeer * p, const MemObject *, const icp_common_t * header)
4eda6afe 880{
32a47e3e 881 peerAlive(p);
5db6bf73 882 ++ p->stats.pings_acked;
62e76326 883
27cd7235 884 if ((icp_opcode) header->opcode <= ICP_END)
5db6bf73 885 ++ p->icp.counts[header->opcode];
62e76326 886
44f2f2e4 887 p->icp.version = (int) header->version;
888}
889
890static void
a3c6762c 891neighborUpdateRtt(CachePeer * p, MemObject * mem)
44f2f2e4 892{
d1b63fc8 893 int rtt, rtt_av_factor;
62e76326 894
d2cd6935 895 if (!mem)
62e76326 896 return;
897
44f2f2e4 898 if (!mem->start_ping.tv_sec)
62e76326 899 return;
900
44f2f2e4 901 rtt = tvSubMsec(mem->start_ping, current_time);
62e76326 902
1b6ef2d2 903 if (rtt < 1 || rtt > 10000)
62e76326 904 return;
905
d1b63fc8 906 rtt_av_factor = RTT_AV_FACTOR;
62e76326 907
d1b63fc8 908 if (p->options.weighted_roundrobin)
62e76326 909 rtt_av_factor = RTT_BACKGROUND_AV_FACTOR;
910
a98bcbee 911 p->stats.rtt = Math::intAverage(p->stats.rtt, rtt, p->stats.pings_acked, rtt_av_factor);
4eda6afe 912}
090089c4 913
399cabec 914#if USE_HTCP
915static void
ced8def3 916neighborAliveHtcp(CachePeer * p, const MemObject *, const HtcpReplyData * htcp)
399cabec 917{
32a47e3e 918 peerAlive(p);
5db6bf73
FC
919 ++ p->stats.pings_acked;
920 ++ p->htcp.counts[htcp->hit ? 1 : 0];
44f2f2e4 921 p->htcp.version = htcp->version;
399cabec 922}
62e76326 923
399cabec 924#endif
925
38792624 926static void
a3c6762c 927neighborCountIgnored(CachePeer * p)
a7e59001 928{
b69f7771 929 if (p == NULL)
62e76326 930 return;
931
5db6bf73 932 ++ p->stats.ignored_replies;
62e76326 933
5db6bf73 934 ++NLateReplies;
a7e59001 935}
936
a3c6762c 937static CachePeer *non_peers = NULL;
e102ebda 938
939static void
b7ac5457 940neighborIgnoreNonPeer(const Ip::Address &from, icp_opcode opcode)
e102ebda 941{
a3c6762c 942 CachePeer *np;
62e76326 943
26ac0430 944 for (np = non_peers; np; np = np->next) {
cc192b50 945 if (np->in_addr != from)
62e76326 946 continue;
947
4dd643d5 948 if (np->in_addr.port() != from.port())
62e76326 949 continue;
950
951 break;
e102ebda 952 }
62e76326 953
26ac0430 954 if (np == NULL) {
719815a0 955 np = new CachePeer;
cc192b50 956 np->in_addr = from;
4dd643d5 957 np->icp.port = from.port();
62e76326 958 np->type = PEER_NONE;
cc192b50 959 np->host = new char[MAX_IPSTRLEN];
4dd643d5 960 from.toStr(np->host,MAX_IPSTRLEN);
62e76326 961 np->next = non_peers;
962 non_peers = np;
e102ebda 963 }
62e76326 964
5db6bf73 965 ++ np->icp.counts[opcode];
62e76326 966
e4cc2fdf 967 if (isPowTen(++np->stats.ignored_replies))
e0236918 968 debugs(15, DBG_IMPORTANT, "WARNING: Ignored " << np->stats.ignored_replies << " replies from non-peer " << np->host);
e102ebda 969}
970
429fdbec 971/* ignoreMulticastReply
26ac0430 972 *
d6827718 973 * * We want to ignore replies from multicast peers if the
a3c6762c 974 * * cache_host_domain rules would normally prevent the CachePeer
d6827718 975 * * from being used
429fdbec 976 */
977static int
cb365059 978ignoreMulticastReply(CachePeer * p, PeerSelector * ps)
429fdbec 979{
b69f7771 980 if (p == NULL)
62e76326 981 return 0;
982
cd196bc8 983 if (!p->options.mcast_responder)
62e76326 984 return 0;
985
cb365059 986 if (peerHTTPOkay(p, ps))
62e76326 987 return 0;
988
429fdbec 989 return 1;
990}
991
4b981814
AJ
992/**
993 * I should attach these records to the entry. We take the first
429fdbec 994 * hit we get our wait until everyone misses. The timeout handler
995 * call needs to nip this shopping list or call one of the misses.
26ac0430 996 *
429fdbec 997 * If a hit process is already started, then sobeit
998 */
b8d8561b 999void
b7ac5457 1000neighborsUdpAck(const cache_key * key, icp_common_t * header, const Ip::Address &from)
090089c4 1001{
a3c6762c 1002 CachePeer *p = NULL;
5ad33356 1003 StoreEntry *entry;
1004 MemObject *mem = NULL;
b6c0e933 1005 peer_t ntype = PEER_NONE;
a7e59001 1006 icp_opcode opcode = (icp_opcode) header->opcode;
090089c4 1007
4a7a3d56 1008 debugs(15, 6, "neighborsUdpAck: opcode " << opcode << " '" << storeKeyText(key) << "'");
62e76326 1009
80d0fe08 1010 if ((entry = Store::Root().findCallbackXXX(key)))
62e76326 1011 mem = entry->mem_obj;
1012
b69f7771 1013 if ((p = whichPeer(from)))
62e76326 1014 neighborAlive(p, mem, header);
1015
27cd7235 1016 if (opcode > ICP_END)
62e76326 1017 return;
1018
4b981814 1019 const char *opcode_d = icp_opcode_str[opcode];
62e76326 1020
41587298 1021 if (p)
62e76326 1022 neighborUpdateRtt(p, mem);
1023
5ad33356 1024 /* Does the entry exist? */
26ac0430 1025 if (NULL == entry) {
bf8fe701 1026 debugs(12, 3, "neighborsUdpAck: Cache key '" << storeKeyText(key) << "' not found");
62e76326 1027 neighborCountIgnored(p);
1028 return;
5ad33356 1029 }
62e76326 1030
2d1c6a4f 1031 /* check if someone is already fetching it */
26ac0430 1032 if (EBIT_TEST(entry->flags, ENTRY_DISPATCHED)) {
bf8fe701 1033 debugs(15, 3, "neighborsUdpAck: '" << storeKeyText(key) << "' already being fetched.");
62e76326 1034 neighborCountIgnored(p);
1035 return;
d2af9477 1036 }
62e76326 1037
26ac0430 1038 if (mem == NULL) {
bf8fe701 1039 debugs(15, 2, "Ignoring " << opcode_d << " for missing mem_obj: " << storeKeyText(key));
62e76326 1040 neighborCountIgnored(p);
1041 return;
8de2f7ad 1042 }
62e76326 1043
26ac0430 1044 if (entry->ping_status != PING_WAITING) {
bf8fe701 1045 debugs(15, 2, "neighborsUdpAck: Late " << opcode_d << " for " << storeKeyText(key));
62e76326 1046 neighborCountIgnored(p);
1047 return;
8de2f7ad 1048 }
62e76326 1049
1bfe9ade 1050 if (!entry->locked()) {
5bd484b5 1051 // TODO: many entries are unlocked; why is this reported at level 1?
e0236918 1052 debugs(12, DBG_IMPORTANT, "neighborsUdpAck: '" << storeKeyText(key) << "' has no locks");
62e76326 1053 neighborCountIgnored(p);
1054 return;
090089c4 1055 }
62e76326 1056
cb365059
EB
1057 if (!mem->ircb_data) {
1058 debugs(12, DBG_IMPORTANT, "BUG: missing ICP callback data for " << *entry);
1059 neighborCountIgnored(p);
1060 return;
1061 }
1062
bf8fe701 1063 debugs(15, 3, "neighborsUdpAck: " << opcode_d << " for '" << storeKeyText(key) << "' from " << (p ? p->host : "source") << " ");
62e76326 1064
26ac0430 1065 if (p) {
5c51bffb 1066 ntype = neighborType(p, mem->request->url);
44f2f2e4 1067 }
62e76326 1068
cb365059 1069 if (ignoreMulticastReply(p, mem->ircb_data)) {
62e76326 1070 neighborCountIgnored(p);
26ac0430 1071 } else if (opcode == ICP_MISS) {
62e76326 1072 if (p == NULL) {
1073 neighborIgnoreNonPeer(from, opcode);
1074 } else {
0c3d3f65 1075 mem->ping_reply_callback(p, ntype, AnyP::PROTO_ICP, header, mem->ircb_data);
62e76326 1076 }
26ac0430 1077 } else if (opcode == ICP_HIT) {
62e76326 1078 if (p == NULL) {
1079 neighborIgnoreNonPeer(from, opcode);
1080 } else {
1081 header->opcode = ICP_HIT;
0c3d3f65 1082 mem->ping_reply_callback(p, ntype, AnyP::PROTO_ICP, header, mem->ircb_data);
62e76326 1083 }
26ac0430 1084 } else if (opcode == ICP_DECHO) {
62e76326 1085 if (p == NULL) {
1086 neighborIgnoreNonPeer(from, opcode);
1087 } else if (ntype == PEER_SIBLING) {
1088 debug_trap("neighborsUdpAck: Found non-ICP cache as SIBLING\n");
1089 debug_trap("neighborsUdpAck: non-ICP neighbors must be a PARENT\n");
1090 } else {
0c3d3f65 1091 mem->ping_reply_callback(p, ntype, AnyP::PROTO_ICP, header, mem->ircb_data);
62e76326 1092 }
26ac0430 1093 } else if (opcode == ICP_SECHO) {
62e76326 1094 if (p) {
e0236918 1095 debugs(15, DBG_IMPORTANT, "Ignoring SECHO from neighbor " << p->host);
62e76326 1096 neighborCountIgnored(p);
62e76326 1097 } else {
e0236918 1098 debugs(15, DBG_IMPORTANT, "Unsolicited SECHO from " << from);
62e76326 1099 }
26ac0430 1100 } else if (opcode == ICP_DENIED) {
62e76326 1101 if (p == NULL) {
1102 neighborIgnoreNonPeer(from, opcode);
1103 } else if (p->stats.pings_acked > 100) {
1104 if (100 * p->icp.counts[ICP_DENIED] / p->stats.pings_acked > 95) {
fa84c01d
FC
1105 debugs(15, DBG_CRITICAL, "95%% of replies from '" << p->host << "' are UDP_DENIED");
1106 debugs(15, DBG_CRITICAL, "Disabling '" << p->host << "', please check your configuration.");
62e76326 1107 neighborRemove(p);
1108 p = NULL;
1109 } else {
1110 neighborCountIgnored(p);
1111 }
1112 }
26ac0430 1113 } else if (opcode == ICP_MISS_NOFETCH) {
0c3d3f65 1114 mem->ping_reply_callback(p, ntype, AnyP::PROTO_ICP, header, mem->ircb_data);
26ac0430 1115 } else {
fa84c01d 1116 debugs(15, DBG_CRITICAL, "neighborsUdpAck: Unexpected ICP reply: " << opcode_d);
d2af9477 1117 }
090089c4 1118}
1119
a3c6762c 1120CachePeer *
40a1495e 1121peerFindByName(const char *name)
98ffb7e4 1122{
a3c6762c 1123 CachePeer *p = NULL;
62e76326 1124
40a1495e 1125 for (p = Config.peers; p; p = p->next) {
62e76326 1126 if (!strcasecmp(name, p->name))
1127 break;
98ffb7e4 1128 }
62e76326 1129
b69f7771 1130 return p;
98ffb7e4 1131}
b012353a 1132
a3c6762c 1133CachePeer *
db1cd23c 1134peerFindByNameAndPort(const char *name, unsigned short port)
1135{
a3c6762c 1136 CachePeer *p = NULL;
62e76326 1137
db1cd23c 1138 for (p = Config.peers; p; p = p->next) {
62e76326 1139 if (strcasecmp(name, p->name))
1140 continue;
1141
1142 if (port != p->http_port)
1143 continue;
1144
1145 break;
db1cd23c 1146 }
62e76326 1147
db1cd23c 1148 return p;
1149}
1150
5269d0bd 1151int
a3c6762c 1152neighborUp(const CachePeer * p)
5269d0bd 1153{
eb406bb7 1154 if (!p->tcp_up) {
8fcf19ec
EB
1155 peerProbeConnect(const_cast<CachePeer*>(p));
1156 return 0;
eb406bb7 1157 }
62e76326 1158
64b8103b 1159 /*
a3c6762c 1160 * The CachePeer can not be UP if we don't have any IP addresses
26ac0430 1161 * for it.
64b8103b 1162 */
cc192b50 1163 if (0 == p->n_addresses) {
1164 debugs(15, 8, "neighborUp: DOWN (no-ip): " << p->host << " (" << p->in_addr << ")");
62e76326 1165 return 0;
cc192b50 1166 }
62e76326 1167
cc192b50 1168 if (p->options.no_query) {
1169 debugs(15, 8, "neighborUp: UP (no-query): " << p->host << " (" << p->in_addr << ")");
67c8308c 1170 return 1;
cc192b50 1171 }
67c8308c 1172
1173 if (p->stats.probe_start != 0 &&
cc192b50 1174 squid_curtime - p->stats.probe_start > Config.Timeout.deadPeer) {
1175 debugs(15, 8, "neighborUp: DOWN (dead): " << p->host << " (" << p->in_addr << ")");
67c8308c 1176 return 0;
cc192b50 1177 }
67c8308c 1178
cc192b50 1179 debugs(15, 8, "neighborUp: UP: " << p->host << " (" << p->in_addr << ")");
03b82057 1180 return 1;
5269d0bd 1181}
1182
0ce8e93b
EB
1183/// \returns the effective connect timeout for this peer
1184time_t
1185peerConnectTimeout(const CachePeer *peer)
1186{
1187 return peer->connect_timeout_raw > 0 ?
1188 peer->connect_timeout_raw : Config.Timeout.peer_connect;
1189}
1190
1191time_t
1192positiveTimeout(const time_t timeout)
1193{
1194 return max(static_cast<time_t>(1), timeout);
1195}
1196
dd4bd44e 1197static void
4a3b98d7 1198peerDNSConfigure(const ipcache_addrs *ia, const Dns::LookupDetails &, void *data)
dd4bd44e 1199{
e8dca475
CT
1200 // TODO: connections to no-longer valid IP addresses should be
1201 // closed when we can detect such IP addresses.
1202
a3c6762c 1203 CachePeer *p = (CachePeer *)data;
62e76326 1204
b69f7771 1205 if (p->n_addresses == 0) {
e0236918 1206 debugs(15, DBG_IMPORTANT, "Configuring " << neighborTypeStr(p) << " " << p->host << "/" << p->http_port << "/" << p->icp.port);
62e76326 1207
1208 if (p->type == PEER_MULTICAST)
e0236918 1209 debugs(15, DBG_IMPORTANT, " Multicast TTL = " << p->mcast.ttl);
b69f7771 1210 }
62e76326 1211
b69f7771 1212 p->n_addresses = 0;
62e76326 1213
dd4bd44e 1214 if (ia == NULL) {
fa84c01d 1215 debugs(0, DBG_CRITICAL, "WARNING: DNS lookup for '" << p->host << "' failed!");
62e76326 1216 return;
dd4bd44e 1217 }
62e76326 1218
fd9c47d1 1219 if (ia->empty()) {
fa84c01d 1220 debugs(0, DBG_CRITICAL, "WARNING: No IP address found for '" << p->host << "'!");
62e76326 1221 return;
dd4bd44e 1222 }
62e76326 1223
fd9c47d1
AR
1224 for (const auto &ip: ia->goodAndBad()) { // TODO: Consider using just good().
1225 if (p->n_addresses < PEER_MAX_ADDRESSES) {
1226 const auto idx = p->n_addresses++;
1227 p->addresses[idx] = ip;
1228 debugs(15, 2, "--> IP address #" << idx << ": " << p->addresses[idx]);
1229 } else {
1230 debugs(15, 3, "ignoring remaining " << (ia->size() - p->n_addresses) << " ips");
1231 break;
1232 }
dd4bd44e 1233 }
62e76326 1234
4dd643d5 1235 p->in_addr.setEmpty();
cc192b50 1236 p->in_addr = p->addresses[0];
4dd643d5 1237 p->in_addr.port(p->icp.port);
62e76326 1238
883b9a4d
AR
1239 peerProbeConnect(p, true); // detect any died or revived peers ASAP
1240
b69f7771 1241 if (p->type == PEER_MULTICAST)
62e76326 1242 peerCountMcastPeersSchedule(p, 10);
1243
9b5c4a9a 1244#if USE_ICMP
e51c8197 1245 if (p->type != PEER_MULTICAST && IamWorkerProcess())
62e76326 1246 if (!p->options.no_netdb_exchange)
1247 eventAddIsh("netdbExchangeStart", netdbExchangeStart, p, 30.0, 1);
9b5c4a9a
AJ
1248#endif
1249
e8dca475
CT
1250 if (p->standby.mgr.valid())
1251 PeerPoolMgr::Checkpoint(p->standby.mgr, "resolved peer");
dd4bd44e 1252}
1253
1254static void
51242273 1255peerRefreshDNS(void *data)
dd4bd44e 1256{
a3c6762c 1257 CachePeer *p = NULL;
62e76326 1258
46ca5fc6 1259 if (eventFind(peerRefreshDNS, NULL))
62e76326 1260 eventDelete(peerRefreshDNS, NULL);
1261
51242273 1262 if (!data && 0 == stat5minClientRequests()) {
62e76326 1263 /* no recent client traffic, wait a bit */
1264 eventAddIsh("peerRefreshDNS", peerRefreshDNS, NULL, 180.0, 1);
1265 return;
dd4bd44e 1266 }
62e76326 1267
1f3c4622 1268 for (p = Config.peers; p; p = p->next)
62e76326 1269 ipcache_nbgethostbyname(p->host, peerDNSConfigure, p);
1270
dd4bd44e 1271 /* Reconfigure the peers every hour */
52040193 1272 eventAddIsh("peerRefreshDNS", peerRefreshDNS, NULL, 3600.0, 1);
dd4bd44e 1273}
e924600d 1274
4ed0e075 1275static void
a3c6762c 1276peerConnectFailedSilent(CachePeer * p)
eb406bb7 1277{
1278 p->stats.last_connect_failure = squid_curtime;
62e76326 1279
eb406bb7 1280 if (!p->tcp_up) {
bf8fe701 1281 debugs(15, 2, "TCP connection to " << p->host << "/" << p->http_port <<
1282 " dead");
62e76326 1283 return;
eb406bb7 1284 }
62e76326 1285
5e263176 1286 -- p->tcp_up;
62e76326 1287
eb406bb7 1288 if (!p->tcp_up) {
e0236918 1289 debugs(15, DBG_IMPORTANT, "Detected DEAD " << neighborTypeStr(p) << ": " << p->name);
62e76326 1290 p->stats.logged_state = PEER_DEAD;
eb406bb7 1291 }
1292}
1293
4ed0e075 1294void
a3c6762c 1295peerConnectFailed(CachePeer *p)
4ed0e075 1296{
e0236918 1297 debugs(15, DBG_IMPORTANT, "TCP connection to " << p->host << "/" << p->http_port << " failed");
4ed0e075 1298 peerConnectFailedSilent(p);
1299}
1300
eb406bb7 1301void
a3c6762c 1302peerConnectSucceded(CachePeer * p)
eb406bb7 1303{
1304 if (!p->tcp_up) {
61beade2 1305 debugs(15, 2, "TCP connection to " << p->host << "/" << p->http_port << " succeeded");
ff9970cc 1306 p->tcp_up = p->connect_fail_limit; // NP: so peerAlive(p) works properly.
32a47e3e 1307 peerAlive(p);
26ac0430
AJ
1308 if (!p->n_addresses)
1309 ipcache_nbgethostbyname(p->host, peerDNSConfigure, p);
1310 } else
ff9970cc 1311 p->tcp_up = p->connect_fail_limit;
eb406bb7 1312}
1313
883b9a4d
AR
1314/// whether new TCP probes are currently banned
1315static bool
1316peerProbeIsBusy(const CachePeer *p)
1317{
1318 if (p->testing_now > 0) {
1319 debugs(15, 8, "yes, probing " << p);
1320 return true;
1321 }
1322 if (squid_curtime - p->stats.last_connect_probe == 0) {
1323 debugs(15, 8, "yes, just probed " << p);
1324 return true;
1325 }
1326 return false;
1327}
4ed0e075 1328/*
26ac0430 1329* peerProbeConnect will be called on dead peers by neighborUp
4ed0e075 1330*/
8fcf19ec 1331static void
883b9a4d 1332peerProbeConnect(CachePeer *p, const bool reprobeIfBusy)
e924600d 1333{
883b9a4d
AR
1334 if (peerProbeIsBusy(p)) {
1335 p->reprobe = reprobeIfBusy;
8fcf19ec
EB
1336 return;
1337 }
883b9a4d 1338 p->reprobe = false;
62e76326 1339
8fcf19ec 1340 const time_t ctimeout = peerConnectTimeout(p);
a3c6762c 1341 /* for each IP address of this CachePeer. find one that we can connect to and probe it. */
5db6bf73 1342 for (int i = 0; i < p->n_addresses; ++i) {
f9b72e0c 1343 Comm::ConnectionPointer conn = new Comm::Connection;
cfd66529 1344 conn->remote = p->addresses[i];
4dd643d5 1345 conn->remote.port(p->http_port);
06c40c46 1346 conn->setPeer(p);
cfd66529 1347 getOutgoingAddress(NULL, conn);
cc192b50 1348
5db6bf73 1349 ++ p->testing_now;
715b5def 1350
aed188fd 1351 AsyncCall::Pointer call = commCbCall(15,3, "peerProbeConnectDone", CommConnectCbPtrFun(peerProbeConnectDone, p));
4accd6d8 1352 Comm::ConnOpener *cs = new Comm::ConnOpener(conn, call, ctimeout);
aed188fd 1353 cs->setHost(p->host);
855150a4 1354 AsyncJob::Start(cs);
715b5def
AJ
1355 }
1356
eb406bb7 1357 p->stats.last_connect_probe = squid_curtime;
e924600d 1358}
1359
1360static void
ced8def3 1361peerProbeConnectDone(const Comm::ConnectionPointer &conn, Comm::Flag status, int, void *data)
e924600d 1362{
a3c6762c 1363 CachePeer *p = (CachePeer*)data;
62e76326 1364
c8407295 1365 if (status == Comm::OK) {
62e76326 1366 peerConnectSucceded(p);
e924600d 1367 } else {
4ed0e075 1368 peerConnectFailedSilent(p);
e924600d 1369 }
62e76326 1370
5e263176 1371 -- p->testing_now;
db9bf30d
AJ
1372 conn->close();
1373 // TODO: log this traffic.
883b9a4d
AR
1374
1375 if (p->reprobe)
1376 peerProbeConnect(p);
e924600d 1377}
1378
429fdbec 1379static void
a3c6762c 1380peerCountMcastPeersSchedule(CachePeer * p, time_t when)
429fdbec 1381{
b515fc11 1382 if (p->mcast.flags.count_event_pending)
62e76326 1383 return;
1384
429fdbec 1385 eventAdd("peerCountMcastPeersStart",
62e76326 1386 peerCountMcastPeersStart,
1387 p,
1388 (double) when, 1);
1389
ccdf4138 1390 p->mcast.flags.count_event_pending = true;
429fdbec 1391}
1392
1393static void
1394peerCountMcastPeersStart(void *data)
1395{
6043e368
AR
1396 // XXX: Do not create lots of complex fake objects (while abusing their
1397 // APIs) to pass around a few basic data points like start_ping and ping!
a3c6762c 1398 CachePeer *p = (CachePeer *)data;
429fdbec 1399 MemObject *mem;
007b8be4 1400 int reqnum;
c8ab5ec6 1401 // TODO: use class AnyP::Uri instead of constructing and re-parsing a string
429fdbec 1402 LOCAL_ARRAY(char, url, MAX_URL);
ce66013b 1403 assert(p->type == PEER_MULTICAST);
ccdf4138 1404 p->mcast.flags.count_event_pending = false;
cc192b50 1405 snprintf(url, MAX_URL, "http://");
4dd643d5 1406 p->in_addr.toUrl(url+7, MAX_URL -8 );
cc192b50 1407 strcat(url, "/");
5ceaee75 1408 const MasterXaction::Pointer mx = new MasterXaction(XactionInitiator::initPeerMcast);
6c880a16 1409 auto *req = HttpRequest::FromUrlXXX(url, mx);
8babada0
AJ
1410 assert(req != nullptr);
1411 StoreEntry *fake = storeCreateEntry(url, url, RequestFlags(), Http::METHOD_GET);
cb835136 1412 const auto psstate = new PeerSelector(nullptr);
b248c2a3
AJ
1413 psstate->request = req;
1414 HTTPMSGLOCK(psstate->request);
429fdbec 1415 psstate->entry = fake;
6043e368 1416 psstate->peerCountMcastPeerXXX = cbdataReference(p);
b4e7f82d 1417 psstate->ping.start = current_time;
429fdbec 1418 mem = fake->mem_obj;
b248c2a3 1419 mem->request = psstate->request;
429fdbec 1420 mem->start_ping = current_time;
b4e7f82d 1421 mem->ping_reply_callback = peerCountHandleIcpReply;
429fdbec 1422 mem->ircb_data = psstate;
e0d28505 1423 mcastSetTtl(icpOutgoingConn->fd, p->mcast.ttl);
007b8be4 1424 p->mcast.id = mem->id;
332dafa2 1425 reqnum = icpSetCacheKey((const cache_key *)fake->key);
d2a6dcba
EB
1426 icpCreateAndSend(ICP_QUERY, 0, url, reqnum, 0,
1427 icpOutgoingConn->fd, p->in_addr, psstate->al);
429fdbec 1428 fake->ping_status = PING_WAITING;
1429 eventAdd("peerCountMcastPeersDone",
62e76326 1430 peerCountMcastPeersDone,
1431 psstate,
99117d82 1432 Config.Timeout.mcast_icp_query / 1000.0, 1);
ccdf4138 1433 p->mcast.flags.counting = true;
429fdbec 1434 peerCountMcastPeersSchedule(p, MCAST_COUNT_RATE);
1435}
1436
1437static void
1438peerCountMcastPeersDone(void *data)
1439{
cb835136 1440 const auto psstate = static_cast<PeerSelector*>(data);
429fdbec 1441 StoreEntry *fake = psstate->entry;
99117d82 1442
6043e368
AR
1443 if (cbdataReferenceValid(psstate->peerCountMcastPeerXXX)) {
1444 CachePeer *p = (CachePeer *)psstate->peerCountMcastPeerXXX;
ccdf4138 1445 p->mcast.flags.counting = false;
a98bcbee 1446 p->mcast.avg_n_members = Math::doubleAverage(p->mcast.avg_n_members, (double) psstate->ping.n_recv, ++p->mcast.n_times_counted, 10);
e0236918 1447 debugs(15, DBG_IMPORTANT, "Group " << p->host << ": " << psstate->ping.n_recv <<
bf8fe701 1448 " replies, "<< std::setw(4)<< std::setprecision(2) <<
1449 p->mcast.avg_n_members <<" average, RTT " << p->stats.rtt);
99117d82 1450 p->mcast.n_replies_expected = (int) p->mcast.avg_n_members;
1451 }
1452
6043e368 1453 cbdataReferenceDone(psstate->peerCountMcastPeerXXX);
99117d82 1454
bc52369a 1455 fake->abort(); // sets ENTRY_ABORTED and initiates releated cleanup
c43405e7 1456 fake->mem_obj->request = nullptr;
acc5dc4c 1457 fake->unlock("peerCountMcastPeersDone");
bd6e2f16 1458 delete psstate;
429fdbec 1459}
1460
1461static void
ced8def3 1462peerCountHandleIcpReply(CachePeer * p, peer_t, AnyP::ProtocolType proto, void *, void *data)
429fdbec 1463{
cb835136 1464 const auto psstate = static_cast<PeerSelector*>(data);
1a827bc0 1465 StoreEntry *fake = psstate->entry;
ced8def3 1466 assert(fake);
1a827bc0 1467 MemObject *mem = fake->mem_obj;
ced8def3 1468 assert(mem);
1a827bc0 1469 int rtt = tvSubMsec(mem->start_ping, current_time);
0c3d3f65 1470 assert(proto == AnyP::PROTO_ICP);
5db6bf73 1471 ++ psstate->ping.n_recv;
ced8def3 1472 int rtt_av_factor = RTT_AV_FACTOR;
62e76326 1473
d1b63fc8 1474 if (p->options.weighted_roundrobin)
62e76326 1475 rtt_av_factor = RTT_BACKGROUND_AV_FACTOR;
1476
a98bcbee 1477 p->stats.rtt = Math::intAverage(p->stats.rtt, rtt, psstate->ping.n_recv, rtt_av_factor);
429fdbec 1478}
ed7f5615 1479
1480static void
1481neighborDumpPeers(StoreEntry * sentry)
1482{
1483 dump_peers(sentry, Config.peers);
1484}
1485
1486static void
1487neighborDumpNonPeers(StoreEntry * sentry)
1488{
1489 dump_peers(sentry, non_peers);
1490}
1491
a369131d 1492void
a3c6762c 1493dump_peer_options(StoreEntry * sentry, CachePeer * p)
a369131d 1494{
cd196bc8 1495 if (p->options.proxy_only)
62e76326 1496 storeAppendPrintf(sentry, " proxy-only");
1497
cd196bc8 1498 if (p->options.no_query)
62e76326 1499 storeAppendPrintf(sentry, " no-query");
1500
d1b63fc8 1501 if (p->options.background_ping)
62e76326 1502 storeAppendPrintf(sentry, " background-ping");
1503
cd196bc8 1504 if (p->options.no_digest)
62e76326 1505 storeAppendPrintf(sentry, " no-digest");
1506
cd196bc8 1507 if (p->options.default_parent)
62e76326 1508 storeAppendPrintf(sentry, " default");
1509
cd196bc8 1510 if (p->options.roundrobin)
62e76326 1511 storeAppendPrintf(sentry, " round-robin");
1512
f7e1d9ce
HN
1513 if (p->options.carp)
1514 storeAppendPrintf(sentry, " carp");
354b5a2b 1515
2f1431ea 1516#if USE_AUTH
f7e1d9ce
HN
1517 if (p->options.userhash)
1518 storeAppendPrintf(sentry, " userhash");
354b5a2b 1519#endif
f7e1d9ce 1520
354b5a2b 1521 if (p->options.sourcehash)
f7e1d9ce 1522 storeAppendPrintf(sentry, " sourcehash");
354b5a2b 1523
d1b63fc8 1524 if (p->options.weighted_roundrobin)
62e76326 1525 storeAppendPrintf(sentry, " weighted-round-robin");
1526
cd196bc8 1527 if (p->options.mcast_responder)
62e76326 1528 storeAppendPrintf(sentry, " multicast-responder");
1529
8a368316
AJ
1530#if PEER_MULTICAST_SIBLINGS
1531 if (p->options.mcast_siblings)
1532 storeAppendPrintf(sentry, " multicast-siblings");
1533#endif
1534
678a066c 1535 if (p->weight != 1)
62e76326 1536 storeAppendPrintf(sentry, " weight=%d", p->weight);
1537
cd196bc8 1538 if (p->options.closest_only)
62e76326 1539 storeAppendPrintf(sentry, " closest-only");
1540
dc9d133b 1541#if USE_HTCP
18191440 1542 if (p->options.htcp) {
62e76326 1543 storeAppendPrintf(sentry, " htcp");
18191440 1544 if (p->options.htcp_oldsquid || p->options.htcp_no_clr || p->options.htcp_no_purge_clr || p->options.htcp_only_clr) {
4ac1880a
FC
1545 bool doneopts = false;
1546 if (p->options.htcp_oldsquid) {
1547 storeAppendPrintf(sentry, "oldsquid");
1548 doneopts = true;
1549 }
1550 if (p->options.htcp_no_clr) {
1551 storeAppendPrintf(sentry, "%sno-clr",(doneopts?",":"="));
1552 doneopts = true;
1553 }
1554 if (p->options.htcp_no_purge_clr) {
1555 storeAppendPrintf(sentry, "%sno-purge-clr",(doneopts?",":"="));
1556 doneopts = true;
1557 }
1558 if (p->options.htcp_only_clr) {
1559 storeAppendPrintf(sentry, "%sonly-clr",(doneopts?",":"="));
1560 //doneopts = true; // uncomment if more opts are added
1561 }
18191440
AJ
1562 }
1563 }
dc9d133b 1564#endif
62e76326 1565
cd196bc8 1566 if (p->options.no_netdb_exchange)
62e76326 1567 storeAppendPrintf(sentry, " no-netdb-exchange");
1568
9a0a18de 1569#if USE_DELAY_POOLS
cd196bc8 1570 if (p->options.no_delay)
62e76326 1571 storeAppendPrintf(sentry, " no-delay");
95e36d02 1572#endif
62e76326 1573
c68e9c6b 1574 if (p->login)
62e76326 1575 storeAppendPrintf(sentry, " login=%s", p->login);
1576
a369131d 1577 if (p->mcast.ttl > 0)
62e76326 1578 storeAppendPrintf(sentry, " ttl=%d", p->mcast.ttl);
1579
0ce8e93b
EB
1580 if (p->connect_timeout_raw > 0)
1581 storeAppendPrintf(sentry, " connect-timeout=%d", (int)p->connect_timeout_raw);
62e76326 1582
ff9970cc
AJ
1583 if (p->connect_fail_limit != PEER_TCP_MAGIC_COUNT)
1584 storeAppendPrintf(sentry, " connect-fail-limit=%d", p->connect_fail_limit);
1585
be753325 1586#if USE_CACHE_DIGESTS
62e76326 1587
be753325 1588 if (p->digest_url)
62e76326 1589 storeAppendPrintf(sentry, " digest-url=%s", p->digest_url);
1590
be753325 1591#endif
62e76326 1592
be753325 1593 if (p->options.allow_miss)
62e76326 1594 storeAppendPrintf(sentry, " allow-miss");
1595
b0758e04
AJ
1596 if (p->options.no_tproxy)
1597 storeAppendPrintf(sentry, " no-tproxy");
1598
be753325 1599 if (p->max_conn > 0)
62e76326 1600 storeAppendPrintf(sentry, " max-conn=%d", p->max_conn);
e8dca475
CT
1601 if (p->standby.limit > 0)
1602 storeAppendPrintf(sentry, " standby=%d", p->standby.limit);
62e76326 1603
be753325 1604 if (p->options.originserver)
62e76326 1605 storeAppendPrintf(sentry, " originserver");
1606
be753325 1607 if (p->domain)
62e76326 1608 storeAppendPrintf(sentry, " forceddomain=%s", p->domain);
1609
26ac0430
AJ
1610 if (p->connection_auth == 0)
1611 storeAppendPrintf(sentry, " connection-auth=off");
1612 else if (p->connection_auth == 1)
1613 storeAppendPrintf(sentry, " connection-auth=on");
1614 else if (p->connection_auth == 2)
1615 storeAppendPrintf(sentry, " connection-auth=auto");
d67acb4e 1616
8250ca31 1617 p->secure.dumpCfg(sentry,"tls-");
a369131d 1618 storeAppendPrintf(sentry, "\n");
1619}
1620
ed7f5615 1621static void
a3c6762c 1622dump_peers(StoreEntry * sentry, CachePeer * peers)
ed7f5615 1623{
cc192b50 1624 char ntoabuf[MAX_IPSTRLEN];
06e6d12a 1625 int i;
62e76326 1626
ed7f5615 1627 if (peers == NULL)
62e76326 1628 storeAppendPrintf(sentry, "There are no neighbors installed.\n");
1629
f1a5d071 1630 for (CachePeer *e = peers; e; e = e->next) {
62e76326 1631 assert(e->host != NULL);
1632 storeAppendPrintf(sentry, "\n%-11.11s: %s\n",
1633 neighborTypeStr(e),
1634 e->name);
1635 storeAppendPrintf(sentry, "Host : %s/%d/%d\n",
1636 e->host,
1637 e->http_port,
1638 e->icp.port);
1639 storeAppendPrintf(sentry, "Flags :");
1640 dump_peer_options(sentry, e);
1641
5db6bf73 1642 for (i = 0; i < e->n_addresses; ++i) {
62e76326 1643 storeAppendPrintf(sentry, "Address[%d] : %s\n", i,
4dd643d5 1644 e->addresses[i].toStr(ntoabuf,MAX_IPSTRLEN) );
62e76326 1645 }
1646
1647 storeAppendPrintf(sentry, "Status : %s\n",
1648 neighborUp(e) ? "Up" : "Down");
1649 storeAppendPrintf(sentry, "FETCHES : %d\n", e->stats.fetches);
1650 storeAppendPrintf(sentry, "OPEN CONNS : %d\n", e->stats.conn_open);
1651 storeAppendPrintf(sentry, "AVG RTT : %d msec\n", e->stats.rtt);
1652
1653 if (!e->options.no_query) {
1654 storeAppendPrintf(sentry, "LAST QUERY : %8d seconds ago\n",
1655 (int) (squid_curtime - e->stats.last_query));
1656
1657 if (e->stats.last_reply > 0)
1658 storeAppendPrintf(sentry, "LAST REPLY : %8d seconds ago\n",
1659 (int) (squid_curtime - e->stats.last_reply));
1660 else
1661 storeAppendPrintf(sentry, "LAST REPLY : none received\n");
1662
1663 storeAppendPrintf(sentry, "PINGS SENT : %8d\n", e->stats.pings_sent);
1664
1665 storeAppendPrintf(sentry, "PINGS ACKED: %8d %3d%%\n",
1666 e->stats.pings_acked,
a98bcbee 1667 Math::intPercent(e->stats.pings_acked, e->stats.pings_sent));
62e76326 1668 }
1669
a98bcbee 1670 storeAppendPrintf(sentry, "IGNORED : %8d %3d%%\n", e->stats.ignored_replies, Math::intPercent(e->stats.ignored_replies, e->stats.pings_acked));
62e76326 1671
1672 if (!e->options.no_query) {
1673 storeAppendPrintf(sentry, "Histogram of PINGS ACKED:\n");
1994bb24 1674#if USE_HTCP
62e76326 1675
1676 if (e->options.htcp) {
1677 storeAppendPrintf(sentry, "\tMisses\t%8d %3d%%\n",
1678 e->htcp.counts[0],
a98bcbee 1679 Math::intPercent(e->htcp.counts[0], e->stats.pings_acked));
62e76326 1680 storeAppendPrintf(sentry, "\tHits\t%8d %3d%%\n",
1681 e->htcp.counts[1],
a98bcbee 1682 Math::intPercent(e->htcp.counts[1], e->stats.pings_acked));
62e76326 1683 } else {
1994bb24 1684#endif
62e76326 1685
2fefc43c 1686 for (auto op : WholeEnum<icp_opcode>()) {
62e76326 1687 if (e->icp.counts[op] == 0)
1688 continue;
1689
1690 storeAppendPrintf(sentry, " %12.12s : %8d %3d%%\n",
1691 icp_opcode_str[op],
1692 e->icp.counts[op],
a98bcbee 1693 Math::intPercent(e->icp.counts[op], e->stats.pings_acked));
62e76326 1694 }
1695
1994bb24 1696#if USE_HTCP
62e76326 1697
1698 }
1699
1994bb24 1700#endif
62e76326 1701
1702 }
1703
1704 if (e->stats.last_connect_failure) {
1705 storeAppendPrintf(sentry, "Last failed connect() at: %s\n",
20efa1c2 1706 Time::FormatHttpd(e->stats.last_connect_failure));
62e76326 1707 }
1708
a98bcbee 1709 storeAppendPrintf(sentry, "keep-alive ratio: %d%%\n", Math::intPercent(e->stats.n_keepalives_recv, e->stats.n_keepalives_sent));
ed7f5615 1710 }
1711}
86aebcda 1712
1713#if USE_HTCP
1714void
fad2588a 1715neighborsHtcpReply(const cache_key * key, HtcpReplyData * htcp, const Ip::Address &from)
86aebcda 1716{
80d0fe08 1717 StoreEntry *e = Store::Root().findCallbackXXX(key);
399cabec 1718 MemObject *mem = NULL;
a3c6762c 1719 CachePeer *p;
399cabec 1720 peer_t ntype = PEER_NONE;
26ac0430
AJ
1721 debugs(15, 6, "neighborsHtcpReply: " <<
1722 (htcp->hit ? "HIT" : "MISS") << " " <<
1723 storeKeyText(key) );
62e76326 1724
88e13392 1725 if (NULL != e)
62e76326 1726 mem = e->mem_obj;
1727
399cabec 1728 if ((p = whichPeer(from)))
62e76326 1729 neighborAliveHtcp(p, mem, htcp);
1730
399cabec 1731 /* Does the entry exist? */
26ac0430 1732 if (NULL == e) {
bf8fe701 1733 debugs(12, 3, "neighyborsHtcpReply: Cache key '" << storeKeyText(key) << "' not found");
62e76326 1734 neighborCountIgnored(p);
1735 return;
399cabec 1736 }
62e76326 1737
399cabec 1738 /* check if someone is already fetching it */
26ac0430 1739 if (EBIT_TEST(e->flags, ENTRY_DISPATCHED)) {
bf8fe701 1740 debugs(15, 3, "neighborsUdpAck: '" << storeKeyText(key) << "' already being fetched.");
62e76326 1741 neighborCountIgnored(p);
1742 return;
399cabec 1743 }
62e76326 1744
26ac0430 1745 if (mem == NULL) {
bf8fe701 1746 debugs(15, 2, "Ignoring reply for missing mem_obj: " << storeKeyText(key));
62e76326 1747 neighborCountIgnored(p);
1748 return;
399cabec 1749 }
62e76326 1750
26ac0430 1751 if (e->ping_status != PING_WAITING) {
bf8fe701 1752 debugs(15, 2, "neighborsUdpAck: Entry " << storeKeyText(key) << " is not PING_WAITING");
62e76326 1753 neighborCountIgnored(p);
1754 return;
399cabec 1755 }
62e76326 1756
1bfe9ade 1757 if (!e->locked()) {
5bd484b5 1758 // TODO: many entries are unlocked; why is this reported at level 1?
e0236918 1759 debugs(12, DBG_IMPORTANT, "neighborsUdpAck: '" << storeKeyText(key) << "' has no locks");
62e76326 1760 neighborCountIgnored(p);
1761 return;
399cabec 1762 }
62e76326 1763
cb365059
EB
1764 if (!mem->ircb_data) {
1765 debugs(12, DBG_IMPORTANT, "BUG: missing HTCP callback data for " << *e);
1766 neighborCountIgnored(p);
1767 return;
1768 }
1769
26ac0430 1770 if (p) {
5c51bffb 1771 ntype = neighborType(p, mem->request->url);
62e76326 1772 neighborUpdateRtt(p, mem);
44f2f2e4 1773 }
62e76326 1774
cb365059 1775 if (ignoreMulticastReply(p, mem->ircb_data)) {
62e76326 1776 neighborCountIgnored(p);
1777 return;
399cabec 1778 }
62e76326 1779
bf8fe701 1780 debugs(15, 3, "neighborsHtcpReply: e = " << e);
ccfbe8f4 1781 // TODO: Refactor (ping_reply_callback,ircb_data) to add CodeContext.
0c3d3f65 1782 mem->ping_reply_callback(p, ntype, AnyP::PROTO_HTCP, htcp, mem->ircb_data);
86aebcda 1783}
62e76326 1784
1bd06eff
BR
1785/*
1786 * Send HTCP CLR messages to all peers configured to receive them.
1787 */
4f4fa815 1788void
8dceeee3 1789neighborsHtcpClear(StoreEntry * e, const char *uri, HttpRequest * req, const HttpRequestMethod &method, htcp_clr_reason reason)
4f4fa815 1790{
a3c6762c 1791 CachePeer *p;
4f4fa815
BR
1792 char buf[128];
1793
4f4fa815
BR
1794 for (p = Config.peers; p; p = p->next) {
1795 if (!p->options.htcp) {
1796 continue;
1797 }
1798 if (p->options.htcp_no_clr) {
1799 continue;
1800 }
1801 if (p->options.htcp_no_purge_clr && reason == HTCP_CLR_PURGE) {
1802 continue;
1803 }
4dd643d5 1804 debugs(15, 3, "neighborsHtcpClear: sending CLR to " << p->in_addr.toUrl(buf, 128));
4f4fa815
BR
1805 htcpClear(e, uri, req, method, p, reason);
1806 }
1807}
1808
86aebcda 1809#endif
f53969cc 1810