blob: ef1001d8766ac5000f4484ac6ff03611ab3a3f71 [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 Tarreaueb472682010-05-28 18:46:57 +020067 if ((s = pool_alloc2(pool2_session)) == NULL) { /* disable this proxy for a while */
68 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 */
82 if (p->mon_mask.s_addr &&
83 addr->ss_family == AF_INET &&
84 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) {
85 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 Tarreaueb472682010-05-28 18:46:57 +020092 if ((t = task_new()) == NULL) { /* disable this proxy for a while */
93 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 if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) ||
98 (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
99 (char *) &one, sizeof(one)) == -1)) {
100 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
101 goto out_free_task;
102 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100103
Willy Tarreaueb472682010-05-28 18:46:57 +0200104 if (p->options & PR_O_TCP_CLI_KA)
105 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200106
Willy Tarreaueb472682010-05-28 18:46:57 +0200107 if (p->options & PR_O_TCP_NOLING)
108 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109
Willy Tarreaueb472682010-05-28 18:46:57 +0200110 if (global.tune.client_sndbuf)
111 setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112
Willy Tarreaueb472682010-05-28 18:46:57 +0200113 if (global.tune.client_rcvbuf)
114 setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115
Willy Tarreaueb472682010-05-28 18:46:57 +0200116 t->process = l->handler;
117 t->context = s;
118 t->nice = l->nice;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119
Willy Tarreaueb472682010-05-28 18:46:57 +0200120 s->task = t;
121 s->listener = l;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122
Willy Tarreaueb472682010-05-28 18:46:57 +0200123 /* Note: initially, the session's backend points to the frontend.
124 * This changes later when switching rules are executed or
125 * when the default backend is assigned.
126 */
127 s->be = s->fe = p;
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200128
Willy Tarreaueb472682010-05-28 18:46:57 +0200129 s->req = s->rep = NULL; /* will be allocated later */
Willy Tarreaue803de22010-01-21 17:43:04 +0100130
Willy Tarreaueb472682010-05-28 18:46:57 +0200131 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
132 s->si[0].err_type = SI_ET_NONE;
133 s->si[0].err_loc = NULL;
134 s->si[0].owner = t;
135 s->si[0].update = stream_sock_data_finish;
136 s->si[0].shutr = stream_sock_shutr;
137 s->si[0].shutw = stream_sock_shutw;
138 s->si[0].chk_rcv = stream_sock_chk_rcv;
139 s->si[0].chk_snd = stream_sock_chk_snd;
140 s->si[0].connect = NULL;
141 s->si[0].iohandler = NULL;
142 s->si[0].fd = cfd;
143 s->si[0].flags = SI_FL_NONE | SI_FL_CAP_SPLTCP; /* TCP splicing capable */
144 if (s->fe->options2 & PR_O2_INDEPSTR)
145 s->si[0].flags |= SI_FL_INDEP_STR;
146 s->si[0].exp = TICK_ETERNITY;
Willy Tarreaue803de22010-01-21 17:43:04 +0100147
Willy Tarreaueb472682010-05-28 18:46:57 +0200148 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
149 s->si[1].err_type = SI_ET_NONE;
150 s->si[1].err_loc = NULL;
151 s->si[1].owner = t;
152 s->si[1].update = stream_sock_data_finish;
153 s->si[1].shutr = stream_sock_shutr;
154 s->si[1].shutw = stream_sock_shutw;
155 s->si[1].chk_rcv = stream_sock_chk_rcv;
156 s->si[1].chk_snd = stream_sock_chk_snd;
157 s->si[1].connect = tcpv4_connect_server;
158 s->si[1].iohandler = NULL;
159 s->si[1].exp = TICK_ETERNITY;
160 s->si[1].fd = -1; /* just to help with debugging */
161 s->si[1].flags = SI_FL_NONE;
162 if (s->be->options2 & PR_O2_INDEPSTR)
163 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164
Willy Tarreaueb472682010-05-28 18:46:57 +0200165 s->srv = s->prev_srv = s->srv_conn = NULL;
166 s->pend_pos = NULL;
167 s->conn_retries = s->be->conn_retries;
Willy Tarreau73de9892006-11-30 11:40:23 +0100168
Willy Tarreaueb472682010-05-28 18:46:57 +0200169 /* init store persistence */
170 s->store_count = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171
Willy Tarreaueb472682010-05-28 18:46:57 +0200172 /* FIXME: the logs are horribly complicated now, because they are
173 * defined in <p>, <p>, and later <be> and <be>.
174 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200175
Willy Tarreaueb472682010-05-28 18:46:57 +0200176 if (s->flags & SN_MONITOR)
177 s->logs.logwait = 0;
178 else
179 s->logs.logwait = p->to_log;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200180
Willy Tarreaueb472682010-05-28 18:46:57 +0200181 if (s->logs.logwait & LW_REQ)
182 s->do_log = http_sess_log;
183 else
184 s->do_log = tcp_sess_log;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100185
Willy Tarreaueb472682010-05-28 18:46:57 +0200186 /* default error reporting function, may be changed by analysers */
187 s->srv_error = default_srv_error;
Emeric Brunb982a3d2010-01-04 15:45:53 +0100188
Willy Tarreaueb472682010-05-28 18:46:57 +0200189 s->logs.accept_date = date; /* user-visible date for logging */
190 s->logs.tv_accept = now; /* corrected date for internal use */
191 tv_zero(&s->logs.tv_request);
192 s->logs.t_queue = -1;
193 s->logs.t_connect = -1;
194 s->logs.t_data = -1;
195 s->logs.t_close = 0;
196 s->logs.bytes_in = s->logs.bytes_out = 0;
197 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
198 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200199
Willy Tarreaueb472682010-05-28 18:46:57 +0200200 s->data_source = DATA_SRC_NONE;
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100201
Willy Tarreaueb472682010-05-28 18:46:57 +0200202 s->uniq_id = totalconn;
203 proxy_inc_fe_ctr(l, p); /* note: cum_beconn will be increased once assigned */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100204
Willy Tarreaueb472682010-05-28 18:46:57 +0200205 txn = &s->txn;
206 /* Those variables will be checked and freed if non-NULL in
207 * session.c:session_free(). It is important that they are
208 * properly initialized.
209 */
210 txn->sessid = NULL;
211 txn->srv_cookie = NULL;
212 txn->cli_cookie = NULL;
213 txn->uri = NULL;
214 txn->req.cap = NULL;
215 txn->rsp.cap = NULL;
216 txn->hdr_idx.v = NULL;
217 txn->hdr_idx.size = txn->hdr_idx.used = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218
Willy Tarreaueb472682010-05-28 18:46:57 +0200219 if (p->mode == PR_MODE_HTTP) {
220 /* the captures are only used in HTTP frontends */
221 if (p->nb_req_cap > 0 &&
222 (txn->req.cap = pool_alloc2(p->req_cap_pool)) == NULL)
223 goto out_fail_reqcap; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224
Willy Tarreaueb472682010-05-28 18:46:57 +0200225 if (p->nb_rsp_cap > 0 &&
226 (txn->rsp.cap = pool_alloc2(p->rsp_cap_pool)) == NULL)
227 goto out_fail_rspcap; /* no memory */
228 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200229
Willy Tarreaueb472682010-05-28 18:46:57 +0200230 if (p->acl_requires & ACL_USE_L7_ANY) {
231 /* we have to allocate header indexes only if we know
232 * that we may make use of them. This of course includes
233 * (mode == PR_MODE_HTTP).
Willy Tarreau042cc792007-03-19 16:20:06 +0100234 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200235 txn->hdr_idx.size = MAX_HTTP_HDR;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100236
Willy Tarreaueb472682010-05-28 18:46:57 +0200237 if ((txn->hdr_idx.v = pool_alloc2(p->hdr_idx_pool)) == NULL)
238 goto out_fail_idx; /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100239
Willy Tarreaueb472682010-05-28 18:46:57 +0200240 /* and now initialize the HTTP transaction state */
241 http_init_txn(s);
242 }
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100243
Willy Tarreaueb472682010-05-28 18:46:57 +0200244 if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
245 && (p->logfac1 >= 0 || p->logfac2 >= 0)) {
246 if (p->to_log) {
247 /* we have the client ip */
248 if (s->logs.logwait & LW_CLIP)
249 if (!(s->logs.logwait &= ~LW_CLIP))
250 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +0200251 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200252 else if (s->cli_addr.ss_family == AF_INET) {
253 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200254
Willy Tarreaueb472682010-05-28 18:46:57 +0200255 if (!(s->flags & SN_FRT_ADDR_SET))
256 get_frt_addr(s);
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200257
Willy Tarreaueb472682010-05-28 18:46:57 +0200258 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr,
259 sn, sizeof(sn)) &&
260 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
261 pn, sizeof(pn))) {
262 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
263 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
264 sn, ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port),
265 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200266 }
267 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200268 else {
269 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200270
271 if (!(s->flags & SN_FRT_ADDR_SET))
272 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273
Willy Tarreaueb472682010-05-28 18:46:57 +0200274 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr,
275 sn, sizeof(sn)) &&
276 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
277 pn, sizeof(pn))) {
278 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
279 pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
280 sn, ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port),
281 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200282 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200283 }
284 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200285
Willy Tarreaueb472682010-05-28 18:46:57 +0200286 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
287 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200288
Willy Tarreaueb472682010-05-28 18:46:57 +0200289 if (!(s->flags & SN_FRT_ADDR_SET))
290 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200291
Willy Tarreaueb472682010-05-28 18:46:57 +0200292 if (s->cli_addr.ss_family == AF_INET) {
293 char pn[INET_ADDRSTRLEN];
294 inet_ntop(AF_INET,
295 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
296 pn, sizeof(pn));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200297
Willy Tarreaueb472682010-05-28 18:46:57 +0200298 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
299 s->uniq_id, p->id, (unsigned short)l->fd, (unsigned short)cfd,
300 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
301 }
302 else {
303 char pn[INET6_ADDRSTRLEN];
304 inet_ntop(AF_INET6,
305 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
306 pn, sizeof(pn));
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200307
Willy Tarreaueb472682010-05-28 18:46:57 +0200308 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
309 s->uniq_id, p->id, (unsigned short)l->fd, (unsigned short)cfd,
310 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
311 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200312
Willy Tarreaueb472682010-05-28 18:46:57 +0200313 write(1, trash, len);
314 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200315
Willy Tarreaueb472682010-05-28 18:46:57 +0200316 if ((s->req = pool_alloc2(pool2_buffer)) == NULL)
317 goto out_fail_req; /* no memory */
Willy Tarreau2df28e82008-08-17 15:20:19 +0200318
Willy Tarreaueb472682010-05-28 18:46:57 +0200319 s->req->size = global.tune.bufsize;
320 buffer_init(s->req);
321 s->req->prod = &s->si[0];
322 s->req->cons = &s->si[1];
323 s->si[0].ib = s->si[1].ob = s->req;
Willy Tarreaudc0a6a02008-08-03 20:38:13 +0200324
Willy Tarreaueb472682010-05-28 18:46:57 +0200325 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud7971282006-07-29 18:36:34 +0200326
Willy Tarreaueb472682010-05-28 18:46:57 +0200327 if (p->mode == PR_MODE_HTTP)
328 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200329
Willy Tarreaueb472682010-05-28 18:46:57 +0200330 /* activate default analysers enabled for this listener */
331 s->req->analysers = l->analysers;
Willy Tarreau54469402006-07-29 16:59:06 +0200332
Willy Tarreaueb472682010-05-28 18:46:57 +0200333 /* note: this should not happen anymore since there's always at least the switching rules */
334 if (!s->req->analysers) {
335 buffer_auto_connect(s->req); /* don't wait to establish connection */
336 buffer_auto_close(s->req); /* let the producer forward close requests */
337 }
Willy Tarreaud7971282006-07-29 18:36:34 +0200338
Willy Tarreaueb472682010-05-28 18:46:57 +0200339 s->req->rto = s->fe->timeout.client;
340 s->req->wto = s->be->timeout.server;
341 s->req->cto = s->be->timeout.connect;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200342
Willy Tarreaueb472682010-05-28 18:46:57 +0200343 if ((s->rep = pool_alloc2(pool2_buffer)) == NULL)
344 goto out_fail_rep; /* no memory */
Willy Tarreau5d707e12009-06-28 11:09:07 +0200345
Willy Tarreaueb472682010-05-28 18:46:57 +0200346 s->rep->size = global.tune.bufsize;
347 buffer_init(s->rep);
348 s->rep->prod = &s->si[1];
349 s->rep->cons = &s->si[0];
350 s->si[0].ob = s->si[1].ib = s->rep;
351 s->rep->analysers = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352
Willy Tarreaueb472682010-05-28 18:46:57 +0200353 s->rep->rto = s->be->timeout.server;
354 s->rep->wto = s->fe->timeout.client;
355 s->rep->cto = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356
Willy Tarreaueb472682010-05-28 18:46:57 +0200357 s->req->rex = TICK_ETERNITY;
358 s->req->wex = TICK_ETERNITY;
359 s->req->analyse_exp = TICK_ETERNITY;
360 s->rep->rex = TICK_ETERNITY;
361 s->rep->wex = TICK_ETERNITY;
362 s->rep->analyse_exp = TICK_ETERNITY;
363 t->expire = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364
Willy Tarreaueb472682010-05-28 18:46:57 +0200365 fd_insert(cfd);
366 fdtab[cfd].owner = &s->si[0];
367 fdtab[cfd].state = FD_STREADY;
368 fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
369 if (p->options & PR_O_TCP_NOLING)
370 fdtab[cfd].flags |= FD_FL_TCP_NOLING;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200371
Willy Tarreaueb472682010-05-28 18:46:57 +0200372 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
373 fdtab[cfd].cb[DIR_RD].b = s->req;
374 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
375 fdtab[cfd].cb[DIR_WR].b = s->rep;
376 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
377 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
Willy Tarreaua7e76142007-11-03 14:28:39 +0100378
Willy Tarreaueb472682010-05-28 18:46:57 +0200379 if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
380 (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK))) {
381 /* Either we got a request from a monitoring system on an HTTP instance,
382 * or we're in health check mode with the 'httpchk' option enabled. In
383 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
384 */
385 struct chunk msg;
386 chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
387 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
388 s->req->analysers = 0;
389 t->expire = s->rep->wex;
390 }
391 else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */
392 struct chunk msg;
393 chunk_initstr(&msg, "OK\n");
394 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
395 s->req->analysers = 0;
396 t->expire = s->rep->wex;
397 }
398 else {
399 EV_FD_SET(cfd, DIR_RD);
400 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200401
Willy Tarreaueb472682010-05-28 18:46:57 +0200402 /* it is important not to call the wakeup function directly but to
403 * pass through task_wakeup(), because this one knows how to apply
404 * priorities to tasks.
405 */
406 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200407
Willy Tarreaueb472682010-05-28 18:46:57 +0200408 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100409
410 /* Error unrolling */
411 out_fail_rep:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200412 pool_free2(pool2_buffer, s->req);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100413 out_fail_req:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200414 pool_free2(p->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100415 out_fail_idx:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200416 pool_free2(p->rsp_cap_pool, txn->rsp.cap);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100417 out_fail_rspcap:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200418 pool_free2(p->req_cap_pool, txn->req.cap);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100419 out_fail_reqcap:
420 out_free_task:
Willy Tarreaua4613182009-03-21 18:13:21 +0100421 task_free(t);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100422 out_free_session:
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100423 LIST_DEL(&s->list);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100424 pool_free2(pool2_session, s);
425 out_close:
Willy Tarreaueb472682010-05-28 18:46:57 +0200426 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200427}
428
Willy Tarreau645513a2010-05-24 20:55:15 +0200429/* set test->i to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200430static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200431acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
432 struct acl_expr *expr, struct acl_test *test) {
Willy Tarreau662b2d82007-05-08 19:56:15 +0200433
Willy Tarreau662b2d82007-05-08 19:56:15 +0200434 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200435
Willy Tarreau645513a2010-05-24 20:55:15 +0200436 test->i = l4->fe->uuid;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200437
Emeric Brun5d16eda2010-01-04 15:47:45 +0100438 return 1;
439}
440
Willy Tarreau645513a2010-05-24 20:55:15 +0200441/* set test->i to the number of connections per second reaching the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200442static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200443acl_fetch_fe_sess_rate(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 Tarreau645513a2010-05-24 20:55:15 +0200446 test->flags = ACL_TEST_F_VOL_TEST;
447 if (expr->arg_len) {
448 /* another proxy was designated, we must look for it */
449 for (px = proxy; px; px = px->next)
450 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
451 break;
452 }
453 if (!px)
454 return 0;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100455
Willy Tarreau645513a2010-05-24 20:55:15 +0200456 test->i = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100457 return 1;
458}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100459
Willy Tarreau645513a2010-05-24 20:55:15 +0200460/* set test->i to the number of concurrent connections on the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200461static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200462acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
463 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200464{
Willy Tarreau645513a2010-05-24 20:55:15 +0200465 test->flags = ACL_TEST_F_VOL_TEST;
466 if (expr->arg_len) {
467 /* another proxy was designated, we must look for it */
468 for (px = proxy; px; px = px->next)
469 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
470 break;
471 }
472 if (!px)
473 return 0;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100474
Willy Tarreau645513a2010-05-24 20:55:15 +0200475 test->i = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100476 return 1;
477}
478
Willy Tarreau8797c062007-05-07 00:55:35 +0200479
480/* Note: must not be declared <const> as its list will be overwritten */
481static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +0200482 { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
483 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
484 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200485 { NULL, NULL, NULL, NULL },
486}};
487
488
489__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200490static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200491{
492 acl_register_keywords(&acl_kws);
493}
494
495
Willy Tarreaubaaee002006-06-26 02:48:02 +0200496/*
497 * Local variables:
498 * c-indent-level: 8
499 * c-basic-offset: 8
500 * End:
501 */