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