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