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