blob: 5429e352af76b69a370b9bc86e223788837ee204 [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 Tarreau63617db2021-10-06 18:23:40 +020022#include <import/ebmbtree.h>
23
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020024#include <haproxy/api.h>
Willy Tarreau5d9ddc52021-10-06 19:54:09 +020025#include <haproxy/acl.h>
26#include <haproxy/activity.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/arg.h>
28#include <haproxy/backend.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020029#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020030#include <haproxy/check.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010031#include <haproxy/conn_stream.h>
32#include <haproxy/cs_utils.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020033#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/global.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020035#include <haproxy/hash.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020036#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020037#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020038#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020039#include <haproxy/htx.h>
Willy Tarreaufbe8da32020-06-04 14:34:27 +020040#include <haproxy/lb_chash.h>
Willy Tarreaub5fc3bf2020-06-04 14:37:38 +020041#include <haproxy/lb_fas.h>
Willy Tarreau02549412020-06-04 14:41:04 +020042#include <haproxy/lb_fwlc.h>
Willy Tarreau546ba422020-06-04 14:45:03 +020043#include <haproxy/lb_fwrr.h>
Willy Tarreau28671592020-06-04 20:22:59 +020044#include <haproxy/lb_map.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020045#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/namespace.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020047#include <haproxy/obj_type.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020048#include <haproxy/payload.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/proto_tcp.h>
50#include <haproxy/protocol.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020051#include <haproxy/proxy.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020052#include <haproxy/queue.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020053#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020054#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020055#include <haproxy/session.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020056#include <haproxy/ssl_sock.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020057#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020058#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020059#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020060#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020061#include <haproxy/time.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020062#include <haproxy/trace.h>
Willy Tarreau732eac42015-07-09 11:40:25 +020063
Christopher Fauleteea8fc72019-11-05 16:18:10 +010064#define TRACE_SOURCE &trace_strm
65
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050066int be_lastsession(const struct proxy *be)
67{
68 if (be->be_counters.last_sess)
69 return now.tv_sec - be->be_counters.last_sess;
70
71 return -1;
72}
73
Bhaskar98634f02013-10-29 23:30:51 -040074/* helper function to invoke the correct hash method */
Dan Dubovikbd57a9f2014-07-08 08:51:03 -070075static unsigned int gen_hash(const struct proxy* px, const char* key, unsigned long len)
Bhaskar98634f02013-10-29 23:30:51 -040076{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -070077 unsigned int hash;
Bhaskar98634f02013-10-29 23:30:51 -040078
79 switch (px->lbprm.algo & BE_LB_HASH_FUNC) {
80 case BE_LB_HFCN_DJB2:
81 hash = hash_djb2(key, len);
82 break;
Willy Tarreaua0f42712013-11-14 14:30:35 +010083 case BE_LB_HFCN_WT6:
84 hash = hash_wt6(key, len);
85 break;
Willy Tarreau324f07f2015-01-20 19:44:50 +010086 case BE_LB_HFCN_CRC32:
87 hash = hash_crc32(key, len);
88 break;
Bhaskar98634f02013-10-29 23:30:51 -040089 case BE_LB_HFCN_SDBM:
90 /* this is the default hash function */
91 default:
92 hash = hash_sdbm(key, len);
93 break;
94 }
95
96 return hash;
97}
98
Willy Tarreaubaaee002006-06-26 02:48:02 +020099/*
100 * This function recounts the number of usable active and backup servers for
101 * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck.
Willy Tarreaub625a082007-11-26 01:15:43 +0100102 * This function also recomputes the total active and backup weights. However,
Willy Tarreauf4cca452008-03-08 21:42:54 +0100103 * it does not update tot_weight nor tot_used. Use update_backend_weight() for
Willy Tarreaub625a082007-11-26 01:15:43 +0100104 * this.
Emeric Brun52a91d32017-08-31 14:41:55 +0200105 * This functions is designed to be called before server's weight and state
106 * commit so it uses 'next' weight and states values.
Christopher Faulet5b517552017-06-09 14:17:53 +0200107 *
108 * threads: this is the caller responsibility to lock data. For now, this
109 * function is called from lb modules, so it should be ok. But if you need to
110 * call it from another place, be careful (and update this comment).
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200112void recount_servers(struct proxy *px)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113{
114 struct server *srv;
115
Willy Tarreau20697042007-11-15 23:26:18 +0100116 px->srv_act = px->srv_bck = 0;
117 px->lbprm.tot_wact = px->lbprm.tot_wbck = 0;
Willy Tarreaub625a082007-11-26 01:15:43 +0100118 px->lbprm.fbck = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119 for (srv = px->srv; srv != NULL; srv = srv->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200120 if (!srv_willbe_usable(srv))
Willy Tarreaub625a082007-11-26 01:15:43 +0100121 continue;
122
Willy Tarreauc93cd162014-05-13 15:54:22 +0200123 if (srv->flags & SRV_F_BACKUP) {
Willy Tarreaub625a082007-11-26 01:15:43 +0100124 if (!px->srv_bck &&
Willy Tarreauf4cca452008-03-08 21:42:54 +0100125 !(px->options & PR_O_USE_ALL_BK))
Willy Tarreaub625a082007-11-26 01:15:43 +0100126 px->lbprm.fbck = srv;
127 px->srv_bck++;
Andrew Rodland13d5ebb2016-10-25 12:49:45 -0400128 srv->cumulative_weight = px->lbprm.tot_wbck;
Emeric Brun52a91d32017-08-31 14:41:55 +0200129 px->lbprm.tot_wbck += srv->next_eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100130 } else {
131 px->srv_act++;
Andrew Rodland13d5ebb2016-10-25 12:49:45 -0400132 srv->cumulative_weight = px->lbprm.tot_wact;
Emeric Brun52a91d32017-08-31 14:41:55 +0200133 px->lbprm.tot_wact += srv->next_eweight;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134 }
135 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100136}
Willy Tarreau20697042007-11-15 23:26:18 +0100137
Willy Tarreaub625a082007-11-26 01:15:43 +0100138/* This function simply updates the backend's tot_weight and tot_used values
139 * after servers weights have been updated. It is designed to be used after
140 * recount_servers() or equivalent.
Christopher Faulet5b517552017-06-09 14:17:53 +0200141 *
142 * threads: this is the caller responsibility to lock data. For now, this
143 * function is called from lb modules, so it should be ok. But if you need to
144 * call it from another place, be careful (and update this comment).
Willy Tarreaub625a082007-11-26 01:15:43 +0100145 */
Willy Tarreauc5d9c802009-10-01 09:17:05 +0200146void update_backend_weight(struct proxy *px)
Willy Tarreaub625a082007-11-26 01:15:43 +0100147{
Willy Tarreau20697042007-11-15 23:26:18 +0100148 if (px->srv_act) {
149 px->lbprm.tot_weight = px->lbprm.tot_wact;
150 px->lbprm.tot_used = px->srv_act;
151 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100152 else if (px->lbprm.fbck) {
153 /* use only the first backup server */
Emeric Brun52a91d32017-08-31 14:41:55 +0200154 px->lbprm.tot_weight = px->lbprm.fbck->next_eweight;
Willy Tarreaub625a082007-11-26 01:15:43 +0100155 px->lbprm.tot_used = 1;
Willy Tarreau20697042007-11-15 23:26:18 +0100156 }
157 else {
Willy Tarreaub625a082007-11-26 01:15:43 +0100158 px->lbprm.tot_weight = px->lbprm.tot_wbck;
159 px->lbprm.tot_used = px->srv_bck;
Willy Tarreau20697042007-11-15 23:26:18 +0100160 }
Willy Tarreaub625a082007-11-26 01:15:43 +0100161}
162
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200163/*
164 * This function tries to find a running server for the proxy <px> following
165 * the source hash method. Depending on the number of active/backup servers,
166 * it will either look for active servers, or for backup servers.
167 * If any server is found, it will be returned. If no valid server is found,
168 * NULL is returned.
169 */
Willy Tarreau59884a62019-01-02 14:48:31 +0100170static struct server *get_server_sh(struct proxy *px, const char *addr, int len, const struct server *avoid)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200171{
172 unsigned int h, l;
173
174 if (px->lbprm.tot_weight == 0)
175 return NULL;
176
177 l = h = 0;
178
179 /* note: we won't hash if there's only one server left */
180 if (px->lbprm.tot_used == 1)
181 goto hash_done;
182
183 while ((l + sizeof (int)) <= len) {
184 h ^= ntohl(*(unsigned int *)(&addr[l]));
185 l += sizeof (int);
186 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500187 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100188 h = full_hash(h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200189 hash_done:
Willy Tarreau6c30be52019-01-14 17:07:39 +0100190 if ((px->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau59884a62019-01-02 14:48:31 +0100191 return chash_get_server_hash(px, h, avoid);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200192 else
193 return map_get_server_hash(px, h);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200194}
195
196/*
197 * This function tries to find a running server for the proxy <px> following
198 * the URI hash method. In order to optimize cache hits, the hash computation
199 * ends at the question mark. Depending on the number of active/backup servers,
200 * it will either look for active servers, or for backup servers.
201 * If any server is found, it will be returned. If no valid server is found,
Willy Tarreaua9a72492019-01-14 16:14:15 +0100202 * NULL is returned. The lbprm.arg_opt{1,2,3} values correspond respectively to
Willy Tarreau3d1119d2020-09-23 08:05:47 +0200203 * the "whole" optional argument (boolean, bit0), the "len" argument (numeric)
204 * and the "depth" argument (numeric).
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200205 *
206 * This code was contributed by Guillaume Dallaire, who also selected this hash
207 * algorithm out of a tens because it gave him the best results.
208 *
209 */
Willy Tarreau59884a62019-01-02 14:48:31 +0100210static struct server *get_server_uh(struct proxy *px, char *uri, int uri_len, const struct server *avoid)
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200211{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700212 unsigned int hash = 0;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200213 int c;
214 int slashes = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400215 const char *start, *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200216
217 if (px->lbprm.tot_weight == 0)
218 return NULL;
219
220 /* note: we won't hash if there's only one server left */
221 if (px->lbprm.tot_used == 1)
222 goto hash_done;
223
Willy Tarreaua9a72492019-01-14 16:14:15 +0100224 if (px->lbprm.arg_opt2) // "len"
225 uri_len = MIN(uri_len, px->lbprm.arg_opt2);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200226
Bhaskar98634f02013-10-29 23:30:51 -0400227 start = end = uri;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200228 while (uri_len--) {
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200229 c = *end;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200230 if (c == '/') {
231 slashes++;
Willy Tarreaua9a72492019-01-14 16:14:15 +0100232 if (slashes == px->lbprm.arg_opt3) /* depth+1 */
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200233 break;
234 }
Willy Tarreau3d1119d2020-09-23 08:05:47 +0200235 else if (c == '?' && !(px->lbprm.arg_opt1 & 1)) // "whole"
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200236 break;
Willy Tarreaufad4ffc2014-10-17 12:11:50 +0200237 end++;
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200238 }
Bhaskar98634f02013-10-29 23:30:51 -0400239
240 hash = gen_hash(px, start, (end - start));
241
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500242 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100243 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200244 hash_done:
Willy Tarreau6c30be52019-01-14 17:07:39 +0100245 if ((px->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau59884a62019-01-02 14:48:31 +0100246 return chash_get_server_hash(px, hash, avoid);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200247 else
248 return map_get_server_hash(px, hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200249}
250
Christopher Faulet5b517552017-06-09 14:17:53 +0200251/*
Willy Tarreau01732802007-11-01 22:48:15 +0100252 * This function tries to find a running server for the proxy <px> following
253 * the URL parameter hash method. It looks for a specific parameter in the
254 * URL and hashes it to compute the server ID. This is useful to optimize
255 * performance by avoiding bounces between servers in contexts where sessions
256 * are shared but cookies are not usable. If the parameter is not found, NULL
257 * is returned. If any server is found, it will be returned. If no valid server
258 * is found, NULL is returned.
Willy Tarreau01732802007-11-01 22:48:15 +0100259 */
Willy Tarreau59884a62019-01-02 14:48:31 +0100260static 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 +0100261{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700262 unsigned int hash = 0;
Bhaskar98634f02013-10-29 23:30:51 -0400263 const char *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200264 const char *p;
265 const char *params;
Willy Tarreau01732802007-11-01 22:48:15 +0100266 int plen;
267
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200268 /* when tot_weight is 0 then so is srv_count */
Willy Tarreau20697042007-11-15 23:26:18 +0100269 if (px->lbprm.tot_weight == 0)
Willy Tarreau01732802007-11-01 22:48:15 +0100270 return NULL;
271
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200272 if ((p = memchr(uri, '?', uri_len)) == NULL)
273 return NULL;
274
Willy Tarreau01732802007-11-01 22:48:15 +0100275 p++;
276
277 uri_len -= (p - uri);
Willy Tarreau4c03d1c2019-01-14 15:23:54 +0100278 plen = px->lbprm.arg_len;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200279 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100280
281 while (uri_len > plen) {
282 /* Look for the parameter name followed by an equal symbol */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200283 if (params[plen] == '=') {
Willy Tarreau4c03d1c2019-01-14 15:23:54 +0100284 if (memcmp(params, px->lbprm.arg_str, plen) == 0) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200285 /* OK, we have the parameter here at <params>, and
Willy Tarreau01732802007-11-01 22:48:15 +0100286 * the value after the equal sign, at <p>
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200287 * skip the equal symbol
Willy Tarreau01732802007-11-01 22:48:15 +0100288 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200289 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400290 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200291 uri_len -= plen + 1;
292
Bhaskar98634f02013-10-29 23:30:51 -0400293 while (uri_len && *end != '&') {
Willy Tarreau01732802007-11-01 22:48:15 +0100294 uri_len--;
Bhaskar98634f02013-10-29 23:30:51 -0400295 end++;
Willy Tarreau01732802007-11-01 22:48:15 +0100296 }
Bhaskar98634f02013-10-29 23:30:51 -0400297 hash = gen_hash(px, start, (end - start));
298
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500299 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100300 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400301
Willy Tarreau6c30be52019-01-14 17:07:39 +0100302 if ((px->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau59884a62019-01-02 14:48:31 +0100303 return chash_get_server_hash(px, hash, avoid);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200304 else
305 return map_get_server_hash(px, hash);
Willy Tarreau01732802007-11-01 22:48:15 +0100306 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200307 }
308 /* skip to next parameter */
309 p = memchr(params, '&', uri_len);
310 if (!p)
311 return NULL;
312 p++;
313 uri_len -= (p - params);
314 params = p;
315 }
316 return NULL;
317}
318
319/*
320 * this does the same as the previous server_ph, but check the body contents
321 */
Willy Tarreau59884a62019-01-02 14:48:31 +0100322static struct server *get_server_ph_post(struct stream *s, const struct server *avoid)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200323{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700324 unsigned int hash = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100325 struct channel *req = &s->req;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200326 struct proxy *px = s->be;
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200327 struct htx *htx = htxbuf(&req->buf);
328 struct htx_blk *blk;
Willy Tarreau4c03d1c2019-01-14 15:23:54 +0100329 unsigned int plen = px->lbprm.arg_len;
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100330 unsigned long len;
331 const char *params, *p, *start, *end;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200332
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100333 if (px->lbprm.tot_weight == 0)
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200334 return NULL;
335
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200336 p = params = NULL;
337 len = 0;
338 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
339 enum htx_blk_type type = htx_get_blk_type(blk);
340 struct ist v;
Willy Tarreauf69d4ff2015-05-02 00:05:47 +0200341
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200342 if (type != HTX_BLK_DATA)
343 continue;
344 v = htx_get_blk_value(htx, blk);
345 p = params = v.ptr;
346 len = v.len;
347 break;
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100348 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200349
350 while (len > plen) {
351 /* Look for the parameter name followed by an equal symbol */
352 if (params[plen] == '=') {
Willy Tarreau4c03d1c2019-01-14 15:23:54 +0100353 if (memcmp(params, px->lbprm.arg_str, plen) == 0) {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200354 /* OK, we have the parameter here at <params>, and
355 * the value after the equal sign, at <p>
356 * skip the equal symbol
357 */
358 p += plen + 1;
Bhaskar98634f02013-10-29 23:30:51 -0400359 start = end = p;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200360 len -= plen + 1;
361
Bhaskar98634f02013-10-29 23:30:51 -0400362 while (len && *end != '&') {
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200363 if (unlikely(!HTTP_IS_TOKEN(*p))) {
Willy Tarreau157dd632009-12-06 19:18:09 +0100364 /* if in a POST, body must be URI encoded or it's not a URI.
Bhaskar98634f02013-10-29 23:30:51 -0400365 * Do not interpret any possible binary data as a parameter.
Willy Tarreau157dd632009-12-06 19:18:09 +0100366 */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200367 if (likely(HTTP_IS_LWS(*p))) /* eol, uncertain uri len */
368 break;
369 return NULL; /* oh, no; this is not uri-encoded.
370 * This body does not contain parameters.
371 */
372 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200373 len--;
Bhaskar98634f02013-10-29 23:30:51 -0400374 end++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200375 /* should we break if vlen exceeds limit? */
376 }
Bhaskar98634f02013-10-29 23:30:51 -0400377 hash = gen_hash(px, start, (end - start));
378
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500379 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100380 hash = full_hash(hash);
Bhaskar98634f02013-10-29 23:30:51 -0400381
Willy Tarreau6c30be52019-01-14 17:07:39 +0100382 if ((px->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau59884a62019-01-02 14:48:31 +0100383 return chash_get_server_hash(px, hash, avoid);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200384 else
385 return map_get_server_hash(px, hash);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200386 }
387 }
Willy Tarreau01732802007-11-01 22:48:15 +0100388 /* skip to next parameter */
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200389 p = memchr(params, '&', len);
Willy Tarreau01732802007-11-01 22:48:15 +0100390 if (!p)
391 return NULL;
392 p++;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200393 len -= (p - params);
394 params = p;
Willy Tarreau01732802007-11-01 22:48:15 +0100395 }
396 return NULL;
397}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200398
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200399
Willy Tarreaubaaee002006-06-26 02:48:02 +0200400/*
Benoitaffb4812009-03-25 13:02:10 +0100401 * This function tries to find a running server for the proxy <px> following
402 * the Header parameter hash method. It looks for a specific parameter in the
403 * URL and hashes it to compute the server ID. This is useful to optimize
404 * performance by avoiding bounces between servers in contexts where sessions
405 * are shared but cookies are not usable. If the parameter is not found, NULL
406 * is returned. If any server is found, it will be returned. If no valid server
Willy Tarreau9fed8582019-01-14 16:04:54 +0100407 * is found, NULL is returned. When lbprm.arg_opt1 is set, the hash will only
408 * apply to the middle part of a domain name ("use_domain_only" option).
Benoitaffb4812009-03-25 13:02:10 +0100409 */
Willy Tarreau59884a62019-01-02 14:48:31 +0100410static struct server *get_server_hh(struct stream *s, const struct server *avoid)
Benoitaffb4812009-03-25 13:02:10 +0100411{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700412 unsigned int hash = 0;
Benoitaffb4812009-03-25 13:02:10 +0100413 struct proxy *px = s->be;
Willy Tarreau484ff072019-01-14 15:28:53 +0100414 unsigned int plen = px->lbprm.arg_len;
Benoitaffb4812009-03-25 13:02:10 +0100415 unsigned long len;
Benoitaffb4812009-03-25 13:02:10 +0100416 const char *p;
Bhaskar98634f02013-10-29 23:30:51 -0400417 const char *start, *end;
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200418 struct htx *htx = htxbuf(&s->req.buf);
419 struct http_hdr_ctx ctx = { .blk = NULL };
Benoitaffb4812009-03-25 13:02:10 +0100420
421 /* tot_weight appears to mean srv_count */
422 if (px->lbprm.tot_weight == 0)
423 return NULL;
424
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200425 /* note: we won't hash if there's only one server left */
426 if (px->lbprm.tot_used == 1)
427 goto hash_done;
428
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200429 http_find_header(htx, ist2(px->lbprm.arg_str, plen), &ctx, 0);
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100430
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200431 /* if the header is not found or empty, let's fallback to round robin */
432 if (!ctx.blk || !ctx.value.len)
433 return NULL;
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100434
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200435 /* Found a the param_name in the headers.
436 * we will compute the hash based on this value ctx.val.
437 */
438 len = ctx.value.len;
439 p = ctx.value.ptr;
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100440
Willy Tarreau9fed8582019-01-14 16:04:54 +0100441 if (!px->lbprm.arg_opt1) {
Bhaskar98634f02013-10-29 23:30:51 -0400442 hash = gen_hash(px, p, len);
Benoitaffb4812009-03-25 13:02:10 +0100443 } else {
444 int dohash = 0;
Cyril Bontéf607d812015-01-04 15:17:36 +0100445 p += len;
Benoitaffb4812009-03-25 13:02:10 +0100446 /* special computation, use only main domain name, not tld/host
447 * going back from the end of string, start hashing at first
448 * dot stop at next.
449 * This is designed to work with the 'Host' header, and requires
450 * a special option to activate this.
451 */
Cyril Bontéf607d812015-01-04 15:17:36 +0100452 end = p;
Benoitaffb4812009-03-25 13:02:10 +0100453 while (len) {
Cyril Bontéf607d812015-01-04 15:17:36 +0100454 if (dohash) {
455 /* Rewind the pointer until the previous char
456 * is a dot, this will allow to set the start
457 * position of the domain. */
458 if (*(p - 1) == '.')
Benoitaffb4812009-03-25 13:02:10 +0100459 break;
Benoitaffb4812009-03-25 13:02:10 +0100460 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100461 else if (*p == '.') {
462 /* The pointer is rewinded to the dot before the
463 * tld, we memorize the end of the domain and
464 * can enter the domain processing. */
465 end = p;
466 dohash = 1;
467 }
Benoitaffb4812009-03-25 13:02:10 +0100468 p--;
Cyril Bontéf607d812015-01-04 15:17:36 +0100469 len--;
Benoitaffb4812009-03-25 13:02:10 +0100470 }
Cyril Bontéf607d812015-01-04 15:17:36 +0100471 start = p;
Bhaskar98634f02013-10-29 23:30:51 -0400472 hash = gen_hash(px, start, (end - start));
Benoitaffb4812009-03-25 13:02:10 +0100473 }
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500474 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100475 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200476 hash_done:
Willy Tarreau6c30be52019-01-14 17:07:39 +0100477 if ((px->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau59884a62019-01-02 14:48:31 +0100478 return chash_get_server_hash(px, hash, avoid);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200479 else
480 return map_get_server_hash(px, hash);
Benoitaffb4812009-03-25 13:02:10 +0100481}
482
Willy Tarreau34db1082012-04-19 17:16:54 +0200483/* RDP Cookie HASH. */
Willy Tarreau59884a62019-01-02 14:48:31 +0100484static struct server *get_server_rch(struct stream *s, const struct server *avoid)
Emeric Brun736aa232009-06-30 17:56:00 +0200485{
Dan Dubovikbd57a9f2014-07-08 08:51:03 -0700486 unsigned int hash = 0;
Emeric Brun736aa232009-06-30 17:56:00 +0200487 struct proxy *px = s->be;
488 unsigned long len;
Emeric Brun736aa232009-06-30 17:56:00 +0200489 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +0200490 struct sample smp;
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200491 int rewind;
Emeric Brun736aa232009-06-30 17:56:00 +0200492
493 /* tot_weight appears to mean srv_count */
494 if (px->lbprm.tot_weight == 0)
495 return NULL;
496
Willy Tarreau37406352012-04-23 16:16:37 +0200497 memset(&smp, 0, sizeof(smp));
Emeric Brun736aa232009-06-30 17:56:00 +0200498
Willy Tarreau6a445eb2018-06-19 07:04:45 +0200499 rewind = co_data(&s->req);
500 c_rew(&s->req, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200501
Willy Tarreau484ff072019-01-14 15:28:53 +0100502 ret = fetch_rdp_cookie_name(s, &smp, px->lbprm.arg_str, px->lbprm.arg_len);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 len = smp.data.u.str.data;
Willy Tarreau664092c2011-12-16 19:11:42 +0100504
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200505 c_adv(&s->req, rewind);
Willy Tarreaud1de8af2012-05-18 22:12:14 +0200506
Willy Tarreau37406352012-04-23 16:16:37 +0200507 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || len == 0)
Emeric Brun736aa232009-06-30 17:56:00 +0200508 return NULL;
509
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200510 /* note: we won't hash if there's only one server left */
511 if (px->lbprm.tot_used == 1)
512 goto hash_done;
513
Willy Tarreau484ff072019-01-14 15:28:53 +0100514 /* Found the param_name in the headers.
Emeric Brun736aa232009-06-30 17:56:00 +0200515 * we will compute the hash based on this value ctx.val.
516 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200517 hash = gen_hash(px, smp.data.u.str.area, len);
Bhaskar98634f02013-10-29 23:30:51 -0400518
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500519 if ((px->lbprm.algo & BE_LB_HASH_MOD) == BE_LB_HMOD_AVAL)
Willy Tarreau798a39c2010-11-24 15:04:29 +0100520 hash = full_hash(hash);
Willy Tarreau39c9ba72009-10-01 21:11:15 +0200521 hash_done:
Willy Tarreau6c30be52019-01-14 17:07:39 +0100522 if ((px->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau59884a62019-01-02 14:48:31 +0100523 return chash_get_server_hash(px, hash, avoid);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200524 else
525 return map_get_server_hash(px, hash);
Emeric Brun736aa232009-06-30 17:56:00 +0200526}
Christopher Faulet5b517552017-06-09 14:17:53 +0200527
Willy Tarreau760e81d2018-05-03 07:20:40 +0200528/* random value */
Willy Tarreau59884a62019-01-02 14:48:31 +0100529static struct server *get_server_rnd(struct stream *s, const struct server *avoid)
Willy Tarreau760e81d2018-05-03 07:20:40 +0200530{
531 unsigned int hash = 0;
532 struct proxy *px = s->be;
Willy Tarreau21c741a2019-01-14 18:14:27 +0100533 struct server *prev, *curr;
534 int draws = px->lbprm.arg_opt1; // number of draws
Willy Tarreau760e81d2018-05-03 07:20:40 +0200535
536 /* tot_weight appears to mean srv_count */
537 if (px->lbprm.tot_weight == 0)
538 return NULL;
539
Willy Tarreau21c741a2019-01-14 18:14:27 +0100540 curr = NULL;
541 do {
542 prev = curr;
Ubuntu1adaddb2021-03-01 07:57:54 +0000543 hash = statistical_prng();
Willy Tarreau21c741a2019-01-14 18:14:27 +0100544 curr = chash_get_server_hash(px, hash, avoid);
545 if (!curr)
546 break;
547
548 /* compare the new server to the previous best choice and pick
549 * the one with the least currently served requests.
550 */
551 if (prev && prev != curr &&
552 curr->served * prev->cur_eweight > prev->served * curr->cur_eweight)
553 curr = prev;
554 } while (--draws > 0);
555
Willy Tarreaub88ae182020-09-29 16:58:30 +0200556 /* if the selected server is full, pretend we have none so that we reach
557 * the backend's queue instead.
558 */
559 if (curr &&
Willy Tarreaua0570452021-06-18 09:30:30 +0200560 (curr->queue.length || (curr->maxconn && curr->served >= srv_dynamic_maxconn(curr))))
Willy Tarreaub88ae182020-09-29 16:58:30 +0200561 curr = NULL;
562
Willy Tarreau21c741a2019-01-14 18:14:27 +0100563 return curr;
Willy Tarreau760e81d2018-05-03 07:20:40 +0200564}
565
Benoitaffb4812009-03-25 13:02:10 +0100566/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200567 * This function applies the load-balancing algorithm to the stream, as
568 * defined by the backend it is assigned to. The stream is then marked as
Willy Tarreau7c669d72008-06-20 15:04:11 +0200569 * 'assigned'.
570 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200571 * This function MAY NOT be called with SF_ASSIGNED already set. If the stream
Willy Tarreau7c669d72008-06-20 15:04:11 +0200572 * had a server previously assigned, it is rebalanced, trying to avoid the same
Willy Tarreau827aee92011-03-10 16:55:02 +0100573 * server, which should still be present in target_srv(&s->target) before the call.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200574 * The function tries to keep the original connection slot if it reconnects to
575 * the same server, otherwise it releases it and tries to offer it.
576 *
Willy Tarreau87b09662015-04-03 00:22:06 +0200577 * It is illegal to call this function with a stream in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200578 *
579 * It may return :
Willy Tarreau664beb82011-03-10 11:38:29 +0100580 * SRV_STATUS_OK if everything is OK. ->srv and ->target are assigned.
Willy Tarreau87b09662015-04-03 00:22:06 +0200581 * SRV_STATUS_NOSRV if no server is available. Stream is not ASSIGNED
582 * SRV_STATUS_FULL if all servers are saturated. Stream is not ASSIGNED
Willy Tarreaubaaee002006-06-26 02:48:02 +0200583 * SRV_STATUS_INTERNAL for other unrecoverable errors.
584 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200585 * Upon successful return, the stream flag SF_ASSIGNED is set to indicate that
Willy Tarreau827aee92011-03-10 16:55:02 +0100586 * it does not need to be called anymore. This means that target_srv(&s->target)
587 * can be trusted in balance and direct modes.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200588 *
589 */
590
Willy Tarreau87b09662015-04-03 00:22:06 +0200591int assign_server(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200592{
Olivier Houchardba4fff52018-12-01 14:40:40 +0100593 struct connection *conn = NULL;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200594 struct server *conn_slot;
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100595 struct server *srv = NULL, *prev_srv;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200596 int err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100597
Simon Horman8effd3d2011-08-13 08:03:52 +0900598 DPRINTF(stderr,"assign_server : s=%p\n",s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200599
Willy Tarreau7c669d72008-06-20 15:04:11 +0200600 err = SRV_STATUS_INTERNAL;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200601 if (unlikely(s->pend_pos || s->flags & SF_ASSIGNED))
Willy Tarreau7c669d72008-06-20 15:04:11 +0200602 goto out_err;
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100603
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100604 prev_srv = objt_server(s->target);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200605 conn_slot = s->srv_conn;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200606
Willy Tarreau7c669d72008-06-20 15:04:11 +0200607 /* We have to release any connection slot before applying any LB algo,
608 * otherwise we may erroneously end up with no available slot.
609 */
610 if (conn_slot)
611 sess_change_server(s, NULL);
612
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100613 /* We will now try to find the good server and store it into <objt_server(s->target)>.
614 * Note that <objt_server(s->target)> may be NULL in case of dispatch or proxy mode,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200615 * as well as if no server is available (check error code).
616 */
Willy Tarreau1a20a5d2007-11-01 21:08:19 +0100617
Willy Tarreau827aee92011-03-10 16:55:02 +0100618 srv = NULL;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100619 s->target = NULL;
Willy Tarreau664beb82011-03-10 11:38:29 +0100620
Olivier Houchardba4fff52018-12-01 14:40:40 +0100621 if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI &&
Olivier Houchard250031e2019-05-29 15:01:50 +0200622 ((s->sess->flags & SESS_FL_PREFER_LAST) ||
Olivier Houchardba4fff52018-12-01 14:40:40 +0100623 (s->be->options & PR_O_PREF_LAST))) {
Olivier Houchard351411f2018-12-27 17:20:54 +0100624 struct sess_srv_list *srv_list;
625 list_for_each_entry(srv_list, &s->sess->srv_list, srv_list) {
626 struct server *tmpsrv = objt_server(srv_list->target);
Olivier Houchardba4fff52018-12-01 14:40:40 +0100627
628 if (tmpsrv && tmpsrv->proxy == s->be &&
Olivier Houchard250031e2019-05-29 15:01:50 +0200629 ((s->sess->flags & SESS_FL_PREFER_LAST) ||
Olivier Houchardba4fff52018-12-01 14:40:40 +0100630 (!s->be->max_ka_queue ||
631 server_has_room(tmpsrv) || (
Willy Tarreaua0570452021-06-18 09:30:30 +0200632 tmpsrv->queue.length + 1 < s->be->max_ka_queue))) &&
Olivier Houchardba4fff52018-12-01 14:40:40 +0100633 srv_currently_usable(tmpsrv)) {
Olivier Houchard351411f2018-12-27 17:20:54 +0100634 list_for_each_entry(conn, &srv_list->conn_list, session_list) {
Willy Tarreau911db9b2020-01-23 16:27:54 +0100635 if (!(conn->flags & CO_FL_WAIT_XPRT)) {
Olivier Houchardba4fff52018-12-01 14:40:40 +0100636 srv = tmpsrv;
637 s->target = &srv->obj_type;
Christopher Faulete91a5262020-07-01 18:56:30 +0200638 if (conn->flags & CO_FL_SESS_IDLE) {
639 conn->flags &= ~CO_FL_SESS_IDLE;
640 s->sess->idle_conns--;
641 }
Olivier Houchardba4fff52018-12-01 14:40:40 +0100642 goto out_ok;
643 }
644 }
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100645 }
646 }
Willy Tarreau9420b122013-12-15 18:58:25 +0100647 }
Christopher Faulet5b517552017-06-09 14:17:53 +0200648
Willy Tarreau82cd5c12020-09-29 17:07:21 +0200649 if (s->be->lbprm.algo & BE_LB_KIND) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200650 /* we must check if we have at least one server available */
651 if (!s->be->lbprm.tot_weight) {
652 err = SRV_STATUS_NOSRV;
653 goto out;
654 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200655
Willy Tarreau82cd5c12020-09-29 17:07:21 +0200656 /* if there's some queue on the backend, with certain algos we
657 * know it's because all servers are full.
658 */
Willy Tarreau7f3c1df2021-06-18 09:22:21 +0200659 if (s->be->queue.length && s->be->queue.length != s->be->beconn &&
Willy Tarreau8ae8c482020-10-22 17:19:07 +0200660 (((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 +0200661 ((s->be->lbprm.algo & (BE_LB_KIND|BE_LB_NEED|BE_LB_PARM)) == BE_LB_ALGO_RR) || // roundrobin
662 ((s->be->lbprm.algo & (BE_LB_KIND|BE_LB_NEED|BE_LB_PARM)) == BE_LB_ALGO_SRR))) { // static-rr
663 err = SRV_STATUS_FULL;
664 goto out;
665 }
666
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200667 /* First check whether we need to fetch some data or simply call
668 * the LB lookup function. Only the hashing functions will need
669 * some input data in fact, and will support multiple algorithms.
670 */
671 switch (s->be->lbprm.algo & BE_LB_LKUP) {
672 case BE_LB_LKUP_RRTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100673 srv = fwrr_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200674 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200675
Willy Tarreauf09c6602012-02-13 17:12:08 +0100676 case BE_LB_LKUP_FSTREE:
677 srv = fas_get_next_server(s->be, prev_srv);
678 break;
679
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200680 case BE_LB_LKUP_LCTREE:
Willy Tarreau827aee92011-03-10 16:55:02 +0100681 srv = fwlc_get_next_server(s->be, prev_srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200682 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200683
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200684 case BE_LB_LKUP_CHTREE:
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200685 case BE_LB_LKUP_MAP:
Willy Tarreau9757a382009-10-03 12:56:50 +0200686 if ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR) {
Willy Tarreau5ffb0452021-06-22 17:31:51 +0200687 /* static-rr (map) or random (chash) */
Willy Tarreau760e81d2018-05-03 07:20:40 +0200688 if ((s->be->lbprm.algo & BE_LB_PARM) == BE_LB_RR_RANDOM)
Willy Tarreau59884a62019-01-02 14:48:31 +0100689 srv = get_server_rnd(s, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200690 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100691 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau9757a382009-10-03 12:56:50 +0200692 break;
693 }
694 else if ((s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_HI) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200695 /* unknown balancing algorithm */
Willy Tarreau7c669d72008-06-20 15:04:11 +0200696 err = SRV_STATUS_INTERNAL;
697 goto out;
698 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200699
700 switch (s->be->lbprm.algo & BE_LB_PARM) {
Christopher Fauleta8e95fe2021-10-25 08:13:25 +0200701 const struct sockaddr_storage *src;
702
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200703 case BE_LB_HASH_SRC:
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200704 src = cs_src(s->csf);
Christopher Fauleta8e95fe2021-10-25 08:13:25 +0200705 if (src && src->ss_family == AF_INET) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200706 srv = get_server_sh(s->be,
Christopher Fauleta8e95fe2021-10-25 08:13:25 +0200707 (void *)&((struct sockaddr_in *)src)->sin_addr,
Willy Tarreau59884a62019-01-02 14:48:31 +0100708 4, prev_srv);
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200709 }
Christopher Fauleta8e95fe2021-10-25 08:13:25 +0200710 else if (src && src->ss_family == AF_INET6) {
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200711 srv = get_server_sh(s->be,
Christopher Fauleta8e95fe2021-10-25 08:13:25 +0200712 (void *)&((struct sockaddr_in6 *)src)->sin6_addr,
Willy Tarreau59884a62019-01-02 14:48:31 +0100713 16, prev_srv);
Willy Tarreau5dd7fa12012-03-31 19:53:37 +0200714 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200715 else {
716 /* unknown IP family */
717 err = SRV_STATUS_INTERNAL;
718 goto out;
719 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200720 break;
721
722 case BE_LB_HASH_URI:
723 /* URI hashing */
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200724 if (IS_HTX_STRM(s) && s->txn->req.msg_state >= HTTP_MSG_BODY) {
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100725 struct ist uri;
726
Christopher Faulet297fbb42019-05-13 14:41:27 +0200727 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Willy Tarreau57a37412020-09-23 08:56:29 +0200728 if (s->be->lbprm.arg_opt1 & 2) {
Amaury Denoyellec453f952021-07-06 11:40:12 +0200729 struct http_uri_parser parser =
730 http_uri_parser_init(uri);
731
732 uri = http_parse_path(&parser);
Tim Duesterhus7b5777d2021-03-02 18:57:28 +0100733 if (!isttest(uri))
Willy Tarreau57a37412020-09-23 08:56:29 +0200734 uri = ist("");
735 }
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100736 srv = get_server_uh(s->be, uri.ptr, uri.len, prev_srv);
737 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200738 break;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200739
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200740 case BE_LB_HASH_PRM:
741 /* URL Parameter hashing */
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200742 if (IS_HTX_STRM(s) && s->txn->req.msg_state >= HTTP_MSG_BODY) {
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100743 struct ist uri;
744
Christopher Faulet297fbb42019-05-13 14:41:27 +0200745 uri = htx_sl_req_uri(http_get_stline(htxbuf(&s->req.buf)));
Christopher Fauletf7679ad2019-02-04 12:02:18 +0100746 srv = get_server_ph(s->be, uri.ptr, uri.len, prev_srv);
Willy Tarreau61a21a32011-03-01 20:35:49 +0100747
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200748 if (!srv && s->txn->meth == HTTP_METH_POST)
749 srv = get_server_ph_post(s, prev_srv);
750 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200751 break;
Benoitaffb4812009-03-25 13:02:10 +0100752
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200753 case BE_LB_HASH_HDR:
754 /* Header Parameter hashing */
Christopher Faulet7d37fbb2019-07-15 15:37:57 +0200755 if (IS_HTX_STRM(s) && s->txn->req.msg_state >= HTTP_MSG_BODY)
756 srv = get_server_hh(s, prev_srv);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200757 break;
758
759 case BE_LB_HASH_RDP:
760 /* RDP Cookie hashing */
Willy Tarreau59884a62019-01-02 14:48:31 +0100761 srv = get_server_rch(s, prev_srv);
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200762 break;
763
764 default:
765 /* unknown balancing algorithm */
766 err = SRV_STATUS_INTERNAL;
767 goto out;
Benoitaffb4812009-03-25 13:02:10 +0100768 }
Emeric Brun736aa232009-06-30 17:56:00 +0200769
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200770 /* If the hashing parameter was not found, let's fall
771 * back to round robin on the map.
772 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100773 if (!srv) {
Willy Tarreau6c30be52019-01-14 17:07:39 +0100774 if ((s->be->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_CHTREE)
Willy Tarreau827aee92011-03-10 16:55:02 +0100775 srv = chash_get_next_server(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200776 else
Willy Tarreau827aee92011-03-10 16:55:02 +0100777 srv = map_get_server_rr(s->be, prev_srv);
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200778 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200779
780 /* end of map-based LB */
Emeric Brun736aa232009-06-30 17:56:00 +0200781 break;
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200782
Willy Tarreau7c669d72008-06-20 15:04:11 +0200783 default:
784 /* unknown balancing algorithm */
785 err = SRV_STATUS_INTERNAL;
786 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200787 }
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200788
Willy Tarreau827aee92011-03-10 16:55:02 +0100789 if (!srv) {
Willy Tarreauda76f4f2009-10-03 12:36:05 +0200790 err = SRV_STATUS_FULL;
791 goto out;
792 }
Willy Tarreau827aee92011-03-10 16:55:02 +0100793 else if (srv != prev_srv) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200794 _HA_ATOMIC_INC(&s->be->be_counters.cum_lbconn);
795 _HA_ATOMIC_INC(&srv->counters.cum_lbconn);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100796 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100797 s->target = &srv->obj_type;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200798 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200799 else if (s->be->options & (PR_O_DISPATCH | PR_O_TRANSP)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100800 s->target = &s->be->obj_type;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200801 }
Willy Tarreau664beb82011-03-10 11:38:29 +0100802 else {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200803 err = SRV_STATUS_NOSRV;
804 goto out;
805 }
806
Olivier Houchard00cf70f2018-11-30 17:24:55 +0100807out_ok:
Willy Tarreaue7dff022015-04-03 01:14:29 +0200808 s->flags |= SF_ASSIGNED;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200809 err = SRV_STATUS_OK;
810 out:
811
812 /* Either we take back our connection slot, or we offer it to someone
813 * else if we don't need it anymore.
814 */
815 if (conn_slot) {
Willy Tarreau827aee92011-03-10 16:55:02 +0100816 if (conn_slot == srv) {
817 sess_change_server(s, srv);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200818 } else {
819 if (may_dequeue_tasks(conn_slot, s->be))
Willy Tarreau9ab78292021-06-22 18:47:51 +0200820 process_srv_queue(conn_slot);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200821 }
822 }
823
824 out_err:
825 return err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200826}
827
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100828/* Allocate an address for the destination endpoint
Willy Tarreaubaaee002006-06-26 02:48:02 +0200829 * The address is taken from the currently assigned server, or from the
830 * dispatch or transparent address.
831 *
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100832 * Returns SRV_STATUS_OK on success.
Amaury Denoyellef7bdf002021-01-21 09:40:19 +0100833 * On error, no address is allocated and SRV_STATUS_INTERNAL is returned.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200834 */
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100835static int alloc_dst_address(struct sockaddr_storage **ss,
836 struct server *srv, struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200837{
Christopher Fauleta8e95fe2021-10-25 08:13:25 +0200838 const struct sockaddr_storage *dst;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200839
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100840 *ss = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200841 if ((s->flags & SF_DIRECT) || (s->be->lbprm.algo & BE_LB_KIND)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200842 /* A server is necessarily known for this stream */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200843 if (!(s->flags & SF_ASSIGNED))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 return SRV_STATUS_INTERNAL;
845
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100846 if (!sockaddr_alloc(ss, NULL, 0))
847 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200848
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100849 **ss = srv->addr;
850 set_host_port(*ss, srv->svc_port);
Christopher Faulet34a3eb42021-11-05 12:02:56 +0100851 if (!is_addr(*ss)) {
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200852 /* if the server has no address, we use the same address
853 * the client asked, which is handy for remapping ports
Willy Tarreau9cf8d3f2014-05-09 22:56:10 +0200854 * locally on multiple addresses at once. Nothing is done
855 * for AF_UNIX addresses.
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200856 */
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200857 dst = cs_dst(s->csf);
Christopher Faulet34a3eb42021-11-05 12:02:56 +0100858 if (dst && dst->ss_family == AF_INET) {
Christopher Fauletae305612021-03-01 11:33:59 +0100859 ((struct sockaddr_in *)*ss)->sin_family = AF_INET;
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100860 ((struct sockaddr_in *)*ss)->sin_addr =
Christopher Fauleta8e95fe2021-10-25 08:13:25 +0200861 ((struct sockaddr_in *)dst)->sin_addr;
Christopher Faulet34a3eb42021-11-05 12:02:56 +0100862 } else if (dst && dst->ss_family == AF_INET6) {
Christopher Fauletae305612021-03-01 11:33:59 +0100863 ((struct sockaddr_in6 *)*ss)->sin6_family = AF_INET6;
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100864 ((struct sockaddr_in6 *)*ss)->sin6_addr =
Christopher Fauleta8e95fe2021-10-25 08:13:25 +0200865 ((struct sockaddr_in6 *)dst)->sin6_addr;
Emeric Brunec810d12010-10-22 16:36:33 +0200866 }
Willy Tarreaud669a4f2010-07-13 14:49:50 +0200867 }
868
Willy Tarreaubaaee002006-06-26 02:48:02 +0200869 /* if this server remaps proxied ports, we'll use
870 * the port the client connected to with an offset. */
Christopher Faulet34a3eb42021-11-05 12:02:56 +0100871 if ((srv->flags & SRV_F_MAPPORTS)) {
David du Colombier6f5ccb12011-03-10 22:26:24 +0100872 int base_port;
873
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200874 dst = cs_dst(s->csf);
Christopher Faulet34a3eb42021-11-05 12:02:56 +0100875 if (dst) {
876 /* First, retrieve the port from the incoming connection */
877 base_port = get_host_port(dst);
David du Colombier6f5ccb12011-03-10 22:26:24 +0100878
Christopher Faulet34a3eb42021-11-05 12:02:56 +0100879 /* Second, assign the outgoing connection's port */
880 base_port += get_host_port(*ss);
881 set_host_port(*ss, base_port);
882 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200883 }
884 }
Willy Tarreau1620ec32011-08-06 17:05:02 +0200885 else if (s->be->options & PR_O_DISPATCH) {
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100886 if (!sockaddr_alloc(ss, NULL, 0))
887 return SRV_STATUS_INTERNAL;
888
Willy Tarreaubaaee002006-06-26 02:48:02 +0200889 /* connect to the defined dispatch addr */
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100890 **ss = s->be->dispatch_addr;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200891 }
Christopher Fauleta8e95fe2021-10-25 08:13:25 +0200892 else if ((s->be->options & PR_O_TRANSP)) {
Christopher Faulet34a3eb42021-11-05 12:02:56 +0100893 if (!sockaddr_alloc(ss, NULL, 0))
Amaury Denoyelle68cf3952021-01-11 15:24:31 +0100894 return SRV_STATUS_INTERNAL;
895
Willy Tarreaubaaee002006-06-26 02:48:02 +0200896 /* in transparent mode, use the original dest addr if no dispatch specified */
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200897 dst = cs_dst(s->csf);
Christopher Faulet34a3eb42021-11-05 12:02:56 +0100898 if (dst && (dst->ss_family == AF_INET || dst->ss_family == AF_INET6))
Christopher Fauleta8e95fe2021-10-25 08:13:25 +0200899 **ss = *dst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200900 }
Willy Tarreau1a1158b2007-01-20 11:07:46 +0100901 else {
902 /* no server and no LB algorithm ! */
903 return SRV_STATUS_INTERNAL;
904 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200905
Willy Tarreaubaaee002006-06-26 02:48:02 +0200906 return SRV_STATUS_OK;
907}
908
Willy Tarreau87b09662015-04-03 00:22:06 +0200909/* This function assigns a server to stream <s> if required, and can add the
Willy Tarreaubaaee002006-06-26 02:48:02 +0200910 * connection to either the assigned server's queue or to the proxy's queue.
Willy Tarreau87b09662015-04-03 00:22:06 +0200911 * If ->srv_conn is set, the stream is first released from the server.
Willy Tarreaue7dff022015-04-03 01:14:29 +0200912 * It may also be called with SF_DIRECT and/or SF_ASSIGNED though. It will
Willy Tarreau7c669d72008-06-20 15:04:11 +0200913 * be called before any connection and after any retry or redispatch occurs.
914 *
Willy Tarreau87b09662015-04-03 00:22:06 +0200915 * It is not allowed to call this function with a stream in a queue.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200916 *
917 * Returns :
918 *
919 * SRV_STATUS_OK if everything is OK.
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100920 * SRV_STATUS_NOSRV if no server is available. objt_server(s->target) = NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200921 * SRV_STATUS_QUEUED if the connection has been queued.
922 * SRV_STATUS_FULL if the server(s) is/are saturated and the
Willy Tarreau827aee92011-03-10 16:55:02 +0100923 * connection could not be queued at the server's,
Willy Tarreau7c669d72008-06-20 15:04:11 +0200924 * which may be NULL if we queue on the backend.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200925 * SRV_STATUS_INTERNAL for other unrecoverable errors.
926 *
927 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200928int assign_server_and_queue(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929{
930 struct pendconn *p;
Willy Tarreau827aee92011-03-10 16:55:02 +0100931 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 int err;
933
934 if (s->pend_pos)
935 return SRV_STATUS_INTERNAL;
936
Willy Tarreau7c669d72008-06-20 15:04:11 +0200937 err = SRV_STATUS_OK;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200938 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100939 struct server *prev_srv = objt_server(s->target);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100940
Willy Tarreau7c669d72008-06-20 15:04:11 +0200941 err = assign_server(s);
Willy Tarreau3d80d912011-03-10 11:42:13 +0100942 if (prev_srv) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200943 /* This stream was previously assigned to a server. We have to
944 * update the stream's and the server's stats :
Willy Tarreau7c669d72008-06-20 15:04:11 +0200945 * - if the server changed :
946 * - set TX_CK_DOWN if txn.flags was TX_CK_VALID
Willy Tarreaue7dff022015-04-03 01:14:29 +0200947 * - set SF_REDISP if it was successfully redispatched
Willy Tarreau7c669d72008-06-20 15:04:11 +0200948 * - increment srv->redispatches and be->redispatches
949 * - if the server remained the same : update retries.
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100950 */
951
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100952 if (prev_srv != objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200953 if (s->txn && (s->txn->flags & TX_CK_MASK) == TX_CK_VALID) {
954 s->txn->flags &= ~TX_CK_MASK;
955 s->txn->flags |= TX_CK_DOWN;
Willy Tarreau7c669d72008-06-20 15:04:11 +0200956 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200957 s->flags |= SF_REDISP;
Willy Tarreau4781b152021-04-06 13:53:36 +0200958 _HA_ATOMIC_INC(&prev_srv->counters.redispatches);
959 _HA_ATOMIC_INC(&s->be->be_counters.redispatches);
Willy Tarreau7c669d72008-06-20 15:04:11 +0200960 } else {
Willy Tarreau4781b152021-04-06 13:53:36 +0200961 _HA_ATOMIC_INC(&prev_srv->counters.retries);
962 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100963 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +0100964 }
965 }
966
Willy Tarreaubaaee002006-06-26 02:48:02 +0200967 switch (err) {
968 case SRV_STATUS_OK:
Willy Tarreaue7dff022015-04-03 01:14:29 +0200969 /* we have SF_ASSIGNED set */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100970 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100971 if (!srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200972 return SRV_STATUS_OK; /* dispatch or proxy mode */
973
974 /* If we already have a connection slot, no need to check any queue */
Willy Tarreau827aee92011-03-10 16:55:02 +0100975 if (s->srv_conn == srv)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200976 return SRV_STATUS_OK;
977
Willy Tarreau87b09662015-04-03 00:22:06 +0200978 /* OK, this stream already has an assigned server, but no
Willy Tarreau7c669d72008-06-20 15:04:11 +0200979 * connection slot yet. Either it is a redispatch, or it was
980 * assigned from persistence information (direct mode).
981 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200982 if ((s->flags & SF_REDIRECTABLE) && srv->rdr_len) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200983 /* server scheduled for redirection, and already assigned. We
984 * don't want to go further nor check the queue.
Willy Tarreau21d2af32008-02-14 20:25:24 +0100985 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100986 sess_change_server(s, srv); /* not really needed in fact */
Willy Tarreau21d2af32008-02-14 20:25:24 +0100987 return SRV_STATUS_OK;
988 }
989
Willy Tarreau87b09662015-04-03 00:22:06 +0200990 /* We might have to queue this stream if the assigned server is full.
Willy Tarreau7c669d72008-06-20 15:04:11 +0200991 * We know we have to queue it into the server's queue, so if a maxqueue
992 * is set on the server, we must also check that the server's queue is
993 * not full, in which case we have to return FULL.
994 */
Willy Tarreau827aee92011-03-10 16:55:02 +0100995 if (srv->maxconn &&
Willy Tarreaua0570452021-06-18 09:30:30 +0200996 (srv->queue.length || srv->served >= srv_dynamic_maxconn(srv))) {
Willy Tarreau7c669d72008-06-20 15:04:11 +0200997
Willy Tarreaua0570452021-06-18 09:30:30 +0200998 if (srv->maxqueue > 0 && srv->queue.length >= srv->maxqueue)
Willy Tarreau7c669d72008-06-20 15:04:11 +0200999 return SRV_STATUS_FULL;
1000
Willy Tarreaubaaee002006-06-26 02:48:02 +02001001 p = pendconn_add(s);
1002 if (p)
1003 return SRV_STATUS_QUEUED;
1004 else
Willy Tarreau7c669d72008-06-20 15:04:11 +02001005 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001006 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001007
1008 /* OK, we can use this server. Let's reserve our place */
Willy Tarreau827aee92011-03-10 16:55:02 +01001009 sess_change_server(s, srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001010 return SRV_STATUS_OK;
1011
1012 case SRV_STATUS_FULL:
Willy Tarreau87b09662015-04-03 00:22:06 +02001013 /* queue this stream into the proxy's queue */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001014 p = pendconn_add(s);
1015 if (p)
1016 return SRV_STATUS_QUEUED;
1017 else
Willy Tarreau7c669d72008-06-20 15:04:11 +02001018 return SRV_STATUS_INTERNAL;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001019
1020 case SRV_STATUS_NOSRV:
Willy Tarreau7c669d72008-06-20 15:04:11 +02001021 return err;
1022
Willy Tarreaubaaee002006-06-26 02:48:02 +02001023 case SRV_STATUS_INTERNAL:
1024 return err;
Willy Tarreau7c669d72008-06-20 15:04:11 +02001025
Willy Tarreaubaaee002006-06-26 02:48:02 +02001026 default:
1027 return SRV_STATUS_INTERNAL;
1028 }
Willy Tarreau5b6995c2008-01-13 16:31:17 +01001029}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001030
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001031/* Allocate an address for source binding on the specified server or backend.
1032 * The allocation is only performed if the connection is intended to be used
1033 * with transparent mode.
1034 *
1035 * Returns SRV_STATUS_OK if no transparent mode or the address was successfully
1036 * allocated. Otherwise returns SRV_STATUS_INTERNAL.
Willy Tarreaub1d67742010-03-29 19:36:59 +02001037 */
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001038static int alloc_bind_address(struct sockaddr_storage **ss,
1039 struct server *srv, struct stream *s)
Willy Tarreaub1d67742010-03-29 19:36:59 +02001040{
Willy Tarreau29fbe512015-08-20 19:35:14 +02001041#if defined(CONFIG_HAP_TRANSPARENT)
Christopher Fauleta8e95fe2021-10-25 08:13:25 +02001042 const struct sockaddr_storage *addr;
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001043 struct conn_src *src = NULL;
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001044 struct sockaddr_in *sin;
1045 char *vptr;
1046 size_t vlen;
1047#endif
Olivier Houchard09a0f032019-01-17 15:59:13 +01001048
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001049 *ss = NULL;
Willy Tarreau827aee92011-03-10 16:55:02 +01001050
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001051#if defined(CONFIG_HAP_TRANSPARENT)
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001052 if (srv && srv->conn_src.opts & CO_SRC_BIND)
1053 src = &srv->conn_src;
1054 else if (s->be->conn_src.opts & CO_SRC_BIND)
1055 src = &s->be->conn_src;
Willy Tarreau294c4732011-12-16 21:35:50 +01001056
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001057 /* no transparent mode, no need to allocate an address, returns OK */
1058 if (!src)
1059 return SRV_STATUS_OK;
Willy Tarreauc0e16f22019-07-17 18:16:30 +02001060
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001061 switch (src->opts & CO_SRC_TPROXY_MASK) {
1062 case CO_SRC_TPROXY_ADDR:
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001063 if (!sockaddr_alloc(ss, NULL, 0))
1064 return SRV_STATUS_INTERNAL;
1065
1066 **ss = src->tproxy_addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001067 break;
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001068
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001069 case CO_SRC_TPROXY_CLI:
1070 case CO_SRC_TPROXY_CIP:
1071 /* FIXME: what can we do if the client connects in IPv6 or unix socket ? */
Christopher Faulet8da67aa2022-03-29 17:53:09 +02001072 addr = cs_src(s->csf);
Christopher Fauleta8e95fe2021-10-25 08:13:25 +02001073 if (!addr)
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001074 return SRV_STATUS_INTERNAL;
1075
1076 if (!sockaddr_alloc(ss, NULL, 0))
1077 return SRV_STATUS_INTERNAL;
1078
Christopher Fauleta8e95fe2021-10-25 08:13:25 +02001079 **ss = *addr;
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001080 break;
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001081
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001082 case CO_SRC_TPROXY_DYN:
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001083 if (!src->bind_hdr_occ || !IS_HTX_STRM(s))
1084 return SRV_STATUS_INTERNAL;
Willy Tarreau294c4732011-12-16 21:35:50 +01001085
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001086 if (!sockaddr_alloc(ss, NULL, 0))
1087 return SRV_STATUS_INTERNAL;
1088
1089 /* bind to the IP in a header */
1090 sin = (struct sockaddr_in *)*ss;
1091 sin->sin_family = AF_INET;
1092 sin->sin_port = 0;
1093 sin->sin_addr.s_addr = 0;
1094 if (!http_get_htx_hdr(htxbuf(&s->req.buf),
1095 ist2(src->bind_hdr_name, src->bind_hdr_len),
1096 src->bind_hdr_occ, NULL, &vptr, &vlen)) {
1097 sockaddr_free(ss);
1098 return SRV_STATUS_INTERNAL;
Willy Tarreaub1d67742010-03-29 19:36:59 +02001099 }
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001100
1101 sin->sin_addr.s_addr = htonl(inetaddr_host_lim(vptr, vptr + vlen));
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001102 break;
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001103
Willy Tarreau9cd7d6c2012-12-08 23:13:33 +01001104 default:
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001105 ;
Willy Tarreaub1d67742010-03-29 19:36:59 +02001106 }
1107#endif
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001108
1109 return SRV_STATUS_OK;
Willy Tarreaub1d67742010-03-29 19:36:59 +02001110}
1111
Olivier Houchard566df302020-03-06 18:18:56 +01001112/* Attempt to get a backend connection from the specified mt_list array
Willy Tarreau0d587112020-07-01 15:04:38 +02001113 * (safe or idle connections). The <is_safe> argument means what type of
1114 * connection the caller wants.
Olivier Houchard566df302020-03-06 18:18:56 +01001115 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001116static struct connection *conn_backend_get(struct stream *s, struct server *srv, int is_safe, int64_t hash)
Olivier Houchard566df302020-03-06 18:18:56 +01001117{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001118 struct connection *conn = NULL;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001119 int i; // thread number
Olivier Houchard566df302020-03-06 18:18:56 +01001120 int found = 0;
Willy Tarreau364f25a2020-07-01 15:55:30 +02001121 int stop;
Olivier Houchard566df302020-03-06 18:18:56 +01001122
1123 /* We need to lock even if this is our own list, because another
1124 * thread may be trying to migrate that connection, and we don't want
1125 * to end up with two threads using the same connection.
1126 */
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001127 i = tid;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001128 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001129 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 +01001130 if (conn)
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001131 conn_delete_from_tree(&conn->hash_node->node);
Willy Tarreau0d587112020-07-01 15:04:38 +02001132
1133 /* If we failed to pick a connection from the idle list, let's try again with
1134 * the safe list.
1135 */
1136 if (!conn && !is_safe && srv->curr_safe_nb > 0) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001137 conn = srv_lookup_conn(&srv->per_thr[tid].safe_conns, hash);
Willy Tarreau0d587112020-07-01 15:04:38 +02001138 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 is_safe = 1;
Willy Tarreau0d587112020-07-01 15:04:38 +02001141 }
1142 }
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001143 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houchard566df302020-03-06 18:18:56 +01001144
1145 /* If we found a connection in our own list, and we don't have to
1146 * steal one from another thread, then we're done.
1147 */
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001148 if (conn)
1149 goto done;
1150
Willy Tarreau76cc6992020-07-01 18:49:24 +02001151 /* pool sharing globally disabled ? */
1152 if (!(global.tune.options & GTUNE_IDLE_POOL_SHARED))
1153 goto done;
1154
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001155 /* Are we allowed to pick from another thread ? We'll still try
1156 * it if we're running low on FDs as we don't want to create
1157 * extra conns in this case, otherwise we can give up if we have
1158 * too few idle conns.
1159 */
1160 if (srv->curr_idle_conns < srv->low_idle_conns &&
1161 ha_used_fds < global.tune.pool_low_count)
1162 goto done;
Olivier Houchard566df302020-03-06 18:18:56 +01001163
Willy Tarreau364f25a2020-07-01 15:55:30 +02001164 /* Lookup all other threads for an idle connection, starting from last
1165 * unvisited thread.
1166 */
1167 stop = srv->next_takeover;
1168 if (stop >= global.nbthread)
1169 stop = 0;
1170
Amaury Denoyelle5f1ded52020-10-14 18:17:03 +02001171 i = stop;
1172 do {
Willy Tarreau364f25a2020-07-01 15:55:30 +02001173 if (!srv->curr_idle_thr[i] || i == tid)
Willy Tarreau151c2532020-07-01 08:24:44 +02001174 continue;
1175
Ubuntub1adf032021-03-01 06:22:17 +00001176 if (HA_SPIN_TRYLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock) != 0)
1177 continue;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001178 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 +01001179 while (conn) {
Olivier Houchard1662cdb2020-07-03 14:04:37 +02001180 if (conn->mux->takeover && conn->mux->takeover(conn, i) == 0) {
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001181 conn_delete_from_tree(&conn->hash_node->node);
Willy Tarreau4781b152021-04-06 13:53:36 +02001182 _HA_ATOMIC_INC(&activity[tid].fd_takeover);
Olivier Houchard566df302020-03-06 18:18:56 +01001183 found = 1;
1184 break;
1185 }
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001186
1187 conn = srv_lookup_conn_next(conn);
Olivier Houchard566df302020-03-06 18:18:56 +01001188 }
Willy Tarreau0d587112020-07-01 15:04:38 +02001189
1190 if (!found && !is_safe && srv->curr_safe_nb > 0) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001191 conn = srv_lookup_conn(&srv->per_thr[i].safe_conns, hash);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001192 while (conn) {
Olivier Houchard1662cdb2020-07-03 14:04:37 +02001193 if (conn->mux->takeover && conn->mux->takeover(conn, i) == 0) {
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001194 conn_delete_from_tree(&conn->hash_node->node);
Willy Tarreau4781b152021-04-06 13:53:36 +02001195 _HA_ATOMIC_INC(&activity[tid].fd_takeover);
Willy Tarreau0d587112020-07-01 15:04:38 +02001196 found = 1;
1197 is_safe = 1;
Willy Tarreau0d587112020-07-01 15:04:38 +02001198 break;
1199 }
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001200
1201 conn = srv_lookup_conn_next(conn);
Willy Tarreau0d587112020-07-01 15:04:38 +02001202 }
1203 }
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001204 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Amaury Denoyelle5f1ded52020-10-14 18:17:03 +02001205 } while (!found && (i = (i + 1 == global.nbthread) ? 0 : i + 1) != stop);
Olivier Houchard566df302020-03-06 18:18:56 +01001206
1207 if (!found)
1208 conn = NULL;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001209 done:
1210 if (conn) {
Willy Tarreau364f25a2020-07-01 15:55:30 +02001211 _HA_ATOMIC_STORE(&srv->next_takeover, (i + 1 == global.nbthread) ? 0 : i + 1);
Amaury Denoyelle9c13b622020-10-14 18:17:04 +02001212
1213 srv_use_conn(srv, conn);
1214
Willy Tarreau4781b152021-04-06 13:53:36 +02001215 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
1216 _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb);
1217 _HA_ATOMIC_DEC(&srv->curr_idle_thr[i]);
Amaury Denoyelle9c13b622020-10-14 18:17:04 +02001218 conn->flags &= ~CO_FL_LIST_MASK;
1219 __ha_barrier_atomic_store();
1220
Amaury Denoyelle0d21dea2020-10-14 18:17:09 +02001221 if ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_SAFE &&
1222 conn->mux->flags & MX_FL_HOL_RISK) {
1223 /* attach the connection to the session private list
1224 */
1225 conn->owner = s->sess;
Willy Tarreau38b4d2e2020-11-20 17:08:15 +01001226 session_add_conn(s->sess, conn, conn->target);
Amaury Denoyelle0d21dea2020-10-14 18:17:09 +02001227 }
1228 else {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001229 ebmb_insert(&srv->per_thr[tid].avail_conns,
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001230 &conn->hash_node->node,
1231 sizeof(conn->hash_node->hash));
Amaury Denoyelle0d21dea2020-10-14 18:17:09 +02001232 }
Olivier Houchard566df302020-03-06 18:18:56 +01001233 }
1234 return conn;
1235}
1236
Willy Tarreaubaaee002006-06-26 02:48:02 +02001237/*
Willy Tarreau87b09662015-04-03 00:22:06 +02001238 * This function initiates a connection to the server assigned to this stream
Christopher Faulet4a0114b2021-12-23 12:24:35 +01001239 * (s->target, (s->csb->si)->addr.to). It will assign a server if none
Willy Tarreau664beb82011-03-10 11:38:29 +01001240 * is assigned yet.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001241 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +02001242 * - SF_ERR_NONE if everything's OK
1243 * - SF_ERR_SRVTO if there are no more servers
1244 * - SF_ERR_SRVCL if the connection was refused by the server
1245 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
1246 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
1247 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +01001248 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001249 * The server-facing stream interface is expected to hold a pre-allocated connection
Christopher Faulet4a0114b2021-12-23 12:24:35 +01001250 * in s->csb->si->conn.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001251 */
Christopher Fauletc983b212022-01-13 09:53:00 +01001252static int connect_server(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001253{
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02001254 struct connection *cli_conn = objt_conn(strm_orig(s));
Olivier Houchard00cf70f2018-11-30 17:24:55 +01001255 struct connection *srv_conn = NULL;
Willy Tarreau827aee92011-03-10 16:55:02 +01001256 struct server *srv;
Amaury Denoyelle7f68d812021-01-26 14:35:26 +01001257 const int reuse_mode = s->be->options & PR_O_REUSE_MASK;
Willy Tarreau34601a82013-12-15 16:33:46 +01001258 int reuse = 0;
Olivier Houchard5cd62172019-01-04 15:52:26 +01001259 int init_mux = 0;
Willy Tarreau9650f372009-08-16 14:02:45 +02001260 int err;
Amaury Denoyelleedadf192021-02-12 13:49:42 +01001261#ifdef USE_OPENSSL
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01001262 struct sample *sni_smp = NULL;
Amaury Denoyelleedadf192021-02-12 13:49:42 +01001263#endif
Amaury Denoyelled10a2002021-02-11 19:45:19 +01001264 struct sockaddr_storage *bind_addr;
Amaury Denoyelle1921d202021-01-14 10:15:29 +01001265 int proxy_line_ret;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001266 int64_t hash = 0;
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01001267 struct conn_hash_params hash_params;
1268
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001269 /* in standard configuration, srv will be valid
1270 * it can be NULL for dispatch mode or transparent backend */
1271 srv = objt_server(s->target);
1272
Amaury Denoyelle926712a2021-10-20 15:04:03 +02001273 if (!(s->flags & SF_ADDR_SET)) {
Christopher Faulet8da67aa2022-03-29 17:53:09 +02001274 err = alloc_dst_address(&s->csb->dst, srv, s);
Amaury Denoyelle926712a2021-10-20 15:04:03 +02001275 if (err != SRV_STATUS_OK)
1276 return SF_ERR_INTERNAL;
Amaury Denoyelle1a58aca2021-01-22 16:47:46 +01001277
Amaury Denoyelle926712a2021-10-20 15:04:03 +02001278 s->flags |= SF_ADDR_SET;
1279 }
1280
1281 err = alloc_bind_address(&bind_addr, srv, s);
1282 if (err != SRV_STATUS_OK)
1283 return SF_ERR_INTERNAL;
Amaury Denoyelle1a58aca2021-01-22 16:47:46 +01001284
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01001285#ifdef USE_OPENSSL
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01001286 if (srv && srv->ssl_ctx.sni) {
1287 sni_smp = sample_fetch_as_type(s->be, s->sess, s,
1288 SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
1289 srv->ssl_ctx.sni, SMP_T_STR);
Amaury Denoyelle926712a2021-10-20 15:04:03 +02001290 }
1291#endif
Amaury Denoyelle4c098002021-02-17 15:59:02 +01001292
Amaury Denoyelle926712a2021-10-20 15:04:03 +02001293 /* do not reuse if mode is not http */
1294 if (!IS_HTX_STRM(s)) {
1295 DBG_TRACE_STATE("skip idle connections reuse: no htx", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1296 goto skip_reuse;
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01001297 }
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01001298
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +02001299 /* disable reuse if websocket stream and the protocol to use is not the
1300 * same as the main protocol of the server.
1301 */
1302 if (unlikely(s->flags & SF_WEBSOCKET) && srv) {
1303 if (!srv_check_reuse_ws(srv)) {
1304 DBG_TRACE_STATE("skip idle connections reuse: websocket stream", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1305 goto skip_reuse;
1306 }
1307 }
1308
Amaury Denoyelle926712a2021-10-20 15:04:03 +02001309 /* first, set unique connection parameters and then calculate hash */
1310 memset(&hash_params, 0, sizeof(hash_params));
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01001311
Amaury Denoyelle926712a2021-10-20 15:04:03 +02001312 /* 1. target */
1313 hash_params.target = s->target;
1314
1315#ifdef USE_OPENSSL
1316 /* 2. sni
1317 * only test if the sample is not null as smp_make_safe (called before
1318 * ssl_sock_set_servername) can only fails if this is not the case
1319 */
1320 if (sni_smp) {
1321 hash_params.sni_prehash =
1322 conn_hash_prehash(sni_smp->data.u.str.area,
1323 sni_smp->data.u.str.data);
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01001324 }
Amaury Denoyelle926712a2021-10-20 15:04:03 +02001325#endif /* USE_OPENSSL */
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01001326
Amaury Denoyelle926712a2021-10-20 15:04:03 +02001327 /* 3. destination address */
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01001328 if (srv && (!is_addr(&srv->addr) || srv->flags & SRV_F_MAPPORTS))
Christopher Faulet8da67aa2022-03-29 17:53:09 +02001329 hash_params.dst_addr = s->csb->dst;
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01001330
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001331 /* 4. source address */
Amaury Denoyelled10a2002021-02-11 19:45:19 +01001332 hash_params.src_addr = bind_addr;
1333
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001334 /* 5. proxy protocol */
Amaury Denoyelle1921d202021-01-14 10:15:29 +01001335 if (srv && srv->pp_opts) {
1336 proxy_line_ret = make_proxy_line(trash.area, trash.size, srv, cli_conn, s);
1337 if (proxy_line_ret) {
Amaury Denoyelle36441f42021-02-17 16:25:31 +01001338 hash_params.proxy_prehash =
1339 conn_hash_prehash(trash.area, proxy_line_ret);
Amaury Denoyelle1921d202021-01-14 10:15:29 +01001340 }
1341 }
1342
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001343 hash = conn_calculate_hash(&hash_params);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01001344
Willy Tarreaub0821862019-07-18 19:26:11 +02001345 /* first, search for a matching connection in the session's idle conns */
Amaury Denoyelle293dcc42021-01-25 10:29:35 +01001346 srv_conn = session_get_conn(s->sess, s->target, hash);
Amaury Denoyelle1252b6f2021-10-20 15:22:01 +02001347 if (srv_conn) {
1348 DBG_TRACE_STATE("reuse connection from session", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Christopher Fauletfcc3d8a2020-07-01 16:36:51 +02001349 reuse = 1;
Amaury Denoyelle1252b6f2021-10-20 15:22:01 +02001350 }
Olivier Houchard00cf70f2018-11-30 17:24:55 +01001351
Amaury Denoyelle7f68d812021-01-26 14:35:26 +01001352 if (srv && !reuse && reuse_mode != PR_O_REUSE_NEVR) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01001353 /* Below we pick connections from the safe, idle or
1354 * available (which are safe too) lists based
Willy Tarreau449d74a2015-08-05 17:16:33 +02001355 * on the strategy, the fact that this is a first or second
1356 * (retryable) request, with the indicated priority (1 or 2) :
1357 *
1358 * SAFE AGGR ALWS
1359 *
1360 * +-----+-----+ +-----+-----+ +-----+-----+
1361 * req| 1st | 2nd | req| 1st | 2nd | req| 1st | 2nd |
1362 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1363 * safe| - | 2 | safe| 1 | 2 | safe| 1 | 2 |
1364 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
1365 * idle| - | 1 | idle| - | 1 | idle| 2 | 1 |
1366 * ----+-----+-----+ ----+-----+-----+ ----+-----+-----+
Willy Tarreaub0821862019-07-18 19:26:11 +02001367 *
1368 * Idle conns are necessarily looked up on the same thread so
1369 * that there is no concurrency issues.
Willy Tarreau449d74a2015-08-05 17:16:33 +02001370 */
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001371 if (!eb_is_empty(&srv->per_thr[tid].avail_conns)) {
1372 srv_conn = srv_lookup_conn(&srv->per_thr[tid].avail_conns, hash);
Amaury Denoyelle1252b6f2021-10-20 15:22:01 +02001373 if (srv_conn) {
1374 DBG_TRACE_STATE("reuse connection from avail", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001375 reuse = 1;
Amaury Denoyelle1252b6f2021-10-20 15:22:01 +02001376 }
Willy Tarreau449d74a2015-08-05 17:16:33 +02001377 }
Amaury Denoyelle1399d692021-01-22 19:37:44 +01001378
Amaury Denoyelle7f68d812021-01-26 14:35:26 +01001379 /* if no available connections found, search for an idle/safe */
Amaury Denoyelle1399d692021-01-22 19:37:44 +01001380 if (!srv_conn && srv->max_idle_conns && srv->curr_idle_conns > 0) {
Amaury Denoyelle7f68d812021-01-26 14:35:26 +01001381 const int not_first_req = s->txn && s->txn->flags & TX_NOT_FIRST;
1382 const int idle = srv->curr_idle_nb > 0;
1383 const int safe = srv->curr_safe_nb > 0;
1384
1385 /* second column of the tables above,
1386 * search for an idle then safe conn */
1387 if (not_first_req) {
1388 if (idle || safe)
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001389 srv_conn = conn_backend_get(s, srv, 0, hash);
Olivier Houchard566df302020-03-06 18:18:56 +01001390 }
Amaury Denoyelle7f68d812021-01-26 14:35:26 +01001391 /* first column of the tables above */
1392 else if (reuse_mode >= PR_O_REUSE_AGGR) {
1393 /* search for a safe conn */
1394 if (safe)
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001395 srv_conn = conn_backend_get(s, srv, 1, hash);
Amaury Denoyelleaa890ae2021-01-25 14:43:17 +01001396
1397 /* search for an idle conn if no safe conn found
1398 * on always reuse mode */
1399 if (!srv_conn &&
1400 reuse_mode == PR_O_REUSE_ALWS && idle) {
1401 /* TODO conn_backend_get should not check the
1402 * safe list is this case */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001403 srv_conn = conn_backend_get(s, srv, 0, hash);
Amaury Denoyelleaa890ae2021-01-25 14:43:17 +01001404 }
Olivier Houchard566df302020-03-06 18:18:56 +01001405 }
Amaury Denoyelle37e25bc2021-01-26 14:35:25 +01001406
Amaury Denoyelle1252b6f2021-10-20 15:22:01 +02001407 if (srv_conn) {
1408 DBG_TRACE_STATE("reuse connection from idle/safe", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Olivier Houchard566df302020-03-06 18:18:56 +01001409 reuse = 1;
Amaury Denoyelle1252b6f2021-10-20 15:22:01 +02001410 }
Olivier Houcharddc2f2752020-02-13 19:12:07 +01001411 }
Willy Tarreau8dff9982015-08-04 20:45:52 +02001412 }
1413
Willy Tarreaub0821862019-07-18 19:26:11 +02001414
1415 /* here reuse might have been set above, indicating srv_conn finally
1416 * is OK.
1417 */
Willy Tarreaub0821862019-07-18 19:26:11 +02001418
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001419 if (ha_used_fds > global.tune.pool_high_count && srv) {
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001420 struct connection *tokill_conn = NULL;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001421 struct conn_hash_node *conn_node = NULL;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001422 struct ebmb_node *node = NULL;
Olivier Houchard88698d92019-04-16 19:07:22 +02001423
1424 /* We can't reuse a connection, and e have more FDs than deemd
1425 * acceptable, attempt to kill an idling connection
1426 */
1427 /* First, try from our own idle list */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001428 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001429 node = ebmb_first(&srv->per_thr[tid].idle_conns);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001430 if (node) {
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001431 conn_node = ebmb_entry(node, struct conn_hash_node, node);
1432 tokill_conn = conn_node->conn;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001433 ebmb_delete(node);
Amaury Denoyelle65bf6002021-03-23 10:44:43 +01001434 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
1435
1436 /* Release the idle lock before calling mux->destroy.
1437 * It will in turn call srv_release_conn through
1438 * conn_free which also uses it.
1439 */
Olivier Houchard88698d92019-04-16 19:07:22 +02001440 tokill_conn->mux->destroy(tokill_conn->ctx);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001441 }
Amaury Denoyelle65bf6002021-03-23 10:44:43 +01001442 else {
1443 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
1444 }
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001445
Olivier Houchard88698d92019-04-16 19:07:22 +02001446 /* If not, iterate over other thread's idling pool, and try to grab one */
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001447 if (!tokill_conn) {
Olivier Houchard88698d92019-04-16 19:07:22 +02001448 int i;
1449
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02001450 for (i = tid; (i = ((i + 1 == global.nbthread) ? 0 : i + 1)) != tid;) {
Willy Tarreau08e2b412019-05-26 11:50:08 +02001451 // just silence stupid gcc which reports an absurd
1452 // out-of-bounds warning for <i> which is always
1453 // exactly zero without threads, but it seems to
1454 // see it possibly larger.
1455 ALREADY_CHECKED(i);
1456
Willy Tarreau9b9f8472021-03-26 20:52:10 +01001457 if (HA_SPIN_TRYLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock) != 0)
1458 continue;
1459
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001460 node = ebmb_first(&srv->per_thr[i].idle_conns);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001461 if (node) {
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001462 conn_node = ebmb_entry(node, struct conn_hash_node, node);
1463 tokill_conn = conn_node->conn;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001464 ebmb_delete(node);
1465 }
1466
1467 if (!tokill_conn) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001468 node = ebmb_first(&srv->per_thr[i].safe_conns);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001469 if (node) {
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001470 conn_node = ebmb_entry(node, struct conn_hash_node, node);
1471 tokill_conn = conn_node->conn;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001472 ebmb_delete(node);
1473 }
1474 }
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01001475 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Amaury Denoyellea3bf62e2021-01-28 10:16:29 +01001476
Olivier Houchard88698d92019-04-16 19:07:22 +02001477 if (tokill_conn) {
1478 /* We got one, put it into the concerned thread's to kill list, and wake it's kill task */
1479
Willy Tarreau2b718102021-04-21 07:32:39 +02001480 MT_LIST_APPEND(&idle_conns[i].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01001481 (struct mt_list *)&tokill_conn->toremove_list);
Willy Tarreau4d82bf52020-06-28 00:19:17 +02001482 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Olivier Houchard88698d92019-04-16 19:07:22 +02001483 break;
1484 }
1485 }
1486 }
1487
1488 }
Willy Tarreau34601a82013-12-15 16:33:46 +01001489
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001490 if (reuse) {
Willy Tarreaub0821862019-07-18 19:26:11 +02001491 if (srv_conn->mux) {
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001492 int avail = srv_conn->mux->avail_streams(srv_conn);
1493
1494 if (avail <= 1) {
Olivier Houchard2442f682018-12-01 17:03:38 +01001495 /* No more streams available, remove it from the list */
Amaury Denoyelle8990b012021-02-19 15:29:16 +01001496 conn_delete_from_tree(&srv_conn->hash_node->node);
Olivier Houchard2442f682018-12-01 17:03:38 +01001497 }
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001498
1499 if (avail >= 1) {
Christopher Faulet95a61e82021-12-22 14:22:03 +01001500 if (srv_conn->mux->attach(srv_conn, s->csb, s->sess) == -1) {
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001501 srv_conn = NULL;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001502 if (cs_reset_endp(s->csb) < 0)
1503 return SF_ERR_INTERNAL;
Christopher Faulet13a35e52021-12-20 15:34:16 +01001504 }
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001505 }
Olivier Houchard134a2042018-12-28 14:45:47 +01001506 else
1507 srv_conn = NULL;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01001508 }
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001509 /* otherwise srv_conn is left intact */
1510 }
1511 else
1512 srv_conn = NULL;
1513
Amaury Denoyelle08d87b32021-01-26 17:35:46 +01001514skip_reuse:
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001515 /* no reuse or failed to reuse the connection above, pick a new one */
1516 if (!srv_conn) {
Christopher Faulet236c93b2020-07-02 09:19:54 +02001517 srv_conn = conn_new(s->target);
Willy Tarreaudc2ac812020-07-15 17:46:32 +02001518 if (srv_conn) {
Amaury Denoyelle1252b6f2021-10-20 15:22:01 +02001519 DBG_TRACE_STATE("alloc new be connection", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Willy Tarreaudc2ac812020-07-15 17:46:32 +02001520 srv_conn->owner = s->sess;
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001521
1522 /* connection will be attached to the session if
1523 * http-reuse mode is never or it is not targeted to a
1524 * server */
1525 if (reuse_mode == PR_O_REUSE_NEVR || !srv)
Willy Tarreaudc2ac812020-07-15 17:46:32 +02001526 conn_set_private(srv_conn);
Willy Tarreau2c7dedd2019-01-24 18:22:19 +01001527
Amaury Denoyelle68967e52021-03-02 12:01:06 +01001528 /* assign bind_addr to srv_conn */
Amaury Denoyelled10a2002021-02-11 19:45:19 +01001529 srv_conn->src = bind_addr;
Amaury Denoyelle68967e52021-03-02 12:01:06 +01001530 bind_addr = NULL;
Amaury Denoyellef7bdf002021-01-21 09:40:19 +01001531
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01001532 if (!sockaddr_alloc(&srv_conn->dst, 0, 0)) {
1533 conn_free(srv_conn);
1534 return SF_ERR_RESOURCE;
1535 }
Amaury Denoyelleaee4fdb2021-10-20 15:22:20 +02001536
1537 srv_conn->hash_node->hash = hash;
Olivier Houchardc0caac22020-03-20 14:26:32 +01001538 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001539 }
1540
Amaury Denoyelle68967e52021-03-02 12:01:06 +01001541 /* if bind_addr is non NULL free it */
1542 sockaddr_free(&bind_addr);
1543
1544 /* srv_conn is still NULL only on allocation failure */
1545 if (!srv_conn)
1546 return SF_ERR_RESOURCE;
1547
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02001548 /* copy the target address into the connection */
Christopher Faulet8da67aa2022-03-29 17:53:09 +02001549 *srv_conn->dst = *s->csb->dst;
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02001550
1551 /* Copy network namespace from client connection */
1552 srv_conn->proxy_netns = cli_conn ? cli_conn->proxy_netns : NULL;
1553
Olivier Houchard1b3c9312021-03-05 23:37:48 +01001554 if (!srv_conn->xprt) {
Willy Tarreauff605db2013-12-20 11:09:51 +01001555 /* set the correct protocol on the output stream interface */
Olivier Houchard1b3c9312021-03-05 23:37:48 +01001556 if (srv) {
Willy Tarreau14e7f292021-10-27 17:41:07 +02001557 if (conn_prepare(srv_conn, protocol_lookup(srv_conn->dst->ss_family, PROTO_TYPE_STREAM, 0), srv->xprt)) {
Olivier Houchard1b3c9312021-03-05 23:37:48 +01001558 conn_free(srv_conn);
1559 return SF_ERR_INTERNAL;
1560 }
1561 } else if (obj_type(s->target) == OBJ_TYPE_PROXY) {
1562 int ret;
1563
Willy Tarreauff605db2013-12-20 11:09:51 +01001564 /* proxies exclusively run on raw_sock right now */
Willy Tarreau14e7f292021-10-27 17:41:07 +02001565 ret = conn_prepare(srv_conn, protocol_lookup(srv_conn->dst->ss_family, PROTO_TYPE_STREAM, 0), xprt_get(XPRT_RAW));
Olivier Houchard1b3c9312021-03-05 23:37:48 +01001566 if (ret < 0 || !(srv_conn->ctrl)) {
Olivier Houchardc0caac22020-03-20 14:26:32 +01001567 conn_free(srv_conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001568 return SF_ERR_INTERNAL;
Olivier Houchardc0caac22020-03-20 14:26:32 +01001569 }
Willy Tarreauff605db2013-12-20 11:09:51 +01001570 }
Olivier Houchardc0caac22020-03-20 14:26:32 +01001571 else {
1572 conn_free(srv_conn);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001573 return SF_ERR_INTERNAL; /* how did we get there ? */
Olivier Houchardc0caac22020-03-20 14:26:32 +01001574 }
Willy Tarreaud02394b2012-05-11 18:32:18 +02001575
Christopher Fauleta9e8b392022-03-23 11:01:09 +01001576 cs_attach_mux(s->csb, NULL, srv_conn);
1577 srv_conn->ctx = s->csb;
1578
Olivier Houchardecffb7d2020-01-24 14:10:55 +01001579#if defined(USE_OPENSSL) && defined(TLSEXT_TYPE_application_layer_protocol_negotiation)
Olivier Houchard12950162018-11-23 14:32:08 +01001580 if (!srv ||
Christopher Fauletb4de4202020-07-30 09:10:36 +02001581 (srv->use_ssl != 1 || (!(srv->ssl_ctx.alpn_str) && !(srv->ssl_ctx.npn_str)) ||
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01001582 srv->mux_proto || !IS_HTX_STRM(s)))
Olivier Houchard201b9f42018-11-21 00:16:29 +01001583#endif
Olivier Houchard5cd62172019-01-04 15:52:26 +01001584 init_mux = 1;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001585
Willy Tarreauff605db2013-12-20 11:09:51 +01001586 /* process the case where the server requires the PROXY protocol to be sent */
1587 srv_conn->send_proxy_ofs = 0;
Olivier Houchard522eea72017-11-03 16:27:47 +01001588
Willy Tarreau7b004922015-08-04 19:34:21 +02001589 if (srv && srv->pp_opts) {
Alexander Liu2a54bb72019-05-22 19:44:48 +08001590 srv_conn->flags |= CO_FL_SEND_PROXY;
Willy Tarreauff605db2013-12-20 11:09:51 +01001591 srv_conn->send_proxy_ofs = 1; /* must compute size */
Willy Tarreauff605db2013-12-20 11:09:51 +01001592 }
Willy Tarreau2a6e8802013-10-24 15:50:53 +02001593
Alexander Liu2a54bb72019-05-22 19:44:48 +08001594 if (srv && (srv->flags & SRV_F_SOCKS4_PROXY)) {
1595 srv_conn->send_proxy_ofs = 1;
1596 srv_conn->flags |= CO_FL_SOCKS4;
1597 }
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +02001598
1599#if defined(USE_OPENSSL) && defined(TLSEXT_TYPE_application_layer_protocol_negotiation)
1600 /* if websocket stream, try to update connection ALPN. */
1601 if (unlikely(s->flags & SF_WEBSOCKET) &&
1602 srv && srv->use_ssl && srv->ssl_ctx.alpn_str) {
1603 char *alpn = "";
1604 int force = 0;
1605
1606 switch (srv->ws) {
1607 case SRV_WS_AUTO:
1608 alpn = "\x08http/1.1";
1609 force = 0;
1610 break;
1611 case SRV_WS_H1:
1612 alpn = "\x08http/1.1";
1613 force = 1;
1614 break;
1615 case SRV_WS_H2:
1616 alpn = "\x02h2";
1617 force = 1;
1618 break;
1619 }
1620
1621 if (!conn_update_alpn(srv_conn, ist(alpn), force))
1622 DBG_TRACE_STATE("update alpn for websocket", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1623 }
1624#endif
Willy Tarreauff605db2013-12-20 11:09:51 +01001625 }
Olivier Houcharde8f5f5d2019-10-25 17:00:54 +02001626 else {
Amaury Denoyelle2b1d9172021-06-17 15:14:49 +02001627 s->flags |= SF_SRV_REUSED;
1628
Amaury Denoyelled7faa3d2021-03-05 15:27:41 +01001629 /* Currently there seems to be no known cases of xprt ready
1630 * without the mux installed here.
1631 */
1632 BUG_ON(!srv_conn->mux);
1633
Amaury Denoyelle2b1d9172021-06-17 15:14:49 +02001634 if (!(srv_conn->mux->ctl(srv_conn, MUX_STATUS, NULL) & MUX_STATUS_READY))
1635 s->flags |= SF_SRV_REUSED_ANTICIPATED;
Olivier Houcharde8f5f5d2019-10-25 17:00:54 +02001636 }
Willy Tarreaub1d67742010-03-29 19:36:59 +02001637
William Lallemandb7ff6a32012-03-02 14:35:21 +01001638 /* flag for logging source ip/port */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001639 if (strm_fe(s)->options2 & PR_O2_SRC_ADDR)
Christopher Faulet4a0114b2021-12-23 12:24:35 +01001640 cs_si(s->csb)->flags |= SI_FL_SRC_ADDR;
William Lallemandb7ff6a32012-03-02 14:35:21 +01001641
Willy Tarreau14f8e862012-08-30 22:23:13 +02001642 /* disable lingering */
1643 if (s->be->options & PR_O_TCP_NOLING)
Christopher Faulet4a0114b2021-12-23 12:24:35 +01001644 cs_si(s->csb)->flags |= SI_FL_NOLINGER;
Willy Tarreau14f8e862012-08-30 22:23:13 +02001645
Willy Tarreauf1573842018-12-14 11:35:36 +01001646 if (s->flags & SF_SRV_REUSED) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001647 _HA_ATOMIC_INC(&s->be->be_counters.reuse);
Willy Tarreaucc79ed22018-12-15 15:11:36 +01001648 if (srv)
Willy Tarreau4781b152021-04-06 13:53:36 +02001649 _HA_ATOMIC_INC(&srv->counters.reuse);
Willy Tarreauf1573842018-12-14 11:35:36 +01001650 } else {
Willy Tarreau4781b152021-04-06 13:53:36 +02001651 _HA_ATOMIC_INC(&s->be->be_counters.connect);
Willy Tarreaucc79ed22018-12-15 15:11:36 +01001652 if (srv)
Willy Tarreau4781b152021-04-06 13:53:36 +02001653 _HA_ATOMIC_INC(&srv->counters.connect);
Willy Tarreauf1573842018-12-14 11:35:36 +01001654 }
1655
Christopher Faulet4a0114b2021-12-23 12:24:35 +01001656 err = si_connect(cs_si(s->csb), srv_conn);
Willy Tarreau09e02032019-07-18 16:18:20 +02001657 if (err != SF_ERR_NONE)
1658 return err;
1659
Christopher Faulet27bd6ff2020-07-01 11:00:18 +02001660#ifdef USE_OPENSSL
Amaury Denoyelle655dec82021-06-01 17:04:10 +02001661 if (!(s->flags & SF_SRV_REUSED)) {
1662 if (smp_make_safe(sni_smp))
1663 ssl_sock_set_servername(srv_conn, sni_smp->data.u.str.area);
1664 }
Christopher Faulet27bd6ff2020-07-01 11:00:18 +02001665#endif /* USE_OPENSSL */
1666
Willy Tarreaua3b17562020-07-31 08:39:31 +02001667 /* The CO_FL_SEND_PROXY flag may have been set by the connect method,
1668 * if so, add our handshake pseudo-XPRT now.
1669 */
1670 if ((srv_conn->flags & CO_FL_HANDSHAKE)) {
1671 if (xprt_add_hs(srv_conn) < 0) {
1672 conn_full_close(srv_conn);
1673 return SF_ERR_INTERNAL;
1674 }
1675 }
Olivier Houchard1b3c9312021-03-05 23:37:48 +01001676 conn_xprt_start(srv_conn);
Willy Tarreaua3b17562020-07-31 08:39:31 +02001677
Olivier Houchard5cd62172019-01-04 15:52:26 +01001678 /* We have to defer the mux initialization until after si_connect()
1679 * has been called, as we need the xprt to have been properly
1680 * initialized, or any attempt to recv during the mux init may
1681 * fail, and flag the connection as CO_FL_ERROR.
1682 */
1683 if (init_mux) {
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +02001684 const struct mux_ops *alt_mux =
1685 likely(!(s->flags & SF_WEBSOCKET)) ? NULL : srv_get_ws_proto(srv);
Christopher Faulet95a61e82021-12-22 14:22:03 +01001686 if (conn_install_mux_be(srv_conn, s->csb, s->sess, alt_mux) < 0) {
Olivier Houchard74931142019-01-29 19:11:16 +01001687 conn_full_close(srv_conn);
Olivier Houchard5cd62172019-01-04 15:52:26 +01001688 return SF_ERR_INTERNAL;
Olivier Houchard74931142019-01-29 19:11:16 +01001689 }
Christopher Faulet1bb6afa2021-03-08 17:57:53 +01001690 if (IS_HTX_STRM(s)) {
Amaury Denoyelle08d87b32021-01-26 17:35:46 +01001691 /* If we're doing http-reuse always, and the connection
1692 * is not private with available streams (an http2
1693 * connection), add it to the available list, so that
1694 * others can use it right away. If the connection is
1695 * private or we're doing http-reuse safe and the mux
1696 * protocol supports multiplexing, add it in the
1697 * session server list.
1698 */
1699 if (srv && reuse_mode == PR_O_REUSE_ALWS &&
1700 !(srv_conn->flags & CO_FL_PRIVATE) &&
1701 srv_conn->mux->avail_streams(srv_conn) > 0) {
Willy Tarreau430bf4a2021-03-04 09:45:32 +01001702 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 +01001703 }
1704 else if (srv_conn->flags & CO_FL_PRIVATE ||
1705 (reuse_mode == PR_O_REUSE_SAFE &&
1706 srv_conn->mux->flags & MX_FL_HOL_RISK)) {
1707 /* If it fail now, the same will be done in mux->detach() callback */
1708 session_add_conn(s->sess, srv_conn, srv_conn->target);
1709 }
Christopher Faulet08016ab2020-07-01 16:10:06 +02001710 }
Olivier Houchard5cd62172019-01-04 15:52:26 +01001711 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001712
Willy Tarreaub1310492021-08-30 09:35:18 +02001713#if defined(USE_OPENSSL) && (defined(OPENSSL_IS_BORINGSSL) || (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L))
Olivier Houchard4cd2af42019-05-06 15:18:27 +02001714
Olivier Houchard8694e5b2019-06-15 00:14:05 +02001715 if (!reuse && cli_conn && srv && srv_conn->mux &&
Olivier Houchard522eea72017-11-03 16:27:47 +01001716 (srv->ssl_ctx.options & SRV_SSL_O_EARLY_DATA) &&
Olivier Houchard865d8392019-05-03 22:46:27 +02001717 /* Only attempt to use early data if either the client sent
1718 * early data, so that we know it can handle a 425, or if
1719 * we are allwoed to retry requests on early data failure, and
1720 * it's our first try
1721 */
1722 ((cli_conn->flags & CO_FL_EARLY_DATA) ||
Christopher Faulet731c8e62022-03-29 16:08:44 +02001723 ((s->be->retry_type & PR_RE_EARLY_ERROR) && !s->conn_retries)) &&
Christopher Faulet908628c2022-03-25 16:43:49 +01001724 !channel_is_empty(cs_oc(s->csb)) &&
Olivier Houchard865d8392019-05-03 22:46:27 +02001725 srv_conn->flags & CO_FL_SSL_WAIT_HS)
Olivier Houchard522eea72017-11-03 16:27:47 +01001726 srv_conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN);
Willy Tarreau46c9d3e2017-11-08 14:25:59 +01001727#endif
Olivier Houchard522eea72017-11-03 16:27:47 +01001728
Willy Tarreau14f8e862012-08-30 22:23:13 +02001729 /* set connect timeout */
Christopher Fauletae024ce2022-03-29 19:02:31 +02001730 s->conn_exp = tick_add_ifset(now_ms, s->be->timeout.connect);
Willy Tarreau14f8e862012-08-30 22:23:13 +02001731
Willy Tarreau827aee92011-03-10 16:55:02 +01001732 if (srv) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02001733 int count;
1734
Willy Tarreaue7dff022015-04-03 01:14:29 +02001735 s->flags |= SF_CURR_SESS;
Willy Tarreau1db42732021-04-06 11:44:07 +02001736 count = _HA_ATOMIC_ADD_FETCH(&srv->cur_sess, 1);
Christopher Faulet29f77e82017-06-08 14:04:45 +02001737 HA_ATOMIC_UPDATE_MAX(&srv->counters.cur_sess_max, count);
Willy Tarreau59b0fec2021-02-17 16:01:37 +01001738 if (s->be->lbprm.server_take_conn)
Willy Tarreau5941ef02021-06-18 18:29:25 +02001739 s->be->lbprm.server_take_conn(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001740 }
1741
Willy Tarreauada4c582020-03-04 16:42:03 +01001742 /* Now handle synchronously connected sockets. We know the stream-int
1743 * is at least in state SI_ST_CON. These ones typically are UNIX
Christopher Fauletaf642df2022-03-30 10:06:11 +02001744 * sockets, socket pairs, andoccasionally TCP connections on the
Willy Tarreauada4c582020-03-04 16:42:03 +01001745 * loopback on a heavily loaded system.
1746 */
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001747 if (srv_conn->flags & CO_FL_ERROR)
1748 s->csb->endp->flags |= CS_EP_ERROR;
Willy Tarreauada4c582020-03-04 16:42:03 +01001749
1750 /* If we had early data, and the handshake ended, then
1751 * we can remove the flag, and attempt to wake the task up,
1752 * in the event there's an analyser waiting for the end of
1753 * the handshake.
1754 */
1755 if (!(srv_conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)))
Christopher Faulete9e48202022-03-22 18:13:29 +01001756 s->csb->endp->flags &= ~CS_EP_WAIT_FOR_HS;
Willy Tarreauada4c582020-03-04 16:42:03 +01001757
Christopher Faulet4a0114b2021-12-23 12:24:35 +01001758 if (!si_state_in(cs_si(s->csb)->state, SI_SB_EST|SI_SB_DIS|SI_SB_CLO) &&
Willy Tarreauada4c582020-03-04 16:42:03 +01001759 (srv_conn->flags & CO_FL_WAIT_XPRT) == 0) {
Christopher Fauletae024ce2022-03-29 19:02:31 +02001760 s->conn_exp = TICK_ETERNITY;
Christopher Faulet908628c2022-03-25 16:43:49 +01001761 cs_oc(s->csb)->flags |= CF_WRITE_NULL;
Christopher Faulet4a0114b2021-12-23 12:24:35 +01001762 if (cs_si(s->csb)->state == SI_ST_CON)
1763 cs_si(s->csb)->state = SI_ST_RDY;
Willy Tarreauada4c582020-03-04 16:42:03 +01001764 }
1765
1766 /* Report EOI on the channel if it was reached from the mux point of
1767 * view.
1768 *
1769 * Note: This test is only required because si_cs_process is also the SI
1770 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
1771 * care of it.
1772 */
Christopher Faulet908628c2022-03-25 16:43:49 +01001773 if ((s->csb->endp->flags & CS_EP_EOI) && !(cs_ic(s->csb)->flags & CF_EOI))
1774 cs_ic(s->csb)->flags |= (CF_EOI|CF_READ_PARTIAL);
Willy Tarreauada4c582020-03-04 16:42:03 +01001775
Christopher Faulet3f5bcd02020-07-29 22:42:27 +02001776 /* catch all sync connect while the mux is not already installed */
1777 if (!srv_conn->mux && !(srv_conn->flags & CO_FL_WAIT_XPRT)) {
1778 if (conn_create_mux(srv_conn) < 0) {
1779 conn_full_close(srv_conn);
1780 return SF_ERR_INTERNAL;
1781 }
1782 }
1783
Willy Tarreaue7dff022015-04-03 01:14:29 +02001784 return SF_ERR_NONE; /* connection is OK */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001785}
1786
1787
Willy Tarreaubaaee002006-06-26 02:48:02 +02001788/* This function performs the "redispatch" part of a connection attempt. It
1789 * will assign a server if required, queue the connection if required, and
1790 * handle errors that might arise at this level. It can change the server
1791 * state. It will return 1 if it encounters an error, switches the server
1792 * state, or has to queue a connection. Otherwise, it will return 0 indicating
1793 * that the connection is ready to use.
1794 */
1795
Willy Tarreau87b09662015-04-03 00:22:06 +02001796int srv_redispatch_connect(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001797{
Willy Tarreau827aee92011-03-10 16:55:02 +01001798 struct server *srv;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001799 int conn_err;
1800
1801 /* We know that we don't have any connection pending, so we will
1802 * try to get a new one, and wait in this state if it's queued
1803 */
Willy Tarreau7c669d72008-06-20 15:04:11 +02001804 redispatch:
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001805 conn_err = assign_server_and_queue(s);
1806 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001807
Willy Tarreaubaaee002006-06-26 02:48:02 +02001808 switch (conn_err) {
1809 case SRV_STATUS_OK:
1810 break;
1811
Willy Tarreau7c669d72008-06-20 15:04:11 +02001812 case SRV_STATUS_FULL:
1813 /* The server has reached its maxqueue limit. Either PR_O_REDISP is set
1814 * and we can redispatch to another server, or it is not and we return
1815 * 503. This only makes sense in DIRECT mode however, because normal LB
1816 * algorithms would never select such a server, and hash algorithms
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001817 * would bring us on the same server again. Note that s->target is set
Willy Tarreau827aee92011-03-10 16:55:02 +01001818 * in this case.
Willy Tarreau7c669d72008-06-20 15:04:11 +02001819 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001820 if (((s->flags & (SF_DIRECT|SF_FORCE_PRST)) == SF_DIRECT) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001821 (s->be->options & PR_O_REDISP)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001822 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Christopher Faulet8da67aa2022-03-29 17:53:09 +02001823 sockaddr_free(&s->csb->dst);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001824 goto redispatch;
1825 }
1826
Christopher Faulet4a0114b2021-12-23 12:24:35 +01001827 if (!cs_si(s->csb)->err_type) {
1828 cs_si(s->csb)->err_type = SI_ET_QUEUE_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001829 }
Willy Tarreau7c669d72008-06-20 15:04:11 +02001830
Willy Tarreau4781b152021-04-06 13:53:36 +02001831 _HA_ATOMIC_INC(&srv->counters.failed_conns);
1832 _HA_ATOMIC_INC(&s->be->be_counters.failed_conns);
Willy Tarreau7c669d72008-06-20 15:04:11 +02001833 return 1;
1834
Willy Tarreaubaaee002006-06-26 02:48:02 +02001835 case SRV_STATUS_NOSRV:
Willy Tarreau827aee92011-03-10 16:55:02 +01001836 /* note: it is guaranteed that srv == NULL here */
Christopher Faulet4a0114b2021-12-23 12:24:35 +01001837 if (!cs_si(s->csb)->err_type) {
1838 cs_si(s->csb)->err_type = SI_ET_CONN_ERR;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001839 }
Krzysztof Piotr Oledzki5a329cf2008-02-22 03:50:19 +01001840
Willy Tarreau4781b152021-04-06 13:53:36 +02001841 _HA_ATOMIC_INC(&s->be->be_counters.failed_conns);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001842 return 1;
1843
1844 case SRV_STATUS_QUEUED:
Christopher Fauletae024ce2022-03-29 19:02:31 +02001845 s->conn_exp = tick_add_ifset(now_ms, s->be->timeout.queue);
Christopher Faulet4a0114b2021-12-23 12:24:35 +01001846 cs_si(s->csb)->state = SI_ST_QUE;
Willy Tarreau87b09662015-04-03 00:22:06 +02001847 /* do nothing else and do not wake any other stream up */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001848 return 1;
1849
Willy Tarreaubaaee002006-06-26 02:48:02 +02001850 case SRV_STATUS_INTERNAL:
1851 default:
Christopher Faulet4a0114b2021-12-23 12:24:35 +01001852 if (!cs_si(s->csb)->err_type) {
1853 cs_si(s->csb)->err_type = SI_ET_CONN_OTHER;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001854 }
1855
Willy Tarreau827aee92011-03-10 16:55:02 +01001856 if (srv)
1857 srv_inc_sess_ctr(srv);
1858 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001859 srv_set_sess_last(srv);
1860 if (srv)
Willy Tarreau4781b152021-04-06 13:53:36 +02001861 _HA_ATOMIC_INC(&srv->counters.failed_conns);
1862 _HA_ATOMIC_INC(&s->be->be_counters.failed_conns);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001863
Willy Tarreau87b09662015-04-03 00:22:06 +02001864 /* release other streams waiting for this server */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001865 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau9ab78292021-06-22 18:47:51 +02001866 process_srv_queue(srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001867 return 1;
1868 }
1869 /* if we get here, it's because we got SRV_STATUS_OK, which also
1870 * means that the connection has not been queued.
1871 */
1872 return 0;
1873}
1874
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001875/* Check if the connection request is in such a state that it can be aborted. */
1876static int back_may_abort_req(struct channel *req, struct stream *s)
1877{
1878 return ((req->flags & (CF_READ_ERROR)) ||
1879 ((req->flags & (CF_SHUTW_NOW|CF_SHUTW)) && /* empty and client aborted */
1880 (channel_is_empty(req) || (s->be->options & PR_O_ABRT_CLOSE))));
1881}
1882
1883/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
1884 * SI_ST_TAR. Other input states are simply ignored.
1885 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
1886 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
1887 * conditions.
1888 */
1889void back_try_conn_req(struct stream *s)
1890{
1891 struct server *srv = objt_server(s->target);
Christopher Faulet908628c2022-03-25 16:43:49 +01001892 struct conn_stream *cs = s->csb;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001893 struct channel *req = &s->req;
1894
1895 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1896
Christopher Faulet908628c2022-03-25 16:43:49 +01001897 if (cs->si->state == SI_ST_ASS) {
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001898 /* Server assigned to connection request, we have to try to connect now */
1899 int conn_err;
1900
1901 /* Before we try to initiate the connection, see if the
1902 * request may be aborted instead.
1903 */
1904 if (back_may_abort_req(req, s)) {
Christopher Faulet908628c2022-03-25 16:43:49 +01001905 cs->si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001906 DBG_TRACE_STATE("connection aborted", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
1907 goto abort_connection;
1908 }
1909
1910 conn_err = connect_server(s);
1911 srv = objt_server(s->target);
1912
1913 if (conn_err == SF_ERR_NONE) {
1914 /* state = SI_ST_CON or SI_ST_EST now */
1915 if (srv)
1916 srv_inc_sess_ctr(srv);
1917 if (srv)
1918 srv_set_sess_last(srv);
1919 DBG_TRACE_STATE("connection attempt", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1920 goto end;
1921 }
1922
1923 /* We have received a synchronous error. We might have to
1924 * abort, retry immediately or redispatch.
1925 */
1926 if (conn_err == SF_ERR_INTERNAL) {
Christopher Faulet908628c2022-03-25 16:43:49 +01001927 if (!cs->si->err_type) {
1928 cs->si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001929 }
1930
1931 if (srv)
1932 srv_inc_sess_ctr(srv);
1933 if (srv)
1934 srv_set_sess_last(srv);
1935 if (srv)
Willy Tarreau4781b152021-04-06 13:53:36 +02001936 _HA_ATOMIC_INC(&srv->counters.failed_conns);
1937 _HA_ATOMIC_INC(&s->be->be_counters.failed_conns);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001938
1939 /* release other streams waiting for this server */
1940 sess_change_server(s, NULL);
1941 if (may_dequeue_tasks(srv, s->be))
Willy Tarreau9ab78292021-06-22 18:47:51 +02001942 process_srv_queue(srv);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001943
1944 /* Failed and not retryable. */
Christopher Faulet908628c2022-03-25 16:43:49 +01001945 si_shutr(cs->si);
1946 si_shutw(cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001947 req->flags |= CF_WRITE_ERROR;
1948
1949 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1950
1951 /* we may need to know the position in the queue for logging */
1952 pendconn_cond_unlink(s->pend_pos);
1953
1954 /* no stream was ever accounted for this server */
Christopher Faulet908628c2022-03-25 16:43:49 +01001955 cs->si->state = SI_ST_CLO;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001956 if (s->srv_error)
Christopher Faulet908628c2022-03-25 16:43:49 +01001957 s->srv_error(s, cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001958 DBG_TRACE_STATE("internal error during connection", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
1959 goto end;
1960 }
1961
1962 /* We are facing a retryable error, but we don't want to run a
1963 * turn-around now, as the problem is likely a source port
1964 * allocation problem, so we want to retry now.
1965 */
Christopher Faulet908628c2022-03-25 16:43:49 +01001966 cs->si->state = SI_ST_CER;
Christopher Faulet6cd56d52022-03-30 10:47:32 +02001967 cs->endp->flags &= ~CS_EP_ERROR;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001968 back_handle_st_cer(s);
1969
1970 DBG_TRACE_STATE("connection error, retry", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
1971 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
1972 }
Christopher Faulet908628c2022-03-25 16:43:49 +01001973 else if (cs->si->state == SI_ST_QUE) {
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001974 /* connection request was queued, check for any update */
1975 if (!pendconn_dequeue(s)) {
1976 /* The connection is not in the queue anymore. Either
1977 * we have a server connection slot available and we
1978 * go directly to the assigned state, or we need to
1979 * load-balance first and go to the INI state.
1980 */
Christopher Fauletae024ce2022-03-29 19:02:31 +02001981 s->conn_exp = TICK_ETERNITY;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001982 if (unlikely(!(s->flags & SF_ASSIGNED)))
Christopher Faulet908628c2022-03-25 16:43:49 +01001983 cs->si->state = SI_ST_REQ;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001984 else {
1985 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Christopher Faulet908628c2022-03-25 16:43:49 +01001986 cs->si->state = SI_ST_ASS;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001987 }
1988 DBG_TRACE_STATE("dequeue connection request", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
1989 goto end;
1990 }
1991
1992 /* Connection request still in queue... */
Christopher Fauletae024ce2022-03-29 19:02:31 +02001993 if (s->flags & SF_CONN_EXP) {
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001994 /* ... and timeout expired */
Christopher Fauletae024ce2022-03-29 19:02:31 +02001995 s->conn_exp = TICK_ETERNITY;
1996 s->flags &= ~SF_CONN_EXP;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01001997 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1998
1999 /* we may need to know the position in the queue for logging */
2000 pendconn_cond_unlink(s->pend_pos);
2001
2002 if (srv)
Willy Tarreau4781b152021-04-06 13:53:36 +02002003 _HA_ATOMIC_INC(&srv->counters.failed_conns);
2004 _HA_ATOMIC_INC(&s->be->be_counters.failed_conns);
Christopher Faulet908628c2022-03-25 16:43:49 +01002005 si_shutr(cs->si);
2006 si_shutw(cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002007 req->flags |= CF_WRITE_TIMEOUT;
Christopher Faulet908628c2022-03-25 16:43:49 +01002008 if (!cs->si->err_type)
2009 cs->si->err_type = SI_ET_QUEUE_TO;
2010 cs->si->state = SI_ST_CLO;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002011 if (s->srv_error)
Christopher Faulet908628c2022-03-25 16:43:49 +01002012 s->srv_error(s, cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002013 DBG_TRACE_STATE("connection request still queued", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2014 goto end;
2015 }
2016
2017 /* Connection remains in queue, check if we have to abort it */
2018 if (back_may_abort_req(req, s)) {
2019 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
2020
2021 /* we may need to know the position in the queue for logging */
2022 pendconn_cond_unlink(s->pend_pos);
2023
Christopher Faulet908628c2022-03-25 16:43:49 +01002024 cs->si->err_type |= SI_ET_QUEUE_ABRT;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002025 DBG_TRACE_STATE("abort queued connection request", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2026 goto abort_connection;
2027 }
2028
2029 /* Nothing changed */
2030 }
Christopher Faulet908628c2022-03-25 16:43:49 +01002031 else if (cs->si->state == SI_ST_TAR) {
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002032 /* Connection request might be aborted */
2033 if (back_may_abort_req(req, s)) {
Christopher Faulet908628c2022-03-25 16:43:49 +01002034 cs->si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002035 DBG_TRACE_STATE("connection aborted", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2036 goto abort_connection;
2037 }
2038
Christopher Fauletae024ce2022-03-29 19:02:31 +02002039 if (!(s->flags & SF_CONN_EXP))
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002040 return; /* still in turn-around */
2041
Christopher Fauletae024ce2022-03-29 19:02:31 +02002042 s->flags &= ~SF_CONN_EXP;
2043 s->conn_exp = TICK_ETERNITY;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002044
2045 /* we keep trying on the same server as long as the stream is
2046 * marked "assigned".
2047 * FIXME: Should we force a redispatch attempt when the server is down ?
2048 */
2049 if (s->flags & SF_ASSIGNED)
Christopher Faulet908628c2022-03-25 16:43:49 +01002050 cs->si->state = SI_ST_ASS;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002051 else
Christopher Faulet908628c2022-03-25 16:43:49 +01002052 cs->si->state = SI_ST_REQ;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002053
2054 DBG_TRACE_STATE("retry connection now", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2055 }
2056
2057 end:
2058 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2059 return;
2060
2061abort_connection:
2062 /* give up */
Christopher Fauletae024ce2022-03-29 19:02:31 +02002063 s->conn_exp = TICK_ETERNITY;
2064 s->flags &= ~SF_CONN_EXP;
Christopher Faulet908628c2022-03-25 16:43:49 +01002065 si_shutr(cs->si);
2066 si_shutw(cs->si);
2067 cs->si->state = SI_ST_CLO;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002068 if (s->srv_error)
Christopher Faulet908628c2022-03-25 16:43:49 +01002069 s->srv_error(s, cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002070 DBG_TRACE_DEVEL("leaving on error", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2071 return;
2072}
2073
2074/* This function initiates a server connection request on a stream interface
2075 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
2076 * a real connection to a server, indicating that a server has been assigned,
2077 * or SI_ST_EST for a successful connection to an applet. It may also return
2078 * SI_ST_QUE, or SI_ST_CLO upon error.
2079 */
2080void back_handle_st_req(struct stream *s)
2081{
Christopher Faulet908628c2022-03-25 16:43:49 +01002082 struct conn_stream *cs = s->csb;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002083
Christopher Faulet908628c2022-03-25 16:43:49 +01002084 if (cs->si->state != SI_ST_REQ)
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002085 return;
2086
2087 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2088
2089 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
2090 /* the applet directly goes to the EST state */
Christopher Faulet95a61e82021-12-22 14:22:03 +01002091 struct appctx *appctx = cs_appctx(s->csb);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002092
2093 if (!appctx || appctx->applet != __objt_applet(s->target))
Christopher Faulet908628c2022-03-25 16:43:49 +01002094 appctx = si_register_handler(cs->si, objt_applet(s->target));
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002095
2096 if (!appctx) {
2097 /* No more memory, let's immediately abort. Force the
2098 * error code to ignore the ERR_LOCAL which is not a
2099 * real error.
2100 */
2101 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
2102
Christopher Faulet908628c2022-03-25 16:43:49 +01002103 si_shutr(cs->si);
2104 si_shutw(cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002105 s->req.flags |= CF_WRITE_ERROR;
Christopher Faulet908628c2022-03-25 16:43:49 +01002106 cs->si->err_type = SI_ET_CONN_RES;
2107 cs->si->state = SI_ST_CLO;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002108 if (s->srv_error)
Christopher Faulet908628c2022-03-25 16:43:49 +01002109 s->srv_error(s, cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002110 DBG_TRACE_STATE("failed to register applet", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2111 goto end;
2112 }
2113
2114 if (tv_iszero(&s->logs.tv_request))
2115 s->logs.tv_request = now;
2116 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Christopher Faulet908628c2022-03-25 16:43:49 +01002117 cs->si->state = SI_ST_EST;
2118 cs->si->err_type = SI_ET_NONE;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002119 be_set_sess_last(s->be);
2120
2121 DBG_TRACE_STATE("applet registered", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2122 /* let back_establish() finish the job */
2123 goto end;
2124 }
2125
2126 /* Try to assign a server */
2127 if (srv_redispatch_connect(s) != 0) {
2128 /* We did not get a server. Either we queued the
2129 * connection request, or we encountered an error.
2130 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002131 if (cs->si->state == SI_ST_QUE) {
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002132 DBG_TRACE_STATE("connection request queued", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2133 goto end;
2134 }
2135
2136 /* we did not get any server, let's check the cause */
Christopher Faulet908628c2022-03-25 16:43:49 +01002137 si_shutr(cs->si);
2138 si_shutw(cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002139 s->req.flags |= CF_WRITE_ERROR;
Christopher Faulet908628c2022-03-25 16:43:49 +01002140 if (!cs->si->err_type)
2141 cs->si->err_type = SI_ET_CONN_OTHER;
2142 cs->si->state = SI_ST_CLO;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002143 if (s->srv_error)
Christopher Faulet908628c2022-03-25 16:43:49 +01002144 s->srv_error(s, cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002145 DBG_TRACE_STATE("connection request failed", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2146 goto end;
2147 }
2148
2149 /* The server is assigned */
2150 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Christopher Faulet908628c2022-03-25 16:43:49 +01002151 cs->si->state = SI_ST_ASS;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002152 be_set_sess_last(s->be);
2153 DBG_TRACE_STATE("connection request assigned to a server", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2154
2155 end:
2156 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2157}
2158
2159/* This function is called with (si->state == SI_ST_CON) meaning that a
2160 * connection was attempted and that the file descriptor is already allocated.
2161 * We must check for timeout, error and abort. Possible output states are
2162 * SI_ST_CER (error), SI_ST_DIS (abort), and SI_ST_CON (no change). This only
2163 * works with connection-based streams. We know that there were no I/O event
2164 * when reaching this function. Timeouts and errors are *not* cleared.
2165 */
2166void back_handle_st_con(struct stream *s)
2167{
Christopher Faulet908628c2022-03-25 16:43:49 +01002168 struct conn_stream *cs = s->csb;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002169 struct channel *req = &s->req;
2170 struct channel *rep = &s->res;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002171
2172 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2173
2174 /* the client might want to abort */
2175 if ((rep->flags & CF_SHUTW) ||
2176 ((req->flags & CF_SHUTW_NOW) &&
2177 (channel_is_empty(req) || (s->be->options & PR_O_ABRT_CLOSE)))) {
Christopher Faulet908628c2022-03-25 16:43:49 +01002178 cs->si->flags |= SI_FL_NOLINGER;
2179 si_shutw(cs->si);
2180 cs->si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002181 if (s->srv_error)
Christopher Faulet908628c2022-03-25 16:43:49 +01002182 s->srv_error(s, cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002183 /* Note: state = SI_ST_DIS now */
2184 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 +01002185 goto end;
2186 }
2187
Willy Tarreau062df2c2020-01-10 06:17:03 +01002188 done:
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002189 /* retryable error ? */
Christopher Faulet6cd56d52022-03-30 10:47:32 +02002190 if ((s->flags & SF_CONN_EXP) || (cs->endp->flags & CS_EP_ERROR)) {
Christopher Faulet908628c2022-03-25 16:43:49 +01002191 if (!cs->si->err_type) {
Christopher Faulet6cd56d52022-03-30 10:47:32 +02002192 if (cs->endp->flags & CS_EP_ERROR)
Christopher Faulet908628c2022-03-25 16:43:49 +01002193 cs->si->err_type = SI_ET_CONN_ERR;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002194 else
Christopher Faulet908628c2022-03-25 16:43:49 +01002195 cs->si->err_type = SI_ET_CONN_TO;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002196 }
2197
Christopher Faulet908628c2022-03-25 16:43:49 +01002198 cs->si->state = SI_ST_CER;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002199 DBG_TRACE_STATE("connection failed, retry", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2200 }
2201
Willy Tarreau062df2c2020-01-10 06:17:03 +01002202 end:
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002203 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2204}
2205
2206/* This function is called with (si->state == SI_ST_CER) meaning that a
2207 * previous connection attempt has failed and that the file descriptor
2208 * has already been released. Possible causes include asynchronous error
2209 * notification and time out. Possible output states are SI_ST_CLO when
2210 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
2211 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
2212 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
2213 * marked as in error state. Timeouts and errors are cleared before retrying.
2214 */
2215void back_handle_st_cer(struct stream *s)
2216{
Christopher Faulet908628c2022-03-25 16:43:49 +01002217 struct conn_stream *cs = s->csb;
Christopher Faulet6cd56d52022-03-30 10:47:32 +02002218 int must_tar = (cs->endp->flags & CS_EP_ERROR);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002219
2220 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2221
Christopher Fauletae024ce2022-03-29 19:02:31 +02002222 s->conn_exp = TICK_ETERNITY;
2223 s->flags &= ~SF_CONN_EXP;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002224
Christopher Faulet731c8e62022-03-29 16:08:44 +02002225 s->conn_retries++;
2226
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002227 /* we probably have to release last stream from the server */
2228 if (objt_server(s->target)) {
Christopher Faulet908628c2022-03-25 16:43:49 +01002229 struct connection *conn = cs_conn(cs);
Christopher Faulet0256da12021-12-15 09:50:17 +01002230
Willy Tarreau88bc8002021-12-06 07:01:02 +00002231 health_adjust(__objt_server(s->target), HANA_STATUS_L4_ERR);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002232
2233 if (s->flags & SF_CURR_SESS) {
2234 s->flags &= ~SF_CURR_SESS;
Willy Tarreau4781b152021-04-06 13:53:36 +02002235 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002236 }
2237
Christopher Faulet6cd56d52022-03-30 10:47:32 +02002238 if ((cs->endp->flags & CS_EP_ERROR) &&
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002239 conn && conn->err_code == CO_ER_SSL_MISMATCH_SNI) {
2240 /* We tried to connect to a server which is configured
2241 * with "verify required" and which doesn't have the
2242 * "verifyhost" directive. The server presented a wrong
2243 * certificate (a certificate for an unexpected name),
2244 * which implies that we have used SNI in the handshake,
2245 * and that the server doesn't have the associated cert
2246 * and presented a default one.
2247 *
2248 * This is a serious enough issue not to retry. It's
2249 * especially important because this wrong name might
2250 * either be the result of a configuration error, and
2251 * retrying will only hammer the server, or is caused
2252 * by the use of a wrong SNI value, most likely
2253 * provided by the client and we don't want to let the
2254 * client provoke retries.
2255 */
Christopher Faulet731c8e62022-03-29 16:08:44 +02002256 s->conn_retries = s->be->conn_retries;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002257 DBG_TRACE_DEVEL("Bad SSL cert, disable connection retries", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2258 }
2259 }
2260
2261 /* ensure that we have enough retries left */
Christopher Faulet731c8e62022-03-29 16:08:44 +02002262 if (s->conn_retries >= s->be->conn_retries || !(s->be->retry_type & PR_RE_CONN_FAILED)) {
Christopher Faulet908628c2022-03-25 16:43:49 +01002263 if (!cs->si->err_type) {
2264 cs->si->err_type = SI_ET_CONN_ERR;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002265 }
2266
2267 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002268 _HA_ATOMIC_INC(&objt_server(s->target)->counters.failed_conns);
2269 _HA_ATOMIC_INC(&s->be->be_counters.failed_conns);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002270 sess_change_server(s, NULL);
2271 if (may_dequeue_tasks(objt_server(s->target), s->be))
Willy Tarreau9ab78292021-06-22 18:47:51 +02002272 process_srv_queue(objt_server(s->target));
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002273
2274 /* shutw is enough so stop a connecting socket */
Christopher Faulet908628c2022-03-25 16:43:49 +01002275 si_shutw(cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002276 s->req.flags |= CF_WRITE_ERROR;
2277 s->res.flags |= CF_READ_ERROR;
2278
Christopher Faulet908628c2022-03-25 16:43:49 +01002279 cs->si->state = SI_ST_CLO;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002280 if (s->srv_error)
Christopher Faulet908628c2022-03-25 16:43:49 +01002281 s->srv_error(s, cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002282
2283 DBG_TRACE_STATE("connection failed", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2284 goto end;
2285 }
2286
Christopher Fauletf822dec2021-06-01 14:06:05 +02002287 /* At this stage, we will trigger a connection retry (with or without
Christopher Faulete00ad352021-12-16 14:44:31 +01002288 * redispatch). Thus we must reset the SI endpoint on the server side
Christopher Fauletf822dec2021-06-01 14:06:05 +02002289 * an close the attached connection. It is especially important to do it
2290 * now if the retry is not immediately performed, to be sure to release
Ilya Shipitsin213bb992021-06-12 15:55:27 +05002291 * resources as soon as possible and to not catch errors from the lower
Christopher Fauletf822dec2021-06-01 14:06:05 +02002292 * layers in an unexpected state (i.e < ST_CONN).
2293 *
2294 * Note: the stream-interface will be switched to ST_REQ, ST_ASS or
Christopher Faulet6cd56d52022-03-30 10:47:32 +02002295 * ST_TAR and CS_EP_ERROR and SF_CONN_EXP flags will be unset.
Christopher Fauletf822dec2021-06-01 14:06:05 +02002296 */
Christopher Faulet908628c2022-03-25 16:43:49 +01002297 if (cs_reset_endp(cs) < 0) {
2298 if (!cs->si->err_type)
2299 cs->si->err_type = SI_ET_CONN_OTHER;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01002300
2301 if (objt_server(s->target))
2302 _HA_ATOMIC_INC(&objt_server(s->target)->counters.internal_errors);
2303 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
2304 sess_change_server(s, NULL);
2305 if (may_dequeue_tasks(objt_server(s->target), s->be))
2306 process_srv_queue(objt_server(s->target));
2307
2308 /* shutw is enough so stop a connecting socket */
Christopher Faulet908628c2022-03-25 16:43:49 +01002309 si_shutw(cs->si);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01002310 s->req.flags |= CF_WRITE_ERROR;
2311 s->res.flags |= CF_READ_ERROR;
2312
Christopher Faulet908628c2022-03-25 16:43:49 +01002313 cs->si->state = SI_ST_CLO;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01002314 if (s->srv_error)
Christopher Faulet908628c2022-03-25 16:43:49 +01002315 s->srv_error(s, cs->si);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01002316
2317 DBG_TRACE_STATE("error resetting endpoint", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2318 goto end;
2319 }
Christopher Fauletf822dec2021-06-01 14:06:05 +02002320
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002321 stream_choose_redispatch(s);
2322
Christopher Faulet6cd56d52022-03-30 10:47:32 +02002323 if (must_tar) {
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002324 /* The error was an asynchronous connection error, and we will
2325 * likely have to retry connecting to the same server, most
2326 * likely leading to the same result. To avoid this, we wait
2327 * MIN(one second, connect timeout) before retrying. We don't
2328 * do it when the failure happened on a reused connection
2329 * though.
2330 */
2331
2332 int delay = 1000;
Amaury Denoyelle2b1d9172021-06-17 15:14:49 +02002333 const int reused = (s->flags & SF_SRV_REUSED) &&
2334 !(s->flags & SF_SRV_REUSED_ANTICIPATED);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002335
2336 if (s->be->timeout.connect && s->be->timeout.connect < delay)
2337 delay = s->be->timeout.connect;
2338
Christopher Faulet908628c2022-03-25 16:43:49 +01002339 if (!cs->si->err_type)
2340 cs->si->err_type = SI_ET_CONN_ERR;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002341
2342 /* only wait when we're retrying on the same server */
Christopher Faulet908628c2022-03-25 16:43:49 +01002343 if ((cs->si->state == SI_ST_ASS ||
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002344 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
Amaury Denoyelle2b1d9172021-06-17 15:14:49 +02002345 (s->be->srv_act <= 1)) && !reused) {
Christopher Faulet908628c2022-03-25 16:43:49 +01002346 cs->si->state = SI_ST_TAR;
Christopher Fauletae024ce2022-03-29 19:02:31 +02002347 s->conn_exp = tick_add(now_ms, MS_TO_TICKS(delay));
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002348 }
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002349 DBG_TRACE_STATE("retry a new connection", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2350 }
2351
2352 end:
2353 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2354}
2355
2356/* This function is called with (si->state == SI_ST_RDY) meaning that a
2357 * connection was attempted, that the file descriptor is already allocated,
2358 * and that it has succeeded. We must still check for errors and aborts.
2359 * Possible output states are SI_ST_EST (established), SI_ST_CER (error),
2360 * and SI_ST_DIS (abort). This only works with connection-based streams.
2361 * Timeouts and errors are *not* cleared.
2362 */
2363void back_handle_st_rdy(struct stream *s)
2364{
Christopher Faulet908628c2022-03-25 16:43:49 +01002365 struct conn_stream *cs = s->csb;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002366 struct channel *req = &s->req;
2367 struct channel *rep = &s->res;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002368
2369 DBG_TRACE_ENTER(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2370 /* We know the connection at least succeeded, though it could have
2371 * since met an error for any other reason. At least it didn't time out
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07002372 * even though the timeout might have been reported right after success.
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002373 * We need to take care of various situations here :
2374 * - everything might be OK. We have to switch to established.
2375 * - an I/O error might have been reported after a successful transfer,
2376 * which is not retryable and needs to be logged correctly, and needs
2377 * established as well
2378 * - SI_ST_CON implies !CF_WROTE_DATA but not conversely as we could
2379 * have validated a connection with incoming data (e.g. TCP with a
2380 * banner protocol), or just a successful connect() probe.
2381 * - the client might have requested a connection abort, this needs to
2382 * be checked before we decide to retry anything.
2383 */
2384
2385 /* it's still possible to handle client aborts or connection retries
2386 * before any data were sent.
2387 */
2388 if (!(req->flags & CF_WROTE_DATA)) {
2389 /* client abort ? */
2390 if ((rep->flags & CF_SHUTW) ||
2391 ((req->flags & CF_SHUTW_NOW) &&
2392 (channel_is_empty(req) || (s->be->options & PR_O_ABRT_CLOSE)))) {
2393 /* give up */
Christopher Faulet908628c2022-03-25 16:43:49 +01002394 cs->si->flags |= SI_FL_NOLINGER;
2395 si_shutw(cs->si);
2396 cs->si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002397 if (s->srv_error)
Christopher Faulet908628c2022-03-25 16:43:49 +01002398 s->srv_error(s, cs->si);
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002399 DBG_TRACE_STATE("client abort during connection attempt", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2400 goto end;
2401 }
2402
2403 /* retryable error ? */
Christopher Faulet6cd56d52022-03-30 10:47:32 +02002404 if (cs->endp->flags & CS_EP_ERROR) {
Christopher Faulet908628c2022-03-25 16:43:49 +01002405 if (!cs->si->err_type)
2406 cs->si->err_type = SI_ET_CONN_ERR;
2407 cs->si->state = SI_ST_CER;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002408 DBG_TRACE_STATE("connection failed, retry", STRM_EV_STRM_PROC|STRM_EV_SI_ST|STRM_EV_STRM_ERR, s);
2409 goto end;
2410 }
2411 }
2412
2413 /* data were sent and/or we had no error, back_establish() will
2414 * now take over.
2415 */
2416 DBG_TRACE_STATE("connection established", STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
Christopher Faulet908628c2022-03-25 16:43:49 +01002417 cs->si->err_type = SI_ET_NONE;
2418 cs->si->state = SI_ST_EST;
Willy Tarreau3a9312a2020-01-09 18:43:15 +01002419
2420 end:
2421 DBG_TRACE_LEAVE(STRM_EV_STRM_PROC|STRM_EV_SI_ST, s);
2422}
2423
Willy Tarreau4aac7db2014-05-16 11:48:10 +02002424/* sends a log message when a backend goes down, and also sets last
2425 * change date.
2426 */
2427void set_backend_down(struct proxy *be)
2428{
2429 be->last_change = now.tv_sec;
Willy Tarreau4781b152021-04-06 13:53:36 +02002430 _HA_ATOMIC_INC(&be->down_trans);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02002431
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01002432 if (!(global.mode & MODE_STARTING)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002433 ha_alert("%s '%s' has no server available!\n", proxy_type_str(be), be->id);
Willy Tarreau6fb8dc12016-11-03 19:42:36 +01002434 send_log(be, LOG_EMERG, "%s %s has no server available!\n", proxy_type_str(be), be->id);
2435 }
Willy Tarreau4aac7db2014-05-16 11:48:10 +02002436}
2437
Willy Tarreau87b09662015-04-03 00:22:06 +02002438/* Apply RDP cookie persistence to the current stream. For this, the function
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002439 * tries to extract an RDP cookie from the request buffer, and look for the
2440 * matching server in the list. If the server is found, it is assigned to the
Willy Tarreau87b09662015-04-03 00:22:06 +02002441 * stream. This always returns 1, and the analyser removes itself from the
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002442 * list. Nothing is performed if a server was already assigned.
2443 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002444int tcp_persist_rdp_cookie(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002445{
2446 struct proxy *px = s->be;
2447 int ret;
Willy Tarreau37406352012-04-23 16:16:37 +02002448 struct sample smp;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002449 struct server *srv = px->srv;
Willy Tarreau04276f32017-01-06 17:41:29 +01002450 uint16_t port;
2451 uint32_t addr;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002452 char *p;
2453
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002454 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002455
Willy Tarreaue7dff022015-04-03 01:14:29 +02002456 if (s->flags & SF_ASSIGNED)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002457 goto no_cookie;
2458
Willy Tarreau37406352012-04-23 16:16:37 +02002459 memset(&smp, 0, sizeof(smp));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002460
Willy Tarreaucadd8c92013-07-22 18:09:52 +02002461 ret = fetch_rdp_cookie_name(s, &smp, s->be->rdp_cookie_name, s->be->rdp_cookie_len);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002462 if (ret == 0 || (smp.flags & SMP_F_MAY_CHANGE) || smp.data.u.str.data == 0)
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002463 goto no_cookie;
2464
Willy Tarreau04276f32017-01-06 17:41:29 +01002465 /* Considering an rdp cookie detected using acl, str ended with <cr><lf> and should return.
2466 * The cookie format is <ip> "." <port> where "ip" is the integer corresponding to the
2467 * server's IP address in network order, and "port" is the integer corresponding to the
2468 * server's port in network order. Comments please Emeric.
2469 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002470 addr = strtoul(smp.data.u.str.area, &p, 10);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002471 if (*p != '.')
2472 goto no_cookie;
2473 p++;
Willy Tarreau04276f32017-01-06 17:41:29 +01002474
2475 port = ntohs(strtoul(p, &p, 10));
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002476 if (*p != '.')
2477 goto no_cookie;
2478
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002479 s->target = NULL;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002480 while (srv) {
Willy Tarreau28e9d062014-05-09 22:47:50 +02002481 if (srv->addr.ss_family == AF_INET &&
Willy Tarreau04276f32017-01-06 17:41:29 +01002482 port == srv->svc_port &&
2483 addr == ((struct sockaddr_in *)&srv->addr)->sin_addr.s_addr) {
Emeric Brun52a91d32017-08-31 14:41:55 +02002484 if ((srv->cur_state != SRV_ST_STOPPED) || (px->options & PR_O_PERSIST)) {
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002485 /* we found the server and it is usable */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002486 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002487 s->target = &srv->obj_type;
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002488 break;
2489 }
2490 }
2491 srv = srv->next;
2492 }
2493
2494no_cookie:
2495 req->analysers &= ~an_bit;
2496 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002497 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau44b90cc2010-05-24 20:27:29 +02002498 return 1;
2499}
2500
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002501int be_downtime(struct proxy *px) {
Willy Tarreaub625a082007-11-26 01:15:43 +01002502 if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002503 return px->down_time;
2504
2505 return now.tv_sec - px->last_change + px->down_time;
2506}
Willy Tarreaubaaee002006-06-26 02:48:02 +02002507
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002508/*
2509 * This function returns a string containing the balancing
2510 * mode of the proxy in a format suitable for stats.
2511 */
2512
2513const char *backend_lb_algo_str(int algo) {
2514
2515 if (algo == BE_LB_ALGO_RR)
2516 return "roundrobin";
2517 else if (algo == BE_LB_ALGO_SRR)
2518 return "static-rr";
Willy Tarreauf09c6602012-02-13 17:12:08 +01002519 else if (algo == BE_LB_ALGO_FAS)
2520 return "first";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002521 else if (algo == BE_LB_ALGO_LC)
2522 return "leastconn";
2523 else if (algo == BE_LB_ALGO_SH)
2524 return "source";
2525 else if (algo == BE_LB_ALGO_UH)
2526 return "uri";
2527 else if (algo == BE_LB_ALGO_PH)
2528 return "url_param";
2529 else if (algo == BE_LB_ALGO_HH)
2530 return "hdr";
2531 else if (algo == BE_LB_ALGO_RCH)
2532 return "rdp-cookie";
Willy Tarreaub3e111b2016-11-26 15:52:04 +01002533 else if (algo == BE_LB_ALGO_NONE)
2534 return "none";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002535 else
Willy Tarreaub3e111b2016-11-26 15:52:04 +01002536 return "unknown";
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002537}
2538
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002539/* This function parses a "balance" statement in a backend section describing
2540 * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002541 * returns -1, it will write an error message into the <err> buffer which will
2542 * automatically be allocated and must be passed as NULL. The trailing '\n'
2543 * will not be written. The function must be called with <args> pointing to the
2544 * first word after "balance".
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002545 */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002546int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002547{
2548 if (!*(args[0])) {
2549 /* if no option is set, use round-robin by default */
Willy Tarreau31682232007-11-29 15:38:04 +01002550 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2551 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002552 return 0;
2553 }
2554
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002555 if (strcmp(args[0], "roundrobin") == 0) {
Willy Tarreau31682232007-11-29 15:38:04 +01002556 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2557 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002558 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002559 else if (strcmp(args[0], "static-rr") == 0) {
Willy Tarreau9757a382009-10-03 12:56:50 +02002560 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2561 curproxy->lbprm.algo |= BE_LB_ALGO_SRR;
2562 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002563 else if (strcmp(args[0], "first") == 0) {
Willy Tarreauf09c6602012-02-13 17:12:08 +01002564 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2565 curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
2566 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002567 else if (strcmp(args[0], "leastconn") == 0) {
Willy Tarreau51406232008-03-10 22:04:20 +01002568 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2569 curproxy->lbprm.algo |= BE_LB_ALGO_LC;
2570 }
Willy Tarreau21c741a2019-01-14 18:14:27 +01002571 else if (!strncmp(args[0], "random", 6)) {
Willy Tarreau760e81d2018-05-03 07:20:40 +02002572 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2573 curproxy->lbprm.algo |= BE_LB_ALGO_RND;
Willy Tarreau21c741a2019-01-14 18:14:27 +01002574 curproxy->lbprm.arg_opt1 = 2;
2575
2576 if (*(args[0] + 6) == '(' && *(args[0] + 7) != ')') { /* number of draws */
2577 const char *beg;
2578 char *end;
2579
2580 beg = args[0] + 7;
2581 curproxy->lbprm.arg_opt1 = strtol(beg, &end, 0);
2582
2583 if (*end != ')') {
2584 if (!*end)
2585 memprintf(err, "random : missing closing parenthesis.");
2586 else
2587 memprintf(err, "random : unexpected character '%c' after argument.", *end);
2588 return -1;
2589 }
2590
2591 if (curproxy->lbprm.arg_opt1 < 1) {
2592 memprintf(err, "random : number of draws must be at least 1.");
2593 return -1;
2594 }
2595 }
Willy Tarreau760e81d2018-05-03 07:20:40 +02002596 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002597 else if (strcmp(args[0], "source") == 0) {
Willy Tarreau31682232007-11-29 15:38:04 +01002598 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2599 curproxy->lbprm.algo |= BE_LB_ALGO_SH;
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002600 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002601 else if (strcmp(args[0], "uri") == 0) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002602 int arg = 1;
2603
Willy Tarreau31682232007-11-29 15:38:04 +01002604 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2605 curproxy->lbprm.algo |= BE_LB_ALGO_UH;
Willy Tarreau57a37412020-09-23 08:56:29 +02002606 curproxy->lbprm.arg_opt1 = 0; // "whole", "path-only"
Willy Tarreaua9a72492019-01-14 16:14:15 +01002607 curproxy->lbprm.arg_opt2 = 0; // "len"
2608 curproxy->lbprm.arg_opt3 = 0; // "depth"
Oskar Stolc8dc41842012-05-19 10:19:54 +01002609
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002610 while (*args[arg]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002611 if (strcmp(args[arg], "len") == 0) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002612 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002613 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002614 return -1;
2615 }
Willy Tarreaua9a72492019-01-14 16:14:15 +01002616 curproxy->lbprm.arg_opt2 = atoi(args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002617 arg += 2;
2618 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002619 else if (strcmp(args[arg], "depth") == 0) {
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002620 if (!*args[arg+1] || (atoi(args[arg+1]) <= 0)) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002621 memprintf(err, "%s : '%s' expects a positive integer (got '%s').", args[0], args[arg], args[arg+1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002622 return -1;
2623 }
2624 /* hint: we store the position of the ending '/' (depth+1) so
2625 * that we avoid a comparison while computing the hash.
2626 */
Willy Tarreaua9a72492019-01-14 16:14:15 +01002627 curproxy->lbprm.arg_opt3 = atoi(args[arg+1]) + 1;
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002628 arg += 2;
2629 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002630 else if (strcmp(args[arg], "whole") == 0) {
Willy Tarreau3d1119d2020-09-23 08:05:47 +02002631 curproxy->lbprm.arg_opt1 |= 1;
Oskar Stolc8dc41842012-05-19 10:19:54 +01002632 arg += 1;
2633 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002634 else if (strcmp(args[arg], "path-only") == 0) {
Willy Tarreau57a37412020-09-23 08:56:29 +02002635 curproxy->lbprm.arg_opt1 |= 2;
2636 arg += 1;
2637 }
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002638 else {
Willy Tarreau57a37412020-09-23 08:56:29 +02002639 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 +02002640 return -1;
2641 }
2642 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002643 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002644 else if (strcmp(args[0], "url_param") == 0) {
Willy Tarreau01732802007-11-01 22:48:15 +01002645 if (!*args[1]) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002646 memprintf(err, "%s requires an URL parameter name.", args[0]);
Willy Tarreau01732802007-11-01 22:48:15 +01002647 return -1;
2648 }
Willy Tarreau31682232007-11-29 15:38:04 +01002649 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2650 curproxy->lbprm.algo |= BE_LB_ALGO_PH;
Willy Tarreaua534fea2008-08-03 12:19:50 +02002651
Willy Tarreau4c03d1c2019-01-14 15:23:54 +01002652 free(curproxy->lbprm.arg_str);
2653 curproxy->lbprm.arg_str = strdup(args[1]);
2654 curproxy->lbprm.arg_len = strlen(args[1]);
Marek Majkowski9c30fc12008-04-27 23:25:55 +02002655 if (*args[2]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002656 if (strcmp(args[2], "check_post") != 0) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002657 memprintf(err, "%s only accepts 'check_post' modifier (got '%s').", args[0], args[2]);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002658 return -1;
2659 }
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002660 }
Benoitaffb4812009-03-25 13:02:10 +01002661 }
2662 else if (!strncmp(args[0], "hdr(", 4)) {
2663 const char *beg, *end;
2664
2665 beg = args[0] + 4;
2666 end = strchr(beg, ')');
2667
2668 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002669 memprintf(err, "hdr requires an http header field name.");
Benoitaffb4812009-03-25 13:02:10 +01002670 return -1;
2671 }
2672
2673 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2674 curproxy->lbprm.algo |= BE_LB_ALGO_HH;
2675
Willy Tarreau484ff072019-01-14 15:28:53 +01002676 free(curproxy->lbprm.arg_str);
2677 curproxy->lbprm.arg_len = end - beg;
2678 curproxy->lbprm.arg_str = my_strndup(beg, end - beg);
Willy Tarreau9fed8582019-01-14 16:04:54 +01002679 curproxy->lbprm.arg_opt1 = 0;
Benoitaffb4812009-03-25 13:02:10 +01002680
2681 if (*args[1]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002682 if (strcmp(args[1], "use_domain_only") != 0) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002683 memprintf(err, "%s only accepts 'use_domain_only' modifier (got '%s').", args[0], args[1]);
Benoitaffb4812009-03-25 13:02:10 +01002684 return -1;
2685 }
Willy Tarreau9fed8582019-01-14 16:04:54 +01002686 curproxy->lbprm.arg_opt1 = 1;
Benoitaffb4812009-03-25 13:02:10 +01002687 }
Emeric Brun736aa232009-06-30 17:56:00 +02002688 }
2689 else if (!strncmp(args[0], "rdp-cookie", 10)) {
2690 curproxy->lbprm.algo &= ~BE_LB_ALGO;
2691 curproxy->lbprm.algo |= BE_LB_ALGO_RCH;
2692
2693 if ( *(args[0] + 10 ) == '(' ) { /* cookie name */
2694 const char *beg, *end;
2695
2696 beg = args[0] + 11;
2697 end = strchr(beg, ')');
2698
2699 if (!end || end == beg) {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002700 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02002701 return -1;
2702 }
2703
Willy Tarreau484ff072019-01-14 15:28:53 +01002704 free(curproxy->lbprm.arg_str);
2705 curproxy->lbprm.arg_str = my_strndup(beg, end - beg);
2706 curproxy->lbprm.arg_len = end - beg;
Emeric Brun736aa232009-06-30 17:56:00 +02002707 }
2708 else if ( *(args[0] + 10 ) == '\0' ) { /* default cookie name 'mstshash' */
Willy Tarreau484ff072019-01-14 15:28:53 +01002709 free(curproxy->lbprm.arg_str);
2710 curproxy->lbprm.arg_str = strdup("mstshash");
2711 curproxy->lbprm.arg_len = strlen(curproxy->lbprm.arg_str);
Emeric Brun736aa232009-06-30 17:56:00 +02002712 }
2713 else { /* syntax */
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002714 memprintf(err, "rdp-cookie : missing cookie name.");
Emeric Brun736aa232009-06-30 17:56:00 +02002715 return -1;
2716 }
Willy Tarreau01732802007-11-01 22:48:15 +01002717 }
Willy Tarreaua0cbda62007-11-01 21:39:54 +01002718 else {
Willy Tarreaua93c74b2012-05-08 18:14:39 +02002719 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 +01002720 return -1;
2721 }
2722 return 0;
2723}
2724
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002725
2726/************************************************************************/
Willy Tarreau1a7eca12013-01-07 22:38:03 +01002727/* All supported sample and ACL keywords must be declared here. */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002728/************************************************************************/
2729
Willy Tarreau34db1082012-04-19 17:16:54 +02002730/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002731 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02002732 * undefined behaviour.
2733 */
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002734static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002735smp_fetch_nbsrv(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002736{
Christopher Faulet37a9e212021-10-12 18:48:05 +02002737 struct proxy *px = args->data.prx;
2738
2739 if (px == NULL)
2740 return 0;
2741 if (px->cap & PR_CAP_DEF)
2742 px = smp->px;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002743
Willy Tarreau37406352012-04-23 16:16:37 +02002744 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002745 smp->data.type = SMP_T_SINT;
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002746
Nenad Merdanovic2754fbc2017-03-12 21:56:56 +01002747 smp->data.u.sint = be_usable_srv(px);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01002748
2749 return 1;
2750}
2751
Willy Tarreau37406352012-04-23 16:16:37 +02002752/* report in smp->flags a success or failure depending on the designated
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002753 * server's state. There is no match function involved since there's no pattern.
Willy Tarreau34db1082012-04-19 17:16:54 +02002754 * Accepts exactly 1 argument. Argument is a server, other types will lead to
2755 * undefined behaviour.
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002756 */
2757static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002758smp_fetch_srv_is_up(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002759{
Willy Tarreau24e32d82012-04-23 23:55:44 +02002760 struct server *srv = args->data.srv;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002761
Willy Tarreau37406352012-04-23 16:16:37 +02002762 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002763 smp->data.type = SMP_T_BOOL;
Emeric Brun52a91d32017-08-31 14:41:55 +02002764 if (!(srv->cur_admin & SRV_ADMF_MAINT) &&
2765 (!(srv->check.state & CHK_ST_CONFIGURED) || (srv->cur_state != SRV_ST_STOPPED)))
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002766 smp->data.u.sint = 1;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002767 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002768 smp->data.u.sint = 0;
Willy Tarreau0b1cd942010-05-16 22:18:27 +02002769 return 1;
2770}
2771
Willy Tarreau34db1082012-04-19 17:16:54 +02002772/* set temp integer to the number of enabled servers on the proxy.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002773 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02002774 * undefined behaviour.
2775 */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002776static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002777smp_fetch_connslots(const struct arg *args, struct sample *smp, const char *kw, void *private)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002778{
2779 struct server *iterator;
Christopher Faulet37a9e212021-10-12 18:48:05 +02002780 struct proxy *px = args->data.prx;
2781
2782 if (px == NULL)
2783 return 0;
2784 if (px->cap & PR_CAP_DEF)
2785 px = smp->px;
Willy Tarreaud28c3532012-04-19 19:28:33 +02002786
Willy Tarreau37406352012-04-23 16:16:37 +02002787 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002788 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002789 smp->data.u.sint = 0;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002790
Christopher Faulet37a9e212021-10-12 18:48:05 +02002791 for (iterator = px->srv; iterator; iterator = iterator->next) {
Emeric Brun52a91d32017-08-31 14:41:55 +02002792 if (iterator->cur_state == SRV_ST_STOPPED)
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002793 continue;
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002794
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002795 if (iterator->maxconn == 0 || iterator->maxqueue == 0) {
Willy Tarreaua5e37562011-12-16 17:06:15 +01002796 /* configuration is stupid */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002797 smp->data.u.sint = -1; /* FIXME: stupid value! */
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002798 return 1;
2799 }
2800
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002801 smp->data.u.sint += (iterator->maxconn - iterator->cur_sess)
Willy Tarreaua0570452021-06-18 09:30:30 +02002802 + (iterator->maxqueue - iterator->queue.length);
Jeffrey 'jf' Lim5051d7b2008-09-04 01:03:03 +08002803 }
2804
2805 return 1;
2806}
2807
Willy Tarreaua5e37562011-12-16 17:06:15 +01002808/* set temp integer to the id of the backend */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01002809static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002810smp_fetch_be_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02002811{
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)
Willy Tarreaube508f12016-03-10 11:47:01 +01002819 return 0;
2820
Willy Tarreauf853c462012-04-23 18:53:56 +02002821 smp->flags = SMP_F_VOL_TXN;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002822 smp->data.type = SMP_T_SINT;
Christopher Fauletd1b44642020-04-30 09:51:15 +02002823 smp->data.u.sint = px->uuid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01002824 return 1;
2825}
2826
Marcin Deranekd2471c22016-12-12 14:08:05 +01002827/* set string to the name of the backend */
2828static int
2829smp_fetch_be_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
2830{
Christopher Fauletd1b44642020-04-30 09:51:15 +02002831 struct proxy *px = NULL;
2832
2833 if (smp->strm)
2834 px = smp->strm->be;
Christopher Fauletf98e6262020-05-06 09:42:04 +02002835 else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd1b44642020-04-30 09:51:15 +02002836 px = __objt_check(smp->sess->origin)->proxy;
2837 if (!px)
Marcin Deranekd2471c22016-12-12 14:08:05 +01002838 return 0;
2839
Christopher Fauletd1b44642020-04-30 09:51:15 +02002840 smp->data.u.str.area = (char *)px->id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002841 if (!smp->data.u.str.area)
Marcin Deranekd2471c22016-12-12 14:08:05 +01002842 return 0;
2843
2844 smp->data.type = SMP_T_STR;
2845 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002846 smp->data.u.str.data = strlen(smp->data.u.str.area);
Marcin Deranekd2471c22016-12-12 14:08:05 +01002847
2848 return 1;
2849}
2850
Willy Tarreaua5e37562011-12-16 17:06:15 +01002851/* set temp integer to the id of the server */
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01002852static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002853smp_fetch_srv_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau37406352012-04-23 16:16:37 +02002854{
Christopher Fauletd1b44642020-04-30 09:51:15 +02002855 struct server *srv = NULL;
Willy Tarreaube508f12016-03-10 11:47:01 +01002856
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)
Willy Tarreau17af4192011-02-23 14:27:06 +01002862 return 0;
2863
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002864 smp->data.type = SMP_T_SINT;
Christopher Fauletd1b44642020-04-30 09:51:15 +02002865 smp->data.u.sint = srv->puid;
Hervé COMMOWICK35ed8012010-12-15 14:04:51 +01002866
2867 return 1;
2868}
2869
vkill1dfd1652019-10-30 16:58:14 +08002870/* set string to the name of the server */
2871static int
2872smp_fetch_srv_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
2873{
Christopher Fauletd1b44642020-04-30 09:51:15 +02002874 struct server *srv = NULL;
vkill1dfd1652019-10-30 16:58:14 +08002875
Christopher Fauletd1b44642020-04-30 09:51:15 +02002876 if (smp->strm)
2877 srv = objt_server(smp->strm->target);
Christopher Fauletf98e6262020-05-06 09:42:04 +02002878 else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd1b44642020-04-30 09:51:15 +02002879 srv = __objt_check(smp->sess->origin)->server;
2880 if (!srv)
vkill1dfd1652019-10-30 16:58:14 +08002881 return 0;
2882
Christopher Fauletd1b44642020-04-30 09:51:15 +02002883 smp->data.u.str.area = srv->id;
vkill1dfd1652019-10-30 16:58:14 +08002884 if (!smp->data.u.str.area)
2885 return 0;
2886
2887 smp->data.type = SMP_T_STR;
2888 smp->data.u.str.data = strlen(smp->data.u.str.area);
2889
2890 return 1;
2891}
2892
Willy Tarreau34db1082012-04-19 17:16:54 +02002893/* set temp integer to the number of connections per second reaching the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002894 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02002895 * undefined behaviour.
2896 */
Willy Tarreau079ff0a2009-03-05 21:34:28 +01002897static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002898smp_fetch_be_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau079ff0a2009-03-05 21:34:28 +01002899{
Christopher Faulet37a9e212021-10-12 18:48:05 +02002900 struct proxy *px = args->data.prx;
2901
2902 if (px == NULL)
2903 return 0;
2904 if (px->cap & PR_CAP_DEF)
2905 px = smp->px;
2906
Willy Tarreau37406352012-04-23 16:16:37 +02002907 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002908 smp->data.type = SMP_T_SINT;
Christopher Faulet37a9e212021-10-12 18:48:05 +02002909 smp->data.u.sint = read_freq_ctr(&px->be_sess_per_sec);
Willy Tarreau079ff0a2009-03-05 21:34:28 +01002910 return 1;
2911}
2912
Willy Tarreau34db1082012-04-19 17:16:54 +02002913/* set temp integer to the number of concurrent connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002914 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02002915 * undefined behaviour.
2916 */
Willy Tarreaua36af912009-10-10 12:02:45 +02002917static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002918smp_fetch_be_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02002919{
Christopher Faulet37a9e212021-10-12 18:48:05 +02002920 struct proxy *px = args->data.prx;
2921
2922 if (px == NULL)
2923 return 0;
2924 if (px->cap & PR_CAP_DEF)
2925 px = smp->px;
2926
Willy Tarreau37406352012-04-23 16:16:37 +02002927 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002928 smp->data.type = SMP_T_SINT;
Christopher Faulet37a9e212021-10-12 18:48:05 +02002929 smp->data.u.sint = px->beconn;
Willy Tarreaua36af912009-10-10 12:02:45 +02002930 return 1;
2931}
2932
Patrick Hemmer4cdf3ab2018-06-14 17:10:27 -04002933/* set temp integer to the number of available connections across available
2934 * servers on the backend.
2935 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
2936 * undefined behaviour.
2937 */
2938static int
2939smp_fetch_be_conn_free(const struct arg *args, struct sample *smp, const char *kw, void *private)
2940{
2941 struct server *iterator;
Christopher Faulet37a9e212021-10-12 18:48:05 +02002942 struct proxy *px = args->data.prx;
Patrick Hemmer4cdf3ab2018-06-14 17:10:27 -04002943 unsigned int maxconn;
2944
Christopher Faulet37a9e212021-10-12 18:48:05 +02002945 if (px == NULL)
2946 return 0;
2947 if (px->cap & PR_CAP_DEF)
2948 px = smp->px;
2949
Patrick Hemmer4cdf3ab2018-06-14 17:10:27 -04002950 smp->flags = SMP_F_VOL_TEST;
2951 smp->data.type = SMP_T_SINT;
2952 smp->data.u.sint = 0;
2953
Christopher Faulet37a9e212021-10-12 18:48:05 +02002954 for (iterator = px->srv; iterator; iterator = iterator->next) {
Patrick Hemmer4cdf3ab2018-06-14 17:10:27 -04002955 if (iterator->cur_state == SRV_ST_STOPPED)
2956 continue;
2957
2958 px = iterator->proxy;
2959 if (!srv_currently_usable(iterator) ||
2960 ((iterator->flags & SRV_F_BACKUP) &&
2961 (px->srv_act || (iterator != px->lbprm.fbck && !(px->options & PR_O_USE_ALL_BK)))))
2962 continue;
2963
2964 if (iterator->maxconn == 0) {
2965 /* one active server is unlimited, return -1 */
2966 smp->data.u.sint = -1;
2967 return 1;
2968 }
2969
2970 maxconn = srv_dynamic_maxconn(iterator);
2971 if (maxconn > iterator->cur_sess)
2972 smp->data.u.sint += maxconn - iterator->cur_sess;
2973 }
2974
2975 return 1;
2976}
2977
Willy Tarreau34db1082012-04-19 17:16:54 +02002978/* set temp integer to the total number of queued connections on the backend.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002979 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02002980 * undefined behaviour.
2981 */
Willy Tarreaua36af912009-10-10 12:02:45 +02002982static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002983smp_fetch_queue_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02002984{
Christopher Faulet37a9e212021-10-12 18:48:05 +02002985 struct proxy *px = args->data.prx;
2986
2987 if (px == NULL)
2988 return 0;
2989 if (px->cap & PR_CAP_DEF)
2990 px = smp->px;
2991
Willy Tarreau37406352012-04-23 16:16:37 +02002992 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002993 smp->data.type = SMP_T_SINT;
Christopher Faulet37a9e212021-10-12 18:48:05 +02002994 smp->data.u.sint = px->totpend;
Willy Tarreaua36af912009-10-10 12:02:45 +02002995 return 1;
2996}
2997
Willy Tarreaua5e37562011-12-16 17:06:15 +01002998/* set temp integer to the total number of queued connections on the backend divided
Willy Tarreaua36af912009-10-10 12:02:45 +02002999 * by the number of running servers and rounded up. If there is no running
3000 * server, we return twice the total, just as if we had half a running server.
3001 * This is more or less correct anyway, since we expect the last server to come
3002 * back soon.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003003 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
Willy Tarreau34db1082012-04-19 17:16:54 +02003004 * undefined behaviour.
Willy Tarreaua36af912009-10-10 12:02:45 +02003005 */
3006static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003007smp_fetch_avg_queue_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua36af912009-10-10 12:02:45 +02003008{
Christopher Faulet37a9e212021-10-12 18:48:05 +02003009 struct proxy *px = args->data.prx;
Willy Tarreaua36af912009-10-10 12:02:45 +02003010 int nbsrv;
Christopher Faulet37a9e212021-10-12 18:48:05 +02003011
3012 if (px == NULL)
3013 return 0;
3014 if (px->cap & PR_CAP_DEF)
3015 px = smp->px;
Willy Tarreaua36af912009-10-10 12:02:45 +02003016
Willy Tarreau37406352012-04-23 16:16:37 +02003017 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003018 smp->data.type = SMP_T_SINT;
Willy Tarreaua36af912009-10-10 12:02:45 +02003019
Nenad Merdanovic2754fbc2017-03-12 21:56:56 +01003020 nbsrv = be_usable_srv(px);
Willy Tarreaua36af912009-10-10 12:02:45 +02003021
3022 if (nbsrv > 0)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003023 smp->data.u.sint = (px->totpend + nbsrv - 1) / nbsrv;
Willy Tarreaua36af912009-10-10 12:02:45 +02003024 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003025 smp->data.u.sint = px->totpend * 2;
Willy Tarreaua36af912009-10-10 12:02:45 +02003026
3027 return 1;
3028}
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01003029
Willy Tarreau34db1082012-04-19 17:16:54 +02003030/* set temp integer to the number of concurrent connections on the server in the backend.
3031 * Accepts exactly 1 argument. Argument is a server, other types will lead to
3032 * undefined behaviour.
3033 */
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02003034static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003035smp_fetch_srv_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02003036{
Willy Tarreauf853c462012-04-23 18:53:56 +02003037 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003038 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003039 smp->data.u.sint = args->data.srv->cur_sess;
Hervé COMMOWICKdaa824e2011-08-05 12:09:44 +02003040 return 1;
3041}
3042
Patrick Hemmer155e93e2018-06-14 18:01:35 -04003043/* set temp integer to the number of available connections on the server in the backend.
3044 * Accepts exactly 1 argument. Argument is a server, other types will lead to
3045 * undefined behaviour.
3046 */
3047static int
3048smp_fetch_srv_conn_free(const struct arg *args, struct sample *smp, const char *kw, void *private)
3049{
3050 unsigned int maxconn;
3051
3052 smp->flags = SMP_F_VOL_TEST;
3053 smp->data.type = SMP_T_SINT;
3054
3055 if (args->data.srv->maxconn == 0) {
3056 /* one active server is unlimited, return -1 */
3057 smp->data.u.sint = -1;
3058 return 1;
3059 }
3060
3061 maxconn = srv_dynamic_maxconn(args->data.srv);
3062 if (maxconn > args->data.srv->cur_sess)
3063 smp->data.u.sint = maxconn - args->data.srv->cur_sess;
3064 else
3065 smp->data.u.sint = 0;
3066
3067 return 1;
3068}
3069
Willy Tarreauff2b7af2017-10-13 11:46:26 +02003070/* set temp integer to the number of connections pending in the server's queue.
3071 * Accepts exactly 1 argument. Argument is a server, other types will lead to
3072 * undefined behaviour.
3073 */
3074static int
3075smp_fetch_srv_queue(const struct arg *args, struct sample *smp, const char *kw, void *private)
3076{
3077 smp->flags = SMP_F_VOL_TEST;
3078 smp->data.type = SMP_T_SINT;
Willy Tarreaua0570452021-06-18 09:30:30 +02003079 smp->data.u.sint = args->data.srv->queue.length;
Willy Tarreauff2b7af2017-10-13 11:46:26 +02003080 return 1;
3081}
3082
Tait Clarridge7896d522012-12-05 21:39:31 -05003083/* set temp integer to the number of enabled servers on the proxy.
3084 * Accepts exactly 1 argument. Argument is a server, other types will lead to
3085 * undefined behaviour.
3086 */
3087static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003088smp_fetch_srv_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Tait Clarridge7896d522012-12-05 21:39:31 -05003089{
3090 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003091 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003092 smp->data.u.sint = read_freq_ctr(&args->data.srv->sess_per_sec);
Tait Clarridge7896d522012-12-05 21:39:31 -05003093 return 1;
3094}
3095
Christopher Faulet1bea8652020-07-10 16:03:45 +02003096/* set temp integer to the server weight.
3097 * Accepts exactly 1 argument. Argument is a server, other types will lead to
3098 * undefined behaviour.
3099 */
3100static int
3101smp_fetch_srv_weight(const struct arg *args, struct sample *smp, const char *kw, void *private)
3102{
3103 struct server *srv = args->data.srv;
3104 struct proxy *px = srv->proxy;
3105
3106 smp->flags = SMP_F_VOL_TEST;
3107 smp->data.type = SMP_T_SINT;
3108 smp->data.u.sint = (srv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv;
3109 return 1;
3110}
3111
3112/* set temp integer to the server initial weight.
3113 * Accepts exactly 1 argument. Argument is a server, other types will lead to
3114 * undefined behaviour.
3115 */
3116static int
3117smp_fetch_srv_iweight(const struct arg *args, struct sample *smp, const char *kw, void *private)
3118{
3119 smp->flags = SMP_F_VOL_TEST;
3120 smp->data.type = SMP_T_SINT;
3121 smp->data.u.sint = args->data.srv->iweight;
3122 return 1;
3123}
3124
3125/* set temp integer to the server user-specified weight.
3126 * Accepts exactly 1 argument. Argument is a server, other types will lead to
3127 * undefined behaviour.
3128 */
3129static int
3130smp_fetch_srv_uweight(const struct arg *args, struct sample *smp, const char *kw, void *private)
3131{
3132 smp->flags = SMP_F_VOL_TEST;
3133 smp->data.type = SMP_T_SINT;
3134 smp->data.u.sint = args->data.srv->uweight;
3135 return 1;
3136}
3137
Amaury Denoyelled91d7792020-12-10 13:43:56 +01003138static int
3139smp_fetch_be_server_timeout(const struct arg *args, struct sample *smp, const char *km, void *private)
3140{
3141 struct proxy *px = NULL;
3142
3143 if (smp->strm)
3144 px = smp->strm->be;
3145 else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
3146 px = __objt_check(smp->sess->origin)->proxy;
3147 if (!px)
3148 return 0;
3149
3150 smp->flags = SMP_F_VOL_TXN;
3151 smp->data.type = SMP_T_SINT;
3152 smp->data.u.sint = TICKS_TO_MS(px->timeout.server);
3153 return 1;
3154}
3155
3156static int
3157smp_fetch_be_tunnel_timeout(const struct arg *args, struct sample *smp, const char *km, void *private)
3158{
3159 struct proxy *px = NULL;
3160
3161 if (smp->strm)
3162 px = smp->strm->be;
3163 else if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
3164 px = __objt_check(smp->sess->origin)->proxy;
3165 if (!px)
3166 return 0;
3167
3168 smp->flags = SMP_F_VOL_TXN;
3169 smp->data.type = SMP_T_SINT;
3170 smp->data.u.sint = TICKS_TO_MS(px->timeout.tunnel);
3171 return 1;
3172}
3173
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01003174static int sample_conv_nbsrv(const struct arg *args, struct sample *smp, void *private)
3175{
3176
3177 struct proxy *px;
3178
3179 if (!smp_make_safe(smp))
3180 return 0;
3181
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003182 px = proxy_find_by_name(smp->data.u.str.area, PR_CAP_BE, 0);
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01003183 if (!px)
3184 return 0;
3185
3186 smp->data.type = SMP_T_SINT;
3187 smp->data.u.sint = be_usable_srv(px);
3188
3189 return 1;
3190}
3191
Nenad Merdanovic177adc92019-08-27 01:58:13 +02003192static int
3193sample_conv_srv_queue(const struct arg *args, struct sample *smp, void *private)
3194{
3195 struct proxy *px;
3196 struct server *srv;
3197 char *bksep;
3198
3199 if (!smp_make_safe(smp))
3200 return 0;
3201
3202 bksep = strchr(smp->data.u.str.area, '/');
3203
3204 if (bksep) {
3205 *bksep = '\0';
3206 px = proxy_find_by_name(smp->data.u.str.area, PR_CAP_BE, 0);
3207 if (!px)
3208 return 0;
3209 smp->data.u.str.area = bksep + 1;
3210 } else {
3211 if (!(smp->px->cap & PR_CAP_BE))
3212 return 0;
3213 px = smp->px;
3214 }
3215
3216 srv = server_find_by_name(px, smp->data.u.str.area);
3217 if (!srv)
3218 return 0;
3219
3220 smp->data.type = SMP_T_SINT;
Willy Tarreaua0570452021-06-18 09:30:30 +02003221 smp->data.u.sint = srv->queue.length;
Nenad Merdanovic177adc92019-08-27 01:58:13 +02003222 return 1;
3223}
Willy Tarreau1a7eca12013-01-07 22:38:03 +01003224
3225/* Note: must not be declared <const> as its list will be overwritten.
3226 * Please take care of keeping this list alphabetically sorted.
3227 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003228static struct sample_fetch_kw_list smp_kws = {ILH, {
Amaury Denoyelled91d7792020-12-10 13:43:56 +01003229 { "avg_queue", smp_fetch_avg_queue_size, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3230 { "be_conn", smp_fetch_be_conn, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3231 { "be_conn_free", smp_fetch_be_conn_free, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3232 { "be_id", smp_fetch_be_id, 0, NULL, SMP_T_SINT, SMP_USE_BKEND, },
3233 { "be_name", smp_fetch_be_name, 0, NULL, SMP_T_STR, SMP_USE_BKEND, },
3234 { "be_server_timeout", smp_fetch_be_server_timeout, 0, NULL, SMP_T_SINT, SMP_USE_BKEND, },
3235 { "be_sess_rate", smp_fetch_be_sess_rate, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3236 { "be_tunnel_timeout", smp_fetch_be_tunnel_timeout, 0, NULL, SMP_T_SINT, SMP_USE_BKEND, },
3237 { "connslots", smp_fetch_connslots, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3238 { "nbsrv", smp_fetch_nbsrv, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3239 { "queue", smp_fetch_queue_size, ARG1(1,BE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3240 { "srv_conn", smp_fetch_srv_conn, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3241 { "srv_conn_free", smp_fetch_srv_conn_free, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3242 { "srv_id", smp_fetch_srv_id, 0, NULL, SMP_T_SINT, SMP_USE_SERVR, },
3243 { "srv_is_up", smp_fetch_srv_is_up, ARG1(1,SRV), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3244 { "srv_name", smp_fetch_srv_name, 0, NULL, SMP_T_STR, SMP_USE_SERVR, },
3245 { "srv_queue", smp_fetch_srv_queue, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3246 { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3247 { "srv_weight", smp_fetch_srv_weight, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3248 { "srv_iweight", smp_fetch_srv_iweight, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3249 { "srv_uweight", smp_fetch_srv_uweight, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau1a7eca12013-01-07 22:38:03 +01003250 { /* END */ },
3251}};
3252
Willy Tarreau0108d902018-11-25 19:14:37 +01003253INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3254
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01003255/* Note: must not be declared <const> as its list will be overwritten */
3256static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Nenad Merdanovic177adc92019-08-27 01:58:13 +02003257 { "nbsrv", sample_conv_nbsrv, 0, NULL, SMP_T_STR, SMP_T_SINT },
3258 { "srv_queue", sample_conv_srv_queue, 0, NULL, SMP_T_STR, SMP_T_SINT },
Nenad Merdanovicb7e7c472017-03-12 21:56:55 +01003259 { /* END */ },
3260}};
3261
Willy Tarreau0108d902018-11-25 19:14:37 +01003262INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);
Willy Tarreau1a7eca12013-01-07 22:38:03 +01003263
Willy Tarreau61612d42012-04-19 18:42:05 +02003264/* Note: must not be declared <const> as its list will be overwritten.
3265 * Please take care of keeping this list alphabetically sorted.
3266 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003267static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau1a7eca12013-01-07 22:38:03 +01003268 { /* END */ },
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01003269}};
3270
Willy Tarreau0108d902018-11-25 19:14:37 +01003271INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
Willy Tarreaua9d3c1e2007-11-30 20:48:53 +01003272
Willy Tarreaubaaee002006-06-26 02:48:02 +02003273/*
3274 * Local variables:
3275 * c-indent-level: 8
3276 * c-basic-offset: 8
3277 * End:
3278 */