]> git.ipfire.org Git - thirdparty/squid.git/blame - src/forward.cc
forwarding statistics
[thirdparty/squid.git] / src / forward.cc
CommitLineData
41462d93 1
2/*
8ddcc35d 3 * $Id: forward.cc,v 1.38 1998/12/16 06:04:15 wessels Exp $
41462d93 4 *
5 * DEBUG: section 17 Request Forwarding
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
e25c139f 9 * ----------------------------------------------------------
41462d93 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.
41462d93 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 *
41462d93 34 */
35
36
37#include "squid.h"
38
db1cd23c 39static PSC fwdStartComplete;
6801f8a8 40static void fwdDispatch(FwdState *);
910169e5 41static void fwdConnectStart(FwdState * fwdState);
42static void fwdStateFree(FwdState * fwdState);
43static PF fwdConnectTimeout;
44static PF fwdServerClosed;
45static CNCB fwdConnectDone;
68bd6892 46static int fwdCheckRetry(FwdState * fwdState);
db1cd23c 47static int fwdReforward(FwdState *);
48static void fwdStartFail(FwdState *);
8ddcc35d 49static void fwdLogReplyStatus(int tries, http_status status);
50static OBJH fwdStats;
51
52#define MAX_FWD_STATS_IDX 9
53static int FwdReplyCodes[MAX_FWD_STATS_IDX+1][HTTP_INVALID_HEADER+1];
db1cd23c 54
55static void
56fwdServerFree(FwdServer * fs)
57{
58 if (fs->peer)
59 cbdataUnlock(fs->peer);
60 memFree(fs, MEM_FWD_SERVER);
61}
62
63static void
64fwdServersFree(FwdServer ** FS)
65{
66 FwdServer *fs;
67 while ((fs = *FS)) {
68 *FS = fs->next;
69 fwdServerFree(fs);
70 }
71}
41462d93 72
73static void
4ca83e82 74fwdStateFree(FwdState * fwdState)
41462d93 75{
f563eea9 76 StoreEntry *e = fwdState->entry;
77 ErrorState *err;
6801f8a8 78 int sfd;
79 static int loop_detect = 0;
0bdf2621 80 debug(17, 3) ("fwdStateFree: %p\n", fwdState);
6801f8a8 81 assert(loop_detect++ == 0);
f563eea9 82 assert(e->mem_obj);
1640c2df 83 if (e->store_status == STORE_PENDING) {
84 if (e->mem_obj->inmem_hi == 0) {
85 assert(fwdState->fail.err_code);
86 err = errorCon(fwdState->fail.err_code, fwdState->fail.http_code);
87 err->request = requestLink(fwdState->request);
88 err->xerrno = fwdState->fail.xerrno;
89 errorAppendEntry(e, err);
90 } else {
91 storeAbort(e, 0);
92 }
f563eea9 93 }
db1cd23c 94 fwdServersFree(&fwdState->servers);
41462d93 95 requestUnlink(fwdState->request);
4ca83e82 96 fwdState->request = NULL;
f563eea9 97 storeUnregisterAbort(e);
98 storeUnlockObject(e);
4ca83e82 99 fwdState->entry = NULL;
6801f8a8 100 sfd = fwdState->server_fd;
101 if (sfd > -1) {
102 comm_remove_close_handler(sfd, fwdServerClosed, fwdState);
103 fwdState->server_fd = -1;
32b2931b 104 debug(17, 3) ("fwdStateFree: closing FD %d\n", sfd);
6801f8a8 105 comm_close(sfd);
106 }
41462d93 107 cbdataFree(fwdState);
6801f8a8 108 loop_detect--;
41462d93 109}
110
68bd6892 111static int
112fwdCheckRetry(FwdState * fwdState)
113{
1640c2df 114 if (fwdState->entry->store_status != STORE_PENDING)
115 return 0;
68bd6892 116 if (fwdState->entry->mem_obj->inmem_hi > 0)
117 return 0;
118 if (fwdState->n_tries > 10)
119 return 0;
120 if (squid_curtime - fwdState->start > 120)
121 return 0;
1d22e062 122 if (pumpMethod(fwdState->request->method))
123 if (0 == pumpRestart(fwdState->request))
124 return 0;
68bd6892 125 return 1;
126}
127
910169e5 128static void
129fwdServerClosed(int fd, void *data)
130{
4ca83e82 131 FwdState *fwdState = data;
68bd6892 132 debug(17, 3) ("fwdServerClosed: FD %d %s\n", fd, storeUrl(fwdState->entry));
6801f8a8 133 assert(fwdState->server_fd == fd);
134 fwdState->server_fd = -1;
68bd6892 135 if (fwdCheckRetry(fwdState)) {
32b2931b 136 debug(17, 3) ("fwdServerClosed: re-forwarding (%d tries, %d secs)\n",
9dbf253d 137 fwdState->n_tries,
138 (int) (squid_curtime - fwdState->start));
f563eea9 139 fwdConnectStart(fwdState);
140 } else {
141 fwdStateFree(fwdState);
142 }
910169e5 143}
144
41462d93 145static void
146fwdConnectDone(int server_fd, int status, void *data)
147{
148 FwdState *fwdState = data;
db1cd23c 149 FwdServer *fs = fwdState->servers;
41462d93 150 ErrorState *err;
db1cd23c 151 request_t *request = fwdState->request;
6801f8a8 152 assert(fwdState->server_fd == server_fd);
41462d93 153 if (status == COMM_ERR_DNS) {
154 debug(17, 4) ("fwdConnectDone: Unknown host: %s\n",
db1cd23c 155 request->host);
41462d93 156 err = errorCon(ERR_DNS_FAIL, HTTP_SERVICE_UNAVAILABLE);
157 err->dnsserver_msg = xstrdup(dns_error_message);
db1cd23c 158 err->request = requestLink(request);
41462d93 159 errorAppendEntry(fwdState->entry, err);
160 comm_close(server_fd);
161 } else if (status != COMM_OK) {
db1cd23c 162 assert(fs);
41462d93 163 err = errorCon(ERR_CONNECT_FAIL, HTTP_SERVICE_UNAVAILABLE);
164 err->xerrno = errno;
db1cd23c 165 if (fs->peer) {
166 err->host = xstrdup(fs->peer->host);
167 err->port = fs->peer->http_port;
168 } else {
169 err->host = xstrdup(request->host);
170 err->port = request->port;
171 }
172 err->request = requestLink(request);
41462d93 173 errorAppendEntry(fwdState->entry, err);
db1cd23c 174 if (fs->peer)
175 peerCheckConnectStart(fs->peer);
41462d93 176 comm_close(server_fd);
177 } else {
178 fd_note(server_fd, storeUrl(fwdState->entry));
179 fd_table[server_fd].uses++;
6801f8a8 180 fwdDispatch(fwdState);
41462d93 181 }
41462d93 182}
183
184static void
185fwdConnectTimeout(int fd, void *data)
186{
187 FwdState *fwdState = data;
188 StoreEntry *entry = fwdState->entry;
189 ErrorState *err;
190 debug(17, 3) ("fwdConnectTimeout: FD %d: '%s'\n", fd, storeUrl(entry));
6801f8a8 191 assert(fd == fwdState->server_fd);
41462d93 192 if (entry->mem_obj->inmem_hi == 0) {
193 err = errorCon(ERR_READ_TIMEOUT, HTTP_GATEWAY_TIMEOUT);
194 err->request = requestLink(fwdState->request);
195 errorAppendEntry(entry, err);
196 } else {
197 storeAbort(entry, 0);
198 }
199 comm_close(fd);
200}
201
202static void
203fwdConnectStart(FwdState * fwdState)
204{
205 const char *url = storeUrl(fwdState->entry);
206 int fd;
207 ErrorState *err;
db1cd23c 208 FwdServer *fs = fwdState->servers;
209 const char *host;
210 unsigned short port;
211 assert(fs);
cddc721b 212 assert(fwdState->server_fd == -1);
41462d93 213 debug(17, 3) ("fwdConnectStart: %s\n", url);
db1cd23c 214 if (fs->peer) {
215 host = fs->peer->host;
216 port = fs->peer->http_port;
217 } else {
218 host = fwdState->request->host;
219 port = fwdState->request->port;
220 }
0bdf2621 221 hierarchyNote(&fwdState->request->hier, fs->code, host);
db1cd23c 222 if ((fd = pconnPop(host, port)) >= 0) {
41462d93 223 debug(17, 3) ("fwdConnectStart: reusing pconn FD %d\n", fd);
9dbf253d 224 fwdState->server_fd = fd;
910169e5 225 comm_add_close_handler(fd, fwdServerClosed, fwdState);
41462d93 226 fwdConnectDone(fd, COMM_OK, fwdState);
227 return;
228 }
229 fd = comm_open(SOCK_STREAM,
230 0,
231 Config.Addrs.tcp_outgoing,
232 0,
233 COMM_NONBLOCKING,
234 url);
235 if (fd < 0) {
236 debug(50, 4) ("fwdConnectStart: %s\n", xstrerror());
237 err = errorCon(ERR_SOCKET_FAILURE, HTTP_INTERNAL_SERVER_ERROR);
238 err->xerrno = errno;
239 err->request = requestLink(fwdState->request);
240 errorAppendEntry(fwdState->entry, err);
910169e5 241 fwdStateFree(fwdState);
41462d93 242 return;
243 }
6801f8a8 244 fwdState->server_fd = fd;
68bd6892 245 fwdState->n_tries++;
910169e5 246 comm_add_close_handler(fd, fwdServerClosed, fwdState);
41462d93 247 commSetTimeout(fd,
248 Config.Timeout.connect,
249 fwdConnectTimeout,
250 fwdState);
db1cd23c 251 commConnectStart(fd, host, port, fwdConnectDone, fwdState);
41462d93 252}
253
254static void
db1cd23c 255fwdStartComplete(FwdServer * servers, void *data)
41462d93 256{
257 FwdState *fwdState = data;
db1cd23c 258 if (servers != NULL) {
259 fwdState->servers = servers;
260 fwdConnectStart(fwdState);
41462d93 261 } else {
db1cd23c 262 fwdStartFail(fwdState);
41462d93 263 }
41462d93 264}
265
266static void
db1cd23c 267fwdStartFail(FwdState * fwdState)
41462d93 268{
41462d93 269 ErrorState *err;
41462d93 270 err = errorCon(ERR_CANNOT_FORWARD, HTTP_SERVICE_UNAVAILABLE);
271 err->request = requestLink(fwdState->request);
272 errorAppendEntry(fwdState->entry, err);
910169e5 273 fwdStateFree(fwdState);
41462d93 274}
910169e5 275
41462d93 276static void
6801f8a8 277fwdDispatch(FwdState * fwdState)
41462d93 278{
279 peer *p;
280 request_t *request = fwdState->request;
281 StoreEntry *entry = fwdState->entry;
282 debug(17, 5) ("fwdDispatch: FD %d: Fetching '%s %s'\n",
910169e5 283 fwdState->client_fd,
41462d93 284 RequestMethodStr[request->method],
285 storeUrl(entry));
d46a87a8 286 /*assert(!EBIT_TEST(entry->flags, ENTRY_DISPATCHED)); */
41462d93 287 assert(entry->ping_status != PING_WAITING);
4ca83e82 288 assert(entry->lock_count);
d46a87a8 289 EBIT_SET(entry->flags, ENTRY_DISPATCHED);
41462d93 290 netdbPingSite(request->host);
e0ebe27c 291 /*
292 * Assert that server_fd is set. This is to guarantee that fwdState
293 * is attached to something and will be deallocated when server_fd
294 * is closed.
295 */
296 assert(fwdState->server_fd > -1);
41462d93 297 if (fwdState->servers && (p = fwdState->servers->peer)) {
298 p->stats.fetches++;
db1cd23c 299 httpStart(fwdState);
41462d93 300 } else {
301 switch (request->protocol) {
302 case PROTO_HTTP:
db1cd23c 303 httpStart(fwdState);
41462d93 304 break;
305 case PROTO_GOPHER:
db1cd23c 306 gopherStart(fwdState);
41462d93 307 break;
308 case PROTO_FTP:
db1cd23c 309 ftpStart(fwdState);
41462d93 310 break;
311 case PROTO_WAIS:
db1cd23c 312 waisStart(fwdState);
41462d93 313 break;
314 case PROTO_CACHEOBJ:
e0ebe27c 315 case PROTO_INTERNAL:
41462d93 316 case PROTO_URN:
ae0b4725 317 fatal_dump("Should never get here");
41462d93 318 break;
319 case PROTO_WHOIS:
db1cd23c 320 whoisStart(fwdState);
41462d93 321 break;
41462d93 322 default:
ce45d320 323 debug(17, 1) ("fwdDispatch: Cannot retrieve '%s'\n",
324 storeUrl(entry));
c68e9c6b 325 fwdFail(fwdState, ERR_UNSUP_REQ, HTTP_BAD_REQUEST, -1);
326 comm_close(fwdState->server_fd);
ce45d320 327 break;
41462d93 328 }
329 }
330}
331
db1cd23c 332static int
333fwdReforward(FwdState * fwdState)
334{
335 StoreEntry *e = fwdState->entry;
336 FwdServer *fs = fwdState->servers;
337 http_status s;
338 assert(e->store_status == STORE_PENDING);
339 assert(e->mem_obj);
d6eb18d6 340 if (!EBIT_TEST(e->flags, ENTRY_FWD_HDR_WAIT)) {
341 debug(17, 3) ("fwdReforward: No, ENTRY_FWD_HDR_WAIT isn't set\n");
342 return 0;
343 }
db1cd23c 344 if (fwdState->n_tries > 9)
345 return 0;
346 assert(fs);
347 fwdState->servers = fs->next;
348 fwdServerFree(fs);
349 if (fwdState->servers == NULL) {
350 debug(17, 3) ("fwdReforward: No forward-servers left\n");
351 return 0;
352 }
353 s = e->mem_obj->reply->sline.status;
354 debug(17, 3) ("fwdReforward: status %d\n", (int) s);
355 switch (s) {
356 case HTTP_FORBIDDEN:
357 case HTTP_INTERNAL_SERVER_ERROR:
358 case HTTP_NOT_IMPLEMENTED:
359 case HTTP_BAD_GATEWAY:
360 case HTTP_SERVICE_UNAVAILABLE:
361 case HTTP_GATEWAY_TIMEOUT:
362 return 1;
363 default:
364 return 0;
365 }
366 /* NOTREACHED */
367}
368
41462d93 369/* PUBLIC FUNCTIONS */
370
41462d93 371void
db1cd23c 372fwdStart(int fd, StoreEntry * e, request_t * r, struct in_addr client_addr)
41462d93 373{
374 FwdState *fwdState;
d1061d2b 375 aclCheck_t ch;
376 int answer;
377 ErrorState *err;
5843eb62 378 /*
db1cd23c 379 * client_addr == no_addr indicates this is an "internal" request
5843eb62 380 * from peer_digest.c, asn.c, netdb.c, etc and should always
381 * be allowed. yuck, I know.
d1061d2b 382 */
db1cd23c 383 if (client_addr.s_addr != no_addr.s_addr) {
5843eb62 384 /*
385 * Check if this host is allowed to fetch MISSES from us (miss_access)
386 */
387 memset(&ch, '\0', sizeof(aclCheck_t));
db1cd23c 388 ch.src_addr = client_addr;
5843eb62 389 ch.request = r;
390 answer = aclCheckFast(Config.accessList.miss, &ch);
391 if (answer == 0) {
392 err = errorCon(ERR_FORWARDING_DENIED, HTTP_FORBIDDEN);
393 err->request = requestLink(r);
db1cd23c 394 err->src_addr = client_addr;
5843eb62 395 errorAppendEntry(e, err);
396 return;
397 }
d1061d2b 398 }
399 debug(17, 3) ("fwdStart: '%s'\n", storeUrl(e));
400 e->mem_obj->request = requestLink(r);
401 e->mem_obj->fd = fd;
402 switch (r->protocol) {
0cdcddb9 403 /*
404 * Note, don't create fwdState for these requests
405 */
e0ebe27c 406 case PROTO_INTERNAL:
d1061d2b 407 internalStart(r, e);
e0ebe27c 408 return;
409 case PROTO_CACHEOBJ:
d1061d2b 410 cachemgrStart(fd, r, e);
e0ebe27c 411 return;
ae0b4725 412 case PROTO_URN:
413 urnStart(r, e);
414 return;
e0ebe27c 415 default:
416 break;
417 }
798b0889 418 fwdState = memAllocate(MEM_FWD_STATE);
db1cd23c 419 cbdataAdd(fwdState, memFree, MEM_FWD_STATE);
d1061d2b 420 fwdState->entry = e;
910169e5 421 fwdState->client_fd = fd;
6801f8a8 422 fwdState->server_fd = -1;
d1061d2b 423 fwdState->request = requestLink(r);
f563eea9 424 fwdState->start = squid_curtime;
d1061d2b 425 storeLockObject(e);
426 storeRegisterAbort(e, fwdAbort, fwdState);
db1cd23c 427 peerSelect(r, e, fwdStartComplete, fwdState);
41462d93 428}
429
41462d93 430int
431fwdCheckDeferRead(int fdnotused, void *data)
432{
433 StoreEntry *e = data;
434 MemObject *mem = e->mem_obj;
435 if (mem == NULL)
436 return 0;
447e176b 437#if DELAY_POOLS
438 if (delayMostBytesWanted(mem, 1) == 0)
439 return 1;
440#endif
41462d93 441 if (mem->inmem_hi - storeLowestMemReaderOffset(e) < READ_AHEAD_GAP)
442 return 0;
443 return 1;
444}
910169e5 445
446void
4ca83e82 447fwdFail(FwdState * fwdState, int err_code, http_status http_code, int xerrno)
910169e5 448{
d46a87a8 449 assert(EBIT_TEST(fwdState->entry->flags, ENTRY_FWD_HDR_WAIT));
32b2931b 450 debug(17, 3) ("fwdFail: %s \"%s\"\n\t%s\n",
4ca83e82 451 err_type_str[err_code],
452 httpStatusString(http_code),
453 storeUrl(fwdState->entry));
454 fwdState->fail.err_code = err_code;
455 fwdState->fail.http_code = http_code;
456 fwdState->fail.xerrno = xerrno;
910169e5 457}
6801f8a8 458
459/*
460 * Called when someone else calls StoreAbort() on this entry
461 */
462void
463fwdAbort(void *data)
464{
9dbf253d 465 FwdState *fwdState = data;
32b2931b 466 debug(17, 3) ("fwdAbort: %s\n", storeUrl(fwdState->entry));
9dbf253d 467 fwdStateFree(fwdState);
6801f8a8 468}
0b49cd31 469
470/*
471 * Frees fwdState without closing FD or generating an abort
472 */
473void
9dbf253d 474fwdUnregister(int fd, FwdState * fwdState)
0b49cd31 475{
9dbf253d 476 debug(17, 3) ("fwdUnregister: %s\n", storeUrl(fwdState->entry));
477 assert(fd = fwdState->server_fd);
478 comm_remove_close_handler(fd, fwdServerClosed, fwdState);
479 fwdState->server_fd = -1;
0b49cd31 480}
db1cd23c 481
482/*
483 * server-side modules call fwdComplete() when they are done
484 * downloading an object. Then, we either 1) re-forward the
485 * request somewhere else if needed, or 2) call storeComplete()
486 * to finish it off
487 */
488void
489fwdComplete(FwdState * fwdState)
490{
491 StoreEntry *e = fwdState->entry;
492 assert(e->store_status == STORE_PENDING);
8a28f65f 493 debug(17, 3) ("fwdComplete: %s\n\tstatus %d\n", storeUrl(e),
db1cd23c 494 e->mem_obj->reply->sline.status);
d6eb18d6 495 if (fwdReforward(fwdState)) {
496 debug(17, 1) ("fwdComplete: re-forwarding %d %s\n",
0bdf2621 497 e->mem_obj->reply->sline.status,
498 storeUrl(e));
8a28f65f 499 if (fwdState->server_fd > -1)
0bdf2621 500 fwdUnregister(fwdState->server_fd, fwdState);
8ddcc35d 501 fwdLogReplyStatus(fwdState->n_tries, e->mem_obj->reply->sline.status);
db1cd23c 502 storeEntryReset(e);
503 fwdStartComplete(fwdState->servers, fwdState);
504 } else {
d6eb18d6 505 debug(17, 3) ("fwdComplete: not re-forwarding status %d\n",
506 e->mem_obj->reply->sline.status);
db1cd23c 507 EBIT_CLR(e->flags, ENTRY_FWD_HDR_WAIT);
508 storeComplete(e);
d6eb18d6 509 /*
510 * If fwdState isn't associated with a server FD, it
511 * won't get freed unless we do it here.
512 */
513 if (fwdState->server_fd < 0)
514 fwdStateFree(fwdState);
db1cd23c 515 }
516}
8ddcc35d 517
518void
519fwdInit(void)
520{
521 cachemgrRegister("forward",
522 "Request Forwarding Statistics",
523 fwdStats, 0, 1);
524}
525
526static void
527fwdLogReplyStatus(int tries, http_status status)
528{
529 if (status > HTTP_INVALID_HEADER)
530 return;
531 assert(tries);
532 tries--;
533 if (tries > MAX_FWD_STATS_IDX)
534 tries = MAX_FWD_STATS_IDX;
535 FwdReplyCodes[tries][status]++;
536}
537
538static void
539fwdStats(StoreEntry *s)
540{
541 int i;
542 int j;
543 for (i=0; i<=(int) HTTP_INVALID_HEADER; i++) {
544 storeAppendPrintf(s, "%3d", i);
545 for (j=0; j<=MAX_FWD_STATS_IDX; j++) {
546 storeAppendPrintf(s, "\t%d", FwdReplyCodes[j][i]);
547 }
548 storeAppendPrintf(s, "\n");
549 }
550}