]>
Commit | Line | Data |
---|---|---|
22f3fd98 | 1 | |
30a4f2a8 | 2 | /* |
83b381d5 | 3 | * $Id: neighbors.cc,v 1.279 2000/05/02 18:23:48 hno Exp $ |
30a4f2a8 | 4 | * |
5 | * DEBUG: section 15 Neighbor Routines | |
6 | * AUTHOR: Harvest Derived | |
7 | * | |
42c04c16 | 8 | * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ |
e25c139f | 9 | * ---------------------------------------------------------- |
30a4f2a8 | 10 | * |
11 | * Squid is the result of efforts by numerous individuals from the | |
12 | * Internet community. Development is led by Duane Wessels of the | |
e25c139f | 13 | * National Laboratory for Applied Network Research and funded by the |
14 | * National Science Foundation. Squid is Copyrighted (C) 1998 by | |
efd900cb | 15 | * the Regents of the University of California. Please see the |
16 | * COPYRIGHT file for full details. Squid incorporates software | |
17 | * developed and/or copyrighted by other sources. Please see the | |
18 | * CREDITS file for full details. | |
30a4f2a8 | 19 | * |
20 | * This program is free software; you can redistribute it and/or modify | |
21 | * it under the terms of the GNU General Public License as published by | |
22 | * the Free Software Foundation; either version 2 of the License, or | |
23 | * (at your option) any later version. | |
24 | * | |
25 | * This program is distributed in the hope that it will be useful, | |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 | * GNU General Public License for more details. | |
29 | * | |
30 | * You should have received a copy of the GNU General Public License | |
31 | * along with this program; if not, write to the Free Software | |
cbdec147 | 32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 33 | * |
019dd986 | 34 | */ |
090089c4 | 35 | |
44a47c6e | 36 | #include "squid.h" |
090089c4 | 37 | |
429fdbec | 38 | /* count mcast group peers every 15 minutes */ |
39 | #define MCAST_COUNT_RATE 900 | |
40 | ||
f5b8bbc4 | 41 | static int peerAllowedToUse(const peer *, request_t *); |
f5b8bbc4 | 42 | static int peerWouldBePinged(const peer *, request_t *); |
43 | static void neighborRemove(peer *); | |
f5b8bbc4 | 44 | static void neighborAlive(peer *, const MemObject *, const icp_common_t *); |
399cabec | 45 | #if USE_HTCP |
46 | static void neighborAliveHtcp(peer *, const MemObject *, const htcpReplyData *); | |
47 | #endif | |
56b62ecc | 48 | static void neighborCountIgnored(peer *); |
f5b8bbc4 | 49 | static void peerRefreshDNS(void *); |
b69f7771 | 50 | static IPH peerDNSConfigure; |
0e7b6b06 | 51 | static EVH peerCheckConnect; |
b69f7771 | 52 | static IPH peerCheckConnect2; |
603a02fd | 53 | static CNCB peerCheckConnectDone; |
f5b8bbc4 | 54 | static void peerCountMcastPeersDone(void *data); |
55 | static void peerCountMcastPeersStart(void *data); | |
56 | static void peerCountMcastPeersSchedule(peer * p, time_t when); | |
b3264694 | 57 | static IRCB peerCountHandleIcpReply; |
f5b8bbc4 | 58 | static void neighborIgnoreNonPeer(const struct sockaddr_in *, icp_opcode); |
ed7f5615 | 59 | static OBJH neighborDumpPeers; |
60 | static OBJH neighborDumpNonPeers; | |
61 | static void dump_peers(StoreEntry * sentry, peer * peers); | |
090089c4 | 62 | |
090089c4 | 63 | static icp_common_t echo_hdr; |
30a4f2a8 | 64 | static u_short echo_port; |
30a4f2a8 | 65 | |
c7a3724d | 66 | static int NLateReplies = 0; |
40a1495e | 67 | static peer *first_ping = NULL; |
28070024 | 68 | |
c7a3724d | 69 | char * |
b69f7771 | 70 | neighborTypeStr(const peer * p) |
69a71bd7 | 71 | { |
e102ebda | 72 | if (p->type == PEER_NONE) |
73 | return "Non-Peer"; | |
b69f7771 | 74 | if (p->type == PEER_SIBLING) |
69a71bd7 | 75 | return "Sibling"; |
b69f7771 | 76 | if (p->type == PEER_MULTICAST) |
7b3bd12c | 77 | return "Multicast Group"; |
69a71bd7 | 78 | return "Parent"; |
79 | } | |
80 | ||
090089c4 | 81 | |
d9f9d78b | 82 | peer * |
0cdcddb9 | 83 | whichPeer(const struct sockaddr_in * from) |
090089c4 | 84 | { |
22e4fa85 | 85 | int j; |
4eda6afe | 86 | u_short port = ntohs(from->sin_port); |
87 | struct in_addr ip = from->sin_addr; | |
b69f7771 | 88 | peer *p = NULL; |
a3d5953d | 89 | debug(15, 3) ("whichPeer: from %s port %d\n", inet_ntoa(ip), port); |
40a1495e | 90 | for (p = Config.peers; p; p = p->next) { |
b69f7771 | 91 | for (j = 0; j < p->n_addresses; j++) { |
399cabec | 92 | if (ip.s_addr == p->addresses[j].s_addr && port == p->icp.port) { |
b69f7771 | 93 | return p; |
090089c4 | 94 | } |
95 | } | |
96 | } | |
4eda6afe | 97 | return NULL; |
090089c4 | 98 | } |
99 | ||
701a8572 | 100 | peer_t |
b69f7771 | 101 | neighborType(const peer * p, const request_t * request) |
24a1003d | 102 | { |
b012353a | 103 | const struct _domain_type *d = NULL; |
b69f7771 | 104 | for (d = p->typelist; d; d = d->next) { |
7e3ce7b9 | 105 | if (0 == matchDomainName(request->host, d->domain)) |
deb79f06 | 106 | if (d->type != PEER_NONE) |
b012353a | 107 | return d->type; |
24a1003d | 108 | } |
b69f7771 | 109 | return p->type; |
24a1003d | 110 | } |
111 | ||
7b3bd12c | 112 | /* |
62fd6124 | 113 | * peerAllowedToUse |
7b3bd12c | 114 | * |
dd4bd44e | 115 | * this function figures out if it is appropriate to fetch REQUEST |
62fd6124 | 116 | * from PEER. |
7b3bd12c | 117 | */ |
b8d8561b | 118 | static int |
b69f7771 | 119 | peerAllowedToUse(const peer * p, request_t * request) |
090089c4 | 120 | { |
b012353a | 121 | const struct _domain_ping *d = NULL; |
090089c4 | 122 | int do_ping = 1; |
f88bb09c | 123 | aclCheck_t checklist; |
ce66013b | 124 | assert(request != NULL); |
9689d97c | 125 | if (neighborType(p, request) == PEER_SIBLING) { |
126 | if (request->flags.nocache) | |
24a1003d | 127 | return 0; |
9689d97c | 128 | if (request->flags.refresh) |
d950cec8 | 129 | return 0; |
9689d97c | 130 | if (request->flags.loopdetect) |
131 | return 0; | |
132 | if (request->flags.need_validation) | |
133 | return 0; | |
134 | } | |
b6a2f15e | 135 | if (p->peer_domain == NULL && p->access == NULL) |
090089c4 | 136 | return do_ping; |
090089c4 | 137 | do_ping = 0; |
b6a2f15e | 138 | for (d = p->peer_domain; d; d = d->next) { |
7e3ce7b9 | 139 | if (0 == matchDomainName(request->host, d->domain)) { |
130faaed | 140 | do_ping = d->do_ping; |
141 | break; | |
142 | } | |
30a4f2a8 | 143 | do_ping = !d->do_ping; |
144 | } | |
b6a2f15e | 145 | if (p->peer_domain && 0 == do_ping) |
130faaed | 146 | return do_ping; |
505e35db | 147 | if (p->access == NULL) |
148 | return do_ping; | |
8ae8bb05 | 149 | checklist.src_addr = request->client_addr; |
3c11d1f5 | 150 | checklist.my_addr = request->my_addr; |
7e3ce7b9 | 151 | checklist.my_port = request->my_port; |
f88bb09c | 152 | checklist.request = request; |
505e35db | 153 | return aclCheckFast(p->access, &checklist); |
090089c4 | 154 | } |
155 | ||
62fd6124 | 156 | /* Return TRUE if it is okay to send an ICP request to this peer. */ |
157 | static int | |
b69f7771 | 158 | peerWouldBePinged(const peer * p, request_t * request) |
62fd6124 | 159 | { |
b69f7771 | 160 | if (!peerAllowedToUse(p, request)) |
62fd6124 | 161 | return 0; |
cd196bc8 | 162 | if (p->options.no_query) |
62fd6124 | 163 | return 0; |
cd196bc8 | 164 | if (p->options.mcast_responder) |
62fd6124 | 165 | return 0; |
83b381d5 | 166 | if (p->n_addresses == 0) |
167 | return 0; | |
62fd6124 | 168 | /* the case below seems strange, but can happen if the |
169 | * URL host is on the other side of a firewall */ | |
b69f7771 | 170 | if (p->type == PEER_SIBLING) |
92695e5e | 171 | if (!request->flags.hierarchical) |
62fd6124 | 172 | return 0; |
83b381d5 | 173 | /* Ping dead peers every timeout interval */ |
174 | if (squid_curtime - p->stats.last_query > Config.Timeout.deadPeer) | |
175 | return 1; | |
176 | if (!neighborUp(p)) | |
dd4bd44e | 177 | return 0; |
62fd6124 | 178 | return 1; |
179 | } | |
180 | ||
181 | /* Return TRUE if it is okay to send an HTTP request to this peer. */ | |
8ff4505b | 182 | int |
b69f7771 | 183 | peerHTTPOkay(const peer * p, request_t * request) |
62fd6124 | 184 | { |
b69f7771 | 185 | if (!peerAllowedToUse(p, request)) |
62fd6124 | 186 | return 0; |
b69f7771 | 187 | if (!neighborUp(p)) |
62fd6124 | 188 | return 0; |
189 | return 1; | |
190 | } | |
191 | ||
3c6e634f | 192 | int |
193 | neighborsCount(request_t * request) | |
090089c4 | 194 | { |
b69f7771 | 195 | peer *p = NULL; |
090089c4 | 196 | int count = 0; |
40a1495e | 197 | for (p = Config.peers; p; p = p->next) |
b69f7771 | 198 | if (peerWouldBePinged(p, request)) |
3c6e634f | 199 | count++; |
a3d5953d | 200 | debug(15, 3) ("neighborsCount: %d\n", count); |
3c6e634f | 201 | return count; |
202 | } | |
090089c4 | 203 | |
deb79f06 | 204 | peer * |
3c6e634f | 205 | getSingleParent(request_t * request) |
206 | { | |
deb79f06 | 207 | peer *p = NULL; |
b69f7771 | 208 | peer *q = NULL; |
40a1495e | 209 | for (q = Config.peers; q; q = q->next) { |
b69f7771 | 210 | if (!peerHTTPOkay(q, request)) |
caebbe00 | 211 | continue; |
b69f7771 | 212 | if (neighborType(q, request) != PEER_PARENT) |
3c6e634f | 213 | return NULL; /* oops, found SIBLING */ |
214 | if (p) | |
215 | return NULL; /* oops, found second parent */ | |
b69f7771 | 216 | p = q; |
090089c4 | 217 | } |
cd196bc8 | 218 | if (p != NULL && !p->options.no_query) |
a369131d | 219 | return NULL; |
a3d5953d | 220 | debug(15, 3) ("getSingleParent: returning %s\n", p ? p->host : "NULL"); |
3c6e634f | 221 | return p; |
090089c4 | 222 | } |
223 | ||
deb79f06 | 224 | peer * |
b8d8561b | 225 | getFirstUpParent(request_t * request) |
090089c4 | 226 | { |
b69f7771 | 227 | peer *p = NULL; |
40a1495e | 228 | for (p = Config.peers; p; p = p->next) { |
b69f7771 | 229 | if (!neighborUp(p)) |
30a4f2a8 | 230 | continue; |
b69f7771 | 231 | if (neighborType(p, request) != PEER_PARENT) |
090089c4 | 232 | continue; |
b69f7771 | 233 | if (!peerHTTPOkay(p, request)) |
e924600d | 234 | continue; |
235 | break; | |
090089c4 | 236 | } |
a3d5953d | 237 | debug(15, 3) ("getFirstUpParent: returning %s\n", p ? p->host : "NULL"); |
b69f7771 | 238 | return p; |
090089c4 | 239 | } |
240 | ||
deb79f06 | 241 | peer * |
48b38d01 | 242 | getRoundRobinParent(request_t * request) |
243 | { | |
b69f7771 | 244 | peer *p; |
245 | peer *q = NULL; | |
40a1495e | 246 | for (p = Config.peers; p; p = p->next) { |
cd196bc8 | 247 | if (!p->options.roundrobin) |
0c8177e6 | 248 | continue; |
b69f7771 | 249 | if (neighborType(p, request) != PEER_PARENT) |
0c8177e6 | 250 | continue; |
b69f7771 | 251 | if (!peerHTTPOkay(p, request)) |
48b38d01 | 252 | continue; |
b69f7771 | 253 | if (q && q->rr_count < p->rr_count) |
48b38d01 | 254 | continue; |
b69f7771 | 255 | q = p; |
48b38d01 | 256 | } |
b69f7771 | 257 | if (q) |
258 | q->rr_count++; | |
a3d5953d | 259 | debug(15, 3) ("getRoundRobinParent: returning %s\n", q ? q->host : "NULL"); |
b69f7771 | 260 | return q; |
48b38d01 | 261 | } |
262 | ||
deb79f06 | 263 | peer * |
5269d0bd | 264 | getDefaultParent(request_t * request) |
265 | { | |
b69f7771 | 266 | peer *p = NULL; |
40a1495e | 267 | for (p = Config.peers; p; p = p->next) { |
b69f7771 | 268 | if (neighborType(p, request) != PEER_PARENT) |
5269d0bd | 269 | continue; |
cd196bc8 | 270 | if (!p->options.default_parent) |
5269d0bd | 271 | continue; |
b69f7771 | 272 | if (!peerHTTPOkay(p, request)) |
5269d0bd | 273 | continue; |
a3d5953d | 274 | debug(15, 3) ("getDefaultParent: returning %s\n", p->host); |
b69f7771 | 275 | return p; |
5269d0bd | 276 | } |
669fefd4 | 277 | debug(15, 3) ("getDefaultParent: returning NULL\n"); |
5269d0bd | 278 | return NULL; |
279 | } | |
280 | ||
6a655df0 | 281 | peer * |
282 | getAnyParent(request_t * request) | |
283 | { | |
284 | peer *p = NULL; | |
285 | for (p = Config.peers; p; p = p->next) { | |
286 | if (neighborType(p, request) != PEER_PARENT) | |
287 | continue; | |
288 | if (!peerHTTPOkay(p, request)) | |
289 | continue; | |
290 | debug(15, 3) ("getAnyParent: returning %s\n", p->host); | |
291 | return p; | |
292 | } | |
293 | debug(15, 3) ("getAnyParent: returning NULL\n"); | |
294 | return NULL; | |
295 | } | |
296 | ||
deb79f06 | 297 | peer * |
b69f7771 | 298 | getNextPeer(peer * p) |
090089c4 | 299 | { |
b69f7771 | 300 | return p->next; |
090089c4 | 301 | } |
302 | ||
deb79f06 | 303 | peer * |
304 | getFirstPeer(void) | |
090089c4 | 305 | { |
40a1495e | 306 | return Config.peers; |
090089c4 | 307 | } |
308 | ||
b8d8561b | 309 | static void |
deb79f06 | 310 | neighborRemove(peer * target) |
30a4f2a8 | 311 | { |
b69f7771 | 312 | peer *p = NULL; |
313 | peer **P = NULL; | |
40a1495e | 314 | p = Config.peers; |
315 | P = &Config.peers; | |
b69f7771 | 316 | while (p) { |
317 | if (target == p) | |
30a4f2a8 | 318 | break; |
b69f7771 | 319 | P = &p->next; |
320 | p = p->next; | |
321 | } | |
322 | if (p) { | |
323 | *P = p->next; | |
b6a2f15e | 324 | cbdataFree(p); |
40a1495e | 325 | Config.npeers--; |
4d64d74a | 326 | } |
40a1495e | 327 | first_ping = Config.peers; |
4d64d74a | 328 | } |
329 | ||
b8d8561b | 330 | void |
331 | neighbors_open(int fd) | |
090089c4 | 332 | { |
30a4f2a8 | 333 | struct sockaddr_in name; |
6637e3a5 | 334 | socklen_t len = sizeof(struct sockaddr_in); |
30a4f2a8 | 335 | struct servent *sep = NULL; |
30a4f2a8 | 336 | memset(&name, '\0', sizeof(struct sockaddr_in)); |
337 | if (getsockname(fd, (struct sockaddr *) &name, &len) < 0) | |
a3d5953d | 338 | debug(15, 1) ("getsockname(%d,%p,%p) failed.\n", fd, &name, &len); |
5999b776 | 339 | peerRefreshDNS((void *) 1); |
30a4f2a8 | 340 | if (0 == echo_hdr.opcode) { |
27cd7235 | 341 | echo_hdr.opcode = ICP_SECHO; |
30a4f2a8 | 342 | echo_hdr.version = ICP_VERSION_CURRENT; |
343 | echo_hdr.length = 0; | |
344 | echo_hdr.reqnum = 0; | |
345 | echo_hdr.flags = 0; | |
346 | echo_hdr.pad = 0; | |
30a4f2a8 | 347 | echo_hdr.shostid = name.sin_addr.s_addr; |
348 | sep = getservbyname("echo", "udp"); | |
349 | echo_port = sep ? ntohs((u_short) sep->s_port) : 7; | |
090089c4 | 350 | } |
c68e4e90 | 351 | first_ping = Config.peers; |
ed7f5615 | 352 | cachemgrRegister("server_list", |
353 | "Peer Cache Statistics", | |
1da3b90b | 354 | neighborDumpPeers, 0, 1); |
22f3fd98 | 355 | cachemgrRegister("non_peers", |
356 | "List of Unknown sites sending ICP messages", | |
1da3b90b | 357 | neighborDumpNonPeers, 0, 1); |
090089c4 | 358 | } |
359 | ||
b8d8561b | 360 | int |
b6c0e933 | 361 | neighborsUdpPing(request_t * request, |
641941c0 | 362 | StoreEntry * entry, |
582b6456 | 363 | IRCB * callback, |
641941c0 | 364 | void *callback_data, |
52040193 | 365 | int *exprep, |
465dc415 | 366 | int *timeout) |
090089c4 | 367 | { |
9fb13bb6 | 368 | const char *url = storeUrl(entry); |
b6c0e933 | 369 | MemObject *mem = entry->mem_obj; |
b69f7771 | 370 | peer *p = NULL; |
090089c4 | 371 | int i; |
1061b406 | 372 | int reqnum = 0; |
6d2296d4 | 373 | int flags; |
9dee2904 | 374 | icp_common_t *query; |
429fdbec | 375 | int queries_sent = 0; |
0a0bf5db | 376 | int peers_pinged = 0; |
090089c4 | 377 | |
40a1495e | 378 | if (Config.peers == NULL) |
090089c4 | 379 | return 0; |
ce66013b | 380 | if (theOutIcpConnection < 0) |
381 | fatal("neighborsUdpPing: There is no ICP socket!"); | |
9fb13bb6 | 382 | assert(entry->swap_status == SWAPOUT_NONE); |
b6c0e933 | 383 | mem->start_ping = current_time; |
b4e7f82d | 384 | mem->ping_reply_callback = callback; |
b6c0e933 | 385 | mem->ircb_data = callback_data; |
465dc415 | 386 | *timeout = 0.0; |
007b8be4 | 387 | reqnum = icpSetCacheKey(entry->key); |
40a1495e | 388 | for (i = 0, p = first_ping; i++ < Config.npeers; p = p->next) { |
b69f7771 | 389 | if (p == NULL) |
40a1495e | 390 | p = Config.peers; |
a3d5953d | 391 | debug(15, 5) ("neighborsUdpPing: Peer %s\n", p->host); |
b69f7771 | 392 | if (!peerWouldBePinged(p, request)) |
deb79f06 | 393 | continue; /* next peer */ |
0a0bf5db | 394 | peers_pinged++; |
a3d5953d | 395 | debug(15, 4) ("neighborsUdpPing: pinging peer %s for '%s'\n", |
b69f7771 | 396 | p->host, url); |
397 | if (p->type == PEER_MULTICAST) | |
03a1ee42 | 398 | mcastSetTtl(theOutIcpConnection, p->mcast.ttl); |
3a4a63e7 | 399 | debug(15, 3) ("neighborsUdpPing: key = '%s'\n", storeKeyText(entry->key)); |
a3d5953d | 400 | debug(15, 3) ("neighborsUdpPing: reqnum = %d\n", reqnum); |
090089c4 | 401 | |
dc9d133b | 402 | #if USE_HTCP |
cd196bc8 | 403 | if (p->options.htcp) { |
1994bb24 | 404 | debug(15, 3) ("neighborsUdpPing: sending HTCP query\n"); |
176ae152 | 405 | htcpQuery(entry, request, p); |
dc9d133b | 406 | } else |
407 | #endif | |
399cabec | 408 | if (p->icp.port == echo_port) { |
a3d5953d | 409 | debug(15, 4) ("neighborsUdpPing: Looks like a dumb cache, send DECHO ping\n"); |
1061b406 | 410 | echo_hdr.reqnum = reqnum; |
27cd7235 | 411 | query = icpCreateMessage(ICP_DECHO, 0, url, reqnum, 0); |
af00901c | 412 | icpUdpSend(theOutIcpConnection, |
b69f7771 | 413 | &p->in_addr, |
9dee2904 | 414 | query, |
721b3dd8 | 415 | LOG_ICP_QUERY, |
aee4e794 | 416 | 0); |
090089c4 | 417 | } else { |
6d2296d4 | 418 | flags = 0; |
17a0a4ee | 419 | if (Config.onoff.query_icmp) |
399cabec | 420 | if (p->icp.version == ICP_VERSION_2) |
429fdbec | 421 | flags |= ICP_FLAG_SRC_RTT; |
27cd7235 | 422 | query = icpCreateMessage(ICP_QUERY, flags, url, reqnum, 0); |
af00901c | 423 | icpUdpSend(theOutIcpConnection, |
b69f7771 | 424 | &p->in_addr, |
9dee2904 | 425 | query, |
721b3dd8 | 426 | LOG_ICP_QUERY, |
aee4e794 | 427 | 0); |
090089c4 | 428 | } |
429fdbec | 429 | queries_sent++; |
090089c4 | 430 | |
b69f7771 | 431 | p->stats.pings_sent++; |
b69f7771 | 432 | if (p->type == PEER_MULTICAST) { |
dc835977 | 433 | /* |
434 | * set a bogus last_reply time so neighborUp() never | |
435 | * says a multicast peer is dead. | |
436 | */ | |
437 | p->stats.last_reply = squid_curtime; | |
b69f7771 | 438 | (*exprep) += p->mcast.n_replies_expected; |
47b780e1 | 439 | } else if (neighborUp(p)) { |
440 | /* its alive, expect a reply from it */ | |
441 | (*exprep)++; | |
465dc415 | 442 | (*timeout) += p->stats.rtt; |
090089c4 | 443 | } else { |
1061b406 | 444 | /* Neighbor is dead; ping it anyway, but don't expect a reply */ |
090089c4 | 445 | /* log it once at the threshold */ |
dc835977 | 446 | if (p->stats.logged_state == PEER_ALIVE) { |
ab4b78e9 | 447 | debug(15, 1) ("Detected DEAD %s: %s/%d/%d\n", |
b69f7771 | 448 | neighborTypeStr(p), |
399cabec | 449 | p->host, p->http_port, p->icp.port); |
dc835977 | 450 | p->stats.logged_state = PEER_DEAD; |
090089c4 | 451 | } |
452 | } | |
dc835977 | 453 | p->stats.last_query = squid_curtime; |
83b381d5 | 454 | if (p->stats.probe_start == 0) |
455 | p->stats.probe_start = squid_curtime; | |
090089c4 | 456 | } |
40a1495e | 457 | if ((first_ping = first_ping->next) == NULL) |
458 | first_ping = Config.peers; | |
090089c4 | 459 | |
0ab965da | 460 | #if ALLOW_SOURCE_PING |
090089c4 | 461 | /* only do source_ping if we have neighbors */ |
40a1495e | 462 | if (Config.npeers) { |
a369131d | 463 | const ipcache_addrs *ia = NULL; |
464 | struct sockaddr_in to_addr; | |
465 | char *host = request->host; | |
17a0a4ee | 466 | if (!Config.onoff.source_ping) { |
a3d5953d | 467 | debug(15, 6) ("neighborsUdpPing: Source Ping is disabled.\n"); |
0a0bf5db | 468 | } else if ((ia = ipcache_gethostbyname(host, 0))) { |
a3d5953d | 469 | debug(15, 6) ("neighborsUdpPing: Source Ping: to %s for '%s'\n", |
1061b406 | 470 | host, url); |
1061b406 | 471 | echo_hdr.reqnum = reqnum; |
16b204c4 | 472 | if (icmp_sock != -1) { |
e5f6c5c2 | 473 | icmpSourcePing(ia->in_addrs[ia->cur], &echo_hdr, url); |
16b204c4 | 474 | } else { |
475 | to_addr.sin_family = AF_INET; | |
e5f6c5c2 | 476 | to_addr.sin_addr = ia->in_addrs[ia->cur]; |
16b204c4 | 477 | to_addr.sin_port = htons(echo_port); |
27cd7235 | 478 | query = icpCreateMessage(ICP_SECHO, 0, url, reqnum, 0); |
16b204c4 | 479 | icpUdpSend(theOutIcpConnection, |
16b204c4 | 480 | &to_addr, |
9dee2904 | 481 | query, |
721b3dd8 | 482 | LOG_ICP_QUERY, |
aee4e794 | 483 | 0); |
16b204c4 | 484 | } |
090089c4 | 485 | } else { |
a3d5953d | 486 | debug(15, 6) ("neighborsUdpPing: Source Ping: unknown host: %s\n", |
1061b406 | 487 | host); |
090089c4 | 488 | } |
489 | } | |
429fdbec | 490 | #endif |
52040193 | 491 | /* |
465dc415 | 492 | * If there is a configured timeout, use it |
52040193 | 493 | */ |
465dc415 | 494 | if (Config.Timeout.icp_query) |
bb337ad7 | 495 | *timeout = Config.Timeout.icp_query; |
28993292 | 496 | else { |
497 | if (*exprep > 0) | |
498 | (*timeout) = 2 * (*timeout) / (*exprep); | |
499 | else | |
500 | *timeout = 2000; /* 2 seconds */ | |
501 | if (Config.Timeout.icp_query_max) | |
502 | if (*timeout > Config.Timeout.icp_query_max) | |
503 | *timeout = Config.Timeout.icp_query_max; | |
504 | } | |
0a0bf5db | 505 | return peers_pinged; |
090089c4 | 506 | } |
507 | ||
26b164ac | 508 | /* lookup the digest of a given peer */ |
509 | lookup_t | |
4b4cd312 | 510 | peerDigestLookup(peer * p, request_t * request, StoreEntry * entry) |
26b164ac | 511 | { |
6cfa8966 | 512 | #if USE_CACHE_DIGESTS |
0c511722 | 513 | const cache_key *key = request ? storeKeyPublic(storeUrl(entry), request->method) : NULL; |
26b164ac | 514 | assert(p); |
0c511722 | 515 | assert(request); |
69c95dd3 | 516 | debug(15, 5) ("peerDigestLookup: peer %s\n", p->host); |
4b4cd312 | 517 | /* does the peeer have a valid digest? */ |
e13ee7ad | 518 | if (!p->digest) { |
519 | debug(15, 5) ("peerDigestLookup: gone!\n"); | |
26b164ac | 520 | return LOOKUP_NONE; |
9689d97c | 521 | } else if (!peerHTTPOkay(p, request)) { |
e13ee7ad | 522 | debug(15, 5) ("peerDigestLookup: !peerHTTPOkay\n"); |
26b164ac | 523 | return LOOKUP_NONE; |
e13ee7ad | 524 | } else if (p->digest->flags.usable) { |
525 | debug(15, 5) ("peerDigestLookup: usable\n"); | |
4b4cd312 | 526 | /* fall through; put here to have common case on top */ ; |
e13ee7ad | 527 | } else if (!p->digest->flags.needed) { |
528 | debug(15, 5) ("peerDigestLookup: note need\n"); | |
529 | peerDigestNeeded(p->digest); | |
26b164ac | 530 | return LOOKUP_NONE; |
531 | } else { | |
e13ee7ad | 532 | debug(15, 5) ("peerDigestLookup: !ready && %srequested\n", |
533 | p->digest->flags.requested ? "" : "!"); | |
26b164ac | 534 | return LOOKUP_NONE; |
535 | } | |
69c95dd3 | 536 | debug(15, 5) ("peerDigestLookup: OK to lookup peer %s\n", p->host); |
e13ee7ad | 537 | assert(p->digest->cd); |
26b164ac | 538 | /* does digest predict a hit? */ |
e13ee7ad | 539 | if (!cacheDigestTest(p->digest->cd, key)) |
26b164ac | 540 | return LOOKUP_MISS; |
69c95dd3 | 541 | debug(15, 5) ("peerDigestLookup: peer %s says HIT!\n", p->host); |
26b164ac | 542 | return LOOKUP_HIT; |
543 | #endif | |
544 | return LOOKUP_NONE; | |
545 | } | |
546 | ||
547 | /* select best peer based on cache digests */ | |
a54d3f8e | 548 | peer * |
549 | neighborsDigestSelect(request_t * request, StoreEntry * entry) | |
550 | { | |
551 | peer *best_p = NULL; | |
6cfa8966 | 552 | #if USE_CACHE_DIGESTS |
a54d3f8e | 553 | const cache_key *key; |
554 | int best_rtt = 0; | |
555 | int choice_count = 0; | |
556 | int ichoice_count = 0; | |
557 | peer *p; | |
558 | int p_rtt; | |
559 | int i; | |
03058802 | 560 | if (!request->flags.hierarchical) |
561 | return NULL; | |
a54d3f8e | 562 | key = storeKeyPublic(storeUrl(entry), request->method); |
563 | for (i = 0, p = first_ping; i++ < Config.npeers; p = p->next) { | |
26b164ac | 564 | lookup_t lookup; |
a54d3f8e | 565 | if (!p) |
566 | p = Config.peers; | |
567 | if (i == 1) | |
568 | first_ping = p; | |
26b164ac | 569 | lookup = peerDigestLookup(p, request, entry); |
570 | if (lookup == LOOKUP_NONE) | |
a54d3f8e | 571 | continue; |
a54d3f8e | 572 | choice_count++; |
26b164ac | 573 | if (lookup == LOOKUP_MISS) |
a54d3f8e | 574 | continue; |
26b164ac | 575 | p_rtt = netdbHostRtt(p->host); |
576 | debug(15, 5) ("neighborsDigestSelect: peer %s rtt: %d\n", | |
a54d3f8e | 577 | p->host, p_rtt); |
578 | /* is this peer better than others in terms of rtt ? */ | |
579 | if (!best_p || (p_rtt && p_rtt < best_rtt)) { | |
580 | best_p = p; | |
581 | best_rtt = p_rtt; | |
4b4cd312 | 582 | if (p_rtt) /* informative choice (aka educated guess) */ |
a54d3f8e | 583 | ichoice_count++; |
584 | debug(15, 4) ("neighborsDigestSelect: peer %s leads with rtt %d\n", | |
585 | p->host, best_rtt); | |
586 | } | |
587 | } | |
26b164ac | 588 | debug(15, 4) ("neighborsDigestSelect: choices: %d (%d)\n", |
589 | choice_count, ichoice_count); | |
590 | peerNoteDigestLookup(request, best_p, | |
591 | best_p ? LOOKUP_HIT : (choice_count ? LOOKUP_MISS : LOOKUP_NONE)); | |
a54d3f8e | 592 | request->hier.n_choices = choice_count; |
593 | request->hier.n_ichoices = ichoice_count; | |
594 | #endif | |
595 | return best_p; | |
596 | } | |
597 | ||
26b164ac | 598 | void |
4b4cd312 | 599 | peerNoteDigestLookup(request_t * request, peer * p, lookup_t lookup) |
26b164ac | 600 | { |
6cfa8966 | 601 | #if USE_CACHE_DIGESTS |
26b164ac | 602 | if (p) |
603 | strncpy(request->hier.cd_host, p->host, sizeof(request->hier.cd_host)); | |
604 | else | |
605 | *request->hier.cd_host = '\0'; | |
606 | request->hier.cd_lookup = lookup; | |
607 | debug(15, 4) ("peerNoteDigestLookup: peer %s, lookup: %s\n", | |
608 | p ? p->host : "<none>", lookup_t_str[lookup]); | |
609 | #endif | |
610 | } | |
611 | ||
d73844ca | 612 | static void |
b69f7771 | 613 | neighborAlive(peer * p, const MemObject * mem, const icp_common_t * header) |
4eda6afe | 614 | { |
39ac34a9 | 615 | if (p->stats.logged_state == PEER_DEAD && p->tcp_up) { |
ab4b78e9 | 616 | debug(15, 1) ("Detected REVIVED %s: %s/%d/%d\n", |
b69f7771 | 617 | neighborTypeStr(p), |
399cabec | 618 | p->host, p->http_port, p->icp.port); |
dc835977 | 619 | p->stats.logged_state = PEER_ALIVE; |
4eda6afe | 620 | } |
dc835977 | 621 | p->stats.last_reply = squid_curtime; |
83b381d5 | 622 | p->stats.probe_start = 0; |
44f2f2e4 | 623 | p->stats.pings_acked++; |
27cd7235 | 624 | if ((icp_opcode) header->opcode <= ICP_END) |
399cabec | 625 | p->icp.counts[header->opcode]++; |
44f2f2e4 | 626 | p->icp.version = (int) header->version; |
627 | } | |
628 | ||
629 | static void | |
630 | neighborUpdateRtt(peer * p, MemObject * mem) | |
631 | { | |
632 | int rtt; | |
d2cd6935 | 633 | if (!mem) |
634 | return; | |
44f2f2e4 | 635 | if (!mem->start_ping.tv_sec) |
636 | return; | |
637 | rtt = tvSubMsec(mem->start_ping, current_time); | |
1b6ef2d2 | 638 | if (rtt < 1 || rtt > 10000) |
639 | return; | |
640 | p->stats.rtt = intAverage(p->stats.rtt, rtt, | |
d46a87a8 | 641 | p->stats.pings_acked, RTT_AV_FACTOR); |
4eda6afe | 642 | } |
090089c4 | 643 | |
399cabec | 644 | #if USE_HTCP |
645 | static void | |
646 | neighborAliveHtcp(peer * p, const MemObject * mem, const htcpReplyData * htcp) | |
647 | { | |
399cabec | 648 | if (p->stats.logged_state == PEER_DEAD && p->tcp_up) { |
649 | debug(15, 1) ("Detected REVIVED %s: %s/%d/%d\n", | |
650 | neighborTypeStr(p), | |
651 | p->host, p->http_port, p->icp.port); | |
652 | p->stats.logged_state = PEER_ALIVE; | |
653 | } | |
654 | p->stats.last_reply = squid_curtime; | |
83b381d5 | 655 | p->stats.probe_start = 0; |
44f2f2e4 | 656 | p->stats.pings_acked++; |
399cabec | 657 | p->htcp.counts[htcp->hit ? 1 : 0]++; |
44f2f2e4 | 658 | p->htcp.version = htcp->version; |
399cabec | 659 | } |
660 | #endif | |
661 | ||
38792624 | 662 | static void |
56b62ecc | 663 | neighborCountIgnored(peer * p) |
a7e59001 | 664 | { |
b69f7771 | 665 | if (p == NULL) |
a7e59001 | 666 | return; |
b69f7771 | 667 | p->stats.ignored_replies++; |
c7a3724d | 668 | NLateReplies++; |
a7e59001 | 669 | } |
670 | ||
e102ebda | 671 | static peer *non_peers = NULL; |
672 | ||
673 | static void | |
674 | neighborIgnoreNonPeer(const struct sockaddr_in *from, icp_opcode opcode) | |
675 | { | |
676 | peer *np; | |
677 | double x; | |
678 | for (np = non_peers; np; np = np->next) { | |
679 | if (np->in_addr.sin_addr.s_addr != from->sin_addr.s_addr) | |
680 | continue; | |
681 | if (np->in_addr.sin_port != from->sin_port) | |
682 | continue; | |
683 | break; | |
684 | } | |
685 | if (np == NULL) { | |
686 | np = xcalloc(1, sizeof(peer)); | |
687 | np->in_addr.sin_addr = from->sin_addr; | |
688 | np->in_addr.sin_port = from->sin_port; | |
399cabec | 689 | np->icp.port = ntohl(from->sin_port); |
e102ebda | 690 | np->type = PEER_NONE; |
691 | np->host = xstrdup(inet_ntoa(from->sin_addr)); | |
692 | np->next = non_peers; | |
693 | non_peers = np; | |
694 | } | |
695 | np->stats.ignored_replies++; | |
399cabec | 696 | np->icp.counts[opcode]++; |
e102ebda | 697 | x = log(np->stats.ignored_replies) / log(10.0); |
698 | if (0.0 != x - (double) (int) x) | |
699 | return; | |
700 | debug(15, 1) ("WARNING: Ignored %d replies from non-peer %s\n", | |
701 | np->stats.ignored_replies, np->host); | |
702 | } | |
703 | ||
429fdbec | 704 | /* ignoreMulticastReply |
705 | * | |
706 | * We want to ignore replies from multicast peers if the | |
707 | * cache_host_domain rules would normally prevent the peer | |
708 | * from being used | |
709 | */ | |
710 | static int | |
b69f7771 | 711 | ignoreMulticastReply(peer * p, MemObject * mem) |
429fdbec | 712 | { |
b69f7771 | 713 | if (p == NULL) |
429fdbec | 714 | return 0; |
cd196bc8 | 715 | if (!p->options.mcast_responder) |
429fdbec | 716 | return 0; |
b69f7771 | 717 | if (peerHTTPOkay(p, mem->request)) |
429fdbec | 718 | return 0; |
719 | return 1; | |
720 | } | |
721 | ||
722 | /* I should attach these records to the entry. We take the first | |
723 | * hit we get our wait until everyone misses. The timeout handler | |
724 | * call needs to nip this shopping list or call one of the misses. | |
725 | * | |
726 | * If a hit process is already started, then sobeit | |
727 | */ | |
b8d8561b | 728 | void |
5ad33356 | 729 | neighborsUdpAck(const cache_key * key, icp_common_t * header, const struct sockaddr_in *from) |
090089c4 | 730 | { |
b69f7771 | 731 | peer *p = NULL; |
5ad33356 | 732 | StoreEntry *entry; |
733 | MemObject *mem = NULL; | |
b6c0e933 | 734 | peer_t ntype = PEER_NONE; |
5e5126cc | 735 | char *opcode_d; |
a7e59001 | 736 | icp_opcode opcode = (icp_opcode) header->opcode; |
090089c4 | 737 | |
5ad33356 | 738 | debug(15, 6) ("neighborsUdpAck: opcode %d '%s'\n", |
739 | (int) opcode, storeKeyText(key)); | |
740 | if (NULL != (entry = storeGet(key))) | |
741 | mem = entry->mem_obj; | |
b69f7771 | 742 | if ((p = whichPeer(from))) |
743 | neighborAlive(p, mem, header); | |
27cd7235 | 744 | if (opcode > ICP_END) |
d2af9477 | 745 | return; |
27cd7235 | 746 | opcode_d = icp_opcode_str[opcode]; |
41587298 | 747 | if (p) |
748 | neighborUpdateRtt(p, mem); | |
5ad33356 | 749 | /* Does the entry exist? */ |
750 | if (NULL == entry) { | |
751 | debug(12, 3) ("neighborsUdpAck: Cache key '%s' not found\n", | |
752 | storeKeyText(key)); | |
56b62ecc | 753 | neighborCountIgnored(p); |
5ad33356 | 754 | return; |
755 | } | |
2d1c6a4f | 756 | /* check if someone is already fetching it */ |
d46a87a8 | 757 | if (EBIT_TEST(entry->flags, ENTRY_DISPATCHED)) { |
5ad33356 | 758 | debug(15, 3) ("neighborsUdpAck: '%s' already being fetched.\n", |
759 | storeKeyText(key)); | |
56b62ecc | 760 | neighborCountIgnored(p); |
d2af9477 | 761 | return; |
762 | } | |
2d1c6a4f | 763 | if (mem == NULL) { |
5ad33356 | 764 | debug(15, 2) ("Ignoring %s for missing mem_obj: %s\n", |
765 | opcode_d, storeKeyText(key)); | |
56b62ecc | 766 | neighborCountIgnored(p); |
8de2f7ad | 767 | return; |
768 | } | |
769 | if (entry->ping_status != PING_WAITING) { | |
674ac814 | 770 | debug(15, 2) ("neighborsUdpAck: Late %s for %s\n", |
5ad33356 | 771 | opcode_d, storeKeyText(key)); |
56b62ecc | 772 | neighborCountIgnored(p); |
8de2f7ad | 773 | return; |
774 | } | |
4eda6afe | 775 | if (entry->lock_count == 0) { |
5ad33356 | 776 | debug(12, 1) ("neighborsUdpAck: '%s' has no locks\n", |
777 | storeKeyText(key)); | |
56b62ecc | 778 | neighborCountIgnored(p); |
4eda6afe | 779 | return; |
090089c4 | 780 | } |
a3d5953d | 781 | debug(15, 3) ("neighborsUdpAck: %s for '%s' from %s \n", |
5ad33356 | 782 | opcode_d, storeKeyText(key), p ? p->host : "source"); |
44f2f2e4 | 783 | if (p) { |
b69f7771 | 784 | ntype = neighborType(p, mem->request); |
44f2f2e4 | 785 | } |
b69f7771 | 786 | if (ignoreMulticastReply(p, mem)) { |
56b62ecc | 787 | neighborCountIgnored(p); |
27cd7235 | 788 | } else if (opcode == ICP_MISS) { |
b69f7771 | 789 | if (p == NULL) { |
e102ebda | 790 | neighborIgnoreNonPeer(from, opcode); |
d2af9477 | 791 | } else { |
b4e7f82d | 792 | mem->ping_reply_callback(p, ntype, PROTO_ICP, header, mem->ircb_data); |
30a4f2a8 | 793 | } |
a7c05555 | 794 | } else if (opcode == ICP_HIT) { |
b69f7771 | 795 | if (p == NULL) { |
e102ebda | 796 | neighborIgnoreNonPeer(from, opcode); |
d2af9477 | 797 | } else { |
27cd7235 | 798 | header->opcode = ICP_HIT; |
b4e7f82d | 799 | mem->ping_reply_callback(p, ntype, PROTO_ICP, header, mem->ircb_data); |
090089c4 | 800 | } |
27cd7235 | 801 | } else if (opcode == ICP_DECHO) { |
b69f7771 | 802 | if (p == NULL) { |
e102ebda | 803 | neighborIgnoreNonPeer(from, opcode); |
deb79f06 | 804 | } else if (ntype == PEER_SIBLING) { |
5e604a0e | 805 | debug_trap("neighborsUdpAck: Found non-ICP cache as SIBLING\n"); |
806 | debug_trap("neighborsUdpAck: non-ICP neighbors must be a PARENT\n"); | |
090089c4 | 807 | } else { |
b4e7f82d | 808 | mem->ping_reply_callback(p, ntype, PROTO_ICP, header, mem->ircb_data); |
090089c4 | 809 | } |
27cd7235 | 810 | } else if (opcode == ICP_SECHO) { |
b69f7771 | 811 | if (p) { |
a3d5953d | 812 | debug(15, 1) ("Ignoring SECHO from neighbor %s\n", p->host); |
56b62ecc | 813 | neighborCountIgnored(p); |
0ab965da | 814 | #if ALLOW_SOURCE_PING |
815 | } else if (Config.onoff.source_ping) { | |
b4e7f82d | 816 | mem->ping_reply_callback(NULL, ntype, PROTO_ICP, header, mem->ircb_data); |
0ab965da | 817 | #endif |
818 | } else { | |
819 | debug(15, 1) ("Unsolicited SECHO from %s\n", inet_ntoa(from->sin_addr)); | |
090089c4 | 820 | } |
27cd7235 | 821 | } else if (opcode == ICP_DENIED) { |
b69f7771 | 822 | if (p == NULL) { |
e102ebda | 823 | neighborIgnoreNonPeer(from, opcode); |
b69f7771 | 824 | } else if (p->stats.pings_acked > 100) { |
399cabec | 825 | if (100 * p->icp.counts[ICP_DENIED] / p->stats.pings_acked > 95) { |
a3d5953d | 826 | debug(15, 0) ("95%% of replies from '%s' are UDP_DENIED\n", p->host); |
827 | debug(15, 0) ("Disabling '%s', please check your configuration.\n", p->host); | |
b69f7771 | 828 | neighborRemove(p); |
829 | p = NULL; | |
e006d372 | 830 | } else { |
56b62ecc | 831 | neighborCountIgnored(p); |
30a4f2a8 | 832 | } |
833 | } | |
27cd7235 | 834 | } else if (opcode == ICP_MISS_NOFETCH) { |
b4e7f82d | 835 | mem->ping_reply_callback(p, ntype, PROTO_ICP, header, mem->ircb_data); |
090089c4 | 836 | } else { |
a3d5953d | 837 | debug(15, 0) ("neighborsUdpAck: Unexpected ICP reply: %s\n", opcode_d); |
d2af9477 | 838 | } |
090089c4 | 839 | } |
840 | ||
deb79f06 | 841 | peer * |
40a1495e | 842 | peerFindByName(const char *name) |
98ffb7e4 | 843 | { |
b69f7771 | 844 | peer *p = NULL; |
40a1495e | 845 | for (p = Config.peers; p; p = p->next) { |
b69f7771 | 846 | if (!strcasecmp(name, p->host)) |
98ffb7e4 | 847 | break; |
848 | } | |
b69f7771 | 849 | return p; |
98ffb7e4 | 850 | } |
b012353a | 851 | |
db1cd23c | 852 | peer * |
853 | peerFindByNameAndPort(const char *name, unsigned short port) | |
854 | { | |
855 | peer *p = NULL; | |
856 | for (p = Config.peers; p; p = p->next) { | |
857 | if (strcasecmp(name, p->host)) | |
858 | continue; | |
859 | if (port != p->http_port) | |
860 | continue; | |
861 | break; | |
862 | } | |
863 | return p; | |
864 | } | |
865 | ||
5269d0bd | 866 | int |
b69f7771 | 867 | neighborUp(const peer * p) |
5269d0bd | 868 | { |
b69f7771 | 869 | if (!p->tcp_up) |
03b82057 | 870 | return 0; |
83b381d5 | 871 | if (p->options.no_query) |
03b82057 | 872 | return 1; |
83b381d5 | 873 | if (p->stats.probe_start != 0 && |
874 | squid_curtime - p->stats.probe_start > Config.Timeout.deadPeer) | |
03b82057 | 875 | return 0; |
876 | return 1; | |
5269d0bd | 877 | } |
878 | ||
e6e3b09b | 879 | void |
db1cd23c | 880 | peerDestroy(void *data, int unused) |
e6e3b09b | 881 | { |
db1cd23c | 882 | peer *p = data; |
ee4a1f5d | 883 | struct _domain_ping *l = NULL; |
884 | struct _domain_ping *nl = NULL; | |
b69f7771 | 885 | if (p == NULL) |
3c6e634f | 886 | return; |
b6a2f15e | 887 | for (l = p->peer_domain; l; l = nl) { |
ee4a1f5d | 888 | nl = l->next; |
889 | safe_free(l->domain); | |
890 | safe_free(l); | |
891 | } | |
b69f7771 | 892 | safe_free(p->host); |
e13ee7ad | 893 | #if USE_CACHE_DIGESTS |
b6a2f15e | 894 | if (p->digest) { |
895 | PeerDigest *pd = p->digest; | |
896 | p->digest = NULL; | |
897 | cbdataUnlock(pd); | |
898 | } | |
e13ee7ad | 899 | #endif |
db1cd23c | 900 | xfree(p); |
e6e3b09b | 901 | } |
c7a3724d | 902 | |
e13ee7ad | 903 | void |
e16d4250 | 904 | peerNoteDigestGone(peer * p) |
e13ee7ad | 905 | { |
906 | #if USE_CACHE_DIGESTS | |
907 | if (p->digest) { | |
fcbbccfe | 908 | PeerDigest *pd = p->digest; |
e13ee7ad | 909 | p->digest = NULL; |
fcbbccfe | 910 | cbdataUnlock(pd); |
e13ee7ad | 911 | } |
912 | #endif | |
913 | } | |
914 | ||
dd4bd44e | 915 | static void |
03a1ee42 | 916 | peerDNSConfigure(const ipcache_addrs * ia, void *data) |
dd4bd44e | 917 | { |
b69f7771 | 918 | peer *p = data; |
dd4bd44e | 919 | struct sockaddr_in *ap; |
920 | int j; | |
b69f7771 | 921 | if (p->n_addresses == 0) { |
a3d5953d | 922 | debug(15, 1) ("Configuring %s %s/%d/%d\n", neighborTypeStr(p), |
399cabec | 923 | p->host, p->http_port, p->icp.port); |
b69f7771 | 924 | if (p->type == PEER_MULTICAST) |
a3d5953d | 925 | debug(15, 1) (" Multicast TTL = %d\n", p->mcast.ttl); |
b69f7771 | 926 | } |
927 | p->n_addresses = 0; | |
dd4bd44e | 928 | if (ia == NULL) { |
a3d5953d | 929 | debug(0, 0) ("WARNING: DNS lookup for '%s' failed!\n", p->host); |
dd4bd44e | 930 | return; |
931 | } | |
c04fe656 | 932 | if ((int) ia->count < 1) { |
a3d5953d | 933 | debug(0, 0) ("WARNING: No IP address found for '%s'!\n", p->host); |
dd4bd44e | 934 | return; |
935 | } | |
c04fe656 | 936 | for (j = 0; j < (int) ia->count && j < PEER_MAX_ADDRESSES; j++) { |
b69f7771 | 937 | p->addresses[j] = ia->in_addrs[j]; |
a3d5953d | 938 | debug(15, 2) ("--> IP address #%d: %s\n", j, inet_ntoa(p->addresses[j])); |
b69f7771 | 939 | p->n_addresses++; |
dd4bd44e | 940 | } |
b69f7771 | 941 | ap = &p->in_addr; |
dd4bd44e | 942 | memset(ap, '\0', sizeof(struct sockaddr_in)); |
943 | ap->sin_family = AF_INET; | |
b69f7771 | 944 | ap->sin_addr = p->addresses[0]; |
399cabec | 945 | ap->sin_port = htons(p->icp.port); |
b69f7771 | 946 | if (p->type == PEER_MULTICAST) |
947 | peerCountMcastPeersSchedule(p, 10); | |
1a827bc0 | 948 | if (p->type != PEER_MULTICAST) |
cd196bc8 | 949 | if (!p->options.no_netdb_exchange) |
223213df | 950 | eventAddIsh("netdbExchangeStart", netdbExchangeStart, p, 30.0, 1); |
dd4bd44e | 951 | } |
952 | ||
953 | static void | |
51242273 | 954 | peerRefreshDNS(void *data) |
dd4bd44e | 955 | { |
b69f7771 | 956 | peer *p = NULL; |
46ca5fc6 | 957 | if (eventFind(peerRefreshDNS, NULL)) |
73cb7e3a | 958 | eventDelete(peerRefreshDNS, NULL); |
51242273 | 959 | if (!data && 0 == stat5minClientRequests()) { |
1f3c4622 | 960 | /* no recent client traffic, wait a bit */ |
5999b776 | 961 | eventAddIsh("peerRefreshDNS", peerRefreshDNS, NULL, 180.0, 1); |
1f3c4622 | 962 | return; |
dd4bd44e | 963 | } |
1f3c4622 | 964 | for (p = Config.peers; p; p = p->next) |
965 | ipcache_nbgethostbyname(p->host, peerDNSConfigure, p); | |
dd4bd44e | 966 | /* Reconfigure the peers every hour */ |
52040193 | 967 | eventAddIsh("peerRefreshDNS", peerRefreshDNS, NULL, 3600.0, 1); |
dd4bd44e | 968 | } |
e924600d | 969 | |
0e7b6b06 | 970 | /* |
971 | * peerCheckConnect will NOT be called by eventRun if the peer/data | |
972 | * pointer becomes invalid. | |
973 | */ | |
e924600d | 974 | static void |
975 | peerCheckConnect(void *data) | |
976 | { | |
977 | peer *p = data; | |
978 | int fd; | |
979 | fd = comm_open(SOCK_STREAM, 0, Config.Addrs.tcp_outgoing, | |
980 | 0, COMM_NONBLOCKING, p->host); | |
981 | if (fd < 0) | |
982 | return; | |
03a1ee42 | 983 | p->test_fd = fd; |
8407afee | 984 | ipcache_nbgethostbyname(p->host, peerCheckConnect2, p); |
e924600d | 985 | } |
986 | ||
987 | static void | |
79d39a72 | 988 | peerCheckConnect2(const ipcache_addrs * ianotused, void *data) |
e924600d | 989 | { |
990 | peer *p = data; | |
03a1ee42 | 991 | commConnectStart(p->test_fd, |
e924600d | 992 | p->host, |
993 | p->http_port, | |
994 | peerCheckConnectDone, | |
995 | p); | |
996 | } | |
997 | ||
998 | static void | |
999 | peerCheckConnectDone(int fd, int status, void *data) | |
1000 | { | |
1001 | peer *p = data; | |
98829f69 | 1002 | if (status == COMM_OK) { |
1003 | p->tcp_up = PEER_TCP_MAGIC_COUNT; | |
feb88ffa | 1004 | debug(15, 1) ("TCP connection to %s/%d succeeded\n", |
e924600d | 1005 | p->host, p->http_port); |
1006 | } else { | |
98829f69 | 1007 | eventAdd("peerCheckConnect", peerCheckConnect, p, 60.0, 1); |
e924600d | 1008 | } |
1009 | comm_close(fd); | |
1010 | return; | |
1011 | } | |
1012 | ||
1013 | void | |
1014 | peerCheckConnectStart(peer * p) | |
1015 | { | |
1016 | if (!p->tcp_up) | |
1017 | return; | |
feb88ffa | 1018 | debug(15, 1) ("TCP connection to %s/%d failed\n", p->host, p->http_port); |
98829f69 | 1019 | p->tcp_up--; |
1020 | if (p->tcp_up != (PEER_TCP_MAGIC_COUNT - 1)) | |
1021 | return; | |
e924600d | 1022 | p->last_fail_time = squid_curtime; |
98829f69 | 1023 | eventAdd("peerCheckConnect", peerCheckConnect, p, 30.0, 1); |
e924600d | 1024 | } |
429fdbec | 1025 | |
1026 | static void | |
1027 | peerCountMcastPeersSchedule(peer * p, time_t when) | |
1028 | { | |
b515fc11 | 1029 | if (p->mcast.flags.count_event_pending) |
429fdbec | 1030 | return; |
1031 | eventAdd("peerCountMcastPeersStart", | |
1032 | peerCountMcastPeersStart, | |
1033 | p, | |
52040193 | 1034 | (double) when, 1); |
b515fc11 | 1035 | p->mcast.flags.count_event_pending = 1; |
429fdbec | 1036 | } |
1037 | ||
1038 | static void | |
1039 | peerCountMcastPeersStart(void *data) | |
1040 | { | |
1041 | peer *p = data; | |
1042 | ps_state *psstate = xcalloc(1, sizeof(ps_state)); | |
1043 | StoreEntry *fake; | |
1044 | MemObject *mem; | |
1045 | icp_common_t *query; | |
007b8be4 | 1046 | int reqnum; |
429fdbec | 1047 | LOCAL_ARRAY(char, url, MAX_URL); |
ce66013b | 1048 | assert(p->type == PEER_MULTICAST); |
b515fc11 | 1049 | p->mcast.flags.count_event_pending = 0; |
042461c3 | 1050 | snprintf(url, MAX_URL, "http://%s/", inet_ntoa(p->in_addr.sin_addr)); |
92695e5e | 1051 | fake = storeCreateEntry(url, url, null_request_flags, METHOD_GET); |
429fdbec | 1052 | psstate->request = requestLink(urlParse(METHOD_GET, url)); |
1053 | psstate->entry = fake; | |
1054 | psstate->callback = NULL; | |
429fdbec | 1055 | psstate->callback_data = p; |
b4e7f82d | 1056 | psstate->ping.start = current_time; |
db1cd23c | 1057 | cbdataAdd(psstate, cbdataXfree, 0); |
429fdbec | 1058 | mem = fake->mem_obj; |
2db609de | 1059 | mem->request = requestLink(psstate->request); |
429fdbec | 1060 | mem->start_ping = current_time; |
b4e7f82d | 1061 | mem->ping_reply_callback = peerCountHandleIcpReply; |
429fdbec | 1062 | mem->ircb_data = psstate; |
03a1ee42 | 1063 | mcastSetTtl(theOutIcpConnection, p->mcast.ttl); |
007b8be4 | 1064 | p->mcast.id = mem->id; |
1065 | reqnum = icpSetCacheKey(fake->key); | |
1066 | query = icpCreateMessage(ICP_QUERY, 0, url, reqnum, 0); | |
429fdbec | 1067 | icpUdpSend(theOutIcpConnection, |
1068 | &p->in_addr, | |
1069 | query, | |
721b3dd8 | 1070 | LOG_ICP_QUERY, |
aee4e794 | 1071 | 0); |
429fdbec | 1072 | fake->ping_status = PING_WAITING; |
1073 | eventAdd("peerCountMcastPeersDone", | |
1074 | peerCountMcastPeersDone, | |
2db609de | 1075 | psstate, |
465dc415 | 1076 | (double) Config.Timeout.mcast_icp_query, 1); |
b515fc11 | 1077 | p->mcast.flags.counting = 1; |
429fdbec | 1078 | peerCountMcastPeersSchedule(p, MCAST_COUNT_RATE); |
1079 | } | |
1080 | ||
1081 | static void | |
1082 | peerCountMcastPeersDone(void *data) | |
1083 | { | |
1084 | ps_state *psstate = data; | |
1085 | peer *p = psstate->callback_data; | |
1086 | StoreEntry *fake = psstate->entry; | |
b515fc11 | 1087 | p->mcast.flags.counting = 0; |
9e4ad609 | 1088 | p->mcast.avg_n_members = doubleAverage(p->mcast.avg_n_members, |
b4e7f82d | 1089 | (double) psstate->ping.n_recv, |
9e4ad609 | 1090 | ++p->mcast.n_times_counted, |
1091 | 10); | |
1a827bc0 | 1092 | debug(15, 1) ("Group %s: %d replies, %4.1f average, RTT %d\n", |
429fdbec | 1093 | p->host, |
b4e7f82d | 1094 | psstate->ping.n_recv, |
1a827bc0 | 1095 | p->mcast.avg_n_members, |
1096 | p->stats.rtt); | |
429fdbec | 1097 | p->mcast.n_replies_expected = (int) p->mcast.avg_n_members; |
b7fe0ab0 | 1098 | EBIT_SET(fake->flags, ENTRY_ABORTED); |
e7a22b88 | 1099 | requestUnlink(fake->mem_obj->request); |
f0d5043e | 1100 | fake->mem_obj->request = NULL; |
429fdbec | 1101 | storeReleaseRequest(fake); |
1102 | storeUnlockObject(fake); | |
e7a22b88 | 1103 | requestUnlink(psstate->request); |
1a827bc0 | 1104 | cbdataFree(psstate); |
429fdbec | 1105 | } |
1106 | ||
1107 | static void | |
5942e8d4 | 1108 | peerCountHandleIcpReply(peer * p, peer_t type, protocol_t proto, void *hdrnotused, void *data) |
429fdbec | 1109 | { |
1110 | ps_state *psstate = data; | |
1a827bc0 | 1111 | StoreEntry *fake = psstate->entry; |
1112 | MemObject *mem = fake->mem_obj; | |
1113 | int rtt = tvSubMsec(mem->start_ping, current_time); | |
86aebcda | 1114 | assert(proto == PROTO_ICP); |
1a827bc0 | 1115 | assert(fake); |
1116 | assert(mem); | |
b4e7f82d | 1117 | psstate->ping.n_recv++; |
1118 | p->stats.rtt = intAverage(p->stats.rtt, rtt, psstate->ping.n_recv, RTT_AV_FACTOR); | |
429fdbec | 1119 | } |
ed7f5615 | 1120 | |
1121 | static void | |
1122 | neighborDumpPeers(StoreEntry * sentry) | |
1123 | { | |
1124 | dump_peers(sentry, Config.peers); | |
1125 | } | |
1126 | ||
1127 | static void | |
1128 | neighborDumpNonPeers(StoreEntry * sentry) | |
1129 | { | |
1130 | dump_peers(sentry, non_peers); | |
1131 | } | |
1132 | ||
a369131d | 1133 | void |
1134 | dump_peer_options(StoreEntry * sentry, peer * p) | |
1135 | { | |
cd196bc8 | 1136 | if (p->options.proxy_only) |
a369131d | 1137 | storeAppendPrintf(sentry, " proxy-only"); |
cd196bc8 | 1138 | if (p->options.no_query) |
a369131d | 1139 | storeAppendPrintf(sentry, " no-query"); |
cd196bc8 | 1140 | if (p->options.no_digest) |
8638fc66 | 1141 | storeAppendPrintf(sentry, " no-digest"); |
cd196bc8 | 1142 | if (p->options.default_parent) |
a369131d | 1143 | storeAppendPrintf(sentry, " default"); |
cd196bc8 | 1144 | if (p->options.roundrobin) |
a369131d | 1145 | storeAppendPrintf(sentry, " round-robin"); |
cd196bc8 | 1146 | if (p->options.mcast_responder) |
a369131d | 1147 | storeAppendPrintf(sentry, " multicast-responder"); |
cd196bc8 | 1148 | if (p->options.closest_only) |
a369131d | 1149 | storeAppendPrintf(sentry, " closest-only"); |
dc9d133b | 1150 | #if USE_HTCP |
cd196bc8 | 1151 | if (p->options.htcp) |
e8d185d2 | 1152 | storeAppendPrintf(sentry, " htcp"); |
dc9d133b | 1153 | #endif |
cd196bc8 | 1154 | if (p->options.no_netdb_exchange) |
1a5dae67 | 1155 | storeAppendPrintf(sentry, " no-netdb-exchange"); |
95e36d02 | 1156 | #if DELAY_POOLS |
cd196bc8 | 1157 | if (p->options.no_delay) |
95e36d02 | 1158 | storeAppendPrintf(sentry, " no-delay"); |
1159 | #endif | |
c68e9c6b | 1160 | if (p->login) |
1161 | storeAppendPrintf(sentry, " login=%s", p->login); | |
a369131d | 1162 | if (p->mcast.ttl > 0) |
1163 | storeAppendPrintf(sentry, " ttl=%d", p->mcast.ttl); | |
1164 | storeAppendPrintf(sentry, "\n"); | |
1165 | } | |
1166 | ||
ed7f5615 | 1167 | static void |
1168 | dump_peers(StoreEntry * sentry, peer * peers) | |
1169 | { | |
1170 | peer *e = NULL; | |
1171 | struct _domain_ping *d = NULL; | |
1172 | icp_opcode op; | |
06e6d12a | 1173 | int i; |
ed7f5615 | 1174 | if (peers == NULL) |
1175 | storeAppendPrintf(sentry, "There are no neighbors installed.\n"); | |
1176 | for (e = peers; e; e = e->next) { | |
1177 | assert(e->host != NULL); | |
1178 | storeAppendPrintf(sentry, "\n%-11.11s: %s/%d/%d\n", | |
1179 | neighborTypeStr(e), | |
1180 | e->host, | |
1181 | e->http_port, | |
399cabec | 1182 | e->icp.port); |
a369131d | 1183 | storeAppendPrintf(sentry, "Flags :"); |
1184 | dump_peer_options(sentry, e); | |
06e6d12a | 1185 | for (i = 0; i < e->n_addresses; i++) { |
1186 | storeAppendPrintf(sentry, "Address[%d] : %s\n", i, | |
1187 | inet_ntoa(e->addresses[i])); | |
1188 | } | |
ed7f5615 | 1189 | storeAppendPrintf(sentry, "Status : %s\n", |
1190 | neighborUp(e) ? "Up" : "Down"); | |
1191 | storeAppendPrintf(sentry, "AVG RTT : %d msec\n", e->stats.rtt); | |
1192 | storeAppendPrintf(sentry, "LAST QUERY : %8d seconds ago\n", | |
1193 | (int) (squid_curtime - e->stats.last_query)); | |
1194 | storeAppendPrintf(sentry, "LAST REPLY : %8d seconds ago\n", | |
1195 | (int) (squid_curtime - e->stats.last_reply)); | |
1196 | storeAppendPrintf(sentry, "PINGS SENT : %8d\n", e->stats.pings_sent); | |
1197 | storeAppendPrintf(sentry, "PINGS ACKED: %8d %3d%%\n", | |
1198 | e->stats.pings_acked, | |
1199 | percent(e->stats.pings_acked, e->stats.pings_sent)); | |
1200 | storeAppendPrintf(sentry, "FETCHES : %8d %3d%%\n", | |
1201 | e->stats.fetches, | |
1202 | percent(e->stats.fetches, e->stats.pings_acked)); | |
1203 | storeAppendPrintf(sentry, "IGNORED : %8d %3d%%\n", | |
1204 | e->stats.ignored_replies, | |
1205 | percent(e->stats.ignored_replies, e->stats.pings_acked)); | |
1206 | storeAppendPrintf(sentry, "Histogram of PINGS ACKED:\n"); | |
1994bb24 | 1207 | #if USE_HTCP |
cd196bc8 | 1208 | if (e->options.htcp) { |
1994bb24 | 1209 | storeAppendPrintf(sentry, "\tMisses\t%8d %3d%%\n", |
1210 | e->htcp.counts[0], | |
1211 | percent(e->htcp.counts[0], e->stats.pings_acked)); | |
1212 | storeAppendPrintf(sentry, "\tHits\t%8d %3d%%\n", | |
1213 | e->htcp.counts[1], | |
1214 | percent(e->htcp.counts[1], e->stats.pings_acked)); | |
1215 | } else { | |
1216 | #endif | |
5942e8d4 | 1217 | for (op = ICP_INVALID; op < ICP_END; op++) { |
1218 | if (e->icp.counts[op] == 0) | |
1219 | continue; | |
1220 | storeAppendPrintf(sentry, " %12.12s : %8d %3d%%\n", | |
1221 | icp_opcode_str[op], | |
1222 | e->icp.counts[op], | |
1223 | percent(e->icp.counts[op], e->stats.pings_acked)); | |
1224 | } | |
1994bb24 | 1225 | #if USE_HTCP |
5942e8d4 | 1226 | } |
1994bb24 | 1227 | #endif |
ed7f5615 | 1228 | if (e->last_fail_time) { |
1229 | storeAppendPrintf(sentry, "Last failed connect() at: %s\n", | |
1230 | mkhttpdlogtime(&(e->last_fail_time))); | |
1231 | } | |
b6a2f15e | 1232 | if (e->peer_domain != NULL) { |
ed7f5615 | 1233 | storeAppendPrintf(sentry, "DOMAIN LIST: "); |
b6a2f15e | 1234 | for (d = e->peer_domain; d; d = d->next) { |
5942e8d4 | 1235 | storeAppendPrintf(sentry, "%s%s ", |
1236 | d->do_ping ? null_string : "!", d->domain); | |
1994bb24 | 1237 | } |
1238 | storeAppendPrintf(sentry, "\n"); | |
ed7f5615 | 1239 | } |
99edd1c3 | 1240 | storeAppendPrintf(sentry, "keep-alive ratio: %d%%\n", |
ed7f5615 | 1241 | percent(e->stats.n_keepalives_recv, e->stats.n_keepalives_sent)); |
1242 | } | |
1243 | } | |
86aebcda | 1244 | |
1245 | #if USE_HTCP | |
1246 | void | |
1247 | neighborsHtcpReply(const cache_key * key, htcpReplyData * htcp, const struct sockaddr_in *from) | |
1248 | { | |
b4e7f82d | 1249 | StoreEntry *e = storeGet(key); |
399cabec | 1250 | MemObject *mem = NULL; |
1251 | peer *p; | |
1252 | peer_t ntype = PEER_NONE; | |
399cabec | 1253 | debug(15, 6) ("neighborsHtcpReply: %s %s\n", |
1254 | htcp->hit ? "HIT" : "MISS", storeKeyText(key)); | |
1255 | if (NULL != (e = storeGet(key))) | |
1256 | mem = e->mem_obj; | |
1257 | if ((p = whichPeer(from))) | |
1258 | neighborAliveHtcp(p, mem, htcp); | |
1259 | /* Does the entry exist? */ | |
1260 | if (NULL == e) { | |
1261 | debug(12, 3) ("neighyborsHtcpReply: Cache key '%s' not found\n", | |
1262 | storeKeyText(key)); | |
1263 | neighborCountIgnored(p); | |
1264 | return; | |
1265 | } | |
1266 | /* check if someone is already fetching it */ | |
d46a87a8 | 1267 | if (EBIT_TEST(e->flags, ENTRY_DISPATCHED)) { |
399cabec | 1268 | debug(15, 3) ("neighborsUdpAck: '%s' already being fetched.\n", |
1269 | storeKeyText(key)); | |
1270 | neighborCountIgnored(p); | |
1271 | return; | |
1272 | } | |
1273 | if (mem == NULL) { | |
1274 | debug(15, 2) ("Ignoring reply for missing mem_obj: %s\n", | |
1275 | storeKeyText(key)); | |
1276 | neighborCountIgnored(p); | |
1277 | return; | |
1278 | } | |
1279 | if (e->ping_status != PING_WAITING) { | |
1280 | debug(15, 2) ("neighborsUdpAck: Entry %s is not PING_WAITING\n", | |
1281 | storeKeyText(key)); | |
1282 | neighborCountIgnored(p); | |
1283 | return; | |
1284 | } | |
1285 | if (e->lock_count == 0) { | |
1286 | debug(12, 1) ("neighborsUdpAck: '%s' has no locks\n", | |
1287 | storeKeyText(key)); | |
1288 | neighborCountIgnored(p); | |
1289 | return; | |
1290 | } | |
44f2f2e4 | 1291 | if (p) { |
399cabec | 1292 | ntype = neighborType(p, mem->request); |
44f2f2e4 | 1293 | neighborUpdateRtt(p, mem); |
1294 | } | |
399cabec | 1295 | if (ignoreMulticastReply(p, mem)) { |
1296 | neighborCountIgnored(p); | |
1297 | return; | |
1298 | } | |
9bc73deb | 1299 | debug(15, 3) ("neighborsHtcpReply: e = %p\n", e); |
b4e7f82d | 1300 | mem->ping_reply_callback(p, ntype, PROTO_HTCP, htcp, mem->ircb_data); |
86aebcda | 1301 | } |
1302 | #endif |