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