blob: 23b9c3c31cf7a55be56123aebf68777859913ac4 [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>
Willy Tarreau0108d902018-11-25 19:14:37 +010029#include <common/initcall.h>
Willy Tarreau8b0cbf92010-10-15 23:23:19 +020030#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034
Willy Tarreau8797c062007-05-07 00:55:35 +020035#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020036#include <proto/arg.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020037#include <proto/channel.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020039#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/log.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020041#include <proto/proto_tcp.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020042#include <proto/http_ana.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 ((fe->mode == PR_MODE_TCP || fe->mode == PR_MODE_HTTP)
61 && (!LIST_ISEMPTY(&fe->logsrvs))) {
62 if (likely(!LIST_ISEMPTY(&fe->logformat))) {
Willy Tarreaueb472682010-05-28 18:46:57 +020063 /* we have the client ip */
64 if (s->logs.logwait & LW_CLIP)
Willy Tarreaud79a3b22012-12-28 09:40:16 +010065 if (!(s->logs.logwait &= ~(LW_CLIP|LW_INIT)))
Willy Tarreaueb472682010-05-28 18:46:57 +020066 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +020067 }
Willy Tarreaua0a4b092019-07-17 11:25:46 +020068 else if (conn && !conn_get_src(conn)) {
69 send_log(fe, LOG_INFO, "Connect from unknown source to listener %d (%s/%s)\n",
70 l->luid, fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
71 }
Willy Tarreaue0232f12015-04-05 18:01:06 +020072 else if (conn) {
Willy Tarreaueb472682010-05-28 18:46:57 +020073 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreaua0a4b092019-07-17 11:25:46 +020074 int port;
Willy Tarreaubaaee002006-06-26 02:48:02 +020075
Willy Tarreau44a7d8e2019-07-17 17:11:40 +020076 switch (addr_to_str(conn->src, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +020077 case AF_INET:
78 case AF_INET6:
Willy Tarreaua0a4b092019-07-17 11:25:46 +020079 if (conn_get_dst(conn)) {
Willy Tarreau44a7d8e2019-07-17 17:11:40 +020080 addr_to_str(conn->dst, sn, sizeof(sn));
81 port = get_host_port(conn->dst);
Willy Tarreaua0a4b092019-07-17 11:25:46 +020082 } else {
83 strcpy(sn, "undetermined address");
84 port = 0;
85 }
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020086 send_log(fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreau44a7d8e2019-07-17 17:11:40 +020087 pn, get_host_port(conn->src),
Willy Tarreaua0a4b092019-07-17 11:25:46 +020088 sn, port,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020089 fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreau631f01c2011-09-05 00:36:48 +020090 break;
91 case AF_UNIX:
92 /* UNIX socket, only the destination is known */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020093 send_log(fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
Willy Tarreaufb0afa72015-04-03 14:46:27 +020094 l->luid,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020095 fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreau631f01c2011-09-05 00:36:48 +020096 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 }
Willy Tarreaueb472682010-05-28 18:46:57 +020098 }
99 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100
Willy Tarreaue0232f12015-04-05 18:01:06 +0200101 if (unlikely((global.mode & MODE_DEBUG) && conn &&
102 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200103 char pn[INET6_ADDRSTRLEN];
Willy Tarreau9c266802016-12-04 19:05:24 +0100104 char alpn[16] = "<none>";
Willy Tarreau20814ff2017-09-15 11:43:32 +0200105 const char *alpn_str = NULL;
106 int alpn_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107
Willy Tarreau20814ff2017-09-15 11:43:32 +0200108 /* try to report the ALPN value when available (also works for NPN) */
Willy Tarreaua0a4b092019-07-17 11:25:46 +0200109 if (conn == cs_conn(objt_cs(s->si[0].end))) {
Willy Tarreau20814ff2017-09-15 11:43:32 +0200110 if (conn_get_alpn(conn, &alpn_str, &alpn_len) && alpn_str) {
111 int len = MIN(alpn_len, sizeof(alpn) - 1);
112 memcpy(alpn, alpn_str, len);
113 alpn[len] = 0;
114 }
Willy Tarreau9c266802016-12-04 19:05:24 +0100115 }
116
Willy Tarreaua0a4b092019-07-17 11:25:46 +0200117 if (!conn_get_src(conn)) {
118 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [listener:%d] ALPN=%s\n",
119 s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->handle.fd,
120 l->luid, alpn);
121 }
Willy Tarreau44a7d8e2019-07-17 17:11:40 +0200122 else switch (addr_to_str(conn->src, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200123 case AF_INET:
124 case AF_INET6:
Willy Tarreau9c266802016-12-04 19:05:24 +0100125 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [%s:%d] ALPN=%s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200126 s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->handle.fd,
Willy Tarreau44a7d8e2019-07-17 17:11:40 +0200127 pn, get_host_port(conn->src), alpn);
Willy Tarreau631f01c2011-09-05 00:36:48 +0200128 break;
129 case AF_UNIX:
130 /* UNIX socket, only the destination is known */
Willy Tarreau9c266802016-12-04 19:05:24 +0100131 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [unix:%d] ALPN=%s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200132 s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->handle.fd,
Willy Tarreau9c266802016-12-04 19:05:24 +0100133 l->luid, alpn);
Willy Tarreau631f01c2011-09-05 00:36:48 +0200134 break;
Emeric Brunab844ea2010-10-22 16:33:18 +0200135 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200136
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200137 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreaueb472682010-05-28 18:46:57 +0200138 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200140 if (fe->mode == PR_MODE_HTTP)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100141 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200142
Willy Tarreau0a6bed22016-12-04 18:39:22 +0100143 if (unlikely(fe->nb_req_cap > 0)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100144 if ((s->req_cap = pool_alloc(fe->req_cap_pool)) == NULL)
Willy Tarreau0a6bed22016-12-04 18:39:22 +0100145 goto out_return; /* no memory */
146 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
147 }
148
149 if (unlikely(fe->nb_rsp_cap > 0)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100150 if ((s->res_cap = pool_alloc(fe->rsp_cap_pool)) == NULL)
Willy Tarreau0a6bed22016-12-04 18:39:22 +0100151 goto out_free_reqcap; /* no memory */
152 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
153 }
154
155 if (fe->http_needed) {
156 /* we have to allocate header indexes only if we know
157 * that we may make use of them. This of course includes
158 * (mode == PR_MODE_HTTP).
159 */
160 if (unlikely(!http_alloc_txn(s)))
161 goto out_free_rspcap; /* no memory */
162
163 /* and now initialize the HTTP transaction state */
164 http_init_txn(s);
165 }
166
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200167 /* everything's OK, let's go on */
Willy Tarreaueb472682010-05-28 18:46:57 +0200168 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100169
170 /* Error unrolling */
Willy Tarreau35a09942010-06-01 17:12:40 +0200171 out_free_rspcap:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100172 pool_free(fe->rsp_cap_pool, s->res_cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200173 out_free_reqcap:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100174 pool_free(fe->req_cap_pool, s->req_cap);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100175 out_return:
Willy Tarreaueb472682010-05-28 18:46:57 +0200176 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200177}
178
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100179/************************************************************************/
180/* All supported sample and ACL keywords must be declared here. */
181/************************************************************************/
182
Willy Tarreaua5e37562011-12-16 17:06:15 +0100183/* set temp integer to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200184static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200185smp_fetch_fe_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +0200186{
Willy Tarreauf853c462012-04-23 18:53:56 +0200187 smp->flags = SMP_F_VOL_SESS;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200188 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200189 smp->data.u.sint = smp->sess->fe->uuid;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100190 return 1;
191}
192
Marcin Deranekd2471c22016-12-12 14:08:05 +0100193/* set string to the name of the frontend */
194static int
195smp_fetch_fe_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
196{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200197 smp->data.u.str.area = (char *)smp->sess->fe->id;
198 if (!smp->data.u.str.area)
Marcin Deranekd2471c22016-12-12 14:08:05 +0100199 return 0;
200
201 smp->data.type = SMP_T_STR;
202 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200203 smp->data.u.str.data = strlen(smp->data.u.str.area);
Marcin Deranekd2471c22016-12-12 14:08:05 +0100204 return 1;
205}
206
Marcin Deranek9a66dfb2018-04-13 14:37:50 +0200207/* set string to the name of the default backend */
208static int
209smp_fetch_fe_defbe(const struct arg *args, struct sample *smp, const char *kw, void *private)
210{
211 if (!smp->sess->fe->defbe.be)
212 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200213 smp->data.u.str.area = (char *)smp->sess->fe->defbe.be->id;
214 if (!smp->data.u.str.area)
Marcin Deranek9a66dfb2018-04-13 14:37:50 +0200215 return 0;
216
217 smp->data.type = SMP_T_STR;
218 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200219 smp->data.u.str.data = strlen(smp->data.u.str.area);
Marcin Deranek9a66dfb2018-04-13 14:37:50 +0200220 return 1;
221}
222
Nenad Merdanovicad9a7e92016-10-03 04:57:37 +0200223/* set temp integer to the number of HTTP requests per second reaching the frontend.
224 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
225 * an undefined behaviour.
226 */
227static int
228smp_fetch_fe_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
229{
230 smp->flags = SMP_F_VOL_TEST;
231 smp->data.type = SMP_T_SINT;
232 smp->data.u.sint = read_freq_ctr(&args->data.prx->fe_req_per_sec);
233 return 1;
234}
235
Willy Tarreau34db1082012-04-19 17:16:54 +0200236/* set temp integer to the number of connections per second reaching the frontend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +0200237 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
Willy Tarreau34db1082012-04-19 17:16:54 +0200238 * an undefined behaviour.
239 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200240static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200241smp_fetch_fe_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200242{
Willy Tarreau37406352012-04-23 16:16:37 +0200243 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200244 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200245 smp->data.u.sint = read_freq_ctr(&args->data.prx->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100246 return 1;
247}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100248
Willy Tarreau34db1082012-04-19 17:16:54 +0200249/* set temp integer to the number of concurrent connections on the frontend
Willy Tarreau0146c2e2012-04-20 11:37:56 +0200250 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
Willy Tarreau34db1082012-04-19 17:16:54 +0200251 * an undefined behaviour.
252 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200253static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200254smp_fetch_fe_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +0200255{
Willy Tarreau37406352012-04-23 16:16:37 +0200256 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200257 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200258 smp->data.u.sint = args->data.prx->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100259 return 1;
260}
261
Willy Tarreau8797c062007-05-07 00:55:35 +0200262
Willy Tarreau61612d42012-04-19 18:42:05 +0200263/* Note: must not be declared <const> as its list will be overwritten.
264 * Please take care of keeping this list alphabetically sorted.
265 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200266static struct sample_fetch_kw_list smp_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200267 { "fe_conn", smp_fetch_fe_conn, ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Marcin Deranek9a66dfb2018-04-13 14:37:50 +0200268 { "fe_defbe", smp_fetch_fe_defbe, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200269 { "fe_id", smp_fetch_fe_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
Marcin Deranekd2471c22016-12-12 14:08:05 +0100270 { "fe_name", smp_fetch_fe_name, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
Nenad Merdanovicad9a7e92016-10-03 04:57:37 +0200271 { "fe_req_rate", smp_fetch_fe_req_rate, ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200272 { "fe_sess_rate", smp_fetch_fe_sess_rate, ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100273 { /* END */ },
274}};
275
Willy Tarreau0108d902018-11-25 19:14:37 +0100276INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100277
278/* Note: must not be declared <const> as its list will be overwritten.
279 * Please take care of keeping this list alphabetically sorted.
280 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200281static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100282 { /* END */ },
Willy Tarreau8797c062007-05-07 00:55:35 +0200283}};
284
Willy Tarreau0108d902018-11-25 19:14:37 +0100285INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +0200286
Willy Tarreaubaaee002006-06-26 02:48:02 +0200287/*
288 * Local variables:
289 * c-indent-level: 8
290 * c-basic-offset: 8
291 * End:
292 */