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