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