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