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