]> git.ipfire.org Git - thirdparty/squid.git/blob - src/neighbors.cc
icp clean-up and ipv6/ipv4 related fixes
[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 "ProtoPort.h"
35 #include "acl/FilledChecklist.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 u_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, 1, "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 http_port_list *s = NULL;
544 next = thisPeer->next;
545
546 if (0 != strcmp(thisPeer->host, me))
547 continue;
548
549 for (s = Config.Sockaddr.http; s; s = s->next) {
550 if (thisPeer->http_port != s->s.GetPort())
551 continue;
552
553 debugs(15, DBG_IMPORTANT, "WARNING: Peer looks like this host");
554
555 debugs(15, DBG_IMPORTANT, " Ignoring " <<
556 neighborTypeStr(thisPeer) << " " << thisPeer->host <<
557 "/" << thisPeer->http_port << "/" <<
558 thisPeer->icp.port);
559
560 neighborRemove(thisPeer);
561 }
562 }
563 }
564
565 peerRefreshDNS((void *) 1);
566
567 sep = getservbyname("echo", "udp");
568 echo_port = sep ? ntohs((u_short) sep->s_port) : 7;
569
570 first_ping = Config.peers;
571 }
572
573 int
574 neighborsUdpPing(HttpRequest * request,
575 StoreEntry * entry,
576 IRCB * callback,
577 void *callback_data,
578 int *exprep,
579 int *timeout)
580 {
581 const char *url = entry->url();
582 MemObject *mem = entry->mem_obj;
583 peer *p = NULL;
584 int i;
585 int reqnum = 0;
586 int flags;
587 icp_common_t *query;
588 int queries_sent = 0;
589 int peers_pinged = 0;
590 int parent_timeout = 0, parent_exprep = 0;
591 int sibling_timeout = 0, sibling_exprep = 0;
592 int mcast_timeout = 0, mcast_exprep = 0;
593
594 if (Config.peers == NULL)
595 return 0;
596
597 assert(entry->swap_status == SWAPOUT_NONE);
598
599 mem->start_ping = current_time;
600
601 mem->ping_reply_callback = callback;
602
603 mem->ircb_data = callback_data;
604
605 reqnum = icpSetCacheKey((const cache_key *)entry->key);
606
607 for (i = 0, p = first_ping; i++ < Config.npeers; p = p->next) {
608 if (p == NULL)
609 p = Config.peers;
610
611 debugs(15, 5, "neighborsUdpPing: Peer " << p->host);
612
613 if (!peerWouldBePinged(p, request))
614 continue; /* next peer */
615
616 peers_pinged++;
617
618 debugs(15, 4, "neighborsUdpPing: pinging peer " << p->host << " for '" << url << "'");
619
620 debugs(15, 3, "neighborsUdpPing: key = '" << entry->getMD5Text() << "'");
621
622 debugs(15, 3, "neighborsUdpPing: reqnum = " << reqnum);
623
624 #if USE_HTCP
625 if (p->options.htcp && !p->options.htcp_only_clr) {
626 if (Config.Port.htcp <= 0) {
627 debugs(15, DBG_CRITICAL, "HTCP is disabled! Cannot send HTCP request to peer.");
628 continue;
629 }
630
631 debugs(15, 3, "neighborsUdpPing: sending HTCP query");
632 if (htcpQuery(entry, request, p) <= 0) 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, 1, "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, 1, "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 debugs(12, 1, "neighborsUdpAck: '" << storeKeyText(key) << "' has no locks");
1029 neighborCountIgnored(p);
1030 return;
1031 }
1032
1033 debugs(15, 3, "neighborsUdpAck: " << opcode_d << " for '" << storeKeyText(key) << "' from " << (p ? p->host : "source") << " ");
1034
1035 if (p) {
1036 ntype = neighborType(p, mem->request);
1037 }
1038
1039 if (ignoreMulticastReply(p, mem)) {
1040 neighborCountIgnored(p);
1041 } else if (opcode == ICP_MISS) {
1042 if (p == NULL) {
1043 neighborIgnoreNonPeer(from, opcode);
1044 } else {
1045 mem->ping_reply_callback(p, ntype, AnyP::PROTO_ICP, header, mem->ircb_data);
1046 }
1047 } else if (opcode == ICP_HIT) {
1048 if (p == NULL) {
1049 neighborIgnoreNonPeer(from, opcode);
1050 } else {
1051 header->opcode = ICP_HIT;
1052 mem->ping_reply_callback(p, ntype, AnyP::PROTO_ICP, header, mem->ircb_data);
1053 }
1054 } else if (opcode == ICP_DECHO) {
1055 if (p == NULL) {
1056 neighborIgnoreNonPeer(from, opcode);
1057 } else if (ntype == PEER_SIBLING) {
1058 debug_trap("neighborsUdpAck: Found non-ICP cache as SIBLING\n");
1059 debug_trap("neighborsUdpAck: non-ICP neighbors must be a PARENT\n");
1060 } else {
1061 mem->ping_reply_callback(p, ntype, AnyP::PROTO_ICP, header, mem->ircb_data);
1062 }
1063 } else if (opcode == ICP_SECHO) {
1064 if (p) {
1065 debugs(15, 1, "Ignoring SECHO from neighbor " << p->host);
1066 neighborCountIgnored(p);
1067 } else {
1068 debugs(15, 1, "Unsolicited SECHO from " << from);
1069 }
1070 } else if (opcode == ICP_DENIED) {
1071 if (p == NULL) {
1072 neighborIgnoreNonPeer(from, opcode);
1073 } else if (p->stats.pings_acked > 100) {
1074 if (100 * p->icp.counts[ICP_DENIED] / p->stats.pings_acked > 95) {
1075 debugs(15, 0, "95%% of replies from '" << p->host << "' are UDP_DENIED");
1076 debugs(15, 0, "Disabling '" << p->host << "', please check your configuration.");
1077 neighborRemove(p);
1078 p = NULL;
1079 } else {
1080 neighborCountIgnored(p);
1081 }
1082 }
1083 } else if (opcode == ICP_MISS_NOFETCH) {
1084 mem->ping_reply_callback(p, ntype, AnyP::PROTO_ICP, header, mem->ircb_data);
1085 } else {
1086 debugs(15, 0, "neighborsUdpAck: Unexpected ICP reply: " << opcode_d);
1087 }
1088 }
1089
1090 peer *
1091 peerFindByName(const char *name)
1092 {
1093 peer *p = NULL;
1094
1095 for (p = Config.peers; p; p = p->next) {
1096 if (!strcasecmp(name, p->name))
1097 break;
1098 }
1099
1100 return p;
1101 }
1102
1103 peer *
1104 peerFindByNameAndPort(const char *name, unsigned short port)
1105 {
1106 peer *p = NULL;
1107
1108 for (p = Config.peers; p; p = p->next) {
1109 if (strcasecmp(name, p->name))
1110 continue;
1111
1112 if (port != p->http_port)
1113 continue;
1114
1115 break;
1116 }
1117
1118 return p;
1119 }
1120
1121 int
1122 neighborUp(const peer * p)
1123 {
1124 if (!p->tcp_up) {
1125 if (!peerProbeConnect((peer *) p)) {
1126 debugs(15, 8, "neighborUp: DOWN (probed): " << p->host << " (" << p->in_addr << ")");
1127 return 0;
1128 }
1129 }
1130
1131 /*
1132 * The peer can not be UP if we don't have any IP addresses
1133 * for it.
1134 */
1135 if (0 == p->n_addresses) {
1136 debugs(15, 8, "neighborUp: DOWN (no-ip): " << p->host << " (" << p->in_addr << ")");
1137 return 0;
1138 }
1139
1140 if (p->options.no_query) {
1141 debugs(15, 8, "neighborUp: UP (no-query): " << p->host << " (" << p->in_addr << ")");
1142 return 1;
1143 }
1144
1145 if (p->stats.probe_start != 0 &&
1146 squid_curtime - p->stats.probe_start > Config.Timeout.deadPeer) {
1147 debugs(15, 8, "neighborUp: DOWN (dead): " << p->host << " (" << p->in_addr << ")");
1148 return 0;
1149 }
1150
1151 debugs(15, 8, "neighborUp: UP: " << p->host << " (" << p->in_addr << ")");
1152 return 1;
1153 }
1154
1155 void
1156 peerDestroy(void *data)
1157 {
1158 peer *p = (peer *)data;
1159
1160 if (p == NULL)
1161 return;
1162
1163 struct _domain_ping *nl = NULL;
1164
1165 for (struct _domain_ping *l = p->peer_domain; l; l = nl) {
1166 nl = l->next;
1167 safe_free(l->domain);
1168 xfree(l);
1169 }
1170
1171 safe_free(p->host);
1172 safe_free(p->name);
1173 safe_free(p->domain);
1174 #if USE_CACHE_DIGESTS
1175
1176 cbdataReferenceDone(p->digest);
1177 #endif
1178 }
1179
1180 void
1181 peerNoteDigestGone(peer * p)
1182 {
1183 #if USE_CACHE_DIGESTS
1184 cbdataReferenceDone(p->digest);
1185 #endif
1186 }
1187
1188 static void
1189 peerDNSConfigure(const ipcache_addrs *ia, const DnsLookupDetails &, void *data)
1190 {
1191 peer *p = (peer *)data;
1192
1193 int j;
1194
1195 if (p->n_addresses == 0) {
1196 debugs(15, 1, "Configuring " << neighborTypeStr(p) << " " << p->host << "/" << p->http_port << "/" << p->icp.port);
1197
1198 if (p->type == PEER_MULTICAST)
1199 debugs(15, 1, " Multicast TTL = " << p->mcast.ttl);
1200 }
1201
1202 p->n_addresses = 0;
1203
1204 if (ia == NULL) {
1205 debugs(0, 0, "WARNING: DNS lookup for '" << p->host << "' failed!");
1206 return;
1207 }
1208
1209 if ((int) ia->count < 1) {
1210 debugs(0, 0, "WARNING: No IP address found for '" << p->host << "'!");
1211 return;
1212 }
1213
1214 p->tcp_up = p->connect_fail_limit;
1215
1216 for (j = 0; j < (int) ia->count && j < PEER_MAX_ADDRESSES; j++) {
1217 p->addresses[j] = ia->in_addrs[j];
1218 debugs(15, 2, "--> IP address #" << j << ": " << p->addresses[j]);
1219 p->n_addresses++;
1220 }
1221
1222 p->in_addr.SetEmpty();
1223 p->in_addr = p->addresses[0];
1224 p->in_addr.SetPort(p->icp.port);
1225
1226 if (p->type == PEER_MULTICAST)
1227 peerCountMcastPeersSchedule(p, 10);
1228
1229 #if USE_ICMP
1230 if (p->type != PEER_MULTICAST)
1231 if (!p->options.no_netdb_exchange)
1232 eventAddIsh("netdbExchangeStart", netdbExchangeStart, p, 30.0, 1);
1233 #endif
1234
1235 }
1236
1237 static void
1238 peerRefreshDNS(void *data)
1239 {
1240 peer *p = NULL;
1241
1242 if (eventFind(peerRefreshDNS, NULL))
1243 eventDelete(peerRefreshDNS, NULL);
1244
1245 if (!data && 0 == stat5minClientRequests()) {
1246 /* no recent client traffic, wait a bit */
1247 eventAddIsh("peerRefreshDNS", peerRefreshDNS, NULL, 180.0, 1);
1248 return;
1249 }
1250
1251 for (p = Config.peers; p; p = p->next)
1252 ipcache_nbgethostbyname(p->host, peerDNSConfigure, p);
1253
1254 /* Reconfigure the peers every hour */
1255 eventAddIsh("peerRefreshDNS", peerRefreshDNS, NULL, 3600.0, 1);
1256 }
1257
1258 static void
1259 peerConnectFailedSilent(peer * p)
1260 {
1261 p->stats.last_connect_failure = squid_curtime;
1262
1263 if (!p->tcp_up) {
1264 debugs(15, 2, "TCP connection to " << p->host << "/" << p->http_port <<
1265 " dead");
1266 return;
1267 }
1268
1269 p->tcp_up--;
1270
1271 if (!p->tcp_up) {
1272 debugs(15, 1, "Detected DEAD " << neighborTypeStr(p) << ": " << p->name);
1273 p->stats.logged_state = PEER_DEAD;
1274 }
1275 }
1276
1277 void
1278 peerConnectFailed(peer *p)
1279 {
1280 debugs(15, 1, "TCP connection to " << p->host << "/" << p->http_port << " failed");
1281 peerConnectFailedSilent(p);
1282 }
1283
1284 void
1285 peerConnectSucceded(peer * p)
1286 {
1287 if (!p->tcp_up) {
1288 debugs(15, 2, "TCP connection to " << p->host << "/" << p->http_port << " succeded");
1289 p->tcp_up = p->connect_fail_limit; // NP: so peerAlive(p) works properly.
1290 peerAlive(p);
1291 if (!p->n_addresses)
1292 ipcache_nbgethostbyname(p->host, peerDNSConfigure, p);
1293 } else
1294 p->tcp_up = p->connect_fail_limit;
1295 }
1296
1297 /*
1298 * peerProbeConnect will be called on dead peers by neighborUp
1299 */
1300 static bool
1301 peerProbeConnect(peer * p)
1302 {
1303 time_t ctimeout = p->connect_timeout > 0 ? p->connect_timeout : Config.Timeout.peer_connect;
1304 bool ret = (squid_curtime - p->stats.last_connect_failure) > (ctimeout * 10);
1305
1306 if (p->testing_now > 0)
1307 return ret;/* probe already running */
1308
1309 if (squid_curtime - p->stats.last_connect_probe == 0)
1310 return ret;/* don't probe to often */
1311
1312 /* for each IP address of this peer. find one that we can connect to and probe it. */
1313 for (int i = 0; i < p->n_addresses; i++) {
1314 Comm::ConnectionPointer conn = new Comm::Connection;
1315 conn->remote = p->addresses[i];
1316 conn->remote.SetPort(p->http_port);
1317 getOutgoingAddress(NULL, conn);
1318
1319 p->testing_now++;
1320
1321 AsyncCall::Pointer call = commCbCall(15,3, "peerProbeConnectDone", CommConnectCbPtrFun(peerProbeConnectDone, p));
1322 Comm::ConnOpener *cs = new Comm::ConnOpener(conn, call, ctimeout);
1323 cs->setHost(p->host);
1324 AsyncJob::Start(cs);
1325 }
1326
1327 p->stats.last_connect_probe = squid_curtime;
1328
1329 return ret;
1330 }
1331
1332 static void
1333 peerProbeConnectDone(const Comm::ConnectionPointer &conn, comm_err_t status, int xerrno, void *data)
1334 {
1335 peer *p = (peer*)data;
1336
1337 if (status == COMM_OK) {
1338 peerConnectSucceded(p);
1339 } else {
1340 peerConnectFailedSilent(p);
1341 }
1342
1343 p->testing_now--;
1344 return;
1345 }
1346
1347 static void
1348 peerCountMcastPeersSchedule(peer * p, time_t when)
1349 {
1350 if (p->mcast.flags.count_event_pending)
1351 return;
1352
1353 eventAdd("peerCountMcastPeersStart",
1354 peerCountMcastPeersStart,
1355 p,
1356 (double) when, 1);
1357
1358 p->mcast.flags.count_event_pending = 1;
1359 }
1360
1361 static void
1362 peerCountMcastPeersStart(void *data)
1363 {
1364 peer *p = (peer *)data;
1365 ps_state *psstate;
1366 StoreEntry *fake;
1367 MemObject *mem;
1368 icp_common_t *query;
1369 int reqnum;
1370 LOCAL_ARRAY(char, url, MAX_URL);
1371 assert(p->type == PEER_MULTICAST);
1372 p->mcast.flags.count_event_pending = 0;
1373 snprintf(url, MAX_URL, "http://");
1374 p->in_addr.ToURL(url+7, MAX_URL -8 );
1375 strcat(url, "/");
1376 fake = storeCreateEntry(url, url, request_flags(), METHOD_GET);
1377 HttpRequest *req = HttpRequest::CreateFromUrl(url);
1378 psstate = new ps_state;
1379 psstate->request = HTTPMSGLOCK(req);
1380 psstate->entry = fake;
1381 psstate->callback = NULL;
1382 psstate->callback_data = cbdataReference(p);
1383 psstate->ping.start = current_time;
1384 mem = fake->mem_obj;
1385 mem->request = HTTPMSGLOCK(psstate->request);
1386 mem->start_ping = current_time;
1387 mem->ping_reply_callback = peerCountHandleIcpReply;
1388 mem->ircb_data = psstate;
1389 mcastSetTtl(icpOutgoingConn->fd, p->mcast.ttl);
1390 p->mcast.id = mem->id;
1391 reqnum = icpSetCacheKey((const cache_key *)fake->key);
1392 query = _icp_common_t::createMessage(ICP_QUERY, 0, url, reqnum, 0);
1393 icpUdpSend(icpOutgoingConn->fd, p->in_addr, query, LOG_ICP_QUERY, 0);
1394 fake->ping_status = PING_WAITING;
1395 eventAdd("peerCountMcastPeersDone",
1396 peerCountMcastPeersDone,
1397 psstate,
1398 Config.Timeout.mcast_icp_query / 1000.0, 1);
1399 p->mcast.flags.counting = 1;
1400 peerCountMcastPeersSchedule(p, MCAST_COUNT_RATE);
1401 }
1402
1403 static void
1404 peerCountMcastPeersDone(void *data)
1405 {
1406 ps_state *psstate = (ps_state *)data;
1407 StoreEntry *fake = psstate->entry;
1408
1409 if (cbdataReferenceValid(psstate->callback_data)) {
1410 peer *p = (peer *)psstate->callback_data;
1411 p->mcast.flags.counting = 0;
1412 p->mcast.avg_n_members = Math::doubleAverage(p->mcast.avg_n_members, (double) psstate->ping.n_recv, ++p->mcast.n_times_counted, 10);
1413 debugs(15, 1, "Group " << p->host << ": " << psstate->ping.n_recv <<
1414 " replies, "<< std::setw(4)<< std::setprecision(2) <<
1415 p->mcast.avg_n_members <<" average, RTT " << p->stats.rtt);
1416 p->mcast.n_replies_expected = (int) p->mcast.avg_n_members;
1417 }
1418
1419 cbdataReferenceDone(psstate->callback_data);
1420
1421 EBIT_SET(fake->flags, ENTRY_ABORTED);
1422 HTTPMSGUNLOCK(fake->mem_obj->request);
1423 fake->releaseRequest();
1424 fake->unlock();
1425 HTTPMSGUNLOCK(psstate->request);
1426 cbdataFree(psstate);
1427 }
1428
1429 static void
1430 peerCountHandleIcpReply(peer * p, peer_t type, AnyP::ProtocolType proto, void *hdrnotused, void *data)
1431 {
1432 int rtt_av_factor;
1433
1434 ps_state *psstate = (ps_state *)data;
1435 StoreEntry *fake = psstate->entry;
1436 MemObject *mem = fake->mem_obj;
1437 int rtt = tvSubMsec(mem->start_ping, current_time);
1438 assert(proto == AnyP::PROTO_ICP);
1439 assert(fake);
1440 assert(mem);
1441 psstate->ping.n_recv++;
1442 rtt_av_factor = RTT_AV_FACTOR;
1443
1444 if (p->options.weighted_roundrobin)
1445 rtt_av_factor = RTT_BACKGROUND_AV_FACTOR;
1446
1447 p->stats.rtt = Math::intAverage(p->stats.rtt, rtt, psstate->ping.n_recv, rtt_av_factor);
1448 }
1449
1450 static void
1451 neighborDumpPeers(StoreEntry * sentry)
1452 {
1453 dump_peers(sentry, Config.peers);
1454 }
1455
1456 static void
1457 neighborDumpNonPeers(StoreEntry * sentry)
1458 {
1459 dump_peers(sentry, non_peers);
1460 }
1461
1462 void
1463 dump_peer_options(StoreEntry * sentry, peer * p)
1464 {
1465 if (p->options.proxy_only)
1466 storeAppendPrintf(sentry, " proxy-only");
1467
1468 if (p->options.no_query)
1469 storeAppendPrintf(sentry, " no-query");
1470
1471 if (p->options.background_ping)
1472 storeAppendPrintf(sentry, " background-ping");
1473
1474 if (p->options.no_digest)
1475 storeAppendPrintf(sentry, " no-digest");
1476
1477 if (p->options.default_parent)
1478 storeAppendPrintf(sentry, " default");
1479
1480 if (p->options.roundrobin)
1481 storeAppendPrintf(sentry, " round-robin");
1482
1483 if (p->options.carp)
1484 storeAppendPrintf(sentry, " carp");
1485
1486 #if USE_AUTH
1487 if (p->options.userhash)
1488 storeAppendPrintf(sentry, " userhash");
1489 #endif
1490
1491 if (p->options.sourcehash)
1492 storeAppendPrintf(sentry, " sourcehash");
1493
1494 if (p->options.weighted_roundrobin)
1495 storeAppendPrintf(sentry, " weighted-round-robin");
1496
1497 if (p->options.mcast_responder)
1498 storeAppendPrintf(sentry, " multicast-responder");
1499
1500 #if PEER_MULTICAST_SIBLINGS
1501 if (p->options.mcast_siblings)
1502 storeAppendPrintf(sentry, " multicast-siblings");
1503 #endif
1504
1505 if (p->weight != 1)
1506 storeAppendPrintf(sentry, " weight=%d", p->weight);
1507
1508 if (p->options.closest_only)
1509 storeAppendPrintf(sentry, " closest-only");
1510
1511 #if USE_HTCP
1512 if (p->options.htcp) {
1513 storeAppendPrintf(sentry, " htcp");
1514 if (p->options.htcp_oldsquid || p->options.htcp_no_clr || p->options.htcp_no_purge_clr || p->options.htcp_only_clr) {
1515 int doneopts=0;
1516 if (p->options.htcp_oldsquid)
1517 storeAppendPrintf(sentry, "%soldsquid",(doneopts++>0?",":"="));
1518 if (p->options.htcp_no_clr)
1519 storeAppendPrintf(sentry, "%sno-clr",(doneopts++>0?",":"="));
1520 if (p->options.htcp_no_purge_clr)
1521 storeAppendPrintf(sentry, "%sno-purge-clr",(doneopts++>0?",":"="));
1522 if (p->options.htcp_only_clr)
1523 storeAppendPrintf(sentry, "%sonly-clr",(doneopts++>0?",":"="));
1524 }
1525 }
1526 #endif
1527
1528 if (p->options.no_netdb_exchange)
1529 storeAppendPrintf(sentry, " no-netdb-exchange");
1530
1531 #if USE_DELAY_POOLS
1532 if (p->options.no_delay)
1533 storeAppendPrintf(sentry, " no-delay");
1534 #endif
1535
1536 if (p->login)
1537 storeAppendPrintf(sentry, " login=%s", p->login);
1538
1539 if (p->mcast.ttl > 0)
1540 storeAppendPrintf(sentry, " ttl=%d", p->mcast.ttl);
1541
1542 if (p->connect_timeout > 0)
1543 storeAppendPrintf(sentry, " connect-timeout=%d", (int) p->connect_timeout);
1544
1545 if (p->connect_fail_limit != PEER_TCP_MAGIC_COUNT)
1546 storeAppendPrintf(sentry, " connect-fail-limit=%d", p->connect_fail_limit);
1547
1548 #if USE_CACHE_DIGESTS
1549
1550 if (p->digest_url)
1551 storeAppendPrintf(sentry, " digest-url=%s", p->digest_url);
1552
1553 #endif
1554
1555 if (p->options.allow_miss)
1556 storeAppendPrintf(sentry, " allow-miss");
1557
1558 if (p->options.no_tproxy)
1559 storeAppendPrintf(sentry, " no-tproxy");
1560
1561 if (p->max_conn > 0)
1562 storeAppendPrintf(sentry, " max-conn=%d", p->max_conn);
1563
1564 if (p->options.originserver)
1565 storeAppendPrintf(sentry, " originserver");
1566
1567 if (p->domain)
1568 storeAppendPrintf(sentry, " forceddomain=%s", p->domain);
1569
1570 if (p->connection_auth == 0)
1571 storeAppendPrintf(sentry, " connection-auth=off");
1572 else if (p->connection_auth == 1)
1573 storeAppendPrintf(sentry, " connection-auth=on");
1574 else if (p->connection_auth == 2)
1575 storeAppendPrintf(sentry, " connection-auth=auto");
1576
1577 storeAppendPrintf(sentry, "\n");
1578 }
1579
1580 static void
1581 dump_peers(StoreEntry * sentry, peer * peers)
1582 {
1583 peer *e = NULL;
1584 char ntoabuf[MAX_IPSTRLEN];
1585 struct _domain_ping *d = NULL;
1586 icp_opcode op;
1587 int i;
1588
1589 if (peers == NULL)
1590 storeAppendPrintf(sentry, "There are no neighbors installed.\n");
1591
1592 for (e = peers; e; e = e->next) {
1593 assert(e->host != NULL);
1594 storeAppendPrintf(sentry, "\n%-11.11s: %s\n",
1595 neighborTypeStr(e),
1596 e->name);
1597 storeAppendPrintf(sentry, "Host : %s/%d/%d\n",
1598 e->host,
1599 e->http_port,
1600 e->icp.port);
1601 storeAppendPrintf(sentry, "Flags :");
1602 dump_peer_options(sentry, e);
1603
1604 for (i = 0; i < e->n_addresses; i++) {
1605 storeAppendPrintf(sentry, "Address[%d] : %s\n", i,
1606 e->addresses[i].NtoA(ntoabuf,MAX_IPSTRLEN) );
1607 }
1608
1609 storeAppendPrintf(sentry, "Status : %s\n",
1610 neighborUp(e) ? "Up" : "Down");
1611 storeAppendPrintf(sentry, "FETCHES : %d\n", e->stats.fetches);
1612 storeAppendPrintf(sentry, "OPEN CONNS : %d\n", e->stats.conn_open);
1613 storeAppendPrintf(sentry, "AVG RTT : %d msec\n", e->stats.rtt);
1614
1615 if (!e->options.no_query) {
1616 storeAppendPrintf(sentry, "LAST QUERY : %8d seconds ago\n",
1617 (int) (squid_curtime - e->stats.last_query));
1618
1619 if (e->stats.last_reply > 0)
1620 storeAppendPrintf(sentry, "LAST REPLY : %8d seconds ago\n",
1621 (int) (squid_curtime - e->stats.last_reply));
1622 else
1623 storeAppendPrintf(sentry, "LAST REPLY : none received\n");
1624
1625 storeAppendPrintf(sentry, "PINGS SENT : %8d\n", e->stats.pings_sent);
1626
1627 storeAppendPrintf(sentry, "PINGS ACKED: %8d %3d%%\n",
1628 e->stats.pings_acked,
1629 Math::intPercent(e->stats.pings_acked, e->stats.pings_sent));
1630 }
1631
1632 storeAppendPrintf(sentry, "IGNORED : %8d %3d%%\n", e->stats.ignored_replies, Math::intPercent(e->stats.ignored_replies, e->stats.pings_acked));
1633
1634 if (!e->options.no_query) {
1635 storeAppendPrintf(sentry, "Histogram of PINGS ACKED:\n");
1636 #if USE_HTCP
1637
1638 if (e->options.htcp) {
1639 storeAppendPrintf(sentry, "\tMisses\t%8d %3d%%\n",
1640 e->htcp.counts[0],
1641 Math::intPercent(e->htcp.counts[0], e->stats.pings_acked));
1642 storeAppendPrintf(sentry, "\tHits\t%8d %3d%%\n",
1643 e->htcp.counts[1],
1644 Math::intPercent(e->htcp.counts[1], e->stats.pings_acked));
1645 } else {
1646 #endif
1647
1648 for (op = ICP_INVALID; op < ICP_END; ++op) {
1649 if (e->icp.counts[op] == 0)
1650 continue;
1651
1652 storeAppendPrintf(sentry, " %12.12s : %8d %3d%%\n",
1653 icp_opcode_str[op],
1654 e->icp.counts[op],
1655 Math::intPercent(e->icp.counts[op], e->stats.pings_acked));
1656 }
1657
1658 #if USE_HTCP
1659
1660 }
1661
1662 #endif
1663
1664 }
1665
1666 if (e->stats.last_connect_failure) {
1667 storeAppendPrintf(sentry, "Last failed connect() at: %s\n",
1668 Time::FormatHttpd(e->stats.last_connect_failure));
1669 }
1670
1671 if (e->peer_domain != NULL) {
1672 storeAppendPrintf(sentry, "DOMAIN LIST: ");
1673
1674 for (d = e->peer_domain; d; d = d->next) {
1675 storeAppendPrintf(sentry, "%s%s ",
1676 d->do_ping ? null_string : "!", d->domain);
1677 }
1678
1679 storeAppendPrintf(sentry, "\n");
1680 }
1681
1682 storeAppendPrintf(sentry, "keep-alive ratio: %d%%\n", Math::intPercent(e->stats.n_keepalives_recv, e->stats.n_keepalives_sent));
1683 }
1684 }
1685
1686 #if USE_HTCP
1687 void
1688 neighborsHtcpReply(const cache_key * key, htcpReplyData * htcp, const Ip::Address &from)
1689 {
1690 StoreEntry *e = Store::Root().get(key);
1691 MemObject *mem = NULL;
1692 peer *p;
1693 peer_t ntype = PEER_NONE;
1694 debugs(15, 6, "neighborsHtcpReply: " <<
1695 (htcp->hit ? "HIT" : "MISS") << " " <<
1696 storeKeyText(key) );
1697
1698 if (NULL != e)
1699 mem = e->mem_obj;
1700
1701 if ((p = whichPeer(from)))
1702 neighborAliveHtcp(p, mem, htcp);
1703
1704 /* Does the entry exist? */
1705 if (NULL == e) {
1706 debugs(12, 3, "neighyborsHtcpReply: Cache key '" << storeKeyText(key) << "' not found");
1707 neighborCountIgnored(p);
1708 return;
1709 }
1710
1711 /* check if someone is already fetching it */
1712 if (EBIT_TEST(e->flags, ENTRY_DISPATCHED)) {
1713 debugs(15, 3, "neighborsUdpAck: '" << storeKeyText(key) << "' already being fetched.");
1714 neighborCountIgnored(p);
1715 return;
1716 }
1717
1718 if (mem == NULL) {
1719 debugs(15, 2, "Ignoring reply for missing mem_obj: " << storeKeyText(key));
1720 neighborCountIgnored(p);
1721 return;
1722 }
1723
1724 if (e->ping_status != PING_WAITING) {
1725 debugs(15, 2, "neighborsUdpAck: Entry " << storeKeyText(key) << " is not PING_WAITING");
1726 neighborCountIgnored(p);
1727 return;
1728 }
1729
1730 if (e->lock_count == 0) {
1731 debugs(12, 1, "neighborsUdpAck: '" << storeKeyText(key) << "' has no locks");
1732 neighborCountIgnored(p);
1733 return;
1734 }
1735
1736 if (p) {
1737 ntype = neighborType(p, mem->request);
1738 neighborUpdateRtt(p, mem);
1739 }
1740
1741 if (ignoreMulticastReply(p, mem)) {
1742 neighborCountIgnored(p);
1743 return;
1744 }
1745
1746 debugs(15, 3, "neighborsHtcpReply: e = " << e);
1747 mem->ping_reply_callback(p, ntype, AnyP::PROTO_HTCP, htcp, mem->ircb_data);
1748 }
1749
1750 /*
1751 * Send HTCP CLR messages to all peers configured to receive them.
1752 */
1753 void
1754 neighborsHtcpClear(StoreEntry * e, const char *uri, HttpRequest * req, const HttpRequestMethod &method, htcp_clr_reason reason)
1755 {
1756 peer *p;
1757 char buf[128];
1758
1759 for (p = Config.peers; p; p = p->next) {
1760 if (!p->options.htcp) {
1761 continue;
1762 }
1763 if (p->options.htcp_no_clr) {
1764 continue;
1765 }
1766 if (p->options.htcp_no_purge_clr && reason == HTCP_CLR_PURGE) {
1767 continue;
1768 }
1769 debugs(15, 3, "neighborsHtcpClear: sending CLR to " << p->in_addr.ToURL(buf, 128));
1770 htcpClear(e, uri, req, method, p, reason);
1771 }
1772 }
1773
1774 #endif