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