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