blob: cdd46cef49aa61b74c0cd7a22ee5957631b25a6a [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
145 /* pre-initialize the other side's stream interface */
146
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;
150 s->si[1].owner = t;
151 s->si[1].update = stream_sock_data_finish;
152 s->si[1].shutr = stream_sock_shutr;
153 s->si[1].shutw = stream_sock_shutw;
154 s->si[1].chk_rcv = stream_sock_chk_rcv;
155 s->si[1].chk_snd = stream_sock_chk_snd;
156 s->si[1].connect = tcpv4_connect_server;
157 s->si[1].iohandler = NULL;
158 s->si[1].exp = TICK_ETERNITY;
159 s->si[1].fd = -1; /* just to help with debugging */
160 s->si[1].flags = SI_FL_NONE;
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200161 if (likely(s->be->options2 & PR_O2_INDEPSTR))
Willy Tarreaueb472682010-05-28 18:46:57 +0200162 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163
Willy Tarreaueb472682010-05-28 18:46:57 +0200164 s->srv = s->prev_srv = s->srv_conn = NULL;
165 s->pend_pos = NULL;
166 s->conn_retries = s->be->conn_retries;
Willy Tarreau73de9892006-11-30 11:40:23 +0100167
Willy Tarreaueb472682010-05-28 18:46:57 +0200168 /* init store persistence */
169 s->store_count = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170
Willy Tarreaueb472682010-05-28 18:46:57 +0200171 /* FIXME: the logs are horribly complicated now, because they are
172 * defined in <p>, <p>, and later <be> and <be>.
173 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200174
Willy Tarreaueb472682010-05-28 18:46:57 +0200175 if (s->flags & SN_MONITOR)
176 s->logs.logwait = 0;
177 else
178 s->logs.logwait = p->to_log;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200179
Willy Tarreaueb472682010-05-28 18:46:57 +0200180 if (s->logs.logwait & LW_REQ)
181 s->do_log = http_sess_log;
182 else
183 s->do_log = tcp_sess_log;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100184
Willy Tarreaueb472682010-05-28 18:46:57 +0200185 /* default error reporting function, may be changed by analysers */
186 s->srv_error = default_srv_error;
Emeric Brunb982a3d2010-01-04 15:45:53 +0100187
Willy Tarreaueb472682010-05-28 18:46:57 +0200188 tv_zero(&s->logs.tv_request);
189 s->logs.t_queue = -1;
190 s->logs.t_connect = -1;
191 s->logs.t_data = -1;
192 s->logs.t_close = 0;
193 s->logs.bytes_in = s->logs.bytes_out = 0;
194 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
195 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200196
Willy Tarreaueb472682010-05-28 18:46:57 +0200197 s->data_source = DATA_SRC_NONE;
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100198
Willy Tarreaueb472682010-05-28 18:46:57 +0200199 txn = &s->txn;
200 /* Those variables will be checked and freed if non-NULL in
201 * session.c:session_free(). It is important that they are
202 * properly initialized.
203 */
204 txn->sessid = NULL;
205 txn->srv_cookie = NULL;
206 txn->cli_cookie = NULL;
207 txn->uri = NULL;
208 txn->req.cap = NULL;
209 txn->rsp.cap = NULL;
210 txn->hdr_idx.v = NULL;
211 txn->hdr_idx.size = txn->hdr_idx.used = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200212
Willy Tarreauf67c9782010-05-23 22:59:00 +0200213 /* Adjust some socket options */
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200214 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1 ||
215 setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
216 (char *) &one, sizeof(one)) == -1)) {
Willy Tarreauf67c9782010-05-23 22:59:00 +0200217 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
218 goto out_free_task;
219 }
220
221 if (p->options & PR_O_TCP_CLI_KA)
222 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
223
224 if (p->options & PR_O_TCP_NOLING)
225 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
226
227 if (global.tune.client_sndbuf)
228 setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
229
230 if (global.tune.client_rcvbuf)
231 setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
232
Willy Tarreaueb472682010-05-28 18:46:57 +0200233 if (p->mode == PR_MODE_HTTP) {
234 /* the captures are only used in HTTP frontends */
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200235 if (unlikely(p->nb_req_cap > 0 &&
236 (txn->req.cap = pool_alloc2(p->req_cap_pool)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200237 goto out_fail_reqcap; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200238
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200239 if (unlikely(p->nb_rsp_cap > 0 &&
240 (txn->rsp.cap = pool_alloc2(p->rsp_cap_pool)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200241 goto out_fail_rspcap; /* no memory */
242 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243
Willy Tarreaueb472682010-05-28 18:46:57 +0200244 if (p->acl_requires & ACL_USE_L7_ANY) {
245 /* we have to allocate header indexes only if we know
246 * that we may make use of them. This of course includes
247 * (mode == PR_MODE_HTTP).
Willy Tarreau042cc792007-03-19 16:20:06 +0100248 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200249 txn->hdr_idx.size = MAX_HTTP_HDR;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100250
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200251 if (unlikely((txn->hdr_idx.v = pool_alloc2(p->hdr_idx_pool)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200252 goto out_fail_idx; /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100253
Willy Tarreaueb472682010-05-28 18:46:57 +0200254 /* and now initialize the HTTP transaction state */
255 http_init_txn(s);
256 }
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100257
Willy Tarreaueb472682010-05-28 18:46:57 +0200258 if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
259 && (p->logfac1 >= 0 || p->logfac2 >= 0)) {
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200260 if (likely(p->to_log)) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200261 /* we have the client ip */
262 if (s->logs.logwait & LW_CLIP)
263 if (!(s->logs.logwait &= ~LW_CLIP))
264 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +0200265 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200266 else if (s->cli_addr.ss_family == AF_INET) {
267 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200268
Willy Tarreaueb472682010-05-28 18:46:57 +0200269 if (!(s->flags & SN_FRT_ADDR_SET))
270 get_frt_addr(s);
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200271
Willy Tarreaueb472682010-05-28 18:46:57 +0200272 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr,
273 sn, sizeof(sn)) &&
274 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
275 pn, sizeof(pn))) {
276 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
277 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
278 sn, ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port),
279 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200280 }
281 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200282 else {
283 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200284
285 if (!(s->flags & SN_FRT_ADDR_SET))
286 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200287
Willy Tarreaueb472682010-05-28 18:46:57 +0200288 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr,
289 sn, sizeof(sn)) &&
290 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
291 pn, sizeof(pn))) {
292 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
293 pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
294 sn, ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port),
295 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200296 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200297 }
298 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200300 if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200301 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200302
Willy Tarreaueb472682010-05-28 18:46:57 +0200303 if (!(s->flags & SN_FRT_ADDR_SET))
304 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200305
Willy Tarreaueb472682010-05-28 18:46:57 +0200306 if (s->cli_addr.ss_family == AF_INET) {
307 char pn[INET_ADDRSTRLEN];
308 inet_ntop(AF_INET,
309 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
310 pn, sizeof(pn));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200311
Willy Tarreaueb472682010-05-28 18:46:57 +0200312 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
313 s->uniq_id, p->id, (unsigned short)l->fd, (unsigned short)cfd,
314 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
315 }
316 else {
317 char pn[INET6_ADDRSTRLEN];
318 inet_ntop(AF_INET6,
319 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
320 pn, sizeof(pn));
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200321
Willy Tarreaueb472682010-05-28 18:46:57 +0200322 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
323 s->uniq_id, p->id, (unsigned short)l->fd, (unsigned short)cfd,
324 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
325 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200326
Willy Tarreaueb472682010-05-28 18:46:57 +0200327 write(1, trash, len);
328 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200329
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200330 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200331 goto out_fail_req; /* no memory */
Willy Tarreau2df28e82008-08-17 15:20:19 +0200332
Willy Tarreaueb472682010-05-28 18:46:57 +0200333 s->req->size = global.tune.bufsize;
334 buffer_init(s->req);
335 s->req->prod = &s->si[0];
336 s->req->cons = &s->si[1];
337 s->si[0].ib = s->si[1].ob = s->req;
Willy Tarreaudc0a6a02008-08-03 20:38:13 +0200338
Willy Tarreaueb472682010-05-28 18:46:57 +0200339 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud7971282006-07-29 18:36:34 +0200340
Willy Tarreaueb472682010-05-28 18:46:57 +0200341 if (p->mode == PR_MODE_HTTP)
342 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200343
Willy Tarreaueb472682010-05-28 18:46:57 +0200344 /* activate default analysers enabled for this listener */
345 s->req->analysers = l->analysers;
Willy Tarreau54469402006-07-29 16:59:06 +0200346
Willy Tarreaueb472682010-05-28 18:46:57 +0200347 /* note: this should not happen anymore since there's always at least the switching rules */
348 if (!s->req->analysers) {
349 buffer_auto_connect(s->req); /* don't wait to establish connection */
350 buffer_auto_close(s->req); /* let the producer forward close requests */
351 }
Willy Tarreaud7971282006-07-29 18:36:34 +0200352
Willy Tarreaueb472682010-05-28 18:46:57 +0200353 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +0200354 s->req->wto = TICK_ETERNITY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200355
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200356 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
Willy Tarreaueb472682010-05-28 18:46:57 +0200357 goto out_fail_rep; /* no memory */
Willy Tarreau5d707e12009-06-28 11:09:07 +0200358
Willy Tarreaueb472682010-05-28 18:46:57 +0200359 s->rep->size = global.tune.bufsize;
360 buffer_init(s->rep);
361 s->rep->prod = &s->si[1];
362 s->rep->cons = &s->si[0];
363 s->si[0].ob = s->si[1].ib = s->rep;
364 s->rep->analysers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365
Willy Tarreaud04e8582010-05-31 12:31:35 +0200366 s->rep->rto = TICK_ETERNITY;
Willy Tarreaueb472682010-05-28 18:46:57 +0200367 s->rep->wto = s->fe->timeout.client;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368
Willy Tarreaueb472682010-05-28 18:46:57 +0200369 s->req->rex = TICK_ETERNITY;
370 s->req->wex = TICK_ETERNITY;
371 s->req->analyse_exp = TICK_ETERNITY;
372 s->rep->rex = TICK_ETERNITY;
373 s->rep->wex = TICK_ETERNITY;
374 s->rep->analyse_exp = TICK_ETERNITY;
375 t->expire = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376
Willy Tarreaueb472682010-05-28 18:46:57 +0200377 fd_insert(cfd);
378 fdtab[cfd].owner = &s->si[0];
379 fdtab[cfd].state = FD_STREADY;
380 fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
381 if (p->options & PR_O_TCP_NOLING)
382 fdtab[cfd].flags |= FD_FL_TCP_NOLING;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200383
Willy Tarreaueb472682010-05-28 18:46:57 +0200384 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
385 fdtab[cfd].cb[DIR_RD].b = s->req;
386 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
387 fdtab[cfd].cb[DIR_WR].b = s->rep;
388 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
389 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
Willy Tarreaua7e76142007-11-03 14:28:39 +0100390
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200391 if (unlikely((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
392 (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200393 /* Either we got a request from a monitoring system on an HTTP instance,
394 * or we're in health check mode with the 'httpchk' option enabled. In
395 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
396 */
397 struct chunk msg;
398 chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
399 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
400 s->req->analysers = 0;
401 t->expire = s->rep->wex;
402 }
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200403 else if (unlikely(p->mode == PR_MODE_HEALTH)) { /* health check mode, no client reading */
Willy Tarreaueb472682010-05-28 18:46:57 +0200404 struct chunk msg;
405 chunk_initstr(&msg, "OK\n");
406 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
407 s->req->analysers = 0;
408 t->expire = s->rep->wex;
409 }
410 else {
411 EV_FD_SET(cfd, DIR_RD);
412 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200413
Willy Tarreaueb472682010-05-28 18:46:57 +0200414 /* it is important not to call the wakeup function directly but to
415 * pass through task_wakeup(), because this one knows how to apply
416 * priorities to tasks.
417 */
418 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200419
Willy Tarreaueb472682010-05-28 18:46:57 +0200420 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100421
422 /* Error unrolling */
423 out_fail_rep:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200424 pool_free2(pool2_buffer, s->req);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100425 out_fail_req:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200426 pool_free2(p->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100427 out_fail_idx:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200428 pool_free2(p->rsp_cap_pool, txn->rsp.cap);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100429 out_fail_rspcap:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200430 pool_free2(p->req_cap_pool, txn->req.cap);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100431 out_fail_reqcap:
432 out_free_task:
Willy Tarreaua4613182009-03-21 18:13:21 +0100433 task_free(t);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100434 out_free_session:
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100435 LIST_DEL(&s->list);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100436 pool_free2(pool2_session, s);
437 out_close:
Willy Tarreaueb472682010-05-28 18:46:57 +0200438 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439}
440
Willy Tarreau645513a2010-05-24 20:55:15 +0200441/* set test->i to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200442static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200443acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
444 struct acl_expr *expr, struct acl_test *test) {
Willy Tarreau662b2d82007-05-08 19:56:15 +0200445
Willy Tarreau662b2d82007-05-08 19:56:15 +0200446 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200447
Willy Tarreau645513a2010-05-24 20:55:15 +0200448 test->i = l4->fe->uuid;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200449
Emeric Brun5d16eda2010-01-04 15:47:45 +0100450 return 1;
451}
452
Willy Tarreau645513a2010-05-24 20:55:15 +0200453/* set test->i to the number of connections per second reaching the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200454static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200455acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
456 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200457{
Willy Tarreau645513a2010-05-24 20:55:15 +0200458 test->flags = ACL_TEST_F_VOL_TEST;
459 if (expr->arg_len) {
460 /* another proxy was designated, we must look for it */
461 for (px = proxy; px; px = px->next)
462 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
463 break;
464 }
465 if (!px)
466 return 0;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100467
Willy Tarreau645513a2010-05-24 20:55:15 +0200468 test->i = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100469 return 1;
470}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100471
Willy Tarreau645513a2010-05-24 20:55:15 +0200472/* set test->i to the number of concurrent connections on the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200473static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200474acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
475 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200476{
Willy Tarreau645513a2010-05-24 20:55:15 +0200477 test->flags = ACL_TEST_F_VOL_TEST;
478 if (expr->arg_len) {
479 /* another proxy was designated, we must look for it */
480 for (px = proxy; px; px = px->next)
481 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
482 break;
483 }
484 if (!px)
485 return 0;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100486
Willy Tarreau645513a2010-05-24 20:55:15 +0200487 test->i = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100488 return 1;
489}
490
Willy Tarreau8797c062007-05-07 00:55:35 +0200491
492/* Note: must not be declared <const> as its list will be overwritten */
493static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +0200494 { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
495 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
496 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200497 { NULL, NULL, NULL, NULL },
498}};
499
500
501__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200502static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200503{
504 acl_register_keywords(&acl_kws);
505}
506
507
Willy Tarreaubaaee002006-06-26 02:48:02 +0200508/*
509 * Local variables:
510 * c-indent-level: 8
511 * c-basic-offset: 8
512 * End:
513 */