blob: e8531e9ffe8045ab4dbde431c27324ad2b8fab81 [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 Tarreau87b09662015-04-03 00:22:06 +020045#include <proto/stream.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 Tarreau87b09662015-04-03 00:22:06 +020049/* Finish a stream 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
Willy Tarreaue0232f12015-04-05 18:01:06 +020051 * disabled, a positive or null value in case of success.
Willy Tarreaubaaee002006-06-26 02:48:02 +020052 */
Willy Tarreau87b09662015-04-03 00:22:06 +020053int frontend_accept(struct stream *s)
Willy Tarreaueb472682010-05-28 18:46:57 +020054{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020055 struct session *sess = s->sess;
Willy Tarreaue0232f12015-04-05 18:01:06 +020056 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020057 struct listener *l = sess->listener;
58 struct proxy *fe = sess->fe;
Willy Tarreaufb0afa72015-04-03 14:46:27 +020059
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020060 if (unlikely(fe->nb_req_cap > 0)) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +020061 if ((s->req_cap = pool_alloc2(fe->req_cap_pool)) == NULL)
Willy Tarreau9654e572014-11-18 18:49:19 +010062 goto out_return; /* no memory */
Willy Tarreaucb7dd012015-04-03 22:16:32 +020063 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau9654e572014-11-18 18:49:19 +010064 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020066 if (unlikely(fe->nb_rsp_cap > 0)) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +020067 if ((s->res_cap = pool_alloc2(fe->rsp_cap_pool)) == NULL)
Willy Tarreau9654e572014-11-18 18:49:19 +010068 goto out_free_reqcap; /* no memory */
Willy Tarreaucb7dd012015-04-03 22:16:32 +020069 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau9654e572014-11-18 18:49:19 +010070 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020071
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020072 if (fe->http_needed) {
Willy Tarreaueb472682010-05-28 18:46:57 +020073 /* we have to allocate header indexes only if we know
74 * that we may make use of them. This of course includes
75 * (mode == PR_MODE_HTTP).
Willy Tarreau042cc792007-03-19 16:20:06 +010076 */
Willy Tarreaueee5b512015-04-03 23:46:31 +020077 if (unlikely(!http_alloc_txn(s)))
Willy Tarreau35a09942010-06-01 17:12:40 +020078 goto out_free_rspcap; /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +010079
Willy Tarreaueb472682010-05-28 18:46:57 +020080 /* and now initialize the HTTP transaction state */
81 http_init_txn(s);
82 }
Willy Tarreaue5f20dc2006-12-03 15:21:35 +010083
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020084 if ((fe->mode == PR_MODE_TCP || fe->mode == PR_MODE_HTTP)
85 && (!LIST_ISEMPTY(&fe->logsrvs))) {
86 if (likely(!LIST_ISEMPTY(&fe->logformat))) {
Willy Tarreaueb472682010-05-28 18:46:57 +020087 /* we have the client ip */
88 if (s->logs.logwait & LW_CLIP)
Willy Tarreaud79a3b22012-12-28 09:40:16 +010089 if (!(s->logs.logwait &= ~(LW_CLIP|LW_INIT)))
Willy Tarreaueb472682010-05-28 18:46:57 +020090 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +020091 }
Willy Tarreaue0232f12015-04-05 18:01:06 +020092 else if (conn) {
Willy Tarreaueb472682010-05-28 18:46:57 +020093 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +020094
Willy Tarreaub363a1f2013-10-01 10:45:07 +020095 conn_get_from_addr(conn);
96 conn_get_to_addr(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020097
Willy Tarreaub363a1f2013-10-01 10:45:07 +020098 switch (addr_to_str(&conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +020099 case AF_INET:
100 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200101 addr_to_str(&conn->addr.to, sn, sizeof(sn));
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200102 send_log(fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200103 pn, get_host_port(&conn->addr.from),
104 sn, get_host_port(&conn->addr.to),
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200105 fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreau631f01c2011-09-05 00:36:48 +0200106 break;
107 case AF_UNIX:
108 /* UNIX socket, only the destination is known */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200109 send_log(fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200110 l->luid,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200111 fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreau631f01c2011-09-05 00:36:48 +0200112 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200114 }
115 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116
Willy Tarreaue0232f12015-04-05 18:01:06 +0200117 if (unlikely((global.mode & MODE_DEBUG) && conn &&
118 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200119 char pn[INET6_ADDRSTRLEN];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200121 conn_get_from_addr(conn);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200123 switch (addr_to_str(&conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200124 case AF_INET:
125 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100126 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
Willy Tarreaue0232f12015-04-05 18:01:06 +0200127 s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->t.sock.fd,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200128 pn, get_host_port(&conn->addr.from));
Willy Tarreau631f01c2011-09-05 00:36:48 +0200129 break;
130 case AF_UNIX:
131 /* UNIX socket, only the destination is known */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100132 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [unix:%d]\n",
Willy Tarreaue0232f12015-04-05 18:01:06 +0200133 s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->t.sock.fd,
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200134 l->luid);
Willy Tarreau631f01c2011-09-05 00:36:48 +0200135 break;
Emeric Brunab844ea2010-10-22 16:33:18 +0200136 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200137
Willy Tarreau89efaed2013-12-13 15:14:55 +0100138 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreaueb472682010-05-28 18:46:57 +0200139 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200140
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200141 if (fe->mode == PR_MODE_HTTP)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100142 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200144 /* everything's OK, let's go on */
Willy Tarreaueb472682010-05-28 18:46:57 +0200145 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100146
147 /* Error unrolling */
Willy Tarreau35a09942010-06-01 17:12:40 +0200148 out_free_rspcap:
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200149 pool_free2(fe->rsp_cap_pool, s->res_cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200150 out_free_reqcap:
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200151 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100152 out_return:
Willy Tarreaueb472682010-05-28 18:46:57 +0200153 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154}
155
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100156/************************************************************************/
157/* All supported sample and ACL keywords must be declared here. */
158/************************************************************************/
159
Willy Tarreaua5e37562011-12-16 17:06:15 +0100160/* set temp integer to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200161static int
Willy Tarreau192252e2015-04-04 01:47:55 +0200162smp_fetch_fe_id(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100163 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +0200164{
Willy Tarreauf853c462012-04-23 18:53:56 +0200165 smp->flags = SMP_F_VOL_SESS;
166 smp->type = SMP_T_UINT;
Willy Tarreau192252e2015-04-04 01:47:55 +0200167 smp->data.uint = sess->fe->uuid;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100168 return 1;
169}
170
Willy Tarreau34db1082012-04-19 17:16:54 +0200171/* set temp integer to the number of connections per second reaching the frontend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +0200172 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
Willy Tarreau34db1082012-04-19 17:16:54 +0200173 * an undefined behaviour.
174 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200175static int
Willy Tarreau192252e2015-04-04 01:47:55 +0200176smp_fetch_fe_sess_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100177 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200178{
Willy Tarreau37406352012-04-23 16:16:37 +0200179 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +0200180 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +0200181 smp->data.uint = read_freq_ctr(&args->data.prx->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100182 return 1;
183}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100184
Willy Tarreau34db1082012-04-19 17:16:54 +0200185/* set temp integer to the number of concurrent connections on the frontend
Willy Tarreau0146c2e2012-04-20 11:37:56 +0200186 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
Willy Tarreau34db1082012-04-19 17:16:54 +0200187 * an undefined behaviour.
188 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200189static int
Willy Tarreau192252e2015-04-04 01:47:55 +0200190smp_fetch_fe_conn(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100191 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +0200192{
Willy Tarreau37406352012-04-23 16:16:37 +0200193 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +0200194 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +0200195 smp->data.uint = args->data.prx->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100196 return 1;
197}
198
Willy Tarreau8797c062007-05-07 00:55:35 +0200199
Willy Tarreau61612d42012-04-19 18:42:05 +0200200/* Note: must not be declared <const> as its list will be overwritten.
201 * Please take care of keeping this list alphabetically sorted.
202 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200203static struct sample_fetch_kw_list smp_kws = {ILH, {
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100204 { "fe_conn", smp_fetch_fe_conn, ARG1(1,FE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
205 { "fe_id", smp_fetch_fe_id, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, },
206 { "fe_sess_rate", smp_fetch_fe_sess_rate, ARG1(1,FE), NULL, SMP_T_UINT, SMP_USE_INTRN, },
207 { /* END */ },
208}};
209
210
211/* Note: must not be declared <const> as its list will be overwritten.
212 * Please take care of keeping this list alphabetically sorted.
213 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200214static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100215 { /* END */ },
Willy Tarreau8797c062007-05-07 00:55:35 +0200216}};
217
218
219__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200220static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200221{
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100222 sample_register_fetches(&smp_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +0200223 acl_register_keywords(&acl_kws);
224}
225
226
Willy Tarreaubaaee002006-06-26 02:48:02 +0200227/*
228 * Local variables:
229 * c-indent-level: 8
230 * c-basic-offset: 8
231 * End:
232 */