blob: b06ecfa77410440075969cf592c51a5d919202b8 [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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020014#include <stdio.h>
15#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020016#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020017
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21
Willy Tarreau48a7e722010-12-24 15:26:39 +010022#include <netinet/tcp.h>
23
Willy Tarreaudcc048a2020-06-04 19:11:43 +020024#include <haproxy/acl.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020025#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/arg.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020027#include <haproxy/chunk.h>
Christopher Faulet8da67aa2022-03-29 17:53:09 +020028#include <haproxy/connection.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 Tarreau5edca2f2022-05-27 09:25:10 +020037#include <haproxy/sc_strm.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020039#include <haproxy/task.h>
Willy Tarreau9310f482021-10-06 16:18:40 +020040#include <haproxy/ticks.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{
Christopher Fauletc9c8e1c2021-10-22 17:39:06 +020050 const struct sockaddr_storage *src, *dst;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020051 struct session *sess = s->sess;
Willy Tarreaue0232f12015-04-05 18:01:06 +020052 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020053 struct listener *l = sess->listener;
54 struct proxy *fe = sess->fe;
Willy Tarreaufb0afa72015-04-03 14:46:27 +020055
Willy Tarreaue36cbcb2015-04-03 15:40:56 +020056 if ((fe->mode == PR_MODE_TCP || fe->mode == PR_MODE_HTTP)
57 && (!LIST_ISEMPTY(&fe->logsrvs))) {
58 if (likely(!LIST_ISEMPTY(&fe->logformat))) {
Willy Tarreaueb472682010-05-28 18:46:57 +020059 /* we have the client ip */
60 if (s->logs.logwait & LW_CLIP)
Willy Tarreaud79a3b22012-12-28 09:40:16 +010061 if (!(s->logs.logwait &= ~(LW_CLIP|LW_INIT)))
Willy Tarreaueb472682010-05-28 18:46:57 +020062 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +020063 }
Willy Tarreaue0232f12015-04-05 18:01:06 +020064 else if (conn) {
Willy Tarreaud68ff012022-05-27 08:57:21 +020065 src = sc_src(s->scf);
Christopher Fauletc9c8e1c2021-10-22 17:39:06 +020066 if (!src)
67 send_log(fe, LOG_INFO, "Connect from unknown source to listener %d (%s/%s)\n",
68 l->luid, fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
69 else {
70 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
71 int port;
Willy Tarreaubaaee002006-06-26 02:48:02 +020072
Christopher Fauletc9c8e1c2021-10-22 17:39:06 +020073 switch (addr_to_str(src, pn, sizeof(pn))) {
74 case AF_INET:
75 case AF_INET6:
Willy Tarreaud68ff012022-05-27 08:57:21 +020076 dst = sc_dst(s->scf);
Christopher Fauletc9c8e1c2021-10-22 17:39:06 +020077 if (dst) {
78 addr_to_str(dst, sn, sizeof(sn));
79 port = get_host_port(dst);
80 } else {
Willy Tarreaufc458ec2023-04-07 18:11:39 +020081 strlcpy2(sn, "undetermined address", sizeof(sn));
Christopher Fauletc9c8e1c2021-10-22 17:39:06 +020082 port = 0;
83 }
84 send_log(fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
85 pn, get_host_port(src),
86 sn, port,
87 fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
88 break;
89 case AF_UNIX:
90 /* UNIX socket, only the destination is known */
91 send_log(fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
92 l->luid,
93 fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
94 break;
Willy Tarreaua0a4b092019-07-17 11:25:46 +020095 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020096 }
Willy Tarreaueb472682010-05-28 18:46:57 +020097 }
98 }
Willy Tarreaubaaee002006-06-26 02:48:02 +020099
Willy Tarreaue0232f12015-04-05 18:01:06 +0200100 if (unlikely((global.mode & MODE_DEBUG) && conn &&
101 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200102 char pn[INET6_ADDRSTRLEN];
Willy Tarreau9c266802016-12-04 19:05:24 +0100103 char alpn[16] = "<none>";
Willy Tarreau20814ff2017-09-15 11:43:32 +0200104 const char *alpn_str = NULL;
105 int alpn_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106
Willy Tarreau20814ff2017-09-15 11:43:32 +0200107 /* try to report the ALPN value when available (also works for NPN) */
Willy Tarreaufd9417b2022-05-18 16:23:22 +0200108 if (conn == sc_conn(s->scf)) {
Willy Tarreau20814ff2017-09-15 11:43:32 +0200109 if (conn_get_alpn(conn, &alpn_str, &alpn_len) && alpn_str) {
110 int len = MIN(alpn_len, sizeof(alpn) - 1);
111 memcpy(alpn, alpn_str, len);
112 alpn[len] = 0;
113 }
Willy Tarreau9c266802016-12-04 19:05:24 +0100114 }
115
Willy Tarreaud68ff012022-05-27 08:57:21 +0200116 src = sc_src(s->scf);
Christopher Fauletc9c8e1c2021-10-22 17:39:06 +0200117 if (!src) {
Willy Tarreaua0a4b092019-07-17 11:25:46 +0200118 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [listener:%d] ALPN=%s\n",
Willy Tarreau38ba6472020-08-27 08:16:52 +0200119 s->uniq_id, fe->id, (unsigned short)l->rx.fd, (unsigned short)conn->handle.fd,
Willy Tarreaua0a4b092019-07-17 11:25:46 +0200120 l->luid, alpn);
121 }
Christopher Fauletc9c8e1c2021-10-22 17:39:06 +0200122 else switch (addr_to_str(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 Tarreau38ba6472020-08-27 08:16:52 +0200126 s->uniq_id, fe->id, (unsigned short)l->rx.fd, (unsigned short)conn->handle.fd,
Christopher Fauletc9c8e1c2021-10-22 17:39:06 +0200127 pn, get_host_port(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 Tarreau38ba6472020-08-27 08:16:52 +0200132 s->uniq_id, fe->id, (unsigned short)l->rx.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 Tarreau2e8ab6b2020-03-14 11:03:20 +0100137 DISGUISE(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)
Christopher Faulet9a790f62023-03-16 14:40:03 +0100141 s->scf->flags |= SC_FL_RCV_ONCE; /* 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 Tarreauec4cfc32021-03-22 21:06:21 +0100144 if ((s->req_cap = pool_zalloc(fe->req_cap_pool)) == NULL)
Willy Tarreau0a6bed22016-12-04 18:39:22 +0100145 goto out_return; /* no memory */
Willy Tarreau0a6bed22016-12-04 18:39:22 +0100146 }
147
148 if (unlikely(fe->nb_rsp_cap > 0)) {
Willy Tarreauec4cfc32021-03-22 21:06:21 +0100149 if ((s->res_cap = pool_zalloc(fe->rsp_cap_pool)) == NULL)
Willy Tarreau0a6bed22016-12-04 18:39:22 +0100150 goto out_free_reqcap; /* no memory */
Willy Tarreau0a6bed22016-12-04 18:39:22 +0100151 }
152
Christopher Faulet75f619a2021-03-08 19:12:58 +0100153 if ((fe->http_needed || IS_HTX_STRM(s)) && !http_create_txn(s))
154 goto out_free_rspcap;
Willy Tarreau0a6bed22016-12-04 18:39:22 +0100155
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200156 /* everything's OK, let's go on */
Willy Tarreaueb472682010-05-28 18:46:57 +0200157 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100158
159 /* Error unrolling */
Willy Tarreau35a09942010-06-01 17:12:40 +0200160 out_free_rspcap:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100161 pool_free(fe->rsp_cap_pool, s->res_cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200162 out_free_reqcap:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100163 pool_free(fe->req_cap_pool, s->req_cap);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100164 out_return:
Willy Tarreaueb472682010-05-28 18:46:57 +0200165 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166}
167
Amaury Denoyelle8ee9fc72023-10-25 15:32:28 +0200168/* Increment current active connection counter. This ensures that global
169 * maxconn is not reached or exceeded. This must be done for every new frontend
170 * connection allocation.
171 *
172 * Returns the new actconn global value. If maxconn reached or exceeded, 0 is
173 * returned : the connection allocation should be cancelled.
174 */
175int increment_actconn()
176{
177 unsigned int count, next_actconn;
178
179 do {
180 count = actconn;
181 if (unlikely(count >= global.maxconn)) {
182 /* maxconn reached */
183 next_actconn = 0;
184 goto end;
185 }
186
187 /* try to increment actconn */
188 next_actconn = count + 1;
189 } while (!_HA_ATOMIC_CAS(&actconn, (int *)(&count), next_actconn) && __ha_cpu_relax());
190
191 end:
192 return next_actconn;
193}
194
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100195/************************************************************************/
196/* All supported sample and ACL keywords must be declared here. */
197/************************************************************************/
198
Willy Tarreaua5e37562011-12-16 17:06:15 +0100199/* set temp integer to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200200static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200201smp_fetch_fe_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +0200202{
Willy Tarreauf853c462012-04-23 18:53:56 +0200203 smp->flags = SMP_F_VOL_SESS;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200204 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200205 smp->data.u.sint = smp->sess->fe->uuid;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100206 return 1;
207}
208
Marcin Deranekd2471c22016-12-12 14:08:05 +0100209/* set string to the name of the frontend */
210static int
211smp_fetch_fe_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
212{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200213 smp->data.u.str.area = (char *)smp->sess->fe->id;
214 if (!smp->data.u.str.area)
Marcin Deranekd2471c22016-12-12 14:08:05 +0100215 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 Deranekd2471c22016-12-12 14:08:05 +0100220 return 1;
221}
222
Marcin Deranek9a66dfb2018-04-13 14:37:50 +0200223/* set string to the name of the default backend */
224static int
225smp_fetch_fe_defbe(const struct arg *args, struct sample *smp, const char *kw, void *private)
226{
227 if (!smp->sess->fe->defbe.be)
228 return 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200229 smp->data.u.str.area = (char *)smp->sess->fe->defbe.be->id;
230 if (!smp->data.u.str.area)
Marcin Deranek9a66dfb2018-04-13 14:37:50 +0200231 return 0;
232
233 smp->data.type = SMP_T_STR;
234 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200235 smp->data.u.str.data = strlen(smp->data.u.str.area);
Marcin Deranek9a66dfb2018-04-13 14:37:50 +0200236 return 1;
237}
238
Nenad Merdanovicad9a7e92016-10-03 04:57:37 +0200239/* set temp integer to the number of HTTP requests per second reaching the frontend.
240 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
241 * an undefined behaviour.
242 */
243static int
244smp_fetch_fe_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
245{
Christopher Faulet37a9e212021-10-12 18:48:05 +0200246 struct proxy *px = args->data.prx;
247
248 if (px == NULL)
249 return 0;
250 if (px->cap & PR_CAP_DEF)
251 px = smp->px;
252
Nenad Merdanovicad9a7e92016-10-03 04:57:37 +0200253 smp->flags = SMP_F_VOL_TEST;
254 smp->data.type = SMP_T_SINT;
Christopher Faulet37a9e212021-10-12 18:48:05 +0200255 smp->data.u.sint = read_freq_ctr(&px->fe_req_per_sec);
Nenad Merdanovicad9a7e92016-10-03 04:57:37 +0200256 return 1;
257}
258
Willy Tarreau34db1082012-04-19 17:16:54 +0200259/* set temp integer to the number of connections per second reaching the frontend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +0200260 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
Willy Tarreau34db1082012-04-19 17:16:54 +0200261 * an undefined behaviour.
262 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200263static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200264smp_fetch_fe_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200265{
Christopher Faulet37a9e212021-10-12 18:48:05 +0200266 struct proxy *px = args->data.prx;
267
268 if (px == NULL)
269 return 0;
270 if (px->cap & PR_CAP_DEF)
271 px = smp->px;
272
Willy Tarreau37406352012-04-23 16:16:37 +0200273 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200274 smp->data.type = SMP_T_SINT;
Christopher Faulet37a9e212021-10-12 18:48:05 +0200275 smp->data.u.sint = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100276 return 1;
277}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100278
Willy Tarreau34db1082012-04-19 17:16:54 +0200279/* set temp integer to the number of concurrent connections on the frontend
Willy Tarreau0146c2e2012-04-20 11:37:56 +0200280 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
Willy Tarreau34db1082012-04-19 17:16:54 +0200281 * an undefined behaviour.
282 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200283static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +0200284smp_fetch_fe_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +0200285{
Christopher Faulet37a9e212021-10-12 18:48:05 +0200286 struct proxy *px = args->data.prx;
287
288 if (px == NULL)
289 return 0;
290 if (px->cap & PR_CAP_DEF)
291 px = smp->px;
292
Willy Tarreau37406352012-04-23 16:16:37 +0200293 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200294 smp->data.type = SMP_T_SINT;
Christopher Faulet37a9e212021-10-12 18:48:05 +0200295 smp->data.u.sint = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100296 return 1;
297}
298
Amaury Denoyelleda184d52020-12-10 13:43:55 +0100299static int
300smp_fetch_fe_client_timeout(const struct arg *args, struct sample *smp, const char *km, void *private)
301{
302 smp->flags = SMP_F_VOL_TXN;
303 smp->data.type = SMP_T_SINT;
304 smp->data.u.sint = TICKS_TO_MS(smp->sess->fe->timeout.client);
305 return 1;
306}
307
Willy Tarreau8797c062007-05-07 00:55:35 +0200308
Willy Tarreau61612d42012-04-19 18:42:05 +0200309/* Note: must not be declared <const> as its list will be overwritten.
310 * Please take care of keeping this list alphabetically sorted.
311 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200312static struct sample_fetch_kw_list smp_kws = {ILH, {
Amaury Denoyelleda184d52020-12-10 13:43:55 +0100313 { "fe_client_timeout", smp_fetch_fe_client_timeout, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
314 { "fe_conn", smp_fetch_fe_conn, ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
315 { "fe_defbe", smp_fetch_fe_defbe, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
316 { "fe_id", smp_fetch_fe_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
317 { "fe_name", smp_fetch_fe_name, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
318 { "fe_req_rate", smp_fetch_fe_req_rate, ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
319 { "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 +0100320 { /* END */ },
321}};
322
Willy Tarreau0108d902018-11-25 19:14:37 +0100323INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100324
325/* Note: must not be declared <const> as its list will be overwritten.
326 * Please take care of keeping this list alphabetically sorted.
327 */
Willy Tarreaudc13c112013-06-21 23:16:39 +0200328static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreaud6896bc2013-01-07 22:48:29 +0100329 { /* END */ },
Willy Tarreau8797c062007-05-07 00:55:35 +0200330}};
331
Willy Tarreau0108d902018-11-25 19:14:37 +0100332INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +0200333
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334/*
335 * Local variables:
336 * c-indent-level: 8
337 * c-basic-offset: 8
338 * End:
339 */