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