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