blob: 52527118b236933c923fe82b53be69cb573cb309 [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 Tarreaudcc048a2020-06-04 19:11:43 +020025#include <haproxy/acl.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020026#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/arg.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020028#include <haproxy/chunk.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020029#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020030#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/global.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020032#include <haproxy/http_ana.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020033#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/proto_tcp.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020035#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020036#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020038#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020039#include <haproxy/task.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020040#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/tools.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau87b09662015-04-03 00:22:06 +020044/* Finish a stream accept() for a proxy (TCP or HTTP). It returns a negative
Willy Tarreauabe8ea52010-11-11 10:56:04 +010045 * value in case of a critical failure which must cause the listener to be
Willy Tarreaue0232f12015-04-05 18:01:06 +020046 * disabled, a positive or null value in case of success.
Willy Tarreaubaaee002006-06-26 02:48:02 +020047 */
Willy Tarreau87b09662015-04-03 00:22:06 +020048int frontend_accept(struct stream *s)
Willy Tarreaueb472682010-05-28 18:46:57 +020049{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020050 struct session *sess = s->sess;
Willy Tarreaue0232f12015-04-05 18:01:06 +020051 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020052 struct listener *l = sess->listener;
53 struct proxy *fe = sess->fe;
Willy Tarreaufb0afa72015-04-03 14:46:27 +020054
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020055 if ((fe->mode == PR_MODE_TCP || fe->mode == PR_MODE_HTTP)
56 && (!LIST_ISEMPTY(&fe->logsrvs))) {
57 if (likely(!LIST_ISEMPTY(&fe->logformat))) {
Willy Tarreaueb472682010-05-28 18:46:57 +020058 /* we have the client ip */
59 if (s->logs.logwait & LW_CLIP)
Willy Tarreaud79a3b22012-12-28 09:40:16 +010060 if (!(s->logs.logwait &= ~(LW_CLIP|LW_INIT)))
Willy Tarreaueb472682010-05-28 18:46:57 +020061 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +020062 }
Willy Tarreaua0a4b092019-07-17 11:25:46 +020063 else if (conn && !conn_get_src(conn)) {
64 send_log(fe, LOG_INFO, "Connect from unknown source to listener %d (%s/%s)\n",
65 l->luid, fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
66 }
Willy Tarreaue0232f12015-04-05 18:01:06 +020067 else if (conn) {
Willy Tarreaueb472682010-05-28 18:46:57 +020068 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreaua0a4b092019-07-17 11:25:46 +020069 int port;
Willy Tarreaubaaee002006-06-26 02:48:02 +020070
Willy Tarreau44a7d8e2019-07-17 17:11:40 +020071 switch (addr_to_str(conn->src, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +020072 case AF_INET:
73 case AF_INET6:
Willy Tarreaua0a4b092019-07-17 11:25:46 +020074 if (conn_get_dst(conn)) {
Willy Tarreau44a7d8e2019-07-17 17:11:40 +020075 addr_to_str(conn->dst, sn, sizeof(sn));
76 port = get_host_port(conn->dst);
Willy Tarreaua0a4b092019-07-17 11:25:46 +020077 } else {
78 strcpy(sn, "undetermined address");
79 port = 0;
80 }
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020081 send_log(fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreau44a7d8e2019-07-17 17:11:40 +020082 pn, get_host_port(conn->src),
Willy Tarreaua0a4b092019-07-17 11:25:46 +020083 sn, port,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020084 fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreau631f01c2011-09-05 00:36:48 +020085 break;
86 case AF_UNIX:
87 /* UNIX socket, only the destination is known */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020088 send_log(fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
Willy Tarreaufb0afa72015-04-03 14:46:27 +020089 l->luid,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020090 fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreau631f01c2011-09-05 00:36:48 +020091 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +020092 }
Willy Tarreaueb472682010-05-28 18:46:57 +020093 }
94 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020095
Willy Tarreaue0232f12015-04-05 18:01:06 +020096 if (unlikely((global.mode & MODE_DEBUG) && conn &&
97 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +020098 char pn[INET6_ADDRSTRLEN];
Willy Tarreau9c266802016-12-04 19:05:24 +010099 char alpn[16] = "<none>";
Willy Tarreau20814ff2017-09-15 11:43:32 +0200100 const char *alpn_str = NULL;
101 int alpn_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102
Willy Tarreau20814ff2017-09-15 11:43:32 +0200103 /* try to report the ALPN value when available (also works for NPN) */
Willy Tarreaua0a4b092019-07-17 11:25:46 +0200104 if (conn == cs_conn(objt_cs(s->si[0].end))) {
Willy Tarreau20814ff2017-09-15 11:43:32 +0200105 if (conn_get_alpn(conn, &alpn_str, &alpn_len) && alpn_str) {
106 int len = MIN(alpn_len, sizeof(alpn) - 1);
107 memcpy(alpn, alpn_str, len);
108 alpn[len] = 0;
109 }
Willy Tarreau9c266802016-12-04 19:05:24 +0100110 }
111
Willy Tarreaua0a4b092019-07-17 11:25:46 +0200112 if (!conn_get_src(conn)) {
113 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [listener:%d] ALPN=%s\n",
Willy Tarreau38ba6472020-08-27 08:16:52 +0200114 s->uniq_id, fe->id, (unsigned short)l->rx.fd, (unsigned short)conn->handle.fd,
Willy Tarreaua0a4b092019-07-17 11:25:46 +0200115 l->luid, alpn);
116 }
Willy Tarreau44a7d8e2019-07-17 17:11:40 +0200117 else switch (addr_to_str(conn->src, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200118 case AF_INET:
119 case AF_INET6:
Willy Tarreau9c266802016-12-04 19:05:24 +0100120 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [%s:%d] ALPN=%s\n",
Willy Tarreau38ba6472020-08-27 08:16:52 +0200121 s->uniq_id, fe->id, (unsigned short)l->rx.fd, (unsigned short)conn->handle.fd,
Willy Tarreau44a7d8e2019-07-17 17:11:40 +0200122 pn, get_host_port(conn->src), alpn);
Willy Tarreau631f01c2011-09-05 00:36:48 +0200123 break;
124 case AF_UNIX:
125 /* UNIX socket, only the destination is known */
Willy Tarreau9c266802016-12-04 19:05:24 +0100126 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [unix:%d] ALPN=%s\n",
Willy Tarreau38ba6472020-08-27 08:16:52 +0200127 s->uniq_id, fe->id, (unsigned short)l->rx.fd, (unsigned short)conn->handle.fd,
Willy Tarreau9c266802016-12-04 19:05:24 +0100128 l->luid, alpn);
Willy Tarreau631f01c2011-09-05 00:36:48 +0200129 break;
Emeric Brunab844ea2010-10-22 16:33:18 +0200130 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200131
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +0100132 DISGUISE(write(1, trash.area, trash.data));
Willy Tarreaueb472682010-05-28 18:46:57 +0200133 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200135 if (fe->mode == PR_MODE_HTTP)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100136 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200137
Willy Tarreau0a6bed22016-12-04 18:39:22 +0100138 if (unlikely(fe->nb_req_cap > 0)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100139 if ((s->req_cap = pool_alloc(fe->req_cap_pool)) == NULL)
Willy Tarreau0a6bed22016-12-04 18:39:22 +0100140 goto out_return; /* no memory */
141 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
142 }
143
144 if (unlikely(fe->nb_rsp_cap > 0)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100145 if ((s->res_cap = pool_alloc(fe->rsp_cap_pool)) == NULL)
Willy Tarreau0a6bed22016-12-04 18:39:22 +0100146 goto out_free_reqcap; /* no memory */
147 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
148 }
149
150 if (fe->http_needed) {
151 /* we have to allocate header indexes only if we know
152 * that we may make use of them. This of course includes
153 * (mode == PR_MODE_HTTP).
154 */
155 if (unlikely(!http_alloc_txn(s)))
156 goto out_free_rspcap; /* no memory */
157
158 /* and now initialize the HTTP transaction state */
159 http_init_txn(s);
160 }
161
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200162 /* everything's OK, let's go on */
Willy Tarreaueb472682010-05-28 18:46:57 +0200163 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100164
165 /* Error unrolling */
Willy Tarreau35a09942010-06-01 17:12:40 +0200166 out_free_rspcap:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100167 pool_free(fe->rsp_cap_pool, s->res_cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200168 out_free_reqcap:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100169 pool_free(fe->req_cap_pool, s->req_cap);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100170 out_return:
Willy Tarreaueb472682010-05-28 18:46:57 +0200171 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172}
173
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100174/************************************************************************/
175/* All supported sample and ACL keywords must be declared here. */
176/************************************************************************/
177
Willy Tarreaua5e37562011-12-16 17:06:15 +0100178/* set temp integer to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200179static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200180smp_fetch_fe_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +0200181{
Willy Tarreauf853c462012-04-23 18:53:56 +0200182 smp->flags = SMP_F_VOL_SESS;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200183 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200184 smp->data.u.sint = smp->sess->fe->uuid;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100185 return 1;
186}
187
Marcin Deranekd2471c22016-12-12 14:08:05 +0100188/* set string to the name of the frontend */
189static int
190smp_fetch_fe_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
191{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200192 smp->data.u.str.area = (char *)smp->sess->fe->id;
193 if (!smp->data.u.str.area)
Marcin Deranekd2471c22016-12-12 14:08:05 +0100194 return 0;
195
196 smp->data.type = SMP_T_STR;
197 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200198 smp->data.u.str.data = strlen(smp->data.u.str.area);
Marcin Deranekd2471c22016-12-12 14:08:05 +0100199 return 1;
200}
201
Marcin Deranek9a66dfb2018-04-13 14:37:50 +0200202/* set string to the name of the default backend */
203static int
204smp_fetch_fe_defbe(const struct arg *args, struct sample *smp, const char *kw, void *private)
205{
206 if (!smp->sess->fe->defbe.be)
207 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200208 smp->data.u.str.area = (char *)smp->sess->fe->defbe.be->id;
209 if (!smp->data.u.str.area)
Marcin Deranek9a66dfb2018-04-13 14:37:50 +0200210 return 0;
211
212 smp->data.type = SMP_T_STR;
213 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200214 smp->data.u.str.data = strlen(smp->data.u.str.area);
Marcin Deranek9a66dfb2018-04-13 14:37:50 +0200215 return 1;
216}
217
Nenad Merdanovicad9a7e92016-10-03 04:57:37 +0200218/* set temp integer to the number of HTTP requests per second reaching the frontend.
219 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
220 * an undefined behaviour.
221 */
222static int
223smp_fetch_fe_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
224{
225 smp->flags = SMP_F_VOL_TEST;
226 smp->data.type = SMP_T_SINT;
227 smp->data.u.sint = read_freq_ctr(&args->data.prx->fe_req_per_sec);
228 return 1;
229}
230
Willy Tarreau34db1082012-04-19 17:16:54 +0200231/* set temp integer to the number of connections per second reaching the frontend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +0200232 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
Willy Tarreau34db1082012-04-19 17:16:54 +0200233 * an undefined behaviour.
234 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200235static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200236smp_fetch_fe_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200237{
Willy Tarreau37406352012-04-23 16:16:37 +0200238 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200239 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200240 smp->data.u.sint = read_freq_ctr(&args->data.prx->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100241 return 1;
242}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100243
Willy Tarreau34db1082012-04-19 17:16:54 +0200244/* set temp integer to the number of concurrent connections on the frontend
Willy Tarreau0146c2e2012-04-20 11:37:56 +0200245 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
Willy Tarreau34db1082012-04-19 17:16:54 +0200246 * an undefined behaviour.
247 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200248static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200249smp_fetch_fe_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +0200250{
Willy Tarreau37406352012-04-23 16:16:37 +0200251 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200252 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200253 smp->data.u.sint = args->data.prx->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100254 return 1;
255}
256
Amaury Denoyelleda184d52020-12-10 13:43:55 +0100257static int
258smp_fetch_fe_client_timeout(const struct arg *args, struct sample *smp, const char *km, void *private)
259{
260 smp->flags = SMP_F_VOL_TXN;
261 smp->data.type = SMP_T_SINT;
262 smp->data.u.sint = TICKS_TO_MS(smp->sess->fe->timeout.client);
263 return 1;
264}
265
Willy Tarreau8797c062007-05-07 00:55:35 +0200266
Willy Tarreau61612d42012-04-19 18:42:05 +0200267/* Note: must not be declared <const> as its list will be overwritten.
268 * Please take care of keeping this list alphabetically sorted.
269 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200270static struct sample_fetch_kw_list smp_kws = {ILH, {
Amaury Denoyelleda184d52020-12-10 13:43:55 +0100271 { "fe_client_timeout", smp_fetch_fe_client_timeout, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
272 { "fe_conn", smp_fetch_fe_conn, ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
273 { "fe_defbe", smp_fetch_fe_defbe, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
274 { "fe_id", smp_fetch_fe_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
275 { "fe_name", smp_fetch_fe_name, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
276 { "fe_req_rate", smp_fetch_fe_req_rate, ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
277 { "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 +0100278 { /* END */ },
279}};
280
Willy Tarreau0108d902018-11-25 19:14:37 +0100281INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100282
283/* Note: must not be declared <const> as its list will be overwritten.
284 * Please take care of keeping this list alphabetically sorted.
285 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200286static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100287 { /* END */ },
Willy Tarreau8797c062007-05-07 00:55:35 +0200288}};
289
Willy Tarreau0108d902018-11-25 19:14:37 +0100290INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +0200291
Willy Tarreaubaaee002006-06-26 02:48:02 +0200292/*
293 * Local variables:
294 * c-indent-level: 8
295 * c-basic-offset: 8
296 * End:
297 */