blob: 0e4d24fa5989a21382380a98740784e57b5b63fb [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 Tarreau35a09942010-06-01 17:12:40 +020072 /* minimum session initialization required for monitor mode below */
Willy Tarreaueb472682010-05-28 18:46:57 +020073 s->flags = 0;
Willy Tarreau35a09942010-06-01 17:12:40 +020074 s->logs.logwait = p->to_log;
Willy Tarreaubaaee002006-06-26 02:48:02 +020075
Willy Tarreaueb472682010-05-28 18:46:57 +020076 /* if this session comes from a known monitoring system, we want to ignore
Willy Tarreau35a09942010-06-01 17:12:40 +020077 * it as soon as possible, which means closing it immediately for TCP, but
78 * cleanly.
Willy Tarreaueb472682010-05-28 18:46:57 +020079 */
Willy Tarreaude3041d2010-05-31 10:56:17 +020080 if (unlikely((l->options & LI_O_CHK_MONNET) &&
Willy Tarreau2281b7f2010-05-28 19:29:49 +020081 addr->ss_family == AF_INET &&
82 (((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 +020083 if (p->mode == PR_MODE_TCP) {
84 pool_free2(pool2_session, s);
85 return 0;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +020086 }
Willy Tarreaueb472682010-05-28 18:46:57 +020087 s->flags |= SN_MONITOR;
Willy Tarreau35a09942010-06-01 17:12:40 +020088 s->logs.logwait = 0;
Willy Tarreaueb472682010-05-28 18:46:57 +020089 }
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +020090
Willy Tarreau35a09942010-06-01 17:12:40 +020091 /* OK, we're keeping the session, so let's properly initialize the session */
92 LIST_ADDQ(&sessions, &s->list);
93 LIST_INIT(&s->back_refs);
94
Willy Tarreau2281b7f2010-05-28 19:29:49 +020095 if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */
Willy Tarreaueb472682010-05-28 18:46:57 +020096 Alert("out of memory in event_accept().\n");
97 goto out_free_session;
98 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020099
Willy Tarreau35a09942010-06-01 17:12:40 +0200100 s->term_trace = 0;
101 s->cli_addr = *addr;
102 s->logs.accept_date = date; /* user-visible date for logging */
103 s->logs.tv_accept = now; /* corrected date for internal use */
104 s->uniq_id = totalconn;
105 proxy_inc_fe_ctr(l, p); /* note: cum_beconn will be increased once assigned */
106
Willy Tarreaueb472682010-05-28 18:46:57 +0200107 t->process = l->handler;
108 t->context = s;
109 t->nice = l->nice;
Willy Tarreau35a09942010-06-01 17:12:40 +0200110 t->expire = TICK_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111
Willy Tarreaueb472682010-05-28 18:46:57 +0200112 s->task = t;
113 s->listener = l;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
Willy Tarreaueb472682010-05-28 18:46:57 +0200115 /* Note: initially, the session's backend points to the frontend.
116 * This changes later when switching rules are executed or
117 * when the default backend is assigned.
118 */
Willy Tarreau35a09942010-06-01 17:12:40 +0200119 s->be = s->fe = p;
Willy Tarreaueb472682010-05-28 18:46:57 +0200120 s->req = s->rep = NULL; /* will be allocated later */
Willy Tarreaue803de22010-01-21 17:43:04 +0100121
Willy Tarreau35a09942010-06-01 17:12:40 +0200122 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
123 * to abort right here as soon as possible, we check the rules before
124 * even initializing the stream interfaces.
125 */
Willy Tarreaua5c0ab22010-05-31 10:30:33 +0200126 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
127 task_free(t);
128 LIST_DEL(&s->list);
129 pool_free2(pool2_session, s);
130 /* let's do a no-linger now to close with a single RST. */
131 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
132 return 0;
Willy Tarreauf67c9782010-05-23 22:59:00 +0200133 }
134
Willy Tarreau35a09942010-06-01 17:12:40 +0200135 /* this part should be common with other protocols */
136 s->si[0].fd = cfd;
137 s->si[0].owner = t;
138 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
139 s->si[0].err_type = SI_ET_NONE;
140 s->si[0].err_loc = NULL;
141 s->si[0].connect = NULL;
142 s->si[0].iohandler = NULL;
143 s->si[0].exp = TICK_ETERNITY;
144 s->si[0].flags = SI_FL_NONE;
145
146 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
147 s->si[0].flags |= SI_FL_INDEP_STR;
148
149 if (addr->ss_family == AF_INET || addr->ss_family == AF_INET6)
150 s->si[0].flags = SI_FL_CAP_SPLTCP; /* TCP/TCPv6 splicing possible */
151
152 /* add the various callbacks */
153 stream_sock_prepare_interface(&s->si[0]);
154
155 /* pre-initialize the other side's stream interface to an INIT state. The
156 * callbacks will be initialized before attempting to connect.
157 */
158 s->si[1].fd = -1; /* just to help with debugging */
159 s->si[1].owner = t;
160 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
161 s->si[1].err_type = SI_ET_NONE;
162 s->si[1].err_loc = NULL;
163 s->si[1].connect = NULL;
Willy Tarreaueb472682010-05-28 18:46:57 +0200164 s->si[1].iohandler = NULL;
Willy Tarreau35a09942010-06-01 17:12:40 +0200165 s->si[1].shutr = stream_int_shutr;
166 s->si[1].shutw = stream_int_shutw;
167 s->si[1].exp = TICK_ETERNITY;
168 s->si[1].flags = SI_FL_NONE;
Willy Tarreaua360d282010-05-31 19:17:12 +0200169
Willy Tarreauee55dc02010-06-01 10:56:34 +0200170 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreaueb472682010-05-28 18:46:57 +0200171 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172
Willy Tarreaueb472682010-05-28 18:46:57 +0200173 s->srv = s->prev_srv = s->srv_conn = NULL;
174 s->pend_pos = NULL;
Willy Tarreau73de9892006-11-30 11:40:23 +0100175
Willy Tarreaueb472682010-05-28 18:46:57 +0200176 /* init store persistence */
177 s->store_count = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178
Willy Tarreau35a09942010-06-01 17:12:40 +0200179 /* Adjust some socket options */
180 if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1)) {
181 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
182 goto out_free_task;
183 }
184
185 txn = &s->txn;
186 /* Those variables will be checked and freed if non-NULL in
187 * session.c:session_free(). It is important that they are
188 * properly initialized.
Willy Tarreaueb472682010-05-28 18:46:57 +0200189 */
Willy Tarreau35a09942010-06-01 17:12:40 +0200190 txn->sessid = NULL;
191 txn->srv_cookie = NULL;
192 txn->cli_cookie = NULL;
193 txn->uri = NULL;
194 txn->req.cap = NULL;
195 txn->rsp.cap = NULL;
196 txn->hdr_idx.v = NULL;
197 txn->hdr_idx.size = txn->hdr_idx.used = 0;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200198
Willy Tarreau35a09942010-06-01 17:12:40 +0200199 if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
200 goto out_free_task; /* no memory */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200201
Willy Tarreau35a09942010-06-01 17:12:40 +0200202 if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
203 goto out_free_req; /* no memory */
Willy Tarreauddb358d2006-12-17 22:55:52 +0100204
Willy Tarreau35a09942010-06-01 17:12:40 +0200205 /* initialize the request buffer */
206 s->req->size = global.tune.bufsize;
207 buffer_init(s->req);
208 s->req->prod = &s->si[0];
209 s->req->cons = &s->si[1];
210 s->si[0].ib = s->si[1].ob = s->req;
211 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
212
213 /* activate default analysers enabled for this listener */
214 s->req->analysers = l->analysers;
215
216 s->req->wto = TICK_ETERNITY;
217 s->req->rto = TICK_ETERNITY;
218 s->req->rex = TICK_ETERNITY;
219 s->req->wex = TICK_ETERNITY;
220 s->req->analyse_exp = TICK_ETERNITY;
221
222 /* initialize response buffer */
223 s->rep->size = global.tune.bufsize;
224 buffer_init(s->rep);
225 s->rep->prod = &s->si[1];
226 s->rep->cons = &s->si[0];
227 s->si[0].ob = s->si[1].ib = s->rep;
228 s->rep->analysers = 0;
229
230 s->rep->rto = TICK_ETERNITY;
231 s->rep->wto = TICK_ETERNITY;
232 s->rep->rex = TICK_ETERNITY;
233 s->rep->wex = TICK_ETERNITY;
234 s->rep->analyse_exp = TICK_ETERNITY;
235
236 /* finish initialization of the accepted file descriptor */
237 fd_insert(cfd);
238 fdtab[cfd].owner = &s->si[0];
239 fdtab[cfd].state = FD_STREADY;
240 fdtab[cfd].flags = 0;
241 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
242 fdtab[cfd].cb[DIR_RD].b = s->req;
243 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
244 fdtab[cfd].cb[DIR_WR].b = s->rep;
245 fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
246 fdinfo[cfd].peerlen = sizeof(s->cli_addr);
247 EV_FD_SET(cfd, DIR_RD);
248
249 /***************** to be moved to the TCP/HTTP frontend's accept() **************/
Emeric Brunb982a3d2010-01-04 15:45:53 +0100250
Willy Tarreaueb472682010-05-28 18:46:57 +0200251 tv_zero(&s->logs.tv_request);
252 s->logs.t_queue = -1;
253 s->logs.t_connect = -1;
254 s->logs.t_data = -1;
255 s->logs.t_close = 0;
256 s->logs.bytes_in = s->logs.bytes_out = 0;
257 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
258 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200259
Willy Tarreau35a09942010-06-01 17:12:40 +0200260 s->data_state = DATA_ST_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200261 s->data_source = DATA_SRC_NONE;
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100262
Willy Tarreau35a09942010-06-01 17:12:40 +0200263 /* FIXME: the logs are horribly complicated now, because they are
264 * defined in <p>, <p>, and later <be> and <be>.
Willy Tarreaueb472682010-05-28 18:46:57 +0200265 */
Willy Tarreau35a09942010-06-01 17:12:40 +0200266 if (s->logs.logwait & LW_REQ)
267 s->do_log = http_sess_log;
268 else
269 s->do_log = tcp_sess_log;
270
271 /* default error reporting function, may be changed by analysers */
272 s->srv_error = default_srv_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273
Willy Tarreauf67c9782010-05-23 22:59:00 +0200274 /* Adjust some socket options */
Willy Tarreau35a09942010-06-01 17:12:40 +0200275 if (unlikely(setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) {
Willy Tarreauf67c9782010-05-23 22:59:00 +0200276 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
Willy Tarreau35a09942010-06-01 17:12:40 +0200277 goto out_delete_cfd;
Willy Tarreauf67c9782010-05-23 22:59:00 +0200278 }
279
280 if (p->options & PR_O_TCP_CLI_KA)
281 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
282
283 if (p->options & PR_O_TCP_NOLING)
284 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
285
286 if (global.tune.client_sndbuf)
287 setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
288
289 if (global.tune.client_rcvbuf)
290 setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
291
Willy Tarreaueb472682010-05-28 18:46:57 +0200292 if (p->mode == PR_MODE_HTTP) {
293 /* the captures are only used in HTTP frontends */
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200294 if (unlikely(p->nb_req_cap > 0 &&
295 (txn->req.cap = pool_alloc2(p->req_cap_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200296 goto out_delete_cfd; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200297
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200298 if (unlikely(p->nb_rsp_cap > 0 &&
299 (txn->rsp.cap = pool_alloc2(p->rsp_cap_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200300 goto out_free_reqcap; /* no memory */
Willy Tarreaueb472682010-05-28 18:46:57 +0200301 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200302
Willy Tarreaueb472682010-05-28 18:46:57 +0200303 if (p->acl_requires & ACL_USE_L7_ANY) {
304 /* we have to allocate header indexes only if we know
305 * that we may make use of them. This of course includes
306 * (mode == PR_MODE_HTTP).
Willy Tarreau042cc792007-03-19 16:20:06 +0100307 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200308 txn->hdr_idx.size = MAX_HTTP_HDR;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100309
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200310 if (unlikely((txn->hdr_idx.v = pool_alloc2(p->hdr_idx_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200311 goto out_free_rspcap; /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100312
Willy Tarreaueb472682010-05-28 18:46:57 +0200313 /* and now initialize the HTTP transaction state */
314 http_init_txn(s);
315 }
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100316
Willy Tarreaueb472682010-05-28 18:46:57 +0200317 if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
318 && (p->logfac1 >= 0 || p->logfac2 >= 0)) {
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200319 if (likely(p->to_log)) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200320 /* we have the client ip */
321 if (s->logs.logwait & LW_CLIP)
322 if (!(s->logs.logwait &= ~LW_CLIP))
323 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +0200324 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200325 else if (s->cli_addr.ss_family == AF_INET) {
326 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200327
Willy Tarreaueb472682010-05-28 18:46:57 +0200328 if (!(s->flags & SN_FRT_ADDR_SET))
329 get_frt_addr(s);
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200330
Willy Tarreaueb472682010-05-28 18:46:57 +0200331 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr,
332 sn, sizeof(sn)) &&
333 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
334 pn, sizeof(pn))) {
335 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
336 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
337 sn, ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port),
338 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339 }
340 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200341 else {
342 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200343
344 if (!(s->flags & SN_FRT_ADDR_SET))
345 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200346
Willy Tarreaueb472682010-05-28 18:46:57 +0200347 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr,
348 sn, sizeof(sn)) &&
349 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
350 pn, sizeof(pn))) {
351 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
352 pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
353 sn, ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port),
354 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200355 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200356 }
357 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200359 if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200360 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361
Willy Tarreaueb472682010-05-28 18:46:57 +0200362 if (!(s->flags & SN_FRT_ADDR_SET))
363 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364
Willy Tarreaueb472682010-05-28 18:46:57 +0200365 if (s->cli_addr.ss_family == AF_INET) {
366 char pn[INET_ADDRSTRLEN];
367 inet_ntop(AF_INET,
368 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
369 pn, sizeof(pn));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370
Willy Tarreaueb472682010-05-28 18:46:57 +0200371 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
372 s->uniq_id, p->id, (unsigned short)l->fd, (unsigned short)cfd,
373 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
374 }
375 else {
376 char pn[INET6_ADDRSTRLEN];
377 inet_ntop(AF_INET6,
378 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
379 pn, sizeof(pn));
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200380
Willy Tarreaueb472682010-05-28 18:46:57 +0200381 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
382 s->uniq_id, p->id, (unsigned short)l->fd, (unsigned short)cfd,
383 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
384 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200385
Willy Tarreaueb472682010-05-28 18:46:57 +0200386 write(1, trash, len);
387 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388
Willy Tarreaueb472682010-05-28 18:46:57 +0200389 if (p->mode == PR_MODE_HTTP)
390 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200391
Willy Tarreaueb472682010-05-28 18:46:57 +0200392 /* note: this should not happen anymore since there's always at least the switching rules */
393 if (!s->req->analysers) {
394 buffer_auto_connect(s->req); /* don't wait to establish connection */
395 buffer_auto_close(s->req); /* let the producer forward close requests */
396 }
Willy Tarreaud7971282006-07-29 18:36:34 +0200397
Willy Tarreaueb472682010-05-28 18:46:57 +0200398 s->req->rto = s->fe->timeout.client;
Willy Tarreaueb472682010-05-28 18:46:57 +0200399 s->rep->wto = s->fe->timeout.client;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200400
Willy Tarreaueb472682010-05-28 18:46:57 +0200401 fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
402 if (p->options & PR_O_TCP_NOLING)
403 fdtab[cfd].flags |= FD_FL_TCP_NOLING;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200404
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200405 if (unlikely((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
406 (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200407 /* Either we got a request from a monitoring system on an HTTP instance,
408 * or we're in health check mode with the 'httpchk' option enabled. In
409 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
410 */
411 struct chunk msg;
412 chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
413 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
414 s->req->analysers = 0;
415 t->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200416 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200417 }
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200418 else if (unlikely(p->mode == PR_MODE_HEALTH)) { /* health check mode, no client reading */
Willy Tarreaueb472682010-05-28 18:46:57 +0200419 struct chunk msg;
420 chunk_initstr(&msg, "OK\n");
421 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
422 s->req->analysers = 0;
423 t->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200424 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200425 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200426
Willy Tarreau35a09942010-06-01 17:12:40 +0200427 /**********************************************/
428
Willy Tarreaueb472682010-05-28 18:46:57 +0200429 /* it is important not to call the wakeup function directly but to
430 * pass through task_wakeup(), because this one knows how to apply
431 * priorities to tasks.
432 */
433 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200434
Willy Tarreaueb472682010-05-28 18:46:57 +0200435 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100436
437 /* Error unrolling */
Willy Tarreau35a09942010-06-01 17:12:40 +0200438 out_free_rspcap:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200439 pool_free2(p->rsp_cap_pool, txn->rsp.cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200440 out_free_reqcap:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200441 pool_free2(p->req_cap_pool, txn->req.cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200442 out_delete_cfd:
443 fd_delete(cfd);
444 pool_free2(pool2_buffer, s->rep);
445 out_free_req:
446 pool_free2(pool2_buffer, s->req);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100447 out_free_task:
Willy Tarreaua4613182009-03-21 18:13:21 +0100448 task_free(t);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100449 out_free_session:
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100450 LIST_DEL(&s->list);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100451 pool_free2(pool2_session, s);
452 out_close:
Willy Tarreaueb472682010-05-28 18:46:57 +0200453 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200454}
455
Willy Tarreau645513a2010-05-24 20:55:15 +0200456/* set test->i to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200457static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200458acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
459 struct acl_expr *expr, struct acl_test *test) {
Willy Tarreau662b2d82007-05-08 19:56:15 +0200460
Willy Tarreau662b2d82007-05-08 19:56:15 +0200461 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200462
Willy Tarreau645513a2010-05-24 20:55:15 +0200463 test->i = l4->fe->uuid;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200464
Emeric Brun5d16eda2010-01-04 15:47:45 +0100465 return 1;
466}
467
Willy Tarreau645513a2010-05-24 20:55:15 +0200468/* set test->i to the number of connections per second reaching the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200469static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200470acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
471 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200472{
Willy Tarreau645513a2010-05-24 20:55:15 +0200473 test->flags = ACL_TEST_F_VOL_TEST;
474 if (expr->arg_len) {
475 /* another proxy was designated, we must look for it */
476 for (px = proxy; px; px = px->next)
477 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
478 break;
479 }
480 if (!px)
481 return 0;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100482
Willy Tarreau645513a2010-05-24 20:55:15 +0200483 test->i = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100484 return 1;
485}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100486
Willy Tarreau645513a2010-05-24 20:55:15 +0200487/* set test->i to the number of concurrent connections on the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200488static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200489acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
490 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200491{
Willy Tarreau645513a2010-05-24 20:55:15 +0200492 test->flags = ACL_TEST_F_VOL_TEST;
493 if (expr->arg_len) {
494 /* another proxy was designated, we must look for it */
495 for (px = proxy; px; px = px->next)
496 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
497 break;
498 }
499 if (!px)
500 return 0;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100501
Willy Tarreau645513a2010-05-24 20:55:15 +0200502 test->i = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100503 return 1;
504}
505
Willy Tarreau8797c062007-05-07 00:55:35 +0200506
507/* Note: must not be declared <const> as its list will be overwritten */
508static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +0200509 { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
510 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
511 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200512 { NULL, NULL, NULL, NULL },
513}};
514
515
516__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200517static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200518{
519 acl_register_keywords(&acl_kws);
520}
521
522
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523/*
524 * Local variables:
525 * c-indent-level: 8
526 * c-basic-offset: 8
527 * End:
528 */