blob: 9bd793f754db692c23f44ba754a5e304ec4981d5 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau03fa5df2010-05-24 21:02:37 +02002 * Frontend variables and functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreau03fa5df2010-05-24 21:02:37 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
Willy Tarreau8797c062007-05-07 00:55:35 +020029#include <proto/acl.h>
Willy Tarreau54469402006-07-29 16:59:06 +020030#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020032#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/log.h>
Willy Tarreaue5f20dc2006-12-03 15:21:35 +010034#include <proto/hdr_idx.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020035#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010037#include <proto/proxy.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020038#include <proto/session.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010039#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/stream_sock.h>
41#include <proto/task.h>
42
43
Willy Tarreau14c8aac2007-05-08 19:46:30 +020044/* Retrieves the original destination address used by the client, and sets the
45 * SN_FRT_ADDR_SET flag.
46 */
47void get_frt_addr(struct session *s)
48{
49 socklen_t namelen = sizeof(s->frt_addr);
50
Willy Tarreau7e5067d2008-12-07 16:27:56 +010051 if (get_original_dst(s->si[0].fd, (struct sockaddr_in *)&s->frt_addr, &namelen) == -1)
52 getsockname(s->si[0].fd, (struct sockaddr *)&s->frt_addr, &namelen);
Willy Tarreau14c8aac2007-05-08 19:46:30 +020053 s->flags |= SN_FRT_ADDR_SET;
54}
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
Willy Tarreaueb472682010-05-28 18:46:57 +020056/* This function is called from the protocol layer accept() in order to instanciate
57 * a new proxy. It returns a positive value upon success, 0 if the connection needs
58 * to be closed and ignored, or a negative value upon critical failure.
Willy Tarreaubaaee002006-06-26 02:48:02 +020059 */
Willy Tarreaueb472682010-05-28 18:46:57 +020060int frontend_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
61{
62 struct proxy *p = l->frontend;
Willy Tarreaubaaee002006-06-26 02:48:02 +020063 struct session *s;
Willy Tarreauc2168d32007-03-03 20:51:44 +010064 struct http_txn *txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +020065 struct task *t;
Willy Tarreaubaaee002006-06-26 02:48:02 +020066
Willy Tarreau2281b7f2010-05-28 19:29:49 +020067 if (unlikely((s = pool_alloc2(pool2_session)) == NULL)) {
Willy Tarreaueb472682010-05-28 18:46:57 +020068 Alert("out of memory in event_accept().\n");
69 goto out_close;
Willy Tarreau79584222009-03-06 09:18:27 +010070 }
71
Willy Tarreaueb472682010-05-28 18:46:57 +020072 LIST_ADDQ(&sessions, &s->list);
73 LIST_INIT(&s->back_refs);
Willy Tarreaubaaee002006-06-26 02:48:02 +020074
Willy Tarreaueb472682010-05-28 18:46:57 +020075 s->flags = 0;
76 s->term_trace = 0;
77 s->cli_addr = *addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +020078
Willy Tarreaueb472682010-05-28 18:46:57 +020079 /* if this session comes from a known monitoring system, we want to ignore
80 * it as soon as possible, which means closing it immediately for TCP.
81 */
Willy Tarreaude3041d2010-05-31 10:56:17 +020082 if (unlikely((l->options & LI_O_CHK_MONNET) &&
Willy Tarreau2281b7f2010-05-28 19:29:49 +020083 addr->ss_family == AF_INET &&
84 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
Willy Tarreaueb472682010-05-28 18:46:57 +020085 if (p->mode == PR_MODE_TCP) {
86 pool_free2(pool2_session, s);
87 return 0;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +020088 }
Willy Tarreaueb472682010-05-28 18:46:57 +020089 s->flags |= SN_MONITOR;
90 }
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +020091
Willy Tarreau2281b7f2010-05-28 19:29:49 +020092 if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */
Willy Tarreaueb472682010-05-28 18:46:57 +020093 Alert("out of memory in event_accept().\n");
94 goto out_free_session;
95 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020096
Willy Tarreaueb472682010-05-28 18:46:57 +020097 t->process = l->handler;
98 t->context = s;
99 t->nice = l->nice;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100
Willy Tarreaueb472682010-05-28 18:46:57 +0200101 s->task = t;
102 s->listener = l;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103
Willy Tarreaueb472682010-05-28 18:46:57 +0200104 /* Note: initially, the session's backend points to the frontend.
105 * This changes later when switching rules are executed or
106 * when the default backend is assigned.
107 */
108 s->be = s->fe = p;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200109
Willy Tarreaueb472682010-05-28 18:46:57 +0200110 s->req = s->rep = NULL; /* will be allocated later */
Willy Tarreaue803de22010-01-21 17:43:04 +0100111
Willy Tarreauf67c9782010-05-23 22:59:00 +0200112 /* this part should be common with other protocols */
Willy Tarreaueb472682010-05-28 18:46:57 +0200113 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
114 s->si[0].err_type = SI_ET_NONE;
115 s->si[0].err_loc = NULL;
116 s->si[0].owner = t;
117 s->si[0].update = stream_sock_data_finish;
118 s->si[0].shutr = stream_sock_shutr;
119 s->si[0].shutw = stream_sock_shutw;
120 s->si[0].chk_rcv = stream_sock_chk_rcv;
121 s->si[0].chk_snd = stream_sock_chk_snd;
122 s->si[0].connect = NULL;
123 s->si[0].iohandler = NULL;
124 s->si[0].fd = cfd;
125 s->si[0].flags = SI_FL_NONE | SI_FL_CAP_SPLTCP; /* TCP splicing capable */
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200126 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreaueb472682010-05-28 18:46:57 +0200127 s->si[0].flags |= SI_FL_INDEP_STR;
128 s->si[0].exp = TICK_ETERNITY;
Willy Tarreaue803de22010-01-21 17:43:04 +0100129
Willy Tarreauf67c9782010-05-23 22:59:00 +0200130 s->logs.accept_date = date; /* user-visible date for logging */
131 s->logs.tv_accept = now; /* corrected date for internal use */
132 s->uniq_id = totalconn;
133 proxy_inc_fe_ctr(l, p); /* note: cum_beconn will be increased once assigned */
134
135 /* now evaluate the tcp-request rules */
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200136 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
137 task_free(t);
138 LIST_DEL(&s->list);
139 pool_free2(pool2_session, s);
140 /* let's do a no-linger now to close with a single RST. */
141 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
142 return 0;
Willy Tarreauf67c9782010-05-23 22:59:00 +0200143 }
144
Willy Tarreaua360d282010-05-31 19:17:12 +0200145 /* pre-initialize the other side's stream interface to an INIT state */
146 s->si[1].owner = t;
Willy Tarreaueb472682010-05-28 18:46:57 +0200147 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
148 s->si[1].err_type = SI_ET_NONE;
149 s->si[1].err_loc = NULL;
Willy Tarreaueb472682010-05-28 18:46:57 +0200150 s->si[1].iohandler = NULL;
151 s->si[1].exp = TICK_ETERNITY;
152 s->si[1].fd = -1; /* just to help with debugging */
Willy Tarreaua360d282010-05-31 19:17:12 +0200153
Willy Tarreaueb472682010-05-28 18:46:57 +0200154 s->si[1].flags = SI_FL_NONE;
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200155 if (likely(s->be->options2 & PR_O2_INDEPSTR))
Willy Tarreaueb472682010-05-28 18:46:57 +0200156 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200157
Willy Tarreaueb472682010-05-28 18:46:57 +0200158 s->srv = s->prev_srv = s->srv_conn = NULL;
159 s->pend_pos = NULL;
160 s->conn_retries = s->be->conn_retries;
Willy Tarreau73de9892006-11-30 11:40:23 +0100161
Willy Tarreaueb472682010-05-28 18:46:57 +0200162 /* init store persistence */
163 s->store_count = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164
Willy Tarreaueb472682010-05-28 18:46:57 +0200165 /* FIXME: the logs are horribly complicated now, because they are
166 * defined in <p>, <p>, and later <be> and <be>.
167 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200168
Willy Tarreaueb472682010-05-28 18:46:57 +0200169 if (s->flags & SN_MONITOR)
170 s->logs.logwait = 0;
171 else
172 s->logs.logwait = p->to_log;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200173
Willy Tarreaueb472682010-05-28 18:46:57 +0200174 if (s->logs.logwait & LW_REQ)
175 s->do_log = http_sess_log;
176 else
177 s->do_log = tcp_sess_log;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100178
Willy Tarreaueb472682010-05-28 18:46:57 +0200179 /* default error reporting function, may be changed by analysers */
180 s->srv_error = default_srv_error;
Emeric Brunb982a3d2010-01-04 15:45:53 +0100181
Willy Tarreaueb472682010-05-28 18:46:57 +0200182 tv_zero(&s->logs.tv_request);
183 s->logs.t_queue = -1;
184 s->logs.t_connect = -1;
185 s->logs.t_data = -1;
186 s->logs.t_close = 0;
187 s->logs.bytes_in = s->logs.bytes_out = 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 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200190
Willy Tarreaueb472682010-05-28 18:46:57 +0200191 s->data_source = DATA_SRC_NONE;
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100192
Willy Tarreaueb472682010-05-28 18:46:57 +0200193 txn = &s->txn;
194 /* Those variables will be checked and freed if non-NULL in
195 * session.c:session_free(). It is important that they are
196 * properly initialized.
197 */
198 txn->sessid = NULL;
199 txn->srv_cookie = NULL;
200 txn->cli_cookie = NULL;
201 txn->uri = NULL;
202 txn->req.cap = NULL;
203 txn->rsp.cap = NULL;
204 txn->hdr_idx.v = NULL;
205 txn->hdr_idx.size = txn->hdr_idx.used = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200206
Willy Tarreauf67c9782010-05-23 22:59:00 +0200207 /* Adjust some socket options */
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200208 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1 ||
209 setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
210 (char *) &one, sizeof(one)) == -1)) {
Willy Tarreauf67c9782010-05-23 22:59:00 +0200211 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
212 goto out_free_task;
213 }
214
215 if (p->options & PR_O_TCP_CLI_KA)
216 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
217
218 if (p->options & PR_O_TCP_NOLING)
219 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
220
221 if (global.tune.client_sndbuf)
222 setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
223
224 if (global.tune.client_rcvbuf)
225 setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
226
Willy Tarreaueb472682010-05-28 18:46:57 +0200227 if (p->mode == PR_MODE_HTTP) {
228 /* the captures are only used in HTTP frontends */
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200229 if (unlikely(p->nb_req_cap > 0 &&
230 (txn->req.cap = pool_alloc2(p->req_cap_pool)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200231 goto out_fail_reqcap; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200232
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200233 if (unlikely(p->nb_rsp_cap > 0 &&
234 (txn->rsp.cap = pool_alloc2(p->rsp_cap_pool)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200235 goto out_fail_rspcap; /* no memory */
236 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200237
Willy Tarreaueb472682010-05-28 18:46:57 +0200238 if (p->acl_requires & ACL_USE_L7_ANY) {
239 /* we have to allocate header indexes only if we know
240 * that we may make use of them. This of course includes
241 * (mode == PR_MODE_HTTP).
Willy Tarreau042cc792007-03-19 16:20:06 +0100242 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200243 txn->hdr_idx.size = MAX_HTTP_HDR;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100244
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200245 if (unlikely((txn->hdr_idx.v = pool_alloc2(p->hdr_idx_pool)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200246 goto out_fail_idx; /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100247
Willy Tarreaueb472682010-05-28 18:46:57 +0200248 /* and now initialize the HTTP transaction state */
249 http_init_txn(s);
250 }
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100251
Willy Tarreaueb472682010-05-28 18:46:57 +0200252 if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
253 && (p->logfac1 >= 0 || p->logfac2 >= 0)) {
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200254 if (likely(p->to_log)) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200255 /* we have the client ip */
256 if (s->logs.logwait & LW_CLIP)
257 if (!(s->logs.logwait &= ~LW_CLIP))
258 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +0200259 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200260 else if (s->cli_addr.ss_family == AF_INET) {
261 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200262
Willy Tarreaueb472682010-05-28 18:46:57 +0200263 if (!(s->flags & SN_FRT_ADDR_SET))
264 get_frt_addr(s);
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200265
Willy Tarreaueb472682010-05-28 18:46:57 +0200266 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr,
267 sn, sizeof(sn)) &&
268 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
269 pn, sizeof(pn))) {
270 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
271 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
272 sn, ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port),
273 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274 }
275 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200276 else {
277 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200278
279 if (!(s->flags & SN_FRT_ADDR_SET))
280 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200281
Willy Tarreaueb472682010-05-28 18:46:57 +0200282 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->frt_addr)->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 *)&s->frt_addr)->sin6_port),
289 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200290 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200291 }
292 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200293
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200294 if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200295 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200296
Willy Tarreaueb472682010-05-28 18:46:57 +0200297 if (!(s->flags & SN_FRT_ADDR_SET))
298 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299
Willy Tarreaueb472682010-05-28 18:46:57 +0200300 if (s->cli_addr.ss_family == AF_INET) {
301 char pn[INET_ADDRSTRLEN];
302 inet_ntop(AF_INET,
303 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
304 pn, sizeof(pn));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200305
Willy Tarreaueb472682010-05-28 18:46:57 +0200306 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
307 s->uniq_id, p->id, (unsigned short)l->fd, (unsigned short)cfd,
308 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
309 }
310 else {
311 char pn[INET6_ADDRSTRLEN];
312 inet_ntop(AF_INET6,
313 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
314 pn, sizeof(pn));
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200315
Willy Tarreaueb472682010-05-28 18:46:57 +0200316 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
317 s->uniq_id, p->id, (unsigned short)l->fd, (unsigned short)cfd,
318 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
319 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200320
Willy Tarreaueb472682010-05-28 18:46:57 +0200321 write(1, trash, len);
322 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200324 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200325 goto out_fail_req; /* no memory */
Willy Tarreau2df28e82008-08-17 15:20:19 +0200326
Willy Tarreaueb472682010-05-28 18:46:57 +0200327 s->req->size = global.tune.bufsize;
328 buffer_init(s->req);
329 s->req->prod = &s->si[0];
330 s->req->cons = &s->si[1];
331 s->si[0].ib = s->si[1].ob = s->req;
Willy Tarreaudc0a6a02008-08-03 20:38:13 +0200332
Willy Tarreaueb472682010-05-28 18:46:57 +0200333 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud7971282006-07-29 18:36:34 +0200334
Willy Tarreaueb472682010-05-28 18:46:57 +0200335 if (p->mode == PR_MODE_HTTP)
336 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200337
Willy Tarreaueb472682010-05-28 18:46:57 +0200338 /* activate default analysers enabled for this listener */
339 s->req->analysers = l->analysers;
Willy Tarreau54469402006-07-29 16:59:06 +0200340
Willy Tarreaueb472682010-05-28 18:46:57 +0200341 /* note: this should not happen anymore since there's always at least the switching rules */
342 if (!s->req->analysers) {
343 buffer_auto_connect(s->req); /* don't wait to establish connection */
344 buffer_auto_close(s->req); /* let the producer forward close requests */
345 }
Willy Tarreaud7971282006-07-29 18:36:34 +0200346
Willy Tarreaueb472682010-05-28 18:46:57 +0200347 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +0200348 s->req->wto = TICK_ETERNITY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200349
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200350 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200351 goto out_fail_rep; /* no memory */
Willy Tarreau5d707e12009-06-28 11:09:07 +0200352
Willy Tarreaueb472682010-05-28 18:46:57 +0200353 s->rep->size = global.tune.bufsize;
354 buffer_init(s->rep);
355 s->rep->prod = &s->si[1];
356 s->rep->cons = &s->si[0];
357 s->si[0].ob = s->si[1].ib = s->rep;
358 s->rep->analysers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359
Willy Tarreaud04e8582010-05-31 12:31:35 +0200360 s->rep->rto = TICK_ETERNITY;
Willy Tarreaueb472682010-05-28 18:46:57 +0200361 s->rep->wto = s->fe->timeout.client;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362
Willy Tarreaueb472682010-05-28 18:46:57 +0200363 s->req->rex = TICK_ETERNITY;
364 s->req->wex = TICK_ETERNITY;
365 s->req->analyse_exp = TICK_ETERNITY;
366 s->rep->rex = TICK_ETERNITY;
367 s->rep->wex = TICK_ETERNITY;
368 s->rep->analyse_exp = TICK_ETERNITY;
369 t->expire = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370
Willy Tarreaueb472682010-05-28 18:46:57 +0200371 fd_insert(cfd);
372 fdtab[cfd].owner = &s->si[0];
373 fdtab[cfd].state = FD_STREADY;
374 fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
375 if (p->options & PR_O_TCP_NOLING)
376 fdtab[cfd].flags |= FD_FL_TCP_NOLING;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200377
Willy Tarreaueb472682010-05-28 18:46:57 +0200378 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
379 fdtab[cfd].cb[DIR_RD].b = s->req;
380 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
381 fdtab[cfd].cb[DIR_WR].b = s->rep;
382 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
383 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
Willy Tarreaua7e76142007-11-03 14:28:39 +0100384
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200385 if (unlikely((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
386 (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200387 /* Either we got a request from a monitoring system on an HTTP instance,
388 * or we're in health check mode with the 'httpchk' option enabled. In
389 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
390 */
391 struct chunk msg;
392 chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
393 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
394 s->req->analysers = 0;
395 t->expire = s->rep->wex;
396 }
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200397 else if (unlikely(p->mode == PR_MODE_HEALTH)) { /* health check mode, no client reading */
Willy Tarreaueb472682010-05-28 18:46:57 +0200398 struct chunk msg;
399 chunk_initstr(&msg, "OK\n");
400 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
401 s->req->analysers = 0;
402 t->expire = s->rep->wex;
403 }
404 else {
405 EV_FD_SET(cfd, DIR_RD);
406 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200407
Willy Tarreaueb472682010-05-28 18:46:57 +0200408 /* it is important not to call the wakeup function directly but to
409 * pass through task_wakeup(), because this one knows how to apply
410 * priorities to tasks.
411 */
412 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
Willy Tarreaueb472682010-05-28 18:46:57 +0200414 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100415
416 /* Error unrolling */
417 out_fail_rep:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200418 pool_free2(pool2_buffer, s->req);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100419 out_fail_req:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200420 pool_free2(p->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100421 out_fail_idx:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200422 pool_free2(p->rsp_cap_pool, txn->rsp.cap);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100423 out_fail_rspcap:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200424 pool_free2(p->req_cap_pool, txn->req.cap);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100425 out_fail_reqcap:
426 out_free_task:
Willy Tarreaua4613182009-03-21 18:13:21 +0100427 task_free(t);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100428 out_free_session:
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100429 LIST_DEL(&s->list);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100430 pool_free2(pool2_session, s);
431 out_close:
Willy Tarreaueb472682010-05-28 18:46:57 +0200432 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433}
434
Willy Tarreau645513a2010-05-24 20:55:15 +0200435/* set test->i to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200436static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200437acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
438 struct acl_expr *expr, struct acl_test *test) {
Willy Tarreau662b2d82007-05-08 19:56:15 +0200439
Willy Tarreau662b2d82007-05-08 19:56:15 +0200440 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200441
Willy Tarreau645513a2010-05-24 20:55:15 +0200442 test->i = l4->fe->uuid;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200443
Emeric Brun5d16eda2010-01-04 15:47:45 +0100444 return 1;
445}
446
Willy Tarreau645513a2010-05-24 20:55:15 +0200447/* set test->i to the number of connections per second reaching the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200448static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200449acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
450 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200451{
Willy Tarreau645513a2010-05-24 20:55:15 +0200452 test->flags = ACL_TEST_F_VOL_TEST;
453 if (expr->arg_len) {
454 /* another proxy was designated, we must look for it */
455 for (px = proxy; px; px = px->next)
456 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
457 break;
458 }
459 if (!px)
460 return 0;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100461
Willy Tarreau645513a2010-05-24 20:55:15 +0200462 test->i = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100463 return 1;
464}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100465
Willy Tarreau645513a2010-05-24 20:55:15 +0200466/* set test->i to the number of concurrent connections on the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200467static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200468acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
469 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200470{
Willy Tarreau645513a2010-05-24 20:55:15 +0200471 test->flags = ACL_TEST_F_VOL_TEST;
472 if (expr->arg_len) {
473 /* another proxy was designated, we must look for it */
474 for (px = proxy; px; px = px->next)
475 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
476 break;
477 }
478 if (!px)
479 return 0;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100480
Willy Tarreau645513a2010-05-24 20:55:15 +0200481 test->i = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100482 return 1;
483}
484
Willy Tarreau8797c062007-05-07 00:55:35 +0200485
486/* Note: must not be declared <const> as its list will be overwritten */
487static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +0200488 { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
489 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
490 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200491 { NULL, NULL, NULL, NULL },
492}};
493
494
495__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200496static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200497{
498 acl_register_keywords(&acl_kws);
499}
500
501
Willy Tarreaubaaee002006-06-26 02:48:02 +0200502/*
503 * Local variables:
504 * c-indent-level: 8
505 * c-basic-offset: 8
506 * End:
507 */