2006-06-26 00:48:02 +00:00
|
|
|
/*
|
2010-05-24 19:02:37 +00:00
|
|
|
* Frontend variables and functions.
|
2006-06-26 00:48:02 +00:00
|
|
|
*
|
2010-05-24 19:02:37 +00:00
|
|
|
* Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
|
2006-06-26 00:48:02 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2006-06-29 15:53:05 +00:00
|
|
|
#include <string.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2006-06-29 15:53:05 +00:00
|
|
|
#include <common/compat.h>
|
2006-06-29 16:54:54 +00:00
|
|
|
#include <common/config.h>
|
2006-06-29 15:53:05 +00:00
|
|
|
#include <common/time.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
#include <types/global.h>
|
|
|
|
|
2007-05-06 22:55:35 +00:00
|
|
|
#include <proto/acl.h>
|
2006-07-29 14:59:06 +00:00
|
|
|
#include <proto/buffers.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
#include <proto/fd.h>
|
2010-05-24 19:02:37 +00:00
|
|
|
#include <proto/frontend.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
#include <proto/log.h>
|
2006-12-03 14:21:35 +00:00
|
|
|
#include <proto/hdr_idx.h>
|
2009-08-16 12:02:45 +00:00
|
|
|
#include <proto/proto_tcp.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
#include <proto/proto_http.h>
|
2009-03-05 17:43:00 +00:00
|
|
|
#include <proto/proxy.h>
|
2007-05-13 17:43:47 +00:00
|
|
|
#include <proto/session.h>
|
2008-11-30 18:48:07 +00:00
|
|
|
#include <proto/stream_interface.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
#include <proto/stream_sock.h>
|
|
|
|
#include <proto/task.h>
|
|
|
|
|
|
|
|
|
2007-05-08 17:46:30 +00:00
|
|
|
/* Retrieves the original destination address used by the client, and sets the
|
|
|
|
* SN_FRT_ADDR_SET flag.
|
|
|
|
*/
|
|
|
|
void get_frt_addr(struct session *s)
|
|
|
|
{
|
|
|
|
socklen_t namelen = sizeof(s->frt_addr);
|
|
|
|
|
2008-12-07 15:27:56 +00:00
|
|
|
if (get_original_dst(s->si[0].fd, (struct sockaddr_in *)&s->frt_addr, &namelen) == -1)
|
|
|
|
getsockname(s->si[0].fd, (struct sockaddr *)&s->frt_addr, &namelen);
|
2007-05-08 17:46:30 +00:00
|
|
|
s->flags |= SN_FRT_ADDR_SET;
|
|
|
|
}
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
/* This function is called from the protocol layer accept() in order to instanciate
|
|
|
|
* a new proxy. It returns a positive value upon success, 0 if the connection needs
|
|
|
|
* to be closed and ignored, or a negative value upon critical failure.
|
2006-06-26 00:48:02 +00:00
|
|
|
*/
|
2010-05-28 16:46:57 +00:00
|
|
|
int frontend_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
|
|
|
|
{
|
|
|
|
struct proxy *p = l->frontend;
|
2006-06-26 00:48:02 +00:00
|
|
|
struct session *s;
|
2007-03-03 19:51:44 +00:00
|
|
|
struct http_txn *txn;
|
2006-06-26 00:48:02 +00:00
|
|
|
struct task *t;
|
2010-05-23 20:59:00 +00:00
|
|
|
struct tcp_rule *rule;
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2010-05-28 17:29:49 +00:00
|
|
|
if (unlikely((s = pool_alloc2(pool2_session)) == NULL)) {
|
2010-05-28 16:46:57 +00:00
|
|
|
Alert("out of memory in event_accept().\n");
|
|
|
|
goto out_close;
|
2009-03-06 08:18:27 +00:00
|
|
|
}
|
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
LIST_ADDQ(&sessions, &s->list);
|
|
|
|
LIST_INIT(&s->back_refs);
|
|
|
|
|
|
|
|
s->flags = 0;
|
|
|
|
s->term_trace = 0;
|
|
|
|
s->cli_addr = *addr;
|
|
|
|
|
|
|
|
/* if this session comes from a known monitoring system, we want to ignore
|
|
|
|
* it as soon as possible, which means closing it immediately for TCP.
|
|
|
|
*/
|
2010-05-28 17:29:49 +00:00
|
|
|
if (unlikely(p->mon_mask.s_addr &&
|
|
|
|
addr->ss_family == AF_INET &&
|
|
|
|
(((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr)) {
|
2010-05-28 16:46:57 +00:00
|
|
|
if (p->mode == PR_MODE_TCP) {
|
|
|
|
pool_free2(pool2_session, s);
|
|
|
|
return 0;
|
2009-08-16 16:20:44 +00:00
|
|
|
}
|
2010-05-28 16:46:57 +00:00
|
|
|
s->flags |= SN_MONITOR;
|
|
|
|
}
|
2009-08-16 16:20:44 +00:00
|
|
|
|
2010-05-28 17:29:49 +00:00
|
|
|
if (unlikely((t = task_new()) == NULL)) { /* disable this proxy for a while */
|
2010-05-28 16:46:57 +00:00
|
|
|
Alert("out of memory in event_accept().\n");
|
|
|
|
goto out_free_session;
|
|
|
|
}
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
t->process = l->handler;
|
|
|
|
t->context = s;
|
|
|
|
t->nice = l->nice;
|
|
|
|
|
|
|
|
s->task = t;
|
|
|
|
s->listener = l;
|
|
|
|
|
|
|
|
/* Note: initially, the session's backend points to the frontend.
|
|
|
|
* This changes later when switching rules are executed or
|
|
|
|
* when the default backend is assigned.
|
|
|
|
*/
|
|
|
|
s->be = s->fe = p;
|
|
|
|
|
|
|
|
s->req = s->rep = NULL; /* will be allocated later */
|
|
|
|
|
2010-05-23 20:59:00 +00:00
|
|
|
/* this part should be common with other protocols */
|
2010-05-28 16:46:57 +00:00
|
|
|
s->si[0].state = s->si[0].prev_state = SI_ST_EST;
|
|
|
|
s->si[0].err_type = SI_ET_NONE;
|
|
|
|
s->si[0].err_loc = NULL;
|
|
|
|
s->si[0].owner = t;
|
|
|
|
s->si[0].update = stream_sock_data_finish;
|
|
|
|
s->si[0].shutr = stream_sock_shutr;
|
|
|
|
s->si[0].shutw = stream_sock_shutw;
|
|
|
|
s->si[0].chk_rcv = stream_sock_chk_rcv;
|
|
|
|
s->si[0].chk_snd = stream_sock_chk_snd;
|
|
|
|
s->si[0].connect = NULL;
|
|
|
|
s->si[0].iohandler = NULL;
|
|
|
|
s->si[0].fd = cfd;
|
|
|
|
s->si[0].flags = SI_FL_NONE | SI_FL_CAP_SPLTCP; /* TCP splicing capable */
|
2010-05-28 17:29:49 +00:00
|
|
|
if (likely(s->fe->options2 & PR_O2_INDEPSTR))
|
2010-05-28 16:46:57 +00:00
|
|
|
s->si[0].flags |= SI_FL_INDEP_STR;
|
|
|
|
s->si[0].exp = TICK_ETERNITY;
|
|
|
|
|
2010-05-23 20:59:00 +00:00
|
|
|
s->logs.accept_date = date; /* user-visible date for logging */
|
|
|
|
s->logs.tv_accept = now; /* corrected date for internal use */
|
|
|
|
s->uniq_id = totalconn;
|
|
|
|
proxy_inc_fe_ctr(l, p); /* note: cum_beconn will be increased once assigned */
|
|
|
|
|
|
|
|
/* now evaluate the tcp-request rules */
|
|
|
|
list_for_each_entry(rule, &p->tcp_req.l4_rules, list) {
|
|
|
|
int ret = ACL_PAT_PASS;
|
|
|
|
|
|
|
|
if (rule->cond) {
|
|
|
|
ret = acl_exec_cond(rule->cond, p, s, &s->txn, ACL_DIR_REQ);
|
|
|
|
ret = acl_pass(ret);
|
|
|
|
if (rule->cond->pol == ACL_COND_UNLESS)
|
|
|
|
ret = !ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
/* we have a matching rule. */
|
|
|
|
if (rule->action == TCP_ACT_REJECT) {
|
|
|
|
p->counters.denied_req++;
|
|
|
|
if (l->counters)
|
|
|
|
l->counters->denied_req++;
|
|
|
|
|
|
|
|
if (!(s->flags & SN_ERR_MASK))
|
|
|
|
s->flags |= SN_ERR_PRXCOND;
|
|
|
|
if (!(s->flags & SN_FINST_MASK))
|
|
|
|
s->flags |= SN_FINST_R;
|
|
|
|
|
|
|
|
task_free(t);
|
|
|
|
LIST_DEL(&s->list);
|
|
|
|
pool_free2(pool2_session, s);
|
|
|
|
|
|
|
|
/* let's do a no-linger now to close with a single RST. */
|
|
|
|
setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* otherwise it's an accept */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pre-initialize the other side's stream interface */
|
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
s->si[1].state = s->si[1].prev_state = SI_ST_INI;
|
|
|
|
s->si[1].err_type = SI_ET_NONE;
|
|
|
|
s->si[1].err_loc = NULL;
|
|
|
|
s->si[1].owner = t;
|
|
|
|
s->si[1].update = stream_sock_data_finish;
|
|
|
|
s->si[1].shutr = stream_sock_shutr;
|
|
|
|
s->si[1].shutw = stream_sock_shutw;
|
|
|
|
s->si[1].chk_rcv = stream_sock_chk_rcv;
|
|
|
|
s->si[1].chk_snd = stream_sock_chk_snd;
|
|
|
|
s->si[1].connect = tcpv4_connect_server;
|
|
|
|
s->si[1].iohandler = NULL;
|
|
|
|
s->si[1].exp = TICK_ETERNITY;
|
|
|
|
s->si[1].fd = -1; /* just to help with debugging */
|
|
|
|
s->si[1].flags = SI_FL_NONE;
|
2010-05-28 17:29:49 +00:00
|
|
|
if (likely(s->be->options2 & PR_O2_INDEPSTR))
|
2010-05-28 16:46:57 +00:00
|
|
|
s->si[1].flags |= SI_FL_INDEP_STR;
|
|
|
|
|
|
|
|
s->srv = s->prev_srv = s->srv_conn = NULL;
|
|
|
|
s->pend_pos = NULL;
|
|
|
|
s->conn_retries = s->be->conn_retries;
|
|
|
|
|
|
|
|
/* init store persistence */
|
|
|
|
s->store_count = 0;
|
|
|
|
|
|
|
|
/* FIXME: the logs are horribly complicated now, because they are
|
|
|
|
* defined in <p>, <p>, and later <be> and <be>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (s->flags & SN_MONITOR)
|
|
|
|
s->logs.logwait = 0;
|
|
|
|
else
|
|
|
|
s->logs.logwait = p->to_log;
|
|
|
|
|
|
|
|
if (s->logs.logwait & LW_REQ)
|
|
|
|
s->do_log = http_sess_log;
|
|
|
|
else
|
|
|
|
s->do_log = tcp_sess_log;
|
|
|
|
|
|
|
|
/* default error reporting function, may be changed by analysers */
|
|
|
|
s->srv_error = default_srv_error;
|
|
|
|
|
|
|
|
tv_zero(&s->logs.tv_request);
|
|
|
|
s->logs.t_queue = -1;
|
|
|
|
s->logs.t_connect = -1;
|
|
|
|
s->logs.t_data = -1;
|
|
|
|
s->logs.t_close = 0;
|
|
|
|
s->logs.bytes_in = s->logs.bytes_out = 0;
|
|
|
|
s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
|
|
|
|
s->logs.srv_queue_size = 0; /* we will get this number soon */
|
|
|
|
|
|
|
|
s->data_source = DATA_SRC_NONE;
|
|
|
|
|
|
|
|
txn = &s->txn;
|
|
|
|
/* Those variables will be checked and freed if non-NULL in
|
|
|
|
* session.c:session_free(). It is important that they are
|
|
|
|
* properly initialized.
|
|
|
|
*/
|
|
|
|
txn->sessid = NULL;
|
|
|
|
txn->srv_cookie = NULL;
|
|
|
|
txn->cli_cookie = NULL;
|
|
|
|
txn->uri = NULL;
|
|
|
|
txn->req.cap = NULL;
|
|
|
|
txn->rsp.cap = NULL;
|
|
|
|
txn->hdr_idx.v = NULL;
|
|
|
|
txn->hdr_idx.size = txn->hdr_idx.used = 0;
|
|
|
|
|
2010-05-23 20:59:00 +00:00
|
|
|
/* Adjust some socket options */
|
2010-05-28 17:29:49 +00:00
|
|
|
if (unlikely(fcntl(cfd, F_SETFL, O_NONBLOCK) == -1 ||
|
|
|
|
setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
|
|
|
|
(char *) &one, sizeof(one)) == -1)) {
|
2010-05-23 20:59:00 +00:00
|
|
|
Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
|
|
|
|
goto out_free_task;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->options & PR_O_TCP_CLI_KA)
|
|
|
|
setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
|
|
|
|
|
|
|
|
if (p->options & PR_O_TCP_NOLING)
|
|
|
|
setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
|
|
|
|
|
|
|
|
if (global.tune.client_sndbuf)
|
|
|
|
setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
|
|
|
|
|
|
|
|
if (global.tune.client_rcvbuf)
|
|
|
|
setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
|
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
if (p->mode == PR_MODE_HTTP) {
|
|
|
|
/* the captures are only used in HTTP frontends */
|
2010-05-28 17:29:49 +00:00
|
|
|
if (unlikely(p->nb_req_cap > 0 &&
|
|
|
|
(txn->req.cap = pool_alloc2(p->req_cap_pool)) == NULL))
|
2010-05-28 16:46:57 +00:00
|
|
|
goto out_fail_reqcap; /* no memory */
|
|
|
|
|
2010-05-28 17:29:49 +00:00
|
|
|
if (unlikely(p->nb_rsp_cap > 0 &&
|
|
|
|
(txn->rsp.cap = pool_alloc2(p->rsp_cap_pool)) == NULL))
|
2010-05-28 16:46:57 +00:00
|
|
|
goto out_fail_rspcap; /* no memory */
|
|
|
|
}
|
2008-08-10 20:55:22 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
if (p->acl_requires & ACL_USE_L7_ANY) {
|
|
|
|
/* we have to allocate header indexes only if we know
|
|
|
|
* that we may make use of them. This of course includes
|
|
|
|
* (mode == PR_MODE_HTTP).
|
2006-06-26 00:48:02 +00:00
|
|
|
*/
|
2010-05-28 16:46:57 +00:00
|
|
|
txn->hdr_idx.size = MAX_HTTP_HDR;
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2010-05-28 17:29:49 +00:00
|
|
|
if (unlikely((txn->hdr_idx.v = pool_alloc2(p->hdr_idx_pool)) == NULL))
|
2010-05-28 16:46:57 +00:00
|
|
|
goto out_fail_idx; /* no memory */
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
/* and now initialize the HTTP transaction state */
|
|
|
|
http_init_txn(s);
|
|
|
|
}
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
|
|
|
|
&& (p->logfac1 >= 0 || p->logfac2 >= 0)) {
|
2010-05-28 17:29:49 +00:00
|
|
|
if (likely(p->to_log)) {
|
2010-05-28 16:46:57 +00:00
|
|
|
/* we have the client ip */
|
|
|
|
if (s->logs.logwait & LW_CLIP)
|
|
|
|
if (!(s->logs.logwait &= ~LW_CLIP))
|
|
|
|
s->do_log(s);
|
2009-07-10 21:52:51 +00:00
|
|
|
}
|
2010-05-28 16:46:57 +00:00
|
|
|
else if (s->cli_addr.ss_family == AF_INET) {
|
|
|
|
char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
if (!(s->flags & SN_FRT_ADDR_SET))
|
|
|
|
get_frt_addr(s);
|
2009-12-22 14:03:09 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr,
|
|
|
|
sn, sizeof(sn)) &&
|
|
|
|
inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
|
|
|
|
pn, sizeof(pn))) {
|
|
|
|
send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
|
|
|
|
pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
|
|
|
|
sn, ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port),
|
|
|
|
p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-28 16:46:57 +00:00
|
|
|
else {
|
|
|
|
char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
|
2007-05-08 17:46:30 +00:00
|
|
|
|
|
|
|
if (!(s->flags & SN_FRT_ADDR_SET))
|
|
|
|
get_frt_addr(s);
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr,
|
|
|
|
sn, sizeof(sn)) &&
|
|
|
|
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
|
|
|
|
pn, sizeof(pn))) {
|
|
|
|
send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
|
|
|
|
pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
|
|
|
|
sn, ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port),
|
|
|
|
p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-28 16:46:57 +00:00
|
|
|
}
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2010-05-28 17:29:49 +00:00
|
|
|
if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
|
2010-05-28 16:46:57 +00:00
|
|
|
int len;
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
if (!(s->flags & SN_FRT_ADDR_SET))
|
|
|
|
get_frt_addr(s);
|
2008-08-17 13:20:19 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
if (s->cli_addr.ss_family == AF_INET) {
|
|
|
|
char pn[INET_ADDRSTRLEN];
|
|
|
|
inet_ntop(AF_INET,
|
|
|
|
(const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
|
|
|
|
pn, sizeof(pn));
|
2008-08-03 18:38:13 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
|
|
|
|
s->uniq_id, p->id, (unsigned short)l->fd, (unsigned short)cfd,
|
|
|
|
pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-05-28 16:46:57 +00:00
|
|
|
char pn[INET6_ADDRSTRLEN];
|
|
|
|
inet_ntop(AF_INET6,
|
|
|
|
(const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
|
|
|
|
pn, sizeof(pn));
|
|
|
|
|
|
|
|
len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
|
|
|
|
s->uniq_id, p->id, (unsigned short)l->fd, (unsigned short)cfd,
|
|
|
|
pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
write(1, trash, len);
|
|
|
|
}
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2010-05-28 17:29:49 +00:00
|
|
|
if (unlikely((s->req = pool_alloc2(pool2_buffer)) == NULL))
|
2010-05-28 16:46:57 +00:00
|
|
|
goto out_fail_req; /* no memory */
|
2009-08-16 16:20:44 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
s->req->size = global.tune.bufsize;
|
|
|
|
buffer_init(s->req);
|
|
|
|
s->req->prod = &s->si[0];
|
|
|
|
s->req->cons = &s->si[1];
|
|
|
|
s->si[0].ib = s->si[1].ob = s->req;
|
2007-11-03 13:28:39 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
|
|
|
|
|
|
|
|
if (p->mode == PR_MODE_HTTP)
|
|
|
|
s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
|
2009-10-04 13:43:17 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
/* activate default analysers enabled for this listener */
|
|
|
|
s->req->analysers = l->analysers;
|
|
|
|
|
|
|
|
/* note: this should not happen anymore since there's always at least the switching rules */
|
|
|
|
if (!s->req->analysers) {
|
|
|
|
buffer_auto_connect(s->req); /* don't wait to establish connection */
|
|
|
|
buffer_auto_close(s->req); /* let the producer forward close requests */
|
|
|
|
}
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2010-05-28 16:46:57 +00:00
|
|
|
s->req->rto = s->fe->timeout.client;
|
|
|
|
s->req->wto = s->be->timeout.server;
|
|
|
|
s->req->cto = s->be->timeout.connect;
|
|
|
|
|
2010-05-28 17:29:49 +00:00
|
|
|
if (unlikely((s->rep = pool_alloc2(pool2_buffer)) == NULL))
|
2010-05-28 16:46:57 +00:00
|
|
|
goto out_fail_rep; /* no memory */
|
|
|
|
|
|
|
|
s->rep->size = global.tune.bufsize;
|
|
|
|
buffer_init(s->rep);
|
|
|
|
s->rep->prod = &s->si[1];
|
|
|
|
s->rep->cons = &s->si[0];
|
|
|
|
s->si[0].ob = s->si[1].ib = s->rep;
|
|
|
|
s->rep->analysers = 0;
|
|
|
|
|
|
|
|
s->rep->rto = s->be->timeout.server;
|
|
|
|
s->rep->wto = s->fe->timeout.client;
|
|
|
|
s->rep->cto = TICK_ETERNITY;
|
|
|
|
|
|
|
|
s->req->rex = TICK_ETERNITY;
|
|
|
|
s->req->wex = TICK_ETERNITY;
|
|
|
|
s->req->analyse_exp = TICK_ETERNITY;
|
|
|
|
s->rep->rex = TICK_ETERNITY;
|
|
|
|
s->rep->wex = TICK_ETERNITY;
|
|
|
|
s->rep->analyse_exp = TICK_ETERNITY;
|
|
|
|
t->expire = TICK_ETERNITY;
|
|
|
|
|
|
|
|
fd_insert(cfd);
|
|
|
|
fdtab[cfd].owner = &s->si[0];
|
|
|
|
fdtab[cfd].state = FD_STREADY;
|
|
|
|
fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
|
|
|
|
if (p->options & PR_O_TCP_NOLING)
|
|
|
|
fdtab[cfd].flags |= FD_FL_TCP_NOLING;
|
|
|
|
|
|
|
|
fdtab[cfd].cb[DIR_RD].f = l->proto->read;
|
|
|
|
fdtab[cfd].cb[DIR_RD].b = s->req;
|
|
|
|
fdtab[cfd].cb[DIR_WR].f = l->proto->write;
|
|
|
|
fdtab[cfd].cb[DIR_WR].b = s->rep;
|
|
|
|
fdinfo[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
|
|
|
|
fdinfo[cfd].peerlen = sizeof(s->cli_addr);
|
|
|
|
|
2010-05-28 17:29:49 +00:00
|
|
|
if (unlikely((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
|
|
|
|
(p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK)))) {
|
2010-05-28 16:46:57 +00:00
|
|
|
/* Either we got a request from a monitoring system on an HTTP instance,
|
|
|
|
* or we're in health check mode with the 'httpchk' option enabled. In
|
|
|
|
* both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
|
|
|
|
*/
|
|
|
|
struct chunk msg;
|
|
|
|
chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
|
|
|
|
stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
|
|
|
|
s->req->analysers = 0;
|
|
|
|
t->expire = s->rep->wex;
|
|
|
|
}
|
2010-05-28 17:29:49 +00:00
|
|
|
else if (unlikely(p->mode == PR_MODE_HEALTH)) { /* health check mode, no client reading */
|
2010-05-28 16:46:57 +00:00
|
|
|
struct chunk msg;
|
|
|
|
chunk_initstr(&msg, "OK\n");
|
|
|
|
stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
|
|
|
|
s->req->analysers = 0;
|
|
|
|
t->expire = s->rep->wex;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
EV_FD_SET(cfd, DIR_RD);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it is important not to call the wakeup function directly but to
|
|
|
|
* pass through task_wakeup(), because this one knows how to apply
|
|
|
|
* priorities to tasks.
|
|
|
|
*/
|
|
|
|
task_wakeup(t, TASK_WOKEN_INIT);
|
|
|
|
|
|
|
|
return 1;
|
2007-11-04 16:51:50 +00:00
|
|
|
|
|
|
|
/* Error unrolling */
|
|
|
|
out_fail_rep:
|
2008-08-03 15:41:33 +00:00
|
|
|
pool_free2(pool2_buffer, s->req);
|
2007-11-04 16:51:50 +00:00
|
|
|
out_fail_req:
|
2008-08-03 15:41:33 +00:00
|
|
|
pool_free2(p->hdr_idx_pool, txn->hdr_idx.v);
|
2007-11-04 16:51:50 +00:00
|
|
|
out_fail_idx:
|
2008-08-03 15:41:33 +00:00
|
|
|
pool_free2(p->rsp_cap_pool, txn->rsp.cap);
|
2007-11-04 16:51:50 +00:00
|
|
|
out_fail_rspcap:
|
2008-08-03 15:41:33 +00:00
|
|
|
pool_free2(p->req_cap_pool, txn->req.cap);
|
2007-11-04 16:51:50 +00:00
|
|
|
out_fail_reqcap:
|
|
|
|
out_free_task:
|
2009-03-21 17:13:21 +00:00
|
|
|
task_free(t);
|
2007-11-04 16:51:50 +00:00
|
|
|
out_free_session:
|
2008-11-23 18:53:55 +00:00
|
|
|
LIST_DEL(&s->list);
|
2007-11-04 16:51:50 +00:00
|
|
|
pool_free2(pool2_session, s);
|
|
|
|
out_close:
|
2010-05-28 16:46:57 +00:00
|
|
|
return -1;
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
2010-05-24 18:55:15 +00:00
|
|
|
/* set test->i to the id of the frontend */
|
2007-06-10 08:06:18 +00:00
|
|
|
static int
|
2010-05-24 18:55:15 +00:00
|
|
|
acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
|
|
|
|
struct acl_expr *expr, struct acl_test *test) {
|
2007-05-08 17:56:15 +00:00
|
|
|
|
|
|
|
test->flags = ACL_TEST_F_READ_ONLY;
|
|
|
|
|
2010-05-24 18:55:15 +00:00
|
|
|
test->i = l4->fe->uuid;
|
2007-05-08 17:56:15 +00:00
|
|
|
|
2010-01-04 14:47:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-05-24 18:55:15 +00:00
|
|
|
/* set test->i to the number of connections per second reaching the frontend */
|
2007-06-10 08:06:18 +00:00
|
|
|
static int
|
2010-05-24 18:55:15 +00:00
|
|
|
acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
|
|
|
|
struct acl_expr *expr, struct acl_test *test)
|
2007-05-08 17:56:15 +00:00
|
|
|
{
|
2010-05-24 18:55:15 +00:00
|
|
|
test->flags = ACL_TEST_F_VOL_TEST;
|
|
|
|
if (expr->arg_len) {
|
|
|
|
/* another proxy was designated, we must look for it */
|
|
|
|
for (px = proxy; px; px = px->next)
|
|
|
|
if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!px)
|
|
|
|
return 0;
|
2010-01-04 14:47:45 +00:00
|
|
|
|
2010-05-24 18:55:15 +00:00
|
|
|
test->i = read_freq_ctr(&px->fe_sess_per_sec);
|
2010-01-04 14:47:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2007-11-29 14:43:32 +00:00
|
|
|
|
2010-05-24 18:55:15 +00:00
|
|
|
/* set test->i to the number of concurrent connections on the frontend */
|
2007-06-10 08:06:18 +00:00
|
|
|
static int
|
2010-05-24 18:55:15 +00:00
|
|
|
acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
|
|
|
|
struct acl_expr *expr, struct acl_test *test)
|
2007-05-06 22:55:35 +00:00
|
|
|
{
|
2010-05-24 18:55:15 +00:00
|
|
|
test->flags = ACL_TEST_F_VOL_TEST;
|
|
|
|
if (expr->arg_len) {
|
|
|
|
/* another proxy was designated, we must look for it */
|
|
|
|
for (px = proxy; px; px = px->next)
|
|
|
|
if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!px)
|
|
|
|
return 0;
|
2010-01-12 20:59:30 +00:00
|
|
|
|
2010-05-24 18:55:15 +00:00
|
|
|
test->i = px->feconn;
|
2010-01-12 20:59:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-05-06 22:55:35 +00:00
|
|
|
|
|
|
|
/* Note: must not be declared <const> as its list will be overwritten */
|
|
|
|
static struct acl_kw_list acl_kws = {{ },{
|
2010-05-24 18:55:15 +00:00
|
|
|
{ "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
|
|
|
|
{ "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
|
|
|
|
{ "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
|
2007-05-06 22:55:35 +00:00
|
|
|
{ NULL, NULL, NULL, NULL },
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
|
|
__attribute__((constructor))
|
2010-05-24 19:02:37 +00:00
|
|
|
static void __frontend_init(void)
|
2007-05-06 22:55:35 +00:00
|
|
|
{
|
|
|
|
acl_register_keywords(&acl_kws);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|