blob: f94c6ab2b231a3704821d57b17de0d9d937f2d79 [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 Tarreaud6896bc2013-01-07 22:48:29 +01004 * Copyright 2000-2013 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 Tarreau48a7e722010-12-24 15:26:39 +010023#include <netinet/tcp.h>
24
Willy Tarreauc7e42382012-08-24 19:22:53 +020025#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreau8b0cbf92010-10-15 23:23:19 +020028#include <common/debug.h>
29#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
Willy Tarreau8797c062007-05-07 00:55:35 +020034#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020035#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020036#include <proto/channel.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020038#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/log.h>
Willy Tarreaue5f20dc2006-12-03 15:21:35 +010040#include <proto/hdr_idx.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020041#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010043#include <proto/proxy.h>
Willy Tarreaud6896bc2013-01-07 22:48:29 +010044#include <proto/sample.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020045#include <proto/session.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010046#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047#include <proto/task.h>
48
Willy Tarreau81f9aa32010-06-01 17:45:26 +020049/* Finish a session accept() for a proxy (TCP or HTTP). It returns a negative
Willy Tarreauabe8ea52010-11-11 10:56:04 +010050 * value in case of a critical failure which must cause the listener to be
51 * disabled, a positive value in case of success, or zero if it is a success
Willy Tarreaub363a1f2013-10-01 10:45:07 +020052 * but the session must be closed ASAP (eg: monitoring). It only supports
53 * sessions with a connection in si[0].
Willy Tarreaubaaee002006-06-26 02:48:02 +020054 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020055int frontend_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +020056{
Willy Tarreaub363a1f2013-10-01 10:45:07 +020057 struct connection *conn = __objt_conn(s->si[0].end);
58 int cfd = conn->t.sock.fd;
Emeric Brunb982a3d2010-01-04 15:45:53 +010059
Willy Tarreaueb472682010-05-28 18:46:57 +020060 tv_zero(&s->logs.tv_request);
61 s->logs.t_queue = -1;
62 s->logs.t_connect = -1;
63 s->logs.t_data = -1;
64 s->logs.t_close = 0;
65 s->logs.bytes_in = s->logs.bytes_out = 0;
66 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
67 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau35a09942010-06-01 17:12:40 +020069 /* FIXME: the logs are horribly complicated now, because they are
70 * defined in <p>, <p>, and later <be> and <be>.
Willy Tarreaueb472682010-05-28 18:46:57 +020071 */
William Lallemandbddd4fd2012-02-27 11:23:10 +010072 s->do_log = sess_log;
Willy Tarreau35a09942010-06-01 17:12:40 +020073
74 /* default error reporting function, may be changed by analysers */
75 s->srv_error = default_srv_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +020076
Willy Tarreauf67c9782010-05-23 22:59:00 +020077 /* Adjust some socket options */
Willy Tarreau9c3bc222010-12-24 14:49:37 +010078 if (s->listener->addr.ss_family == AF_INET || s->listener->addr.ss_family == AF_INET6) {
79 if (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
80 (char *) &one, sizeof(one)) == -1)
81 goto out_return;
Willy Tarreauf67c9782010-05-23 22:59:00 +020082
Willy Tarreau9c3bc222010-12-24 14:49:37 +010083 if (s->fe->options & PR_O_TCP_CLI_KA)
84 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE,
85 (char *) &one, sizeof(one));
Willy Tarreauf67c9782010-05-23 22:59:00 +020086
Willy Tarreau9c3bc222010-12-24 14:49:37 +010087 if (s->fe->options & PR_O_TCP_NOLING)
Willy Tarreauad38ace2013-12-15 14:19:38 +010088 fdtab[cfd].linger_risk = 1;
89
Willy Tarreau48a7e722010-12-24 15:26:39 +010090#if defined(TCP_MAXSEG)
91 if (s->listener->maxseg < 0) {
92 /* we just want to reduce the current MSS by that value */
93 int mss;
Willy Tarreau7d286a02011-01-05 15:42:54 +010094 socklen_t mss_len = sizeof(mss);
Willy Tarreau48a7e722010-12-24 15:26:39 +010095 if (getsockopt(cfd, IPPROTO_TCP, TCP_MAXSEG, &mss, &mss_len) == 0) {
96 mss += s->listener->maxseg; /* remember, it's < 0 */
97 setsockopt(cfd, IPPROTO_TCP, TCP_MAXSEG, &mss, sizeof(mss));
98 }
99 }
100#endif
Willy Tarreau9c3bc222010-12-24 14:49:37 +0100101 }
Willy Tarreauf67c9782010-05-23 22:59:00 +0200102
103 if (global.tune.client_sndbuf)
104 setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
105
106 if (global.tune.client_rcvbuf)
107 setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
108
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200109 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200110 /* the captures are only used in HTTP frontends */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200111 if (unlikely(s->fe->nb_req_cap > 0 &&
112 (s->txn.req.cap = pool_alloc2(s->fe->req_cap_pool)) == NULL))
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100113 goto out_return; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200115 if (unlikely(s->fe->nb_rsp_cap > 0 &&
116 (s->txn.rsp.cap = pool_alloc2(s->fe->rsp_cap_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200117 goto out_free_reqcap; /* no memory */
Willy Tarreaueb472682010-05-28 18:46:57 +0200118 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119
Willy Tarreau25320b22013-03-24 07:22:08 +0100120 if (s->fe->http_needed) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200121 /* we have to allocate header indexes only if we know
122 * that we may make use of them. This of course includes
123 * (mode == PR_MODE_HTTP).
Willy Tarreau042cc792007-03-19 16:20:06 +0100124 */
Willy Tarreauac1932d2011-10-24 19:14:41 +0200125 s->txn.hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100126
Willy Tarreau34eb6712011-10-24 18:15:04 +0200127 if (unlikely((s->txn.hdr_idx.v = pool_alloc2(pool2_hdr_idx)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200128 goto out_free_rspcap; /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100129
Willy Tarreaueb472682010-05-28 18:46:57 +0200130 /* and now initialize the HTTP transaction state */
131 http_init_txn(s);
132 }
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100133
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200134 if ((s->fe->mode == PR_MODE_TCP || s->fe->mode == PR_MODE_HTTP)
William Lallemand0f99e342011-10-12 17:50:54 +0200135 && (!LIST_ISEMPTY(&s->fe->logsrvs))) {
Willy Tarreaud79a3b22012-12-28 09:40:16 +0100136 if (likely(!LIST_ISEMPTY(&s->fe->logformat))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200137 /* we have the client ip */
138 if (s->logs.logwait & LW_CLIP)
Willy Tarreaud79a3b22012-12-28 09:40:16 +0100139 if (!(s->logs.logwait &= ~(LW_CLIP|LW_INIT)))
Willy Tarreaueb472682010-05-28 18:46:57 +0200140 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +0200141 }
Willy Tarreau631f01c2011-09-05 00:36:48 +0200142 else {
Willy Tarreaueb472682010-05-28 18:46:57 +0200143 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200144
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200145 conn_get_from_addr(conn);
146 conn_get_to_addr(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200147
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200148 switch (addr_to_str(&conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200149 case AF_INET:
150 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200151 addr_to_str(&conn->addr.to, sn, sizeof(sn));
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200152 send_log(s->fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200153 pn, get_host_port(&conn->addr.from),
154 sn, get_host_port(&conn->addr.to),
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200155 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreau631f01c2011-09-05 00:36:48 +0200156 break;
157 case AF_UNIX:
158 /* UNIX socket, only the destination is known */
159 send_log(s->fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
160 s->listener->luid,
161 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
162 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200164 }
165 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200167 if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200168 char pn[INET6_ADDRSTRLEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200170 conn_get_from_addr(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200172 switch (addr_to_str(&conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200173 case AF_INET:
174 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100175 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
176 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200177 pn, get_host_port(&conn->addr.from));
Willy Tarreau631f01c2011-09-05 00:36:48 +0200178 break;
179 case AF_UNIX:
180 /* UNIX socket, only the destination is known */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100181 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [unix:%d]\n",
182 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
183 s->listener->luid);
Willy Tarreau631f01c2011-09-05 00:36:48 +0200184 break;
Emeric Brunab844ea2010-10-22 16:33:18 +0200185 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200186
Willy Tarreau89efaed2013-12-13 15:14:55 +0100187 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreaueb472682010-05-28 18:46:57 +0200188 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200190 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200191 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200192
Willy Tarreaueb472682010-05-28 18:46:57 +0200193 /* note: this should not happen anymore since there's always at least the switching rules */
194 if (!s->req->analysers) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200195 channel_auto_connect(s->req); /* don't wait to establish connection */
196 channel_auto_close(s->req); /* let the producer forward close requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200197 }
Willy Tarreaud7971282006-07-29 18:36:34 +0200198
Willy Tarreaueb472682010-05-28 18:46:57 +0200199 s->req->rto = s->fe->timeout.client;
Willy Tarreaueb472682010-05-28 18:46:57 +0200200 s->rep->wto = s->fe->timeout.client;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200201
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200202 /* everything's OK, let's go on */
Willy Tarreaueb472682010-05-28 18:46:57 +0200203 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100204
205 /* Error unrolling */
Willy Tarreau35a09942010-06-01 17:12:40 +0200206 out_free_rspcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200207 pool_free2(s->fe->rsp_cap_pool, s->txn.rsp.cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200208 out_free_reqcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200209 pool_free2(s->fe->req_cap_pool, s->txn.req.cap);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100210 out_return:
Willy Tarreaueb472682010-05-28 18:46:57 +0200211 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200212}
213
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100214/************************************************************************/
215/* All supported sample and ACL keywords must be declared here. */
216/************************************************************************/
217
Willy Tarreaua5e37562011-12-16 17:06:15 +0100218/* set temp integer to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200219static int
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100220smp_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200221 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau37406352012-04-23 16:16:37 +0200222{
Willy Tarreauf853c462012-04-23 18:53:56 +0200223 smp->flags = SMP_F_VOL_SESS;
224 smp->type = SMP_T_UINT;
225 smp->data.uint = l4->fe->uuid;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100226 return 1;
227}
228
Willy Tarreau34db1082012-04-19 17:16:54 +0200229/* set temp integer to the number of connections per second reaching the frontend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +0200230 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
Willy Tarreau34db1082012-04-19 17:16:54 +0200231 * an undefined behaviour.
232 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200233static int
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100234smp_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200235 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200236{
Willy Tarreau37406352012-04-23 16:16:37 +0200237 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +0200238 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +0200239 smp->data.uint = read_freq_ctr(&args->data.prx->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100240 return 1;
241}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100242
Willy Tarreau34db1082012-04-19 17:16:54 +0200243/* set temp integer to the number of concurrent connections on the frontend
Willy Tarreau0146c2e2012-04-20 11:37:56 +0200244 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
Willy Tarreau34db1082012-04-19 17:16:54 +0200245 * an undefined behaviour.
246 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200247static int
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100248smp_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +0200249 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +0200250{
Willy Tarreau37406352012-04-23 16:16:37 +0200251 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +0200252 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +0200253 smp->data.uint = args->data.prx->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100254 return 1;
255}
256
Willy Tarreau8797c062007-05-07 00:55:35 +0200257
Willy Tarreau61612d42012-04-19 18:42:05 +0200258/* Note: must not be declared <const> as its list will be overwritten.
259 * Please take care of keeping this list alphabetically sorted.
260 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200261static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100262 { "fe_conn", smp_fetch_fe_conn, ARG1(1,FE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
263 { "fe_id", smp_fetch_fe_id, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, },
264 { "fe_sess_rate", smp_fetch_fe_sess_rate, ARG1(1,FE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
265 { /* END */ },
266}};
267
268
269/* Note: must not be declared <const> as its list will be overwritten.
270 * Please take care of keeping this list alphabetically sorted.
271 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200272static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100273 { /* END */ },
Willy Tarreau8797c062007-05-07 00:55:35 +0200274}};
275
276
277__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200278static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200279{
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100280 sample_register_fetches(&smp_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +0200281 acl_register_keywords(&acl_kws);
282}
283
284
Willy Tarreaubaaee002006-06-26 02:48:02 +0200285/*
286 * Local variables:
287 * c-indent-level: 8
288 * c-basic-offset: 8
289 * End:
290 */