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