blob: b222e6682c4200300c9487d38e580c4afb436581 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Backend variables and functions.
3 *
Willy Tarreau1a7eca12013-01-07 22:38:03 +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>
17#include <syslog.h>
Willy Tarreauf19cf372006-11-14 15:40:51 +010018#include <string.h>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +020019#include <ctype.h>
Dmitry Sivachenkocaf58982009-08-24 15:11:06 +040020#include <sys/types.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreaudcc048a2020-06-04 19:11:43 +020022#include <haproxy/acl.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/arg.h>
25#include <haproxy/backend.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020026#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020027#include <haproxy/check.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020028#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020029#include <haproxy/global.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020030#include <haproxy/hash.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020031#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020032#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020033#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020034#include <haproxy/htx.h>
Willy Tarreaufbe8da32020-06-04 14:34:27 +020035#include <haproxy/lb_chash.h>
Willy Tarreaub5fc3bf2020-06-04 14:37:38 +020036#include <haproxy/lb_fas.h>
Willy Tarreau02549412020-06-04 14:41:04 +020037#include <haproxy/lb_fwlc.h>
Willy Tarreau546ba422020-06-04 14:45:03 +020038#include <haproxy/lb_fwrr.h>
Willy Tarreau28671592020-06-04 20:22:59 +020039#include <haproxy/lb_map.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020040#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/namespace.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020042#include <haproxy/obj_type.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020043#include <haproxy/payload.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020044#include <haproxy/proto_tcp.h>
45#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020046#include <haproxy/proxy.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020047#include <haproxy/queue.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020048#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020049#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020050#include <haproxy/session.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020051#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020052#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020053#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020054#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020055#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020056#include <haproxy/time.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020057#include <haproxy/trace.h>
Willy Tarreau732eac42015-07-09 11:40:25 +020058
Christopher Fauleteea8fc72019-11-05 16:18:10 +010059#define TRACE_SOURCE &trace_strm
60
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050061int be_lastsession(const struct proxy *be)
62{
63 if (be->be_counters.last_sess)
64 return now.tv_sec - be->be_counters.last_sess;
65
66 return -1;
67}
68
Bhaskar98634f02013-10-29 23:30:51 -040069/* helper function to invoke the correct hash method */
Dan Dubovikbd57a9f2014-07-08 08:51:03 -070070static unsigned int gen_hash(const struct proxy* px, const char* key, unsigned long len)
Bhaskar98634f02013-10-29 23:30:51 -040071{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -070072 unsigned int hash;
Bhaskar98634f02013-10-29 23:30:51 -040073
74 switch (px->lbprm.algo & BE_LB_HASH_FUNC) {
75 case BE_LB_HFCN_DJB2:
76 hash = hash_djb2(key, len);
77 break;
Willy Tarreaua0f42712013-11-14 14:30:35 +010078 case BE_LB_HFCN_WT6:
79 hash = hash_wt6(key, len);
80 break;
Willy Tarreau324f07f2015-01-20 19:44:50 +010081 case BE_LB_HFCN_CRC32:
82 hash = hash_crc32(key, len);
83 break;
Bhaskar98634f02013-10-29 23:30:51 -040084 case BE_LB_HFCN_SDBM:
85 /* this is the default hash function */
86 default:
87 hash = hash_sdbm(key, len);
88 break;
89 }
90
91 return hash;
92}
93
Willy Tarreaubaaee002006-06-26 02:48:02 +020094/*
95 * This function recounts the number of usable active and backup servers for
96 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +010097 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +010098 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +010099 * this.
Emeric Brun52a91d32017-08-31 14:41:55 +0200100 * This functions is designed to be called before server's weight and state
101 * commit so it uses 'next' weight and states values.
Christopher Faulet5b517552017-06-09 14:17:53 +0200102 *
103 * threads: this is the caller responsibility to lock data. For now, this
104 * function is called from lb modules, so it should be ok. But if you need to
105 * call it from another place, be careful (and update this comment).
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200107void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108{
109 struct server *srv;
110
Willy Tarreau20697042007-11-15 23:26:18 +0100111 px->srv_act = px->srv_bck = 0;
112 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +0100113 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 for (srv = px->srv; srv != NULL; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200115 if (!srv_willbe_usable(srv))
Willy Tarreaub625a082007-11-26 01:15:43 +0100116 continue;
117
Willy Tarreauc93cd162014-05-13 15:54:22 +0200118 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100119 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +0100120 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +0100121 px->lbprm.fbck = srv;
122 px->srv_bck++;
Andrew Rodland13d5ebb2016-10-25 12:49:45 -0400123 srv->cumulative_weight = px->lbprm.tot_wbck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200124 px->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100125 } else {
126 px->srv_act++;
Andrew Rodland13d5ebb2016-10-25 12:49:45 -0400127 srv->cumulative_weight = px->lbprm.tot_wact;
Emeric Brun52a91d32017-08-31 14:41:55 +0200128 px->lbprm.tot_wact += srv->next_eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129 }
130 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100131}
Willy Tarreau20697042007-11-15 23:26:18 +0100132
Willy Tarreaub625a082007-11-26 01:15:43 +0100133/* This function simply updates the backend's tot_weight and tot_used values
134 * after servers weights have been updated. It is designed to be used after
135 * recount_servers() or equivalent.
Christopher Faulet5b517552017-06-09 14:17:53 +0200136 *
137 * threads: this is the caller responsibility to lock data. For now, this
138 * function is called from lb modules, so it should be ok. But if you need to
139 * call it from another place, be careful (and update this comment).
Willy Tarreaub625a082007-11-26 01:15:43 +0100140 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200141void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100142{
Willy Tarreau20697042007-11-15 23:26:18 +0100143 if (px->srv_act) {
144 px->lbprm.tot_weight = px->lbprm.tot_wact;
145 px->lbprm.tot_used = px->srv_act;
146 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100147 else if (px->lbprm.fbck) {
148 /* use only the first backup server */
Emeric Brun52a91d32017-08-31 14:41:55 +0200149 px->lbprm.tot_weight = px->lbprm.fbck->next_eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100150 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100151 }
152 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100153 px->lbprm.tot_weight = px->lbprm.tot_wbck;
154 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100155 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100156}
157
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200158/*
159 * This function tries to find a running server for the proxy <px> following
160 * the source hash method. Depending on the number of active/backup servers,
161 * it will either look for active servers, or for backup servers.
162 * If any server is found, it will be returned. If no valid server is found,
163 * NULL is returned.
164 */
Willy Tarreau59884a62019-01-02 14:48:31 +0100165static struct server *get_server_sh(struct proxy *px, const char *addr, int len, const struct server *avoid)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200166{
167 unsigned int h, l;
168
169 if (px->lbprm.tot_weight == 0)
170 return NULL;
171
172 l = h = 0;
173
174 /* note: we won't hash if there's only one server left */
175 if (px->lbprm.tot_used == 1)
176 goto hash_done;
177
178 while ((l + sizeof (int)) <= len) {
179 h ^= ntohl(*(unsigned int *)(&addr[l]));
180 l += sizeof (int);
181 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500182 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100183 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200184 hash_done:
Willy Tarreau6c30be52019-01-14 17:07:39 +0100185 if ((px->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau59884a62019-01-02 14:48:31 +0100186 return chash_get_server_hash(px, h, avoid);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200187 else
188 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200189}
190
191/*
192 * This function tries to find a running server for the proxy <px> following
193 * the URI hash method. In order to optimize cache hits, the hash computation
194 * ends at the question mark. Depending on the number of active/backup servers,
195 * it will either look for active servers, or for backup servers.
196 * If any server is found, it will be returned. If no valid server is found,
Willy Tarreaua9a72492019-01-14 16:14:15 +0100197 * NULL is returned. The lbprm.arg_opt{1,2,3} values correspond respectively to
Willy Tarreau3d1119d2020-09-23 08:05:47 +0200198 * the "whole" optional argument (boolean, bit0), the "len" argument (numeric)
199 * and the "depth" argument (numeric).
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200200 *
201 * This code was contributed by Guillaume Dallaire, who also selected this hash
202 * algorithm out of a tens because it gave him the best results.
203 *
204 */
Willy Tarreau59884a62019-01-02 14:48:31 +0100205static struct server *get_server_uh(struct proxy *px, char *uri, int uri_len, const struct server *avoid)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200206{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700207 unsigned int hash = 0;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200208 int c;
209 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400210 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200211
212 if (px->lbprm.tot_weight == 0)
213 return NULL;
214
215 /* note: we won't hash if there's only one server left */
216 if (px->lbprm.tot_used == 1)
217 goto hash_done;
218
Willy Tarreaua9a72492019-01-14 16:14:15 +0100219 if (px->lbprm.arg_opt2) // "len"
220 uri_len = MIN(uri_len, px->lbprm.arg_opt2);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200221
Bhaskar98634f02013-10-29 23:30:51 -0400222 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200223 while (uri_len--) {
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200224 c = *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200225 if (c == '/') {
226 slashes++;
Willy Tarreaua9a72492019-01-14 16:14:15 +0100227 if (slashes == px->lbprm.arg_opt3) /* depth+1 */
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200228 break;
229 }
Willy Tarreau3d1119d2020-09-23 08:05:47 +0200230 else if (c == '?' && !(px->lbprm.arg_opt1 & 1)) // "whole"
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200231 break;
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200232 end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200233 }
Bhaskar98634f02013-10-29 23:30:51 -0400234
235 hash = gen_hash(px, start, (end - start));
236
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500237 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100238 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200239 hash_done:
Willy Tarreau6c30be52019-01-14 17:07:39 +0100240 if ((px->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau59884a62019-01-02 14:48:31 +0100241 return chash_get_server_hash(px, hash, avoid);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200242 else
243 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200244}
245
Christopher Faulet5b517552017-06-09 14:17:53 +0200246/*
Willy Tarreau01732802007-11-01 22:48:15 +0100247 * This function tries to find a running server for the proxy <px> following
248 * the URL parameter hash method. It looks for a specific parameter in the
249 * URL and hashes it to compute the server ID. This is useful to optimize
250 * performance by avoiding bounces between servers in contexts where sessions
251 * are shared but cookies are not usable. If the parameter is not found, NULL
252 * is returned. If any server is found, it will be returned. If no valid server
253 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100254 */
Willy Tarreau59884a62019-01-02 14:48:31 +0100255static struct server *get_server_ph(struct proxy *px, const char *uri, int uri_len, const struct server *avoid)
Willy Tarreau01732802007-11-01 22:48:15 +0100256{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700257 unsigned int hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400258 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200259 const char *p;
260 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100261 int plen;
262
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200263 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100264 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100265 return NULL;
266
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200267 if ((p = memchr(uri, '?', uri_len)) == NULL)
268 return NULL;
269
Willy Tarreau01732802007-11-01 22:48:15 +0100270 p++;
271
272 uri_len -= (p - uri);
Willy Tarreau4c03d1c2019-01-14 15:23:54 +0100273 plen = px->lbprm.arg_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200274 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100275
276 while (uri_len > plen) {
277 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200278 if (params[plen] == '=') {
Willy Tarreau4c03d1c2019-01-14 15:23:54 +0100279 if (memcmp(params, px->lbprm.arg_str, plen) == 0) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200280 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100281 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200282 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100283 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200284 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400285 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200286 uri_len -= plen + 1;
287
Bhaskar98634f02013-10-29 23:30:51 -0400288 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100289 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400290 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100291 }
Bhaskar98634f02013-10-29 23:30:51 -0400292 hash = gen_hash(px, start, (end - start));
293
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500294 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100295 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400296
Willy Tarreau6c30be52019-01-14 17:07:39 +0100297 if ((px->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau59884a62019-01-02 14:48:31 +0100298 return chash_get_server_hash(px, hash, avoid);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200299 else
300 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100301 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200302 }
303 /* skip to next parameter */
304 p = memchr(params, '&', uri_len);
305 if (!p)
306 return NULL;
307 p++;
308 uri_len -= (p - params);
309 params = p;
310 }
311 return NULL;
312}
313
314/*
315 * this does the same as the previous server_ph, but check the body contents
316 */
Willy Tarreau59884a62019-01-02 14:48:31 +0100317static struct server *get_server_ph_post(struct stream *s, const struct server *avoid)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200318{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700319 unsigned int hash = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100320 struct channel *req = &s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200321 struct proxy *px = s->be;
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200322 struct htx *htx = htxbuf(&req->buf);
323 struct htx_blk *blk;
Willy Tarreau4c03d1c2019-01-14 15:23:54 +0100324 unsigned int plen = px->lbprm.arg_len;
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100325 unsigned long len;
326 const char *params, *p, *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200327
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100328 if (px->lbprm.tot_weight == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200329 return NULL;
330
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200331 p = params = NULL;
332 len = 0;
333 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
334 enum htx_blk_type type = htx_get_blk_type(blk);
335 struct ist v;
Willy Tarreauf69d4ff2015-05-02 00:05:47 +0200336
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200337 if (type != HTX_BLK_DATA)
338 continue;
339 v = htx_get_blk_value(htx, blk);
340 p = params = v.ptr;
341 len = v.len;
342 break;
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100343 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200344
345 while (len > plen) {
346 /* Look for the parameter name followed by an equal symbol */
347 if (params[plen] == '=') {
Willy Tarreau4c03d1c2019-01-14 15:23:54 +0100348 if (memcmp(params, px->lbprm.arg_str, plen) == 0) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200349 /* OK, we have the parameter here at <params>, and
350 * the value after the equal sign, at <p>
351 * skip the equal symbol
352 */
353 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400354 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200355 len -= plen + 1;
356
Bhaskar98634f02013-10-29 23:30:51 -0400357 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200358 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100359 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400360 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100361 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200362 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
363 break;
364 return NULL; /* oh, no; this is not uri-encoded.
365 * This body does not contain parameters.
366 */
367 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200368 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400369 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200370 /* should we break if vlen exceeds limit? */
371 }
Bhaskar98634f02013-10-29 23:30:51 -0400372 hash = gen_hash(px, start, (end - start));
373
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500374 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100375 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400376
Willy Tarreau6c30be52019-01-14 17:07:39 +0100377 if ((px->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau59884a62019-01-02 14:48:31 +0100378 return chash_get_server_hash(px, hash, avoid);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200379 else
380 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200381 }
382 }
Willy Tarreau01732802007-11-01 22:48:15 +0100383 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200384 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100385 if (!p)
386 return NULL;
387 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200388 len -= (p - params);
389 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100390 }
391 return NULL;
392}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200393
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200394
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395/*
Benoitaffb4812009-03-25 13:02:10 +0100396 * This function tries to find a running server for the proxy <px> following
397 * the Header parameter hash method. It looks for a specific parameter in the
398 * URL and hashes it to compute the server ID. This is useful to optimize
399 * performance by avoiding bounces between servers in contexts where sessions
400 * are shared but cookies are not usable. If the parameter is not found, NULL
401 * is returned. If any server is found, it will be returned. If no valid server
Willy Tarreau9fed8582019-01-14 16:04:54 +0100402 * is found, NULL is returned. When lbprm.arg_opt1 is set, the hash will only
403 * apply to the middle part of a domain name ("use_domain_only" option).
Benoitaffb4812009-03-25 13:02:10 +0100404 */
Willy Tarreau59884a62019-01-02 14:48:31 +0100405static struct server *get_server_hh(struct stream *s, const struct server *avoid)
Benoitaffb4812009-03-25 13:02:10 +0100406{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700407 unsigned int hash = 0;
Benoitaffb4812009-03-25 13:02:10 +0100408 struct proxy *px = s->be;
Willy Tarreau484ff072019-01-14 15:28:53 +0100409 unsigned int plen = px->lbprm.arg_len;
Benoitaffb4812009-03-25 13:02:10 +0100410 unsigned long len;
Benoitaffb4812009-03-25 13:02:10 +0100411 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400412 const char *start, *end;
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200413 struct htx *htx = htxbuf(&s->req.buf);
414 struct http_hdr_ctx ctx = { .blk = NULL };
Benoitaffb4812009-03-25 13:02:10 +0100415
416 /* tot_weight appears to mean srv_count */
417 if (px->lbprm.tot_weight == 0)
418 return NULL;
419
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200420 /* note: we won't hash if there's only one server left */
421 if (px->lbprm.tot_used == 1)
422 goto hash_done;
423
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200424 http_find_header(htx, ist2(px->lbprm.arg_str, plen), &ctx, 0);
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100425
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200426 /* if the header is not found or empty, let's fallback to round robin */
427 if (!ctx.blk || !ctx.value.len)
428 return NULL;
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100429
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200430 /* Found a the param_name in the headers.
431 * we will compute the hash based on this value ctx.val.
432 */
433 len = ctx.value.len;
434 p = ctx.value.ptr;
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100435
Willy Tarreau9fed8582019-01-14 16:04:54 +0100436 if (!px->lbprm.arg_opt1) {
Bhaskar98634f02013-10-29 23:30:51 -0400437 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100438 } else {
439 int dohash = 0;
Cyril Bontéf607d812015-01-04 15:17:36 +0100440 p += len;
Benoitaffb4812009-03-25 13:02:10 +0100441 /* special computation, use only main domain name, not tld/host
442 * going back from the end of string, start hashing at first
443 * dot stop at next.
444 * This is designed to work with the 'Host' header, and requires
445 * a special option to activate this.
446 */
Cyril Bontéf607d812015-01-04 15:17:36 +0100447 end = p;
Benoitaffb4812009-03-25 13:02:10 +0100448 while (len) {
Cyril Bontéf607d812015-01-04 15:17:36 +0100449 if (dohash) {
450 /* Rewind the pointer until the previous char
451 * is a dot, this will allow to set the start
452 * position of the domain. */
453 if (*(p - 1) == '.')
Benoitaffb4812009-03-25 13:02:10 +0100454 break;
Benoitaffb4812009-03-25 13:02:10 +0100455 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100456 else if (*p == '.') {
457 /* The pointer is rewinded to the dot before the
458 * tld, we memorize the end of the domain and
459 * can enter the domain processing. */
460 end = p;
461 dohash = 1;
462 }
Benoitaffb4812009-03-25 13:02:10 +0100463 p--;
Cyril Bontéf607d812015-01-04 15:17:36 +0100464 len--;
Benoitaffb4812009-03-25 13:02:10 +0100465 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100466 start = p;
Bhaskar98634f02013-10-29 23:30:51 -0400467 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100468 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500469 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100470 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200471 hash_done:
Willy Tarreau6c30be52019-01-14 17:07:39 +0100472 if ((px->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau59884a62019-01-02 14:48:31 +0100473 return chash_get_server_hash(px, hash, avoid);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200474 else
475 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100476}
477
Willy Tarreau34db1082012-04-19 17:16:54 +0200478/* RDP Cookie HASH. */
Willy Tarreau59884a62019-01-02 14:48:31 +0100479static struct server *get_server_rch(struct stream *s, const struct server *avoid)
Emeric Brun736aa232009-06-30 17:56:00 +0200480{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700481 unsigned int hash = 0;
Emeric Brun736aa232009-06-30 17:56:00 +0200482 struct proxy *px = s->be;
483 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200484 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200485 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200486 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200487
488 /* tot_weight appears to mean srv_count */
489 if (px->lbprm.tot_weight == 0)
490 return NULL;
491
Willy Tarreau37406352012-04-23 16:16:37 +0200492 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200493
Willy Tarreau6a445eb2018-06-19 07:04:45 +0200494 rewind = co_data(&s->req);
495 c_rew(&s->req, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200496
Willy Tarreau484ff072019-01-14 15:28:53 +0100497 ret = fetch_rdp_cookie_name(s, &smp, px->lbprm.arg_str, px->lbprm.arg_len);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200498 len = smp.data.u.str.data;
Willy Tarreau664092c2011-12-16 19:11:42 +0100499
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200500 c_adv(&s->req, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200501
Willy Tarreau37406352012-04-23 16:16:37 +0200502 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200503 return NULL;
504
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200505 /* note: we won't hash if there's only one server left */
506 if (px->lbprm.tot_used == 1)
507 goto hash_done;
508
Willy Tarreau484ff072019-01-14 15:28:53 +0100509 /* Found the param_name in the headers.
Emeric Brun736aa232009-06-30 17:56:00 +0200510 * we will compute the hash based on this value ctx.val.
511 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200512 hash = gen_hash(px, smp.data.u.str.area, len);
Bhaskar98634f02013-10-29 23:30:51 -0400513
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500514 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100515 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200516 hash_done:
Willy Tarreau6c30be52019-01-14 17:07:39 +0100517 if ((px->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau59884a62019-01-02 14:48:31 +0100518 return chash_get_server_hash(px, hash, avoid);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200519 else
520 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200521}
Christopher Faulet5b517552017-06-09 14:17:53 +0200522
Willy Tarreau760e81d2018-05-03 07:20:40 +0200523/* random value */
Willy Tarreau59884a62019-01-02 14:48:31 +0100524static struct server *get_server_rnd(struct stream *s, const struct server *avoid)
Willy Tarreau760e81d2018-05-03 07:20:40 +0200525{
526 unsigned int hash = 0;
527 struct proxy *px = s->be;
Willy Tarreau21c741a2019-01-14 18:14:27 +0100528 struct server *prev, *curr;
529 int draws = px->lbprm.arg_opt1; // number of draws
Willy Tarreau760e81d2018-05-03 07:20:40 +0200530
531 /* tot_weight appears to mean srv_count */
532 if (px->lbprm.tot_weight == 0)
533 return NULL;
534
Willy Tarreau21c741a2019-01-14 18:14:27 +0100535 curr = NULL;
536 do {
537 prev = curr;
Ubuntu1adaddb2021-03-01 07:57:54 +0000538 hash = statistical_prng();
Willy Tarreau21c741a2019-01-14 18:14:27 +0100539 curr = chash_get_server_hash(px, hash, avoid);
540 if (!curr)
541 break;
542
543 /* compare the new server to the previous best choice and pick
544 * the one with the least currently served requests.
545 */
546 if (prev && prev != curr &&
547 curr->served * prev->cur_eweight > prev->served * curr->cur_eweight)
548 curr = prev;
549 } while (--draws > 0);
550
Willy Tarreaub88ae182020-09-29 16:58:30 +0200551 /* if the selected server is full, pretend we have none so that we reach
552 * the backend's queue instead.
553 */
554 if (curr &&
555 (curr->nbpend || (curr->maxconn && curr->served >= srv_dynamic_maxconn(curr))))
556 curr = NULL;
557
Willy Tarreau21c741a2019-01-14 18:14:27 +0100558 return curr;
Willy Tarreau760e81d2018-05-03 07:20:40 +0200559}
560
Benoitaffb4812009-03-25 13:02:10 +0100561/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200562 * This function applies the load-balancing algorithm to the stream, as
563 * defined by the backend it is assigned to. The stream is then marked as
Willy Tarreau7c669d72008-06-20 15:04:11 +0200564 * 'assigned'.
565 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200566 * This function MAY NOT be called with SF_ASSIGNED already set. If the stream
Willy Tarreau7c669d72008-06-20 15:04:11 +0200567 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100568 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200569 * The function tries to keep the original connection slot if it reconnects to
570 * the same server, otherwise it releases it and tries to offer it.
571 *
Willy Tarreau87b09662015-04-03 00:22:06 +0200572 * It is illegal to call this function with a stream in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573 *
574 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100575 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau87b09662015-04-03 00:22:06 +0200576 * SRV_STATUS_NOSRV if no server is available. Stream is not ASSIGNED
577 * SRV_STATUS_FULL if all servers are saturated. Stream is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200578 * SRV_STATUS_INTERNAL for other unrecoverable errors.
579 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200580 * Upon successful return, the stream flag SF_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100581 * it does not need to be called anymore. This means that target_srv(&s->target)
582 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200583 *
584 */
585
Willy Tarreau87b09662015-04-03 00:22:06 +0200586int assign_server(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200587{
Olivier Houchardba4fff52018-12-01 14:40:40 +0100588 struct connection *conn = NULL;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200589 struct server *conn_slot;
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100590 struct server *srv = NULL, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200591 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100592
Simon Horman8effd3d2011-08-13 08:03:52 +0900593 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200594
Willy Tarreau7c669d72008-06-20 15:04:11 +0200595 err = SRV_STATUS_INTERNAL;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200596 if (unlikely(s->pend_pos || s->flags & SF_ASSIGNED))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200597 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100598
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100599 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200600 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200601
Willy Tarreau7c669d72008-06-20 15:04:11 +0200602 /* We have to release any connection slot before applying any LB algo,
603 * otherwise we may erroneously end up with no available slot.
604 */
605 if (conn_slot)
606 sess_change_server(s, NULL);
607
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100608 /* We will now try to find the good server and store it into <objt_server(s->target)>.
609 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200610 * as well as if no server is available (check error code).
611 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100612
Willy Tarreau827aee92011-03-10 16:55:02 +0100613 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100614 s->target = NULL;
Willy Tarreau664beb82011-03-10 11:38:29 +0100615
Olivier Houchardba4fff52018-12-01 14:40:40 +0100616 if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI &&
Olivier Houchard250031e2019-05-29 15:01:50 +0200617 ((s->sess->flags & SESS_FL_PREFER_LAST) ||
Olivier Houchardba4fff52018-12-01 14:40:40 +0100618 (s->be->options & PR_O_PREF_LAST))) {
Olivier Houchard351411f2018-12-27 17:20:54 +0100619 struct sess_srv_list *srv_list;
620 list_for_each_entry(srv_list, &s->sess->srv_list, srv_list) {
621 struct server *tmpsrv = objt_server(srv_list->target);
Olivier Houchardba4fff52018-12-01 14:40:40 +0100622
623 if (tmpsrv && tmpsrv->proxy == s->be &&
Olivier Houchard250031e2019-05-29 15:01:50 +0200624 ((s->sess->flags & SESS_FL_PREFER_LAST) ||
Olivier Houchardba4fff52018-12-01 14:40:40 +0100625 (!s->be->max_ka_queue ||
626 server_has_room(tmpsrv) || (
627 tmpsrv->nbpend + 1 < s->be->max_ka_queue))) &&
628 srv_currently_usable(tmpsrv)) {
Olivier Houchard351411f2018-12-27 17:20:54 +0100629 list_for_each_entry(conn, &srv_list->conn_list, session_list) {
Willy Tarreau911db9b2020-01-23 16:27:54 +0100630 if (!(conn->flags & CO_FL_WAIT_XPRT)) {
Olivier Houchardba4fff52018-12-01 14:40:40 +0100631 srv = tmpsrv;
632 s->target = &srv->obj_type;
Christopher Faulete91a5262020-07-01 18:56:30 +0200633 if (conn->flags & CO_FL_SESS_IDLE) {
634 conn->flags &= ~CO_FL_SESS_IDLE;
635 s->sess->idle_conns--;
636 }
Olivier Houchardba4fff52018-12-01 14:40:40 +0100637 goto out_ok;
638 }
639 }
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100640 }
641 }
Willy Tarreau9420b122013-12-15 18:58:25 +0100642 }
Christopher Faulet5b517552017-06-09 14:17:53 +0200643
Willy Tarreau82cd5c12020-09-29 17:07:21 +0200644 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200645 /* we must check if we have at least one server available */
646 if (!s->be->lbprm.tot_weight) {
647 err = SRV_STATUS_NOSRV;
648 goto out;
649 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650
Willy Tarreau82cd5c12020-09-29 17:07:21 +0200651 /* if there's some queue on the backend, with certain algos we
652 * know it's because all servers are full.
653 */
Willy Tarreaua7ed7d42021-06-09 15:56:16 +0200654 if (s->be->nbpend && s->be->nbpend != s->be->beconn &&
Willy Tarreau8ae8c482020-10-22 17:19:07 +0200655 (((s->be->lbprm.algo & (BE_LB_KIND|BE_LB_NEED|BE_LB_PARM)) == BE_LB_ALGO_FAS)|| // first
Willy Tarreau82cd5c12020-09-29 17:07:21 +0200656 ((s->be->lbprm.algo & (BE_LB_KIND|BE_LB_NEED|BE_LB_PARM)) == BE_LB_ALGO_RR) || // roundrobin
657 ((s->be->lbprm.algo & (BE_LB_KIND|BE_LB_NEED|BE_LB_PARM)) == BE_LB_ALGO_SRR))) { // static-rr
658 err = SRV_STATUS_FULL;
659 goto out;
660 }
661
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200662 /* First check whether we need to fetch some data or simply call
663 * the LB lookup function. Only the hashing functions will need
664 * some input data in fact, and will support multiple algorithms.
665 */
666 switch (s->be->lbprm.algo & BE_LB_LKUP) {
667 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100668 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200669 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200670
Willy Tarreauf09c6602012-02-13 17:12:08 +0100671 case BE_LB_LKUP_FSTREE:
672 srv = fas_get_next_server(s->be, prev_srv);
673 break;
674
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200675 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100676 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200677 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200678
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200679 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200680 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200681 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau760e81d2018-05-03 07:20:40 +0200682 if ((s->be->lbprm.algo & BE_LB_PARM) == BE_LB_RR_RANDOM)
Willy Tarreau59884a62019-01-02 14:48:31 +0100683 srv = get_server_rnd(s, prev_srv);
Willy Tarreau6c30be52019-01-14 17:07:39 +0100684 else if ((s->be->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100685 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200686 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100687 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200688 break;
689 }
690 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200691 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200692 err = SRV_STATUS_INTERNAL;
693 goto out;
694 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200695
696 switch (s->be->lbprm.algo & BE_LB_PARM) {
697 case BE_LB_HASH_SRC:
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200698 conn = objt_conn(strm_orig(s));
Willy Tarreauc0e16f22019-07-17 18:16:30 +0200699 if (conn && conn_get_src(conn) && conn->src->ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200700 srv = get_server_sh(s->be,
Willy Tarreauc0e16f22019-07-17 18:16:30 +0200701 (void *)&((struct sockaddr_in *)conn->src)->sin_addr,
Willy Tarreau59884a62019-01-02 14:48:31 +0100702 4, prev_srv);
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200703 }
Willy Tarreauc0e16f22019-07-17 18:16:30 +0200704 else if (conn && conn_get_src(conn) && conn->src->ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200705 srv = get_server_sh(s->be,
Willy Tarreauc0e16f22019-07-17 18:16:30 +0200706 (void *)&((struct sockaddr_in6 *)conn->src)->sin6_addr,
Willy Tarreau59884a62019-01-02 14:48:31 +0100707 16, prev_srv);
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200708 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200709 else {
710 /* unknown IP family */
711 err = SRV_STATUS_INTERNAL;
712 goto out;
713 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200714 break;
715
716 case BE_LB_HASH_URI:
717 /* URI hashing */
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200718 if (IS_HTX_STRM(s) && s->txn->req.msg_state >= HTTP_MSG_BODY) {
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100719 struct ist uri;
720
Christopher Faulet297fbb42019-05-13 14:41:27 +0200721 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau57a37412020-09-23 08:56:29 +0200722 if (s->be->lbprm.arg_opt1 & 2) {
723 uri = http_get_path(uri);
Tim Duesterhus7b5777d2021-03-02 18:57:28 +0100724 if (!isttest(uri))
Willy Tarreau57a37412020-09-23 08:56:29 +0200725 uri = ist("");
726 }
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100727 srv = get_server_uh(s->be, uri.ptr, uri.len, prev_srv);
728 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200729 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200730
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200731 case BE_LB_HASH_PRM:
732 /* URL Parameter hashing */
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200733 if (IS_HTX_STRM(s) && s->txn->req.msg_state >= HTTP_MSG_BODY) {
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100734 struct ist uri;
735
Christopher Faulet297fbb42019-05-13 14:41:27 +0200736 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100737 srv = get_server_ph(s->be, uri.ptr, uri.len, prev_srv);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100738
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200739 if (!srv && s->txn->meth == HTTP_METH_POST)
740 srv = get_server_ph_post(s, prev_srv);
741 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200742 break;
Benoitaffb4812009-03-25 13:02:10 +0100743
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200744 case BE_LB_HASH_HDR:
745 /* Header Parameter hashing */
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200746 if (IS_HTX_STRM(s) && s->txn->req.msg_state >= HTTP_MSG_BODY)
747 srv = get_server_hh(s, prev_srv);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200748 break;
749
750 case BE_LB_HASH_RDP:
751 /* RDP Cookie hashing */
Willy Tarreau59884a62019-01-02 14:48:31 +0100752 srv = get_server_rch(s, prev_srv);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200753 break;
754
755 default:
756 /* unknown balancing algorithm */
757 err = SRV_STATUS_INTERNAL;
758 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100759 }
Emeric Brun736aa232009-06-30 17:56:00 +0200760
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200761 /* If the hashing parameter was not found, let's fall
762 * back to round robin on the map.
763 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100764 if (!srv) {
Willy Tarreau6c30be52019-01-14 17:07:39 +0100765 if ((s->be->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100766 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200767 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100768 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200769 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200770
771 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200772 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200773
Willy Tarreau7c669d72008-06-20 15:04:11 +0200774 default:
775 /* unknown balancing algorithm */
776 err = SRV_STATUS_INTERNAL;
777 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200778 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200779
Willy Tarreau827aee92011-03-10 16:55:02 +0100780 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200781 err = SRV_STATUS_FULL;
782 goto out;
783 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100784 else if (srv != prev_srv) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200785 _HA_ATOMIC_INC(&s->be->be_counters.cum_lbconn);
786 _HA_ATOMIC_INC(&srv->counters.cum_lbconn);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100787 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100788 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200789 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200790 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100791 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792 }
Olivier Houchard2442f682018-12-01 17:03:38 +0100793 else if ((s->be->options & PR_O_HTTP_PROXY)) {
794 conn = cs_conn(objt_cs(s->si[1].end));
795
Willy Tarreauc0e16f22019-07-17 18:16:30 +0200796 if (conn && conn->dst && is_addr(conn->dst)) {
Olivier Houchard2442f682018-12-01 17:03:38 +0100797 /* in proxy mode, we need a valid destination address */
798 s->target = &s->be->obj_type;
799 } else {
800 err = SRV_STATUS_NOSRV;
801 goto out;
802 }
Willy Tarreau664beb82011-03-10 11:38:29 +0100803 }
804 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200805 err = SRV_STATUS_NOSRV;
806 goto out;
807 }
808
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100809out_ok:
Willy Tarreaue7dff022015-04-03 01:14:29 +0200810 s->flags |= SF_ASSIGNED;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200811 err = SRV_STATUS_OK;
812 out:
813
814 /* Either we take back our connection slot, or we offer it to someone
815 * else if we don't need it anymore.
816 */
817 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100818 if (conn_slot == srv) {
819 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200820 } else {
821 if (may_dequeue_tasks(conn_slot, s->be))
Amaury Denoyellecaaafd02021-06-18 11:11:36 +0200822 process_srv_queue(conn_slot, 0);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200823 }
824 }
825
826 out_err:
827 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200828}
829
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100830/* Allocate an address for the destination endpoint
Willy Tarreaubaaee002006-06-26 02:48:02 +0200831 * The address is taken from the currently assigned server, or from the
832 * dispatch or transparent address.
833 *
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100834 * Returns SRV_STATUS_OK on success.
Amaury Denoyellef7bdf002021-01-21 09:40:19 +0100835 * On error, no address is allocated and SRV_STATUS_INTERNAL is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200836 */
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100837static int alloc_dst_address(struct sockaddr_storage **ss,
838 struct server *srv, struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200839{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200840 struct connection *cli_conn = objt_conn(strm_orig(s));
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200841
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100842 *ss = NULL;
Willy Tarreauc0e16f22019-07-17 18:16:30 +0200843
Willy Tarreaue7dff022015-04-03 01:14:29 +0200844 if ((s->flags & SF_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200845 /* A server is necessarily known for this stream */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200846 if (!(s->flags & SF_ASSIGNED))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847 return SRV_STATUS_INTERNAL;
848
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100849 if (!sockaddr_alloc(ss, NULL, 0))
850 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200851
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100852 **ss = srv->addr;
853 set_host_port(*ss, srv->svc_port);
854
855 if (!is_addr(*ss) && cli_conn) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200856 /* if the server has no address, we use the same address
857 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200858 * locally on multiple addresses at once. Nothing is done
859 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200860 */
Willy Tarreau3cc01d82019-07-17 11:27:38 +0200861 if (!conn_get_dst(cli_conn)) {
862 /* do nothing if we can't retrieve the address */
Willy Tarreauc0e16f22019-07-17 18:16:30 +0200863 } else if (cli_conn->dst->ss_family == AF_INET) {
Christopher Fauletae305612021-03-01 11:33:59 +0100864 ((struct sockaddr_in *)*ss)->sin_family = AF_INET;
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100865 ((struct sockaddr_in *)*ss)->sin_addr =
866 ((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Willy Tarreauc0e16f22019-07-17 18:16:30 +0200867 } else if (cli_conn->dst->ss_family == AF_INET6) {
Christopher Fauletae305612021-03-01 11:33:59 +0100868 ((struct sockaddr_in6 *)*ss)->sin6_family = AF_INET6;
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100869 ((struct sockaddr_in6 *)*ss)->sin6_addr =
870 ((struct sockaddr_in6 *)cli_conn->dst)->sin6_addr;
Emeric Brunec810d12010-10-22 16:36:33 +0200871 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200872 }
873
Willy Tarreaubaaee002006-06-26 02:48:02 +0200874 /* if this server remaps proxied ports, we'll use
875 * the port the client connected to with an offset. */
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100876 if ((srv->flags & SRV_F_MAPPORTS) && cli_conn) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100877 int base_port;
878
Willy Tarreau3cc01d82019-07-17 11:27:38 +0200879 if (conn_get_dst(cli_conn)) {
880 /* First, retrieve the port from the incoming connection */
Willy Tarreauc0e16f22019-07-17 18:16:30 +0200881 base_port = get_host_port(cli_conn->dst);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100882
Willy Tarreau3cc01d82019-07-17 11:27:38 +0200883 /* Second, assign the outgoing connection's port */
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100884 base_port += get_host_port(*ss);
885 set_host_port(*ss, base_port);
Willy Tarreau3cc01d82019-07-17 11:27:38 +0200886 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200887 }
888 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200889 else if (s->be->options & PR_O_DISPATCH) {
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100890 if (!sockaddr_alloc(ss, NULL, 0))
891 return SRV_STATUS_INTERNAL;
892
Willy Tarreaubaaee002006-06-26 02:48:02 +0200893 /* connect to the defined dispatch addr */
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100894 **ss = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200895 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200896 else if ((s->be->options & PR_O_TRANSP) && cli_conn) {
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100897 if (!sockaddr_alloc(ss, NULL, 0))
898 return SRV_STATUS_INTERNAL;
899
Willy Tarreaubaaee002006-06-26 02:48:02 +0200900 /* in transparent mode, use the original dest addr if no dispatch specified */
Willy Tarreau3cc01d82019-07-17 11:27:38 +0200901 if (conn_get_dst(cli_conn) &&
Willy Tarreauc0e16f22019-07-17 18:16:30 +0200902 (cli_conn->dst->ss_family == AF_INET || cli_conn->dst->ss_family == AF_INET6))
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100903 **ss = *cli_conn->dst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200904 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100905 else if (s->be->options & PR_O_HTTP_PROXY) {
906 /* If HTTP PROXY option is set, then server is already assigned
907 * during incoming client request parsing. */
908 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100909 else {
910 /* no server and no LB algorithm ! */
911 return SRV_STATUS_INTERNAL;
912 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200913
Willy Tarreaubaaee002006-06-26 02:48:02 +0200914 return SRV_STATUS_OK;
915}
916
Willy Tarreau87b09662015-04-03 00:22:06 +0200917/* This function assigns a server to stream <s> if required, and can add the
Willy Tarreaubaaee002006-06-26 02:48:02 +0200918 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau87b09662015-04-03 00:22:06 +0200919 * If ->srv_conn is set, the stream is first released from the server.
Willy Tarreaue7dff022015-04-03 01:14:29 +0200920 * It may also be called with SF_DIRECT and/or SF_ASSIGNED though. It will
Willy Tarreau7c669d72008-06-20 15:04:11 +0200921 * be called before any connection and after any retry or redispatch occurs.
922 *
Willy Tarreau87b09662015-04-03 00:22:06 +0200923 * It is not allowed to call this function with a stream in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924 *
925 * Returns :
926 *
927 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100928 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929 * SRV_STATUS_QUEUED if the connection has been queued.
930 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100931 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200932 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200933 * SRV_STATUS_INTERNAL for other unrecoverable errors.
934 *
935 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200936int assign_server_and_queue(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937{
938 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100939 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200940 int err;
941
942 if (s->pend_pos)
943 return SRV_STATUS_INTERNAL;
944
Willy Tarreau7c669d72008-06-20 15:04:11 +0200945 err = SRV_STATUS_OK;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200946 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100947 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100948
Willy Tarreau7c669d72008-06-20 15:04:11 +0200949 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100950 if (prev_srv) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200951 /* This stream was previously assigned to a server. We have to
952 * update the stream's and the server's stats :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200953 * - if the server changed :
954 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
Willy Tarreaue7dff022015-04-03 01:14:29 +0200955 * - set SF_REDISP if it was successfully redispatched
Willy Tarreau7c669d72008-06-20 15:04:11 +0200956 * - increment srv->redispatches and be->redispatches
957 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100958 */
959
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100960 if (prev_srv != objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200961 if (s->txn && (s->txn->flags & TX_CK_MASK) == TX_CK_VALID) {
962 s->txn->flags &= ~TX_CK_MASK;
963 s->txn->flags |= TX_CK_DOWN;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200964 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200965 s->flags |= SF_REDISP;
Willy Tarreau4781b152021-04-06 13:53:36 +0200966 _HA_ATOMIC_INC(&prev_srv->counters.redispatches);
967 _HA_ATOMIC_INC(&s->be->be_counters.redispatches);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200968 } else {
Willy Tarreau4781b152021-04-06 13:53:36 +0200969 _HA_ATOMIC_INC(&prev_srv->counters.retries);
970 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100971 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100972 }
973 }
974
Willy Tarreaubaaee002006-06-26 02:48:02 +0200975 switch (err) {
976 case SRV_STATUS_OK:
Willy Tarreaue7dff022015-04-03 01:14:29 +0200977 /* we have SF_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100978 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100979 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200980 return SRV_STATUS_OK; /* dispatch or proxy mode */
981
982 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100983 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200984 return SRV_STATUS_OK;
985
Willy Tarreau87b09662015-04-03 00:22:06 +0200986 /* OK, this stream already has an assigned server, but no
Willy Tarreau7c669d72008-06-20 15:04:11 +0200987 * connection slot yet. Either it is a redispatch, or it was
988 * assigned from persistence information (direct mode).
989 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200990 if ((s->flags & SF_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200991 /* server scheduled for redirection, and already assigned. We
992 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100993 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100994 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100995 return SRV_STATUS_OK;
996 }
997
Willy Tarreau87b09662015-04-03 00:22:06 +0200998 /* We might have to queue this stream if the assigned server is full.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200999 * We know we have to queue it into the server's queue, so if a maxqueue
1000 * is set on the server, we must also check that the server's queue is
1001 * not full, in which case we have to return FULL.
1002 */
Willy Tarreau827aee92011-03-10 16:55:02 +01001003 if (srv->maxconn &&
1004 (srv->nbpend || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +02001005
Willy Tarreau827aee92011-03-10 16:55:02 +01001006 if (srv->maxqueue > 0 && srv->nbpend >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +02001007 return SRV_STATUS_FULL;
1008
Willy Tarreaubaaee002006-06-26 02:48:02 +02001009 p = pendconn_add(s);
1010 if (p)
1011 return SRV_STATUS_QUEUED;
1012 else
Willy Tarreau7c669d72008-06-20 15:04:11 +02001013 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001014 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001015
1016 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +01001017 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001018 return SRV_STATUS_OK;
1019
1020 case SRV_STATUS_FULL:
Willy Tarreau87b09662015-04-03 00:22:06 +02001021 /* queue this stream into the proxy's queue */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001022 p = pendconn_add(s);
1023 if (p)
1024 return SRV_STATUS_QUEUED;
1025 else
Willy Tarreau7c669d72008-06-20 15:04:11 +02001026 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001027
1028 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +02001029 return err;
1030
Willy Tarreaubaaee002006-06-26 02:48:02 +02001031 case SRV_STATUS_INTERNAL:
1032 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001033
Willy Tarreaubaaee002006-06-26 02:48:02 +02001034 default:
1035 return SRV_STATUS_INTERNAL;
1036 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +01001037}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001038
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001039/* Allocate an address for source binding on the specified server or backend.
1040 * The allocation is only performed if the connection is intended to be used
1041 * with transparent mode.
1042 *
1043 * Returns SRV_STATUS_OK if no transparent mode or the address was successfully
1044 * allocated. Otherwise returns SRV_STATUS_INTERNAL.
Willy Tarreaub1d67742010-03-29 19:36:59 +02001045 */
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001046static int alloc_bind_address(struct sockaddr_storage **ss,
1047 struct server *srv, struct stream *s)
Willy Tarreaub1d67742010-03-29 19:36:59 +02001048{
Willy Tarreau29fbe512015-08-20 19:35:14 +02001049#if defined(CONFIG_HAP_TRANSPARENT)
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001050 struct conn_src *src = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001051 struct connection *cli_conn;
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001052 struct sockaddr_in *sin;
1053 char *vptr;
1054 size_t vlen;
1055#endif
Olivier Houchard09a0f032019-01-17 15:59:13 +01001056
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001057 *ss = NULL;
Willy Tarreau827aee92011-03-10 16:55:02 +01001058
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001059#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001060 if (srv && srv->conn_src.opts & CO_SRC_BIND)
1061 src = &srv->conn_src;
1062 else if (s->be->conn_src.opts & CO_SRC_BIND)
1063 src = &s->be->conn_src;
Willy Tarreau294c4732011-12-16 21:35:50 +01001064
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001065 /* no transparent mode, no need to allocate an address, returns OK */
1066 if (!src)
1067 return SRV_STATUS_OK;
Willy Tarreauc0e16f22019-07-17 18:16:30 +02001068
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001069 switch (src->opts & CO_SRC_TPROXY_MASK) {
1070 case CO_SRC_TPROXY_ADDR:
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001071 if (!sockaddr_alloc(ss, NULL, 0))
1072 return SRV_STATUS_INTERNAL;
1073
1074 **ss = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001075 break;
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001076
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001077 case CO_SRC_TPROXY_CLI:
1078 case CO_SRC_TPROXY_CIP:
1079 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001080 cli_conn = objt_conn(strm_orig(s));
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001081 if (!cli_conn || !conn_get_src(cli_conn))
1082 return SRV_STATUS_INTERNAL;
1083
1084 if (!sockaddr_alloc(ss, NULL, 0))
1085 return SRV_STATUS_INTERNAL;
1086
1087 **ss = *cli_conn->src;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001088 break;
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001089
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001090 case CO_SRC_TPROXY_DYN:
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001091 if (!src->bind_hdr_occ || !IS_HTX_STRM(s))
1092 return SRV_STATUS_INTERNAL;
Willy Tarreau294c4732011-12-16 21:35:50 +01001093
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001094 if (!sockaddr_alloc(ss, NULL, 0))
1095 return SRV_STATUS_INTERNAL;
1096
1097 /* bind to the IP in a header */
1098 sin = (struct sockaddr_in *)*ss;
1099 sin->sin_family = AF_INET;
1100 sin->sin_port = 0;
1101 sin->sin_addr.s_addr = 0;
1102 if (!http_get_htx_hdr(htxbuf(&s->req.buf),
1103 ist2(src->bind_hdr_name, src->bind_hdr_len),
1104 src->bind_hdr_occ, NULL, &vptr, &vlen)) {
1105 sockaddr_free(ss);
1106 return SRV_STATUS_INTERNAL;
Willy Tarreaub1d67742010-03-29 19:36:59 +02001107 }
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001108
1109 sin->sin_addr.s_addr = htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001110 break;
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001111
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001112 default:
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001113 ;
Willy Tarreaub1d67742010-03-29 19:36:59 +02001114 }
1115#endif
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001116
1117 return SRV_STATUS_OK;
Willy Tarreaub1d67742010-03-29 19:36:59 +02001118}
1119
Olivier Houchard566df302020-03-06 18:18:56 +01001120/* Attempt to get a backend connection from the specified mt_list array
Willy Tarreau0d587112020-07-01 15:04:38 +02001121 * (safe or idle connections). The <is_safe> argument means what type of
1122 * connection the caller wants.
Olivier Houchard566df302020-03-06 18:18:56 +01001123 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001124static struct connection *conn_backend_get(struct stream *s, struct server *srv, int is_safe, int64_t hash)
Olivier Houchard566df302020-03-06 18:18:56 +01001125{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001126 struct connection *conn = NULL;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001127 int i; // thread number
Olivier Houchard566df302020-03-06 18:18:56 +01001128 int found = 0;
Willy Tarreau364f25a2020-07-01 15:55:30 +02001129 int stop;
Olivier Houchard566df302020-03-06 18:18:56 +01001130
1131 /* We need to lock even if this is our own list, because another
1132 * thread may be trying to migrate that connection, and we don't want
1133 * to end up with two threads using the same connection.
1134 */
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001135 i = tid;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001136 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001137 conn = srv_lookup_conn(is_safe ? &srv->per_thr[tid].safe_conns : &srv->per_thr[tid].idle_conns, hash);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001138 if (conn)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001139 conn_delete_from_tree(&conn->hash_node->node);
Willy Tarreau0d587112020-07-01 15:04:38 +02001140
1141 /* If we failed to pick a connection from the idle list, let's try again with
1142 * the safe list.
1143 */
1144 if (!conn && !is_safe && srv->curr_safe_nb > 0) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001145 conn = srv_lookup_conn(&srv->per_thr[tid].safe_conns, hash);
Willy Tarreau0d587112020-07-01 15:04:38 +02001146 if (conn) {
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001147 conn_delete_from_tree(&conn->hash_node->node);
Willy Tarreau0d587112020-07-01 15:04:38 +02001148 is_safe = 1;
Willy Tarreau0d587112020-07-01 15:04:38 +02001149 }
1150 }
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001151 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard566df302020-03-06 18:18:56 +01001152
1153 /* If we found a connection in our own list, and we don't have to
1154 * steal one from another thread, then we're done.
1155 */
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001156 if (conn)
1157 goto done;
1158
Willy Tarreau76cc6992020-07-01 18:49:24 +02001159 /* pool sharing globally disabled ? */
1160 if (!(global.tune.options & GTUNE_IDLE_POOL_SHARED))
1161 goto done;
1162
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001163 /* Are we allowed to pick from another thread ? We'll still try
1164 * it if we're running low on FDs as we don't want to create
1165 * extra conns in this case, otherwise we can give up if we have
1166 * too few idle conns.
1167 */
1168 if (srv->curr_idle_conns < srv->low_idle_conns &&
1169 ha_used_fds < global.tune.pool_low_count)
1170 goto done;
Olivier Houchard566df302020-03-06 18:18:56 +01001171
Willy Tarreau364f25a2020-07-01 15:55:30 +02001172 /* Lookup all other threads for an idle connection, starting from last
1173 * unvisited thread.
1174 */
1175 stop = srv->next_takeover;
1176 if (stop >= global.nbthread)
1177 stop = 0;
1178
Amaury Denoyelle5f1ded52020-10-14 18:17:03 +02001179 i = stop;
1180 do {
Willy Tarreau364f25a2020-07-01 15:55:30 +02001181 if (!srv->curr_idle_thr[i] || i == tid)
Willy Tarreau151c2532020-07-01 08:24:44 +02001182 continue;
1183
Ubuntub1adf032021-03-01 06:22:17 +00001184 if (HA_SPIN_TRYLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock) != 0)
1185 continue;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001186 conn = srv_lookup_conn(is_safe ? &srv->per_thr[i].safe_conns : &srv->per_thr[i].idle_conns, hash);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001187 while (conn) {
Olivier Houchard1662cdb2020-07-03 14:04:37 +02001188 if (conn->mux->takeover && conn->mux->takeover(conn, i) == 0) {
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001189 conn_delete_from_tree(&conn->hash_node->node);
Willy Tarreau4781b152021-04-06 13:53:36 +02001190 _HA_ATOMIC_INC(&activity[tid].fd_takeover);
Olivier Houchard566df302020-03-06 18:18:56 +01001191 found = 1;
1192 break;
1193 }
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001194
1195 conn = srv_lookup_conn_next(conn);
Olivier Houchard566df302020-03-06 18:18:56 +01001196 }
Willy Tarreau0d587112020-07-01 15:04:38 +02001197
1198 if (!found && !is_safe && srv->curr_safe_nb > 0) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001199 conn = srv_lookup_conn(&srv->per_thr[i].safe_conns, hash);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001200 while (conn) {
Olivier Houchard1662cdb2020-07-03 14:04:37 +02001201 if (conn->mux->takeover && conn->mux->takeover(conn, i) == 0) {
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001202 conn_delete_from_tree(&conn->hash_node->node);
Willy Tarreau4781b152021-04-06 13:53:36 +02001203 _HA_ATOMIC_INC(&activity[tid].fd_takeover);
Willy Tarreau0d587112020-07-01 15:04:38 +02001204 found = 1;
1205 is_safe = 1;
Willy Tarreau0d587112020-07-01 15:04:38 +02001206 break;
1207 }
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001208
1209 conn = srv_lookup_conn_next(conn);
Willy Tarreau0d587112020-07-01 15:04:38 +02001210 }
1211 }
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001212 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Amaury Denoyelle5f1ded52020-10-14 18:17:03 +02001213 } while (!found && (i = (i + 1 == global.nbthread) ? 0 : i + 1) != stop);
Olivier Houchard566df302020-03-06 18:18:56 +01001214
1215 if (!found)
1216 conn = NULL;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001217 done:
1218 if (conn) {
Willy Tarreau364f25a2020-07-01 15:55:30 +02001219 _HA_ATOMIC_STORE(&srv->next_takeover, (i + 1 == global.nbthread) ? 0 : i + 1);
Amaury Denoyelle9c13b622020-10-14 18:17:04 +02001220
1221 srv_use_conn(srv, conn);
1222
Willy Tarreau4781b152021-04-06 13:53:36 +02001223 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
1224 _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb);
1225 _HA_ATOMIC_DEC(&srv->curr_idle_thr[i]);
Amaury Denoyelle9c13b622020-10-14 18:17:04 +02001226 conn->flags &= ~CO_FL_LIST_MASK;
1227 __ha_barrier_atomic_store();
1228
Amaury Denoyelle0d21dea2020-10-14 18:17:09 +02001229 if ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_SAFE &&
1230 conn->mux->flags & MX_FL_HOL_RISK) {
1231 /* attach the connection to the session private list
1232 */
1233 conn->owner = s->sess;
Willy Tarreau38b4d2e2020-11-20 17:08:15 +01001234 session_add_conn(s->sess, conn, conn->target);
Amaury Denoyelle0d21dea2020-10-14 18:17:09 +02001235 }
1236 else {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001237 ebmb_insert(&srv->per_thr[tid].avail_conns,
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001238 &conn->hash_node->node,
1239 sizeof(conn->hash_node->hash));
Amaury Denoyelle0d21dea2020-10-14 18:17:09 +02001240 }
Olivier Houchard566df302020-03-06 18:18:56 +01001241 }
1242 return conn;
1243}
1244
Willy Tarreaubaaee002006-06-26 02:48:02 +02001245/*
Willy Tarreau87b09662015-04-03 00:22:06 +02001246 * This function initiates a connection to the server assigned to this stream
Willy Tarreau350f4872014-11-28 14:42:25 +01001247 * (s->target, s->si[1].addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +01001248 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001249 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001250 * - SF_ERR_NONE if everything's OK
1251 * - SF_ERR_SRVTO if there are no more servers
1252 * - SF_ERR_SRVCL if the connection was refused by the server
1253 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1254 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1255 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01001256 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001257 * The server-facing stream interface is expected to hold a pre-allocated connection
Willy Tarreau350f4872014-11-28 14:42:25 +01001258 * in s->si[1].conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001259 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001260int connect_server(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001261{
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02001262 struct connection *cli_conn = objt_conn(strm_orig(s));
Olivier Houchard00cf70f2018-11-30 17:24:55 +01001263 struct connection *srv_conn = NULL;
Olivier Houchard2442f682018-12-01 17:03:38 +01001264 struct conn_stream *srv_cs = NULL;
Willy Tarreau827aee92011-03-10 16:55:02 +01001265 struct server *srv;
Amaury Denoyelle7f68d812021-01-26 14:35:26 +01001266 const int reuse_mode = s->be->options & PR_O_REUSE_MASK;
Willy Tarreau34601a82013-12-15 16:33:46 +01001267 int reuse = 0;
Olivier Houchard5cd62172019-01-04 15:52:26 +01001268 int init_mux = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001269 int err;
Amaury Denoyelleedadf192021-02-12 13:49:42 +01001270#ifdef USE_OPENSSL
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01001271 struct sample *sni_smp = NULL;
Amaury Denoyelleedadf192021-02-12 13:49:42 +01001272#endif
Amaury Denoyelled10a2002021-02-11 19:45:19 +01001273 struct sockaddr_storage *bind_addr;
Amaury Denoyelle1921d202021-01-14 10:15:29 +01001274 int proxy_line_ret;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001275 int64_t hash = 0;
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01001276 struct conn_hash_params hash_params;
1277
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001278 /* in standard configuration, srv will be valid
1279 * it can be NULL for dispatch mode or transparent backend */
1280 srv = objt_server(s->target);
1281
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01001282 /* first, set unique connection parameters and then calculate hash */
1283 memset(&hash_params, 0, sizeof(hash_params));
Amaury Denoyelle1a58aca2021-01-22 16:47:46 +01001284
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001285 /* 1. target */
1286 hash_params.target = s->target;
Amaury Denoyelle1a58aca2021-01-22 16:47:46 +01001287
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01001288#ifdef USE_OPENSSL
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001289 /* 2. sni */
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01001290 if (srv && srv->ssl_ctx.sni) {
1291 sni_smp = sample_fetch_as_type(s->be, s->sess, s,
1292 SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
1293 srv->ssl_ctx.sni, SMP_T_STR);
Amaury Denoyelle4c098002021-02-17 15:59:02 +01001294
1295 /* only test if the sample is not null as smp_make_safe (called
1296 * before ssl_sock_set_servername) can only fails if this is
1297 * not the case
1298 */
1299 if (sni_smp) {
Amaury Denoyelle36441f42021-02-17 16:25:31 +01001300 hash_params.sni_prehash =
1301 conn_hash_prehash(sni_smp->data.u.str.area,
1302 sni_smp->data.u.str.data);
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01001303 }
1304 }
1305#endif /* USE_OPENSSL */
1306
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001307 /* 3. destination address */
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01001308 if (!(s->flags & SF_ADDR_SET)) {
1309 err = alloc_dst_address(&s->target_addr, srv, s);
1310 if (err != SRV_STATUS_OK)
1311 return SF_ERR_INTERNAL;
1312
1313 s->flags |= SF_ADDR_SET;
1314 }
1315
1316 if (srv && (!is_addr(&srv->addr) || srv->flags & SRV_F_MAPPORTS))
1317 hash_params.dst_addr = s->target_addr;
1318
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001319 /* 4. source address */
Amaury Denoyelled10a2002021-02-11 19:45:19 +01001320 err = alloc_bind_address(&bind_addr, srv, s);
1321 if (err != SRV_STATUS_OK)
1322 return SF_ERR_INTERNAL;
1323
1324 hash_params.src_addr = bind_addr;
1325
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001326 /* 5. proxy protocol */
Amaury Denoyelle1921d202021-01-14 10:15:29 +01001327 if (srv && srv->pp_opts) {
1328 proxy_line_ret = make_proxy_line(trash.area, trash.size, srv, cli_conn, s);
1329 if (proxy_line_ret) {
Amaury Denoyelle36441f42021-02-17 16:25:31 +01001330 hash_params.proxy_prehash =
1331 conn_hash_prehash(trash.area, proxy_line_ret);
Amaury Denoyelle1921d202021-01-14 10:15:29 +01001332 }
1333 }
1334
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001335 hash = conn_calculate_hash(&hash_params);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01001336
Willy Tarreaua5797aa2019-07-18 18:40:06 +02001337 /* This will catch some corner cases such as lying connections resulting from
1338 * retries or connect timeouts but will rarely trigger.
Olivier Houchard0fa989f2018-12-05 17:08:55 +01001339 */
Willy Tarreaua5797aa2019-07-18 18:40:06 +02001340 si_release_endpoint(&s->si[1]);
1341
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001342 /* do not reuse if mode is not http */
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01001343 if (!IS_HTX_STRM(s))
Amaury Denoyelle08d87b32021-01-26 17:35:46 +01001344 goto skip_reuse;
1345
Amaury Denoyelle8e99b842021-10-18 14:39:57 +02001346 /* disable reuse if websocket stream and the protocol to use is not the
1347 * same as the main protocol of the server.
1348 */
1349 if (unlikely(s->flags & SF_WEBSOCKET) && srv) {
1350 if (!srv_check_reuse_ws(srv)) {
1351 DBG_TRACE_STATE("skip idle connections reuse: websocket stream", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1352 goto skip_reuse;
1353 }
1354 }
1355
Willy Tarreaub0821862019-07-18 19:26:11 +02001356 /* first, search for a matching connection in the session's idle conns */
Amaury Denoyelle293dcc42021-01-25 10:29:35 +01001357 srv_conn = session_get_conn(s->sess, s->target, hash);
Christopher Fauletfcc3d8a2020-07-01 16:36:51 +02001358 if (srv_conn)
1359 reuse = 1;
Olivier Houchard00cf70f2018-11-30 17:24:55 +01001360
Amaury Denoyelle7f68d812021-01-26 14:35:26 +01001361 if (srv && !reuse && reuse_mode != PR_O_REUSE_NEVR) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01001362 /* Below we pick connections from the safe, idle or
1363 * available (which are safe too) lists based
Willy Tarreau449d74a2015-08-05 17:16:33 +02001364 * on the strategy, the fact that this is a first or second
1365 * (retryable) request, with the indicated priority (1 or 2) :
1366 *
1367 * SAFE AGGR ALWS
1368 *
1369 * +-----+-----+ +-----+-----+ +-----+-----+
1370 * req| 1st | 2nd | req| 1st | 2nd | req| 1st | 2nd |
1371 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1372 * safe| - | 2 | safe| 1 | 2 | safe| 1 | 2 |
1373 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1374 * idle| - | 1 | idle| - | 1 | idle| 2 | 1 |
1375 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
Willy Tarreaub0821862019-07-18 19:26:11 +02001376 *
1377 * Idle conns are necessarily looked up on the same thread so
1378 * that there is no concurrency issues.
Willy Tarreau449d74a2015-08-05 17:16:33 +02001379 */
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001380 if (!eb_is_empty(&srv->per_thr[tid].avail_conns)) {
1381 srv_conn = srv_lookup_conn(&srv->per_thr[tid].avail_conns, hash);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001382 if (srv_conn)
1383 reuse = 1;
Willy Tarreau449d74a2015-08-05 17:16:33 +02001384 }
Amaury Denoyelle1399d692021-01-22 19:37:44 +01001385
Amaury Denoyelle7f68d812021-01-26 14:35:26 +01001386 /* if no available connections found, search for an idle/safe */
Amaury Denoyelle1399d692021-01-22 19:37:44 +01001387 if (!srv_conn && srv->max_idle_conns && srv->curr_idle_conns > 0) {
Amaury Denoyelle7f68d812021-01-26 14:35:26 +01001388 const int not_first_req = s->txn && s->txn->flags & TX_NOT_FIRST;
1389 const int idle = srv->curr_idle_nb > 0;
1390 const int safe = srv->curr_safe_nb > 0;
1391
1392 /* second column of the tables above,
1393 * search for an idle then safe conn */
1394 if (not_first_req) {
1395 if (idle || safe)
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001396 srv_conn = conn_backend_get(s, srv, 0, hash);
Olivier Houchard566df302020-03-06 18:18:56 +01001397 }
Amaury Denoyelle7f68d812021-01-26 14:35:26 +01001398 /* first column of the tables above */
1399 else if (reuse_mode >= PR_O_REUSE_AGGR) {
1400 /* search for a safe conn */
1401 if (safe)
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001402 srv_conn = conn_backend_get(s, srv, 1, hash);
Amaury Denoyelleaa890ae2021-01-25 14:43:17 +01001403
1404 /* search for an idle conn if no safe conn found
1405 * on always reuse mode */
1406 if (!srv_conn &&
1407 reuse_mode == PR_O_REUSE_ALWS && idle) {
1408 /* TODO conn_backend_get should not check the
1409 * safe list is this case */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001410 srv_conn = conn_backend_get(s, srv, 0, hash);
Amaury Denoyelleaa890ae2021-01-25 14:43:17 +01001411 }
Olivier Houchard566df302020-03-06 18:18:56 +01001412 }
Amaury Denoyelle37e25bc2021-01-26 14:35:25 +01001413
Christopher Faulet3d52f0f2020-07-02 15:45:56 +02001414 if (srv_conn)
Olivier Houchard566df302020-03-06 18:18:56 +01001415 reuse = 1;
Olivier Houcharddc2f2752020-02-13 19:12:07 +01001416 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001417 }
1418
Willy Tarreaub0821862019-07-18 19:26:11 +02001419
1420 /* here reuse might have been set above, indicating srv_conn finally
1421 * is OK.
1422 */
Willy Tarreaub0821862019-07-18 19:26:11 +02001423
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001424 if (ha_used_fds > global.tune.pool_high_count && srv) {
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001425 struct connection *tokill_conn = NULL;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001426 struct conn_hash_node *conn_node = NULL;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001427 struct ebmb_node *node = NULL;
Olivier Houchard88698d92019-04-16 19:07:22 +02001428
1429 /* We can't reuse a connection, and e have more FDs than deemd
1430 * acceptable, attempt to kill an idling connection
1431 */
1432 /* First, try from our own idle list */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001433 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001434 node = ebmb_first(&srv->per_thr[tid].idle_conns);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001435 if (node) {
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001436 conn_node = ebmb_entry(node, struct conn_hash_node, node);
1437 tokill_conn = conn_node->conn;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001438 ebmb_delete(node);
Amaury Denoyelle65bf6002021-03-23 10:44:43 +01001439 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
1440
1441 /* Release the idle lock before calling mux->destroy.
1442 * It will in turn call srv_release_conn through
1443 * conn_free which also uses it.
1444 */
Olivier Houchard88698d92019-04-16 19:07:22 +02001445 tokill_conn->mux->destroy(tokill_conn->ctx);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001446 }
Amaury Denoyelle65bf6002021-03-23 10:44:43 +01001447 else {
1448 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
1449 }
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001450
Olivier Houchard88698d92019-04-16 19:07:22 +02001451 /* If not, iterate over other thread's idling pool, and try to grab one */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001452 if (!tokill_conn) {
Olivier Houchard88698d92019-04-16 19:07:22 +02001453 int i;
1454
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02001455 for (i = tid; (i = ((i + 1 == global.nbthread) ? 0 : i + 1)) != tid;) {
Willy Tarreau08e2b412019-05-26 11:50:08 +02001456 // just silence stupid gcc which reports an absurd
1457 // out-of-bounds warning for <i> which is always
1458 // exactly zero without threads, but it seems to
1459 // see it possibly larger.
1460 ALREADY_CHECKED(i);
1461
Willy Tarreau9b9f8472021-03-26 20:52:10 +01001462 if (HA_SPIN_TRYLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock) != 0)
1463 continue;
1464
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001465 node = ebmb_first(&srv->per_thr[i].idle_conns);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001466 if (node) {
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001467 conn_node = ebmb_entry(node, struct conn_hash_node, node);
1468 tokill_conn = conn_node->conn;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001469 ebmb_delete(node);
1470 }
1471
1472 if (!tokill_conn) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001473 node = ebmb_first(&srv->per_thr[i].safe_conns);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001474 if (node) {
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001475 conn_node = ebmb_entry(node, struct conn_hash_node, node);
1476 tokill_conn = conn_node->conn;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001477 ebmb_delete(node);
1478 }
1479 }
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001480 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Amaury Denoyellea3bf62e2021-01-28 10:16:29 +01001481
Olivier Houchard88698d92019-04-16 19:07:22 +02001482 if (tokill_conn) {
1483 /* We got one, put it into the concerned thread's to kill list, and wake it's kill task */
1484
Willy Tarreau2b718102021-04-21 07:32:39 +02001485 MT_LIST_APPEND(&idle_conns[i].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001486 (struct mt_list *)&tokill_conn->toremove_list);
Willy Tarreau4d82bf52020-06-28 00:19:17 +02001487 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Olivier Houchard88698d92019-04-16 19:07:22 +02001488 break;
1489 }
1490 }
1491 }
1492
1493 }
Willy Tarreau34601a82013-12-15 16:33:46 +01001494
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001495 if (reuse) {
Willy Tarreaub0821862019-07-18 19:26:11 +02001496 if (srv_conn->mux) {
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001497 int avail = srv_conn->mux->avail_streams(srv_conn);
1498
1499 if (avail <= 1) {
Olivier Houchard2442f682018-12-01 17:03:38 +01001500 /* No more streams available, remove it from the list */
Christopher Faulet618f1032022-04-22 17:56:24 +02001501 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001502 conn_delete_from_tree(&srv_conn->hash_node->node);
Christopher Faulet618f1032022-04-22 17:56:24 +02001503 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard2442f682018-12-01 17:03:38 +01001504 }
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001505
1506 if (avail >= 1) {
1507 srv_cs = srv_conn->mux->attach(srv_conn, s->sess);
Olivier Houchard2444aa52020-01-20 13:56:01 +01001508 if (srv_cs)
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001509 si_attach_cs(&s->si[1], srv_cs);
Olivier Houchard2444aa52020-01-20 13:56:01 +01001510 else
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001511 srv_conn = NULL;
1512 }
Olivier Houchard134a2042018-12-28 14:45:47 +01001513 else
1514 srv_conn = NULL;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01001515 }
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001516 /* otherwise srv_conn is left intact */
1517 }
1518 else
1519 srv_conn = NULL;
1520
Amaury Denoyelle08d87b32021-01-26 17:35:46 +01001521skip_reuse:
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001522 /* no reuse or failed to reuse the connection above, pick a new one */
1523 if (!srv_conn) {
Christopher Faulet236c93b2020-07-02 09:19:54 +02001524 srv_conn = conn_new(s->target);
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001525 srv_cs = NULL;
Christopher Fauletc64badd2020-07-01 15:12:43 +02001526
Willy Tarreaudc2ac812020-07-15 17:46:32 +02001527 if (srv_conn) {
1528 srv_conn->owner = s->sess;
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001529
1530 /* connection will be attached to the session if
1531 * http-reuse mode is never or it is not targeted to a
1532 * server */
1533 if (reuse_mode == PR_O_REUSE_NEVR || !srv)
Willy Tarreaudc2ac812020-07-15 17:46:32 +02001534 conn_set_private(srv_conn);
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001535
Amaury Denoyelle68967e52021-03-02 12:01:06 +01001536 /* assign bind_addr to srv_conn */
Amaury Denoyelled10a2002021-02-11 19:45:19 +01001537 srv_conn->src = bind_addr;
Amaury Denoyelle68967e52021-03-02 12:01:06 +01001538 bind_addr = NULL;
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001539
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01001540 if (!sockaddr_alloc(&srv_conn->dst, 0, 0)) {
1541 conn_free(srv_conn);
1542 return SF_ERR_RESOURCE;
1543 }
Amaury Denoyelle5546a632021-10-20 15:22:20 +02001544
1545 srv_conn->hash_node->hash = hash;
Olivier Houchardc0caac22020-03-20 14:26:32 +01001546 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001547 }
1548
Amaury Denoyelle68967e52021-03-02 12:01:06 +01001549 /* if bind_addr is non NULL free it */
1550 sockaddr_free(&bind_addr);
1551
1552 /* srv_conn is still NULL only on allocation failure */
1553 if (!srv_conn)
1554 return SF_ERR_RESOURCE;
1555
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02001556 /* copy the target address into the connection */
1557 *srv_conn->dst = *s->target_addr;
1558
1559 /* Copy network namespace from client connection */
1560 srv_conn->proxy_netns = cli_conn ? cli_conn->proxy_netns : NULL;
1561
Olivier Houchard1b3c9312021-03-05 23:37:48 +01001562 if (!srv_conn->xprt) {
Willy Tarreauff605db2013-12-20 11:09:51 +01001563 /* set the correct protocol on the output stream interface */
Olivier Houchard1b3c9312021-03-05 23:37:48 +01001564 if (srv) {
1565 if (conn_prepare(srv_conn, protocol_by_family(srv_conn->dst->ss_family), srv->xprt)) {
1566 conn_free(srv_conn);
1567 return SF_ERR_INTERNAL;
1568 }
1569 } else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1570 int ret;
1571
Willy Tarreauff605db2013-12-20 11:09:51 +01001572 /* proxies exclusively run on raw_sock right now */
Olivier Houchard1b3c9312021-03-05 23:37:48 +01001573 ret = conn_prepare(srv_conn, protocol_by_family(srv_conn->dst->ss_family), xprt_get(XPRT_RAW));
1574 if (ret < 0 || !(srv_conn->ctrl)) {
Olivier Houchardc0caac22020-03-20 14:26:32 +01001575 conn_free(srv_conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001576 return SF_ERR_INTERNAL;
Olivier Houchardc0caac22020-03-20 14:26:32 +01001577 }
Willy Tarreauff605db2013-12-20 11:09:51 +01001578 }
Olivier Houchardc0caac22020-03-20 14:26:32 +01001579 else {
1580 conn_free(srv_conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001581 return SF_ERR_INTERNAL; /* how did we get there ? */
Olivier Houchardc0caac22020-03-20 14:26:32 +01001582 }
Willy Tarreaud02394b2012-05-11 18:32:18 +02001583
Olivier Houchard8af03b32020-01-22 17:34:54 +01001584 srv_cs = si_alloc_cs(&s->si[1], srv_conn);
1585 if (!srv_cs) {
1586 conn_free(srv_conn);
1587 return SF_ERR_RESOURCE;
1588 }
1589 srv_conn->ctx = srv_cs;
Olivier Houchardecffb7d2020-01-24 14:10:55 +01001590#if defined(USE_OPENSSL) && defined(TLSEXT_TYPE_application_layer_protocol_negotiation)
Olivier Houchard12950162018-11-23 14:32:08 +01001591 if (!srv ||
Christopher Fauletb4de4202020-07-30 09:10:36 +02001592 (srv->use_ssl != 1 || (!(srv->ssl_ctx.alpn_str) && !(srv->ssl_ctx.npn_str)) ||
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01001593 srv->mux_proto || !IS_HTX_STRM(s)))
Olivier Houchard201b9f42018-11-21 00:16:29 +01001594#endif
Olivier Houchard5cd62172019-01-04 15:52:26 +01001595 init_mux = 1;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001596
Willy Tarreauff605db2013-12-20 11:09:51 +01001597 /* process the case where the server requires the PROXY protocol to be sent */
1598 srv_conn->send_proxy_ofs = 0;
Olivier Houchard522eea72017-11-03 16:27:47 +01001599
Willy Tarreau7b004922015-08-04 19:34:21 +02001600 if (srv && srv->pp_opts) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001601 srv_conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreauff605db2013-12-20 11:09:51 +01001602 srv_conn->send_proxy_ofs = 1; /* must compute size */
Willy Tarreauff605db2013-12-20 11:09:51 +01001603 if (cli_conn)
Willy Tarreau3cc01d82019-07-17 11:27:38 +02001604 conn_get_dst(cli_conn);
Willy Tarreauff605db2013-12-20 11:09:51 +01001605 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001606
Alexander Liu2a54bb72019-05-22 19:44:48 +08001607 if (srv && (srv->flags & SRV_F_SOCKS4_PROXY)) {
1608 srv_conn->send_proxy_ofs = 1;
1609 srv_conn->flags |= CO_FL_SOCKS4;
1610 }
Amaury Denoyelle8e99b842021-10-18 14:39:57 +02001611
1612#if defined(USE_OPENSSL) && defined(TLSEXT_TYPE_application_layer_protocol_negotiation)
1613 /* if websocket stream, try to update connection ALPN. */
1614 if (unlikely(s->flags & SF_WEBSOCKET) &&
1615 srv && srv->use_ssl && srv->ssl_ctx.alpn_str) {
1616 char *alpn = "";
1617 int force = 0;
1618
1619 switch (srv->ws) {
1620 case SRV_WS_AUTO:
1621 alpn = "\x08http/1.1";
1622 force = 0;
1623 break;
1624 case SRV_WS_H1:
1625 alpn = "\x08http/1.1";
1626 force = 1;
1627 break;
1628 case SRV_WS_H2:
1629 alpn = "\x02h2";
1630 force = 1;
1631 break;
1632 }
1633
1634 if (!conn_update_alpn(srv_conn, ist(alpn), force))
1635 DBG_TRACE_STATE("update alpn for websocket", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1636 }
1637#endif
Willy Tarreauff605db2013-12-20 11:09:51 +01001638 }
Olivier Houcharde8f5f5d2019-10-25 17:00:54 +02001639 else {
Amaury Denoyelle05550a02021-06-17 15:14:49 +02001640 s->flags |= SF_SRV_REUSED;
1641
Amaury Denoyelled7faa3d2021-03-05 15:27:41 +01001642 /* Currently there seems to be no known cases of xprt ready
1643 * without the mux installed here.
1644 */
1645 BUG_ON(!srv_conn->mux);
1646
Amaury Denoyelle05550a02021-06-17 15:14:49 +02001647 if (!(srv_conn->mux->ctl(srv_conn, MUX_STATUS, NULL) & MUX_STATUS_READY))
1648 s->flags |= SF_SRV_REUSED_ANTICIPATED;
Olivier Houcharde8f5f5d2019-10-25 17:00:54 +02001649 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001650
William Lallemandb7ff6a32012-03-02 14:35:21 +01001651 /* flag for logging source ip/port */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001652 if (strm_fe(s)->options2 & PR_O2_SRC_ADDR)
Willy Tarreau350f4872014-11-28 14:42:25 +01001653 s->si[1].flags |= SI_FL_SRC_ADDR;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001654
Willy Tarreau14f8e862012-08-30 22:23:13 +02001655 /* disable lingering */
1656 if (s->be->options & PR_O_TCP_NOLING)
Willy Tarreau350f4872014-11-28 14:42:25 +01001657 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau14f8e862012-08-30 22:23:13 +02001658
Willy Tarreauf1573842018-12-14 11:35:36 +01001659 if (s->flags & SF_SRV_REUSED) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001660 _HA_ATOMIC_INC(&s->be->be_counters.reuse);
Willy Tarreaucc79ed22018-12-15 15:11:36 +01001661 if (srv)
Willy Tarreau4781b152021-04-06 13:53:36 +02001662 _HA_ATOMIC_INC(&srv->counters.reuse);
Willy Tarreauf1573842018-12-14 11:35:36 +01001663 } else {
Willy Tarreau4781b152021-04-06 13:53:36 +02001664 _HA_ATOMIC_INC(&s->be->be_counters.connect);
Willy Tarreaucc79ed22018-12-15 15:11:36 +01001665 if (srv)
Willy Tarreau4781b152021-04-06 13:53:36 +02001666 _HA_ATOMIC_INC(&srv->counters.connect);
Willy Tarreauf1573842018-12-14 11:35:36 +01001667 }
1668
Olivier Houchard201b9f42018-11-21 00:16:29 +01001669 err = si_connect(&s->si[1], srv_conn);
Willy Tarreau09e02032019-07-18 16:18:20 +02001670 if (err != SF_ERR_NONE)
1671 return err;
1672
Christopher Faulet27bd6ff2020-07-01 11:00:18 +02001673#ifdef USE_OPENSSL
Amaury Denoyelle4568a4b2021-06-01 17:04:10 +02001674 if (!(s->flags & SF_SRV_REUSED)) {
1675 if (smp_make_safe(sni_smp))
1676 ssl_sock_set_servername(srv_conn, sni_smp->data.u.str.area);
1677 }
Christopher Faulet27bd6ff2020-07-01 11:00:18 +02001678#endif /* USE_OPENSSL */
1679
Willy Tarreaua3b17562020-07-31 08:39:31 +02001680 /* The CO_FL_SEND_PROXY flag may have been set by the connect method,
1681 * if so, add our handshake pseudo-XPRT now.
1682 */
1683 if ((srv_conn->flags & CO_FL_HANDSHAKE)) {
1684 if (xprt_add_hs(srv_conn) < 0) {
1685 conn_full_close(srv_conn);
1686 return SF_ERR_INTERNAL;
1687 }
1688 }
Olivier Houchard1b3c9312021-03-05 23:37:48 +01001689 conn_xprt_start(srv_conn);
Willy Tarreaua3b17562020-07-31 08:39:31 +02001690
Olivier Houchard5cd62172019-01-04 15:52:26 +01001691 /* We have to defer the mux initialization until after si_connect()
1692 * has been called, as we need the xprt to have been properly
1693 * initialized, or any attempt to recv during the mux init may
1694 * fail, and flag the connection as CO_FL_ERROR.
1695 */
1696 if (init_mux) {
Amaury Denoyelle8e99b842021-10-18 14:39:57 +02001697 const struct mux_ops *alt_mux =
1698 likely(!(s->flags & SF_WEBSOCKET)) ? NULL : srv_get_ws_proto(srv);
1699 if (conn_install_mux_be(srv_conn, srv_cs, s->sess, alt_mux) < 0) {
Olivier Houchard74931142019-01-29 19:11:16 +01001700 conn_full_close(srv_conn);
Olivier Houchard5cd62172019-01-04 15:52:26 +01001701 return SF_ERR_INTERNAL;
Olivier Houchard74931142019-01-29 19:11:16 +01001702 }
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01001703 if (IS_HTX_STRM(s)) {
Amaury Denoyelle08d87b32021-01-26 17:35:46 +01001704 /* If we're doing http-reuse always, and the connection
1705 * is not private with available streams (an http2
1706 * connection), add it to the available list, so that
1707 * others can use it right away. If the connection is
1708 * private or we're doing http-reuse safe and the mux
1709 * protocol supports multiplexing, add it in the
1710 * session server list.
1711 */
1712 if (srv && reuse_mode == PR_O_REUSE_ALWS &&
1713 !(srv_conn->flags & CO_FL_PRIVATE) &&
1714 srv_conn->mux->avail_streams(srv_conn) > 0) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001715 ebmb_insert(&srv->per_thr[tid].avail_conns, &srv_conn->hash_node->node, sizeof(srv_conn->hash_node->hash));
Amaury Denoyelle08d87b32021-01-26 17:35:46 +01001716 }
1717 else if (srv_conn->flags & CO_FL_PRIVATE ||
1718 (reuse_mode == PR_O_REUSE_SAFE &&
1719 srv_conn->mux->flags & MX_FL_HOL_RISK)) {
1720 /* If it fail now, the same will be done in mux->detach() callback */
1721 session_add_conn(s->sess, srv_conn, srv_conn->target);
1722 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02001723 }
Olivier Houchard5cd62172019-01-04 15:52:26 +01001724 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001725
Willy Tarreau42afc592021-08-30 09:35:18 +02001726#if defined(USE_OPENSSL) && (defined(OPENSSL_IS_BORINGSSL) || (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L))
Olivier Houchard4cd2af42019-05-06 15:18:27 +02001727
Olivier Houchard8694e5b2019-06-15 00:14:05 +02001728 if (!reuse && cli_conn && srv && srv_conn->mux &&
Olivier Houchard522eea72017-11-03 16:27:47 +01001729 (srv->ssl_ctx.options & SRV_SSL_O_EARLY_DATA) &&
Olivier Houchard865d8392019-05-03 22:46:27 +02001730 /* Only attempt to use early data if either the client sent
1731 * early data, so that we know it can handle a 425, or if
1732 * we are allwoed to retry requests on early data failure, and
1733 * it's our first try
1734 */
1735 ((cli_conn->flags & CO_FL_EARLY_DATA) ||
1736 ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
1737 s->si[1].conn_retries == s->be->conn_retries)) &&
1738 !channel_is_empty(si_oc(&s->si[1])) &&
1739 srv_conn->flags & CO_FL_SSL_WAIT_HS)
Olivier Houchard522eea72017-11-03 16:27:47 +01001740 srv_conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN);
Willy Tarreau46c9d3e2017-11-08 14:25:59 +01001741#endif
Olivier Houchard522eea72017-11-03 16:27:47 +01001742
Willy Tarreau14f8e862012-08-30 22:23:13 +02001743 /* set connect timeout */
Willy Tarreau350f4872014-11-28 14:42:25 +01001744 s->si[1].exp = tick_add_ifset(now_ms, s->be->timeout.connect);
Willy Tarreau14f8e862012-08-30 22:23:13 +02001745
Willy Tarreau827aee92011-03-10 16:55:02 +01001746 if (srv) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02001747 int count;
1748
Willy Tarreaue7dff022015-04-03 01:14:29 +02001749 s->flags |= SF_CURR_SESS;
Willy Tarreau1db42732021-04-06 11:44:07 +02001750 count = _HA_ATOMIC_ADD_FETCH(&srv->cur_sess, 1);
Christopher Faulet29f77e82017-06-08 14:04:45 +02001751 HA_ATOMIC_UPDATE_MAX(&srv->counters.cur_sess_max, count);
Willy Tarreau59b0fec2021-02-17 16:01:37 +01001752 if (s->be->lbprm.server_take_conn)
1753 s->be->lbprm.server_take_conn(srv, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001754 }
1755
Willy Tarreauada4c582020-03-04 16:42:03 +01001756 /* Now handle synchronously connected sockets. We know the stream-int
1757 * is at least in state SI_ST_CON. These ones typically are UNIX
1758 * sockets, socket pairs, and occasionally TCP connections on the
1759 * loopback on a heavily loaded system.
1760 */
1761 if ((srv_conn->flags & CO_FL_ERROR || srv_cs->flags & CS_FL_ERROR))
1762 s->si[1].flags |= SI_FL_ERR;
1763
1764 /* If we had early data, and the handshake ended, then
1765 * we can remove the flag, and attempt to wake the task up,
1766 * in the event there's an analyser waiting for the end of
1767 * the handshake.
1768 */
1769 if (!(srv_conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)))
1770 srv_cs->flags &= ~CS_FL_WAIT_FOR_HS;
1771
1772 if (!si_state_in(s->si[1].state, SI_SB_EST|SI_SB_DIS|SI_SB_CLO) &&
1773 (srv_conn->flags & CO_FL_WAIT_XPRT) == 0) {
1774 s->si[1].exp = TICK_ETERNITY;
1775 si_oc(&s->si[1])->flags |= CF_WRITE_NULL;
1776 if (s->si[1].state == SI_ST_CON)
1777 s->si[1].state = SI_ST_RDY;
1778 }
1779
1780 /* Report EOI on the channel if it was reached from the mux point of
1781 * view.
1782 *
1783 * Note: This test is only required because si_cs_process is also the SI
1784 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
1785 * care of it.
1786 */
1787 if ((srv_cs->flags & CS_FL_EOI) && !(si_ic(&s->si[1])->flags & CF_EOI))
1788 si_ic(&s->si[1])->flags |= (CF_EOI|CF_READ_PARTIAL);
1789
Christopher Faulet3f5bcd02020-07-29 22:42:27 +02001790 /* catch all sync connect while the mux is not already installed */
1791 if (!srv_conn->mux && !(srv_conn->flags & CO_FL_WAIT_XPRT)) {
1792 if (conn_create_mux(srv_conn) < 0) {
1793 conn_full_close(srv_conn);
1794 return SF_ERR_INTERNAL;
1795 }
1796 }
1797
Willy Tarreaue7dff022015-04-03 01:14:29 +02001798 return SF_ERR_NONE; /* connection is OK */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001799}
1800
1801
Willy Tarreaubaaee002006-06-26 02:48:02 +02001802/* This function performs the "redispatch" part of a connection attempt. It
1803 * will assign a server if required, queue the connection if required, and
1804 * handle errors that might arise at this level. It can change the server
1805 * state. It will return 1 if it encounters an error, switches the server
1806 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1807 * that the connection is ready to use.
1808 */
1809
Willy Tarreau87b09662015-04-03 00:22:06 +02001810int srv_redispatch_connect(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001811{
Willy Tarreau827aee92011-03-10 16:55:02 +01001812 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001813 int conn_err;
1814
1815 /* We know that we don't have any connection pending, so we will
1816 * try to get a new one, and wait in this state if it's queued
1817 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001818 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001819 conn_err = assign_server_and_queue(s);
1820 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001821
Willy Tarreaubaaee002006-06-26 02:48:02 +02001822 switch (conn_err) {
1823 case SRV_STATUS_OK:
1824 break;
1825
Willy Tarreau7c669d72008-06-20 15:04:11 +02001826 case SRV_STATUS_FULL:
1827 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1828 * and we can redispatch to another server, or it is not and we return
1829 * 503. This only makes sense in DIRECT mode however, because normal LB
1830 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001831 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001832 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001833 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001834 if (((s->flags & (SF_DIRECT|SF_FORCE_PRST)) == SF_DIRECT) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001835 (s->be->options & PR_O_REDISP)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001836 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02001837 sockaddr_free(&s->target_addr);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001838 goto redispatch;
1839 }
1840
Willy Tarreau350f4872014-11-28 14:42:25 +01001841 if (!s->si[1].err_type) {
1842 s->si[1].err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001843 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001844
Willy Tarreau4781b152021-04-06 13:53:36 +02001845 _HA_ATOMIC_INC(&srv->counters.failed_conns);
1846 _HA_ATOMIC_INC(&s->be->be_counters.failed_conns);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001847 return 1;
1848
Willy Tarreaubaaee002006-06-26 02:48:02 +02001849 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001850 /* note: it is guaranteed that srv == NULL here */
Willy Tarreau350f4872014-11-28 14:42:25 +01001851 if (!s->si[1].err_type) {
1852 s->si[1].err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001853 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001854
Willy Tarreau4781b152021-04-06 13:53:36 +02001855 _HA_ATOMIC_INC(&s->be->be_counters.failed_conns);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001856 return 1;
1857
1858 case SRV_STATUS_QUEUED:
Willy Tarreau350f4872014-11-28 14:42:25 +01001859 s->si[1].exp = tick_add_ifset(now_ms, s->be->timeout.queue);
1860 s->si[1].state = SI_ST_QUE;
Willy Tarreau87b09662015-04-03 00:22:06 +02001861 /* do nothing else and do not wake any other stream up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001862 return 1;
1863
Willy Tarreaubaaee002006-06-26 02:48:02 +02001864 case SRV_STATUS_INTERNAL:
1865 default:
Willy Tarreau350f4872014-11-28 14:42:25 +01001866 if (!s->si[1].err_type) {
1867 s->si[1].err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001868 }
1869
Willy Tarreau827aee92011-03-10 16:55:02 +01001870 if (srv)
1871 srv_inc_sess_ctr(srv);
1872 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001873 srv_set_sess_last(srv);
1874 if (srv)
Willy Tarreau4781b152021-04-06 13:53:36 +02001875 _HA_ATOMIC_INC(&srv->counters.failed_conns);
1876 _HA_ATOMIC_INC(&s->be->be_counters.failed_conns);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001877
Willy Tarreau87b09662015-04-03 00:22:06 +02001878 /* release other streams waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001879 if (may_dequeue_tasks(srv, s->be))
Amaury Denoyellecaaafd02021-06-18 11:11:36 +02001880 process_srv_queue(srv, 0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001881 return 1;
1882 }
1883 /* if we get here, it's because we got SRV_STATUS_OK, which also
1884 * means that the connection has not been queued.
1885 */
1886 return 0;
1887}
1888
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001889/* Check if the connection request is in such a state that it can be aborted. */
1890static int back_may_abort_req(struct channel *req, struct stream *s)
1891{
1892 return ((req->flags & (CF_READ_ERROR)) ||
1893 ((req->flags & (CF_SHUTW_NOW|CF_SHUTW)) && /* empty and client aborted */
1894 (channel_is_empty(req) || (s->be->options & PR_O_ABRT_CLOSE))));
1895}
1896
1897/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
1898 * SI_ST_TAR. Other input states are simply ignored.
1899 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
1900 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
1901 * conditions.
1902 */
1903void back_try_conn_req(struct stream *s)
1904{
1905 struct server *srv = objt_server(s->target);
1906 struct stream_interface *si = &s->si[1];
1907 struct channel *req = &s->req;
1908
1909 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1910
1911 if (si->state == SI_ST_ASS) {
1912 /* Server assigned to connection request, we have to try to connect now */
1913 int conn_err;
1914
1915 /* Before we try to initiate the connection, see if the
1916 * request may be aborted instead.
1917 */
1918 if (back_may_abort_req(req, s)) {
1919 si->err_type |= SI_ET_CONN_ABRT;
1920 DBG_TRACE_STATE("connection aborted", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
1921 goto abort_connection;
1922 }
1923
1924 conn_err = connect_server(s);
1925 srv = objt_server(s->target);
1926
1927 if (conn_err == SF_ERR_NONE) {
1928 /* state = SI_ST_CON or SI_ST_EST now */
1929 if (srv)
1930 srv_inc_sess_ctr(srv);
1931 if (srv)
1932 srv_set_sess_last(srv);
1933 DBG_TRACE_STATE("connection attempt", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1934 goto end;
1935 }
1936
1937 /* We have received a synchronous error. We might have to
1938 * abort, retry immediately or redispatch.
1939 */
1940 if (conn_err == SF_ERR_INTERNAL) {
1941 if (!si->err_type) {
1942 si->err_type = SI_ET_CONN_OTHER;
1943 }
1944
1945 if (srv)
1946 srv_inc_sess_ctr(srv);
1947 if (srv)
1948 srv_set_sess_last(srv);
1949 if (srv)
Willy Tarreau4781b152021-04-06 13:53:36 +02001950 _HA_ATOMIC_INC(&srv->counters.failed_conns);
1951 _HA_ATOMIC_INC(&s->be->be_counters.failed_conns);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001952
1953 /* release other streams waiting for this server */
1954 sess_change_server(s, NULL);
1955 if (may_dequeue_tasks(srv, s->be))
Amaury Denoyellecaaafd02021-06-18 11:11:36 +02001956 process_srv_queue(srv, 0);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001957
1958 /* Failed and not retryable. */
1959 si_shutr(si);
1960 si_shutw(si);
1961 req->flags |= CF_WRITE_ERROR;
1962
1963 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1964
1965 /* we may need to know the position in the queue for logging */
1966 pendconn_cond_unlink(s->pend_pos);
1967
1968 /* no stream was ever accounted for this server */
1969 si->state = SI_ST_CLO;
1970 if (s->srv_error)
1971 s->srv_error(s, si);
1972 DBG_TRACE_STATE("internal error during connection", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
1973 goto end;
1974 }
1975
1976 /* We are facing a retryable error, but we don't want to run a
1977 * turn-around now, as the problem is likely a source port
1978 * allocation problem, so we want to retry now.
1979 */
1980 si->state = SI_ST_CER;
1981 si->flags &= ~SI_FL_ERR;
1982 back_handle_st_cer(s);
1983
1984 DBG_TRACE_STATE("connection error, retry", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
1985 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
1986 }
1987 else if (si->state == SI_ST_QUE) {
1988 /* connection request was queued, check for any update */
1989 if (!pendconn_dequeue(s)) {
1990 /* The connection is not in the queue anymore. Either
1991 * we have a server connection slot available and we
1992 * go directly to the assigned state, or we need to
1993 * load-balance first and go to the INI state.
1994 */
1995 si->exp = TICK_ETERNITY;
1996 if (unlikely(!(s->flags & SF_ASSIGNED)))
1997 si->state = SI_ST_REQ;
1998 else {
1999 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2000 si->state = SI_ST_ASS;
2001 }
2002 DBG_TRACE_STATE("dequeue connection request", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2003 goto end;
2004 }
2005
2006 /* Connection request still in queue... */
2007 if (si->flags & SI_FL_EXP) {
2008 /* ... and timeout expired */
2009 si->exp = TICK_ETERNITY;
2010 si->flags &= ~SI_FL_EXP;
2011 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2012
2013 /* we may need to know the position in the queue for logging */
2014 pendconn_cond_unlink(s->pend_pos);
2015
2016 if (srv)
Willy Tarreau4781b152021-04-06 13:53:36 +02002017 _HA_ATOMIC_INC(&srv->counters.failed_conns);
2018 _HA_ATOMIC_INC(&s->be->be_counters.failed_conns);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002019 si_shutr(si);
2020 si_shutw(si);
2021 req->flags |= CF_WRITE_TIMEOUT;
2022 if (!si->err_type)
2023 si->err_type = SI_ET_QUEUE_TO;
2024 si->state = SI_ST_CLO;
2025 if (s->srv_error)
2026 s->srv_error(s, si);
2027 DBG_TRACE_STATE("connection request still queued", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2028 goto end;
2029 }
2030
2031 /* Connection remains in queue, check if we have to abort it */
2032 if (back_may_abort_req(req, s)) {
2033 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2034
2035 /* we may need to know the position in the queue for logging */
2036 pendconn_cond_unlink(s->pend_pos);
2037
2038 si->err_type |= SI_ET_QUEUE_ABRT;
2039 DBG_TRACE_STATE("abort queued connection request", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2040 goto abort_connection;
2041 }
2042
2043 /* Nothing changed */
2044 }
2045 else if (si->state == SI_ST_TAR) {
2046 /* Connection request might be aborted */
2047 if (back_may_abort_req(req, s)) {
2048 si->err_type |= SI_ET_CONN_ABRT;
2049 DBG_TRACE_STATE("connection aborted", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2050 goto abort_connection;
2051 }
2052
2053 if (!(si->flags & SI_FL_EXP))
2054 return; /* still in turn-around */
2055
2056 si->flags &= ~SI_FL_EXP;
2057 si->exp = TICK_ETERNITY;
2058
2059 /* we keep trying on the same server as long as the stream is
2060 * marked "assigned".
2061 * FIXME: Should we force a redispatch attempt when the server is down ?
2062 */
2063 if (s->flags & SF_ASSIGNED)
2064 si->state = SI_ST_ASS;
2065 else
2066 si->state = SI_ST_REQ;
2067
2068 DBG_TRACE_STATE("retry connection now", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2069 }
2070
2071 end:
2072 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2073 return;
2074
2075abort_connection:
2076 /* give up */
2077 si->exp = TICK_ETERNITY;
2078 si->flags &= ~SI_FL_EXP;
2079 si_shutr(si);
2080 si_shutw(si);
2081 si->state = SI_ST_CLO;
2082 if (s->srv_error)
2083 s->srv_error(s, si);
2084 DBG_TRACE_DEVEL("leaving on error", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2085 return;
2086}
2087
2088/* This function initiates a server connection request on a stream interface
2089 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
2090 * a real connection to a server, indicating that a server has been assigned,
2091 * or SI_ST_EST for a successful connection to an applet. It may also return
2092 * SI_ST_QUE, or SI_ST_CLO upon error.
2093 */
2094void back_handle_st_req(struct stream *s)
2095{
2096 struct stream_interface *si = &s->si[1];
2097
2098 if (si->state != SI_ST_REQ)
2099 return;
2100
2101 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2102
2103 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
2104 /* the applet directly goes to the EST state */
2105 struct appctx *appctx = objt_appctx(si->end);
2106
2107 if (!appctx || appctx->applet != __objt_applet(s->target))
2108 appctx = si_register_handler(si, objt_applet(s->target));
2109
2110 if (!appctx) {
2111 /* No more memory, let's immediately abort. Force the
2112 * error code to ignore the ERR_LOCAL which is not a
2113 * real error.
2114 */
2115 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
2116
2117 si_shutr(si);
2118 si_shutw(si);
2119 s->req.flags |= CF_WRITE_ERROR;
2120 si->err_type = SI_ET_CONN_RES;
2121 si->state = SI_ST_CLO;
2122 if (s->srv_error)
2123 s->srv_error(s, si);
2124 DBG_TRACE_STATE("failed to register applet", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2125 goto end;
2126 }
2127
2128 if (tv_iszero(&s->logs.tv_request))
2129 s->logs.tv_request = now;
2130 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2131 si->state = SI_ST_EST;
2132 si->err_type = SI_ET_NONE;
2133 be_set_sess_last(s->be);
2134
2135 DBG_TRACE_STATE("applet registered", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2136 /* let back_establish() finish the job */
2137 goto end;
2138 }
2139
2140 /* Try to assign a server */
2141 if (srv_redispatch_connect(s) != 0) {
2142 /* We did not get a server. Either we queued the
2143 * connection request, or we encountered an error.
2144 */
2145 if (si->state == SI_ST_QUE) {
2146 DBG_TRACE_STATE("connection request queued", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2147 goto end;
2148 }
2149
2150 /* we did not get any server, let's check the cause */
2151 si_shutr(si);
2152 si_shutw(si);
2153 s->req.flags |= CF_WRITE_ERROR;
2154 if (!si->err_type)
2155 si->err_type = SI_ET_CONN_OTHER;
2156 si->state = SI_ST_CLO;
2157 if (s->srv_error)
2158 s->srv_error(s, si);
2159 DBG_TRACE_STATE("connection request failed", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2160 goto end;
2161 }
2162
2163 /* The server is assigned */
2164 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2165 si->state = SI_ST_ASS;
2166 be_set_sess_last(s->be);
2167 DBG_TRACE_STATE("connection request assigned to a server", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2168
2169 end:
2170 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2171}
2172
2173/* This function is called with (si->state == SI_ST_CON) meaning that a
2174 * connection was attempted and that the file descriptor is already allocated.
2175 * We must check for timeout, error and abort. Possible output states are
2176 * SI_ST_CER (error), SI_ST_DIS (abort), and SI_ST_CON (no change). This only
2177 * works with connection-based streams. We know that there were no I/O event
2178 * when reaching this function. Timeouts and errors are *not* cleared.
2179 */
2180void back_handle_st_con(struct stream *s)
2181{
2182 struct stream_interface *si = &s->si[1];
2183 struct channel *req = &s->req;
2184 struct channel *rep = &s->res;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002185
2186 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2187
2188 /* the client might want to abort */
2189 if ((rep->flags & CF_SHUTW) ||
2190 ((req->flags & CF_SHUTW_NOW) &&
2191 (channel_is_empty(req) || (s->be->options & PR_O_ABRT_CLOSE)))) {
2192 si->flags |= SI_FL_NOLINGER;
2193 si_shutw(si);
2194 si->err_type |= SI_ET_CONN_ABRT;
2195 if (s->srv_error)
2196 s->srv_error(s, si);
2197 /* Note: state = SI_ST_DIS now */
2198 DBG_TRACE_STATE("client abort during connection attempt", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
Willy Tarreau062df2c2020-01-10 06:17:03 +01002199 goto end;
2200 }
2201
Willy Tarreau062df2c2020-01-10 06:17:03 +01002202 done:
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002203 /* retryable error ? */
Willy Tarreau062df2c2020-01-10 06:17:03 +01002204 if (si->flags & (SI_FL_EXP|SI_FL_ERR)) {
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002205 if (!si->err_type) {
2206 if (si->flags & SI_FL_ERR)
2207 si->err_type = SI_ET_CONN_ERR;
2208 else
2209 si->err_type = SI_ET_CONN_TO;
2210 }
2211
2212 si->state = SI_ST_CER;
2213 DBG_TRACE_STATE("connection failed, retry", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2214 }
2215
Willy Tarreau062df2c2020-01-10 06:17:03 +01002216 end:
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002217 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2218}
2219
2220/* This function is called with (si->state == SI_ST_CER) meaning that a
2221 * previous connection attempt has failed and that the file descriptor
2222 * has already been released. Possible causes include asynchronous error
2223 * notification and time out. Possible output states are SI_ST_CLO when
2224 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
2225 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
2226 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
2227 * marked as in error state. Timeouts and errors are cleared before retrying.
2228 */
2229void back_handle_st_cer(struct stream *s)
2230{
2231 struct stream_interface *si = &s->si[1];
2232 struct conn_stream *cs = objt_cs(si->end);
2233 struct connection *conn = cs_conn(cs);
2234
2235 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2236
2237 si->exp = TICK_ETERNITY;
2238 si->flags &= ~SI_FL_EXP;
2239
2240 /* we probably have to release last stream from the server */
2241 if (objt_server(s->target)) {
Willy Tarreau66182592021-12-06 07:01:02 +00002242 health_adjust(__objt_server(s->target), HANA_STATUS_L4_ERR);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002243
2244 if (s->flags & SF_CURR_SESS) {
2245 s->flags &= ~SF_CURR_SESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02002246 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002247 }
2248
2249 if ((si->flags & SI_FL_ERR) &&
2250 conn && conn->err_code == CO_ER_SSL_MISMATCH_SNI) {
2251 /* We tried to connect to a server which is configured
2252 * with "verify required" and which doesn't have the
2253 * "verifyhost" directive. The server presented a wrong
2254 * certificate (a certificate for an unexpected name),
2255 * which implies that we have used SNI in the handshake,
2256 * and that the server doesn't have the associated cert
2257 * and presented a default one.
2258 *
2259 * This is a serious enough issue not to retry. It's
2260 * especially important because this wrong name might
2261 * either be the result of a configuration error, and
2262 * retrying will only hammer the server, or is caused
2263 * by the use of a wrong SNI value, most likely
2264 * provided by the client and we don't want to let the
2265 * client provoke retries.
2266 */
2267 si->conn_retries = 0;
2268 DBG_TRACE_DEVEL("Bad SSL cert, disable connection retries", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2269 }
2270 }
2271
2272 /* ensure that we have enough retries left */
2273 si->conn_retries--;
2274 if (si->conn_retries < 0 || !(s->be->retry_type & PR_RE_CONN_FAILED)) {
2275 if (!si->err_type) {
2276 si->err_type = SI_ET_CONN_ERR;
2277 }
2278
2279 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002280 _HA_ATOMIC_INC(&objt_server(s->target)->counters.failed_conns);
2281 _HA_ATOMIC_INC(&s->be->be_counters.failed_conns);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002282 sess_change_server(s, NULL);
2283 if (may_dequeue_tasks(objt_server(s->target), s->be))
Amaury Denoyellecaaafd02021-06-18 11:11:36 +02002284 process_srv_queue(objt_server(s->target), 0);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002285
2286 /* shutw is enough so stop a connecting socket */
2287 si_shutw(si);
2288 s->req.flags |= CF_WRITE_ERROR;
2289 s->res.flags |= CF_READ_ERROR;
2290
2291 si->state = SI_ST_CLO;
2292 if (s->srv_error)
2293 s->srv_error(s, si);
2294
2295 DBG_TRACE_STATE("connection failed", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2296 goto end;
2297 }
2298
Christopher Fauletd9090782021-06-01 14:06:05 +02002299 /* At this stage, we will trigger a connection retry (with or without
2300 * redispatch). Thus we must release the SI endpoint on the server side
2301 * an close the attached connection. It is especially important to do it
2302 * now if the retry is not immediately performed, to be sure to release
2303 * ressources as soon as possible and to not catch errors from the lower
2304 * layers in an unexpected state (i.e < ST_CONN).
2305 *
2306 * Note: the stream-interface will be switched to ST_REQ, ST_ASS or
2307 * ST_TAR and SI_FL_ERR and SI_FL_EXP flags will be unset.
2308 */
2309 si_release_endpoint(&s->si[1]);
2310
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002311 stream_choose_redispatch(s);
2312
2313 if (si->flags & SI_FL_ERR) {
2314 /* The error was an asynchronous connection error, and we will
2315 * likely have to retry connecting to the same server, most
2316 * likely leading to the same result. To avoid this, we wait
2317 * MIN(one second, connect timeout) before retrying. We don't
2318 * do it when the failure happened on a reused connection
2319 * though.
2320 */
2321
2322 int delay = 1000;
Amaury Denoyelle05550a02021-06-17 15:14:49 +02002323 const int reused = (s->flags & SF_SRV_REUSED) &&
2324 !(s->flags & SF_SRV_REUSED_ANTICIPATED);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002325
2326 if (s->be->timeout.connect && s->be->timeout.connect < delay)
2327 delay = s->be->timeout.connect;
2328
2329 if (!si->err_type)
2330 si->err_type = SI_ET_CONN_ERR;
2331
2332 /* only wait when we're retrying on the same server */
2333 if ((si->state == SI_ST_ASS ||
2334 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
Amaury Denoyelle05550a02021-06-17 15:14:49 +02002335 (s->be->srv_act <= 1)) && !reused) {
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002336 si->state = SI_ST_TAR;
2337 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
2338 }
2339 si->flags &= ~SI_FL_ERR;
2340 DBG_TRACE_STATE("retry a new connection", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2341 }
2342
2343 end:
2344 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2345}
2346
2347/* This function is called with (si->state == SI_ST_RDY) meaning that a
2348 * connection was attempted, that the file descriptor is already allocated,
2349 * and that it has succeeded. We must still check for errors and aborts.
2350 * Possible output states are SI_ST_EST (established), SI_ST_CER (error),
2351 * and SI_ST_DIS (abort). This only works with connection-based streams.
2352 * Timeouts and errors are *not* cleared.
2353 */
2354void back_handle_st_rdy(struct stream *s)
2355{
2356 struct stream_interface *si = &s->si[1];
2357 struct channel *req = &s->req;
2358 struct channel *rep = &s->res;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002359
2360 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2361 /* We know the connection at least succeeded, though it could have
2362 * since met an error for any other reason. At least it didn't time out
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07002363 * even though the timeout might have been reported right after success.
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002364 * We need to take care of various situations here :
2365 * - everything might be OK. We have to switch to established.
2366 * - an I/O error might have been reported after a successful transfer,
2367 * which is not retryable and needs to be logged correctly, and needs
2368 * established as well
2369 * - SI_ST_CON implies !CF_WROTE_DATA but not conversely as we could
2370 * have validated a connection with incoming data (e.g. TCP with a
2371 * banner protocol), or just a successful connect() probe.
2372 * - the client might have requested a connection abort, this needs to
2373 * be checked before we decide to retry anything.
2374 */
2375
2376 /* it's still possible to handle client aborts or connection retries
2377 * before any data were sent.
2378 */
2379 if (!(req->flags & CF_WROTE_DATA)) {
2380 /* client abort ? */
2381 if ((rep->flags & CF_SHUTW) ||
2382 ((req->flags & CF_SHUTW_NOW) &&
2383 (channel_is_empty(req) || (s->be->options & PR_O_ABRT_CLOSE)))) {
2384 /* give up */
2385 si->flags |= SI_FL_NOLINGER;
2386 si_shutw(si);
2387 si->err_type |= SI_ET_CONN_ABRT;
2388 if (s->srv_error)
2389 s->srv_error(s, si);
2390 DBG_TRACE_STATE("client abort during connection attempt", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2391 goto end;
2392 }
2393
2394 /* retryable error ? */
2395 if (si->flags & SI_FL_ERR) {
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002396 if (!si->err_type)
2397 si->err_type = SI_ET_CONN_ERR;
2398 si->state = SI_ST_CER;
2399 DBG_TRACE_STATE("connection failed, retry", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2400 goto end;
2401 }
2402 }
2403
2404 /* data were sent and/or we had no error, back_establish() will
2405 * now take over.
2406 */
2407 DBG_TRACE_STATE("connection established", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2408 si->err_type = SI_ET_NONE;
2409 si->state = SI_ST_EST;
2410
2411 end:
2412 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2413}
2414
Willy Tarreau4aac7db2014-05-16 11:48:10 +02002415/* sends a log message when a backend goes down, and also sets last
2416 * change date.
2417 */
2418void set_backend_down(struct proxy *be)
2419{
2420 be->last_change = now.tv_sec;
Willy Tarreau4781b152021-04-06 13:53:36 +02002421 _HA_ATOMIC_INC(&be->down_trans);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02002422
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01002423 if (!(global.mode & MODE_STARTING)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002424 ha_alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01002425 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
2426 }
Willy Tarreau4aac7db2014-05-16 11:48:10 +02002427}
2428
Willy Tarreau87b09662015-04-03 00:22:06 +02002429/* Apply RDP cookie persistence to the current stream. For this, the function
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002430 * tries to extract an RDP cookie from the request buffer, and look for the
2431 * matching server in the list. If the server is found, it is assigned to the
Willy Tarreau87b09662015-04-03 00:22:06 +02002432 * stream. This always returns 1, and the analyser removes itself from the
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002433 * list. Nothing is performed if a server was already assigned.
2434 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002435int tcp_persist_rdp_cookie(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002436{
2437 struct proxy *px = s->be;
2438 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02002439 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002440 struct server *srv = px->srv;
Willy Tarreau04276f32017-01-06 17:41:29 +01002441 uint16_t port;
2442 uint32_t addr;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002443 char *p;
2444
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002445 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002446
Willy Tarreaue7dff022015-04-03 01:14:29 +02002447 if (s->flags & SF_ASSIGNED)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002448 goto no_cookie;
2449
Willy Tarreau37406352012-04-23 16:16:37 +02002450 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002451
Willy Tarreaucadd8c92013-07-22 18:09:52 +02002452 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002453 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.u.str.data == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002454 goto no_cookie;
2455
Willy Tarreau04276f32017-01-06 17:41:29 +01002456 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return.
2457 * The cookie format is <ip> "." <port> where "ip" is the integer corresponding to the
2458 * server's IP address in network order, and "port" is the integer corresponding to the
2459 * server's port in network order. Comments please Emeric.
2460 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002461 addr = strtoul(smp.data.u.str.area, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002462 if (*p != '.')
2463 goto no_cookie;
2464 p++;
Willy Tarreau04276f32017-01-06 17:41:29 +01002465
2466 port = ntohs(strtoul(p, &p, 10));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002467 if (*p != '.')
2468 goto no_cookie;
2469
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002470 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002471 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02002472 if (srv->addr.ss_family == AF_INET &&
Willy Tarreau04276f32017-01-06 17:41:29 +01002473 port == srv->svc_port &&
2474 addr == ((struct sockaddr_in *)&srv->addr)->sin_addr.s_addr) {
Emeric Brun52a91d32017-08-31 14:41:55 +02002475 if ((srv->cur_state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002476 /* we found the server and it is usable */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002477 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002478 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002479 break;
2480 }
2481 }
2482 srv = srv->next;
2483 }
2484
2485no_cookie:
2486 req->analysers &= ~an_bit;
2487 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002488 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002489 return 1;
2490}
2491
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002492int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01002493 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002494 return px->down_time;
2495
2496 return now.tv_sec - px->last_change + px->down_time;
2497}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002498
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002499/*
2500 * This function returns a string containing the balancing
2501 * mode of the proxy in a format suitable for stats.
2502 */
2503
2504const char *backend_lb_algo_str(int algo) {
2505
2506 if (algo == BE_LB_ALGO_RR)
2507 return "roundrobin";
2508 else if (algo == BE_LB_ALGO_SRR)
2509 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01002510 else if (algo == BE_LB_ALGO_FAS)
2511 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002512 else if (algo == BE_LB_ALGO_LC)
2513 return "leastconn";
2514 else if (algo == BE_LB_ALGO_SH)
2515 return "source";
2516 else if (algo == BE_LB_ALGO_UH)
2517 return "uri";
2518 else if (algo == BE_LB_ALGO_PH)
2519 return "url_param";
2520 else if (algo == BE_LB_ALGO_HH)
2521 return "hdr";
2522 else if (algo == BE_LB_ALGO_RCH)
2523 return "rdp-cookie";
Willy Tarreaub3e111b2016-11-26 15:52:04 +01002524 else if (algo == BE_LB_ALGO_NONE)
2525 return "none";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002526 else
Willy Tarreaub3e111b2016-11-26 15:52:04 +01002527 return "unknown";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002528}
2529
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002530/* This function parses a "balance" statement in a backend section describing
2531 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002532 * returns -1, it will write an error message into the <err> buffer which will
2533 * automatically be allocated and must be passed as NULL. The trailing '\n'
2534 * will not be written. The function must be called with <args> pointing to the
2535 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002536 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002537int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002538{
2539 if (!*(args[0])) {
2540 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01002541 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2542 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002543 return 0;
2544 }
2545
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002546 if (strcmp(args[0], "roundrobin") == 0) {
Willy Tarreau31682232007-11-29 15:38:04 +01002547 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2548 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002549 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002550 else if (strcmp(args[0], "static-rr") == 0) {
Willy Tarreau9757a382009-10-03 12:56:50 +02002551 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2552 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
2553 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002554 else if (strcmp(args[0], "first") == 0) {
Willy Tarreauf09c6602012-02-13 17:12:08 +01002555 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2556 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
2557 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002558 else if (strcmp(args[0], "leastconn") == 0) {
Willy Tarreau51406232008-03-10 22:04:20 +01002559 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2560 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
2561 }
Willy Tarreau21c741a2019-01-14 18:14:27 +01002562 else if (!strncmp(args[0], "random", 6)) {
Willy Tarreau760e81d2018-05-03 07:20:40 +02002563 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2564 curproxy->lbprm.algo |= BE_LB_ALGO_RND;
Willy Tarreau21c741a2019-01-14 18:14:27 +01002565 curproxy->lbprm.arg_opt1 = 2;
2566
2567 if (*(args[0] + 6) == '(' && *(args[0] + 7) != ')') { /* number of draws */
2568 const char *beg;
2569 char *end;
2570
2571 beg = args[0] + 7;
2572 curproxy->lbprm.arg_opt1 = strtol(beg, &end, 0);
2573
2574 if (*end != ')') {
2575 if (!*end)
2576 memprintf(err, "random : missing closing parenthesis.");
2577 else
2578 memprintf(err, "random : unexpected character '%c' after argument.", *end);
2579 return -1;
2580 }
2581
2582 if (curproxy->lbprm.arg_opt1 < 1) {
2583 memprintf(err, "random : number of draws must be at least 1.");
2584 return -1;
2585 }
2586 }
Willy Tarreau760e81d2018-05-03 07:20:40 +02002587 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002588 else if (strcmp(args[0], "source") == 0) {
Willy Tarreau31682232007-11-29 15:38:04 +01002589 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2590 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002591 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002592 else if (strcmp(args[0], "uri") == 0) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002593 int arg = 1;
2594
Willy Tarreau31682232007-11-29 15:38:04 +01002595 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2596 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Willy Tarreau57a37412020-09-23 08:56:29 +02002597 curproxy->lbprm.arg_opt1 = 0; // "whole", "path-only"
Willy Tarreaua9a72492019-01-14 16:14:15 +01002598 curproxy->lbprm.arg_opt2 = 0; // "len"
2599 curproxy->lbprm.arg_opt3 = 0; // "depth"
Oskar Stolc8dc41842012-05-19 10:19:54 +01002600
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002601 while (*args[arg]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002602 if (strcmp(args[arg], "len") == 0) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002603 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002604 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002605 return -1;
2606 }
Willy Tarreaua9a72492019-01-14 16:14:15 +01002607 curproxy->lbprm.arg_opt2 = atoi(args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002608 arg += 2;
2609 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002610 else if (strcmp(args[arg], "depth") == 0) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002611 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002612 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002613 return -1;
2614 }
2615 /* hint: we store the position of the ending '/' (depth+1) so
2616 * that we avoid a comparison while computing the hash.
2617 */
Willy Tarreaua9a72492019-01-14 16:14:15 +01002618 curproxy->lbprm.arg_opt3 = atoi(args[arg+1]) + 1;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002619 arg += 2;
2620 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002621 else if (strcmp(args[arg], "whole") == 0) {
Willy Tarreau3d1119d2020-09-23 08:05:47 +02002622 curproxy->lbprm.arg_opt1 |= 1;
Oskar Stolc8dc41842012-05-19 10:19:54 +01002623 arg += 1;
2624 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002625 else if (strcmp(args[arg], "path-only") == 0) {
Willy Tarreau57a37412020-09-23 08:56:29 +02002626 curproxy->lbprm.arg_opt1 |= 2;
2627 arg += 1;
2628 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002629 else {
Willy Tarreau57a37412020-09-23 08:56:29 +02002630 memprintf(err, "%s only accepts parameters 'len', 'depth', 'path-only', and 'whole' (got '%s').", args[0], args[arg]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002631 return -1;
2632 }
2633 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002634 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002635 else if (strcmp(args[0], "url_param") == 0) {
Willy Tarreau01732802007-11-01 22:48:15 +01002636 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002637 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01002638 return -1;
2639 }
Willy Tarreau31682232007-11-29 15:38:04 +01002640 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2641 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002642
Willy Tarreau4c03d1c2019-01-14 15:23:54 +01002643 free(curproxy->lbprm.arg_str);
2644 curproxy->lbprm.arg_str = strdup(args[1]);
2645 curproxy->lbprm.arg_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002646 if (*args[2]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002647 if (strcmp(args[2], "check_post") != 0) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002648 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002649 return -1;
2650 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002651 }
Benoitaffb4812009-03-25 13:02:10 +01002652 }
2653 else if (!strncmp(args[0], "hdr(", 4)) {
2654 const char *beg, *end;
2655
2656 beg = args[0] + 4;
2657 end = strchr(beg, ')');
2658
2659 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002660 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01002661 return -1;
2662 }
2663
2664 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2665 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
2666
Willy Tarreau484ff072019-01-14 15:28:53 +01002667 free(curproxy->lbprm.arg_str);
2668 curproxy->lbprm.arg_len = end - beg;
2669 curproxy->lbprm.arg_str = my_strndup(beg, end - beg);
Willy Tarreau9fed8582019-01-14 16:04:54 +01002670 curproxy->lbprm.arg_opt1 = 0;
Benoitaffb4812009-03-25 13:02:10 +01002671
2672 if (*args[1]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002673 if (strcmp(args[1], "use_domain_only") != 0) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002674 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01002675 return -1;
2676 }
Willy Tarreau9fed8582019-01-14 16:04:54 +01002677 curproxy->lbprm.arg_opt1 = 1;
Benoitaffb4812009-03-25 13:02:10 +01002678 }
Emeric Brun736aa232009-06-30 17:56:00 +02002679 }
2680 else if (!strncmp(args[0], "rdp-cookie", 10)) {
2681 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2682 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
2683
2684 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
2685 const char *beg, *end;
2686
2687 beg = args[0] + 11;
2688 end = strchr(beg, ')');
2689
2690 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002691 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02002692 return -1;
2693 }
2694
Willy Tarreau484ff072019-01-14 15:28:53 +01002695 free(curproxy->lbprm.arg_str);
2696 curproxy->lbprm.arg_str = my_strndup(beg, end - beg);
2697 curproxy->lbprm.arg_len = end - beg;
Emeric Brun736aa232009-06-30 17:56:00 +02002698 }
2699 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
Willy Tarreau484ff072019-01-14 15:28:53 +01002700 free(curproxy->lbprm.arg_str);
2701 curproxy->lbprm.arg_str = strdup("mstshash");
2702 curproxy->lbprm.arg_len = strlen(curproxy->lbprm.arg_str);
Emeric Brun736aa232009-06-30 17:56:00 +02002703 }
2704 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002705 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02002706 return -1;
2707 }
Willy Tarreau01732802007-11-01 22:48:15 +01002708 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002709 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002710 memprintf(err, "only supports 'roundrobin', 'static-rr', 'leastconn', 'source', 'uri', 'url_param', 'hdr(name)' and 'rdp-cookie(name)' options.");
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002711 return -1;
2712 }
2713 return 0;
2714}
2715
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002716
2717/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01002718/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002719/************************************************************************/
2720
Willy Tarreau34db1082012-04-19 17:16:54 +02002721/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002722 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02002723 * undefined behaviour.
2724 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002725static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002726smp_fetch_nbsrv(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002727{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002728 struct proxy *px;
2729
Willy Tarreau37406352012-04-23 16:16:37 +02002730 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002731 smp->data.type = SMP_T_SINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02002732 px = args->data.prx;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002733
Nenad Merdanovic2754fbc2017-03-12 21:56:56 +01002734 smp->data.u.sint = be_usable_srv(px);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002735
2736 return 1;
2737}
2738
Willy Tarreau37406352012-04-23 16:16:37 +02002739/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002740 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02002741 * Accepts exactly 1 argument. Argument is a server, other types will lead to
2742 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002743 */
2744static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002745smp_fetch_srv_is_up(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002746{
Willy Tarreau24e32d82012-04-23 23:55:44 +02002747 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002748
Willy Tarreau37406352012-04-23 16:16:37 +02002749 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002750 smp->data.type = SMP_T_BOOL;
Emeric Brun52a91d32017-08-31 14:41:55 +02002751 if (!(srv->cur_admin & SRV_ADMF_MAINT) &&
2752 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->cur_state != SRV_ST_STOPPED)))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002753 smp->data.u.sint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002754 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002755 smp->data.u.sint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002756 return 1;
2757}
2758
Willy Tarreau34db1082012-04-19 17:16:54 +02002759/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002760 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02002761 * undefined behaviour.
2762 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002763static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002764smp_fetch_connslots(const struct arg *args, struct sample *smp, const char *kw, void *private)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002765{
2766 struct server *iterator;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002767
Willy Tarreau37406352012-04-23 16:16:37 +02002768 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002769 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002770 smp->data.u.sint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002771
Willy Tarreau24e32d82012-04-23 23:55:44 +02002772 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +02002773 if (iterator->cur_state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002774 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002775
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002776 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01002777 /* configuration is stupid */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002778 smp->data.u.sint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002779 return 1;
2780 }
2781
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002782 smp->data.u.sint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreau422aa072012-04-20 20:49:27 +02002783 + (iterator->maxqueue - iterator->nbpend);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002784 }
2785
2786 return 1;
2787}
2788
Willy Tarreaua5e37562011-12-16 17:06:15 +01002789/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01002790static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002791smp_fetch_be_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02002792{
Christopher Fauletd1b44642020-04-30 09:51:15 +02002793 struct proxy *px = NULL;
2794
2795 if (smp->strm)
2796 px = smp->strm->be;
Christopher Fauletf98e6262020-05-06 09:42:04 +02002797 else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd1b44642020-04-30 09:51:15 +02002798 px = __objt_check(smp->sess->origin)->proxy;
2799 if (!px)
Willy Tarreaube508f12016-03-10 11:47:01 +01002800 return 0;
2801
Willy Tarreauf853c462012-04-23 18:53:56 +02002802 smp->flags = SMP_F_VOL_TXN;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002803 smp->data.type = SMP_T_SINT;
Christopher Fauletd1b44642020-04-30 09:51:15 +02002804 smp->data.u.sint = px->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01002805 return 1;
2806}
2807
Marcin Deranekd2471c22016-12-12 14:08:05 +01002808/* set string to the name of the backend */
2809static int
2810smp_fetch_be_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
2811{
Christopher Fauletd1b44642020-04-30 09:51:15 +02002812 struct proxy *px = NULL;
2813
2814 if (smp->strm)
2815 px = smp->strm->be;
Christopher Fauletf98e6262020-05-06 09:42:04 +02002816 else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd1b44642020-04-30 09:51:15 +02002817 px = __objt_check(smp->sess->origin)->proxy;
2818 if (!px)
Marcin Deranekd2471c22016-12-12 14:08:05 +01002819 return 0;
2820
Christopher Fauletd1b44642020-04-30 09:51:15 +02002821 smp->data.u.str.area = (char *)px->id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002822 if (!smp->data.u.str.area)
Marcin Deranekd2471c22016-12-12 14:08:05 +01002823 return 0;
2824
2825 smp->data.type = SMP_T_STR;
2826 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002827 smp->data.u.str.data = strlen(smp->data.u.str.area);
Marcin Deranekd2471c22016-12-12 14:08:05 +01002828
2829 return 1;
2830}
2831
Willy Tarreaua5e37562011-12-16 17:06:15 +01002832/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01002833static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002834smp_fetch_srv_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02002835{
Christopher Fauletd1b44642020-04-30 09:51:15 +02002836 struct server *srv = NULL;
Willy Tarreaube508f12016-03-10 11:47:01 +01002837
Christopher Fauletd1b44642020-04-30 09:51:15 +02002838 if (smp->strm)
2839 srv = objt_server(smp->strm->target);
Christopher Fauletf98e6262020-05-06 09:42:04 +02002840 else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd1b44642020-04-30 09:51:15 +02002841 srv = __objt_check(smp->sess->origin)->server;
2842 if (!srv)
Willy Tarreau17af4192011-02-23 14:27:06 +01002843 return 0;
2844
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002845 smp->data.type = SMP_T_SINT;
Christopher Fauletd1b44642020-04-30 09:51:15 +02002846 smp->data.u.sint = srv->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01002847
2848 return 1;
2849}
2850
vkill1dfd1652019-10-30 16:58:14 +08002851/* set string to the name of the server */
2852static int
2853smp_fetch_srv_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
2854{
Christopher Fauletd1b44642020-04-30 09:51:15 +02002855 struct server *srv = NULL;
vkill1dfd1652019-10-30 16:58:14 +08002856
Christopher Fauletd1b44642020-04-30 09:51:15 +02002857 if (smp->strm)
2858 srv = objt_server(smp->strm->target);
Christopher Fauletf98e6262020-05-06 09:42:04 +02002859 else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd1b44642020-04-30 09:51:15 +02002860 srv = __objt_check(smp->sess->origin)->server;
2861 if (!srv)
vkill1dfd1652019-10-30 16:58:14 +08002862 return 0;
2863
Christopher Fauletd1b44642020-04-30 09:51:15 +02002864 smp->data.u.str.area = srv->id;
vkill1dfd1652019-10-30 16:58:14 +08002865 if (!smp->data.u.str.area)
2866 return 0;
2867
2868 smp->data.type = SMP_T_STR;
2869 smp->data.u.str.data = strlen(smp->data.u.str.area);
2870
2871 return 1;
2872}
2873
Willy Tarreau34db1082012-04-19 17:16:54 +02002874/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002875 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02002876 * undefined behaviour.
2877 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01002878static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002879smp_fetch_be_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01002880{
Willy Tarreau37406352012-04-23 16:16:37 +02002881 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002882 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002883 smp->data.u.sint = read_freq_ctr(&args->data.prx->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01002884 return 1;
2885}
2886
Willy Tarreau34db1082012-04-19 17:16:54 +02002887/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002888 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02002889 * undefined behaviour.
2890 */
Willy Tarreaua36af912009-10-10 12:02:45 +02002891static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002892smp_fetch_be_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02002893{
Willy Tarreau37406352012-04-23 16:16:37 +02002894 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002895 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002896 smp->data.u.sint = args->data.prx->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02002897 return 1;
2898}
2899
Patrick Hemmer4cdf3ab2018-06-14 17:10:27 -04002900/* set temp integer to the number of available connections across available
2901 * servers on the backend.
2902 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
2903 * undefined behaviour.
2904 */
2905static int
2906smp_fetch_be_conn_free(const struct arg *args, struct sample *smp, const char *kw, void *private)
2907{
2908 struct server *iterator;
2909 struct proxy *px;
2910 unsigned int maxconn;
2911
2912 smp->flags = SMP_F_VOL_TEST;
2913 smp->data.type = SMP_T_SINT;
2914 smp->data.u.sint = 0;
2915
2916 for (iterator = args->data.prx->srv; iterator; iterator = iterator->next) {
2917 if (iterator->cur_state == SRV_ST_STOPPED)
2918 continue;
2919
2920 px = iterator->proxy;
2921 if (!srv_currently_usable(iterator) ||
2922 ((iterator->flags & SRV_F_BACKUP) &&
2923 (px->srv_act || (iterator != px->lbprm.fbck && !(px->options & PR_O_USE_ALL_BK)))))
2924 continue;
2925
2926 if (iterator->maxconn == 0) {
2927 /* one active server is unlimited, return -1 */
2928 smp->data.u.sint = -1;
2929 return 1;
2930 }
2931
2932 maxconn = srv_dynamic_maxconn(iterator);
2933 if (maxconn > iterator->cur_sess)
2934 smp->data.u.sint += maxconn - iterator->cur_sess;
2935 }
2936
2937 return 1;
2938}
2939
Willy Tarreau34db1082012-04-19 17:16:54 +02002940/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002941 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02002942 * undefined behaviour.
2943 */
Willy Tarreaua36af912009-10-10 12:02:45 +02002944static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002945smp_fetch_queue_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02002946{
Willy Tarreau37406352012-04-23 16:16:37 +02002947 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002948 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002949 smp->data.u.sint = args->data.prx->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02002950 return 1;
2951}
2952
Willy Tarreaua5e37562011-12-16 17:06:15 +01002953/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02002954 * by the number of running servers and rounded up. If there is no running
2955 * server, we return twice the total, just as if we had half a running server.
2956 * This is more or less correct anyway, since we expect the last server to come
2957 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002958 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02002959 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02002960 */
2961static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002962smp_fetch_avg_queue_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02002963{
2964 int nbsrv;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002965 struct proxy *px;
Willy Tarreaua36af912009-10-10 12:02:45 +02002966
Willy Tarreau37406352012-04-23 16:16:37 +02002967 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002968 smp->data.type = SMP_T_SINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02002969 px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02002970
Nenad Merdanovic2754fbc2017-03-12 21:56:56 +01002971 nbsrv = be_usable_srv(px);
Willy Tarreaua36af912009-10-10 12:02:45 +02002972
2973 if (nbsrv > 0)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002974 smp->data.u.sint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02002975 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002976 smp->data.u.sint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02002977
2978 return 1;
2979}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002980
Willy Tarreau34db1082012-04-19 17:16:54 +02002981/* set temp integer to the number of concurrent connections on the server in the backend.
2982 * Accepts exactly 1 argument. Argument is a server, other types will lead to
2983 * undefined behaviour.
2984 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02002985static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002986smp_fetch_srv_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02002987{
Willy Tarreauf853c462012-04-23 18:53:56 +02002988 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002989 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002990 smp->data.u.sint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02002991 return 1;
2992}
2993
Patrick Hemmer155e93e2018-06-14 18:01:35 -04002994/* set temp integer to the number of available connections on the server in the backend.
2995 * Accepts exactly 1 argument. Argument is a server, other types will lead to
2996 * undefined behaviour.
2997 */
2998static int
2999smp_fetch_srv_conn_free(const struct arg *args, struct sample *smp, const char *kw, void *private)
3000{
3001 unsigned int maxconn;
3002
3003 smp->flags = SMP_F_VOL_TEST;
3004 smp->data.type = SMP_T_SINT;
3005
3006 if (args->data.srv->maxconn == 0) {
3007 /* one active server is unlimited, return -1 */
3008 smp->data.u.sint = -1;
3009 return 1;
3010 }
3011
3012 maxconn = srv_dynamic_maxconn(args->data.srv);
3013 if (maxconn > args->data.srv->cur_sess)
3014 smp->data.u.sint = maxconn - args->data.srv->cur_sess;
3015 else
3016 smp->data.u.sint = 0;
3017
3018 return 1;
3019}
3020
Willy Tarreauff2b7af2017-10-13 11:46:26 +02003021/* set temp integer to the number of connections pending in the server's queue.
3022 * Accepts exactly 1 argument. Argument is a server, other types will lead to
3023 * undefined behaviour.
3024 */
3025static int
3026smp_fetch_srv_queue(const struct arg *args, struct sample *smp, const char *kw, void *private)
3027{
3028 smp->flags = SMP_F_VOL_TEST;
3029 smp->data.type = SMP_T_SINT;
3030 smp->data.u.sint = args->data.srv->nbpend;
3031 return 1;
3032}
3033
Tait Clarridge7896d522012-12-05 21:39:31 -05003034/* set temp integer to the number of enabled servers on the proxy.
3035 * Accepts exactly 1 argument. Argument is a server, other types will lead to
3036 * undefined behaviour.
3037 */
3038static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003039smp_fetch_srv_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Tait Clarridge7896d522012-12-05 21:39:31 -05003040{
3041 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003042 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003043 smp->data.u.sint = read_freq_ctr(&args->data.srv->sess_per_sec);
Tait Clarridge7896d522012-12-05 21:39:31 -05003044 return 1;
3045}
3046
Christopher Faulet1bea8652020-07-10 16:03:45 +02003047/* set temp integer to the server weight.
3048 * Accepts exactly 1 argument. Argument is a server, other types will lead to
3049 * undefined behaviour.
3050 */
3051static int
3052smp_fetch_srv_weight(const struct arg *args, struct sample *smp, const char *kw, void *private)
3053{
3054 struct server *srv = args->data.srv;
3055 struct proxy *px = srv->proxy;
3056
3057 smp->flags = SMP_F_VOL_TEST;
3058 smp->data.type = SMP_T_SINT;
3059 smp->data.u.sint = (srv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv;
3060 return 1;
3061}
3062
3063/* set temp integer to the server initial weight.
3064 * Accepts exactly 1 argument. Argument is a server, other types will lead to
3065 * undefined behaviour.
3066 */
3067static int
3068smp_fetch_srv_iweight(const struct arg *args, struct sample *smp, const char *kw, void *private)
3069{
3070 smp->flags = SMP_F_VOL_TEST;
3071 smp->data.type = SMP_T_SINT;
3072 smp->data.u.sint = args->data.srv->iweight;
3073 return 1;
3074}
3075
3076/* set temp integer to the server user-specified weight.
3077 * Accepts exactly 1 argument. Argument is a server, other types will lead to
3078 * undefined behaviour.
3079 */
3080static int
3081smp_fetch_srv_uweight(const struct arg *args, struct sample *smp, const char *kw, void *private)
3082{
3083 smp->flags = SMP_F_VOL_TEST;
3084 smp->data.type = SMP_T_SINT;
3085 smp->data.u.sint = args->data.srv->uweight;
3086 return 1;
3087}
3088
Amaury Denoyelled91d7792020-12-10 13:43:56 +01003089static int
3090smp_fetch_be_server_timeout(const struct arg *args, struct sample *smp, const char *km, void *private)
3091{
3092 struct proxy *px = NULL;
3093
3094 if (smp->strm)
3095 px = smp->strm->be;
3096 else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
3097 px = __objt_check(smp->sess->origin)->proxy;
3098 if (!px)
3099 return 0;
3100
3101 smp->flags = SMP_F_VOL_TXN;
3102 smp->data.type = SMP_T_SINT;
3103 smp->data.u.sint = TICKS_TO_MS(px->timeout.server);
3104 return 1;
3105}
3106
3107static int
3108smp_fetch_be_tunnel_timeout(const struct arg *args, struct sample *smp, const char *km, void *private)
3109{
3110 struct proxy *px = NULL;
3111
3112 if (smp->strm)
3113 px = smp->strm->be;
3114 else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
3115 px = __objt_check(smp->sess->origin)->proxy;
3116 if (!px)
3117 return 0;
3118
3119 smp->flags = SMP_F_VOL_TXN;
3120 smp->data.type = SMP_T_SINT;
3121 smp->data.u.sint = TICKS_TO_MS(px->timeout.tunnel);
3122 return 1;
3123}
3124
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01003125static int sample_conv_nbsrv(const struct arg *args, struct sample *smp, void *private)
3126{
3127
3128 struct proxy *px;
3129
3130 if (!smp_make_safe(smp))
3131 return 0;
3132
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003133 px = proxy_find_by_name(smp->data.u.str.area, PR_CAP_BE, 0);
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01003134 if (!px)
3135 return 0;
3136
3137 smp->data.type = SMP_T_SINT;
3138 smp->data.u.sint = be_usable_srv(px);
3139
3140 return 1;
3141}
3142
Nenad Merdanovic177adc92019-08-27 01:58:13 +02003143static int
3144sample_conv_srv_queue(const struct arg *args, struct sample *smp, void *private)
3145{
3146 struct proxy *px;
3147 struct server *srv;
3148 char *bksep;
3149
3150 if (!smp_make_safe(smp))
3151 return 0;
3152
3153 bksep = strchr(smp->data.u.str.area, '/');
3154
3155 if (bksep) {
3156 *bksep = '\0';
3157 px = proxy_find_by_name(smp->data.u.str.area, PR_CAP_BE, 0);
3158 if (!px)
3159 return 0;
3160 smp->data.u.str.area = bksep + 1;
3161 } else {
3162 if (!(smp->px->cap & PR_CAP_BE))
3163 return 0;
3164 px = smp->px;
3165 }
3166
3167 srv = server_find_by_name(px, smp->data.u.str.area);
3168 if (!srv)
3169 return 0;
3170
3171 smp->data.type = SMP_T_SINT;
3172 smp->data.u.sint = srv->nbpend;
3173 return 1;
3174}
Willy Tarreau1a7eca12013-01-07 22:38:03 +01003175
3176/* Note: must not be declared <const> as its list will be overwritten.
3177 * Please take care of keeping this list alphabetically sorted.
3178 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003179static struct sample_fetch_kw_list smp_kws = {ILH, {
Amaury Denoyelled91d7792020-12-10 13:43:56 +01003180 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3181 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3182 { "be_conn_free", smp_fetch_be_conn_free, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3183 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_SINT, SMP_USE_BKEND, },
3184 { "be_name", smp_fetch_be_name, 0, NULL, SMP_T_STR, SMP_USE_BKEND, },
3185 { "be_server_timeout", smp_fetch_be_server_timeout, 0, NULL, SMP_T_SINT, SMP_USE_BKEND, },
3186 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3187 { "be_tunnel_timeout", smp_fetch_be_tunnel_timeout, 0, NULL, SMP_T_SINT, SMP_USE_BKEND, },
3188 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3189 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3190 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3191 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3192 { "srv_conn_free", smp_fetch_srv_conn_free, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3193 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_SINT, SMP_USE_SERVR, },
3194 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3195 { "srv_name", smp_fetch_srv_name, 0, NULL, SMP_T_STR, SMP_USE_SERVR, },
3196 { "srv_queue", smp_fetch_srv_queue, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3197 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3198 { "srv_weight", smp_fetch_srv_weight, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3199 { "srv_iweight", smp_fetch_srv_iweight, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3200 { "srv_uweight", smp_fetch_srv_uweight, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau1a7eca12013-01-07 22:38:03 +01003201 { /* END */ },
3202}};
3203
Willy Tarreau0108d902018-11-25 19:14:37 +01003204INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3205
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01003206/* Note: must not be declared <const> as its list will be overwritten */
3207static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Nenad Merdanovic177adc92019-08-27 01:58:13 +02003208 { "nbsrv", sample_conv_nbsrv, 0, NULL, SMP_T_STR, SMP_T_SINT },
3209 { "srv_queue", sample_conv_srv_queue, 0, NULL, SMP_T_STR, SMP_T_SINT },
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01003210 { /* END */ },
3211}};
3212
Willy Tarreau0108d902018-11-25 19:14:37 +01003213INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
Willy Tarreau1a7eca12013-01-07 22:38:03 +01003214
Willy Tarreau61612d42012-04-19 18:42:05 +02003215/* Note: must not be declared <const> as its list will be overwritten.
3216 * Please take care of keeping this list alphabetically sorted.
3217 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003218static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01003219 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01003220}};
3221
Willy Tarreau0108d902018-11-25 19:14:37 +01003222INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01003223
Willy Tarreaubaaee002006-06-26 02:48:02 +02003224/*
3225 * Local variables:
3226 * c-indent-level: 8
3227 * c-basic-offset: 8
3228 * End:
3229 */