blob: c4b2bf4fd90e0d2ea8f7e8b5d1733ece2ffc1997 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Client-side variables and functions.
3 *
4 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020018
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
Willy Tarreau2dd0d472006-06-29 17:53:05 +020023#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020024#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
27#include <types/backend.h>
28#include <types/buffers.h>
29#include <types/global.h>
30#include <types/httperr.h>
31#include <types/polling.h>
32#include <types/proxy.h>
33#include <types/server.h>
34#include <types/session.h>
35
Willy Tarreau54469402006-07-29 16:59:06 +020036#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/client.h>
38#include <proto/fd.h>
39#include <proto/log.h>
Willy Tarreaue5f20dc2006-12-03 15:21:35 +010040#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/proto_http.h>
42#include <proto/stream_sock.h>
43#include <proto/task.h>
44
45
46
47/*
48 * FIXME: This should move to the STREAM_SOCK code then split into TCP and HTTP.
49 */
50
51/*
52 * this function is called on a read event from a listen socket, corresponding
53 * to an accept. It tries to accept as many connections as possible.
54 * It returns 0.
55 */
56int event_accept(int fd) {
57 struct proxy *p = (struct proxy *)fdtab[fd].owner;
58 struct session *s;
59 struct task *t;
60 int cfd;
61 int max_accept;
62
63 if (global.nbproc > 1)
64 max_accept = 8; /* let other processes catch some connections too */
65 else
66 max_accept = -1;
67
68 while (p->nbconn < p->maxconn && max_accept--) {
69 struct sockaddr_storage addr;
70 socklen_t laddr = sizeof(addr);
71
72 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) {
73 switch (errno) {
74 case EAGAIN:
75 case EINTR:
76 case ECONNABORTED:
77 return 0; /* nothing more to accept */
78 case ENFILE:
79 send_log(p, LOG_EMERG,
80 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
81 p->id, maxfd);
82 return 0;
83 case EMFILE:
84 send_log(p, LOG_EMERG,
85 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
86 p->id, maxfd);
87 return 0;
88 case ENOBUFS:
89 case ENOMEM:
90 send_log(p, LOG_EMERG,
91 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
92 p->id, maxfd);
93 return 0;
94 default:
95 return 0;
96 }
97 }
98
99 if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */
100 Alert("out of memory in event_accept().\n");
Willy Tarreau2a429502006-10-15 14:52:29 +0200101 MY_FD_CLR(fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102 p->state = PR_STIDLE;
103 close(cfd);
104 return 0;
105 }
106
107 /* if this session comes from a known monitoring system, we want to ignore
108 * it as soon as possible, which means closing it immediately for TCP.
109 */
110 s->flags = 0;
111 if (addr.ss_family == AF_INET &&
112 p->mon_mask.s_addr &&
113 (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) {
114 if (p->mode == PR_MODE_TCP) {
115 close(cfd);
116 pool_free(session, s);
117 continue;
118 }
119 s->flags |= SN_MONITOR;
120 }
121
122 if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */
123 Alert("out of memory in event_accept().\n");
Willy Tarreau2a429502006-10-15 14:52:29 +0200124 MY_FD_CLR(fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125 p->state = PR_STIDLE;
126 close(cfd);
127 pool_free(session, s);
128 return 0;
129 }
130
131 s->cli_addr = addr;
132 if (cfd >= global.maxsock) {
133 Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n");
134 close(cfd);
135 pool_free(task, t);
136 pool_free(session, s);
137 return 0;
138 }
139
140 if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) ||
141 (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
142 (char *) &one, sizeof(one)) == -1)) {
143 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
144 close(cfd);
145 pool_free(task, t);
146 pool_free(session, s);
147 return 0;
148 }
149
150 if (p->options & PR_O_TCP_CLI_KA)
151 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
152
153 t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
154 t->wq = LIST_HEAD(wait_queue[0]); /* but already has a wait queue assigned */
155 t->state = TASK_IDLE;
156 t->process = process_session;
157 t->context = s;
158
159 s->task = t;
Willy Tarreau73de9892006-11-30 11:40:23 +0100160 s->be = s->fe = s->fi = p;
161
Willy Tarreaubaaee002006-06-26 02:48:02 +0200162 s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */
163 s->srv_state = SV_STIDLE;
164 s->req = s->rep = NULL; /* will be allocated later */
165
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166 s->cli_fd = cfd;
167 s->srv_fd = -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200168 s->srv = NULL;
169 s->pend_pos = NULL;
170 s->conn_retries = p->conn_retries;
171
172 if (s->flags & SN_MONITOR)
173 s->logs.logwait = 0;
174 else
175 s->logs.logwait = p->to_log;
176
177 s->logs.tv_accept = now;
178 s->logs.t_request = -1;
179 s->logs.t_queue = -1;
180 s->logs.t_connect = -1;
181 s->logs.t_data = -1;
182 s->logs.t_close = 0;
183 s->logs.uri = NULL;
184 s->logs.cli_cookie = NULL;
185 s->logs.srv_cookie = NULL;
186 s->logs.status = -1;
187 s->logs.bytes = 0;
188 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
189 s->logs.srv_queue_size = 0; /* we will get this number soon */
190
191 s->data_source = DATA_SRC_NONE;
192
193 s->uniq_id = totalconn;
194 p->cum_conn++;
195
Willy Tarreau45e73e32006-12-17 00:05:15 +0100196 s->rsp_cap = NULL;
197 s->hreq.cap = NULL;
198 s->hreq.hdr_idx.v = NULL;
199 s->hreq.hdr_idx.size = s->hreq.hdr_idx.used = 0;
200
201 if (p->mode == PR_MODE_HTTP) {
202 s->hreq.hdr_state = HTTP_PA_EMPTY; /* at the very beginning of the request */
203 s->hreq.start.len = -1;
204 s->hreq.auth_hdr.len = -1;
205 s->hreq.sor = s->hreq.eoh = 0; /* relative to the buffer */
206
207 s->hreq.hdr_idx.size = MAX_HTTP_HDR;
208
209 if (p->nb_req_cap > 0) {
210 if ((s->hreq.cap =
211 pool_alloc_from(p->req_cap_pool, p->nb_req_cap*sizeof(char *)))
212 == NULL) { /* no memory */
213 close(cfd); /* nothing can be done for this fd without memory */
214 pool_free(task, t);
215 pool_free(session, s);
216 return 0;
217 }
218 memset(s->hreq.cap, 0, p->nb_req_cap*sizeof(char *));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200219 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200220
Willy Tarreau45e73e32006-12-17 00:05:15 +0100221
222 if (p->nb_rsp_cap > 0) {
223 if ((s->rsp_cap =
224 pool_alloc_from(p->rsp_cap_pool, p->nb_rsp_cap*sizeof(char *)))
225 == NULL) { /* no memory */
226 if (s->hreq.cap != NULL)
227 pool_free_to(p->req_cap_pool, s->hreq.cap);
228 close(cfd); /* nothing can be done for this fd without memory */
229 pool_free(task, t);
230 pool_free(session, s);
231 return 0;
232 }
233 memset(s->rsp_cap, 0, p->nb_rsp_cap*sizeof(char *));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200234 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200235
Willy Tarreau45e73e32006-12-17 00:05:15 +0100236
237 if ((s->hreq.hdr_idx.v =
238 pool_alloc_from(p->hdr_idx_pool, s->hreq.hdr_idx.size*sizeof(*s->hreq.hdr_idx.v)))
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100239 == NULL) { /* no memory */
240 if (s->rsp_cap != NULL)
241 pool_free_to(p->rsp_cap_pool, s->rsp_cap);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100242 if (s->hreq.cap != NULL)
243 pool_free_to(p->req_cap_pool, s->hreq.cap);
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100244 close(cfd); /* nothing can be done for this fd without memory */
245 pool_free(task, t);
246 pool_free(session, s);
247 return 0;
248 }
Willy Tarreau45e73e32006-12-17 00:05:15 +0100249 hdr_idx_init(&s->hreq.hdr_idx);
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100250 }
251
Willy Tarreaubaaee002006-06-26 02:48:02 +0200252 if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
253 && (p->logfac1 >= 0 || p->logfac2 >= 0)) {
254 struct sockaddr_storage sockname;
255 socklen_t namelen = sizeof(sockname);
256
257 if (addr.ss_family != AF_INET ||
Willy Tarreau73de9892006-11-30 11:40:23 +0100258 !(s->fe->options & PR_O_TRANSP) ||
Willy Tarreaubaaee002006-06-26 02:48:02 +0200259 get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1)
260 getsockname(cfd, (struct sockaddr *)&sockname, &namelen);
261
262 if (p->to_log) {
263 /* we have the client ip */
264 if (s->logs.logwait & LW_CLIP)
265 if (!(s->logs.logwait &= ~LW_CLIP))
266 sess_log(s);
267 }
268 else if (s->cli_addr.ss_family == AF_INET) {
269 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
270 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sockname)->sin_addr,
271 sn, sizeof(sn)) &&
272 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
273 pn, sizeof(pn))) {
274 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
275 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
276 sn, ntohs(((struct sockaddr_in *)&sockname)->sin_port),
277 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
278 }
279 }
280 else {
281 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
282 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sockname)->sin6_addr,
283 sn, sizeof(sn)) &&
284 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
285 pn, sizeof(pn))) {
286 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
287 pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
288 sn, ntohs(((struct sockaddr_in6 *)&sockname)->sin6_port),
289 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
290 }
291 }
292 }
293
294 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
295 struct sockaddr_in sockname;
296 socklen_t namelen = sizeof(sockname);
297 int len;
298 if (addr.ss_family != AF_INET ||
Willy Tarreau73de9892006-11-30 11:40:23 +0100299 !(s->fe->options & PR_O_TRANSP) ||
Willy Tarreaubaaee002006-06-26 02:48:02 +0200300 get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1)
301 getsockname(cfd, (struct sockaddr *)&sockname, &namelen);
302
303 if (s->cli_addr.ss_family == AF_INET) {
304 char pn[INET_ADDRSTRLEN];
305 inet_ntop(AF_INET,
306 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
307 pn, sizeof(pn));
308
309 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
310 s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd,
311 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
312 }
313 else {
314 char pn[INET6_ADDRSTRLEN];
315 inet_ntop(AF_INET6,
316 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
317 pn, sizeof(pn));
318
319 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
320 s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd,
321 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
322 }
323
324 write(1, trash, len);
325 }
326
327 if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100328 if (s->hreq.hdr_idx.v != NULL)
329 pool_free_to(p->hdr_idx_pool, s->hreq.hdr_idx.v);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330 if (s->rsp_cap != NULL)
331 pool_free_to(p->rsp_cap_pool, s->rsp_cap);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100332 if (s->hreq.cap != NULL)
333 pool_free_to(p->req_cap_pool, s->hreq.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334 close(cfd); /* nothing can be done for this fd without memory */
335 pool_free(task, t);
336 pool_free(session, s);
337 return 0;
338 }
339
Willy Tarreau54469402006-07-29 16:59:06 +0200340 buffer_init(s->req);
341 s->req->rlim += BUFSIZE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200342 if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */
343 s->req->rlim -= MAXREWRITE;
344
Willy Tarreau73de9892006-11-30 11:40:23 +0100345 s->req->rto = s->fe->clitimeout;
346 s->req->wto = s->be->srvtimeout;
347 s->req->cto = s->be->srvtimeout;
Willy Tarreaud7971282006-07-29 18:36:34 +0200348
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349 if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */
350 pool_free(buffer, s->req);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100351 if (s->hreq.hdr_idx.v != NULL)
352 pool_free_to(p->hdr_idx_pool, s->hreq.hdr_idx.v);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353 if (s->rsp_cap != NULL)
354 pool_free_to(p->rsp_cap_pool, s->rsp_cap);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100355 if (s->hreq.cap != NULL)
356 pool_free_to(p->req_cap_pool, s->hreq.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357 close(cfd); /* nothing can be done for this fd without memory */
358 pool_free(task, t);
359 pool_free(session, s);
360 return 0;
361 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362
Willy Tarreau54469402006-07-29 16:59:06 +0200363 buffer_init(s->rep);
364
Willy Tarreau73de9892006-11-30 11:40:23 +0100365 s->rep->rto = s->be->srvtimeout;
366 s->rep->wto = s->be->clitimeout;
Willy Tarreaud7971282006-07-29 18:36:34 +0200367 s->rep->cto = 0;
368
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369 fdtab[cfd].owner = t;
370 fdtab[cfd].state = FD_STREADY;
Willy Tarreaud7971282006-07-29 18:36:34 +0200371 fdtab[cfd].cb[DIR_RD].f = &stream_sock_read;
Willy Tarreau54469402006-07-29 16:59:06 +0200372 fdtab[cfd].cb[DIR_RD].b = s->req;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200373 fdtab[cfd].cb[DIR_WR].f = &stream_sock_write;
Willy Tarreau54469402006-07-29 16:59:06 +0200374 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200375
376 if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
377 (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK)))
378 /* Either we got a request from a monitoring system on an HTTP instance,
379 * or we're in health check mode with the 'httpchk' option enabled. In
380 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
381 */
382 client_retnclose(s, 19, "HTTP/1.0 200 OK\r\n\r\n"); /* forge a 200 response */
383 else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */
384 client_retnclose(s, 3, "OK\n"); /* forge an "OK" response */
385 }
386 else {
Willy Tarreau2a429502006-10-15 14:52:29 +0200387 MY_FD_SET(cfd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388 }
389
390#if defined(DEBUG_FULL) && defined(ENABLE_EPOLL)
391 if (PrevReadEvent) {
Willy Tarreau2a429502006-10-15 14:52:29 +0200392 assert(!(MY_FD_ISSET(cfd, PrevReadEvent)));
393 assert(!(MY_FD_ISSET(cfd, PrevWriteEvent)));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200394 }
395#endif
396 fd_insert(cfd);
397
Willy Tarreaud7971282006-07-29 18:36:34 +0200398 tv_eternity(&s->req->rex);
399 tv_eternity(&s->req->wex);
400 tv_eternity(&s->req->cex);
401 tv_eternity(&s->rep->rex);
402 tv_eternity(&s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200403
Willy Tarreau73de9892006-11-30 11:40:23 +0100404 if (s->fe->clitimeout) {
Willy Tarreau2a429502006-10-15 14:52:29 +0200405 if (MY_FD_ISSET(cfd, StaticReadEvent))
Willy Tarreau73de9892006-11-30 11:40:23 +0100406 tv_delayfrom(&s->req->rex, &now, s->fe->clitimeout);
Willy Tarreau2a429502006-10-15 14:52:29 +0200407 if (MY_FD_ISSET(cfd, StaticWriteEvent))
Willy Tarreau73de9892006-11-30 11:40:23 +0100408 tv_delayfrom(&s->rep->wex, &now, s->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409 }
410
Willy Tarreaud7971282006-07-29 18:36:34 +0200411 tv_min(&t->expire, &s->req->rex, &s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412
413 task_queue(t);
414
415 if (p->mode != PR_MODE_HEALTH)
416 task_wakeup(&rq, t);
417
418 p->nbconn++;
419 if (p->nbconn > p->nbconn_max)
420 p->nbconn_max = p->nbconn;
421 actconn++;
422 totalconn++;
423
424 // fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p\n", p, actconn, totalconn, t);
425 } /* end of while (p->nbconn < p->maxconn) */
426 return 0;
427}
428
429
430
431/*
432 * Local variables:
433 * c-indent-level: 8
434 * c-basic-offset: 8
435 * End:
436 */