blob: fc40929f459b03af271e7ab65fd8d61137c83c07 [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 Tarreauee55dc02010-06-01 10:56:34 +0200155 if (likely(s->fe->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;
Willy Tarreau73de9892006-11-30 11:40:23 +0100160
Willy Tarreaueb472682010-05-28 18:46:57 +0200161 /* init store persistence */
162 s->store_count = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163
Willy Tarreaueb472682010-05-28 18:46:57 +0200164 /* FIXME: the logs are horribly complicated now, because they are
165 * defined in <p>, <p>, and later <be> and <be>.
166 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200167
Willy Tarreaueb472682010-05-28 18:46:57 +0200168 if (s->flags & SN_MONITOR)
169 s->logs.logwait = 0;
170 else
171 s->logs.logwait = p->to_log;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200172
Willy Tarreaueb472682010-05-28 18:46:57 +0200173 if (s->logs.logwait & LW_REQ)
174 s->do_log = http_sess_log;
175 else
176 s->do_log = tcp_sess_log;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100177
Willy Tarreaueb472682010-05-28 18:46:57 +0200178 /* default error reporting function, may be changed by analysers */
179 s->srv_error = default_srv_error;
Emeric Brunb982a3d2010-01-04 15:45:53 +0100180
Willy Tarreaueb472682010-05-28 18:46:57 +0200181 tv_zero(&s->logs.tv_request);
182 s->logs.t_queue = -1;
183 s->logs.t_connect = -1;
184 s->logs.t_data = -1;
185 s->logs.t_close = 0;
186 s->logs.bytes_in = s->logs.bytes_out = 0;
187 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
188 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189
Willy Tarreaueb472682010-05-28 18:46:57 +0200190 s->data_source = DATA_SRC_NONE;
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100191
Willy Tarreaueb472682010-05-28 18:46:57 +0200192 txn = &s->txn;
193 /* Those variables will be checked and freed if non-NULL in
194 * session.c:session_free(). It is important that they are
195 * properly initialized.
196 */
197 txn->sessid = NULL;
198 txn->srv_cookie = NULL;
199 txn->cli_cookie = NULL;
200 txn->uri = NULL;
201 txn->req.cap = NULL;
202 txn->rsp.cap = NULL;
203 txn->hdr_idx.v = NULL;
204 txn->hdr_idx.size = txn->hdr_idx.used = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200205
Willy Tarreauf67c9782010-05-23 22:59:00 +0200206 /* Adjust some socket options */
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200207 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1 ||
208 setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
209 (char *) &one, sizeof(one)) == -1)) {
Willy Tarreauf67c9782010-05-23 22:59:00 +0200210 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
211 goto out_free_task;
212 }
213
214 if (p->options & PR_O_TCP_CLI_KA)
215 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
216
217 if (p->options & PR_O_TCP_NOLING)
218 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
219
220 if (global.tune.client_sndbuf)
221 setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
222
223 if (global.tune.client_rcvbuf)
224 setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
225
Willy Tarreaueb472682010-05-28 18:46:57 +0200226 if (p->mode == PR_MODE_HTTP) {
227 /* the captures are only used in HTTP frontends */
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200228 if (unlikely(p->nb_req_cap > 0 &&
229 (txn->req.cap = pool_alloc2(p->req_cap_pool)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200230 goto out_fail_reqcap; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200232 if (unlikely(p->nb_rsp_cap > 0 &&
233 (txn->rsp.cap = pool_alloc2(p->rsp_cap_pool)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200234 goto out_fail_rspcap; /* no memory */
235 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200236
Willy Tarreaueb472682010-05-28 18:46:57 +0200237 if (p->acl_requires & ACL_USE_L7_ANY) {
238 /* we have to allocate header indexes only if we know
239 * that we may make use of them. This of course includes
240 * (mode == PR_MODE_HTTP).
Willy Tarreau042cc792007-03-19 16:20:06 +0100241 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200242 txn->hdr_idx.size = MAX_HTTP_HDR;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100243
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200244 if (unlikely((txn->hdr_idx.v = pool_alloc2(p->hdr_idx_pool)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200245 goto out_fail_idx; /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100246
Willy Tarreaueb472682010-05-28 18:46:57 +0200247 /* and now initialize the HTTP transaction state */
248 http_init_txn(s);
249 }
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100250
Willy Tarreaueb472682010-05-28 18:46:57 +0200251 if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
252 && (p->logfac1 >= 0 || p->logfac2 >= 0)) {
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200253 if (likely(p->to_log)) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200254 /* we have the client ip */
255 if (s->logs.logwait & LW_CLIP)
256 if (!(s->logs.logwait &= ~LW_CLIP))
257 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +0200258 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200259 else if (s->cli_addr.ss_family == AF_INET) {
260 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200261
Willy Tarreaueb472682010-05-28 18:46:57 +0200262 if (!(s->flags & SN_FRT_ADDR_SET))
263 get_frt_addr(s);
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200264
Willy Tarreaueb472682010-05-28 18:46:57 +0200265 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr,
266 sn, sizeof(sn)) &&
267 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
268 pn, sizeof(pn))) {
269 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
270 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
271 sn, ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port),
272 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273 }
274 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200275 else {
276 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200277
278 if (!(s->flags & SN_FRT_ADDR_SET))
279 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200280
Willy Tarreaueb472682010-05-28 18:46:57 +0200281 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr,
282 sn, sizeof(sn)) &&
283 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
284 pn, sizeof(pn))) {
285 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
286 pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
287 sn, ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port),
288 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200289 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200290 }
291 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200292
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200293 if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200294 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200295
Willy Tarreaueb472682010-05-28 18:46:57 +0200296 if (!(s->flags & SN_FRT_ADDR_SET))
297 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298
Willy Tarreaueb472682010-05-28 18:46:57 +0200299 if (s->cli_addr.ss_family == AF_INET) {
300 char pn[INET_ADDRSTRLEN];
301 inet_ntop(AF_INET,
302 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
303 pn, sizeof(pn));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200304
Willy Tarreaueb472682010-05-28 18:46:57 +0200305 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
306 s->uniq_id, p->id, (unsigned short)l->fd, (unsigned short)cfd,
307 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
308 }
309 else {
310 char pn[INET6_ADDRSTRLEN];
311 inet_ntop(AF_INET6,
312 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
313 pn, sizeof(pn));
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200314
Willy Tarreaueb472682010-05-28 18:46:57 +0200315 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
316 s->uniq_id, p->id, (unsigned short)l->fd, (unsigned short)cfd,
317 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
318 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200319
Willy Tarreaueb472682010-05-28 18:46:57 +0200320 write(1, trash, len);
321 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200322
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200323 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200324 goto out_fail_req; /* no memory */
Willy Tarreau2df28e82008-08-17 15:20:19 +0200325
Willy Tarreaueb472682010-05-28 18:46:57 +0200326 s->req->size = global.tune.bufsize;
327 buffer_init(s->req);
328 s->req->prod = &s->si[0];
329 s->req->cons = &s->si[1];
330 s->si[0].ib = s->si[1].ob = s->req;
Willy Tarreaudc0a6a02008-08-03 20:38:13 +0200331
Willy Tarreaueb472682010-05-28 18:46:57 +0200332 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud7971282006-07-29 18:36:34 +0200333
Willy Tarreaueb472682010-05-28 18:46:57 +0200334 if (p->mode == PR_MODE_HTTP)
335 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336
Willy Tarreaueb472682010-05-28 18:46:57 +0200337 /* activate default analysers enabled for this listener */
338 s->req->analysers = l->analysers;
Willy Tarreau54469402006-07-29 16:59:06 +0200339
Willy Tarreaueb472682010-05-28 18:46:57 +0200340 /* note: this should not happen anymore since there's always at least the switching rules */
341 if (!s->req->analysers) {
342 buffer_auto_connect(s->req); /* don't wait to establish connection */
343 buffer_auto_close(s->req); /* let the producer forward close requests */
344 }
Willy Tarreaud7971282006-07-29 18:36:34 +0200345
Willy Tarreaueb472682010-05-28 18:46:57 +0200346 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +0200347 s->req->wto = TICK_ETERNITY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200348
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200349 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200350 goto out_fail_rep; /* no memory */
Willy Tarreau5d707e12009-06-28 11:09:07 +0200351
Willy Tarreaueb472682010-05-28 18:46:57 +0200352 s->rep->size = global.tune.bufsize;
353 buffer_init(s->rep);
354 s->rep->prod = &s->si[1];
355 s->rep->cons = &s->si[0];
356 s->si[0].ob = s->si[1].ib = s->rep;
357 s->rep->analysers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358
Willy Tarreaud04e8582010-05-31 12:31:35 +0200359 s->rep->rto = TICK_ETERNITY;
Willy Tarreaueb472682010-05-28 18:46:57 +0200360 s->rep->wto = s->fe->timeout.client;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361
Willy Tarreaueb472682010-05-28 18:46:57 +0200362 s->req->rex = TICK_ETERNITY;
363 s->req->wex = TICK_ETERNITY;
364 s->req->analyse_exp = TICK_ETERNITY;
365 s->rep->rex = TICK_ETERNITY;
366 s->rep->wex = TICK_ETERNITY;
367 s->rep->analyse_exp = TICK_ETERNITY;
368 t->expire = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369
Willy Tarreaueb472682010-05-28 18:46:57 +0200370 fd_insert(cfd);
371 fdtab[cfd].owner = &s->si[0];
372 fdtab[cfd].state = FD_STREADY;
373 fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
374 if (p->options & PR_O_TCP_NOLING)
375 fdtab[cfd].flags |= FD_FL_TCP_NOLING;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200376
Willy Tarreaueb472682010-05-28 18:46:57 +0200377 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
378 fdtab[cfd].cb[DIR_RD].b = s->req;
379 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
380 fdtab[cfd].cb[DIR_WR].b = s->rep;
381 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
382 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
Willy Tarreaua7e76142007-11-03 14:28:39 +0100383
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200384 if (unlikely((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
385 (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200386 /* Either we got a request from a monitoring system on an HTTP instance,
387 * or we're in health check mode with the 'httpchk' option enabled. In
388 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
389 */
390 struct chunk msg;
391 chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
392 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
393 s->req->analysers = 0;
394 t->expire = s->rep->wex;
395 }
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200396 else if (unlikely(p->mode == PR_MODE_HEALTH)) { /* health check mode, no client reading */
Willy Tarreaueb472682010-05-28 18:46:57 +0200397 struct chunk msg;
398 chunk_initstr(&msg, "OK\n");
399 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
400 s->req->analysers = 0;
401 t->expire = s->rep->wex;
402 }
403 else {
404 EV_FD_SET(cfd, DIR_RD);
405 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200406
Willy Tarreaueb472682010-05-28 18:46:57 +0200407 /* it is important not to call the wakeup function directly but to
408 * pass through task_wakeup(), because this one knows how to apply
409 * priorities to tasks.
410 */
411 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200412
Willy Tarreaueb472682010-05-28 18:46:57 +0200413 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100414
415 /* Error unrolling */
416 out_fail_rep:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200417 pool_free2(pool2_buffer, s->req);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100418 out_fail_req:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200419 pool_free2(p->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100420 out_fail_idx:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200421 pool_free2(p->rsp_cap_pool, txn->rsp.cap);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100422 out_fail_rspcap:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200423 pool_free2(p->req_cap_pool, txn->req.cap);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100424 out_fail_reqcap:
425 out_free_task:
Willy Tarreaua4613182009-03-21 18:13:21 +0100426 task_free(t);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100427 out_free_session:
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100428 LIST_DEL(&s->list);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100429 pool_free2(pool2_session, s);
430 out_close:
Willy Tarreaueb472682010-05-28 18:46:57 +0200431 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200432}
433
Willy Tarreau645513a2010-05-24 20:55:15 +0200434/* set test->i to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200435static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200436acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
437 struct acl_expr *expr, struct acl_test *test) {
Willy Tarreau662b2d82007-05-08 19:56:15 +0200438
Willy Tarreau662b2d82007-05-08 19:56:15 +0200439 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200440
Willy Tarreau645513a2010-05-24 20:55:15 +0200441 test->i = l4->fe->uuid;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200442
Emeric Brun5d16eda2010-01-04 15:47:45 +0100443 return 1;
444}
445
Willy Tarreau645513a2010-05-24 20:55:15 +0200446/* set test->i to the number of connections per second reaching the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200447static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200448acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
449 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200450{
Willy Tarreau645513a2010-05-24 20:55:15 +0200451 test->flags = ACL_TEST_F_VOL_TEST;
452 if (expr->arg_len) {
453 /* another proxy was designated, we must look for it */
454 for (px = proxy; px; px = px->next)
455 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
456 break;
457 }
458 if (!px)
459 return 0;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100460
Willy Tarreau645513a2010-05-24 20:55:15 +0200461 test->i = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100462 return 1;
463}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100464
Willy Tarreau645513a2010-05-24 20:55:15 +0200465/* set test->i to the number of concurrent connections on the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200466static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200467acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
468 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200469{
Willy Tarreau645513a2010-05-24 20:55:15 +0200470 test->flags = ACL_TEST_F_VOL_TEST;
471 if (expr->arg_len) {
472 /* another proxy was designated, we must look for it */
473 for (px = proxy; px; px = px->next)
474 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
475 break;
476 }
477 if (!px)
478 return 0;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100479
Willy Tarreau645513a2010-05-24 20:55:15 +0200480 test->i = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100481 return 1;
482}
483
Willy Tarreau8797c062007-05-07 00:55:35 +0200484
485/* Note: must not be declared <const> as its list will be overwritten */
486static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +0200487 { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
488 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
489 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200490 { NULL, NULL, NULL, NULL },
491}};
492
493
494__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200495static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200496{
497 acl_register_keywords(&acl_kws);
498}
499
500
Willy Tarreaubaaee002006-06-26 02:48:02 +0200501/*
502 * Local variables:
503 * c-indent-level: 8
504 * c-basic-offset: 8
505 * End:
506 */