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