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